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>
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 [[ noreturn ]] void shutdown(int code);
68 #ifdef HANDLE_VT_SWITCHING
70 struct vt_mode old_vtmode;
73 static const char* TAG = "Main";
75 // Linux main function and sighandler
77 int main(int argc, char** argv)
82 config = new Config();
83 if (!config->loadFile())
85 printf("Parse error in config.json\n");
91 while ((c = getopt(argc, argv, "cdns:")) != -1)
99 config->set("main", "debug", true); // and...
102 config->set("main", "daemonize", false);
105 config->set("main", "argv_server", optarg);
108 printf("Unknown option\n");
111 printf("Option error\n");
116 // Start Log --------------------------------------------------------------------------------------------------
118 loggerNT = new LogNT();
121 printf("Failed to create LogNT object\n");
125 std::string logFileName("stdout");
126 config->getString("log", "filename", logFileName);
128 config->getBool("main", "debug", debugEnabled);
129 if (!loggerNT->init(logFileName, debugEnabled ? 1 : 0)) // NCONFIG x2
131 printf("Could not initialise log object. Aborting.\n");
135 //logger->log("Main", Log::INFO, "Starting up...");
136 loggerNT->info(TAG, "Starting up...");
138 // Daemonize --------------------------------------------------------------------------------------------------
141 config->getBool("main", "daemonize", daemonize);
145 pid_t forkTest = fork();
147 { printf("Cannot fork (1).\n"); exit(1); }
148 if (forkTest != 0) _exit(0); // PID returned, I am the parent
149 // otherwise, I am the child
153 { printf("Cannot fork (2).\n"); exit(1); }
154 if (forkTest != 0) _exit(0); // PID returned, I am the parent
155 // otherwise, I am the child
161 // Set up signal handling ------------------------------------------------------------------------------------------
166 int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL);
169 //logger->log("Main", Log::EMERG, "Could not block signals: %i", sigBlockResult);
170 loggerNT->crit(TAG, "Could not block signals: {}", sigBlockResult);
174 // Start signal receiver thread
175 std::thread threadSignalReceiver(threadSignalReceiverFunction);
176 threadSignalReceiver.detach();
178 loggerNT->info(TAG, "Signal handlers set up successfully");
180 // VT Switching -----------------------------------------------------------------------------------------------
182 #ifdef HANDLE_VT_SWITCHING
186 (fdtty = open("/dev/tty", O_WRONLY),0) == -1 // FIXME. Why the ,0 ? Surely this kills the log line below
191 loggerNT->warn(TAG, "Could not open /dev/tty. Please change permissions");
196 if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1)
198 loggerNT->crit(TAG, "Could not retrieve free virtual console, please change permissions");
202 ioctl(fdtty, VT_ACTIVATE, free_vt);
203 ioctl(fdtty, VT_WAITACTIVE, free_vt);
204 ioctl(fdtty, VT_LOCKSWITCH, 1);
209 // Test area ------------------------------------------------------------------------------------------------------
213 // Run control ----------------------------------------------------------------------------------------------------
215 control = new Control();
218 loggerNT->crit(TAG, "Control module failed to create");
222 if (!control->init(crashed))
224 loggerNT->crit(TAG, "Control module failed to initialise");
236 // -------------------------------------------------------------------------------------------------------------------
238 void threadSignalReceiverFunction()
245 if(sigwait(&set, &sig))
247 loggerNT->crit(TAG, "Sigwait returned fail - signal handler exiting");
251 loggerNT->info(TAG, "Signal received: {}", sig);
253 if ((sig == SIGINT) || (sig == SIGTERM)) control->stop();
259 // -------------------------------------------------------------------------------------------------------------------
261 [[ noreturn ]] void shutdown(int code)
263 // FIXME, send a del all to boxstack first, then get rid of it after control?
264 if (control) // shut down control here in case views have posted messages
268 loggerNT->info(TAG, "Control module shut down");
271 #ifdef HANDLE_VT_SWITCHING
272 ioctl(fdtty, VT_UNLOCKSWITCH, 1);
278 loggerNT->info(TAG, "Log module shutting down... bye!\n\n");
279 #warning loggerNT has no shutdown
280 //loggerNT->shutdown();
292 // -------------------------------------------------------------------------------------------------------------------
294 long long getTimeMS()
297 clock_gettime(VOMP_LINUX_CLOCK, &ts);
298 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000LL;
301 int getClockRealTime(struct timespec *tp) // FIXME - del if all goes chrono
303 return clock_gettime(CLOCK_REALTIME, tp);