backends: Add method/property to get accelerometer availability

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1311
This commit is contained in:
Carlos Garnacho 2020-06-11 18:12:12 +02:00
parent cba82d19b3
commit 7f9949a5e3
2 changed files with 63 additions and 6 deletions

View File

@ -34,6 +34,17 @@ enum
static guint signals[N_SIGNALS]; static guint signals[N_SIGNALS];
enum
{
PROP_0,
PROP_HAS_ACCELEROMETER,
PROP_LAST
};
static GParamSpec *props[PROP_LAST];
struct _MetaOrientationManager struct _MetaOrientationManager
{ {
GObject parent_instance; GObject parent_instance;
@ -44,6 +55,7 @@ struct _MetaOrientationManager
GDBusProxy *iio_proxy; GDBusProxy *iio_proxy;
MetaOrientation prev_orientation; MetaOrientation prev_orientation;
MetaOrientation curr_orientation; MetaOrientation curr_orientation;
guint has_accel : 1;
GSettings *settings; GSettings *settings;
}; };
@ -71,22 +83,24 @@ orientation_from_string (const char *orientation)
static void static void
read_iio_proxy (MetaOrientationManager *self) read_iio_proxy (MetaOrientationManager *self)
{ {
gboolean has_accel = FALSE;
GVariant *v; GVariant *v;
self->curr_orientation = META_ORIENTATION_UNDEFINED; self->curr_orientation = META_ORIENTATION_UNDEFINED;
if (!self->iio_proxy) if (!self->iio_proxy)
return; {
self->has_accel = FALSE;
return;
}
v = g_dbus_proxy_get_cached_property (self->iio_proxy, "HasAccelerometer"); v = g_dbus_proxy_get_cached_property (self->iio_proxy, "HasAccelerometer");
if (v) if (v)
{ {
has_accel = g_variant_get_boolean (v); self->has_accel = !!g_variant_get_boolean (v);
g_variant_unref (v); g_variant_unref (v);
} }
if (has_accel) if (self->has_accel)
{ {
v = g_dbus_proxy_get_cached_property (self->iio_proxy, "AccelerometerOrientation"); v = g_dbus_proxy_get_cached_property (self->iio_proxy, "AccelerometerOrientation");
if (v) if (v)
@ -100,11 +114,16 @@ read_iio_proxy (MetaOrientationManager *self)
static void static void
sync_state (MetaOrientationManager *self) sync_state (MetaOrientationManager *self)
{ {
if (g_settings_get_boolean (self->settings, ORIENTATION_LOCK_KEY)) gboolean had_accel = self->has_accel;
return;
read_iio_proxy (self); read_iio_proxy (self);
if (had_accel != self->has_accel)
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HAS_ACCELEROMETER]);
if (g_settings_get_boolean (self->settings, ORIENTATION_LOCK_KEY))
return;
if (self->prev_orientation == self->curr_orientation) if (self->prev_orientation == self->curr_orientation)
return; return;
@ -241,6 +260,25 @@ meta_orientation_manager_init (MetaOrientationManager *self)
sync_state (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 static void
meta_orientation_manager_finalize (GObject *object) meta_orientation_manager_finalize (GObject *object)
{ {
@ -263,6 +301,7 @@ meta_orientation_manager_class_init (MetaOrientationManagerClass *klass)
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = meta_orientation_manager_finalize; gobject_class->finalize = meta_orientation_manager_finalize;
gobject_class->get_property = meta_orientation_manager_get_property;
signals[ORIENTATION_CHANGED] = signals[ORIENTATION_CHANGED] =
g_signal_new ("orientation-changed", g_signal_new ("orientation-changed",
@ -271,6 +310,16 @@ meta_orientation_manager_class_init (MetaOrientationManagerClass *klass)
0, 0,
NULL, NULL, NULL, NULL, NULL, NULL,
G_TYPE_NONE, 0); 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 MetaOrientation
@ -278,3 +327,9 @@ meta_orientation_manager_get_orientation (MetaOrientationManager *self)
{ {
return self->curr_orientation; return self->curr_orientation;
} }
gboolean
meta_orientation_manager_has_accelerometer (MetaOrientationManager *self)
{
return self->has_accel;
}

View File

@ -39,4 +39,6 @@ G_DECLARE_FINAL_TYPE (MetaOrientationManager, meta_orientation_manager,
MetaOrientation meta_orientation_manager_get_orientation (MetaOrientationManager *self); MetaOrientation meta_orientation_manager_get_orientation (MetaOrientationManager *self);
gboolean meta_orientation_manager_has_accelerometer (MetaOrientationManager *self);
#endif /* META_ORIENTATION_MANAGER_H */ #endif /* META_ORIENTATION_MANAGER_H */