mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor: format code
This commit is contained in:
@ -1,14 +1,12 @@
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
waybar::modules::sway::Ipc::Ipc()
|
||||
{
|
||||
waybar::modules::sway::Ipc::Ipc() {
|
||||
const std::string& socketPath = getSocketPath();
|
||||
fd_ = open(socketPath);
|
||||
fd_event_ = open(socketPath);
|
||||
}
|
||||
|
||||
waybar::modules::sway::Ipc::~Ipc()
|
||||
{
|
||||
waybar::modules::sway::Ipc::~Ipc() {
|
||||
// To fail the IPC header
|
||||
write(fd_, "close-sway-ipc", 14);
|
||||
write(fd_event_, "close-sway-ipc", 14);
|
||||
@ -17,17 +15,16 @@ waybar::modules::sway::Ipc::~Ipc()
|
||||
close(fd_event_);
|
||||
}
|
||||
|
||||
const std::string waybar::modules::sway::Ipc::getSocketPath() const
|
||||
{
|
||||
const char *env = getenv("SWAYSOCK");
|
||||
const std::string waybar::modules::sway::Ipc::getSocketPath() const {
|
||||
const char* env = getenv("SWAYSOCK");
|
||||
if (env != nullptr) {
|
||||
return std::string(env);
|
||||
}
|
||||
std::string str;
|
||||
{
|
||||
std::string str_buf;
|
||||
FILE* in;
|
||||
char buf[512] = { 0 };
|
||||
FILE* in;
|
||||
char buf[512] = {0};
|
||||
if ((in = popen("sway --get-socketpath 2>/dev/null", "r")) == nullptr) {
|
||||
throw std::runtime_error("Failed to get socket path");
|
||||
}
|
||||
@ -43,10 +40,9 @@ const std::string waybar::modules::sway::Ipc::getSocketPath() const
|
||||
return str;
|
||||
}
|
||||
|
||||
int waybar::modules::sway::Ipc::open(const std::string& socketPath) const
|
||||
{
|
||||
int waybar::modules::sway::Ipc::open(const std::string& socketPath) const {
|
||||
struct sockaddr_un addr = {0};
|
||||
int fd = -1;
|
||||
int fd = -1;
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
throw std::runtime_error("Unable to open Unix socket");
|
||||
}
|
||||
@ -54,18 +50,16 @@ int waybar::modules::sway::Ipc::open(const std::string& socketPath) const
|
||||
strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
if (::connect(fd, reinterpret_cast<struct sockaddr *>(&addr), l) == -1) {
|
||||
if (::connect(fd, reinterpret_cast<struct sockaddr*>(&addr), l) == -1) {
|
||||
throw std::runtime_error("Unable to connect to Sway");
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
struct waybar::modules::sway::Ipc::ipc_response
|
||||
waybar::modules::sway::Ipc::recv(int fd) const
|
||||
{
|
||||
struct waybar::modules::sway::Ipc::ipc_response waybar::modules::sway::Ipc::recv(int fd) const {
|
||||
std::string header;
|
||||
header.resize(ipc_header_size_);
|
||||
auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic_.size());
|
||||
auto data32 = reinterpret_cast<uint32_t*>(header.data() + ipc_magic_.size());
|
||||
size_t total = 0;
|
||||
|
||||
while (total < ipc_header_size_) {
|
||||
@ -91,16 +85,14 @@ struct waybar::modules::sway::Ipc::ipc_response
|
||||
}
|
||||
total += res;
|
||||
}
|
||||
return { data32[0], data32[1], &payload.front() };
|
||||
return {data32[0], data32[1], &payload.front()};
|
||||
}
|
||||
|
||||
struct waybar::modules::sway::Ipc::ipc_response
|
||||
waybar::modules::sway::Ipc::send(int fd, uint32_t type,
|
||||
const std::string& payload) const
|
||||
{
|
||||
struct waybar::modules::sway::Ipc::ipc_response waybar::modules::sway::Ipc::send(
|
||||
int fd, uint32_t type, const std::string& payload) const {
|
||||
std::string header;
|
||||
header.resize(ipc_header_size_);
|
||||
auto data32 = reinterpret_cast<uint32_t *>(header.data() + ipc_magic_.size());
|
||||
auto data32 = reinterpret_cast<uint32_t*>(header.data() + ipc_magic_.size());
|
||||
memcpy(header.data(), ipc_magic_.c_str(), ipc_magic_.size());
|
||||
data32[0] = payload.size();
|
||||
data32[1] = type;
|
||||
@ -114,23 +106,18 @@ struct waybar::modules::sway::Ipc::ipc_response
|
||||
return recv(fd);
|
||||
}
|
||||
|
||||
struct waybar::modules::sway::Ipc::ipc_response
|
||||
waybar::modules::sway::Ipc::sendCmd(uint32_t type,
|
||||
const std::string& payload) const
|
||||
{
|
||||
struct waybar::modules::sway::Ipc::ipc_response waybar::modules::sway::Ipc::sendCmd(
|
||||
uint32_t type, const std::string& payload) const {
|
||||
return send(fd_, type, payload);
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Ipc::subscribe(const std::string& payload) const
|
||||
{
|
||||
void waybar::modules::sway::Ipc::subscribe(const std::string& payload) const {
|
||||
auto res = send(fd_event_, IPC_SUBSCRIBE, payload);
|
||||
if (res.payload != "{\"success\": true}") {
|
||||
throw std::runtime_error("Unable to subscribe ipc event");
|
||||
}
|
||||
}
|
||||
|
||||
struct waybar::modules::sway::Ipc::ipc_response
|
||||
waybar::modules::sway::Ipc::handleEvent() const
|
||||
{
|
||||
struct waybar::modules::sway::Ipc::ipc_response waybar::modules::sway::Ipc::handleEvent() const {
|
||||
return recv(fd_event_);
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
#include "modules/sway/mode.hpp"
|
||||
|
||||
waybar::modules::sway::Mode::Mode(const std::string& id, const Bar& bar, const Json::Value& config)
|
||||
: ALabel(config, "{}"), bar_(bar)
|
||||
{
|
||||
: ALabel(config, "{}"), bar_(bar) {
|
||||
label_.set_name("mode");
|
||||
if (!id.empty()) {
|
||||
label_.get_style_context()->add_class(id);
|
||||
@ -13,8 +12,7 @@ waybar::modules::sway::Mode::Mode(const std::string& id, const Bar& bar, const J
|
||||
dp.emit();
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Mode::worker()
|
||||
{
|
||||
void waybar::modules::sway::Mode::worker() {
|
||||
thread_ = [this] {
|
||||
try {
|
||||
auto res = ipc_.handleEvent();
|
||||
@ -31,8 +29,7 @@ void waybar::modules::sway::Mode::worker()
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::sway::Mode::update() -> void
|
||||
{
|
||||
auto waybar::modules::sway::Mode::update() -> void {
|
||||
if (mode_.empty()) {
|
||||
event_box_.hide();
|
||||
} else {
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "modules/sway/window.hpp"
|
||||
|
||||
waybar::modules::sway::Window::Window(const std::string& id, const Bar &bar, const Json::Value& config)
|
||||
: ALabel(config, "{}"), bar_(bar), windowId_(-1)
|
||||
{
|
||||
waybar::modules::sway::Window::Window(const std::string& id, const Bar& bar,
|
||||
const Json::Value& config)
|
||||
: ALabel(config, "{}"), bar_(bar), windowId_(-1) {
|
||||
label_.set_name("window");
|
||||
if (!id.empty()) {
|
||||
label_.get_style_context()->add_class(id);
|
||||
@ -17,24 +17,22 @@ waybar::modules::sway::Window::Window(const std::string& id, const Bar &bar, con
|
||||
worker();
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Window::worker()
|
||||
{
|
||||
void waybar::modules::sway::Window::worker() {
|
||||
thread_ = [this] {
|
||||
try {
|
||||
auto res = ipc_.handleEvent();
|
||||
auto parsed = parser_.parse(res.payload);
|
||||
// Check for waybar prevents flicker when hovering window module
|
||||
if ((parsed["change"] == "focus" || parsed["change"] == "title")
|
||||
&& parsed["container"]["focused"].asBool()
|
||||
&& parsed["container"]["name"].asString() != "waybar") {
|
||||
if ((parsed["change"] == "focus" || parsed["change"] == "title") &&
|
||||
parsed["container"]["focused"].asBool() &&
|
||||
parsed["container"]["name"].asString() != "waybar") {
|
||||
window_ = Glib::Markup::escape_text(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())
|
||||
|| (parsed["change"] == "focus" && parsed["current"]["focus"].isArray()
|
||||
&& parsed["current"]["focus"].empty())) {
|
||||
} else if ((parsed["change"] == "close" && parsed["container"]["focused"].asBool() &&
|
||||
windowId_ == parsed["container"]["id"].asInt()) ||
|
||||
(parsed["change"] == "focus" && parsed["current"]["focus"].isArray() &&
|
||||
parsed["current"]["focus"].empty())) {
|
||||
window_.clear();
|
||||
windowId_ = -1;
|
||||
dp.emit();
|
||||
@ -45,31 +43,27 @@ void waybar::modules::sway::Window::worker()
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::sway::Window::update() -> void
|
||||
{
|
||||
auto waybar::modules::sway::Window::update() -> void {
|
||||
label_.set_markup(fmt::format(format_, window_));
|
||||
if (tooltipEnabled()) {
|
||||
label_.set_tooltip_text(window_);
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<int, 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 const& node : nodes) {
|
||||
if (node["focused"].asBool() && node["type"] == "con") {
|
||||
return { node["id"].asInt(), node["name"].asString() };
|
||||
return {node["id"].asInt(), node["name"].asString()};
|
||||
}
|
||||
auto [id, name] = getFocusedNode(node["nodes"]);
|
||||
if (id > -1 && !name.empty()) {
|
||||
return { id, name };
|
||||
return {id, name};
|
||||
}
|
||||
}
|
||||
return { -1, std::string() };
|
||||
return {-1, std::string()};
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Window::getFocusedWindow()
|
||||
{
|
||||
void waybar::modules::sway::Window::getFocusedWindow() {
|
||||
try {
|
||||
auto res = ipc_.sendCmd(IPC_GET_TREE);
|
||||
auto parsed = parser_.parse(res.payload);
|
||||
@ -77,7 +71,7 @@ void waybar::modules::sway::Window::getFocusedWindow()
|
||||
windowId_ = id;
|
||||
window_ = name;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ auto waybar::modules::sway::Workspaces::update() -> void {
|
||||
});
|
||||
if (ws == workspaces_.end() ||
|
||||
(!config_["all-outputs"].asBool() && (*ws)["output"].asString() != bar_.output->name)) {
|
||||
it = buttons_.erase(it);
|
||||
it = buttons_.erase(it);
|
||||
needReorder = true;
|
||||
} else {
|
||||
++it;
|
||||
@ -78,11 +78,11 @@ auto waybar::modules::sway::Workspaces::update() -> void {
|
||||
if (needReorder) {
|
||||
box_.reorder_child(button, getWorkspaceIndex(node["name"].asString()));
|
||||
}
|
||||
auto icon = getIcon(node["name"].asString(), node);
|
||||
auto icon = getIcon(node["name"].asString(), node);
|
||||
std::string output = icon;
|
||||
if (config_["format"].isString()) {
|
||||
auto format = config_["format"].asString();
|
||||
output = fmt::format(format,
|
||||
output = fmt::format(format,
|
||||
fmt::arg("icon", icon),
|
||||
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
||||
fmt::arg("index", node["num"].asString()));
|
||||
@ -101,14 +101,14 @@ auto waybar::modules::sway::Workspaces::update() -> void {
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Workspaces::addWorkspace(const Json::Value &node) {
|
||||
auto icon = getIcon(node["name"].asString(), node);
|
||||
auto icon = getIcon(node["name"].asString(), node);
|
||||
auto format = config_["format"].isString()
|
||||
? fmt::format(config_["format"].asString(),
|
||||
fmt::arg("icon", icon),
|
||||
fmt::arg("name", trimWorkspaceName(node["name"].asString())),
|
||||
fmt::arg("index", node["num"].asString()))
|
||||
: icon;
|
||||
auto pair = buttons_.emplace(node["name"].asString(), format);
|
||||
auto pair = buttons_.emplace(node["name"].asString(), format);
|
||||
auto &button = pair.first->second;
|
||||
if (!config_["disable-markup"].asBool()) {
|
||||
static_cast<Gtk::Label *>(button.get_children()[0])->set_markup(format);
|
||||
@ -201,9 +201,9 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e) {
|
||||
|
||||
const std::string waybar::modules::sway::Workspaces::getCycleWorkspace(uint8_t focused_workspace,
|
||||
bool prev) const {
|
||||
auto inc = prev ? -1 : 1;
|
||||
auto inc = prev ? -1 : 1;
|
||||
int size = workspaces_.size();
|
||||
uint8_t idx = 0;
|
||||
uint8_t idx = 0;
|
||||
for (int i = focused_workspace; i < size && i >= 0; i += inc) {
|
||||
bool same_output = (workspaces_[i]["output"].asString() == bar_.output->name &&
|
||||
!config_["all-outputs"].asBool()) ||
|
||||
|
Reference in New Issue
Block a user