Handle screens disconnection (#29)

This commit is contained in:
Alex
2018-08-19 13:39:57 +02:00
committed by GitHub
parent ce50a627be
commit 6705134034
23 changed files with 205 additions and 133 deletions

View File

@ -3,9 +3,10 @@
#include "factory.hpp"
#include "util/json.hpp"
waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
waybar::Bar::Bar(Client &client,
std::unique_ptr<struct wl_output *> &&p_output, uint32_t p_wl_name)
: client(client), window{Gtk::WindowType::WINDOW_TOPLEVEL},
output(std::move(p_output))
output(std::move(p_output)), wl_name(std::move(p_wl_name))
{
static const struct zxdg_output_v1_listener xdgOutputListener = {
.logical_position = handleLogicalPosition,
@ -22,29 +23,39 @@ waybar::Bar::Bar(Client &client, std::unique_ptr<struct wl_output *> &&p_output)
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,
};
zwlr_layer_surface_v1_add_listener(layer_surface,
&layer_surface_listener, this);
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;
}
auto height = config_["height"] ? config_["height"].asUInt() : height_;
auto width = config_["width"] ? config_["width"].asUInt() : width_;
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);
wl_surface_commit(surface);
}
@ -71,7 +82,7 @@ void waybar::Bar::handleName(void* data, struct zxdg_output_v1* /*xdg_output*/,
const char* name)
{
auto o = static_cast<waybar::Bar *>(data);
o->outputName = name;
o->output_name = name;
}
void waybar::Bar::handleDescription(void* /*data*/,
@ -86,13 +97,17 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data,
{
auto o = static_cast<waybar::Bar *>(data);
o->window.show_all();
o->setWidth(o->config_["width"] ? o->config_["width"].asUInt() : width);
zwlr_layer_surface_v1_ack_configure(surface, serial);
if (o->height_ != height) {
height = o->height_;
std::cout << fmt::format("New Height: {}", height) << std::endl;
zwlr_layer_surface_v1_set_size(surface, o->width_, height);
zwlr_layer_surface_v1_set_exclusive_zone(surface, o->visible ? height : 0);
if (width != o->width_ || height != o->height_) {
o->width_ = width;
o->height_ = height;
std::cout << fmt::format(
"Bar configured (width: {}, height: {}) for output: {}",
o->width_, o->height_, o->output_name) << std::endl;
o->window.set_size_request(o->width_, o->height_);
o->window.resize(o->width_, o->height_);
zwlr_layer_surface_v1_set_exclusive_zone(surface, o->height_);
zwlr_layer_surface_v1_set_size(surface, o->width_, o->height_);
wl_surface_commit(o->surface);
}
}
@ -101,24 +116,13 @@ void waybar::Bar::layerSurfaceHandleClosed(void* data,
struct zwlr_layer_surface_v1* /*surface*/)
{
auto o = static_cast<waybar::Bar *>(data);
std::cout << "Bar removed from output: " + o->output_name << std::endl;
zwlr_layer_surface_v1_destroy(o->layer_surface);
o->layer_surface = nullptr;
wl_surface_destroy(o->surface);
o->surface = nullptr;
o->window.close();
}
auto waybar::Bar::setWidth(uint32_t width) -> void
{
if (width == width_) {
return;
}
std::cout << fmt::format("Bar width configured: {}", width) << std::endl;
width_ = width;
window.set_size_request(width);
window.resize(width, height_);
zwlr_layer_surface_v1_set_size(layer_surface, width, height_ + 1);
wl_surface_commit(surface);
wl_output_destroy(*o->output);
zxdg_output_v1_destroy(o->xdg_output_);
o->modules_left_.clear();
o->modules_center_.clear();
o->modules_right_.clear();
}
auto waybar::Bar::toggle() -> void
@ -154,6 +158,27 @@ auto waybar::Bar::setupCss() -> void
}
}
void waybar::Bar::getModules(Factory factory, const std::string& pos)
{
if (config_[pos]) {
for (const auto &name : config_[pos]) {
try {
if (pos == "modules-left") {
modules_left_.emplace_back(factory.makeModule(name.asString()));
}
if (pos == "modules-center") {
modules_center_.emplace_back(factory.makeModule(name.asString()));
}
if (pos == "modules-right") {
modules_right_.emplace_back(factory.makeModule(name.asString()));
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
}
}
auto waybar::Bar::setupWidgets() -> void
{
auto &left = *Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 0));
@ -167,30 +192,17 @@ auto waybar::Bar::setupWidgets() -> void
box.pack_end(right, true, true);
Factory factory(*this, config_);
if (config_["modules-left"]) {
for (const auto &name : config_["modules-left"]) {
auto module = factory.makeModule(name.asString());
if (module != nullptr) {
left.pack_start(*module, false, true, 0);
}
}
getModules(factory, "modules-left");
getModules(factory, "modules-center");
getModules(factory, "modules-right");
for (auto& module : modules_left_) {
left.pack_start(*module, false, true, 0);
}
if (config_["modules-center"]) {
for (const auto &name : config_["modules-center"]) {
auto module = factory.makeModule(name.asString());
if (module != nullptr) {
center.pack_start(*module, true, false, 0);
}
}
for (auto& module : modules_center_) {
center.pack_start(*module, true, true, 0);
}
if (config_["modules-right"]) {
std::reverse(config_["modules-right"].begin(), config_["modules-right"].end());
for (const auto &name : config_["modules-right"]) {
auto module = factory.makeModule(name.asString());
if (module != nullptr) {
right.pack_end(*module, false, false, 0);
}
}
std::reverse(modules_right_.begin(), modules_right_.end());
for (auto& module : modules_right_) {
right.pack_end(*module, false, false, 0);
}
}

View File

@ -1,7 +1,8 @@
#include "client.hpp"
waybar::Client::Client(int argc, char* argv[])
: gtk_main(argc, argv), gdk_display(Gdk::Display::get_default()),
: gtk_app(Gtk::Application::create(argc, argv, "org.alexays.waybar")),
gdk_display(Gdk::Display::get_default()),
wl_display(gdk_wayland_display_get_wl_display(gdk_display->gobj()))
{
auto getFirstValidPath = [] (std::vector<std::string> possiblePaths) {
@ -48,7 +49,7 @@ void waybar::Client::handleGlobal(void *data, struct wl_registry *registry,
*output = static_cast<struct wl_output *>(wl_registry_bind(registry, name,
&wl_output_interface, version));
if (o->xdg_output_manager != nullptr) {
o->bars.emplace_back(std::make_unique<Bar>(*o, std::move(output)));
o->bars.emplace_back(std::make_unique<Bar>(*o, std::move(output), name));
}
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
o->seat = static_cast<struct wl_seat *>(wl_registry_bind(registry, name,
@ -61,10 +62,16 @@ void waybar::Client::handleGlobal(void *data, struct wl_registry *registry,
}
}
void waybar::Client::handleGlobalRemove(void* /*data*/,
struct wl_registry* /*registry*/, uint32_t /*name*/)
void waybar::Client::handleGlobalRemove(void* data,
struct wl_registry* /*registry*/, uint32_t name)
{
// TODO(someone)
auto o = static_cast<waybar::Client *>(data);
for (auto it = o->bars.begin(); it != o->bars.end(); ++it) {
if ((**it).wl_name == name) {
o->bars.erase(it);
break;
}
}
}
void waybar::Client::bindInterfaces()
@ -81,7 +88,8 @@ void waybar::Client::bindInterfaces()
int waybar::Client::main(int /*argc*/, char* /*argv*/[])
{
bindInterfaces();
Gtk::Main::run();
gtk_app->hold();
gtk_app->run();
bars.clear();
zxdg_output_manager_v1_destroy(xdg_output_manager);
zwlr_layer_shell_v1_destroy(layer_shell);

View File

@ -4,7 +4,7 @@ waybar::Factory::Factory(Bar &bar, Json::Value config)
: _bar(bar), _config(std::move(config))
{}
waybar::IModule *waybar::Factory::makeModule(const std::string &name)
waybar::IModule* waybar::Factory::makeModule(const std::string &name)
{
try {
if (name == "battery") {
@ -34,13 +34,12 @@ waybar::IModule *waybar::Factory::makeModule(const std::string &name)
if (name.compare(0, 7, "custom/") == 0 && name.size() > 7) {
return new waybar::modules::Custom(name.substr(7), _config[name]);
}
std::cerr << "Unknown module: " + name << std::endl;
} catch (const std::exception& e) {
auto err = fmt::format("Disabling module \"{}\", {}", name, e.what());
std::cerr << err << std::endl;
throw std::runtime_error(err);
} catch (...) {
auto err = fmt::format("Disabling module \"{}\", Unknown reason", name);
std::cerr << err << std::endl;
throw std::runtime_error(err);
}
return nullptr;
throw std::runtime_error("Unknown module: " + name);
}

View File

@ -16,26 +16,32 @@ waybar::modules::Battery::Battery(Json::Value config)
if (batteries_.empty()) {
throw std::runtime_error("No batteries.");
}
auto fd = inotify_init1(IN_CLOEXEC);
if (fd == -1) {
fd_ = inotify_init1(IN_CLOEXEC);
if (fd_ == -1) {
throw std::runtime_error("Unable to listen batteries.");
}
for (auto &bat : batteries_) {
inotify_add_watch(fd, (bat / "uevent").c_str(), IN_ACCESS);
inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
}
// Trigger first value
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
// Trigger first values
update();
label_.set_name("battery");
thread_ = [this, fd] {
thread_.sig_update.connect(sigc::mem_fun(*this, &Battery::update));
thread_ = [this] {
struct inotify_event event = {0};
int nbytes = read(fd, &event, sizeof(event));
int nbytes = read(fd_, &event, sizeof(event));
if (nbytes != sizeof(event)) {
return;
}
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
thread_.sig_update.emit();
};
}
waybar::modules::Battery::~Battery()
{
close(fd_);
}
auto waybar::modules::Battery::update() -> void
{
try {

View File

@ -5,9 +5,10 @@ waybar::modules::Clock::Clock(Json::Value config)
{
label_.set_name("clock");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60;
thread_.sig_update.connect(sigc::mem_fun(*this, &Clock::update));
thread_ = [this, interval] {
auto now = waybar::chrono::clock::now();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Clock::update));
thread_.sig_update.emit();
auto timeout = std::chrono::floor<std::chrono::seconds>(now
+ std::chrono::seconds(interval));
thread_.sleep_until(timeout);

View File

@ -5,8 +5,9 @@ waybar::modules::Cpu::Cpu(Json::Value config)
{
label_.set_name("cpu");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10;
thread_.sig_update.connect(sigc::mem_fun(*this, &Cpu::update));
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update));
thread_.sig_update.emit();
thread_.sleep_for(chrono::seconds(interval));
};
}

View File

@ -16,10 +16,11 @@ waybar::modules::Custom::Custom(std::string name, Json::Value config)
}
}
if (can_update) {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Custom::update));
thread_.sig_update.emit();
}
thread_.sleep_for(chrono::seconds(interval));
};
thread_.sig_update.connect(sigc::mem_fun(*this, &Custom::update));
}
auto waybar::modules::Custom::update() -> void

View File

@ -5,8 +5,9 @@ waybar::modules::Memory::Memory(Json::Value config)
{
label_.set_name("memory");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
thread_.sig_update.connect(sigc::mem_fun(*this, &Memory::update));
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update));
thread_.sig_update.emit();
thread_.sleep_for(chrono::seconds(interval));
};
}

View File

@ -29,8 +29,10 @@ waybar::modules::Network::Network(Json::Value config)
}
}
label_.set_name("network");
// Trigger first values
getInfo();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update));
update();
thread_.sig_update.connect(sigc::mem_fun(*this, &Network::update));
thread_ = [this] {
char buf[4096];
uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf),
@ -67,11 +69,16 @@ waybar::modules::Network::Network(Json::Value config)
}
if (need_update) {
getInfo();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update));
thread_.sig_update.emit();
}
};
}
waybar::modules::Network::~Network()
{
close(sock_fd_);
}
auto waybar::modules::Network::update() -> void
{
auto format = config_["format"] ? config_["format"].asString() : "{ifname}";

View File

@ -28,6 +28,13 @@ waybar::modules::Pulseaudio::Pulseaudio(Json::Value config)
pa_threaded_mainloop_unlock(mainloop_);
}
waybar::modules::Pulseaudio::~Pulseaudio()
{
mainloop_api_->quit(mainloop_api_, 0);
pa_threaded_mainloop_stop(mainloop_);
pa_threaded_mainloop_free(mainloop_);
}
void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data)
{
auto pa = static_cast<waybar::modules::Pulseaudio *>(data);

View File

@ -10,6 +10,7 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
ipc_eventfd_ = ipcOpenSocket(socketPath);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"window\" ]");
getFocusedWindow();
thread_.sig_update.connect(sigc::mem_fun(*this, &Window::update));
thread_ = [this] {
try {
auto res = ipcRecvResponse(ipc_eventfd_);
@ -17,7 +18,7 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
if ((parsed["change"] == "focus" || parsed["change"] == "title")
&& parsed["container"]["focused"].asBool()) {
window_ = parsed["container"]["name"].asString();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
thread_.sig_update.emit();
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
@ -25,6 +26,12 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
};
}
waybar::modules::sway::Window::~Window()
{
close(ipcfd_);
close(ipc_eventfd_);
}
auto waybar::modules::sway::Window::update() -> void
{
label_.set_text(window_);

View File

@ -9,30 +9,35 @@ waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
ipcfd_ = ipcOpenSocket(socketPath);
ipc_eventfd_ = ipcOpenSocket(socketPath);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"workspace\" ]");
thread_.sig_update.connect(sigc::mem_fun(*this, &Workspaces::update));
thread_ = [this] {
try {
// Wait for the name of the output
if (!config_["all-outputs"].asBool() && bar_.outputName.empty()) {
while (bar_.outputName.empty()) {
if (!config_["all-outputs"].asBool() && bar_.output_name.empty()) {
while (bar_.output_name.empty()) {
thread_.sleep_for(chrono::milliseconds(150));
}
} else if (!workspaces_.empty()) {
ipcRecvResponse(ipc_eventfd_);
}
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
workspaces_ = parser_.parse(res.payload);
Glib::signal_idle()
.connect_once(sigc::mem_fun(*this, &Workspaces::update));
thread_.sig_update.emit();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
};
}
waybar::modules::sway::Workspaces::~Workspaces()
{
close(ipcfd_);
close(ipc_eventfd_);
}
auto waybar::modules::sway::Workspaces::update() -> void
{
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
workspaces_ = parser_.parse(res.payload);
bool needReorder = false;
for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
@ -46,7 +51,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
}
for (auto node : workspaces_) {
if (!config_["all-outputs"].asBool()
&& bar_.outputName != node["output"].asString()) {
&& bar_.output_name != node["output"].asString()) {
continue;
}
auto it = buttons_.find(node["num"].asInt());