Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 91e2aba

Browse files
committedFeb 7, 2025
modules/dwl: split layout in it's own module
1 parent c32d5e3 commit 91e2aba

File tree

8 files changed

+227
-16
lines changed

8 files changed

+227
-16
lines changed
 

‎include/modules/dwl/layout.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <fmt/format.h>
4+
5+
#include <string>
6+
7+
#include "AAppIconLabel.hpp"
8+
#include "bar.hpp"
9+
#include "dwl-ipc-unstable-v2-client-protocol.h"
10+
#include "util/json.hpp"
11+
12+
namespace waybar::modules::dwl {
13+
14+
class Layout : public ALabel, public sigc::trackable {
15+
public:
16+
Layout(const std::string &, const waybar::Bar &, const Json::Value &);
17+
~Layout();
18+
19+
void handle_layout(const uint32_t layout);
20+
void handle_layout_symbol(const char *layout_symbol);
21+
void handle_frame();
22+
23+
struct zdwl_ipc_manager_v2 *status_manager_;
24+
25+
private:
26+
const Bar &bar_;
27+
28+
std::string layout_symbol_;
29+
uint32_t layout_;
30+
31+
struct zdwl_ipc_output_v2 *output_status_;
32+
};
33+
34+
} // namespace waybar::modules::dwl

