Skip to content

Commit 7f73a8c

Browse files
authored
Merge pull request #441 from alebastr/gtk-layer-shell
Use gtk-layer-shell library for correct positioning of popups
2 parents 2277ddd + dde700f commit 7f73a8c

File tree

7 files changed

+162
-108
lines changed

7 files changed

+162
-108
lines changed

include/bar.hpp

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <gdkmm/monitor.h>
34
#include <glibmm/refptr.h>
45
#include <gtkmm/box.h>
56
#include <gtkmm/cssprovider.h>
@@ -15,10 +16,11 @@ namespace waybar {
1516

1617
class Factory;
1718
struct waybar_output {
18-
struct wl_output * output = nullptr;
19-
std::string name;
20-
uint32_t wl_name;
21-
struct zxdg_output_v1 *xdg_output = nullptr;
19+
Glib::RefPtr<Gdk::Monitor> monitor;
20+
std::string name;
21+
22+
std::unique_ptr<struct zxdg_output_v1, decltype(&zxdg_output_v1_destroy)> xdg_output = {
23+
nullptr, &zxdg_output_v1_destroy};
2224
};
2325

2426
class Bar {
@@ -30,13 +32,12 @@ class Bar {
3032
auto toggle() -> void;
3133
void handleSignal(int);
3234

33-
struct waybar_output * output;
34-
Json::Value config;
35-
Gtk::Window window;
36-
struct wl_surface * surface;
37-
struct zwlr_layer_surface_v1 *layer_surface;
38-
bool visible = true;
39-
bool vertical = false;
35+
struct waybar_output *output;
36+
Json::Value config;
37+
Gtk::Window window;
38+
struct wl_surface * surface;
39+
bool visible = true;
40+
bool vertical = false;
4041

4142
private:
4243
static constexpr const char *MIN_HEIGHT_MSG =
@@ -51,7 +52,9 @@ class Bar {
5152
uint32_t, uint32_t);
5253
static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *);
5354

54-
void destroyOutput();
55+
#ifdef HAVE_GTK_LAYER_SHELL
56+
void initGtkLayerShell();
57+
#endif
5558
void onConfigure(GdkEventConfigure *ev);
5659
void onRealize();
5760
void onMap(GdkEventAny *ev);
@@ -68,6 +71,9 @@ class Bar {
6871
int bottom = 0;
6972
int left = 0;
7073
} margins_;
74+
struct zwlr_layer_surface_v1 *layer_surface_;
75+
// use gtk-layer-shell instead of handling layer surfaces directly
76+
bool use_gls_ = false;
7177
uint32_t width_ = 0;
7278
uint32_t height_ = 1;
7379
uint8_t anchor_;

include/client.hpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@ class Client {
3434
bool isValidOutput(const Json::Value &config, std::unique_ptr<struct waybar_output> &output);
3535
auto setupConfig(const std::string &config_file) -> void;
3636
auto setupCss(const std::string &css_file) -> void;
37-
std::unique_ptr<struct waybar_output> &getOutput(uint32_t wl_name);
37+
std::unique_ptr<struct waybar_output> &getOutput(void *);
3838
std::vector<Json::Value> getOutputConfigs(std::unique_ptr<struct waybar_output> &output);
3939

4040
static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name,
4141
const char *interface, uint32_t version);
4242
static void handleGlobalRemove(void *data, struct wl_registry *registry, uint32_t name);
43-
static void handleLogicalPosition(void *, struct zxdg_output_v1 *, int32_t, int32_t);
44-
static void handleLogicalSize(void *, struct zxdg_output_v1 *, int32_t, int32_t);
45-
static void handleDone(void *, struct zxdg_output_v1 *);
46-
static void handleName(void *, struct zxdg_output_v1 *, const char *);
47-
static void handleDescription(void *, struct zxdg_output_v1 *, const char *);
43+
static void handleOutputName(void *, struct zxdg_output_v1 *, const char *);
44+
void handleMonitorAdded(Glib::RefPtr<Gdk::Monitor> monitor);
45+
void handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor);
4846

4947
Json::Value config_;
5048
Glib::RefPtr<Gtk::StyleContext> style_context_;

meson.build

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ spdlog = dependency('spdlog', version : ['>=1.3.1'], fallback : ['spdlog', 'spdl
5252
wayland_client = dependency('wayland-client')
5353
wayland_cursor = dependency('wayland-cursor')
5454
wayland_protos = dependency('wayland-protocols')
55-
gtkmm = dependency('gtkmm-3.0')
55+
gtkmm = dependency('gtkmm-3.0', version : ['>=3.22.0'])
5656
dbusmenu_gtk = dependency('dbusmenu-gtk3-0.4', required: get_option('dbusmenu-gtk'))
5757
giounix = dependency('gio-unix-2.0', required: get_option('dbusmenu-gtk'))
5858
jsoncpp = dependency('jsoncpp')
@@ -62,6 +62,9 @@ libnlgen = dependency('libnl-genl-3.0', required: get_option('libnl'))
6262
libpulse = dependency('libpulse', required: get_option('pulseaudio'))
6363
libudev = dependency('libudev', required: get_option('libudev'))
6464
libmpdclient = dependency('libmpdclient', required: get_option('mpd'))
65+
gtk_layer_shell = dependency('gtk-layer-shell-0',
66+
required: get_option('gtk-layer-shell'),
67+
fallback : ['gtk-layer-shell', 'gtk_layer_shell_dep'])
6568
systemd = dependency('systemd', required: get_option('systemd'))
6669

6770
prefix = get_option('prefix')
@@ -136,6 +139,10 @@ if libmpdclient.found()
136139
src_files += 'src/modules/mpd.cpp'
137140
endif
138141

142+
if gtk_layer_shell.found()
143+
add_project_arguments('-DHAVE_GTK_LAYER_SHELL', language: 'cpp')
144+
endif
145+
139146
subdir('protocol')
140147

141148
executable(
@@ -158,7 +165,8 @@ executable(
158165
libnlgen,
159166
libpulse,
160167
libudev,
161-
libmpdclient
168+
libmpdclient,
169+
gtk_layer_shell
162170
],
163171
include_directories: [include_directories('include')],
164172
install: true,

meson_options.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ option('dbusmenu-gtk', type: 'feature', value: 'auto', description: 'Enable supp
77
option('man-pages', type: 'feature', value: 'auto', description: 'Generate and install man pages')
88
option('mpd', type: 'feature', value: 'auto', description: 'Enable support for the Music Player Daemon')
99
option('out', type: 'string', value : '/', description: 'output prefix directory')
10+
option('gtk-layer-shell', type: 'feature', value: 'disabled', description: 'Use gtk-layer-shell library for popups support')

src/bar.cpp

+74-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#ifdef HAVE_GTK_LAYER_SHELL
2+
#include <gtk-layer-shell.h>
3+
#endif
4+
15
#include "bar.hpp"
26
#include "client.hpp"
37
#include "factory.hpp"
@@ -8,7 +12,7 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
812
config(w_config),
913
window{Gtk::WindowType::WINDOW_TOPLEVEL},
1014
surface(nullptr),
11-
layer_surface(nullptr),
15+
layer_surface_(nullptr),
1216
anchor_(ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP),
1317
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
1418
center_(Gtk::ORIENTATION_HORIZONTAL, 0),
@@ -28,11 +32,6 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
2832
height_ = config["height"].isUInt() ? config["height"].asUInt() : height_;
2933
width_ = config["width"].isUInt() ? config["width"].asUInt() : width_;
3034

31-
window.signal_realize().connect_notify(sigc::mem_fun(*this, &Bar::onRealize));
32-
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
33-
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Bar::onConfigure));
34-
window.set_size_request(width_, height_);
35-
3635
if (config["position"] == "bottom") {
3736
anchor_ = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
3837
} else if (config["position"] == "left") {
@@ -98,6 +97,17 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
9897
margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps};
9998
}
10099

100+
#ifdef HAVE_GTK_LAYER_SHELL
101+
use_gls_ = config["gtk-layer-shell"].isBool() ? config["gtk-layer-shell"].asBool() : true;
102+
if (use_gls_) {
103+
initGtkLayerShell();
104+
}
105+
#endif
106+
107+
window.signal_realize().connect_notify(sigc::mem_fun(*this, &Bar::onRealize));
108+
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
109+
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Bar::onConfigure));
110+
window.set_size_request(width_, height_);
101111
setupWidgets();
102112

