keys.c 8.84 KB
Newer Older
Klaus Schmidinger's avatar
Klaus Schmidinger committed
1 2 3 4 5 6
/*
 * keys.c: Remote control Key handling
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
7
 * $Id: keys.c 4.0 2012/12/04 12:52:52 kls Exp $
Klaus Schmidinger's avatar
Klaus Schmidinger committed
8 9 10
 */

#include "keys.h"
Klaus Schmidinger's avatar
Klaus Schmidinger committed
11
#include "plugin.h"
Klaus Schmidinger's avatar
Klaus Schmidinger committed
12 13

static tKey keyTable[] = { // "Up" and "Down" must be the first two keys!
Klaus Schmidinger's avatar
Klaus Schmidinger committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
                    { kUp,            trNOOP("Key$Up")          },
                    { kDown,          trNOOP("Key$Down")        },
                    { kMenu,          trNOOP("Key$Menu")        },
                    { kOk,            trNOOP("Key$Ok")          },
                    { kBack,          trNOOP("Key$Back")        },
                    { kLeft,          trNOOP("Key$Left")        },
                    { kRight,         trNOOP("Key$Right")       },
                    { kRed,           trNOOP("Key$Red")         },
                    { kGreen,         trNOOP("Key$Green")       },
                    { kYellow,        trNOOP("Key$Yellow")      },
                    { kBlue,          trNOOP("Key$Blue")        },
                    { k0,                    "0"                },
                    { k1,                    "1"                },
                    { k2,                    "2"                },
                    { k3,                    "3"                },
                    { k4,                    "4"                },
                    { k5,                    "5"                },
                    { k6,                    "6"                },
                    { k7,                    "7"                },
                    { k8,                    "8"                },
                    { k9,                    "9"                },
                    { kInfo,          trNOOP("Key$Info")        },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
36
                    { kPlayPause,     trNOOP("Key$Play/Pause")  },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
                    { kPlay,          trNOOP("Key$Play")        },
                    { kPause,         trNOOP("Key$Pause")       },
                    { kStop,          trNOOP("Key$Stop")        },
                    { kRecord,        trNOOP("Key$Record")      },
                    { kFastFwd,       trNOOP("Key$FastFwd")     },
                    { kFastRew,       trNOOP("Key$FastRew")     },
                    { kNext,          trNOOP("Key$Next")        },
                    { kPrev,          trNOOP("Key$Prev")        },
                    { kPower,         trNOOP("Key$Power")       },
                    { kChanUp,        trNOOP("Key$Channel+")    },
                    { kChanDn,        trNOOP("Key$Channel-")    },
                    { kChanPrev,      trNOOP("Key$PrevChannel") },
                    { kVolUp,         trNOOP("Key$Volume+")     },
                    { kVolDn,         trNOOP("Key$Volume-")     },
                    { kMute,          trNOOP("Key$Mute")        },
                    { kAudio,         trNOOP("Key$Audio")       },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
53
                    { kSubtitles,     trNOOP("Key$Subtitles")   },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
54 55 56 57 58 59
                    { kSchedule,      trNOOP("Key$Schedule")    },
                    { kChannels,      trNOOP("Key$Channels")    },
                    { kTimers,        trNOOP("Key$Timers")      },
                    { kRecordings,    trNOOP("Key$Recordings")  },
                    { kSetup,         trNOOP("Key$Setup")       },
                    { kCommands,      trNOOP("Key$Commands")    },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
60
                    { kUser0,         trNOOP("Key$User0")       },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
61 62 63 64 65 66 67 68 69 70 71 72
                    { kUser1,         trNOOP("Key$User1")       },
                    { kUser2,         trNOOP("Key$User2")       },
                    { kUser3,         trNOOP("Key$User3")       },
                    { kUser4,         trNOOP("Key$User4")       },
                    { kUser5,         trNOOP("Key$User5")       },
                    { kUser6,         trNOOP("Key$User6")       },
                    { kUser7,         trNOOP("Key$User7")       },
                    { kUser8,         trNOOP("Key$User8")       },
                    { kUser9,         trNOOP("Key$User9")       },
                    { kNone,                 ""                 },
                    { k_Setup,               "_Setup"           },
                    { kNone,                 NULL               },
Klaus Schmidinger's avatar
Klaus Schmidinger committed
73 74
                  };

Klaus Schmidinger's avatar
Klaus Schmidinger committed
75
// --- cKey ------------------------------------------------------------------
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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

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++) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
127 128 129 130 131
         const char *n = k->name;
         const char *p = strchr(n, '$');
         if (p)
            n = p + 1;
         if (strcasecmp(n, Name) == 0)
