[gnome-shell/wip/hadess/add-nvidia-other-gpu-support: 2/4] shell: Add API to access switcheroo-control D-Bus proxy



commit 3ab8370ddb877f57e8a5628c4049073ce766f404
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Dec 3 12:07:54 2019 +0100

    shell: Add API to access switcheroo-control D-Bus proxy
    
    See: https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/781

 src/shell-global.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shell-global.h |  3 +++
 2 files changed, 69 insertions(+)
---
diff --git a/src/shell-global.c b/src/shell-global.c
index 77e167c107..4b331d63cc 100644
--- a/src/shell-global.c
+++ b/src/shell-global.c
@@ -44,6 +44,7 @@
 #include "shell-window-tracker.h"
 #include "shell-wm.h"
 #include "st.h"
+#include "switcheroo-control.h"
 
 static ShellGlobal *the_object = NULL;
 
@@ -81,6 +82,9 @@ struct _ShellGlobal {
   gboolean has_modal;
   gboolean frame_timestamps;
   gboolean frame_finish_timestamp;
+
+  GDBusProxy *switcheroo_control;
+  GCancellable *switcheroo_cancellable;
 };
 
 enum {
@@ -102,6 +106,7 @@ enum {
   PROP_FOCUS_MANAGER,
   PROP_FRAME_TIMESTAMPS,
   PROP_FRAME_FINISH_TIMESTAMP,
+  PROP_SWITCHEROO_CONTROL,
 };
 
 /* Signals */
@@ -116,6 +121,29 @@ G_DEFINE_TYPE(ShellGlobal, shell_global, G_TYPE_OBJECT);
 
 static guint shell_global_signals [LAST_SIGNAL] = { 0 };
 
+static void
+switcheroo_control_ready_cb (GObject      *source_object,
+                             GAsyncResult *res,
+                             gpointer      user_data)
+{
+  ShellGlobal *global;
+  GError *error = NULL;
+  ShellNetHadessSwitcherooControl *control;
+
+  control = shell_net_hadess_switcheroo_control_proxy_new_for_bus_finish (res, &error);
+  if (!control)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_debug ("Could not get switcheroo-control GDBusProxy: %s", error->message);
+      g_clear_error (&error);
+      return;
+    }
+
+  global = user_data;
+  global->switcheroo_control = G_DBUS_PROXY (control);
+  g_debug ("Got switcheroo-control proxy successfully");
+}
+
 static void
 shell_global_set_property(GObject         *object,
                           guint            prop_id,
@@ -210,6 +238,9 @@ shell_global_get_property(GObject         *object,
     case PROP_FRAME_FINISH_TIMESTAMP:
       g_value_set_boolean (value, global->frame_finish_timestamp);
       break;
+    case PROP_SWITCHEROO_CONTROL:
+      g_value_set_object (value, global->switcheroo_control);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -309,6 +340,15 @@ shell_global_init (ShellGlobal *global)
   global->save_ops = g_hash_table_new_full (g_file_hash,
                                             (GEqualFunc) g_file_equal,
                                             g_object_unref, g_object_unref);
+
+  global->switcheroo_cancellable = g_cancellable_new ();
+  shell_net_hadess_switcheroo_control_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
+                                                         G_DBUS_PROXY_FLAGS_NONE,
+                                                         "net.hadess.SwitcherooControl",
+                                                         "/net/hadess/SwitcherooControl",
+                                                         global->switcheroo_cancellable,
+                                                         switcheroo_control_ready_cb,
+                                                         global);
 }
 
 static void
@@ -321,6 +361,9 @@ shell_global_finalize (GObject *object)
 
   the_object = NULL;
 
+  g_cancellable_cancel (global->switcheroo_cancellable);
+  g_clear_object (&global->switcheroo_cancellable);
+
   g_clear_object (&global->userdatadir_path);
   g_clear_object (&global->runtime_state_path);
 
@@ -477,6 +520,13 @@ shell_global_class_init (ShellGlobalClass *klass)
                                                          "Whether at the end of a frame to call glFinish and 
log paintCompletedTimestamp",
                                                          FALSE,
                                                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (gobject_class,
+                                   PROP_SWITCHEROO_CONTROL,
+                                   g_param_spec_object ("switcheroo-control",
+                                                        "switcheroo-control",
+                                                        "D-Bus Proxy for switcheroo-control daemon",
+                                                        SHELL_TYPE_NET_HADESS_SWITCHEROO_CONTROL,
+                                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 }
 
 /*
@@ -1260,6 +1310,22 @@ shell_global_sync_pointer (ShellGlobal *global)
   clutter_event_put ((ClutterEvent *)&event);
 }
 
+/**
+ * shell_global_get_switcheroo_control:
+ * @global: A #ShellGlobal
+ *
+ * Get the global #GDBusProxy instance for the switcheroo-control
+ * daemon.
+ *
+ * Return value: (transfer none): the #GDBusProxy for the daemon,
+ *   or %NULL on error.
+ */
+GDBusProxy *
+shell_global_get_switcheroo_control    (ShellGlobal  *global)
+{
+  return global->switcheroo_control;
+}
+
 /**
  * shell_global_get_settings:
  * @global: A #ShellGlobal
diff --git a/src/shell-global.h b/src/shell-global.h
index ad67c72c07..e1fa60aefb 100644
--- a/src/shell-global.h
+++ b/src/shell-global.h
@@ -66,6 +66,9 @@ void shell_global_run_at_leisure (ShellGlobal          *global,
 /* Misc utilities / Shell API */
 void     shell_global_sync_pointer              (ShellGlobal  *global);
 
+GDBusProxy *
+         shell_global_get_switcheroo_control    (ShellGlobal  *global);
+
 GAppLaunchContext *
          shell_global_create_app_launch_context (ShellGlobal  *global,
                                                  guint32       timestamp,


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