feat(session): switch desktop environments through logind sessions

Activate the target graphical session instead of rebooting, so a KDE or
GNOME session stays alive while another one is used. Switching relies on
logind VT activation, which needs no root privileges and never restarts
the display manager.
This commit is contained in:
BlackAngelTV committed 2026-11-04 13:06:26 +01:00
1 parent e8c44c6c86
commit 75c7c5c0ad
11 files changed
+883

No files matched your search

+184
View File
@@ -0,0 +1,184 @@
#include "session-catalog.h"
#include "process.h"
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <utility>
namespace fws {
namespace {
struct DesktopFile {
std::string name;
std::string exec;
std::string desktops;
};
DesktopFile parseDesktopFile(const std::filesystem::path& path) {
DesktopFile fields;
std::ifstream stream(path);
std::string line;
bool inDesktopEntry = false;
while (std::getline(stream, line)) {
line = trim(line);
if (line.empty() || line[0] == '#') {
continue;
}
if (line.front() == '[' && line.back() == ']') {
// Only the main group carries the session metadata.
inDesktopEntry = line == "[Desktop Entry]";
continue;
}
if (!inDesktopEntry) {
continue;
}
const auto separator = line.find('=');
if (separator == std::string::npos) {
continue;
}
const std::string key = trim(line.substr(0, separator));
const std::string value = trim(line.substr(separator + 1));
if (key == "Name") {
fields.name = value;
} else if (key == "Exec") {
fields.exec = value;
} else if (key == "DesktopNames") {
fields.desktops = value;
}
}
return fields;
}
std::string lower(std::string value) {
std::transform(value.begin(), value.end(), value.begin(),
[](unsigned char character) { return static_cast<char>(std::tolower(character)); });
return value;
}
std::string valueForKey(const std::vector<std::string>& lines, const std::string& key) {
const std::string prefix = key + "=";
for (const std::string& line : lines) {
if (line.rfind(prefix, 0) == 0) {
return line.substr(prefix.size());
}
}
return {};
}
} // namespace
std::vector<DesktopEntry> listInstalledDesktops() {
std::vector<DesktopEntry> entries;
const std::vector<std::pair<std::filesystem::path, bool>> directories = {
{"/usr/share/xsessions", false},
{"/usr/local/share/xsessions", false},
{"/usr/share/wayland-sessions", true},
{"/usr/local/share/wayland-sessions", true},
};
for (const auto& [directory, wayland] : directories) {
std::error_code error;
if (!std::filesystem::is_directory(directory, error)) {
continue;
}
for (const auto& item : std::filesystem::directory_iterator(directory, error)) {
if (error) {
break;
}
if (!item.is_regular_file() || item.path().extension() != ".desktop") {
continue;
}
const DesktopFile fields = parseDesktopFile(item.path());
if (fields.name.empty() || fields.exec.empty()) {
continue;
}
DesktopEntry entry;
entry.id = item.path().stem().string();
entry.name = fields.name;
entry.exec = fields.exec;
entry.desktops = fields.desktops;
entry.wayland = wayland;
entries.push_back(std::move(entry));
}
}
std::sort(entries.begin(), entries.end(), [](const DesktopEntry& left, const DesktopEntry& right) {
return lower(left.name) < lower(right.name);
});
return entries;
}
std::vector<SessionInfo> listGraphicalSessions() {
std::vector<SessionInfo> 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)) {
continue;
}
if (sessionUid != uid) {
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<std::string> 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);
});
return sessions;
}
std::string currentSessionId() {
const char* value = std::getenv("XDG_SESSION_ID");
return value != nullptr ? value : "";
}
std::string currentDesktopName() {
const char* value = std::getenv("XDG_CURRENT_DESKTOP");
return value != nullptr ? value : "";
}
} // namespace fws