From 23f48cb8b71f1210799e3b398aeb221bcbdf8d32 Mon Sep 17 00:00:00 2001 From: Alexis Date: Thu, 9 Aug 2018 20:22:01 +0200 Subject: [PATCH] refactor: prefer static methods --- .gitignore | 1 + .vscode/settings.json | 14 ---- include/bar.hpp | 13 ++++ include/client.hpp | 10 +-- include/modules/workspaces.hpp | 8 ++- src/bar.cpp | 128 ++++++++++++++++----------------- src/client.cpp | 63 ++++++++-------- src/modules/workspaces.cpp | 63 ++++++++-------- 8 files changed, 152 insertions(+), 148 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index e980e122..d50458c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store *~ vgcore.* +/.vscode *.swp packagecache /subprojects/fmt-4.1.0 diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index d1dab300..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "files.associations": { - "atomic": "cpp", - "array": "cpp", - "hash_map": "cpp", - "deque": "cpp", - "list": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "initializer_list": "cpp", - "string_view": "cpp", - "valarray": "cpp" - } -} \ No newline at end of file diff --git a/include/bar.hpp b/include/bar.hpp index ca59feeb..1439e21b 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -20,6 +20,19 @@ namespace waybar { auto setWidth(int) -> void; auto toggle() -> void; private: + static void _handleGeometry(void *data, struct wl_output *wl_output, + int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, + int32_t subpixel, const char *make, const char *model, int32_t transform); + static void _handleMode(void *data, struct wl_output *wl_output, + uint32_t f, int32_t w, int32_t h, int32_t refresh); + static void _handleDone(void *data, struct wl_output *); + static void _handleScale(void *data, struct wl_output *wl_output, + int32_t factor); + static void _layerSurfaceHandleConfigure(void *data, + struct zwlr_layer_surface_v1 *surface, uint32_t serial, uint32_t width, + uint32_t height); + static void _layerSurfaceHandleClosed(void *data, + struct zwlr_layer_surface_v1 *surface); auto _setupConfig() -> void; auto _setupWidgets() -> void; auto _setupCss() -> void; diff --git a/include/client.hpp b/include/client.hpp index 6e70b0dc..24a838d3 100644 --- a/include/client.hpp +++ b/include/client.hpp @@ -37,14 +37,14 @@ namespace waybar { struct wl_seat *seat; util::ptr_vec bars; - struct { - sigc::signal workspace_state; - sigc::signal focused_window_name; - } signals; - Client(int argc, char* argv[]); void bind_interfaces(); auto setup_css(); int main(int argc, char* argv[]); + private: + static void _handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version); + static void _handle_global_remove(void *data, + struct wl_registry *registry, uint32_t name); }; } diff --git a/include/modules/workspaces.hpp b/include/modules/workspaces.hpp index 71b2f6b6..c5929617 100644 --- a/include/modules/workspaces.hpp +++ b/include/modules/workspaces.hpp @@ -12,13 +12,17 @@ namespace waybar::modules { public: Workspaces(waybar::Bar &bar); auto update() -> void; - void updateThread(); operator Gtk::Widget &(); - util::SleeperThread *thread; private: + void _updateThread(); + static void _handle_idle(void *data, + struct org_kde_kwin_idle_timeout *timer); + static void _handle_resume(void *data, + struct org_kde_kwin_idle_timeout *timer); void _addWorkspace(Json::Value node); Json::Value _getWorkspaces(); Bar &_bar; + util::SleeperThread *_thread; Gtk::Box *_box; std::unordered_map _buttons; int _ipcSocketfd; diff --git a/src/bar.cpp b/src/bar.cpp index 76bac20d..9c7d1889 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -7,75 +7,16 @@ #include "factory.hpp" #include "util/chrono.hpp" -static void handleGeometry(void *data, struct wl_output *wl_output, int32_t x, - int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, - const char *make, const char *model, int32_t transform) -{ - // Nothing here -} - -static void handleMode(void *data, struct wl_output *wl_output, uint32_t f, - int32_t w, int32_t h, int32_t refresh) -{ - auto o = reinterpret_cast(data); - o->setWidth(w); -} - -static void handleDone(void *data, struct wl_output *) -{ - // Nothing here -} - -static void handleScale(void *data, struct wl_output *wl_output, - int32_t factor) -{ - // Nothing here -} - -static const struct wl_output_listener outputListener = { - .geometry = handleGeometry, - .mode = handleMode, - .done = handleDone, - .scale = handleScale, -}; - -static void layerSurfaceHandleConfigure( - void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, - uint32_t width, uint32_t height) -{ - auto o = reinterpret_cast(data); - o->window.show_all(); - zwlr_layer_surface_v1_ack_configure(surface, serial); - if (o->client.height != height) - { - height = o->client.height; - std::cout << fmt::format("New Height: {}", height) << std::endl; - zwlr_layer_surface_v1_set_size(surface, width, height); - zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0); - wl_surface_commit(o->surface); - } -} - -static void layerSurfaceHandleClosed(void *data, - struct zwlr_layer_surface_v1 *surface) -{ - auto o = reinterpret_cast(data); - zwlr_layer_surface_v1_destroy(o->layerSurface); - o->layerSurface = nullptr; - wl_surface_destroy(o->surface); - o->surface = nullptr; - o->window.close(); -} - -static const struct zwlr_layer_surface_v1_listener layerSurfaceListener = { - .configure = layerSurfaceHandleConfigure, - .closed = layerSurfaceHandleClosed, -}; - waybar::Bar::Bar(Client &client, std::unique_ptr &&p_output) : client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL}, output(std::move(p_output)) { + static const struct wl_output_listener outputListener = { + .geometry = _handleGeometry, + .mode = _handleMode, + .done = _handleDone, + .scale = _handleScale, + }; wl_output_add_listener(*output, &outputListener, this); window.set_title("waybar"); window.set_decorated(false); @@ -93,11 +34,68 @@ waybar::Bar::Bar(Client &client, std::unique_ptr &&p_output) ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT); zwlr_layer_surface_v1_set_size(layerSurface, _width, client.height); + static const struct zwlr_layer_surface_v1_listener layerSurfaceListener = { + .configure = _layerSurfaceHandleConfigure, + .closed = _layerSurfaceHandleClosed, + }; zwlr_layer_surface_v1_add_listener(layerSurface, &layerSurfaceListener, this); wl_surface_commit(surface); } +void waybar::Bar::_handleGeometry(void *data, struct wl_output *wl_output, + int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, + int32_t subpixel, const char *make, const char *model, int32_t transform) +{ + // Nothing here +} + +void waybar::Bar::_handleMode(void *data, struct wl_output *wl_output, + uint32_t f, int32_t w, int32_t h, int32_t refresh) +{ + auto o = reinterpret_cast(data); + o->setWidth(w); +} + +void waybar::Bar::_handleDone(void *data, struct wl_output *) +{ + // Nothing here +} + +void waybar::Bar::_handleScale(void *data, struct wl_output *wl_output, + int32_t factor) +{ + // Nothing here +} + +void waybar::Bar::_layerSurfaceHandleConfigure( + void *data, struct zwlr_layer_surface_v1 *surface, uint32_t serial, + uint32_t width, uint32_t height) +{ + auto o = reinterpret_cast(data); + o->window.show_all(); + zwlr_layer_surface_v1_ack_configure(surface, serial); + if (o->client.height != height) + { + height = o->client.height; + std::cout << fmt::format("New Height: {}", height) << std::endl; + zwlr_layer_surface_v1_set_size(surface, width, height); + zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0); + wl_surface_commit(o->surface); + } +} + +void waybar::Bar::_layerSurfaceHandleClosed(void *data, + struct zwlr_layer_surface_v1 *surface) +{ + auto o = reinterpret_cast(data); + zwlr_layer_surface_v1_destroy(o->layerSurface); + o->layerSurface = nullptr; + wl_surface_destroy(o->surface); + o->surface = nullptr; + o->window.close(); +} + auto waybar::Bar::setWidth(int width) -> void { if (width == this->_width) return; diff --git a/src/client.cpp b/src/client.cpp index 37bc5c69..b44e29a5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,37 +1,5 @@ #include "client.hpp" -static void handle_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version) -{ - auto o = reinterpret_cast(data); - if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) { - o->layer_shell = (zwlr_layer_shell_v1 *)wl_registry_bind(registry, name, - &zwlr_layer_shell_v1_interface, version); - } else if (!strcmp(interface, wl_output_interface.name)) { - auto output = std::make_unique(); - *output = (struct wl_output *)wl_registry_bind(registry, name, - &wl_output_interface, version); - o->bars.emplace_back(*o, std::move(output)); - } else if (!strcmp(interface, org_kde_kwin_idle_interface.name)) { - o->idle_manager = (org_kde_kwin_idle *)wl_registry_bind(registry, name, - &org_kde_kwin_idle_interface, version); - } else if (!strcmp(interface, wl_seat_interface.name)) { - o->seat = (struct wl_seat *)wl_registry_bind(registry, name, - &wl_seat_interface, version); - } -} - -static void handle_global_remove(void *data, - struct wl_registry *registry, uint32_t name) -{ - // TODO -} - -static const struct wl_registry_listener registry_listener = { - .global = handle_global, - .global_remove = handle_global_remove, -}; - waybar::Client::Client(int argc, char* argv[]) : gtk_main(argc, argv), gdk_display(Gdk::Display::get_default()), @@ -70,9 +38,40 @@ waybar::Client::Client(int argc, char* argv[]) } +void waybar::Client::_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) +{ + auto o = reinterpret_cast(data); + if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) { + o->layer_shell = (zwlr_layer_shell_v1 *)wl_registry_bind(registry, name, + &zwlr_layer_shell_v1_interface, version); + } else if (!strcmp(interface, wl_output_interface.name)) { + auto output = std::make_unique(); + *output = (struct wl_output *)wl_registry_bind(registry, name, + &wl_output_interface, version); + o->bars.emplace_back(*o, std::move(output)); + } else if (!strcmp(interface, org_kde_kwin_idle_interface.name)) { + o->idle_manager = (org_kde_kwin_idle *)wl_registry_bind(registry, name, + &org_kde_kwin_idle_interface, version); + } else if (!strcmp(interface, wl_seat_interface.name)) { + o->seat = (struct wl_seat *)wl_registry_bind(registry, name, + &wl_seat_interface, version); + } +} + +void waybar::Client::_handle_global_remove(void *data, + struct wl_registry *registry, uint32_t name) +{ + // TODO +} + void waybar::Client::bind_interfaces() { registry = wl_display_get_registry(wlDisplay); + static const struct wl_registry_listener registry_listener = { + .global = _handle_global, + .global_remove = _handle_global_remove, + }; wl_registry_add_listener(registry, ®istry_listener, this); wl_display_roundtrip(wlDisplay); } diff --git a/src/modules/workspaces.cpp b/src/modules/workspaces.cpp index 5ba7e24c..835689ae 100644 --- a/src/modules/workspaces.cpp +++ b/src/modules/workspaces.cpp @@ -1,28 +1,8 @@ #include "modules/workspaces.hpp" #include "ipc/client.hpp" -static void handle_idle(void *data, struct org_kde_kwin_idle_timeout *timer) { - auto o = reinterpret_cast(data); - if (o->thread) { - delete o->thread; - o->thread = nullptr; - } -} - -static void handle_resume(void *data, struct org_kde_kwin_idle_timeout *timer) { - auto o = reinterpret_cast(data); - if (!o->thread) { - o->updateThread(); - } -} - -static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = { - .idle = handle_idle, - .resumed = handle_resume, -}; - waybar::modules::Workspaces::Workspaces(Bar &bar) - : thread(nullptr), _bar(bar), _box(Gtk::manage(new Gtk::Box)) + : _bar(bar), _thread(nullptr), _box(Gtk::manage(new Gtk::Box)) { _box->get_style_context()->add_class("workspaces"); std::string socketPath = get_socketpath(); @@ -34,17 +14,13 @@ waybar::modules::Workspaces::Workspaces(Bar &bar) _idle_timer = org_kde_kwin_idle_get_idle_timeout(_bar.client.idle_manager, _bar.client.seat, 10000); // 10 seconds + static const struct org_kde_kwin_idle_timeout_listener idle_timer_listener = { + .idle = _handle_idle, + .resumed = _handle_resume, + }; org_kde_kwin_idle_timeout_add_listener(_idle_timer, &idle_timer_listener, this); - updateThread(); -} - -void waybar::modules::Workspaces::updateThread() -{ - thread = new waybar::util::SleeperThread([this] { - update(); - thread->sleep_for(waybar::chrono::milliseconds(250)); - }); + _updateThread(); } auto waybar::modules::Workspaces::update() -> void @@ -79,6 +55,33 @@ auto waybar::modules::Workspaces::update() -> void } } +void waybar::modules::Workspaces::_updateThread() +{ + _thread = new waybar::util::SleeperThread([this] { + update(); + _thread->sleep_for(waybar::chrono::milliseconds(250)); + }); +} + +void waybar::modules::Workspaces::_handle_idle(void *data, + struct org_kde_kwin_idle_timeout *timer) { + auto o = reinterpret_cast(data); + if (o->_thread) { + delete o->_thread; + o->_thread = nullptr; + std::cout << "IDLE" << std::endl; + } +} + +void waybar::modules::Workspaces::_handle_resume(void *data, + struct org_kde_kwin_idle_timeout *timer) { + auto o = reinterpret_cast(data); + if (!o->_thread) { + o->_updateThread(); + std::cout << "RESUME" << std::endl; + } +} + void waybar::modules::Workspaces::_addWorkspace(Json::Value node) { auto pair = _buttons.emplace(node["num"].asInt(), node["name"].asString());