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/>.
37 #ifdef HANDLE_VT_SWITCHING
38 #include <sys/ioctl.h>
49 #include "vsleeptimer.h"
51 #ifdef VOMP_PLATTFORM_NMT
53 #include "osddirectfb.h"
58 #ifdef VOMP_PLATFORM_RASPBERRY
59 #include "ledraspberry.h"
60 #include "osdopenvg.h"
67 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
68 void threadSignalReceiverFunction();
71 ULLONG htonll(ULLONG);
72 ULLONG ntohll(ULLONG);
73 void shutdown(int code);
75 // Global variables --------------------------------------------------------------------------------------------------
87 Sleeptimer* sleeptimer;
89 #ifdef HANDLE_VT_SWITCHING
91 struct vt_mode old_vtmode;
94 // Linux main function and sighandler
96 int main(int argc, char** argv)
98 bool daemonize = true;
99 bool debugEnabled = false;
100 bool crashed = false;
101 char* setServer = NULL;
104 while ((c = getopt(argc, argv, "cdns:")) != -1)
112 debugEnabled = true; // and...
121 printf("Unknown option\n");
124 printf("Option error\n");
129 // Init global vars ------------------------------------------------------------------------------------------------
131 timers = new Timers();
133 inputMan = new InputMan();
135 led = new Led_TYPE();
136 osd = new Osd_TYPE();
137 audio = new Audio_TYPE();
138 video = new Video_TYPE();
140 boxstack = new BoxStack();
141 command = new Command();
143 sleeptimer = new Sleeptimer();
145 if (!logger || !timers || !inputMan || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
147 printf("Could not create objects. Memory problems?\n");
151 // Get logging module started --------------------------------------------------------------------------------------
153 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
155 printf("Could not initialise log object. Aborting.\n");
159 logger->log("Core", Log::INFO, "Starting up...");
161 // Daemonize if not -d
166 pid_t forkTest = fork();
168 { printf("Cannot fork (1).\n"); exit(1); }
169 if (forkTest != 0) _exit(0); // PID returned, I am the parent
170 // otherwise, I am the child
174 { printf("Cannot fork (2).\n"); exit(1); }
175 if (forkTest != 0) _exit(0); // PID returned, I am the parent
176 // otherwise, I am the child
182 // Set up signal handling ------------------------------------------------------------------------------------------
187 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
190 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
194 // Start signal receiver thread
195 std::thread threadSignalReceiver(threadSignalReceiverFunction);
196 threadSignalReceiver.detach();
198 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
200 // VT Switching -----------------------------------------------------------------------------------------------
202 #ifdef HANDLE_VT_SWITCHING
206 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
211 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
216 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
218 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
222 ioctl(fdtty, VT_ACTIVATE, free_vt);
223 ioctl(fdtty, VT_WAITACTIVE, free_vt);
224 ioctl(fdtty, VT_LOCKSWITCH, 1);
229 // Init modules ----------------------------------------------------------------------------------------------------
232 success = inputMan->init();
236 logger->log("Core", Log::INFO, "InputMan module initialised");
240 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
244 success = led->init(-1);
247 logger->log("Core", Log::INFO, "LED module initialised");
251 logger->log("Core", Log::EMERG, "LED module failed to initialise");
255 success = timers->init();
258 logger->log("Core", Log::INFO, "Timers module initialised");
262 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
266 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
268 success = video->init(videoFormat);
271 logger->log("Core", Log::INFO, "Video module initialised");
275 logger->log("Core", Log::EMERG, "Video module failed to initialise");
279 success = osd->init();
282 logger->log("Core", Log::INFO, "OSD module initialised");
286 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
290 success = audio->init(Audio::MPEG2_PES);
293 logger->log("Core", Log::INFO, "Audio module initialised");
297 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
301 success = vdr->init();
304 logger->log("Core", Log::INFO, "VDR module initialised");
308 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
312 success = boxstack->init();
315 logger->log("Core", Log::INFO, "BoxStack module initialised");
319 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
323 success = command->init(crashed, setServer);
326 logger->log("Core", Log::INFO, "Command module initialised");
330 logger->log("Core", Log::EMERG, "Command module failed to initialise");
334 // Other init ------------------------------------------------------------------------------------------------------
336 logger->log("Core", Log::NOTICE, "Startup successful");
338 // Run main loop ---------------------------------------------------------------------------------------------------
340 // Ok, all major device components and other bits are loaded and ready
343 // When that returns quit ------------------------------------------------------------------------------------------
348 // -------------------------------------------------------------------------------------------------------------------
350 void threadSignalReceiverFunction()
357 if(sigwait(&set, &sig))
359 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
363 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
365 if ((sig == SIGINT) || (sig == SIGTERM)) command->stop();
371 // -------------------------------------------------------------------------------------------------------------------
373 void shutdown(int code)
377 boxstack->shutdown();
379 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
382 // FIXME, send a del all to boxstack first, then get rid of it after command?
383 if (command) // shut down command here in case views have posted messages
388 logger->log("Core", Log::NOTICE, "Command module shut down");
395 logger->log("Core", Log::NOTICE, "VDR module shut down");
402 logger->log("Core", Log::NOTICE, "OSD module shut down");
409 logger->log("Core", Log::NOTICE, "Audio module shut down");
416 logger->log("Core", Log::NOTICE, "Video module shut down");
423 logger->log("Core", Log::NOTICE, "Timers module shut down");
430 logger->log("Core", Log::NOTICE, "LED module shut down");
435 inputMan->shutdown();
437 logger->log("Core", Log::NOTICE, "InputMan module shut down");
443 logger->log("Core", Log::NOTICE, "WOL module shut down");
449 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
452 #ifdef HANDLE_VT_SWITCHING
453 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
459 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
467 // -------------------------------------------------------------------------------------------------------------------
469 ULLONG htonll(ULLONG a)
471 #if BYTE_ORDER == BIG_ENDIAN
476 b = ((a << 56) & 0xFF00000000000000ULL)
477 | ((a << 40) & 0x00FF000000000000ULL)
478 | ((a << 24) & 0x0000FF0000000000ULL)
479 | ((a << 8) & 0x000000FF00000000ULL)
480 | ((a >> 8) & 0x00000000FF000000ULL)
481 | ((a >> 24) & 0x0000000000FF0000ULL)
482 | ((a >> 40) & 0x000000000000FF00ULL)
483 | ((a >> 56) & 0x00000000000000FFULL) ;
489 ULLONG ntohll(ULLONG a)
494 void MILLISLEEP(ULONG a)
497 struct timespec delayTime;
498 delayTime.tv_sec = a / 1000;
499 delayTime.tv_nsec = (a % 1000) * 1000000;
500 nanosleep(&delayTime, NULL);
506 long long getTimeMS()
509 clock_gettime(VOMP_LINUX_CLOCK, &ts);
510 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
513 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
515 return clock_gettime(CLOCK_REALTIME, tp);
518 int min(UINT a, int b)
524 int max(int a, int b)
530 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
532 auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
533 std::chrono::milliseconds e = tms.time_since_epoch();
534 long long c = e.count();
535 time_t tt = c / 1000;
537 auto stm = std::localtime(&tt);
538 std::stringstream ss;
539 ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;