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;
33 private bool hide_after_closing_power_menu;
37 screen_locked = false;
39 hide_after_closing_power_menu = false;
41 usage = zavai.registry.sbus.get_object(
42 "org.freesmartphone.ousaged",
43 "/org/freesmartphone/Usage",
44 "org.freesmartphone.Usage");
46 zavai.input.power_button.power_button += on_power_button;
47 zavai.input.power_button.request("zavai.ui.powerbutton.power");
50 public void shutdown()
52 zavai.input.power_button.release("zavai.ui.powerbutton.power");
55 public void do_suspend() { usage.Suspend(); }
56 public void do_shutdown() { usage.Shutdown(); }
57 public void do_reboot() { usage.Reboot(); }
59 public void set_screen_lock(bool locked)
61 if (locked && screen_locked)
63 if (!locked && !screen_locked)
68 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
69 if (screen_lock_fd < 0)
71 zavai.log.error("Cannot open /dev/input/event1");
75 int EVIOCGRAB = 0x40044590;
76 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
78 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
79 Posix.close(screen_lock_fd);
83 Posix.close(screen_lock_fd);
85 screen_locked = locked;
88 private void on_power_button(Posix.timeval* time, bool pressed)
94 // TODO: short press: turn on backlight for a bit
95 // TODO: long press: unlock
96 set_screen_lock(false);
106 public class BatteryIcon : Gtk.StatusIcon
108 public dynamic DBus.Object battery;
109 protected string last_status;
110 protected int last_capacity;
114 battery = zavai.registry.sbus.get_object(
115 "org.freesmartphone.odeviced",
116 "/org/freesmartphone/Device/PowerSupply/battery",
117 "org.freesmartphone.Device.PowerSupply");
119 // activate += on_activate;
121 battery.PowerStatus += on_power_status;
122 battery.Capacity += on_capacity;
124 last_status = battery.GetPowerStatus();
125 last_capacity = battery.GetCapacity();
130 private void on_power_status(dynamic DBus.Object bat, string status)
132 zavai.log.info("New battery status: " + status);
133 last_status = status;
137 private void on_capacity(dynamic DBus.Object bat, int val)
139 stderr.printf("NEW CAPACITY: %d\n", val);
145 private void on_activate()
150 protected void update_icon()
152 string name = zavai.config.icondir + "/battery/";
154 if (last_status == "charging")
155 name += "%02d0_charging_500.png".printf(last_capacity/10);
157 name += "%02d0.png".printf(last_capacity/10);
159 stderr.printf("Loading icon from %s\n", name);
165 public class ScreenLockButton : Gtk.Button
167 public ScreenLockButton()
169 label = "Lock screen";
170 clicked += on_clicked;
171 set_size_request(0, zavai.config.min_button_height);
174 public void on_clicked()
176 zavai.log.info("Locking screen");
177 power.set_screen_lock(true);
181 public class SuspendButton : Gtk.Button
183 public SuspendButton()
186 clicked += on_clicked;
187 set_size_request(0, zavai.config.min_button_height);
190 public void on_clicked()
192 zavai.log.info("Suspending the phone via FSO");
197 public class ShutdownButton : Gtk.Button
199 public ShutdownButton()
202 clicked += on_clicked;
203 set_size_request(0, zavai.config.min_button_height);
206 public void on_clicked()
208 zavai.log.info("Shutting down the phone via FSO");
213 public class RebootButton : Gtk.Button
215 public RebootButton()
218 clicked += on_clicked;
219 set_size_request(0, zavai.config.min_button_height);
222 public void on_clicked()
224 zavai.log.info("Rebooting the phone via FSO");
229 // For a list of dbus services, look in /etc/dbus-1/system.d/
230 public class Backlight: zavai.Service
232 public dynamic DBus.Object usage;
238 usage = zavai.registry.sbus.get_object(
239 "org.freesmartphone.ousaged",
240 "/org/freesmartphone/Usage",
241 "org.freesmartphone.Usage");
244 /// Request GPS resource
245 public override void start()
249 usage.RequestResource("Display");
250 zavai.log.info("Acquired display");
252 } catch (GLib.Error e) {
253 zavai.log.error(e.message);
258 // Release usage of GPS
259 public override void stop()
261 if (!started) return;
263 usage.ReleaseResource("Display");
264 zavai.log.info("Released display");
266 } catch (GLib.Error e) {
267 zavai.log.error(e.message);
273 public class PowerMenu : zavai.Resource, Gtk.Window
275 protected Gtk.VBox vbox;
276 protected ScreenLockButton act_screen_lock;
277 protected SuspendButton act_suspend;
278 protected ShutdownButton act_shutdown;
279 protected RebootButton act_reboot;
280 protected ServiceRequestLink act_backlight_on;
284 type = Gtk.WindowType.POPUP;
285 title = "Power Menu";
287 vbox = new Gtk.VBox(true, 0);
290 //destroy += Gtk.main_quit;
291 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
292 //visibility_notify_event += on_visibility;
293 set_skip_pager_hint(true);
294 set_skip_taskbar_hint(true);
295 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
297 act_screen_lock = new ScreenLockButton();
298 vbox.pack_start(act_screen_lock, false, false, 0);
300 act_suspend = new SuspendButton();
301 vbox.pack_start(act_suspend, false, false, 0);
303 act_shutdown = new ShutdownButton();
304 vbox.pack_start(act_shutdown, false, false, 0);
306 act_reboot = new RebootButton();
307 vbox.pack_start(act_reboot, false, false, 0);
309 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
310 vbox.pack_start(act_backlight_on, false, false, 0);
315 // TODO: do more in case it is visible but has no visibility (is covered by others)
321 public void shutdown() {}
326 PowerMenu power_menu;
327 BatteryIcon battery_icon;
333 backlight = new Backlight();
334 zavai.registry.register_service(backlight);
336 battery_icon = new BatteryIcon();
337 battery_icon.set_visible(true);
339 power_menu = new PowerMenu();
340 zavai.registry.register_resource("powermenu", power_menu);
342 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
345 raise_icon = new RaiseIcon();
346 raise_icon.set_visible(true);
348 close_or_back = new CloseOrBack();
349 close_or_back.set_visible(true);
351 window_list = new WindowList("Current apps");
352 zavai.registry.register_applet("wm.list", window_list);
353 zavai.registry.getmenu("menu.main").add_applet("wm.list");
356 launcher = new Launcher("Run program");
358 zavai.log.error("Not running launcher: " + e.message);
362 if (launcher != null)
364 zavai.registry.register_applet("wm.launcher", launcher);
365 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");