From 19f9acb482a5d640e64ebf1d0b0bbfca3d15aad5 Mon Sep 17 00:00:00 2001 From: Sergey Naumov Date: Sun, 24 Apr 2016 10:33:19 +0300 Subject: [PATCH 1/3] Shipping heavy objects in std::shared_ptr --- examples/workspaces.cpp | 20 ++++++++++---------- include/i3ipc++/ipc.hpp | 4 ++-- src/ipc.cpp | 42 ++++++++++++++++++++--------------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/examples/workspaces.cpp b/examples/workspaces.cpp index acde6fa..174914c 100644 --- a/examples/workspaces.cpp +++ b/examples/workspaces.cpp @@ -30,17 +30,17 @@ void dump_tree_container(const i3ipc::container_t& c, std::string& prefix) { int main() { i3ipc::connection conn; for (auto& w : conn.get_workspaces()) { - std::cout << '#' << std::hex << w.num << std::dec - << "\n\tName: " << w.name - << "\n\tVisible: " << w.visible - << "\n\tFocused: " << w.focused - << "\n\tUrgent: " << w.urgent + std::cout << '#' << std::hex << w->num << std::dec + << "\n\tName: " << w->name + << "\n\tVisible: " << w->visible + << "\n\tFocused: " << w->focused + << "\n\tUrgent: " << w->urgent << "\n\tRect: " - << "\n\t\tX: " << w.rect.x - << "\n\t\tY: " << w.rect.y - << "\n\t\tWidth: " << w.rect.width - << "\n\t\tHeight: " << w.rect.height - << "\n\tOutput: " << w.output + << "\n\t\tX: " << w->rect.x + << "\n\t\tY: " << w->rect.y + << "\n\t\tWidth: " << w->rect.width + << "\n\t\tHeight: " << w->rect.height + << "\n\tOutput: " << w->output << std::endl; } std::string prefix_buf; diff --git a/include/i3ipc++/ipc.hpp b/include/i3ipc++/ipc.hpp index 50c535e..ab8eb91 100644 --- a/include/i3ipc++/ipc.hpp +++ b/include/i3ipc++/ipc.hpp @@ -182,13 +182,13 @@ public: * Request a list of workspaces * @return List of workspaces */ - std::vector get_workspaces() const; + std::vector< std::shared_ptr > get_workspaces() const; /** * Request a list of outputs * @return List of outputs */ - std::vector get_outputs() const; + std::vector< std::shared_ptr > get_outputs() const; /** * Request a version of i3 diff --git a/src/ipc.cpp b/src/ipc.cpp index db19ede..e4409f5 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -113,7 +113,7 @@ static std::shared_ptr parse_container_from_json(const Json::Value #undef i3IPC_TYPE_STR } -static workspace_t parse_workspace_from_json(const Json::Value& value) { +static std::shared_ptr parse_workspace_from_json(const Json::Value& value) { Json::Value num = value["num"]; Json::Value name = value["name"]; Json::Value visible = value["visible"]; @@ -122,29 +122,29 @@ static workspace_t parse_workspace_from_json(const Json::Value& value) { Json::Value rect = value["rect"]; Json::Value output = value["output"]; - return { - .num = num.asInt(), - .name = name.asString(), - .visible = visible.asBool(), - .focused = focused.asBool(), - .urgent = urgent.asBool(), - .rect = parse_rect_from_json(rect), - .output = output.asString(), - }; + std::shared_ptr p; + p->num = num.asInt(); + p->name = name.asString(); + p->visible = visible.asBool(); + p->focused = focused.asBool(); + p->urgent = urgent.asBool(); + p->rect = parse_rect_from_json(rect); + p->output = output.asString(); + return p; } -static output_t parse_output_from_json(const Json::Value& value) { +static std::shared_ptr parse_output_from_json(const Json::Value& value) { Json::Value name = value["name"]; Json::Value active = value["active"]; Json::Value current_workspace = value["current_workspace"]; Json::Value rect = value["rect"]; - return { - .name = name.asString(), - .active = active.asBool(), - .current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString()), - .rect = parse_rect_from_json(rect), - }; + std::shared_ptr p; + p->name = name.asString(); + p->active = active.asBool(); + p->current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString()); + p->rect = parse_rect_from_json(rect); + return p; } @@ -332,14 +332,14 @@ std::shared_ptr connection::get_tree() const { } -std::vector connection::get_outputs() const { +std::vector< std::shared_ptr > connection::get_outputs() const { #define i3IPC_TYPE_STR "GET_OUTPUTS" auto buf = i3_msg(m_main_socket, ClientMessageType::GET_OUTPUTS); Json::Value root; IPC_JSON_READ(root) IPC_JSON_ASSERT_TYPE_ARRAY(root, "root") - std::vector outputs; + std::vector< std::shared_ptr > outputs; for (auto w : root) { outputs.push_back(parse_output_from_json(w)); @@ -350,14 +350,14 @@ std::vector connection::get_outputs() const { } -std::vector connection::get_workspaces() const { +std::vector< std::shared_ptr > connection::get_workspaces() const { #define i3IPC_TYPE_STR "GET_WORKSPACES" auto buf = i3_msg(m_main_socket, ClientMessageType::GET_WORKSPACES); Json::Value root; IPC_JSON_READ(root) IPC_JSON_ASSERT_TYPE_ARRAY(root, "root") - std::vector workspaces; + std::vector< std::shared_ptr > workspaces; for (auto w : root) { workspaces.push_back(parse_workspace_from_json(w)); From b94c4653274d7179573c0c6384c011a4de2211d5 Mon Sep 17 00:00:00 2001 From: Sergey Naumov Date: Sun, 24 Apr 2016 10:40:43 +0300 Subject: [PATCH 2/3] Shipping all payload in workspace event --- include/i3ipc++/ipc.hpp | 13 ++++++++++++- src/ipc.cpp | 22 ++++++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/i3ipc++/ipc.hpp b/include/i3ipc++/ipc.hpp index ab8eb91..584252d 100644 --- a/include/i3ipc++/ipc.hpp +++ b/include/i3ipc++/ipc.hpp @@ -153,6 +153,17 @@ struct container_t { std::list< std::shared_ptr > nodes; }; + +/** + * A workspace event + */ +struct workspace_event_t { + WorkspaceEventType type; + std::shared_ptr current; ///< Current focused workspace + std::shared_ptr old; ///< Old (previous) workspace @note With some WindowEventType could be null +}; + + /** * @deprecated */ @@ -231,7 +242,7 @@ public: */ void handle_event(); - sigc::signal signal_workspace_event; ///< Workspace event signal + sigc::signal signal_workspace_event; ///< Workspace event signal sigc::signal signal_output_event; ///< Output event signal sigc::signal signal_mode_event; ///< Output mode event signal sigc::signal signal_window_event; ///< Window event signal diff --git a/src/ipc.cpp b/src/ipc.cpp index e4409f5..b08d1dd 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -176,25 +176,35 @@ connection::connection(const std::string& socket_path) : m_main_socket(i3_conne signal_event.connect([this](EventType event_type, const std::shared_ptr& buf) { switch (event_type) { case ET_WORKSPACE: { - WorkspaceEventType type; + workspace_event_t ev; Json::Value root; IPC_JSON_READ(root); std::string change = root["change"].asString(); if (change == "focus") { - type = WorkspaceEventType::FOCUS; + ev.type = WorkspaceEventType::FOCUS; } else if (change == "init") { - type = WorkspaceEventType::INIT; + ev.type = WorkspaceEventType::INIT; } else if (change == "empty") { - type = WorkspaceEventType::EMPTY; + ev.type = WorkspaceEventType::EMPTY; } else if (change == "urgent") { - type = WorkspaceEventType::URGENT; + ev.type = WorkspaceEventType::URGENT; } else { I3IPC_WARN("Unknown workspace event type " << change) break; } I3IPC_DEBUG("WORKSPACE " << change) - signal_workspace_event.emit(type); + Json::Value current = root["current"]; + Json::Value old = root["current"]; + + if (!current.isNull()) { + ev.current = parse_workspace_from_json(current); + } + if (!old.isNull()) { + ev.old = parse_workspace_from_json(old); + } + + signal_workspace_event.emit(ev); break; } case ET_OUTPUT: From 61cd3686da8af50f237acffeb325e4ecbce77564 Mon Sep 17 00:00:00 2001 From: Sergey Naumov Date: Sun, 24 Apr 2016 10:47:31 +0300 Subject: [PATCH 3/3] Shipping all payload in window event --- include/i3ipc++/ipc.hpp | 11 ++++++++++- src/ipc.cpp | 25 +++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/i3ipc++/ipc.hpp b/include/i3ipc++/ipc.hpp index 584252d..a36b297 100644 --- a/include/i3ipc++/ipc.hpp +++ b/include/i3ipc++/ipc.hpp @@ -164,6 +164,15 @@ struct workspace_event_t { }; +/** + * A window event + */ +struct window_event_t { + WindowEventType type; + std::shared_ptr container; ///< A container event associated with @note With some WindowEventType could be null +}; + + /** * @deprecated */ @@ -245,7 +254,7 @@ public: sigc::signal signal_workspace_event; ///< Workspace event signal sigc::signal signal_output_event; ///< Output event signal sigc::signal signal_mode_event; ///< Output mode event signal - sigc::signal signal_window_event; ///< Window event signal + sigc::signal signal_window_event; ///< Window event signal sigc::signal signal_barconfig_update_event; ///< Barconfig update event signal sigc::signal&> signal_event; ///< i3 event signal @note Default handler routes event to signal according to type private: diff --git a/src/ipc.cpp b/src/ipc.cpp index b08d1dd..76714a9 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -216,30 +216,35 @@ connection::connection(const std::string& socket_path) : m_main_socket(i3_conne signal_mode_event.emit(); break; case ET_WINDOW: { - WindowEventType type; + window_event_t ev; Json::Value root; IPC_JSON_READ(root); std::string change = root["change"].asString(); if (change == "new") { - type = WindowEventType::NEW; + ev.type = WindowEventType::NEW; } else if (change == "close") { - type = WindowEventType::CLOSE; + ev.type = WindowEventType::CLOSE; } else if (change == "focus") { - type = WindowEventType::FOCUS; + ev.type = WindowEventType::FOCUS; } else if (change == "title") { - type = WindowEventType::TITLE; + ev.type = WindowEventType::TITLE; } else if (change == "fullscreen_mode") { - type = WindowEventType::FULLSCREEN_MODE; + ev.type = WindowEventType::FULLSCREEN_MODE; } else if (change == "move") { - type = WindowEventType::MOVE; + ev.type = WindowEventType::MOVE; } else if (change == "floating") { - type = WindowEventType::FLOATING; + ev.type = WindowEventType::FLOATING; } else if (change == "urgent") { - type = WindowEventType::URGENT; + ev.type = WindowEventType::URGENT; } I3IPC_DEBUG("WINDOW " << change) - signal_window_event.emit(type); + Json::Value container = root["container"]; + if (!container.isNull()) { + ev.container = parse_container_from_json(container); + } + + signal_window_event.emit(ev); break; } case ET_BARCONFIG_UPDATE: