forked from g4klx/MMDVMCal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.cpp
129 lines (100 loc) · 2.22 KB
/
Console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Console.h"
#include <cstdio>
#if defined(_WIN32) || defined(_WIN64)
#include <conio.h>
CConsole::CConsole()
{
}
CConsole::~CConsole()
{
}
bool CConsole::open()
{
return true;
}
int CConsole::getChar()
{
if (::_kbhit() == 0)
return -1;
return ::_getch();
}
void CConsole::close()
{
}
#else
#include <cstring>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
CConsole::CConsole() :
m_termios()
{
::memset(&m_termios, 0x00U, sizeof(termios));
}
CConsole::~CConsole()
{
}
bool CConsole::open()
{
termios tios;
int n = ::tcgetattr(STDIN_FILENO, &tios);
if (n != 0) {
::fprintf(stderr, "tcgetattr: returned %d\r\n", n);
return -1;
}
m_termios = tios;
::cfmakeraw(&tios);
n = ::tcsetattr(STDIN_FILENO, TCSANOW, &tios);
if (n != 0) {
::fprintf(stderr, "tcsetattr: returned %d\r\n", n);
return -1;
}
return true;
}
int CConsole::getChar()
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
int n = ::select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
if (n <= 0) {
if (n < 0)
::fprintf(stderr, "select: returned %d\r\n", n);
return -1;
}
char c;
n = ::read(STDIN_FILENO, &c, 1);
if (n <= 0) {
if (n < 0)
::fprintf(stderr, "read: returned %d\r\n", n);
return -1;
}
return c;
}
void CConsole::close()
{
int n = ::tcsetattr(STDIN_FILENO, TCSANOW, &m_termios);
if (n != 0)
::fprintf(stderr, "tcsetattr: returned %d\r\n", n);
}
#endif