103113
if (window.get_realized()) {
@@ -131,11 +141,43 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
131141
tmp_width = ev->width;
132142
}
133143
}
134-
if (tmp_width != width_ || tmp_height != height_) {
144+
if (use_gls_) {
145+
width_ = tmp_width;
146+
height_ = tmp_height;
147+
spdlog::debug("Set surface size {}x{} for output {}", width_, height_, output->name);
148+
setExclusiveZone(tmp_width, tmp_height);
149+
} else if (tmp_width != width_ || tmp_height != height_) {
135150
setSurfaceSize(tmp_width, tmp_height);
136151
}
137152
}
138153

154+
#ifdef HAVE_GTK_LAYER_SHELL
155+
void waybar::Bar::initGtkLayerShell() {
156+
auto gtk_window = window.gobj();
157+
// this has to be executed before GtkWindow.realize
158+
gtk_layer_init_for_window(gtk_window);
159+
gtk_layer_set_keyboard_interactivity(gtk_window, FALSE);
160+
auto layer = config["layer"] == "top" ? GTK_LAYER_SHELL_LAYER_TOP : GTK_LAYER_SHELL_LAYER_BOTTOM;
161+
gtk_layer_set_layer(gtk_window, layer);
162+
gtk_layer_set_monitor(gtk_window, output->monitor->gobj());
163+
gtk_layer_set_namespace(gtk_window, "waybar");
164+
165+
gtk_layer_set_anchor(
166+
gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
167+
gtk_layer_set_anchor(
168+
gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
169+
gtk_layer_set_anchor(
170+
gtk_window, GTK_LAYER_SHELL_EDGE_TOP, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP);
171+
gtk_layer_set_anchor(
172+
gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
173+
174+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, margins_.left);
175+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, margins_.right);
176+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_TOP, margins_.top);
177+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, margins_.bottom);
178+
}
179+
#endif
180+
139181
void waybar::Bar::onRealize() {
140182
auto gdk_window = window.get_window()->gobj();
141183
gdk_wayland_window_set_use_custom_surface(gdk_window);
@@ -145,24 +187,30 @@ void waybar::Bar::onMap(GdkEventAny* ev) {
145187
auto gdk_window = window.get_window()->gobj();
146188
surface = gdk_wayland_window_get_wl_surface(gdk_window);
147189

190+
if (use_gls_) {
191+
return;
192+
}
193+
148194
auto client = waybar::Client::inst();
195+
// owned by output->monitor; no need to destroy
196+
auto wl_output = gdk_wayland_monitor_get_wl_output(output->monitor->gobj());
149197
auto layer =
150198
config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
151-
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
152-
client->layer_shell, surface, output->output, layer, "waybar");
199+
layer_surface_ = zwlr_layer_shell_v1_get_layer_surface(
200+
client->layer_shell, surface, wl_output, layer, "waybar");
153201

154-
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
155-
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_);
202+
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface_, false);
203+
zwlr_layer_surface_v1_set_anchor(layer_surface_, anchor_);
156204
zwlr_layer_surface_v1_set_margin(
157-
layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left);
205+
layer_surface_, margins_.top, margins_.right, margins_.bottom, margins_.left);
158206
setSurfaceSize(width_, height_);
159207
setExclusiveZone(width_, height_);
160208

