Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 1 | /* |
Uwe Hermann | 630293b | 2013-04-23 22:10:41 +0200 | [diff] [blame] | 2 | * This file is part of the sigrok-cli project. |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | 20fb52e | 2013-11-19 11:48:06 +0100 | [diff] [blame] | 20 | #include "sigrok-cli.h" |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 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> |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 30 | |
| 31 | #ifdef _WIN32 |
Uwe Hermann | 1999ae0 | 2012-01-02 14:33:42 +0100 | [diff] [blame] | 32 | static HANDLE stdin_handle; |
| 33 | static DWORD stdin_mode; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 34 | #else |
Uwe Hermann | 1999ae0 | 2012-01-02 14:33:42 +0100 | [diff] [blame] | 35 | static struct termios term_orig; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 36 | #endif |
| 37 | |
Uwe Hermann | 68cdf17 | 2012-02-29 22:32:34 +0100 | [diff] [blame] | 38 | static int received_anykey(int fd, int revents, void *cb_data) |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 39 | { |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 40 | struct sr_session *session; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 41 | (void)fd; |
| 42 | (void)revents; |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 43 | |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 44 | session = cb_data; |
| 45 | sr_session_source_remove(session, STDIN_FILENO); |
| 46 | sr_session_stop(session); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 47 | |
| 48 | return TRUE; |
| 49 | } |
| 50 | |
| 51 | /* Turn off buffering on stdin. */ |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 52 | void add_anykey(struct sr_session *session) |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 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 | |
Bert Vermeulen | 4bf77ec | 2014-07-17 22:49:20 +0200 | [diff] [blame] | 71 | sr_session_source_add(session, STDIN_FILENO, G_IO_IN, -1, |
| 72 | received_anykey, session); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 73 | |
Bert Vermeulen | 8170b8e | 2012-05-26 04:54:34 +0200 | [diff] [blame] | 74 | g_message("Press any key to stop acquisition."); |
Uwe Hermann | 43e5747 | 2011-12-30 11:23:22 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Restore stdin attributes. */ |
| 78 | void clear_anykey(void) |
| 79 | { |
| 80 | #ifdef _WIN32 |
| 81 | SetConsoleMode(stdin_handle, stdin_mode); |
| 82 | #else |
| 83 | tcflush(STDIN_FILENO, TCIFLUSH); |
| 84 | tcsetattr(STDIN_FILENO, TCSANOW, &term_orig); |
| 85 | #endif |
| 86 | } |