switched slider handling to a fork()ed process

pull/2/merge
HW 12 years ago
parent 4e542f7d4f
commit 877ecdfb71

@ -19,29 +19,95 @@
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <linux/input.h>
#include "input.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 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 i;
int fd;
const char* inputdevice = luaL_checkstring(L, 1);
for(i=0; i<NUM_FDS; i++) {
if(inputfds[i] == -1) {
inputfds[i] = open(inputdevice, O_RDONLY | O_NONBLOCK, 0);
if(inputfds[i] != -1) {
ioctl(inputfds[i], EVIOCGRAB, 1);
return 0;
} else {
return luaL_error(L, "error opening input device <%s>: %d", inputdevice, errno);
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];
}
} 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);
}
}
return luaL_error(L, "no free slot for new input device <%s>", inputdevice);
#else
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return luaL_error(L, "cannot initialize SDL.");
@ -66,7 +132,7 @@ static int waitForInput(lua_State *L) {
fd_set fds;
struct timeval timeout;
int i, num, nfds;
int usecs = luaL_optint(L, 1, 0x7FFFFFFF);
int usecs = luaL_optint(L, 1, -1); // we check for <0 later
timeout.tv_sec = (usecs/1000000);
timeout.tv_usec = (usecs%1000000);
@ -81,7 +147,11 @@ static int waitForInput(lua_State *L) {
nfds = inputfds[i] + 1;
}
num = select(nfds, &fds, NULL, NULL, &timeout);
/* 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);
}

@ -1,19 +1,11 @@
#!/bin/sh
SLIDER_EVENT_PIPE="/tmp/event_slider"
export LC_ALL="en_US.UTF-8"
echo unlock > /proc/keypad
echo unlock > /proc/fiveway
cd /mnt/us/kindlepdfviewer/
# create the named pipe for power slider event
if [ ! -p $SLIDER_EVENT_PIPE ]; then
mkfifo $SLIDER_EVENT_PIPE
fi
killall slider_watcher
./slider_watcher $SLIDER_EVENT_PIPE &
cd /mnt/us/kindlepdfviewer/
./reader.lua "$1" 2> /mnt/us/kindlepdfviewer/crash.log
./reader.lua $1 2> /mnt/us/kindlepdfviewer/crash.log
killall -cont cvm
killall slider_watcher
echo 1 > /proc/eink_fb/update_display

@ -69,9 +69,6 @@ function showusage()
print("-g, --goto=page start reading on page")
print("-G, --gamma=GAMMA set gamma correction")
print(" (floating point notation, e.g. \"1.5\")")
print("-d, --device=DEVICE set device specific configuration,")
print(" currently one of \"kdxg\" (default), \"k3\"")
print(" \"emu\" (DXG emulation)")
print("-h, --help show this usage help")
print("")
print("If you give the name of a directory instead of a file path, a file")
@ -89,22 +86,14 @@ if optarg["h"] then
return showusage()
end
if optarg["d"] == "k3" then
-- for now, the only difference is the additional input device
input.open("/dev/input/event0")
input.open("/dev/input/event1")
input.open("/dev/input/event2")
input.open("/tmp/event_slider")
setK3Keycodes()
elseif optarg["d"] == "emu" then
if util.isEmulated()==1 then
input.open("")
-- SDL key codes
setEmuKeycodes()
else
input.open("slider")
input.open("/dev/input/event0")
input.open("/dev/input/event1")
input.open("/tmp/event_slider")
-- check if we are running on Kindle 3 (additional volume input)
local f=lfs.attributes("/dev/input/event2")

Loading…
Cancel
Save