2024-01-14 18:37:49 +00:00
|
|
|
#include "bar.hpp"
|
2019-12-28 00:31:18 +00:00
|
|
|
|
2024-01-14 18:37:49 +00:00
|
|
|
#include <gtk-layer-shell.h>
|
2020-10-21 06:18:58 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2020-10-23 07:49:09 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
2018-08-08 21:54:58 +00:00
|
|
|
#include "client.hpp"
|
2018-08-09 10:05:48 +00:00
|
|
|
#include "factory.hpp"
|
2021-10-31 22:55:13 +00:00
|
|
|
#include "group.hpp"
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2020-10-20 02:34:48 +00:00
|
|
|
#ifdef HAVE_SWAY
|
|
|
|
#include "modules/sway/bar.hpp"
|
|
|
|
#endif
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
namespace waybar {
|
|
|
|
static constexpr const char* MIN_HEIGHT_MSG =
|
2021-10-28 16:37:11 +00:00
|
|
|
"Requested height: {} is less than the minimum height: {} required by the modules";
|
2020-10-22 05:16:12 +00:00
|
|
|
|
|
|
|
static constexpr const char* MIN_WIDTH_MSG =
|
2021-10-28 16:37:11 +00:00
|
|
|
"Requested width: {} is less than the minimum width: {} required by the modules";
|
2020-10-22 05:16:12 +00:00
|
|
|
|
|
|
|
static constexpr const char* BAR_SIZE_MSG = "Bar configured (width: {}, height: {}) for output: {}";
|
|
|
|
|
2021-11-20 03:29:51 +00:00
|
|
|
const Bar::bar_mode_map Bar::PRESET_MODES = { //
|
2021-11-20 03:31:41 +00:00
|
|
|
{"default",
|
|
|
|
{// Special mode to hold the global bar configuration
|
|
|
|
.layer = bar_layer::BOTTOM,
|
|
|
|
.exclusive = true,
|
|
|
|
.passthrough = false,
|
|
|
|
.visible = true}},
|
2021-11-20 03:29:51 +00:00
|
|
|
{"dock",
|
|
|
|
{// Modes supported by the sway config; see man sway-bar(5)
|
|
|
|
.layer = bar_layer::BOTTOM,
|
|
|
|
.exclusive = true,
|
|
|
|
.passthrough = false,
|
|
|
|
.visible = true}},
|
|
|
|
{"hide",
|
|
|
|
{//
|
|
|
|
.layer = bar_layer::TOP,
|
|
|
|
.exclusive = false,
|
|
|
|
.passthrough = false,
|
|
|
|
.visible = true}},
|
|
|
|
{"invisible",
|
|
|
|
{//
|
2024-05-19 11:53:09 +00:00
|
|
|
.layer = std::nullopt,
|
2021-11-20 03:29:51 +00:00
|
|
|
.exclusive = false,
|
|
|
|
.passthrough = true,
|
|
|
|
.visible = false}},
|
|
|
|
{"overlay",
|
|
|
|
{//
|
|
|
|
.layer = bar_layer::TOP,
|
|
|
|
.exclusive = false,
|
|
|
|
.passthrough = true,
|
|
|
|
.visible = true}}};
|
|
|
|
|
2024-02-19 09:50:40 +00:00
|
|
|
const std::string Bar::MODE_DEFAULT = "default";
|
|
|
|
const std::string Bar::MODE_INVISIBLE = "invisible";
|
2021-11-23 16:46:58 +00:00
|
|
|
const std::string_view DEFAULT_BAR_ID = "bar-0";
|
2021-11-20 03:29:51 +00:00
|
|
|
|
2021-11-28 20:19:45 +00:00
|
|
|
/* Deserializer for enum bar_layer */
|
2024-05-19 11:53:09 +00:00
|
|
|
void from_json(const Json::Value& j, std::optional<bar_layer>& l) {
|
2021-11-28 20:19:45 +00:00
|
|
|
if (j == "bottom") {
|
|
|
|
l = bar_layer::BOTTOM;
|
|
|
|
} else if (j == "top") {
|
|
|
|
l = bar_layer::TOP;
|
|
|
|
} else if (j == "overlay") {
|
|
|
|
l = bar_layer::OVERLAY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deserializer for struct bar_mode */
|
|
|
|
void from_json(const Json::Value& j, bar_mode& m) {
|
|
|
|
if (j.isObject()) {
|
|
|
|
if (auto v = j["layer"]; v.isString()) {
|
|
|
|
from_json(v, m.layer);
|
|
|
|
}
|
|
|
|
if (auto v = j["exclusive"]; v.isBool()) {
|
|
|
|
m.exclusive = v.asBool();
|
|
|
|
}
|
|
|
|
if (auto v = j["passthrough"]; v.isBool()) {
|
|
|
|
m.passthrough = v.asBool();
|
|
|
|
}
|
|
|
|
if (auto v = j["visible"]; v.isBool()) {
|
|
|
|
m.visible = v.asBool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 03:14:39 +00:00
|
|
|
/* Deserializer for enum Gtk::PositionType */
|
|
|
|
void from_json(const Json::Value& j, Gtk::PositionType& pos) {
|
|
|
|
if (j == "left") {
|
|
|
|
pos = Gtk::POS_LEFT;
|
|
|
|
} else if (j == "right") {
|
|
|
|
pos = Gtk::POS_RIGHT;
|
|
|
|
} else if (j == "top") {
|
|
|
|
pos = Gtk::POS_TOP;
|
|
|
|
} else if (j == "bottom") {
|
|
|
|
pos = Gtk::POS_BOTTOM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Glib::ustring to_string(Gtk::PositionType pos) {
|
|
|
|
switch (pos) {
|
|
|
|
case Gtk::POS_LEFT:
|
|
|
|
return "left";
|
|
|
|
case Gtk::POS_RIGHT:
|
|
|
|
return "right";
|
|
|
|
case Gtk::POS_TOP:
|
|
|
|
return "top";
|
|
|
|
case Gtk::POS_BOTTOM:
|
|
|
|
return "bottom";
|
|
|
|
}
|
2024-02-18 11:01:36 +00:00
|
|
|
throw std::runtime_error("Invalid Gtk::PositionType");
|
2024-02-15 03:14:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-28 20:19:45 +00:00
|
|
|
/* Deserializer for JSON Object -> map<string compatible type, Value>
|
|
|
|
* Assumes that all the values in the object are deserializable to the same type.
|
|
|
|
*/
|
|
|
|
template <typename Key, typename Value,
|
2024-02-19 09:50:40 +00:00
|
|
|
typename = std::enable_if_t<std::is_convertible<std::string, Key>::value>>
|
2021-11-28 20:19:45 +00:00
|
|
|
void from_json(const Json::Value& j, std::map<Key, Value>& m) {
|
|
|
|
if (j.isObject()) {
|
|
|
|
for (auto it = j.begin(); it != j.end(); ++it) {
|
|
|
|
from_json(*it, m[it.key().asString()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
}; // namespace waybar
|
|
|
|
|
2019-04-25 11:25:06 +00:00
|
|
|
waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
2019-04-18 15:43:16 +00:00
|
|
|
: output(w_output),
|
2019-04-25 11:25:06 +00:00
|
|
|
config(w_config),
|
2020-05-27 07:10:38 +00:00
|
|
|
window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
2023-08-21 19:03:20 +00:00
|
|
|
x_global(0),
|
|
|
|
y_global(0),
|
|
|
|
margins_{.top = 0, .right = 0, .bottom = 0, .left = 0},
|
2019-04-18 15:43:16 +00:00
|
|
|
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");
|
2018-10-04 16:47:06 +00:00
|
|
|
window.set_name("waybar");
|
2018-10-29 20:52:53 +00:00
|
|
|
window.set_decorated(false);
|
2019-06-08 18:04:34 +00:00
|
|
|
window.get_style_context()->add_class(output->name);
|
2019-07-04 01:09:31 +00:00
|
|
|
window.get_style_context()->add_class(config["name"].asString());
|
2019-04-18 15:43:16 +00:00
|
|
|
|
2024-02-15 03:14:39 +00:00
|
|
|
from_json(config["position"], position);
|
|
|
|
orientation = (position == Gtk::POS_LEFT || position == Gtk::POS_RIGHT)
|
|
|
|
? Gtk::ORIENTATION_VERTICAL
|
|
|
|
: Gtk::ORIENTATION_HORIZONTAL;
|
2018-08-19 11:39:57 +00:00
|
|
|
|
2024-02-15 03:14:39 +00:00
|
|
|
window.get_style_context()->add_class(to_string(position));
|
|
|
|
|
|
|
|
left_ = Gtk::Box(orientation, 0);
|
|
|
|
center_ = Gtk::Box(orientation, 0);
|
|
|
|
right_ = Gtk::Box(orientation, 0);
|
|
|
|
box_ = Gtk::Box(orientation, 0);
|
2021-01-17 09:05:18 +00:00
|
|
|
|
2020-12-28 20:44:16 +00: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 09:59:04 +00:00
|
|
|
|
2021-10-20 09:11:49 +00:00
|
|
|
if (config["spacing"].isInt()) {
|
|
|
|
int spacing = config["spacing"].asInt();
|
|
|
|
left_.set_spacing(spacing);
|
|
|
|
center_.set_spacing(spacing);
|
|
|
|
right_.set_spacing(spacing);
|
|
|
|
}
|
|
|
|
|
2024-02-19 10:55:55 +00:00
|
|
|
height_ = config["height"].isUInt() ? config["height"].asUInt() : 0;
|
|
|
|
width_ = config["width"].isUInt() ? config["width"].asUInt() : 0;
|
2020-10-22 05:16:12 +00:00
|
|
|
|
2019-08-23 06:29:18 +00: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()) {
|
2022-04-06 06:37:19 +00:00
|
|
|
std::istringstream iss(config["margin"].asString());
|
2019-08-23 06:29:18 +00:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:03:20 +00:00
|
|
|
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Bar::onConfigure));
|
|
|
|
output->monitor->property_geometry().signal_changed().connect(
|
|
|
|
sigc::mem_fun(*this, &Bar::onOutputGeometryChanged));
|
|
|
|
|
2024-02-19 10:55:55 +00:00
|
|
|
// this has to be executed before GtkWindow.realize
|
|
|
|
auto* gtk_window = window.gobj();
|
|
|
|
gtk_layer_init_for_window(gtk_window);
|
|
|
|
gtk_layer_set_keyboard_mode(gtk_window, GTK_LAYER_SHELL_KEYBOARD_MODE_NONE);
|
|
|
|
gtk_layer_set_monitor(gtk_window, output->monitor->gobj());
|
|
|
|
gtk_layer_set_namespace(gtk_window, "waybar");
|
|
|
|
|
|
|
|
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, margins_.left);
|
|
|
|
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, margins_.right);
|
|
|
|
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_TOP, margins_.top);
|
|
|
|
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, margins_.bottom);
|
|
|
|
|
|
|
|
window.set_size_request(width_, height_);
|
2018-08-19 11:39:57 +00:00
|
|
|
|
2022-04-17 22:19:36 +00:00
|
|
|
// Position needs to be set after calculating the height due to the
|
|
|
|
// GTK layer shell anchors logic relying on the dimensions of the bar.
|
2024-02-19 10:55:55 +00:00
|
|
|
setPosition(position);
|
2018-10-29 20:52:53 +00:00
|
|
|
|
2021-11-28 20:19:45 +00:00
|
|
|
/* Read custom modes if available */
|
|
|
|
if (auto modes = config.get("modes", {}); modes.isObject()) {
|
|
|
|
from_json(modes, configured_modes);
|
2021-11-20 03:31:41 +00:00
|
|
|
}
|
2021-09-15 15:35:50 +00:00
|
|
|
|
2021-11-28 20:19:45 +00:00
|
|
|
/* Update "default" mode with the global bar options */
|
|
|
|
from_json(config, configured_modes[MODE_DEFAULT]);
|
2021-09-15 15:35:50 +00:00
|
|
|
|
2021-11-28 20:19:45 +00:00
|
|
|
if (auto mode = config.get("mode", {}); mode.isString()) {
|
2021-11-20 03:31:41 +00:00
|
|
|
setMode(config["mode"].asString());
|
|
|
|
} else {
|
|
|
|
setMode(MODE_DEFAULT);
|
2021-09-15 15:35:50 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 11:01:00 +00:00
|
|
|
if (config["start_hidden"].asBool()) {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
|
2020-08-10 01:58:01 +00:00
|
|
|
|
2020-10-20 02:34:48 +00:00
|
|
|
#if HAVE_SWAY
|
|
|
|
if (auto ipc = config["ipc"]; ipc.isBool() && ipc.asBool()) {
|
|
|
|
bar_id = Client::inst()->bar_id;
|
|
|
|
if (auto id = config["id"]; id.isString()) {
|
|
|
|
bar_id = id.asString();
|
|
|
|
}
|
2021-11-23 16:46:58 +00:00
|
|
|
if (bar_id.empty()) {
|
|
|
|
bar_id = DEFAULT_BAR_ID;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
_ipc_client = std::make_unique<BarIpcClient>(*this);
|
|
|
|
} catch (const std::exception& exc) {
|
|
|
|
spdlog::warn("Failed to open bar ipc connection: {}", exc.what());
|
|
|
|
}
|
2020-10-20 02:34:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
setupWidgets();
|
|
|
|
window.show_all();
|
2020-11-30 20:05:18 +00: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 01:58:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 02:34:48 +00:00
|
|
|
/* Need to define it here because of forward declared members */
|
|
|
|
waybar::Bar::~Bar() = default;
|
|
|
|
|
2024-02-19 09:50:40 +00:00
|
|
|
void waybar::Bar::setMode(const std::string& mode) {
|
2021-11-20 04:02:57 +00:00
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
|
|
|
auto style = window.get_style_context();
|
|
|
|
/* remove styles added by previous setMode calls */
|
|
|
|
style->remove_class("mode-"s + last_mode_);
|
|
|
|
|
2021-11-20 03:29:51 +00:00
|
|
|
auto it = configured_modes.find(mode);
|
|
|
|
if (it != configured_modes.end()) {
|
|
|
|
last_mode_ = mode;
|
2021-11-20 04:02:57 +00:00
|
|
|
style->add_class("mode-"s + last_mode_);
|
2021-11-20 03:29:51 +00:00
|
|
|
setMode(it->second);
|
|
|
|
} else {
|
|
|
|
spdlog::warn("Unknown mode \"{}\" requested", mode);
|
|
|
|
last_mode_ = MODE_DEFAULT;
|
2021-11-20 04:02:57 +00:00
|
|
|
style->add_class("mode-"s + last_mode_);
|
2021-11-20 03:29:51 +00:00
|
|
|
setMode(configured_modes.at(MODE_DEFAULT));
|
2021-09-15 15:35:50 +00:00
|
|
|
}
|
2021-11-20 03:29:51 +00:00
|
|
|
}
|
2021-09-15 15:35:50 +00:00
|
|
|
|
2021-11-20 03:29:51 +00:00
|
|
|
void waybar::Bar::setMode(const struct bar_mode& mode) {
|
2024-02-19 10:55:55 +00:00
|
|
|
auto* gtk_window = window.gobj();
|
|
|
|
|
2024-05-19 11:53:09 +00:00
|
|
|
if (mode.layer == bar_layer::BOTTOM) {
|
|
|
|
gtk_layer_set_layer(gtk_window, GTK_LAYER_SHELL_LAYER_BOTTOM);
|
|
|
|
} else if (mode.layer == bar_layer::TOP) {
|
|
|
|
gtk_layer_set_layer(gtk_window, GTK_LAYER_SHELL_LAYER_TOP);
|
2024-02-19 10:55:55 +00:00
|
|
|
} else if (mode.layer == bar_layer::OVERLAY) {
|
2024-05-19 11:53:09 +00:00
|
|
|
gtk_layer_set_layer(gtk_window, GTK_LAYER_SHELL_LAYER_OVERLAY);
|
2024-02-19 10:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode.exclusive) {
|
|
|
|
gtk_layer_auto_exclusive_zone_enable(gtk_window);
|
|
|
|
} else {
|
|
|
|
gtk_layer_set_exclusive_zone(gtk_window, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPassThrough(passthrough_ = mode.passthrough);
|
2021-11-21 19:00:57 +00:00
|
|
|
|
|
|
|
if (mode.visible) {
|
|
|
|
window.get_style_context()->remove_class("hidden");
|
|
|
|
window.set_opacity(1);
|
|
|
|
} else {
|
|
|
|
window.get_style_context()->add_class("hidden");
|
|
|
|
window.set_opacity(0);
|
|
|
|
}
|
2024-02-19 10:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Bar::setPassThrough(bool passthrough) {
|
|
|
|
auto gdk_window = window.get_window();
|
|
|
|
if (gdk_window) {
|
|
|
|
Cairo::RefPtr<Cairo::Region> region;
|
|
|
|
if (passthrough) {
|
|
|
|
region = Cairo::Region::create();
|
|
|
|
}
|
|
|
|
gdk_window->input_shape_combine_region(region, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Bar::setPosition(Gtk::PositionType position) {
|
|
|
|
std::array<gboolean, GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER> anchors;
|
|
|
|
anchors.fill(TRUE);
|
|
|
|
|
|
|
|
auto orientation = (position == Gtk::POS_LEFT || position == Gtk::POS_RIGHT)
|
|
|
|
? Gtk::ORIENTATION_VERTICAL
|
|
|
|
: Gtk::ORIENTATION_HORIZONTAL;
|
|
|
|
|
|
|
|
switch (position) {
|
|
|
|
case Gtk::POS_LEFT:
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_RIGHT] = FALSE;
|
|
|
|
break;
|
|
|
|
case Gtk::POS_RIGHT:
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_LEFT] = FALSE;
|
|
|
|
break;
|
|
|
|
case Gtk::POS_BOTTOM:
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_TOP] = FALSE;
|
|
|
|
break;
|
|
|
|
default: /* Gtk::POS_TOP */
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_BOTTOM] = FALSE;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
// Disable anchoring for other edges too if the width
|
|
|
|
// or the height has been set to a value other than 'auto'
|
|
|
|
// otherwise the bar will use all space
|
|
|
|
uint32_t configured_width = config["width"].isUInt() ? config["width"].asUInt() : 0;
|
|
|
|
uint32_t configured_height = config["height"].isUInt() ? config["height"].asUInt() : 0;
|
|
|
|
if (orientation == Gtk::ORIENTATION_VERTICAL && configured_height > 1) {
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_TOP] = FALSE;
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_BOTTOM] = FALSE;
|
|
|
|
} else if (orientation == Gtk::ORIENTATION_HORIZONTAL && configured_width > 1) {
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_LEFT] = FALSE;
|
|
|
|
anchors[GTK_LAYER_SHELL_EDGE_RIGHT] = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, anchors[edge]);
|
|
|
|
}
|
2021-09-15 15:35:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
void waybar::Bar::onMap(GdkEventAny*) {
|
2020-08-10 01:58:01 +00:00
|
|
|
/*
|
|
|
|
* Obtain a pointer to the custom layer surface for modules that require it (idle_inhibitor).
|
|
|
|
*/
|
2019-05-25 15:50:45 +00:00
|
|
|
auto gdk_window = window.get_window()->gobj();
|
|
|
|
surface = gdk_wayland_window_get_wl_surface(gdk_window);
|
2023-08-21 19:03:20 +00:00
|
|
|
configureGlobalOffset(gdk_window_get_width(gdk_window), gdk_window_get_height(gdk_window));
|
2024-02-19 10:55:55 +00:00
|
|
|
|
|
|
|
setPassThrough(passthrough_);
|
2018-08-09 18:22:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
void waybar::Bar::setVisible(bool value) {
|
|
|
|
visible = value;
|
2022-03-12 20:32:08 +00:00
|
|
|
if (auto mode = config.get("mode", {}); mode.isString()) {
|
|
|
|
setMode(visible ? config["mode"].asString() : MODE_INVISIBLE);
|
|
|
|
} else {
|
|
|
|
setMode(visible ? MODE_DEFAULT : MODE_INVISIBLE);
|
|
|
|
}
|
2019-05-09 13:10:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 05:16:12 +00:00
|
|
|
void waybar::Bar::toggle() { setVisible(!visible); }
|
2019-09-01 07:54:53 +00:00
|
|
|
|
2019-04-18 15:43:16 +00: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 11:25:06 +00:00
|
|
|
if (config.isMember(module_name)) {
|
|
|
|
Json::Value& module = config[module_name];
|
2019-04-18 15:43:16 +00: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 08:30:54 +00:00
|
|
|
if (click == "click-right") {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 3U;
|
2019-05-09 08:30:54 +00:00
|
|
|
} else if (click == "click-middle") {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 2U;
|
2019-05-09 08:30:54 +00:00
|
|
|
} else if (click == "click-backward") {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 8U;
|
2019-05-09 08:30:54 +00:00
|
|
|
} else if (click == "click-forward") {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 9U;
|
2019-04-18 15:43:16 +00:00
|
|
|
} else {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 1U; // default click-left
|
2019-04-18 15:43:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 1U;
|
2019-04-18 15:43:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-24 10:37:24 +00:00
|
|
|
module["format-alt-click"] = 1U;
|
2019-02-01 20:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
void waybar::Bar::setupAltFormatKeyForModuleList(const char* module_list_name) {
|
2019-04-25 11:25:06 +00:00
|
|
|
if (config.isMember(module_list_name)) {
|
|
|
|
Json::Value& modules = config[module_list_name];
|
2019-04-18 15:43:16 +00:00
|
|
|
for (const Json::Value& module_name : modules) {
|
|
|
|
if (module_name.isString()) {
|
2024-03-23 17:39:22 +00:00
|
|
|
auto ref = module_name.asString();
|
|
|
|
if (ref.compare(0, 6, "group/") == 0 && ref.size() > 6) {
|
|
|
|
Json::Value& group_modules = config[ref]["modules"];
|
|
|
|
for (const Json::Value& module_name : group_modules) {
|
|
|
|
if (module_name.isString()) {
|
|
|
|
setupAltFormatKeyForModule(module_name.asString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setupAltFormatKeyForModule(ref);
|
|
|
|
}
|
2019-02-01 20:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-10 22:32:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
void waybar::Bar::handleSignal(int signal) {
|
2021-10-31 22:55:13 +00:00
|
|
|
for (auto& module : modules_all_) {
|
2023-01-22 00:57:28 +00:00
|
|
|
module->refresh(signal);
|
2019-03-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
|
2023-10-14 16:08:44 +00:00
|
|
|
waybar::Group* group = nullptr) {
|
2021-10-31 22:55:13 +00:00
|
|
|
auto module_list = group ? config[pos]["modules"] : config[pos];
|
|
|
|
if (module_list.isArray()) {
|
|
|
|
for (const auto& name : module_list) {
|
2018-08-19 11:39:57 +00:00
|
|
|
try {
|
2021-10-31 22:55:13 +00:00
|
|
|
auto ref = name.asString();
|
|
|
|
AModule* module;
|
|
|
|
|
|
|
|
if (ref.compare(0, 6, "group/") == 0 && ref.size() > 6) {
|
2022-06-13 19:17:17 +00:00
|
|
|
auto hash_pos = ref.find('#');
|
|
|
|
auto id_name = ref.substr(6, hash_pos - 6);
|
|
|
|
auto class_name = hash_pos != std::string::npos ? ref.substr(hash_pos + 1) : "";
|
|
|
|
|
2023-10-14 20:17:19 +00:00
|
|
|
auto vertical = (group ? group->getBox().get_orientation() : box_.get_orientation()) ==
|
|
|
|
Gtk::ORIENTATION_VERTICAL;
|
2023-10-14 16:08:44 +00:00
|
|
|
|
2022-06-13 19:17:17 +00:00
|
|
|
auto group_module = new waybar::Group(id_name, class_name, config[ref], vertical);
|
2023-10-14 16:08:44 +00:00
|
|
|
getModules(factory, ref, group_module);
|
2021-10-31 22:55:13 +00:00
|
|
|
module = group_module;
|
|
|
|
} else {
|
2023-10-26 21:08:57 +00:00
|
|
|
module = factory.makeModule(ref, pos);
|
2018-08-19 11:39:57 +00:00
|
|
|
}
|
2021-10-31 22:55:13 +00:00
|
|
|
|
2021-12-03 22:56:51 +00:00
|
|
|
std::shared_ptr<AModule> module_sp(module);
|
|
|
|
modules_all_.emplace_back(module_sp);
|
2021-10-31 22:55:13 +00:00
|
|
|
if (group) {
|
2023-10-14 20:17:19 +00:00
|
|
|
group->addWidget(*module);
|
2021-10-31 22:55:13 +00:00
|
|
|
} else {
|
|
|
|
if (pos == "modules-left") {
|
2021-12-03 22:56:51 +00:00
|
|
|
modules_left_.emplace_back(module_sp);
|
2021-10-31 22:55:13 +00:00
|
|
|
}
|
|
|
|
if (pos == "modules-center") {
|
2021-12-03 22:56:51 +00:00
|
|
|
modules_center_.emplace_back(module_sp);
|
2021-10-31 22:55:13 +00:00
|
|
|
}
|
|
|
|
if (pos == "modules-right") {
|
2021-12-03 22:56:51 +00:00
|
|
|
modules_right_.emplace_back(module_sp);
|
2021-10-31 22:55:13 +00:00
|
|
|
}
|
2018-08-19 11:39:57 +00:00
|
|
|
}
|
2022-06-13 19:17:17 +00:00
|
|
|
module->dp.connect([module, ref] {
|
2019-06-16 13:14:31 +00:00
|
|
|
try {
|
|
|
|
module->update();
|
|
|
|
} catch (const std::exception& e) {
|
2022-06-13 19:17:17 +00:00
|
|
|
spdlog::error("{}: {}", ref, e.what());
|
2019-06-16 13:14:31 +00:00
|
|
|
}
|
2018-11-24 10:13:52 +00:00
|
|
|
});
|
2018-08-19 11:39:57 +00:00
|
|
|
} catch (const std::exception& e) {
|
2019-05-18 23:44:45 +00:00
|
|
|
spdlog::warn("module {}: {}", name.asString(), e.what());
|
2018-08-19 11:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:43:16 +00:00
|
|
|
auto waybar::Bar::setupWidgets() -> void {
|
2018-12-26 10:13:36 +00:00
|
|
|
window.add(box_);
|
2019-06-28 12:16:09 +00:00
|
|
|
box_.pack_start(left_, false, false);
|
2021-12-28 12:57:10 +00:00
|
|
|
if (config["fixed-center"].isBool() ? config["fixed-center"].asBool() : true) {
|
|
|
|
box_.set_center_widget(center_);
|
|
|
|
} else {
|
|
|
|
box_.pack_start(center_, true, false);
|
|
|
|
}
|
2019-06-28 12:16:09 +00:00
|
|
|
box_.pack_end(right_, false, false);
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2019-04-19 09:56:40 +00:00
|
|
|
// Convert to button code for every module that is used.
|
|
|
|
setupAltFormatKeyForModuleList("modules-left");
|
|
|
|
setupAltFormatKeyForModuleList("modules-right");
|
|
|
|
setupAltFormatKeyForModuleList("modules-center");
|
|
|
|
|
2019-04-25 11:25:06 +00:00
|
|
|
Factory factory(*this, 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_) {
|
2019-06-28 12:16:09 +00:00
|
|
|
left_.pack_start(*module, false, false);
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|
2018-09-04 21:50:08 +00:00
|
|
|
for (auto const& module : modules_center_) {
|
2019-06-28 12:16:09 +00:00
|
|
|
center_.pack_start(*module, false, false);
|
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_) {
|
2019-06-28 12:16:09 +00:00
|
|
|
right_.pack_end(*module, false, false);
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
}
|
2023-08-21 19:03:20 +00:00
|
|
|
|
|
|
|
void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
|
2024-02-19 10:55:55 +00: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 (orientation == Gtk::ORIENTATION_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;
|
|
|
|
|
2023-08-21 19:03:20 +00:00
|
|
|
configureGlobalOffset(ev->width, ev->height);
|
2024-02-19 10:55:55 +00:00
|
|
|
spdlog::info(BAR_SIZE_MSG, ev->width, ev->height, output->name);
|
2023-08-21 19:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Bar::configureGlobalOffset(int width, int height) {
|
|
|
|
auto monitor_geometry = *output->monitor->property_geometry().get_value().gobj();
|
|
|
|
int x;
|
|
|
|
int y;
|
2024-02-15 03:14:39 +00:00
|
|
|
switch (position) {
|
|
|
|
case Gtk::POS_BOTTOM:
|
|
|
|
if (width + margins_.left + margins_.right >= monitor_geometry.width)
|
|
|
|
x = margins_.left;
|
|
|
|
else
|
|
|
|
x = (monitor_geometry.width - width) / 2;
|
|
|
|
y = monitor_geometry.height - height - margins_.bottom;
|
|
|
|
break;
|
|
|
|
case Gtk::POS_LEFT:
|
2023-08-21 19:03:20 +00:00
|
|
|
x = margins_.left;
|
2024-02-15 03:14:39 +00:00
|
|
|
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
|
|
|
|
y = margins_.top;
|
|
|
|
else
|
|
|
|
y = (monitor_geometry.height - height) / 2;
|
|
|
|
break;
|
|
|
|
case Gtk::POS_RIGHT:
|
|
|
|
x = monitor_geometry.width - width - margins_.right;
|
|
|
|
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
|
|
|
|
y = margins_.top;
|
|
|
|
else
|
|
|
|
y = (monitor_geometry.height - height) / 2;
|
|
|
|
break;
|
2024-02-19 10:55:55 +00:00
|
|
|
default: /* Gtk::POS_TOP */
|
2024-02-15 03:14:39 +00:00
|
|
|
if (width + margins_.left + margins_.right >= monitor_geometry.width)
|
|
|
|
x = margins_.left;
|
|
|
|
else
|
|
|
|
x = (monitor_geometry.width - width) / 2;
|
2023-08-21 19:03:20 +00:00
|
|
|
y = margins_.top;
|
2024-02-15 03:14:39 +00:00
|
|
|
break;
|
2023-08-21 19:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x_global = x + monitor_geometry.x;
|
|
|
|
y_global = y + monitor_geometry.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Bar::onOutputGeometryChanged() {
|
|
|
|
configureGlobalOffset(window.get_width(), window.get_height());
|
|
|
|
}
|