]> git.vomp.tv Git - vompclient-marten.git/blob - command.cc
New widescreen mode
[vompclient-marten.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   return 1;
60 }
61
62 int Command::shutdown()
63 {
64   if (!initted) return 0;
65   initted = 0;
66   return 1;
67 }
68
69 void Command::stop()
70 {
71 //  VDR::getInstance()->cancelFindingServer();
72   irun = 0;
73 }
74
75 void Command::run()
76 {
77   if (!initted) return;
78   irun = 1;
79
80   mainPid = getpid();
81
82   Video* video = Video::getInstance();
83
84   UCHAR screenSize = video->getFormat();
85
86   // moved from startup because surface delete doesn't work
87
88   // just in case
89   video->signalOn();
90   Led::getInstance()->on();
91
92   // Blue background
93   View* v = new View();
94   v->create(video->getScreenWidth(), video->getScreenHeight());
95   v->setBackgroundColour(Colour::VIDEOBLUE);
96   v->draw();
97   v->show();
98   viewman->add(v);
99   viewman->removeView(v);
100
101   // Wallpaper
102   wallpaper = new VWallpaper();
103   if (screenSize == Video::PAL)
104   {
105     logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
106     wallpaper->init("/wallpaperPAL.jpg");
107   }
108   else
109   {
110     logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
111     wallpaper->init("/wallpaperNTSC.jpg");
112   }
113   wallpaper->draw();
114   wallpaper->show();
115   viewman->add(wallpaper);
116
117   VConnect* vconnect = new VConnect();
118   viewman->add(vconnect);
119   vconnect->run();
120
121
122   UCHAR button = 0;
123   while(irun)
124   {
125     button = remote->getButtonPress(2);  // FIXME why is this set to 2 and not 0? so it can quit
126     if ((button == Remote::NA_NONE) || (button == Remote::NA_UNKNOWN)) continue;
127
128     if (button != Remote::NA_SIGNAL) handleCommand(button);
129     processMessageQueue();
130   }
131 }
132
133 void Command::postMessage(Message* m)
134 {
135   MessageQueue::postMessage(m);
136   kill(mainPid, SIGURG);
137 }
138
139 void Command::processMessage(Message* m)
140 {
141   logger->log("Command", Log::DEBUG, "processing message %i", m->message);
142
143   switch(m->message)
144   {
145     case Message::STANDBY:
146     {
147       doStandby();
148       break;
149     }
150     case Message::STOP_PLAYBACK:
151     {
152       handleCommand(Remote::STOP); // an odd way of doing it, but so simple
153       break;
154     }
155     case Message::STREAM_END:
156     {
157       // post a message to ViewMan and then run the viewman message queue
158       Message* m = new Message();
159       m->message = Message::STREAM_END;
160       m->to = VVideoLive::getInstance();
161       viewman->postMessage(m);
162       handleCommand(Remote::NA_NONE);
163       break;
164     }
165     case Message::VDR_CONNECTED:
166     {
167       doJustConnected((VConnect*)m->from);
168       break;
169     }
170   }
171 }
172
173 void Command::handleCommand(int button)
174 {
175   if (isStandby && (button != Remote::POWER)) return;
176
177   if (viewman->handleCommand(button)) return;
178
179   // command was not handled
180
181   switch(button)
182   {
183     case Remote::DF_LEFT:
184     case Remote::DF_RIGHT:
185     case Remote::VOLUMEUP:
186     case Remote::VOLUMEDOWN:
187     {
188       VVolume* v = new VVolume();
189       v->handleCommand(button); // this will draw+show
190       viewman->add(v);
191       viewman->timedDelete(v, 2, 1);
192       return;
193     }
194     case Remote::MUTE:
195     {
196       VMute* v = new VMute();
197       v->draw();
198       v->show();
199       viewman->add(v);
200       viewman->timedDelete(v, 2, 1);
201       return;
202     }
203     case Remote::POWER:
204     {
205       doStandby();
206       return;
207     }
208 #ifdef DEV
209     case Remote::RECORD:
210     {
211       Osd::getInstance()->screenShot("/out.jpg");
212       return;
213     }
214 #endif
215   }
216 }
217
218 void Command::doStandby()
219 {
220   if (isStandby)
221   {
222     Video::getInstance()->signalOn();
223     Led::getInstance()->on();
224     isStandby = 0;
225
226
227     VConnect* vconnect = new VConnect();
228     viewman->add(vconnect);
229     vconnect->run();
230   }
231   else
232   {
233     Video::getInstance()->signalOff();
234     viewman->removeAll();
235     wallpaper->show();
236
237     VDR::getInstance()->configSave("General", "Last Power State", "Off");
238     VDR::getInstance()->disconnect();
239     Led::getInstance()->off();
240     isStandby = 1;
241   }
242 }
243
244 void Command::doReboot()
245 {
246   VDR::getInstance()->disconnect();
247   // just kill it...
248   logger->log("Command", Log::NOTICE, "Reboot");
249   reboot(LINUX_REBOOT_CMD_RESTART);
250 }
251
252 void Command::doJustConnected(VConnect* vconnect)
253 {
254   Video* video = Video::getInstance();
255   viewman->removeView(vconnect, 0, 1);
256
257   VInfo* vi = new VInfo();
258   vi->create(400, 200);
259   if (video->getFormat() == Video::PAL)
260     vi->setScreenPos(170, 200);
261   else
262     vi->setScreenPos(160, 150);
263
264   vi->setMainText("\n               Connected, loading config");
265   vi->draw();
266   vi->show();
267   viewman->add(vi);
268
269
270   VDR* vdr = VDR::getInstance();
271   char* config;
272
273   // Power off if first boot and config says so
274   if (firstBoot)
275   {
276     firstBoot = 0;
277
278     config = vdr->configLoad("General", "Power After Boot");
279
280     if (config)
281     {
282       if (!strcasecmp(config, "On"))
283       {
284         logger->log("Command", Log::INFO, "Config says Power After Boot = On");
285       }
286       else if (!strcasecmp(config, "Off"))
287       {
288         logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
289         doStandby();
290         delete[] config;
291         return; // quit here
292       }
293       else if (!strcasecmp(config, "Last state"))
294       {
295         char* lastPowerState = vdr->configLoad("General", "Last Power State");
296         if (lastPowerState)
297         {
298           if (!strcasecmp(lastPowerState, "On"))
299           {
300             logger->log("Command", Log::INFO, "Config says Last Power State = On");
301           }
302           else if (!strcasecmp(lastPowerState, "Off"))
303           {
304             logger->log("Command", Log::INFO, "Config says Last Power State = Off");
305             doStandby();
306             delete[] config;
307             return; // quit here
308           }
309           else
310           {
311             logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
312           }
313         }
314         else
315         {
316           logger->log("Command", Log::INFO, "Config General/Last Power State not found");
317         }
318       }
319       else
320       {
321         logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
322       }
323       delete[] config;
324     }
325     else
326     {
327       logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
328     }
329   }
330
331
332   // Go S-Video if config says so
333
334   config = vdr->configLoad("TV", "S-Video");
335
336   if (config)
337   {
338     if (!strcasecmp(config, "Yes"))
339     {
340       logger->log("Command", Log::INFO, "Switching to S-Video as S-Video=%s", config);
341       video->setConnection(Video::SVIDEO);
342     }
343     else
344     {
345       logger->log("Command", Log::INFO, "Leaving video output as S-Video=%s", config);
346       video->setConnection(Video::COMPOSITERGB);
347     }
348     delete[] config;
349   }
350   else
351   {
352     logger->log("Command", Log::INFO, "Config TV/S-Video not found");
353   }
354
355   // Set remote type
356
357   config = vdr->configLoad("General", "Remote type");
358
359   if (config)
360   {
361     if (!strcasecmp(config, "New"))
362     {
363       logger->log("Command", Log::INFO, "Switching to New remote type");
364       remote->setRemoteType(Remote::NEWREMOTE);
365     }
366     else
367     {
368       logger->log("Command", Log::INFO, "Switching to Old remote type");
369       remote->setRemoteType(Remote::OLDREMOTE);
370     }
371     delete[] config;
372   }
373   else
374   {
375     logger->log("Command", Log::INFO, "Config General/Remote type not found");
376     remote->setRemoteType(Remote::OLDREMOTE);
377   }
378
379   // Get TV aspect ratio
380
381   config = vdr->configLoad("TV", "Aspect");
382   if (config)
383   {
384     if (!strcasecmp(config, "16:9"))
385     {
386       logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
387       video->setTVsize(Video::ASPECT16X9);
388     }
389     else
390     {
391       logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
392       video->setTVsize(Video::ASPECT4X3);
393     }
394     delete[] config;
395   }
396   else
397   {
398     logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
399     video->setTVsize(Video::ASPECT4X3);
400   }
401
402   config = vdr->configLoad("TV", "Widemode");
403   if (config)
404   {
405     if (!strcasecmp(config, "Letterbox"))
406     {
407       logger->log("Command", Log::INFO, "Setting letterbox mode");
408       video->setMode(Video::LETTERBOX);
409     }
410     else
411     {
412       logger->log("Command", Log::INFO, "Setting chop-sides mode");
413       video->setMode(Video::NORMAL);
414     }
415     delete[] config;
416   }
417   else
418   {
419     logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
420     video->setMode(Video::NORMAL);
421   }
422
423   // config done
424
425   // Save power state = on
426
427   vdr->configSave("General", "Last Power State", "On");
428
429
430   viewman->removeView(vi);
431
432   VWelcome* vw = new VWelcome();
433   vw->draw();
434   vw->show();
435   viewman->add(vw);
436 //  viewman->redrawAll();
437
438 /*
439   handleCommand(Remote::DOWN);
440   handleCommand(Remote::DOWN);
441   handleCommand(Remote::DOWN);
442   handleCommand(Remote::OK);
443 */
444 }