161209
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
162210
.configure = layerSurfaceHandleConfigure,
163211
.closed = layerSurfaceHandleClosed,
164212
};
165-
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
213+
zwlr_layer_surface_v1_add_listener(layer_surface_, &layer_surface_listener, this);
166214

167215
wl_surface_commit(surface);
168216
wl_display_roundtrip(client->wl_display);
@@ -182,7 +230,15 @@ void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) {
182230
}
183231
}
184232
spdlog::debug("Set exclusive zone {} for output {}", zone, output->name);
185-
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
233+
234+
#ifdef HAVE_GTK_LAYER_SHELL
235+
if (use_gls_) {
236+
gtk_layer_set_exclusive_zone(window.gobj(), zone);
237+
} else
238+
#endif
239+
{
240+
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface_, zone);
241+
}
186242
}
187243

188244
void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) {
@@ -198,7 +254,7 @@ void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) {
198254
width += margins_.right + margins_.left;
199255
}
200256
spdlog::debug("Set surface size {}x{} for output {}", width, height, output->name);
201-
zwlr_layer_surface_v1_set_size(layer_surface, width, height);
257+
zwlr_layer_surface_v1_set_size(layer_surface_, width, height);
202258
}
203259

204260
// Converting string to button code rn as to avoid doing it later
@@ -282,9 +338,9 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf
282338

283339
void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) {
284340
auto o = static_cast<waybar::Bar*>(data);
285-
if (o->layer_surface) {
286-
zwlr_layer_surface_v1_destroy(o->layer_surface);
287-
o->layer_surface = nullptr;
341+
if (o->layer_surface_) {
342+
zwlr_layer_surface_v1_destroy(o->layer_surface_);
343+
o->layer_surface_ = nullptr;
288344
}
289345
o->modules_left_.clear();
290346
o->modules_center_.clear();

0 commit comments

Comments
 (0)