]> git.vomp.tv Git - vompclient.git/blob - command.cc
Big code cleanup
[vompclient.git] / command.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "command.h"
22
23 Command* Command::instance = NULL;
24
25 Command::Command()
26 {
27   if (instance) return;
28   instance = this;
29   initted = 0;
30   isStandby = 0;
31   firstBoot = 1;
32 }
33
34 Command::~Command()
35 {
36   instance = NULL;
37 }
38
39 Command* Command::getInstance()
40 {
41   return instance;
42 }
43
44 int Command::init()
45 {
46   if (initted) return 0;
47   initted = 1;
48
49   logger = Log::getInstance();
50   viewman = ViewMan::getInstance();
51   remote = Remote::getInstance();
52
53   if (!logger || !viewman || !remote)
54   {
55     initted = 0;
56     return 0;
57   }
58
59   pthread_mutex_init(&masterLock, NULL);
60
61   return 1;
62 }
63
64 int Command::shutdown()
65 {
66   if (!initted) return 0;
67   initted = 0;
68   return 1;
69 }
70
71 void Command::stop()
72 {
73 //  VDR::getInstance()->cancelFindingServer();
74   irun = 0;
75 }
76
77 void Command::doWallpaper()
78 {
79   Video* video = Video::getInstance();
80
81   // Blue background
82   View* v = new View();
83   v->create(video->getScreenWidth(), video->getScreenHeight());
84   v->setBackgroundColour(Colour::VIDEOBLUE);
85   v->draw();
86   viewman->add(v);
87   viewman->updateView(v);
88   viewman->removeView(v);
89
90   // Wallpaper
91   wallpaper = new VWallpaper();
92   if (video->getFormat() == Video::PAL)
93   {
94     logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
95     wallpaper->init("/wallpaperPAL.jpg");
96   }
97   else
98   {
99     logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
100     wallpaper->init("/wallpaperNTSC.jpg");
101   }
102   wallpaper->draw();
103   viewman->add(wallpaper);
104   viewman->updateView(wallpaper);
105 }
106
107 void Command::run()
108 {
109   if (!initted) return;
110   irun = 1;
111
112   mainPid = getpid();
113
114   // just in case
115   Video::getInstance()->signalOn();
116   Led::getInstance()->on();
117
118   doWallpaper();
119
120   // End of startup. Lock the mutex and put the first view up
121
122   pthread_mutex_lock(&masterLock);
123
124
125   VConnect* vconnect = new VConnect();
126   viewman->add(vconnect);
127   vconnect->run();
128
129
130   UCHAR button = 0;
131   while(irun)
132   {
133     // unlock and wait
134     pthread_mutex_unlock(&masterLock);
135
136     button = remote->getButtonPress(2);  // FIXME why is this set to 2 and not 0? so it can quit
137     // something happened, lock and process
138
139     pthread_mutex_lock(&masterLock);
140
141     if ((button == Remote::NA_NONE) || (button == Remote::NA_UNKNOWN)) continue;
142
143     if (button != Remote::NA_SIGNAL) handleCommand(button);
144     processMessageQueue();
145   }
146
147   pthread_mutex_unlock(&masterLock);
148 }
149
150 void Command::postMessage(Message* m)
151 {
152   // This is locked here in case the main loop is not waiting for an event, but is processing one
153   // it could be killed but then not react to it because the signal wouldn't cause
154   // remote->getButtonPress to break
155   // locking the mutex ensures that the master thread is waiting on getButtonPress
156
157   pthread_mutex_lock(&masterLock);
158   MessageQueue::postMessage(m);
159   kill(mainPid, SIGURG);
160   pthread_mutex_unlock(&masterLock);
161 }
162
163 bool Command::postMessageIfNotBusy(Message* m)
164 {
165   // This is for the timers module
166   // If the masterlock is locked then the timers module wants to
167   // cancel delivery
168
169   if (pthread_mutex_trylock(&masterLock) != EBUSY)
170   {
171     MessageQueue::postMessage(m);
172     kill(mainPid, SIGURG);
173     pthread_mutex_unlock(&masterLock);
174     return true;
175   }
176   else
177   {
178     return false;
179   }
180 }
181
182 void Command::processMessage(Message* m)
183 {
184   logger->log("Command", Log::DEBUG, "processing message %i", m->message);
185
186   switch(m->message)
187   {
188     case Message::STANDBY:
189     {
190       doStandby();
191       break;
192     }
193     case Message::STOP_PLAYBACK:
194     {
195       handleCommand(Remote::STOP); // an odd way of doing it, but so simple
196       break;
197     }
198     case Message::STREAM_END:
199     {
200       VVideoLive::getInstance()->streamEnd();
201       break;
202     }
203     case Message::VDR_CONNECTED:
204     {
205       doJustConnected((VConnect*)m->from);
206       break;
207     }
208     case Message::TIMER:
209     {
210       // FIXME - go to one message queue only - then instead of having
211       // objects deriving from messagequeues, make them derive from
212       // messagereceiver - then one messagequeue can deliver any message to anywhere
213
214       // deliver timer
215
216       ((TimerReceiver*)m->to)->timercall(m->parameter);
217       handleCommand(Remote::NA_NONE); // in case any timer has posted messages to viewman,
218                                       // run viewman message queue here. FIXME improve this!
219       break;
220     }
221     case Message::SCREENSHOT:
222     {
223       Osd::getInstance()->screenShot("/out.jpg");
224     }
225   }
226 }
227
228 void Command::handleCommand(int button)
229 {
230   if (isStandby && (button != Remote::POWER)) return;
231
232   if (viewman->handleCommand(button)) return;
233
234   // command was not handled
235
236   switch(button)
237   {
238     case Remote::DF_LEFT:
239     case Remote::DF_RIGHT:
240     case Remote::VOLUMEUP:
241     case Remote::VOLUMEDOWN:
242     {
243       VVolume* v = new VVolume();
244       viewman->add(v);
245       v->handleCommand(button); // this will draw+show
246       return;
247     }
248     case Remote::MUTE:
249     {
250       VMute* v = new VMute();
251       v->draw();
252       viewman->add(v);
253       viewman->updateView(v);
254       return;
255     }
256     case Remote::POWER:
257     {
258       doStandby();
259       return;
260     }
261   }
262 }
263
264 void Command::sig1()
265 {
266 #ifdef DEV
267   Message* m = new Message(); // break into master mutex
268   m->message = Message::SCREENSHOT;
269   m->to = this;
270   postMessage(m);
271 #endif
272 }
273
274 void Command::doStandby()
275 {
276   if (isStandby)
277   {
278     Video::getInstance()->signalOn();
279     Led::getInstance()->on();
280     isStandby = 0;
281
282
283     VConnect* vconnect = new VConnect();
284     viewman->add(vconnect);
285     vconnect->run();
286   }
287   else
288   {
289     Video::getInstance()->signalOff();
290     viewman->removeAll();
291     viewman->updateView(wallpaper);
292
293     VDR::getInstance()->configSave("General", "Last Power State", "Off");
294     VDR::getInstance()->disconnect();
295     Led::getInstance()->off();
296     isStandby = 1;
297   }
298 }
299
300 void Command::doReboot()
301 {
302   VDR::getInstance()->disconnect();
303   // just kill it...
304   logger->log("Command", Log::NOTICE, "Reboot");
305   reboot(LINUX_REBOOT_CMD_RESTART);
306 }
307
308 void Command::doJustConnected(VConnect* vconnect)
309 {
310   I18n::initialize();
311   Video* video = Video::getInstance();
312   viewman->removeView(vconnect);
313
314   VInfo* vi = new VInfo();
315   vi->create(400, 200);
316   if (video->getFormat() == Video::PAL)
317     vi->setScreenPos(170, 200);
318   else
319     vi->setScreenPos(160, 150);
320
321   vi->setOneLiner(tr("Connected, loading config"));
322   vi->draw();
323   viewman->add(vi);
324   viewman->updateView(vi);
325
326   VDR* vdr = VDR::getInstance();
327   char* config;
328
329   // See if config says to override video format (PAL/NTSC)
330   config = vdr->configLoad("General", "Override Video Format");
331   if (config)
332   {
333     logger->log("Command", Log::DEBUG, "Override Video Format is present");
334
335     if (   (!strcmp(config, "PAL") && (video->getFormat() == Video::NTSC))
336         || (!strcmp(config, "NTSC") && (video->getFormat() == Video::PAL))  )
337     {
338       // Oh sheesh, need to switch format. Bye bye TV...
339
340       // Take everything down
341       viewman->removeAll();
342       viewman->removeView(wallpaper);
343       Osd* osd = Osd::getInstance();
344       osd->shutdown();
345       video->shutdown();
346
347       // Get video and osd back up with the new mode
348       if (!strcmp(config, "PAL"))
349       {
350         logger->log("Command", Log::DEBUG, "Switching to PAL");
351         video->init(Video::PAL);
352       }
353       else if (!strcmp(config, "NTSC"))
354       {
355         logger->log("Command", Log::DEBUG, "Switching to NTSC");
356         video->init(Video::NTSC);
357       }
358       osd->init("/dev/stbgfx");
359
360       // Put the wallpaper back
361       doWallpaper();
362
363       // Re add the vinfo
364       vi = new VInfo();
365       vi->create(400, 200);
366       if (video->getFormat() == Video::PAL)
367         vi->setScreenPos(170, 200);
368       else
369         vi->setScreenPos(160, 150);
370
371       vi->setOneLiner(tr("Connected, loading config"));
372       vi->draw();
373       viewman->add(vi);
374       viewman->updateView(vi);
375     }
376     else
377     {
378       logger->log("Command", Log::DEBUG, "Already in requested mode, or request was not 'PAL' or 'NTSC'");
379     }
380   }
381   else
382   {
383     logger->log("Command", Log::DEBUG, "Phew, no dangerous on-the-fly mode switching to do!");
384   }
385
386   // Power off if first boot and config says so
387   if (firstBoot)
388   {
389     firstBoot = 0;
390
391     logger->log("Command", Log::DEBUG, "Load power after boot");
392
393     config = vdr->configLoad("General", "Power After Boot");
394
395     if (config)
396     {
397       if (!strcasecmp(config, "On"))
398       {
399         logger->log("Command", Log::INFO, "Config says Power After Boot = On");
400       }
401       else if (!strcasecmp(config, "Off"))
402       {
403         logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
404         doStandby();
405         delete[] config;
406         return; // quit here
407       }
408       else if (!strcasecmp(config, "Last state"))
409       {
410         char* lastPowerState = vdr->configLoad("General", "Last Power State");
411         if (lastPowerState)
412         {
413           if (!strcasecmp(lastPowerState, "On"))
414           {
415             logger->log("Command", Log::INFO, "Config says Last Power State = On");
416           }
417           else if (!strcasecmp(lastPowerState, "Off"))
418           {
419             logger->log("Command", Log::INFO, "Config says Last Power State = Off");
420             doStandby();
421             delete[] config;
422             return; // quit here
423           }
424           else
425           {
426             logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
427           }
428         }
429         else
430         {
431           logger->log("Command", Log::INFO, "Config General/Last Power State not found");
432         }
433       }
434       else
435       {
436         logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
437       }
438       delete[] config;
439     }
440     else
441     {
442       logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
443     }
444   }
445
446
447   // Go S-Video if config says so
448
449   config = vdr->configLoad("TV", "Connection");
450
451   if (config)
452   {
453     if (!strcasecmp(config, "S-Video"))
454     {
455       logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
456       video->setConnection(Video::SVIDEO);
457     }
458     else
459     {
460       logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
461       video->setConnection(Video::COMPOSITERGB);
462     }
463     delete[] config;
464   }
465   else
466   {
467     logger->log("Command", Log::INFO, "Config TV/S-Video not found");
468   }
469
470   // Set remote type
471
472   config = vdr->configLoad("General", "Remote type");
473
474   if (config)
475   {
476     if (!strcasecmp(config, "New"))
477     {
478       logger->log("Command", Log::INFO, "Switching to New remote type");
479       remote->setRemoteType(Remote::NEWREMOTE);
480     }
481     else
482     {
483       logger->log("Command", Log::INFO, "Switching to Old remote type");
484       remote->setRemoteType(Remote::OLDREMOTE);
485     }
486     delete[] config;
487   }
488   else
489   {
490     logger->log("Command", Log::INFO, "Config General/Remote type not found");
491     remote->setRemoteType(Remote::OLDREMOTE);
492   }
493
494   // Get TV aspect ratio
495
496   config = vdr->configLoad("TV", "Aspect");
497   if (config)
498   {
499     if (!strcasecmp(config, "16:9"))
500     {
501       logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
502       video->setTVsize(Video::ASPECT16X9);
503     }
504     else
505     {
506       logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
507       video->setTVsize(Video::ASPECT4X3);
508     }
509     delete[] config;
510   }
511   else
512   {
513     logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
514     video->setTVsize(Video::ASPECT4X3);
515   }
516
517   config = vdr->configLoad("TV", "Widemode");
518   if (config)
519   {
520     if (!strcasecmp(config, "Letterbox"))
521     {
522       logger->log("Command", Log::INFO, "Setting letterbox mode");
523       video->setMode(Video::LETTERBOX);
524     }
525     else
526     {
527       logger->log("Command", Log::INFO, "Setting chop-sides mode");
528       video->setMode(Video::NORMAL);
529     }
530     delete[] config;
531   }
532   else
533   {
534     logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
535     video->setMode(Video::NORMAL);
536   }
537
538   // config done
539
540   // Save power state = on
541
542   vdr->configSave("General", "Last Power State", "On");
543
544
545   viewman->removeView(vi);
546
547   VWelcome* vw = new VWelcome();
548   vw->draw();
549   viewman->add(vw);
550   viewman->updateView(vw);
551 }