Keyboard icon gives feedback on screen lock
[gregoa/zavai.git] / src / app_power.vala
index 04d71116027b5932695aee395bc9a5603930b881..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,35 +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 sec
-                               // TODO: long press: unlock
-                               set_screen_lock(false);
-                       }
-                       else
-                       {
-                               if (zavai.app.current_name == "menu.power" && zavai.app.visibility)
+                               time_t now = new time_t();
+                               if (now < last_down + 2)
                                {
-                                       // Hide or back
-                                       zavai.app.back();
-                                       if (hide_after_closing_power_menu)
-                                               zavai.app.hide();
-                               } else if (zavai.app.current_name == "menu.power") {
-                                       // Unhide
-                                       zavai.app.ensure_visible();
+                                       // Short press: turn on backlight for a bit
+                                       backlight.wiggle();
                                } else {
-                                       zavai.app.push_applet("menu.power");
-                                       hide_after_closing_power_menu = !zavai.app.visibility;
-                                       zavai.app.ensure_visible();
+                                       // Long press: unlock
+                                       set_screen_lock(false);
                                }
                        }
+                       else
+                       {
+                               power_menu.toggle();
+                       }
+                       last_down = 0;
                }
        }
 }
@@ -188,6 +192,7 @@ public class ScreenLockButton : Gtk.Button
        {
                zavai.log.info("Locking screen");
                power.set_screen_lock(true);
+               power_menu.hide();
        }
 }
 
@@ -204,6 +209,7 @@ public class SuspendButton : Gtk.Button
        {
                zavai.log.info("Suspending the phone via FSO");
                power.do_suspend();
+               power_menu.hide();
        }
 }
 
@@ -220,6 +226,7 @@ public class ShutdownButton : Gtk.Button
        {
                zavai.log.info("Shutting down the phone via FSO");
                power.do_shutdown();
+               power_menu.hide();
        }
 }
 
@@ -236,6 +243,7 @@ public class RebootButton : Gtk.Button
        {
                zavai.log.info("Rebooting the phone via FSO");
                power.do_reboot();
+               power_menu.hide();
        }
 }
 
@@ -254,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()
        {
@@ -283,10 +299,103 @@ public class Backlight: zavai.Service
        }
 }
 
+public class PowerMenu : zavai.Resource, Gtk.Window
+{
+       protected Gtk.VBox vbox;
+       protected ScreenLockButton act_screen_lock;
+       protected SuspendButton act_suspend;
+       protected ShutdownButton act_shutdown;
+       protected RebootButton act_reboot;
+       protected ServiceRequestLink act_backlight_on;
+       protected bool shown;
+
+       public PowerMenu()
+       {
+               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(false, 0);
+               add(vbox);
+
+               //destroy += Gtk.main_quit;
+               //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
+               //visibility_notify_event += on_visibility;
+               set_skip_pager_hint(true);
+               set_skip_taskbar_hint(true);
+               set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
+
+               act_screen_lock = new ScreenLockButton();
+               vbox.pack_start(act_screen_lock, false, false, 0);
+
+               act_suspend = new SuspendButton();
+               vbox.pack_start(act_suspend, false, false, 0);
+
+               act_shutdown = new ShutdownButton();
+               vbox.pack_start(act_shutdown, false, false, 0);
+
+               act_reboot = new RebootButton();
+               vbox.pack_start(act_reboot, false, false, 0);
+
+               act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
+               vbox.pack_start(act_backlight_on, false, false, 0);
+
+               //vbox.show_all();
+       }
+
+       public void toggle()
+       {
+               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)
+                       visible = !visible;
+                       if (visible)
+                               present();
+               }                               
+       }
+
+       public void hide()
+       {
+               visible = false;
+       }
+
+       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()
 {
@@ -297,17 +406,12 @@ public void init()
        battery_icon = new BatteryIcon();
        battery_icon.set_visible(true);
 
-    // Menus
-    var menu_power = new zavai.Menu("Power menu");
-       menu_power.add_widget(new ScreenLockButton());
-       menu_power.add_widget(new SuspendButton());
-       menu_power.add_widget(new ShutdownButton());
-       menu_power.add_widget(new RebootButton());
-    menu_power.add_service_toggle("backlight", "Keep backlight on", "Let backlight fade");
-    zavai.registry.register_menu("menu.power", menu_power);
-
-
+       power_menu = new PowerMenu();
+       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();