summaryrefslogtreecommitdiff
path: root/remote.c
blob: 6078c26ba7e0012ac38133c8fa6fd2ed590dca94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * remote.c: General Remote Control handling
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: remote.c 1.30 2002/10/27 15:15:58 kls Exp $
 */

#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>

#if defined REMOTE_KBD
#include <ncurses.h>
#endif

#include "tools.h"

// --- cRemote ---------------------------------------------------------------

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;

cRemote::cRemote(const char *Name)
{
  name = Name ? strdup(Name) : NULL;
  Remotes.Add(this);
}

cRemote::~cRemote()
{
  free(name);
}

const char *cRemote::GetSetup(void)
{
  return Keys.GetSetup(Name());
}

void cRemote::PutSetup(const char *Setup)
{
  Keys.PutSetup(Name(), Setup);
}

void cRemote::Clear(void)
{
  cMutexLock MutexLock(&mutex);
  in = out = 0;
  if (learning) {
     free(unknownCode);
     unknownCode = NULL;
     }
}

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;
        }
     return false;
     }
  return true; // only a real key shall report an overflow!
}

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;
}

bool cRemote::Put(uint64 Code, bool Repeat, bool Release)
{
  char buffer[32];
  snprintf(buffer, sizeof(buffer), "%016LX", Code);
  return Put(buffer, Repeat, Release);
}

bool cRemote::Put(const char *Code, bool Repeat, bool Release)
{
  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);
     }
  if (learning) {
     free(unknownCode);
     unknownCode = strdup(Code);
     keyPressed.Broadcast();
     }
  return false;
}

eKeys cRemote::Get(int WaitMs, char **UnknownCode)
{
  for (;;) {
      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;
         }
      }
}

// --- cRemotes --------------------------------------------------------------

cRemotes Remotes;

// --- cKbdRemote ------------------------------------------------------------

#if defined REMOTE_KBD

cKbdRemote::cKbdRemote(void)
:cRemote("KBD")
{
  Start();
}

cKbdRemote::~cKbdRemote()
{
  Cancel();
}

void cKbdRemote::Action(void)
{
  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);
      }
  dsyslog("KBD remote control thread ended (pid=%d)", getpid());
}

#endif