Files
FWS-DeskManger/src/gui.cpp
T
BlackAngelTV c172346052 feat(gui): report the autologin left by an interrupted launch
Launches start from this window, so the leftover file that would log the account in at the next boot has to be visible here, while the launch that clears it is still one click away.
2026-11-16 08:28:23 +01:00

844 lines
31 KiB
C++

#include <gtk/gtk.h>
#include <string>
#include <vector>
#include "desktop-catalog.h"
#include "session-catalog.h"
#include "session-launcher.h"
#include "session-switcher.h"
namespace {
// Palette kept in one place: it mirrors the identity used on the FWS website
// (dark canvas, violet accent, soft-bordered cards).
constexpr const char* kThemeCss = R"CSS(
window {
background-color: #0a0d14;
color: #e7eaf5;
font-family: "Inter", "Cantarell", "Noto Sans", sans-serif;
}
headerbar {
background-color: #0f131c;
background-image: none;
border-bottom: 1px solid #1f2634;
box-shadow: none;
min-height: 48px;
padding: 0 8px;
}
.icon-button {
background-color: transparent;
background-image: none;
border: none;
border-radius: 999px;
box-shadow: none;
padding: 6px;
min-width: 28px;
min-height: 28px;
}
.icon-button:hover {
background-color: #1e2434;
}
.wordmark {
background-color: #6d5efc;
background-image: none;
color: #ffffff;
font-size: 11px;
font-weight: 800;
letter-spacing: 1px;
padding: 4px 8px;
border-radius: 8px;
}
.app-title {
font-size: 14px;
font-weight: 600;
color: #eef1ff;
}
.eyebrow {
font-size: 11px;
font-weight: 700;
letter-spacing: 2px;
color: #a78bfa;
}
.hero-title {
font-size: 24px;
font-weight: 700;
color: #f5f7ff;
}
.hero-text {
font-size: 13px;
color: #8f99af;
}
.section-title {
font-size: 11px;
font-weight: 700;
letter-spacing: 2px;
color: #6f7a92;
}
.card {
background-color: #121724;
border: 1px solid #222b3d;
border-radius: 14px;
}
.card:hover {
background-color: #161c2b;
border-color: #33415c;
}
.profile-name {
font-size: 15px;
font-weight: 700;
color: #f4f6ff;
}
.profile-status {
font-size: 12px;
color: #98a2b8;
}
button {
border-radius: 999px;
font-size: 13px;
font-weight: 600;
padding: 6px 16px;
}
/* Own classes rather than the theme's suggested/destructive variants: the
installed GTK theme was repainting the buttons and washing the accent out. */
button.primary {
background-color: #6d5efc;
background-image: none;
border: none;
box-shadow: none;
color: #ffffff;
}
button.primary:hover {
background-color: #7d6fff;
}
button.primary:active {
background-color: #5b4ce6;
}
button.danger {
background-color: transparent;
background-image: none;
border: 1px solid #3d2833;
box-shadow: none;
color: #f28585;
}
button.danger:hover {
background-color: rgba(248, 113, 113, 0.14);
border-color: #8a4a52;
}
button:disabled {
opacity: 0.5;
}
.status-text {
font-size: 12px;
color: #8a94ab;
}
.log-card {
background-color: #0d1018;
border: 1px solid #232a3a;
border-radius: 14px;
padding: 10px;
}
.log-card textview,
.log-card textview text {
background-color: transparent;
color: #c3cbe0;
}
scrollbar slider {
background-color: #2c3547;
border-radius: 999px;
min-width: 6px;
min-height: 6px;
}
spinner {
color: #a78bfa;
}
)CSS";
// App-level provider: the stylesheet rides along with the window instead of
// depending on a theme file installed on the system.
void installTheme() {
GdkDisplay* display = gdk_display_get_default();
if (display == nullptr) {
return;
}
GtkCssProvider* provider = gtk_css_provider_new();
#if GTK_CHECK_VERSION(4, 12, 0)
gtk_css_provider_load_from_string(provider, kThemeCss);
#else
gtk_css_provider_load_from_data(provider, kThemeCss, -1);
#endif
gtk_style_context_add_provider_for_display(display, GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_unref(provider);
}
enum class RowMode { Install, SwitchTo, Greeter, Launch };
struct RowWidgets {
const fws::DesktopProfile* profile = nullptr;
GtkWidget* status = nullptr;
GtkWidget* action = nullptr;
RowMode mode = RowMode::Install;
};
class ManagerWindow {
public:
static int run(int argc, char** argv) {
GtkApplication* application =
gtk_application_new("tech.nocodevar.FwsDeskManager", G_APPLICATION_DEFAULT_FLAGS);
auto* manager = new ManagerWindow();
g_signal_connect(application, "activate", G_CALLBACK(&ManagerWindow::onActivate), manager);
const int status = g_application_run(G_APPLICATION(application), argc, argv);
g_object_unref(application);
delete manager;
return status;
}
private:
ManagerWindow() = default;
static void onActivate(GtkApplication* application, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
if (self->window_ == nullptr) {
installTheme();
// The site is dark; follow it even when the desktop prefers light.
if (GtkSettings* settings = gtk_settings_get_default(); settings != nullptr) {
g_object_set(settings, "gtk-application-prefer-dark-theme", TRUE, nullptr);
}
self->buildWindow(application);
}
gtk_window_present(GTK_WINDOW(self->window_));
}
static void onRefreshClicked(GtkButton*, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
if (!self->busy_) {
self->refreshRows();
}
}
static void onActionClicked(GtkButton* button, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
if (self->busy_) {
return;
}
const int index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "fws-row-index"));
if (index < 0 || index >= static_cast<int>(self->rows_.size())) {
return;
}
RowWidgets& row = self->rows_[static_cast<std::size_t>(index)];
switch (row.mode) {
case RowMode::Install:
self->startInstall(*row.profile);
break;
case RowMode::SwitchTo:
self->switchTo(*row.profile);
break;
case RowMode::Greeter:
self->askGreeter(*row.profile);
break;
case RowMode::Launch:
self->askLaunch(*row.profile);
break;
}
}
static void onInstallDone(GObject* source, GAsyncResult* result, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
GError* error = nullptr;
char* output = nullptr;
g_subprocess_communicate_utf8_finish(G_SUBPROCESS(source), result, &output, nullptr, &error);
const bool success = g_subprocess_get_successful(G_SUBPROCESS(source)) != FALSE;
std::string text = output != nullptr ? output : "";
if (error != nullptr) {
text += error->message;
g_error_free(error);
}
g_free(output);
self->finishInstall(success, text);
g_clear_object(&self->pending_);
}
// GtkAlertDialog exposes no "response" signal: the chosen button index is
// only available from gtk_alert_dialog_choose_finish().
static void onGreeterChosen(GObject* source, GAsyncResult* result, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
GError* error = nullptr;
const int response =
gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(source), result, &error);
if (error != nullptr) {
g_error_free(error);
return;
}
if (response != 1) {
return;
}
self->goToGreeter(false);
}
static void onForceChosen(GObject* source, GAsyncResult* result, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
GError* error = nullptr;
const int response =
gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(source), result, &error);
if (error != nullptr) {
g_error_free(error);
return;
}
if (response != 1) {
return;
}
self->goToGreeter(true);
}
static void onLaunchChosen(GObject* source, GAsyncResult* result, gpointer data) {
auto* self = static_cast<ManagerWindow*>(data);
GError* error = nullptr;
const int response =
gtk_alert_dialog_choose_finish(GTK_ALERT_DIALOG(source), result, &error);
if (error != nullptr) {
g_error_free(error);
return;
}
if (response != 1 || self->launchProfile_ == nullptr) {
return;
}
self->startLaunch(*self->launchProfile_);
}
void buildWindow(GtkApplication* application) {
window_ = gtk_application_window_new(application);
gtk_window_set_title(GTK_WINDOW(window_), "FWS DeskManager");
gtk_window_set_default_size(GTK_WINDOW(window_), 800, 720);
GtkWidget* header = gtk_header_bar_new();
// One line only: a stacked title was taller than the header bar and got
// clipped at the top edge of the window.
GtkWidget* brand = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_widget_set_valign(brand, GTK_ALIGN_CENTER);
GtkWidget* logo = gtk_label_new("FWS");
gtk_widget_add_css_class(logo, "wordmark");
gtk_widget_set_valign(logo, GTK_ALIGN_CENTER);
GtkWidget* appTitle = gtk_label_new("DeskManager");
gtk_widget_add_css_class(appTitle, "app-title");
gtk_widget_set_valign(appTitle, GTK_ALIGN_CENTER);
gtk_box_append(GTK_BOX(brand), logo);
gtk_box_append(GTK_BOX(brand), appTitle);
gtk_header_bar_set_title_widget(GTK_HEADER_BAR(header), brand);
refreshButton_ = gtk_button_new_from_icon_name("view-refresh-symbolic");
gtk_widget_add_css_class(refreshButton_, "icon-button");
gtk_widget_set_tooltip_text(refreshButton_, "Rafraîchir l'état des environnements");
g_signal_connect(refreshButton_, "clicked", G_CALLBACK(&ManagerWindow::onRefreshClicked), this);
gtk_header_bar_pack_end(GTK_HEADER_BAR(header), refreshButton_);
gtk_window_set_titlebar(GTK_WINDOW(window_), header);
GtkWidget* root = gtk_box_new(GTK_ORIENTATION_VERTICAL, 14);
// Centred column: on a wide window the rows stay readable instead of
// stretching a lone button to the far right edge.
gtk_widget_set_halign(root, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(root, 680, -1);
gtk_widget_set_margin_start(root, 20);
gtk_widget_set_margin_end(root, 20);
gtk_widget_set_margin_top(root, 18);
gtk_widget_set_margin_bottom(root, 18);
gtk_window_set_child(GTK_WINDOW(window_), root);
// Banner block: the same role as the site's hero, so the window reads as
// part of the same identity before the list of environments.
GtkWidget* hero = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
GtkWidget* eyebrow = gtk_label_new("FWS ISO");
gtk_widget_add_css_class(eyebrow, "eyebrow");
gtk_label_set_xalign(GTK_LABEL(eyebrow), 0.0F);
GtkWidget* heroTitle = gtk_label_new("Choisissez votre environnement");
gtk_widget_add_css_class(heroTitle, "hero-title");
gtk_label_set_xalign(GTK_LABEL(heroTitle), 0.0F);
GtkWidget* intro = gtk_label_new(
"Installez un environnement puis lancez-le sans passer par l'ecran de connexion : "
"la session precedente est fermee au profit de la nouvelle.");
gtk_label_set_wrap(GTK_LABEL(intro), TRUE);
gtk_label_set_xalign(GTK_LABEL(intro), 0.0F);
gtk_widget_add_css_class(intro, "hero-text");
gtk_box_append(GTK_BOX(hero), eyebrow);
gtk_box_append(GTK_BOX(hero), heroTitle);
gtk_box_append(GTK_BOX(hero), intro);
gtk_box_append(GTK_BOX(root), hero);
GtkWidget* sectionTitle = gtk_label_new("ENVIRONNEMENTS");
gtk_widget_add_css_class(sectionTitle, "section-title");
gtk_label_set_xalign(GTK_LABEL(sectionTitle), 0.0F);
gtk_widget_set_margin_top(sectionTitle, 4);
gtk_box_append(GTK_BOX(root), sectionTitle);
GtkWidget* scroller = gtk_scrolled_window_new();
gtk_widget_set_vexpand(scroller, TRUE);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller), GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
rowsBox_ = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroller), rowsBox_);
gtk_box_append(GTK_BOX(root), scroller);
GtkWidget* footer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
spinner_ = gtk_spinner_new();
gtk_widget_set_visible(spinner_, FALSE);
gtk_box_append(GTK_BOX(footer), spinner_);
statusLabel_ = gtk_label_new("Prêt.");
gtk_label_set_xalign(GTK_LABEL(statusLabel_), 0.0F);
gtk_widget_set_hexpand(statusLabel_, TRUE);
gtk_widget_add_css_class(statusLabel_, "status-text");
gtk_box_append(GTK_BOX(footer), statusLabel_);
gtk_box_append(GTK_BOX(root), footer);
logExpander_ = gtk_expander_new("Détails de l'installation");
gtk_widget_add_css_class(logExpander_, "log-card");
gtk_expander_set_expanded(GTK_EXPANDER(logExpander_), FALSE);
GtkWidget* logScroller = gtk_scrolled_window_new();
gtk_widget_set_size_request(logScroller, -1, 150);
logView_ = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(logView_), FALSE);
gtk_text_view_set_monospace(GTK_TEXT_VIEW(logView_), TRUE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(logView_), FALSE);
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(logScroller), logView_);
gtk_expander_set_child(GTK_EXPANDER(logExpander_), logScroller);
gtk_widget_set_visible(logExpander_, FALSE);
gtk_box_append(GTK_BOX(root), logExpander_);
buildRows();
refreshRows();
// A launch killed before its cleanup leaves the autologin armed: the next
// launch clears it, but the window has to say so before the reboot.
const std::string stale = fws::staleAutologinFile();
if (!stale.empty()) {
setStatus("Autologin residuel (« " + stale +
" ») : le prochain lancement le supprime.");
}
}
void buildRows() {
rows_.reserve(fws::supportedDesktops().size());
for (const fws::DesktopProfile& profile : fws::supportedDesktops()) {
GtkWidget* card = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
gtk_widget_add_css_class(card, "card");
gtk_widget_set_margin_start(card, 18);
gtk_widget_set_margin_end(card, 18);
gtk_widget_set_margin_top(card, 16);
gtk_widget_set_margin_bottom(card, 16);
GtkWidget* text = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4);
gtk_widget_set_hexpand(text, TRUE);
gtk_widget_set_valign(text, GTK_ALIGN_CENTER);
GtkWidget* name = gtk_label_new(profile.name.c_str());
gtk_widget_add_css_class(name, "profile-name");
gtk_label_set_xalign(GTK_LABEL(name), 0.0F);
GtkWidget* status = gtk_label_new("…");
gtk_widget_add_css_class(status, "profile-status");
gtk_label_set_xalign(GTK_LABEL(status), 0.0F);
gtk_box_append(GTK_BOX(text), name);
gtk_box_append(GTK_BOX(text), status);
GtkWidget* action = gtk_button_new_with_label("Installer");
gtk_widget_set_valign(action, GTK_ALIGN_CENTER);
gtk_widget_add_css_class(action, "primary");
g_signal_connect(action, "clicked", G_CALLBACK(&ManagerWindow::onActionClicked), this);
gtk_box_append(GTK_BOX(card), text);
gtk_box_append(GTK_BOX(card), action);
gtk_box_append(GTK_BOX(rowsBox_), card);
RowWidgets row;
row.profile = &profile;
row.status = status;
row.action = action;
row.mode = RowMode::Install;
rows_.push_back(row);
g_object_set_data(G_OBJECT(action), "fws-row-index",
GINT_TO_POINTER(static_cast<int>(rows_.size()) - 1));
}
}
// The session this window runs in: XDG_SESSION_ID, or the seat's active one
// when the variable is missing (pkexec, systemd-run...).
fws::SessionInfo currentSession() const {
fws::SessionInfo session;
if (fws::findGraphicalSession(fws::currentSessionId(), session)) {
return session;
}
fws::findGraphicalSession(fws::activeGraphicalSessionId(), session);
return session;
}
// Lets the status text paint before the blocking logind calls run.
static void flushPendingEvents() {
while (g_main_context_iteration(nullptr, FALSE) != FALSE) {
}
}
void refreshRows() {
const std::string current = currentSession().id;
for (RowWidgets& row : rows_) {
std::string sessionId;
const bool installed = fws::isDesktopInstalled(*row.profile);
const bool active = installed && fws::findActiveSession(*row.profile, sessionId);
const bool isCurrent = active && !current.empty() && sessionId == current;
gtk_widget_remove_css_class(row.action, "primary");
gtk_widget_remove_css_class(row.action, "danger");
if (!installed) {
gtk_label_set_markup(GTK_LABEL(row.status),
"<span foreground='#f5b84d'>Non installé</span>");
gtk_button_set_label(GTK_BUTTON(row.action), "Installer");
gtk_widget_add_css_class(row.action, "primary");
row.mode = RowMode::Install;
} else if (isCurrent) {
gtk_label_set_markup(GTK_LABEL(row.status),
"<span foreground='#4ade80'>Installé</span> · session actuelle");
// Activating the session already on screen would do nothing, so
// the useful action from here is leaving for the greeter.
gtk_button_set_label(GTK_BUTTON(row.action), "Écran de connexion");
gtk_widget_add_css_class(row.action, "primary");
row.mode = RowMode::Greeter;
} else if (active) {
gtk_label_set_markup(GTK_LABEL(row.status),
"<span foreground='#4ade80'>Installé</span> · session active");
gtk_button_set_label(GTK_BUTTON(row.action), "Basculer");
gtk_widget_add_css_class(row.action, "primary");
row.mode = RowMode::SwitchTo;
} else {
// Without a display manager that supports one-shot autologin,
// the greeter stays the only way in.
std::string reason;
if (fws::canStartWithoutGreeter(reason)) {
gtk_label_set_markup(GTK_LABEL(row.status),
"<span foreground='#4ade80'>Installé</span> · aucune "
"session");
gtk_button_set_label(GTK_BUTTON(row.action), "Lancer");
gtk_widget_set_tooltip_text(
row.action,
"Démarrer l'environnement sans écran de connexion : la session "
"actuelle sera fermée.");
gtk_widget_add_css_class(row.action, "primary");
row.mode = RowMode::Launch;
} else {
// The greeter opens on the session it remembered, not on the one
// this row points at, so the entry to pick is named right here.
const std::string entry = fws::greeterSessionName(row.profile->sessionNames);
if (entry.empty()) {
gtk_label_set_markup(GTK_LABEL(row.status),
"<span foreground='#4ade80'>Installé</span> · "
"aucune session");
} else {
gchar* markup = g_markup_printf_escaped(
"<span foreground='#4ade80'>Installé</span> · aucune session · "
"choisir « %s » au greeter",
entry.c_str());
gtk_label_set_markup(GTK_LABEL(row.status), markup);
g_free(markup);
}
gtk_button_set_label(GTK_BUTTON(row.action), "Écran de connexion");
gtk_widget_set_tooltip_text(row.action, nullptr);
gtk_widget_add_css_class(row.action, "danger");
row.mode = RowMode::Greeter;
}
}
gtk_widget_set_sensitive(row.action, selfBusy() ? FALSE : TRUE);
}
}
void startInstall(const fws::DesktopProfile& profile) {
const std::vector<std::string> arguments = fws::installArguments(profile);
std::vector<const char*> argv;
argv.reserve(arguments.size() + 1);
for (const std::string& argument : arguments) {
argv.push_back(argument.c_str());
}
argv.push_back(nullptr);
GError* error = nullptr;
GSubprocess* process = g_subprocess_newv(
argv.data(),
static_cast<GSubprocessFlags>(G_SUBPROCESS_FLAGS_STDOUT_PIPE |
G_SUBPROCESS_FLAGS_STDERR_MERGE),
&error);
if (process == nullptr) {
showError(std::string("Impossible de lancer l'installation : ") +
(error != nullptr ? error->message : "pkexec indisponible"));
if (error != nullptr) {
g_error_free(error);
}
return;
}
pending_ = process;
setBusy(true, "Installation de " + profile.name + "…");
gtk_text_buffer_set_text(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(logView_)), "", -1);
g_subprocess_communicate_utf8_async(process, nullptr, nullptr,
&ManagerWindow::onInstallDone, this);
}
void finishInstall(bool success, const std::string& output) {
setBusy(false, success ? "Installation terminée. Utilisez l'écran de connexion pour "
"ouvrir le nouvel environnement."
: "Échec de l'installation.");
if (!output.empty()) {
GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(logView_));
gtk_text_buffer_set_text(buffer, output.c_str(), -1);
gtk_widget_set_visible(logExpander_, TRUE);
gtk_expander_set_expanded(GTK_EXPANDER(logExpander_), !success);
}
refreshRows();
if (!success) {
showError("L'installation a échoué. Consultez les détails dans la fenêtre.");
}
}
void switchTo(const fws::DesktopProfile& profile) {
std::string sessionId;
if (!fws::findActiveSession(profile, sessionId)) {
refreshRows();
return;
}
if (sessionId == currentSession().id) {
setStatus("Vous êtes déjà sur " + profile.name + ".");
return;
}
// activateSession waits for the seat to move and rolls back, so the
// window blocks for a few seconds instead of leaving a black VT behind.
setStatus("Bascule vers " + profile.name + "…");
flushPendingEvents();
std::string error;
if (!fws::activateSession(sessionId, error)) {
showError("Échec de la bascule : " + error);
setStatus("La session précédente a été conservée.");
return;
}
setStatus("Bascule vers " + profile.name + "…");
}
// The greeter starts whichever session its own selector is set on, and no
// bus API lets us choose it: the dialog names the entry to pick before the
// user is sent to another VT, where this window is no longer visible.
void askGreeter(const fws::DesktopProfile& profile) {
std::string sessionId;
const bool running = fws::findActiveSession(profile, sessionId);
greeterIsCurrent_ = running && sessionId == currentSession().id;
greeterEntry_ = fws::greeterSessionName(profile.sessionNames);
std::string message;
if (greeterIsCurrent_) {
message = profile.name +
" tourne deja dans votre session. Sur l'ecran de connexion, ouvrez le "
"selecteur de session et choisissez un autre environnement : relancer "
"le meme environnement ne demarre pas une seconde session. Rien n'est "
"ferme ici.";
} else if (!greeterEntry_.empty()) {
message = "L'ecran de connexion va s'afficher. Choisissez « " + greeterEntry_ +
" » dans le selecteur de session, puis connectez-vous : c'est ce choix "
"qui decide de l'environnement demarre.";
} else {
message = "L'ecran de connexion va s'afficher. Choisissez " + profile.name +
" dans le selecteur de session, puis connectez-vous.";
}
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
const char* buttons[] = {"Annuler", "Continuer", nullptr};
gtk_alert_dialog_set_buttons(dialog, buttons);
gtk_alert_dialog_set_cancel_button(dialog, 0);
gtk_alert_dialog_set_default_button(dialog, 1);
gtk_alert_dialog_choose(dialog, GTK_WINDOW(window_), nullptr,
&ManagerWindow::onGreeterChosen, this);
g_object_unref(dialog);
}
void askForce(const std::string& reason) {
const std::string message =
reason +
"\n\nRedemarrer le gestionnaire d'affichage pour retrouver un ecran de connexion ? "
"L'authentification est demandee par polkit ; la session courante sera fermee et "
"les applications non enregistrees seront perdues.";
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
const char* buttons[] = {"Annuler", "Redemarrer et fermer", nullptr};
gtk_alert_dialog_set_buttons(dialog, buttons);
gtk_alert_dialog_set_cancel_button(dialog, 0);
gtk_alert_dialog_set_default_button(dialog, 0);
gtk_alert_dialog_choose(dialog, GTK_WINDOW(window_), nullptr,
&ManagerWindow::onForceChosen, this);
g_object_unref(dialog);
}
void goToGreeter(bool force) {
setStatus(force ? "Redémarrage de l'écran de connexion…"
: "Ouverture de l'écran de connexion…");
flushPendingEvents();
const fws::GreeterOutcome outcome = fws::switchToGreeter(force);
if (outcome.ok) {
if (force) {
setStatus("Écran de connexion redémarré.");
} else if (!greeterIsCurrent_ && !greeterEntry_.empty()) {
setStatus("Écran de connexion affiché : choisissez « " + greeterEntry_ +
" » dans le sélecteur de session pour l'ouvrir.");
} else {
setStatus("Écran de connexion affiché ; la session reste ouverte.");
}
refreshRows();
return;
}
// Nothing has been closed yet: offer the destructive path explicitly
// instead of dropping the user on a blank VT.
if (outcome.forceRequired && !force) {
askForce(outcome.error);
setStatus("La session courante a été conservée.");
return;
}
showError(outcome.error);
setStatus("Retour à l'écran de connexion impossible.");
}
// Starting a session replaces the current one, so the decision is explicit
// before polkit asks for the password.
void askLaunch(const fws::DesktopProfile& profile) {
launchProfile_ = &profile;
const std::string message =
"Lancer " + profile.name +
" sans passer par l'ecran de connexion ?\n\nLa session courante sera fermee : "
"les applications non enregistrees seront perdues. L'authentification est "
"demandee par polkit.";
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
const char* buttons[] = {"Annuler", "Lancer et fermer", nullptr};
gtk_alert_dialog_set_buttons(dialog, buttons);
gtk_alert_dialog_set_cancel_button(dialog, 0);
gtk_alert_dialog_set_default_button(dialog, 1);
gtk_alert_dialog_choose(dialog, GTK_WINDOW(window_), nullptr,
&ManagerWindow::onLaunchChosen, this);
g_object_unref(dialog);
}
void startLaunch(const fws::DesktopProfile& profile) {
setStatus("Lancement de " + profile.name + " sans écran de connexion…");
setBusy(true, "Lancement de " + profile.name + "…");
flushPendingEvents();
// Blocks while the display manager restarts and autologins; the window is
// then closed with the session it belongs to.
const fws::LaunchOutcome outcome = fws::launchDesktop(profile);
if (!outcome.ok) {
setBusy(false, "Échec du lancement de " + profile.name + ".");
showError(outcome.error);
refreshRows();
return;
}
setBusy(false, profile.name + " a démarré ; la session précédente se ferme.");
refreshRows();
}
void showError(const std::string& message) {
GtkAlertDialog* dialog = gtk_alert_dialog_new("%s", message.c_str());
gtk_alert_dialog_show(dialog, GTK_WINDOW(window_));
g_object_unref(dialog);
}
void setStatus(const std::string& message) {
gtk_label_set_text(GTK_LABEL(statusLabel_), message.c_str());
}
void setBusy(bool busy, const std::string& message) {
busy_ = busy;
gtk_widget_set_visible(spinner_, busy ? TRUE : FALSE);
if (busy) {
gtk_spinner_start(GTK_SPINNER(spinner_));
} else {
gtk_spinner_stop(GTK_SPINNER(spinner_));
}
gtk_widget_set_sensitive(refreshButton_, busy ? FALSE : TRUE);
setStatus(message);
refreshRows();
}
bool selfBusy() const { return busy_; }
GtkWidget* window_ = nullptr;
GtkWidget* rowsBox_ = nullptr;
GtkWidget* spinner_ = nullptr;
GtkWidget* statusLabel_ = nullptr;
GtkWidget* logView_ = nullptr;
GtkWidget* logExpander_ = nullptr;
GtkWidget* refreshButton_ = nullptr;
std::vector<RowWidgets> rows_;
GSubprocess* pending_ = nullptr;
bool busy_ = false;
// Filled by askLaunch(): the profile the pending dialog starts.
const fws::DesktopProfile* launchProfile_ = nullptr;
// Filled by askGreeter(): the greeter entry to name once it is on screen.
std::string greeterEntry_;
bool greeterIsCurrent_ = false;
};
} // namespace
int main(int argc, char** argv) {
// pkexec re-enters the same executable for the privileged launch: GTK is
// never initialised on that path.
if (argc == 4 && std::string(argv[1]) == "--privileged-launch") {
return fws::runLaunchHelper(argv[2], argv[3]);
}
// Same re-entry for removing a drop-in, which the watchdog unit runs as root
// when it notices a launch that never cleaned up.
if (argc == 2 && std::string(argv[1]) == "--privileged-cleanup") {
return fws::runAutologinCleanup();
}
return ManagerWindow::run(argc, argv);
}