2022-07-01 10:46:28 +00:00
|
|
|
#include "modules/hyprland/backend.hpp"
|
|
|
|
|
2022-07-01 13:16:54 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <netdb.h>
|
2022-07-01 10:46:28 +00:00
|
|
|
#include <netinet/in.h>
|
2022-07-01 13:16:54 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2022-07-01 10:46:28 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-07-01 13:16:54 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
2022-07-01 10:46:28 +00:00
|
|
|
|
2022-07-01 13:16:54 +00:00
|
|
|
namespace waybar::modules::hyprland {
|
|
|
|
|
|
|
|
void IPC::startIPC() {
|
2022-07-01 10:46:28 +00:00
|
|
|
// will start IPC and relay events to parseIPC
|
|
|
|
|
|
|
|
std::thread([&]() {
|
|
|
|
// check for hyprland
|
|
|
|
const char* HIS = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
|
|
|
|
if (!HIS) {
|
|
|
|
spdlog::warn("Hyprland is not running, Hyprland IPC will not be available.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!modulesReady) return;
|
|
|
|
|
|
|
|
spdlog::info("Hyprland IPC starting");
|
|
|
|
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int socketfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (socketfd == -1) {
|
|
|
|
spdlog::error("Hyprland IPC: socketfd failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
// socket path
|
|
|
|
std::string socketPath = "/tmp/hypr/" + std::string(HIS) + "/.socket2.sock";
|
|
|
|
|
|
|
|
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(socketfd, (struct sockaddr*)&addr, l) == -1) {
|
|
|
|
spdlog::error("Hyprland IPC: Unable to connect?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file = fdopen(socketfd, "r");
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// read
|
|
|
|
|
|
|
|
char buffer[1024]; // Hyprland socket2 events are max 1024 bytes
|
|
|
|
auto recievedCharPtr = fgets(buffer, 1024, file);
|
|
|
|
|
|
|
|
if (!recievedCharPtr) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackMutex.lock();
|
|
|
|
|
|
|
|
std::string messageRecieved(buffer);
|
|
|
|
|
|
|
|
messageRecieved = messageRecieved.substr(0, messageRecieved.find_first_of('\n'));
|
|
|
|
|
|
|
|
spdlog::debug("hyprland IPC received {}", messageRecieved);
|
|
|
|
|
|
|
|
parseIPC(messageRecieved);
|
|
|
|
|
|
|
|
callbackMutex.unlock();
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
}
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
|
2022-07-01 13:16:54 +00:00
|
|
|
void IPC::parseIPC(const std::string& ev) {
|
2022-07-01 10:46:28 +00:00
|
|
|
// todo
|
|
|
|
std::string request = ev.substr(0, ev.find_first_of('>'));
|
|
|
|
|
|
|
|
for (auto& [eventname, handler] : callbacks) {
|
|
|
|
if (eventname == request) {
|
2022-11-09 08:34:19 +00:00
|
|
|
handler->onEvent(ev);
|
2022-07-01 10:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:34:19 +00:00
|
|
|
void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
|
|
|
|
if (!ev_handler) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-01 10:46:28 +00:00
|
|
|
callbackMutex.lock();
|
|
|
|
|
2022-11-09 08:34:19 +00:00
|
|
|
callbacks.emplace_back(std::make_pair(ev, ev_handler));
|
|
|
|
|
|
|
|
callbackMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPC::unregisterForIPC(EventHandler* ev_handler) {
|
2022-11-10 08:19:49 +00:00
|
|
|
if (!ev_handler) {
|
2022-11-09 08:34:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callbackMutex.lock();
|
|
|
|
|
2022-11-10 08:19:49 +00:00
|
|
|
for (auto it = callbacks.begin(); it != callbacks.end();) {
|
2022-11-09 08:34:19 +00:00
|
|
|
auto it_current = it;
|
|
|
|
it++;
|
|
|
|
auto& [eventname, handler] = *it_current;
|
|
|
|
if (handler == ev_handler) {
|
|
|
|
callbacks.erase(it_current);
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 10:46:28 +00:00
|
|
|
|
|
|
|
callbackMutex.unlock();
|
2022-07-01 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string IPC::getSocket1Reply(const std::string& rq) {
|
|
|
|
// basically hyprctl
|
2023-04-17 07:09:13 +00:00
|
|
|
|
2022-12-02 14:18:32 +00:00
|
|
|
struct addrinfo ai_hints;
|
2023-04-17 07:09:13 +00:00
|
|
|
struct addrinfo* ai_res = NULL;
|
2022-07-01 13:16:54 +00:00
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (SERVERSOCKET < 0) {
|
|
|
|
spdlog::error("Hyprland IPC: Couldn't open a socket (1)");
|
|
|
|
return "";
|
|
|
|
}
|
2023-04-17 07:09:13 +00:00
|
|
|
|
2022-12-02 14:18:32 +00:00
|
|
|
memset(&ai_hints, 0, sizeof(struct addrinfo));
|
|
|
|
ai_hints.ai_family = AF_UNSPEC;
|
|
|
|
ai_hints.ai_socktype = SOCK_STREAM;
|
2022-07-01 13:16:54 +00:00
|
|
|
|
2023-04-23 11:31:02 +00:00
|
|
|
if (getaddrinfo("localhost", NULL, &ai_hints, &ai_res) != 0) {
|
2022-07-01 13:16:54 +00:00
|
|
|
spdlog::error("Hyprland IPC: Couldn't get host (2)");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the instance signature
|
|
|
|
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
|
|
|
|
if (!instanceSig) {
|
|
|
|
spdlog::error("Hyprland IPC: HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string instanceSigStr = std::string(instanceSig);
|
|
|
|
|
|
|
|
sockaddr_un serverAddress = {0};
|
|
|
|
serverAddress.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock";
|
|
|
|
|
|
|
|
strcpy(serverAddress.sun_path, socketPath.c_str());
|
|
|
|
|
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
|
|
|
spdlog::error("Hyprland IPC: Couldn't connect to " + socketPath + ". (3)");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sizeWritten = write(SERVERSOCKET, rq.c_str(), rq.length());
|
|
|
|
|
|
|
|
if (sizeWritten < 0) {
|
|
|
|
spdlog::error("Hyprland IPC: Couldn't write (4)");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[8192] = {0};
|
2023-06-20 10:36:48 +00:00
|
|
|
std::string response;
|
2022-07-01 13:16:54 +00:00
|
|
|
|
2023-06-20 10:36:48 +00:00
|
|
|
do {
|
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
2022-07-01 13:16:54 +00:00
|
|
|
|
2023-06-20 10:36:48 +00:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
spdlog::error("Hyprland IPC: Couldn't read (5)");
|
|
|
|
close(SERVERSOCKET);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
response.append(buffer, sizeWritten);
|
2023-07-16 11:58:15 +00:00
|
|
|
} while (sizeWritten > 0);
|
2022-07-01 13:16:54 +00:00
|
|
|
|
|
|
|
close(SERVERSOCKET);
|
2023-06-20 10:36:48 +00:00
|
|
|
return response;
|
2022-07-01 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 23:52:01 +00:00
|
|
|
Json::Value IPC::getSocket1JsonReply(const std::string& rq) {
|
|
|
|
return parser_.parse(getSocket1Reply("j/" + rq));
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:34:19 +00:00
|
|
|
} // namespace waybar::modules::hyprland
|