-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinput.c
174 lines (152 loc) · 4.15 KB
/
input.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
pidvbip - tvheadend client for the Raspberry Pi
(C) Dave Chapman 2012-2013
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 <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include "input.h"
#include "msgqueue.h"
#define KEY_RELEASE 0
#define KEY_PRESS 1
#define KEY_KEEPING_PRESSED 2
static int get_input_key(int fd)
{
struct input_event ev[64];
int i;
size_t rb = read(fd, ev, sizeof(ev));
if (rb < (int) sizeof(struct input_event)) {
fprintf(stderr,"Short read\n");
return -1;
}
for (i = 0; i < (int)(rb / sizeof(struct input_event));i++) {
if (ev[i].type == EV_KEY) {
if ((ev[i].value == KEY_PRESS) || (ev[i].value == KEY_KEEPING_PRESSED)) {
fprintf(stderr,"input code %d\n",ev[1].code);
switch(ev[1].code) {
case KEY_0:
case KEY_NUMERIC_0:
return '0';
case KEY_1:
case KEY_NUMERIC_1:
return '1';
case KEY_2:
case KEY_NUMERIC_2:
return '2';
case KEY_3:
case KEY_NUMERIC_3:
return '3';
case KEY_4:
case KEY_NUMERIC_4:
return '4';
case KEY_5:
case KEY_NUMERIC_5:
return '5';
case KEY_6:
case KEY_NUMERIC_6:
return '6';
case KEY_7:
case KEY_NUMERIC_7:
return '7';
case KEY_8:
case KEY_NUMERIC_8:
return '8';
case KEY_9:
case KEY_NUMERIC_9:
return '9';
case KEY_H:
return 'h';
case KEY_I:
case KEY_INFO:
case KEY_LEFTMETA:
return 'i';
case KEY_Q:
case KEY_RED:
return 'q';
case KEY_N:
case KEY_PAGEUP:
case KEY_CHANNELUP:
return 'n';
case KEY_P:
case KEY_PAGEDOWN:
case KEY_CHANNELDOWN:
return 'p';
case KEY_UP:
return 'u';
case KEY_DOWN:
return 'd';
case KEY_LEFT:
return 'l';
case KEY_RIGHT:
return 'r';
case KEY_O:
case KEY_STOPCD:
case KEY_TAPE:
return 'o';
case KEY_SCREEN:
case BTN_TRIGGER_HAPPY16:
return ' ';
default: break;
}
}
}
}
return -1;
}
static void input_thread(struct msgqueue_t* msgqueue)
{
int inputfd;
char inputname[256] = "Unknown";
char *inputdevice = "/dev/input/event0";
int c;
if ((inputfd = open(inputdevice, O_RDONLY)) >= 0) {
ioctl (inputfd, EVIOCGNAME (sizeof (inputname)), inputname);
fprintf(stderr,"Using %s - %s\n", inputdevice, inputname);
/* Disable auto-repeat (for now...) */
int ioctl_params[2] = { 0, 0 };
ioctl(inputfd,EVIOCSREP,ioctl_params);
}
while(1) {
struct timeval tv = { 0L, 100000L }; /* 100ms */
fd_set fds;
int maxfd = 0;
FD_ZERO(&fds);
FD_SET(0, &fds);
if (inputfd >= 0) {
FD_SET(inputfd,&fds);
maxfd = inputfd;
}
c = -1;
if (select(maxfd+1, &fds, NULL, NULL, &tv)==0) {
c = -1;
} else {
if (FD_ISSET(0,&fds)) {
c = getchar();
}
if ((inputfd >= 0) && (FD_ISSET(inputfd,&fds))) {
c = get_input_key(inputfd);
}
}
if (c != -1) {
msgqueue_add(msgqueue,c);
}
}
}
void input_init(struct msgqueue_t* msgqueue)
{
pthread_t thread;
pthread_create(&thread,NULL,(void * (*)(void *))input_thread,(void*)msgqueue);
}