]> git.vomp.tv Git - vompclient.git/blob - remote.cc
More compiler warning fixes
[vompclient.git] / remote.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "remote.h"
22 #include "wremoteconfig.h"
23 #include "i18n.h"
24 #include "log.h"
25 #include "vdr.h"
26 #include "wtabbar.h"
27
28 Remote* Remote::instance = NULL;
29
30 #ifndef _MSC_VER
31
32 const ULONG Remote::NOLEARNMODE;
33 // Not buttons
34 const  UCHAR Remote::NA_LEARN;
35 const  UCHAR Remote::NA_NONE;
36 const  UCHAR Remote::NA_UNKNOWN;
37 const  UCHAR Remote::NA_SIGNAL;
38 const  UCHAR Remote::DF_UP;
39 const  UCHAR Remote::DF_DOWN;
40 const  UCHAR Remote::DF_LEFT;
41 const  UCHAR Remote::DF_RIGHT;
42
43 // Problem common buttons
44 const  UCHAR Remote::VOLUMEUP;
45 const  UCHAR Remote::VOLUMEDOWN;
46 const  UCHAR Remote::CHANNELUP;
47 const  UCHAR Remote::CHANNELDOWN;
48
49 // Common buttons
50 const  UCHAR Remote::ZERO;
51 const  UCHAR Remote::ONE;
52 const  UCHAR Remote::TWO;
53 const  UCHAR Remote::THREE;
54 const  UCHAR Remote::FOUR;
55 const  UCHAR Remote::FIVE;
56 const  UCHAR Remote::SIX;
57 const  UCHAR Remote::SEVEN;
58 const  UCHAR Remote::EIGHT;
59 const  UCHAR Remote::NINE;
60 const  UCHAR Remote::POWER;
61 const  UCHAR Remote::GO;
62 const  UCHAR Remote::BACK;
63 const  UCHAR Remote::MENU;
64 const  UCHAR Remote::RED;
65 const  UCHAR Remote::GREEN;
66 const  UCHAR Remote::YELLOW;
67 const  UCHAR Remote::BLUE;
68 const  UCHAR Remote::MUTE;
69 const  UCHAR Remote::RADIO;
70 const  UCHAR Remote::REVERSE;
71 const  UCHAR Remote::PLAY;
72 const  UCHAR Remote::FORWARD;
73 const  UCHAR Remote::RECORD;
74 const  UCHAR Remote::STOP;
75 const  UCHAR Remote::PAUSE;
76 const  UCHAR Remote::SKIPBACK;
77 const  UCHAR Remote::SKIPFORWARD;
78 const  UCHAR Remote::OK;
79
80 // Old remote only
81 const  UCHAR Remote::FULL;
82
83 // New remote only
84 const  UCHAR Remote::TV;
85 const  UCHAR Remote::VIDEOS;
86 const  UCHAR Remote::MUSIC;
87 const  UCHAR Remote::PICTURES;
88 const  UCHAR Remote::GUIDE;
89 const  UCHAR Remote::UP;
90 const  UCHAR Remote::DOWN;
91 const  UCHAR Remote::LEFT;
92 const  UCHAR Remote::RIGHT;
93 const  UCHAR Remote::PREVCHANNEL;
94 const  UCHAR Remote::STAR;
95 const  UCHAR Remote::HASH;
96
97 // Android only
98 const  UCHAR Remote::PLAYPAUSE;
99
100
101 // Remote types
102 const  UCHAR Remote::OLDREMOTE;
103 const  UCHAR Remote::NEWREMOTE;
104
105 #endif
106
107 Remote::Remote()
108 {
109   if (instance) return;
110   instance = this;
111   remoteType = OLDREMOTE;
112   learnmode = NOLEARNMODE;
113 }
114
115 Remote::~Remote()
116 {
117   instance = NULL;
118 }
119
120 Remote* Remote::getInstance()
121 {
122   return instance;
123 }
124
125 void Remote::setRemoteType(UCHAR newType)
126 {
127   if ((newType != OLDREMOTE) && (newType != NEWREMOTE)) return;
128   remoteType = newType;
129 }
130
131 void Remote::EnterLearningMode(UCHAR command)
132 {
133     learnmode = command; //Armed
134 }
135
136 void Remote::ResetToDefault()
137 {
138   translist.clear();
139   InitHWCListwithDefaults();
140 }
141
142
143 UCHAR Remote::TranslateHWCFixed(ULLONG code)
144 {
145   switch (code)
146   {
147     case DOWN:
148       return DOWN;
149     case UP:
150       return UP;
151     case LEFT:
152       return LEFT;
153     case RIGHT:
154       return RIGHT;
155     case DF_DOWN:
156       return DOWN;
157     case DF_UP:
158       return UP;
159     case DF_LEFT:
160       return LEFT;
161     case DF_RIGHT:
162       return RIGHT;
163     case MENU:
164       return MENU;
165     case BACK:
166       return BACK;
167     case OK:
168       return OK;
169     default:
170       return NA_UNKNOWN;
171   }
172 }
173
174 const char*Remote::HardcodedTranslateStr(UCHAR command)
175 {
176   switch (command)
177   {
178     case DOWN:
179       return tr("Down");
180     case UP:
181       return tr("Up");
182     case LEFT:
183       return tr("Left");
184     case RIGHT:
185       return tr("Right");
186     case MENU:
187       return tr("Menu");
188     case BACK:
189       return tr("Back");
190     case OK:
191       return tr("Ok");
192     default:
193       return NULL;
194   }
195 }
196
197 UCHAR Remote::TranslateHWCList(ULLONG code)
198 {
199   if (learnmode != NOLEARNMODE)
200   {
201     setHWCtoCommand(code, learnmode);
202     learnmode = NOLEARNMODE;
203     return NA_LEARN;
204   }
205   RemoteTranslationList::iterator it = translist.find(code);
206   if (it == translist.end())
207   {
208     return NA_UNKNOWN;
209   }
210   else
211   {
212     return it->second;
213   }
214 }
215
216 UCHAR Remote::TranslateHWC(ULLONG code)
217 {
218   UCHAR ret = TranslateHWCFixed(code);
219   if (ret == NA_UNKNOWN)
220   {
221     ret = TranslateHWCList(code);
222   }
223   else
224   {
225     learnmode = NOLEARNMODE;
226   }
227
228   if (ret == NA_UNKNOWN)
229   {
230     return NA_UNKNOWN;
231   }
232   return ret;
233 }
234
235 void Remote::setHWCtoCommand(ULLONG hcw, UCHAR command)
236 {
237   translist[hcw] = command;
238 }
239
240 void Remote::unsetHWC(ULLONG hcw)
241 {
242   translist.erase(hcw);
243 }
244
245 void Remote::LoadKeysConfig(VDR *vdr,const char *cfg)
246 {
247         ULONG number=0;
248         if (sscanf(cfg,"%ld",&number) != 1) return;
249         Log::getInstance()->log("Remote", Log::INFO, "Config General/Remote keys num keys %d",number);
250         char keybuf[1024];
251         for (ULONG i = 0; i < number; i++) {
252                 sprintf(keybuf, "RemoteKey%lu", i);
253                 const char *keytrans = vdr->configLoad("General", keybuf);
254                 if (keytrans) {
255                         ULONG ul1, ul2;
256                         ULONG uc;
257                         if (sscanf(keytrans, "%lXI%lXK%lX", &ul1, &ul2, &uc) == 3) {
258                                 translist[((ULLONG) ul1) | ((ULLONG) ul2) << 32] = (UCHAR) uc;
259                         }
260                         delete[] keytrans;
261                 }
262
263         }
264 }
265
266 void Remote::SaveKeysConfig()
267 {
268   int number=0;
269   char buffer[1024];
270   char keybuf[1024];
271   RemoteTranslationList::const_iterator it;
272   for (it = translist.begin(); it != translist.end(); it++)
273   {
274           sprintf(buffer,"%08lXI%08lXK%02X",
275                           (ULONG)it->first ,(ULONG) (it->first >> 32), it->second);
276           sprintf(keybuf,"RemoteKey%d",number);
277           VDR::getInstance()->configSave("General",keybuf,buffer);
278           number++;
279   }
280   sprintf(buffer,"%d",number);
281   VDR::getInstance()->configSave("General","RemoteKeyNum",buffer);
282 }
283
284
285 void Remote::InitHWCListwithDefaults()
286 {
287   translist[VOLUMEUP] = VOLUMEUP;
288   translist[VOLUMEDOWN] = VOLUMEDOWN;
289   translist[CHANNELUP] = CHANNELUP;
290   translist[CHANNELDOWN] = CHANNELDOWN;
291
292   // Common buttons
293   translist[ZERO] = ZERO;
294   translist[ONE] = ONE;
295   translist[TWO] = TWO;
296   translist[THREE] = THREE;
297   translist[FOUR] = FOUR;
298   translist[FIVE] = FIVE;
299   translist[SIX] = SIX;
300   translist[SEVEN] = SEVEN;
301   translist[EIGHT] = EIGHT;
302   translist[NINE] = NINE;
303   translist[POWER] = POWER;
304   translist[GO] = GO;
305   translist[RED] = RED;
306   translist[GREEN] = GREEN;
307   translist[YELLOW] = YELLOW;
308   translist[BLUE] = BLUE;
309
310   translist[MUTE] = MUTE;
311   translist[RADIO] = RADIO;
312   translist[REVERSE] = REVERSE;
313   translist[FORWARD] = FORWARD;
314   translist[RECORD] = RECORD;
315   translist[STOP] = STOP;
316   translist[PAUSE] = PAUSE;
317   translist[PLAY] = PLAY;
318   translist[SKIPBACK] = SKIPBACK;
319   translist[SKIPFORWARD] = SKIPFORWARD;
320
321   // Old remote only
322   translist[FULL] = FULL;
323
324   // New remote only
325   translist[TV] = TV;
326   translist[VIDEOS] = VIDEOS;
327   translist[MUSIC] = MUSIC;
328   translist[PICTURES] = PICTURES;
329   translist[GUIDE] = GUIDE;
330   translist[PREVCHANNEL] = PREVCHANNEL;
331   translist[STAR] = STAR;
332   translist[HASH] = HASH;
333 }
334
335 const char *Remote::CommandDesc(UCHAR number)
336 {
337   switch (number)
338   {
339     case VOLUMEUP:
340       return tr("Volume Up");
341
342     case VOLUMEDOWN:
343       return tr("Volume Down");
344     case CHANNELUP:
345       return tr("Channel up");
346     case CHANNELDOWN:
347       return tr("Channel down");
348     case ZERO:
349       return "0";
350     case ONE:
351       return "1";
352     case TWO:
353       return "2";
354     case THREE:
355       return "3";
356     case FOUR:
357       return "4";
358     case FIVE:
359       return "5";
360     case SIX:
361       return "6";
362     case SEVEN:
363       return "7";
364     case EIGHT:
365       return "8";
366     case NINE:
367       return "9";
368     case POWER:
369       return tr("Power");
370     case GO:
371       return tr("Go");
372     case BACK:
373       return tr("Back");
374     case MENU:
375       return tr("Menu");
376     case RED:
377       return tr("Red");
378     case GREEN:
379       return tr("Green");
380     case YELLOW:
381       return tr("Yellow");
382     case BLUE:
383       return tr("Blue");
384     case MUTE:
385       return tr("Mute");
386     case RADIO:
387       return tr("Radio");
388     case REVERSE:
389       return tr("Reverse");
390     case PLAY:
391       return tr("Play");
392     case FORWARD:
393       return tr("Forward");
394     case RECORD:
395       return tr("Record");
396     case STOP:
397       return tr("Stop");
398     case PAUSE:
399       return tr("Pause");
400     case SKIPBACK:
401       return tr("Skip back");
402     case SKIPFORWARD:
403       return tr("Skip forward");
404     case OK:
405       return tr("Ok");
406     case FULL:
407       return tr("Fullscreen");
408     case TV:
409       return tr("TV");
410     case VIDEOS:
411       return tr("Videos");
412     case MUSIC:
413       return tr("Music");
414     case PICTURES:
415       return tr("Pictures");
416     case GUIDE:
417       return tr("Guide");
418     case UP:
419       return tr("Up");
420     case DOWN:
421       return tr("Down");
422     case LEFT:
423       return tr("Left");
424     case RIGHT:
425       return tr("Right");
426     case PREVCHANNEL:
427       return tr("Previous Channel");
428     case STAR:
429       return tr("Star");
430     case HASH:
431       return tr("Hash");
432     case PLAYPAUSE:
433        return tr("Play/Pause");
434
435     default:
436       return NULL;
437   }
438 }
439
440 char* Remote::HCWDesc(ULLONG hcw)
441 {
442   char *dest,*temp;
443   temp=(char*)CommandDesc((UCHAR)hcw);
444   if (temp != NULL)
445   {
446     dest=new char[strlen(temp)+1];
447     strcpy(dest,temp);
448   }
449   else
450   {
451     dest=new char[20];
452     sprintf(dest,"C:%lX",(ULONG)hcw);
453   }
454   return dest;
455 }
456
457 char *Remote::CommandTranslateStr(UCHAR command)
458 {
459   char *desc;
460   int length=5;//:+\t+0
461   int keys=0; //max 10;
462   char *commanddesc=(char*)CommandDesc(command);
463   if (commanddesc != NULL)
464   {
465     length+=strlen(commanddesc);
466   }
467   char *preassigneddesc=(char*)HardcodedTranslateStr(command);
468   if (preassigneddesc != NULL)
469   {
470     length+=strlen(preassigneddesc);
471   }
472
473   char *keydesc[10];
474   RemoteTranslationList::const_iterator it;
475   for (it = translist.begin(); it != translist.end(); it++)
476   {
477     if (it->second == command)
478     {
479       keydesc[keys] = HCWDesc(it->first);
480       length += strlen(keydesc[keys])+2;
481       keys ++;
482       if (keys == 10) break;
483     }
484   }
485
486   desc=new char [length];
487   char *current=desc;
488   if (commanddesc != NULL)
489   {
490     current+=sprintf(current,"%s:\t ",commanddesc);
491   }
492   else
493   {
494     current+=sprintf(current,":\t ");
495   }
496   if (preassigneddesc != NULL)
497   {
498     current+=sprintf(current,"%s\t",preassigneddesc);
499   }
500   else
501   {
502     current+=sprintf(current,"\t");
503   }
504   for (int i = 0;i < keys; i++)
505   {
506     current += sprintf(current,"%s, ",keydesc[i]);
507     delete [] keydesc[i];
508   }
509   return desc;
510 }
511
512 bool Remote::addOptionPagesToWTB(WTabBar *wtb)
513 {
514     WRemoteConfig* wrc = new WRemoteConfig();
515     wtb->addTab(tr("Remote Control"), wrc);
516     return true;
517 }
518
519 bool Remote::loadOptionsfromServer(VDR* vdr)
520 {
521    // Set remote keys
522   char * config;
523   config = vdr->configLoad("General", "RemoteKeyNum");
524
525   if (config)
526   {
527     Log::getInstance()->log("Remote", Log::INFO, "Config General/Remote keys load");
528     LoadKeysConfig(vdr,config);
529     delete[] config;
530   }
531   else
532   {
533     Log::getInstance()->log("Remote", Log::INFO, "Config General/Remote keys not found");
534     InitHWCListwithDefaults();
535   }
536   return true;
537 }
538
539 bool Remote::saveOptionstoServer()
540 {
541     SaveKeysConfig();
542     return true;
543 }