2 Copyright 2004-2021 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>
40 #ifdef HANDLE_VT_SWITCHING
41 #include <sys/ioctl.h>
51 #ifdef VOMP_PLATTFORM_NMT
53 #include "osddirectfb.h"
59 #ifndef WIN32 // FIXME do we need any if WIN32 stuff in here? windows has own winmain file
60 void threadSignalReceiverFunction();
63 [[ noreturn ]] void shutdown(int code);
70 #ifdef HANDLE_VT_SWITCHING
72 struct vt_mode old_vtmode;
75 static const char* TAG = "Main";
77 // Linux main function and sighandler
79 int main(int argc, char** argv)
84 config = new Config();
85 if (!config->loadFile())
87 printf("Parse error in config.json\n");
93 while ((c = getopt(argc, argv, "cdns:")) != -1)
101 config->set("main", "debug", true); // and...
104 config->set("main", "daemonize", false);
107 config->set("main", "argv_server", optarg);
110 printf("Unknown option\n");
113 printf("Option error\n");
118 // Start Log --------------------------------------------------------------------------------------------------
122 printf("Failed to create Log object\n");
127 config->getBool("main", "debug", debugEnabled);
128 if (!logger->init(Log::DEBUG, "dummy", debugEnabled ? 1 : 0)) // NCONFIG x2
130 printf("Could not initialise log object. Aborting.\n");
134 logger->log("Main", Log::INFO, "Old log starting up...");
138 loggerNT = new LogNT();
141 printf("Failed to create LogNT object\n");
145 std::string logFileName("stdout");
146 config->getString("log", "filename", logFileName);
147 if (!loggerNT->init(logFileName, debugEnabled ? 1 : 0)) // NCONFIG x2
149 printf("Could not initialise log object. Aborting.\n");
153 //logger->log("Main", Log::INFO, "Starting up...");
154 loggerNT->info(TAG, "Starting up...");
156 // Daemonize --------------------------------------------------------------------------------------------------
159 config->getBool("main", "daemonize", daemonize);
163 pid_t forkTest = fork();
165 { printf("Cannot fork (1).\n"); exit(1); }
166 if (forkTest != 0) _exit(0); // PID returned, I am the parent
167 // otherwise, I am the child
171 { printf("Cannot fork (2).\n"); exit(1); }
172 if (forkTest != 0) _exit(0); // PID returned, I am the parent
173 // otherwise, I am the child
179 // Set up signal handling ------------------------------------------------------------------------------------------
184 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
187 //logger->log("Main", Log::EMERG, "Could not block signals: %i", sigBlockResult);
188 loggerNT->crit(TAG, "Could not block signals: {}", sigBlockResult);
192 // Start signal receiver thread
193 std::thread threadSignalReceiver(threadSignalReceiverFunction);
194 threadSignalReceiver.detach();
196 loggerNT->info(TAG, "Signal handlers set up successfully");
198 // VT Switching -----------------------------------------------------------------------------------------------
200 #ifdef HANDLE_VT_SWITCHING
204 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
209 loggerNT->warn(TAG, "Could not open /dev/tty. Please change permissions");
214 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
216 loggerNT->crit(TAG, "Could not retrieve free virtual console, please change permissions");
220 ioctl(fdtty, VT_ACTIVATE, free_vt);
221 ioctl(fdtty, VT_WAITACTIVE, free_vt);
222 ioctl(fdtty, VT_LOCKSWITCH, 1);
227 // Test area ------------------------------------------------------------------------------------------------------
231 // Run control ----------------------------------------------------------------------------------------------------
233 control = new Control();
236 loggerNT->crit(TAG, "Control module failed to create");
240 if (!control->init(crashed))
242 loggerNT->crit(TAG, "Control module failed to initialise");
254 // -------------------------------------------------------------------------------------------------------------------
256 void threadSignalReceiverFunction()
263 if(sigwait(&set, &sig))
265 loggerNT->crit(TAG, "Sigwait returned fail - signal handler exiting");
269 loggerNT->info(TAG, "Signal received: {}", sig);
271 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
277 // -------------------------------------------------------------------------------------------------------------------
279 [[ noreturn ]] void shutdown(int code)
281 // FIXME, send a del all to boxstack first, then get rid of it after control?
282 if (control) // shut down control here in case views have posted messages
286 loggerNT->info(TAG, "Control module shut down");
289 #ifdef HANDLE_VT_SWITCHING
290 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
296 loggerNT->info(TAG, "Log module shutting down... bye!\n\n");
309 // -------------------------------------------------------------------------------------------------------------------
311 long long getTimeMS()
314 clock_gettime(VOMP_LINUX_CLOCK, &ts);
315 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
318 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
320 return clock_gettime(CLOCK_REALTIME, tp);