Makefile.am: Fix typo in 'ChangeLog' target.
[sigrok-cli.git] / anykey.c
blob1e006b52d3f4ba718d61cbff31b6fb4ac3b4c10b
1 /*
2 * This file is part of the sigrok 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 <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 <libsigrok/libsigrok.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
39 static int received_anykey(int fd, int revents, void *cb_data)
41 (void)fd;
42 (void)revents;
43 (void)cb_data;
45 sr_session_stop();
47 return TRUE;
50 /* Turn off buffering on stdin. */
51 void add_anykey(void)
53 #ifdef _WIN32
54 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
56 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
57 /* TODO: Error handling. */
60 SetConsoleMode(stdin_handle, 0);
61 #else
62 struct termios term;
64 tcgetattr(STDIN_FILENO, &term);
65 memcpy(&term_orig, &term, sizeof(struct termios));
66 term.c_lflag &= ~(ECHO | ICANON | ISIG);
67 tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
68 #endif
70 sr_session_source_add(STDIN_FILENO, G_IO_IN, -1, received_anykey, NULL);
72 g_message("Press any key to stop acquisition.");
75 /* Restore stdin attributes. */
76 void clear_anykey(void)
78 #ifdef _WIN32
79 SetConsoleMode(stdin_handle, stdin_mode);
80 #else
81 tcflush(STDIN_FILENO, TCIFLUSH);
82 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
83 #endif