refactor(Bar): roundtrip before setup widgets

This commit is contained in:
Alex 2019-04-19 11:56:40 +02:00
parent 42dc0c4c71
commit 171ecd53aa
4 changed files with 21 additions and 35 deletions

View File

@ -38,6 +38,12 @@ class Bar {
bool vertical = false; bool vertical = false;
private: private:
static inline const std::string MIN_HEIGHT_MSG =
"Requested height: {} exceeds the minimum height: {} required by the modules";
static inline const std::string MIN_WIDTH_MSG =
"Requested width: {} exceeds the minimum width: {} required by the modules";
static inline const std::string BAR_SIZE_MSG =
"Bar configured (width: {}, height: {}) for output: {}";
static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t, static void layerSurfaceHandleConfigure(void *, struct zwlr_layer_surface_v1 *, uint32_t,
uint32_t, uint32_t); uint32_t, uint32_t);
static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *); static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *);

View File

@ -21,7 +21,6 @@ class Client {
struct wl_registry * registry = nullptr; struct wl_registry * registry = nullptr;
struct zwlr_layer_shell_v1 * layer_shell = nullptr; struct zwlr_layer_shell_v1 * layer_shell = nullptr;
struct zxdg_output_manager_v1 * xdg_output_manager = nullptr; struct zxdg_output_manager_v1 * xdg_output_manager = nullptr;
struct wl_seat * seat = nullptr;
struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager = nullptr; struct zwp_idle_inhibit_manager_v1 *idle_inhibit_manager = nullptr;
std::vector<std::unique_ptr<Bar>> bars; std::vector<std::unique_ptr<Bar>> bars;

View File

@ -14,7 +14,6 @@ waybar::Bar::Bar(struct waybar_output* w_output)
window.set_title("waybar"); window.set_title("waybar");
window.set_name("waybar"); window.set_name("waybar");
window.set_decorated(false); window.set_decorated(false);
window.set_resizable(false);
if (output->config["position"] == "right" || output->config["position"] == "left") { if (output->config["position"] == "right" || output->config["position"] == "left") {
height_ = 0; height_ = 0;
@ -29,15 +28,11 @@ waybar::Bar::Bar(struct waybar_output* w_output)
gdk_wayland_window_set_use_custom_surface(gdk_window); gdk_wayland_window_set_use_custom_surface(gdk_window);
surface = gdk_wayland_window_get_wl_surface(gdk_window); surface = gdk_wayland_window_get_wl_surface(gdk_window);
// Convert to button code for every module that is used.
setupAltFormatKeyForModuleList("modules-left");
setupAltFormatKeyForModuleList("modules-right");
setupAltFormatKeyForModuleList("modules-center");
std::size_t layer = output->config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP std::size_t layer = output->config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP
: ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
auto client = waybar::Client::inst();
layer_surface = zwlr_layer_shell_v1_get_layer_surface( layer_surface = zwlr_layer_shell_v1_get_layer_surface(
waybar::Client::inst()->layer_shell, surface, output->output, layer, "waybar"); client->layer_shell, surface, output->output, layer, "waybar");
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layerSurfaceHandleConfigure, .configure = layerSurfaceHandleConfigure,
.closed = layerSurfaceHandleClosed, .closed = layerSurfaceHandleClosed,
@ -72,6 +67,7 @@ waybar::Bar::Bar(struct waybar_output* w_output)
zwlr_layer_surface_v1_set_size(layer_surface, width, height); zwlr_layer_surface_v1_set_size(layer_surface, width, height);
wl_surface_commit(surface); wl_surface_commit(surface);
wl_display_roundtrip(client->wl_display);
setupWidgets(); setupWidgets();
} }
@ -136,7 +132,6 @@ void waybar::Bar::handleSignal(int signal) {
void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surface_v1* surface, void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surface_v1* surface,
uint32_t serial, uint32_t width, uint32_t height) { uint32_t serial, uint32_t width, uint32_t height) {
auto o = static_cast<waybar::Bar*>(data); auto o = static_cast<waybar::Bar*>(data);
zwlr_layer_surface_v1_ack_configure(surface, serial);
if (width != o->width_ || height != o->height_) { if (width != o->width_ || height != o->height_) {
o->width_ = width; o->width_ = width;
o->height_ = height; o->height_ = height;
@ -146,31 +141,18 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf
int min_width, min_height; int min_width, min_height;
o->window.get_size(min_width, min_height); o->window.get_size(min_width, min_height);
if (o->height_ < static_cast<uint32_t>(min_height)) { if (o->height_ < static_cast<uint32_t>(min_height)) {
std::cout << fmt::format( std::cout << fmt::format(MIN_HEIGHT_MSG, o->height_, min_height) << std::endl;
"Requested height: {} exceeds the minimum \
height: {} required by the modules",
o->height_,
min_height)
<< std::endl;
o->height_ = min_height; o->height_ = min_height;
} }
if (o->width_ < static_cast<uint32_t>(min_width)) { if (o->width_ < static_cast<uint32_t>(min_width)) {
std::cout << fmt::format( std::cout << fmt::format(MIN_WIDTH_MSG, o->height_, min_width) << std::endl;
"Requested width: {} exceeds the minimum \
width: {} required by the modules",
o->height_,
min_width)
<< std::endl;
o->width_ = min_width; o->width_ = min_width;
} }
std::cout << fmt::format("Bar configured (width: {}, height: {}) for output: {}", std::cout << fmt::format(BAR_SIZE_MSG, o->width_, o->height_, o->output->name) << std::endl;
o->width_,
o->height_,
o->output->name)
<< std::endl;
wl_surface_commit(o->surface); wl_surface_commit(o->surface);
} }
zwlr_layer_surface_v1_ack_configure(surface, serial);
} }
void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) { void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) {
@ -227,6 +209,11 @@ auto waybar::Bar::setupWidgets() -> void {
box_.set_center_widget(center_); box_.set_center_widget(center_);
box_.pack_end(right_, true, true); box_.pack_end(right_, true, true);
// Convert to button code for every module that is used.
setupAltFormatKeyForModuleList("modules-left");
setupAltFormatKeyForModuleList("modules-right");
setupAltFormatKeyForModuleList("modules-center");
Factory factory(*this, output->config); Factory factory(*this, output->config);
getModules(factory, "modules-left"); getModules(factory, "modules-left");
getModules(factory, "modules-center"); getModules(factory, "modules-center");

View File

@ -39,17 +39,13 @@ void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, uint
wl_registry_bind(registry, name, &wl_output_interface, version)); wl_registry_bind(registry, name, &wl_output_interface, version));
client->outputs_.emplace_back(new struct waybar_output({wl_output, "", name, nullptr})); client->outputs_.emplace_back(new struct waybar_output({wl_output, "", name, nullptr}));
client->handleOutput(client->outputs_.back()); client->handleOutput(client->outputs_.back());
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
client->seat = static_cast<struct wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, version));
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 && } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 &&
version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
client->xdg_output_manager = static_cast<struct zxdg_output_manager_v1 *>(wl_registry_bind( client->xdg_output_manager = static_cast<struct zxdg_output_manager_v1 *>(wl_registry_bind(
registry, name, &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION)); registry, name, &zxdg_output_manager_v1_interface, ZXDG_OUTPUT_V1_NAME_SINCE_VERSION));
} else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) { } else if (strcmp(interface, zwp_idle_inhibit_manager_v1_interface.name) == 0) {
waybar::Client::inst()->idle_inhibit_manager = client->idle_inhibit_manager = static_cast<struct zwp_idle_inhibit_manager_v1 *>(
static_cast<struct zwp_idle_inhibit_manager_v1 *>( wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1));
wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1));
} }
} }
@ -216,10 +212,9 @@ void waybar::Client::bindInterfaces() {
}; };
wl_registry_add_listener(registry, &registry_listener, this); wl_registry_add_listener(registry, &registry_listener, this);
wl_display_roundtrip(wl_display); wl_display_roundtrip(wl_display);
if (!layer_shell || !seat || !xdg_output_manager) { if (!layer_shell || !xdg_output_manager) {
throw std::runtime_error("Failed to acquire required resources."); throw std::runtime_error("Failed to acquire required resources.");
} }
wl_display_roundtrip(wl_display);
} }
int waybar::Client::main(int argc, char *argv[]) { int waybar::Client::main(int argc, char *argv[]) {
@ -266,7 +261,6 @@ int waybar::Client::main(int argc, char *argv[]) {
zwlr_layer_shell_v1_destroy(layer_shell); zwlr_layer_shell_v1_destroy(layer_shell);
zwp_idle_inhibit_manager_v1_destroy(idle_inhibit_manager); zwp_idle_inhibit_manager_v1_destroy(idle_inhibit_manager);
wl_registry_destroy(registry); wl_registry_destroy(registry);
wl_seat_destroy(seat);
wl_display_disconnect(wl_display); wl_display_disconnect(wl_display);
return 0; return 0;
} }