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);
96 // Global variables --------------------------------------------------------------------------------------------------
109 Sleeptimer* sleeptimer;
111 #ifdef HANDLE_VT_SWITCHING
113 struct vt_mode old_vtmode;
116 // Linux MVP main function and sighandler
118 int main(int argc, char** argv)
120 #ifdef VOMP_PLATTFORM_MVP
121 if (strstr(argv[0], "ticonfig")) return ticonfig_main(argc, argv);
124 bool daemonize = true;
125 bool debugEnabled = false;
126 bool crashed = false;
127 char* setServer = NULL;
130 while ((c = getopt(argc, argv, "cdns:")) != -1)
138 debugEnabled = true; // and...
146 printf("Unknown option\n");
149 printf("Option error\n");
154 // Init global vars ------------------------------------------------------------------------------------------------
156 timers = new Timers();
159 mtd = new Mtd_TYPE();
160 remote = new Remote_TYPE();
161 led = new Led_TYPE();
162 osd = new Osd_TYPE();
163 audio = new Audio_TYPE();
164 video = new Video_TYPE();
168 boxstack = new BoxStack();
169 command = new Command();
171 sleeptimer = new Sleeptimer();
173 if (!logger || !remote || !mtd || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
175 printf("Could not create objects. Memory problems?\n");
179 // Get logging module started --------------------------------------------------------------------------------------
181 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
183 printf("Could not initialise log object. Aborting.\n");
187 logger->log("Core", Log::INFO, "Starting up...");
189 // Daemonize if not -d
194 pid_t forkTest = fork();
196 { printf("Cannot fork (1).\n"); exit(1); }
197 if (forkTest != 0) _exit(0); // PID returned, I am the parent
198 // otherwise, I am the child
202 { printf("Cannot fork (2).\n"); exit(1); }
203 if (forkTest != 0) _exit(0); // PID returned, I am the parent
204 // otherwise, I am the child
210 // Set up signal handling ------------------------------------------------------------------------------------------
212 sighandler_t sigtest;
214 sigtest = signal(SIGPIPE, SIG_IGN);
215 if (sigtest == SIG_ERR)
217 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
220 sigtest = signal(SIGINT, sighandler);
221 if (sigtest == SIG_ERR)
223 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
226 sigtest = signal(SIGTERM, sighandler);
227 if (sigtest == SIG_ERR)
229 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
232 sigtest = signal(SIGUSR1, sighandler);
233 if (sigtest == SIG_ERR)
235 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
238 sigtest = signal(SIGUSR2, sighandler);
239 if (sigtest == SIG_ERR)
241 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
244 sigtest = signal(SIGURG, sighandler);
245 if (sigtest == SIG_ERR)
247 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
251 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
253 #ifdef HANDLE_VT_SWITCHING
254 if ((fdtty = open("/dev/tty", O_WRONLY),0) == -1) {
255 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
258 if (ioctl(fdtty,VT_OPENQRY,&free_vt)==-1 || free_vt==-1){
259 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
261 ioctl(fdtty,VT_ACTIVATE,free_vt);
262 ioctl(fdtty,VT_WAITACTIVE,free_vt);
263 ioctl(fdtty, VT_LOCKSWITCH, 1);
269 // Init modules ----------------------------------------------------------------------------------------------------
272 success = remote->init(RemoteStartDev);
276 logger->log("Core", Log::INFO, "Remote module initialised");
280 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
283 #ifdef VOMP_PLATTFORM_MVP
284 success = led->init(((RemoteMVP*)remote)->getDevice());
286 success = led->init(-1);
290 logger->log("Core", Log::INFO, "LED module initialised");
294 logger->log("Core", Log::EMERG, "LED module failed to initialise");
298 success = mtd->init();
301 logger->log("Core", Log::INFO, "Mtd module initialised");
305 logger->log("Core", Log::EMERG, "Mtd module failed to initialise");
309 success = timers->init();
312 logger->log("Core", Log::INFO, "Timers module initialised");
316 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
320 UCHAR videoFormat = (UCHAR)mtd->getPALorNTSC();
321 if (videoFormat == Video::PAL) logger->log("Core", Log::INFO, "Read from MTD: PAL 720x576");
322 else if (videoFormat == Video::NTSC) logger->log("Core", Log::INFO, "Read from MTD: NTSC 720x480");
323 else logger->log("Core", Log::INFO, "No help from MTD. Assuming NTSC 720x480");
325 success = video->init(videoFormat);
328 logger->log("Core", Log::INFO, "Video module initialised");
332 logger->log("Core", Log::EMERG, "Video module failed to initialise");
336 success = osd->init((void*)OsdStartDev);
339 logger->log("Core", Log::INFO, "OSD module initialised");
343 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
347 success = audio->init(Audio::MPEG2_PES);
350 logger->log("Core", Log::INFO, "Audio module initialised");
354 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
358 success = vdr->init(3024);
361 logger->log("Core", Log::INFO, "VDR module initialised");
365 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
369 success = boxstack->init();
372 logger->log("Core", Log::INFO, "BoxStack module initialised");
376 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
380 success = command->init(crashed, setServer);
383 logger->log("Core", Log::INFO, "Command module initialised");
387 logger->log("Core", Log::EMERG, "Command module failed to initialise");
391 // Other init ------------------------------------------------------------------------------------------------------
393 logger->log("Core", Log::NOTICE, "Startup successful");
395 // Run main loop ---------------------------------------------------------------------------------------------------
397 // Ok, all major device components and other bits are loaded and ready
400 // When that returns quit ------------------------------------------------------------------------------------------
405 // -------------------------------------------------------------------------------------------------------------------
407 void sighandler(int signalReceived)
409 if (command) command->setSignal(signalReceived);
414 // -------------------------------------------------------------------------------------------------------------------
416 void shutdown(int code)
420 boxstack->shutdown();
422 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
425 // FIXME, send a del all to boxstack first, then get rid of it after command?
426 if (command) // shut down command here in case views have posted messages
431 logger->log("Core", Log::NOTICE, "Command module shut down");
438 logger->log("Core", Log::NOTICE, "VDR module shut down");
445 logger->log("Core", Log::NOTICE, "OSD module shut down");
452 logger->log("Core", Log::NOTICE, "Audio module shut down");
459 logger->log("Core", Log::NOTICE, "Video module shut down");
466 logger->log("Core", Log::NOTICE, "Timers module shut down");
473 logger->log("Core", Log::NOTICE, "MTD module shut down");
480 logger->log("Core", Log::NOTICE, "LED module shut down");
487 logger->log("Core", Log::NOTICE, "Remote module shut down");
493 logger->log("Core", Log::NOTICE, "WOL module shut down");
499 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
501 #ifdef HANDLE_VT_SWITCHING
502 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
508 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
516 // -------------------------------------------------------------------------------------------------------------------
518 ULLONG htonll(ULLONG a)
520 #if BYTE_ORDER == BIG_ENDIAN
525 b = ((a << 56) & 0xFF00000000000000ULL)
526 | ((a << 40) & 0x00FF000000000000ULL)
527 | ((a << 24) & 0x0000FF0000000000ULL)
528 | ((a << 8) & 0x000000FF00000000ULL)
529 | ((a >> 8) & 0x00000000FF000000ULL)
530 | ((a >> 24) & 0x0000000000FF0000ULL)
531 | ((a >> 40) & 0x000000000000FF00ULL)
532 | ((a >> 56) & 0x00000000000000FFULL) ;
538 ULLONG ntohll(ULLONG a)
543 void MILLISLEEP(ULONG a)
546 struct timespec delayTime;
547 delayTime.tv_sec = a / 1000;
548 delayTime.tv_nsec = (a % 1000) * 1000000;
549 nanosleep(&delayTime, NULL);
555 long long getTimeMS() {
557 clock_gettime(VOMP_LINUX_CLOCK, &ts);
558 return ts.tv_sec*1000+ts.tv_nsec/1000000LL;
561 int getClockRealTime(struct timespec *tp)
563 return clock_gettime(CLOCK_REALTIME, tp);
567 int min(UINT a, int b)
573 int max(int a, int b)