Skip to content

Commit

Permalink
ipc: migrate to jsoncpp
Browse files Browse the repository at this point in the history
nlohmann/json has very slow compilation times.
  • Loading branch information
ammen99 committed Jul 26, 2024
1 parent 8f9779a commit 86ad639
Show file tree
Hide file tree
Showing 20 changed files with 497 additions and 484 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ libinput = dependency('libinput', version: '>=1.7.0')
pixman = dependency('pixman-1')
xkbcommon = dependency('xkbcommon')
libdl = meson.get_compiler('cpp').find_library('dl')
json = dependency('nlohmann_json', version: '>= 3.11.2')
json = dependency('jsoncpp')

# We're not to use system wlroots: So we'll use the subproject
if get_option('use_system_wlroots').disabled()
Expand Down
67 changes: 36 additions & 31 deletions plugins/ipc-rules/ipc-events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "ipc-rules-common.hpp"
#include <set>
#include "plugins/ipc/ipc-method-repository.hpp"
#include "wayfire/seat.hpp"
#include <wayfire/per-output-plugin.hpp>

namespace wf
Expand Down Expand Up @@ -33,18 +34,18 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
}
}

nlohmann::json data;
Json::Value data;
data["event"] = "output-added";
data["output"] = output_to_json(output);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
}

void handle_output_removed(wf::output_t *output) override
{
nlohmann::json data;
Json::Value data;
data["event"] = "output-removed";
data["output"] = output_to_json(output);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
}

// Template FOO for efficient management of signals: ensure that only actually listened-for signals
Expand Down Expand Up @@ -128,23 +129,27 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
std::map<wf::ipc::client_interface_t*, std::set<std::string>> clients;

