[gnome-settings-daemon/wip/hadess/lid-is-closed-removal: 165/165] power: Use logind to get lid status




commit 4c93697f89a8365694010585506a86c095a3c284
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Apr 22 15:56:50 2021 +0200

    power: Use logind to get lid status
    
    Rather than upower, where the lid status functions have been obsolete
    for some time.

 plugins/power/gsd-power-manager.c | 55 +++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 19 deletions(-)
---
diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c
index 757986e3..cc4c64f8 100644
--- a/plugins/power/gsd-power-manager.c
+++ b/plugins/power/gsd-power-manager.c
@@ -154,7 +154,6 @@ struct _GsdPowerManager
         gboolean                 screensaver_active;
 
         /* State */
-        gboolean                 lid_is_present;
         gboolean                 lid_is_closed;
         gboolean                 session_is_active;
         UpClient                *up_client;
@@ -1559,19 +1558,40 @@ do_lid_closed_action (GsdPowerManager *manager)
         }
 }
 
+static gboolean
+is_lid_closed (GsdPowerManager *manager)
+{
+        GVariant *variant;
+        gboolean is_lid_closed = FALSE;
+
+        variant = g_dbus_proxy_get_cached_property (manager->logind_proxy,
+                                                    "LidClosed");
+        if (variant) {
+                is_lid_closed = g_variant_get_boolean (variant);
+                g_variant_unref (variant);
+        }
+
+        return is_lid_closed;
+}
+
 static void
-lid_state_changed_cb (UpClient *client, GParamSpec *pspec, GsdPowerManager *manager)
+logind_proxy_changed_cb (GDBusProxy *proxy,
+                         GVariant   *changed_properties,
+                         GStrv       invalidated_properties,
+                         gpointer    user_data)
 {
+        GsdPowerManager *manager = user_data;
+        g_autoptr(GVariant) lid_closed = NULL;
         gboolean tmp;
 
-        if (!manager->lid_is_present)
+        lid_closed = g_dbus_proxy_get_cached_property (proxy, "LidClosed");
+        if (lid_closed == NULL)
                 return;
-
-        /* same lid state */
-        tmp = up_client_get_lid_is_closed (manager->up_client);
-        if (manager->lid_is_closed == tmp)
+        tmp = g_variant_get_boolean (lid_closed);
+        if (tmp == manager->lid_is_closed)
                 return;
-        manager->lid_is_closed = tmp;
+
+       manager->lid_is_closed = tmp;
         g_debug ("up changed: lid is now %s", tmp ? "closed" : "open");
 
         if (manager->lid_is_closed)
@@ -2847,11 +2867,9 @@ on_rr_screen_acquired (GObject      *object,
         manager->session_is_active = is_session_active (manager);
 
         /* set up the screens */
-        if (manager->lid_is_present) {
-                g_signal_connect (manager->rr_screen, "changed", G_CALLBACK (on_randr_event), manager);
-                watch_external_monitor (manager->rr_screen);
-                on_randr_event (manager->rr_screen, manager);
-        }
+        g_signal_connect (manager->rr_screen, "changed", G_CALLBACK (on_randr_event), manager);
+        watch_external_monitor (manager->rr_screen);
+        on_randr_event (manager->rr_screen, manager);
 
         manager->screensaver_proxy = gnome_settings_bus_get_screen_saver_proxy ();
 
@@ -2869,8 +2887,6 @@ on_rr_screen_acquired (GObject      *object,
                           G_CALLBACK (engine_device_added_cb), manager);
         g_signal_connect (manager->up_client, "device-removed",
                           G_CALLBACK (engine_device_removed_cb), manager);
-        g_signal_connect_after (manager->up_client, "notify::lid-is-closed",
-                                G_CALLBACK (lid_state_changed_cb), manager);
         g_signal_connect (manager->up_client, "notify::on-battery",
                           G_CALLBACK (up_client_on_battery_cb), manager);
 
@@ -3051,11 +3067,7 @@ gsd_power_manager_start (GsdPowerManager *manager,
         g_debug ("Starting power manager");
         gnome_settings_profile_start (NULL);
 
-        /* Check whether we have a lid first */
         manager->up_client = up_client_new ();
-        manager->lid_is_present = up_client_get_lid_is_present (manager->up_client);
-        if (manager->lid_is_present)
-                manager->lid_is_closed = up_client_get_lid_is_closed (manager->up_client);
 
         /* Set up the logind proxy */
         manager->logind_proxy =
@@ -3072,6 +3084,11 @@ gsd_power_manager_start (GsdPowerManager *manager,
                 return FALSE;
         }
 
+        /* Check whether the lid is closed */
+        manager->lid_is_closed = is_lid_closed (manager);
+        g_signal_connect (manager->logind_proxy, "g-properties-changed",
+                          G_CALLBACK (logind_proxy_changed_cb), manager);
+
         /* coldplug the list of screens */
         gnome_rr_screen_new_async (gdk_screen_get_default (),
                                    on_rr_screen_acquired, manager);


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