Home

Dokumentation

Impressum

Dokumentation VDR
 

Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

keys.c

Go to the documentation of this file.
00001 /*
00002  * keys.c: Remote control Key handling
00003  *
00004  * See the main source file 'vdr.c' for copyright information and
00005  * how to reach the author.
00006  *
00007  * $Id: keys.c 1.4 2002/11/30 16:01:37 kls Exp $
00008  */
00009 
00010 #include "keys.h"
00011 #include "plugin.h"
00012 
00013 static tKey keyTable[] = { // "Up" and "Down" must be the first two keys!
00014                     { kUp,            "Up"         },
00015                     { kDown,          "Down"       },
00016                     { kMenu,          "Menu"       },
00017                     { kOk,            "Ok"         },
00018                     { kBack,          "Back"       },
00019                     { kLeft,          "Left"       },
00020                     { kRight,         "Right"      },
00021                     { kRed,           "Red"        },
00022                     { kGreen,         "Green"      },
00023                     { kYellow,        "Yellow"     },
00024                     { kBlue,          "Blue"       },
00025                     { k0,             "0"          },
00026                     { k1,             "1"          },
00027                     { k2,             "2"          },
00028                     { k3,             "3"          },
00029                     { k4,             "4"          },
00030                     { k5,             "5"          },
00031                     { k6,             "6"          },
00032                     { k7,             "7"          },
00033                     { k8,             "8"          },
00034                     { k9,             "9"          },
00035                     { kPlay,          "Play"       },
00036                     { kPause,         "Pause"      },
00037                     { kStop,          "Stop"       },
00038                     { kRecord,        "Record"     },
00039                     { kFastFwd,       "FastFwd"    },
00040                     { kFastRew,       "FastRew"    },
00041                     { kPower,         "Power"      },
00042                     { kChanUp,        "Channel+"   },
00043                     { kChanDn,        "Channel-"   },
00044                     { kVolUp,         "Volume+"    },
00045                     { kVolDn,         "Volume-"    },
00046                     { kMute,          "Mute"       },
00047                     { kSchedule,      "Schedule"   },
00048                     { kChannels,      "Channels"   },
00049                     { kTimers,        "Timers"     },
00050                     { kRecordings,    "Recordings" },
00051                     { kSetup,         "Setup"      },
00052                     { kCommands,      "Commands"   },
00053                     { kUser1,         "User1"      },
00054                     { kUser2,         "User2"      },
00055                     { kUser3,         "User3"      },
00056                     { kUser4,         "User4"      },
00057                     { kUser5,         "User5"      },
00058                     { kUser6,         "User6"      },
00059                     { kUser7,         "User7"      },
00060                     { kUser8,         "User8"      },
00061                     { kUser9,         "User9"      },
00062                     { kNone,          ""           },
00063                     { k_Setup,        "_Setup"     },
00064                     { kNone,          NULL         },
00065                   };
00066 
00067 // -- cKey -------------------------------------------------------------------
00068 
00069 cKey::cKey(void)
00070 {
00071   remote = code = NULL;
00072   key = kNone;
00073 }
00074 
00075 cKey::cKey(const char *Remote, const char *Code, eKeys Key)
00076 {
00077   remote = strdup(Remote);
00078   code = strdup(Code);
00079   key = Key;
00080 }
00081 
00082 cKey::~cKey()
00083 {
00084   free(remote);
00085   free(code);
00086 }
00087 
00088 bool cKey::Parse(char *s)
00089 {
00090   char *p = strchr(s, '.');
00091   if (p) {
00092      *p++ = 0;
00093      remote = strdup(s);
00094      char *q = strpbrk(p, " \t");
00095      if (q) {
00096         *q++ = 0;
00097         key = FromString(p);
00098         if (key != kNone) {
00099            q = skipspace(q);
00100            if (*q) {
00101               code = strdup(q);
00102               return true;
00103               }
00104            }
00105         }
00106      }
00107   return false;
00108 }
00109 
00110 bool cKey::Save(FILE *f)
00111 {
00112   return fprintf(f, "%s.%-10s %s\n", remote, ToString(key), code) > 0;
00113 }
00114 
00115 eKeys cKey::FromString(const char *Name)
00116 {
00117   if (Name) {
00118      for (tKey *k = keyTable; k->name; k++) {
00119          if (strcasecmp(k->name, Name) == 0)
00120             return k->type;
00121          }
00122      }
00123   return kNone;
00124 }
00125 
00126 const char *cKey::ToString(eKeys Key)
00127 {
00128   for (tKey *k = keyTable; k->name; k++) {
00129       if (k->type == Key)
00130          return k->name;
00131       }
00132   return NULL;
00133 }
00134 
00135 // -- cKeys ------------------------------------------------------------------
00136 
00137 cKeys Keys;
00138 
00139 bool cKeys::KnowsRemote(const char *Remote)
00140 {
00141   if (Remote) {
00142      for (cKey *k = First(); k; k = Next(k)) {
00143          if (strcmp(Remote, k->Remote()) == 0)
00144             return true;
00145          }
00146      }
00147   return false;
00148 }
00149 
00150 eKeys cKeys::Get(const char *Remote, const char *Code)
00151 {
00152   if (Remote && Code) {
00153      for (cKey *k = First(); k; k = Next(k)) {
00154          if (strcmp(Remote, k->Remote()) == 0 && strcmp(Code, k->Code()) == 0)
00155             return k->Key();
00156          }
00157      }
00158   return kNone;
00159 }
00160 
00161 const char *cKeys::GetSetup(const char *Remote)
00162 {
00163   if (Remote) {
00164      for (cKey *k = First(); k; k = Next(k)) {
00165          if (strcmp(Remote, k->Remote()) == 0 && k->Key() == k_Setup)
00166             return k->Code();
00167          }
00168      }
00169   return NULL;
00170 }
00171 
00172 void cKeys::PutSetup(const char *Remote, const char *Setup)
00173 {
00174   if (!GetSetup(Remote))
00175      Add(new cKey(Remote, Setup, k_Setup));
00176   else
00177      esyslog("ERROR: called PutSetup() for %s, but setup has already been defined!", Remote);
00178 }
00179 
00180 // -- cKeyMacro --------------------------------------------------------------
00181 
00182 cKeyMacro::cKeyMacro(void)
00183 {
00184   for (int i = 0; i < MAXKEYSINMACRO; i++)
00185       macro[i] = kNone;
00186   plugin = NULL;
00187 }
00188 
00189 cKeyMacro::~cKeyMacro()
00190 {
00191   free(plugin);
00192 }
00193 
00194 bool cKeyMacro::Parse(char *s)
00195 {
00196   int n = 0;
00197   char *p;
00198   while ((p = strtok(s, " \t")) != NULL) {
00199         if (n < MAXKEYSINMACRO) {
00200            if (*p == '@') {
00201               if (plugin) {
00202                  esyslog("ERROR: only one @plugin allowed per macro");
00203                  return false;
00204                  }
00205               if (!n) {
00206                  esyslog("ERROR: @plugin can't be first in macro");
00207                  return false;
00208                  }
00209               macro[n++] = k_Plugin;
00210               if (n < MAXKEYSINMACRO) {
00211                  macro[n] = kOk;
00212                  plugin = strdup(p + 1);
00213                  if (!cPluginManager::GetPlugin(plugin)) {
00214                     esyslog("ERROR: unknown plugin '%s'", plugin);
00215                     return false;
00216                     }
00217                  }
00218               else {
00219                  esyslog("ERROR: key macro too long");
00220                  return false;
00221                  }
00222               }
00223            else {
00224               macro[n] = cKey::FromString(p);
00225               if (macro[n] == kNone) {
00226                  esyslog("ERROR: unknown key '%s'", p);
00227                  return false;
00228                  }
00229               }
00230            n++;
00231            s = NULL;
00232            }
00233         else {
00234            esyslog("ERROR: key macro too long");
00235            return false;
00236            }
00237         }
00238   if (n < 2) {
00239      esyslog("ERROR: empty key macro");
00240      }
00241   return true;
00242 }
00243 
00244 // -- cKeyMacros -------------------------------------------------------------
00245 
00246 cKeyMacros KeyMacros;
00247 
00248 const cKeyMacro *cKeyMacros::Get(eKeys Key)
00249 {
00250   for (cKeyMacro *k = First(); k; k = Next(k)) {
00251       if (*k->Macro() == Key)
00252          return k;
00253       }
00254   return NULL;
00255 }

Generated on Wed Feb 5 23:30:09 2003 for VDR by doxygen1.3-rc2