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
27 // Compute a-b in microseconds
28 static long timediff(Posix.timeval* a, Posix.timeval* b)
30 return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec);
33 public class Power : zavai.Resource, Object
35 public dynamic DBus.Object usage;
36 public bool screen_locked;
37 private int screen_lock_fd;
38 // Timestamp of the past power button pressed even (0 if the button has
40 private Posix.timeval last_down;
42 private bool hide_after_closing_power_menu;
44 public signal void screen_lock_changed(bool state);
48 screen_locked = false;
50 hide_after_closing_power_menu = false;
52 last_down.tv_usec = 0;
54 usage = zavai.registry.sbus.get_object(
55 "org.freesmartphone.ousaged",
56 "/org/freesmartphone/Usage",
57 "org.freesmartphone.Usage");
59 zavai.input.power_button.power_button += on_power_button;
60 zavai.input.power_button.request("zavai.ui.powerbutton.power");
63 public void shutdown()
65 zavai.input.power_button.release("zavai.ui.powerbutton.power");
68 public void do_suspend() { usage.Suspend(); }
69 public void do_shutdown() { usage.Shutdown(); }
70 public void do_reboot() { usage.Reboot(); }
72 public void set_screen_lock(bool locked)
74 if (locked && screen_locked)
76 if (!locked && !screen_locked)
81 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
82 if (screen_lock_fd < 0)
84 zavai.log.error("Cannot open /dev/input/event1");
88 int EVIOCGRAB = 0x40044590;
89 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
91 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
92 Posix.close(screen_lock_fd);
96 Posix.close(screen_lock_fd);
98 screen_locked = locked;
102 screen_lock_changed(locked);
105 private void on_power_button(Posix.timeval* t, bool pressed)
107 bool short_press = false;
108 bool long_press = false;
112 if (last_down.tv_sec == 0)
116 long diff = timediff(t, &last_down);
117 long_press = diff >= 1500000;
120 long diff = timediff(t, &last_down);
126 last_down.tv_sec = 0;
127 last_down.tv_usec = 0;
132 // Short press: turn on backlight for a bit
135 // Short press: toggle power menu
137 } else if (long_press) {
139 // Long press: unlock
140 set_screen_lock(false);
142 // Long press: lock screen
143 set_screen_lock(true);
148 public class BatteryIcon : Gtk.StatusIcon
150 public dynamic DBus.Object battery;
151 protected string last_status;
152 protected int last_capacity;
156 battery = zavai.registry.sbus.get_object(
157 "org.freesmartphone.odeviced",
158 "/org/freesmartphone/Device/PowerSupply/battery",
159 "org.freesmartphone.Device.PowerSupply");
161 // activate += on_activate;
163 battery.PowerStatus += on_power_status;
164 battery.Capacity += on_capacity;
166 last_status = battery.GetPowerStatus();
167 last_capacity = battery.GetCapacity();
172 private void on_power_status(dynamic DBus.Object bat, string status)
174 zavai.log.info("New battery status: " + status);
175 last_status = status;
179 private void on_capacity(dynamic DBus.Object bat, int val)
181 stderr.printf("NEW CAPACITY: %d\n", val);
187 private void on_activate()
192 protected void update_icon()
194 string name = zavai.config.icondir + "/battery/";
196 if (last_status == "charging")
197 name += "%02d0_charging_500.png".printf(last_capacity/10);
199 name += "%02d0.png".printf(last_capacity/10);
201 stderr.printf("Loading icon from %s\n", name);
207 public class ScreenLockButton : Gtk.Button
209 public ScreenLockButton()
211 label = "Lock screen";
212 clicked += on_clicked;
213 set_size_request(0, zavai.config.min_button_height);
216 public void on_clicked()
218 zavai.log.info("Locking screen");
219 power.set_screen_lock(true);
224 public class SuspendButton : Gtk.Button
226 public SuspendButton()
229 clicked += on_clicked;
230 set_size_request(0, zavai.config.min_button_height);
233 public void on_clicked()
235 zavai.log.info("Suspending the phone via FSO");
241 public class ShutdownButton : Gtk.Button
243 public ShutdownButton()
246 clicked += on_clicked;
247 set_size_request(0, zavai.config.min_button_height);
250 public void on_clicked()
252 zavai.log.info("Shutting down the phone via FSO");
258 public class RebootButton : Gtk.Button
260 public RebootButton()
263 clicked += on_clicked;
264 set_size_request(0, zavai.config.min_button_height);
267 public void on_clicked()
269 zavai.log.info("Rebooting the phone via FSO");
275 // For a list of dbus services, look in /etc/dbus-1/system.d/
276 public class Backlight: zavai.Service
278 public dynamic DBus.Object usage;
284 usage = zavai.registry.sbus.get_object(
285 "org.freesmartphone.ousaged",
286 "/org/freesmartphone/Usage",
287 "org.freesmartphone.Usage");
290 // Turn the backlight and then let it fade off
293 // There must be a better method
294 usage.RequestResource("Display");
295 usage.ReleaseResource("Display");
298 /// Request GPS resource
299 public override void start()
303 usage.RequestResource("Display");
304 zavai.log.info("Acquired display");
306 } catch (GLib.Error e) {
307 zavai.log.error(e.message);
312 // Release usage of GPS
313 public override void stop()
315 if (!started) return;
317 usage.ReleaseResource("Display");
318 zavai.log.info("Released display");
320 } catch (GLib.Error e) {
321 zavai.log.error(e.message);
327 public class PowerMenu : zavai.Resource, Gtk.Window
329 protected Gtk.VBox vbox;
330 protected ScreenLockButton act_screen_lock;
331 protected SuspendButton act_suspend;
332 protected ShutdownButton act_shutdown;
333 protected RebootButton act_reboot;
334 protected ServiceRequestLink act_backlight_on;
335 protected bool shown;
339 type = Gtk.WindowType.TOPLEVEL;
340 title = "Power Menu";
342 destroy_with_parent = true;
343 set_transient_for(zavai.app);
345 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
346 set_size_request(300, 500);
348 vbox = new Gtk.VBox(false, 0);
351 //destroy += Gtk.main_quit;
352 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
353 //visibility_notify_event += on_visibility;
354 set_skip_pager_hint(true);
355 set_skip_taskbar_hint(true);
356 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
358 act_screen_lock = new ScreenLockButton();
359 vbox.pack_start(act_screen_lock, false, false, 0);
361 act_suspend = new SuspendButton();
362 vbox.pack_start(act_suspend, false, false, 0);
364 act_shutdown = new ShutdownButton();
365 vbox.pack_start(act_shutdown, false, false, 0);
367 act_reboot = new RebootButton();
368 vbox.pack_start(act_reboot, false, false, 0);
370 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
371 vbox.pack_start(act_backlight_on, false, false, 0);
386 // TODO: do more in case it is visible but has no visibility (is covered by others)
398 public void shutdown() {}
402 public class TogglePowerMenu : Gtk.Button
404 public TogglePowerMenu()
406 label = "Toggle power menu";
407 clicked += on_clicked;
408 set_size_request(0, zavai.config.min_button_height);
411 public void on_clicked()
413 zavai.log.info("Toggling power menu");
420 PowerMenu power_menu;
421 BatteryIcon battery_icon;
423 //TogglePowerMenu tpm;
428 backlight = new Backlight();
429 zavai.registry.register_service(backlight);
431 battery_icon = new BatteryIcon();
432 battery_icon.set_visible(true);
434 power_menu = new PowerMenu();
435 zavai.registry.register_resource("powermenu", power_menu);
437 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
438 //tpm = new TogglePowerMenu();
439 //zavai.registry.getmenu("menu.main").add_widget(tpm);
442 raise_icon = new RaiseIcon();
443 raise_icon.set_visible(true);
445 close_or_back = new CloseOrBack();
446 close_or_back.set_visible(true);
448 window_list = new WindowList("Current apps");
449 zavai.registry.register_applet("wm.list", window_list);
450 zavai.registry.getmenu("menu.main").add_applet("wm.list");
453 launcher = new Launcher("Run program");
455 zavai.log.error("Not running launcher: " + e.message);
459 if (launcher != null)
461 zavai.registry.register_applet("wm.launcher", launcher);
462 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");