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 #include "vsleeptimer.h"
55 #ifdef VOMP_PLATTFORM_NMT
57 #include "osddirectfb.h"
62 #ifdef VOMP_PLATFORM_RASPBERRY
63 #include "ledraspberry.h"
64 #include "osdopenvg.h"
71 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
72 void threadSignalReceiverFunction();
75 ULLONG htonll(ULLONG);
76 ULLONG ntohll(ULLONG);
77 void shutdown(int code);
78 const std::string& getCommandLineServer();
80 // Global variables --------------------------------------------------------------------------------------------------
91 Sleeptimer* sleeptimer;
93 // Temporary, will move to Config system
94 std::string argvServer;
96 #ifdef HANDLE_VT_SWITCHING
98 struct vt_mode old_vtmode;
101 // Linux main function and sighandler
103 int main(int argc, char** argv)
105 bool daemonize = true;
106 bool debugEnabled = false;
107 bool crashed = false;
110 while ((c = getopt(argc, argv, "cdns:")) != -1)
118 debugEnabled = true; // and...
127 printf("Unknown option\n");
130 printf("Option error\n");
135 // Init global vars ------------------------------------------------------------------------------------------------
137 timers = new Timers();
139 inputMan = new InputMan();
141 led = new Led_TYPE();
142 osd = new Osd_TYPE();
143 audio = new Audio_TYPE();
144 video = new Video_TYPE();
146 control = new Control();
148 sleeptimer = new Sleeptimer();
150 if (!logger || !timers || !inputMan || !led || !osd || !video || !audio || !control || !wol || !sleeptimer)
152 printf("Could not create objects. Memory problems?\n");
156 // Get logging module started --------------------------------------------------------------------------------------
158 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0))
160 printf("Could not initialise log object. Aborting.\n");
164 logger->log("Core", Log::INFO, "Starting up...");
166 // Daemonize if not -d
171 pid_t forkTest = fork();
173 { printf("Cannot fork (1).\n"); exit(1); }
174 if (forkTest != 0) _exit(0); // PID returned, I am the parent
175 // otherwise, I am the child
179 { printf("Cannot fork (2).\n"); exit(1); }
180 if (forkTest != 0) _exit(0); // PID returned, I am the parent
181 // otherwise, I am the child
187 // Set up signal handling ------------------------------------------------------------------------------------------
192 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
195 logger->log("Core", Log::EMERG, "Could not block signals: %i", sigBlockResult);
199 // Start signal receiver thread
200 std::thread threadSignalReceiver(threadSignalReceiverFunction);
201 threadSignalReceiver.detach();
203 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
205 // VT Switching -----------------------------------------------------------------------------------------------
207 #ifdef HANDLE_VT_SWITCHING
211 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
216 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
221 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
223 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
227 ioctl(fdtty, VT_ACTIVATE, free_vt);
228 ioctl(fdtty, VT_WAITACTIVE, free_vt);
229 ioctl(fdtty, VT_LOCKSWITCH, 1);
234 // Init modules ----------------------------------------------------------------------------------------------------
237 success = inputMan->init();
241 logger->log("Core", Log::INFO, "InputMan module initialised");
245 logger->log("Core", Log::EMERG, "InputMan module failed to initialise");
249 success = led->init(-1);
252 logger->log("Core", Log::INFO, "LED module initialised");
256 logger->log("Core", Log::EMERG, "LED module failed to initialise");
260 success = timers->init();
263 logger->log("Core", Log::INFO, "Timers module initialised");
267 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
271 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
273 success = video->init(videoFormat);
276 logger->log("Core", Log::INFO, "Video module initialised");
280 logger->log("Core", Log::EMERG, "Video module failed to initialise");
284 success = osd->init();
287 logger->log("Core", Log::INFO, "OSD module initialised");
291 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
295 success = audio->init(Audio::MPEG2_PES);
298 logger->log("Core", Log::INFO, "Audio module initialised");
302 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
306 success = vdr->init();
309 logger->log("Core", Log::INFO, "VDR module initialised");
313 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
321 success = control->init(crashed);
324 logger->log("Core", Log::INFO, "Control module initialised");
328 logger->log("Core", Log::EMERG, "Control module failed to initialise");
332 // Other init ------------------------------------------------------------------------------------------------------
334 logger->log("Core", Log::NOTICE, "Startup successful");
336 // Run main loop ---------------------------------------------------------------------------------------------------
338 // Ok, all major device components and other bits are loaded and ready
341 // When that returns quit ------------------------------------------------------------------------------------------
349 // -------------------------------------------------------------------------------------------------------------------
351 void threadSignalReceiverFunction()
358 if(sigwait(&set, &sig))
360 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
364 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
366 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
372 // -------------------------------------------------------------------------------------------------------------------
374 void shutdown(int code)
376 // FIXME, send a del all to boxstack first, then get rid of it after control?
377 if (control) // shut down control here in case views have posted messages
381 logger->log("Core", Log::NOTICE, "Control module shut down");
388 logger->log("Core", Log::NOTICE, "VDR module shut down");
395 logger->log("Core", Log::NOTICE, "OSD module shut down");
402 logger->log("Core", Log::NOTICE, "Audio module shut down");
409 logger->log("Core", Log::NOTICE, "Video module shut down");
416 logger->log("Core", Log::NOTICE, "Timers module shut down");
423 logger->log("Core", Log::NOTICE, "LED module shut down");
428 inputMan->shutdown();
430 logger->log("Core", Log::NOTICE, "InputMan module shut down");
436 logger->log("Core", Log::NOTICE, "WOL module shut down");
442 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
445 #ifdef HANDLE_VT_SWITCHING
446 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
452 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
460 // -------------------------------------------------------------------------------------------------------------------
462 ULLONG htonll(ULLONG a)
464 #if BYTE_ORDER == BIG_ENDIAN
469 b = ((a << 56) & 0xFF00000000000000ULL)
470 | ((a << 40) & 0x00FF000000000000ULL)
471 | ((a << 24) & 0x0000FF0000000000ULL)
472 | ((a << 8) & 0x000000FF00000000ULL)
473 | ((a >> 8) & 0x00000000FF000000ULL)
474 | ((a >> 24) & 0x0000000000FF0000ULL)
475 | ((a >> 40) & 0x000000000000FF00ULL)
476 | ((a >> 56) & 0x00000000000000FFULL) ;
482 ULLONG ntohll(ULLONG a)
487 void MILLISLEEP(ULONG a)
490 struct timespec delayTime;
491 delayTime.tv_sec = a / 1000;
492 delayTime.tv_nsec = (a % 1000) * 1000000;
493 nanosleep(&delayTime, NULL);
499 long long getTimeMS()
502 clock_gettime(VOMP_LINUX_CLOCK, &ts);
503 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
506 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
508 return clock_gettime(CLOCK_REALTIME, tp);
511 int min(UINT a, int b)
517 int max(int a, int b)
523 std::string tp2str(const std::chrono::time_point<std::chrono::system_clock>& tp)
525 auto tms = std::chrono::time_point_cast<std::chrono::milliseconds>(tp);
526 std::chrono::milliseconds e = tms.time_since_epoch();
527 long long c = e.count();
528 time_t tt = c / 1000;
530 auto stm = std::localtime(&tt);
531 std::stringstream ss;
532 ss << std::put_time(stm, "%T") << "." << std::setfill('0') << std::setw(3) << ttm;
536 const std::string& getCommandLineServer()