Merge branch 'master' into output-exclusion

This commit is contained in:
Alex
2020-04-17 23:42:58 +02:00
committed by GitHub
56 changed files with 555 additions and 70 deletions

View File

@ -31,7 +31,7 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
}
auto ALabel::update() -> void {
// Nothing here
AModule::update();
}
std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) {

View File

@ -29,7 +29,10 @@ AModule::~AModule() {
}
auto AModule::update() -> void {
// Nothing here
// Run user-provided update handler if configured
if (config_["on-update"].isString()) {
pid_.push_back(util::command::forkExec(config_["on-update"].asString()));
}
}
bool AModule::handleToggle(GdkEventButton* const& e) {

View File

@ -175,6 +175,11 @@ void waybar::Bar::initGtkLayerShell() {
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, margins_.right);
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_TOP, margins_.top);
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, margins_.bottom);
if (width_ > 1 && height_ > 1) {
/* configure events are not emitted if the bar is using initial size */
setExclusiveZone(width_, height_);
}
}
#endif

View File

@ -66,6 +66,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "temperature") {
return new waybar::modules::Temperature(id, config_[name]);
}
if (ref == "bluetooth") {
return new waybar::modules::Bluetooth(id, config_[name]);
}
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
return new waybar::modules::Custom(ref.substr(7), id, config_[name]);
}

View File

@ -187,6 +187,8 @@ auto waybar::modules::Backlight::update() -> void {
}
previous_best_ = best == nullptr ? std::nullopt : std::optional{*best};
previous_format_ = format_;
// Call parent update
ALabel::update();
}
template <class ForwardIt>

View File

@ -115,6 +115,16 @@ const std::tuple<uint8_t, float, std::string> waybar::modules::Battery::getInfos
time_remaining = -(float)(total_energy_full - total_energy) / total_power;
}
uint16_t capacity = total / batteries_.size();
// Handle full-at
if (config_["full-at"].isUInt()) {
auto full_at = config_["full-at"].asUInt();
if (full_at < 100) {
capacity = static_cast<float>(capacity / full_at) * 100;
if (capacity > full_at) {
capacity = full_at;
}
}
}
return {capacity, time_remaining, status};
} catch (const std::exception& e) {
spdlog::error("Battery: {}", e.what());
@ -192,4 +202,6 @@ auto waybar::modules::Battery::update() -> void {
fmt::arg("icon", getIcon(capacity, state)),
fmt::arg("time", formatTimeRemaining(time_remaining))));
}
// Call parent update
ALabel::update();
}

45
src/modules/bluetooth.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "modules/bluetooth.hpp"
#include "util/rfkill.hpp"
#include <linux/rfkill.h>
#include <time.h>
waybar::modules::Bluetooth::Bluetooth(const std::string& id, const Json::Value& config)
: ALabel(config, "bluetooth", id, "{icon}", 10),
status_("disabled"),
rfkill_{RFKILL_TYPE_BLUETOOTH} {
thread_ = [this] {
dp.emit();
rfkill_.waitForEvent();
};
intervall_thread_ = [this] {
auto now = std::chrono::system_clock::now();
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());
thread_.sleep_until(timeout - diff);
dp.emit();
};
}
auto waybar::modules::Bluetooth::update() -> void {
if (rfkill_.getState()) {
status_ = "disabled";
} else {
status_ = "enabled";
}
label_.set_markup(
fmt::format(
format_,
fmt::arg("status", status_),
fmt::arg("icon", getIcon(0, status_))));
if (tooltipEnabled()) {
if (config_["tooltip-format"].isString()) {
auto tooltip_format = config_["tooltip-format"].asString();
auto tooltip_text = fmt::format(tooltip_format, status_);
label_.set_tooltip_text(tooltip_text);
} else {
label_.set_tooltip_text(status_);
}
}
}

View File

