build.yml: Add missing runners
[sigrok-cli.git] / anykey.c
blobb1ac9ef430a80df681ee1442e99d8bbc6712fe11
1 /*
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/>.
20 #include <config.h>
21 #include <stdio.h>
22 #ifdef _WIN32
23 #include <windows.h>
24 #else
25 #include <termios.h>
26 #include <unistd.h>
27 #endif
28 #include <string.h>
29 #include <glib.h>
30 #include "sigrok-cli.h"
32 #ifdef _WIN32
33 static HANDLE stdin_handle;
34 static DWORD stdin_mode;
35 #else
36 static struct termios term_orig;
37 #endif
38 static unsigned int watch_id = 0;
40 static gboolean received_anykey(GIOChannel *source,
41 GIOCondition condition, void *data)
43 struct sr_session *session;
45 (void)source;
46 (void)condition;
47 session = data;
49 watch_id = 0;
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)
59 GIOChannel *channel;
61 #ifdef _WIN32
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);
70 #else
71 struct termios term;
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);
79 #endif
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)
93 if (watch_id != 0) {
94 g_source_remove(watch_id);
95 watch_id = 0;
97 #ifdef _WIN32
98 SetConsoleMode(stdin_handle, stdin_mode);
99 #else
100 tcflush(STDIN_FILENO, TCIFLUSH);
101 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
102 #endif