mirror of
https://github.com/rad4day/Waybar.git
synced 2025-07-20 01:42:38 +02:00
feat(sway): add focused window name
This commit is contained in:
95
src/modules/sway/ipc/client.cpp
Normal file
95
src/modules/sway/ipc/client.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
|
||||
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
|
||||
|
||||
std::string get_socketpath(void) {
|
||||
const char *env = getenv("SWAYSOCK");
|
||||
if (env) return std::string(env);
|
||||
std::string str;
|
||||
{
|
||||
std::string str_buf;
|
||||
FILE* in;
|
||||
char buf[512] = { 0 };
|
||||
if (!(in = popen("sway --get-socketpath 2>/dev/null", "r"))) {
|
||||
throw std::runtime_error("Failed to get socket path");
|
||||
}
|
||||
while (fgets(buf, sizeof(buf), in) != nullptr) {
|
||||
str_buf.append(buf, sizeof(buf));
|
||||
}
|
||||
pclose(in);
|
||||
str = str_buf;
|
||||
}
|
||||
if (str.back() == '\n') {
|
||||
str.pop_back();
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
int ipc_open_socket(std::string socket_path) {
|
||||
struct sockaddr_un addr;
|
||||
int socketfd;
|
||||
if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
throw std::runtime_error("Unable to open Unix socket");
|
||||
}
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, socket_path.c_str(), sizeof(addr.sun_path) - 1);
|
||||
addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
|
||||
int l = sizeof(struct sockaddr_un);
|
||||
if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) {
|
||||
throw std::runtime_error("Unable to connect to " + socket_path);
|
||||
}
|
||||
return socketfd;
|
||||
}
|
||||
|
||||
struct ipc_response ipc_recv_response(int socketfd) {
|
||||
struct ipc_response response;
|
||||
char data[ipc_header_size];
|
||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||
size_t total = 0;
|
||||
|
||||
while (total < ipc_header_size) {
|
||||
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0);
|
||||
if (received <= 0) {
|
||||
throw std::runtime_error("Unable to receive IPC response");
|
||||
}
|
||||
total += received;
|
||||
}
|
||||
|
||||
total = 0;
|
||||
response.size = data32[0];
|
||||
response.type = data32[1];
|
||||
char payload[response.size + 1];
|
||||
|
||||
while (total < response.size) {
|
||||
ssize_t received = recv(socketfd, payload + total, response.size - total, 0);
|
||||
if (received < 0) {
|
||||
throw std::runtime_error("Unable to receive IPC response");
|
||||
}
|
||||
total += received;
|
||||
}
|
||||
payload[response.size] = '\0';
|
||||
response.payload = std::string(payload);
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
|
||||
char data[ipc_header_size];
|
||||
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
|
||||
memcpy(data, ipc_magic, sizeof(ipc_magic));
|
||||
data32[0] = *len;
|
||||
data32[1] = type;
|
||||
|
||||
if (send(socketfd, data, ipc_header_size, 0) == -1)
|
||||
throw std::runtime_error("Unable to send IPC header");
|
||||
if (send(socketfd, payload, *len, 0) == -1)
|
||||
throw std::runtime_error("Unable to send IPC payload");
|
||||
struct ipc_response resp = ipc_recv_response(socketfd);
|
||||
*len = resp.size;
|
||||
return resp.payload;
|
||||
}
|
66
src/modules/sway/window.cpp
Normal file
66
src/modules/sway/window.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "modules/sway/window.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
|
||||
: _bar(bar), _config(config)
|
||||
{
|
||||
_label.set_name("window");
|
||||
std::string socketPath = get_socketpath();
|
||||
_ipcfd = ipc_open_socket(socketPath);
|
||||
_ipcEventfd = ipc_open_socket(socketPath);
|
||||
const char *subscribe = "[ \"window\" ]";
|
||||
uint32_t len = strlen(subscribe);
|
||||
ipc_single_command(_ipcEventfd, IPC_SUBSCRIBE, subscribe, &len);
|
||||
_getFocusedWindow();
|
||||
_thread = [this] {
|
||||
try {
|
||||
if (_bar.outputName.empty()) {
|
||||
// Wait for the name of the output
|
||||
while (_bar.outputName.empty())
|
||||
_thread.sleep_for(chrono::milliseconds(150));
|
||||
}
|
||||
auto res = ipc_recv_response(_ipcEventfd);
|
||||
auto parsed = _parser.parse(res.payload);
|
||||
if (parsed["change"] != "focus")
|
||||
return;
|
||||
_window = parsed["container"]["name"].asString();
|
||||
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::sway::Window::update() -> void
|
||||
{
|
||||
_label.set_text(_window);
|
||||
}
|
||||
|
||||
std::string waybar::modules::sway::Window::_getFocusedNode(Json::Value nodes)
|
||||
{
|
||||
for (auto &node : nodes) {
|
||||
if (node["focused"].asBool())
|
||||
return node["name"].asString();
|
||||
auto res = _getFocusedNode(node["nodes"]);
|
||||
if (!res.empty())
|
||||
return res;
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void waybar::modules::sway::Window::_getFocusedWindow()
|
||||
{
|
||||
try {
|
||||
uint32_t len = 0;
|
||||
auto res = ipc_single_command(_ipcfd, IPC_GET_TREE, nullptr, &len);
|
||||
auto parsed = _parser.parse(res);
|
||||
_window = _getFocusedNode(parsed["nodes"]);
|
||||
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
waybar::modules::sway::Window::operator Gtk::Widget &() {
|
||||
return _label;
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
#include "modules/workspaces.hpp"
|
||||
#include "ipc/client.hpp"
|
||||
#include "modules/sway/workspaces.hpp"
|
||||
#include "modules/sway/ipc/client.hpp"
|
||||
|
||||
waybar::modules::Workspaces::Workspaces(Bar &bar, Json::Value config)
|
||||
waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
|
||||
: _bar(bar), _config(config), _scrolling(false)
|
||||
{
|
||||
_box.set_name("workspaces");
|
||||
@@ -22,15 +22,16 @@ waybar::modules::Workspaces::Workspaces(Bar &bar, Json::Value config)
|
||||
uint32_t len = 0;
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto str = ipc_single_command(_ipcfd, IPC_GET_WORKSPACES, nullptr, &len);
|
||||
_workspaces = _getWorkspaces(str);
|
||||
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Workspaces::update));
|
||||
_workspaces = _parser.parse(str);
|
||||
Glib::signal_idle()
|
||||
.connect_once(sigc::mem_fun(*this, &Workspaces::update));
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
auto waybar::modules::Workspaces::update() -> void
|
||||
auto waybar::modules::sway::Workspaces::update() -> void
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
bool needReorder = false;
|
||||
@@ -73,7 +74,7 @@ auto waybar::modules::Workspaces::update() -> void
|
||||
_scrolling = false;
|
||||
}
|
||||
|
||||
void waybar::modules::Workspaces::_addWorkspace(Json::Value node)
|
||||
void waybar::modules::sway::Workspaces::_addWorkspace(Json::Value node)
|
||||
{
|
||||
auto icon = _getIcon(node["name"].asString());
|
||||
auto pair = _buttons.emplace(node["num"].asInt(), icon);
|
||||
@@ -105,7 +106,7 @@ void waybar::modules::Workspaces::_addWorkspace(Json::Value node)
|
||||
button.show();
|
||||
}
|
||||
|
||||
std::string waybar::modules::Workspaces::_getIcon(std::string name)
|
||||
std::string waybar::modules::sway::Workspaces::_getIcon(std::string name)
|
||||
{
|
||||
if (_config["format-icons"][name])
|
||||
return _config["format-icons"][name].asString();
|
||||
@@ -114,7 +115,7 @@ std::string waybar::modules::Workspaces::_getIcon(std::string name)
|
||||
return name;
|
||||
}
|
||||
|
||||
bool waybar::modules::Workspaces::_handleScroll(GdkEventScroll *e)
|
||||
bool waybar::modules::sway::Workspaces::_handleScroll(GdkEventScroll *e)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
// Avoid concurrent scroll event
|
||||
@@ -155,7 +156,7 @@ bool waybar::modules::Workspaces::_handleScroll(GdkEventScroll *e)
|
||||
return true;
|
||||
}
|
||||
|
||||
int waybar::modules::Workspaces::_getPrevWorkspace()
|
||||
int waybar::modules::sway::Workspaces::_getPrevWorkspace()
|
||||
{
|
||||
int current = -1;
|
||||
for (uint16_t i = 0; i != _workspaces.size(); i += 1)
|
||||
@@ -168,7 +169,7 @@ int waybar::modules::Workspaces::_getPrevWorkspace()
|
||||
return current;
|
||||
}
|
||||
|
||||
int waybar::modules::Workspaces::_getNextWorkspace()
|
||||
int waybar::modules::sway::Workspaces::_getNextWorkspace()
|
||||
{
|
||||
int current = -1;
|
||||
for (uint16_t i = 0; i != _workspaces.size(); i += 1)
|
||||
@@ -181,18 +182,6 @@ int waybar::modules::Workspaces::_getNextWorkspace()
|
||||
return current;
|
||||
}
|
||||
|
||||
Json::Value waybar::modules::Workspaces::_getWorkspaces(const std::string data)
|
||||
{
|
||||
Json::Value res;
|
||||
try {
|
||||
std::string err;
|
||||
res = _parser.parse(data);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
waybar::modules::Workspaces::operator Gtk::Widget &() {
|
||||
waybar::modules::sway::Workspaces::operator Gtk::Widget &() {
|
||||
return _box;
|
||||
}
|
Reference in New Issue
Block a user