|
| 1 | +#include "modules/dwl/layout.hpp" |
| 2 | + |
| 3 | +#include <spdlog/spdlog.h> |
| 4 | + |
| 5 | +#include "client.hpp" |
| 6 | +#include "dwl-ipc-unstable-v2-client-protocol.h" |
| 7 | +#include "glibmm/markup.h" |
| 8 | +#include "util/rewrite_string.hpp" |
| 9 | + |
| 10 | +namespace waybar::modules::dwl { |
| 11 | + |
| 12 | +static void toggle_visibility(void *data, zdwl_ipc_output_v2 *zdwl_output_v2) { |
| 13 | + // Intentionally empty |
| 14 | +} |
| 15 | + |
| 16 | +static void active(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, uint32_t active) { |
| 17 | + // Intentionally empty |
| 18 | +} |
| 19 | + |
| 20 | +static void set_tag(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, uint32_t tag, uint32_t state, |
| 21 | + uint32_t clients, uint32_t focused) { |
| 22 | + // Intentionally empty |
| 23 | +} |
| 24 | + |
| 25 | +static void set_layout_symbol(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *layout) { |
| 26 | + static_cast<Layout *>(data)->handle_layout_symbol(layout); |
| 27 | +} |
| 28 | + |
| 29 | +static void title(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *title) { |
| 30 | + // Intentionally empty |
| 31 | +} |
| 32 | + |
| 33 | +static void dwl_frame(void *data, zdwl_ipc_output_v2 *zdwl_output_v2) { |
| 34 | + static_cast<Layout *>(data)->handle_frame(); |
| 35 | +} |
| 36 | + |
| 37 | +static void set_layout(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, uint32_t layout) { |
| 38 | + static_cast<Layout *>(data)->handle_layout(layout); |
| 39 | +} |
| 40 | + |
| 41 | +static void appid(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *appid) { |
| 42 | + // Intentionally empty |
| 43 | +}; |
| 44 | + |
| 45 | +static const zdwl_ipc_output_v2_listener output_status_listener_impl{ |
| 46 | + .toggle_visibility = toggle_visibility, |
| 47 | + .active = active, |
| 48 | + .tag = set_tag, |
| 49 | + .layout = set_layout, |
| 50 | + .title = title, |
| 51 | + .appid = appid, |
| 52 | + .layout_symbol = set_layout_symbol, |
| 53 | + .frame = dwl_frame, |
| 54 | +}; |
| 55 | + |
| 56 | +static void handle_global(void *data, struct wl_registry *registry, uint32_t name, |
| 57 | + const char *interface, uint32_t version) { |
| 58 | + if (std::strcmp(interface, zdwl_ipc_manager_v2_interface.name) == 0) { |
| 59 | + static_cast<Layout *>(data)->status_manager_ = static_cast<struct zdwl_ipc_manager_v2 *>( |
| 60 | + (zdwl_ipc_manager_v2 *)wl_registry_bind(registry, name, &zdwl_ipc_manager_v2_interface, 1)); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { |
| 65 | + /* Ignore event */ |
| 66 | +} |
| 67 | + |
| 68 | +static const wl_registry_listener registry_listener_impl = {.global = handle_global, |
| 69 | + .global_remove = handle_global_remove}; |
| 70 | + |
| 71 | +Layout::Layout(const std::string &id, const Bar &bar, const Json::Value &config) |
| 72 | + : waybar::ALabel(config, "layout", id, "{layout}", false, false, false, false), bar_(bar) { |
| 73 | + struct wl_display *display = Client::inst()->wl_display; |
| 74 | + struct wl_registry *registry = wl_display_get_registry(display); |
| 75 | + |
| 76 | + wl_registry_add_listener(registry, ®istry_listener_impl, this); |
| 77 | + wl_display_roundtrip(display); |
| 78 | + |
| 79 | + if (status_manager_ == nullptr) { |
| 80 | + spdlog::error("dwl_status_manager_v2 not advertised"); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + struct wl_output *output = gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj()); |
| 85 | + output_status_ = zdwl_ipc_manager_v2_get_output(status_manager_, output); |
| 86 | + zdwl_ipc_output_v2_add_listener(output_status_, &output_status_listener_impl, this); |
| 87 | + zdwl_ipc_manager_v2_destroy(status_manager_); |
| 88 | +} |
| 89 | + |
| 90 | +Layout::~Layout() { |
| 91 | + if (output_status_ != nullptr) { |
| 92 | + zdwl_ipc_output_v2_destroy(output_status_); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void Layout::handle_layout_symbol(const char *layout_symbol) { |
| 97 | + layout_symbol_ = Glib::Markup::escape_text(layout_symbol); |
| 98 | +} |
| 99 | + |
| 100 | +void Layout::handle_layout(const uint32_t layout) { layout_ = layout; } |
| 101 | + |
| 102 | +void Layout::handle_frame() { |
| 103 | + label_.set_markup(fmt::format(fmt::runtime(format_), fmt::arg("layout", layout_symbol_))); |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace waybar::modules::dwl |
0 commit comments