From fed2c87f67de43e3672d1a4a7767d59e7ff22ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 10 Sep 2024 11:02:09 +0800 Subject: [PATCH] TerminalTheme (macOS): fix hanging on GNU screen 5.0 --- src/common/io/io_unix.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index 62c25dc59..c63956df9 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -4,9 +4,13 @@ #include #include -#include #include #include +#ifndef __APPLE__ +#include +#else +#include +#endif #if FF_HAVE_WORDEXP #include @@ -161,8 +165,20 @@ const char* ffGetTerminalResponse(const char* request, const char* format, ...) ffWriteFDData(ftty, strlen(request), request); //Give the terminal some time to respond + #ifndef __APPLE__ if(poll(&(struct pollfd) { .fd = ftty, .events = POLLIN }, 1, FF_IO_TERM_RESP_WAIT_MS) <= 0) - return "poll() timeout or failed"; + return "poll(/dev/tty) timeout or failed"; + #else + { + // On macOS, poll(/dev/tty) always returns immediately + // See also https://nathancraddock.com/blog/macos-dev-tty-polling/ + fd_set rd; + FD_ZERO(&rd); + FD_SET(ftty, &rd); + if(select(ftty + 1, &rd, NULL, NULL, &(struct timeval) { .tv_sec = FF_IO_TERM_RESP_WAIT_MS / 1000, .tv_usec = (FF_IO_TERM_RESP_WAIT_MS % 1000) * 1000 }) <= 0) + return "select(/dev/tty) timeout or failed"; + } + #endif char buffer[512]; ssize_t bytesRead = read(ftty, buffer, sizeof(buffer) - 1);