temp-monitor/src/main.cpp
2026-01-09 22:04:23 +01:00

66 lines
1.3 KiB
C++

#include <gtk/gtk.h>
#include <glib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "mainwindow.h"
// Declaration from resources.c
extern "C" {
GResource *resources_get_resource();
}
GMainLoop *gMainLoop = nullptr;
int main(int argc, char *argv[])
{
// Daemonize the process
pid_t pid = fork();
if (pid < 0) {
return 1; // Fork failed
}
if (pid > 0) {
return 0; // Parent process exits
}
// Child process continues as daemon
umask(0);
// Create new session
if (setsid() < 0) {
return 1;
}
// Change directory to root
chdir("/");
// Close standard file descriptors
int fd = open("/dev/null", O_RDWR);
if (fd != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
close(fd);
}
gtk_init();
// Register resources containing the application icon
GResource *resource = resources_get_resource();
g_resources_register(resource);
gMainLoop = g_main_loop_new(nullptr, FALSE);
MainWindow *window = new MainWindow();
window->show();
// Run the main event loop
g_main_loop_run(gMainLoop);
g_main_loop_unref(gMainLoop);
return 0;
}