Use a timer to detect long presses
authorEnrico Zini <enrico@enricozini.org>
Sun, 16 Aug 2009 18:15:21 +0000 (19:15 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 16 Aug 2009 18:15:21 +0000 (19:15 +0100)
src/app_power.vala

index e6d99103e5dfe059128e6090eecec2182d6b1664..b442c3ab42c9d101226a30a18421d94e40f69f03 100644 (file)
@@ -43,6 +43,10 @@ public class Power : zavai.Resource, Object
 
        public signal void screen_lock_changed(bool state);
 
+       public signal void power_short_press();
+       public signal void power_long_press();
+       private uint button_press_timeout;
+
        public Power()
        {
                screen_locked = false;
@@ -50,6 +54,7 @@ public class Power : zavai.Resource, Object
                hide_after_closing_power_menu = false;
                last_down.tv_sec = 0;
                last_down.tv_usec = 0;
+               button_press_timeout = 0;
 
                usage = zavai.registry.sbus.get_object(
                        "org.freesmartphone.ousaged",
@@ -58,6 +63,9 @@ public class Power : zavai.Resource, Object
 
                zavai.input.power_button.power_button += on_power_button;
                zavai.input.power_button.request("zavai.ui.powerbutton.power");
+
+               power_short_press += on_power_short_press;
+               power_long_press += on_power_long_press;
        }
 
        public void shutdown()
@@ -102,6 +110,15 @@ public class Power : zavai.Resource, Object
                screen_lock_changed(locked);
        }
 
+       private bool on_power_button_timeout()
+       {
+               last_down.tv_sec = 0;
+               last_down.tv_usec = 0;
+               power_long_press();
+               // Do not reschedule
+               return false;
+       }
+
        private void on_power_button(Posix.timeval* t, bool pressed)
        {
                bool short_press = false;
@@ -115,6 +132,7 @@ stderr.printf("EVENT %d\n", (int)pressed);
                        {
 stderr.printf("  FIRST PRESSED\n");
                                last_down = *t;
+                               button_press_timeout = Timeout.add(1500, on_power_button_timeout);
                        }
                        else
                        {
@@ -123,34 +141,52 @@ stderr.printf("  PRESSED FOR %ld\n", diff);
                                long_press = diff >= 1500000;
                        }
                } else {
-                       long diff = timediff(t, &last_down);
+                       if (last_down.tv_sec == 0)
+                       {
+                               // Ignore: release has been simulated with the timeout
+                       } else {
+                               if (button_press_timeout != 0)
+                               {
+                                       // Cancel the timeout
+                                       Source.remove(button_press_timeout);
+                                       button_press_timeout = 0;
+                               }
+                               long diff = timediff(t, &last_down);
 stderr.printf("  RELEASED AFTER %ld\n", diff);
-                       if (diff >= 1500000)
-                               long_press = true;
-                       else
-                               short_press = true;
+                               if (diff >= 1500000)
+                                       long_press = true;
+                               else
+                                       short_press = true;
 
-                       last_down.tv_sec = 0;
-                       last_down.tv_usec = 0;
+                               last_down.tv_sec = 0;
+                               last_down.tv_usec = 0;
+                       }
                }
 
 stderr.printf("  LP %d SP %d\n", (int)long_press, (int)short_press);
 
-               if (short_press) {
-                       if (screen_locked)
-                               // Short press: turn on backlight for a bit
-                               backlight.wiggle();
-                       else
-                               // Short press: toggle power menu
-                               power_menu.toggle();
-               } else if (long_press) {
-                       if (screen_locked)
-                               // Long press: unlock
-                               set_screen_lock(false);
-                       else
-                               // Long press: lock screen
-                               set_screen_lock(true);
-               }
+               if (long_press) power_long_press();
+               if (short_press) power_short_press();
+       }
+
+       private void on_power_short_press()
+       {
+               if (screen_locked)
+                       // Short press: turn on backlight for a bit
+                       backlight.wiggle();
+               else
+                       // Short press: toggle power menu
+                       power_menu.toggle();
+       }
+
+       private void on_power_long_press()
+       {
+               if (screen_locked)
+                       // Long press: unlock
+                       set_screen_lock(false);
+               else
+                       // Long press: lock screen
+                       set_screen_lock(true);
        }
 }