2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include "sigrok-cli.h"
33 static HANDLE stdin_handle;
34 static DWORD stdin_mode;
36 static struct termios term_orig;
38 static unsigned int watch_id = 0;
40 static gboolean received_anykey(GIOChannel *source,
41 GIOCondition condition, void *data)
43 struct sr_session *session;
50 sr_session_stop(session);
52 return G_SOURCE_REMOVE;
55 /* Turn off buffering on stdin and watch for input.
57 void add_anykey(struct sr_session *session)
62 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
64 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
65 /* TODO: Error handling. */
67 SetConsoleMode(stdin_handle, 0);
69 channel = g_io_channel_win32_new_fd(0);
73 tcgetattr(STDIN_FILENO, &term);
74 memcpy(&term_orig, &term, sizeof(struct termios));
75 term.c_lflag &= ~(ECHO | ICANON | ISIG);
76 tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
78 channel = g_io_channel_unix_new(STDIN_FILENO);
80 g_io_channel_set_encoding(channel, NULL, NULL);
81 g_io_channel_set_buffered(channel, FALSE);
83 watch_id = g_io_add_watch(channel, G_IO_IN, &received_anykey, session);
84 g_io_channel_unref(channel);
86 g_message("Press any key to stop acquisition.");
89 /* Remove the event watch and restore stdin attributes.
91 void clear_anykey(void)
94 g_source_remove(watch_id);
98 SetConsoleMode(stdin_handle, stdin_mode);
100 tcflush(STDIN_FILENO, TCIFLUSH);
101 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);