diff --git a/src/desktop-catalog.cpp b/src/desktop-catalog.cpp index d00f5df..24b05c0 100644 --- a/src/desktop-catalog.cpp +++ b/src/desktop-catalog.cpp @@ -46,16 +46,38 @@ bool isDesktopInstalled(const DesktopProfile& profile) { } 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)) { - sessionId = session.id; - return true; + matches = true; + break; } } + if (!matches) { + continue; + } + if (session.active) { + sessionId = session.id; + return true; + } + if (fallback.empty()) { + fallback = session.id; + } } - return false; + + if (fallback.empty()) { + return false; + } + sessionId = fallback; + return true; } std::vector installArguments(const DesktopProfile& profile) { diff --git a/src/desktop-catalog.h b/src/desktop-catalog.h index ca8367c..6446e8a 100644 --- a/src/desktop-catalog.h +++ b/src/desktop-catalog.h @@ -17,7 +17,7 @@ const std::vector& supportedDesktops(); // True when every pacman target of the profile is installed. bool isDesktopInstalled(const DesktopProfile& profile); -// Finds an active logind session for the profile. +// Finds a logind session for the profile, preferring the one on the seat. bool findActiveSession(const DesktopProfile& profile, std::string& sessionId); // pkexec + pacman command used to install the profile. diff --git a/src/session-catalog.cpp b/src/session-catalog.cpp index 472417c..a88e899 100644 --- a/src/session-catalog.cpp +++ b/src/session-catalog.cpp @@ -74,6 +74,89 @@ std::string valueForKey(const std::vector& lines, const std::string return {}; } +bool isSessionId(const std::string& value) { + return !value.empty() && + std::all_of(value.begin(), value.end(), + [](unsigned char character) { return std::isdigit(character) != 0; }); +} + +struct SessionRow { + std::string id; + std::string uid; + std::string user; + std::string seat; + std::string tty; +}; + +// Every logind session, whatever its owner: greeter sessions belong to the +// display manager account, not to the person in front of the screen. +std::vector listSessionRows() { + std::vector rows; + const CommandResult listing = runCommand({"loginctl", "list-sessions", "--no-legend"}); + if (!listing.started || listing.exitCode != 0) { + return rows; + } + + for (const std::string& line : splitLines(listing.output)) { + std::istringstream stream(line); + SessionRow row; + if (!(stream >> row.id >> row.uid >> row.user >> row.seat >> row.tty)) { + continue; + } + if (!isSessionId(row.id)) { + continue; + } + rows.push_back(std::move(row)); + } + return rows; +} + +std::vector sessionProperties(const std::string& id, + const std::vector& names) { + std::vector arguments = {"loginctl", "show-session", id}; + for (const std::string& name : names) { + arguments.push_back("-p"); + arguments.push_back(name); + } + + const CommandResult details = runCommand(arguments); + if (!details.started || details.exitCode != 0) { + return {}; + } + return splitLines(details.output); +} + +SessionInfo describeSession(const SessionRow& row) { + SessionInfo info; + info.id = row.id; + info.user = row.user; + info.seat = row.seat; + info.tty = row.tty; + + const std::vector properties = + sessionProperties(row.id, {"Type", "Class", "Active", "Desktop", "TTY", "Seat", "State"}); + if (properties.empty()) { + return info; + } + + info.type = valueForKey(properties, "Type"); + // logind is authoritative: the list columns can lag behind a VT switch. + info.seat = valueForKey(properties, "Seat"); + info.tty = valueForKey(properties, "TTY"); + info.desktop = valueForKey(properties, "Desktop"); + info.state = valueForKey(properties, "State"); + info.active = valueForKey(properties, "Active") == "yes"; + info.graphical = valueForKey(properties, "Class") == "user" && + (info.type == "x11" || info.type == "wayland"); + return info; +} + +// Session ids are numeric, so comparing length first keeps the order numeric +// without parsing (and without std::stoi throwing on an unexpected value). +bool idBefore(const std::string& left, const std::string& right) { + return left.size() != right.size() ? left.size() < right.size() : left < right; +} + } // namespace std::vector listInstalledDesktops() { @@ -117,60 +200,98 @@ std::vector listInstalledDesktops() { return entries; } +std::string greeterSessionName(const std::vector& desktopNames) { + std::string fromDesktopNames; + for (const DesktopEntry& entry : listInstalledDesktops()) { + for (const std::string& name : desktopNames) { + const std::string wanted = lower(name); + if (lower(entry.id) == wanted || lower(entry.name) == wanted) { + return entry.name; + } + // DesktopNames is a semicolon separated list, e.g. "XFCE". + std::istringstream values(lower(entry.desktops)); + std::string value; + while (std::getline(values, value, ';')) { + if (trim(value) == wanted && fromDesktopNames.empty()) { + fromDesktopNames = entry.name; + } + } + } + } + // DesktopNames also matches the other session files of the same desktop, so + // the id/name match above wins and this is only the fallback. + return fromDesktopNames; +} + std::vector listGraphicalSessions() { std::vector sessions; - const CommandResult listing = runCommand({"loginctl", "list-sessions", "--no-legend"}); - if (!listing.started || listing.exitCode != 0) { - return sessions; - } - const std::string uid = std::to_string(getuid()); - for (const std::string& line : splitLines(listing.output)) { - std::istringstream stream(line); - std::string id; - std::string sessionUid; - std::string user; - std::string seat; - std::string tty; - if (!(stream >> id >> sessionUid >> user >> seat >> tty)) { + for (const SessionRow& row : listSessionRows()) { + if (row.uid != uid) { continue; } - if (sessionUid != uid) { + SessionInfo info = describeSession(row); + if (!info.graphical) { continue; } - - const CommandResult details = runCommand( - {"loginctl", "show-session", id, "-p", "Type", "-p", "Class", "-p", "Active", - "-p", "Desktop", "-p", "TTY", "-p", "Seat"}); - if (!details.started || details.exitCode != 0) { - continue; - } - - const std::vector properties = splitLines(details.output); - const std::string type = valueForKey(properties, "Type"); - const std::string sessionClass = valueForKey(properties, "Class"); - if (sessionClass != "user" || (type != "x11" && type != "wayland")) { - continue; - } - - SessionInfo info; - info.id = id; - info.user = user; - info.seat = valueForKey(properties, "Seat"); - info.tty = valueForKey(properties, "TTY"); - info.type = type; - info.desktop = valueForKey(properties, "Desktop"); - info.active = valueForKey(properties, "Active") == "yes"; - info.graphical = true; sessions.push_back(std::move(info)); } - std::sort(sessions.begin(), sessions.end(), [](const SessionInfo& left, const SessionInfo& right) { - return std::stoi(left.id) < std::stoi(right.id); - }); + std::sort(sessions.begin(), sessions.end(), + [](const SessionInfo& left, const SessionInfo& right) { return idBefore(left.id, right.id); }); return sessions; } +bool findGraphicalSession(const std::string& id, SessionInfo& session) { + if (!isSessionId(id)) { + return false; + } + for (const SessionInfo& candidate : listGraphicalSessions()) { + if (candidate.id == id) { + session = candidate; + return true; + } + } + return false; +} + +bool isSessionActive(const std::string& id) { + if (!isSessionId(id)) { + return false; + } + const std::vector properties = sessionProperties(id, {"Active"}); + return valueForKey(properties, "Active") == "yes"; +} + +std::string activeGraphicalSessionId() { + for (const SessionInfo& session : listGraphicalSessions()) { + if (session.active) { + return session.id; + } + } + return {}; +} + +std::string findGreeterSessionId(const std::string& seat) { + std::string inactive; + for (const SessionRow& row : listSessionRows()) { + const std::vector properties = sessionProperties(row.id, {"Class", "Seat", "Active"}); + if (properties.empty() || valueForKey(properties, "Class") != "greeter") { + continue; + } + if (!seat.empty() && valueForKey(properties, "Seat") != seat) { + continue; + } + if (valueForKey(properties, "Active") == "yes") { + return row.id; + } + if (inactive.empty()) { + inactive = row.id; + } + } + return inactive; +} + std::string currentSessionId() { const char* value = std::getenv("XDG_SESSION_ID"); return value != nullptr ? value : ""; diff --git a/src/session-catalog.h b/src/session-catalog.h index d7e2d0a..0590f1a 100644 --- a/src/session-catalog.h +++ b/src/session-catalog.h @@ -20,6 +20,7 @@ struct SessionInfo { std::string tty; std::string type; // x11, wayland, tty, ... std::string desktop; // KDE, GNOME, Hyprland, ... + std::string state; // logind State: active, online, closing, ... bool active = false; bool graphical = false; }; @@ -30,6 +31,22 @@ std::vector listInstalledDesktops(); // Reads the current user's graphical sessions from systemd-logind. std::vector listGraphicalSessions(); +// Looks up one of the current user's graphical sessions by logind id. +bool findGraphicalSession(const std::string& id, SessionInfo& session); + +// True when logind reports the session as the active one on its seat. +bool isSessionActive(const std::string& id); + +// Id of the active graphical session of the current user, empty when none. +std::string activeGraphicalSessionId(); + +// Greeter session (Class=greeter) known to logind, optionally on one seat. +std::string findGreeterSessionId(const std::string& seat); + +// Name of the installed session file the greeter lists for one of these desktop +// names, empty when nothing matches (the greeter then keeps its own default). +std::string greeterSessionName(const std::vector& desktopNames); + std::string currentSessionId(); std::string currentDesktopName();