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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <linux/errno.h>
28 #include "remotewin.h"
36 #include "vserverselect.h"
42 #include "timerreceiver.h"
45 #include "vvideolive.h"
55 Command* Command::instance = NULL;
74 Command* Command::getInstance()
79 int Command::init(bool tcrashed, char* tServer)
81 if (initted) return 0;
86 logger = Log::getInstance();
87 boxstack = BoxStack::getInstance();
88 remote = Remote::getInstance();
90 remote->InitHWCListwithDefaults();
92 if (!logger || !boxstack || !remote)
98 pthread_mutex_init(&masterLock, NULL);
100 masterLock=CreateMutex(NULL,FALSE,NULL);
106 int Command::shutdown()
108 if (!initted) return 0;
115 // VDR::getInstance()->cancelFindingServer();
120 void Command::doWallpaper()
122 Video* video = Video::getInstance();
125 Boxx* bbg = new Boxx();
126 bbg->setSize(video->getScreenWidth(), video->getScreenHeight());
128 bbg->fillColour(Colour::VIDEOBLUE);
130 boxstack->update(bbg);
131 boxstack->remove(bbg);
134 WJpeg* wallpaperj = new WJpeg();
135 wallpaperj->setSize(video->getScreenWidth(), video->getScreenHeight());
136 wallpaperj->createBuffer();
138 if (video->getFormat() == Video::PAL)
140 logger->log("Command", Log::DEBUG, "PAL wallpaper selected");
141 wallpaperj->init("/wallpaperPAL.jpg");
145 logger->log("Command", Log::DEBUG, "NTSC wallpaper selected");
146 wallpaperj->init("/wallpaperNTSC.jpg");
150 boxstack->add(wallpaperj);
151 boxstack->update(wallpaperj);
153 wallpaper = wallpaperj;
158 if (!initted) return;
165 Video::getInstance()->signalOn();
166 Led::getInstance()->on();
170 // End of startup. Lock the mutex and put the first view up
171 // logger->log("Command", Log::DEBUG, "WANT LOCK");
173 pthread_mutex_lock(&masterLock);
175 WaitForSingleObject(masterLock, INFINITE );
177 //logger->log("Command", Log::DEBUG, "LOCKED");
185 VConnect* vconnect = new VConnect(server);
186 boxstack->add(vconnect);
190 // Start method 2 of getting commands in...
197 //logger->log("Command", Log::DEBUG, "UNLOCK");
199 pthread_mutex_unlock(&masterLock);
201 ReleaseMutex(masterLock);
203 button = remote->getButtonPress(2); // FIXME why is this set to 2 and not 0? so it can quit
204 // something happened, lock and process
206 // logger->log("Command", Log::DEBUG, "WANT LOCK");
208 pthread_mutex_lock(&masterLock);
210 WaitForSingleObject(masterLock, INFINITE );
212 // logger->log("Command", Log::DEBUG, "LOCK");
214 if ((button == Remote::NA_NONE) /*|| (button == Remote::NA_UNKNOWN)*/) continue;
216 if (button != Remote::NA_SIGNAL) handleCommand(button);
217 processMessageQueue();
220 //logger->log("Command", Log::DEBUG, "UNLOCK");
222 pthread_mutex_unlock(&masterLock);
224 ReleaseMutex(masterLock);
228 void Command::postMessage(Message* m)
230 // This is locked here in case the main loop is not waiting for an event, but is processing one
231 // it could be killed but then not react to it because the signal wouldn't cause
232 // remote->getButtonPress to break
233 // locking the mutex ensures that the master thread is waiting on getButtonPress
236 //logger->log("Command", Log::DEBUG, "WANT LOCK");
238 pthread_mutex_lock(&masterLock);
240 WaitForSingleObject(masterLock, INFINITE );
242 //logger->log("Command", Log::DEBUG, "LOCK");
243 MessageQueue::postMessage(m);
246 kill(mainPid, SIGURG);
247 pthread_mutex_unlock(&masterLock);
249 ((RemoteWin*)Remote::getInstance())->Signal();
250 ReleaseMutex(masterLock);
252 //logger->log("Command", Log::DEBUG, "UNLOCK");
255 void Command::postMessageNoLock(Message* m)
257 // As above but use this one if this message is being posted because of a button press
258 // the mutex is already locked, locking around postMessage is not needed as the
259 // queue is guaranteed to be run when the button has been processed
260 MessageQueue::postMessage(m);
263 bool Command::postMessageIfNotBusy(Message* m)
265 // Used for Windows mouse events
267 //logger->log("Command", Log::DEBUG, "TRY LOCK");
269 if (pthread_mutex_trylock(&masterLock) != EBUSY)
271 //logger->log("Command", Log::DEBUG, "LOCK");
272 MessageQueue::postMessage(m);
273 kill(mainPid, SIGURG);
274 pthread_mutex_unlock(&masterLock);
275 //logger->log("Command", Log::DEBUG, "UNLOCK");
283 switch (WaitForSingleObject(masterLock, 0 ))
284 { //FIXME this is not "if not busy" check
285 case WAIT_OBJECT_0: //but with proper argument 0 this did not work
286 // case WAIT_ABANDONED:
287 MessageQueue::postMessage(m);
288 ((RemoteWin*)Remote::getInstance())->Signal();
289 ReleaseMutex(masterLock);
292 case WAIT_ABANDONED: return false;
293 case WAIT_TIMEOUT: return false;
299 void Command::postMessageFromOuterSpace(Message* m)
302 Yet another way of getting messages into Command. This one is for events that
303 are not standard button presses (or UDP generated buttons). It is also not for
304 events that are generated as a result of other events (events that can safely
305 call postMessageNoLock and be guaranteed that the message will be processed
306 because it is known that the queue is currently being processed).
307 This is for events that come from outer space and can occur when the master
308 mutex is locked or not, they need to be queued and executed but it doesn't
310 Actually so far it is for events caused by the video stream - aspect ratio
311 changes. These can occur when the master mutex is locked and so postMessage
312 doesn't work. postMessageNoLock doesn't work because if the mutex *isn't*
313 locked at the time then the message could be sat around a while before
315 The whole message system was at first supposed to prevent the problem of
316 calling a function on an object that had just been deleted, by ordering
317 messages such that all calls are done before object deletion. However,
318 because of the new centralised messaging system and the fact that BoxStack
319 locates the destination object before calling it, the messaging system now
320 allows the kind of sloppy calls it was supposed to stop. Weird huh. This
321 is mentioned here because the video stream might generate an event just as
322 the user hits stop. The mutex is locked, and by the time the message
323 is examined the vvideorec/live has been deleted. This doesn't matter because
324 boxstack will drop the message if it can't find the matching object to
326 Finally, all this is fine and dandy, except that I'm not 100% sure that
327 this sloppy postMessage and hope a queued signal will force it to be processed
328 thingy will actually work. Hmmm.
329 Lastly <g>, I will consider making the naming system a little more sane
333 logger->log("Command", Log::DEBUG, "PMFOS called");
334 MessageQueue::postMessage(m);
337 kill(mainPid, SIGURG);
339 ((RemoteWin*)Remote::getInstance())->Signal();
343 void Command::processMessage(Message* m)
345 // FIXME - a slight modification - how if messagereceivers were to register
346 // themselves as receivers to avoid the calling-a-deleted-object problem
347 // then only deliver/register/unregister would have to be protected
349 logger->log("Command", Log::DEBUG, "processing message %i", m->message);
356 case Message::STANDBY:
364 case Message::STOP_PLAYBACK:
366 handleCommand(Remote::STOP); // an odd way of doing it, but so simple
369 case Message::STREAM_END:
371 VVideoLive::getInstance()->streamEnd();
374 // Also connection_lost comes from player - anywhere else?
378 case Message::VDR_CONNECTED:
380 doJustConnected((VConnect*)m->from);
383 case Message::SCREENSHOT:
385 Osd::getInstance()->screenShot("/out.jpg");
388 case Message::CONNECTION_LOST:
393 case Message::UDP_BUTTON:
395 handleCommand(m->parameter);
398 case Message::CHANGE_LANGUAGE:
400 boxstack->removeAll();
401 boxstack->update(wallpaper);
403 if (!VDR::getInstance()->isConnected()) { connectionLost(); break; }
404 VWelcome* vw = new VWelcome();
407 boxstack->update(vw);
410 case Message::LAST_VIEW_CLOSE:
412 // Shouldn't be done like this. Some generic message pass back from vinfo perhaps
419 // VWelcome* vw = new VWelcome();
421 // boxstack->add(vw);
422 // boxstack->update(vw);
432 Instead of sending through the boxstack, implement a more generic MessageReceiver interface
433 and have potential receivers register with something
434 When a message needs to be delivered, check if the receiver is still registered, if so, deliver the message
435 This could all be done using the existing big command mutex to keep it simple
438 logger->log("Command", Log::DEBUG, "Sending message to boxstack");
439 boxstack->processMessage(m);
443 void Command::handleCommand(int button)
445 if (isStandby && (button != Remote::POWER)) return;
446 if (!connLost && boxstack->handleCommand(button)) return; // don't send to boxstack if connLost
448 // command was not handled
452 case Remote::DF_LEFT:
453 case Remote::DF_RIGHT:
454 case Remote::VOLUMEUP:
455 case Remote::VOLUMEDOWN:
457 VVolume* v = new VVolume();
459 v->handleCommand(button); // this will draw+show
464 VMute* v = new VMute();
478 if (!connLost) return; // if connLost, handle Remote::OK
488 Message* m = new Message(); // break into master mutex
489 m->message = Message::SCREENSHOT;
495 void Command::doStandby()
499 Video::getInstance()->signalOn();
500 Led::getInstance()->on();
504 VConnect* vconnect = new VConnect(server);
505 boxstack->add(vconnect);
510 boxstack->removeAll();
511 Video::getInstance()->signalOff();
512 boxstack->update(wallpaper);
514 VDR::getInstance()->configSave("General", "Last Power State", "Off");
515 VDR::getInstance()->disconnect();
516 Led::getInstance()->off();
519 stop(); //different behavoiur on windows, we exit
524 void Command::doFromTheTop(bool which)
528 connLost = new VInfo();
529 connLost->setSize(360, 200);
530 connLost->createBuffer();
531 if (Video::getInstance()->getFormat() == Video::PAL)
532 connLost->setPosition(190, 170);
534 connLost->setPosition(180, 120);
535 connLost->setOneLiner(tr("Connection lost"));
536 connLost->setDropThrough();
537 connLost->setBorderOn(1);
538 connLost->setTitleBarColour(Colour::DANGER);
539 connLost->okButton();
541 boxstack->add(connLost);
542 boxstack->update(connLost);
543 remote->clearBuffer();
547 VDR::getInstance()->disconnect();
548 boxstack->removeAll();
549 boxstack->update(wallpaper);
551 VConnect* vconnect = new VConnect(server);
552 boxstack->add(vconnect);
557 void Command::doReboot()
559 VDR::getInstance()->disconnect();
561 logger->log("Command", Log::NOTICE, "Reboot");
563 reboot(LINUX_REBOOT_CMD_RESTART);
564 #endif //Would we support this on windows?
567 void Command::connectionLost()
569 Message* m = new Message(); // break into master mutex
570 m->message = Message::CONNECTION_LOST;
572 postMessageNoLock(m);
575 void Command::buildCrashedBox()
577 VInfo* crash = new VInfo();
578 crash->setSize(360, 250);
579 crash->createBuffer();
580 if (Video::getInstance()->getFormat() == Video::PAL)
581 crash->setPosition(190, 146);
583 crash->setPosition(180, 96);
584 crash->setMainText("Oops, vomp crashed.. :(\nPlease report this crash to the author, with as much detail as possible about what you were doing at the time that might have caused the crash.");
585 crash->setBorderOn(1);
586 crash->setTitleBarColour(Colour::DANGER);
588 crash->setExitable();
590 boxstack->add(crash);
591 boxstack->update(crash);
594 void Command::doJustConnected(VConnect* vconnect)
597 if (!VDR::getInstance()->isConnected()) { connectionLost(); return; }
599 Video* video = Video::getInstance();
600 Audio* audio = Audio::getInstance();
601 boxstack->remove(vconnect);
603 VInfo* vi = new VInfo();
604 vi->setSize(400, 200);
606 if (video->getFormat() == Video::PAL)
607 vi->setPosition(170, 200);
609 vi->setPosition(160, 150);
610 vi->setOneLiner(tr("Connected, loading config"));
613 boxstack->update(vi);
615 VDR* vdr = VDR::getInstance();
618 // See if config says to override video format (PAL/NTSC)
619 config = vdr->configLoad("General", "Override Video Format");
622 logger->log("Command", Log::DEBUG, "Override Video Format is present");
624 if ( (!strcmp(config, "PAL") && (video->getFormat() == Video::NTSC))
625 || (!strcmp(config, "NTSC") && (video->getFormat() == Video::PAL)) )
627 // Oh sheesh, need to switch format. Bye bye TV...
629 // Take everything down
630 boxstack->removeAll();
631 boxstack->remove(wallpaper);
632 Osd* osd = Osd::getInstance();
636 // Get video and osd back up with the new mode
637 if (!strcmp(config, "PAL"))
639 logger->log("Command", Log::DEBUG, "Switching to PAL");
640 video->init(Video::PAL);
642 else if (!strcmp(config, "NTSC"))
644 logger->log("Command", Log::DEBUG, "Switching to NTSC");
645 video->init(Video::NTSC);
647 osd->init((char*)("/dev/stbgfx"));
649 // Put the wallpaper back
654 vi->setSize(400, 200);
656 if (video->getFormat() == Video::PAL)
657 vi->setPosition(170, 200);
659 vi->setPosition(160, 150);
661 vi->setOneLiner(tr("Connected, loading config"));
664 boxstack->update(vi);
668 logger->log("Command", Log::DEBUG, "Already in requested mode, or request was not 'PAL' or 'NTSC'");
673 logger->log("Command", Log::DEBUG, "Phew, no dangerous on-the-fly mode switching to do!");
676 // Power off if first boot and config says so
681 logger->log("Command", Log::DEBUG, "Load power after boot");
683 config = vdr->configLoad("General", "Power After Boot");
687 if (!STRCASECMP(config, "On"))
689 logger->log("Command", Log::INFO, "Config says Power After Boot = On");
691 else if (!STRCASECMP(config, "Off"))
693 logger->log("Command", Log::INFO, "Config says Power After Boot = Off");
698 else if (!STRCASECMP(config, "Last state"))
700 char* lastPowerState = vdr->configLoad("General", "Last Power State");
703 if (!STRCASECMP(lastPowerState, "On"))
705 logger->log("Command", Log::INFO, "Config says Last Power State = On");
707 else if (!STRCASECMP(lastPowerState, "Off"))
709 logger->log("Command", Log::INFO, "Config says Last Power State = Off");
716 logger->log("Command", Log::INFO, "Config General/Last Power State not understood");
721 logger->log("Command", Log::INFO, "Config General/Last Power State not found");
726 logger->log("Command", Log::INFO, "Config/Power After Boot not understood");
732 logger->log("Command", Log::INFO, "Config General/Power After Boot not found");
737 // Go S-Video if config says so
739 config = vdr->configLoad("TV", "Connection");
743 if (!STRCASECMP(config, "S-Video"))
745 logger->log("Command", Log::INFO, "Switching to S-Video as Connection=%s", config);
746 video->setConnection(Video::SVIDEO);
750 logger->log("Command", Log::INFO, "Switching to RGB/Composite as Connection=%s", config);
751 video->setConnection(Video::COMPOSITERGB);
757 logger->log("Command", Log::INFO, "Config TV/S-Video not found");
762 config = vdr->configLoad("General", "Remote type");
766 if (!STRCASECMP(config, "New"))
768 logger->log("Command", Log::INFO, "Switching to New remote type");
769 remote->setRemoteType(Remote::NEWREMOTE);
773 logger->log("Command", Log::INFO, "Switching to Old remote type");
774 remote->setRemoteType(Remote::OLDREMOTE);
780 logger->log("Command", Log::INFO, "Config General/Remote type not found");
781 remote->setRemoteType(Remote::OLDREMOTE);
787 // Get TV aspect ratio
789 config = vdr->configLoad("TV", "Aspect");
792 if (!STRCASECMP(config, "16:9"))
794 logger->log("Command", Log::INFO, "/// Switching to TV aspect 16:9");
795 video->setTVsize(Video::ASPECT16X9);
799 logger->log("Command", Log::INFO, "/// Switching to TV aspect 4:3");
800 video->setTVsize(Video::ASPECT4X3);
806 logger->log("Command", Log::INFO, "Config TV/Aspect type not found, going 4:3");
807 video->setTVsize(Video::ASPECT4X3);
810 config = vdr->configLoad("TV", "Widemode");
813 if (!STRCASECMP(config, "Letterbox"))
815 logger->log("Command", Log::INFO, "Setting letterbox mode");
816 video->setMode(Video::LETTERBOX);
820 logger->log("Command", Log::INFO, "Setting chop-sides mode");
821 video->setMode(Video::NORMAL);
827 logger->log("Command", Log::INFO, "Config TV/Widemode not found, Setting chop-sides mode");
828 video->setMode(Video::NORMAL);
831 config = vdr->configLoad("Advanced", "TCP receive window");
834 size_t newTCPsize = atoi(config);
837 logger->log("Command", Log::INFO, "Setting TCP window size %i", newTCPsize);
838 vdr->setReceiveWindow(newTCPsize);
842 logger->log("Command", Log::INFO, "TCP window size not found, setting 2048");
843 vdr->setReceiveWindow(2048); // Default
846 config = vdr->configLoad("Advanced", "Disable WOL");
849 if (!STRCASECMP(config, "Yes"))
851 logger->log("Command", Log::INFO, "Config says disable WOL");
852 Wol::getInstance()->setEnabled(false);
856 logger->log("Command", Log::INFO, "Config says enable WOL");
857 Wol::getInstance()->setEnabled(true);
864 logger->log("Command", Log::INFO, "By default, enable WOL");
865 Wol::getInstance()->setEnabled(true);
867 /* device dependend config */
868 audio->loadOptionsfromServer(vdr);
869 video->loadOptionsfromServer(vdr);
870 remote->loadOptionsfromServer(vdr);
873 // Save power state = on
875 vdr->configSave("General", "Last Power State", "On");
877 // Make sure connection didn't die
878 if (!vdr->isConnected())
880 Command::getInstance()->connectionLost();
884 boxstack->remove(vi);
886 VWelcome* vw = new VWelcome();
889 boxstack->update(vw);
891 // Enter pre-keys here
892 // handleCommand(Remote::OK);
893 // handleCommand(Remote::ONE);
894 // handleCommand(Remote::SIX);
895 // handleCommand(Remote::OK);
896 // handleCommand(Remote::UP);
897 // handleCommand(Remote::PLAY);
898 // handleCommand(Remote::DOWN);
899 // handleCommand(Remote::DOWN);
900 // handleCommand(Remote::DOWN);
901 // handleCommand(Remote::OK);
902 // handleCommand(Remote::RED);