2019-12-28 01:31:18 +01:00
|
|
|
#ifdef HAVE_GTK_LAYER_SHELL
|
|
|
|
#include <gtk-layer-shell.h>
|
|
|
|
#endif
|
|
|
|
|
2020-10-21 08:18:58 +02:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
2018-08-08 23:54:58 +02:00
|
|
|
#include "bar.hpp"
|
|
|
|
#include "client.hpp"
|
2018-08-09 12:05:48 +02:00
|
|
|
#include "factory.hpp"
|
2020-10-22 07:45:03 +02:00
|
|
|
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
namespace waybar {
|
|
|
|
static constexpr const char* MIN_HEIGHT_MSG =
|
2021-10-28 18:37:11 +02:00
|
|
|
"Requested height: {} is less than the minimum height: {} required by the modules";
|
2020-10-22 07:16:12 +02:00
|
|
|
|
|
|
|
static constexpr const char* MIN_WIDTH_MSG =
|
2021-10-28 18:37:11 +02:00
|
|
|
"Requested width: {} is less than the minimum width: {} required by the modules";
|
2020-10-22 07:16:12 +02:00
|
|
|
|
|
|
|
static constexpr const char* BAR_SIZE_MSG = "Bar configured (width: {}, height: {}) for output: {}";
|
|
|
|
|
|
|
|
static constexpr const char* SIZE_DEFINED =
|
|
|
|
"{} size is defined in the config file so it will stay like that";
|
|
|
|
|
|
|
|
#ifdef HAVE_GTK_LAYER_SHELL
|
|
|
|
struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
|
|
|
|
GLSSurfaceImpl(Gtk::Window& window, struct waybar_output& output) : window_{window} {
|
|
|
|
output_name_ = output.name;
|
|
|
|
// this has to be executed before GtkWindow.realize
|
|
|
|
gtk_layer_init_for_window(window_.gobj());
|
|
|
|
gtk_layer_set_keyboard_interactivity(window.gobj(), FALSE);
|
|
|
|
gtk_layer_set_monitor(window_.gobj(), output.monitor->gobj());
|
|
|
|
gtk_layer_set_namespace(window_.gobj(), "waybar");
|
|
|
|
|
2021-01-17 10:05:18 +01:00
|
|
|
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &GLSSurfaceImpl::onMap));
|
2020-10-22 07:16:12 +02:00
|
|
|
window.signal_configure_event().connect_notify(
|
2020-10-28 16:06:40 +01:00
|
|
|
sigc::mem_fun(*this, &GLSSurfaceImpl::onConfigure));
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setExclusiveZone(bool enable) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
if (enable) {
|
|
|
|
gtk_layer_auto_exclusive_zone_enable(window_.gobj());
|
|
|
|
} else {
|
|
|
|
gtk_layer_set_exclusive_zone(window_.gobj(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setMargins(const struct bar_margins& margins) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
gtk_layer_set_margin(window_.gobj(), GTK_LAYER_SHELL_EDGE_LEFT, margins.left);
|
|
|
|
gtk_layer_set_margin(window_.gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, margins.right);
|
|
|
|
gtk_layer_set_margin(window_.gobj(), GTK_LAYER_SHELL_EDGE_TOP, margins.top);
|
|
|
|
gtk_layer_set_margin(window_.gobj(), GTK_LAYER_SHELL_EDGE_BOTTOM, margins.bottom);
|
|
|
|
}
|
|
|
|
|
2020-10-23 08:04:58 +02:00
|
|
|
void setLayer(bar_layer value) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
auto layer = GTK_LAYER_SHELL_LAYER_BOTTOM;
|
2020-10-23 08:04:58 +02:00
|
|
|
if (value == bar_layer::TOP) {
|
2020-10-22 07:16:12 +02:00
|
|
|
layer = GTK_LAYER_SHELL_LAYER_TOP;
|
2020-10-23 08:04:58 +02:00
|
|
|
} else if (value == bar_layer::OVERLAY) {
|
2020-10-22 07:16:12 +02:00
|
|
|
layer = GTK_LAYER_SHELL_LAYER_OVERLAY;
|
|
|
|
}
|
|
|
|
gtk_layer_set_layer(window_.gobj(), layer);
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:05:18 +01:00
|
|
|
void setPassThrough(bool enable) override {
|
|
|
|
passthrough_ = enable;
|
|
|
|
auto gdk_window = window_.get_window();
|
|
|
|
if (gdk_window) {
|
|
|
|
Cairo::RefPtr<Cairo::Region> region;
|
|
|
|
if (enable) {
|
|
|
|
region = Cairo::Region::create();
|
|
|
|
}
|
|
|
|
gdk_window->input_shape_combine_region(region, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setPosition(const std::string_view& position) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
auto unanchored = GTK_LAYER_SHELL_EDGE_BOTTOM;
|
|
|
|
vertical_ = false;
|
|
|
|
if (position == "bottom") {
|
|
|
|
unanchored = GTK_LAYER_SHELL_EDGE_TOP;
|
|
|
|
} else if (position == "left") {
|
|
|
|
unanchored = GTK_LAYER_SHELL_EDGE_RIGHT;
|
|
|
|
vertical_ = true;
|
|
|
|
} else if (position == "right") {
|
|
|
|
vertical_ = true;
|
|
|
|
unanchored = GTK_LAYER_SHELL_EDGE_LEFT;
|
|
|
|
}
|
|
|
|
for (auto edge : {GTK_LAYER_SHELL_EDGE_LEFT,
|
|
|
|
GTK_LAYER_SHELL_EDGE_RIGHT,
|
|
|
|
GTK_LAYER_SHELL_EDGE_TOP,
|
|
|
|
GTK_LAYER_SHELL_EDGE_BOTTOM}) {
|
|
|
|
gtk_layer_set_anchor(window_.gobj(), edge, unanchored != edge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setSize(uint32_t width, uint32_t height) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
width_ = width;
|
|
|
|
height_ = height;
|
|
|
|
window_.set_size_request(width_, height_);
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
Gtk::Window& window_;
|
|
|
|
std::string output_name_;
|
|
|
|
uint32_t width_;
|
|
|
|
uint32_t height_;
|
2021-01-17 10:05:18 +01:00
|
|
|
bool passthrough_ = false;
|
2020-10-22 07:16:12 +02:00
|
|
|
bool vertical_ = false;
|
|
|
|
|
2021-01-17 10:05:18 +01:00
|
|
|
void onMap(GdkEventAny* ev) { setPassThrough(passthrough_); }
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void onConfigure(GdkEventConfigure* ev) {
|
2020-10-22 07:16:12 +02:00
|
|
|
/*
|
|
|
|
* GTK wants new size for the window.
|
|
|
|
* Actual resizing and management of the exclusve zone is handled within the gtk-layer-shell
|
|
|
|
* code. This event handler only updates stored size of the window and prints some warnings.
|
|
|
|
*
|
|
|
|
* Note: forced resizing to a window smaller than required by GTK would not work with
|
|
|
|
* gtk-layer-shell.
|
|
|
|
*/
|
|
|
|
if (vertical_) {
|
|
|
|
if (width_ > 1 && ev->width > static_cast<int>(width_)) {
|
|
|
|
spdlog::warn(MIN_WIDTH_MSG, width_, ev->width);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (height_ > 1 && ev->height > static_cast<int>(height_)) {
|
|
|
|
spdlog::warn(MIN_HEIGHT_MSG, height_, ev->height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
width_ = ev->width;
|
|
|
|
height_ = ev->height;
|
|
|
|
spdlog::info(BAR_SIZE_MSG, width_, height_, output_name_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct RawSurfaceImpl : public BarSurface, public sigc::trackable {
|
|
|
|
RawSurfaceImpl(Gtk::Window& window, struct waybar_output& output) : window_{window} {
|
|
|
|
output_ = gdk_wayland_monitor_get_wl_output(output.monitor->gobj());
|
|
|
|
output_name_ = output.name;
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
window.signal_realize().connect_notify(sigc::mem_fun(*this, &RawSurfaceImpl::onRealize));
|
|
|
|
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &RawSurfaceImpl::onMap));
|
2020-10-22 07:16:12 +02:00
|
|
|
window.signal_configure_event().connect_notify(
|
2020-10-28 16:06:40 +01:00
|
|
|
sigc::mem_fun(*this, &RawSurfaceImpl::onConfigure));
|
2020-10-22 07:16:12 +02:00
|
|
|
|
|
|
|
if (window.get_realized()) {
|
2020-10-28 16:06:40 +01:00
|
|
|
onRealize();
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setExclusiveZone(bool enable) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
exclusive_zone_ = enable;
|
|
|
|
if (layer_surface_) {
|
|
|
|
auto zone = 0;
|
|
|
|
if (enable) {
|
|
|
|
// exclusive zone already includes margin for anchored edge,
|
|
|
|
// only opposite margin should be added
|
|
|
|
if ((anchor_ & VERTICAL_ANCHOR) == VERTICAL_ANCHOR) {
|
|
|
|
zone += width_;
|
|
|
|
zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) ? margins_.right : margins_.left;
|
|
|
|
} else {
|
|
|
|
zone += height_;
|
|
|
|
zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) ? margins_.bottom : margins_.top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spdlog::debug("Set exclusive zone {} for output {}", zone, output_name_);
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface_.get(), zone);
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 08:04:58 +02:00
|
|
|
void setLayer(bar_layer layer) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
layer_ = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
|
2020-10-23 08:04:58 +02:00
|
|
|
if (layer == bar_layer::TOP) {
|
2020-10-22 07:16:12 +02:00
|
|
|
layer_ = ZWLR_LAYER_SHELL_V1_LAYER_TOP;
|
2020-10-23 08:04:58 +02:00
|
|
|
} else if (layer == bar_layer::OVERLAY) {
|
2020-10-22 07:16:12 +02:00
|
|
|
layer_ = ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY;
|
|
|
|
}
|
|
|
|
// updating already mapped window
|
|
|
|
if (layer_surface_) {
|
2020-10-23 09:49:09 +02:00
|
|
|
if (zwlr_layer_surface_v1_get_version(layer_surface_.get()) >=
|
2020-10-22 07:16:12 +02:00
|
|
|
ZWLR_LAYER_SURFACE_V1_SET_LAYER_SINCE_VERSION) {
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_surface_v1_set_layer(layer_surface_.get(), layer_);
|
2020-10-22 07:16:12 +02:00
|
|
|
} else {
|
2020-10-23 08:04:58 +02:00
|
|
|
spdlog::warn("Unable to change layer: layer-shell implementation is too old");
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setMargins(const struct bar_margins& margins) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
margins_ = margins;
|
|
|
|
// updating already mapped window
|
|
|
|
if (layer_surface_) {
|
|
|
|
zwlr_layer_surface_v1_set_margin(
|
2020-10-23 09:49:09 +02:00
|
|
|
layer_surface_.get(), margins_.top, margins_.right, margins_.bottom, margins_.left);
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:05:18 +01:00
|
|
|
void setPassThrough(bool enable) override {
|
|
|
|
passthrough_ = enable;
|
|
|
|
/* GTK overwrites any region changes applied directly to the wl_surface,
|
|
|
|
* thus the same GTK region API as in the GLS impl has to be used. */
|
|
|
|
auto gdk_window = window_.get_window();
|
|
|
|
if (gdk_window) {
|
|
|
|
Cairo::RefPtr<Cairo::Region> region;
|
|
|
|
if (enable) {
|
|
|
|
region = Cairo::Region::create();
|
|
|
|
}
|
|
|
|
gdk_window->input_shape_combine_region(region, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setPosition(const std::string_view& position) override {
|
2020-10-22 07:16:12 +02:00
|
|
|
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
|
|
|
|
if (position == "bottom") {
|
|
|
|
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
|
|
|
|
} else if (position == "left") {
|
|
|
|
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
|
|
|
|
} else if (position == "right") {
|
|
|
|
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// updating already mapped window
|
|
|
|
if (layer_surface_) {
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_surface_v1_set_anchor(layer_surface_.get(), anchor_);
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setSize(uint32_t width, uint32_t height) override {
|
2020-10-23 11:59:04 +02:00
|
|
|
configured_width_ = width_ = width;
|
|
|
|
configured_height_ = height_ = height;
|
2020-10-22 07:16:12 +02:00
|
|
|
// layer_shell.configure handler should update exclusive zone if size changes
|
|
|
|
window_.set_size_request(width, height);
|
|
|
|
};
|
|
|
|
|
2020-10-23 09:13:23 +02:00
|
|
|
void commit() override {
|
|
|
|
if (surface_) {
|
|
|
|
wl_surface_commit(surface_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
private:
|
|
|
|
constexpr static uint8_t VERTICAL_ANCHOR =
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
|
|
|
|
constexpr static uint8_t HORIZONTAL_ANCHOR =
|
|
|
|
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
template <auto fn>
|
|
|
|
using deleter_fn = std::integral_constant<decltype(fn), fn>;
|
|
|
|
using layer_surface_ptr =
|
|
|
|
std::unique_ptr<zwlr_layer_surface_v1, deleter_fn<zwlr_layer_surface_v1_destroy>>;
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
Gtk::Window& window_;
|
|
|
|
std::string output_name_;
|
2020-10-23 11:59:04 +02:00
|
|
|
uint32_t configured_width_ = 0;
|
|
|
|
uint32_t configured_height_ = 0;
|
|
|
|
uint32_t width_ = 0;
|
|
|
|
uint32_t height_ = 0;
|
2020-10-22 07:16:12 +02:00
|
|
|
uint8_t anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
|
|
|
|
bool exclusive_zone_ = true;
|
2021-01-17 10:05:18 +01:00
|
|
|
bool passthrough_ = false;
|
2020-10-22 07:16:12 +02:00
|
|
|
struct bar_margins margins_;
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_shell_v1_layer layer_ = ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
|
|
|
|
struct wl_output* output_ = nullptr; // owned by GTK
|
|
|
|
struct wl_surface* surface_ = nullptr; // owned by GTK
|
|
|
|
layer_surface_ptr layer_surface_;
|
2020-10-22 07:16:12 +02:00
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void onRealize() {
|
2020-10-22 07:16:12 +02:00
|
|
|
auto gdk_window = window_.get_window()->gobj();
|
|
|
|
gdk_wayland_window_set_use_custom_surface(gdk_window);
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void onMap(GdkEventAny* ev) {
|
2020-10-23 09:13:23 +02:00
|
|
|
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
|
|
|
|
.configure = onSurfaceConfigure,
|
|
|
|
.closed = onSurfaceClosed,
|
|
|
|
};
|
2020-10-22 07:16:12 +02:00
|
|
|
auto client = Client::inst();
|
|
|
|
auto gdk_window = window_.get_window()->gobj();
|
|
|
|
surface_ = gdk_wayland_window_get_wl_surface(gdk_window);
|
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
layer_surface_.reset(zwlr_layer_shell_v1_get_layer_surface(
|
|
|
|
client->layer_shell, surface_, output_, layer_, "waybar"));
|
2020-10-22 07:16:12 +02:00
|
|
|
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_surface_v1_add_listener(layer_surface_.get(), &layer_surface_listener, this);
|
|
|
|
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface_.get(), false);
|
|
|
|
zwlr_layer_surface_v1_set_anchor(layer_surface_.get(), anchor_);
|
2020-10-22 07:16:12 +02:00
|
|
|
zwlr_layer_surface_v1_set_margin(
|
2020-10-23 09:49:09 +02:00
|
|
|
layer_surface_.get(), margins_.top, margins_.right, margins_.bottom, margins_.left);
|
2020-10-23 09:13:23 +02:00
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
setSurfaceSize(width_, height_);
|
|
|
|
setExclusiveZone(exclusive_zone_);
|
2021-01-17 10:05:18 +01:00
|
|
|
setPassThrough(passthrough_);
|
2020-10-22 07:16:12 +02:00
|
|
|
|
2020-10-23 09:13:23 +02:00
|
|
|
commit();
|
2020-10-22 07:16:12 +02:00
|
|
|
wl_display_roundtrip(client->wl_display);
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void onConfigure(GdkEventConfigure* ev) {
|
2020-10-22 07:16:12 +02:00
|
|
|
/*
|
|
|
|
* GTK wants new size for the window.
|
|
|
|
*
|
|
|
|
* Prefer configured size if it's non-default.
|
|
|
|
* If the size is not set and the window is smaller than requested by GTK, request resize from
|
|
|
|
* layer surface.
|
|
|
|
*/
|
|
|
|
auto tmp_height = height_;
|
|
|
|
auto tmp_width = width_;
|
|
|
|
if (ev->height > static_cast<int>(height_)) {
|
|
|
|
// Default minimal value
|
|
|
|
if (height_ > 1) {
|
|
|
|
spdlog::warn(MIN_HEIGHT_MSG, height_, ev->height);
|
|
|
|
}
|
2020-10-23 11:59:04 +02:00
|
|
|
if (configured_height_ > 1) {
|
2020-10-22 07:16:12 +02:00
|
|
|
spdlog::info(SIZE_DEFINED, "Height");
|
2020-10-23 11:59:04 +02:00
|
|
|
} else {
|
|
|
|
tmp_height = ev->height;
|
|
|
|
}
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
if (ev->width > static_cast<int>(width_)) {
|
|
|
|
// Default minimal value
|
|
|
|
if (width_ > 1) {
|
|
|
|
spdlog::warn(MIN_WIDTH_MSG, width_, ev->width);
|
|
|
|
}
|
2020-10-23 11:59:04 +02:00
|
|
|
if (configured_width_ > 1) {
|
2020-10-22 07:16:12 +02:00
|
|
|
spdlog::info(SIZE_DEFINED, "Width");
|
2020-10-23 11:59:04 +02:00
|
|
|
} else {
|
|
|
|
tmp_width = ev->width;
|
|
|
|
}
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
if (tmp_width != width_ || tmp_height != height_) {
|
2020-10-28 16:06:40 +01:00
|
|
|
setSurfaceSize(tmp_width, tmp_height);
|
2020-10-23 09:13:23 +02:00
|
|
|
commit();
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
void setSurfaceSize(uint32_t width, uint32_t height) {
|
2020-10-22 07:16:12 +02:00
|
|
|
/* If the client is anchored to two opposite edges, layer_surface.configure will return
|
|
|
|
* size without margins for the axis.
|
|
|
|
* layer_surface.set_size, however, expects size with margins for the anchored axis.
|
|
|
|
* This is not specified by wlr-layer-shell and based on actual behavior of sway.
|
2020-10-23 11:59:04 +02:00
|
|
|
*
|
|
|
|
* If the size for unanchored axis is not set (0), change request to 1 to avoid automatic
|
|
|
|
* assignment by the compositor.
|
2020-10-22 07:16:12 +02:00
|
|
|
*/
|
2020-10-23 11:59:04 +02:00
|
|
|
if ((anchor_ & VERTICAL_ANCHOR) == VERTICAL_ANCHOR) {
|
|
|
|
width = width > 0 ? width : 1;
|
|
|
|
if (height > 1) {
|
|
|
|
height += margins_.top + margins_.bottom;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
height = height > 0 ? height : 1;
|
|
|
|
if (width > 1) {
|
|
|
|
width += margins_.right + margins_.left;
|
|
|
|
}
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
spdlog::debug("Set surface size {}x{} for output {}", width, height, output_name_);
|
2020-10-23 09:49:09 +02:00
|
|
|
zwlr_layer_surface_v1_set_size(layer_surface_.get(), width, height);
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
static void onSurfaceConfigure(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial,
|
|
|
|
uint32_t width, uint32_t height) {
|
2020-10-22 07:16:12 +02:00
|
|
|
auto o = static_cast<RawSurfaceImpl*>(data);
|
|
|
|
if (width != o->width_ || height != o->height_) {
|
|
|
|
o->width_ = width;
|
|
|
|
o->height_ = height;
|
|
|
|
o->window_.set_size_request(o->width_, o->height_);
|
|
|
|
o->window_.resize(o->width_, o->height_);
|
2020-10-28 16:06:40 +01:00
|
|
|
o->setExclusiveZone(o->exclusive_zone_);
|
2020-10-22 07:16:12 +02:00
|
|
|
spdlog::info(BAR_SIZE_MSG,
|
|
|
|
o->width_ == 1 ? "auto" : std::to_string(o->width_),
|
|
|
|
o->height_ == 1 ? "auto" : std::to_string(o->height_),
|
|
|
|
o->output_name_);
|
2020-10-23 09:13:23 +02:00
|
|
|
o->commit();
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
zwlr_layer_surface_v1_ack_configure(surface, serial);
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:06:40 +01:00
|
|
|
static void onSurfaceClosed(void* data, struct zwlr_layer_surface_v1* /* surface */) {
|
2020-10-22 07:16:12 +02:00
|
|
|
auto o = static_cast<RawSurfaceImpl*>(data);
|
2020-10-23 09:49:09 +02:00
|
|
|
o->layer_surface_.reset();
|
2020-10-22 07:16:12 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace waybar
|
|
|
|
|
2019-04-25 13:25:06 +02:00
|
|
|
waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
2019-04-18 17:43:16 +02:00
|
|
|
: output(w_output),
|
2019-04-25 13:25:06 +02:00
|
|
|
config(w_config),
|
2020-05-27 09:10:38 +02:00
|
|
|
window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
2020-10-23 08:04:58 +02:00
|
|
|
layer_{bar_layer::BOTTOM},
|
2019-04-18 17:43:16 +02:00
|
|
|
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
|
|
|
|
center_(Gtk::ORIENTATION_HORIZONTAL, 0),
|
|
|
|
right_(Gtk::ORIENTATION_HORIZONTAL, 0),
|
|
|
|
box_(Gtk::ORIENTATION_HORIZONTAL, 0) {
|
2018-08-09 20:22:01 +02:00
|
|
|
window.set_title("waybar");
|
2018-10-04 18:47:06 +02:00
|
|
|
window.set_name("waybar");
|
2018-10-29 21:52:53 +01:00
|
|
|
window.set_decorated(false);
|
2019-06-08 20:04:34 +02:00
|
|
|
window.get_style_context()->add_class(output->name);
|
2019-07-04 03:09:31 +02:00
|
|
|
window.get_style_context()->add_class(config["name"].asString());
|
2019-07-04 03:02:09 +02:00
|
|
|
window.get_style_context()->add_class(config["position"].asString());
|
2019-04-18 17:43:16 +02:00
|
|
|
|
2020-10-23 08:04:58 +02:00
|
|
|
if (config["layer"] == "top") {
|
|
|
|
layer_ = bar_layer::TOP;
|
|
|
|
} else if (config["layer"] == "overlay") {
|
|
|
|
layer_ = bar_layer::OVERLAY;
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:40:25 +01:00
|
|
|
if (config["exclusive"].isBool()) {
|
|
|
|
exclusive = config["exclusive"].asBool();
|
|
|
|
} else if (layer_ == bar_layer::OVERLAY) {
|
|
|
|
// swaybar defaults: overlay mode does not reserve an exclusive zone
|
|
|
|
exclusive = false;
|
|
|
|
}
|
|
|
|
|
2021-01-17 10:05:18 +01:00
|
|
|
bool passthrough = false;
|
|
|
|
if (config["passthrough"].isBool()) {
|
|
|
|
passthrough = config["passthrough"].asBool();
|
|
|
|
} else if (layer_ == bar_layer::OVERLAY) {
|
|
|
|
// swaybar defaults: overlay mode does not accept pointer events.
|
|
|
|
passthrough = true;
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
auto position = config["position"].asString();
|
2018-08-19 13:39:57 +02:00
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
if (position == "right" || position == "left") {
|
2019-03-22 12:25:05 +01:00
|
|
|
left_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0);
|
|
|
|
center_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0);
|
|
|
|
right_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0);
|
|
|
|
box_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0);
|
|
|
|
vertical = true;
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
2021-01-17 10:05:18 +01:00
|
|
|
|
2020-12-28 21:44:16 +01:00
|
|
|
left_.get_style_context()->add_class("modules-left");
|
|
|
|
center_.get_style_context()->add_class("modules-center");
|
|
|
|
right_.get_style_context()->add_class("modules-right");
|
2020-10-23 11:59:04 +02:00
|
|
|
|
2021-10-20 11:11:49 +02:00
|
|
|
if (config["spacing"].isInt()) {
|
|
|
|
int spacing = config["spacing"].asInt();
|
|
|
|
left_.set_spacing(spacing);
|
|
|
|
center_.set_spacing(spacing);
|
|
|
|
right_.set_spacing(spacing);
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:59:04 +02:00
|
|
|
uint32_t height = config["height"].isUInt() ? config["height"].asUInt() : 0;
|
|
|
|
uint32_t width = config["width"].isUInt() ? config["width"].asUInt() : 0;
|
2020-10-22 07:16:12 +02:00
|
|
|
|
|
|
|
struct bar_margins margins_;
|
2018-08-19 13:39:57 +02:00
|
|
|
|
2019-08-23 08:29:18 +02:00
|
|
|
if (config["margin-top"].isInt() || config["margin-right"].isInt() ||
|
|
|
|
config["margin-bottom"].isInt() || config["margin-left"].isInt()) {
|
|
|
|
margins_ = {
|
|
|
|
config["margin-top"].isInt() ? config["margin-top"].asInt() : 0,
|
|
|
|
config["margin-right"].isInt() ? config["margin-right"].asInt() : 0,
|
|
|
|
config["margin-bottom"].isInt() ? config["margin-bottom"].asInt() : 0,
|
|
|
|
config["margin-left"].isInt() ? config["margin-left"].asInt() : 0,
|
|
|
|
};
|
|
|
|
} else if (config["margin"].isString()) {
|
|
|
|
std::istringstream iss(config["margin"].asString());
|
|
|
|
std::vector<std::string> margins{std::istream_iterator<std::string>(iss), {}};
|
|
|
|
try {
|
|
|
|
if (margins.size() == 1) {
|
|
|
|
auto gaps = std::stoi(margins[0], nullptr, 10);
|
|
|
|
margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps};
|
|
|
|
}
|
|
|
|
if (margins.size() == 2) {
|
|
|
|
auto vertical_margins = std::stoi(margins[0], nullptr, 10);
|
|
|
|
auto horizontal_margins = std::stoi(margins[1], nullptr, 10);
|
|
|
|
margins_ = {.top = vertical_margins,
|
|
|
|
.right = horizontal_margins,
|
|
|
|
.bottom = vertical_margins,
|
|
|
|
.left = horizontal_margins};
|
|
|
|
}
|
|
|
|
if (margins.size() == 3) {
|
|
|
|
auto horizontal_margins = std::stoi(margins[1], nullptr, 10);
|
|
|
|
margins_ = {.top = std::stoi(margins[0], nullptr, 10),
|
|
|
|
.right = horizontal_margins,
|
|
|
|
.bottom = std::stoi(margins[2], nullptr, 10),
|
|
|
|
.left = horizontal_margins};
|
|
|
|
}
|
|
|
|
if (margins.size() == 4) {
|
|
|
|
margins_ = {.top = std::stoi(margins[0], nullptr, 10),
|
|
|
|
.right = std::stoi(margins[1], nullptr, 10),
|
|
|
|
.bottom = std::stoi(margins[2], nullptr, 10),
|
|
|
|
.left = std::stoi(margins[3], nullptr, 10)};
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
spdlog::warn("Invalid margins: {}", config["margin"].asString());
|
|
|
|
}
|
|
|
|
} else if (config["margin"].isInt()) {
|
|
|
|
auto gaps = config["margin"].asInt();
|
|
|
|
margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps};
|
|
|
|
}
|
|
|
|
|
2019-12-28 01:31:18 +01:00
|
|
|
#ifdef HAVE_GTK_LAYER_SHELL
|
2020-10-22 07:16:12 +02:00
|
|
|
bool use_gls = config["gtk-layer-shell"].isBool() ? config["gtk-layer-shell"].asBool() : true;
|
|
|
|
if (use_gls) {
|
|
|
|
surface_impl_ = std::make_unique<GLSSurfaceImpl>(window, *output);
|
|
|
|
} else
|
2019-12-28 01:31:18 +01:00
|
|
|
#endif
|
2020-10-22 07:16:12 +02:00
|
|
|
{
|
|
|
|
surface_impl_ = std::make_unique<RawSurfaceImpl>(window, *output);
|
2020-08-10 03:58:01 +02:00
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
|
2020-10-23 08:04:58 +02:00
|
|
|
surface_impl_->setLayer(layer_);
|
2021-01-17 10:40:25 +01:00
|
|
|
surface_impl_->setExclusiveZone(exclusive);
|
2020-10-28 16:06:40 +01:00
|
|
|
surface_impl_->setMargins(margins_);
|
2021-01-17 10:05:18 +01:00
|
|
|
surface_impl_->setPassThrough(passthrough);
|
2020-10-28 16:06:40 +01:00
|
|
|
surface_impl_->setPosition(position);
|
2020-10-23 11:59:04 +02:00
|
|
|
surface_impl_->setSize(width, height);
|
2018-10-29 21:52:53 +01:00
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
|
2020-08-10 03:58:01 +02:00
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
setupWidgets();
|
|
|
|
window.show_all();
|
2020-11-30 21:05:18 +01:00
|
|
|
|
|
|
|
if (spdlog::should_log(spdlog::level::debug)) {
|
|
|
|
// Unfortunately, this function isn't in the C++ bindings, so we have to call the C version.
|
|
|
|
char* gtk_tree = gtk_style_context_to_string(
|
|
|
|
window.get_style_context()->gobj(),
|
|
|
|
(GtkStyleContextPrintFlags)(GTK_STYLE_CONTEXT_PRINT_RECURSE |
|
|
|
|
GTK_STYLE_CONTEXT_PRINT_SHOW_STYLE));
|
|
|
|
spdlog::debug("GTK widget tree:\n{}", gtk_tree);
|
|
|
|
g_free(gtk_tree);
|
|
|
|
}
|
2020-08-10 03:58:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
void waybar::Bar::onMap(GdkEventAny*) {
|
2020-08-10 03:58:01 +02:00
|
|
|
/*
|
|
|
|
* Obtain a pointer to the custom layer surface for modules that require it (idle_inhibitor).
|
|
|
|
*/
|
2019-05-25 17:50:45 +02:00
|
|
|
auto gdk_window = window.get_window()->gobj();
|
|
|
|
surface = gdk_wayland_window_get_wl_surface(gdk_window);
|
2018-08-09 20:22:01 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
void waybar::Bar::setVisible(bool value) {
|
|
|
|
visible = value;
|
|
|
|
if (!visible) {
|
|
|
|
window.get_style_context()->add_class("hidden");
|
|
|
|
window.set_opacity(0);
|
2020-10-23 08:04:58 +02:00
|
|
|
surface_impl_->setLayer(bar_layer::BOTTOM);
|
2020-10-22 07:16:12 +02:00
|
|
|
} else {
|
|
|
|
window.get_style_context()->remove_class("hidden");
|
|
|
|
window.set_opacity(1);
|
2020-10-23 08:04:58 +02:00
|
|
|
surface_impl_->setLayer(layer_);
|
2019-12-28 01:31:18 +01:00
|
|
|
}
|
2021-01-17 10:40:25 +01:00
|
|
|
surface_impl_->setExclusiveZone(exclusive && visible);
|
2020-10-23 09:13:23 +02:00
|
|
|
surface_impl_->commit();
|
2019-05-09 15:10:13 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 07:16:12 +02:00
|
|
|
void waybar::Bar::toggle() { setVisible(!visible); }
|
2019-09-01 09:54:53 +02:00
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
// Converting string to button code rn as to avoid doing it later
|
|
|
|
void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) {
|
2019-04-25 13:25:06 +02:00
|
|
|
if (config.isMember(module_name)) {
|
|
|
|
Json::Value& module = config[module_name];
|
2019-04-18 17:43:16 +02:00
|
|
|
if (module.isMember("format-alt")) {
|
|
|
|
if (module.isMember("format-alt-click")) {
|
|
|
|
Json::Value& click = module["format-alt-click"];
|
|
|
|
if (click.isString()) {
|
2019-05-09 10:30:54 +02:00
|
|
|
if (click == "click-right") {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 3U;
|
2019-05-09 10:30:54 +02:00
|
|
|
} else if (click == "click-middle") {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 2U;
|
2019-05-09 10:30:54 +02:00
|
|
|
} else if (click == "click-backward") {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 8U;
|
2019-05-09 10:30:54 +02:00
|
|
|
} else if (click == "click-forward") {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 9U;
|
2019-04-18 17:43:16 +02:00
|
|
|
} else {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 1U; // default click-left
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 1U;
|
2019-04-18 17:43:16 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 12:37:24 +02:00
|
|
|
module["format-alt-click"] = 1U;
|
2019-02-01 21:45:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) {
|
2019-04-25 13:25:06 +02:00
|
|
|
if (config.isMember(module_list_name)) {
|
|
|
|
Json::Value& modules = config[module_list_name];
|
2019-04-18 17:43:16 +02:00
|
|
|
for (const Json::Value& module_name : modules) {
|
|
|
|
if (module_name.isString()) {
|
|
|
|
setupAltFormatKeyForModule(module_name.asString());
|
2019-02-01 21:45:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-11 00:32:59 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
void waybar::Bar::handleSignal(int signal) {
|
2019-03-18 18:46:44 +01:00
|
|
|
for (auto& module : modules_left_) {
|
|
|
|
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
|
2019-04-24 12:37:24 +02:00
|
|
|
if (custom != nullptr) {
|
|
|
|
custom->refresh(signal);
|
|
|
|
}
|
2019-03-18 18:46:44 +01:00
|
|
|
}
|
|
|
|
for (auto& module : modules_center_) {
|
|
|
|
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
|
2019-04-24 12:37:24 +02:00
|
|
|
if (custom != nullptr) {
|
|
|
|
custom->refresh(signal);
|
|
|
|
}
|
2019-03-18 18:46:44 +01:00
|
|
|
}
|
|
|
|
for (auto& module : modules_right_) {
|
|
|
|
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
|
2019-04-24 12:37:24 +02:00
|
|
|
if (custom != nullptr) {
|
|
|
|
custom->refresh(signal);
|
|
|
|
}
|
2019-03-18 18:46:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
|
2019-04-25 13:25:06 +02:00
|
|
|
if (config[pos].isArray()) {
|
|
|
|
for (const auto& name : config[pos]) {
|
2018-08-19 13:39:57 +02:00
|
|
|
try {
|
2018-08-20 14:50:45 +02:00
|
|
|
auto module = factory.makeModule(name.asString());
|
2018-08-19 13:39:57 +02:00
|
|
|
if (pos == "modules-left") {
|
2018-08-20 14:50:45 +02:00
|
|
|
modules_left_.emplace_back(module);
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
|
|
|
if (pos == "modules-center") {
|
2018-08-20 14:50:45 +02:00
|
|
|
modules_center_.emplace_back(module);
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
|
|
|
if (pos == "modules-right") {
|
2018-08-20 14:50:45 +02:00
|
|
|
modules_right_.emplace_back(module);
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
2018-11-24 11:20:03 +01:00
|
|
|
module->dp.connect([module, &name] {
|
2019-06-16 15:14:31 +02:00
|
|
|
try {
|
|
|
|
module->update();
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
spdlog::error("{}: {}", name.asString(), e.what());
|
|
|
|
}
|
2018-11-24 11:13:52 +01:00
|
|
|
});
|
2018-08-19 13:39:57 +02:00
|
|
|
} catch (const std::exception& e) {
|
2019-05-19 01:44:45 +02:00
|
|
|
spdlog::warn("module {}: {}", name.asString(), e.what());
|
2018-08-19 13:39:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:43:16 +02:00
|
|
|
auto waybar::Bar::setupWidgets() -> void {
|
2018-12-26 11:13:36 +01:00
|
|
|
window.add(box_);
|
2019-06-28 14:16:09 +02:00
|
|
|
box_.pack_start(left_, false, false);
|
2018-12-26 11:13:36 +01:00
|
|
|
box_.set_center_widget(center_);
|
2019-06-28 14:16:09 +02:00
|
|
|
box_.pack_end(right_, false, false);
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2019-04-19 11:56:40 +02:00
|
|
|
// Convert to button code for every module that is used.
|
|
|
|
setupAltFormatKeyForModuleList("modules-left");
|
|
|
|
setupAltFormatKeyForModuleList("modules-right");
|
|
|
|
setupAltFormatKeyForModuleList("modules-center");
|
|
|
|
|
2019-04-25 13:25:06 +02:00
|
|
|
Factory factory(*this, config);
|
2018-08-19 13:39:57 +02:00
|
|
|
getModules(factory, "modules-left");
|
|
|
|
getModules(factory, "modules-center");
|
|
|
|
getModules(factory, "modules-right");
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& module : modules_left_) {
|
2019-06-28 14:16:09 +02:00
|
|
|
left_.pack_start(*module, false, false);
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& module : modules_center_) {
|
2019-06-28 14:16:09 +02:00
|
|
|
center_.pack_start(*module, false, false);
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
std::reverse(modules_right_.begin(), modules_right_.end());
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& module : modules_right_) {
|
2019-06-28 14:16:09 +02:00
|
|
|
right_.pack_end(*module, false, false);
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|