File: remote.c

package info (click to toggle)
vdr-plugin-remote 0.3.3-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 212 kB
  • ctags: 118
  • sloc: ansic: 1,337; makefile: 105; sh: 79
file content (795 lines) | stat: -rw-r--r-- 21,778 bytes parent folder | download
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/*
 * Remote Control plugin for the Video Disk Recorder
 *
 * remote.c: main source file
 *
 * See the README file for copyright information and how to reach the author.
 */


#include <vdr/plugin.h>
#ifdef REMOTE_FEATURE_LIRC
#include <vdr/lirc.h>
#endif
#include <stdio.h>
#include <sys/fcntl.h>
#include <getopt.h>
#include <termios.h>
#include <linux/input.h>
#include "i18n.h"
#include "remote.h"
#ifdef REMOTE_FEATURE_TCPIP
#include "remotetcp.h"
#endif
#include "ttystatus.h"


#define NUMREMOTES      10        // maximum number of remote control devices
 
#define KEYMAP_DEVICE_AV7110   "/proc/av7110_ir"

static const char *VERSION        = "0.3.3";
static const char *DESCRIPTION    = "Remote control";



// ---------------------------------------------------------------------------
cRemoteGeneric::cRemoteGeneric(const char *name, int f, char *d)
    :cRemote(name)
// ---------------------------------------------------------------------------
{
    fh = f;
    device = d;
    polldelay     = 40;   // ms
    repeatdelay   = 350;  // ms
    repeatfreq    = 100;  // ms
    repeattimeout = 500;  // ms
}


// ---------------------------------------------------------------------------
cRemoteGeneric::~cRemoteGeneric()
// ---------------------------------------------------------------------------
{
    Cancel();
}


// ---------------------------------------------------------------------------
bool cRemoteGeneric::Put(uint64 Code, bool Repeat, bool Release)
// ---------------------------------------------------------------------------
{
    return cRemote::Put(Code, Repeat, Release);
}


// ---------------------------------------------------------------------------
void cRemoteGeneric::Action(void)
// ---------------------------------------------------------------------------
{
#if VDRVERSNUM <= 10317
    int now, first = 0, last = 0;
#else
    cTimeMs first, rate, timeout;
#endif
    uint64 code, lastcode = INVALID_KEY;
    bool repeat = false;

    for (;;)
    {
        if (polldelay)
            usleep(1000*polldelay);

        code = getKey();
        if (code == INVALID_KEY)
        {
            esyslog("error reading '%s'\n", device);
            usleep(100000*polldelay);
            continue;
        }

#if VDRVERSNUM <= 10317
        now = time_ms();
#endif

        if (keyPressed(code))
        {
            // key down
#if VDRVERSNUM <= 10317
            if (now - last > repeattimeout)
#else
            if (timeout.TimedOut())
#endif
            {
                if (repeat)
                {
                    Put(lastcode,false,true);
                    DSYSLOG("%s: timeout %016llx\n", device, code);
                    repeat = false;
                }
                lastcode = INVALID_KEY;
            }

            if (code != lastcode)
            {
                Put(code);
                DSYSLOG("%s: press %016llx\n", device, code);
                lastcode = code;
#if VDRVERSNUM <= 10317
                last = first = now;
#else
                first.Set(repeatdelay);
                rate.Set(repeatfreq);
                timeout.Set(repeattimeout);
#endif
                repeat = false;
            }
            else
            {
#if VDRVERSNUM <= 10317
                if (now - first < repeatdelay || now - last < repeatfreq)
#else
                if (!first.TimedOut() || !rate.TimedOut())
#endif
                    continue;
                Put(code,true);
                DSYSLOG("%s: repeat %016llx\n", device, code);
#if VDRVERSNUM <= 10317
                last = now;
#else
                rate.Set(repeatfreq);
                timeout.Set(repeattimeout);
#endif
                repeat = true;
            }
        }
        else
        {
            // key up
            if (repeat)
            {
                Put(lastcode,false,true);
                DSYSLOG("%s: release %016llx\n", device, lastcode);
                repeat = false;
            }
            lastcode = INVALID_KEY;
        }
    }/* for */
}


/*****************************************************************************/


