1
0
mirror of https://github.com/VDR4Arch/vdr.git synced 2023-10-10 13:36:52 +02:00
vdr/remote.c

181 lines
3.5 KiB
C
Raw Permalink Normal View History

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-10-27 15:46:30 +01:00
* $Id: remote.c 1.30 2002/10/27 15:15:58 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 <termios.h>
#include <unistd.h>
2000-07-15 12:39:20 +02:00
#if defined REMOTE_KBD
#include <ncurses.h>
#endif
2000-02-19 13:36:48 +01:00
#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;
bool cRemote::learning = false;
char *cRemote::unknownCode = NULL;
cMutex cRemote::mutex;
cCondVar cRemote::keyPressed;
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
{
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);
if ((Key & k_Release) != 0)
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) {
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-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 ------------------------------------------------------------
2002-09-29 13:40:45 +02:00
#if defined REMOTE_KBD
2000-10-08 09:25:20 +02:00
2002-09-29 13:40:45 +02:00
cKbdRemote::cKbdRemote(void)
:cRemote("KBD")
2000-07-15 12:39:20 +02:00
{
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
{
2000-12-08 16:23:32 +01:00
Cancel();
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);
for (;;) {//XXX
int Command = getch();
if (Command != EOF)
Put(Command);
Poller.Poll(100);
2000-10-08 09:25:20 +02:00
}
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
}
#endif