summaryrefslogtreecommitdiff
path: root/remote.c
blob: cc8024423eb24b3ead48e077104335ca07679f71 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * 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 4.1 2019/05/13 13:01:44 kls Exp $
 */

#include "remote.h"
#include <fcntl.h>
#define __STDC_FORMAT_MACROS // Required for format specifiers
#include <inttypes.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include "tools.h"

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

#define INITTIMEOUT   10000 // ms
#define REPEATTIMEOUT  1000 // ms

eKeys cRemote::keys[MaxKeys];
int cRemote::in = 0;
int cRemote::out = 0;
cTimeMs cRemote::repeatTimeout(-1);
cRemote *cRemote::learning = NULL;
char *cRemote::unknownCode = NULL;
cMutex cRemote::mutex;
cCondVar cRemote::keyPressed;
const char *cRemote::keyMacroPlugin = NULL;
const char *cRemote::callPlugin = NULL;
bool cRemote::enabled = true;
time_t cRemote::lastActivity = 0;

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

cRemote::~cRemote()
{
  Remotes.Del(this, false);
  free(name);
}

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

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

bool cRemote::Initialize(void)
{
  if (Ready()) {
     char *NewCode = NULL;
     eKeys Key = Get(INITTIMEOUT, &NewCode);
     if (Key != kNone || NewCode)
        return true;
     }
  return false;
}

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

bool cRemote::Put(eKeys Key, bool AtFront)
{
  if (Key != kNone) {
     cMutexLock MutexLock(&mutex);
     if (in != out && (keys[out] & k_Repeat) && (Key & k_Release))
        Clear();
     int d = out - in;
     if (d <= 0)
        d = MaxKeys + d;
     if (d - 1 > 0) {
        if (AtFront) {
           if (--out < 0)
              out = MaxKeys - 1;
           keys[out] = Key;
           }
        else {
           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) {
     keyMacroPlugin = km->Plugin();
     cMutexLock MutexLock(&mutex);
     for (int i = km->NumKeys(); --i > 0; ) {
         if (!Put(km->Macro()[i], true))
            return false;
         }
     }
  return true;
}

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

bool cRemote::Put(const char *Code, bool Repeat, bool Release)
{
  if (learning && this != learning)
     return false;
  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;
}

bool cRemote::CallPlugin(const char *Plugin)
{
  cMutexLock MutexLock(&mutex);
  if (!callPlugin) {
     callPlugin = Plugin;
     Put(k_Plugin);
     return true;
     }
  return false;
}

const char *cRemote::GetPlugin(void)
{
  cMutexLock MutexLock(&mutex);
  const char *p = keyMacroPlugin;
  if (p)
     keyMacroPlugin = NULL;
  else {
     p = callPlugin;
     callPlugin = NULL;
     }
  return p;
}

bool cRemote::HasKeys(void)
{
  cMutexLock MutexLock(&mutex);
  return in != out && !(keys[out] & k_Repeat);
}

eKeys cRemote::Get(int WaitMs, char **UnknownCode)
{
  for (;;) {
      cMutexLock MutexLock(&mutex);
      if (in != out) {
         eKeys k = keys[out];
         if (++out >= MaxKeys)
            out = 0;
         if ((k & k_Repeat) != 0)
            repeatTimeout.Set(REPEATTIMEOUT);
         TriggerLastActivity();
         return enabled ? k : kNone;
         }
      else if (!WaitMs || !keyPressed.TimedWait(mutex, WaitMs) && repeatTimeout.TimedOut())
         return kNone;
      else if (learning && UnknownCode && unknownCode) {
         *UnknownCode = unknownCode;
         unknownCode = NULL;
         return kNone;
         }
      }
}

void cRemote::TriggerLastActivity(void)
{
  lastActivity = time(NULL);
}

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

cRemotes Remotes;

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

struct tKbdMap {
  eKbdFunc func;
  uint64_t code;
  };

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

bool cKbdRemote::kbdAvailable = false;
bool cKbdRemote::rawMode = false;

cKbdRemote::cKbdRemote(void)
:cRemote("KBD")
,cThread("KBD remote control")
{
  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);
     }
  kbdAvailable = true;
  systemIsUtf8 = !cCharSetConv::SystemCharacterTable() || strcmp(cCharSetConv::SystemCharacterTable(), "UTF-8") == 0;
  Start();
}

