13#include <linux/version.h>
14#define HAVE_KERNEL_LIRC (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
16#include <netinet/in.h>
17#include <sys/socket.h>
21#include <linux/lirc.h>
25#define RECONNECTDELAY 3000
40 void Connect(
const char *DeviceName);
78 addr.sun_family = AF_UNIX;
86 if ((
f = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
87 if (connect(
f, (
struct sockaddr *)&
addr,
sizeof(
addr)) >= 0)
110 bool pressed =
false;
117 int ret = ready ?
safe_read(
f, buf,
sizeof(buf)) : -1;
119 if (
f < 0 || ready && ret <= 0) {
120 esyslog(
"ERROR: lircd connection broken, trying to reconnect every %.1f seconds",
float(
RECONNECTDELAY) / 1000);
127 isyslog(
"reconnected to lircd");
133 if (ready && ret > 0) {
137 if (sscanf(buf,
"%*x %x %29s", &count, KeyName) != 2) {
138 esyslog(
"ERROR: unparsable lirc command: %s", buf);
141 int Delta = ThisTime.
Elapsed();
147 Put(LastKeyName,
false,
true);
148 strn0cpy(LastKeyName, KeyName,
sizeof(LastKeyName));
161 timeout = Delta * 3 / 2;
165 Put(KeyName, repeat);
169 if (pressed && repeat)
170 Put(LastKeyName,
false,
true);
184 unsigned mode = LIRC_MODE_SCANCODE;
185 f = open(DeviceName, O_RDONLY, 0);
188 else if (ioctl(
f, LIRC_SET_REC_MODE, &mode)) {
206 uint64_t FirstTime = 0, LastTime = 0;
207 uint32_t LastKeyCode = 0;
208 uint16_t LastFlags =
false;
209 bool SeenRepeat =
false;
214 ssize_t ret = read(
f, &sc,
sizeof sc);
216 if (ret ==
sizeof sc) {
217 const bool SameKey = sc.keycode == LastKeyCode && !((sc.flags ^ LastFlags) & LIRC_SCANCODE_FLAG_TOGGLE);
219 if (sc.flags & LIRC_SCANCODE_FLAG_REPEAT != 0)
223 if (SameKey && uint((sc.timestamp - FirstTime) / 1000000) < uint(
Setup.
RcRepeatDelay))
226 if (!SameKey || (SeenRepeat && !(sc.flags & LIRC_SCANCODE_FLAG_REPEAT))) {
229 Put(LastKeyCode,
false,
true);
231 FirstTime = sc.timestamp;
232 LastKeyCode = sc.keycode;
233 LastFlags = sc.flags;
240 LastTime = sc.timestamp;
241 Put(sc.keycode, repeat);
245 Put(LastKeyCode,
false,
true);
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
static bool FileReady(int FileDes, int TimeoutMs=1000)
cLircDevRemote(const char *DeviceName)
virtual void Action(void)
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
void Connect(const char *DeviceName)
static void NewLircRemote(const char *Name)
cLircRemote(const char *Name)
cLircUsrRemote(const char *DeviceName)
virtual void Action(void)
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
bool Put(uint64_t Code, bool Repeat=false, bool Release=false)
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
uint64_t Elapsed(void) const
void Set(int Ms=0)
Sets the timer.