Merge branch 'Alexays:master' into sort-workspaces-by-number

This commit is contained in:
herlev
2022-10-18 12:19:00 +02:00
committed by GitHub
12 changed files with 368 additions and 7 deletions

View File

@ -35,6 +35,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "sway/language") {
return new waybar::modules::sway::Language(id, config_[name]);
}
if (ref == "sway/scratchpad") {
return new waybar::modules::sway::Scratchpad(id, config_[name]);
}
#endif
#ifdef HAVE_WLR
if (ref == "wlr/taskbar") {
@ -81,6 +84,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "clock") {
return new waybar::modules::Clock(id, config_[name]);
}
if (ref == "user") {
return new waybar::modules::User(id, config_[name]);
}
if (ref == "disk") {
return new waybar::modules::Disk(id, config_[name]);
}

View File

@ -36,6 +36,7 @@ waybar::modules::Pulseaudio::Pulseaudio(const std::string &id, const Json::Value
}
waybar::modules::Pulseaudio::~Pulseaudio() {
pa_context_disconnect(context_);
mainloop_api_->quit(mainloop_api_, 0);
pa_threaded_mainloop_stop(mainloop_);
pa_threaded_mainloop_free(mainloop_);

View File

@ -0,0 +1,82 @@
#include "modules/sway/scratchpad.hpp"
#include <spdlog/spdlog.h>
#include <string>
namespace waybar::modules::sway {
Scratchpad::Scratchpad(const std::string& id, const Json::Value& config)
: ALabel(config, "scratchpad", id,
config["format"].isString() ? config["format"].asString() : "{icon} {count}"),
tooltip_format_(config_["tooltip-format"].isString() ? config_["tooltip-format"].asString()
: "{app}: {title}"),
show_empty_(config_["show-empty"].isBool() ? config_["show-empty"].asBool() : false),
tooltip_enabled_(config_["tooltip"].isBool() ? config_["tooltip"].asBool() : true),
tooltip_text_(""),
count_(0) {
ipc_.subscribe(R"(["window"])");
ipc_.signal_event.connect(sigc::mem_fun(*this, &Scratchpad::onEvent));
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Scratchpad::onCmd));
getTree();
ipc_.setWorker([this] {
try {
ipc_.handleEvent();
} catch (const std::exception& e) {
spdlog::error("Scratchpad: {}", e.what());
}
});
}
auto Scratchpad::update() -> void {
if (count_ || show_empty_) {
event_box_.show();
label_.set_markup(
fmt::format(format_, fmt::arg("icon", getIcon(count_, "", config_["format-icons"].size())),
fmt::arg("count", count_)));
if (tooltip_enabled_) {
label_.set_tooltip_markup(tooltip_text_);
}
} else {
event_box_.hide();
}
if (count_) {
label_.get_style_context()->remove_class("empty");
} else {
label_.get_style_context()->add_class("empty");
}
ALabel::update();
}
auto Scratchpad::getTree() -> void {
try {
ipc_.sendCmd(IPC_GET_TREE);
} catch (const std::exception& e) {
spdlog::error("Scratchpad: {}", e.what());
}
}
auto Scratchpad::onCmd(const struct Ipc::ipc_response& res) -> void {
try {
std::lock_guard<std::mutex> lock(mutex_);
auto tree = parser_.parse(res.payload);
count_ = tree["nodes"][0]["nodes"][0]["floating_nodes"].size();
if (tooltip_enabled_) {
tooltip_text_.clear();
for (const auto& window : tree["nodes"][0]["nodes"][0]["floating_nodes"]) {
tooltip_text_.append(fmt::format(tooltip_format_ + '\n',
fmt::arg("app", window["app_id"].asString()),
fmt::arg("title", window["name"].asString())));
}
if (!tooltip_text_.empty()) {
tooltip_text_.pop_back();
}
}
dp.emit();
} catch (const std::exception& e) {
spdlog::error("Scratchpad: {}", e.what());
}
}
auto Scratchpad::onEvent(const struct Ipc::ipc_response& res) -> void { getTree(); }
} // namespace waybar::modules::sway