‎include/modules/dwl/window.hpp

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class Window : public AAppIconLabel, public sigc::trackable {
1616
Window(const std::string &, const waybar::Bar &, const Json::Value &);
1717
~Window();
1818

19-
void handle_layout(const uint32_t layout);
2019
void handle_title(const char *title);
2120
void handle_appid(const char *ppid);
22-
void handle_layout_symbol(const char *layout_symbol);
2321
void handle_frame();
2422

2523
struct zdwl_ipc_manager_v2 *status_manager_;
@@ -29,8 +27,6 @@ class Window : public AAppIconLabel, public sigc::trackable {
2927

3028
std::string title_;
3129
std::string appid_;
32-
std::string layout_symbol_;
33-
uint32_t layout_;
3430

3531
struct zdwl_ipc_output_v2 *output_status_;
3632
};

‎man/waybar-dwl-layout.5.scd

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
waybar-dwl-layout(5)
2+
3+
# NAME
4+
5+
waybar - dwl layout module
6+
7+
# DESCRIPTION
8+
9+
The *layout* module displays the layout of the currently focused workspace in DWL
10+
11+
# CONFIGURATION
12+
13+
Addressed by *dwl/layout*
14+
15+
*format*: ++
16+
typeof: string ++
17+
default: {layout} ++
18+
The format, how information should be displayed.
19+
20+
*rotate*: ++
21+
typeof: integer ++
22+
Positive value to rotate the text label (in 90 degree increments).
23+
24+
*max-length*: ++
25+
typeof: integer ++
26+
The maximum length in character the module should display.
27+
28+
*min-length*: ++
29+
typeof: integer ++
30+
The minimum length in characters the module should accept.
31+
32+
*align*: ++
33+
typeof: float ++
34+
The alignment of the label within the module, where 0 is left-aligned and 1 is right-aligned. If the module is rotated, it will follow the flow of the text.
35+
36+
*justify*: ++
37+
typeof: string ++
38+
The alignment of the text within the module's label, allowing options 'left', 'right', or 'center' to define the positioning.
39+
40+
*on-click*: ++
41+
typeof: string ++
42+
Command to execute when clicked on the module.
43+
44+
*on-click-middle*: ++
45+
typeof: string ++
46+
Command to execute when middle-clicked on the module using mousewheel.
47+
48+
*on-click-right*: ++
49+
typeof: string ++
50+
Command to execute when you right-click on the module.
51+
52+
*on-update*: ++
53+
typeof: string ++
54+
Command to execute when the module is updated.
55+
56+
*on-scroll-up*: ++
57+
typeof: string ++
58+
Command to execute when scrolling up on the module.
59+
60+
*on-scroll-down*: ++
61+
typeof: string ++
62+
Command to execute when scrolling down on the module.
63+
64+
*smooth-scrolling-threshold*: ++
65+
typeof: double ++
66+
Threshold to be used when scrolling.
67+
68+
# FORMAT REPLACEMENTS
69+
70+
*{layout}*: The layout of the focused window.
71+
72+
# EXAMPLES
73+
74+
```
75+
"dwl/layout": {
76+
"format": "{layout}",
77+
"max-length": 50,
78+
}
79+
```

‎man/waybar-dwl-window.5.scd

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ Addressed by *dwl/window*
9595

9696
*{app_id}*: The app_id of the focused window.
9797

98-
*{layout}*: The layout of the focused window.
99-
10098
# REWRITE RULES
10199

102100
*rewrite* is an object where keys are regular expressions and values are

‎meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ if true
295295
add_project_arguments('-DHAVE_DWL', language: 'cpp')
296296
src_files += files('src/modules/dwl/tags.cpp')
297297
src_files += files('src/modules/dwl/window.cpp')
298+
src_files += files('src/modules/dwl/layout.cpp')
298299
man_files += files('man/waybar-dwl-tags.5.scd')
299300
man_files += files('man/waybar-dwl-window.5.scd')
300301
endif

‎src/factory.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifdef HAVE_DWL
3030
#include "modules/dwl/tags.hpp"
3131
#include "modules/dwl/window.hpp"
32+
#include "modules/dwl/layout.hpp"
3233
#endif
3334
#ifdef HAVE_HYPRLAND
3435
#include "modules/hyprland/language.hpp"
@@ -196,6 +197,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
196197
if (ref == "dwl/window") {
197198
return new waybar::modules::dwl::Window(id, bar_, config_[name]);
198199
}
200+
if (ref == "dwl/layout") {
201+
return new waybar::modules::dwl::Layout(id, bar_, config_[name]);
202+
}
199203
#endif
200204
#ifdef HAVE_HYPRLAND
201205
if (ref == "hyprland/window") {

‎src/modules/dwl/layout.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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, &registry_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

‎src/modules/dwl/window.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static void set_tag(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, uint32_t tag
2828
}
2929

3030
static void set_layout_symbol(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *layout) {
31-
static_cast<Window *>(data)->handle_layout_symbol(layout);
31+
// Intentionally empty
3232
}
3333

3434
static void title(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *title) {
@@ -40,7 +40,7 @@ static void dwl_frame(void *data, zdwl_ipc_output_v2 *zdwl_output_v2) {
4040
}
4141

4242
static void set_layout(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, uint32_t layout) {
43-
static_cast<Window *>(data)->handle_layout(layout);
43+
// Intentionally empty
4444
}
4545

4646
static void appid(void *data, zdwl_ipc_output_v2 *zdwl_output_v2, const char *appid) {
@@ -102,16 +102,9 @@ void Window::handle_title(const char *title) { title_ = Glib::Markup::escape_tex
102102

103103
void Window::handle_appid(const char *appid) { appid_ = Glib::Markup::escape_text(appid); }
104104

105-
void Window::handle_layout_symbol(const char *layout_symbol) {
106-
layout_symbol_ = Glib::Markup::escape_text(layout_symbol);
107-
}
108-
109-
void Window::handle_layout(const uint32_t layout) { layout_ = layout; }
110-
111105
void Window::handle_frame() {
112106
label_.set_markup(waybar::util::rewriteString(
113-
fmt::format(fmt::runtime(format_), fmt::arg("title", title_),
114-
fmt::arg("layout", layout_symbol_), fmt::arg("app_id", appid_)),
107+
fmt::format(fmt::runtime(format_), fmt::arg("title", title_), fmt::arg("app_id", appid_)),
115108
config_["rewrite"]));
116109
updateAppIconName(appid_, "");
117110
updateAppIcon();

0 commit comments

Comments
 (0)
Please sign in to comment.