Skip to content

Commit 7d2ce3c

Browse files
committed
Merge branch 'iss-3-events' into dev, resolved #2
2 parents 2962a3c + 61cd368 commit 7d2ce3c

File tree

3 files changed

+86
-51
lines changed

3 files changed

+86
-51
lines changed

examples/workspaces.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ void dump_tree_container(const i3ipc::container_t& c, std::string& prefix) {
3030
int main() {
3131
i3ipc::connection conn;
3232
for (auto& w : conn.get_workspaces()) {
33-
std::cout << '#' << std::hex << w.num << std::dec
34-
<< "\n\tName: " << w.name
35-
<< "\n\tVisible: " << w.visible
36-
<< "\n\tFocused: " << w.focused
37-
<< "\n\tUrgent: " << w.urgent
33+
std::cout << '#' << std::hex << w->num << std::dec
34+
<< "\n\tName: " << w->name
35+
<< "\n\tVisible: " << w->visible
36+
<< "\n\tFocused: " << w->focused
37+
<< "\n\tUrgent: " << w->urgent
3838
<< "\n\tRect: "
39-
<< "\n\t\tX: " << w.rect.x
40-
<< "\n\t\tY: " << w.rect.y
41-
<< "\n\t\tWidth: " << w.rect.width
42-
<< "\n\t\tHeight: " << w.rect.height
43-
<< "\n\tOutput: " << w.output
39+
<< "\n\t\tX: " << w->rect.x
40+
<< "\n\t\tY: " << w->rect.y
41+
<< "\n\t\tWidth: " << w->rect.width
42+
<< "\n\t\tHeight: " << w->rect.height
43+
<< "\n\tOutput: " << w->output
4444
<< std::endl;
4545
}
4646
std::string prefix_buf;

include/i3ipc++/ipc.hpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ struct container_t {
153153
std::list< std::shared_ptr<container_t> > nodes;
154154
};
155155

156+
157+
/**
158+
* A workspace event
159+
*/
160+
struct workspace_event_t {
161+
WorkspaceEventType type;
162+
std::shared_ptr<workspace_t> current; ///< Current focused workspace
163+
std::shared_ptr<workspace_t> old; ///< Old (previous) workspace @note With some WindowEventType could be null
164+
};
165+
166+
167+
/**
168+
* A window event
169+
*/
170+
struct window_event_t {
171+
WindowEventType type;
172+
std::shared_ptr<container_t> container; ///< A container event associated with @note With some WindowEventType could be null
173+
};
174+
175+
156176
/**
157177
* @deprecated
158178
*/
@@ -182,13 +202,13 @@ class connection {
182202
* Request a list of workspaces
183203
* @return List of workspaces
184204
*/
185-
std::vector<workspace_t> get_workspaces() const;
205+
std::vector< std::shared_ptr<workspace_t> > get_workspaces() const;
186206

187207
/**
188208
* Request a list of outputs
189209
* @return List of outputs
190210
*/
191-
std::vector<output_t> get_outputs() const;
211+
std::vector< std::shared_ptr<output_t> > get_outputs() const;
192212

193213
/**
194214
* Request a version of i3
@@ -231,10 +251,10 @@ class connection {
231251
*/
232252
void handle_event();
233253

234-
sigc::signal<void, WorkspaceEventType> signal_workspace_event; ///< Workspace event signal
254+
sigc::signal<void, const workspace_event_t&> signal_workspace_event; ///< Workspace event signal
235255
sigc::signal<void> signal_output_event; ///< Output event signal
236256
sigc::signal<void> signal_mode_event; ///< Output mode event signal
237-
sigc::signal<void, WindowEventType> signal_window_event; ///< Window event signal
257+
sigc::signal<void, const window_event_t&> signal_window_event; ///< Window event signal
238258
sigc::signal<void> signal_barconfig_update_event; ///< Barconfig update event signal
239259
sigc::signal<void, EventType, const std::shared_ptr<const buf_t>&> signal_event; ///< i3 event signal @note Default handler routes event to signal according to type
240260
private:

src/ipc.cpp

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static std::shared_ptr<container_t> parse_container_from_json(const Json::Value
113113
#undef i3IPC_TYPE_STR
114114
}
115115

116-
static workspace_t parse_workspace_from_json(const Json::Value& value) {
116+
static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value& value) {
117117
Json::Value num = value["num"];
118118
Json::Value name = value["name"];
119119
Json::Value visible = value["visible"];
@@ -122,29 +122,29 @@ static workspace_t parse_workspace_from_json(const Json::Value& value) {
122122
Json::Value rect = value["rect"];
123123
Json::Value output = value["output"];
124124

125-
return {
126-
.num = num.asInt(),
127-
.name = name.asString(),
128-
.visible = visible.asBool(),
129-
.focused = focused.asBool(),
130-
.urgent = urgent.asBool(),
131-
.rect = parse_rect_from_json(rect),
132-
.output = output.asString(),
133-
};
125+
std::shared_ptr<workspace_t> p;
126+
p->num = num.asInt();
127+
p->name = name.asString();
128+
p->visible = visible.asBool();
129+
p->focused = focused.asBool();
130+
p->urgent = urgent.asBool();
131+
p->rect = parse_rect_from_json(rect);
132+
p->output = output.asString();
133+
return p;
134134
}
135135

136-
static output_t parse_output_from_json(const Json::Value& value) {
136+
static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& value) {
137137
Json::Value name = value["name"];
138138
Json::Value active = value["active"];
139139
Json::Value current_workspace = value["current_workspace"];
140140
Json::Value rect = value["rect"];
141141

142-
return {
143-
.name = name.asString(),
144-
.active = active.asBool(),
145-
.current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString()),
146-
.rect = parse_rect_from_json(rect),
147-
};
142+
std::shared_ptr<output_t> p;
143+
p->name = name.asString();
144+
p->active = active.asBool();
145+
p->current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString());
146+
p->rect = parse_rect_from_json(rect);
147+
return p;
148148
}
149149

150150

@@ -176,25 +176,35 @@ connection::connection(const std::string& socket_path) : m_main_socket(i3_conne
176176
signal_event.connect([this](EventType event_type, const std::shared_ptr<const buf_t>& buf) {
177177
switch (event_type) {
178178
case ET_WORKSPACE: {
179-
WorkspaceEventType type;
179+
workspace_event_t ev;
180180
Json::Value root;
181181
IPC_JSON_READ(root);
182182
std::string change = root["change"].asString();
183183
if (change == "focus") {
184-
type = WorkspaceEventType::FOCUS;
184+
ev.type = WorkspaceEventType::FOCUS;
185185
} else if (change == "init") {
186-
type = WorkspaceEventType::INIT;
186+
ev.type = WorkspaceEventType::INIT;
187187
} else if (change == "empty") {
188-
type = WorkspaceEventType::EMPTY;
188+
ev.type = WorkspaceEventType::EMPTY;
189189
} else if (change == "urgent") {
190-
type = WorkspaceEventType::URGENT;
190+
ev.type = WorkspaceEventType::URGENT;
191191
} else {
192192
I3IPC_WARN("Unknown workspace event type " << change)
193193
break;
194194
}
195195
I3IPC_DEBUG("WORKSPACE " << change)
196196

197-
signal_workspace_event.emit(type);
197+
Json::Value current = root["current"];
198+
Json::Value old = root["current"];
199+
200+
if (!current.isNull()) {
201+
ev.current = parse_workspace_from_json(current);
202+
}
203+
if (!old.isNull()) {
204+
ev.old = parse_workspace_from_json(old);
205+
}
206+
207+
signal_workspace_event.emit(ev);
198208
break;
199209
}
200210
case ET_OUTPUT:
@@ -206,30 +216,35 @@ connection::connection(const std::string& socket_path) : m_main_socket(i3_conne
206216
signal_mode_event.emit();
207217
break;
208218
case ET_WINDOW: {
209-
WindowEventType type;
219+
window_event_t ev;
210220
Json::Value root;
211221
IPC_JSON_READ(root);
212222
std::string change = root["change"].asString();
213223
if (change == "new") {
214-
type = WindowEventType::NEW;
224+
ev.type = WindowEventType::NEW;
215225
} else if (change == "close") {
216-
type = WindowEventType::CLOSE;
226+
ev.type = WindowEventType::CLOSE;
217227
} else if (change == "focus") {
218-
type = WindowEventType::FOCUS;
228+
ev.type = WindowEventType::FOCUS;
219229
} else if (change == "title") {
220-
type = WindowEventType::TITLE;
230+
ev.type = WindowEventType::TITLE;
221231
} else if (change == "fullscreen_mode") {
222-
type = WindowEventType::FULLSCREEN_MODE;
232+
ev.type = WindowEventType::FULLSCREEN_MODE;
223233
} else if (change == "move") {
224-
type = WindowEventType::MOVE;
234+
ev.type = WindowEventType::MOVE;
225235
} else if (change == "floating") {
226-
type = WindowEventType::FLOATING;
236+
ev.type = WindowEventType::FLOATING;
227237
} else if (change == "urgent") {
228-
type = WindowEventType::URGENT;
238+
ev.type = WindowEventType::URGENT;
229239
}
230240
I3IPC_DEBUG("WINDOW " << change)
231241

232-
signal_window_event.emit(type);
242+
Json::Value container = root["container"];
243+
if (!container.isNull()) {
244+
ev.container = parse_container_from_json(container);
245+
}
246+
247+
signal_window_event.emit(ev);
233248
break;
234249
}
235250
case ET_BARCONFIG_UPDATE:
@@ -332,14 +347,14 @@ std::shared_ptr<container_t> connection::get_tree() const {
332347
}
333348

334349

335-
std::vector<output_t> connection::get_outputs() const {
350+
std::vector< std::shared_ptr<output_t> > connection::get_outputs() const {
336351
#define i3IPC_TYPE_STR "GET_OUTPUTS"
337352
auto buf = i3_msg(m_main_socket, ClientMessageType::GET_OUTPUTS);
338353
Json::Value root;
339354
IPC_JSON_READ(root)
340355
IPC_JSON_ASSERT_TYPE_ARRAY(root, "root")
341356

342-
std::vector<output_t> outputs;
357+
std::vector< std::shared_ptr<output_t> > outputs;
343358

344359
for (auto w : root) {
345360
outputs.push_back(parse_output_from_json(w));
@@ -350,14 +365,14 @@ std::vector<output_t> connection::get_outputs() const {
350365
}
351366

352367

353-
std::vector<workspace_t> connection::get_workspaces() const {
368+
std::vector< std::shared_ptr<workspace_t> > connection::get_workspaces() const {
354369
#define i3IPC_TYPE_STR "GET_WORKSPACES"
355370
auto buf = i3_msg(m_main_socket, ClientMessageType::GET_WORKSPACES);
356371
Json::Value root;
357372
IPC_JSON_READ(root)
358373
IPC_JSON_ASSERT_TYPE_ARRAY(root, "root")
359374

360-
std::vector<workspace_t> workspaces;
375+
std::vector< std::shared_ptr<workspace_t> > workspaces;
361376

362377
for (auto w : root) {
363378
workspaces.push_back(parse_workspace_from_json(w));

0 commit comments

Comments
 (0)