95
src/main.cpp
95
src/main.cpp
@@ -1,57 +1,84 @@
|
||||
#include <algorithm>
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fcntl.h>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <print>
|
||||
#include <signal.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cpptrace/cpptrace.hpp>
|
||||
#include <cpptrace/from_current.hpp>
|
||||
#include <tinyfiledialogs.h>
|
||||
|
||||
#include "App.hpp"
|
||||
|
||||
bool signal_running();
|
||||
|
||||
std::optional<App> g_app{};
|
||||
std::optional<App> g_app {};
|
||||
|
||||
auto main() -> int {
|
||||
if (signal_running()) {
|
||||
return 0;
|
||||
}
|
||||
auto main() -> int
|
||||
{
|
||||
if (signal_running()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::signal(SIGINT, [](int) {
|
||||
if (g_app)
|
||||
g_app->stop();
|
||||
});
|
||||
std::signal(SIGINT, [](int) {
|
||||
if (g_app)
|
||||
g_app->stop();
|
||||
});
|
||||
|
||||
g_app.emplace();
|
||||
g_app->run();
|
||||
CPPTRACE_TRY
|
||||
{
|
||||
g_app.emplace();
|
||||
g_app->run();
|
||||
}
|
||||
CPPTRACE_CATCH(std::exception const &e)
|
||||
{
|
||||
std::string what { e.what() };
|
||||
std::ranges::replace(what, '"', '.');
|
||||
std::ranges::replace(what, '\'', '.');
|
||||
std::ranges::replace(what, '`', '.');
|
||||
if (what.empty()) {
|
||||
std::println(std::cerr, "Unexpected exception!");
|
||||
} else {
|
||||
std::println(std::cerr, "Unexpected exception! Error: {}", what);
|
||||
}
|
||||
cpptrace::from_current_exception().print();
|
||||
tinyfd_messageBox(
|
||||
"Unexpected exception", what.c_str(), "ok", "error", 1);
|
||||
}
|
||||
}
|
||||
|
||||
bool signal_running() {
|
||||
const char *lock_path = "/tmp/waylight.lock";
|
||||
int fd = open(lock_path, O_CREAT | O_RDWR, 0666);
|
||||
if (fd == -1)
|
||||
return false;
|
||||
bool signal_running()
|
||||
{
|
||||
char const *lock_path = "/tmp/waylight.lock";
|
||||
int fd = open(lock_path, O_CREAT | O_RDWR, 0666);
|
||||
if (fd == -1)
|
||||
return false;
|
||||
|
||||
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
|
||||
FILE *f = fopen(lock_path, "r");
|
||||
if (f) {
|
||||
pid_t pid;
|
||||
if (fscanf(f, "%d", &pid) == 1)
|
||||
kill(pid, SIGUSR1);
|
||||
fclose(f);
|
||||
}
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
|
||||
FILE *f = fopen(lock_path, "r");
|
||||
if (f) {
|
||||
pid_t pid;
|
||||
if (fscanf(f, "%d", &pid) == 1)
|
||||
kill(pid, SIGUSR1);
|
||||
fclose(f);
|
||||
}
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, 0) == -1) {
|
||||
close(fd);
|
||||
unlink(lock_path);
|
||||
return false;
|
||||
}
|
||||
if (ftruncate(fd, 0) == -1) {
|
||||
close(fd);
|
||||
unlink(lock_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
dprintf(fd, "%d\n", getpid());
|
||||
return false;
|
||||
dprintf(fd, "%d\n", getpid());
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user