]> sigrok.org Git - sigrok-cli.git/blame_incremental - anykey.c
accept multiple --get requests for multiple channel groups
[sigrok-cli.git] / anykey.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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"
31
32#ifdef _WIN32
33static HANDLE stdin_handle;
34static DWORD stdin_mode;
35#else
36static struct termios term_orig;
37#endif
38static unsigned int watch_id = 0;
39
40static gboolean received_anykey(GIOChannel *source,
41 GIOCondition condition, void *data)
42{
43 struct sr_session *session;
44
45 (void)source;
46 (void)condition;
47 session = data;
48
49 watch_id = 0;
50 sr_session_stop(session);
51
52 return G_SOURCE_REMOVE;
53}
54
55/* Turn off buffering on stdin and watch for input.
56 */
57void add_anykey(struct sr_session *session)
58{
59 GIOChannel *channel;
60
61#ifdef _WIN32
62 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
63
64 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
65 /* TODO: Error handling. */
66 }
67 SetConsoleMode(stdin_handle, 0);
68
69 channel = g_io_channel_win32_new_fd(0);
70#else
71 struct termios term;
72
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);
77
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);
82
83 watch_id = g_io_add_watch(channel, G_IO_IN, &received_anykey, session);
84 g_io_channel_unref(channel);
85
86 g_message("Press any key to stop acquisition.");
87}
88
89/* Remove the event watch and restore stdin attributes.
90 */
91void clear_anykey(void)
92{
93 if (watch_id != 0) {
94 g_source_remove(watch_id);
95 watch_id = 0;
96 }
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
103}