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
|
/*
* lirc.c: LIRC remote control
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* LIRC support added by Carsten Koch <Carsten.Koch@icem.de> 2000-06-16.
*
* $Id: lirc.c 3.1 2013/08/22 09:22:14 kls Exp $
*/
#include "lirc.h"
#include <netinet/in.h>
#include <sys/socket.h>
#define RECONNECTDELAY 3000 // ms
cLircRemote::cLircRemote(const char *DeviceName)
:cRemote("LIRC")
,cThread("LIRC remote control")
{
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, DeviceName);
if (Connect()) {
Start();
return;
}
f = -1;
}
cLircRemote::~cLircRemote()
{
int fh = f;
f = -1;
Cancel();
if (fh >= 0)
close(fh);
}
bool cLircRemote::Connect(void)
{
if ((f = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
if (connect(f, (struct sockaddr *)&addr, sizeof(addr)) >= 0)
return true;
LOG_ERROR_STR(addr.sun_path);
close(f);
f = -1;
}
else
LOG_ERROR_STR(addr.sun_path);
return false;
}
bool cLircRemote::Ready(void)
{
return f >= 0;
}
void cLircRemote::Action(void)
{
cTimeMs FirstTime;
cTimeMs LastTime;
cTimeMs ThisTime;
char buf[LIRC_BUFFER_SIZE];
char LastKeyName[LIRC_KEY_BUF] = "";
bool pressed = false;
bool repeat = false;
int timeout = -1;
while (Running() && f >= 0) {
bool ready = cFile::FileReady(f, timeout);
int ret = ready ? safe_read(f, buf, sizeof(buf)) : -1;
if (ready && ret <= 0 ) {
esyslog("ERROR: lircd connection broken, trying to reconnect every %.1f seconds", float(RECONNECTDELAY) / 1000);
close(f);
f = -1;
while (Running() && f < 0) {
cCondWait::SleepMs(RECONNECTDELAY);
if (Connect()) {
isyslog("reconnected to lircd");
break;
}
}
}
if (ready && ret > 0) {
buf[ret - 1] = 0;
int count;
char KeyName[LIRC_KEY_BUF];
if (sscanf(buf, "%*x %x %29s", &count, KeyName) != 2) { // '29' in '%29s' is LIRC_KEY_BUF-1!
esyslog("ERROR: unparseable lirc command: %s", buf);
continue;
}
int Delta = ThisTime.Elapsed(); // the time between two subsequent LIRC events
ThisTime.Set();
if (count == 0) {
if (strcmp(KeyName, LastKeyName) == 0 && FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
continue; // skip keys coming in too fast
if (repeat)
Put(LastKeyName, false, true);
strcpy(LastKeyName, KeyName);
pressed = true;
repeat = false;
FirstTime.Set();
timeout = -1;
}
else if (FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
continue; // repeat function kicks in after a short delay
else if (LastTime.Elapsed() < (uint)Setup.RcRepeatDelta)
continue; // skip same keys coming in too fast
else {
pressed = true;
repeat = true;
timeout = Delta * 10 / 9;
}
if (pressed) {
LastTime.Set();
Put(KeyName, repeat);
}
}
else if (pressed && repeat) { // the last one was a repeat, so let's generate a release
Put(LastKeyName, false, true);
pressed = false;
repeat = false;
*LastKeyName = 0;
timeout = -1;
}
else {
pressed = false;
repeat = false;
*LastKeyName = 0;
timeout = -1;
}
}
}
|