// ---------------------------------------------------------------------------
//
// Try to identify input device.
//
// input:   fh - file handle
//          name - device name
//
// returns:  0 - unknown device
//           1 - full-featured card
//           2 - other DVB card, e.g. budget receiver
//          -1 - error
//
// ---------------------------------------------------------------------------
int identifyInputDevice(const int fh, char *name)
// ---------------------------------------------------------------------------
{
    char description[256];

    // check name of input device
    if (ioctl(fh, EVIOCGNAME(sizeof(description)), description) < 0)
        return -1;

    dsyslog("device %s: %s", name, description);

    if (!strcmp(description, "DVB on-card IR receiver"))
        return 1;

    if (strstr(description, "DVB") || strstr(description, "dvb"))
        return 2;

    return 0;
}


// ---------------------------------------------------------------------------
bool cRemoteDevInput::loadKeymap(const char *devname, uint32_t options)
// ---------------------------------------------------------------------------
{
    int fh;
    uint16_t keymap[2+256];
    int n;

    fh = open(devname, O_RDWR);
    if (fh < 0)
    {
        int err = errno;
        esyslog("%s: unable to open '%s': %s", Name(), devname, strerror(err));
        EOSD(tr("%s: %s"), devname, strerror(err));
        return false;
    }

    keymap[0] = (uint16_t) options;
    keymap[1] = (uint16_t) (options >> 16);

    for (int i = 1; i <= 256; i++)
        keymap[1+i] = i;

    n = write(fh, keymap, sizeof keymap);

    close(fh);

    if (n == sizeof keymap)
    {
        dsyslog("%s: keymap loaded '%s' flags %.8x", Name(), devname, options);
        return true;
    }
    else
    {
        esyslog("%s: error uploading keymap to '%s'", Name(), devname);
        MSG_ERROR(tr("Error uploading keymap"));
        return false;
    }
}


// ---------------------------------------------------------------------------
cRemoteDevInput::cRemoteDevInput(const char *name, int f, char *d)
    :cRemoteGeneric(name, f, d)
// ---------------------------------------------------------------------------
{
    testMode = false;

    Start();

    // autorepeat
#define BITS_PER_LONG	(sizeof(unsigned long) * 8)
    unsigned long data[EV_MAX];
    memset(data, 0, sizeof data);
    ioctl(f, EVIOCGBIT(0,EV_MAX), data); 
    if ( data[EV_REP/BITS_PER_LONG] & (1 << EV_REP%BITS_PER_LONG) )
    {
        // autorepeat driver
        dsyslog("%s: autorepeat supported", name);
        polldelay = 0;
    }
    else
    {
        // non-autorepeat drivers
        polldelay = repeatdelay = repeatfreq = repeattimeout = 0;
    }

    // grab device if possible (kernel 2.6)
#ifndef EVIOCGRAB
    // required if an old /usr/include/linux/input.h is used with a new kernel :-(
#define EVIOCGRAB  _IOW('E', 0x90, int)   /* Grab/Release device */
#endif
    data[0] = 1;
    if (ioctl(f, EVIOCGRAB, data) == 0)
        dsyslog("%s: exclusive access granted", name);

    // setup keymap
    const char *setupStr = GetSetup();
    char kDevname[256]; memset(kDevname, 0, sizeof kDevname);
    uint32_t kOptions = 0;
    int kAddr = -1;

    if (setupStr)
    {
        sscanf(setupStr, "%s %x %d", kDevname, &kOptions, &kAddr);
        if (kAddr != -1)
            kOptions |= ((kAddr << 16) | 0x4000);
    }

    if (kDevname[0])
    {    
        loadKeymap(kDevname, kOptions);
    }
}


