From 0e138361813294bbacd8e6bf6bd621303fad40c6 Mon Sep 17 00:00:00 2001
From: MagneFire <IDaNLContact@gmail.com>
Date: Sun, 7 Mar 2021 23:03:41 +0100
Subject: [PATCH] Ambient Mode: Wait for compositor when sending enable/disable
 request. Low power mode wouldn't activate properly after boot sometimes, this
 is likely due to the compositor not being available at the time. So wait
 until we know that the compositor is available (ready to receive DBus
 messages).

---
 modules/display.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/modules/display.c b/modules/display.c
index 42ca0e5..85d0df3 100644
--- a/modules/display.c
+++ b/modules/display.c
@@ -7128,6 +7128,9 @@ compositor_stm_enter_state(compositor_stm_t *self)
         compositor_stm_cancel_killer(self);
         compositor_stm_cancel_panic(self);
 
+        /* Compositor is ready to receive D-Bus messages, send request to enable/disable lpm. */
+        compositor_stm_send_lpm_request(self);
+
         /* Wake display state machine */
         mdy_stm_schedule_rethink();
         break;
@@ -9915,11 +9918,13 @@ EXIT:
     mce_log(LL_DEVEL, "The compositor %s support for ambient mode.",
             low_power_mode_supported ? "has" : "hasn't");
 
-    // We have a variable that we should use to set lpm mode availability based on ack.
-    mdy_low_power_mode_supported = low_power_mode_supported;
+    if (!mdy_low_power_mode_supported != !low_power_mode_supported) {
+        // We have a variable that we should use to set lpm mode availability based on ack.
+        mdy_low_power_mode_supported = low_power_mode_supported;
 
-    // Enable/disable lpm mode based on support from the compositor.
-    compositor_stm_send_lpm_request(mdy_compositor_ipc);
+        // Enable/disable lpm mode based on support from the compositor.
+        compositor_stm_send_lpm_request(mdy_compositor_ipc);
+    }
 
     return status;
 }
@@ -11410,8 +11415,7 @@ static void mdy_setting_init(void)
                            MCE_DEFAULT_USE_LOW_POWER_MODE,
                            mdy_setting_cb,
                            &mdy_use_low_power_mode_setting_id);
-    
-    compositor_stm_send_lpm_request(mdy_compositor_ipc);
+
     /* Blanking inhibit modes */
     mce_setting_track_int(MCE_SETTING_BLANKING_INHIBIT_MODE,
                           &mdy_blanking_inhibit_mode,
