From 12ff82e9137a323773b45b66f82930735349f095 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 26 Nov 2016 10:33:32 +0100 Subject: [PATCH] feat(xworkspaces): Change current desktop --- include/modules/xworkspaces.hpp | 11 ++++++ include/x11/ewmh.hpp | 2 ++ src/modules/xworkspaces.cpp | 59 ++++++++++++++++++++++++++++++--- src/x11/ewmh.cpp | 4 +++ 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/include/modules/xworkspaces.hpp b/include/modules/xworkspaces.hpp index eb54885c..7fb8d03b 100644 --- a/include/modules/xworkspaces.hpp +++ b/include/modules/xworkspaces.hpp @@ -51,6 +51,10 @@ namespace modules { void update(); string get_output(); bool build(builder* builder, const string& tag) const; + bool handle_event(string cmd); + bool receive_events() const { + return true; + } protected: void rebuild_desktops(); @@ -64,6 +68,11 @@ namespace modules { static constexpr const char* TAG_LABEL_MONITOR{""}; static constexpr const char* TAG_LABEL_STATE{""}; + static constexpr const char* EVENT_PREFIX{"xworkspaces-"}; + static constexpr const char* EVENT_CLICK{"focus="}; + static constexpr const char* EVENT_SCROLL_UP{"next"}; + static constexpr const char* EVENT_SCROLL_DOWN{"prev"}; + connection& m_connection; ewmh_connection_t m_ewmh; vector m_monitors; @@ -74,6 +83,8 @@ namespace modules { label_t m_monitorlabel; iconset_t m_icons; bool m_pinworkspaces{false}; + bool m_click{true}; + bool m_scroll{true}; size_t m_index{0}; }; } diff --git a/include/x11/ewmh.hpp b/include/x11/ewmh.hpp index 103a864e..749334ac 100644 --- a/include/x11/ewmh.hpp +++ b/include/x11/ewmh.hpp @@ -27,6 +27,8 @@ namespace ewmh_util { vector get_desktop_names(xcb_ewmh_connection_t* conn, int screen = 0); uint32_t get_current_desktop(xcb_ewmh_connection_t* conn, int screen = 0); xcb_window_t get_active_window(xcb_ewmh_connection_t* conn, int screen = 0); + + void change_current_desktop(xcb_ewmh_connection_t* conn, uint32_t desktop); } POLYBAR_NS_END diff --git a/src/modules/xworkspaces.cpp b/src/modules/xworkspaces.cpp index 5500a9e2..37ec343f 100644 --- a/src/modules/xworkspaces.cpp +++ b/src/modules/xworkspaces.cpp @@ -29,6 +29,8 @@ namespace modules { void xworkspaces_module::setup() { // Load config values m_pinworkspaces = m_conf.get(name(), "pin-workspaces", m_pinworkspaces); + m_click = m_conf.get(name(), "enable-click", m_click); + m_scroll = m_conf.get(name(), "enable-scroll", m_scroll); // Initialize ewmh atoms if ((m_ewmh = ewmh_util::initialize()) == nullptr) { @@ -157,9 +159,11 @@ namespace modules { } if (current == n) { - m_viewports.back()->desktops.emplace_back(make_pair(desktop_state::ACTIVE, m_labels[desktop_state::ACTIVE]->clone())); + m_viewports.back()->desktops.emplace_back( + make_pair(desktop_state::ACTIVE, m_labels[desktop_state::ACTIVE]->clone())); } else { - m_viewports.back()->desktops.emplace_back(make_pair(desktop_state::EMPTY, m_labels[desktop_state::EMPTY]->clone())); + m_viewports.back()->desktops.emplace_back( + make_pair(desktop_state::EMPTY, m_labels[desktop_state::EMPTY]->clone())); } auto& desktop = m_viewports.back()->desktops.back(); @@ -193,17 +197,62 @@ namespace modules { return true; } else if (tag == TAG_LABEL_STATE) { size_t num{0}; - for (auto&& d : m_viewports[m_index]->desktops) { - if (d.second.get()) { + + if (m_scroll) { + builder->cmd(mousebtn::SCROLL_DOWN, string{EVENT_PREFIX} + string{EVENT_SCROLL_DOWN}); + builder->cmd(mousebtn::SCROLL_UP, string{EVENT_PREFIX} + string{EVENT_SCROLL_UP}); + } + + for (auto&& desktop : m_viewports[m_index]->desktops) { + if (!desktop.second.get()) { + continue; + } + + if (m_click) { + builder->cmd(mousebtn::LEFT, string{EVENT_PREFIX} + string{EVENT_CLICK} + to_string(num++)); + builder->node(desktop.second); + builder->cmd_close(); + } else { num++; - builder->node(d.second); + builder->node(desktop.second); } } + + if (m_scroll) { + builder->cmd_close(); + builder->cmd_close(); + } + return num > 0; } return false; } + + bool xworkspaces_module::handle_event(string cmd) { + if (cmd.find(EVENT_PREFIX) != 0) { + return false; + } + + cmd.erase(0, strlen(EVENT_PREFIX)); + + if (cmd.compare(0, strlen(EVENT_CLICK), EVENT_CLICK) == 0) { + cmd.erase(0, strlen(EVENT_CLICK)); + ewmh_util::change_current_desktop(m_ewmh.get(), atoi(cmd.c_str())); + + } else if (cmd.compare(0, strlen(EVENT_SCROLL_UP), EVENT_SCROLL_UP) == 0) { + auto current = ewmh_util::get_current_desktop(m_ewmh.get()); + ewmh_util::change_current_desktop(m_ewmh.get(), current + 1); + + } else if (cmd.compare(0, strlen(EVENT_SCROLL_DOWN), EVENT_SCROLL_DOWN) == 0) { + auto current = ewmh_util::get_current_desktop(m_ewmh.get()); + ewmh_util::change_current_desktop(m_ewmh.get(), current - 1); + } + + m_connection.flush(); + + return true; + } } POLYBAR_NS_END diff --git a/src/x11/ewmh.cpp b/src/x11/ewmh.cpp index 3e70c6a8..0243bf6b 100644 --- a/src/x11/ewmh.cpp +++ b/src/x11/ewmh.cpp @@ -131,6 +131,10 @@ namespace ewmh_util { } return win; } + + void change_current_desktop(xcb_ewmh_connection_t* conn, uint32_t desktop) { + xcb_ewmh_request_change_current_desktop(conn, 0, desktop, XCB_CURRENT_TIME); + } } POLYBAR_NS_END