2 Copyright 2004-2020 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, see <https://www.gnu.org/licenses/>.
31 #ifdef HANDLE_VT_SWITCHING
33 #include <sys/ioctl.h>
42 #ifdef VOMP_PLATTFORM_NMT
44 #include "remotelirc.h"
46 #include "osddirectfb.h"
52 #ifdef VOMP_PLATFORM_RASPBERRY
54 #include "remotelinux.h"
55 #include "ledraspberry.h"
56 #include "osdopenvg.h"
64 #include "vsleeptimer.h"
68 void sighandler(int signalReceived);
71 void shutdown(int code);
73 // Global variables --------------------------------------------------------------------------------------------------
85 Sleeptimer* sleeptimer;
87 #ifdef HANDLE_VT_SWITCHING
89 struct vt_mode old_vtmode;
92 // Linux main function and sighandler
94 int main(int argc, char** argv)
96 bool daemonize = true;
97 bool debugEnabled = false;
99 char* setServer = NULL;
102 while ((c = getopt(argc, argv, "cdns:")) != -1)
110 debugEnabled = true; // and...
118 printf("Unknown option\n");
121 printf("Option error\n");
126 // Init global vars ------------------------------------------------------------------------------------------------
128 timers = new Timers();
131 remote = new Remote_TYPE();
132 led = new Led_TYPE();
133 osd = new Osd_TYPE();
134 audio = new Audio_TYPE();
135 video = new Video_TYPE();
139 boxstack = new BoxStack();
140 command = new Command();
142 sleeptimer = new Sleeptimer();
144 if (!logger || !remote || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
146 printf("Could not create objects. Memory problems?\n");
150 // Get logging module started --------------------------------------------------------------------------------------
152 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
154 printf("Could not initialise log object. Aborting.\n");
158 logger->log("Core", Log::INFO, "Starting up...");
160 // Daemonize if not -d
165 pid_t forkTest = fork();
167 { printf("Cannot fork (1).\n"); exit(1); }
168 if (forkTest != 0) _exit(0); // PID returned, I am the parent
169 // otherwise, I am the child
173 { printf("Cannot fork (2).\n"); exit(1); }
174 if (forkTest != 0) _exit(0); // PID returned, I am the parent
175 // otherwise, I am the child
181 // Set up signal handling ------------------------------------------------------------------------------------------
183 sighandler_t sigtest;
185 sigtest = signal(SIGPIPE, SIG_IGN);
186 if (sigtest == SIG_ERR)
188 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
191 sigtest = signal(SIGINT, sighandler);
192 if (sigtest == SIG_ERR)
194 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
197 sigtest = signal(SIGTERM, sighandler);
198 if (sigtest == SIG_ERR)
200 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
203 sigtest = signal(SIGUSR1, sighandler);
204 if (sigtest == SIG_ERR)
206 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
209 sigtest = signal(SIGUSR2, sighandler);
210 if (sigtest == SIG_ERR)
212 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
215 sigtest = signal(SIGURG, sighandler);
216 if (sigtest == SIG_ERR)
218 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
222 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
224 #ifdef HANDLE_VT_SWITCHING
225 if ((fdtty = open("/dev/tty", O_WRONLY),0) == -1) {
226 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
229 if (ioctl(fdtty,VT_OPENQRY,&free_vt)==-1 || free_vt==-1){
230 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
232 ioctl(fdtty,VT_ACTIVATE,free_vt);
233 ioctl(fdtty,VT_WAITACTIVE,free_vt);
234 ioctl(fdtty, VT_LOCKSWITCH, 1);
240 // Init modules ----------------------------------------------------------------------------------------------------
243 success = remote->init(RemoteStartDev);
247 logger->log("Core", Log::INFO, "Remote module initialised");
251 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
255 success = led->init(-1);
258 logger->log("Core", Log::INFO, "LED module initialised");
262 logger->log("Core", Log::EMERG, "LED module failed to initialise");
266 success = timers->init();
269 logger->log("Core", Log::INFO, "Timers module initialised");
273 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
277 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
279 success = video->init(videoFormat);
282 logger->log("Core", Log::INFO, "Video module initialised");
286 logger->log("Core", Log::EMERG, "Video module failed to initialise");
290 success = osd->init();
293 logger->log("Core", Log::INFO, "OSD module initialised");
297 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
301 success = audio->init(Audio::MPEG2_PES);
304 logger->log("Core", Log::INFO, "Audio module initialised");
308 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
312 success = vdr->init();
315 logger->log("Core", Log::INFO, "VDR module initialised");
319 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
323 success = boxstack->init();
326 logger->log("Core", Log::INFO, "BoxStack module initialised");
330 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
334 success = command->init(crashed, setServer);
337 logger->log("Core", Log::INFO, "Command module initialised");
341 logger->log("Core", Log::EMERG, "Command module failed to initialise");
345 // Other init ------------------------------------------------------------------------------------------------------
347 logger->log("Core", Log::NOTICE, "Startup successful");
349 // Run main loop ---------------------------------------------------------------------------------------------------
351 // Ok, all major device components and other bits are loaded and ready
354 // When that returns quit ------------------------------------------------------------------------------------------
359 // -------------------------------------------------------------------------------------------------------------------
361 void sighandler(int signalReceived)
363 if (command) command->setSignal(signalReceived);
368 // -------------------------------------------------------------------------------------------------------------------
370 void shutdown(int code)
374 boxstack->shutdown();
376 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
379 // FIXME, send a del all to boxstack first, then get rid of it after command?
380 if (command) // shut down command here in case views have posted messages
385 logger->log("Core", Log::NOTICE, "Command module shut down");
392 logger->log("Core", Log::NOTICE, "VDR module shut down");
399 logger->log("Core", Log::NOTICE, "OSD module shut down");
406 logger->log("Core", Log::NOTICE, "Audio module shut down");
413 logger->log("Core", Log::NOTICE, "Video module shut down");
420 logger->log("Core", Log::NOTICE, "Timers module shut down");
427 logger->log("Core", Log::NOTICE, "LED module shut down");
434 logger->log("Core", Log::NOTICE, "Remote module shut down");
440 logger->log("Core", Log::NOTICE, "WOL module shut down");
446 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
448 #ifdef HANDLE_VT_SWITCHING
449 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
455 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
463 // -------------------------------------------------------------------------------------------------------------------
465 ULLONG htonll(ULLONG a)
467 #if BYTE_ORDER == BIG_ENDIAN
472 b = ((a << 56) & 0xFF00000000000000ULL)
473 | ((a << 40) & 0x00FF000000000000ULL)
474 | ((a << 24) & 0x0000FF0000000000ULL)
475 | ((a << 8) & 0x000000FF00000000ULL)
476 | ((a >> 8) & 0x00000000FF000000ULL)
477 | ((a >> 24) & 0x0000000000FF0000ULL)
478 | ((a >> 40) & 0x000000000000FF00ULL)
479 | ((a >> 56) & 0x00000000000000FFULL) ;
485 ULLONG ntohll(ULLONG a)
490 void MILLISLEEP(ULONG a)
493 struct timespec delayTime;
494 delayTime.tv_sec = a / 1000;
495 delayTime.tv_nsec = (a % 1000) * 1000000;
496 nanosleep(&delayTime, NULL);
502 long long getTimeMS()
505 clock_gettime(VOMP_LINUX_CLOCK, &ts);
506 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
509 int getClockRealTime(struct timespec *tp)
511 return clock_gettime(CLOCK_REALTIME, tp);
514 int min(UINT a, int b)
520 int max(int a, int b)