wf::ipc::method_callback_full on_client_watch =
[=] (nlohmann::json data, wf::ipc::client_interface_t *client)
[=] (Json::Value data, wf::ipc::client_interface_t *client)
{
static constexpr const char *EVENTS = "events";
WFJSON_OPTIONAL_FIELD(data, EVENTS, array);
if (data.isMember(EVENTS) && !data[EVENTS].isArray())
{
return wf::ipc::json_error("Event list is not an array!");
}

std::set<std::string> subscribed_to;
if (data.contains(EVENTS))
if (data.isMember(EVENTS))
{
for (auto& sub : data[EVENTS])
{
if (!sub.is_string())
if (!sub.isString())
{
return wf::ipc::json_error("Event list contains non-string entries!");
}

if (signal_map.count(sub))
if (signal_map.count(sub.asString()))
{
subscribed_to.insert((std::string)sub);
subscribed_to.insert(sub.asString());
}
}
} else
Expand Down Expand Up @@ -177,13 +182,13 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>

void send_view_to_subscribes(wayfire_view view, std::string event_name)
{
nlohmann::json event;
Json::Value event;
event["event"] = event_name;
event["view"] = view_to_json(view);
send_event_to_subscribes(event, event_name);
}

void send_event_to_subscribes(const nlohmann::json& data, const std::string& event_name)
void send_event_to_subscribes(const Json::Value& data, const std::string& event_name)
{
for (auto& [client, events] : clients)
{
Expand All @@ -207,32 +212,32 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
wf::signal::connection_t<wf::view_set_output_signal> on_view_set_output =
[=] (wf::view_set_output_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "view-set-output";
data["output"] = output_to_json(ev->output);
data["view"] = view_to_json(ev->view);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::view_geometry_changed_signal> on_view_geometry_changed =
[=] (wf::view_geometry_changed_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "view-geometry-changed";
data["old-geometry"] = wf::ipc::geometry_to_json(ev->old_geometry);
data["view"] = view_to_json(ev->view);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::view_moved_to_wset_signal> on_view_moved_to_wset =
[=] (wf::view_moved_to_wset_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "view-wset-changed";
data["old-wset"] = wset_to_json(ev->old_wset.get());
data["new-wset"] = wset_to_json(ev->new_wset.get());
data["view"] = view_to_json(ev->view);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::keyboard_focus_changed_signal> on_kbfocus_changed =
Expand All @@ -244,12 +249,12 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
// Tiled rule handler.
wf::signal::connection_t<wf::view_tiled_signal> _tiled = [=] (wf::view_tiled_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "view-tiled";
data["old-edges"] = ev->old_edges;
data["new-edges"] = ev->new_edges;
data["view"] = view_to_json(ev->view);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

// Minimized rule handler.
Expand All @@ -273,12 +278,12 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
wf::signal::connection_t<wf::view_change_workspace_signal> _view_workspace =
[=] (wf::view_change_workspace_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "view-workspace-changed";
data["from"] = wf::ipc::point_to_json(ev->from);
data["to"] = wf::ipc::point_to_json(ev->to);
data["view"] = view_to_json(ev->view);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::view_title_changed_signal> on_title_changed =
Expand All @@ -296,48 +301,48 @@ class ipc_rules_events_methods_t : public wf::per_output_tracker_mixin_t<>
wf::signal::connection_t<wf::output_plugin_activated_changed_signal> on_plugin_activation_changed =
[=] (wf::output_plugin_activated_changed_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "plugin-activation-state-changed";
data["plugin"] = ev->plugin_name;
data["state"] = ev->activated;
data["output"] = ev->output ? (int)ev->output->get_id() : -1;
data["output-data"] = output_to_json(ev->output);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::output_gain_focus_signal> on_output_gain_focus =
[=] (wf::output_gain_focus_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "output-gain-focus";
data["output"] = output_to_json(ev->output);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::workspace_set_changed_signal> on_wset_changed =
[=] (wf::workspace_set_changed_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "output-wset-changed";
data["new-wset"] = ev->new_wset ? (int)ev->new_wset->get_id() : -1;
data["output"] = ev->output ? (int)ev->output->get_id() : -1;
data["new-wset-data"] = wset_to_json(ev->new_wset.get());
data["output-data"] = output_to_json(ev->output);
send_event_to_subscribes(data, data["event"]);
send_event_to_subscribes(data, data["event"].asString());
};

wf::signal::connection_t<wf::workspace_changed_signal> on_wset_workspace_changed =
[=] (wf::workspace_changed_signal *ev)
{
nlohmann::json data;
Json::Value data;
data["event"] = "wset-workspace-changed";
data["previous-workspace"] = wf::ipc::point_to_json(ev->old_viewport);
data["new-workspace"] = wf::ipc::point_to_json(ev->new_viewport);
data["output"] = ev->output ? (int)ev->output->get_id() : -1;
data["wset"] = (ev->output && ev->output->wset()) ? (int)ev->output->wset()->get_id() : -1;
data["output-data"] = output_to_json(ev->output);
data["wset-data"] = ev->output ? wset_to_json(ev->output->wset().get()) : nullptr;
send_event_to_subscribes(data, data["event"]);
data["wset-data"] = ev->output ? wset_to_json(ev->output->wset().get()) : Json::nullValue;
send_event_to_subscribes(data, data["event"].asString());
};
};
}
19 changes: 10 additions & 9 deletions plugins/ipc-rules/ipc-input-methods.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "plugins/ipc/ipc-helpers.hpp"
#include "plugins/ipc/ipc-method-repository.hpp"
#include "wayfire/core.hpp"
#include "wayfire/debug.hpp"
Expand Down Expand Up @@ -49,34 +50,34 @@ class ipc_rules_input_methods_t
}
}

wf::ipc::method_callback list_input_devices = [&] (const nlohmann::json&)
wf::ipc::method_callback list_input_devices = [&] (const Json::Value&)
{
auto response = nlohmann::json::array();
Json::Value response = Json::arrayValue;
for (auto& device : wf::get_core().get_input_devices())
{
nlohmann::json d;
Json::Value d;
d["id"] = (intptr_t)device->get_wlr_handle();
d["name"] = nonull(device->get_wlr_handle()->name);
d["vendor"] = device->get_wlr_handle()->vendor;
d["product"] = device->get_wlr_handle()->product;
d["type"] = wlr_input_device_type_to_string(device->get_wlr_handle()->type);
d["enabled"] = device->is_enabled();
response.push_back(d);
response.append(d);
}

return response;
};

wf::ipc::method_callback configure_input_device = [&] (const nlohmann::json& data)
wf::ipc::method_callback configure_input_device = [&] (const Json::Value& data)
{
WFJSON_EXPECT_FIELD(data, "id", number_unsigned);
WFJSON_EXPECT_FIELD(data, "enabled", boolean);
auto id = wf::ipc::json_get_int64(data, "id");
auto enabled = wf::ipc::json_get_bool(data, "enabled");

for (auto& device : wf::get_core().get_input_devices())
{
if ((intptr_t)device->get_wlr_handle() == data["id"])
if ((intptr_t)device->get_wlr_handle() == id)
{
device->set_enabled(data["enabled"]);
device->set_enabled(enabled);

return wf::ipc::json_ok();
}
Expand Down
18 changes: 9 additions & 9 deletions plugins/ipc-rules/ipc-rules-common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#include <wayfire/unstable/wlr-surface-node.hpp>
#include <wayfire/view-helpers.hpp>

static inline nlohmann::json output_to_json(wf::output_t *o)
static inline Json::Value output_to_json(wf::output_t *o)
{
if (!o)
{
return nullptr;
return Json::nullValue;
}

nlohmann::json response;
Json::Value response;
response["id"] = o->get_id();
response["name"] = o->to_string();
response["geometry"] = wf::ipc::geometry_to_json(o->get_layout_geometry());
Expand Down Expand Up @@ -165,15 +165,15 @@ static inline std::string get_view_type(wayfire_view view)
return "unknown";
}

static inline nlohmann::json view_to_json(wayfire_view view)
static inline Json::Value view_to_json(wayfire_view view)
{
if (!view)
{
return nullptr;
return Json::nullValue;
}

auto output = view->get_output();
nlohmann::json description;
Json::Value description;
description["id"] = view->get_id();
description["pid"] = get_view_pid(view);
description["title"] = view->get_title();
Expand Down Expand Up @@ -206,14 +206,14 @@ static inline nlohmann::json view_to_json(wayfire_view view)
return description;
}

static inline nlohmann::json wset_to_json(wf::workspace_set_t *wset)
static inline Json::Value wset_to_json(wf::workspace_set_t *wset)
{
if (!wset)
{
return nullptr;
return Json::nullValue;
}

nlohmann::json response;
Json::Value response;
response["index"] = wset->get_index();
response["name"] = wset->to_string();

Expand Down
Loading

0 comments on commit 86ad639

Please sign in to comment.