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);
46 public signal void power_short_press();
47 public signal void power_long_press();
48 private uint button_press_timeout;
52 screen_locked = false;
54 hide_after_closing_power_menu = false;
56 last_down.tv_usec = 0;
57 button_press_timeout = 0;
59 usage = zavai.registry.sbus.get_object(
60 "org.freesmartphone.ousaged",
61 "/org/freesmartphone/Usage",
62 "org.freesmartphone.Usage");
64 zavai.input.power_button.power_button += on_power_button;
65 zavai.input.power_button.request("zavai.ui.powerbutton.power");
67 power_short_press += on_power_short_press;
68 power_long_press += on_power_long_press;
71 public void shutdown()
73 zavai.input.power_button.release("zavai.ui.powerbutton.power");
76 public void do_suspend()
81 zavai.log.error("Suspending phone: " + e.message);
84 public void do_shutdown()
89 zavai.log.error("Shutting down phone: " + e.message);
92 public void do_reboot()
97 zavai.log.error("Rebooting phone: " + e.message);
101 public void set_screen_lock(bool locked)
103 if (locked && screen_locked)
105 if (!locked && !screen_locked)
110 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
111 if (screen_lock_fd < 0)
113 zavai.log.error("Cannot open /dev/input/event1");
117 int EVIOCGRAB = 0x40044590;
118 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
120 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
121 Posix.close(screen_lock_fd);
125 Posix.close(screen_lock_fd);
127 screen_locked = locked;
131 screen_lock_changed(locked);
134 private bool on_power_button_timeout()
136 last_down.tv_sec = 0;
137 last_down.tv_usec = 0;
143 private void on_power_button(Posix.timeval* t, bool pressed)
145 bool short_press = false;
146 bool long_press = false;
150 if (last_down.tv_sec == 0)
153 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
157 long diff = timediff(t, &last_down);
158 long_press = diff >= 1000000;
161 if (last_down.tv_sec == 0)
163 // Ignore: release has been simulated with the timeout
165 if (button_press_timeout != 0)
167 // Cancel the timeout
168 Source.remove(button_press_timeout);
169 button_press_timeout = 0;
171 long diff = timediff(t, &last_down);
177 last_down.tv_sec = 0;
178 last_down.tv_usec = 0;
182 if (long_press) power_long_press();
183 if (short_press) power_short_press();
186 private void on_power_short_press()
189 // Short press: turn on backlight for a bit
192 // Short press: toggle power menu
196 private void on_power_long_press()
199 // Long press: unlock
200 set_screen_lock(false);
202 // Long press: lock screen
203 set_screen_lock(true);
207 public class BatteryIcon : Gtk.StatusIcon
209 public dynamic DBus.Object battery;
210 protected string last_status;
211 protected int last_capacity;
215 battery = zavai.registry.sbus.get_object(
216 "org.freesmartphone.odeviced",
217 "/org/freesmartphone/Device/PowerSupply/battery",
218 "org.freesmartphone.Device.PowerSupply");
220 // activate += on_activate;
222 battery.PowerStatus += on_power_status;
223 battery.Capacity += on_capacity;
225 last_status = battery.GetPowerStatus();
226 last_capacity = battery.GetCapacity();
231 private void on_power_status(dynamic DBus.Object bat, string status)
233 zavai.log.info("New battery status: " + status);
234 last_status = status;
238 private void on_capacity(dynamic DBus.Object bat, int val)
240 stderr.printf("NEW CAPACITY: %d\n", val);
246 private void on_activate()
251 protected void update_icon()
253 string name = zavai.config.icondir + "/battery/";
255 if (last_status == "charging")
256 name += "%02d0_charging_500.png".printf(last_capacity/10);
258 name += "%02d0.png".printf(last_capacity/10);
260 stderr.printf("Loading icon from %s\n", name);
266 public class ScreenLockButton : Gtk.Button
268 public ScreenLockButton()
270 label = "Lock screen";
271 clicked += on_clicked;
272 set_size_request(0, zavai.config.min_button_height);
275 public void on_clicked()
277 zavai.log.info("Locking screen");
278 power.set_screen_lock(true);
279 power_menu.hide_menu();
283 public class SuspendButton : Gtk.Button
285 public SuspendButton()
288 clicked += on_clicked;
289 set_size_request(0, zavai.config.min_button_height);
292 public void on_clicked()
294 zavai.log.info("Suspending the phone via FSO");
296 power_menu.hide_menu();
300 public class ShutdownButton : Gtk.Button
302 public ShutdownButton()
305 clicked += on_clicked;
306 set_size_request(0, zavai.config.min_button_height);
309 public void on_clicked()
311 zavai.log.info("Shutting down the phone via FSO");
313 power_menu.hide_menu();
317 public class RebootButton : Gtk.Button
319 public RebootButton()
322 clicked += on_clicked;
323 set_size_request(0, zavai.config.min_button_height);
326 public void on_clicked()
328 zavai.log.info("Rebooting the phone via FSO");
330 power_menu.hide_menu();
334 // For a list of dbus services, look in /etc/dbus-1/system.d/
335 public class Backlight: zavai.Service
337 public dynamic DBus.Object usage;
343 usage = zavai.registry.sbus.get_object(
344 "org.freesmartphone.ousaged",
345 "/org/freesmartphone/Usage",
346 "org.freesmartphone.Usage");
349 // Turn the backlight and then let it fade off
352 // There must be a better method
354 usage.RequestResource("Display");
355 usage.ReleaseResource("Display");
357 zavai.log.error("Requesting/releasing resource Display: " + e.message);
361 /// Request GPS resource
362 public override void start()
366 usage.RequestResource("Display");
367 zavai.log.info("Acquired display");
369 } catch (GLib.Error e) {
370 zavai.log.error(e.message);
375 // Release usage of GPS
376 public override void stop()
378 if (!started) return;
380 usage.ReleaseResource("Display");
381 zavai.log.info("Released display");
383 } catch (GLib.Error e) {
384 zavai.log.error(e.message);
390 public class PowerMenu : zavai.Resource, Gtk.Window
392 protected Gtk.VBox vbox;
393 protected ScreenLockButton act_screen_lock;
394 protected SuspendButton act_suspend;
395 protected ShutdownButton act_shutdown;
396 protected RebootButton act_reboot;
397 protected ServiceRequestLink act_backlight_on;
398 protected bool shown;
402 type = Gtk.WindowType.TOPLEVEL;
403 title = "Power Menu";
405 destroy_with_parent = true;
406 set_transient_for(zavai.app);
408 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
409 set_size_request(300, 500);
411 vbox = new Gtk.VBox(false, 0);
414 //destroy += Gtk.main_quit;
415 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
416 //visibility_notify_event += on_visibility;
417 set_skip_pager_hint(true);
418 set_skip_taskbar_hint(true);
419 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
421 act_screen_lock = new ScreenLockButton();
422 vbox.pack_start(act_screen_lock, false, false, 0);
424 act_suspend = new SuspendButton();
425 vbox.pack_start(act_suspend, false, false, 0);
427 act_shutdown = new ShutdownButton();
428 vbox.pack_start(act_shutdown, false, false, 0);
430 act_reboot = new RebootButton();
431 vbox.pack_start(act_reboot, false, false, 0);
433 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
434 act_backlight_on.toggled += (src) => { this.hide_menu(); };
435 vbox.pack_start(act_backlight_on, false, false, 0);
450 // TODO: do more in case it is visible but has no visibility (is covered by others)
457 public void hide_menu()
462 public void shutdown() {}
466 public class TogglePowerMenu : Gtk.Button
468 public TogglePowerMenu()
470 label = "Toggle power menu";
471 clicked += on_clicked;
472 set_size_request(0, zavai.config.min_button_height);
475 public void on_clicked()
477 zavai.log.info("Toggling power menu");
484 PowerMenu power_menu;
485 BatteryIcon battery_icon;
487 //TogglePowerMenu tpm;
492 backlight = new Backlight();
493 zavai.registry.register_service(backlight);
495 battery_icon = new BatteryIcon();
496 battery_icon.set_visible(true);
498 power_menu = new PowerMenu();
499 zavai.registry.register_resource("powermenu", power_menu);
501 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
502 //tpm = new TogglePowerMenu();
503 //zavai.registry.getmenu("menu.main").add_widget(tpm);
506 raise_icon = new RaiseIcon();
507 raise_icon.set_visible(true);
509 close_or_back = new CloseOrBack();
510 close_or_back.set_visible(true);
512 window_list = new WindowList("Current apps");
513 zavai.registry.register_applet("wm.list", window_list);
514 zavai.registry.getmenu("menu.main").add_applet("wm.list");
517 launcher = new Launcher("Run program");
519 zavai.log.error("Not running launcher: " + e.message);
523 if (launcher != null)
525 zavai.registry.register_applet("wm.launcher", launcher);
526 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");