2 * app_power - zavai power handling
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 namespace powerbutton {
27 public class Power : zavai.Resource, Object
29 public dynamic DBus.Object usage;
30 public bool screen_locked;
31 private int screen_lock_fd;
35 screen_locked = false;
38 usage = zavai.registry.sbus.get_object(
39 "org.freesmartphone.ousaged",
40 "/org/freesmartphone/Usage",
41 "org.freesmartphone.Usage");
43 zavai.input.power_button.power_button += on_power_button;
44 zavai.input.power_button.request("zavai.ui.powerbutton.power");
47 public void shutdown()
49 zavai.input.power_button.release("zavai.ui.powerbutton.power");
52 public void do_suspend() { usage.Suspend(); }
53 public void do_shutdown() { usage.Shutdown(); }
54 public void do_reboot() { usage.Reboot(); }
56 public void set_screen_lock(bool locked)
58 if (locked && screen_locked)
60 if (!locked && !screen_locked)
65 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
66 if (screen_lock_fd < 0)
68 zavai.log.error("Cannot open /dev/input/event1");
72 int EVIOCGRAB = 0x40044590;
73 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
75 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
76 Posix.close(screen_lock_fd);
80 Posix.close(screen_lock_fd);
82 screen_locked = locked;
85 private void on_power_button(Posix.timeval* time, bool pressed)
90 set_screen_lock(false);
93 zavai.app.push_applet("menu.power");
94 zavai.app.ensure_visible();
100 public class BatteryIcon : Gtk.StatusIcon
102 public dynamic DBus.Object battery;
103 protected string last_status;
104 protected int last_capacity;
108 battery = zavai.registry.sbus.get_object(
109 "org.freesmartphone.odeviced",
110 "/org/freesmartphone/Device/PowerSupply/battery",
111 "org.freesmartphone.Device.PowerSupply");
113 // activate += on_activate;
115 battery.PowerStatus += on_power_status;
116 battery.Capacity += on_capacity;
118 last_status = battery.GetPowerStatus();
119 last_capacity = battery.GetCapacity();
124 private void on_power_status(dynamic DBus.Object bat, string status)
126 zavai.log.info("New battery status: " + status);
127 last_status = status;
131 private void on_capacity(dynamic DBus.Object bat, int val)
133 stderr.printf("NEW CAPACITY: %d\n", val);
139 private void on_activate()
144 protected void update_icon()
146 string name = zavai.config.icondir + "/battery/";
148 if (last_status == "charging")
149 name += "%02d0_charging_500.png".printf(last_capacity/10);
151 name += "%02d0.png".printf(last_capacity/10);
153 stderr.printf("Loading icon from %s\n", name);
159 public class ScreenLockButton : Gtk.Button
161 public ScreenLockButton()
163 label = "Lock screen";
164 clicked += on_clicked;
165 set_size_request(0, zavai.config.min_button_height);
168 public void on_clicked()
170 zavai.log.info("Locking screen");
171 power.set_screen_lock(true);
175 public class SuspendButton : Gtk.Button
177 public SuspendButton()
180 clicked += on_clicked;
181 set_size_request(0, zavai.config.min_button_height);
184 public void on_clicked()
186 zavai.log.info("Suspending the phone via FSO");
191 public class ShutdownButton : Gtk.Button
193 public ShutdownButton()
196 clicked += on_clicked;
197 set_size_request(0, zavai.config.min_button_height);
200 public void on_clicked()
202 zavai.log.info("Shutting down the phone via FSO");
207 public class RebootButton : Gtk.Button
209 public RebootButton()
212 clicked += on_clicked;
213 set_size_request(0, zavai.config.min_button_height);
216 public void on_clicked()
218 zavai.log.info("Rebooting the phone via FSO");
224 BatteryIcon battery_icon;
230 battery_icon = new BatteryIcon();
231 battery_icon.set_visible(true);
234 var menu_power = new zavai.Menu("Power menu");
235 menu_power.add_widget(new ScreenLockButton());
236 menu_power.add_widget(new SuspendButton());
237 menu_power.add_widget(new ShutdownButton());
238 menu_power.add_widget(new RebootButton());
239 zavai.registry.register_menu("menu.power", menu_power);
241 zavai.registry.getmenu("menu.main").add_applet("menu.power");
244 raise_icon = new RaiseIcon();
245 raise_icon.set_visible(true);
247 close_or_back = new CloseOrBack();
248 close_or_back.set_visible(true);
250 window_list = new WindowList("Current apps");
251 zavai.registry.register_applet("wm.list", window_list);
252 zavai.registry.getmenu("menu.main").add_applet("wm.list");
255 launcher = new Launcher("Run program");
257 zavai.log.error("Not running launcher: " + e.message);
261 if (launcher != null)
263 zavai.registry.register_applet("wm.launcher", launcher);
264 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");