[gnome-shell/wip/hadess/PrefersNonDefaultGPU: 3/3] shell-app: Add PrefersNonDefaultGPU support to shell_app_launch()



commit 1e09f89ac257dde0b7010a08edf8af2fdbab8b4c
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Apr 29 11:20:40 2020 +0200

    shell-app: Add PrefersNonDefaultGPU support to shell_app_launch()
    
    Read the "PrefersNonDefaultGPU" key in desktop files to figure out
    whether the application prefers running on the discrete GPU, or the
    default GPU, and apply that.
    
    Update the "Launch..." contextual menu to allow launching on
    the default GPU if the application "prefers [the] non default GPU".
    
    See:
    https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#recognized-keys
    
    https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/1804

 js/ui/appDisplay.js | 10 +++++++---
 src/shell-app.c     | 18 ++++++++++++------
 src/shell-app.h     | 16 +++++++++++-----
 3 files changed, 30 insertions(+), 14 deletions(-)
---
diff --git a/js/ui/appDisplay.js b/js/ui/appDisplay.js
index 59eaa6e7b5..088ef0450e 100644
--- a/js/ui/appDisplay.js
+++ b/js/ui/appDisplay.js
@@ -2510,10 +2510,14 @@ var AppIconMenu = class AppIconMenu extends PopupMenu.PopupMenu {
 
             if (discreteGpuAvailable &&
                 this._source.app.state == Shell.AppState.STOPPED) {
-                this._onDiscreteGpuMenuItem = this._appendMenuItem(_("Launch using Dedicated Graphics 
Card"));
-                this._onDiscreteGpuMenuItem.connect('activate', () => {
+                let appPrefersNonDefaultGPU = appInfo.get_boolean('PrefersNonDefaultGPU');
+                let GpuPref = appPrefersNonDefaultGPU ? Shell.AppLaunchGpu.DEFAULT : 
Shell.AppLaunchGpu.DISCRETE;
+                this._onGpuMenuItem = this._appendMenuItem(appPrefersNonDefaultGPU
+                    ? _("Launch using Default Graphics Card")
+                    : _("Launch using Dedicated Graphics Card"));
+                this._onGpuMenuItem.connect('activate', () => {
                     this._source.animateLaunch();
-                    this._source.app.launch(0, -1, true);
+                    this._source.app.launch(0, -1, GpuPref);
                     this.emit('activate-window', null);
                 });
             }
diff --git a/src/shell-app.c b/src/shell-app.c
index 716a91c4f8..f73d960cfd 100644
--- a/src/shell-app.c
+++ b/src/shell-app.c
@@ -1339,20 +1339,21 @@ apply_discrete_gpu_env (GAppLaunchContext *context,
  * shell_app_launch:
  * @timestamp: Event timestamp, or 0 for current event timestamp
  * @workspace: Start on this workspace, or -1 for default
- * @discrete_gpu: Whether to start on the discrete GPU
+ * @gpu_pref: the GPU to prefer launching on
  * @error: A #GError
  */
 gboolean
-shell_app_launch (ShellApp     *app,
-                  guint         timestamp,
-                  int           workspace,
-                  gboolean      discrete_gpu,
-                  GError      **error)
+shell_app_launch (ShellApp           *app,
+                  guint               timestamp,
+                  int                 workspace,
+                  ShellAppLaunchGpu   gpu_pref,
+                  GError            **error)
 {
   ShellGlobal *global;
   GAppLaunchContext *context;
   gboolean ret;
   GSpawnFlags flags;
+  gboolean discrete_gpu = FALSE;
 
   if (app->info == NULL)
     {
@@ -1369,6 +1370,11 @@ shell_app_launch (ShellApp     *app,
 
   global = shell_global_get ();
   context = shell_global_create_app_launch_context (global, timestamp, workspace);
+  if (gpu_pref == SHELL_APP_LAUNCH_GPU_APP_PREF)
+    discrete_gpu = g_desktop_app_info_get_boolean (app->info, "PrefersNonDefaultGPU");
+  else
+    discrete_gpu = (gpu_pref == SHELL_APP_LAUNCH_GPU_DISCRETE);
+
   if (discrete_gpu)
     apply_discrete_gpu_env (context, global);
 
diff --git a/src/shell-app.h b/src/shell-app.h
index 8a09b642dc..a6b55c3359 100644
--- a/src/shell-app.h
+++ b/src/shell-app.h
@@ -18,6 +18,12 @@ typedef enum {
   SHELL_APP_STATE_RUNNING
 } ShellAppState;
 
+typedef enum {
+  SHELL_APP_LAUNCH_GPU_APP_PREF = 0,
+  SHELL_APP_LAUNCH_GPU_DISCRETE,
+  SHELL_APP_LAUNCH_GPU_DEFAULT
+} ShellAppLaunchGpu;
+
 const char *shell_app_get_id (ShellApp *app);
 
 GDesktopAppInfo *shell_app_get_app_info (ShellApp *app);
@@ -51,11 +57,11 @@ GSList *shell_app_get_pids (ShellApp *app);
 
 gboolean shell_app_is_on_workspace (ShellApp *app, MetaWorkspace *workspace);
 
-gboolean shell_app_launch (ShellApp     *app,
-                           guint         timestamp,
-                           int           workspace,
-                           gboolean      discrete_gpu,
-                           GError      **error);
+gboolean shell_app_launch (ShellApp           *app,
+                           guint               timestamp,
+                           int                 workspace,
+                           ShellAppLaunchGpu   gpu_pref,
+                           GError            **error);
 
 void shell_app_launch_action (ShellApp        *app,
                               const char      *action_name,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]