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/>.
21 // Work towards main and winmain only containing what they need for their own platforms
22 // Move anything common to utils or control. Move any module startup / shutdown that can
23 // be done in control to there.
41 #ifdef HANDLE_VT_SWITCHING
42 #include <sys/ioctl.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 --------------------------------------------------------------------------------------------------
86 // Temporary, will move to Config system
87 std::string argvServer;
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;
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();
131 inputMan = new InputMan();
133 led = new Led_TYPE();
134 osd = new Osd_TYPE();
135 audio = new Audio_TYPE();
136 video = new Video_TYPE();
138 control = new Control();
140 if (!logger || !timers || !inputMan || !led || !osd || !video || !audio || !control)
142 printf("Could not create objects. Memory problems?\n");
146 // Get logging module started --------------------------------------------------------------------------------------
148 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0)) // NCONFIG x2
150 printf("Could not initialise log object. Aborting.\n");
154 logger->log("Core", Log::INFO, "Starting up...");
156 // Daemonize if not -d
161 pid_t forkTest = fork();
163 { printf("Cannot fork (1).\n"); exit(1); }
164 if (forkTest != 0) _exit(0); // PID returned, I am the parent
165 // otherwise, I am the child
169 { printf("Cannot fork (2).\n"); exit(1); }
170 if (forkTest != 0) _exit(0); // PID returned, I am the parent
171 // otherwise, I am the child
177 // Set up signal handling ------------------------------------------------------------------------------------------
182 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
185 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
189 // Start signal receiver thread
190 std::thread threadSignalReceiver(threadSignalReceiverFunction);
191 threadSignalReceiver.detach();
193 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
195 // VT Switching -----------------------------------------------------------------------------------------------
197 #ifdef HANDLE_VT_SWITCHING
201 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
206 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
211 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
213 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
217 ioctl(fdtty, VT_ACTIVATE, free_vt);
218 ioctl(fdtty, VT_WAITACTIVE, free_vt);
219 ioctl(fdtty, VT_LOCKSWITCH, 1);
224 // Init modules ----------------------------------------------------------------------------------------------------
227 success = inputMan->init();
231 logger->log("Core", Log::INFO, "InputMan module initialised");
235 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
239 success = led->init(-1);
242 logger->log("Core", Log::INFO, "LED module initialised");
246 logger->log("Core", Log::EMERG, "LED module failed to initialise");
250 success = timers->init();
253 logger->log("Core", Log::INFO, "Timers module initialised");
257 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
261 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
263 success = video->init(videoFormat);
266 logger->log("Core", Log::INFO, "Video module initialised");
270 logger->log("Core", Log::EMERG, "Video module failed to initialise");
274 success = osd->init();
277 logger->log("Core", Log::INFO, "OSD module initialised");
281 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
285 success = audio->init(Audio::MPEG2_PES);
288 logger->log("Core", Log::INFO, "Audio module initialised");
292 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
301 bool bsuccess = control->init(crashed);
304 logger->log("Core", Log::INFO, "Control module initialised");
308 logger->log("Core", Log::EMERG, "Control module failed to initialise");
312 // Other init ------------------------------------------------------------------------------------------------------
314 logger->log("Core", Log::NOTICE, "Startup successful");
316 // Run main loop ---------------------------------------------------------------------------------------------------
318 // Ok, all major device components and other bits are loaded and ready
321 // When that returns quit ------------------------------------------------------------------------------------------
329 // -------------------------------------------------------------------------------------------------------------------
331 void threadSignalReceiverFunction()
338 if(sigwait(&set, &sig))
340 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
344 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
346 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
352 // -------------------------------------------------------------------------------------------------------------------
354 void shutdown(int code)
356 // FIXME, send a del all to boxstack first, then get rid of it after control?
357 if (control) // shut down control here in case views have posted messages
361 logger->log("Core", Log::NOTICE, "Control module shut down");
368 logger->log("Core", Log::NOTICE, "OSD module shut down");
375 logger->log("Core", Log::NOTICE, "Audio module shut down");
382 logger->log("Core", Log::NOTICE, "Video module shut down");
389 logger->log("Core", Log::NOTICE, "Timers module shut down");
396 logger->log("Core", Log::NOTICE, "LED module shut down");
401 inputMan->shutdown();
403 logger->log("Core", Log::NOTICE, "InputMan module shut down");
408 #ifdef HANDLE_VT_SWITCHING
409 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
415 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
423 // -------------------------------------------------------------------------------------------------------------------
425 ULLONG htonll(ULLONG a)
427 #if BYTE_ORDER == BIG_ENDIAN
432 b = ((a << 56) & 0xFF00000000000000ULL)
433 | ((a << 40) & 0x00FF000000000000ULL)
434 | ((a << 24) & 0x0000FF0000000000ULL)
435 | ((a << 8) & 0x000000FF00000000ULL)
436 | ((a >> 8) & 0x00000000FF000000ULL)
437 | ((a >> 24) & 0x0000000000FF0000ULL)
438 | ((a >> 40) & 0x000000000000FF00ULL)
439 | ((a >> 56) & 0x00000000000000FFULL) ;
445 ULLONG ntohll(ULLONG a)
450 void MILLISLEEP(ULONG a)
453 struct timespec delayTime;
454 delayTime.tv_sec = a / 1000;
455 delayTime.tv_nsec = (a % 1000) * 1000000;
456 nanosleep(&delayTime, NULL);
462 long long getTimeMS()
465 clock_gettime(VOMP_LINUX_CLOCK, &ts);
466 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
469 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
471 return clock_gettime(CLOCK_REALTIME, tp);
474 int min(UINT a, int b)
480 int max(int a, int b)
486 const std::string& getCommandLineServer()