// ---------------------------------------------------------------------------
bool cRemoteDevInput::Initialize()
// ---------------------------------------------------------------------------
{
    testMode = true;

    char setupStr[256]; memset (setupStr, 0, sizeof setupStr);
		
    // load keymap for full-featured cards
    if (identifyInputDevice(fh, device) == 1)
    {
        char *kDevname = "/proc/av7110_ir";
        uint32_t kOptions;
        int kAddr = -1;
        int i, n;

	for (n = 0; n < 2; n++)
        {
            if (n == 0)
            {
                MSG_INFO(tr("Press any key to use pre-loaded keymap"));
                for (testKey = 0, i = 0; testKey == 0 && i < 35; i++)
                    usleep(200000);
                if (testKey != 0)
                {
                    MSG_INFO(tr("User-supplied keymap will be used"));
                    break;
                }
            }

            kOptions = 0x0000;
            MSG_INFO(tr("Remote control test - press and hold down any key"));
            loadKeymap(kDevname, kOptions);
            for (testKey = 0, i = 0; testKey == 0 && i < 10; i++)
                usleep(200000);
            if (testKey != 0)
            {
                for (i = 0; i < 64; i++)
                {
                    int a = (i & 0x1f);
                    loadKeymap(kDevname, kOptions | 0x4000 | (a << 16));
                    usleep(400000);
                    testKey = 0;
                    usleep(400000);
                    if (testKey != 0)
                    {
                        kAddr = a;
			break;
		    }
		}
                MSG_INFO(tr("RC5 protocol detected"));
                sprintf (setupStr, "%s %.8x %d", kDevname, kOptions, kAddr);
                break;
            }

            kOptions = 0x8000;
            loadKeymap(kDevname, kOptions);
            for (testKey = 0, i = 0; testKey == 0 && i < 10; i++)
                usleep(200000);
            if (testKey != 0)
            {
                for (i = 0; i < 64; i++)
                {
                    int a = (i & 0x1f);
                    loadKeymap(kDevname, kOptions | 0x4000 | (a << 16));
                    usleep(400000);
                    testKey = 0;
                    usleep(400000);
                    if (testKey != 0)
                    {
                        kAddr = a;
                        break;
                    }
                }
                MSG_INFO(tr("RC5 protocol detected (inverted signal)"));
                sprintf (setupStr, "%s %.8x %d", kDevname, kOptions, kAddr);
                break;
            }

            kOptions = 0x0001;
            loadKeymap(kDevname, kOptions);
            for (testKey = 0, i = 0; testKey == 0 && i < 10; i++)
                usleep(200000);
            if (testKey != 0)
            {
                MSG_INFO(tr("RCMM protocol detected"));
                sprintf (setupStr, "%s %.8x %d", kDevname, kOptions, kAddr);
                break;
            }

            kOptions = 0x8001;
            loadKeymap(kDevname, kOptions);
            for (testKey = 0, i = 0; testKey == 0 && i < 10; i++)
                usleep(200000);
            if (testKey != 0)
            {
                MSG_INFO(tr("RCMM protocol detected (inverted signal)"));
                sprintf (setupStr, "%s %.8x %d", kDevname, kOptions, kAddr);
                break;
            }
        }/* for */

        if (testKey == 0)
        {
            MSG_ERROR(tr("No remote control detected"));
            esyslog("%s: no remote control detected", device);
            usleep(5000000);
            testMode = false;
            return false;
        }
    }/* DVB card */

    if (setupStr[0])
        PutSetup(setupStr);

    testMode = false;
    return true;
}


// ---------------------------------------------------------------------------
uint64 cRemoteDevInput::getKey(void)
// ---------------------------------------------------------------------------
{
    struct input_event ev;
    int n;
    uint64 code;

    n = read(fh, &ev, sizeof ev);
    if (n == sizeof ev)
    {
        if (ev.value)
            ev.value = 1;
        code = ((uint64)ev.value << 32) | ((uint64)ev.type << 16) | (uint64)ev.code;
    }
    else
        code = INVALID_KEY;

    if (testMode)
    {
        testKey = code;
        code = 0ULL;
    }

    return code;
}


// ---------------------------------------------------------------------------
bool cRemoteDevInput::keyPressed(uint64 code)
// ---------------------------------------------------------------------------
{
    return (code & 0xFFFF00000000ULL);
}


/*****************************************************************************/


#ifdef REMOTE_FEATURE_LIRCOLD
// ---------------------------------------------------------------------------
uint64 cRemoteDevLirc::getKey(void)
// ---------------------------------------------------------------------------
{
    unsigned long code;
    int n;

    n = read(fh, &code, sizeof code);
    if (n != sizeof code)
        return INVALID_KEY;
    else
        return (uint64)code;
}


