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.
|
|
|
|
*
|
2002-12-08 14:30:32 +01:00
|
|
|
* $Id: remote.c 1.34 2002/12/08 13:37:13 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
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
eKeys cRemote::keys[MaxKeys];
|
|
|
|
int cRemote::in = 0;
|
|
|
|
int cRemote::out = 0;
|
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;
|
2002-12-01 10:48:08 +01:00
|
|
|
const char *cRemote::plugin = NULL;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
bool cRemote::Put(eKeys Key)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
keys[in] = Key;
|
|
|
|
if (++in >= MaxKeys)
|
|
|
|
in = 0;
|
|
|
|
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) {
|
2002-12-01 10:48:08 +01:00
|
|
|
plugin = km->Plugin();
|
2002-10-27 15:46:30 +01:00
|
|
|
for (int i = 1; i < MAXKEYSINMACRO; i++) {
|
|
|
|
if (km->Macro()[i] != kNone) {
|
|
|
|
if (!Put(km->Macro()[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
bool cRemote::Put(uint64 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
else if (!WaitMs || !keyPressed.TimedWait(mutex, WaitMs)) {
|
|
|
|
if (learning && UnknownCode) {
|
|
|
|
*UnknownCode = unknownCode;
|
|
|
|
unknownCode = NULL;
|
|
|
|
}
|
|
|
|
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-09-29 13:40:45 +02:00
|
|
|
cKbdRemote::cKbdRemote(void)
|
|
|
|
:cRemote("KBD")
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-12-08 14:30:32 +01:00
|
|
|
active = false;
|
|
|
|
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-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-08 14:30:32 +01:00
|
|
|
active = false;
|
|
|
|
Cancel(3);
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|
|
|
|
|
2002-09-29 13:40:45 +02:00
|
|
|
void cKbdRemote::Action(void)
|
2000-07-15 12:39:20 +02:00
|
|
|
{
|
2002-09-29 13:40:45 +02:00
|
|
|
dsyslog("KBD remote control thread started (pid=%d)", getpid());
|
|
|
|
cPoller Poller(STDIN_FILENO);
|
2002-12-08 14:30:32 +01:00
|
|
|
active = true;
|
|
|
|
while (active) {
|
|
|
|
if (Poller.Poll(100)) {
|
|
|
|
uint64 Command = 0;
|
|
|
|
uint i = 0;
|
|
|
|
int t0 = time_ms();
|
|
|
|
while (active && i < sizeof(Command)) {
|
|
|
|
uchar ch;
|
|
|
|
int r = read(STDIN_FILENO, &ch, 1);
|
|
|
|
if (r == 1) {
|
|
|
|
Command <<= 8;
|
|
|
|
Command |= ch;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (r == 0) {
|
|
|
|
// don't know why, but sometimes special keys that start with
|
|
|
|
// 0x1B ('ESC') cause a short gap between the 0x1B and the rest
|
|
|
|
// of their codes, so we'll need to wait some 100ms to see if
|
|
|
|
// there is more coming up - or whether this really is the 'ESC'
|
|
|
|
// key (if somebody knows how to clean this up, please let me know):
|
|
|
|
if (Command == 0x1B && time_ms() - t0 < 100)
|
|
|
|
continue;
|
|
|
|
if (Command)
|
|
|
|
Put(Command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-29 13:40:45 +02:00
|
|
|
dsyslog("KBD remote control thread ended (pid=%d)", getpid());
|
2000-07-15 12:39:20 +02:00
|
|
|
}
|