Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module "sway/hide" for swaybar integration #860

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Bar {
~Bar() = default;

auto toggle() -> void;
auto setVisible(bool) -> void;
void handleSignal(int);

struct waybar_output *output;
Expand Down
1 change: 1 addition & 0 deletions include/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "modules/sway/mode.hpp"
#include "modules/sway/window.hpp"
#include "modules/sway/workspaces.hpp"
#include "modules/sway/hide.hpp"
#endif
#ifdef HAVE_WLR
#include "modules/wlr/taskbar.hpp"
Expand Down
37 changes: 37 additions & 0 deletions include/modules/sway/hide.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <fmt/format.h>
#include <tuple>
#include <mutex>
#include "ALabel.hpp"
#include "bar.hpp"
#include "client.hpp"
#include "modules/sway/ipc/client.hpp"
#include "util/json.hpp"
#include "util/sleeper_thread.hpp"

namespace waybar::modules::sway {

class Hide : public ALabel, public sigc::trackable {
public:
Hide(const std::string&, const waybar::Bar&, const Json::Value&);
~Hide() = default;
auto update() -> void;

private:
void onEvent(const struct Ipc::ipc_response&);
void worker();

std::string current_mode_;
bool visible_by_modifier_;
const Bar& bar_;
std::string window_;
int windowId_;
util::JsonParser parser_;

util::SleeperThread thread_;
std::mutex mutex_;
Ipc ipc_;
};

} // namespace waybar::modules::sway
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ src_files += [
'src/modules/sway/ipc/client.cpp',
'src/modules/sway/mode.cpp',
'src/modules/sway/window.cpp',
'src/modules/sway/workspaces.cpp'
'src/modules/sway/workspaces.cpp',
'src/modules/sway/hide.cpp',
]

if true
Expand Down
8 changes: 6 additions & 2 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,8 @@ void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface
o->modules_right_.clear();
}

auto waybar::Bar::toggle() -> void {
visible = !visible;
auto waybar::Bar::setVisible(bool nvis) -> void {
visible = nvis;
if (!visible) {
window.get_style_context()->add_class("hidden");
window.set_opacity(0);
Expand All @@ -404,6 +404,10 @@ auto waybar::Bar::toggle() -> void {
}
}

auto waybar::Bar::toggle() -> void {
return setVisible(!visible);
}

void waybar::Bar::getModules(const Factory& factory, const std::string& pos) {
if (config[pos].isArray()) {
for (const auto& name : config[pos]) {
Expand Down
3 changes: 3 additions & 0 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "sway/window") {
return new waybar::modules::sway::Window(id, bar_, config_[name]);
}
if (ref == "sway/hide") {
return new waybar::modules::sway::Hide(id, bar_, config_[name]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that be a module?
Maybe we can implement it as a generic SwayBarConfigHandler class that is created by the waybar::Client instance when the SWAYSOCK is defined and controls all the bars.
The benefit is that on multi-monitor configurations bar events would be handled once.

}
#endif
#ifdef HAVE_WLR
if (ref == "wlr/taskbar") {
Expand Down
50 changes: 50 additions & 0 deletions src/modules/sway/hide.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "modules/sway/hide.hpp"
#include <spdlog/spdlog.h>
#include "client.hpp"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"

namespace waybar::modules::sway {

Hide::Hide(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "hide", id, "{}", 0, true), bar_(bar), windowId_(-1) {
ipc_.subscribe(R"(["bar_state_update","barconfig_update"])");
ipc_.signal_event.connect(sigc::mem_fun(*this, &Hide::onEvent));
// Launch worker
worker();
}

void Hide::onEvent(const struct Ipc::ipc_response& res) {
auto payload = parser_.parse(res.payload);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a mutex here, might fix your crash

Copy link
Author

@somini somini Sep 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My C++ knowledge is almost 0, I just copy pasted some bits from other places that looked correct. 😞

2253f3e crashes less, but it still fails. Logs:

**
Gtk:ERROR:../gtk/gtk/gtkcssinheritvalue.c:33:gtk_css_value_inherit_free: code should not be reached
**
Gtk:ERROR:../gtk/gtk/gtkcssinheritvalue.c:33:gtk_css_value_inherit_free: code should not be reached
Bail out! Gtk:ERROR:../gtk/gtk/gtkcssinheritvalue.c:33:gtk_css_value_inherit_free: code should not be reached
Bail out! Gtk:ERROR:../gtk/gtk/gtkcssinheritvalue.c:33:gtk_css_value_inherit_free: code should not be reached
Aborted (core dumped)

Sounds like hiding a painting bar is a no-no.

I think the mutex should be on the setVisible module for the bar itself, but waiting for the first paint. Is there some kind of event queue where we can add the hide/show events, instead of being done ASAP?

Copy link
Author

@somini somini Sep 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased this to the latest master and force-pushed

std::lock_guard<std::mutex> lock(mutex_);
if (payload.isMember("mode")) {
Copy link
Contributor

@alebastr alebastr Sep 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that each waybar instance supposed to have bar id (waybar -b bar-0) and events from sway could be addressed to the specific bar or to all bars.
Somewhere around here you should check if the current waybar instance needs to handle the event.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to use just call setVisible on the bar_ object, but I got an error about mismatching const which is above my meagre C++-fu.

// barconfig_update: get mode
auto mode = payload["mode"].asString();
if (mode == "hide") {
// Hide the bars when configuring the "hide" bar
for (auto& bar : waybar::Client::inst()->bars) {
bar->setVisible(false);
}
}
} else if (payload.isMember("visible_by_modifier")) {
// bar_state_update: get visible_by_modifier
visible_by_modifier_ = payload["visible_by_modifier"].asBool();
spdlog::info("WayBar Shown: {}", visible_by_modifier_);
for (auto& bar : waybar::Client::inst()->bars) {
bar->setVisible(visible_by_modifier_);
}
}
}

void Hide::worker() {
thread_ = [this] {
try {
ipc_.handleEvent();
} catch (const std::exception& e) {
spdlog::error("Hide: {}", e.what());
}
};
}

auto Hide::update() -> void {
}
} // namespace waybar::modules::sway