From 60f8a7f6810a8c6d155190077369a268b9de1a7b Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sun, 25 Jan 2026 22:33:49 +0100 Subject: [PATCH] shell-app: Improve discrete GPU detection The original logic only took into account if a GPU is the "Default", meaning the GPU used during boot, which was intended for Laptops with Hybrid Graphics. The new logic uses the new Discrete key to figure out which GPU is the most appropriate to use in order of: - the first non-default GPU if it is discrete - the first discrete GPU if it exists - the first non-default GPU Part-of: --- .../net.hadess.SwitcherooControl.xml | 3 +- src/shell-app.c | 45 ++++++++++++++++--- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/data/dbus-interfaces/net.hadess.SwitcherooControl.xml b/data/dbus-interfaces/net.hadess.SwitcherooControl.xml index e52bc1a0d2..59a889654f 100644 --- a/data/dbus-interfaces/net.hadess.SwitcherooControl.xml +++ b/data/dbus-interfaces/net.hadess.SwitcherooControl.xml @@ -38,7 +38,8 @@ will contain a user-facing name for the GPU, the "Environment" (as) key will contain an array of even number of strings, each being an environment variable to set to use the GPU, followed by its value, the "Default" (b) key - will tag the default (usually integrated) GPU. + will tag the default GPU, the "Discrete" (b) key tags if the GPU is a + dedicated component. --> diff --git a/src/shell-app.c b/src/shell-app.c index 916783a597..84b75beb9c 100644 --- a/src/shell-app.c +++ b/src/shell-app.c @@ -1268,8 +1268,11 @@ apply_discrete_gpu_env (GAppLaunchContext *context, ShellGlobal *global) { GDBusProxy *proxy; - GVariant* variant; + GVariant *variant; guint num_children, i; + g_autoptr(GVariant) first_nondefault_discrete = NULL; + g_autoptr(GVariant) first_discrete = NULL; + g_autoptr(GVariant) first_nondefault = NULL; proxy = shell_global_get_switcheroo_control (global); if (!proxy) @@ -1289,19 +1292,47 @@ apply_discrete_gpu_env (GAppLaunchContext *context, for (i = 0; i < num_children; i++) { g_autoptr(GVariant) gpu = NULL; - g_autoptr(GVariant) env = NULL; g_autoptr(GVariant) default_variant = NULL; - g_autofree const char **env_s = NULL; - guint j; + g_autoptr(GVariant) discrete_variant = NULL; + gboolean is_default = FALSE; + gboolean is_discrete = FALSE; gpu = g_variant_get_child_value (variant, i); if (!gpu || !g_variant_is_of_type (gpu, G_VARIANT_TYPE ("a{s*}"))) continue; - /* Skip over the default GPU */ - default_variant = g_variant_lookup_value (gpu, "Default", NULL); - if (!default_variant || g_variant_get_boolean (default_variant)) + default_variant = g_variant_lookup_value (gpu, "Default", G_VARIANT_TYPE_BOOLEAN); + if (default_variant) + is_default = g_variant_get_boolean (default_variant); + + discrete_variant = g_variant_lookup_value (gpu, "Discrete", G_VARIANT_TYPE_BOOLEAN); + if (discrete_variant) + is_discrete = g_variant_get_boolean (discrete_variant); + + if (is_discrete) + { + if (first_nondefault_discrete == NULL && !is_default) + first_nondefault_discrete = g_variant_ref (gpu); + + if (first_discrete == NULL) + first_discrete = g_variant_ref (gpu); + } + + if (first_nondefault == NULL && !is_default) + first_nondefault = g_variant_ref (gpu); + } + + GVariant *gpu_list[] = { first_nondefault_discrete, first_discrete, first_nondefault }; + + for (i = 0; i < G_N_ELEMENTS (gpu_list); ++i) + { + GVariant *gpu = gpu_list[i]; + g_autoptr(GVariant) env = NULL; + g_autofree const char **env_s = NULL; + guint j; + + if (!gpu) continue; env = g_variant_lookup_value (gpu, "Environment", NULL); -- GitLab