Merge branch 'master' into feat-rtsignal

pull/228/head
hoellen 2019-03-18 19:04:11 +01:00 committed by GitHub
commit 1f924c9c06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 118 additions and 122 deletions

View File

@ -21,8 +21,8 @@ class Network : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
static const uint8_t MAX_RETRY = 5; static const uint8_t MAX_RETRY = 5;
static const uint8_t EPOLL_MAX = 255;
static int handleEvents(struct nl_msg*, void*);
static int handleScan(struct nl_msg*, void*); static int handleScan(struct nl_msg*, void*);
void worker(); void worker();
@ -31,6 +31,7 @@ class Network : public ALabel {
void createEventSocket(); void createEventSocket();
int getExternalInterface(); int getExternalInterface();
void getInterfaceAddress(); void getInterfaceAddress();
void handleEvents();
int netlinkRequest(void*, uint32_t, uint32_t groups = 0); int netlinkRequest(void*, uint32_t, uint32_t groups = 0);
int netlinkResponse(void*, uint32_t, uint32_t groups = 0); int netlinkResponse(void*, uint32_t, uint32_t groups = 0);
void parseEssid(struct nlattr**); void parseEssid(struct nlattr**);
@ -44,7 +45,7 @@ class Network : public ALabel {
sa_family_t family_; sa_family_t family_;
struct sockaddr_nl nladdr_ = {0}; struct sockaddr_nl nladdr_ = {0};
struct nl_sock* sk_ = nullptr; struct nl_sock* sk_ = nullptr;
struct nl_sock* info_sock_ = nullptr; int info_sock_;
int efd_; int efd_;
int ev_fd_; int ev_fd_;
int nl80211_id_; int nl80211_id_;

View File

@ -23,9 +23,8 @@ class Workspaces : public IModule {
void onButtonReady(const Json::Value&, Gtk::Button&); void onButtonReady(const Json::Value&, Gtk::Button&);
std::string getIcon(const std::string&, const Json::Value&); std::string getIcon(const std::string&, const Json::Value&);
bool handleScroll(GdkEventScroll*); bool handleScroll(GdkEventScroll*);
std::string getPrevWorkspace(); const std::string getCycleWorkspace(uint8_t current, bool prev) const;
std::string getNextWorkspace(); uint16_t getWorkspaceIndex(const std::string &name) const;
uint16_t getWorkspaceIndex(const std::string &name);
std::string trimWorkspaceName(std::string); std::string trimWorkspaceName(std::string);
const Bar& bar_; const Bar& bar_;

View File

@ -232,7 +232,6 @@ void waybar::Bar::layerSurfaceHandleClosed(void* data,
struct zwlr_layer_surface_v1* /*surface*/) struct zwlr_layer_surface_v1* /*surface*/)
{ {
auto o = static_cast<waybar::Bar *>(data); auto o = static_cast<waybar::Bar *>(data);
std::cout << "Bar removed from output: " + o->output_name << std::endl;
zwlr_layer_surface_v1_destroy(o->layer_surface); zwlr_layer_surface_v1_destroy(o->layer_surface);
wl_output_destroy(*o->output); wl_output_destroy(*o->output);
zxdg_output_v1_destroy(o->xdg_output_); zxdg_output_v1_destroy(o->xdg_output_);

View File

@ -67,8 +67,10 @@ void waybar::Client::handleGlobalRemove(void* data,
{ {
auto o = static_cast<waybar::Client *>(data); auto o = static_cast<waybar::Client *>(data);
for (auto it = o->bars.begin(); it != o->bars.end(); ++it) { for (auto it = o->bars.begin(); it != o->bars.end(); ++it) {
if ((**it).wl_name == name) { if ((*it)->wl_name == name) {
auto output_name = (*it)->output_name;
o->bars.erase(it); o->bars.erase(it);
std::cout << "Bar removed from output: " + output_name << std::endl;
break; break;
} }
} }

View File

@ -66,7 +66,6 @@ void waybar::modules::Custom::continuousWorker()
} }
return; return;
} }
std::string output = buff; std::string output = buff;
// Remove last newline // Remove last newline

View File

@ -2,8 +2,8 @@
#include "modules/network.hpp" #include "modules/network.hpp"
waybar::modules::Network::Network(const std::string& id, const Json::Value& config) waybar::modules::Network::Network(const std::string& id, const Json::Value& config)
: ALabel(config, "{ifname}", 60), family_(AF_INET), efd_(-1), ev_fd_(-1), : ALabel(config, "{ifname}", 60), family_(AF_INET), info_sock_(-1), efd_(-1),
cidr_(-1), signal_strength_dbm_(0), signal_strength_(0) ev_fd_(-1), cidr_(-1), signal_strength_dbm_(0), signal_strength_(0)
{ {
label_.set_name("network"); label_.set_name("network");
if (!id.empty()) { if (!id.empty()) {
@ -39,11 +39,8 @@ waybar::modules::Network::~Network()
if (efd_ > -1) { if (efd_ > -1) {
close(efd_); close(efd_);
} }
if (info_sock_ != nullptr) { if (info_sock_ != -1) {
nl_socket_drop_membership(info_sock_, RTMGRP_LINK); close(info_sock_);
nl_socket_drop_membership(info_sock_, RTMGRP_IPV4_IFADDR);
nl_close(info_sock_);
nl_socket_free(info_sock_);
} }
if (sk_ != nullptr) { if (sk_ != nullptr) {
nl_close(sk_); nl_close(sk_);
@ -53,19 +50,18 @@ waybar::modules::Network::~Network()
void waybar::modules::Network::createInfoSocket() void waybar::modules::Network::createInfoSocket()
{ {
info_sock_ = nl_socket_alloc(); struct sockaddr_nl sa;
if (nl_connect(info_sock_, NETLINK_ROUTE) != 0) { info_sock_ = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (info_sock_ < 0) {
throw std::runtime_error("Can't connect network socket"); throw std::runtime_error("Can't connect network socket");
} }
if (nl_socket_add_membership(info_sock_, RTMGRP_LINK) != 0) { sa.nl_family = AF_NETLINK;
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE
| RTMGRP_IPV6_IFADDR | RTMGRP_IPV6_ROUTE;
auto ret = bind(info_sock_, (struct sockaddr *)&sa, sizeof(sa));
if (ret < 0) {
throw std::runtime_error("Can't add membership"); throw std::runtime_error("Can't add membership");
} }
if (nl_socket_add_membership(info_sock_, RTMGRP_IPV4_IFADDR) != 0) {
throw std::runtime_error("Can't add membership");
}
nl_socket_disable_seq_check(info_sock_);
nl_socket_set_nonblocking(info_sock_);
nl_socket_modify_cb(info_sock_, NL_CB_VALID, NL_CB_CUSTOM, handleEvents, this);
efd_ = epoll_create1(0); efd_ = epoll_create1(0);
if (efd_ < 0) { if (efd_ < 0) {
throw std::runtime_error("Can't create epoll"); throw std::runtime_error("Can't create epoll");
@ -80,11 +76,10 @@ void waybar::modules::Network::createInfoSocket()
} }
} }
{ {
auto fd = nl_socket_get_fd(info_sock_);
struct epoll_event event; struct epoll_event event;
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP; event.events = EPOLLIN | EPOLLET;
event.data.fd = fd; event.data.fd = info_sock_;
if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) { if (epoll_ctl(efd_, EPOLL_CTL_ADD, info_sock_, &event) == -1) {
throw std::runtime_error("Can't add epoll event"); throw std::runtime_error("Can't add epoll event");
} }
} }
@ -114,16 +109,15 @@ void waybar::modules::Network::worker()
} }
thread_timer_.sleep_for(interval_); thread_timer_.sleep_for(interval_);
}; };
thread_ = [this] { struct epoll_event events[EPOLL_MAX] = {0};
struct epoll_event events[16]; thread_ = [this, &events] {
int ec = epoll_wait(efd_, events, 16, -1); int ec = epoll_wait(efd_, events, EPOLL_MAX, -1);
if (ec > 0) { if (ec > 0) {
for (auto i = 0; i < ec; i++) { for (auto i = 0; i < ec; i++) {
if (events[i].data.fd == nl_socket_get_fd(info_sock_)) { if (events[i].data.fd == ev_fd_) {
nl_recvmsgs_default(info_sock_);
} else {
thread_.stop(); thread_.stop();
break; } else if (events[i].events & EPOLLIN) {
handleEvents();
} }
} }
} else if (ec == -1) { } else if (ec == -1) {
@ -377,7 +371,7 @@ int waybar::modules::Network::netlinkRequest(void *req,
sa.nl_groups = groups; sa.nl_groups = groups;
struct iovec iov = { req, reqlen }; struct iovec iov = { req, reqlen };
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 }; struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
return sendmsg(nl_socket_get_fd(info_sock_), &msg, 0); return sendmsg(info_sock_, &msg, 0);
} }
int waybar::modules::Network::netlinkResponse(void *resp, int waybar::modules::Network::netlinkResponse(void *resp,
@ -388,53 +382,64 @@ int waybar::modules::Network::netlinkResponse(void *resp,
sa.nl_groups = groups; sa.nl_groups = groups;
struct iovec iov = { resp, resplen }; struct iovec iov = { resp, resplen };
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 }; struct msghdr msg = { &sa, sizeof(sa), &iov, 1, nullptr, 0, 0 };
auto ret = recvmsg(nl_socket_get_fd(info_sock_), &msg, 0); auto ret = recvmsg(info_sock_, &msg, 0);
if (msg.msg_flags & MSG_TRUNC) { if (msg.msg_flags & MSG_TRUNC) {
return -1; return -1;
} }
return ret; return ret;
} }
int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) { void waybar::modules::Network::handleEvents() {
auto net = static_cast<waybar::modules::Network *>(data); struct sockaddr_nl addr;
bool need_update = false; char buff[2048] = {0};
nlmsghdr *nh = nlmsg_hdr(msg); socklen_t len = 0;
if (nh->nlmsg_type == RTM_NEWADDR) {
need_update = true; while (true) {
} len = sizeof(addr);
if (nh->nlmsg_type < RTM_NEWADDR) { auto ret = recvfrom(info_sock_, (void *)buff, sizeof(buff), 0,
auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh)); (struct sockaddr *)&addr, &len);
if (rtif->ifi_index == static_cast<int>(net->ifid_)) { auto nh = (struct nlmsghdr *)buff;
need_update = true; for(; NLMSG_OK(nh, ret); nh = NLMSG_NEXT(nh, ret)) {
if (!(rtif->ifi_flags & IFF_RUNNING)) { bool need_update = false;
net->disconnected(); if (nh->nlmsg_type == RTM_NEWADDR) {
net->dp.emit(); need_update = true;
} }
} if (nh->nlmsg_type < RTM_NEWADDR) {
} auto rtif = static_cast<struct ifinfomsg *>(NLMSG_DATA(nh));
if (net->ifid_ <= 0 && !net->config_["interface"].isString()) { if (rtif->ifi_index == static_cast<int>(ifid_)) {
for (uint8_t i = 0; i < MAX_RETRY; i += 1) { need_update = true;
net->ifid_ = net->getExternalInterface(); if (!(rtif->ifi_flags & IFF_RUNNING)) {
if (net->ifid_ > 0) { disconnected();
break; dp.emit();
return;
}
}
} }
// Need to wait before get external interface if (ifid_ <= 0 && !config_["interface"].isString()) {
net->thread_.sleep_for(std::chrono::seconds(1)); for (uint8_t i = 0; i < MAX_RETRY; i += 1) {
} ifid_ = getExternalInterface();
if (net->ifid_ > 0) { if (ifid_ > 0) {
char ifname[IF_NAMESIZE]; break;
if_indextoname(net->ifid_, ifname); }
net->ifname_ = ifname; // Need to wait before get external interface
need_update = true; thread_.sleep_for(std::chrono::seconds(1));
}
if (ifid_ > 0) {
char ifname[IF_NAMESIZE];
if_indextoname(ifid_, ifname);
ifname_ = ifname;
need_update = true;
}
}
if (need_update) {
if (ifid_ > 0) {
getInfo();
}
dp.emit();
}
break;
} }
} }
if (need_update) {
if (net->ifid_ > 0) {
net->getInfo();
}
net->dp.emit();
}
return NL_SKIP;
} }
int waybar::modules::Network::handleScan(struct nl_msg *msg, void *data) { int waybar::modules::Network::handleScan(struct nl_msg *msg, void *data) {

View File

@ -87,8 +87,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
} else { } else {
button.set_label(icon); button.set_label(icon);
} }
onButtonReady(node, button);
onButtonReady(node, button);
} }
} }
if (scrolling_) { if (scrolling_) {
@ -117,8 +116,8 @@ void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node)
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
}); });
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
if (!config_["disable-scroll"].asBool()) { if (!config_["disable-scroll"].asBool()) {
button.add_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event() button.signal_scroll_event()
.connect(sigc::mem_fun(*this, &Workspaces::handleScroll)); .connect(sigc::mem_fun(*this, &Workspaces::handleScroll));
} }
@ -158,86 +157,78 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
if (scrolling_) { if (scrolling_) {
return false; return false;
} }
std::lock_guard<std::mutex> lock(mutex_);
uint8_t idx;
scrolling_ = true; scrolling_ = true;
std::string name; for (idx = 0; idx < workspaces_.size(); idx += 1) {
uint16_t idx = 0; if (workspaces_[idx]["focused"].asBool()) {
{ break;
std::lock_guard<std::mutex> lock(mutex_);
for (; idx < workspaces_.size(); idx += 1) {
if (workspaces_[idx]["focused"].asBool()) {
name = workspaces_[idx]["name"].asString();
break;
}
} }
} }
if (name.empty()) { if (idx == workspaces_.size()) {
scrolling_ = false; scrolling_ = false;
return false; return false;
} }
std::string name;
if (e->direction == GDK_SCROLL_UP) { if (e->direction == GDK_SCROLL_UP) {
name = getNextWorkspace(); name = getCycleWorkspace(idx, false);
} }
if (e->direction == GDK_SCROLL_DOWN) { if (e->direction == GDK_SCROLL_DOWN) {
name = getPrevWorkspace(); name = getCycleWorkspace(idx, true);
} }
if (e->direction == GDK_SCROLL_SMOOTH) { if (e->direction == GDK_SCROLL_SMOOTH) {
gdouble delta_x, delta_y; gdouble delta_x, delta_y;
gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e), gdk_event_get_scroll_deltas(reinterpret_cast<const GdkEvent *>(e),
&delta_x, &delta_y); &delta_x, &delta_y);
if (delta_y < 0) { if (delta_y < 0) {
name = getNextWorkspace(); name = getCycleWorkspace(idx, false);
} else if (delta_y > 0) { } else if (delta_y > 0) {
name = getPrevWorkspace(); name = getCycleWorkspace(idx, true);
} }
} }
if (!name.empty()) { if (name.empty() || name == workspaces_[idx]["name"].asString()) {
std::lock_guard<std::mutex> lock(mutex_); scrolling_ = false;
if (name == workspaces_[idx]["name"].asString()) { return false;
scrolling_ = false;
return false;
}
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
std::this_thread::sleep_for(std::chrono::milliseconds(150));
} }
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", name));
std::this_thread::sleep_for(std::chrono::milliseconds(150));
return true; return true;
} }
std::string waybar::modules::sway::Workspaces::getPrevWorkspace() const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(
uint8_t focused_workspace, bool prev) const
{ {
for (uint16_t i = 0; i < workspaces_.size(); i += 1) { auto inc = prev ? -1 : 1;
if (workspaces_[i]["focused"].asBool()) { int size = workspaces_.size();
if (i > 0) { uint8_t idx = 0;
return workspaces_[i - 1]["name"].asString(); for (int i = focused_workspace; i < size && i >= 0; i += inc) {
} bool same_output = (workspaces_[i]["output"].asString() == bar_.output_name
return workspaces_[workspaces_.size() - 1]["name"].asString(); && !config_["all-outputs"].asBool()) || config_["all-outputs"].asBool();
bool same_name =
workspaces_[i]["name"].asString() == workspaces_[focused_workspace]["name"].asString();
if (same_output && !same_name) {
return workspaces_[i]["name"].asString();
} }
if (prev && i - 1 < 0) {
i = size;
} else if (!prev && i + 1 >= size) {
i = -1;
} else if (idx >= workspaces_.size()) {
return "";
}
idx += 1;
} }
return ""; return "";
} }
std::string waybar::modules::sway::Workspaces::getNextWorkspace() uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name) const
{
for (uint16_t i = 0; i < workspaces_.size(); i += 1) {
if (workspaces_[i]["focused"].asBool()) {
if (i + 1U < workspaces_.size()) {
return workspaces_[i + 1]["name"].asString();
}
return workspaces_[0]["String"].asString();
}
}
return "";
}
uint16_t waybar::modules::sway::Workspaces::getWorkspaceIndex(const std::string &name)
{ {
uint16_t idx = 0; uint16_t idx = 0;
for (const auto &workspace : workspaces_) { for (const auto &workspace : workspaces_) {
if (workspace["name"].asString() == name) { if (workspace["name"].asString() == name) {
return idx; return idx;
} }
if (!config_["all-outputs"].asBool() && workspace["output"].asString() != bar_.output_name) { if (!(!config_["all-outputs"].asBool() && workspace["output"].asString() != bar_.output_name)) {
// Nothing here
} else {
idx += 1; idx += 1;
} }
} }