X-Git-Url: https://sigrok.org/gitweb/?p=sigrok-cli.git;a=blobdiff_plain;f=anykey.c;h=b1ac9ef430a80df681ee1442e99d8bbc6712fe11;hp=be16ff11a37fa6895a4385d93665f890f2cb8b4e;hb=6bb3c3dd27c0477705a5c0684a8c3fd506a35f48;hpb=43e5747a59ed92243c217f7e36da2ac35bbcd80d diff --git a/anykey.c b/anykey.c index be16ff1..b1ac9ef 100644 --- a/anykey.c +++ b/anykey.c @@ -1,5 +1,5 @@ /* - * This file is part of the sigrok project. + * This file is part of the sigrok-cli project. * * Copyright (C) 2011 Bert Vermeulen * @@ -17,48 +17,56 @@ * along with this program. If not, see . */ +#include #include #ifdef _WIN32 #include #else #include -#endif #include +#endif #include #include -#include #include "sigrok-cli.h" #ifdef _WIN32 -HANDLE stdin_handle; -DWORD stdin_mode; +static HANDLE stdin_handle; +static DWORD stdin_mode; #else -struct termios term_orig; +static struct termios term_orig; #endif +static unsigned int watch_id = 0; -static int received_anykey(int fd, int revents, void *user_data) +static gboolean received_anykey(GIOChannel *source, + GIOCondition condition, void *data) { - /* Avoid compiler warnings. */ - (void)fd; - (void)revents; - (void)user_data; + struct sr_session *session; + + (void)source; + (void)condition; + session = data; - sr_session_stop(); + watch_id = 0; + sr_session_stop(session); - return TRUE; + return G_SOURCE_REMOVE; } -/* Turn off buffering on stdin. */ -void add_anykey(void) +/* Turn off buffering on stdin and watch for input. + */ +void add_anykey(struct sr_session *session) { + GIOChannel *channel; + #ifdef _WIN32 stdin_handle = GetStdHandle(STD_INPUT_HANDLE); if (!GetConsoleMode(stdin_handle, &stdin_mode)) { /* TODO: Error handling. */ } - SetConsoleMode(stdin_handle, 0); + + channel = g_io_channel_win32_new_fd(0); #else struct termios term; @@ -66,16 +74,26 @@ void add_anykey(void) memcpy(&term_orig, &term, sizeof(struct termios)); term.c_lflag &= ~(ECHO | ICANON | ISIG); tcsetattr(STDIN_FILENO, TCSADRAIN, &term); + + channel = g_io_channel_unix_new(STDIN_FILENO); #endif + g_io_channel_set_encoding(channel, NULL, NULL); + g_io_channel_set_buffered(channel, FALSE); - sr_session_source_add(STDIN_FILENO, G_IO_IN, -1, received_anykey, NULL); + watch_id = g_io_add_watch(channel, G_IO_IN, &received_anykey, session); + g_io_channel_unref(channel); - printf("Press any key to stop acquisition.\n"); + g_message("Press any key to stop acquisition."); } -/* Restore stdin attributes. */ +/* Remove the event watch and restore stdin attributes. + */ void clear_anykey(void) { + if (watch_id != 0) { + g_source_remove(watch_id); + watch_id = 0; + } #ifdef _WIN32 SetConsoleMode(stdin_handle, stdin_mode); #else