]> sigrok.org Git - sigrok-cli.git/blame - anykey.c
cli: use , as a separator for PD stacks, like all other options.
[sigrok-cli.git] / anykey.c
CommitLineData
43e5747a
UH
1/*
2 * This file is part of the sigrok 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 <stdio.h>
21#ifdef _WIN32
22#include <windows.h>
23#else
24#include <termios.h>
25#endif
26#include <unistd.h>
27#include <string.h>
28#include <glib.h>
29#include <sigrok.h>
30#include "sigrok-cli.h"
31
32#ifdef _WIN32
1999ae06
UH
33static HANDLE stdin_handle;
34static DWORD stdin_mode;
43e5747a 35#else
1999ae06 36static struct termios term_orig;
43e5747a
UH
37#endif
38
39static int received_anykey(int fd, int revents, void *user_data)
40{
41 /* Avoid compiler warnings. */
42 (void)fd;
43 (void)revents;
44 (void)user_data;
45
46 sr_session_stop();
47
48 return TRUE;
49}
50
51/* Turn off buffering on stdin. */
52void add_anykey(void)
53{
54#ifdef _WIN32
55 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
56
57 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
58 /* TODO: Error handling. */
59 }
60
61 SetConsoleMode(stdin_handle, 0);
62#else
63 struct termios term;
64
65 tcgetattr(STDIN_FILENO, &term);
66 memcpy(&term_orig, &term, sizeof(struct termios));
67 term.c_lflag &= ~(ECHO | ICANON | ISIG);
68 tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
69#endif
70
71 sr_session_source_add(STDIN_FILENO, G_IO_IN, -1, received_anykey, NULL);
72
73 printf("Press any key to stop acquisition.\n");
74}
75
76/* Restore stdin attributes. */
77void clear_anykey(void)
78{
79#ifdef _WIN32
80 SetConsoleMode(stdin_handle, stdin_mode);
81#else
82 tcflush(STDIN_FILENO, TCIFLUSH);
83 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
84#endif
85}