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/>.
31 #ifdef HANDLE_VT_SWITCHING
33 #include <sys/ioctl.h>
42 #ifdef VOMP_PLATTFORM_NMT
44 #include "remotelirc.h"
46 #include "osddirectfb.h"
52 #ifdef VOMP_PLATFORM_RASPBERRY
54 #include "remotelinux.h"
55 #include "ledraspberry.h"
56 #include "osdopenvg.h"
64 #include "vsleeptimer.h"
67 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
68 void threadSignalReceiverFunction();
71 void shutdown(int code);
73 // Global variables --------------------------------------------------------------------------------------------------
85 Sleeptimer* sleeptimer;
87 #ifdef HANDLE_VT_SWITCHING
89 struct vt_mode old_vtmode;
92 // Linux main function and sighandler
94 int main(int argc, char** argv)
96 bool daemonize = true;
97 bool debugEnabled = false;
99 char* setServer = NULL;
102 while ((c = getopt(argc, argv, "cdns:")) != -1)
110 debugEnabled = true; // and...
118 printf("Unknown option\n");
121 printf("Option error\n");
126 // Init global vars ------------------------------------------------------------------------------------------------
128 timers = new Timers();
131 remote = new Remote_TYPE();
132 led = new Led_TYPE();
133 osd = new Osd_TYPE();
134 audio = new Audio_TYPE();
135 video = new Video_TYPE();
139 boxstack = new BoxStack();
140 command = new Command();
142 sleeptimer = new Sleeptimer();
144 if (!logger || !remote || !led || !osd || !video || !audio || !boxstack || !command || !wol || !sleeptimer)
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();
198 sighandler_t sigtest;
200 sigtest = signal(SIGPIPE, SIG_IGN);
201 if (sigtest == SIG_ERR)
203 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGPIPE. Aborting.");
206 sigtest = signal(SIGINT, sighandler);
207 if (sigtest == SIG_ERR)
209 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGINT. Aborting.");
212 sigtest = signal(SIGTERM, sighandler);
213 if (sigtest == SIG_ERR)
215 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGTERM. Aborting.");
218 sigtest = signal(SIGUSR1, sighandler);
219 if (sigtest == SIG_ERR)
221 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR1. Aborting.");
224 sigtest = signal(SIGUSR2, sighandler);
225 if (sigtest == SIG_ERR)
227 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGUSR2. Aborting.");
230 sigtest = signal(SIGURG, sighandler);
231 if (sigtest == SIG_ERR)
233 logger->log("Core", Log::EMERG, "Could not set up signal handler for SIGURG. Aborting.");
238 logger->log("Core", Log::INFO, "Signal handlers set up successfully");
240 #ifdef HANDLE_VT_SWITCHING
241 if ((fdtty = open("/dev/tty", O_WRONLY),0) == -1) {
242 logger->log("Core", Log::EMERG, "Could not open /dev/tty. Please change permissions");
245 if (ioctl(fdtty,VT_OPENQRY,&free_vt)==-1 || free_vt==-1){
246 logger->log("Core", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
248 ioctl(fdtty,VT_ACTIVATE,free_vt);
249 ioctl(fdtty,VT_WAITACTIVE,free_vt);
250 ioctl(fdtty, VT_LOCKSWITCH, 1);
256 // Init modules ----------------------------------------------------------------------------------------------------
259 success = remote->init(RemoteStartDev);
263 logger->log("Core", Log::INFO, "Remote module initialised");
267 logger->log("Core", Log::EMERG, "Remote module failed to initialise");
271 success = led->init(-1);
274 logger->log("Core", Log::INFO, "LED module initialised");
278 logger->log("Core", Log::EMERG, "LED module failed to initialise");
282 success = timers->init();
285 logger->log("Core", Log::INFO, "Timers module initialised");
289 logger->log("Core", Log::EMERG, "Timers module failed to initialise");
293 UCHAR videoFormat = Video::PAL; // PALNTSC FIXME
295 success = video->init(videoFormat);
298 logger->log("Core", Log::INFO, "Video module initialised");
302 logger->log("Core", Log::EMERG, "Video module failed to initialise");
306 success = osd->init();
309 logger->log("Core", Log::INFO, "OSD module initialised");
313 logger->log("Core", Log::EMERG, "OSD module failed to initialise");
317 success = audio->init(Audio::MPEG2_PES);
320 logger->log("Core", Log::INFO, "Audio module initialised");
324 logger->log("Core", Log::EMERG, "Audio module failed to initialise");
328 success = vdr->init();
331 logger->log("Core", Log::INFO, "VDR module initialised");
335 logger->log("Core", Log::EMERG, "VDR module failed to initialise");
339 success = boxstack->init();
342 logger->log("Core", Log::INFO, "BoxStack module initialised");
346 logger->log("Core", Log::EMERG, "BoxStack module failed to initialise");
350 success = command->init(crashed, setServer);
353 logger->log("Core", Log::INFO, "Command module initialised");
357 logger->log("Core", Log::EMERG, "Command module failed to initialise");
361 // Other init ------------------------------------------------------------------------------------------------------
363 logger->log("Core", Log::NOTICE, "Startup successful");
365 // Run main loop ---------------------------------------------------------------------------------------------------
367 // Ok, all major device components and other bits are loaded and ready
370 // When that returns quit ------------------------------------------------------------------------------------------
375 // -------------------------------------------------------------------------------------------------------------------
377 void threadSignalReceiverFunction()
384 if(sigwait(&set, &sig))
386 logger->log("Core", Log::CRIT, "Sigwait returned fail - signal handler exiting");
390 logger->log("Core", Log::NOTICE, "Signal received: %i", sig);
392 if ((sig == SIGINT) || (sig == SIGTERM)) command->stop();
398 // -------------------------------------------------------------------------------------------------------------------
400 void shutdown(int code)
404 boxstack->shutdown();
406 logger->log("Core", Log::NOTICE, "BoxStack module shut down");
409 // FIXME, send a del all to boxstack first, then get rid of it after command?
410 if (command) // shut down command here in case views have posted messages
415 logger->log("Core", Log::NOTICE, "Command module shut down");
422 logger->log("Core", Log::NOTICE, "VDR module shut down");
429 logger->log("Core", Log::NOTICE, "OSD module shut down");
436 logger->log("Core", Log::NOTICE, "Audio module shut down");
443 logger->log("Core", Log::NOTICE, "Video module shut down");
450 logger->log("Core", Log::NOTICE, "Timers module shut down");
457 logger->log("Core", Log::NOTICE, "LED module shut down");
464 logger->log("Core", Log::NOTICE, "Remote module shut down");
470 logger->log("Core", Log::NOTICE, "WOL module shut down");
476 logger->log("Core", Log::NOTICE, "Sleeptimer module shut down");
478 #ifdef HANDLE_VT_SWITCHING
479 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
485 logger->log("Core", Log::NOTICE, "Log module shutting down... bye!\n\n");
493 // -------------------------------------------------------------------------------------------------------------------
495 ULLONG htonll(ULLONG a)
497 #if BYTE_ORDER == BIG_ENDIAN
502 b = ((a << 56) & 0xFF00000000000000ULL)
503 | ((a << 40) & 0x00FF000000000000ULL)
504 | ((a << 24) & 0x0000FF0000000000ULL)
505 | ((a << 8) & 0x000000FF00000000ULL)
506 | ((a >> 8) & 0x00000000FF000000ULL)
507 | ((a >> 24) & 0x0000000000FF0000ULL)
508 | ((a >> 40) & 0x000000000000FF00ULL)
509 | ((a >> 56) & 0x00000000000000FFULL) ;
515 ULLONG ntohll(ULLONG a)
520 void MILLISLEEP(ULONG a)
523 struct timespec delayTime;
524 delayTime.tv_sec = a / 1000;
525 delayTime.tv_nsec = (a % 1000) * 1000000;
526 nanosleep(&delayTime, NULL);
532 long long getTimeMS()
535 clock_gettime(VOMP_LINUX_CLOCK, &ts);
536 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
539 int getClockRealTime(struct timespec *tp)
541 return clock_gettime(CLOCK_REALTIME, tp);
544 int min(UINT a, int b)
550 int max(int a, int b)