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 {
class Bar;
class Factory {
public:
Factory(Bar& bar, const Json::Value& config);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,12 +4,12 @@
namespace waybar::util::command {
struct cmd_res {
struct res {
int exit_code;
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"));
if (!fp) {

View File

@ -56,7 +56,7 @@ src_files = files(
'src/client.cpp'
)
if find_program('sway').found()
if find_program('sway', required : false).found()
add_project_arguments('-DHAVE_SWAY', language: 'cpp')
src_files += [
'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-right": ["pulseaudio", "network", "cpu", "memory", "battery", "clock", "tray"],
// Modules configuration
"sway/workspaces": {
// "disable-scroll": true,
// "all-outputs": true,
// "format-icons": {
// "1": "",
// "2": "",
// "3": "",
// "4": "",
// "5": ""
// }
},
// "sway/workspaces": {
// "disable-scroll": true,
// "all-outputs": true,
// "format": "{name}: {icon}",
// "format-icons": {
// "1": "",
// "2": "",
// "3": "",
// "4": "",
// "5": "",
// "urgent": "",
// "focused": "",
// "default": ""
// }
// },
"sway/window": {
"max-length": 50
},
@ -58,6 +62,7 @@
"custom/spotify": {
"format": " {}",
"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-if": "pgrep spotify"
}

View File

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

View File

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

View File

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

View File

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

View File

@ -13,7 +13,7 @@ waybar::modules::Cpu::Cpu(const Json::Value& config)
auto waybar::modules::Cpu::update() -> void
{
struct sysinfo info = {0};
struct sysinfo info = {};
if (sysinfo(&info) == 0) {
float f_load = 1.f / (1u << SI_LOAD_SHIFT);
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"]) {
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] {
bool can_update = true;
if (config_["exec-if"]) {
@ -24,25 +28,53 @@ void waybar::modules::Custom::worker()
}
}
if (can_update) {
output_ = waybar::util::command::exec(config_["exec"].asString());
dp.emit();
}
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 res = waybar::util::command::exec(config_["exec"].asString());
// 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_.set_name("");
} else {
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_tooltip_text(str);
label_.show();
}
}
}

View File

@ -13,7 +13,7 @@ waybar::modules::Memory::Memory(const Json::Value& config)
auto waybar::modules::Memory::update() -> void
{
struct sysinfo info = {0};
struct sysinfo info = {};
if (sysinfo(&info) == 0) {
auto total = info.totalram * 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).
*/
do {
uint64_t len = netlinkResponse(sock_fd_, resp, route_buffer_size);
auto len = netlinkResponse(sock_fd_, resp, route_buffer_size);
if (len < 0) {
goto out;
}
@ -255,10 +255,10 @@ out:
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)
{
struct sockaddr_nl sa = {0};
struct sockaddr_nl sa = {};
sa.nl_family = AF_NETLINK;
sa.nl_groups = groups;
struct iovec iov = { req, reqlen };
@ -266,11 +266,11 @@ uint64_t waybar::modules::Network::netlinkRequest(int fd, void *req,
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)
{
uint64_t ret;
struct sockaddr_nl sa = {0};
int ret;
struct sockaddr_nl sa = {};
sa.nl_family = AF_NETLINK;
sa.nl_groups = groups;
struct iovec iov = { resp, resplen };

View File

@ -1,7 +1,7 @@
#include "modules/sway/window.hpp"
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");
ipc_.connect();
@ -20,6 +20,13 @@ void waybar::modules::sway::Window::worker()
if ((parsed["change"] == "focus" || parsed["change"] == "title")
&& parsed["container"]["focused"].asBool()) {
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();
}
} catch (const std::exception& e) {
@ -34,18 +41,19 @@ auto waybar::modules::sway::Window::update() -> void
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) {
if (node["focused"].asBool() && node["type"] == "con") {
return node["name"].asString();
return { node["id"].asInt(), node["name"].asString() };
}
auto res = getFocusedNode(node["nodes"]);
if (!res.empty()) {
return res;
auto [id, name] = getFocusedNode(node["nodes"]);
if (id > -1 && !name.empty()) {
return { id, name };
}
}
return std::string();
return { -1, std::string() };
}
void waybar::modules::sway::Window::getFocusedWindow()
@ -53,7 +61,9 @@ void waybar::modules::sway::Window::getFocusedWindow()
try {
auto res = ipc_.sendCmd(IPC_GET_TREE);
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));
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;

View File

@ -78,6 +78,14 @@ auto waybar::modules::sway::Workspaces::update() -> void
if (needReorder) {
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();
}
}
@ -88,12 +96,13 @@ auto waybar::modules::sway::Workspaces::update() -> void
void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
{
auto icon = getIcon(node["name"].asString());
auto pair = buttons_.emplace(node["num"].asInt(), icon);
auto icon = getIcon(node["name"].asString(), node);
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;
if (icon != node["name"].asString()) {
button.get_style_context()->add_class("icon");
}
box_.pack_start(button, false, false, 0);
button.set_relief(Gtk::RELIEF_NONE);
button.signal_clicked().connect([this, pair] {
@ -123,13 +132,19 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
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]) {
return config_["format-icons"][name].asString();
}
if (config_["format-icons"]["default"]) {
return config_["format-icons"]["default"].asString();
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] && node[key].asBool()) {
return config_["format-icons"][key].asString();
}
} else if (config_["format-icons"][key]) {
return config_["format-icons"][key].asString();
}
}
return name;
}