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);
74 const std::string& getCommandLineServer();
76 // Global variables --------------------------------------------------------------------------------------------------
88 Sleeptimer* sleeptimer;
90 // Temporary, will move to Config system
91 std::string argvServer;
93 #ifdef HANDLE_VT_SWITCHING
95 struct vt_mode old_vtmode;
98 // Linux main function and sighandler
100 int main(int argc, char** argv)
102 bool daemonize = true;
103 bool debugEnabled = false;
104 bool crashed = false;
107 while ((c = getopt(argc, argv, "cdns:")) != -1)
115 debugEnabled = true; // and...
124 printf("Unknown option\n");
127 printf("Option error\n");
132 // Init global vars ------------------------------------------------------------------------------------------------
134 timers = new Timers();
136 inputMan = new InputMan();
138 led = new Led_TYPE();
139 osd = new Osd_TYPE();
140 audio = new Audio_TYPE();
141 video = new Video_TYPE();
143 boxstack = new BoxStack();
144 command = new Command();
146 sleeptimer = new Sleeptimer();
148 if (!logger || !timers || !inputMan || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
150 printf("Could not create objects. Memory problems?\n");
154 // Get logging module started --------------------------------------------------------------------------------------
156 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
158 printf("Could not initialise log object. Aborting.\n");
162 logger->log("Core", Log::INFO, "Starting up...");
164 // Daemonize if not -d
169 pid_t forkTest = fork();
171 { printf("Cannot fork (1).\n"); exit(1); }
172 if (forkTest != 0) _exit(0); // PID returned, I am the parent
173 // otherwise, I am the child
177 { printf("Cannot fork (2).\n"); exit(1); }
178 if (forkTest != 0) _exit(0); // PID returned, I am the parent
179 // otherwise, I am the child
185 // Set up signal handling ------------------------------------------------------------------------------------------
190 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
193 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
197 // Start signal receiver thread
198 std::thread threadSignalReceiver(threadSignalReceiverFunction);
199 threadSignalReceiver.detach();
201 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
203 // VT Switching -----------------------------------------------------------------------------------------------
205 #ifdef HANDLE_VT_SWITCHING
209 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
214 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
219 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
221 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
225 ioctl(fdtty, VT_ACTIVATE, free_vt);
226 ioctl(fdtty, VT_WAITACTIVE, free_vt);
227 ioctl(fdtty, VT_LOCKSWITCH, 1);
232 // Init modules ----------------------------------------------------------------------------------------------------
235 success = inputMan->init();
239 logger->log("Core", Log::INFO, "InputMan module initialised");
243 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
247 success = led->init(-1);
250 logger->log("Core", Log::INFO, "LED module initialised");
254 logger->log("Core", Log::EMERG, "LED module failed to initialise");
258 success = timers->init();
261 logger->log("Core", Log::INFO, "Timers module initialised");
265 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
269 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
271 success = video->init(videoFormat);
274 logger->log("Core", Log::INFO, "Video module initialised");
278 logger->log("Core", Log::EMERG, "Video module failed to initialise");
282 success = osd->init();
285 logger->log("Core", Log::INFO, "OSD module initialised");
289 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
293 success = audio->init(Audio::MPEG2_PES);
296 logger->log("Core", Log::INFO, "Audio module initialised");
300 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
304 success = vdr->init();
307 logger->log("Core", Log::INFO, "VDR module initialised");
311 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
315 success = boxstack->init();
318 logger->log("Core", Log::INFO, "BoxStack module initialised");
322 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
326 success = command->init(crashed);
329 logger->log("Core", Log::INFO, "Command module initialised");
333 logger->log("Core", Log::EMERG, "Command module failed to initialise");
337 // Other init ------------------------------------------------------------------------------------------------------
339 logger->log("Core", Log::NOTICE, "Startup successful");
341 // Run main loop ---------------------------------------------------------------------------------------------------
343 // Ok, all major device components and other bits are loaded and ready
346 // When that returns quit ------------------------------------------------------------------------------------------
351 // -------------------------------------------------------------------------------------------------------------------
353 void threadSignalReceiverFunction()
360 if(sigwait(&set, &sig))
362 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
366 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
368 if ((sig == SIGINT) || (sig == SIGTERM)) command->stop();
374 // -------------------------------------------------------------------------------------------------------------------
376 void shutdown(int code)
380 boxstack->shutdown();
382 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
385 // FIXME, send a del all to boxstack first, then get rid of it after command?
386 if (command) // shut down command here in case views have posted messages
391 logger->log("Core", Log::NOTICE, "Command module shut down");
398 logger->log("Core", Log::NOTICE, "VDR module shut down");
405 logger->log("Core", Log::NOTICE, "OSD module shut down");
412 logger->log("Core", Log::NOTICE, "Audio module shut down");
419 logger->log("Core", Log::NOTICE, "Video module shut down");
426 logger->log("Core", Log::NOTICE, "Timers module shut down");
433 logger->log("Core", Log::NOTICE, "LED module shut down");
438 inputMan->shutdown();
440 logger->log("Core", Log::NOTICE, "InputMan module shut down");
446 logger->log("Core", Log::NOTICE, "WOL module shut down");
452 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
455 #ifdef HANDLE_VT_SWITCHING
456 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
462 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
470 // -------------------------------------------------------------------------------------------------------------------
472 ULLONG htonll(ULLONG a)
474 #if BYTE_ORDER == BIG_ENDIAN
479 b = ((a << 56) & 0xFF00000000000000ULL)
480 | ((a << 40) & 0x00FF000000000000ULL)
481 | ((a << 24) & 0x0000FF0000000000ULL)
482 | ((a << 8) & 0x000000FF00000000ULL)
483 | ((a >> 8) & 0x00000000FF000000ULL)
484 | ((a >> 24) & 0x0000000000FF0000ULL)
485 | ((a >> 40) & 0x000000000000FF00ULL)
486 | ((a >> 56) & 0x00000000000000FFULL) ;
492 ULLONG ntohll(ULLONG a)
497 void MILLISLEEP(ULONG a)
500 struct timespec delayTime;
501 delayTime.tv_sec = a / 1000;
502 delayTime.tv_nsec = (a % 1000) * 1000000;
503 nanosleep(&delayTime, NULL);
509 long long getTimeMS()
512 clock_gettime(VOMP_LINUX_CLOCK, &ts);
513 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
516 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
518 return clock_gettime(CLOCK_REALTIME, tp);
521 int min(UINT a, int b)
527 int max(int a, int b)
533 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
535 auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
536 std::chrono::milliseconds e = tms.time_since_epoch();
537 long long c = e.count();
538 time_t tt = c / 1000;
540 auto stm = std::localtime(&tt);
541 std::stringstream ss;
542 ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
546 const std::string& getCommandLineServer()