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();
77 void Command::doWallpaper()
79 Video* video = Video::getInstance();
83 v->create(video->getScreenWidth(), video->getScreenHeight());
84 v->setBackgroundColour(Colour::VIDEOBLUE);
87 viewman->updateView(v);
88 viewman->removeView(v);
91 wallpaper = new VWallpaper();
92 if (video->getFormat() == Video::PAL)
94 logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
95 wallpaper->init("/wallpaperPAL.jpg");
99 logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
100 wallpaper->init("/wallpaperNTSC.jpg");
103 viewman->add(wallpaper);
104 viewman->updateView(wallpaper);
109 if (!initted) return;
115 Video::getInstance()->signalOn();
116 Led::getInstance()->on();
120 // End of startup. Lock the mutex and put the first view up
122 pthread_mutex_lock(&masterLock);
125 VConnect* vconnect = new VConnect();
126 viewman->add(vconnect);
134 pthread_mutex_unlock(&masterLock);
136 button = remote->getButtonPress(2); // FIXME why is this set to 2 and not 0? so it can quit
137 // something happened, lock and process
139 pthread_mutex_lock(&masterLock);
141 if ((button == Remote::NA_NONE) || (button == Remote::NA_UNKNOWN)) continue;
143 if (button != Remote::NA_SIGNAL) handleCommand(button);
144 processMessageQueue();
147 pthread_mutex_unlock(&masterLock);
150 void Command::postMessage(Message* m)
152 // This is locked here in case the main loop is not waiting for an event, but is processing one
153 // it could be killed but then not react to it because the signal wouldn't cause
154 // remote->getButtonPress to break
155 // locking the mutex ensures that the master thread is waiting on getButtonPress
157 pthread_mutex_lock(&masterLock);
158 MessageQueue::postMessage(m);
159 kill(mainPid, SIGURG);
160 pthread_mutex_unlock(&masterLock);
163 bool Command::postMessageIfNotBusy(Message* m)
165 // This is for the timers module
166 // If the masterlock is locked then the timers module wants to
169 if (pthread_mutex_trylock(&masterLock) != EBUSY)
171 MessageQueue::postMessage(m);
172 kill(mainPid, SIGURG);
173 pthread_mutex_unlock(&masterLock);
182 void Command::processMessage(Message* m)
184 logger->log("Command", Log::DEBUG, "processing message %i", m->message);
188 case Message::STANDBY:
193 case Message::STOP_PLAYBACK:
195 handleCommand(Remote::STOP); // an odd way of doing it, but so simple
198 case Message::STREAM_END:
200 VVideoLive::getInstance()->streamEnd();
203 case Message::VDR_CONNECTED:
205 doJustConnected((VConnect*)m->from);
210 // FIXME - go to one message queue only - then instead of having
211 // objects deriving from messagequeues, make them derive from
212 // messagereceiver - then one messagequeue can deliver any message to anywhere
216 ((TimerReceiver*)m->to)->timercall(m->parameter);
217 handleCommand(Remote::NA_NONE); // in case any timer has posted messages to viewman,
218 // run viewman message queue here. FIXME improve this!
221 case Message::SCREENSHOT:
223 Osd::getInstance()->screenShot("/out.jpg");
228 void Command::handleCommand(int button)
230 if (isStandby && (button != Remote::POWER)) return;
232 if (viewman->handleCommand(button)) return;
234 // command was not handled
238 case Remote::DF_LEFT:
239 case Remote::DF_RIGHT:
240 case Remote::VOLUMEUP:
241 case Remote::VOLUMEDOWN:
243 VVolume* v = new VVolume();
245 v->handleCommand(button); // this will draw+show
250 VMute* v = new VMute();
253 viewman->updateView(v);
267 Message* m = new Message(); // break into master mutex
268 m->message = Message::SCREENSHOT;
274 void Command::doStandby()
278 Video::getInstance()->signalOn();
279 Led::getInstance()->on();
283 VConnect* vconnect = new VConnect();
284 viewman->add(vconnect);
289 Video::getInstance()->signalOff();
290 viewman->removeAll();
291 viewman->updateView(wallpaper);
293 VDR::getInstance()->configSave("General", "Last Power State", "Off");
294 VDR::getInstance()->disconnect();
295 Led::getInstance()->off();
300 void Command::doReboot()
302 VDR::getInstance()->disconnect();
304 logger->log("Command", Log::NOTICE, "Reboot");
305 reboot(LINUX_REBOOT_CMD_RESTART);
308 void Command::doJustConnected(VConnect* vconnect)
311 Video* video = Video::getInstance();
312 viewman->removeView(vconnect);
314 VInfo* vi = new VInfo();
315 vi->create(400, 200);
316 if (video->getFormat() == Video::PAL)
317 vi->setScreenPos(170, 200);
319 vi->setScreenPos(160, 150);
321 vi->setOneLiner(tr("Connected, loading config"));
324 viewman->updateView(vi);
326 VDR* vdr = VDR::getInstance();
329 // See if config says to override video format (PAL/NTSC)
330 config = vdr->configLoad("General", "Override Video Format");
333 logger->log("Command", Log::DEBUG, "Override Video Format is present");
335 if ( (!strcmp(config, "PAL") && (video->getFormat() == Video::NTSC))
336 || (!strcmp(config, "NTSC") && (video->getFormat() == Video::PAL)) )
338 // Oh sheesh, need to switch format. Bye bye TV...
340 // Take everything down
341 viewman->removeAll();
342 viewman->removeView(wallpaper);
343 Osd* osd = Osd::getInstance();
347 // Get video and osd back up with the new mode
348 if (!strcmp(config, "PAL"))
350 logger->log("Command", Log::DEBUG, "Switching to PAL");
351 video->init(Video::PAL);
353 else if (!strcmp(config, "NTSC"))
355 logger->log("Command", Log::DEBUG, "Switching to NTSC");
356 video->init(Video::NTSC);
358 osd->init("/dev/stbgfx");
360 // Put the wallpaper back
365 vi->create(400, 200);
366 if (video->getFormat() == Video::PAL)
367 vi->setScreenPos(170, 200);
369 vi->setScreenPos(160, 150);
371 vi->setOneLiner(tr("Connected, loading config"));
374 viewman->updateView(vi);
378 logger->log("Command", Log::DEBUG, "Already in requested mode, or request was not 'PAL' or 'NTSC'");
383 logger->log("Command", Log::DEBUG, "Phew, no dangerous on-the-fly mode switching to do!");
386 // Power off if first boot and config says so
391 logger->log("Command", Log::DEBUG, "Load power after boot");
393 config = vdr->configLoad("General", "Power After Boot");
397 if (!strcasecmp(config, "On"))
399 logger->log("Command", Log::INFO, "Config says Power After Boot = On");
401 else if (!strcasecmp(config, "Off"))
403 logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
408 else if (!strcasecmp(config, "Last state"))
410 char* lastPowerState = vdr->configLoad("General", "Last Power State");
413 if (!strcasecmp(lastPowerState, "On"))
415 logger->log("Command", Log::INFO, "Config says Last Power State = On");
417 else if (!strcasecmp(lastPowerState, "Off"))
419 logger->log("Command", Log::INFO, "Config says Last Power State = Off");
426 logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
431 logger->log("Command", Log::INFO, "Config General/Last Power State not found");
436 logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
442 logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
447 // Go S-Video if config says so
449 config = vdr->configLoad("TV", "Connection");
453 if (!strcasecmp(config, "S-Video"))
455 logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
456 video->setConnection(Video::SVIDEO);
460 logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
461 video->setConnection(Video::COMPOSITERGB);
467 logger->log("Command", Log::INFO, "Config TV/S-Video not found");
472 config = vdr->configLoad("General", "Remote type");
476 if (!strcasecmp(config, "New"))
478 logger->log("Command", Log::INFO, "Switching to New remote type");
479 remote->setRemoteType(Remote::NEWREMOTE);
483 logger->log("Command", Log::INFO, "Switching to Old remote type");
484 remote->setRemoteType(Remote::OLDREMOTE);
490 logger->log("Command", Log::INFO, "Config General/Remote type not found");
491 remote->setRemoteType(Remote::OLDREMOTE);
494 // Get TV aspect ratio
496 config = vdr->configLoad("TV", "Aspect");
499 if (!strcasecmp(config, "16:9"))
501 logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
502 video->setTVsize(Video::ASPECT16X9);
506 logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
507 video->setTVsize(Video::ASPECT4X3);
513 logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
514 video->setTVsize(Video::ASPECT4X3);
517 config = vdr->configLoad("TV", "Widemode");
520 if (!strcasecmp(config, "Letterbox"))
522 logger->log("Command", Log::INFO, "Setting letterbox mode");
523 video->setMode(Video::LETTERBOX);
527 logger->log("Command", Log::INFO, "Setting chop-sides mode");
528 video->setMode(Video::NORMAL);
534 logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
535 video->setMode(Video::NORMAL);
540 // Save power state = on
542 vdr->configSave("General", "Last Power State", "On");
545 viewman->removeView(vi);
547 VWelcome* vw = new VWelcome();
550 viewman->updateView(vw);