From 43ca66779b7d267cc6c52bdea75d3ff8eb60132f Mon Sep 17 00:00:00 2001 From: Tuur Vanhoutte <4633209+zjeffer@users.noreply.github.com> Date: Fri, 27 Dec 2024 15:40:46 +0100 Subject: hyprpm: use glaze to parse hyprctl plugin list (#8812) * Use std::filesystem::path in hyprpm DataState to avoid concatenating strings with (folder + "/" + file) * Added getPluginStates helper method in DataState * Small clang-tidy improvements --- hyprpm/src/core/PluginManager.cpp | 48 ++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 28 deletions(-) (limited to 'hyprpm/src/core/PluginManager.cpp') diff --git a/hyprpm/src/core/PluginManager.cpp b/hyprpm/src/core/PluginManager.cpp index 48b108c8..a3bd5a09 100644 --- a/hyprpm/src/core/PluginManager.cpp +++ b/hyprpm/src/core/PluginManager.cpp @@ -7,10 +7,8 @@ #include #include -#include #include #include -#include #include #include #include @@ -21,6 +19,7 @@ #include #include +#include #include #include @@ -83,13 +82,13 @@ SHyprlandVersion CPluginManager::getHyprlandVersion(bool running) { hlbranch = hlbranch.substr(0, hlbranch.find(" at commit ")); std::string hldate = HLVERCALL.substr(HLVERCALL.find("Date: ") + 6); - hldate = hldate.substr(0, hldate.find("\n")); + hldate = hldate.substr(0, hldate.find('\n')); std::string hlcommits; if (HLVERCALL.contains("commits:")) { hlcommits = HLVERCALL.substr(HLVERCALL.find("commits:") + 9); - hlcommits = hlcommits.substr(0, hlcommits.find(" ")); + hlcommits = hlcommits.substr(0, hlcommits.find(' ')); } int commits = 0; @@ -378,7 +377,7 @@ eHeadersErrors CPluginManager::headersValid() { // find headers commit const std::string& cmd = std::format("PKG_CONFIG_PATH=\"{}/share/pkgconfig\" pkgconf --cflags --keep-system-cflags hyprland", DataState::getHeadersPath()); - auto headers = execAndGet(cmd.c_str()); + auto headers = execAndGet(cmd); if (!headers.contains("-I/")) return HEADERS_MISSING; @@ -790,35 +789,28 @@ ePluginLoadStateReturn CPluginManager::ensurePluginsLoadState() { const auto HOME = getenv("HOME"); const auto HIS = getenv("HYPRLAND_INSTANCE_SIGNATURE"); if (!HOME || !HIS) { - std::println(stderr, "PluginManager: no $HOME or HIS"); + std::println(stderr, "PluginManager: no $HOME or $HYPRLAND_INSTANCE_SIGNATURE"); return LOADSTATE_FAIL; } - const auto HYPRPMPATH = DataState::getDataStatePath() + "/"; + const auto HYPRPMPATH = DataState::getDataStatePath(); - auto pluginLines = execAndGet("hyprctl plugins list | grep Plugin"); + const auto json = glz::read_json(execAndGet("hyprctl plugins list -j")); + if (!json) { + std::println(stderr, "PluginManager: couldn't parse hyprctl output"); + return LOADSTATE_FAIL; + } std::vector loadedPlugins; + for (const auto& plugin : json.value()) { + if (!plugin.is_object() || !plugin.contains("name")) { + std::println(stderr, "PluginManager: couldn't parse plugin object"); + return LOADSTATE_FAIL; + } + loadedPlugins.emplace_back(plugin["name"].get()); + } std::println("{}", successString("Ensuring plugin load state")); - // iterate line by line - while (!pluginLines.empty()) { - auto plLine = pluginLines.substr(0, pluginLines.find('\n')); - - if (pluginLines.find('\n') != std::string::npos) - pluginLines = pluginLines.substr(pluginLines.find('\n') + 1); - else - pluginLines = ""; - - if (plLine.back() != ':') - continue; - - plLine = plLine.substr(7); - plLine = plLine.substr(0, plLine.find(" by ")); - - loadedPlugins.push_back(plLine); - } - // get state const auto REPOS = DataState::getAllRepositories(); @@ -853,7 +845,7 @@ ePluginLoadStateReturn CPluginManager::ensurePluginsLoadState() { for (auto const& p : loadedPlugins) { if (!enabled(p)) { // unload - if (!loadUnloadPlugin(HYPRPMPATH + repoForName(p) + "/" + p + ".so", false)) { + if (!loadUnloadPlugin(HYPRPMPATH / repoForName(p) / (p + ".so"), false)) { std::println("{}", infoString("{} will be unloaded after restarting Hyprland", p)); hyprlandVersionMismatch = true; } else @@ -870,7 +862,7 @@ ePluginLoadStateReturn CPluginManager::ensurePluginsLoadState() { if (std::find_if(loadedPlugins.begin(), loadedPlugins.end(), [&](const auto& other) { return other == p.name; }) != loadedPlugins.end()) continue; - if (!loadUnloadPlugin(HYPRPMPATH + repoForName(p.name) + "/" + p.filename, true)) { + if (!loadUnloadPlugin(HYPRPMPATH / repoForName(p.name) / p.filename, true)) { std::println("{}", infoString("{} will be loaded after restarting Hyprland", p.name)); hyprlandVersionMismatch = true; } else -- cgit v1.2.3