From 7afcf656bd25e2ed6b33bae1cbbc4a62d66e1938 Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Thu, 2 Jun 2022 13:59:33 +0200 Subject: [PATCH] Added hyprctl devices --- hyprctl/main.cpp | 2 ++ src/debug/HyprCtl.cpp | 20 ++++++++++++++++++++ src/helpers/MiscFunctions.cpp | 29 ++++++++++++++++++++++++----- src/managers/InputManager.hpp | 9 ++++----- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 5b932441..60ee9606 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -23,6 +23,7 @@ usage: hyprctl [command] [(opt)args] clients activewindow layers + devices dispatch keyword version @@ -117,6 +118,7 @@ int main(int argc, char** argv) { else if (!strcmp(argv[1], "activewindow")) request("activewindow"); else if (!strcmp(argv[1], "layers")) request("layers"); else if (!strcmp(argv[1], "version")) request("version"); + else if (!strcmp(argv[1], "devices")) request("devices"); 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); diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 07c388e3..c890dbb2 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -72,6 +72,24 @@ std::string layersRequest() { return result; } +std::string devicesRequest() { + std::string result = ""; + + result += "mice:\n"; + + for (auto& m : g_pInputManager->m_lMice) { + result += getFormat("\tMouse at %x:\n\t\t%s\n", &m, m.mouse->name); + } + + result += "\n\nKeyboards:\n"; + + for (auto& k : g_pInputManager->m_lKeyboards) { + result += getFormat("\tKeyboard at %x:\n\t\t%s\n", &k, k.keyboard->name); + } + + return result; +} + std::string versionRequest() { std::string result = "Hyprland, built from branch " + std::string(GIT_BRANCH) + " at commit " + GIT_COMMIT_HASH + GIT_DIRTY + " (" + GIT_COMMIT_MESSAGE + ").\nflags: (if any)\n"; @@ -186,6 +204,8 @@ std::string getReply(std::string request) { return versionRequest(); else if (request == "reload") return reloadRequest(); + else if (request == "devices") + return devicesRequest(); else if (request.find("dispatch") == 0) return dispatchRequest(request); else if (request.find("keyword") == 0) diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index 6c53b8f2..7a947832 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -46,16 +46,35 @@ void wlr_signal_emit_safe(struct wl_signal *signal, void *data) { } std::string getFormat(const char *fmt, ...) { - char buf[2048] = ""; + char buf[LOGMESSAGESIZE] = ""; + char* outputStr; + int logLen; va_list args; va_start(args, fmt); - - vsprintf(buf, fmt, args); - + logLen = vsnprintf(buf, sizeof buf, fmt, args); va_end(args); - return std::string(buf); + if ((long unsigned int)logLen < sizeof buf) { + outputStr = strdup(buf); + } else { + outputStr = (char*)malloc(logLen + 1); + + if (!outputStr) { + printf("CRITICAL: Cannot alloc size %d for log! (Out of memory?)", logLen + 1); + return ""; + } + + va_start(args, fmt); + vsnprintf(outputStr, logLen + 1U, fmt, args); + va_end(args); + } + + std::string output = std::string(outputStr); + + free(outputStr); + + return output; } void scaleBox(wlr_box* box, float scale) { diff --git a/src/managers/InputManager.hpp b/src/managers/InputManager.hpp index 6850929b..3f5afc8a 100644 --- a/src/managers/InputManager.hpp +++ b/src/managers/InputManager.hpp @@ -36,12 +36,11 @@ public: SDrag m_sDrag; - std::list m_lConstraints; + std::list m_lConstraints; + std::list m_lKeyboards; + std::list m_lMice; -private: - - std::list m_lKeyboards; - std::list m_lMice; + private: void mouseMoveUnified(uint32_t, bool refocus = false); };