Keyboard icon gives feedback on screen lock
[gregoa/zavai.git] / src / app_power.vala
index cd049f0c4d35860d9d825225d255efd186beffe8..f3f587b01585312271754f352d26ef58b3bcfdd4 100644 (file)
@@ -22,18 +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",
@@ -80,19 +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)
-                               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
                        {
-                               zavai.app.push_applet("menu.power");
-                               zavai.app.ensure_visible();
+                               power_menu.toggle();
                        }
+                       last_down = 0;
                }
        }
 }
@@ -169,6 +192,7 @@ public class ScreenLockButton : Gtk.Button
        {
                zavai.log.info("Locking screen");
                power.set_screen_lock(true);
+               power_menu.hide();
        }
 }
 
@@ -185,6 +209,7 @@ public class SuspendButton : Gtk.Button
        {
                zavai.log.info("Suspending the phone via FSO");
                power.do_suspend();
+               power_menu.hide();
        }
 }
 
@@ -201,6 +226,7 @@ public class ShutdownButton : Gtk.Button
        {
                zavai.log.info("Shutting down the phone via FSO");
                power.do_shutdown();
+               power_menu.hide();
        }
 }
 
@@ -217,28 +243,175 @@ public class RebootButton : Gtk.Button
        {
                zavai.log.info("Rebooting the phone via FSO");
                power.do_reboot();
+               power_menu.hide();
+       }
+}
+
+// For a list of dbus services, look in /etc/dbus-1/system.d/
+public class Backlight: zavai.Service
+{
+       public dynamic DBus.Object usage;
+
+       public Backlight()
+       {
+               name = "backlight";
+
+               usage = zavai.registry.sbus.get_object(
+                       "org.freesmartphone.ousaged",
+                       "/org/freesmartphone/Usage",
+                       "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()
+       {
+               if (started) return;
+               try {
+                       usage.RequestResource("Display");
+                       zavai.log.info("Acquired display");
+                       base.start();
+               } catch (GLib.Error e) {
+                       zavai.log.error(e.message);
+               }
+               base.start();
+       }
+
+       // Release usage of GPS
+       public override void stop()
+       {
+               if (!started) return;
+               try {
+                       usage.ReleaseResource("Display");
+                       zavai.log.info("Released display");
+                       base.stop();
+               } catch (GLib.Error e) {
+                       zavai.log.error(e.message);
+               }
+               base.stop();
+       }
+}
+
+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()
 {
        power = new Power();
+       backlight = new Backlight();
+       zavai.registry.register_service(backlight);
 
        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());
-    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();