-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
133 lines (118 loc) · 2.21 KB
/
io.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
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <locale.h>
#include <fcntl.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <linux/input.h>
#include <poll.h>
#include <stdint.h>
#include "current.h"
#include "io.h"
#include "rc_device.h"
extern int instance;
extern int get_instance(void);
extern int rcvalue;
struct input_event ev;
static unsigned short rccode=-1;
static int rc;
int InitRC(void)
{
char rc_device[32];
get_rc_device(rc_device);
// only for debug
// printf("rc_device: using %s\n", rc_device);
rc = open(rc_device, O_RDONLY | O_CLOEXEC);
if(rc == -1)
{
perror(__plugin__ " <open remote control>");
exit(1);
}
fcntl(rc, F_SETFL, O_NONBLOCK | O_SYNC);
while(RCKeyPressed());
return 1;
}
int CloseRC(void)
{
while(RCKeyPressed());
close(rc);
return 1;
}
int RCKeyPressed(void)
{
if(read(rc, &ev, sizeof(ev)) == sizeof(ev))
{
if(ev.value)
{
rcvalue=ev.value;
rccode=ev.code;
return 1;
}
}
rccode = -1;
return 0;
}
void ClearRC(void)
{
struct pollfd pfd;
pfd.fd = rc;
pfd.events = POLLIN;
pfd.revents = 0;
do
poll(&pfd, 1, 300);
while(read(rc, &ev, sizeof(ev)) == sizeof(ev));
}
int GetRCCode(int timeout_in_ms)
{
int rv = -1;
if (get_instance()>instance)
{
return rv;
}
if (timeout_in_ms) {
struct pollfd pfd;
struct timeval tv;
uint64_t ms_now, ms_final;
pfd.fd = rc;
pfd.events = POLLIN;
pfd.revents = 0;
gettimeofday( &tv, NULL );
ms_now = tv.tv_usec/1000 + tv.tv_sec * 1000;
if (timeout_in_ms > 0)
ms_final = ms_now + timeout_in_ms;
else
ms_final = UINT64_MAX;
while (ms_final > ms_now) {
switch(poll(&pfd, 1, timeout_in_ms)) {
case -1:
perror("GetRCCode: poll() failed");
case 0:
return -1;
default:
;
}
if(RCKeyPressed()) {
rv = rccode;
while(RCKeyPressed());
return rv;
}
gettimeofday( &tv, NULL );
ms_now = tv.tv_usec/1000 + tv.tv_sec * 1000;
if (timeout_in_ms > 0)
timeout_in_ms = (int)(ms_final - ms_now);
}
} else if(RCKeyPressed()) {
rv = rccode;
while(RCKeyPressed());
}
return rv;
}