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/>.
26 #include <sys/types.h>
41 #ifdef HANDLE_VT_SWITCHING
42 #include <sys/ioctl.h>
50 #ifdef VOMP_PLATTFORM_NMT
52 #include "osddirectfb.h"
58 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
59 void threadSignalReceiverFunction();
62 void shutdown(int code);
63 const std::string& getCommandLineServer();
68 // Temporary, will move to Config system NCONFIG
69 std::string argvServer;
71 #ifdef HANDLE_VT_SWITCHING
73 struct vt_mode old_vtmode;
76 // Linux main function and sighandler
78 int main(int argc, char** argv)
80 bool daemonize = true;
81 bool debugEnabled = false;
85 while ((c = getopt(argc, argv, "cdns:")) != -1)
93 debugEnabled = true; // and...
102 printf("Unknown option\n");
105 printf("Option error\n");
110 // Start Log --------------------------------------------------------------------------------------------------
114 printf("Failed to create Log object\n");
118 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0)) // NCONFIG x2
120 printf("Could not initialise log object. Aborting.\n");
124 logger->log("Main", Log::INFO, "Starting up...");
126 // Daemonize --------------------------------------------------------------------------------------------------
131 pid_t forkTest = fork();
133 { printf("Cannot fork (1).\n"); exit(1); }
134 if (forkTest != 0) _exit(0); // PID returned, I am the parent
135 // otherwise, I am the child
139 { printf("Cannot fork (2).\n"); exit(1); }
140 if (forkTest != 0) _exit(0); // PID returned, I am the parent
141 // otherwise, I am the child
147 // Set up signal handling ------------------------------------------------------------------------------------------
152 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
155 logger->log("Main", Log::EMERG, "Could not block signals: %i", sigBlockResult);
159 // Start signal receiver thread
160 std::thread threadSignalReceiver(threadSignalReceiverFunction);
161 threadSignalReceiver.detach();
163 logger->log("Main", Log::INFO, "Signal handlers set up successfully");
165 // VT Switching -----------------------------------------------------------------------------------------------
167 #ifdef HANDLE_VT_SWITCHING
171 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
176 logger->log("Main", Log::EMERG, "Could not open /dev/tty. Please change permissions");
181 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
183 logger->log("Main", Log::EMERG, "Could not retrieve free virtual console, please change permissions");
187 ioctl(fdtty, VT_ACTIVATE, free_vt);
188 ioctl(fdtty, VT_WAITACTIVE, free_vt);
189 ioctl(fdtty, VT_LOCKSWITCH, 1);
194 // Run control ----------------------------------------------------------------------------------------------------
196 control = new Control();
199 logger->log("Main", Log::EMERG, "Control module failed to create");
203 if (!control->init(crashed))
205 logger->log("Main", Log::EMERG, "Control module failed to initialise");
218 // -------------------------------------------------------------------------------------------------------------------
220 void threadSignalReceiverFunction()
227 if(sigwait(&set, &sig))
229 logger->log("Main", Log::CRIT, "Sigwait returned fail - signal handler exiting");
233 logger->log("Main", Log::NOTICE, "Signal received: %i", sig);
235 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
241 // -------------------------------------------------------------------------------------------------------------------
243 void shutdown(int code)
245 // FIXME, send a del all to boxstack first, then get rid of it after control?
246 if (control) // shut down control here in case views have posted messages
250 logger->log("Main", Log::NOTICE, "Control module shut down");
253 #ifdef HANDLE_VT_SWITCHING
254 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
260 logger->log("Main", Log::NOTICE, "Log module shutting down... bye!\n\n");
268 // -------------------------------------------------------------------------------------------------------------------
270 long long getTimeMS()
273 clock_gettime(VOMP_LINUX_CLOCK, &ts);
274 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
277 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
279 return clock_gettime(CLOCK_REALTIME, tp);
282 const std::string& getCommandLineServer()