summaryrefslogtreecommitdiff
path: root/keys.c
blob: 0d91d673002864f9475d0bc3961cde27904e2916 (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
/*
 * keys.c: Remote control Key handling
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: keys.c 1.1 2002/09/29 09:56:51 kls Exp $
 */

#include "keys.h"

static tKey keyTable[] = { // "Up" and "Down" must be the first two keys!
                    { kUp,            "Up"      },
                    { kDown,          "Down"    },
                    { kMenu,          "Menu"    },
                    { kOk,            "Ok"      },
                    { kBack,          "Back"    },
                    { kLeft,          "Left"    },
                    { kRight,         "Right"   },
                    { kRed,           "Red"     },
                    { kGreen,         "Green"   },
                    { kYellow,        "Yellow"  },
                    { kBlue,          "Blue"    },
                    { k0,             "0"       },
                    { k1,             "1"       },
                    { k2,             "2"       },
                    { k3,             "3"       },
                    { k4,             "4"       },
                    { k5,             "5"       },
                    { k6,             "6"       },
                    { k7,             "7"       },
                    { k8,             "8"       },
                    { k9,             "9"       },
                    { kPower,         "Power"   },
                    { kVolUp,         "Volume+" },
                    { kVolDn,         "Volume-" },
                    { kMute,          "Mute"    },
                    { kNone,          ""        },
                    { k_Setup,        "_Setup"  },
                    { kNone,          NULL      },
                  };

// -- cKey -------------------------------------------------------------------

cKey::cKey(void)
{
  remote = code = NULL;
  key = kNone;
}

cKey::cKey(const char *Remote, const char *Code, eKeys Key)
{
  remote = strdup(Remote);
  code = strdup(Code);
  key = Key;
}

cKey::~cKey()
{
  free(remote);
  free(code);
}

bool cKey::Parse(char *s)
{
  char *p = strchr(s, '.');
  if (p) {
     *p++ = 0;
     remote = strdup(s);
     char *q = strpbrk(p, " \t");
     if (q) {
        *q++ = 0;
        key = FromString(p);
        if (key != kNone) {
           q = skipspace(q);
           if (*q) {
              code = strdup(q);
              return true;
              }
           }
        }
     }
  return false;
}

bool cKey::Save(FILE *f)
{
  return fprintf(f, "%s.%-10s %s\n", remote, ToString(key), code) > 0;
}

eKeys cKey::FromString(const char *Name)
{
  if (Name) {
     for (tKey *k = keyTable; k->name; k++) {
         if (strcasecmp(k->name, Name) == 0)
            return k->type;
         }
     }
  return kNone;
}

const char *cKey::ToString(eKeys Key)
{
  for (tKey *k = keyTable; k->name; k++) {
      if (k->type == Key)
         return k->name;
      }
  return NULL;
}

// -- cKeys ------------------------------------------------------------------

cKeys Keys;

bool cKeys::KnowsRemote(const char *Remote)
{
  if (Remote) {
     for (cKey *k = First(); k; k = Next(k)) {
         if (strcmp(Remote, k->Remote()) == 0)
            return true;
         }
     }
  return false;
}

eKeys cKeys::Get(const char *Remote, const char *Code)
{
  if (Remote && Code) {
     for (cKey *k = First(); k; k = Next(k)) {
         if (strcmp(Remote, k->Remote()) == 0 && strcmp(Code, k->Code()) == 0)
            return k->Key();
         }
     }
  return kNone;
}

const char *cKeys::GetSetup(const char *Remote)
{
  if (Remote) {
     for (cKey *k = First(); k; k = Next(k)) {
         if (strcmp(Remote, k->Remote()) == 0 && k->Key() == k_Setup)
            return k->Code();
         }
     }
  return NULL;
}

void cKeys::PutSetup(const char *Remote, const char *Setup)
{
  if (!GetSetup(Remote))
     Add(new cKey(Remote, Setup, k_Setup));
  else
     esyslog("ERROR: called PutSetup() for %s, but setup has already been defined!", Remote);
}