From 1ee2bd9009608370d02ab35510109e88efcf003d Mon Sep 17 00:00:00 2001
From: MagneFire <IDaNLContact@gmail.com>
Date: Sun, 7 Mar 2021 23:07:09 +0100
Subject: [PATCH] Ambient Mode: Exit ambient mode when touch is detected.

---
 modules/display.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/modules/display.c b/modules/display.c
index 85d0df3..3c50973 100644
--- a/modules/display.c
+++ b/modules/display.c
@@ -413,6 +413,7 @@ static bool                mdy_shutdown_in_progress(void);
  * ------------------------------------------------------------------------- */
 
 static void                mdy_datapipe_ambient_light_level_cb(gconstpointer data);;
+static void                mdy_datapipe_touch_detected_cb(gconstpointer data);
 static void                mdy_datapipe_packagekit_locked_cb(gconstpointer data);;
 static void                mdy_datapipe_system_state_cb(gconstpointer data);
 static void                mdy_datapipe_submode_cb(gconstpointer data);
@@ -1767,6 +1768,28 @@ EXIT:
     return;
 }
 
+/**
+ * Handle touchscreen detections.
+ *
+ * @param data The touch pressed/unpressed in a pointer
+ */
+static void mdy_datapipe_touch_detected_cb(gconstpointer data)
+{
+    gboolean touch_detected = GPOINTER_TO_INT(data);
+
+    /* Log by default as it might help analyzing lpm problems */
+    mce_log(LL_DEBUG, "touch_detected = %d", touch_detected);
+
+    switch( display_state_curr ) {
+    case MCE_DISPLAY_LPM_ON:
+        /* Screen is in LPM mode, exit LPM mode when touch is detected. */
+        mce_datapipe_request_display_state(MCE_DISPLAY_ON);
+        break;
+    default:
+        break;
+    }
+}
+
 /** Keypad slide input state; assume closed */
 static cover_state_t keyboard_slide_input_state = COVER_CLOSED;
 
@@ -2276,6 +2299,10 @@ static datapipe_handler_t mdy_datapipe_handlers[] =
         .datapipe  = &light_sensor_filtered_pipe,
         .output_cb = mdy_datapipe_ambient_light_level_cb,
     },
+    {
+        .datapipe  = &touch_detected_pipe,
+        .output_cb = mdy_datapipe_touch_detected_cb,
+    },
     {
         .datapipe  = &packagekit_locked_pipe,
         .output_cb = mdy_datapipe_packagekit_locked_cb,
