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;
40 Command* Command::getInstance()
47 if (initted) return 0;
50 logger = Log::getInstance();
51 viewman = ViewMan::getInstance();
52 remote = Remote::getInstance();
54 if (!logger || !viewman || !remote)
60 pthread_mutex_init(&masterLock, NULL);
65 int Command::shutdown()
67 if (!initted) return 0;
74 // VDR::getInstance()->cancelFindingServer();
78 void Command::doWallpaper()
80 Video* video = Video::getInstance();
84 v->create(video->getScreenWidth(), video->getScreenHeight());
85 v->setBackgroundColour(Colour::VIDEOBLUE);
88 viewman->updateView(v);
89 viewman->removeView(v);
92 wallpaper = new VWallpaper();
93 if (video->getFormat() == Video::PAL)
95 logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
96 wallpaper->init("/wallpaperPAL.jpg");
100 logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
101 wallpaper->init("/wallpaperNTSC.jpg");
104 viewman->add(wallpaper);
105 viewman->updateView(wallpaper);
110 if (!initted) return;
116 Video::getInstance()->signalOn();
117 Led::getInstance()->on();
121 // End of startup. Lock the mutex and put the first view up
123 pthread_mutex_lock(&masterLock);
126 VConnect* vconnect = new VConnect();
127 viewman->add(vconnect);
135 pthread_mutex_unlock(&masterLock);
137 button = remote->getButtonPress(2); // FIXME why is this set to 2 and not 0? so it can quit
138 // something happened, lock and process
140 pthread_mutex_lock(&masterLock);
142 if ((button == Remote::NA_NONE) || (button == Remote::NA_UNKNOWN)) continue;
144 if (button != Remote::NA_SIGNAL) handleCommand(button);
145 processMessageQueue();
148 pthread_mutex_unlock(&masterLock);
151 void Command::postMessage(Message* m)
153 // This is locked here in case the main loop is not waiting for an event, but is processing one
154 // it could be killed but then not react to it because the signal wouldn't cause
155 // remote->getButtonPress to break
156 // locking the mutex ensures that the master thread is waiting on getButtonPress
159 pthread_mutex_lock(&masterLock);
160 MessageQueue::postMessage(m);
161 kill(mainPid, SIGURG);
162 pthread_mutex_unlock(&masterLock);
165 void Command::postMessageNoLock(Message* m)
167 // As above but use this one if this message is being posted because of a button press
168 // the mutex is already locked, locking around postMessage is not needed as the
169 // queue is guaranteed to be run when the button has been processed
170 MessageQueue::postMessage(m);
173 bool Command::postMessageIfNotBusy(Message* m)
175 // This is for the timers module
176 // If the masterlock is locked then the timers module wants to
179 if (pthread_mutex_trylock(&masterLock) != EBUSY)
181 MessageQueue::postMessage(m);
182 kill(mainPid, SIGURG);
183 pthread_mutex_unlock(&masterLock);
192 void Command::processMessage(Message* m)
194 logger->log("Command", Log::DEBUG, "processing message %i", m->message);
198 case Message::STANDBY:
203 case Message::STOP_PLAYBACK:
205 handleCommand(Remote::STOP); // an odd way of doing it, but so simple
208 case Message::STREAM_END:
210 VVideoLive::getInstance()->streamEnd();
213 case Message::VDR_CONNECTED:
215 doJustConnected((VConnect*)m->from);
220 // FIXME - go to one message queue only - then instead of having
221 // objects deriving from messagequeues, make them derive from
222 // messagereceiver - then one messagequeue can deliver any message to anywhere
226 ((TimerReceiver*)m->to)->timercall(m->parameter);
227 handleCommand(Remote::NA_NONE); // in case any timer has posted messages to viewman,
228 // run viewman message queue here. FIXME improve this!
231 case Message::SCREENSHOT:
233 Osd::getInstance()->screenShot("/out.jpg");
236 case Message::CONNECTION_LOST:
244 void Command::handleCommand(int button)
246 if (isStandby && (button != Remote::POWER)) return;
248 if (!connLost && viewman->handleCommand(button)) return; // don't send to viewman if connLost
250 // command was not handled
254 case Remote::DF_LEFT:
255 case Remote::DF_RIGHT:
256 case Remote::VOLUMEUP:
257 case Remote::VOLUMEDOWN:
259 VVolume* v = new VVolume();
261 v->handleCommand(button); // this will draw+show
266 VMute* v = new VMute();
269 viewman->updateView(v);
279 if (!connLost) return; // if connLost, handle Remote::OK
289 Message* m = new Message(); // break into master mutex
290 m->message = Message::SCREENSHOT;
296 void Command::doStandby()
300 Video::getInstance()->signalOn();
301 Led::getInstance()->on();
305 VConnect* vconnect = new VConnect();
306 viewman->add(vconnect);
311 viewman->removeAll();
312 Video::getInstance()->signalOff();
313 viewman->updateView(wallpaper);
315 VDR::getInstance()->configSave("General", "Last Power State", "Off");
316 VDR::getInstance()->disconnect();
317 Led::getInstance()->off();
322 void Command::doFromTheTop(bool which)
326 connLost = new VInfo();
327 connLost->create(360, 200);
328 if (Video::getInstance()->getFormat() == Video::PAL)
329 connLost->setScreenPos(190, 170);
331 connLost->setScreenPos(180, 120);
332 connLost->setOneLiner(tr("Connection lost"));
333 connLost->setDropThrough();
334 connLost->setBorderOn(1);
335 connLost->setTitleBarColour(Colour::DANGER);
336 connLost->okButton();
338 viewman->add(connLost);
339 viewman->updateView(connLost);
343 VDR::getInstance()->disconnect();
344 viewman->removeAll();
345 viewman->updateView(wallpaper);
347 VConnect* vconnect = new VConnect();
348 viewman->add(vconnect);
353 void Command::doReboot()
355 VDR::getInstance()->disconnect();
357 logger->log("Command", Log::NOTICE, "Reboot");
358 reboot(LINUX_REBOOT_CMD_RESTART);
361 void Command::connectionLost()
363 Message* m = new Message(); // break into master mutex
364 m->message = Message::CONNECTION_LOST;
366 postMessageNoLock(m);
369 void Command::doJustConnected(VConnect* vconnect)
372 Video* video = Video::getInstance();
373 viewman->removeView(vconnect);
375 VInfo* vi = new VInfo();
376 vi->create(400, 200);
377 if (video->getFormat() == Video::PAL)
378 vi->setScreenPos(170, 200);
380 vi->setScreenPos(160, 150);
382 vi->setOneLiner(tr("Connected, loading config"));
385 viewman->updateView(vi);
387 VDR* vdr = VDR::getInstance();
390 // See if config says to override video format (PAL/NTSC)
391 config = vdr->configLoad("General", "Override Video Format");
394 logger->log("Command", Log::DEBUG, "Override Video Format is present");
396 if ( (!strcmp(config, "PAL") && (video->getFormat() == Video::NTSC))
397 || (!strcmp(config, "NTSC") && (video->getFormat() == Video::PAL)) )
399 // Oh sheesh, need to switch format. Bye bye TV...
401 // Take everything down
402 viewman->removeAll();
403 viewman->removeView(wallpaper);
404 Osd* osd = Osd::getInstance();
408 // Get video and osd back up with the new mode
409 if (!strcmp(config, "PAL"))
411 logger->log("Command", Log::DEBUG, "Switching to PAL");
412 video->init(Video::PAL);
414 else if (!strcmp(config, "NTSC"))
416 logger->log("Command", Log::DEBUG, "Switching to NTSC");
417 video->init(Video::NTSC);
419 osd->init("/dev/stbgfx");
421 // Put the wallpaper back
426 vi->create(400, 200);
427 if (video->getFormat() == Video::PAL)
428 vi->setScreenPos(170, 200);
430 vi->setScreenPos(160, 150);
432 vi->setOneLiner(tr("Connected, loading config"));
435 viewman->updateView(vi);
439 logger->log("Command", Log::DEBUG, "Already in requested mode, or request was not 'PAL' or 'NTSC'");
444 logger->log("Command", Log::DEBUG, "Phew, no dangerous on-the-fly mode switching to do!");
447 // Power off if first boot and config says so
452 logger->log("Command", Log::DEBUG, "Load power after boot");
454 config = vdr->configLoad("General", "Power After Boot");
458 if (!strcasecmp(config, "On"))
460 logger->log("Command", Log::INFO, "Config says Power After Boot = On");
462 else if (!strcasecmp(config, "Off"))
464 logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
469 else if (!strcasecmp(config, "Last state"))
471 char* lastPowerState = vdr->configLoad("General", "Last Power State");
474 if (!strcasecmp(lastPowerState, "On"))
476 logger->log("Command", Log::INFO, "Config says Last Power State = On");
478 else if (!strcasecmp(lastPowerState, "Off"))
480 logger->log("Command", Log::INFO, "Config says Last Power State = Off");
487 logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
492 logger->log("Command", Log::INFO, "Config General/Last Power State not found");
497 logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
503 logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
508 // Go S-Video if config says so
510 config = vdr->configLoad("TV", "Connection");
514 if (!strcasecmp(config, "S-Video"))
516 logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
517 video->setConnection(Video::SVIDEO);
521 logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
522 video->setConnection(Video::COMPOSITERGB);
528 logger->log("Command", Log::INFO, "Config TV/S-Video not found");
533 config = vdr->configLoad("General", "Remote type");
537 if (!strcasecmp(config, "New"))
539 logger->log("Command", Log::INFO, "Switching to New remote type");
540 remote->setRemoteType(Remote::NEWREMOTE);
544 logger->log("Command", Log::INFO, "Switching to Old remote type");
545 remote->setRemoteType(Remote::OLDREMOTE);
551 logger->log("Command", Log::INFO, "Config General/Remote type not found");
552 remote->setRemoteType(Remote::OLDREMOTE);
555 // Get TV aspect ratio
557 config = vdr->configLoad("TV", "Aspect");
560 if (!strcasecmp(config, "16:9"))
562 logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
563 video->setTVsize(Video::ASPECT16X9);
567 logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
568 video->setTVsize(Video::ASPECT4X3);
574 logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
575 video->setTVsize(Video::ASPECT4X3);
578 config = vdr->configLoad("TV", "Widemode");
581 if (!strcasecmp(config, "Letterbox"))
583 logger->log("Command", Log::INFO, "Setting letterbox mode");
584 video->setMode(Video::LETTERBOX);
588 logger->log("Command", Log::INFO, "Setting chop-sides mode");
589 video->setMode(Video::NORMAL);
595 logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
596 video->setMode(Video::NORMAL);
599 config = vdr->configLoad("Advanced", "TCP receive window");
602 size_t newTCPsize = atoi(config);
605 logger->log("Command", Log::INFO, "Setting TCP window size %i", newTCPsize);
606 vdr->setReceiveWindow(newTCPsize);
610 logger->log("Command", Log::INFO, "TCP window size not found, setting 2048");
611 vdr->setReceiveWindow(2048); // Default
617 // Save power state = on
619 vdr->configSave("General", "Last Power State", "On");
622 viewman->removeView(vi);
624 VWelcome* vw = new VWelcome();
627 viewman->updateView(vw);
629 // Enter pre-keys here
630 // handleCommand(Remote::THREE);
631 // handleCommand(Remote::DOWN);
632 // handleCommand(Remote::OK);
633 // handleCommand(Remote::PLAY);