From 9867f218307241961f9e7220085a9e3c23cab0af Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Thu, 26 Nov 2020 11:54:43 +0100 Subject: terminal: process input when foregrounded When mpv is in the background because it was started with `mpv foo.mp3 &`, or the user did ctrl+z bg, and is then brought to the foreground with fg, it buffers input until you press enter. This makes it accept input almost immediately. Having a short interval isn't important, since input is buffered until the next loop iteration. Closes #8120. --- osdep/terminal-unix.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/osdep/terminal-unix.c b/osdep/terminal-unix.c index 78eb4c4618..86c3030cdd 100644 --- a/osdep/terminal-unix.c +++ b/osdep/terminal-unix.c @@ -45,6 +45,14 @@ // Timeout in ms after which the (normally ambiguous) ESC key is detected. #define ESC_TIMEOUT 100 +// Timeout in ms after which the poll for input is aborted. The FG/BG state is +// tested before every wait, and a positive value allows reactivating input +// processing when mpv is brought to the foreground while it was running in the +// background. In such a situation, an infinite timeout (-1) will keep mpv +// waiting for input without realizing the terminal state changed - and thus +// buffer all keypresses until ENTER is pressed. +#define INPUT_TIMEOUT 1000 + static volatile struct termios tio_orig; static volatile int tio_orig_set; @@ -397,7 +405,7 @@ static void *terminal_thread(void *ptr) { .events = POLLIN, .fd = death_pipe[0] }, { .events = POLLIN, .fd = tty_in } }; - int r = polldev(fds, stdin_ok ? 2 : 1, buf.len ? ESC_TIMEOUT : -1); + int r = polldev(fds, stdin_ok ? 2 : 1, buf.len ? ESC_TIMEOUT : INPUT_TIMEOUT); if (fds[0].revents) break; if (fds[1].revents) { -- cgit v1.2.3