[mutter/wip/carlosg/centralized-panel-auto-orientation: 1/3] backends: Add method/property to get accelerometer availability



commit 3654d6b1b9f9ae1f127eb112e893abe68a44759e
Author: Carlos Garnacho <carlosg gnome org>
Date:   Thu Jun 11 18:12:12 2020 +0200

    backends: Add method/property to get accelerometer availability

 src/backends/meta-orientation-manager.c | 69 ++++++++++++++++++++++++++++++---
 src/backends/meta-orientation-manager.h |  2 +
 2 files changed, 65 insertions(+), 6 deletions(-)
---
diff --git a/src/backends/meta-orientation-manager.c b/src/backends/meta-orientation-manager.c
index 9bf9e41dab..c711b711e7 100644
--- a/src/backends/meta-orientation-manager.c
+++ b/src/backends/meta-orientation-manager.c
@@ -34,6 +34,17 @@ enum
 
 static guint signals[N_SIGNALS];
 
+enum
+{
+  PROP_0,
+
+  PROP_HAS_ACCELEROMETER,
+
+  PROP_LAST
+};
+
+static GParamSpec *props[PROP_LAST];
+
 struct _MetaOrientationManager
 {
   GObject parent_instance;
@@ -44,6 +55,8 @@ struct _MetaOrientationManager
   GDBusProxy *iio_proxy;
   MetaOrientation prev_orientation;
   MetaOrientation curr_orientation;
+  guint has_accel : 1;
+  guint prev_has_accel : 1;
 
   GSettings *settings;
 };
@@ -71,22 +84,24 @@ orientation_from_string (const char *orientation)
 static void
 read_iio_proxy (MetaOrientationManager *self)
 {
-  gboolean has_accel = FALSE;
   GVariant *v;
 
   self->curr_orientation = META_ORIENTATION_UNDEFINED;
 
   if (!self->iio_proxy)
-    return;
+    {
+      self->has_accel = FALSE;
+      return;
+    }
 
   v = g_dbus_proxy_get_cached_property (self->iio_proxy, "HasAccelerometer");
   if (v)
     {
-      has_accel = g_variant_get_boolean (v);
+      self->has_accel = !!g_variant_get_boolean (v);
       g_variant_unref (v);
     }
 
-  if (has_accel)
+  if (self->has_accel)
     {
       v = g_dbus_proxy_get_cached_property (self->iio_proxy, "AccelerometerOrientation");
       if (v)
@@ -100,11 +115,17 @@ read_iio_proxy (MetaOrientationManager *self)
 static void
 sync_state (MetaOrientationManager *self)
 {
+  read_iio_proxy (self);
+
+  if (self->prev_has_accel != self->has_accel)
+    {
+      self->prev_has_accel = self->has_accel;
+      g_object_notify (G_OBJECT (self), "has-accelerometer");
+    }
+
   if (g_settings_get_boolean (self->settings, ORIENTATION_LOCK_KEY))
     return;
 
-  read_iio_proxy (self);
-
   if (self->prev_orientation == self->curr_orientation)
     return;
 
@@ -241,6 +262,25 @@ meta_orientation_manager_init (MetaOrientationManager *self)
   sync_state (self);
 }
 
+static void
+meta_orientation_manager_get_property (GObject    *object,
+                                       guint       prop_id,
+                                       GValue     *value,
+                                       GParamSpec *pspec)
+{
+  MetaOrientationManager *self = META_ORIENTATION_MANAGER (object);
+
+  switch (prop_id)
+    {
+    case PROP_HAS_ACCELEROMETER:
+      g_value_set_boolean (value, self->has_accel);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
 static void
 meta_orientation_manager_finalize (GObject *object)
 {
@@ -263,6 +303,7 @@ meta_orientation_manager_class_init (MetaOrientationManagerClass *klass)
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
 
   gobject_class->finalize = meta_orientation_manager_finalize;
+  gobject_class->get_property = meta_orientation_manager_get_property;
 
   signals[ORIENTATION_CHANGED] =
     g_signal_new ("orientation-changed",
@@ -271,6 +312,16 @@ meta_orientation_manager_class_init (MetaOrientationManagerClass *klass)
                   0,
                   NULL, NULL, NULL,
                   G_TYPE_NONE, 0);
+
+  props[PROP_HAS_ACCELEROMETER] =
+    g_param_spec_boolean ("has-accelerometer",
+                          "Has accelerometer",
+                          "Has accelerometer",
+                          FALSE,
+                          G_PARAM_READABLE |
+                          G_PARAM_EXPLICIT_NOTIFY |
+                          G_PARAM_STATIC_STRINGS);
+  g_object_class_install_properties (gobject_class, PROP_LAST, props);
 }
 
 MetaOrientation
@@ -278,3 +329,9 @@ meta_orientation_manager_get_orientation (MetaOrientationManager *self)
 {
   return self->curr_orientation;
 }
+
+gboolean
+meta_orientation_manager_has_accelerometer (MetaOrientationManager *self)
+{
+  return self->has_accel;
+}
diff --git a/src/backends/meta-orientation-manager.h b/src/backends/meta-orientation-manager.h
index 58a84368ff..9ae87957d4 100644
--- a/src/backends/meta-orientation-manager.h
+++ b/src/backends/meta-orientation-manager.h
@@ -39,4 +39,6 @@ G_DECLARE_FINAL_TYPE (MetaOrientationManager, meta_orientation_manager,
 
 MetaOrientation meta_orientation_manager_get_orientation (MetaOrientationManager *self);
 
+gboolean meta_orientation_manager_has_accelerometer (MetaOrientationManager *self);
+
 #endif  /* META_ORIENTATION_MANAGER_H */


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