quick commit

This commit is contained in:
2025-12-09 00:25:28 +01:00
parent 2570445aef
commit cd3a9e6503
2 changed files with 11 additions and 20 deletions

View File

@@ -10,17 +10,14 @@ class HyprlandService
HyprlandService(); HyprlandService();
~HyprlandService(); ~HyprlandService();
// Setup the connection
void start();
// Signal that emits (EventName, EventData)
// Example: emits ("workspace", "1")
sigc::signal<void(std::string, std::string)> on_event; sigc::signal<void(std::string, std::string)> on_event;
void start();
void on_hyprland_event(std::string event, std::string data); void on_hyprland_event(std::string event, std::string data);
private: private:
int m_fd = -1; // File descriptor for the socket int m_fd = -1;
std::string m_buffer; // Buffer to handle partial socket reads std::string m_buffer;
bool on_socket_read(Glib::IOCondition condition); bool on_socket_read(Glib::IOCondition condition);
void parse_message(const std::string &line); void parse_message(const std::string &line);

View File

@@ -27,6 +27,8 @@ void HyprlandService::on_hyprland_event(std::string event, std::string data)
else if (event == "activewindow") else if (event == "activewindow")
{ {
std::cout << "Active window changed" << std::endl; std::cout << "Active window changed" << std::endl;
} else {
std::cout << event << " " << data << std::endl;
} }
} }
@@ -34,9 +36,10 @@ void HyprlandService::start()
{ {
std::string socket_path = get_socket_path(); std::string socket_path = get_socket_path();
if (socket_path.empty()) if (socket_path.empty())
{
return; return;
}
// 1. Create Socket
m_fd = socket(AF_UNIX, SOCK_STREAM, 0); m_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (m_fd == -1) if (m_fd == -1)
{ {
@@ -44,7 +47,6 @@ void HyprlandService::start()
return; return;
} }
// 2. Connect
struct sockaddr_un addr; struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
@@ -60,8 +62,6 @@ void HyprlandService::start()
std::cout << "[Hyprland] Connected to event socket." << std::endl; std::cout << "[Hyprland] Connected to event socket." << std::endl;
// 3. Register with GLib Main Loop
// This tells GTK to call 'on_socket_read' whenever there is data to read
Glib::signal_io().connect( Glib::signal_io().connect(
sigc::mem_fun(*this, &HyprlandService::on_socket_read), sigc::mem_fun(*this, &HyprlandService::on_socket_read),
m_fd, m_fd,
@@ -70,10 +70,8 @@ void HyprlandService::start()
bool HyprlandService::on_socket_read(Glib::IOCondition condition) bool HyprlandService::on_socket_read(Glib::IOCondition condition)
{ {
// Handle disconnection or errors
auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR; auto error_mask = Glib::IOCondition::IO_HUP | Glib::IOCondition::IO_ERR;
// 2. Perform the bitwise AND, then cast to int to check if non-zero
if (static_cast<int>(condition & error_mask) != 0) if (static_cast<int>(condition & error_mask) != 0)
{ {
std::cerr << "[Hyprland] Socket disconnected." << std::endl; std::cerr << "[Hyprland] Socket disconnected." << std::endl;
@@ -81,7 +79,7 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
m_fd = -1; m_fd = -1;
return false; return false;
} }
// Read data
char buffer[4096]; char buffer[4096];
ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1); ssize_t bytes_read = read(m_fd, buffer, sizeof(buffer) - 1);
@@ -90,8 +88,8 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
buffer[bytes_read] = '\0'; buffer[bytes_read] = '\0';
m_buffer.append(buffer); m_buffer.append(buffer);
// Process line by line
size_t pos = 0; size_t pos = 0;
while ((pos = m_buffer.find('\n')) != std::string::npos) while ((pos = m_buffer.find('\n')) != std::string::npos)
{ {
std::string line = m_buffer.substr(0, pos); std::string line = m_buffer.substr(0, pos);
@@ -100,20 +98,17 @@ bool HyprlandService::on_socket_read(Glib::IOCondition condition)
} }
} }
return true; // Continue listening return true;
} }
void HyprlandService::parse_message(const std::string &line) void HyprlandService::parse_message(const std::string &line)
{ {
// Hyprland events look like: "event>>data"
// Example: "workspace>>1" or "activewindow>>550a12,firefox"
size_t split = line.find(">>"); size_t split = line.find(">>");
if (split != std::string::npos) if (split != std::string::npos)
{ {
std::string event_name = line.substr(0, split); std::string event_name = line.substr(0, split);
std::string event_data = line.substr(split + 2); std::string event_data = line.substr(split + 2);
// Emit the signal
on_event.emit(event_name, event_data); on_event.emit(event_name, event_data);
} }
} }
@@ -129,6 +124,5 @@ std::string HyprlandService::get_socket_path()
return ""; return "";
} }
// Path format: $XDG_RUNTIME_DIR/hypr/$SIGNATURE/.socket2.sock
return std::string(runtime) + "/hypr/" + sig + "/.socket2.sock"; return std::string(runtime) + "/hypr/" + sig + "/.socket2.sock";
} }