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.
32 #ifdef HANDLE_VT_SWITCHING
34 #include <sys/ioctl.h>
44 #ifdef VOMP_PLATTFORM_MVP
48 #include "remotemvp.h"
56 int ticonfig_main(int, char**);
61 #ifdef VOMP_PLATTFORM_NMT
64 #include "remotelirc.h"
66 #include "osddirectfb.h"
72 #ifdef VOMP_PLATTFORM_RASPBERRY
74 #include "mtdraspberry.h"
75 #include "remotelinux.h"
76 #include "ledraspberry.h"
77 #include "osdopenvg.h"
87 #include "vsleeptimer.h"
91 void sighandler(int signalReceived);
94 void shutdown(int code);
98 // Global variables --------------------------------------------------------------------------------------------------
111 Sleeptimer* sleeptimer;
113 #ifdef HANDLE_VT_SWITCHING
115 struct vt_mode old_vtmode;
118 // Linux MVP main function and sighandler
120 int main(int argc, char** argv)
122 #ifdef VOMP_PLATTFORM_MVP
123 if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
126 bool daemonize = true;
127 bool debugEnabled = false;
128 bool crashed = false;
129 char* setServer = NULL;
132 while ((c = getopt(argc, argv, "cdns:")) != -1)
140 debugEnabled = true; // and...
148 printf("Unknown option\n");
151 printf("Option error\n");
156 // Init global vars ------------------------------------------------------------------------------------------------
158 timers = new Timers();
161 mtd = new Mtd_TYPE();
162 remote = new Remote_TYPE();
163 led = new Led_TYPE();
164 osd = new Osd_TYPE();
165 audio = new Audio_TYPE();
166 video = new Video_TYPE();
170 boxstack = new BoxStack();
171 command = new Command();
173 sleeptimer = new Sleeptimer();
175 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
177 printf("Could not create objects. Memory problems?\n");
181 // Get logging module started --------------------------------------------------------------------------------------
183 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
185 printf("Could not initialise log object. Aborting.\n");
189 logger->log("Core", Log::INFO, "Starting up...");
191 // Daemonize if not -d
196 pid_t forkTest = fork();
198 { printf("Cannot fork (1).\n"); exit(1); }
199 if (forkTest != 0) _exit(0); // PID returned, I am the parent
200 // otherwise, I am the child
204 { printf("Cannot fork (2).\n"); exit(1); }
205 if (forkTest != 0) _exit(0); // PID returned, I am the parent
206 // otherwise, I am the child
212 // Set up signal handling ------------------------------------------------------------------------------------------
214 sighandler_t sigtest;
216 sigtest = signal(SIGPIPE, SIG_IGN);
217 if (sigtest == SIG_ERR)
219 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
222 sigtest = signal(SIGINT, sighandler);
223 if (sigtest == SIG_ERR)
225 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
228 sigtest = signal(SIGTERM, sighandler);
229 if (sigtest == SIG_ERR)
231 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
234 sigtest = signal(SIGUSR1, sighandler);
235 if (sigtest == SIG_ERR)
237 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
241 sigtest = signal(SIGUSR2, sighandler);
242 if (sigtest == SIG_ERR)
244 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
248 sigtest = signal(SIGURG, sighandler);
249 if (sigtest == SIG_ERR)
251 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
255 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
257 #ifdef HANDLE_VT_SWITCHING
258 if ((fdtty = open("/dev/tty", O_WRONLY),0) == -1) {
259 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
262 if (ioctl(fdtty,VT_OPENQRY,&free_vt)==-1 || free_vt==-1){
263 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
265 ioctl(fdtty,VT_ACTIVATE,free_vt);
266 ioctl(fdtty,VT_WAITACTIVE,free_vt);
267 ioctl(fdtty, VT_LOCKSWITCH, 1);
273 // Init modules ----------------------------------------------------------------------------------------------------
276 success = remote->init(RemoteStartDev);
280 logger->log("Core", Log::INFO, "Remote module initialised");
284 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
287 #ifdef VOMP_PLATTFORM_MVP
288 success = led->init(((RemoteMVP*)remote)->getDevice());
290 success = led->init(-1);
294 logger->log("Core", Log::INFO, "LED module initialised");
298 logger->log("Core", Log::EMERG, "LED module failed to initialise");
302 success = mtd->init();
305 logger->log("Core", Log::INFO, "Mtd module initialised");
309 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
313 success = timers->init();
316 logger->log("Core", Log::INFO, "Timers module initialised");
320 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
324 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
325 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
326 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
327 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
329 success = video->init(videoFormat);
332 logger->log("Core", Log::INFO, "Video module initialised");
336 logger->log("Core", Log::EMERG, "Video module failed to initialise");
340 success = osd->init((void*)OsdStartDev);
343 logger->log("Core", Log::INFO, "OSD module initialised");
347 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
351 success = audio->init(Audio::MPEG2_PES);
354 logger->log("Core", Log::INFO, "Audio module initialised");
358 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
362 success = vdr->init(3024);
365 logger->log("Core", Log::INFO, "VDR module initialised");
369 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
373 success = boxstack->init();
376 logger->log("Core", Log::INFO, "BoxStack module initialised");
380 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
384 success = command->init(crashed, setServer);
387 logger->log("Core", Log::INFO, "Command module initialised");
391 logger->log("Core", Log::EMERG, "Command module failed to initialise");
395 // Other init ------------------------------------------------------------------------------------------------------
397 logger->log("Core", Log::NOTICE, "Startup successful");
399 // Run main loop ---------------------------------------------------------------------------------------------------
401 // Ok, all major device components and other bits are loaded and ready
404 // When that returns quit ------------------------------------------------------------------------------------------
410 // -------------------------------------------------------------------------------------------------------------------
412 void sighandler(int signalReceived)
414 logger->log("Core", Log::NOTICE, "Signal %i received", signalReceived);
416 switch (signalReceived)
420 logger->log("Core", Log::NOTICE, "Interrupt signal, shutting down...");
421 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
426 logger->log("Core", Log::NOTICE, "Term signal, shutting down...");
427 command->stop(); // FIXME this is probably not safe - use the messaging system / is that even safe?
438 logger->log("Core", Log::DEBUG, "SIGUSR1 caught");
439 logger->upLogLevel();
444 logger->log("Core", Log::DEBUG, "SIGUSR2 caught");
445 logger->downLogLevel();
451 logger->log("Core", Log::DEBUG, "SIGURG caught");
458 // -------------------------------------------------------------------------------------------------------------------
460 void shutdown(int code)
464 boxstack->shutdown();
466 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
469 // FIXME, send a del all to boxstack first, then get rid of it after command?
470 if (command) // shut down command here in case views have posted messages
474 logger->log("Core", Log::NOTICE, "Command module shut down");
481 logger->log("Core", Log::NOTICE, "VDR module shut down");
488 logger->log("Core", Log::NOTICE, "OSD module shut down");
495 logger->log("Core", Log::NOTICE, "Audio module shut down");
502 logger->log("Core", Log::NOTICE, "Video module shut down");
509 logger->log("Core", Log::NOTICE, "Timers module shut down");
516 logger->log("Core", Log::NOTICE, "MTD module shut down");
523 logger->log("Core", Log::NOTICE, "LED module shut down");
530 logger->log("Core", Log::NOTICE, "Remote module shut down");
536 logger->log("Core", Log::NOTICE, "WOL module shut down");
542 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
544 #ifdef HANDLE_VT_SWITCHING
545 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
551 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
559 // -------------------------------------------------------------------------------------------------------------------
561 ULLONG htonll(ULLONG a)
563 #if BYTE_ORDER == BIG_ENDIAN
568 b = ((a << 56) & 0xFF00000000000000ULL)
569 | ((a << 40) & 0x00FF000000000000ULL)
570 | ((a << 24) & 0x0000FF0000000000ULL)
571 | ((a << 8) & 0x000000FF00000000ULL)
572 | ((a >> 8) & 0x00000000FF000000ULL)
573 | ((a >> 24) & 0x0000000000FF0000ULL)
574 | ((a >> 40) & 0x000000000000FF00ULL)
575 | ((a >> 56) & 0x00000000000000FFULL) ;
581 ULLONG ntohll(ULLONG a)
586 void MILLISLEEP(ULONG a)
589 struct timespec delayTime;
590 delayTime.tv_sec = a / 1000;
591 delayTime.tv_nsec = (a % 1000) * 1000000;
592 nanosleep(&delayTime, NULL);
598 long long getTimeMS() {
600 clock_gettime(VOMP_LINUX_CLOCK, &ts);
601 return ts.tv_sec*1000+ts.tv_nsec/1000000LL;
604 int min(UINT a, int b)
610 int max(int a, int b)