Keyboard icon gives feedback on screen lock
[gregoa/zavai.git] / src / app_power.vala
index 1edd502bc88baf79f4d40027b8e22eb8e163d52e..f3f587b01585312271754f352d26ef58b3bcfdd4 100644 (file)
@@ -22,21 +22,27 @@ using GLib;
 
 namespace zavai {
 namespace ui {
-namespace powerbutton {
+namespace power {
 
 public class Power : zavai.Resource, Object
 {
        public dynamic DBus.Object usage;
        public bool screen_locked;
        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 bool hide_after_closing_power_menu;
 
+       public signal void screen_lock_changed(bool state);
+
        public Power()
        {
                screen_locked = false;
                screen_lock_fd = -1;
                hide_after_closing_power_menu = false;
+               last_down = 0;
 
                usage = zavai.registry.sbus.get_object(
                        "org.freesmartphone.ousaged",
@@ -83,22 +89,33 @@ public class Power : zavai.Resource, Object
                        Posix.close(screen_lock_fd);
                }
                screen_locked = locked;
+
+               screen_lock_changed(locked);
        }
 
-       private void on_power_button(Posix.timeval* time, bool pressed)
+       private void on_power_button(Posix.timeval* t, bool pressed)
        {
-               if (!pressed)
+               if (pressed)
                {
+                       last_down = t->tv_sec;
+               } else {
                        if (screen_locked)
                        {
-                               // TODO: short press: turn on backlight for a bit
-                               // TODO: long press: unlock
-                               set_screen_lock(false);
+                               time_t now = new time_t();
+                               if (now < last_down + 2)
+                               {
+                                       // Short press: turn on backlight for a bit
+                                       backlight.wiggle();
+                               } else {
+                                       // Long press: unlock
+                                       set_screen_lock(false);
+                               }
                        }
                        else
                        {
                                power_menu.toggle();
                        }
+                       last_down = 0;
                }
        }
 }
@@ -245,6 +262,14 @@ public class Backlight: zavai.Service
                        "org.freesmartphone.Usage");
        }
 
+       // Turn the backlight and then let it fade off
+       public void wiggle()
+       {
+               // There must be a better method
+               usage.RequestResource("Display");
+               usage.ReleaseResource("Display");
+       }
+
        /// Request GPS resource
        public override void start()
        {
@@ -286,11 +311,16 @@ public class PowerMenu : zavai.Resource, Gtk.Window
 
        public PowerMenu()
        {
-               type = Gtk.WindowType.POPUP;
+               type = Gtk.WindowType.TOPLEVEL;
                title = "Power Menu";
                shown = false;
+               destroy_with_parent = true;
+               set_transient_for(zavai.app);
+               set_modal(true);
+               set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
+               set_size_request(300, 500);
 
-               vbox = new Gtk.VBox(true, 0);
+               vbox = new Gtk.VBox(false, 0);
                add(vbox);
 
                //destroy += Gtk.main_quit;
@@ -315,7 +345,7 @@ public class PowerMenu : zavai.Resource, Gtk.Window
                act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
                vbox.pack_start(act_backlight_on, false, false, 0);
 
-               vbox.show_all();
+               //vbox.show_all();
        }
 
        public void toggle()
@@ -323,6 +353,9 @@ public class PowerMenu : zavai.Resource, Gtk.Window
                if (!shown)
                {
                        show_all();
+                       show();
+                       visible = true;
+                       present();
                        shown = true;
                } else {
                        // TODO: do more in case it is visible but has no visibility (is covered by others)
@@ -340,11 +373,29 @@ public class PowerMenu : zavai.Resource, Gtk.Window
        public void shutdown() {}
 }
 
+/*
+public class TogglePowerMenu : Gtk.Button
+{
+       public TogglePowerMenu()
+       {
+               label = "Toggle power menu";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Toggling power menu");
+               power_menu.toggle();
+       }
+}
+*/
 
 Power power;
 PowerMenu power_menu;
 BatteryIcon battery_icon;
 Backlight backlight;
+//TogglePowerMenu tpm;
 
 public void init()
 {
@@ -359,6 +410,8 @@ public void init()
        zavai.registry.register_resource("powermenu", power_menu);
        
     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
+       //tpm = new TogglePowerMenu();
+    //zavai.registry.getmenu("menu.main").add_widget(tpm);
 
     /*
        raise_icon = new RaiseIcon();