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.
40 #include "remotemvp.h"
49 #include "remotelirc.h"
51 #include "osddirectfb.h"
59 #include "vsleeptimer.h"
63 void sighandler(int signalReceived);
66 void shutdown(int code);
72 int ticonfig_main(int, char**);
77 // Global variables --------------------------------------------------------------------------------------------------
90 Sleeptimer* sleeptimer;
92 // Linux MVP main function and sighandler
94 int main(int argc, char** argv)
97 if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
100 bool daemonize = true;
101 bool debugEnabled = false;
102 bool crashed = false;
103 char* setServer = NULL;
106 while ((c = getopt(argc, argv, "cdns:")) != -1)
114 debugEnabled = true; // and...
122 printf("Unknown option\n");
125 printf("Option error\n");
130 // Init global vars ------------------------------------------------------------------------------------------------
132 timers = new Timers();
136 remote = new RemoteMVP();
139 audio = new AudioMVP();
140 video = new VideoMVP();
143 remote = new RemoteLirc();
145 osd = new OsdDirectFB();
146 audio = new AudioNMT();
147 video = new VideoNMT();
151 boxstack = new BoxStack();
152 command = new Command();
154 sleeptimer = new Sleeptimer();
156 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
158 printf("Could not create objects. Memory problems?\n");
162 // Get logging module started --------------------------------------------------------------------------------------
164 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
166 printf("Could not initialise log object. Aborting.\n");
170 logger->log("Core", Log::INFO, "Starting up...");
172 // Daemonize if not -d
177 pid_t forkTest = fork();
179 { printf("Cannot fork (1).\n"); exit(1); }
180 if (forkTest != 0) _exit(0); // PID returned, I am the parent
181 // otherwise, I am the child
185 { printf("Cannot fork (2).\n"); exit(1); }
186 if (forkTest != 0) _exit(0); // PID returned, I am the parent
187 // otherwise, I am the child
193 // Set up signal handling ------------------------------------------------------------------------------------------
195 sighandler_t sigtest;
197 sigtest = signal(SIGPIPE, SIG_IGN);
198 if (sigtest == SIG_ERR)
200 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
203 sigtest = signal(SIGINT, sighandler);
204 if (sigtest == SIG_ERR)
206 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
209 sigtest = signal(SIGTERM, sighandler);
210 if (sigtest == SIG_ERR)
212 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
215 sigtest = signal(SIGUSR1, sighandler);
216 if (sigtest == SIG_ERR)
218 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
222 sigtest = signal(SIGUSR2, sighandler);
223 if (sigtest == SIG_ERR)
225 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
229 sigtest = signal(SIGURG, sighandler);
230 if (sigtest == SIG_ERR)
232 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
236 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
239 // Init modules ----------------------------------------------------------------------------------------------------
242 success = remote->init("/dev/rawir");
244 success = remote->init("/dev/lircd");
248 logger->log("Core", Log::INFO, "Remote module initialised");
252 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
256 success = led->init(((RemoteMVP*)remote)->getDevice());
258 success = led->init(-1);
262 logger->log("Core", Log::INFO, "LED module initialised");
266 logger->log("Core", Log::EMERG, "LED module failed to initialise");
270 success = mtd->init();
273 logger->log("Core", Log::INFO, "Mtd module initialised");
277 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
281 success = timers->init();
284 logger->log("Core", Log::INFO, "Timers module initialised");
288 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
292 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
293 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
294 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
295 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
297 success = video->init(videoFormat);
300 logger->log("Core", Log::INFO, "Video module initialised");
304 logger->log("Core", Log::EMERG, "Video module failed to initialise");
308 success = osd->init((void*)("/dev/stbgfx"));
311 logger->log("Core", Log::INFO, "OSD module initialised");
315 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
319 success = audio->init(Audio::MPEG2_PES);
322 logger->log("Core", Log::INFO, "Audio module initialised");
326 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
330 success = vdr->init(3024);
333 logger->log("Core", Log::INFO, "VDR module initialised");
337 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
341 success = boxstack->init();
344 logger->log("Core", Log::INFO, "BoxStack module initialised");
348 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
352 success = command->init(crashed, setServer);
355 logger->log("Core", Log::INFO, "Command module initialised");
359 logger->log("Core", Log::EMERG, "Command module failed to initialise");
363 // Other init ------------------------------------------------------------------------------------------------------
365 logger->log("Core", Log::NOTICE, "Startup successful");
367 // Run main loop ---------------------------------------------------------------------------------------------------
369 // Ok, all major device components and other bits are loaded and ready
372 // When that returns quit ------------------------------------------------------------------------------------------
378 // -------------------------------------------------------------------------------------------------------------------
380 void sighandler(int signalReceived)
382 logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
384 switch (signalReceived)
388 logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
389 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
394 logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
395 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
406 logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
407 logger->upLogLevel();
412 logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
413 logger->downLogLevel();
419 logger->log("Core", Log::DEBUG, "SIGURG caught");
426 // -------------------------------------------------------------------------------------------------------------------
428 void shutdown(int code)
432 boxstack->shutdown();
434 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
437 // FIXME, send a del all to boxstack first, then get rid of it after command?
438 if (command) // shut down command here in case views have posted messages
442 logger->log("Core", Log::NOTICE, "Command module shut down");
449 logger->log("Core", Log::NOTICE, "VDR module shut down");
456 logger->log("Core", Log::NOTICE, "OSD module shut down");
463 logger->log("Core", Log::NOTICE, "Audio module shut down");
470 logger->log("Core", Log::NOTICE, "Video module shut down");
477 logger->log("Core", Log::NOTICE, "Timers module shut down");
484 logger->log("Core", Log::NOTICE, "MTD module shut down");
491 logger->log("Core", Log::NOTICE, "LED module shut down");
498 logger->log("Core", Log::NOTICE, "Remote module shut down");
504 logger->log("Core", Log::NOTICE, "WOL module shut down");
510 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
515 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
523 // -------------------------------------------------------------------------------------------------------------------
525 ULLONG ntohll(ULLONG a)
530 ULLONG htonll(ULLONG a)
532 #if BYTE_ORDER == BIG_ENDIAN
537 b = ((a << 56) & 0xFF00000000000000ULL)
538 | ((a << 40) & 0x00FF000000000000ULL)
539 | ((a << 24) & 0x0000FF0000000000ULL)
540 | ((a << 8) & 0x000000FF00000000ULL)
541 | ((a >> 8) & 0x00000000FF000000ULL)
542 | ((a >> 24) & 0x0000000000FF0000ULL)
543 | ((a >> 40) & 0x000000000000FF00ULL)
544 | ((a >> 56) & 0x00000000000000FFULL) ;
550 void MILLISLEEP(ULONG a)
553 struct timespec delayTime;
554 delayTime.tv_sec = a / 1000;
555 delayTime.tv_nsec = (a % 1000) * 1000000;
556 nanosleep(&delayTime, NULL);
562 int min(UINT a, int b)
568 int max(int a, int b)