#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <termios.h>
#include <unistd.h>
#include <linux/kd.h>
char *tty_name;
int tty_fd;
+ ply_fd_watch_t *tty_fd_watch;
ply_window_mode_t mode;
};
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)
{
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;
}
{
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;
}
{
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);
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);
}
ply_event_loop_watch_for_timeout (loop,
- 1.0,
+ 15.0,
(ply_event_loop_timeout_handler_t)
on_timeout,
window);