From 413270724bdcb3cf635efbd3874f76b11781dce9 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Sun, 18 May 2008 19:09:16 -0400 Subject: [PATCH] Set active terminal into non-canonical mode We want to be able to gain access to key presses immediately, so we can pass them on to the splash plugin to display to the user in some form. The "pass to the splash plugin" part isn't implemented yet. --- src/ply-window.c | 52 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/ply-window.c b/src/ply-window.c index bc0bfec2..b0be5903 100644 --- a/src/ply-window.c +++ b/src/ply-window.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -47,6 +48,7 @@ struct _ply_window char *tty_name; int tty_fd; + ply_fd_watch_t *tty_fd_watch; ply_window_mode_t mode; }; @@ -66,6 +68,28 @@ ply_window_new (const char *tty_name) return window; } +static void +on_key_event (ply_window_t *window) +{ + char buffer[64] = ""; + + ply_read (window->tty_fd, buffer, sizeof (buffer) - 1); +} + +bool +ply_window_set_unbuffered_input (ply_window_t *window) +{ + struct termios term_attributes; + + tcgetattr (window->tty_fd, &term_attributes); + term_attributes.c_lflag &= ~ICANON; + + if (tcsetattr (window->tty_fd, TCSAFLUSH, &term_attributes) != 0) + return false; + + return true; +} + bool ply_window_open (ply_window_t *window) { @@ -78,9 +102,18 @@ ply_window_open (ply_window_t *window) if (window->tty_fd < 0) return false; + if (!ply_window_set_unbuffered_input (window)) + return false; + if (!ply_window_set_mode (window, PLY_WINDOW_MODE_TEXT)) return false; + if (window->loop != NULL) + window->tty_fd_watch = ply_event_loop_watch_fd (window->loop, window->tty_fd, + PLY_EVENT_LOOP_FD_STATUS_HAS_DATA, + (ply_event_handler_t) on_key_event, + NULL, window); + return true; } @@ -89,6 +122,12 @@ ply_window_close (ply_window_t *window) { ply_window_set_mode (window, PLY_WINDOW_MODE_TEXT); + if (window->tty_fd_watch != NULL) + { + ply_event_loop_stop_watching_fd (window->loop, window->tty_fd_watch); + window->tty_fd_watch = NULL; + } + close (window->tty_fd); window->tty_fd = -1; } @@ -134,11 +173,12 @@ ply_window_detach_from_event_loop (ply_window_t *window) { assert (window != NULL); window->loop = NULL; + window->tty_fd_watch = NULL; } void -ply_window_attach_to_event_loop (ply_window_t *window, - ply_event_loop_t *loop) +ply_window_attach_to_event_loop (ply_window_t *window, + ply_event_loop_t *loop) { assert (window != NULL); assert (loop != NULL); @@ -146,6 +186,12 @@ ply_window_attach_to_event_loop (ply_window_t *window, window->loop = loop; + if (window->tty_fd >= 0) + window->tty_fd_watch = ply_event_loop_watch_fd (window->loop, window->tty_fd, + PLY_EVENT_LOOP_FD_STATUS_HAS_DATA, + (ply_event_handler_t) on_key_event, + NULL, window); + ply_event_loop_watch_for_exit (loop, (ply_event_loop_exit_handler_t) ply_window_detach_from_event_loop, window); @@ -202,7 +248,7 @@ main (int argc, } ply_event_loop_watch_for_timeout (loop, - 1.0, + 15.0, (ply_event_loop_timeout_handler_t) on_timeout, window);