]> sigrok.org Git - sigrok-cli.git/blame_incremental - anykey.c
Build: Move _POSIX_C_SOURCE definition to config.h
[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#endif
27#include <unistd.h>
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
38
39static int received_anykey(int fd, int revents, void *cb_data)
40{
41 struct sr_session *session;
42
43 (void)fd;
44 (void)revents;
45
46 session = cb_data;
47 sr_session_source_remove(session, STDIN_FILENO);
48 sr_session_stop(session);
49
50 return TRUE;
51}
52
53/* Turn off buffering on stdin. */
54void add_anykey(struct sr_session *session)
55{
56#ifdef _WIN32
57 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
58
59 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
60 /* TODO: Error handling. */
61 }
62
63 SetConsoleMode(stdin_handle, 0);
64#else
65 struct termios term;
66
67 tcgetattr(STDIN_FILENO, &term);
68 memcpy(&term_orig, &term, sizeof(struct termios));
69 term.c_lflag &= ~(ECHO | ICANON | ISIG);
70 tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
71#endif
72
73 sr_session_source_add(session, STDIN_FILENO, G_IO_IN, -1,
74 received_anykey, session);
75
76 g_message("Press any key to stop acquisition.");
77}
78
79/* Restore stdin attributes. */
80void clear_anykey(void)
81{
82#ifdef _WIN32
83 SetConsoleMode(stdin_handle, stdin_mode);
84#else
85 tcflush(STDIN_FILENO, TCIFLUSH);
86 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
87#endif
88}