Waybar/src/bar.cpp

242 lines
8.7 KiB
C++
Raw Normal View History

2018-08-08 21:54:58 +00:00
#include "bar.hpp"
#include "client.hpp"
2018-08-09 10:05:48 +00:00
#include "factory.hpp"
2018-08-08 21:54:58 +00:00
waybar::Bar::Bar(struct waybar_output* w_output)
: output(w_output),
window{Gtk::WindowType::WINDOW_TOPLEVEL},
surface(nullptr),
layer_surface(nullptr),
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
center_(Gtk::ORIENTATION_HORIZONTAL, 0),
right_(Gtk::ORIENTATION_HORIZONTAL, 0),
box_(Gtk::ORIENTATION_HORIZONTAL, 0) {
2018-08-09 18:22:01 +00:00
window.set_title("waybar");
window.set_name("waybar");
2018-10-29 20:52:53 +00:00
window.set_decorated(false);
2018-10-25 08:43:37 +00:00
window.set_resizable(false);
if (output->config["position"] == "right" || output->config["position"] == "left") {
height_ = 0;
width_ = 30;
}
window.set_size_request(width_, height_);
2018-08-19 11:39:57 +00:00
2019-04-17 17:33:49 +00:00
auto gtk_window = window.gobj();
auto gtk_widget = GTK_WIDGET(gtk_window);
gtk_widget_realize(gtk_widget);
auto gdk_window = window.get_window()->gobj();
2018-08-16 12:29:41 +00:00
gdk_wayland_window_set_use_custom_surface(gdk_window);
surface = gdk_wayland_window_get_wl_surface(gdk_window);
2019-03-10 09:34:56 +00:00
// 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
: ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
layer_surface = zwlr_layer_shell_v1_get_layer_surface(waybar::Client::inst()->layer_shell,
surface, output->output, layer, "waybar");
2018-08-19 11:39:57 +00:00
2018-08-16 12:29:41 +00:00
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layerSurfaceHandleConfigure,
.closed = layerSurfaceHandleClosed,
2018-08-09 18:22:01 +00:00
};
2019-02-01 20:45:59 +00:00
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
2018-08-19 11:39:57 +00:00
auto height = output->config["height"].isUInt() ? output->config["height"].asUInt() : height_;
auto width = output->config["width"].isUInt() ? output->config["width"].asUInt() : width_;
std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
if (output->config["position"] == "bottom") {
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
} else if (output->config["position"] == "left") {
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
} else if (output->config["position"] == "right") {
anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
}
if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM || anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) {
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
} else if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT ||
anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) {
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
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 11:39:57 +00:00
}
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor);
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, vertical ? width : height);
2018-08-19 11:39:57 +00:00
zwlr_layer_surface_v1_set_size(layer_surface, width, height);
2018-08-09 18:22:01 +00:00
wl_surface_commit(surface);
2018-10-29 20:52:53 +00:00
setupWidgets();
2018-08-09 18:22:01 +00:00
}
// Converting string to button code rn as to avoid doing it later
void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) {
if (output->config.isMember(module_name)) {
Json::Value& module = output->config[module_name];
if (module.isMember("format-alt")) {
if (module.isMember("format-alt-click")) {
Json::Value& click = module["format-alt-click"];
if (click.isString()) {
std::string str_click = click.asString();
if (str_click == "click-right") {
module["format-alt-click"] = 3u;
} else if (str_click == "click-middle") {
module["format-alt-click"] = 2u;
} else if (str_click == "click-backward") {
module["format-alt-click"] = 8u;
} else if (str_click == "click-forward") {
module["format-alt-click"] = 9u;
} else {
module["format-alt-click"] = 1u; // default click-left
}
} else {
module["format-alt-click"] = 1u;
}
} else {
module["format-alt-click"] = 1u;
2019-02-01 20:45:59 +00:00
}
}
}
}
void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) {
if (output->config.isMember(module_list_name)) {
Json::Value& modules = output->config[module_list_name];
for (const Json::Value& module_name : modules) {
if (module_name.isString()) {
setupAltFormatKeyForModule(module_name.asString());
2019-02-01 20:45:59 +00:00
}
}
}
}
void waybar::Bar::handleSignal(int signal) {
2019-03-18 17:46:44 +00:00
for (auto& module : modules_left_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if (custom) custom->refresh(signal);
2019-03-18 17:46:44 +00:00
}
for (auto& module : modules_center_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if (custom) custom->refresh(signal);
2019-03-18 17:46:44 +00:00
}
for (auto& module : modules_right_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if (custom) custom->refresh(signal);
2019-03-18 17:46:44 +00:00
}
}
void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surface_v1* surface,
uint32_t serial, uint32_t width, uint32_t height) {
auto o = static_cast<waybar::Bar*>(data);
2018-08-08 21:54:58 +00:00
zwlr_layer_surface_v1_ack_configure(surface, serial);
2018-08-19 11:39:57 +00:00
if (width != o->width_ || height != o->height_) {
o->width_ = width;
o->height_ = height;
2018-11-01 21:00:38 +00:00
o->window.set_size_request(o->width_, o->height_);
o->window.resize(o->width_, o->height_);
int min_width, min_height;
o->window.get_size(min_width, min_height);
if (o->height_ < static_cast<uint32_t>(min_height)) {
std::cout << fmt::format(
"Requested height: {} exceeds the minimum \
height: {} required by the modules",
o->height_, min_height)
<< std::endl;
2018-11-01 21:00:38 +00:00
o->height_ = min_height;
}
if (o->width_ < static_cast<uint32_t>(min_width)) {
std::cout << fmt::format(
"Requested width: {} exceeds the minimum \
width: {} required by the modules",
o->height_, min_width)
<< std::endl;
o->width_ = min_width;
}
std::cout << fmt::format("Bar configured (width: {}, height: {}) for output: {}", o->width_,
o->height_, o->output->name)
<< std::endl;
2018-11-01 21:00:38 +00:00
2018-08-08 21:54:58 +00:00
wl_surface_commit(o->surface);
}
}
void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) {
auto o = static_cast<waybar::Bar*>(data);
2018-08-16 12:29:41 +00:00
zwlr_layer_surface_v1_destroy(o->layer_surface);
2018-08-19 11:39:57 +00:00
o->modules_left_.clear();
o->modules_center_.clear();
o->modules_right_.clear();
2018-08-08 21:54:58 +00:00
}
auto waybar::Bar::toggle() -> void {
2018-08-08 21:54:58 +00:00
visible = !visible;
2018-08-16 12:29:41 +00:00
auto zone = visible ? height_ : 0;
2019-04-15 09:11:00 +00:00
if (!visible) {
window.get_style_context()->add_class("hidded");
} else {
window.get_style_context()->remove_class("hidded");
}
2018-08-16 12:29:41 +00:00
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
2018-08-08 21:54:58 +00:00
wl_surface_commit(surface);
}
void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
if (output->config[pos].isArray()) {
for (const auto& name : output->config[pos]) {
2018-08-19 11:39:57 +00:00
try {
2018-08-20 12:50:45 +00:00
auto module = factory.makeModule(name.asString());
2018-08-19 11:39:57 +00:00
if (pos == "modules-left") {
2018-08-20 12:50:45 +00:00
modules_left_.emplace_back(module);
2018-08-19 11:39:57 +00:00
}
if (pos == "modules-center") {
2018-08-20 12:50:45 +00:00
modules_center_.emplace_back(module);
2018-08-19 11:39:57 +00:00
}
if (pos == "modules-right") {
2018-08-20 12:50:45 +00:00
modules_right_.emplace_back(module);
2018-08-19 11:39:57 +00:00
}
module->dp.connect([module, &name] {
2018-11-24 10:13:52 +00:00
try {
module->update();
} catch (const std::exception& e) {
std::cerr << name.asString() + ": " + e.what() << std::endl;
2018-11-24 10:13:52 +00:00
}
});
2018-08-19 11:39:57 +00:00
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
}
}
auto waybar::Bar::setupWidgets() -> void {
2018-12-26 10:13:36 +00:00
window.add(box_);
box_.pack_start(left_, true, true);
box_.set_center_widget(center_);
box_.pack_end(right_, true, true);
2018-08-08 21:54:58 +00:00
Factory factory(*this, output->config);
2018-08-19 11:39:57 +00:00
getModules(factory, "modules-left");
getModules(factory, "modules-center");
getModules(factory, "modules-right");
2018-09-04 21:50:08 +00:00
for (auto const& module : modules_left_) {
2018-12-26 10:13:36 +00:00
left_.pack_start(*module, false, true, 0);
2018-08-09 10:05:48 +00:00
}
2018-09-04 21:50:08 +00:00
for (auto const& module : modules_center_) {
2018-12-26 10:13:36 +00:00
center_.pack_start(*module, true, true, 0);
2018-08-09 10:05:48 +00:00
}
2018-08-19 11:39:57 +00:00
std::reverse(modules_right_.begin(), modules_right_.end());
2018-09-04 21:50:08 +00:00
for (auto const& module : modules_right_) {
2018-12-26 10:13:36 +00:00
right_.pack_end(*module, false, false, 0);
2018-08-09 10:05:48 +00:00
}
2019-02-01 20:45:59 +00:00
window.show_all();
2018-08-08 21:54:58 +00:00
}