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 ULLONG htonll(ULLONG);
71 ULLONG ntohll(ULLONG);
72 void shutdown(int code);
74 // Global variables --------------------------------------------------------------------------------------------------
86 Sleeptimer* sleeptimer;
88 #ifdef HANDLE_VT_SWITCHING
90 struct vt_mode old_vtmode;
93 // Linux main function and sighandler
95 int main(int argc, char** argv)
97 bool daemonize = true;
98 bool debugEnabled = false;
100 char* setServer = NULL;
103 while ((c = getopt(argc, argv, "cdns:")) != -1)
111 debugEnabled = true; // and...
120 printf("Unknown option\n");
123 printf("Option error\n");
128 // Init global vars ------------------------------------------------------------------------------------------------
130 timers = new Timers();
132 inputMan = new InputMan();
134 led = new Led_TYPE();
135 osd = new Osd_TYPE();
136 audio = new Audio_TYPE();
137 video = new Video_TYPE();
139 boxstack = new BoxStack();
140 command = new Command();
142 sleeptimer = new Sleeptimer();
144 if (!logger || !timers || !inputMan || !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 ------------------------------------------------------------------------------------------
186 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
189 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
193 // Start signal receiver thread
194 std::thread threadSignalReceiver(threadSignalReceiverFunction);
195 threadSignalReceiver.detach();
197 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
199 // VT Switching -----------------------------------------------------------------------------------------------
201 #ifdef HANDLE_VT_SWITCHING
205 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
210 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
215 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
217 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
221 ioctl(fdtty, VT_ACTIVATE, free_vt);
222 ioctl(fdtty, VT_WAITACTIVE, free_vt);
223 ioctl(fdtty, VT_LOCKSWITCH, 1);
228 // Init modules ----------------------------------------------------------------------------------------------------
231 success = inputMan->init();
235 logger->log("Core", Log::INFO, "InputMan module initialised");
239 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
243 success = led->init(-1);
246 logger->log("Core", Log::INFO, "LED module initialised");
250 logger->log("Core", Log::EMERG, "LED module failed to initialise");
254 success = timers->init();
257 logger->log("Core", Log::INFO, "Timers module initialised");
261 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
265 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
267 success = video->init(videoFormat);
270 logger->log("Core", Log::INFO, "Video module initialised");
274 logger->log("Core", Log::EMERG, "Video module failed to initialise");
278 success = osd->init();
281 logger->log("Core", Log::INFO, "OSD module initialised");
285 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
289 success = audio->init(Audio::MPEG2_PES);
292 logger->log("Core", Log::INFO, "Audio module initialised");
296 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
300 success = vdr->init();
303 logger->log("Core", Log::INFO, "VDR module initialised");
307 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
311 success = boxstack->init();
314 logger->log("Core", Log::INFO, "BoxStack module initialised");
318 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
322 success = command->init(crashed, setServer);
325 logger->log("Core", Log::INFO, "Command module initialised");
329 logger->log("Core", Log::EMERG, "Command module failed to initialise");
333 // Other init ------------------------------------------------------------------------------------------------------
335 logger->log("Core", Log::NOTICE, "Startup successful");
337 // Run main loop ---------------------------------------------------------------------------------------------------
339 // Ok, all major device components and other bits are loaded and ready
342 // When that returns quit ------------------------------------------------------------------------------------------
347 // -------------------------------------------------------------------------------------------------------------------
349 void threadSignalReceiverFunction()
356 if(sigwait(&set, &sig))
358 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
362 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
364 if ((sig == SIGINT) || (sig == SIGTERM)) command->stop();
370 // -------------------------------------------------------------------------------------------------------------------
372 void shutdown(int code)
376 boxstack->shutdown();
378 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
381 // FIXME, send a del all to boxstack first, then get rid of it after command?
382 if (command) // shut down command here in case views have posted messages
387 logger->log("Core", Log::NOTICE, "Command module shut down");
394 logger->log("Core", Log::NOTICE, "VDR module shut down");
401 logger->log("Core", Log::NOTICE, "OSD module shut down");
408 logger->log("Core", Log::NOTICE, "Audio module shut down");
415 logger->log("Core", Log::NOTICE, "Video module shut down");
422 logger->log("Core", Log::NOTICE, "Timers module shut down");
429 logger->log("Core", Log::NOTICE, "LED module shut down");
434 inputMan->shutdown();
436 logger->log("Core", Log::NOTICE, "InputMan module shut down");
442 logger->log("Core", Log::NOTICE, "WOL module shut down");
448 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
450 #ifdef HANDLE_VT_SWITCHING
451 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
457 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
465 // -------------------------------------------------------------------------------------------------------------------
467 ULLONG htonll(ULLONG a)
469 #if BYTE_ORDER == BIG_ENDIAN
474 b = ((a << 56) & 0xFF00000000000000ULL)
475 | ((a << 40) & 0x00FF000000000000ULL)
476 | ((a << 24) & 0x0000FF0000000000ULL)
477 | ((a << 8) & 0x000000FF00000000ULL)
478 | ((a >> 8) & 0x00000000FF000000ULL)
479 | ((a >> 24) & 0x0000000000FF0000ULL)
480 | ((a >> 40) & 0x000000000000FF00ULL)
481 | ((a >> 56) & 0x00000000000000FFULL) ;
487 ULLONG ntohll(ULLONG a)
492 void MILLISLEEP(ULONG a)
495 struct timespec delayTime;
496 delayTime.tv_sec = a / 1000;
497 delayTime.tv_nsec = (a % 1000) * 1000000;
498 nanosleep(&delayTime, NULL);
504 long long getTimeMS()
507 clock_gettime(VOMP_LINUX_CLOCK, &ts);
508 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
511 int getClockRealTime(struct timespec *tp)
513 return clock_gettime(CLOCK_REALTIME, tp);
516 int min(UINT a, int b)
522 int max(int a, int b)