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"
|
2018-08-13 21:23:43 +02:00
|
|
|
#include "util/json.hpp"
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2018-08-20 14:50:45 +02:00
|
|
|
waybar::Bar::Bar(const Client& client,
|
2018-08-19 13:39:57 +02:00
|
|
|
std::unique_ptr<struct wl_output *> &&p_output, uint32_t p_wl_name)
|
2018-08-09 20:22:01 +02:00
|
|
|
: client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
2018-08-20 14:50:45 +02:00
|
|
|
surface(nullptr), layer_surface(nullptr),
|
2018-12-26 11:13:36 +01:00
|
|
|
output(std::move(p_output)), wl_name(p_wl_name),
|
|
|
|
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
|
|
|
{
|
2018-08-11 00:32:59 +02:00
|
|
|
static const struct zxdg_output_v1_listener xdgOutputListener = {
|
2018-08-16 14:29:41 +02:00
|
|
|
.logical_position = handleLogicalPosition,
|
|
|
|
.logical_size = handleLogicalSize,
|
|
|
|
.done = handleDone,
|
|
|
|
.name = handleName,
|
|
|
|
.description = handleDescription,
|
2018-08-11 00:32:59 +02:00
|
|
|
};
|
2018-08-16 14:29:41 +02:00
|
|
|
xdg_output_ =
|
|
|
|
zxdg_output_manager_v1_get_xdg_output(client.xdg_output_manager, *output);
|
|
|
|
zxdg_output_v1_add_listener(xdg_output_, &xdgOutputListener, this);
|
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);
|
2018-10-25 10:43:37 +02:00
|
|
|
window.set_resizable(false);
|
2018-08-16 14:29:41 +02:00
|
|
|
setupConfig();
|
|
|
|
setupCss();
|
2018-08-19 13:39:57 +02:00
|
|
|
|
2018-10-29 21:52:53 +01:00
|
|
|
auto wrap = reinterpret_cast<GtkWidget*>(window.gobj());
|
|
|
|
gtk_widget_realize(wrap);
|
|
|
|
GdkWindow *gdk_window = gtk_widget_get_window(wrap);
|
2018-08-16 14:29:41 +02:00
|
|
|
gdk_wayland_window_set_use_custom_surface(gdk_window);
|
|
|
|
surface = gdk_wayland_window_get_wl_surface(gdk_window);
|
2019-02-01 21:45:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::Bar::initBar()
|
|
|
|
{
|
|
|
|
std::size_t layer_top = config_["layer"] == "top"
|
2018-08-16 14:29:41 +02:00
|
|
|
? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
|
|
|
|
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
|
|
|
|
client.layer_shell, surface, *output, layer_top, "waybar");
|
2018-08-19 13:39:57 +02:00
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
|
|
|
|
.configure = layerSurfaceHandleConfigure,
|
|
|
|
.closed = layerSurfaceHandleClosed,
|
2018-08-09 20:22:01 +02:00
|
|
|
};
|
2019-02-01 21:45:59 +01:00
|
|
|
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
|
2018-08-19 13:39:57 +02:00
|
|
|
|
|
|
|
std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
|
|
|
|
if (config_["position"] == "bottom") {
|
|
|
|
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
|
|
|
|
} else {
|
|
|
|
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:27:16 +02:00
|
|
|
auto height = config_["height"].isUInt() ? config_["height"].asUInt() : height_;
|
|
|
|
auto width = config_["width"].isUInt() ? config_["width"].asUInt() : width_;
|
2018-08-19 13:39:57 +02:00
|
|
|
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor);
|
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height);
|
|
|
|
zwlr_layer_surface_v1_set_size(layer_surface, width, height);
|
|
|
|
|
2018-08-09 20:22:01 +02:00
|
|
|
wl_surface_commit(surface);
|
2018-10-29 21:52:53 +01:00
|
|
|
|
|
|
|
setupWidgets();
|
2018-08-09 20:22:01 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::handleLogicalPosition(void* /*data*/,
|
|
|
|
struct zxdg_output_v1* /*zxdg_output_v1*/, int32_t /*x*/, int32_t /*y*/)
|
2018-08-11 00:32:59 +02:00
|
|
|
{
|
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::handleLogicalSize(void* /*data*/,
|
|
|
|
struct zxdg_output_v1* /*zxdg_output_v1*/, int32_t /*width*/,
|
|
|
|
int32_t /*height*/)
|
2018-08-11 00:32:59 +02:00
|
|
|
{
|
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::handleDone(void* /*data*/,
|
|
|
|
struct zxdg_output_v1* /*zxdg_output_v1*/)
|
2018-08-11 00:32:59 +02:00
|
|
|
{
|
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2019-02-01 21:45:59 +01:00
|
|
|
bool waybar::Bar::isValidOutput(const Json::Value &config)
|
|
|
|
{
|
|
|
|
bool found = true;
|
|
|
|
if (config["output"].isArray()) {
|
|
|
|
bool in_array = false;
|
|
|
|
for (auto const &output : config["output"]) {
|
|
|
|
if (output.isString() && output.asString() == output_name) {
|
|
|
|
in_array = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found = in_array;
|
|
|
|
}
|
|
|
|
if (config["output"].isString() && config["output"].asString() != output_name) {
|
|
|
|
found = false;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::handleName(void* data, struct zxdg_output_v1* /*xdg_output*/,
|
|
|
|
const char* name)
|
2018-08-11 00:32:59 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
auto o = static_cast<waybar::Bar *>(data);
|
2018-08-19 13:39:57 +02:00
|
|
|
o->output_name = name;
|
2019-02-01 21:45:59 +01:00
|
|
|
bool found = true;
|
|
|
|
if (o->config_.isArray()) {
|
|
|
|
bool in_array = false;
|
|
|
|
for (auto const &config : o->config_) {
|
|
|
|
if (config.isObject() && o->isValidOutput(config)) {
|
|
|
|
in_array = true;
|
|
|
|
o->config_ = config;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
found = in_array;
|
|
|
|
} else {
|
|
|
|
found = o->isValidOutput(o->config_);
|
|
|
|
}
|
|
|
|
if (!found) {
|
2019-02-04 22:09:01 +01:00
|
|
|
wl_output_destroy(*o->output);
|
|
|
|
zxdg_output_v1_destroy(o->xdg_output_);
|
2019-02-01 21:45:59 +01:00
|
|
|
} else {
|
|
|
|
o->initBar();
|
|
|
|
}
|
2018-08-11 00:32:59 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::handleDescription(void* /*data*/,
|
|
|
|
struct zxdg_output_v1* /*zxdg_output_v1*/, const char* /*description*/)
|
2018-08-11 00:32:59 +02:00
|
|
|
{
|
|
|
|
// Nothing here
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::layerSurfaceHandleConfigure(void* data,
|
|
|
|
struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width,
|
|
|
|
uint32_t height)
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
auto o = static_cast<waybar::Bar *>(data);
|
2018-08-08 23:54:58 +02:00
|
|
|
zwlr_layer_surface_v1_ack_configure(surface, serial);
|
2018-08-19 13:39:57 +02:00
|
|
|
if (width != o->width_ || height != o->height_) {
|
|
|
|
o->width_ = width;
|
|
|
|
o->height_ = height;
|
2018-11-01 22:00:38 +01:00
|
|
|
o->window.set_size_request(o->width_, o->height_);
|
|
|
|
o->window.resize(o->width_, o->height_);
|
|
|
|
|
|
|
|
int dummy_width, min_height;
|
|
|
|
o->window.get_size(dummy_width, min_height);
|
2018-11-02 12:35:26 +01:00
|
|
|
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 22:00:38 +01:00
|
|
|
o->height_ = min_height;
|
|
|
|
}
|
2018-08-19 13:39:57 +02:00
|
|
|
std::cout << fmt::format(
|
|
|
|
"Bar configured (width: {}, height: {}) for output: {}",
|
|
|
|
o->width_, o->height_, o->output_name) << std::endl;
|
2018-11-01 22:00:38 +01:00
|
|
|
|
2018-08-08 23:54:58 +02:00
|
|
|
wl_surface_commit(o->surface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
void waybar::Bar::layerSurfaceHandleClosed(void* data,
|
|
|
|
struct zwlr_layer_surface_v1* /*surface*/)
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
auto o = static_cast<waybar::Bar *>(data);
|
2018-08-19 13:39:57 +02:00
|
|
|
std::cout << "Bar removed from output: " + o->output_name << std::endl;
|
2018-08-16 14:29:41 +02:00
|
|
|
zwlr_layer_surface_v1_destroy(o->layer_surface);
|
2018-08-19 13:39:57 +02:00
|
|
|
wl_output_destroy(*o->output);
|
|
|
|
zxdg_output_v1_destroy(o->xdg_output_);
|
|
|
|
o->modules_left_.clear();
|
|
|
|
o->modules_center_.clear();
|
|
|
|
o->modules_right_.clear();
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::Bar::toggle() -> void
|
|
|
|
{
|
|
|
|
visible = !visible;
|
2018-08-16 14:29:41 +02:00
|
|
|
auto zone = visible ? height_ : 0;
|
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
|
2018-08-08 23:54:58 +02:00
|
|
|
wl_surface_commit(surface);
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
auto waybar::Bar::setupConfig() -> void
|
2018-08-09 12:05:48 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
std::ifstream file(client.config_file);
|
|
|
|
if (!file.is_open()) {
|
2018-08-09 12:05:48 +02:00
|
|
|
throw std::runtime_error("Can't open config file");
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
std::string str((std::istreambuf_iterator<char>(file)),
|
|
|
|
std::istreambuf_iterator<char>());
|
2018-08-20 14:50:45 +02:00
|
|
|
util::JsonParser parser;
|
2018-08-16 14:29:41 +02:00
|
|
|
config_ = parser.parse(str);
|
2019-03-02 20:07:12 +01:00
|
|
|
|
|
|
|
// Converting string to button code rn as to avoid doing it later
|
|
|
|
auto setupAltFormatKeyForModule = [this](const std::string& module_name){
|
|
|
|
if (config_.isMember(module_name)) {
|
|
|
|
Json::Value& module = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto setupAltFormatKeyForModuleList = [this, &setupAltFormatKeyForModule](const char* module_list_name) {
|
|
|
|
if (config_.isMember(module_list_name)) {
|
|
|
|
Json::Value& modules = config_[module_list_name];
|
|
|
|
for (const Json::Value& module_name : modules) {
|
|
|
|
if (module_name.isString()) {
|
|
|
|
setupAltFormatKeyForModule(module_name.asString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert to button code for every module that is used.
|
|
|
|
setupAltFormatKeyForModuleList("modules-left");
|
|
|
|
setupAltFormatKeyForModuleList("modules-right");
|
|
|
|
setupAltFormatKeyForModuleList("modules-center");
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
auto waybar::Bar::setupCss() -> void
|
2018-08-09 12:05:48 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
css_provider_ = Gtk::CssProvider::create();
|
|
|
|
style_context_ = Gtk::StyleContext::create();
|
2018-08-09 12:05:48 +02:00
|
|
|
|
2018-08-20 14:50:45 +02:00
|
|
|
// Load our css file, wherever that may be hiding
|
2018-08-16 14:29:41 +02:00
|
|
|
if (css_provider_->load_from_path(client.css_file)) {
|
2018-08-09 12:05:48 +02:00
|
|
|
Glib::RefPtr<Gdk::Screen> screen = window.get_screen();
|
2018-08-16 14:29:41 +02:00
|
|
|
style_context_->add_provider_for_screen(screen, css_provider_,
|
2018-08-09 12:05:48 +02:00
|
|
|
GTK_STYLE_PROVIDER_PRIORITY_USER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 14:50:45 +02:00
|
|
|
void waybar::Bar::getModules(const Factory& factory, const std::string& pos)
|
2018-08-19 13:39:57 +02:00
|
|
|
{
|
2018-10-26 09:27:16 +02:00
|
|
|
if (config_[pos].isArray()) {
|
2018-08-19 13:39:57 +02:00
|
|
|
for (const auto &name : config_[pos]) {
|
|
|
|
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] {
|
2018-11-24 11:13:52 +01:00
|
|
|
try {
|
|
|
|
module->update();
|
|
|
|
} catch (const std::exception& e) {
|
2018-11-24 11:20:03 +01:00
|
|
|
std::cerr << name.asString() + ": " + e.what() << std::endl;
|
2018-11-24 11:13:52 +01:00
|
|
|
}
|
|
|
|
});
|
2018-08-19 13:39:57 +02:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
auto waybar::Bar::setupWidgets() -> void
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
2018-12-26 11:13:36 +01:00
|
|
|
window.add(box_);
|
|
|
|
box_.pack_start(left_, true, true);
|
|
|
|
box_.set_center_widget(center_);
|
|
|
|
box_.pack_end(right_, true, true);
|
2018-08-08 23:54:58 +02:00
|
|
|
|
2018-08-16 14:29:41 +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_) {
|
2018-12-26 11:13:36 +01:00
|
|
|
left_.pack_start(*module, false, true, 0);
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-09-04 23:50:08 +02:00
|
|
|
for (auto const& module : modules_center_) {
|
2018-12-26 11:13:36 +01:00
|
|
|
center_.pack_start(*module, true, true, 0);
|
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_) {
|
2018-12-26 11:13:36 +01:00
|
|
|
right_.pack_end(*module, false, false, 0);
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2019-02-01 21:45:59 +01:00
|
|
|
window.show_all();
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|