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.
42 #ifdef HANDLE_VT_SWITCHING
43 #include <sys/ioctl.h>
53 #ifdef VOMP_PLATTFORM_NMT
55 #include "osddirectfb.h"
60 #ifdef VOMP_PLATFORM_RASPBERRY
61 #include "ledraspberry.h"
62 #include "osdopenvg.h"
69 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
70 void threadSignalReceiverFunction();
73 ULLONG htonll(ULLONG);
74 ULLONG ntohll(ULLONG);
75 void shutdown(int code);
76 const std::string& getCommandLineServer();
78 // Global variables --------------------------------------------------------------------------------------------------
89 // Temporary, will move to Config system
90 std::string argvServer;
92 #ifdef HANDLE_VT_SWITCHING
94 struct vt_mode old_vtmode;
97 // Linux main function and sighandler
99 int main(int argc, char** argv)
101 bool daemonize = true;
102 bool debugEnabled = false;
103 bool crashed = false;
106 while ((c = getopt(argc, argv, "cdns:")) != -1)
114 debugEnabled = true; // and...
123 printf("Unknown option\n");
126 printf("Option error\n");
131 // Init global vars ------------------------------------------------------------------------------------------------
133 timers = new Timers();
135 inputMan = new InputMan();
137 led = new Led_TYPE();
138 osd = new Osd_TYPE();
139 audio = new Audio_TYPE();
140 video = new Video_TYPE();
142 control = new Control();
144 if (!logger || !timers || !inputMan || !led || !osd || !video || !audio || !control)
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");
315 bool bsuccess = control->init(crashed);
318 logger->log("Core", Log::INFO, "Control module initialised");
322 logger->log("Core", Log::EMERG, "Control module failed to initialise");
326 // Other init ------------------------------------------------------------------------------------------------------
328 logger->log("Core", Log::NOTICE, "Startup successful");
330 // Run main loop ---------------------------------------------------------------------------------------------------
332 // Ok, all major device components and other bits are loaded and ready
335 // When that returns quit ------------------------------------------------------------------------------------------
343 // -------------------------------------------------------------------------------------------------------------------
345 void threadSignalReceiverFunction()
352 if(sigwait(&set, &sig))
354 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
358 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
360 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
366 // -------------------------------------------------------------------------------------------------------------------
368 void shutdown(int code)
370 // FIXME, send a del all to boxstack first, then get rid of it after control?
371 if (control) // shut down control here in case views have posted messages
375 logger->log("Core", Log::NOTICE, "Control module shut down");
382 logger->log("Core", Log::NOTICE, "VDR module shut down");
389 logger->log("Core", Log::NOTICE, "OSD module shut down");
396 logger->log("Core", Log::NOTICE, "Audio module shut down");
403 logger->log("Core", Log::NOTICE, "Video module shut down");
410 logger->log("Core", Log::NOTICE, "Timers module shut down");
417 logger->log("Core", Log::NOTICE, "LED module shut down");
422 inputMan->shutdown();
424 logger->log("Core", Log::NOTICE, "InputMan module shut down");
429 #ifdef HANDLE_VT_SWITCHING
430 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
436 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
444 // -------------------------------------------------------------------------------------------------------------------
446 ULLONG htonll(ULLONG a)
448 #if BYTE_ORDER == BIG_ENDIAN
453 b = ((a << 56) & 0xFF00000000000000ULL)
454 | ((a << 40) & 0x00FF000000000000ULL)
455 | ((a << 24) & 0x0000FF0000000000ULL)
456 | ((a << 8) & 0x000000FF00000000ULL)
457 | ((a >> 8) & 0x00000000FF000000ULL)
458 | ((a >> 24) & 0x0000000000FF0000ULL)
459 | ((a >> 40) & 0x000000000000FF00ULL)
460 | ((a >> 56) & 0x00000000000000FFULL) ;
466 ULLONG ntohll(ULLONG a)
471 void MILLISLEEP(ULONG a)
474 struct timespec delayTime;
475 delayTime.tv_sec = a / 1000;
476 delayTime.tv_nsec = (a % 1000) * 1000000;
477 nanosleep(&delayTime, NULL);
483 long long getTimeMS()
486 clock_gettime(VOMP_LINUX_CLOCK, &ts);
487 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
490 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
492 return clock_gettime(CLOCK_REALTIME, tp);
495 int min(UINT a, int b)
501 int max(int a, int b)
507 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
509 auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
510 std::chrono::milliseconds e = tms.time_since_epoch();
511 long long c = e.count();
512 time_t tt = c / 1000;
514 auto stm = std::localtime(&tt);
515 std::stringstream ss;
516 ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
520 const std::string& getCommandLineServer()