Keyboard icon gives feedback on screen lock
[gregoa/zavai.git] / src / app_power.vala
index d3bcaeeb0af75621a5f78a5583b787697c7cfe95..f3f587b01585312271754f352d26ef58b3bcfdd4 100644 (file)
@@ -22,264 +22,396 @@ using GLib;
 
 namespace zavai {
 namespace ui {
-namespace powerbutton {
+namespace power {
 
-/*
-public class RaiseIcon : Gtk.StatusIcon
+public class Power : zavai.Resource, Object
 {
-       public RaiseIcon()
+       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()
        {
-               activate += on_activate;
-               zavai.app.visibility_changed += on_visibility_changed;
-               update_icon();
+               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",
+                       "/org/freesmartphone/Usage",
+                       "org.freesmartphone.Usage");
+
+               zavai.input.power_button.power_button += on_power_button;
+               zavai.input.power_button.request("zavai.ui.powerbutton.power");
        }
 
-       private void on_visibility_changed(bool visible)
+       public void shutdown()
        {
-               update_icon();
+               zavai.input.power_button.release("zavai.ui.powerbutton.power");
        }
 
-       private void on_activate()
+       public void do_suspend() { usage.Suspend(); }
+       public void do_shutdown() { usage.Shutdown(); }
+       public void do_reboot() { usage.Reboot(); }
+
+       public void set_screen_lock(bool locked)
        {
-               zavai.app.toggle_visibility();
-               update_icon();
+               if (locked && screen_locked)
+                       return;
+               if (!locked && !screen_locked)
+                       return;
+
+               if (locked)
+               {
+                       screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
+                       if (screen_lock_fd < 0)
+                       {
+                               zavai.log.error("Cannot open /dev/input/event1");
+                               return;
+                       }
+
+                       int EVIOCGRAB = 0x40044590;
+                       if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
+                       {
+                               zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
+                               Posix.close(screen_lock_fd);
+                               return;
+                       }
+               } else {
+                       Posix.close(screen_lock_fd);
+               }
+               screen_locked = locked;
+
+               screen_lock_changed(locked);
        }
 
-       protected void update_icon()
+       private void on_power_button(Posix.timeval* t, bool pressed)
        {
-               string name = zavai.config.icondir + "/";
-               if (!zavai.app.visibility)
-                       name += "zavai_on.png";
-               else
-                       name += "zavai_off.png";
-               set_from_file(name);
+               if (pressed)
+               {
+                       last_down = t->tv_sec;
+               } else {
+                       if (screen_locked)
+                       {
+                               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;
+               }
        }
 }
 
-public class CloseOrBack : Gtk.StatusIcon
+public class BatteryIcon : Gtk.StatusIcon
 {
-       public CloseOrBack()
+       public dynamic DBus.Object battery;
+       protected string last_status;
+       protected int last_capacity;
+
+       public BatteryIcon()
+       {
+               battery = zavai.registry.sbus.get_object(
+                       "org.freesmartphone.odeviced",
+                       "/org/freesmartphone/Device/PowerSupply/battery",
+                       "org.freesmartphone.Device.PowerSupply");
+
+               // activate += on_activate;
+
+               battery.PowerStatus += on_power_status;
+               battery.Capacity += on_capacity;
+
+               last_status = battery.GetPowerStatus();
+               last_capacity = battery.GetCapacity();
+               
+               update_icon();
+       }
+
+       private void on_power_status(dynamic DBus.Object bat, string status)
        {
-               activate += on_activate;
-               zavai.app.visibility_changed += on_visibility_changed;
+               zavai.log.info("New battery status: " + status);
+               last_status = status;
                update_icon();
        }
 
-       private void on_visibility_changed(bool visible)
+       private void on_capacity(dynamic DBus.Object bat, int val)
        {
+stderr.printf("NEW CAPACITY: %d\n", val);
+               last_capacity = val;
                update_icon();
        }
 
+       /*
        private void on_activate()
        {
-               if (zavai.app.visibility)
-               {
-                       // Back
-               } else {
-                       // Close current app
-                       Gdk.Window w = zavai.app.get_screen().get_active_window();
-                       if (w != zavai.app.window)
-                       {
-                               w.destroy();
-                       }
-               }
        }
+       */
 
        protected void update_icon()
        {
-               string name = zavai.config.icondir + "/";
-               if (!zavai.app.visibility)
-                       name += "quit_on.png";
+               string name = zavai.config.icondir + "/battery/";
+
+               if (last_status == "charging")
+                       name += "%02d0_charging_500.png".printf(last_capacity/10);
                else
-                       name += "quit_off.png";
+                       name += "%02d0.png".printf(last_capacity/10);
+
+stderr.printf("Loading icon from %s\n", name);
+
                set_from_file(name);
        }
 }
 
-public class WindowList : Applet
+public class ScreenLockButton : Gtk.Button
 {
-       Wnck.Tasklist selector;
+       public ScreenLockButton()
+       {
+               label = "Lock screen";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
 
-       public WindowList(string label)
+       public void on_clicked()
        {
-               _label = label;
-               selector = new Wnck.Tasklist(Wnck.Screen.get_default());
-               pack_start(selector, true, true, 0);
+               zavai.log.info("Locking screen");
+               power.set_screen_lock(true);
+               power_menu.hide();
        }
 }
-*/
 
-public class CommandButton : Gtk.Button
+public class SuspendButton : Gtk.Button
 {
-       private string command;
-
-       public CommandButton(string name, string command)
+       public SuspendButton()
        {
-               label = name;
-               this.command = command;
+               label = "Suspend";
                clicked += on_clicked;
                set_size_request(0, zavai.config.min_button_height);
        }
 
        public void on_clicked()
        {
-               zavai.log.info("Run program: " + command);
-               string[] args = command.split(" ");
-               Pid pid;
-               Process.spawn_async(
-                       Environment.get_home_dir(),
-                       args,
-                       null,
-                       SpawnFlags.SEARCH_PATH,
-                       null,
-                       out pid);
+               zavai.log.info("Suspending the phone via FSO");
+               power.do_suspend();
+               power_menu.hide();
        }
 }
 
-private bool screen_locked;
-private int screen_lock_fd;
-
-private void set_screen_lock(bool locked)
+public class ShutdownButton : Gtk.Button
 {
-       if (locked && screen_locked)
-               return;
-       if (!locked && !screen_locked)
-               return;
-
-       if (locked)
+       public ShutdownButton()
        {
-               screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
-               if (screen_lock_fd < 0)
-               {
-                       zavai.log.error("Cannot open /dev/input/event1");
-                       return;
-               }
+               label = "Shut down";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
 
-               int EVIOCGRAB = 0x40044590;
-               if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
-               {
-                       zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
-                       Posix.close(screen_lock_fd);
-                       return;
-               }
-       } else {
-               Posix.close(screen_lock_fd);
+       public void on_clicked()
+       {
+               zavai.log.info("Shutting down the phone via FSO");
+               power.do_shutdown();
+               power_menu.hide();
        }
-       screen_locked = locked;
 }
 
-public class ScreenLockButton : Gtk.Button
+public class RebootButton : Gtk.Button
 {
-       public ScreenLockButton(string name)
+       public RebootButton()
        {
-               label = name;
+               label = "Reboot";
                clicked += on_clicked;
                set_size_request(0, zavai.config.min_button_height);
        }
 
        public void on_clicked()
        {
-               zavai.log.info("Locking screen");
-               set_screen_lock(true);
+               zavai.log.info("Rebooting the phone via FSO");
+               power.do_reboot();
+               power_menu.hide();
        }
 }
 
-private void on_power_button(Posix.timeval* time, bool pressed)
+// For a list of dbus services, look in /etc/dbus-1/system.d/
+public class Backlight: zavai.Service
 {
-       if (!pressed)
+       public dynamic DBus.Object usage;
+
+       public Backlight()
        {
-               if (screen_locked)
-                       set_screen_lock(false);
-               else
-               {
-                       zavai.app.push_applet("menu.power");
-                       zavai.app.ensure_visible();
+               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 BatteryIcon : Gtk.StatusIcon
+public class PowerMenu : zavai.Resource, Gtk.Window
 {
-       public dynamic DBus.Object battery;
-       protected string last_status;
-       protected int last_capacity;
-
-       public BatteryIcon()
+       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()
        {
-               battery = zavai.registry.sbus.get_object(
-                       "org.freesmartphone.odeviced",
-                       "/org/freesmartphone/Device/PowerSupply/battery",
-                       "org.freesmartphone.Device.PowerSupply");
+               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);
 
-               // activate += on_activate;
+               vbox = new Gtk.VBox(false, 0);
+               add(vbox);
 
-               battery.PowerStatus += on_power_status;
-               battery.Capacity += on_capacity;
+               //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);
 
-               last_status = battery.GetPowerStatus();
-               last_capacity = battery.GetCapacity();
-               
-               update_icon();
+               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();
        }
 
-       private void on_power_status(dynamic DBus.Object bat, string status)
+       public void toggle()
        {
-               zavai.log.info("New battery status: " + status);
-               last_status = status;
-               update_icon();
+               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();
+               }                               
        }
 
-       private void on_capacity(dynamic DBus.Object bat, int val)
+       public void hide()
        {
-stderr.printf("NEW CAPACITY: %d\n", val);
-               last_capacity = val;
-               update_icon();
+               visible = false;
        }
 
-       /*
-       private void on_activate()
+       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);
        }
-       */
 
-       protected void update_icon()
+       public void on_clicked()
        {
-               string name = zavai.config.icondir + "/battery/";
-
-               if (last_status == "charging")
-                       name += "%02d0_charging_500.png".printf(last_capacity/10);
-               else
-                       name += "%02d0.png".printf(last_capacity/10);
-
-stderr.printf("Loading icon from %s\n", name);
-
-               set_from_file(name);
+               zavai.log.info("Toggling power menu");
+               power_menu.toggle();
        }
 }
+*/
 
+Power power;
+PowerMenu power_menu;
 BatteryIcon battery_icon;
+Backlight backlight;
+//TogglePowerMenu tpm;
 
 public void init()
 {
-       screen_locked = false;
-       screen_lock_fd = -1;
-
-       zavai.input.power_button.power_button += on_power_button;
+       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("Lock screen"));
-       menu_power.add_widget(new CommandButton("Suspend", "apm -s"));
-       menu_power.add_widget(new CommandButton("Shutdown", "shutdown -h now"));
-       menu_power.add_widget(new CommandButton("Reboot", "shutdown -r now"));
-    zavai.registry.register_menu("menu.power", menu_power);
-
-    zavai.registry.getmenu("menu.main").add_applet("menu.power");
-
-    zavai.registry.gets("input.power_button").request("powerbutton");
+       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();