#include "desktop-catalog.h" #include "process.h" #include "session-catalog.h" #include #include namespace fws { namespace { std::string lower(std::string value) { std::transform(value.begin(), value.end(), value.begin(), [](unsigned char character) { return static_cast(std::tolower(character)); }); return value; } bool pacmanKnows(const std::vector& arguments) { const CommandResult result = runCommand(arguments); return result.started && result.exitCode == 0; } bool targetInstalled(const std::string& target) { // A target is either a package (pacman -Q) or a group (pacman -Qg). if (pacmanKnows({"pacman", "-Q", target})) { return true; } return pacmanKnows({"pacman", "-Qg", target}); } } // namespace const std::vector& supportedDesktops() { static const std::vector desktops = { {"xfce", "XFCE", {"xfce4"}, {"xfce", "xfce4"}, {"xfce", "xfce4-session"}}, {"gnome", "GNOME", {"gnome"}, {"gnome"}, {"gnome", "gnome-xorg"}}, // Plasma 6 renamed its session files; the Wayland entry comes first. {"plasma", "KDE Plasma", {"plasma-meta"}, {"kde", "plasma"}, {"plasma", "plasmawayland", "plasmax11", "plasmadesktop"}}, {"hyprland", "Hyprland", {"hyprland"}, {"hyprland"}, {"hyprland"}}, {"i3", "i3", {"i3"}, {"i3"}, {"i3"}}, }; return desktops; } const DesktopProfile* findDesktopProfile(const std::string& name) { const std::string wanted = lower(name); if (wanted.empty()) { return nullptr; } for (const DesktopProfile& profile : supportedDesktops()) { if (lower(profile.id) == wanted || lower(profile.name) == wanted || lower(profile.name).rfind(wanted, 0) == 0) { return &profile; } } return nullptr; } bool isDesktopInstalled(const DesktopProfile& profile) { return std::all_of(profile.packages.begin(), profile.packages.end(), targetInstalled); } bool findActiveSession(const DesktopProfile& profile, std::string& sessionId) { // Prefer the session that owns the seat: a stale or closing session of the // same desktop would activate a VT that has nothing left to draw. std::string fallback; for (const SessionInfo& session : listGraphicalSessions()) { if (session.state == "closing" || session.state == "closing-failed") { continue; } const std::string desktop = lower(session.desktop); bool matches = false; for (const std::string& name : profile.sessionNames) { if (desktop == lower(name)) { matches = true; break; } } if (!matches) { continue; } if (session.active) { sessionId = session.id; return true; } if (fallback.empty()) { fallback = session.id; } } if (fallback.empty()) { return false; } sessionId = fallback; return true; } std::vector installArguments(const DesktopProfile& profile) { std::vector arguments = {"pkexec", "pacman", "-S", "--needed", "--noconfirm"}; arguments.insert(arguments.end(), profile.packages.begin(), profile.packages.end()); return arguments; } } // namespace fws