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/>.
32 #ifdef HANDLE_VT_SWITCHING
34 #include <sys/ioctl.h>
43 #ifdef VOMP_PLATTFORM_NMT
46 #include "osddirectfb.h"
52 #ifdef VOMP_PLATFORM_RASPBERRY
54 #include "ledraspberry.h"
55 #include "osdopenvg.h"
63 #include "vsleeptimer.h"
66 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
67 void threadSignalReceiverFunction();
70 void shutdown(int code);
72 // Global variables --------------------------------------------------------------------------------------------------
84 Sleeptimer* sleeptimer;
86 #ifdef HANDLE_VT_SWITCHING
88 struct vt_mode old_vtmode;
91 // Linux main function and sighandler
93 int main(int argc, char** argv)
95 bool daemonize = true;
96 bool debugEnabled = false;
98 char* setServer = NULL;
101 while ((c = getopt(argc, argv, "cdns:")) != -1)
109 debugEnabled = true; // and...
117 printf("Unknown option\n");
120 printf("Option error\n");
125 // Init global vars ------------------------------------------------------------------------------------------------
127 timers = new Timers();
129 inputMan = new InputMan();
131 led = new Led_TYPE();
132 osd = new Osd_TYPE();
133 audio = new Audio_TYPE();
134 video = new Video_TYPE();
136 boxstack = new BoxStack();
137 command = new Command();
139 sleeptimer = new Sleeptimer();
141 if (!logger || !inputMan || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
143 printf("Could not create objects. Memory problems?\n");
147 // Get logging module started --------------------------------------------------------------------------------------
149 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
151 printf("Could not initialise log object. Aborting.\n");
155 logger->log("Core", Log::INFO, "Starting up...");
157 // Daemonize if not -d
162 pid_t forkTest = fork();
164 { printf("Cannot fork (1).\n"); exit(1); }
165 if (forkTest != 0) _exit(0); // PID returned, I am the parent
166 // otherwise, I am the child
170 { printf("Cannot fork (2).\n"); exit(1); }
171 if (forkTest != 0) _exit(0); // PID returned, I am the parent
172 // otherwise, I am the child
178 // Set up signal handling ------------------------------------------------------------------------------------------
183 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
186 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
190 // Start signal receiver thread
191 std::thread threadSignalReceiver(threadSignalReceiverFunction);
192 threadSignalReceiver.detach();
194 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
196 // VT Switching -----------------------------------------------------------------------------------------------
198 #ifdef HANDLE_VT_SWITCHING
202 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
207 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
212 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
214 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
218 ioctl(fdtty, VT_ACTIVATE, free_vt);
219 ioctl(fdtty, VT_WAITACTIVE, free_vt);
220 ioctl(fdtty, VT_LOCKSWITCH, 1);
225 // Init modules ----------------------------------------------------------------------------------------------------
228 success = inputMan->init();
232 logger->log("Core", Log::INFO, "InputMan module initialised");
236 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
240 success = led->init(-1);
243 logger->log("Core", Log::INFO, "LED module initialised");
247 logger->log("Core", Log::EMERG, "LED module failed to initialise");
251 success = timers->init();
254 logger->log("Core", Log::INFO, "Timers module initialised");
258 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
262 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
264 success = video->init(videoFormat);
267 logger->log("Core", Log::INFO, "Video module initialised");
271 logger->log("Core", Log::EMERG, "Video module failed to initialise");
275 success = osd->init();
278 logger->log("Core", Log::INFO, "OSD module initialised");
282 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
286 success = audio->init(Audio::MPEG2_PES);
289 logger->log("Core", Log::INFO, "Audio module initialised");
293 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
297 success = vdr->init();
300 logger->log("Core", Log::INFO, "VDR module initialised");
304 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
308 success = boxstack->init();
311 logger->log("Core", Log::INFO, "BoxStack module initialised");
315 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
319 success = command->init(crashed, setServer);
322 logger->log("Core", Log::INFO, "Command module initialised");
326 logger->log("Core", Log::EMERG, "Command module failed to initialise");
330 // Other init ------------------------------------------------------------------------------------------------------
332 logger->log("Core", Log::NOTICE, "Startup successful");
334 // Run main loop ---------------------------------------------------------------------------------------------------
336 // Ok, all major device components and other bits are loaded and ready
339 // When that returns quit ------------------------------------------------------------------------------------------
344 // -------------------------------------------------------------------------------------------------------------------
346 void threadSignalReceiverFunction()
353 if(sigwait(&set, &sig))
355 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
359 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
361 if ((sig == SIGINT) || (sig == SIGTERM)) command->stop();
367 // -------------------------------------------------------------------------------------------------------------------
369 void shutdown(int code)
373 boxstack->shutdown();
375 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
378 // FIXME, send a del all to boxstack first, then get rid of it after command?
379 if (command) // shut down command here in case views have posted messages
384 logger->log("Core", Log::NOTICE, "Command module shut down");
391 logger->log("Core", Log::NOTICE, "VDR module shut down");
398 logger->log("Core", Log::NOTICE, "OSD module shut down");
405 logger->log("Core", Log::NOTICE, "Audio module shut down");
412 logger->log("Core", Log::NOTICE, "Video module shut down");
419 logger->log("Core", Log::NOTICE, "Timers module shut down");
426 logger->log("Core", Log::NOTICE, "LED module shut down");
431 inputMan->shutdown();
433 logger->log("Core", Log::NOTICE, "InputMan module shut down");
439 logger->log("Core", Log::NOTICE, "WOL module shut down");
445 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
447 #ifdef HANDLE_VT_SWITCHING
448 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
454 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
462 // -------------------------------------------------------------------------------------------------------------------
464 ULLONG htonll(ULLONG a)
466 #if BYTE_ORDER == BIG_ENDIAN
471 b = ((a << 56) & 0xFF00000000000000ULL)
472 | ((a << 40) & 0x00FF000000000000ULL)
473 | ((a << 24) & 0x0000FF0000000000ULL)
474 | ((a << 8) & 0x000000FF00000000ULL)
475 | ((a >> 8) & 0x00000000FF000000ULL)
476 | ((a >> 24) & 0x0000000000FF0000ULL)
477 | ((a >> 40) & 0x000000000000FF00ULL)
478 | ((a >> 56) & 0x00000000000000FFULL) ;
484 ULLONG ntohll(ULLONG a)
489 void MILLISLEEP(ULONG a)
492 struct timespec delayTime;
493 delayTime.tv_sec = a / 1000;
494 delayTime.tv_nsec = (a % 1000) * 1000000;
495 nanosleep(&delayTime, NULL);
501 long long getTimeMS()
504 clock_gettime(VOMP_LINUX_CLOCK, &ts);
505 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
508 int getClockRealTime(struct timespec *tp)
510 return clock_gettime(CLOCK_REALTIME, tp);
513 int min(UINT a, int b)
519 int max(int a, int b)