8
8
#include < sys/types.h>
9
9
#include < sys/un.h>
10
10
#include < unistd.h>
11
- #include " giomm/datainputstream.h"
12
- #include " giomm/dataoutputstream.h"
13
- #include " giomm/unixinputstream.h"
14
- #include " giomm/unixoutputstream.h"
15
11
16
12
#include < iostream>
17
13
#include < string>
18
14
#include < thread>
19
15
16
+ #include " giomm/datainputstream.h"
17
+ #include " giomm/dataoutputstream.h"
18
+ #include " giomm/unixinputstream.h"
19
+ #include " giomm/unixoutputstream.h"
20
+
20
21
namespace waybar ::modules::niri {
21
22
22
23
int IPC::connectToSocket () {
23
- const char * socket_path = getenv (" NIRI_SOCKET" );
24
+ const char * socket_path = getenv (" NIRI_SOCKET" );
24
25
25
- if (socket_path == nullptr ) {
26
- spdlog::warn (" Niri is not running, niri IPC will not be available." );
27
- return -1 ;
28
- }
26
+ if (socket_path == nullptr ) {
27
+ spdlog::warn (" Niri is not running, niri IPC will not be available." );
28
+ return -1 ;
29
+ }
29
30
30
- struct sockaddr_un addr;
31
- int socketfd = socket (AF_UNIX, SOCK_STREAM, 0 );
31
+ struct sockaddr_un addr;
32
+ int socketfd = socket (AF_UNIX, SOCK_STREAM, 0 );
32
33
33
- if (socketfd == -1 ) {
34
- throw std::runtime_error (" socketfd failed" );
35
- }
34
+ if (socketfd == -1 ) {
35
+ throw std::runtime_error (" socketfd failed" );
36
+ }
36
37
37
- addr.sun_family = AF_UNIX;
38
+ addr.sun_family = AF_UNIX;
38
39
39
- strncpy (addr.sun_path , socket_path, sizeof (addr.sun_path ) - 1 );
40
+ strncpy (addr.sun_path , socket_path, sizeof (addr.sun_path ) - 1 );
40
41
41
- addr.sun_path [sizeof (addr.sun_path ) - 1 ] = 0 ;
42
+ addr.sun_path [sizeof (addr.sun_path ) - 1 ] = 0 ;
42
43
43
- int l = sizeof (struct sockaddr_un );
44
+ int l = sizeof (struct sockaddr_un );
44
45
45
- if (connect (socketfd, (struct sockaddr *)&addr, l) == -1 ) {
46
- close (socketfd);
47
- throw std::runtime_error (" unable to connect" );
48
- }
46
+ if (connect (socketfd, (struct sockaddr *)&addr, l) == -1 ) {
47
+ close (socketfd);
48
+ throw std::runtime_error (" unable to connect" );
49
+ }
49
50
50
- return socketfd;
51
+ return socketfd;
51
52
}
52
53
53
54
void IPC::startIPC () {
@@ -61,8 +62,7 @@ void IPC::startIPC() {
61
62
spdlog::error (" Niri IPC: failed to start, reason: {}" , e.what ());
62
63
return ;
63
64
}
64
- if (socketfd == -1 )
65
- return ;
65
+ if (socketfd == -1 ) return ;
66
66
67
67
spdlog::info (" Niri IPC starting" );
68
68
@@ -87,7 +87,7 @@ void IPC::startIPC() {
87
87
88
88
try {
89
89
parseIPC (line);
90
- } catch (std::exception & e) {
90
+ } catch (std::exception & e) {
91
91
spdlog::warn (" Failed to parse IPC message: {}, reason: {}" , line, e.what ());
92
92
} catch (...) {
93
93
throw ;
@@ -98,11 +98,10 @@ void IPC::startIPC() {
98
98
}).detach ();
99
99
}
100
100
101
- void IPC::parseIPC (const std::string& line) {
101
+ void IPC::parseIPC (const std::string & line) {
102
102
const auto ev = parser_.parse (line);
103
103
const auto members = ev.getMemberNames ();
104
- if (members.size () != 1 )
105
- throw std::runtime_error (" Event must have a single member" );
104
+ if (members.size () != 1 ) throw std::runtime_error (" Event must have a single member" );
106
105
107
106
{
108
107
auto lock = lockData ();
@@ -112,17 +111,15 @@ void IPC::parseIPC(const std::string& line) {
112
111
const auto &values = payload[" workspaces" ];
113
112
std::copy (values.begin (), values.end (), std::back_inserter (workspaces_));
114
113
115
- std::sort (workspaces_.begin (), workspaces_.end (),
116
- [](const auto &a, const auto &b) {
117
- const auto &aOutput = a[" output" ].asString ();
118
- const auto &bOutput = b[" output" ].asString ();
119
- const auto aIdx = a[" idx" ].asUInt ();
120
- const auto bIdx = b[" idx" ].asUInt ();
121
- if (aOutput == bOutput)
122
- return aIdx < bIdx;
123
- return aOutput < bOutput;
124
- });
125
- } else if (const auto & payload = ev[" WorkspaceActivated" ]) {
114
+ std::sort (workspaces_.begin (), workspaces_.end (), [](const auto &a, const auto &b) {
115
+ const auto &aOutput = a[" output" ].asString ();
116
+ const auto &bOutput = b[" output" ].asString ();
117
+ const auto aIdx = a[" idx" ].asUInt ();
118
+ const auto bIdx = b[" idx" ].asUInt ();
119
+ if (aOutput == bOutput) return aIdx < bIdx;
120
+ return aOutput < bOutput;
121
+ });
122
+ } else if (const auto &payload = ev[" WorkspaceActivated" ]) {
126
123
const auto id = payload[" id" ].asUInt64 ();
127
124
const auto focused = payload[" focused" ].asBool ();
128
125
auto it = std::find_if (workspaces_.begin (), workspaces_.end (),
@@ -132,19 +129,18 @@ void IPC::parseIPC(const std::string& line) {
132
129
const auto &output = ws[" output" ].asString ();
133
130
for (auto &ws : workspaces_) {
134
131
const auto got_activated = (ws[" id" ].asUInt64 () == id);
135
- if (ws[" output" ] == output)
136
- ws[" is_active" ] = got_activated;
132
+ if (ws[" output" ] == output) ws[" is_active" ] = got_activated;
137
133
138
- if (focused)
139
- ws[" is_focused" ] = got_activated;
134
+ if (focused) ws[" is_focused" ] = got_activated;
140
135
}
141
136
} else {
142
137
spdlog::error (" Activated unknown workspace" );
143
138
}
144
- } else if (const auto & payload = ev[" WorkspaceActiveWindowChanged" ]) {
139
+ } else if (const auto & payload = ev[" WorkspaceActiveWindowChanged" ]) {
145
140
const auto workspaceId = payload[" workspace_id" ].asUInt64 ();
146
- auto it = std::find_if (workspaces_.begin (), workspaces_.end (),
147
- [workspaceId](const auto &ws) { return ws[" id" ].asUInt64 () == workspaceId; });
141
+ auto it = std::find_if (workspaces_.begin (), workspaces_.end (), [workspaceId](const auto &ws) {
142
+ return ws[" id" ].asUInt64 () == workspaceId;
143
+ });
148
144
if (it != workspaces_.end ()) {
149
145
auto &ws = *it;
150
146
ws[" active_window_id" ] = payload[" active_window_id" ];
@@ -157,9 +153,8 @@ void IPC::parseIPC(const std::string& line) {
157
153
keyboardLayoutCurrent_ = layouts[" current_idx" ].asUInt ();
158
154
159
155
keyboardLayoutNames_.clear ();
160
- for (const auto &fullName : names)
161
- keyboardLayoutNames_.push_back (fullName.asString ());
162
- } else if (const auto & payload = ev[" KeyboardLayoutSwitched" ]) {
156
+ for (const auto &fullName : names) keyboardLayoutNames_.push_back (fullName.asString ());
157
+ } else if (const auto &payload = ev[" KeyboardLayoutSwitched" ]) {
163
158
keyboardLayoutCurrent_ = payload[" idx" ].asUInt ();
164
159
} else if (const auto &payload = ev[" WindowsChanged" ]) {
165
160
windows_.clear ();
@@ -201,14 +196,14 @@ void IPC::parseIPC(const std::string& line) {
201
196
202
197
std::unique_lock lock (callbackMutex_);
203
198
204
- for (auto & [eventname, handler] : callbacks_) {
199
+ for (auto & [eventname, handler] : callbacks_) {
205
200
if (eventname == members[0 ]) {
206
201
handler->onEvent (ev);
207
202
}
208
203
}
209
204
}
210
205
211
- void IPC::registerForIPC (const std::string& ev, EventHandler* ev_handler) {
206
+ void IPC::registerForIPC (const std::string & ev, EventHandler * ev_handler) {
212
207
if (ev_handler == nullptr ) {
213
208
return ;
214
209
}
@@ -217,15 +212,15 @@ void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
217
212
callbacks_.emplace_back (ev, ev_handler);
218
213
}
219
214
220
- void IPC::unregisterForIPC (EventHandler* ev_handler) {
215
+ void IPC::unregisterForIPC (EventHandler * ev_handler) {
221
216
if (ev_handler == nullptr ) {
222
217
return ;
223
218
}
224
219
225
220
std::unique_lock lock (callbackMutex_);
226
221
227
222
for (auto it = callbacks_.begin (); it != callbacks_.end ();) {
228
- auto & [eventname, handler] = *it;
223
+ auto & [eventname, handler] = *it;
229
224
if (handler == ev_handler) {
230
225
it = callbacks_.erase (it);
231
226
} else {
@@ -234,10 +229,9 @@ void IPC::unregisterForIPC(EventHandler* ev_handler) {
234
229
}
235
230
}
236
231
237
- Json::Value IPC::send (const Json::Value& request) {
232
+ Json::Value IPC::send (const Json::Value & request) {
238
233
int socketfd = connectToSocket ();
239
- if (socketfd == -1 )
240
- throw std::runtime_error (" Niri is not running" );
234
+ if (socketfd == -1 ) throw std::runtime_error (" Niri is not running" );
241
235
242
236
auto unix_istream = Gio::UnixInputStream::create (socketfd, true );
243
237
auto unix_ostream = Gio::UnixOutputStream::create (socketfd, false );
@@ -256,13 +250,12 @@ Json::Value IPC::send(const Json::Value& request) {
256
250
throw std::runtime_error (" error writing to niri socket" );
257
251
258
252
std::string line;
259
- if (!istream->read_line (line))
260
- throw std::runtime_error (" error reading from niri socket" );
253
+ if (!istream->read_line (line)) throw std::runtime_error (" error reading from niri socket" );
261
254
262
255
std::istringstream iss (std::move (line));
263
256
Json::Value response;
264
257
iss >> response;
265
258
return response;
266
259
}
267
260
268
- } // namespace waybar::modules::hyprland
261
+ } // namespace waybar::modules::niri
0 commit comments