From 4b8c8c355f508694323bf4d0f7ea736b9a97b3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Tue, 10 Jun 2025 16:46:58 +0200 Subject: [PATCH] Outsourced command execution to service --- include/ShellService.h | 11 +++++++++++ src/HyprlandService.cpp | 35 +++++++++-------------------------- src/ShellService.cpp | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 26 deletions(-) create mode 100644 include/ShellService.h create mode 100644 src/ShellService.cpp diff --git a/include/ShellService.h b/include/ShellService.h new file mode 100644 index 0000000..2ffe314 --- /dev/null +++ b/include/ShellService.h @@ -0,0 +1,11 @@ +#ifndef SHELL_SERVICE_H +#define SHELL_SERVICE_H + +#include + +class ShellService { + public: + static std::string exec(const std::string& command); +}; + +#endif \ No newline at end of file diff --git a/src/HyprlandService.cpp b/src/HyprlandService.cpp index 1c72cc6..54d32f5 100644 --- a/src/HyprlandService.cpp +++ b/src/HyprlandService.cpp @@ -1,35 +1,18 @@ +#include #include "../include/HyprlandService.h" +#include "../include/ShellService.h" +#include "../include/Macros.h" +#include "../include/FileService.h" using json = nlohmann::json; -std::string HyprlandService::exec(const std::string& command) { - char buffer[128]; - std::string result = ""; - - FILE* pipe = popen(command.c_str(), "r"); - if (!pipe) { - return "popen failed"; - } - - while (fgets(buffer, sizeof(buffer), pipe) != NULL) { - result += buffer; - } - - int status = pclose(pipe); - if (status == -1) { - return "Error closing pipe"; - } - - return result; -}; - Workspace HyprlandService::getCurrentWorkspace() { - json j = json::parse(exec(HYPRCTL_BINARY " activeworkspace -j")); + json j = json::parse(ShellService::exec(HYPRCTL_BINARY " activeworkspace -j")); return j.get(); }; std::list HyprlandService::getClients() { - json j = json::parse(exec(HYPRCTL_BINARY " clients -j")); + json j = json::parse(ShellService::exec(HYPRCTL_BINARY " clients -j")); return j.get>(); }; @@ -49,15 +32,15 @@ std::list HyprlandService::getClientsOnActiveWorkspace() { }; void HyprlandService::setClientFloating(Client& c) { - exec(HYPRCTL_BINARY " dispatch setfloating address:" + c.address); + ShellService::exec(HYPRCTL_BINARY " dispatch setfloating address:" + c.address); }; void HyprlandService::setClientTiled(Client& c) { - exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address); + ShellService::exec(HYPRCTL_BINARY " dispatch settiled address:" + c.address); } void HyprlandService::toggleClientFloating(Client& c) { - exec(HYPRCTL_BINARY " dispatch togglefloating address:" + c.address); + ShellService::exec(HYPRCTL_BINARY " dispatch togglefloating address:" + c.address); }; void HyprlandService::setFloatingRule(bool b) { diff --git a/src/ShellService.cpp b/src/ShellService.cpp new file mode 100644 index 0000000..4c300c0 --- /dev/null +++ b/src/ShellService.cpp @@ -0,0 +1,22 @@ +#include "../include/ShellService.h" + +std::string ShellService::exec(const std::string& command) { + char buffer[128]; + std::string result = ""; + + FILE* pipe = popen(command.c_str(), "r"); + if (!pipe) { + return "popen failed"; + } + + while (fgets(buffer, sizeof(buffer), pipe) != NULL) { + result += buffer; + } + + int status = pclose(pipe); + if (status == -1) { + return "Error closing pipe"; + } + + return result; +}; \ No newline at end of file