forked from popcornmix/omxplayer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyboard.cpp
132 lines (112 loc) · 2.91 KB
/
Keyboard.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
130
131
132
/*
*
* Copyright (C) 2022 Michael Walsh
* Copyright (C) 2012 Edgar Hucek
*
* 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 <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "utils/log.h"
#include "Keyboard.h"
#include "KeyConfig.h"
Keyboard::Keyboard(const char *filename)
{
// setKeymap
KeyConfig::buildKeymap(filename, m_keymap);
if (isatty(STDIN_FILENO))
{
struct termios new_termios;
tcgetattr(STDIN_FILENO, &orig_termios);
new_termios = orig_termios;
new_termios.c_lflag &= ~(ICANON | ECHO | ECHOCTL | ECHONL);
new_termios.c_cflag |= HUPCL;
new_termios.c_cc[VMIN] = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
orig_fl = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, FNDELAY);
}
else
{
orig_fl = fcntl(STDIN_FILENO, F_GETFL);
fcntl(STDIN_FILENO, F_SETFL, orig_fl | O_NONBLOCK);
}
Create();
}
Keyboard::~Keyboard()
{
m_bAbort = true;
if (ThreadHandle())
{
StopThread();
}
if (isatty(STDIN_FILENO))
{
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
fcntl(STDIN_FILENO, F_SETFL, orig_fl);
}
else
{
fcntl(STDIN_FILENO, F_SETFL, orig_fl);
}
}
void Keyboard::Sleep(unsigned int dwMilliSeconds)
{
struct timespec req;
req.tv_sec = dwMilliSeconds / 1000;
req.tv_nsec = (dwMilliSeconds % 1000) * 1000000;
while ( nanosleep(&req, &req) == -1 && errno == EINTR && (req.tv_nsec > 0 || req.tv_sec > 0));
}
void Keyboard::Process()
{
while(!m_bAbort)
{
int ch[8];
int chnum = 0;
while ((ch[chnum] = getchar()) != EOF) chnum++;
if(chnum > 0)
{
int result = 0;
if(chnum == 1)
{
result = ch[0];
}
else if(ch[0] == '\e')
{
if(chnum > 5) chnum = 5;
for(int i = 1; i < chnum; i++)
result = (result << 8) | ch[i];
}
else
{
if(chnum > 4) chnum = 4;
for(int i = 0; i < chnum; i++)
result = (result << 8) | ch[i];
}
CLogLog(LOGDEBUG, "Keyboard: character %c (0x%x)", result, result);
int action = m_keymap[result];
if(action != 0)
m_action = action;
}
Sleep(20);
}
}
enum Action Keyboard::getEvent()
{
return (enum Action)m_action.exchange(-1);
}