2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
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.
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.
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
23 Command* Command::instance = NULL;
39 Command* Command::getInstance()
46 if (initted) return 0;
49 logger = Log::getInstance();
50 viewman = ViewMan::getInstance();
51 remote = Remote::getInstance();
53 if (!logger || !viewman || !remote)
59 pthread_mutex_init(&masterLock, NULL);
64 int Command::shutdown()
66 if (!initted) return 0;
73 // VDR::getInstance()->cancelFindingServer();
84 Video* video = Video::getInstance();
86 UCHAR screenSize = video->getFormat();
88 // moved from startup because surface delete doesn't work
92 Led::getInstance()->on();
96 v->create(video->getScreenWidth(), video->getScreenHeight());
97 v->setBackgroundColour(Colour::VIDEOBLUE);
101 viewman->removeView(v);
104 wallpaper = new VWallpaper();
105 if (screenSize == Video::PAL)
107 logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
108 wallpaper->init("/wallpaperPAL.jpg");
112 logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
113 wallpaper->init("/wallpaperNTSC.jpg");
117 viewman->add(wallpaper);
119 // End of startup. Lock the mutex and put the first view up
121 pthread_mutex_lock(&masterLock);
124 VConnect* vconnect = new VConnect();
125 viewman->add(vconnect);
133 pthread_mutex_unlock(&masterLock);
135 button = remote->getButtonPress(2); // FIXME why is this set to 2 and not 0? so it can quit
136 // something happened, lock and process
138 pthread_mutex_lock(&masterLock);
140 if ((button == Remote::NA_NONE) || (button == Remote::NA_UNKNOWN)) continue;
142 if (button != Remote::NA_SIGNAL) handleCommand(button);
143 processMessageQueue();
146 pthread_mutex_unlock(&masterLock);
149 void Command::postMessage(Message* m)
151 // This is locked here in case the main loop is not waiting for an event, but is processing one
152 // it could be killed but then not react to it because the signal wouldn't cause
153 // remote->getButtonPress to break
154 // locking the mutex ensures that the master thread is waiting on getButtonPress
156 pthread_mutex_lock(&masterLock);
157 MessageQueue::postMessage(m);
158 kill(mainPid, SIGURG);
159 pthread_mutex_unlock(&masterLock);
162 bool Command::postMessageIfNotBusy(Message* m)
164 // This is for the timers module
165 // If the masterlock is locked then the timers module wants to
168 if (pthread_mutex_trylock(&masterLock) != EBUSY)
170 MessageQueue::postMessage(m);
171 kill(mainPid, SIGURG);
172 pthread_mutex_unlock(&masterLock);
181 void Command::processMessage(Message* m)
183 logger->log("Command", Log::DEBUG, "processing message %i", m->message);
187 case Message::STANDBY:
192 case Message::STOP_PLAYBACK:
194 handleCommand(Remote::STOP); // an odd way of doing it, but so simple
197 case Message::STREAM_END:
199 // post a message to ViewMan and then run the viewman message queue
200 Message* m2 = new Message();
201 m2->message = Message::STREAM_END;
202 m2->to = VVideoLive::getInstance();
203 viewman->postMessage(m2);
204 handleCommand(Remote::NA_NONE);
207 case Message::VDR_CONNECTED:
209 doJustConnected((VConnect*)m->from);
214 // FIXME - go to one message queue only - then instead of having
215 // objects deriving from messagequeues, make them derive from
216 // messagereceiver - then one messagequeue can deliver any message to anywhere
219 logger->log("Command", Log::DEBUG, "1: %p", m);
220 logger->log("Command", Log::DEBUG, "2: %p", m->to);
221 logger->log("Command", Log::DEBUG, "3: %lu", m->parameter);
225 ((TimerReceiver*)m->to)->timercall(m->parameter);
226 handleCommand(Remote::NA_NONE); // in case any timer has posted messages to viewman,
227 // run viewman message queue here. FIXME improve this!
228 // FIXME unlock main mutex
233 void Command::handleCommand(int button)
235 if (isStandby && (button != Remote::POWER)) return;
237 if (viewman->handleCommand(button)) return;
239 // command was not handled
243 case Remote::DF_LEFT:
244 case Remote::DF_RIGHT:
245 case Remote::VOLUMEUP:
246 case Remote::VOLUMEDOWN:
248 VVolume* v = new VVolume();
249 v->handleCommand(button); // this will draw+show
255 VMute* v = new VMute();
269 Osd::getInstance()->screenShot("/out.jpg");
276 void Command::doStandby()
280 Video::getInstance()->signalOn();
281 Led::getInstance()->on();
285 VConnect* vconnect = new VConnect();
286 viewman->add(vconnect);
291 Video::getInstance()->signalOff();
292 viewman->removeAll();
295 VDR::getInstance()->configSave("General", "Last Power State", "Off");
296 VDR::getInstance()->disconnect();
297 Led::getInstance()->off();
302 void Command::doReboot()
304 VDR::getInstance()->disconnect();
306 logger->log("Command", Log::NOTICE, "Reboot");
307 reboot(LINUX_REBOOT_CMD_RESTART);
310 void Command::doJustConnected(VConnect* vconnect)
313 Video* video = Video::getInstance();
314 viewman->removeView(vconnect);
316 VInfo* vi = new VInfo();
317 vi->create(400, 200);
318 if (video->getFormat() == Video::PAL)
319 vi->setScreenPos(170, 200);
321 vi->setScreenPos(160, 150);
323 vi->setOneLiner(tr("Connected, loading config"));
329 VDR* vdr = VDR::getInstance();
332 // Power off if first boot and config says so
337 config = vdr->configLoad("General", "Power After Boot");
341 if (!strcasecmp(config, "On"))
343 logger->log("Command", Log::INFO, "Config says Power After Boot = On");
345 else if (!strcasecmp(config, "Off"))
347 logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
352 else if (!strcasecmp(config, "Last state"))
354 char* lastPowerState = vdr->configLoad("General", "Last Power State");
357 if (!strcasecmp(lastPowerState, "On"))
359 logger->log("Command", Log::INFO, "Config says Last Power State = On");
361 else if (!strcasecmp(lastPowerState, "Off"))
363 logger->log("Command", Log::INFO, "Config says Last Power State = Off");
370 logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
375 logger->log("Command", Log::INFO, "Config General/Last Power State not found");
380 logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
386 logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
391 // Go S-Video if config says so
393 config = vdr->configLoad("TV", "Connection");
397 if (!strcasecmp(config, "S-Video"))
399 logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
400 video->setConnection(Video::SVIDEO);
404 logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
405 video->setConnection(Video::COMPOSITERGB);
411 logger->log("Command", Log::INFO, "Config TV/S-Video not found");
416 config = vdr->configLoad("General", "Remote type");
420 if (!strcasecmp(config, "New"))
422 logger->log("Command", Log::INFO, "Switching to New remote type");
423 remote->setRemoteType(Remote::NEWREMOTE);
427 logger->log("Command", Log::INFO, "Switching to Old remote type");
428 remote->setRemoteType(Remote::OLDREMOTE);
434 logger->log("Command", Log::INFO, "Config General/Remote type not found");
435 remote->setRemoteType(Remote::OLDREMOTE);
438 // Get TV aspect ratio
440 config = vdr->configLoad("TV", "Aspect");
443 if (!strcasecmp(config, "16:9"))
445 logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
446 video->setTVsize(Video::ASPECT16X9);
450 logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
451 video->setTVsize(Video::ASPECT4X3);
457 logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
458 video->setTVsize(Video::ASPECT4X3);
461 config = vdr->configLoad("TV", "Widemode");
464 if (!strcasecmp(config, "Letterbox"))
466 logger->log("Command", Log::INFO, "Setting letterbox mode");
467 video->setMode(Video::LETTERBOX);
471 logger->log("Command", Log::INFO, "Setting chop-sides mode");
472 video->setMode(Video::NORMAL);
478 logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
479 video->setMode(Video::NORMAL);
484 // Save power state = on
486 vdr->configSave("General", "Last Power State", "On");
489 viewman->removeView(vi);
491 VWelcome* vw = new VWelcome();