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.
|
|
|
|
*
|
2004-12-19 18:08:09 +01:00
|
|
|
* $Id: lirc.c 1.9 2004/12/19 18:05:13 kls Exp $
|
2002-09-29 13:40:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lirc.h"
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#define REPEATLIMIT 20 // ms
|
|
|
|
#define REPEATDELAY 350 // ms
|
2003-04-27 11:41:51 +02:00
|
|
|
#define KEYPRESSDELAY 150 // ms
|
2002-09-29 13:40:45 +02:00
|
|
|
|
|
|
|
cLircRemote::cLircRemote(char *DeviceName)
|
|
|
|
:cRemote("LIRC")
|
2003-10-18 12:29:08 +02:00
|
|
|
,cThread("LIRC remote control")
|
2002-09-29 13:40:45 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(addr.sun_path, DeviceName);
|
|
|
|
if ((f = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
|
|
|
|
if (connect(f, (struct sockaddr *)&addr, sizeof(addr)) >= 0) {
|
|
|
|
Start();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG_ERROR_STR(DeviceName);
|
|
|
|
close(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(DeviceName);
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cLircRemote::~cLircRemote()
|
|
|
|
{
|
|
|
|
Cancel();
|
2004-12-18 13:26:07 +01:00
|
|
|
if (f >= 0)
|
|
|
|
close(f);
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
|
|
|
|
2003-04-12 14:37:57 +02:00
|
|
|
bool cLircRemote::Ready(void)
|
|
|
|
{
|
|
|
|
return f >= 0;
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
void cLircRemote::Action(void)
|
|
|
|
{
|
2004-12-19 18:08:09 +01:00
|
|
|
cTimeMs FirstTime;
|
|
|
|
cTimeMs LastTime;
|
2002-09-29 13:40:45 +02:00
|
|
|
char buf[LIRC_BUFFER_SIZE];
|
|
|
|
char LastKeyName[LIRC_KEY_BUF] = "";
|
|
|
|
bool repeat = false;
|
2003-04-06 15:43:41 +02:00
|
|
|
int timeout = -1;
|
2002-09-29 13:40:45 +02:00
|
|
|
|
|
|
|
for (; f >= 0;) {
|
|
|
|
|
2003-04-06 15:43:41 +02:00
|
|
|
bool ready = cFile::FileReady(f, timeout);
|
|
|
|
int ret = ready ? safe_read(f, buf, sizeof(buf)) : -1;
|
|
|
|
|
2003-04-25 13:46:56 +02:00
|
|
|
if (ready && ret <= 0 ) {
|
|
|
|
esyslog("ERROR: lircd connection lost");
|
|
|
|
close(f);
|
|
|
|
f = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ready && ret > 21) {
|
2002-09-29 13:40:45 +02:00
|
|
|
int count;
|
|
|
|
char KeyName[LIRC_KEY_BUF];
|
|
|
|
sscanf(buf, "%*x %x %29s", &count, KeyName); // '29' in '%29s' is LIRC_KEY_BUF-1!
|
|
|
|
if (count == 0) {
|
2004-12-19 18:08:09 +01:00
|
|
|
if (strcmp(KeyName, LastKeyName) == 0 && FirstTime.Elapsed() < KEYPRESSDELAY)
|
2003-04-27 11:41:51 +02:00
|
|
|
continue; // skip keys coming in too fast
|
2003-04-06 15:46:06 +02:00
|
|
|
if (repeat)
|
|
|
|
Put(LastKeyName, false, true);
|
2002-09-29 13:40:45 +02:00
|
|
|
strcpy(LastKeyName, KeyName);
|
|
|
|
repeat = false;
|
2004-12-19 18:08:09 +01:00
|
|
|
FirstTime.Set();
|
2003-04-06 15:43:41 +02:00
|
|
|
timeout = -1;
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
|
|
|
else {
|
2004-12-19 18:08:09 +01:00
|
|
|
if (FirstTime.Elapsed() < REPEATDELAY)
|
2002-09-29 13:40:45 +02:00
|
|
|
continue; // repeat function kicks in after a short delay
|
|
|
|
repeat = true;
|
2003-04-06 15:43:41 +02:00
|
|
|
timeout = REPEATDELAY;
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
2004-12-19 18:08:09 +01:00
|
|
|
LastTime.Set();
|
2002-09-29 13:40:45 +02:00
|
|
|
Put(KeyName, repeat);
|
|
|
|
}
|
|
|
|
else if (repeat) { // the last one was a repeat, so let's generate a release
|
2004-12-19 18:08:09 +01:00
|
|
|
if (LastTime.Elapsed() >= REPEATDELAY) {
|
2002-09-29 13:40:45 +02:00
|
|
|
Put(LastKeyName, false, true);
|
|
|
|
repeat = false;
|
|
|
|
*LastKeyName = 0;
|
2003-04-06 15:43:41 +02:00
|
|
|
timeout = -1;
|
2002-09-29 13:40:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|