|
11 | 11 |
|
12 | 12 | #include <filesystem> |
13 | 13 | #include <string> |
14 | | -#include <thread> |
15 | 14 |
|
16 | 15 | namespace waybar::modules::hyprland { |
17 | 16 |
|
@@ -44,71 +43,96 @@ std::filesystem::path IPC::getSocketFolder(const char* instanceSig) { |
44 | 43 | return socketFolder_; |
45 | 44 | } |
46 | 45 |
|
47 | | -void IPC::startIPC() { |
| 46 | +IPC::IPC() { |
48 | 47 | // will start IPC and relay events to parseIPC |
| 48 | + ipcThread_ = std::thread([this]() { socketListener(); }); |
| 49 | +} |
49 | 50 |
|
50 | | - std::thread([&]() { |
51 | | - // check for hyprland |
52 | | - const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE"); |
53 | | - |
54 | | - if (his == nullptr) { |
55 | | - spdlog::warn("Hyprland is not running, Hyprland IPC will not be available."); |
56 | | - return; |
| 51 | +IPC::~IPC() { |
| 52 | + running_ = false; |
| 53 | + spdlog::info("Hyprland IPC stopping..."); |
| 54 | + if (socketfd_ != -1) { |
| 55 | + spdlog::trace("Shutting down socket"); |
| 56 | + if (shutdown(socketfd_, SHUT_RDWR) == -1) { |
| 57 | + spdlog::error("Hyprland IPC: Couldn't shutdown socket"); |
| 58 | + } |
| 59 | + spdlog::trace("Closing socket"); |
| 60 | + if (close(socketfd_) == -1) { |
| 61 | + spdlog::error("Hyprland IPC: Couldn't close socket"); |
57 | 62 | } |
| 63 | + } |
| 64 | + ipcThread_.join(); |
| 65 | +} |
58 | 66 |
|
59 | | - if (!modulesReady) return; |
| 67 | +IPC& IPC::inst() { |
| 68 | + static IPC ipc; |
| 69 | + return ipc; |
| 70 | +} |
60 | 71 |
|
61 | | - spdlog::info("Hyprland IPC starting"); |
| 72 | +void IPC::socketListener() { |
| 73 | + // check for hyprland |
| 74 | + const char* his = getenv("HYPRLAND_INSTANCE_SIGNATURE"); |
62 | 75 |
|
63 | | - struct sockaddr_un addr; |
64 | | - int socketfd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 76 | + if (his == nullptr) { |
| 77 | + spdlog::warn("Hyprland is not running, Hyprland IPC will not be available."); |
| 78 | + return; |
| 79 | + } |
65 | 80 |
|
66 | | - if (socketfd == -1) { |
67 | | - spdlog::error("Hyprland IPC: socketfd failed"); |
68 | | - return; |
69 | | - } |
| 81 | + if (!modulesReady) return; |
70 | 82 |
|
71 | | - addr.sun_family = AF_UNIX; |
| 83 | + spdlog::info("Hyprland IPC starting"); |
72 | 84 |
|
73 | | - auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock"; |
74 | | - strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1); |
| 85 | + struct sockaddr_un addr; |
| 86 | + socketfd_ = socket(AF_UNIX, SOCK_STREAM, 0); |
75 | 87 |
|
76 | | - addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 88 | + if (socketfd_ == -1) { |
| 89 | + spdlog::error("Hyprland IPC: socketfd failed"); |
| 90 | + return; |
| 91 | + } |
77 | 92 |
|
78 | | - int l = sizeof(struct sockaddr_un); |
| 93 | + addr.sun_family = AF_UNIX; |
79 | 94 |
|
80 | | - if (connect(socketfd, (struct sockaddr*)&addr, l) == -1) { |
81 | | - spdlog::error("Hyprland IPC: Unable to connect?"); |
82 | | - return; |
83 | | - } |
| 95 | + auto socketPath = IPC::getSocketFolder(his) / ".socket2.sock"; |
| 96 | + strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1); |
84 | 97 |
|
85 | | - auto* file = fdopen(socketfd, "r"); |
| 98 | + addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
86 | 99 |
|
87 | | - while (true) { |
88 | | - std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes |
| 100 | + int l = sizeof(struct sockaddr_un); |
89 | 101 |
|
90 | | - auto* receivedCharPtr = fgets(buffer.data(), buffer.size(), file); |
| 102 | + if (connect(socketfd_, (struct sockaddr*)&addr, l) == -1) { |
| 103 | + spdlog::error("Hyprland IPC: Unable to connect?"); |
| 104 | + return; |
| 105 | + } |
| 106 | + auto* file = fdopen(socketfd_, "r"); |
| 107 | + if (file == nullptr) { |
| 108 | + spdlog::error("Hyprland IPC: Couldn't open file descriptor"); |
| 109 | + return; |
| 110 | + } |
| 111 | + while (running_) { |
| 112 | + std::array<char, 1024> buffer; // Hyprland socket2 events are max 1024 bytes |
91 | 113 |
|
92 | | - if (receivedCharPtr == nullptr) { |
93 | | - std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
94 | | - continue; |
95 | | - } |
| 114 | + auto* receivedCharPtr = fgets(buffer.data(), buffer.size(), file); |
96 | 115 |
|
97 | | - std::string messageReceived(buffer.data()); |
98 | | - messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n')); |
99 | | - spdlog::debug("hyprland IPC received {}", messageReceived); |
| 116 | + if (receivedCharPtr == nullptr) { |
| 117 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 118 | + continue; |
| 119 | + } |
100 | 120 |
|
101 | | - try { |
102 | | - parseIPC(messageReceived); |
103 | | - } catch (std::exception& e) { |
104 | | - spdlog::warn("Failed to parse IPC message: {}, reason: {}", messageReceived, e.what()); |
105 | | - } catch (...) { |
106 | | - throw; |
107 | | - } |
| 121 | + std::string messageReceived(buffer.data()); |
| 122 | + messageReceived = messageReceived.substr(0, messageReceived.find_first_of('\n')); |
| 123 | + spdlog::debug("hyprland IPC received {}", messageReceived); |
108 | 124 |
|
109 | | - std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 125 | + try { |
| 126 | + parseIPC(messageReceived); |
| 127 | + } catch (std::exception& e) { |
| 128 | + spdlog::warn("Failed to parse IPC message: {}, reason: {}", messageReceived, e.what()); |
| 129 | + } catch (...) { |
| 130 | + throw; |
110 | 131 | } |
111 | | - }).detach(); |
| 132 | + |
| 133 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 134 | + } |
| 135 | + spdlog::debug("Hyprland IPC stopped"); |
112 | 136 | } |
113 | 137 |
|
114 | 138 | void IPC::parseIPC(const std::string& ev) { |
|
0 commit comments