Merge remote-tracking branch 'origin/master' into tray-gdbus

This commit is contained in:
topisani 2018-10-04 18:04:36 +02:00
commit b231054b69
21 changed files with 132 additions and 63 deletions

View File

@ -20,6 +20,8 @@
namespace waybar { namespace waybar {
class Bar;
class Factory { class Factory {
public: public:
Factory(Bar& bar, const Json::Value& config); Factory(Bar& bar, const Json::Value& config);

View File

@ -13,10 +13,12 @@ class Custom : public ALabel {
Custom(const std::string, const Json::Value&); Custom(const std::string, const Json::Value&);
auto update() -> void; auto update() -> void;
private: private:
void worker(); void delayWorker();
void continuousWorker();
const std::string name_; const std::string name_;
waybar::util::SleeperThread thread_; waybar::util::SleeperThread thread_;
waybar::util::command::res output_;
}; };
} }

View File

@ -17,8 +17,8 @@ class Network : public ALabel {
~Network(); ~Network();
auto update() -> void; auto update() -> void;
private: private:
static uint64_t netlinkRequest(int, void*, uint32_t, uint32_t groups = 0); static int netlinkRequest(int, void*, uint32_t, uint32_t groups = 0);
static uint64_t netlinkResponse(int, void*, uint32_t, uint32_t groups = 0); static int netlinkResponse(int, void*, uint32_t, uint32_t groups = 0);
static int scanCb(struct nl_msg*, void*); static int scanCb(struct nl_msg*, void*);
void disconnected(); void disconnected();
@ -33,7 +33,7 @@ class Network : public ALabel {
int ifid_; int ifid_;
sa_family_t family_; sa_family_t family_;
int sock_fd_; int sock_fd_;
struct sockaddr_nl nladdr_ = {0}; struct sockaddr_nl nladdr_ = {};
struct nl_sock* sk_ = nullptr; struct nl_sock* sk_ = nullptr;
int nl80211_id_; int nl80211_id_;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <fmt/format.h> #include <fmt/format.h>
#include <tuple>
#include "bar.hpp" #include "bar.hpp"
#include "client.hpp" #include "client.hpp"
#include "util/chrono.hpp" #include "util/chrono.hpp"
@ -16,7 +17,7 @@ class Window : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
void worker(); void worker();
std::string getFocusedNode(Json::Value nodes); std::tuple<int, std::string> getFocusedNode(Json::Value nodes);
void getFocusedWindow(); void getFocusedWindow();
Bar& bar_; Bar& bar_;
@ -24,6 +25,7 @@ class Window : public ALabel {
util::JsonParser parser_; util::JsonParser parser_;
Ipc ipc_; Ipc ipc_;
std::string window_; std::string window_;
int windowId_;
}; };
} }

View File

@ -18,7 +18,7 @@ class Workspaces : public IModule {
private: private:
void worker(); void worker();
void addWorkspace(Json::Value); void addWorkspace(Json::Value);
std::string getIcon(std::string); std::string getIcon(std::string, Json::Value);
bool handleScroll(GdkEventScroll*); bool handleScroll(GdkEventScroll*);
int getPrevWorkspace(); int getPrevWorkspace();
int getNextWorkspace(); int getNextWorkspace();

View File

@ -70,7 +70,7 @@ struct SleeperThread {
condvar_.notify_all(); condvar_.notify_all();
} }
~SleeperThread() auto stop()
{ {
do_run_ = false; do_run_ = false;
condvar_.notify_all(); condvar_.notify_all();
@ -79,6 +79,11 @@ struct SleeperThread {
} }
} }
~SleeperThread()
{
stop();
}
private: private:
std::thread thread_; std::thread thread_;
std::condition_variable condvar_; std::condition_variable condvar_;

View File

