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-09 20:22:01 +02:00
|
|
|
waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
|
|
|
|
: client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
|
|
|
output(std::move(p_output))
|
|
|
|
{
|
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");
|
|
|
|
window.set_decorated(false);
|
2018-08-16 14:29:41 +02:00
|
|
|
setupConfig();
|
|
|
|
setupCss();
|
|
|
|
setupWidgets();
|
|
|
|
if (config_["height"]) {
|
|
|
|
height_ = config_["height"].asUInt();
|
|
|
|
}
|
|
|
|
Gtk::Widget& wrap(window);
|
|
|
|
gtk_widget_realize(wrap.gobj());
|
|
|
|
GdkWindow *gdk_window = gtk_widget_get_window(wrap.gobj());
|
|
|
|
gdk_wayland_window_set_use_custom_surface(gdk_window);
|
|
|
|
surface = gdk_wayland_window_get_wl_surface(gdk_window);
|
|
|
|
std::size_t layer_top = config_["layer"] == "top"
|
|
|
|
? 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");
|
|
|
|
std::size_t position_bottom = config_["position"] == "bottom"
|
|
|
|
? ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM : ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
|
|
|
|
zwlr_layer_surface_v1_set_anchor(layer_surface, position_bottom
|
|
|
|
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
|
|
|
|
zwlr_layer_surface_v1_set_size(layer_surface, width_, height_);
|
|
|
|
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
|
|
|
|
.configure = layerSurfaceHandleConfigure,
|
|
|
|
.closed = layerSurfaceHandleClosed,
|
2018-08-09 20:22:01 +02:00
|
|
|
};
|
2018-08-16 14:29:41 +02:00
|
|
|
zwlr_layer_surface_v1_add_listener(layer_surface,
|
|
|
|
&layer_surface_listener, this);
|
2018-08-09 20:22:01 +02:00
|
|
|
wl_surface_commit(surface);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-11 00:32:59 +02:00
|
|
|
o->outputName = name;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
o->window.show_all();
|
2018-08-16 14:29:41 +02:00
|
|
|
o->setWidth(o->config_["width"] ? o->config_["width"].asUInt() : width);
|
2018-08-08 23:54:58 +02:00
|
|
|
zwlr_layer_surface_v1_ack_configure(surface, serial);
|
2018-08-16 14:29:41 +02:00
|
|
|
if (o->height_ != height) {
|
|
|
|
height = o->height_;
|
2018-08-08 23:54:58 +02:00
|
|
|
std::cout << fmt::format("New Height: {}", height) << std::endl;
|
2018-08-16 14:29:41 +02:00
|
|
|
zwlr_layer_surface_v1_set_size(surface, o->width_, height);
|
2018-08-08 23:54:58 +02:00
|
|
|
zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0);
|
|
|
|
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);
|
|
|
|
zwlr_layer_surface_v1_destroy(o->layer_surface);
|
|
|
|
o->layer_surface = nullptr;
|
2018-08-08 23:54:58 +02:00
|
|
|
wl_surface_destroy(o->surface);
|
2018-08-09 10:50:16 +02:00
|
|
|
o->surface = nullptr;
|
2018-08-08 23:54:58 +02:00
|
|
|
o->window.close();
|
|
|
|
}
|
|
|
|
|
2018-08-10 18:46:31 +02:00
|
|
|
auto waybar::Bar::setWidth(uint32_t width) -> void
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
if (width == width_) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-09 11:12:46 +02:00
|
|
|
std::cout << fmt::format("Bar width configured: {}", width) << std::endl;
|
2018-08-16 14:29:41 +02:00
|
|
|
width_ = width;
|
2018-08-08 23:54:58 +02:00
|
|
|
window.set_size_request(width);
|
2018-08-16 14:29:41 +02:00
|
|
|
window.resize(width, height_);
|
|
|
|
zwlr_layer_surface_v1_set_size(layer_surface, width, height_ + 1);
|
2018-08-08 23:54:58 +02:00
|
|
|
wl_surface_commit(surface);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-13 21:23:43 +02:00
|
|
|
util::JsonParser parser;
|
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-16 14:29:41 +02:00
|
|
|
config_ = parser.parse(str);
|
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
|
|
|
|
|
|
|
// 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-16 14:29:41 +02:00
|
|
|
auto waybar::Bar::setupWidgets() -> void
|
2018-08-08 23:54:58 +02:00
|
|
|
{
|
|
|
|
auto &left = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
|
|
|
|
auto ¢er = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
|
|
|
|
auto &right = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
|
|
|
|
|
|
|
|
auto &box1 = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
|
|
|
|
window.add(box1);
|
|
|
|
box1.set_homogeneous(true);
|
|
|
|
box1.pack_start(left, true, true);
|
|
|
|
box1.pack_start(center, false, false);
|
|
|
|
box1.pack_end(right, true, true);
|
|
|
|
|
2018-08-16 14:29:41 +02:00
|
|
|
Factory factory(*this, config_);
|
|
|
|
|
|
|
|
if (config_["modules-left"]) {
|
|
|
|
for (const auto &name : config_["modules-left"]) {
|
2018-08-13 14:05:13 +02:00
|
|
|
auto module = factory.makeModule(name.asString());
|
2018-08-16 14:29:41 +02:00
|
|
|
if (module != nullptr) {
|
2018-08-13 14:05:13 +02:00
|
|
|
left.pack_start(*module, false, true, 0);
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
if (config_["modules-center"]) {
|
|
|
|
for (const auto &name : config_["modules-center"]) {
|
2018-08-13 14:05:13 +02:00
|
|
|
auto module = factory.makeModule(name.asString());
|
2018-08-16 14:29:41 +02:00
|
|
|
if (module != nullptr) {
|
2018-08-13 14:05:13 +02:00
|
|
|
center.pack_start(*module, true, false, 10);
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 14:29:41 +02:00
|
|
|
if (config_["modules-right"]) {
|
|
|
|
std::reverse(config_["modules-right"].begin(), config_["modules-right"].end());
|
|
|
|
for (const auto &name : config_["modules-right"]) {
|
2018-08-13 14:05:13 +02:00
|
|
|
auto module = factory.makeModule(name.asString());
|
2018-08-16 14:29:41 +02:00
|
|
|
if (module != nullptr) {
|
2018-08-13 14:05:13 +02:00
|
|
|
right.pack_end(*module, false, false, 0);
|
2018-08-16 14:29:41 +02:00
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 23:54:58 +02:00
|
|
|
}
|