2000-02-19 13:36:48 +01:00
|
|
|
/*
|
2002-09-29 13:40:45 +02:00
|
|
|
* remote.c: General Remote Control handling
|
2000-02-19 13:36:48 +01:00
|
|
|
*
|
2000-04-24 09:46:05 +02:00
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
2000-02-19 13:36:48 +01:00
|
|
|
* how to reach the author.
|
|
|
|
*
|
2007-02-25 10:56:29 +01:00
|
|
|
* $Id: remote.c 1.56 2007/02/24 13:23:12 kls Exp $
|
2000-02-19 13:36:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "remote.h"
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "tools.h"
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
// --- cRemote ---------------------------------------------------------------
|
2000-07-15 12:39:20 +02:00
|
|
|
|
2006-01-29 12:39:04 +01:00
|
|
|
#define INITTIMEOUT 10000 // ms
|
|
|
|
#define REPEATTIMEOUT 1000 // ms
|
2004-05-28 14:28:36 +02:00
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
eKeys cRemote::keys[MaxKeys];
|
|
|
|
int cRemote::in = 0;
|
|
|
|
int cRemote::out = 0;
|
2006-01-29 12:39:04 +01:00
|
|
|
cTimeMs cRemote::repeatTimeout;
|
2002-11-01 10:53:07 +01:00
|
|
|
cRemote *cRemote::learning = NULL;
|
2002-09-29 13:40:45 +02:00
|
|
|
char *cRemote::unknownCode = NULL;
|
|
|
|
cMutex cRemote::mutex;
|
|
|
|
cCondVar cRemote::keyPressed;
|
2006-10-14 11:09:01 +02:00
|
|
|
const char *cRemote::keyMacroPlugin = NULL;
|
|
|
|
const char *cRemote::callPlugin = NULL;
|
2007-02-25 10:56:29 +01:00
|
|
|
time_t cRemote::lastActivity = 0;
|
2000-07-15 12:39:20 +02:00
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
cRemote::cRemote(const char *Name)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-10-12 15:22:29 +02:00
|
|
|
name = Name ? strdup(Name) : NULL;
|
2002-09-29 13:40:45 +02:00
|
|
|
Remotes.Add(this);
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
cRemote::~cRemote()
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
free(name);
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
const char *cRemote::GetSetup(void)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
return Keys.GetSetup(Name());
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
void cRemote::PutSetup(const char *Setup)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
Keys.PutSetup(Name(), Setup);
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2004-05-28 14:28:36 +02:00
|
|
|
bool cRemote::Initialize(void)
|
|
|
|
{
|
|
|
|
if (Ready()) {
|
|
|
|
char *NewCode = NULL;
|
|
|
|
eKeys Key = Get(INITTIMEOUT, &NewCode);
|
|
|
|
if (Key != kNone || NewCode)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
void cRemote::Clear(void)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
in = out = 0;
|
|
|
|
if (learning) {
|
|
|
|
free(unknownCode);
|
|
|
|
unknownCode = NULL;
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-02 10:54:00 +02:00
|
|
|
bool cRemote::Put(eKeys Key, bool AtFront)
|
2002-09-29 13:40:45 +02:00
|
|
|
{
|
|
|
|
if (Key != kNone) {
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2002-12-07 11:48:10 +01:00
|
|
|
if (in != out && (keys[out] & k_Repeat) && (Key & k_Release))
|
2002-09-29 13:40:45 +02:00
|
|
|
Clear();
|
|
|
|
int d = out - in;
|
|
|
|
if (d <= 0)
|
|
|
|
d = MaxKeys + d;
|
|
|
|
if (d - 1 > 0) {
|
2003-05-02 10:54:00 +02:00
|
|
|
if (AtFront) {
|
|
|
|
if (--out < 0)
|
|
|
|
out = MaxKeys - 1;
|
|
|
|
keys[out] = Key;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
keys[in] = Key;
|
|
|
|
if (++in >= MaxKeys)
|
|
|
|
in = 0;
|
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
keyPressed.Broadcast();
|
|
|
|
return true;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
return false;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
return true; // only a real key shall report an overflow!
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
2002-10-27 15:46:30 +01:00
|
|
|
bool cRemote::PutMacro(eKeys Key)
|
|
|
|
{
|
|
|
|
const cKeyMacro *km = KeyMacros.Get(Key);
|
|
|
|
if (km) {
|
2006-10-14 11:09:01 +02:00
|
|
|
keyMacroPlugin = km->Plugin();
|
2006-10-14 10:46:19 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2006-10-14 10:41:20 +02:00
|
|
|
for (int i = km->NumKeys(); --i > 0; ) {
|
|
|
|
if (!Put(km->Macro()[i], true))
|
|
|
|
return false;
|
2002-10-27 15:46:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
bool cRemote::Put(uint64_t Code, bool Repeat, bool Release)
|
2000-10-08 09:25:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
char buffer[32];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%016LX", Code);
|
|
|
|
return Put(buffer, Repeat, Release);
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
bool cRemote::Put(const char *Code, bool Repeat, bool Release)
|
2000-04-23 15:38:16 +02:00
|
|
|
{
|
2002-11-01 10:53:07 +01:00
|
|
|
if (learning && this != learning)
|
|
|
|
return false;
|
2002-09-29 13:40:45 +02:00
|
|
|
eKeys Key = Keys.Get(Name(), Code);
|
|
|
|
if (Key != kNone) {
|
|
|
|
if (Repeat)
|
|
|
|
Key = eKeys(Key | k_Repeat);
|
|
|
|
if (Release)
|
|
|
|
Key = eKeys(Key | k_Release);
|
|
|
|
return Put(Key);
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
if (learning) {
|
|
|
|
free(unknownCode);
|
|
|
|
unknownCode = strdup(Code);
|
|
|
|
keyPressed.Broadcast();
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-17 09:10:39 +02:00
|
|
|
bool cRemote::CallPlugin(const char *Plugin)
|
2005-09-03 12:36:51 +02:00
|
|
|
{
|
2006-04-17 09:10:39 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2006-10-14 11:09:01 +02:00
|
|
|
if (!callPlugin) {
|
|
|
|
callPlugin = Plugin;
|
2006-04-17 09:10:39 +02:00
|
|
|
Put(k_Plugin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cRemote::GetPlugin(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2006-10-14 11:09:01 +02:00
|
|
|
const char *p = keyMacroPlugin;
|
|
|
|
if (p)
|
|
|
|
keyMacroPlugin = NULL;
|
|
|
|
else {
|
|
|
|
p = callPlugin;
|
|
|
|
callPlugin = NULL;
|
|
|
|
}
|
2006-04-17 09:10:39 +02:00
|
|
|
return p;
|
2005-09-03 12:36:51 +02:00
|
|
|
}
|
|
|
|
|
2003-05-01 14:48:54 +02:00
|
|
|
bool cRemote::HasKeys(void)
|
|
|
|
{
|
2005-03-20 13:27:01 +01:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2003-05-01 14:48:54 +02:00
|
|
|
return in != out && !(keys[out] & k_Repeat);
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
eKeys cRemote::Get(int WaitMs, char **UnknownCode)
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
|
|
|
for (;;) {
|
2002-09-29 13:40:45 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (in != out) {
|
|
|
|
eKeys k = keys[out];
|
|
|
|
if (++out >= MaxKeys)
|
|
|
|
out = 0;
|
2006-01-29 12:39:04 +01:00
|
|
|
if ((k & k_Repeat) != 0)
|
|
|
|
repeatTimeout.Set(REPEATTIMEOUT);
|
2007-02-25 10:56:29 +01:00
|
|
|
lastActivity = time(NULL);
|
2002-09-29 13:40:45 +02:00
|
|
|
return k;
|
|
|
|
}
|
2006-05-12 12:42:57 +02:00
|
|
|
else if (!WaitMs || !keyPressed.TimedWait(mutex, WaitMs) && repeatTimeout.TimedOut())
|
|
|
|
return kNone;
|
|
|
|
else if (learning && UnknownCode && unknownCode) {
|
|
|
|
*UnknownCode = unknownCode;
|
|
|
|
unknownCode = NULL;
|
2002-09-29 13:40:45 +02:00
|
|
|
return kNone;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
// --- cRemotes --------------------------------------------------------------
|
2000-10-08 09:25:20 +02:00
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
cRemotes Remotes;
|
2000-02-19 13:36:48 +01:00
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
// --- cKbdRemote ------------------------------------------------------------
|
2000-05-07 09:28:39 +02:00
|
|
|
|
2002-12-15 10:58:00 +01:00
|
|
|
struct tKbdMap {
|
|
|
|
eKbdFunc func;
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t code;
|
2002-12-15 10:58:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static tKbdMap KbdMap[] = {
|
|
|
|
{ kfF1, 0x0000001B5B31317EULL },
|
|
|
|
{ kfF2, 0x0000001B5B31327EULL },
|
|
|
|
{ kfF3, 0x0000001B5B31337EULL },
|
|
|
|
{ kfF4, 0x0000001B5B31347EULL },
|
|
|
|
{ kfF5, 0x0000001B5B31357EULL },
|
|
|
|
{ kfF6, 0x0000001B5B31377EULL },
|
|
|
|
{ kfF7, 0x0000001B5B31387EULL },
|
|
|
|
{ kfF8, 0x0000001B5B31397EULL },
|
|
|
|
{ kfF9, 0x0000001B5B32307EULL },
|
|
|
|
{ kfF10, 0x0000001B5B32317EULL },
|
|
|
|
{ kfF11, 0x0000001B5B32327EULL },
|
|
|
|
{ kfF12, 0x0000001B5B32337EULL },
|
|
|
|
{ kfUp, 0x00000000001B5B41ULL },
|
|
|
|
{ kfDown, 0x00000000001B5B42ULL },
|
|
|
|
{ kfLeft, 0x00000000001B5B44ULL },
|
|
|
|
{ kfRight, 0x00000000001B5B43ULL },
|
|
|
|
{ kfHome, 0x00000000001B5B48ULL },
|
|
|
|
{ kfEnd, 0x00000000001B5B46ULL },
|
|
|
|
{ kfPgUp, 0x000000001B5B357EULL },
|
|
|
|
{ kfPgDown, 0x000000001B5B367EULL },
|
|
|
|
{ kfIns, 0x000000001B5B327EULL },
|
|
|
|
{ kfDel, 0x000000001B5B337EULL },
|
|
|
|
{ kfNone, 0x0000000000000000ULL }
|
|
|
|
};
|
|
|
|
|
2002-12-15 15:58:59 +01:00
|
|
|
bool cKbdRemote::kbdAvailable = false;
|
2002-12-15 10:58:00 +01:00
|
|
|
bool cKbdRemote::rawMode = false;
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
cKbdRemote::cKbdRemote(void)
|
|
|
|
:cRemote("KBD")
|
2003-10-18 12:29:08 +02:00
|
|
|
,cThread("KBD remote control")
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-12-08 14:30:32 +01:00
|
|
|
tcgetattr(STDIN_FILENO, &savedTm);
|
|
|
|
struct termios tm;
|
|
|
|
if (tcgetattr(STDIN_FILENO, &tm) == 0) {
|
|
|
|
tm.c_iflag = 0;
|
|
|
|
tm.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
tm.c_cc[VMIN] = 0;
|
|
|
|
tm.c_cc[VTIME] = 0;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tm);
|
|
|
|
}
|
2002-12-15 15:58:59 +01:00
|
|
|
kbdAvailable = true;
|
2002-09-29 13:40:45 +02:00
|
|
|
Start();
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
cKbdRemote::~cKbdRemote()
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-12-15 15:58:59 +01:00
|
|
|
kbdAvailable = false;
|
2002-12-08 14:30:32 +01:00
|
|
|
Cancel(3);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-12-15 10:58:00 +01:00
|
|
|
void cKbdRemote::SetRawMode(bool RawMode)
|
|
|
|
{
|
|
|
|
rawMode = RawMode;
|
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t cKbdRemote::MapFuncToCode(int Func)
|
2002-12-15 10:58:00 +01:00
|
|
|
{
|
|
|
|
for (tKbdMap *p = KbdMap; p->func != kfNone; p++) {
|
|
|
|
if (p->func == Func)
|
|
|
|
return p->code;
|
|
|
|
}
|
|
|
|
return (Func <= 0xFF) ? Func : 0;
|
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
int cKbdRemote::MapCodeToFunc(uint64_t Code)
|
2002-12-15 10:58:00 +01:00
|
|
|
{
|
|
|
|
for (tKbdMap *p = KbdMap; p->func != kfNone; p++) {
|
|
|
|
if (p->code == Code)
|
|
|
|
return p->func;
|
|
|
|
}
|
|
|
|
return (Code <= 0xFF) ? Code : kfNone;
|
|
|
|
}
|
|
|
|
|
2006-01-01 14:28:47 +01:00
|
|
|
int cKbdRemote::ReadKey(void)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
cPoller Poller(STDIN_FILENO);
|
2006-01-01 14:28:47 +01:00
|
|
|
if (Poller.Poll(50)) {
|
|
|
|
uchar ch = 0;
|
|
|
|
int r = safe_read(STDIN_FILENO, &ch, 1);
|
|
|
|
if (r == 1)
|
|
|
|
return ch;
|
|
|
|
if (r < 0)
|
|
|
|
LOG_ERROR_STR("cKbdRemote");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t cKbdRemote::ReadKeySequence(void)
|
2006-01-01 14:28:47 +01:00
|
|
|
{
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t k = 0;
|
2006-01-01 14:28:47 +01:00
|
|
|
int key1;
|
|
|
|
|
|
|
|
if ((key1 = ReadKey()) >= 0) {
|
|
|
|
k = key1;
|
|
|
|
if (key1 == 0x1B) {
|
|
|
|
// Start of escape sequence
|
|
|
|
if ((key1 = ReadKey()) >= 0) {
|
|
|
|
k <<= 8;
|
|
|
|
k |= key1 & 0xFF;
|
|
|
|
switch (key1) {
|
|
|
|
case 0x4F: // 3-byte sequence
|
|
|
|
if ((key1 = ReadKey()) >= 0) {
|
|
|
|
k <<= 8;
|
|
|
|
k |= key1 & 0xFF;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x5B: // 3- or more-byte sequence
|
|
|
|
if ((key1 = ReadKey()) >= 0) {
|
|
|
|
k <<= 8;
|
|
|
|
k |= key1 & 0xFF;
|
|
|
|
switch (key1) {
|
|
|
|
case 0x31 ... 0x3F: // more-byte sequence
|
2006-01-15 15:00:00 +01:00
|
|
|
case 0x5B: // strange, may apparently occur
|
2006-01-08 17:17:20 +01:00
|
|
|
do {
|
|
|
|
if ((key1 = ReadKey()) < 0)
|
|
|
|
break; // Sequence ends here
|
|
|
|
k <<= 8;
|
|
|
|
k |= key1 & 0xFF;
|
|
|
|
} while (key1 != 0x7E);
|
2006-01-01 14:28:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cKbdRemote::Action(void)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
while (Running()) {
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t Command = ReadKeySequence();
|
2006-01-01 14:28:47 +01:00
|
|
|
if (Command) {
|
|
|
|
if (rawMode || !Put(Command)) {
|
|
|
|
int func = MapCodeToFunc(Command);
|
|
|
|
if (func)
|
|
|
|
Put(KBDKEY(func));
|
|
|
|
}
|
2002-12-08 14:30:32 +01:00
|
|
|
}
|
|
|
|
}
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|