]> sigrok.org Git - sigrok-cli.git/blame - anykey.c
Fix various clang warnings.
[sigrok-cli.git] / anykey.c
CommitLineData
43e5747a 1/*
630293b4 2 * This file is part of the sigrok-cli project.
43e5747a
UH
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
d486cbdd 20#include <config.h>
43e5747a
UH
21#include <stdio.h>
22#ifdef _WIN32
23#include <windows.h>
24#else
25#include <termios.h>
43e5747a 26#include <unistd.h>
fd65ec4c 27#endif
43e5747a
UH
28#include <string.h>
29#include <glib.h>
662a1e27 30#include "sigrok-cli.h"
43e5747a
UH
31
32#ifdef _WIN32
1999ae06
UH
33static HANDLE stdin_handle;
34static DWORD stdin_mode;
43e5747a 35#else
1999ae06 36static struct termios term_orig;
43e5747a 37#endif
fd65ec4c 38static unsigned int watch_id = 0;
43e5747a 39
fd65ec4c
DE
40static gboolean received_anykey(GIOChannel *source,
41 GIOCondition condition, void *data)
43e5747a 42{
4bf77ec6 43 struct sr_session *session;
f0f54487 44
fd65ec4c
DE
45 (void)source;
46 (void)condition;
47 session = data;
43e5747a 48
fd65ec4c 49 watch_id = 0;
4bf77ec6 50 sr_session_stop(session);
43e5747a 51
fd65ec4c 52 return G_SOURCE_REMOVE;
43e5747a
UH
53}
54
fd65ec4c
DE
55/* Turn off buffering on stdin and watch for input.
56 */
4bf77ec6 57void add_anykey(struct sr_session *session)
43e5747a 58{
fd65ec4c
DE
59 GIOChannel *channel;
60
43e5747a
UH
61#ifdef _WIN32
62 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
63
64 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
65 /* TODO: Error handling. */
66 }
43e5747a 67 SetConsoleMode(stdin_handle, 0);
fd65ec4c
DE
68
69 channel = g_io_channel_win32_new_fd(0);
43e5747a
UH
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);
fd65ec4c
DE
77
78 channel = g_io_channel_unix_new(STDIN_FILENO);
43e5747a 79#endif
fd65ec4c
DE
80 g_io_channel_set_encoding(channel, NULL, NULL);
81 g_io_channel_set_buffered(channel, FALSE);
43e5747a 82
fd65ec4c
DE
83 watch_id = g_io_add_watch(channel, G_IO_IN, &received_anykey, session);
84 g_io_channel_unref(channel);
43e5747a 85
8170b8ea 86 g_message("Press any key to stop acquisition.");
43e5747a
UH
87}
88
fd65ec4c
DE
89/* Remove the event watch and restore stdin attributes.
90 */
43e5747a
UH
91void clear_anykey(void)
92{
fd65ec4c
DE
93 if (watch_id != 0) {
94 g_source_remove(watch_id);
95 watch_id = 0;
96 }
43e5747a
UH
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}