114
src/modules/user.cpp Normal file
View File

@ -0,0 +1,114 @@
#include "modules/user.hpp"
#include <fmt/chrono.h>
#include <glibmm/miscutils.h>
#include <unistd.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#if HAVE_CPU_LINUX
#include <sys/sysinfo.h>
#endif
#if HAVE_CPU_BSD
#include <time.h>
#endif
namespace waybar::modules {
User::User(const std::string& id, const Json::Value& config)
: AIconLabel(config, "user", id, "{user} {work_H}:{work_M}", 60, false, false, true) {
if (AIconLabel::iconEnabled()) {
this->init_avatar(AIconLabel::config_);
}
this->init_update_worker();
}
long User::uptime_as_seconds() {
long uptime = 0;
#if HAVE_CPU_LINUX
struct sysinfo s_info;
if (0 == sysinfo(&s_info)) {
uptime = s_info.uptime;
}
#endif
#if HAVE_CPU_BSD
struct timespec s_info;
if (0 == clock_gettime(CLOCK_UPTIME_PRECISE, &s_info)) {
uptime = s_info.tv_sec;
}
#endif
return uptime;
}
std::string User::get_user_login() { return Glib::get_user_name(); }
std::string User::get_user_home_dir() { return Glib::get_home_dir(); }
void User::init_update_worker() {
this->thread_ = [this] {
ALabel::dp.emit();
auto now = std::chrono::system_clock::now();
auto diff = now.time_since_epoch() % ALabel::interval_;
this->thread_.sleep_for(ALabel::interval_ - diff);
};
}
void User::init_avatar(const Json::Value& config) {
int height =
config["height"].isUInt() ? config["height"].asUInt() : this->defaultUserImageHeight_;
int width = config["width"].isUInt() ? config["width"].asUInt() : this->defaultUserImageWidth_;
if (config["avatar"].isString()) {
std::string userAvatar = config["avatar"].asString();
if (!userAvatar.empty()) {
this->init_user_avatar(userAvatar, width, height);
return;
}
}
this->init_default_user_avatar(width, width);
}
std::string User::get_default_user_avatar_path() {
return this->get_user_home_dir() + "/" + ".face";
}
void User::init_default_user_avatar(int width, int height) {
this->init_user_avatar(this->get_default_user_avatar_path(), width, height);
}
void User::init_user_avatar(const std::string& path, int width, int height) {
this->pixbuf_ = Gdk::Pixbuf::create_from_file(path, width, height);
AIconLabel::image_.set(this->pixbuf_);
}
auto User::update() -> void {
std::string systemUser = this->get_user_login();
std::transform(systemUser.cbegin(), systemUser.cend(), systemUser.begin(),
[](unsigned char c) { return std::toupper(c); });
long uptimeSeconds = this->uptime_as_seconds();
auto workSystemTimeSeconds = std::chrono::seconds(uptimeSeconds);
auto currentSystemTime = std::chrono::system_clock::now();
auto startSystemTime = currentSystemTime - workSystemTimeSeconds;
long workSystemDays = uptimeSeconds / 86400;
auto label = fmt::format(ALabel::format_, fmt::arg("up_H", fmt::format("{:%H}", startSystemTime)),
fmt::arg("up_M", fmt::format("{:%M}", startSystemTime)),
fmt::arg("up_d", fmt::format("{:%d}", startSystemTime)),
fmt::arg("up_m", fmt::format("{:%m}", startSystemTime)),
fmt::arg("up_Y", fmt::format("{:%Y}", startSystemTime)),
fmt::arg("work_d", workSystemDays),
fmt::arg("work_H", fmt::format("{:%H}", workSystemTimeSeconds)),
fmt::arg("work_M", fmt::format("{:%M}", workSystemTimeSeconds)),
fmt::arg("work_S", fmt::format("{:%S}", workSystemTimeSeconds)),
fmt::arg("user", systemUser));
ALabel::label_.set_markup(label);
ALabel::update();
}
}; // namespace waybar::modules