refactor: format && better output management

This commit is contained in:
Alex
2019-04-18 17:43:16 +02:00
parent 817c42841b
commit 807ef32357
13 changed files with 479 additions and 475 deletions

View File

@ -1,9 +1,9 @@
#include "modules/idle_inhibitor.hpp"
#include "util/command.hpp"
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "{status}"), bar_(bar), status_("deactivated"), idle_inhibitor_(nullptr)
{
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config)
: ALabel(config, "{status}"), bar_(bar), status_("deactivated"), idle_inhibitor_(nullptr) {
label_.set_name("idle_inhibitor");
if (!id.empty()) {
label_.get_style_context()->add_class(id);
@ -14,21 +14,18 @@ waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar&
dp.emit();
}
waybar::modules::IdleInhibitor::~IdleInhibitor()
{
if(idle_inhibitor_) {
waybar::modules::IdleInhibitor::~IdleInhibitor() {
if (idle_inhibitor_) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr;
}
}
auto waybar::modules::IdleInhibitor::update() -> void
{
auto waybar::modules::IdleInhibitor::update() -> void {
label_.set_markup(
fmt::format(format_, fmt::arg("status", status_),
fmt::arg("icon", getIcon(0, status_))));
fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_))));
label_.get_style_context()->add_class(status_);
if(tooltipEnabled()) {
if (tooltipEnabled()) {
label_.set_tooltip_text(status_);
}
}
@ -39,10 +36,10 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
if (idle_inhibitor_) {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr;
status_ = "deactivated";
status_ = "deactivated";
} else {
idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor(
bar_.client.idle_inhibit_manager, bar_.surface);
waybar::Client::inst()->idle_inhibit_manager, bar_.surface);
status_ = "activated";
}
if (config_["on-click"].isString() && e->button == 1) {

View File

@ -72,7 +72,7 @@ void waybar::modules::Network::createInfoSocket()
}
{
ev_fd_ = eventfd(0, EFD_NONBLOCK);
struct epoll_event event;
struct epoll_event event = {0};
event.events = EPOLLIN | EPOLLET;
event.data.fd = ev_fd_;
if (epoll_ctl(efd_, EPOLL_CTL_ADD, ev_fd_, &event) == -1) {
@ -81,7 +81,7 @@ void waybar::modules::Network::createInfoSocket()
}
{
auto fd = nl_socket_get_fd(info_sock_);
struct epoll_event event;
struct epoll_event event = {0};
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
event.data.fd = fd;
if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) {
@ -114,7 +114,7 @@ void waybar::modules::Network::worker()
}
thread_timer_.sleep_for(interval_);
};
struct epoll_event events[EPOLL_MAX];
struct epoll_event events[EPOLL_MAX] = {{0}};
thread_ = [this, &events] {
int ec = epoll_wait(efd_, events, EPOLL_MAX, -1);
if (ec > 0) {

View File

@ -16,7 +16,19 @@ Host::Host(const std::size_t id, const Json::Value& config,
on_add_(on_add),
on_remove_(on_remove) {}
Host::~Host() { Gio::DBus::unwatch_name(bus_name_id_); }
Host::~Host() {
if (bus_name_id_ > 0) {
Gio::DBus::unwatch_name(bus_name_id_);
bus_name_id_ = 0;
}
if (watcher_id_ > 0) {
Gio::DBus::unwatch_name(watcher_id_);
watcher_id_ = 0;
}
g_cancellable_cancel(cancellable_);
g_clear_object(&cancellable_);
g_clear_object(&watcher_);
}
void Host::busAcquired(const Glib::RefPtr<Gio::DBus::Connection>& conn, Glib::ustring name) {
watcher_id_ = Gio::DBus::watch_name(conn, "org.kde.StatusNotifierWatcher",

View File

@ -8,7 +8,6 @@ waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, const Js
watcher_(),
host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1)) {
std::cout << "Tray is in beta, so there may be bugs or even be unusable." << std::endl;
if (config_["spacing"].isUInt()) {
box_.set_spacing(config_["spacing"].asUInt());
}

View File

@ -1,11 +1,11 @@
#include "modules/sway/workspaces.hpp"
waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar& bar,
const Json::Value& config)
: bar_(bar), config_(config),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
scrolling_(false)
{
waybar::modules::sway::Workspaces::Workspaces(const std::string &id, const Bar &bar,
const Json::Value &config)
: bar_(bar),
config_(config),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
scrolling_(false) {
box_.set_name("workspaces");
if (!id.empty()) {
box_.get_style_context()->add_class(id);
@ -15,8 +15,7 @@ waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar&
worker();
}
void waybar::modules::sway::Workspaces::worker()
{
void waybar::modules::sway::Workspaces::worker() {
thread_ = [this] {
try {
if (!workspaces_.empty()) {
@ -24,36 +23,35 @@ void waybar::modules::sway::Workspaces::worker()
}
{
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
if (thread_.isRunning()) {
workspaces_ = parser_.parse(res.payload);
}
}
dp.emit();
} catch (const std::exception& e) {
} catch (const std::exception &e) {
std::cerr << "Workspaces: " << e.what() << std::endl;
}
};
}
auto waybar::modules::sway::Workspaces::update() -> void
{
bool needReorder = false;
auto waybar::modules::sway::Workspaces::update() -> void {
bool needReorder = false;
std::lock_guard<std::mutex> lock(mutex_);
for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
[it](auto node) -> bool { return node["name"].asString() == it->first; });
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(), [it](auto node) -> bool {
return node["name"].asString() == it->first;
});
if (ws == workspaces_.end() ||
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output_name)) {
it = buttons_.erase(it);
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output->name)) {
it = buttons_.erase(it);
needReorder = true;
} else {
++it;
}
}
for (auto const& node : workspaces_) {
if (!config_["all-outputs"].asBool()
&& bar_.output_name != node["output"].asString()) {
for (auto const &node : workspaces_) {
if (!config_["all-outputs"].asBool() && bar_.output->name != node["output"].asString()) {
continue;
}
auto it = buttons_.find(node["name"].asString());
@ -80,16 +78,17 @@ auto waybar::modules::sway::Workspaces::update() -> void
if (needReorder) {
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
}
auto icon = getIcon(node["name"].asString(), node);
auto icon = getIcon(node["name"].asString(), node);
std::string output = icon;
if (config_["format"].isString()) {
auto format = config_["format"].asString();
output = fmt::format(format, fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()));
output = fmt::format(format,
fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()));
}
if (!config_["disable-markup"].asBool()) {
static_cast<Gtk::Label*>(button.get_children()[0])->set_markup(output);
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(output);
} else {
button.set_label(output);
}
@ -101,34 +100,33 @@ auto waybar::modules::sway::Workspaces::update() -> void
}
}
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node)
{
auto icon = getIcon(node["name"].asString(), node);
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) {
auto icon = getIcon(node["name"].asString(), node);
auto format = config_["format"].isString()
? fmt::format(config_["format"].asString(), fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()))
: icon;
auto pair = buttons_.emplace(node["name"].asString(), format);
? fmt::format(config_["format"].asString(),
fmt::arg("icon", icon),
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
fmt::arg("index", node["num"].asString()))
: icon;
auto pair = buttons_.emplace(node["name"].asString(), format);
auto &button = pair.first->second;
if (!config_["disable-markup"].asBool()) {
static_cast<Gtk::Label*>(button.get_children()[0])->set_markup(format);
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(format);
}
box_.pack_start(button, false, false, 0);
button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] {
try {
std::lock_guard<std::mutex> lock(mutex_);
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
ipc_.sendCmd(IPC_COMMAND, cmd);
} catch (const std::exception& e) {
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
}
});
if (!config_["disable-scroll"].asBool()) {
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event()
.connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
button.signal_scroll_event().connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
}
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
if (node["focused"].asBool()) {
@ -141,14 +139,13 @@ void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node)
button.get_style_context()->add_class("urgent");
}
onButtonReady(node, button);
onButtonReady(node, button);
}
std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name,
const Json::Value &node)
{
std::vector<std::string> keys = { name, "urgent", "focused", "visible", "default" };
for (auto const& key : keys) {
const Json::Value &node) {
std::vector<std::string> keys = {name, "urgent", "focused", "visible", "default"};
for (auto const &key : keys) {
if (key == "focused" || key == "visible" || key == "urgent") {
if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString();
@ -160,14 +157,13 @@ std::string waybar::modules::sway::Workspaces::getIcon(const std::string &name,
return name;
}
bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
{
bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) {
// Avoid concurrent scroll event
if (scrolling_) {
return false;
}
std::lock_guard<std::mutex> lock(mutex_);
uint8_t idx;
uint8_t idx;
scrolling_ = true;
for (idx = 0; idx < workspaces_.size(); idx += 1) {
if (workspaces_[idx]["focused"].asBool()) {
@ -180,15 +176,14 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
}
std::string name;
if (e->direction == GDK_SCROLL_UP) {
name = getCycleWorkspace(idx, true);
name = getCycleWorkspace(idx, true);
}
if (e->direction == GDK_SCROLL_DOWN) {
name = getCycleWorkspace(idx, false);
name = getCycleWorkspace(idx, false);
}
if (e->direction == GDK_SCROLL_SMOOTH) {
gdouble delta_x, delta_y;
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e),
&delta_x, &delta_y);
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e), &delta_x, &delta_y);
if (delta_y < 0) {
name = getCycleWorkspace(idx, true);
} else if (delta_y > 0) {
@ -204,17 +199,17 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
return true;
}
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(
uint8_t focused_workspace, bool prev) const
{
auto inc = prev ? -1 : 1;
int size = workspaces_.size();
uint8_t idx = 0;
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(uint8_t focused_workspace,
bool prev) const {
auto inc = prev ? -1 : 1;
int size = workspaces_.size();
uint8_t idx = 0;
for (int i = focused_workspace; i < size && i >= 0; i += inc) {
bool same_output = (workspaces_[i]["output"].asString() == bar_.output_name
&& !config_["all-outputs"].asBool()) || config_["all-outputs"].asBool();
bool same_output = (workspaces_[i]["output"].asString() == bar_.output->name &&
!config_["all-outputs"].asBool()) ||
config_["all-outputs"].asBool();
bool same_name =
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
if (same_output && !same_name) {
return workspaces_[i]["name"].asString();
}
@ -230,42 +225,39 @@ const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(
return "";
}
uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const
{
uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const {
uint16_t idx = 0;
for (const auto &workspace : workspaces_) {
if (workspace["name"].asString() == name) {
return idx;
}
if (!(!config_["all-outputs"].asBool() && workspace["output"].asString() != bar_.output_name)) {
if (!(!config_["all-outputs"].asBool() &&
workspace["output"].asString() != bar_.output->name)) {
idx += 1;
}
}
return workspaces_.size();
}
std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name)
{
std::string waybar::modules::sway::Workspaces::trimWorkspaceName(std::string name) {
std::size_t found = name.find(":");
if (found!=std::string::npos) {
return name.substr(found+1);
if (found != std::string::npos) {
return name.substr(found + 1);
}
return name;
}
void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value& node, Gtk::Button& button)
{
if (config_["current-only"].asBool()) {
if (node["focused"].asBool()) {
button.show();
} else {
button.hide();
}
} else {
button.show();
}
void waybar::modules::sway::Workspaces::onButtonReady(const Json::Value &node,
Gtk::Button & button) {
if (config_["current-only"].asBool()) {
if (node["focused"].asBool()) {
button.show();
} else {
button.hide();
}
} else {
button.show();
}
}
waybar::modules::sway::Workspaces::operator Gtk::Widget &() {
return box_;
}
waybar::modules::sway::Workspaces::operator Gtk::Widget &() { return box_; }