]> sigrok.org Git - sigrok-cli.git/blame - anykey.c
Don't open an output file in the case of srzip.
[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
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>
662a1e27 29#include "sigrok-cli.h"
43e5747a
UH
30
31#ifdef _WIN32
1999ae06
UH
32static HANDLE stdin_handle;
33static DWORD stdin_mode;
43e5747a 34#else
1999ae06 35static struct termios term_orig;
43e5747a
UH
36#endif
37
68cdf174 38static int received_anykey(int fd, int revents, void *cb_data)
43e5747a 39{
4bf77ec6 40 struct sr_session *session;
f0f54487 41
43e5747a
UH
42 (void)fd;
43 (void)revents;
43e5747a 44
4bf77ec6
BV
45 session = cb_data;
46 sr_session_source_remove(session, STDIN_FILENO);
47 sr_session_stop(session);
43e5747a
UH
48
49 return TRUE;
50}
51
52/* Turn off buffering on stdin. */
4bf77ec6 53void add_anykey(struct sr_session *session)
43e5747a
UH
54{
55#ifdef _WIN32
56 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
57
58 if (!GetConsoleMode(stdin_handle, &stdin_mode)) {
59 /* TODO: Error handling. */
60 }
61
62 SetConsoleMode(stdin_handle, 0);
63#else
64 struct termios term;
65
66 tcgetattr(STDIN_FILENO, &term);
67 memcpy(&term_orig, &term, sizeof(struct termios));
68 term.c_lflag &= ~(ECHO | ICANON | ISIG);
69 tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
70#endif
71
4bf77ec6
BV
72 sr_session_source_add(session, STDIN_FILENO, G_IO_IN, -1,
73 received_anykey, session);
43e5747a 74
8170b8ea 75 g_message("Press any key to stop acquisition.");
43e5747a
UH
76}
77
78/* Restore stdin attributes. */
79void clear_anykey(void)
80{
81#ifdef _WIN32
82 SetConsoleMode(stdin_handle, stdin_mode);
83#else
84 tcflush(STDIN_FILENO, TCIFLUSH);
85 tcsetattr(STDIN_FILENO, TCSANOW, &term_orig);
86#endif
87}