From b7b3344188e3ebd2baed14bc288ee42a1ae8d6f2 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Sat, 2 Jul 2022 18:27:44 +0200 Subject: [PATCH] hyprctl: added support for hyprpaper --- hyprctl/main.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index fab353c0..a1a1122d 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -27,6 +27,7 @@ const std::string USAGE = R"#(usage: hyprctl [command] [(opt)args] keyword version kill + hyprpaper reload)#"; void request(std::string arg) { @@ -75,7 +76,65 @@ void request(std::string arg) { char buffer[8192] = {0}; - sizeWritten = read(SERVERSOCKET,buffer, 8192); + sizeWritten = read(SERVERSOCKET, buffer, 8192); + + if (sizeWritten < 0) { + std::cout << "Couldn't read (5)"; + return; + } + + close(SERVERSOCKET); + + std::cout << std::string(buffer); +} + +void requestHyprpaper(std::string arg) { + const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0); + + if (SERVERSOCKET < 0) { + std::cout << "Couldn't open a socket (1)"; + return; + } + + const auto SERVER = gethostbyname("localhost"); + + if (!SERVER) { + std::cout << "Couldn't get host (2)"; + return; + } + + // get the instance signature + auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE"); + + if (!instanceSig) { + std::cout << "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 + "/.hyprpaper.sock"; + + strcpy(serverAddress.sun_path, socketPath.c_str()); + + if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) { + std::cout << "Couldn't connect to " << socketPath << ". (3)"; + return; + } + + auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length()); + + if (sizeWritten < 0) { + std::cout << "Couldn't write (4)"; + return; + } + + char buffer[8192] = {0}; + + sizeWritten = read(SERVERSOCKET, buffer, 8192); if (sizeWritten < 0) { std::cout << "Couldn't read (5)"; @@ -110,6 +169,17 @@ void keywordRequest(int argc, char** argv) { request(rq); } +void hyprpaperRequest(int argc, char** argv) { + if (argc < 4) { + std::cout << "hyprpaper requires 2 params"; + return; + } + + std::string rq = std::string(argv[2]) + " " + std::string(argv[3]); + + requestHyprpaper(rq); +} + void batchRequest(int argc, char** argv) { std::string rq = "[[BATCH]]" + std::string(argv[2]); @@ -135,6 +205,7 @@ int main(int argc, char** argv) { else if (!strcmp(argv[1], "reload")) request("reload"); else if (!strcmp(argv[1], "dispatch")) dispatchRequest(argc, argv); else if (!strcmp(argv[1], "keyword")) keywordRequest(argc, argv); + else if (!strcmp(argv[1], "hyprpaper")) hyprpaperRequest(argc, argv); else if (!strcmp(argv[1], "--batch")) batchRequest(argc, argv); else if (!strcmp(argv[1], "--help")) printf("%s", USAGE.c_str()); else {