@ -4,12 +4,12 @@
namespace waybar::util::command { namespace waybar::util::command {
struct cmd_res { struct res {
int exit_code; int exit_code;
std::string out; std::string out;
}; };
inline struct cmd_res exec(const std::string cmd) inline struct res exec(const std::string cmd)
{ {
FILE* fp(popen(cmd.c_str(), "r")); FILE* fp(popen(cmd.c_str(), "r"));
if (!fp) { if (!fp) {

View File

@ -56,7 +56,7 @@ src_files = files(
'src/client.cpp' 'src/client.cpp'
) )
if find_program('sway').found() if find_program('sway', required : false).found()
add_project_arguments('-DHAVE_SWAY', language: 'cpp') add_project_arguments('-DHAVE_SWAY', language: 'cpp')
src_files += [ src_files += [
'src/modules/sway/ipc/client.cpp', 'src/modules/sway/ipc/client.cpp',

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -8,17 +8,21 @@
"modules-center": ["sway/window"], "modules-center": ["sway/window"],
"modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock", "tray"], "modules-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock", "tray"],
// Modules configuration // Modules configuration
"sway/workspaces": { // "sway/workspaces": {
// "disable-scroll": true, // "disable-scroll": true,
// "all-outputs": true, // "all-outputs": true,
// "format-icons": { // "format": "{name}: {icon}",
// "1": "", // "format-icons": {
// "2": "", // "1": "",
// "3": "", // "2": "",
// "4": "", // "3": "",
// "5": "" // "4": "",
// } // "5": "",
}, // "urgent": "",
// "focused": "",
// "default": ""
// }
// },
"sway/window": { "sway/window": {
"max-length": 50 "max-length": 50
}, },
@ -58,6 +62,7 @@
"custom/spotify": { "custom/spotify": {
"format": " {}", "format": " {}",
"max-length": 40, "max-length": 40,
"interval": 30, // Remove this if your script is endless and write in loop
"exec": "$HOME/.config/waybar/mediaplayer.sh", // Script in resources folder "exec": "$HOME/.config/waybar/mediaplayer.sh", // Script in resources folder
"exec-if": "pgrep spotify" "exec-if": "pgrep spotify"
} }

View File

@ -18,10 +18,6 @@ window {
border-bottom: 3px solid transparent; border-bottom: 3px solid transparent;
} }
#workspaces button.icon label {
font-size: 10px;
}
#workspaces button.focused { #workspaces button.focused {
background: #64727D; background: #64727D;
border-bottom: 3px solid white; border-bottom: 3px solid white;

View File

@ -24,7 +24,7 @@ auto waybar::ALabel::update() -> void
// Nothing here // Nothing here
} }
bool waybar::ALabel::handleToggle(GdkEventButton* const& ev) bool waybar::ALabel::handleToggle(GdkEventButton* const& /*ev*/)
{ {
alt = !alt; alt = !alt;
if (alt) { if (alt) {

View File

@ -14,10 +14,10 @@ waybar::IModule* waybar::Factory::makeModule(const std::string &name) const
if (name == "sway/workspaces") { if (name == "sway/workspaces") {
return new waybar::modules::sway::Workspaces(bar_, config_[name]); return new waybar::modules::sway::Workspaces(bar_, config_[name]);
} }
#endif
if (name == "sway/window") { if (name == "sway/window") {
return new waybar::modules::sway::Window(bar_, config_[name]); return new waybar::modules::sway::Window(bar_, config_[name]);
} }
#endif
if (name == "memory") { if (name == "memory") {
return new waybar::modules::Memory(config_[name]); return new waybar::modules::Memory(config_[name]);
} }

View File

@ -13,7 +13,7 @@ int main(int argc, char* argv[])
try { try {
waybar::Client c(argc, argv); waybar::Client c(argc, argv);
waybar::client = &c; waybar::client = &c;
std::signal(SIGUSR1, [] (int signal) { std::signal(SIGUSR1, [] (int /*signal*/) {
for (auto& bar : waybar::client->bars) { for (auto& bar : waybar::client->bars) {
(*bar).toggle(); (*bar).toggle();
} }

View File

@ -13,7 +13,7 @@ waybar::modules::Cpu::Cpu(const Json::Value& config)
auto waybar::modules::Cpu::update() -> void auto waybar::modules::Cpu::update() -> void
{ {
struct sysinfo info = {0}; struct sysinfo info = {};
if (sysinfo(&info) == 0) { if (sysinfo(&info) == 0) {
float f_load = 1.f / (1u << SI_LOAD_SHIFT); float f_load = 1.f / (1u << SI_LOAD_SHIFT);
uint16_t load = info.loads[0] * f_load * 100 / get_nprocs(); uint16_t load = info.loads[0] * f_load * 100 / get_nprocs();

View File

@ -7,12 +7,16 @@ waybar::modules::Custom::Custom(const std::string name,
if (!config_["exec"]) { if (!config_["exec"]) {
throw std::runtime_error(name_ + " has no exec path."); throw std::runtime_error(name_ + " has no exec path.");
} }
worker(); if (config_["interval"]) {
delayWorker();
} else {
continuousWorker();
}
} }
void waybar::modules::Custom::worker() void waybar::modules::Custom::delayWorker()
{ {
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30; auto interval = config_["interval"].asUInt();
thread_ = [this, interval] { thread_ = [this, interval] {
bool can_update = true; bool can_update = true;
if (config_["exec-if"]) { if (config_["exec-if"]) {
@ -24,25 +28,53 @@ void waybar::modules::Custom::worker()
} }
} }
if (can_update) { if (can_update) {
output_ = waybar::util::command::exec(config_["exec"].asString());
dp.emit(); dp.emit();
} }
thread_.sleep_for(chrono::seconds(interval)); thread_.sleep_for(chrono::seconds(interval));
}; };
} }
void waybar::modules::Custom::continuousWorker()
{
auto cmd = config_["exec"].asString();
FILE* fp(popen(cmd.c_str(), "r"));
if (!fp) {
throw std::runtime_error("Unable to open " + cmd);
}
thread_ = [this, fp] {
char* buff = nullptr;
size_t len = 0;
if (getline(&buff, &len, fp) == -1) {
pclose(fp);
thread_.stop();
output_ = { 1, "" };
dp.emit();
return;
}
std::string output = buff;
// Remove last newline
if (!output.empty() && output[output.length()-1] == '\n') {
output.erase(output.length()-1);
}
output_ = { 0, output };
dp.emit();
};
}
auto waybar::modules::Custom::update() -> void auto waybar::modules::Custom::update() -> void
{ {
auto res = waybar::util::command::exec(config_["exec"].asString());
// Hide label if output is empty // Hide label if output is empty
if (res.out.empty() || res.exit_code != 0) { if (output_.out.empty() || output_.exit_code != 0) {
label_.hide(); label_.hide();
label_.set_name(""); label_.set_name("");
} else { } else {
label_.set_name("custom-" + name_); label_.set_name("custom-" + name_);
auto str = fmt::format(format_, res.out); auto str = fmt::format(format_, output_.out);
label_.set_text(str); label_.set_text(str);
label_.set_tooltip_text(str); label_.set_tooltip_text(str);
label_.show(); label_.show();
} }
} }

View File

@ -13,7 +13,7 @@ waybar::modules::Memory::Memory(const Json::Value& config)
auto waybar::modules::Memory::update() -> void auto waybar::modules::Memory::update() -> void
{ {
struct sysinfo info = {0}; struct sysinfo info = {};
if (sysinfo(&info) == 0) { if (sysinfo(&info) == 0) {
auto total = info.totalram * info.mem_unit; auto total = info.totalram * info.mem_unit;
auto freeram = info.freeram * info.mem_unit; auto freeram = info.freeram * info.mem_unit;

View File

@ -165,7 +165,7 @@ int waybar::modules::Network::getExternalInterface()
* consume responses till NLMSG_DONE/NLMSG_ERROR is encountered). * consume responses till NLMSG_DONE/NLMSG_ERROR is encountered).
*/ */
do { do {
uint64_t len = netlinkResponse(sock_fd_, resp, route_buffer_size); auto len = netlinkResponse(sock_fd_, resp, route_buffer_size);
if (len < 0) { if (len < 0) {
goto out; goto out;
} }
@ -255,10 +255,10 @@ out:
return ifidx; return ifidx;
} }
uint64_t waybar::modules::Network::netlinkRequest(int fd, void *req, int waybar::modules::Network::netlinkRequest(int fd, void *req,
uint32_t reqlen, uint32_t groups) uint32_t reqlen, uint32_t groups)
{ {
struct sockaddr_nl sa = {0}; struct sockaddr_nl sa = {};
sa.nl_family = AF_NETLINK; sa.nl_family = AF_NETLINK;
sa.nl_groups = groups; sa.nl_groups = groups;
struct iovec iov = { req, reqlen }; struct iovec iov = { req, reqlen };
@ -266,11 +266,11 @@ uint64_t waybar::modules::Network::netlinkRequest(int fd, void *req,
return sendmsg(fd, &msg, 0); return sendmsg(fd, &msg, 0);
} }
uint64_t waybar::modules::Network::netlinkResponse(int fd, void *resp, int waybar::modules::Network::netlinkResponse(int fd, void *resp,
uint32_t resplen, uint32_t groups) uint32_t resplen, uint32_t groups)
{ {
uint64_t ret; int ret;
struct sockaddr_nl sa = {0}; struct sockaddr_nl sa = {};
sa.nl_family = AF_NETLINK; sa.nl_family = AF_NETLINK;
sa.nl_groups = groups; sa.nl_groups = groups;
struct iovec iov = { resp, resplen }; struct iovec iov = { resp, resplen };

View File

@ -1,7 +1,7 @@
#include "modules/sway/window.hpp" #include "modules/sway/window.hpp"
waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config) waybar::modules::sway::Window::Window(Bar &bar, const Json::Value& config)
: ALabel(config, "{}"), bar_(bar) : ALabel(config, "{}"), bar_(bar), windowId_(-1)
{ {
label_.set_name("window"); label_.set_name("window");
ipc_.connect(); ipc_.connect();
@ -20,6 +20,13 @@ void waybar::modules::sway::Window::worker()
if ((parsed["change"] == "focus" || parsed["change"] == "title") if ((parsed["change"] == "focus" || parsed["change"] == "title")
&& parsed["container"]["focused"].asBool()) { && parsed["container"]["focused"].asBool()) {
window_ = parsed["container"]["name"].asString(); window_ = parsed["container"]["name"].asString();
windowId_ = parsed["container"]["id"].asInt();
dp.emit();
} else if (parsed["change"] == "close"
&& parsed["container"]["focused"].asBool()
&& windowId_ == parsed["container"]["id"].asInt()) {
window_.clear();
windowId_ = -1;
dp.emit(); dp.emit();
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
@ -34,18 +41,19 @@ auto waybar::modules::sway::Window::update() -> void
label_.set_tooltip_text(window_); label_.set_tooltip_text(window_);
} }
std::string waybar::modules::sway::Window::getFocusedNode(Json::Value nodes) std::tuple<int, std::string> waybar::modules::sway::Window::getFocusedNode(
Json::Value nodes)
{ {
for (auto &node : nodes) { for (auto &node : nodes) {
if (node["focused"].asBool() && node["type"] == "con") { if (node["focused"].asBool() && node["type"] == "con") {
return node["name"].asString(); return { node["id"].asInt(), node["name"].asString() };
} }
auto res = getFocusedNode(node["nodes"]); auto [id, name] = getFocusedNode(node["nodes"]);
if (!res.empty()) { if (id > -1 && !name.empty()) {
return res; return { id, name };
} }
} }
return std::string(); return { -1, std::string() };
} }
void waybar::modules::sway::Window::getFocusedWindow() void waybar::modules::sway::Window::getFocusedWindow()
@ -53,7 +61,9 @@ void waybar::modules::sway::Window::getFocusedWindow()
try { try {
auto res = ipc_.sendCmd(IPC_GET_TREE); auto res = ipc_.sendCmd(IPC_GET_TREE);
auto parsed = parser_.parse(res.payload); auto parsed = parser_.parse(res.payload);
window_ = getFocusedNode(parsed["nodes"]); auto [id, name] = getFocusedNode(parsed["nodes"]);
windowId_ = id;
window_ = name;
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update)); Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
} catch (const std::exception &e) { } catch (const std::exception &e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;

View File

@ -78,6 +78,14 @@ auto waybar::modules::sway::Workspaces::update() -> void
if (needReorder) { if (needReorder) {
box_.reorder_child(button, node["num"].asInt()); box_.reorder_child(button, node["num"].asInt());
} }
auto icon = getIcon(node["name"].asString(), node);
if (config_["format"]) {
auto format = config_["format"].asString();
button.set_label(fmt::format(format, fmt::arg("icon", icon),
fmt::arg("name", node["name"].asString())));
} else {
button.set_label(icon);
}
button.show(); button.show();
} }
} }
@ -88,12 +96,13 @@ auto waybar::modules::sway::Workspaces::update() -> void
void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node) void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
{ {
auto icon = getIcon(node["name"].asString()); auto icon = getIcon(node["name"].asString(), node);
auto pair = buttons_.emplace(node["num"].asInt(), icon); auto format = config_["format"]
? fmt::format(config_["format"].asString(), fmt::arg("icon", icon),
fmt::arg("name", node["name"].asString()))
: icon;
auto pair = buttons_.emplace(node["num"].asInt(), format);
auto &button = pair.first->second; auto &button = pair.first->second;
if (icon != node["name"].asString()) {
button.get_style_context()->add_class("icon");
}
box_.pack_start(button, false, false, 0); box_.pack_start(button, false, false, 0);
button.set_relief(Gtk::RELIEF_NONE); button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] { button.signal_clicked().connect([this, pair] {
@ -123,13 +132,19 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
button.show(); button.show();
} }
std::string waybar::modules::sway::Workspaces::getIcon(std::string name) std::string waybar::modules::sway::Workspaces::getIcon(std::string name,
Json::Value node)
{ {
if (config_["format-icons"][name]) { std::vector<std::string> keys = {
return config_["format-icons"][name].asString(); name, "urgent", "focused", "visible", "default"};
} for (auto const& key : keys) {
if (config_["format-icons"]["default"]) { if (key == "focused" || key == "visible" || key == "urgent") {
return config_["format-icons"]["default"].asString(); if (config_["format-icons"][key] && node[key].asBool()) {
return config_["format-icons"][key].asString();
}
} else if (config_["format-icons"][key]) {
return config_["format-icons"][key].asString();
}
} }
return name; return name;
} }