add river/window module

This commit is contained in:
trevnels 2022-05-19 15:20:04 -04:00
parent 9bc821bdac
commit e2b676b800
4 changed files with 164 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#endif
#ifdef HAVE_RIVER
#include "modules/river/tags.hpp"
#include "modules/river/window.hpp"
#endif
#if defined(__linux__) && !defined(NO_FILESYSTEM)
#include "modules/battery.hpp"

View File

@ -0,0 +1,33 @@
#pragma once
#include <gtkmm/button.h>
#include <wayland-client.h>
#include "ALabel.hpp"
#include "bar.hpp"
#include "river-status-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
namespace waybar::modules::river {
class Window : public waybar::ALabel {
public:
Window(const std::string &, const waybar::Bar &, const Json::Value &);
~Window();
// Handlers for wayland events
void handle_focused_view(const char *title);
void handle_focused_output(struct wl_output *output);
void handle_unfocused_output(struct wl_output *output);
struct zriver_status_manager_v1 *status_manager_;
struct wl_seat *seat_;
private:
const waybar::Bar &bar_;
struct wl_output *output_; // stores the output this module belongs to
struct wl_output *focused_output_; // stores the currently focused output
struct zriver_seat_status_v1 *seat_status_;
};
} /* namespace waybar::modules::river */

View File

@ -45,6 +45,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "river/tags") {
return new waybar::modules::river::Tags(id, bar_, config_[name]);
}
if (ref == "river/window") {
return new waybar::modules::river::Window(id, bar_, config_[name]);
}
#endif
if (ref == "idle_inhibitor") {
return new waybar::modules::IdleInhibitor(id, bar_, config_[name]);

View File

@ -0,0 +1,127 @@
#include "modules/river/window.hpp"
#include <gtkmm/button.h>
#include <gtkmm/label.h>
#include <spdlog/spdlog.h>
#include <wayland-client.h>
#include <algorithm>
#include "client.hpp"
#include "xdg-output-unstable-v1-client-protocol.h"
namespace waybar::modules::river {
static void listen_focused_view(void *data, struct zriver_seat_status_v1 *zriver_seat_status_v1,
const char *title) {
static_cast<Window *>(data)->handle_focused_view(title);
}
static void listen_focused_output(void *data, struct zriver_seat_status_v1 *zriver_seat_status_v1,
struct wl_output *output) {
static_cast<Window *>(data)->handle_focused_output(output);
}
static void listen_unfocused_output(void *data, struct zriver_seat_status_v1 *zriver_seat_status_v1,
struct wl_output *output) {
static_cast<Window *>(data)->handle_unfocused_output(output);
}
static const zriver_seat_status_v1_listener seat_status_listener_impl{
.focused_output = listen_focused_output,
.unfocused_output = listen_unfocused_output,
.focused_view = listen_focused_view,
};
static void handle_global(void *data, struct wl_registry *registry, uint32_t name,
const char *interface, uint32_t version) {
if (std::strcmp(interface, zriver_status_manager_v1_interface.name) == 0) {
version = std::min<uint32_t>(version, 2);
static_cast<Window *>(data)->status_manager_ = static_cast<struct zriver_status_manager_v1 *>(
wl_registry_bind(registry, name, &zriver_status_manager_v1_interface, version));
}
if (std::strcmp(interface, wl_seat_interface.name) == 0) {
version = std::min<uint32_t>(version, 1);
static_cast<Window *>(data)->seat_ = static_cast<struct wl_seat *>(
wl_registry_bind(registry, name, &wl_seat_interface, version));
}
}
static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
/* Ignore event */
}
static const wl_registry_listener registry_listener_impl = {.global = handle_global,
.global_remove = handle_global_remove};
Window::Window(const std::string &id, const waybar::Bar &bar, const Json::Value &config)
: waybar::ALabel(config, "window", id, "{}", 30),
status_manager_{nullptr},
seat_{nullptr},
bar_(bar),
seat_status_{nullptr} {
struct wl_display *display = Client::inst()->wl_display;
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener_impl, this);
wl_display_roundtrip(display);
output_ = gdk_wayland_monitor_get_wl_output(bar_.output->monitor->gobj());
if (!status_manager_) {
spdlog::error("river_status_manager_v1 not advertised");
return;
}
if (!seat_) {
spdlog::error("wl_seat not advertised");
}
label_.hide(); // hide the label until populated
ALabel::update();
seat_status_ = zriver_status_manager_v1_get_river_seat_status(status_manager_, seat_);
zriver_seat_status_v1_add_listener(seat_status_, &seat_status_listener_impl, this);
zriver_status_manager_v1_destroy(status_manager_);
}
Window::~Window() {
if (seat_status_) {
zriver_seat_status_v1_destroy(seat_status_);
}
}
void Window::handle_focused_view(const char *title) {
// don't change the label on unfocused outputs.
// this makes the current output report its currently focused view, and unfocused outputs will
// report their last focused views. when freshly starting the bar, unfocused outputs don't have a
// last focused view, and will get blank labels until they are brought into focus at least once.
if (focused_output_ != output_) return;
if (std::strcmp(title, "") == 0) {
label_.hide(); // hide empty labels
} else {
label_.show();
label_.set_markup(title);
}
ALabel::update();
}
void Window::handle_focused_output(struct wl_output *output) {
if (output_ == output) { // if we focused the output this bar belongs to
label_.get_style_context()->add_class("focused");
ALabel::update();
}
focused_output_ = output;
}
void Window::handle_unfocused_output(struct wl_output *output) {
if (output_ == output) { // if we unfocused the output this bar belongs to
label_.get_style_context()->remove_class("focused");
ALabel::update();
}
}
} /* namespace waybar::modules::river */