]> git.vomp.tv Git - vompclient.git/blob - inputman.cc
Start work on local JSON config system. Start using it with Input classes
[vompclient.git] / inputman.cc
1 /*
2     Copyright 2020 Chris Tallon; 2012 Marten Richter
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, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include "log.h"
22 #include "wremoteconfig.h"
23 #include "wtabbar.h"
24 #ifdef VOMP_PLATFORM_RASPBERRY
25 #include "inputlinux.h"
26 #include "inputcec.h"
27 #endif
28 #include "inputudp.h"
29 #include "inputlirc.h"
30 #ifdef WIN32
31 #include "inputwin.h"
32 #endif
33 #include "i18n.h"
34 #include "input.h"
35
36 #include "inputman.h"
37
38 InputMan* InputMan::instance = NULL;
39
40 InputMan::InputMan()
41 {
42   instance = this;
43 }
44
45 InputMan::~InputMan()
46 {
47   instance = NULL;
48 }
49
50 InputMan* InputMan::getInstance()
51 {
52   return instance;
53 }
54
55 bool InputMan::init()
56 {
57   bool ret;
58   bool oneOK{};
59
60 #ifdef VOMP_PLATFORM_RASPBERRY
61   inputLinux = new InputLinux();
62   ret = inputLinux->init();
63   if (ret)
64     oneOK = true;
65   else
66     { delete inputLinux; inputLinux = NULL; }
67
68   bool cecEnabled = true;
69   Config::getInstance()->getBool("input", "mod_cec_enabled", cecEnabled);
70   if (cecEnabled)
71   {
72     inputCEC = new InputCEC();
73     ret = inputCEC->init();
74     if (ret)
75       oneOK = true;
76     else
77       { delete inputCEC; inputCEC = NULL; }
78   }
79 #endif
80
81   bool udpEnabled = true;
82   Config::getInstance()->getBool("input", "mod_udp_enabled", udpEnabled);
83   if (udpEnabled)
84   {
85     inputUDP = new InputUDP();
86     ret = inputUDP->init();
87     if (ret)
88       oneOK = true;
89     else
90       { delete inputUDP; inputUDP = NULL; }
91   }
92
93   bool lircEnabled = false;
94   Config::getInstance()->getBool("input", "mod_lirc_enabled", lircEnabled);
95   if (lircEnabled)
96   {
97     inputLirc = new InputLIRC();
98     ret = inputLirc->init();
99     if (ret)
100       oneOK = true;
101     else
102       { delete inputLirc; inputLirc = NULL; }
103   }
104
105 #ifdef WIN32
106   inputWin = new InputWin();
107   ret = inputWin->init();
108   if (ret)
109     oneOK = true;
110   else
111     { delete inputWin; inputWin = NULL; }
112 #endif
113
114   if (!oneOK)
115   {
116     Log::getInstance()->log("InputMan", Log::CRIT, "InputMan could not init any input module");
117     return false;
118   }
119
120   initted = true;
121   return true;
122 }
123
124 bool InputMan::start()
125 {
126   Log::getInstance()->log("InputMan", Log::DEBUG, "Start");
127
128   bool oneOK{};
129
130 #ifdef VOMP_PLATFORM_RASPBERRY
131   if (inputLinux && inputLinux->start()) oneOK = true;
132 #endif
133
134   if (inputUDP && inputUDP->start()) oneOK = true;
135
136   if (inputLirc)
137   {
138     std::string lircIP;
139     bool checkA = Config::getInstance()->getString("input", "lirc_ip", lircIP);
140     int lircPort = 8765;
141     bool checkB = Config::getInstance()->getInt("input", "lirc_port", lircPort);
142
143     if (!checkA || !checkB)
144     {
145       delete inputLirc;
146       inputLirc = NULL;
147     }
148     else
149     {
150       if (inputLirc->start(lircIP, lircPort))
151       {
152         oneOK = true;
153       }
154       else
155       {
156         delete inputLirc;
157         inputLirc = NULL;
158       }
159     }
160   }
161
162   if (!oneOK)
163     Log::getInstance()->log("InputMan", Log::CRIT, "InputMan could not start any input module");
164
165   return oneOK;
166 }
167
168 void InputMan::stop()
169 {
170   Log::getInstance()->log("InputMan", Log::DEBUG, "Stop called");
171
172 #ifdef VOMP_PLATFORM_RASPBERRY
173   if (inputLinux) inputLinux->stop();
174 #endif
175   if (inputUDP) inputUDP->stop();
176   if (inputLirc) inputLirc->stop();
177 }
178
179 void InputMan::shutdown()
180 {
181   Log::getInstance()->log("InputMan", Log::DEBUG, "Shutdown start");
182
183 #ifdef VOMP_PLATFORM_RASPBERRY
184
185   if (inputLinux)
186   {
187     Log::getInstance()->log("InputMan", Log::DEBUG, "Shutdown start - Linux");
188     inputLinux->stop();
189     inputLinux->shutdown();
190     delete inputLinux;
191     inputLinux = NULL;
192   }
193
194   if (inputCEC)
195   {
196     Log::getInstance()->log("InputMan", Log::DEBUG, "Shutdown start - CEC");
197     inputCEC->shutdown();
198     delete inputCEC;
199     inputCEC = NULL;
200   }
201
202 #endif
203
204   if (inputUDP)
205   {
206     Log::getInstance()->log("InputMan", Log::DEBUG, "Shutdown start - UDP");
207     inputUDP->stop();
208     inputUDP->shutdown();
209     delete inputUDP;
210     inputUDP = NULL;
211   }
212
213   if (inputLirc)
214   {
215     Log::getInstance()->log("InputMan", Log::DEBUG, "Shutdown start - LIRC");
216     inputLirc->stop();
217     inputLirc->shutdown();
218     delete inputLirc;
219     inputLirc = NULL;
220   }
221
222   initted = false;
223 }
224
225 bool InputMan::mayHaveFewButtons()
226 {
227   // 052 returned true if remotelinux was in effect - linux or CEC. What was it for?
228
229   if (inputLinux || inputCEC) return true;
230   return false;
231 }
232
233 bool InputMan::handlesVolume()
234 {
235 #ifdef VOMP_PLATFORM_RASPBERRY
236   if (!inputCEC) return false;
237   return inputCEC->handlesVolume();
238 #else
239   return false;
240 #endif
241 }
242
243 void InputMan::volumeUp()
244 {
245 #ifdef VOMP_PLATFORM_RASPBERRY
246         if (inputCEC) inputCEC->volumeUp();
247 #endif
248 }
249
250 void InputMan::volumeDown()
251 {
252 #ifdef VOMP_PLATFORM_RASPBERRY
253         if (inputCEC) inputCEC->volumeDown();
254 #endif
255 }
256
257 void InputMan::volumeMute()
258 {
259 #ifdef VOMP_PLATFORM_RASPBERRY
260   if (inputCEC) inputCEC->volumeMute();
261 #endif
262 }
263
264 void InputMan::changePowerState(bool powerOn)
265 {
266 #ifdef VOMP_PLATFORM_RASPBERRY
267   if (inputCEC) inputCEC->changePowerState(powerOn);
268 #endif
269 }
270
271 bool InputMan::addOptionsToPanes(int panenumber, Options* options, WOptionPane* pane)
272 {
273 #ifdef VOMP_PLATFORM_RASPBERRY
274   if (inputLinux) inputLinux->addOptionsToPanes(panenumber, options, pane);
275   if (inputCEC) inputCEC->addOptionsToPanes(panenumber, options, pane);
276 #endif
277
278   return true; // FIXME
279 }
280
281 bool InputMan::addOptionPagesToWTB(WTabBar *wtb)
282 {
283   WRemoteConfig* wrc = new WRemoteConfig();
284   wtb->addTab(tr("Remote Control"), wrc);
285
286   return true; // FIXME
287 }
288
289 bool InputMan::loadOptionsFromServer(VDR* vdr)
290 {
291 #ifdef VOMP_PLATFORM_RASPBERRY
292   if (inputLinux) inputLinux->loadOptionsFromServer(vdr);
293   if (inputCEC) inputCEC->loadOptionsFromServer(vdr);
294 #endif
295
296   return true; // FIXME
297 }
298
299 bool InputMan::saveOptionstoServer()
300 {
301 #ifdef VOMP_PLATFORM_RASPBERRY
302   if (inputLinux) inputLinux->saveOptionstoServer();
303   if (inputCEC) inputCEC->saveOptionstoServer();
304 #endif
305   return true; // FIXME
306 }
307
308 const char* InputMan::getVompKeyName(UCHAR number)
309 {
310   switch (number)
311   {
312     case Input::VOLUMEUP:
313       return tr("Volume Up");
314     case Input::VOLUMEDOWN:
315       return tr("Volume Down");
316     case Input::CHANNELUP:
317       return tr("Channel up");
318     case Input::CHANNELDOWN:
319       return tr("Channel down");
320     case Input::ZERO:
321       return "0";
322     case Input::ONE:
323       return "1";
324     case Input::TWO:
325       return "2";
326     case Input::THREE:
327       return "3";
328     case Input::FOUR:
329       return "4";
330     case Input::FIVE:
331       return "5";
332     case Input::SIX:
333       return "6";
334     case Input::SEVEN:
335       return "7";
336     case Input::EIGHT:
337       return "8";
338     case Input::NINE:
339       return "9";
340     case Input::POWER:
341       return tr("Power");
342     case Input::GO:
343       return tr("Go");
344     case Input::BACK:
345       return tr("Back");
346     case Input::MENU:
347       return tr("Menu");
348     case Input::RED:
349       return tr("Red");
350     case Input::GREEN:
351       return tr("Green");
352     case Input::YELLOW:
353       return tr("Yellow");
354     case Input::BLUE:
355       return tr("Blue");
356     case Input::MUTE:
357       return tr("Mute");
358     case Input::RADIO:
359       return tr("Radio");
360     case Input::REVERSE:
361       return tr("Reverse");
362     case Input::PLAY:
363       return tr("Play");
364     case Input::FORWARD:
365       return tr("Forward");
366     case Input::RECORD:
367       return tr("Record");
368     case Input::STOP:
369       return tr("Stop");
370     case Input::PAUSE:
371       return tr("Pause");
372     case Input::SKIPBACK:
373       return tr("Skip back");
374     case Input::SKIPFORWARD:
375       return tr("Skip forward");
376     case Input::OK:
377       return tr("Ok");
378     case Input::FULL:
379       return tr("Fullscreen");
380     case Input::TV:
381       return tr("TV");
382     case Input::VIDEOS:
383       return tr("Videos");
384     case Input::MUSIC:
385       return tr("Music");
386     case Input::PICTURES:
387       return tr("Pictures");
388     case Input::GUIDE:
389       return tr("Guide");
390     case Input::UP:
391       return tr("Up");
392     case Input::DOWN:
393       return tr("Down");
394     case Input::LEFT:
395       return tr("Left");
396     case Input::RIGHT:
397       return tr("Right");
398     case Input::PREVCHANNEL:
399       return tr("Previous Channel");
400     case Input::STAR:
401       return tr("Star");
402     case Input::HASH:
403       return tr("Hash");
404     case Input::PLAYPAUSE:
405        return tr("Play/Pause");
406
407     default:
408       return NULL;
409   }
410 }
411
412 const UCHAR InputMan::getVompKeyNumber(const char* vompKeyName)
413 {
414   if      (!strcmp(vompKeyName, "VOLUMEUP")) return Input::VOLUMEUP;
415   else if (!strcmp(vompKeyName, "VOLUMEDOWN")) return Input::VOLUMEDOWN;
416   else if (!strcmp(vompKeyName, "CHANNELUP")) return Input::CHANNELUP;
417   else if (!strcmp(vompKeyName, "CHANNELDOWN")) return Input::CHANNELDOWN;
418   else if (!strcmp(vompKeyName, "ZERO")) return Input::ZERO;
419   else if (!strcmp(vompKeyName, "ONE")) return Input::ONE;
420   else if (!strcmp(vompKeyName, "TWO")) return Input::TWO;
421   else if (!strcmp(vompKeyName, "THREE")) return Input::THREE;
422   else if (!strcmp(vompKeyName, "FOUR")) return Input::FOUR;
423   else if (!strcmp(vompKeyName, "FIVE")) return Input::FIVE;
424   else if (!strcmp(vompKeyName, "SIX")) return Input::SIX;
425   else if (!strcmp(vompKeyName, "SEVEN")) return Input::SEVEN;
426   else if (!strcmp(vompKeyName, "EIGHT")) return Input::EIGHT;
427   else if (!strcmp(vompKeyName, "NINE")) return Input::NINE;
428   else if (!strcmp(vompKeyName, "POWER")) return Input::POWER;
429   else if (!strcmp(vompKeyName, "GO")) return Input::GO;
430   else if (!strcmp(vompKeyName, "BACK")) return Input::BACK;
431   else if (!strcmp(vompKeyName, "MENU")) return Input::MENU;
432   else if (!strcmp(vompKeyName, "RED")) return Input::RED;
433   else if (!strcmp(vompKeyName, "GREEN")) return Input::GREEN;
434   else if (!strcmp(vompKeyName, "YELLOW")) return Input::YELLOW;
435   else if (!strcmp(vompKeyName, "BLUE")) return Input::BLUE;
436   else if (!strcmp(vompKeyName, "MUTE")) return Input::MUTE;
437   else if (!strcmp(vompKeyName, "RADIO")) return Input::RADIO;
438   else if (!strcmp(vompKeyName, "REVERSE")) return Input::REVERSE;
439   else if (!strcmp(vompKeyName, "PLAY")) return Input::PLAY;
440   else if (!strcmp(vompKeyName, "FORWARD")) return Input::FORWARD;
441   else if (!strcmp(vompKeyName, "RECORD")) return Input::RECORD;
442   else if (!strcmp(vompKeyName, "STOP")) return Input::STOP;
443   else if (!strcmp(vompKeyName, "PAUSE")) return Input::PAUSE;
444   else if (!strcmp(vompKeyName, "SKIPBACK")) return Input::SKIPBACK;
445   else if (!strcmp(vompKeyName, "SKIPFORWARD")) return Input::SKIPFORWARD;
446   else if (!strcmp(vompKeyName, "OK")) return Input::OK;
447   else if (!strcmp(vompKeyName, "FULL")) return Input::FULL;
448   else if (!strcmp(vompKeyName, "TV")) return Input::TV;
449   else if (!strcmp(vompKeyName, "VIDEOS")) return Input::VIDEOS;
450   else if (!strcmp(vompKeyName, "MUSIC")) return Input::MUSIC;
451   else if (!strcmp(vompKeyName, "PICTURES")) return Input::PICTURES;
452   else if (!strcmp(vompKeyName, "GUIDE")) return Input::GUIDE;
453   else if (!strcmp(vompKeyName, "UP")) return Input::UP;
454   else if (!strcmp(vompKeyName, "DOWN")) return Input::DOWN;
455   else if (!strcmp(vompKeyName, "LEFT")) return Input::LEFT;
456   else if (!strcmp(vompKeyName, "RIGHT")) return Input::RIGHT;
457   else if (!strcmp(vompKeyName, "PREVCHANNEL")) return Input::PREVCHANNEL;
458   else if (!strcmp(vompKeyName, "STAR")) return Input::STAR;
459   else if (!strcmp(vompKeyName, "HASH")) return Input::HASH;
460   else if (!strcmp(vompKeyName, "PLAYPAUSE")) return Input::PLAYPAUSE;
461   else if (!strcmp(vompKeyName, "POWERON")) return Input::POWERON;
462   else if (!strcmp(vompKeyName, "POWEROFF")) return Input::POWEROFF;
463   else return NULL;
464 }
465
466 std::string InputMan::getHardCodedHardwareKeyNamesForVompKey(UCHAR vompKey)
467 {
468   // Go through each active Input class and get the hardware key name for vompKey
469
470   // which doesn't make any sense
471
472   std::string keyNames;
473
474 #ifdef VOMP_PLATFORM_RASPBERRY
475   if (inputLinux)
476   {
477     std::string k = inputLinux->getHardCodedHardwareKeyNamesForVompKey(vompKey);
478     if (k.size()) keyNames += k;
479   }
480
481   if (inputCEC)
482   {
483     std::string k = inputCEC->getHardCodedHardwareKeyNamesForVompKey(vompKey);
484     if (k.size()) { keyNames += ", "; keyNames += k; }
485   }
486 #endif
487
488   if (inputUDP)
489   {
490     std::string k = inputUDP->getHardCodedHardwareKeyNamesForVompKey(vompKey);
491     if (k.size()) { keyNames += ", "; keyNames += k; }
492   }
493
494   return keyNames;
495 }
496
497 std::string InputMan::getAllHardwareKeyNamesAssignedToVompKey(UCHAR vompKey)
498 {
499   std::string keyNames;
500
501 #ifdef VOMP_PLATFORM_RASPBERRY
502   if (inputLinux)
503   {
504     std::string k = inputLinux->getAllHardwareKeyNamesAssignedToVompKey(vompKey);
505     if (k.size()) keyNames += k;
506   }
507
508   if (inputCEC)
509   {
510     std::string k = inputCEC->getAllHardwareKeyNamesAssignedToVompKey(vompKey);
511     if (k.size()) { keyNames += ", "; keyNames += k; }
512   }
513 #endif
514
515   if (inputUDP)
516   {
517     std::string k = inputUDP->getAllHardwareKeyNamesAssignedToVompKey(vompKey);
518     if (k.size()) { keyNames += ", "; keyNames += k; }
519   }
520
521   return keyNames;
522 }
523
524 void InputMan::ResetToDefault()
525 {
526 #ifdef VOMP_PLATFORM_RASPBERRY
527
528   if (inputLinux) inputLinux->ResetToDefault();
529
530   if (inputCEC) inputCEC->ResetToDefault();
531
532 #endif
533
534   if (inputUDP) inputUDP->ResetToDefault();
535 }
536
537 void InputMan::EnterLearningMode(UCHAR vompKey)
538 {
539 #ifdef VOMP_PLATFORM_RASPBERRY
540   if (inputLinux) inputLinux->EnterLearningMode(vompKey);
541
542 //  if (inputCEC) inputCEC->EnterLearningMode();  FIXME - is there any such thing?
543 #endif
544 }
545
546 void InputMan::cancelLearnMode()
547 {
548 #ifdef VOMP_PLATFORM_RASPBERRY
549   if (inputLinux) inputLinux->cancelLearnMode();
550   if (inputCEC) inputCEC->cancelLearnMode();
551 #endif
552   if (inputUDP) inputUDP->cancelLearnMode();
553 }