Klaus Schmidinger's avatar
Klaus Schmidinger committed
132 133 134 135 136 137
            return k->type;
         }
     }
  return kNone;
}

Klaus Schmidinger's avatar
Klaus Schmidinger committed
138
const char *cKey::ToString(eKeys Key, bool Translate)
Klaus Schmidinger's avatar
Klaus Schmidinger committed
139 140
{
  for (tKey *k = keyTable; k->name; k++) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
141 142 143 144 145 146 147 148 149
      if (k->type == Key) {
         const char *n = k->name;
         if (Translate)
            n = tr(n);
         const char *p = strchr(n, '$');
         if (p)
            n = p + 1;
         return n;
         }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
150 151 152 153
      }
  return NULL;
}

Klaus Schmidinger's avatar
Klaus Schmidinger committed
154
// --- cKeys -----------------------------------------------------------------
Klaus Schmidinger's avatar
Klaus Schmidinger committed
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

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);
}
Klaus Schmidinger's avatar
Klaus Schmidinger committed
198

Klaus Schmidinger's avatar
Klaus Schmidinger committed
199
// --- cKeyMacro -------------------------------------------------------------
Klaus Schmidinger's avatar
Klaus Schmidinger committed
200 201 202

cKeyMacro::cKeyMacro(void)
{
Klaus Schmidinger's avatar
Klaus Schmidinger committed
203
  numKeys = 0;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
204
  for (int i = 0; i < MAXKEYSINMACRO; i++)
Klaus Schmidinger's avatar
Klaus Schmidinger committed
205
      macro[i] = kNone; // for compatibility with old code that doesn't know about NumKeys()
Klaus Schmidinger's avatar
Klaus Schmidinger committed
206 207 208 209 210 211
  plugin = NULL;
}

cKeyMacro::~cKeyMacro()
{
  free(plugin);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
212 213 214 215 216 217
}

bool cKeyMacro::Parse(char *s)
{
  int n = 0;
  char *p;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
218 219
  char *strtok_next;
  while ((p = strtok_r(s, " \t", &strtok_next)) != NULL) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
220
        if (n < MAXKEYSINMACRO) {
Klaus Schmidinger's avatar
Klaus Schmidinger committed
221 222 223 224 225 226 227 228 229
           if (*p == '@') {
              if (plugin) {
                 esyslog("ERROR: only one @plugin allowed per macro");
                 return false;
                 }
              if (!n) {
                 esyslog("ERROR: @plugin can't be first in macro");
                 return false;
                 }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
230
              macro[n] = k_Plugin;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
231 232 233 234
              if (n < MAXKEYSINMACRO) {
                 plugin = strdup(p + 1);
                 if (!cPluginManager::GetPlugin(plugin)) {
                    esyslog("ERROR: unknown plugin '%s'", plugin);
Klaus Schmidinger's avatar
Klaus Schmidinger committed
235 236
                    // this is not a fatal error - plugins may or may not be loaded
                    macro[--n] = kNone; // makes sure the key doesn't cause any side effects
Klaus Schmidinger's avatar
Klaus Schmidinger committed
237 238 239 240 241 242 243 244 245 246 247 248 249
                    }
                 }
              else {
                 esyslog("ERROR: key macro too long");
                 return false;
                 }
              }
           else {
              macro[n] = cKey::FromString(p);
              if (macro[n] == kNone) {
                 esyslog("ERROR: unknown key '%s'", p);
                 return false;
                 }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
250 251 252 253 254 255 256 257 258
              }
           n++;
           s = NULL;
           }
        else {
           esyslog("ERROR: key macro too long");
           return false;
           }
        }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
259 260 261
  if (n < 2)
     esyslog("ERROR: empty key macro"); // non fatal
  numKeys = n;
Klaus Schmidinger's avatar
Klaus Schmidinger committed
262 263 264
  return true;
}

Klaus Schmidinger's avatar
Klaus Schmidinger committed
265
// --- cKeyMacros ------------------------------------------------------------
Klaus Schmidinger's avatar
Klaus Schmidinger committed
266 267 268 269 270

cKeyMacros KeyMacros;

const cKeyMacro *cKeyMacros::Get(eKeys Key)
{
Klaus Schmidinger's avatar
Klaus Schmidinger committed
271 272 273 274 275 276
  if (Key != kNone) {
     for (cKeyMacro *k = First(); k; k = Next(k)) {
         if (*k->Macro() == Key)
            return k;
         }
     }
Klaus Schmidinger's avatar
Klaus Schmidinger committed
277 278
  return NULL;
}