Skip to content

Commit abe80f9

Browse files
committed
Refraction of JSON parsing
1 parent 46bb858 commit abe80f9

File tree

1 file changed

+100
-92
lines changed

1 file changed

+100
-92
lines changed

src/ipc.cpp

Lines changed: 100 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,104 @@ inline rect_t parse_rect_from_json(const Json::Value& value) {
5050
}
5151

5252

53+
static std::shared_ptr<container_t> parse_container_from_json(const Json::Value& o) {
54+
#define i3IPC_TYPE_STR "GET_TREE" // FIXME
55+
std::shared_ptr<container_t> container (new container_t());
56+
IPC_JSON_ASSERT_TYPE_OBJECT(o, "o")
57+
58+
container->id = o["id"].asUInt64();
59+
container->xwindow_id= o["window"].asUInt64();
60+
container->name = o["name"].asString();
61+
container->type = o["type"].asString();
62+
container->current_border_width = o["current_border_width"].asInt();
63+
container->percent = o["percent"].asFloat();
64+
container->rect = parse_rect_from_json(o["rect"]);
65+
container->window_rect = parse_rect_from_json(o["window_rect"]);
66+
container->deco_rect = parse_rect_from_json(o["deco_rect"]);
67+
container->geometry = parse_rect_from_json(o["geometry"]);
68+
container->urgent = o["urgent"].asBool();
69+
container->focused = o["focused"].asBool();
70+
71+
container->border = BorderStyle::UNKNOWN;
72+
std::string border = o["border"].asString();
73+
if (border == "normal") {
74+
container->border = BorderStyle::NORMAL;
75+
} else if (border == "none") {
76+
container->border = BorderStyle::NONE;
77+
} else if (border == "1pixel") {
78+
container->border = BorderStyle::ONE_PIXEL;
79+
} else {
80+
container->border_raw = border;
81+
I3IPC_WARN("Got a unknown \"border\" property: \"" << border << "\". Perhaps its neccessary to update i3ipc++. If you are using latest, note maintainer about this")
82+
}
83+
84+
container->layout = ContainerLayout::UNKNOWN;
85+
std::string layout = o["layout"].asString();
86+
87+
if (layout == "splith") {
88+
container->layout = ContainerLayout::SPLIT_H;
89+
} else if (layout == "splitv") {
90+
container->layout = ContainerLayout::SPLIT_V;
91+
} else if (layout == "stacked") {
92+
container->layout = ContainerLayout::STACKED;
93+
} else if (layout == "tabbed") {
94+
container->layout = ContainerLayout::TABBED;
95+
} else if (layout == "dockarea") {
96+
container->layout = ContainerLayout::DOCKAREA;
97+
} else if (layout == "output") {
98+
container->layout = ContainerLayout::OUTPUT;
99+
} else {
100+
container->layout_raw = border;
101+
I3IPC_WARN("Got a unknown \"layout\" property: \"" << layout << "\". Perhaps its neccessary to update i3ipc++. If you are using latest, note maintainer about this")
102+
}
103+
104+
Json::Value nodes = o["nodes"];
105+
if (!nodes.isNull()) {
106+
IPC_JSON_ASSERT_TYPE_ARRAY(nodes, "nodes")
107+
for (Json::ArrayIndex i = 0; i < nodes.size(); i++) {
108+
container->nodes.push_back(parse_container_from_json(nodes[i]));
109+
}
110+
}
111+
112+
return container;
113+
#undef i3IPC_TYPE_STR
114+
}
115+
116+
static workspace_t parse_workspace_from_json(const Json::Value& value) {
117+
Json::Value num = value["num"];
118+
Json::Value name = value["name"];
119+
Json::Value visible = value["visible"];
120+
Json::Value focused = value["focused"];
121+
Json::Value urgent = value["urgent"];
122+
Json::Value rect = value["rect"];
123+
Json::Value output = value["output"];
124+
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+
};
134+
}
135+
136+
static output_t parse_output_from_json(const Json::Value& value) {
137+
Json::Value name = value["name"];
138+
Json::Value active = value["active"];
139+
Json::Value current_workspace = value["current_workspace"];
140+
Json::Value rect = value["rect"];
141+
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+
};
148+
}
149+
150+
53151
std::string get_socketpath() {
54152
std::string str;
55153
{
@@ -224,70 +322,6 @@ version_t I3Connection::get_version() const {
224322
}
225323

226324

227-
static std::shared_ptr<container_t> parse_container_from_json(const Json::Value& o) {
228-
#define i3IPC_TYPE_STR "GET_TREE"
229-
std::shared_ptr<container_t> container (new container_t());
230-
IPC_JSON_ASSERT_TYPE_OBJECT(o, "o")
231-
232-
container->id = o["id"].asUInt64();
233-
container->xwindow_id= o["window"].asUInt64();
234-
container->name = o["name"].asString();
235-
container->type = o["type"].asString();
236-
container->current_border_width = o["current_border_width"].asInt();
237-
container->percent = o["percent"].asFloat();
238-
container->rect = parse_rect_from_json(o["rect"]);
239-
container->window_rect = parse_rect_from_json(o["window_rect"]);
240-
container->deco_rect = parse_rect_from_json(o["deco_rect"]);
241-
container->geometry = parse_rect_from_json(o["geometry"]);
242-
container->urgent = o["urgent"].asBool();
243-
container->focused = o["focused"].asBool();
244-
245-
container->border = BorderStyle::UNKNOWN;
246-
std::string border = o["border"].asString();
247-
if (border == "normal") {
248-
container->border = BorderStyle::NORMAL;
249-
} else if (border == "none") {
250-
container->border = BorderStyle::NONE;
251-
} else if (border == "1pixel") {
252-
container->border = BorderStyle::ONE_PIXEL;
253-
} else {
254-
container->border_raw = border;
255-
I3IPC_WARN("Got a unknown \"border\" property: \"" << border << "\". Perhaps its neccessary to update i3ipc++. If you are using latest, note maintainer about this")
256-
}
257-
258-
container->layout = ContainerLayout::UNKNOWN;
259-
std::string layout = o["layout"].asString();
260-
261-
if (layout == "splith") {
262-
container->layout = ContainerLayout::SPLIT_H;
263-
} else if (layout == "splitv") {
264-
container->layout = ContainerLayout::SPLIT_V;
265-
} else if (layout == "stacked") {
266-
container->layout = ContainerLayout::STACKED;
267-
} else if (layout == "tabbed") {
268-
container->layout = ContainerLayout::TABBED;
269-
} else if (layout == "dockarea") {
270-
container->layout = ContainerLayout::DOCKAREA;
271-
} else if (layout == "output") {
272-
container->layout = ContainerLayout::OUTPUT;
273-
} else {
274-
container->layout_raw = border;
275-
I3IPC_WARN("Got a unknown \"layout\" property: \"" << layout << "\". Perhaps its neccessary to update i3ipc++. If you are using latest, note maintainer about this")
276-
}
277-
278-
Json::Value nodes = o["nodes"];
279-
if (!nodes.isNull()) {
280-
IPC_JSON_ASSERT_TYPE_ARRAY(nodes, "nodes")
281-
for (Json::ArrayIndex i = 0; i < nodes.size(); i++) {
282-
container->nodes.push_back(parse_container_from_json(nodes[i]));
283-
}
284-
}
285-
286-
return container;
287-
#undef i3IPC_TYPE_STR
288-
}
289-
290-
291325
std::shared_ptr<container_t> I3Connection::get_tree() const {
292326
#define i3IPC_TYPE_STR "GET_TREE"
293327
auto buf = i3_msg(m_main_socket, ClientMessageType::GET_TREE);
@@ -308,17 +342,7 @@ std::vector<output_t> I3Connection::get_outputs() const {
308342
std::vector<output_t> outputs;
309343

310344
for (auto w : root) {
311-
Json::Value name = w["name"];
312-
Json::Value active = w["active"];
313-
Json::Value current_workspace = w["current_workspace"];
314-
Json::Value rect = w["rect"];
315-
316-
outputs.push_back({
317-
.name = name.asString(),
318-
.active = active.asBool(),
319-
.current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString()),
320-
.rect = parse_rect_from_json(rect),
321-
});
345+
outputs.push_back(parse_output_from_json(w));
322346
}
323347

324348
return outputs;
@@ -336,23 +360,7 @@ std::vector<workspace_t> I3Connection::get_workspaces() const {
336360
std::vector<workspace_t> workspaces;
337361

338362
for (auto w : root) {
339-
Json::Value num = w["num"];
340-
Json::Value name = w["name"];
341-
Json::Value visible = w["visible"];
342-
Json::Value focused = w["focused"];
343-
Json::Value urgent = w["urgent"];
344-
Json::Value rect = w["rect"];
345-
Json::Value output = w["output"];
346-
347-
workspaces.push_back({
348-
.num = num.asInt(),
349-
.name = name.asString(),
350-
.visible = visible.asBool(),
351-
.focused = focused.asBool(),
352-
.urgent = urgent.asBool(),
353-
.rect = parse_rect_from_json(rect),
354-
.output = output.asString(),
355-
});
363+
workspaces.push_back(parse_workspace_from_json(w));
356364
}
357365

358366
return workspaces;

0 commit comments

Comments
 (0)