Shorter time for long press, detect properly, no need to release to get a long press
authorEnrico Zini <enrico@enricozini.org>
Sun, 16 Aug 2009 17:56:07 +0000 (18:56 +0100)
committerEnrico Zini <enrico@enricozini.org>
Sun, 16 Aug 2009 17:56:07 +0000 (18:56 +0100)
src/app_power.vala

index cddcb4f0b100a1d55095fc66df35ca063ab8cf71..10c8fb98165fc1c8319a59dc789ffef20c8fcd4d 100644 (file)
@@ -24,6 +24,12 @@ namespace zavai {
 namespace ui {
 namespace power {
 
+// Compute a-b in microseconds
+static long timediff(Posix.timeval* a, Posix.timeval* b)
+{
+       return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec);
+}
+
 public class Power : zavai.Resource, Object
 {
        public dynamic DBus.Object usage;
@@ -31,7 +37,7 @@ public class Power : zavai.Resource, Object
        private int screen_lock_fd;
        // Timestamp of the past power button pressed even (0 if the button has
        // been released)
-       private time_t last_down;
+       private Posix.timeval last_down;
 
        private bool hide_after_closing_power_menu;
 
@@ -42,7 +48,8 @@ public class Power : zavai.Resource, Object
                screen_locked = false;
                screen_lock_fd = -1;
                hide_after_closing_power_menu = false;
-               last_down = 0;
+               last_down.tv_sec = 0;
+               last_down.tv_usec = 0;
 
                usage = zavai.registry.sbus.get_object(
                        "org.freesmartphone.ousaged",
@@ -97,31 +104,43 @@ public class Power : zavai.Resource, Object
 
        private void on_power_button(Posix.timeval* t, bool pressed)
        {
+               bool short_press = false;
+               bool long_press = false;
+
                if (pressed)
                {
-                       last_down = t->tv_sec;
-               } else {
-                       time_t now = new time_t();
-                       bool short_press = now < last_down + 2;
-                       if (screen_locked)
-                       {
-                               if (short_press)
-                                       // Short press: turn on backlight for a bit
-                                       backlight.wiggle();
-                               else
-                                       // Long press: unlock
-                                       set_screen_lock(false);
-                       }
+                       if (last_down.tv_sec == 0)
+                               last_down = *t;
                        else
                        {
-                               if (short_press)
-                                       // Short press: toggle power menu
-                                       power_menu.toggle();
-                               else
-                                       // Long press: lock screen
-                                       set_screen_lock(true);
+                               long diff = timediff(t, &last_down);
+                               long_press = diff >= 1500000;
                        }
-                       last_down = 0;
+               } else {
+                       long diff = timediff(t, &last_down);
+                       if (diff >= 1500000)
+                               long_press = true;
+                       else
+                               short_press = true;
+
+                       last_down.tv_sec = 0;
+                       last_down.tv_usec = 0;
+               }
+
+               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);
                }
        }
 }