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

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

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;
}