forked from koreader/kindlepdfviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
238 lines (213 loc) · 5.63 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
KindlePDFViewer: input abstraction for Lua
Copyright (C) 2011 Hans-Werner Hilse <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include "input.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define OUTPUT_SIZE 21
#define CODE_IN_SAVER 10000
#define CODE_OUT_SAVER 10001
#define NUM_FDS 4
int inputfds[4] = { -1, -1, -1, -1 };
int slider_pid = -1;
int findFreeFdSlot() {
int i;
for(i=0; i<NUM_FDS; i++) {
if(inputfds[i] == -1) {
return i;
}
}
return -1;
}
static int openInputDevice(lua_State *L) {
#ifndef EMULATE_READER
int fd;
const char* inputdevice = luaL_checkstring(L, 1);
fd = findFreeFdSlot();
if(fd == -1) {
return luaL_error(L, "no free slot for new input device <%s>", inputdevice);
}
if(!strcmp("slider",inputdevice)) {
/* special case: the power slider */
int pipefd[2];
int childpid;
pipe(pipefd);
if((childpid = fork()) == -1) {
return luaL_error(L, "cannot fork() slider event listener");
}
if(childpid == 0) {
FILE *fp;
char std_out[OUTPUT_SIZE] = "";
struct input_event ev;
int ret;
__u16 key_code = 10000;
close(pipefd[0]);
ev.type = EV_KEY;
ev.code = key_code;
ev.value = 1;
/* listen power slider events */
while(1) {
fp = popen("exec lipc-wait-event com.lab126.powerd goingToScreenSaver,outOfScreenSaver", "r");
if(fgets(std_out, OUTPUT_SIZE, fp) == NULL) {
break;
}
pclose(fp);
if(std_out[0] == 'g') {
ev.code = CODE_IN_SAVER;
} else if(std_out[0] == 'o') {
ev.code = CODE_OUT_SAVER;
} else {
printf("Unrecognized event.\n");
}
/* fill event struct */
gettimeofday(&ev.time, NULL);
/* generate event */
if(write(pipefd[1], &ev, sizeof(struct input_event)) == -1) {
break;
}
}
exit(0); /* cannot be reached?! */
} else {
close(pipefd[1]);
inputfds[fd] = pipefd[0];
slider_pid = childpid;
}
} else {
inputfds[fd] = open(inputdevice, O_RDONLY | O_NONBLOCK, 0);
if(inputfds[fd] != -1) {
ioctl(inputfds[fd], EVIOCGRAB, 1);
return 0;
} else {
return luaL_error(L, "error opening input device <%s>: %d", inputdevice, errno);
}
}
#else
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return luaL_error(L, "cannot initialize SDL.");
}
return 0;
#endif
}
static int closeInputDevices(lua_State *L) {
int i;
for(i=0; i<NUM_FDS; i++) {
if(inputfds[i] != -1) {
ioctl(inputfds[i], EVIOCGRAB, 0);
close(i);
}
}
if(slider_pid != -1) {
/* kill and wait for child process */
kill(slider_pid, SIGTERM);
waitpid(-1, NULL, 0);
}
return 0;
}
static int waitForInput(lua_State *L) {
#ifndef EMULATE_READER
fd_set fds;
struct timeval timeout;
int i, num, nfds;
int usecs = luaL_optint(L, 1, -1); // we check for <0 later
timeout.tv_sec = (usecs/1000000);
timeout.tv_usec = (usecs%1000000);
nfds = 0;
FD_ZERO(&fds);
for(i=0; i<NUM_FDS; i++) {
if(inputfds[i] != -1)
FD_SET(inputfds[i], &fds);
if(inputfds[i] + 1 > nfds)
nfds = inputfds[i] + 1;
}
/* when no value is given as argument, we pass
* NULL to select() for the timeout value, setting no
* timeout at all.
*/
num = select(nfds, &fds, NULL, NULL, (usecs < 0) ? NULL : &timeout);
if(num < 0) {
return luaL_error(L, "Waiting for input failed: %d\n", errno);
}
for(i=0; i<NUM_FDS; i++) {
if(inputfds[i] != -1 && FD_ISSET(inputfds[i], &fds)) {
struct input_event input;
int n;
n = read(inputfds[i], &input, sizeof(struct input_event));
if(n == sizeof(struct input_event)) {
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushinteger(L, (int) input.type);
lua_settable(L, -3);
lua_pushstring(L, "code");
lua_pushinteger(L, (int) input.code);
lua_settable(L, -3);
lua_pushstring(L, "value");
lua_pushinteger(L, (int) input.value);
lua_settable(L, -3);
return 1;
}
}
}
return 0;
#else
SDL_Event event;
while(SDL_WaitEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushinteger(L, EV_KEY);
lua_settable(L, -3);
lua_pushstring(L, "code");
lua_pushinteger(L, event.key.keysym.scancode);
lua_settable(L, -3);
lua_pushstring(L, "value");
lua_pushinteger(L, 1);
lua_settable(L, -3);
return 1;
case SDL_KEYUP:
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushinteger(L, EV_KEY);
lua_settable(L, -3);
lua_pushstring(L, "code");
lua_pushinteger(L, event.key.keysym.scancode);
lua_settable(L, -3);
lua_pushstring(L, "value");
lua_pushinteger(L, 0);
lua_settable(L, -3);
return 1;
case SDL_QUIT:
return luaL_error(L, "application forced to quit");
}
}
return luaL_error(L, "error waiting for SDL event.");
#endif
}
static const struct luaL_Reg input_func[] = {
{"open", openInputDevice},
{"closeAll", closeInputDevices},
{"waitForEvent", waitForInput},
{NULL, NULL}
};
int luaopen_input(lua_State *L) {
luaL_register(L, "input", input_func);
return 1;
}