blob: f11b0d7b031a4a3f94deefad037c861f828cdfc7 [file] [log] [blame]
Uwe Hermann43e57472011-12-30 11:23:22 +01001/*
Uwe Hermann630293b2013-04-23 22:10:41 +02002 * This file is part of the sigrok-cli project.
Uwe Hermann43e57472011-12-30 11:23:22 +01003 *
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
Uwe Hermann20fb52e2013-11-19 11:48:06 +010020#include "sigrok-cli.h"
Uwe Hermann43e57472011-12-30 11:23:22 +010021#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>
Uwe Hermann43e57472011-12-30 11:23:22 +010030
31#ifdef _WIN32
Uwe Hermann1999ae02012-01-02 14:33:42 +010032static HANDLE stdin_handle;
33static DWORD stdin_mode;
Uwe Hermann43e57472011-12-30 11:23:22 +010034#else
Uwe Hermann1999ae02012-01-02 14:33:42 +010035static struct termios term_orig;
Uwe Hermann43e57472011-12-30 11:23:22 +010036#endif
37
Uwe Hermann68cdf172012-02-29 22:32:34 +010038static int received_anykey(int fd, int revents, void *cb_data)
Uwe Hermann43e57472011-12-30 11:23:22 +010039{
Uwe Hermann43e57472011-12-30 11:23:22 +010040 (void)fd;
41 (void)revents;
Uwe Hermann68cdf172012-02-29 22:32:34 +010042 (void)cb_data;
Uwe Hermann43e57472011-12-30 11:23:22 +010043
Bert Vermeulen89a9bb02013-04-30 00:07:16 +020044 sr_session_source_remove(STDIN_FILENO);
Uwe Hermann43e57472011-12-30 11:23:22 +010045 sr_session_stop();
46
47 return TRUE;
48}
49
50/* Turn off buffering on stdin. */
51void add_anykey(void)
52{
53#ifdef _WIN32
54 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
55
56 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
57 /* TODO: Error handling. */
58 }
59
60 SetConsoleMode(stdin_handle, 0);
61#else
62 struct termios term;
63
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
69
70 sr_session_source_add(STDIN_FILENO, G_IO_IN, -1, received_anykey, NULL);
71
Bert Vermeulen8170b8e2012-05-26 04:54:34 +020072 g_message("Press any key to stop acquisition.");
Uwe Hermann43e57472011-12-30 11:23:22 +010073}
74
75/* Restore stdin attributes. */
76void clear_anykey(void)
77{
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
84}