// ---------------------------------------------------------------------------
bool cRemoteDevLirc::keyPressed(uint64 code)
// ---------------------------------------------------------------------------
{
    return (code & 0x80);
}
#endif // REMOTE_FEATURE_LIRCOLD


/*****************************************************************************/


// ---------------------------------------------------------------------------
cRemoteDevTty::cRemoteDevTty(const char *name, int f, char *d)
    :cRemoteGeneric(name, f, d)
// ---------------------------------------------------------------------------
{
    struct termios t;

    if (!tcgetattr(f, &tm))
    {
        t = tm;   
        t.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(f, TCSANOW, &t);
    }
    polldelay     = 0;
    repeattimeout = 80;
    if (f >= 0)
        Start();
}


// ---------------------------------------------------------------------------
cRemoteDevTty::~cRemoteDevTty()
// ---------------------------------------------------------------------------
{
    tcsetattr(fh, TCSANOW, &tm);
}


// ---------------------------------------------------------------------------
uint64 cRemoteDevTty::getKey(void)
// ---------------------------------------------------------------------------
{
    int n;
    uint64 code = 0;

    n = read(fh, &code, sizeof code);
    return (n > 0) ? code : INVALID_KEY;
}


// ---------------------------------------------------------------------------
bool cRemoteDevTty::keyPressed(uint64 code)
// ---------------------------------------------------------------------------
{
    return true;
}


// ---------------------------------------------------------------------------
bool cRemoteDevTty::Put(uint64 Code, bool Repeat, bool Release)
// ---------------------------------------------------------------------------
{
    bool rc = cRemote::Put(Code, Repeat, Release);

    if (!rc && Code <= 0xff)
        rc = cRemote::Put(KBDKEY(Code));

    return rc;
}



/*****************************************************************************/
class cPluginRemote : public cPlugin
/*****************************************************************************/
{
private:
  int  devcnt;
  char devtyp[NUMREMOTES];
  char *devnam[NUMREMOTES];
  int  fh[NUMREMOTES];
public:
  cPluginRemote(void);
  virtual ~cPluginRemote();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Start(void);
};


// ---------------------------------------------------------------------------
cPluginRemote::cPluginRemote(void)
// ---------------------------------------------------------------------------
{
    for (int i = 0; i < NUMREMOTES; i++)
    {
        devtyp[i] = '\0';
        devnam[i] = NULL;
        fh[i] = -1;
    }
    devcnt = 0;
}


// ---------------------------------------------------------------------------
cPluginRemote::~cPluginRemote()
// ---------------------------------------------------------------------------
{
    // must not delete any remotes, see PLUGINS.html!

    for (int i = 0; i < devcnt; i++)
    {
        if (fh[i] >= 0)
            close(fh[i]);
        fh[i] = -1;
    }
    devcnt = 0;
}


// ---------------------------------------------------------------------------
const char *cPluginRemote::CommandLineHelp(void)
// ---------------------------------------------------------------------------
{
    return "  -i dev,   --input=dev    kernel input device (/dev/input/...)\n"
#ifdef REMOTE_FEATURE_LIRC
           "  -l dev,   --lirc=dev     lirc device (/dev/lircd)\n"
#endif
#ifdef REMOTE_FEATURE_LIRCOLD
           "  -l dev,   --lirc=dev     kernel lirc device (/dev/lirc)\n"
#endif
#ifdef REMOTE_FEATURE_TCPIP
           "  -p tcp:n, --port=tcp:n   listen on tcp port <n>\n"
#endif
           "  -t dev,   --tty=dev      tty device\n"
           "  -T dev,   --TTY=dev      tty device with 'OSD'\n";
}


