2002-09-29 13:40:45 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2022-11-26 13:37:06 +01:00
|
|
|
* $Id: lirc.c 5.1 2022/11/26 13:37:06 kls Exp $
|
2002-09-29 13:40:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lirc.h"
|
2022-11-26 13:37:06 +01:00
|
|
|
#include <linux/version.h>
|
|
|
|
#define HAVE_KERNEL_LIRC (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
|
|
|
|
// cLircUsrRemote
|
2002-09-29 13:40:45 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
2022-11-26 13:37:06 +01:00
|
|
|
#include <sys/un.h>
|
|
|
|
// cLircDevRemote
|
|
|
|
#if HAVE_KERNEL_LIRC
|
|
|
|
#include <linux/lirc.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
2002-09-29 13:40:45 +02:00
|
|
|
|
2006-01-27 16:03:32 +01:00
|
|
|
#define RECONNECTDELAY 3000 // ms
|
2002-09-29 13:40:45 +02:00
|
|
|
|
2022-11-26 13:37:06 +01:00
|
|
|
class cLircUsrRemote : public cLircRemote {
|
|
|
|
private:
|
|
|
|
enum { LIRC_KEY_BUF = 30, LIRC_BUFFER_SIZE = 128 };
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
bool Connect(void);
|
|
|
|
virtual void Action(void);
|
|
|
|
public:
|
|
|
|
cLircUsrRemote(const char *DeviceName);
|
|
|
|
};
|
|
|
|
|
|
|
|
#if HAVE_KERNEL_LIRC
|
|
|
|
class cLircDevRemote : public cLircRemote {
|
|
|
|
private:
|
|
|
|
void Connect(const char *DeviceName);
|
|
|
|
virtual void Action(void);
|
|
|
|
public:
|
|
|
|
cLircDevRemote(const char *DeviceName);
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// --- cLircRemote -----------------------------------------------------------
|
|
|
|
|
|
|
|
cLircRemote::cLircRemote(const char *Name)
|
|
|
|
:cRemote(Name)
|
2003-10-18 12:29:08 +02:00
|
|
|
,cThread("LIRC remote control")
|
2002-09-29 13:40:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cLircRemote::~cLircRemote()
|
|
|
|
{
|
2005-01-14 14:21:52 +01:00
|
|
|
int fh = f;
|
|
|
|
f = -1;
|
2002-09-29 13:40:45 +02:00
|
|
|
Cancel();
|
2005-01-14 14:21:52 +01:00
|
|
|
if (fh >= 0)
|
|
|
|
close(fh);
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-26 13:37:06 +01:00
|
|
|
void cLircRemote::NewLircRemote(const char *Name)
|
|
|
|
{
|
|
|
|
#if HAVE_KERNEL_LIRC
|
|
|
|
if (startswith(Name, "/dev/"))
|
|
|
|
new cLircDevRemote(Name);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
new cLircUsrRemote(Name);
|
|
|
|
}
|
|
|
|
// --- cLircUsrRemote --------------------------------------------------------
|
|
|
|
|
|
|
|
cLircUsrRemote::cLircUsrRemote(const char *DeviceName)
|
|
|
|
: cLircRemote("LIRC")
|
|
|
|
{
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strn0cpy(addr.sun_path, DeviceName, sizeof(addr.sun_path));
|
|
|
|
Connect();
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cLircUsrRemote::Connect(void)
|
2006-01-27 16:03:32 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-04-12 14:37:57 +02:00
|
|
|
bool cLircRemote::Ready(void)
|
|
|
|
{
|
|
|
|
return f >= 0;
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:37:06 +01:00
|
|
|
void cLircUsrRemote::Action(void)
|
2002-09-29 13:40:45 +02:00
|
|
|
{
|
2004-12-19 18:08:09 +01:00
|
|
|
cTimeMs FirstTime;
|
|
|
|
cTimeMs LastTime;
|
2013-01-31 12:13:39 +01:00
|
|
|
cTimeMs ThisTime;
|
2002-09-29 13:40:45 +02:00
|
|
|
char buf[LIRC_BUFFER_SIZE];
|
|
|
|
char LastKeyName[LIRC_KEY_BUF] = "";
|
2013-01-31 12:13:39 +01:00
|
|
|
bool pressed = false;
|
2002-09-29 13:40:45 +02:00
|
|
|
bool repeat = false;
|
2003-04-06 15:43:41 +02:00
|
|
|
int timeout = -1;
|
2002-09-29 13:40:45 +02:00
|
|
|
|
2013-10-29 16:03:43 +01:00
|
|
|
while (Running()) {
|
2002-09-29 13:40:45 +02:00
|
|
|
|
2013-10-29 16:03:43 +01:00
|
|
|
bool ready = f >= 0 && cFile::FileReady(f, timeout);
|
2005-08-15 12:30:21 +02:00
|
|
|
int ret = ready ? safe_read(f, buf, sizeof(buf)) : -1;
|
2003-04-06 15:43:41 +02:00
|
|
|
|
2013-10-29 16:03:43 +01:00
|
|
|
if (f < 0 || ready && ret <= 0) {
|
2006-01-27 16:03:32 +01:00
|
|
|
esyslog("ERROR: lircd connection broken, trying to reconnect every %.1f seconds", float(RECONNECTDELAY) / 1000);
|
2013-10-29 16:03:43 +01:00
|
|
|
if (f >= 0)
|
|
|
|
close(f);
|
2005-08-15 12:30:21 +02:00
|
|
|
f = -1;
|
2006-01-27 16:03:32 +01:00
|
|
|
while (Running() && f < 0) {
|
|
|
|
cCondWait::SleepMs(RECONNECTDELAY);
|
|
|
|
if (Connect()) {
|
|
|
|
isyslog("reconnected to lircd");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-08-15 12:30:21 +02:00
|
|
|
}
|
2003-04-25 13:46:56 +02:00
|
|
|
|
2011-03-08 15:38:58 +01:00
|
|
|
if (ready && ret > 0) {
|
|
|
|
buf[ret - 1] = 0;
|
2005-08-15 12:30:21 +02:00
|
|
|
int count;
|
|
|
|
char KeyName[LIRC_KEY_BUF];
|
2005-09-02 12:52:37 +02:00
|
|
|
if (sscanf(buf, "%*x %x %29s", &count, KeyName) != 2) { // '29' in '%29s' is LIRC_KEY_BUF-1!
|
2020-09-16 13:48:33 +02:00
|
|
|
esyslog("ERROR: unparsable lirc command: %s", buf);
|
2005-09-02 12:52:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-01-31 12:13:39 +01:00
|
|
|
int Delta = ThisTime.Elapsed(); // the time between two subsequent LIRC events
|
|
|
|
ThisTime.Set();
|
2017-05-30 11:05:00 +02:00
|
|
|
if (count == 0) { // new key pressed
|
2013-02-03 15:58:46 +01:00
|
|
|
if (strcmp(KeyName, LastKeyName) == 0 && FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
|
2005-08-15 12:30:21 +02:00
|
|
|
continue; // skip keys coming in too fast
|
|
|
|
if (repeat)
|
2017-05-30 11:05:00 +02:00
|
|
|
Put(LastKeyName, false, true); // generated release for previous repeated key
|
|
|
|
strn0cpy(LastKeyName, KeyName, sizeof(LastKeyName));
|
2013-01-31 12:13:39 +01:00
|
|
|
pressed = true;
|
2005-08-15 12:30:21 +02:00
|
|
|
repeat = false;
|
|
|
|
FirstTime.Set();
|
|
|
|
timeout = -1;
|
|
|
|
}
|
2013-02-03 15:58:46 +01:00
|
|
|
else if (FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
|
2013-01-30 13:03:00 +01:00
|
|
|
continue; // repeat function kicks in after a short delay
|
2013-02-03 15:58:46 +01:00
|
|
|
else if (LastTime.Elapsed() < (uint)Setup.RcRepeatDelta)
|
2013-01-30 13:03:00 +01:00
|
|
|
continue; // skip same keys coming in too fast
|
2005-08-15 12:30:21 +02:00
|
|
|
else {
|
2013-08-22 09:35:35 +02:00
|
|
|
pressed = true;
|
2005-08-15 12:30:21 +02:00
|
|
|
repeat = true;
|
2017-05-30 11:05:00 +02:00
|
|
|
timeout = Delta * 3 / 2;
|
2005-08-15 12:30:21 +02:00
|
|
|
}
|
2013-02-11 15:31:09 +01:00
|
|
|
if (pressed) {
|
2013-01-31 12:13:39 +01:00
|
|
|
LastTime.Set();
|
|
|
|
Put(KeyName, repeat);
|
2013-02-11 15:31:09 +01:00
|
|
|
}
|
2005-08-15 12:30:21 +02:00
|
|
|
}
|
2013-01-31 12:13:39 +01:00
|
|
|
else {
|
2017-05-30 11:05:00 +02:00
|
|
|
if (pressed && repeat) // the last one was a repeat, so let's generate a release
|
|
|
|
Put(LastKeyName, false, true);
|
2013-01-31 12:13:39 +01:00
|
|
|
pressed = false;
|
|
|
|
repeat = false;
|
|
|
|
*LastKeyName = 0;
|
|
|
|
timeout = -1;
|
2005-08-15 12:30:21 +02:00
|
|
|
}
|
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
2022-11-26 13:37:06 +01:00
|
|
|
|
|
|
|
// --- cLircDevRemote --------------------------------------------------------
|
|
|
|
|
|
|
|
#if HAVE_KERNEL_LIRC
|
|
|
|
inline void cLircDevRemote::Connect(const char *DeviceName)
|
|
|
|
{
|
|
|
|
unsigned mode = LIRC_MODE_SCANCODE;
|
|
|
|
f = open(DeviceName, O_RDONLY, 0);
|
|
|
|
if (f < 0)
|
|
|
|
LOG_ERROR_STR(DeviceName);
|
|
|
|
else if (ioctl(f, LIRC_SET_REC_MODE, &mode)) {
|
|
|
|
LOG_ERROR_STR(DeviceName);
|
|
|
|
close(f);
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cLircDevRemote::cLircDevRemote(const char *DeviceName)
|
|
|
|
: cLircRemote("DEV_LIRC")
|
|
|
|
{
|
|
|
|
Connect(DeviceName);
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cLircDevRemote::Action(void)
|
|
|
|
{
|
|
|
|
if (f < 0)
|
|
|
|
return;
|
|
|
|
uint64_t FirstTime = 0, LastTime = 0;
|
|
|
|
uint32_t LastKeyCode = 0;
|
|
|
|
uint16_t LastFlags = false;
|
|
|
|
bool SeenRepeat = false;
|
|
|
|
bool repeat = false;
|
|
|
|
|
|
|
|
while (Running()) {
|
|
|
|
lirc_scancode sc;
|
|
|
|
ssize_t ret = read(f, &sc, sizeof sc);
|
|
|
|
|
|
|
|
if (ret == sizeof sc) {
|
|
|
|
const bool SameKey = sc.keycode == LastKeyCode && !((sc.flags ^ LastFlags) & LIRC_SCANCODE_FLAG_TOGGLE);
|
|
|
|
|
|
|
|
if (sc.flags & LIRC_SCANCODE_FLAG_REPEAT != 0)
|
|
|
|
// Before Linux 6.0, this flag is never set for some devices.
|
|
|
|
SeenRepeat = true;
|
|
|
|
|
|
|
|
if (SameKey && uint((sc.timestamp - FirstTime) / 1000000) < uint(Setup.RcRepeatDelay))
|
|
|
|
continue; // skip keys coming in too fast
|
|
|
|
|
|
|
|
if (!SameKey || (SeenRepeat && !(sc.flags & LIRC_SCANCODE_FLAG_REPEAT))) {
|
|
|
|
// This is a key-press event, not key-repeat.
|
|
|
|
if (repeat)
|
|
|
|
Put(LastKeyCode, false, true); // generated release for previous key
|
|
|
|
repeat = false;
|
|
|
|
FirstTime = sc.timestamp;
|
|
|
|
LastKeyCode = sc.keycode;
|
|
|
|
LastFlags = sc.flags;
|
|
|
|
}
|
|
|
|
else if (uint((sc.timestamp - LastTime) / 1000000) < uint(Setup.RcRepeatDelta))
|
|
|
|
continue; // filter out too frequent key-repeat events
|
|
|
|
else
|
|
|
|
repeat = true;
|
|
|
|
|
|
|
|
LastTime = sc.timestamp;
|
|
|
|
Put(sc.keycode, repeat);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (repeat) // the last one was a repeat, so let's generate a release
|
|
|
|
Put(LastKeyCode, false, true);
|
|
|
|
repeat = false;
|
|
|
|
LastKeyCode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|