@ -54,6 +54,8 @@ auto waybar::modules::Clock::update() -> void {
label_.set_tooltip_markup(text);
}
}
// Call parent update
ALabel::update();
}
auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string {

View File

@ -18,6 +18,8 @@ auto waybar::modules::Cpu::update() -> void {
}
label_.set_markup(fmt::format(format_, fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage)));
getState(cpu_usage);
// Call parent update
ALabel::update();
}
uint16_t waybar::modules::Cpu::getCpuLoad() {

View File

@ -49,19 +49,24 @@ void waybar::modules::Custom::continuousWorker() {
thread_ = [&] {
char* buff = nullptr;
size_t len = 0;
bool restart = false;
if (getline(&buff, &len, fp_) == -1) {
int exit_code = 1;
if (fp_) {
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
fp_ = nullptr;
}
thread_.stop();
if (exit_code != 0) {
output_ = {exit_code, ""};
dp.emit();
spdlog::error("{} stopped unexpectedly, is it endless?", name_);
}
return;
if (config_["restart-interval"].isUInt()) {
restart = true;
} else {
thread_.stop();
return;
}
}
std::string output = buff;
@ -71,6 +76,14 @@ void waybar::modules::Custom::continuousWorker() {
}
output_ = {0, output};
dp.emit();
if (restart) {
pid_ = -1;
fp_ = util::command::open(cmd, pid_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt()));
}
};
}
@ -128,6 +141,8 @@ auto waybar::modules::Custom::update() -> void {
event_box_.show();
}
}
// Call parent update
ALabel::update();
}
void waybar::modules::Custom::parseOutputRaw() {

View File

@ -73,4 +73,6 @@ auto waybar::modules::Disk::update() -> void {
));
}
event_box_.show();
// Call parent update
ALabel::update();
}

View File

