Files
Waybar/src/main.cpp
T

162 lines
4.2 KiB
C++
Raw Normal View History

2025-04-12 17:21:57 -07:00
#include <fcntl.h>
2022-04-06 08:37:19 +02:00
#include <spdlog/spdlog.h>
#include <sys/types.h>
#include <sys/wait.h>
2018-08-08 23:54:58 +02:00
#include <csignal>
2020-07-21 12:36:48 +10:00
#include <list>
#include <mutex>
2022-04-06 08:37:19 +02:00
#include "client.hpp"
2025-04-12 17:21:57 -07:00
#include "util/SafeSignal.hpp"
2018-08-08 23:54:58 +02:00
std::mutex reap_mtx;
2020-07-21 12:36:48 +10:00
std::list<pid_t> reap;
2025-04-12 17:21:57 -07:00
static int signal_pipe_write_fd;
// Write a single signal to `signal_pipe_write_fd`.
// This function is set as a signal handler, so it must be async-signal-safe.
static void writeSignalToPipe(int signum) {
ssize_t amt = write(signal_pipe_write_fd, &signum, sizeof(int));
// There's not much we can safely do inside of a signal handler.
// Let's just ignore any errors.
(void) amt;
}
// This initializes `signal_pipe_write_fd`, and sets up signal handlers.
//
// This function will run forever, emitting every `SIGUSR1`, `SIGUSR2`,
// `SIGINT`, `SIGCHLD`, and `SIGRTMIN + 1`...`SIGRTMAX` signal received
// to `signal_handler`.
2025-04-12 17:21:57 -07:00
static void catchSignals(waybar::SafeSignal<int> &signal_handler) {
int fd[2];
pipe(fd);
int signal_pipe_read_fd = fd[0];
signal_pipe_write_fd = fd[1];
// This pipe should be able to buffer ~thousands of signals. If it fills up,
// we'll drop signals instead of blocking.
// We can't allow the write end to block because we'll be writing to it in a
// signal handler, which could interrupt the loop that's reading from it and
// deadlock.
fcntl(signal_pipe_write_fd, F_SETFL, O_NONBLOCK);
std::signal(SIGUSR1, writeSignalToPipe);
std::signal(SIGUSR2, writeSignalToPipe);
std::signal(SIGINT, writeSignalToPipe);
std::signal(SIGCHLD, writeSignalToPipe);
2025-04-12 17:21:57 -07:00
for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
std::signal(sig, writeSignalToPipe);
}
while (true) {
int signum;
ssize_t amt = read(signal_pipe_read_fd, &signum, sizeof(int));
if (amt < 0) {
spdlog::error("read from signal pipe failed with error {}, closing thread", strerror(errno));
break;
}
if (amt != sizeof(int)) {
continue;
}
signal_handler.emit(signum);
}
}
// Must be called on the main thread.
2025-04-12 17:48:16 -07:00
//
// If this signal should restart or close the bar, this function will write
// `true` or `false`, respectively, into `reload`.
static void handleSignalMainThread(int signum, bool &reload) {
2025-04-12 17:21:57 -07:00
if (signum >= SIGRTMIN + 1 && signum <= SIGRTMAX) {
for (auto& bar : waybar::Client::inst()->bars) {
bar->handleSignal(signum);
}
return;
}
switch (signum) {
case SIGUSR1:
spdlog::debug("Visibility toggled");
for (auto& bar : waybar::Client::inst()->bars) {
bar->toggle();
}
break;
case SIGUSR2:
spdlog::info("Reloading...");
reload = true;
waybar::Client::inst()->reset();
break;
case SIGINT:
spdlog::info("Quitting.");
reload = false;
waybar::Client::inst()->reset();
break;
case SIGCHLD:
spdlog::debug("Received SIGCHLD in signalThread");
if (!reap.empty()) {
reap_mtx.lock();
for (auto it = reap.begin(); it != reap.end(); ++it) {
if (waitpid(*it, nullptr, WNOHANG) == *it) {
spdlog::debug("Reaped child with PID: {}", *it);
it = reap.erase(it);
}
}
reap_mtx.unlock();
}
break;
2025-04-12 17:21:57 -07:00
default:
spdlog::debug("Received signal with number {}, but not handling", signum);
break;
}
}
int main(int argc, char* argv[]) {
2018-08-08 23:54:58 +02:00
try {
2024-07-02 10:12:41 -05:00
auto* client = waybar::Client::inst();
2025-04-12 17:48:16 -07:00
bool reload;
2025-04-12 17:21:57 -07:00
waybar::SafeSignal<int> posix_signal_received;
posix_signal_received.connect([&](int signum) {
2025-04-12 17:48:16 -07:00
handleSignalMainThread(signum, reload);
2025-04-12 17:21:57 -07:00
});
std::thread signal_thread([&]() {
catchSignals(posix_signal_received);
});
// Every `std::thread` must be joined or detached.
// This thread should run forever, so detach it.
signal_thread.detach();
auto ret = 0;
do {
reload = false;
ret = client->main(argc, argv);
} while (reload);
std::signal(SIGUSR1, SIG_IGN);
std::signal(SIGUSR2, SIG_IGN);
2024-10-30 17:48:35 +01:00
std::signal(SIGINT, SIG_IGN);
delete client;
return ret;
2018-08-08 23:54:58 +02:00
} catch (const std::exception& e) {
2019-05-18 19:44:45 -04:00
spdlog::error("{}", e.what());
2018-08-08 23:54:58 +02:00
return 1;
} catch (const Glib::Exception& e) {
2019-05-18 19:44:45 -04:00
spdlog::error("{}", static_cast<std::string>(e.what()));
2018-08-08 23:54:58 +02:00
return 1;
}
}