cKbdRemote::~cKbdRemote()
{
  kbdAvailable = false;
  Cancel(3);
  tcsetattr(STDIN_FILENO, TCSANOW, &savedTm);
}

void cKbdRemote::SetRawMode(bool RawMode)
{
  rawMode = RawMode;
}

uint64_t cKbdRemote::MapFuncToCode(int Func)
{
  for (tKbdMap *p = KbdMap; p->func != kfNone; p++) {
      if (p->func == Func)
         return p->code;
      }
  return (Func <= 0xFF) ? Func : 0;
}

int cKbdRemote::MapCodeToFunc(uint64_t Code)
{
  for (tKbdMap *p = KbdMap; p->func != kfNone; p++) {
      if (p->code == Code)
         return p->func;
      }
  if (Code <= 0xFF)
     return Code;
  return kfNone;
}

void cKbdRemote::PutKey(uint64_t Code, bool Repeat, bool Release)
{
  if (rawMode || (!Put(Code, Repeat, Release) && !IsLearning())) {
     if (int func = MapCodeToFunc(Code))
        Put(KBDKEY(func), Repeat, Release);
     }
}

int cKbdRemote::ReadKey(void)
{
  cPoller Poller(STDIN_FILENO);
  if (Poller.Poll(Setup.RcRepeatDelta * 3 / 2)) {
     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;
}

uint64_t cKbdRemote::ReadKeySequence(void)
{
  uint64_t k = 0;
  int key1;

  if ((key1 = ReadKey()) >= 0) {
     k = key1;
     if (systemIsUtf8 && (key1 & 0xC0) == 0xC0) {
        char bytes[4] = { 0 };
        bytes[0] = key1;
        int bytescount = 1;
        if ((key1 & 0xF0) == 0xF0)
           bytescount = 3;
        else if ((key1 & 0xE0) == 0xE0)
           bytescount = 2;
        for (int i = 0; i < bytescount; i++) {
            if ((key1 = ReadKey()) >= 0)
               bytes[i + 1] = key1;
            }
        k = Utf8CharGet(bytes);
        if (k > 0xFF)
           k = 0;
        }
     else 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
                       case 0x5B: // strange, may apparently occur
                            do {
                               if ((key1 = ReadKey()) < 0)
                                  break; // Sequence ends here
                               k <<= 8;
                               k |= key1 & 0xFF;
                               } while (key1 != 0x7E);
                            break;
                       default: ;
                       }
                     }
                  break;
             default: ;
             }
           }
        }
     }
  return k;
}

void cKbdRemote::Action(void)
{
  cTimeMs FirstTime;
  cTimeMs LastTime;
  uint64_t FirstCommand = 0;
  uint64_t LastCommand = 0;
  bool Delayed = false;
  bool Repeat = false;

  while (Running()) {
        uint64_t Command = ReadKeySequence();
        if (Command) {
           if (Command == LastCommand) {
              // If two keyboard events with the same command come in without an intermediate
              // timeout, this is a long key press that caused the repeat function to kick in:
              Delayed = false;
              FirstCommand = 0;
              if (FirstTime.Elapsed() < (uint)Setup.RcRepeatDelay)
                 continue; // repeat function kicks in after a short delay
              if (LastTime.Elapsed() < (uint)Setup.RcRepeatDelta)
                 continue; // skip same keys coming in too fast
              PutKey(Command, true);
              Repeat = true;
              LastTime.Set();
              }
           else if (Command == FirstCommand) {
              // If the same command comes in twice with an intermediate timeout, we
              // need to delay the second command to see whether it is going to be
              // a repeat function or a separate key press:
              Delayed = true;
              }
           else {
              // This is a totally new key press, so we accept it immediately:
              PutKey(Command);
              Delayed = false;
              FirstCommand = Command;
              FirstTime.Set();
              }
           }
        else if (Repeat) {
           // Timeout after a repeat function, so we generate a 'release':
           PutKey(LastCommand, false, true);
           Repeat = false;
           }
        else if (Delayed && FirstCommand) {
           // Timeout after two normal key presses of the same key, so accept the
           // delayed key:
           PutKey(FirstCommand);
           Delayed = false;
           FirstCommand = 0;
           FirstTime.Set();
           }
        else if (FirstCommand && FirstTime.Elapsed() > (uint)Setup.RcRepeatDelay) {
           // Don't wait too long for that second key press:
           Delayed = false;
           FirstCommand = 0;
           }
        LastCommand = Command;
        }
}