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);
182 public class SuspendButton : Gtk.Button
184 public SuspendButton()
187 clicked += on_clicked;
188 set_size_request(0, zavai.config.min_button_height);
191 public void on_clicked()
193 zavai.log.info("Suspending the phone via FSO");
199 public class ShutdownButton : Gtk.Button
201 public ShutdownButton()
204 clicked += on_clicked;
205 set_size_request(0, zavai.config.min_button_height);
208 public void on_clicked()
210 zavai.log.info("Shutting down the phone via FSO");
216 public class RebootButton : Gtk.Button
218 public RebootButton()
221 clicked += on_clicked;
222 set_size_request(0, zavai.config.min_button_height);
225 public void on_clicked()
227 zavai.log.info("Rebooting the phone via FSO");
233 // For a list of dbus services, look in /etc/dbus-1/system.d/
234 public class Backlight: zavai.Service
236 public dynamic DBus.Object usage;
242 usage = zavai.registry.sbus.get_object(
243 "org.freesmartphone.ousaged",
244 "/org/freesmartphone/Usage",
245 "org.freesmartphone.Usage");
248 /// Request GPS resource
249 public override void start()
253 usage.RequestResource("Display");
254 zavai.log.info("Acquired display");
256 } catch (GLib.Error e) {
257 zavai.log.error(e.message);
262 // Release usage of GPS
263 public override void stop()
265 if (!started) return;
267 usage.ReleaseResource("Display");
268 zavai.log.info("Released display");
270 } catch (GLib.Error e) {
271 zavai.log.error(e.message);
277 public class PowerMenu : zavai.Resource, Gtk.Window
279 protected Gtk.VBox vbox;
280 protected ScreenLockButton act_screen_lock;
281 protected SuspendButton act_suspend;
282 protected ShutdownButton act_shutdown;
283 protected RebootButton act_reboot;
284 protected ServiceRequestLink act_backlight_on;
285 protected bool shown;
289 type = Gtk.WindowType.POPUP;
290 title = "Power Menu";
293 vbox = new Gtk.VBox(true, 0);
296 //destroy += Gtk.main_quit;
297 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
298 //visibility_notify_event += on_visibility;
299 set_skip_pager_hint(true);
300 set_skip_taskbar_hint(true);
301 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
303 act_screen_lock = new ScreenLockButton();
304 vbox.pack_start(act_screen_lock, false, false, 0);
306 act_suspend = new SuspendButton();
307 vbox.pack_start(act_suspend, false, false, 0);
309 act_shutdown = new ShutdownButton();
310 vbox.pack_start(act_shutdown, false, false, 0);
312 act_reboot = new RebootButton();
313 vbox.pack_start(act_reboot, false, false, 0);
315 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
316 vbox.pack_start(act_backlight_on, false, false, 0);
328 // TODO: do more in case it is visible but has no visibility (is covered by others)
340 public void shutdown() {}
345 PowerMenu power_menu;
346 BatteryIcon battery_icon;
352 backlight = new Backlight();
353 zavai.registry.register_service(backlight);
355 battery_icon = new BatteryIcon();
356 battery_icon.set_visible(true);
358 power_menu = new PowerMenu();
359 zavai.registry.register_resource("powermenu", power_menu);
361 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
364 raise_icon = new RaiseIcon();
365 raise_icon.set_visible(true);
367 close_or_back = new CloseOrBack();
368 close_or_back.set_visible(true);
370 window_list = new WindowList("Current apps");
371 zavai.registry.register_applet("wm.list", window_list);
372 zavai.registry.getmenu("menu.main").add_applet("wm.list");
375 launcher = new Launcher("Run program");
377 zavai.log.error("Not running launcher: " + e.message);
381 if (launcher != null)
383 zavai.registry.register_applet("wm.launcher", launcher);
384 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");