2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/slider_watcher.c

115 lines
3.0 KiB
C
Raw Normal View History

2012-04-06 05:51:37 +00:00
/*
KindlePDFViewer: power slider key event watcher
Copyright (C) 2012 Qingping Hou <dave2008713@gamil.com>
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/>.
*/
2012-10-02 01:11:16 +00:00
#include "popen-noshell/popen_noshell.h"
#include <err.h>
2012-04-06 05:51:37 +00:00
#include <stdio.h>
2012-10-02 01:11:16 +00:00
#include <signal.h>
#include <stdlib.h>
2012-04-06 05:51:37 +00:00
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
2012-10-02 01:11:16 +00:00
#include <sys/wait.h>
2012-04-06 05:51:37 +00:00
#define CODE_IN_SAVER 10000
#define CODE_OUT_SAVER 10001
int
main ( int argc, char *argv[] )
{
2012-10-02 01:11:16 +00:00
int fd;
2012-04-06 05:51:37 +00:00
FILE *fp;
2012-10-02 01:11:16 +00:00
char std_out[256];
int status;
struct popen_noshell_pass_to_pclose pclose_arg;
2012-04-06 05:51:37 +00:00
struct input_event ev;
__u16 key_code = 10000;
/* create the npipe if not exists */
/*if(access(argv[1], F_OK)){*/
/*printf("npipe %s not found, try to create it...\n", argv[1]);*/
/*if(mkfifo(argv[1], 0777)) {*/
/*printf("Create npipe %s failed!\n", argv[1]);*/
/*}*/
/*}*/
/* open npipe for writing */
fd = open(argv[1], O_RDWR | O_NONBLOCK);
if(fd < 0) {
2012-10-02 01:11:16 +00:00
printf("Open %s failed: %s\n", argv[1], strerror(errno));
2012-04-06 05:51:37 +00:00
exit(EXIT_FAILURE);
}
/* initialize event struct */
ev.type = EV_KEY;
ev.code = key_code;
ev.value = 1;
2012-10-02 01:11:16 +00:00
/* listen power slider events */
char *argv[] = {"lipc-wait-event", "-m", "-s", "0", "com.lab126.powerd", "goingToScreenSaver,outOfScreenSaver", (char *) NULL};
2012-10-02 01:11:16 +00:00
fp = popen_noshell("lipc-wait-event", (const char * const *)chargv, "r", &pclose_arg, 0);
2012-10-02 01:11:16 +00:00
if (!fp) {
err(EXIT_FAILURE, "popen_noshell()");
}
while(fgets(std_out, sizeof(std_out)-1, fp)) {
/* printf("Got line: %s", std_out); */
2012-04-06 05:51:37 +00:00
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");
exit(EXIT_FAILURE);
}
2012-10-02 01:11:16 +00:00
/* fill event struct */
gettimeofday(&ev.time, NULL);
/* printf("Send event %d\n", ev.code); */
2012-04-06 05:51:37 +00:00
/* generate event */
2012-10-02 01:11:16 +00:00
if(write(fd, &ev, sizeof(struct input_event)) == -1) {
printf("Failed to generate event.\n");
}
}
status = pclose_noshell(&pclose_arg);
if (status == -1) {
err(EXIT_FAILURE, "pclose_noshell()");
} else {
printf("Power slider event listener child exited with status %d.\n", status);
if WIFEXITED(status) {
printf("Child exited normally with status: %d.\n", WEXITSTATUS(status));
}
if WIFSIGNALED(status) {
printf("Child terminated by signal: %d.\n", WTERMSIG(status));
}
2012-04-06 05:51:37 +00:00
}
close(fd);
return EXIT_SUCCESS;
}