vdr 2.8.2
lirc.c
Go to the documentation of this file.
1/*
2 * lirc.c: LIRC remote control
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * LIRC support added by Carsten Koch <Carsten.Koch@icem.de> 2000-06-16.
8 *
9 * $Id: lirc.c 5.5 2026/05/30 11:59:32 kls Exp $
10 */
11
12#include "lirc.h"
13#include <linux/version.h>
14#define HAVE_KERNEL_LIRC (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
15// cLircUsrRemote
16#include <netinet/in.h>
17#include <sys/socket.h>
18#include <sys/un.h>
19// cLircDevRemote
20#if HAVE_KERNEL_LIRC
21#include <linux/lirc.h>
22#include <sys/ioctl.h>
23#endif
24
25#define RECONNECTDELAY 3000 // ms
26#define LIRC_REPEAT_TIMEOUT 250 // ms (sufficient to compensate 1 lost transmission)
27
29private:
30 enum { LIRC_KEY_BUF = 30, LIRC_BUFFER_SIZE = 128 };
31 struct sockaddr_un addr;
32 bool Connect(void);
33 virtual void Action(void) override;
34public:
35 cLircUsrRemote(const char *DeviceName);
36 };
37
38#if HAVE_KERNEL_LIRC
40private:
41 virtual void Action(void) override;
42public:
43 cLircDevRemote(void);
44 bool Connect(const char *DeviceName);
45 };
46#endif
47
48// --- cLircRemote -----------------------------------------------------------
49
52,cThread("LIRC remote control")
53{
54}
55
57{
58 int fh = f;
59 f = -1;
60 Cancel();
61 if (fh >= 0)
62 close(fh);
63}
64
66{
67#if HAVE_KERNEL_LIRC
69 if (r->Connect(Name))
70 return;
71 delete r;
72#endif
74}
75// --- cLircUsrRemote --------------------------------------------------------
76
77cLircUsrRemote::cLircUsrRemote(const char *DeviceName)
78: cLircRemote("LIRC")
79{
80 addr.sun_family = AF_UNIX;
81 strn0cpy(addr.sun_path, DeviceName, sizeof(addr.sun_path));
82 Connect();
83 Start();
84}
85
87{
88 if ((f = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
89 if (connect(f, (struct sockaddr *)&addr, sizeof(addr)) >= 0)
90 return true;
91 LOG_ERROR_STR(addr.sun_path);
92 close(f);
93 f = -1;
94 }
95 else
96 LOG_ERROR_STR(addr.sun_path);
97 return false;
98}
99
101{
102 return f >= 0;
103}
104
106{
107 cTimeMs FirstTime;
108 cTimeMs LastTime;
109 char buf[LIRC_BUFFER_SIZE];
110 char LastKeyName[LIRC_KEY_BUF] = "";
111 bool repeat = false;
112 int timeout = -1;
113
114 while (Running()) {
115
116 bool ready = f >= 0 && cFile::FileReady(f, timeout);
117 int ret = ready ? safe_read(f, buf, sizeof(buf)) : -1;
118
119 if (f < 0 || ready && ret <= 0) {
120 esyslog("ERROR: lircd connection broken, trying to reconnect every %.1f seconds", float(RECONNECTDELAY) / 1000);
121 if (f >= 0)
122 close(f);
123 f = -1;
124 while (Running() && f < 0) {
126 if (Connect()) {
127 isyslog("reconnected to lircd");
128 break;
129 }
130 }
131 }
132
133 else if (ready) {
134 buf[ret - 1] = 0;
135 int count;
136 char KeyName[LIRC_KEY_BUF];
137 if (sscanf(buf, "%*x %x %29s", &count, KeyName) != 2) { // '29' in '%29s' is LIRC_KEY_BUF-1!
138 esyslog("ERROR: unparsable lirc command: %s", buf);
139 continue;
140 }
141 if (count == 0) { // new key pressed
142 if (repeat)
143 Put(LastKeyName, false, true); // generated release for previous repeated key
144 strn0cpy(LastKeyName, KeyName, sizeof(LastKeyName));
145 FirstTime.Set();
146 repeat = false;
147 timeout = -1;
148 }
149 else { // repeat of a continuously pressed key
150 if (FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
151 continue; // repeat function kicks in after a short delay
152 if (LastTime.Elapsed() < (uint)Setup.RcRepeatDelta)
153 continue; // skip identical keys coming in too fast
154 repeat = true;
155 timeout = LIRC_REPEAT_TIMEOUT;
156 }
157 LastTime.Set();
158 Put(KeyName, repeat);
159 }
160 else { // no key pressed within timeout
161 if (repeat) // the last one was a repeat, so let's generate a release
162 Put(LastKeyName, false, true);
163 repeat = false;
164 timeout = -1;
165 }
166 }
167}
168
169// --- cLircDevRemote --------------------------------------------------------
170
171#if HAVE_KERNEL_LIRC
172bool cLircDevRemote::Connect(const char *DeviceName)
173{
174 unsigned mode = LIRC_MODE_SCANCODE;
175 f = open(DeviceName, O_RDONLY, 0);
176 if (f < 0) {
177 switch (errno) {
178 case ENXIO:
179 case ENODEV:
180 // Do not complain about an attempt to open a lircd socket file.
181 break;
182 default:
183 LOG_ERROR_STR(DeviceName);
184 }
185 }
186 else if (ioctl(f, LIRC_SET_REC_MODE, &mode)) {
187 LOG_ERROR_STR(DeviceName);
188 close(f);
189 f = -1;
190 }
191 if (f >= 0)
192 Start();
193 return f >= 0;
194}
195
197:cLircRemote("DEV_LIRC")
198{
199}
200
202{
203 if (f < 0)
204 return;
205 uint64_t FirstTime = 0, LastTime = 0;
206 uint32_t LastKeyCode = 0;
207 uint16_t LastFlags = false;
208 bool SeenRepeat = false;
209 bool repeat = false;
210
211 while (Running()) {
212 lirc_scancode sc;
213 ssize_t ret = read(f, &sc, sizeof sc);
214
215 if (ret == sizeof sc) {
216 const bool SameKey = sc.keycode == LastKeyCode && !((sc.flags ^ LastFlags) & LIRC_SCANCODE_FLAG_TOGGLE);
217
218 if ((sc.flags & LIRC_SCANCODE_FLAG_REPEAT) != 0)
219 // Before Linux 6.0, this flag is never set for some devices.
220 SeenRepeat = true;
221
222 if (SameKey && uint((sc.timestamp - FirstTime) / 1000000) < uint(Setup.RcRepeatDelay))
223 continue; // skip keys coming in too fast
224
225 if (!SameKey || (SeenRepeat && !(sc.flags & LIRC_SCANCODE_FLAG_REPEAT))) {
226 // This is a key-press event, not key-repeat.
227 if (repeat)
228 Put(LastKeyCode, false, true); // generated release for previous key
229 repeat = false;
230 FirstTime = sc.timestamp;
231 LastKeyCode = sc.keycode;
232 LastFlags = sc.flags;
233 }
234 else if (uint((sc.timestamp - LastTime) / 1000000) < uint(Setup.RcRepeatDelta))
235 continue; // filter out too frequent key-repeat events
236 else
237 repeat = true;
238
239 LastTime = sc.timestamp;
240 Put(sc.keycode, repeat);
241 }
242 else {
243 if (repeat) // the last one was a repeat, so let's generate a release
244 Put(LastKeyCode, false, true);
245 repeat = false;
246 LastKeyCode = 0;
247 }
248 }
249}
250#endif
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition thread.c:73
static bool FileReady(int FileDes, int TimeoutMs=1000)
Definition tools.c:1747
virtual void Action(void) override
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
Definition lirc.c:201
bool Connect(const char *DeviceName)
Definition lirc.c:172
cLircDevRemote(void)
Definition lirc.c:196
virtual ~cLircRemote() override
Definition lirc.c:56
virtual bool Ready(void) override
Definition lirc.c:100
static void NewLircRemote(const char *Name)
Definition lirc.c:65
int f
Definition lirc.h:18
cLircRemote(const char *Name)
Definition lirc.c:50
@ LIRC_BUFFER_SIZE
Definition lirc.c:30
@ LIRC_KEY_BUF
Definition lirc.c:30
cLircUsrRemote(const char *DeviceName)
Definition lirc.c:77
struct sockaddr_un addr
Definition lirc.c:31
virtual void Action(void) override
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
Definition lirc.c:105
bool Connect(void)
Definition lirc.c:86
const char * Name(void)
Definition remote.h:47
bool Put(uint64_t Code, bool Repeat=false, bool Release=false)
Definition remote.c:130
cRemote(const char *Name)
Definition remote.c:40
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition thread.c:320
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:102
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:254
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
Definition thread.c:370
uint64_t Elapsed(void) const
Returns the number of milliseconds that have elapsed since the last call to Set().
Definition tools.c:829
void Set(int Ms=0)
Sets the timer.
Definition tools.c:816
cSetup Setup
Definition config.c:372
#define LIRC_REPEAT_TIMEOUT
Definition lirc.c:26
#define RECONNECTDELAY
Definition lirc.c:25
ssize_t safe_read(int filedes, void *buffer, size_t size)
Definition tools.c:57
char * strn0cpy(char *dest, const char *src, size_t n)
Definition tools.c:135
#define LOG_ERROR_STR(s)
Definition tools.h:40
#define esyslog(a...)
Definition tools.h:35
#define isyslog(a...)
Definition tools.h:36