]> git.vomp.tv Git - vompclient-marten.git/blob - command.cc
Clean up of all video init system, NTSC/PAL, screen size functions.
[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             }
204           }
205           else
206           {
207             logger->log("Command", Log::INFO, "Config TV/S-Video not found");
208           }
209
210 /*
211
212           if ((isWidescreen != NULL) && (!strcmp(isWidescreen, "Yes")))
213           {
214             logger->log("Command", Log::DEBUG, "Setting 16x9");
215
216             int a = video->setAspectRatio(Video::ASPECT16X9);
217             printf("success = %i\n", a);
218             video->reset();
219           }
220           else
221           {
222             //temp
223             logger->log("Command", Log::DEBUG, "Setting 4x3");
224             int a = video->setAspectRatio(Video::ASPECT4X3);
225             printf("success = %i\n", a);
226             video->reset();
227           }
228 */
229
230           // end of test area
231
232         }
233         else
234         {
235           state = 0;
236         }
237       }
238
239       if (state == 0) continue;
240       // state can't be 1
241       // if state == 2 then drop to remote handling below
242       // state can't be 3
243       // if state == 4 then drop through to main system, job done
244     }
245
246
247     button = remote->getButtonPress(2);  // FIXME why is this set to 2 and not 0? so it can quit
248     if ((button == Remote::NONE) || (button == Remote::UNKNOWN)) continue;
249
250     if (button != Remote::SIGNAL) handleCommand(button);
251     processMessageQueue();
252   }
253 }
254
255 void Command::postMessage(Message* m)
256 {
257   MessageQueue::postMessage(m);
258   kill(mainPid, SIGURG);
259 }
260
261 void Command::processMessage(Message* m)
262 {
263   logger->log("Command", Log::DEBUG, "processing message %i", m->message);
264
265   switch(m->message)
266   {
267     case Message::STANDBY:
268     {
269       doStandby();
270       break;
271     }
272     case Message::STOP_PLAYBACK:
273     {
274       handleCommand(Remote::STOP); // an odd way of doing it, but so simple
275       break;
276     }
277   }
278 }
279
280 void Command::clearServerIPs()
281 {
282   // Clear the serverIPs vector
283   for(UINT k = 0; k < serverIPs.size(); k++)
284   {
285     delete[] serverIPs[k];
286   }
287   serverIPs.clear();
288 }
289
290 void Command::handleCommand(int button)
291 {
292   if (state == 5 && (button != Remote::POWER)) return;
293
294   if (viewman->handleCommand(button)) return;
295
296   // command was not handled
297
298   switch(button)
299   {
300     case Remote::LEFT:
301     case Remote::RIGHT:
302     {
303       VVolume* v = new VVolume();
304       v->handleCommand(button); // this will draw+show
305       viewman->add(v);
306       viewman->timedDelete(v, 2, 1);
307       return;
308     }
309     case Remote::MUTE:
310     {
311       VMute* v = new VMute();
312       v->draw();
313       v->show();
314       viewman->add(v);
315       viewman->timedDelete(v, 2, 1);
316       return;
317     }
318     case Remote::POWER:
319     {
320       doStandby();
321       return;
322     }
323     case Remote::RECORD:
324     {
325       Osd::getInstance()->screenShot("/out.jpg");
326       return;
327     }
328   }
329 }
330
331 void Command::broadcastForServers()
332 {
333   VInfo* viewWait = new VInfo();
334   viewWait->setDimensions(200, 400);
335   if (Video::getInstance()->getFormat() == Video::PAL)
336   {
337     viewWait->setScreenPos(170, 200);
338   }
339   else
340   {
341     viewWait->setScreenPos(160, 150);
342   }
343   viewWait->setMainText("\n                        Locating server");
344   viewWait->draw();
345   viewWait->show();
346   viewman->add(viewWait);
347
348
349   VDR* vdr = VDR::getInstance();
350   vdr->findServers(serverIPs);
351   if (!irun) return;
352   viewman->removeView(viewWait);
353 }
354
355 int Command::connectToServer(int vectorIndex)
356 {
357   char a[60];
358   struct timespec ts;
359
360   VDR* vdr = VDR::getInstance();
361   vdr->setServerIP(serverIPs[vectorIndex]);
362
363   logger->log("Command", Log::NOTICE, "Connecting to server at %s", serverIPs[vectorIndex]);
364
365   VInfo* viewWait = new VInfo();
366   viewWait->setDimensions(200, 400);
367   if (Video::getInstance()->getFormat() == Video::PAL)
368   {
369     viewWait->setScreenPos(170, 200);
370   }
371   else
372   {
373     viewWait->setScreenPos(160, 150);
374   }
375   viewWait->setMainText("\n                     Connecting to VDR");
376   viewWait->draw();
377   viewWait->show();
378   viewman->add(viewWait);
379
380   int success = vdr->connect();
381
382   if (success)
383   {
384     logger->log("Command", Log::DEBUG, "Connected ok, doing login");
385     success = vdr->doLogin();
386
387     if (success)
388     {
389       strcpy(a, "\n                            Connected");
390       ts.tv_sec = 0;
391       ts.tv_nsec = 500000000;
392     }
393     else
394     {
395       strcpy(a, "\n                           Login failed");
396       ts.tv_sec = 3;
397       ts.tv_nsec = 0;
398     }
399   }
400   else
401   {
402     strcpy(a, "\n                      Connection failed");
403     ts.tv_sec = 3;
404     ts.tv_nsec = 0;
405   }
406
407   viewWait->setMainText(a);
408   viewWait->draw();
409   viewWait->show();
410
411   if (irun) nanosleep(&ts, NULL); // only do the wait if we aren't shutting down
412
413 /*
414   vdr->configSave("Section Name This Is blah blah blah 495834509725049375032end", "thekey with this space ", "1234value");
415   sleep(10);
416
417   char* conf = vdr->configLoad("fred", "bill");
418   if (conf)
419   {
420     //printf("And the config value returned is %s\n", conf);
421     delete[] conf;
422   }
423   else
424   {
425     //printf("Conf value return is NULL :(\n");
426   }
427 */
428
429   viewman->removeView(viewWait);
430
431   return success;
432 }
433
434 void Command::doStandby()
435 {
436   if (state == 5)
437   {
438     Video::getInstance()->signalOn();
439     Led::getInstance()->on();
440     state = 0;
441   }
442   else
443   {
444     ViewMan* viewman = ViewMan::getInstance();
445
446     viewman->removeAll();
447     viewman->redrawAll();
448
449
450     VDR::getInstance()->disconnect();
451     Video::getInstance()->signalOff();
452     Led::getInstance()->off();
453     state = 5;
454   }
455 }
456
457 void Command::doReboot()
458 {
459   VDR::getInstance()->disconnect();
460   // just kill it...
461   logger->log("Command", Log::NOTICE, "Reboot");
462   reboot(LINUX_REBOOT_CMD_RESTART);
463 }