]> git.vomp.tv Git - vompclient-marten.git/blob - command.cc
fix to make it switch back to rgb if told to do so by config
[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   state = 0;
31 }
32
33 Command::~Command()
34 {
35   instance = NULL;
36 }
37
38 Command* Command::getInstance()
39 {
40   return instance;
41 }
42
43 int Command::init()
44 {
45   if (initted) return 0;
46   initted = 1;
47
48   logger = Log::getInstance();
49   viewman = ViewMan::getInstance();
50   remote = Remote::getInstance();
51
52   if (!logger || !viewman || !remote)
53   {
54     initted = 0;
55     return 0;
56   }
57
58   return 1;
59 }
60
61 int Command::shutdown()
62 {
63   if (!initted) return 0;
64   initted = 0;
65   return 1;
66 }
67
68 void Command::stop()
69 {
70   VDR::getInstance()->cancelFindingServer();
71   irun = 0;
72 }
73
74 void Command::run()
75 {
76   if (!initted) return;
77   irun = 1;
78
79   mainPid = getpid();
80
81   Video* video = Video::getInstance();
82
83   UCHAR screenSize = video->getFormat();
84
85   // moved from startup because surface delete doesn't work
86
87   // just in case
88   video->signalOn();
89   Led::getInstance()->on();
90
91   // Blue background
92   View* v = new View();
93   v->setDimensions(video->getScreenHeight(), video->getScreenWidth());
94   v->setBackgroundColour(Colour::VIDEOBLUE);
95   v->draw();
96   v->show();
97   viewman->add(v);
98   viewman->removeView(v);
99
100   // Wallpaper
101   VWallpaper* w = new VWallpaper();
102   if (screenSize == Video::PAL)
103   {
104     logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
105     w->init("/wallpaperPAL.jpg");
106   }
107   else
108   {
109     logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
110     w->init("/wallpaperNTSC.jpg");
111   }
112   w->draw();
113   w->show();
114   viewman->add(w);
115
116
117 //  handleCommand(Remote::TWO);
118 //  handleCommand(Remote::UP);
119 //  handleCommand(Remote::PLAY);
120
121 //  handleCommand(Remote::DOWN);
122 //  handleCommand(Remote::OK);
123
124 //  handleCommand(Remote::DOWN);
125 //  handleCommand(Remote::DOWN);
126 //  handleCommand(Remote::DOWN);
127 //  handleCommand(Remote::OK);
128
129   // state values:
130   // 0 = not connected
131   // 1 = got servers
132   // 2 = multiple servers, showing select box
133   // 3 = a server has been selected
134   // 4 = connected
135   // 5 = standby
136
137   int selectServer;
138   UCHAR button = 0;
139   while(irun)
140   {
141     if (state != 4) // not really necessary, but chops out all the connecting rubbish if already connected
142     {
143       if (state == 0)
144       {
145         clearServerIPs();
146         broadcastForServers();
147         if (!irun) break;
148         state = 1;
149       }
150
151       if (state == 1)
152       {
153         if (serverIPs.size() == 1)
154         {
155           selectServer = 0;
156           state = 3;
157         }
158         else
159         {
160           selectServer = -1;
161           state = 2;
162           VServerSelect* vs = new VServerSelect(&serverIPs, &selectServer);
163           vs->draw();
164           vs->show();
165           viewman->add(vs);
166         }
167       }
168
169       if (state == 2)
170       {
171         // entering loop for 2nd time.. has a server been selected?
172         if (selectServer != -1) state = 3;
173       }
174
175       if (state == 3)
176       {
177         logger->log("Command", Log::DEBUG, "Server selected: %i", selectServer);
178
179         int success = connectToServer(selectServer);
180         clearServerIPs();
181
182         if (success)
183         {
184           VWelcome* vw = new VWelcome();
185           vw->draw();
186           vw->show();
187           viewman->add(vw);
188           state = 4;
189
190           // test area for just after connect
191           char* svideo = VDR::getInstance()->configLoad("TV", "S-Video");
192
193           if (svideo)
194           {
195             if (!strcasecmp(svideo, "Yes"))
196             {
197               logger->log("Command", Log::INFO, "Switching to S-Video as S-Video=%s", svideo);
198               video->setConnection(Video::SVIDEO);
199             }
200             else
201             {
202               logger->log("Command", Log::INFO, "Leaving video output as S-Video=%s", svideo);
203               video->setConnection(Video::COMPOSITERGB);
204             }
205           }
206           else
207           {
208             logger->log("Command", Log::INFO, "Config TV/S-Video not found");
209           }
210
211 /*
212
213           if ((isWidescreen != NULL) && (!strcmp(isWidescreen, "Yes")))
214           {
215             logger->log("Command", Log::DEBUG, "Setting 16x9");
216
217             int a = video->setAspectRatio(Video::ASPECT16X9);
218             printf("success = %i\n", a);
219             video->reset();
220           }
221           else
222           {
223             //temp
224             logger->log("Command", Log::DEBUG, "Setting 4x3");
225             int a = video->setAspectRatio(Video::ASPECT4X3);
226             printf("success = %i\n", a);
227             video->reset();
228           }
229 */
230
231           // end of test area
232
233         }
234         else
235         {
236           state = 0;
237         }
238       }
239
240       if (state == 0) continue;
241       // state can't be 1
242       // if state == 2 then drop to remote handling below
243       // state can't be 3
244       // if state == 4 then drop through to main system, job done
245     }
246
247
248     button = remote->getButtonPress(2);  // FIXME why is this set to 2 and not 0? so it can quit
249     if ((button == Remote::NONE) || (button == Remote::UNKNOWN)) continue;
250
251     if (button != Remote::SIGNAL) handleCommand(button);
252     processMessageQueue();
253   }
254 }
255
256 void Command::postMessage(Message* m)
257 {
258   MessageQueue::postMessage(m);
259   kill(mainPid, SIGURG);
260 }
261
262 void Command::processMessage(Message* m)
263 {
264   logger->log("Command", Log::DEBUG, "processing message %i", m->message);
265
266   switch(m->message)
267   {
268     case Message::STANDBY:
269     {
270       doStandby();
271       break;
272     }
273     case Message::STOP_PLAYBACK:
274     {
275       handleCommand(Remote::STOP); // an odd way of doing it, but so simple
276       break;
277     }
278   }
279 }
280
281 void Command::clearServerIPs()
282 {
283   // Clear the serverIPs vector
284   for(UINT k = 0; k < serverIPs.size(); k++)
285   {
286     delete[] serverIPs[k];
287   }
288   serverIPs.clear();
289 }
290
291 void Command::handleCommand(int button)
292 {
293   if (state == 5 && (button != Remote::POWER)) return;
294
295   if (viewman->handleCommand(button)) return;
296
297   // command was not handled
298
299   switch(button)
300   {
301     case Remote::LEFT:
302     case Remote::RIGHT:
303     {
304       VVolume* v = new VVolume();
305       v->handleCommand(button); // this will draw+show
306       viewman->add(v);
307       viewman->timedDelete(v, 2, 1);
308       return;
309     }
310     case Remote::MUTE:
311     {
312       VMute* v = new VMute();
313       v->draw();
314       v->show();
315       viewman->add(v);
316       viewman->timedDelete(v, 2, 1);
317       return;
318     }
319     case Remote::POWER:
320     {
321       doStandby();
322       return;
323     }
324     case Remote::RECORD:
325     {
326       Osd::getInstance()->screenShot("/out.jpg");
327       return;
328     }
329   }
330 }
331
332 void Command::broadcastForServers()
333 {
334   VInfo* viewWait = new VInfo();
335   viewWait->setDimensions(200, 400);
336   if (Video::getInstance()->getFormat() == Video::PAL)
337   {
338     viewWait->setScreenPos(170, 200);
339   }
340   else
341   {
342     viewWait->setScreenPos(160, 150);
343   }
344   viewWait->setMainText("\n                        Locating server");
345   viewWait->draw();
346   viewWait->show();
347   viewman->add(viewWait);
348
349
350   VDR* vdr = VDR::getInstance();
351   vdr->findServers(serverIPs);
352   if (!irun) return;
353   viewman->removeView(viewWait);
354 }
355
356 int Command::connectToServer(int vectorIndex)
357 {
358   char a[60];
359   struct timespec ts;
360
361   VDR* vdr = VDR::getInstance();
362   vdr->setServerIP(serverIPs[vectorIndex]);
363
364   logger->log("Command", Log::NOTICE, "Connecting to server at %s", serverIPs[vectorIndex]);
365
366   VInfo* viewWait = new VInfo();
367   viewWait->setDimensions(200, 400);
368   if (Video::getInstance()->getFormat() == Video::PAL)
369   {
370     viewWait->setScreenPos(170, 200);
371   }
372   else
373   {
374     viewWait->setScreenPos(160, 150);
375   }
376   viewWait->setMainText("\n                     Connecting to VDR");
377   viewWait->draw();
378   viewWait->show();
379   viewman->add(viewWait);
380
381   int success = vdr->connect();
382
383   if (success)
384   {
385     logger->log("Command", Log::DEBUG, "Connected ok, doing login");
386     success = vdr->doLogin();
387
388     if (success)
389     {
390       strcpy(a, "\n                            Connected");
391       ts.tv_sec = 0;
392       ts.tv_nsec = 500000000;
393     }
394     else
395     {
396       strcpy(a, "\n                           Login failed");
397       ts.tv_sec = 3;
398       ts.tv_nsec = 0;
399     }
400   }
401   else
402   {
403     strcpy(a, "\n                      Connection failed");
404     ts.tv_sec = 3;
405     ts.tv_nsec = 0;
406   }
407
408   viewWait->setMainText(a);
409   viewWait->draw();
410   viewWait->show();
411
412   if (irun) nanosleep(&ts, NULL); // only do the wait if we aren't shutting down
413
414 /*
415   vdr->configSave("Section Name This Is blah blah blah 495834509725049375032end", "thekey with this space ", "1234value");
416   sleep(10);
417
418   char* conf = vdr->configLoad("fred", "bill");
419   if (conf)
420   {
421     //printf("And the config value returned is %s\n", conf);
422     delete[] conf;
423   }
424   else
425   {
426     //printf("Conf value return is NULL :(\n");
427   }
428 */
429
430   viewman->removeView(viewWait);
431
432   return success;
433 }
434
435 void Command::doStandby()
436 {
437   if (state == 5)
438   {
439     Video::getInstance()->signalOn();
440     Led::getInstance()->on();
441     state = 0;
442   }
443   else
444   {
445     ViewMan* viewman = ViewMan::getInstance();
446
447     viewman->removeAll();
448     viewman->redrawAll();
449
450
451     VDR::getInstance()->disconnect();
452     Video::getInstance()->signalOff();
453     Led::getInstance()->off();
454     state = 5;
455   }
456 }
457
458 void Command::doReboot()
459 {
460   VDR::getInstance()->disconnect();
461   // just kill it...
462   logger->log("Command", Log::NOTICE, "Reboot");
463   reboot(LINUX_REBOOT_CMD_RESTART);
464 }