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 // post a message to ViewMan and then run the viewman message queue
201 Message* m2 = new Message();
202 m2->message = Message::STREAM_END;
203 m2->to = VVideoLive::getInstance();
204 viewman->postMessage(m2);
205 handleCommand(Remote::NA_NONE);
208 case Message::VDR_CONNECTED:
210 doJustConnected((VConnect*)m->from);
215 // FIXME - go to one message queue only - then instead of having
216 // objects deriving from messagequeues, make them derive from
217 // messagereceiver - then one messagequeue can deliver any message to anywhere
221 ((TimerReceiver*)m->to)->timercall(m->parameter);
222 handleCommand(Remote::NA_NONE); // in case any timer has posted messages to viewman,
223 // run viewman message queue here. FIXME improve this!
226 case Message::SCREENSHOT:
228 Osd::getInstance()->screenShot("/out.jpg");
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();
250 v->handleCommand(button); // this will draw+show
255 VMute* v = new VMute();
258 viewman->updateView(v);
272 Message* m = new Message();
273 m->message = Message::SCREENSHOT;
279 void Command::doStandby()
283 Video::getInstance()->signalOn();
284 Led::getInstance()->on();
288 VConnect* vconnect = new VConnect();
289 viewman->add(vconnect);
294 Video::getInstance()->signalOff();
295 viewman->removeAll();
296 viewman->updateView(wallpaper);
298 VDR::getInstance()->configSave("General", "Last Power State", "Off");
299 VDR::getInstance()->disconnect();
300 Led::getInstance()->off();
305 void Command::doReboot()
307 VDR::getInstance()->disconnect();
309 logger->log("Command", Log::NOTICE, "Reboot");
310 reboot(LINUX_REBOOT_CMD_RESTART);
313 void Command::doJustConnected(VConnect* vconnect)
316 Video* video = Video::getInstance();
317 viewman->removeView(vconnect);
319 VInfo* vi = new VInfo();
320 vi->create(400, 200);
321 if (video->getFormat() == Video::PAL)
322 vi->setScreenPos(170, 200);
324 vi->setScreenPos(160, 150);
326 vi->setOneLiner(tr("Connected, loading config"));
329 viewman->updateView(vi);
331 VDR* vdr = VDR::getInstance();
334 // See if config says to override video format (PAL/NTSC)
335 config = vdr->configLoad("General", "Override Video Format");
338 logger->log("Command", Log::DEBUG, "Override Video Format is present");
340 if ( (!strcmp(config, "PAL") && (video->getFormat() == Video::NTSC))
341 || (!strcmp(config, "NTSC") && (video->getFormat() == Video::PAL)) )
343 // Oh sheesh, need to switch format. Bye bye TV...
345 // Take everything down
346 viewman->removeAll();
347 viewman->removeView(wallpaper);
348 Osd* osd = Osd::getInstance();
352 // Get video and osd back up with the new mode
353 if (!strcmp(config, "PAL"))
355 logger->log("Command", Log::DEBUG, "Switching to PAL");
356 video->init(Video::PAL);
358 else if (!strcmp(config, "NTSC"))
360 logger->log("Command", Log::DEBUG, "Switching to NTSC");
361 video->init(Video::NTSC);
363 osd->init("/dev/stbgfx");
365 // Put the wallpaper back
370 vi->create(400, 200);
371 if (video->getFormat() == Video::PAL)
372 vi->setScreenPos(170, 200);
374 vi->setScreenPos(160, 150);
376 vi->setOneLiner(tr("Connected, loading config"));
379 viewman->updateView(vi);
383 logger->log("Command", Log::DEBUG, "Already in requested mode, or request was not 'PAL' or 'NTSC'");
388 logger->log("Command", Log::DEBUG, "Phew, no dangerous on-the-fly mode switching to do!");
391 // Power off if first boot and config says so
396 logger->log("Command", Log::DEBUG, "Load power after boot");
398 config = vdr->configLoad("General", "Power After Boot");
402 if (!strcasecmp(config, "On"))
404 logger->log("Command", Log::INFO, "Config says Power After Boot = On");
406 else if (!strcasecmp(config, "Off"))
408 logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
413 else if (!strcasecmp(config, "Last state"))
415 char* lastPowerState = vdr->configLoad("General", "Last Power State");
418 if (!strcasecmp(lastPowerState, "On"))
420 logger->log("Command", Log::INFO, "Config says Last Power State = On");
422 else if (!strcasecmp(lastPowerState, "Off"))
424 logger->log("Command", Log::INFO, "Config says Last Power State = Off");
431 logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
436 logger->log("Command", Log::INFO, "Config General/Last Power State not found");
441 logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
447 logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
452 // Go S-Video if config says so
454 config = vdr->configLoad("TV", "Connection");
458 if (!strcasecmp(config, "S-Video"))
460 logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
461 video->setConnection(Video::SVIDEO);
465 logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
466 video->setConnection(Video::COMPOSITERGB);
472 logger->log("Command", Log::INFO, "Config TV/S-Video not found");
477 config = vdr->configLoad("General", "Remote type");
481 if (!strcasecmp(config, "New"))
483 logger->log("Command", Log::INFO, "Switching to New remote type");
484 remote->setRemoteType(Remote::NEWREMOTE);
488 logger->log("Command", Log::INFO, "Switching to Old remote type");
489 remote->setRemoteType(Remote::OLDREMOTE);
495 logger->log("Command", Log::INFO, "Config General/Remote type not found");
496 remote->setRemoteType(Remote::OLDREMOTE);
499 // Get TV aspect ratio
501 config = vdr->configLoad("TV", "Aspect");
504 if (!strcasecmp(config, "16:9"))
506 logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
507 video->setTVsize(Video::ASPECT16X9);
511 logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
512 video->setTVsize(Video::ASPECT4X3);
518 logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
519 video->setTVsize(Video::ASPECT4X3);
522 config = vdr->configLoad("TV", "Widemode");
525 if (!strcasecmp(config, "Letterbox"))
527 logger->log("Command", Log::INFO, "Setting letterbox mode");
528 video->setMode(Video::LETTERBOX);
532 logger->log("Command", Log::INFO, "Setting chop-sides mode");
533 video->setMode(Video::NORMAL);
539 logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
540 video->setMode(Video::NORMAL);
545 // Save power state = on
547 vdr->configSave("General", "Last Power State", "On");
550 viewman->removeView(vi);
552 VWelcome* vw = new VWelcome();
555 viewman->updateView(vw);