Merge branch 'iss-3-events' into dev, resolved #2

This commit is contained in:
Sergey Naumov
2016-04-24 10:48:36 +03:00
3 changed files with 86 additions and 51 deletions

View File

@@ -30,17 +30,17 @@ void dump_tree_container(const i3ipc::container_t& c, std::string& prefix) {
int main() { int main() {
i3ipc::connection conn; i3ipc::connection conn;
for (auto& w : conn.get_workspaces()) { for (auto& w : conn.get_workspaces()) {
std::cout << '#' << std::hex << w.num << std::dec std::cout << '#' << std::hex << w->num << std::dec
<< "\n\tName: " << w.name << "\n\tName: " << w->name
<< "\n\tVisible: " << w.visible << "\n\tVisible: " << w->visible
<< "\n\tFocused: " << w.focused << "\n\tFocused: " << w->focused
<< "\n\tUrgent: " << w.urgent << "\n\tUrgent: " << w->urgent
<< "\n\tRect: " << "\n\tRect: "
<< "\n\t\tX: " << w.rect.x << "\n\t\tX: " << w->rect.x
<< "\n\t\tY: " << w.rect.y << "\n\t\tY: " << w->rect.y
<< "\n\t\tWidth: " << w.rect.width << "\n\t\tWidth: " << w->rect.width
<< "\n\t\tHeight: " << w.rect.height << "\n\t\tHeight: " << w->rect.height
<< "\n\tOutput: " << w.output << "\n\tOutput: " << w->output
<< std::endl; << std::endl;
} }
std::string prefix_buf; std::string prefix_buf;

View File

@@ -153,6 +153,26 @@ struct container_t {
std::list< std::shared_ptr<container_t> > nodes; std::list< std::shared_ptr<container_t> > nodes;
}; };
/**
* A workspace event
*/
struct workspace_event_t {
WorkspaceEventType type;
std::shared_ptr<workspace_t> current; ///< Current focused workspace
std::shared_ptr<workspace_t> old; ///< Old (previous) workspace @note With some WindowEventType could be null
};
/**
* A window event
*/
struct window_event_t {
WindowEventType type;
std::shared_ptr<container_t> container; ///< A container event associated with @note With some WindowEventType could be null
};
/** /**
* @deprecated * @deprecated
*/ */
@@ -182,13 +202,13 @@ public:
* Request a list of workspaces * Request a list of workspaces
* @return List of workspaces * @return List of workspaces
*/ */
std::vector<workspace_t> get_workspaces() const; std::vector< std::shared_ptr<workspace_t> > get_workspaces() const;
/** /**
* Request a list of outputs * Request a list of outputs
* @return List of outputs * @return List of outputs
*/ */
std::vector<output_t> get_outputs() const; std::vector< std::shared_ptr<output_t> > get_outputs() const;
/** /**
* Request a version of i3 * Request a version of i3
@@ -231,10 +251,10 @@ public:
*/ */
void handle_event(); void handle_event();
sigc::signal<void, WorkspaceEventType> signal_workspace_event; ///< Workspace event signal sigc::signal<void, const workspace_event_t&> signal_workspace_event; ///< Workspace event signal
sigc::signal<void> signal_output_event; ///< Output event signal sigc::signal<void> signal_output_event; ///< Output event signal
sigc::signal<void> signal_mode_event; ///< Output mode event signal sigc::signal<void> signal_mode_event; ///< Output mode event signal
sigc::signal<void, WindowEventType> signal_window_event; ///< Window event signal sigc::signal<void, const window_event_t&> signal_window_event; ///< Window event signal
sigc::signal<void> signal_barconfig_update_event; ///< Barconfig update event signal sigc::signal<void> signal_barconfig_update_event; ///< Barconfig update event signal
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 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
private: private:

View File

@@ -113,7 +113,7 @@ static std::shared_ptr<container_t> parse_container_from_json(const Json::Value
#undef i3IPC_TYPE_STR #undef i3IPC_TYPE_STR
} }
static workspace_t parse_workspace_from_json(const Json::Value& value) { static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value& value) {
Json::Value num = value["num"]; Json::Value num = value["num"];
Json::Value name = value["name"]; Json::Value name = value["name"];
Json::Value visible = value["visible"]; 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 rect = value["rect"];
Json::Value output = value["output"]; Json::Value output = value["output"];
return { std::shared_ptr<workspace_t> p;
.num = num.asInt(), p->num = num.asInt();
.name = name.asString(), p->name = name.asString();
.visible = visible.asBool(), p->visible = visible.asBool();
.focused = focused.asBool(), p->focused = focused.asBool();
.urgent = urgent.asBool(), p->urgent = urgent.asBool();
.rect = parse_rect_from_json(rect), p->rect = parse_rect_from_json(rect);
.output = output.asString(), p->output = output.asString();
}; return p;
} }
static output_t parse_output_from_json(const Json::Value& value) { static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& value) {
Json::Value name = value["name"]; Json::Value name = value["name"];
Json::Value active = value["active"]; Json::Value active = value["active"];
Json::Value current_workspace = value["current_workspace"]; Json::Value current_workspace = value["current_workspace"];
Json::Value rect = value["rect"]; Json::Value rect = value["rect"];
return { std::shared_ptr<output_t> p;
.name = name.asString(), p->name = name.asString();
.active = active.asBool(), p->active = active.asBool();
.current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString()), p->current_workspace = (current_workspace.isNull() ? std::string() : current_workspace.asString());
.rect = parse_rect_from_json(rect), p->rect = parse_rect_from_json(rect);
}; return p;
} }
@@ -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<const buf_t>& buf) { signal_event.connect([this](EventType event_type, const std::shared_ptr<const buf_t>& buf) {
switch (event_type) { switch (event_type) {
case ET_WORKSPACE: { case ET_WORKSPACE: {
WorkspaceEventType type; workspace_event_t ev;
Json::Value root; Json::Value root;
IPC_JSON_READ(root); IPC_JSON_READ(root);
std::string change = root["change"].asString(); std::string change = root["change"].asString();
if (change == "focus") { if (change == "focus") {
type = WorkspaceEventType::FOCUS; ev.type = WorkspaceEventType::FOCUS;
} else if (change == "init") { } else if (change == "init") {
type = WorkspaceEventType::INIT; ev.type = WorkspaceEventType::INIT;
} else if (change == "empty") { } else if (change == "empty") {
type = WorkspaceEventType::EMPTY; ev.type = WorkspaceEventType::EMPTY;
} else if (change == "urgent") { } else if (change == "urgent") {
type = WorkspaceEventType::URGENT; ev.type = WorkspaceEventType::URGENT;
} else { } else {
I3IPC_WARN("Unknown workspace event type " << change) I3IPC_WARN("Unknown workspace event type " << change)
break; break;
} }
I3IPC_DEBUG("WORKSPACE " << change) 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; break;
} }
case ET_OUTPUT: case ET_OUTPUT:
@@ -206,30 +216,35 @@ connection::connection(const std::string& socket_path) : m_main_socket(i3_conne
signal_mode_event.emit(); signal_mode_event.emit();
break; break;
case ET_WINDOW: { case ET_WINDOW: {
WindowEventType type; window_event_t ev;
Json::Value root; Json::Value root;
IPC_JSON_READ(root); IPC_JSON_READ(root);
std::string change = root["change"].asString(); std::string change = root["change"].asString();
if (change == "new") { if (change == "new") {
type = WindowEventType::NEW; ev.type = WindowEventType::NEW;
} else if (change == "close") { } else if (change == "close") {
type = WindowEventType::CLOSE; ev.type = WindowEventType::CLOSE;
} else if (change == "focus") { } else if (change == "focus") {
type = WindowEventType::FOCUS; ev.type = WindowEventType::FOCUS;
} else if (change == "title") { } else if (change == "title") {
type = WindowEventType::TITLE; ev.type = WindowEventType::TITLE;
} else if (change == "fullscreen_mode") { } else if (change == "fullscreen_mode") {
type = WindowEventType::FULLSCREEN_MODE; ev.type = WindowEventType::FULLSCREEN_MODE;
} else if (change == "move") { } else if (change == "move") {
type = WindowEventType::MOVE; ev.type = WindowEventType::MOVE;
} else if (change == "floating") { } else if (change == "floating") {
type = WindowEventType::FLOATING; ev.type = WindowEventType::FLOATING;
} else if (change == "urgent") { } else if (change == "urgent") {
type = WindowEventType::URGENT; ev.type = WindowEventType::URGENT;
} }
I3IPC_DEBUG("WINDOW " << change) 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; break;
} }
case ET_BARCONFIG_UPDATE: case ET_BARCONFIG_UPDATE:
@@ -332,14 +347,14 @@ std::shared_ptr<container_t> connection::get_tree() const {
} }
std::vector<output_t> connection::get_outputs() const { std::vector< std::shared_ptr<output_t> > connection::get_outputs() const {
#define i3IPC_TYPE_STR "GET_OUTPUTS" #define i3IPC_TYPE_STR "GET_OUTPUTS"
auto buf = i3_msg(m_main_socket, ClientMessageType::GET_OUTPUTS); auto buf = i3_msg(m_main_socket, ClientMessageType::GET_OUTPUTS);
Json::Value root; Json::Value root;
IPC_JSON_READ(root) IPC_JSON_READ(root)
IPC_JSON_ASSERT_TYPE_ARRAY(root, "root") IPC_JSON_ASSERT_TYPE_ARRAY(root, "root")
std::vector<output_t> outputs; std::vector< std::shared_ptr<output_t> > outputs;
for (auto w : root) { for (auto w : root) {
outputs.push_back(parse_output_from_json(w)); outputs.push_back(parse_output_from_json(w));
@@ -350,14 +365,14 @@ std::vector<output_t> connection::get_outputs() const {
} }
std::vector<workspace_t> connection::get_workspaces() const { std::vector< std::shared_ptr<workspace_t> > connection::get_workspaces() const {
#define i3IPC_TYPE_STR "GET_WORKSPACES" #define i3IPC_TYPE_STR "GET_WORKSPACES"
auto buf = i3_msg(m_main_socket, ClientMessageType::GET_WORKSPACES); auto buf = i3_msg(m_main_socket, ClientMessageType::GET_WORKSPACES);
Json::Value root; Json::Value root;
IPC_JSON_READ(root) IPC_JSON_READ(root)
IPC_JSON_ASSERT_TYPE_ARRAY(root, "root") IPC_JSON_ASSERT_TYPE_ARRAY(root, "root")
std::vector<workspace_t> workspaces; std::vector< std::shared_ptr<workspace_t> > workspaces;
for (auto w : root) { for (auto w : root) {
workspaces.push_back(parse_workspace_from_json(w)); workspaces.push_back(parse_workspace_from_json(w));