@ -32,6 +32,8 @@ auto waybar::modules::IdleInhibitor::update() -> void {
if (tooltipEnabled()) {
label_.set_tooltip_text(status_);
}
// Call parent update
ALabel::update();
}
bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {

View File

@ -10,11 +10,23 @@ waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config
auto waybar::modules::Memory::update() -> void {
parseMeminfo();
if (memtotal_ > 0 && memfree_ >= 0) {
auto total_ram_gigabytes = memtotal_ / std::pow(1024, 2);
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
auto available_ram_gigabytes = memfree_ / std::pow(1024, 2);
unsigned long memtotal = meminfo_["MemTotal"];
unsigned long memfree;
if (meminfo_.count("MemAvailable")) {
// New kernels (3.4+) have an accurate available memory field.
memfree = meminfo_["MemAvailable"];
} else {
// Old kernel; give a best-effort approximation of available memory.
memfree = meminfo_["MemFree"] + meminfo_["Buffers"] + meminfo_["Cached"] +
meminfo_["SReclaimable"] - meminfo_["Shmem"];
}
if (memtotal > 0 && memfree >= 0) {
auto total_ram_gigabytes = memtotal / std::pow(1024, 2);
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
auto used_ram_gigabytes = (memtotal - memfree) / std::pow(1024, 2);
auto available_ram_gigabytes = memfree / std::pow(1024, 2);
getState(used_ram_percentage);
label_.set_markup(fmt::format(format_,
@ -30,10 +42,11 @@ auto waybar::modules::Memory::update() -> void {
} else {
event_box_.hide();
}
// Call parent update
ALabel::update();
}
void waybar::modules::Memory::parseMeminfo() {
int64_t memfree = -1, membuffer = -1, memcache = -1, memavail = -1;
std::ifstream info(data_dir_);
if (!info.is_open()) {
throw std::runtime_error("Can't open " + data_dir_);
@ -44,23 +57,9 @@ void waybar::modules::Memory::parseMeminfo() {
if (posDelim == std::string::npos) {
continue;
}
std::string name = line.substr(0, posDelim);
int64_t value = std::stol(line.substr(posDelim + 1));
if (name.compare("MemTotal") == 0) {
memtotal_ = value;
} else if (name.compare("MemAvailable") == 0) {
memavail = value;
} else if (name.compare("MemFree") == 0) {
memfree = value;
} else if (name.compare("Buffers") == 0) {
membuffer = value;
} else if (name.compare("Cached") == 0) {
memcache = value;
}
if (memtotal_ > 0 && (memavail >= 0 || (memfree > -1 && membuffer > -1 && memcache > -1))) {
break;
}
meminfo_[name] = value;
}
memfree_ = memavail >= 0 ? memavail : memfree + membuffer + memcache;
}

View File

@ -56,6 +56,9 @@ auto waybar::modules::MPD::update() -> void {
}
setLabel();
// Call parent update
ALabel::update();
}
std::thread waybar::modules::MPD::event_listener() {

View File

@ -4,7 +4,7 @@
#include <fstream>
#include <cassert>
#include "util/format.hpp"
#include "util/rfkill.hpp"
namespace {
@ -86,7 +86,8 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
cidr_(-1),
signal_strength_dbm_(0),
signal_strength_(0),
frequency_(0) {
frequency_(0),
rfkill_{RFKILL_TYPE_WLAN} {
auto down_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_DOWN_TOTAL_KEY);
auto up_octets = read_netstat(BANDWIDTH_CATEGORY, BANDWIDTH_UP_TOTAL_KEY);
if (down_octets) {
@ -196,6 +197,7 @@ void waybar::modules::Network::createInfoSocket() {
}
void waybar::modules::Network::worker() {
// update via here not working
thread_timer_ = [this] {
{
std::lock_guard<std::mutex> lock(mutex_);
@ -206,6 +208,16 @@ void waybar::modules::Network::worker() {
}
thread_timer_.sleep_for(interval_);
};
thread_rfkill_ = [this] {
rfkill_.waitForEvent();
{
std::lock_guard<std::mutex> lock(mutex_);
if (ifid_ > 0) {
getInfo();
dp.emit();
}
}
};
thread_ = [this] {
std::array<struct epoll_event, EPOLL_MAX> events{};
@ -222,7 +234,11 @@ void waybar::modules::Network::worker() {
}
const std::string waybar::modules::Network::getNetworkState() const {
if (ifid_ == -1) return "disconnected";
if (ifid_ == -1) {
if (rfkill_.getState())
return "disabled";
return "disconnected";
}
if (ipaddr_.empty()) return "linked";
if (essid_.empty()) return "ethernet";
return "wifi";
@ -315,6 +331,9 @@ auto waybar::modules::Network::update() -> void {
label_.set_tooltip_text(text);
}
}
// Call parent update
ALabel::update();
}
// Based on https://gist.github.com/Yawning/c70d804d4b8ae78cc698

View File

@ -21,7 +21,7 @@ waybar::modules::Pulseaudio::Pulseaudio(const std::string &id, const Json::Value
if (context_ == nullptr) {
throw std::runtime_error("pa_context_new() failed.");
}
if (pa_context_connect(context_, nullptr, PA_CONTEXT_NOAUTOSPAWN, nullptr) < 0) {
if (pa_context_connect(context_, nullptr, PA_CONTEXT_NOFAIL, nullptr) < 0) {
auto err =
fmt::format("pa_context_connect() failed: {}", pa_strerror(pa_context_errno(context_)));
throw std::runtime_error(err);
@ -215,7 +215,11 @@ auto waybar::modules::Pulseaudio::update() -> void {
} else {
label_.get_style_context()->remove_class("bluetooth");
}
if (muted_ ) {
if (muted_) {
// Check muted bluetooth format exist, otherwise fallback to default muted format
if (format_name != "format" && !config_[format_name + "-muted"].isString()) {
format_name = "format";
}
format_name = format_name + "-muted";
label_.get_style_context()->add_class("muted");
} else {
@ -241,4 +245,7 @@ auto waybar::modules::Pulseaudio::update() -> void {
if (tooltipEnabled()) {
label_.set_tooltip_text(desc_);
}
// Call parent update
ALabel::update();
}

View File

@ -40,6 +40,8 @@ auto Tray::update() -> void {
} else {
box_.show_all();
}
// Call parent update
AModule::update();
}
} // namespace waybar::modules::SNI

View File

@ -47,6 +47,8 @@ auto Mode::update() -> void {
}
event_box_.show();
}
// Call parent update
ALabel::update();
}
} // namespace waybar::modules::sway

View File

@ -60,6 +60,8 @@ auto Window::update() -> void {
if (tooltipEnabled()) {
label_.set_tooltip_text(window_);
}
// Call parent update
ALabel::update();
}
std::tuple<std::size_t, int, std::string, std::string> Window::getFocusedNode(

View File

@ -157,12 +157,13 @@ auto Workspaces::update() -> void {
if (needReorder) {
box_.reorder_child(button, it - workspaces_.begin());
}
std::string output = getIcon((*it)["name"].asString(), *it);
std::string output = (*it)["name"].asString();
if (config_["format"].isString()) {
auto format = config_["format"].asString();
output = fmt::format(format,
fmt::arg("icon", output),
fmt::arg("name", trimWorkspaceName((*it)["name"].asString())),
fmt::arg("icon", getIcon(output, *it)),
fmt::arg("value", output),
fmt::arg("name", trimWorkspaceName(output)),
fmt::arg("index", (*it)["num"].asString()));
}
if (!config_["disable-markup"].asBool()) {
@ -172,6 +173,8 @@ auto Workspaces::update() -> void {
}
onButtonReady(*it, button);
}
// Call parent update
AModule::update();
}
Gtk::Button &Workspaces::addButton(const Json::Value &node) {
@ -205,6 +208,8 @@ std::string Workspaces::getIcon(const std::string &name, const Json::Value &node
if (config_["format-icons"][key].isString() && node[key].asBool()) {
return config_["format-icons"][key].asString();
}
} else if (config_["format_icons"]["persistent"].isString() && node["target_output"].isString()) {
return config_["format-icons"]["persistent"].asString();
} else if (config_["format-icons"][key].isString()) {
return config_["format-icons"][key].asString();
}

View File

@ -1,9 +1,12 @@
#include "modules/temperature.hpp"
#include <filesystem>
waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config)
: ALabel(config, "temperature", id, "{temperatureC}°C", 10) {
if (config_["hwmon-path"].isString()) {
file_path_ = config_["hwmon-path"].asString();
} else if (config_["hwmon-path-abs"].isString() && config_["input-filename"].isString()) {
file_path_ = (*std::filesystem::directory_iterator(config_["hwmon-path-abs"].asString())).path().u8string() + "/" + config_["input-filename"].asString();
} else {
auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0;
file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone);
@ -33,6 +36,8 @@ auto waybar::modules::Temperature::update() -> void {
fmt::arg("temperatureC", temperature_c),
fmt::arg("temperatureF", temperature_f),
fmt::arg("icon", getIcon(temperature_c, "", max_temp))));
// Call parent update
ALabel::update();
}
std::tuple<uint16_t, uint16_t> waybar::modules::Temperature::getTemperature() {

83
src/util/rfkill.cpp Normal file
View File

@ -0,0 +1,83 @@
/* https://git.kernel.org/pub/scm/linux/kernel/git/jberg/rfkill.git/
*
* Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
* Copyright 2009 Marcel Holtmann <marcel@holtmann.org>
* Copyright 2009 Tim Gardner <tim.gardner@canonical.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include "util/rfkill.hpp"
#include <linux/rfkill.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <fcntl.h>
#include <sys/poll.h>
#include <cerrno>
#include <stdexcept>
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type)
: rfkill_type_(rfkill_type) {
}
void waybar::util::Rfkill::waitForEvent() {
struct rfkill_event event;
struct pollfd p;
ssize_t len;
int fd, n;
fd = open("/dev/rfkill", O_RDONLY);
if (fd < 0) {
throw std::runtime_error("Can't open RFKILL control device");
return;
}
memset(&p, 0, sizeof(p));
p.fd = fd;
p.events = POLLIN | POLLHUP;
while (1) {
n = poll(&p, 1, -1);
if (n < 0) {
throw std::runtime_error("Failed to poll RFKILL control device");
break;
}
if (n == 0)
continue;
len = read(fd, &event, sizeof(event));
if (len < 0) {
throw std::runtime_error("Reading of RFKILL events failed");
break;
}
if (len != RFKILL_EVENT_SIZE_V1) {
throw std::runtime_error("Wrong size of RFKILL event");
continue;
}
if(event.type == rfkill_type_ && event.op == RFKILL_OP_CHANGE) {
state_ = event.soft || event.hard;
break;
}
}
close(fd);
return;
}
bool waybar::util::Rfkill::getState() const {
return state_;
}