mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Clean (#31)
This commit is contained in:
@ -1,15 +1,18 @@
|
||||
#include "modules/sway/workspaces.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
|
||||
: bar_(bar), config_(std::move(config)), scrolling_(false)
|
||||
waybar::modules::sway::Workspaces::Workspaces(Bar& bar,
|
||||
const Json::Value& config)
|
||||
: bar_(bar), config_(config), scrolling_(false)
|
||||
{
|
||||
box_.set_name("workspaces");
|
||||
std::string socketPath = getSocketPath();
|
||||
ipcfd_ = ipcOpenSocket(socketPath);
|
||||
ipc_eventfd_ = ipcOpenSocket(socketPath);
|
||||
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"workspace\" ]");
|
||||
thread_.sig_update.connect(sigc::mem_fun(*this, &Workspaces::update));
|
||||
ipc_.connect();
|
||||
ipc_.subscribe("[ \"workspace\" ]");
|
||||
// Launch worker
|
||||
worker();
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Workspaces::worker()
|
||||
{
|
||||
thread_ = [this] {
|
||||
try {
|
||||
// Wait for the name of the output
|
||||
@ -18,28 +21,24 @@ waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
|
||||
thread_.sleep_for(chrono::milliseconds(150));
|
||||
}
|
||||
} else if (!workspaces_.empty()) {
|
||||
ipcRecvResponse(ipc_eventfd_);
|
||||
ipc_.handleEvent();
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
|
||||
workspaces_ = parser_.parse(res.payload);
|
||||
thread_.emit();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto res = ipc_.sendCmd(IPC_GET_WORKSPACES);
|
||||
workspaces_ = parser_.parse(res.payload);
|
||||
}
|
||||
dp.emit();
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
waybar::modules::sway::Workspaces::~Workspaces()
|
||||
{
|
||||
close(ipcfd_);
|
||||
close(ipc_eventfd_);
|
||||
}
|
||||
|
||||
auto waybar::modules::sway::Workspaces::update() -> void
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
bool needReorder = false;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto it = buttons_.begin(); it != buttons_.end();) {
|
||||
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
|
||||
[it](auto node) -> bool { return node["num"].asInt() == it->first; });
|
||||
@ -101,7 +100,7 @@ void waybar::modules::sway::Workspaces::addWorkspace(Json::Value node)
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto cmd = fmt::format("workspace \"{}\"", pair.first->first);
|
||||
ipcSingleCommand(ipcfd_, IPC_COMMAND, cmd);
|
||||
ipc_.sendCmd(IPC_COMMAND, cmd);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
@ -144,11 +143,13 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
|
||||
scrolling_ = true;
|
||||
int id = -1;
|
||||
uint16_t idx = 0;
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (; idx < workspaces_.size(); idx += 1) {
|
||||
if (workspaces_[idx]["focused"].asBool()) {
|
||||
id = workspaces_[idx]["num"].asInt();
|
||||
break;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (; idx < workspaces_.size(); idx += 1) {
|
||||
if (workspaces_[idx]["focused"].asBool()) {
|
||||
id = workspaces_[idx]["num"].asInt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (id == -1) {
|
||||
@ -171,12 +172,15 @@ bool waybar::modules::sway::Workspaces::handleScroll(GdkEventScroll *e)
|
||||
id = getPrevWorkspace();
|
||||
}
|
||||
}
|
||||
if (id == workspaces_[idx]["num"].asInt()) {
|
||||
scrolling_ = false;
|
||||
return false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (id == workspaces_[idx]["num"].asInt()) {
|
||||
scrolling_ = false;
|
||||
return false;
|
||||
}
|
||||
ipc_.sendCmd(IPC_COMMAND, fmt::format("workspace \"{}\"", id));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(150));
|
||||
}
|
||||
ipcSingleCommand(ipcfd_, IPC_COMMAND, fmt::format("workspace \"{}\"", id));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(150));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user