]> git.vomp.tv Git - vompclient-marten.git/blob - command.cc
Fix for cant power down if server unavailable
[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->setDimensions(video->getScreenHeight(), video->getScreenWidth());
95   v->setBackgroundColour(Colour::VIDEOBLUE);
96   v->draw();
97   v->show();
98   viewman->add(v);
99   viewman->removeView(v);
100
101   // Wallpaper
102   VWallpaper* w = new VWallpaper();
103   if (screenSize == Video::PAL)
104   {
105     logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
106     w->init("/wallpaperPAL.jpg");
107   }
108   else
109   {
110     logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
111     w->init("/wallpaperNTSC.jpg");
112   }
113   w->draw();
114   w->show();
115   viewman->add(w);
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::NONE) || (button == Remote::UNKNOWN)) continue;
127
128     if (button != Remote::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::VDR_CONNECTED:
156     {
157       doJustConnected((VConnect*)m->from);
158       break;
159     }
160   }
161 }
162
163 void Command::handleCommand(int button)
164 {
165   if (isStandby && (button != Remote::POWER)) return;
166
167   if (viewman->handleCommand(button)) return;
168
169   // command was not handled
170
171   switch(button)
172   {
173     case Remote::LEFT:
174     case Remote::RIGHT:
175     {
176       VVolume* v = new VVolume();
177       v->handleCommand(button); // this will draw+show
178       viewman->add(v);
179       viewman->timedDelete(v, 2, 1);
180       return;
181     }
182     case Remote::MUTE:
183     {
184       VMute* v = new VMute();
185       v->draw();
186       v->show();
187       viewman->add(v);
188       viewman->timedDelete(v, 2, 1);
189       return;
190     }
191     case Remote::POWER:
192     {
193       doStandby();
194       return;
195     }
196     case Remote::RECORD:
197     {
198       Osd::getInstance()->screenShot("/out.jpg");
199       return;
200     }
201   }
202 }
203
204 void Command::doStandby()
205 {
206   if (isStandby)
207   {
208     Video::getInstance()->signalOn();
209     Led::getInstance()->on();
210     isStandby = 0;
211
212
213     VConnect* vconnect = new VConnect();
214     viewman->add(vconnect);
215     vconnect->run();
216   }
217   else
218   {
219     ViewMan* viewman = ViewMan::getInstance();
220
221     viewman->removeAll();
222     viewman->redrawAll();
223
224     VDR::getInstance()->configSave("General", "Last Power State", "Off");
225     VDR::getInstance()->disconnect();
226     Video::getInstance()->signalOff();
227     Led::getInstance()->off();
228     isStandby = 1;
229   }
230 }
231
232 void Command::doReboot()
233 {
234   VDR::getInstance()->disconnect();
235   // just kill it...
236   logger->log("Command", Log::NOTICE, "Reboot");
237   reboot(LINUX_REBOOT_CMD_RESTART);
238 }
239
240 void Command::doJustConnected(VConnect* vconnect)
241 {
242   VDR* vdr = VDR::getInstance();
243   ViewMan* viewman = ViewMan::getInstance();
244   viewman->removeView(vconnect, 0, 1);
245
246   // Power off if first boot and config says so
247   if (firstBoot)
248   {
249     firstBoot = 0;
250
251     char* powerAfterBoot = vdr->configLoad("General", "Power After Boot");
252
253     if (powerAfterBoot)
254     {
255       if (!strcasecmp(powerAfterBoot, "On"))
256       {
257         logger->log("Command", Log::INFO, "Config says Power After Boot = On");
258       }
259       else if (!strcasecmp(powerAfterBoot, "Off"))
260       {
261         logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
262         doStandby();
263         return; // quit here
264       }
265       else if (!strcasecmp(powerAfterBoot, "Last state"))
266       {
267         char* lastPowerState = vdr->configLoad("General", "Last Power State");
268         if (lastPowerState)
269         {
270           if (!strcasecmp(lastPowerState, "On"))
271           {
272             logger->log("Command", Log::INFO, "Config says Last Power State = On");
273           }
274           else if (!strcasecmp(lastPowerState, "Off"))
275           {
276             logger->log("Command", Log::INFO, "Config says Last Power State = Off");
277             doStandby();
278             return; // quit here
279           }
280           else
281           {
282             logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
283           }
284         }
285         else
286         {
287           logger->log("Command", Log::INFO, "Config General/Last Power State not found");
288         }
289       }
290       else
291       {
292         logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
293       }
294     }
295     else
296     {
297       logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
298     }
299   }
300
301   // Save power state = on
302
303   vdr->configSave("General", "Last Power State", "On");
304
305
306   // Go S-Video if config says so
307
308   char* svideo = vdr->configLoad("TV", "S-Video");
309
310   if (svideo)
311   {
312     if (!strcasecmp(svideo, "Yes"))
313     {
314       logger->log("Command", Log::INFO, "Switching to S-Video as S-Video=%s", svideo);
315       Video::getInstance()->setConnection(Video::SVIDEO);
316     }
317     else
318     {
319       logger->log("Command", Log::INFO, "Leaving video output as S-Video=%s", svideo);
320       Video::getInstance()->setConnection(Video::COMPOSITERGB);
321     }
322   }
323   else
324   {
325     logger->log("Command", Log::INFO, "Config TV/S-Video not found");
326   }
327
328   // s video done
329
330   VWelcome* vw = new VWelcome();
331   viewman->add(vw);
332   viewman->redrawAll();
333 }