Shutdown, reboot and suspend via frameworkd
authorEnrico Zini <enrico@enricozini.org>
Wed, 12 Aug 2009 17:43:13 +0000 (18:43 +0100)
committerEnrico Zini <enrico@enricozini.org>
Wed, 12 Aug 2009 17:43:13 +0000 (18:43 +0100)
src/app.vala
src/app_power.vala

index 43802f3e92bbef28f7f6abaad0bc3abbeb2339e1..a6b143990cf642cc98d5386296c95b389eef282b 100644 (file)
@@ -365,6 +365,34 @@ public class ServiceRequestLink : Gtk.ToggleButton
        }
 }
 
        }
 }
 
+public class CommandButton : Gtk.Button
+{
+       private string command;
+
+       public CommandButton(string name, string command)
+       {
+               label = name;
+               this.command = command;
+               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.Zavai app;
 
 }
 zavai.Zavai app;
 
 }
index d3bcaeeb0af75621a5f78a5583b787697c7cfe95..56ee8dafd0541c91e689532e088c966330964419 100644 (file)
@@ -24,176 +24,75 @@ namespace zavai {
 namespace ui {
 namespace powerbutton {
 
 namespace ui {
 namespace powerbutton {
 
-/*
-public class RaiseIcon : Gtk.StatusIcon
+public class Power : zavai.Resource, Object
 {
 {
-       public RaiseIcon()
-       {
-               activate += on_activate;
-               zavai.app.visibility_changed += on_visibility_changed;
-               update_icon();
-       }
+       public dynamic DBus.Object usage;
+       public bool screen_locked;
+       private int screen_lock_fd;
 
 
-       private void on_visibility_changed(bool visible)
+       public Power()
        {
        {
-               update_icon();
-       }
+               screen_locked = false;
+               screen_lock_fd = -1;
 
 
-       private void on_activate()
-       {
-               zavai.app.toggle_visibility();
-               update_icon();
-       }
+               usage = zavai.registry.sbus.get_object(
+                       "org.freesmartphone.ousaged",
+                       "/org/freesmartphone/Usage",
+                       "org.freesmartphone.Usage");
 
 
-       protected void update_icon()
-       {
-               string name = zavai.config.icondir + "/";
-               if (!zavai.app.visibility)
-                       name += "zavai_on.png";
-               else
-                       name += "zavai_off.png";
-               set_from_file(name);
+               zavai.input.power_button.power_button += on_power_button;
+               zavai.input.power_button.request("zavai.ui.powerbutton.power");
        }
        }
-}
 
 
-public class CloseOrBack : Gtk.StatusIcon
-{
-       public CloseOrBack()
+       public void shutdown()
        {
        {
-               activate += on_activate;
-               zavai.app.visibility_changed += on_visibility_changed;
-               update_icon();
+               zavai.input.power_button.release("zavai.ui.powerbutton.power");
        }
 
        }
 
-       private void on_visibility_changed(bool visible)
-       {
-               update_icon();
-       }
+       public void do_suspend() { usage.Suspend(); }
+       public void do_shutdown() { usage.Shutdown(); }
+       public void do_reboot() { usage.Reboot(); }
 
 
-       private void on_activate()
+       public void set_screen_lock(bool locked)
        {
        {
-               if (zavai.app.visibility)
+               if (locked && screen_locked)
+                       return;
+               if (!locked && !screen_locked)
+                       return;
+
+               if (locked)
                {
                {
-                       // Back
-               } else {
-                       // Close current app
-                       Gdk.Window w = zavai.app.get_screen().get_active_window();
-                       if (w != zavai.app.window)
+                       screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
+                       if (screen_lock_fd < 0)
                        {
                        {
-                               w.destroy();
+                               zavai.log.error("Cannot open /dev/input/event1");
+                               return;
                        }
                        }
-               }
-       }
-
-       protected void update_icon()
-       {
-               string name = zavai.config.icondir + "/";
-               if (!zavai.app.visibility)
-                       name += "quit_on.png";
-               else
-                       name += "quit_off.png";
-               set_from_file(name);
-       }
-}
-
-public class WindowList : Applet
-{
-       Wnck.Tasklist selector;
-
-       public WindowList(string label)
-       {
-               _label = label;
-               selector = new Wnck.Tasklist(Wnck.Screen.get_default());
-               pack_start(selector, true, true, 0);
-       }
-}
-*/
-
-public class CommandButton : Gtk.Button
-{
-       private string command;
 
 
-       public CommandButton(string name, string command)
-       {
-               label = name;
-               this.command = command;
-               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);
-       }
-}
-
-private bool screen_locked;
-private int screen_lock_fd;
-
-private void set_screen_lock(bool locked)
-{
-       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");
+                       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);
                        Posix.close(screen_lock_fd);
-                       return;
                }
                }
-       } else {
-               Posix.close(screen_lock_fd);
+               screen_locked = locked;
        }
        }
-       screen_locked = locked;
-}
 
 
-public class ScreenLockButton : Gtk.Button
-{
-       public ScreenLockButton(string name)
+       private void on_power_button(Posix.timeval* time, bool pressed)
        {
        {
-               label = name;
-               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);
-       }
-}
-
-private void on_power_button(Posix.timeval* time, bool pressed)
-{
-       if (!pressed)
-       {
-               if (screen_locked)
-                       set_screen_lock(false);
-               else
+               if (!pressed)
                {
                {
-                       zavai.app.push_applet("menu.power");
-                       zavai.app.ensure_visible();
+                       if (screen_locked)
+                               set_screen_lock(false);
+                       else
+                       {
+                               zavai.app.push_applet("menu.power");
+                               zavai.app.ensure_visible();
+                       }
                }
        }
 }
                }
        }
 }
@@ -257,30 +156,90 @@ stderr.printf("Loading icon from %s\n", name);
        }
 }
 
        }
 }
 
+public class ScreenLockButton : Gtk.Button
+{
+       public ScreenLockButton()
+       {
+               label = "Lock screen";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Locking screen");
+               power.set_screen_lock(true);
+       }
+}
+
+public class SuspendButton : Gtk.Button
+{
+       public SuspendButton()
+       {
+               label = "Suspend";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Suspending the phone via FSO");
+               power.do_suspend();
+       }
+}
+
+public class ShutdownButton : Gtk.Button
+{
+       public ShutdownButton()
+       {
+               label = "Shut down";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Shutting down the phone via FSO");
+               power.do_shutdown();
+       }
+}
+
+public class RebootButton : Gtk.Button
+{
+       public RebootButton()
+       {
+               label = "Reboot";
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
+
+       public void on_clicked()
+       {
+               zavai.log.info("Rebooting the phone via FSO");
+               power.do_reboot();
+       }
+}
+
+Power power;
 BatteryIcon battery_icon;
 
 public void init()
 {
 BatteryIcon battery_icon;
 
 public void init()
 {
-       screen_locked = false;
-       screen_lock_fd = -1;
-
-       zavai.input.power_button.power_button += on_power_button;
+       power = new Power();
 
        battery_icon = new BatteryIcon();
        battery_icon.set_visible(true);
 
     // Menus
     var menu_power = new zavai.Menu("Power menu");
 
        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"));
+       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);
 
     zavai.registry.getmenu("menu.main").add_applet("menu.power");
 
     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");
-
     /*
        raise_icon = new RaiseIcon();
        raise_icon.set_visible(true);
     /*
        raise_icon = new RaiseIcon();
        raise_icon.set_visible(true);