Files
waylight/src/main.cpp
2025-10-17 00:20:39 +03:00

89 lines
1.6 KiB
C++

#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"
namespace Waylight {
bool signal_running();
std::optional<App> g_app {};
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 (ftruncate(fd, 0) == -1) {
close(fd);
unlink(lock_path);
return false;
}
dprintf(fd, "%d\n", getpid());
return false;
}
} // namespace Waylight
auto main() -> int
{
if (Waylight::signal_running()) {
return 0;
}
std::signal(SIGINT, [](int) {
if (Waylight::g_app)
Waylight::g_app->stop();
});
CPPTRACE_TRY
{
Waylight::g_app.emplace();
Waylight::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);
}
}