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