// ---------------------------------------------------------------------------
bool cPluginRemote::ProcessArgs(int argc, char *argv[])
// ---------------------------------------------------------------------------
{
    static struct option long_options[] =
            { { "input", required_argument, NULL, 'i' },
              { "lirc",  required_argument, NULL, 'l' },
              { "port",  required_argument, NULL, 'p' },
              { "tty",   required_argument, NULL, 't' },
              { "TTY",   required_argument, NULL, 'T' },
              { NULL } };
    int c;

    while ((c = getopt_long(argc, argv, "i:l:p:t:T:", long_options, NULL)) != -1)
    {
        switch (c)
        {
          case 'i':
          case 'l':
          case 'p':
          case 't':
          case 'T':
              if (devcnt >= NUMREMOTES)
              {
                  esyslog("%s: too many remotes", Name());
                  return false;
              }
              devtyp[devcnt] = c;
              devnam[devcnt] = optarg;
	      devcnt++;
              break;

          default:
              esyslog("%s: invalid argument", Name());
              return false;
        }
    }

    return true;
}


// ---------------------------------------------------------------------------
bool cPluginRemote::Start(void)
// ---------------------------------------------------------------------------
{
    bool ok = false;

    // translations
    RegisterI18n(remotePhrases);

    // no device specified by the user, set default
    if (devcnt == 0)
    {
        devtyp[0] = 'i';
        devnam[0] = "autodetect";
        devcnt = 1;
    }

    /* probe eventX devices */
    for (int i = 0; i < devcnt; i++)
    {
        if (devtyp[i] == 'i' && strcmp(devnam[i], "autodetect") == 0)
        {
            char nam[80];

            for (int j = 0; ; j++)
            {
                sprintf(nam, "/dev/input/event%d", j);
                fh[i] = open(nam, O_RDONLY);
                if (fh[i] < 0)
                {
                    switch (errno)
                    {
                        case EACCES:   // permission denied: ignore device
                            continue;
                        default:       // no more devices: stop scanning
                            break;
                    }
                    break;
                }
		
                if (identifyInputDevice(fh[i], nam) >= 1)
                {
                    // found DVB card receiver
                    devnam[i] = strdup(nam);
                    close(fh[i]);
                    break;
                }

                // unknown device, try next one
                close(fh[i]);
            } // for j
        } // if autodetect

        // use default device if nothing could be identified
        if (devtyp[i] == 'i' && strcmp(devnam[i], "autodetect") == 0)
            devnam[i] = "/dev/input/event0";
    } // for i

    for (int i = 0; i < devcnt; i++)
    {
        switch (devtyp[i])
        {
#ifdef REMOTE_FEATURE_LIRC
            case 'l':
                fh[i] = access(devnam[i], R_OK);
                break;
#endif
            case 'p':
                fh[i] = 0;
                break;

            case 'T':
                fh[i] = open(devnam[i], O_RDWR);
                break;

            default:
                fh[i] = open(devnam[i], O_RDONLY);
                break;
        }

        if (fh[i] < 0)
        {
            esyslog("%s: unable to open '%s': %s",
                    Name(), devnam[i], strerror(errno));
            EOSD(tr("%s: %s"), devnam[i], strerror(errno));
            continue;
        }
	
        // at least, one device opened successfully
        ok = true;
        dsyslog("%s: using '%s'", Name(), devnam[i]);

        // build name for remote.conf
        char nam[25];
        char *cp = strrchr(devnam[i], '/');
	if (cp)
            sprintf (nam, "%s-%s", Name(), cp+1);
        else
            sprintf (nam, "%s-%s", Name(), devnam[i]);

	switch (devtyp[i])
        {
            case 'i':
                new cRemoteDevInput(nam,fh[i],devnam[i]);
                break;

#ifdef REMOTE_FEATURE_LIRC
            case 'l':
                new cLircRemote(devnam[i]); // use vdr's lirc code
                break;
#endif
#ifdef REMOTE_FEATURE_LIRCOLD
            case 'l':
                new cRemoteDevLirc(nam,fh[i],devnam[i]);
                break;
#endif

#ifdef REMOTE_FEATURE_TCPIP
            case 'p':
                new cTcpRemote(nam, -1, devnam[i]);
                break;
#endif
            case 'T':
                new cTtyStatus(fh[i]);
                // fall thru
            case 't':
                new cRemoteDevTty(nam,fh[i],devnam[i]);
                break;
        }
    } // for
    
    if (!ok)
        esyslog("%s: fatal error - unable to open input device", Name());

    return ok;
}


/*****************************************************************************/


VDRPLUGINCREATOR(cPluginRemote); // Don't touch this!