Merge pull request #4834 from khaneliman/niri

feat(niri): niri depends on socket
This commit is contained in:
Alexis Rouillard
2026-02-23 23:43:30 +01:00
committed by GitHub
2 changed files with 7 additions and 13 deletions

View File

@ -17,7 +17,7 @@ class EventHandler {
class IPC { class IPC {
public: public:
IPC() { startIPC(); } IPC();
void registerForIPC(const std::string& ev, EventHandler* ev_handler); void registerForIPC(const std::string& ev, EventHandler* ev_handler);
void unregisterForIPC(EventHandler* handler); void unregisterForIPC(EventHandler* handler);

View File

@ -20,12 +20,13 @@
namespace waybar::modules::niri { namespace waybar::modules::niri {
IPC::IPC() { startIPC(); }
int IPC::connectToSocket() { int IPC::connectToSocket() {
const char* socket_path = getenv("NIRI_SOCKET"); const char* socket_path = getenv("NIRI_SOCKET");
if (socket_path == nullptr) { if (socket_path == nullptr) {
spdlog::warn("Niri is not running, niri IPC will not be available."); throw std::runtime_error("Niri IPC: NIRI_SOCKET was not set! (Is Niri running?)");
return -1;
} }
struct sockaddr_un addr; struct sockaddr_un addr;
@ -54,15 +55,9 @@ int IPC::connectToSocket() {
void IPC::startIPC() { void IPC::startIPC() {
// will start IPC and relay events to parseIPC // will start IPC and relay events to parseIPC
std::thread([&]() { int socketfd = connectToSocket();
int socketfd;
try { std::thread([this, socketfd]() {
socketfd = connectToSocket();
} catch (std::exception& e) {
spdlog::error("Niri IPC: failed to start, reason: {}", e.what());
return;
}
if (socketfd == -1) return;
spdlog::info("Niri IPC starting"); spdlog::info("Niri IPC starting");
@ -242,7 +237,6 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
Json::Value IPC::send(const Json::Value& request) { Json::Value IPC::send(const Json::Value& request) {
int socketfd = connectToSocket(); int socketfd = connectToSocket();
if (socketfd == -1) throw std::runtime_error("Niri is not running");
auto unix_istream = Gio::UnixInputStream::create(socketfd, true); auto unix_istream = Gio::UnixInputStream::create(socketfd, true);
auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false); auto unix_ostream = Gio::UnixOutputStream::create(socketfd, false);