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() { usage.Suspend(); }
77 public void do_shutdown() { usage.Shutdown(); }
78 public void do_reboot() { usage.Reboot(); }
80 public void set_screen_lock(bool locked)
82 if (locked && screen_locked)
84 if (!locked && !screen_locked)
89 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
90 if (screen_lock_fd < 0)
92 zavai.log.error("Cannot open /dev/input/event1");
96 int EVIOCGRAB = 0x40044590;
97 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
99 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
100 Posix.close(screen_lock_fd);
104 Posix.close(screen_lock_fd);
106 screen_locked = locked;
110 screen_lock_changed(locked);
113 private bool on_power_button_timeout()
115 last_down.tv_sec = 0;
116 last_down.tv_usec = 0;
122 private void on_power_button(Posix.timeval* t, bool pressed)
124 bool short_press = false;
125 bool long_press = false;
127 stderr.printf("EVENT %d\n", (int)pressed);
131 if (last_down.tv_sec == 0)
133 stderr.printf(" FIRST PRESSED\n");
135 button_press_timeout = Timeout.add(1500, on_power_button_timeout);
139 long diff = timediff(t, &last_down);
140 stderr.printf(" PRESSED FOR %ld\n", diff);
141 long_press = diff >= 1500000;
144 if (last_down.tv_sec == 0)
146 // Ignore: release has been simulated with the timeout
148 if (button_press_timeout != 0)
150 // Cancel the timeout
151 Source.remove(button_press_timeout);
152 button_press_timeout = 0;
154 long diff = timediff(t, &last_down);
155 stderr.printf(" RELEASED AFTER %ld\n", diff);
161 last_down.tv_sec = 0;
162 last_down.tv_usec = 0;
166 stderr.printf(" LP %d SP %d\n", (int)long_press, (int)short_press);
168 if (long_press) power_long_press();
169 if (short_press) power_short_press();
172 private void on_power_short_press()
175 // Short press: turn on backlight for a bit
178 // Short press: toggle power menu
182 private void on_power_long_press()
185 // Long press: unlock
186 set_screen_lock(false);
188 // Long press: lock screen
189 set_screen_lock(true);
193 public class BatteryIcon : Gtk.StatusIcon
195 public dynamic DBus.Object battery;
196 protected string last_status;
197 protected int last_capacity;
201 battery = zavai.registry.sbus.get_object(
202 "org.freesmartphone.odeviced",
203 "/org/freesmartphone/Device/PowerSupply/battery",
204 "org.freesmartphone.Device.PowerSupply");
206 // activate += on_activate;
208 battery.PowerStatus += on_power_status;
209 battery.Capacity += on_capacity;
211 last_status = battery.GetPowerStatus();
212 last_capacity = battery.GetCapacity();
217 private void on_power_status(dynamic DBus.Object bat, string status)
219 zavai.log.info("New battery status: " + status);
220 last_status = status;
224 private void on_capacity(dynamic DBus.Object bat, int val)
226 stderr.printf("NEW CAPACITY: %d\n", val);
232 private void on_activate()
237 protected void update_icon()
239 string name = zavai.config.icondir + "/battery/";
241 if (last_status == "charging")
242 name += "%02d0_charging_500.png".printf(last_capacity/10);
244 name += "%02d0.png".printf(last_capacity/10);
246 stderr.printf("Loading icon from %s\n", name);
252 public class ScreenLockButton : Gtk.Button
254 public ScreenLockButton()
256 label = "Lock screen";
257 clicked += on_clicked;
258 set_size_request(0, zavai.config.min_button_height);
261 public void on_clicked()
263 zavai.log.info("Locking screen");
264 power.set_screen_lock(true);
269 public class SuspendButton : Gtk.Button
271 public SuspendButton()
274 clicked += on_clicked;
275 set_size_request(0, zavai.config.min_button_height);
278 public void on_clicked()
280 zavai.log.info("Suspending the phone via FSO");
286 public class ShutdownButton : Gtk.Button
288 public ShutdownButton()
291 clicked += on_clicked;
292 set_size_request(0, zavai.config.min_button_height);
295 public void on_clicked()
297 zavai.log.info("Shutting down the phone via FSO");
303 public class RebootButton : Gtk.Button
305 public RebootButton()
308 clicked += on_clicked;
309 set_size_request(0, zavai.config.min_button_height);
312 public void on_clicked()
314 zavai.log.info("Rebooting the phone via FSO");
320 // For a list of dbus services, look in /etc/dbus-1/system.d/
321 public class Backlight: zavai.Service
323 public dynamic DBus.Object usage;
329 usage = zavai.registry.sbus.get_object(
330 "org.freesmartphone.ousaged",
331 "/org/freesmartphone/Usage",
332 "org.freesmartphone.Usage");
335 // Turn the backlight and then let it fade off
338 // There must be a better method
339 usage.RequestResource("Display");
340 usage.ReleaseResource("Display");
343 /// Request GPS resource
344 public override void start()
348 usage.RequestResource("Display");
349 zavai.log.info("Acquired display");
351 } catch (GLib.Error e) {
352 zavai.log.error(e.message);
357 // Release usage of GPS
358 public override void stop()
360 if (!started) return;
362 usage.ReleaseResource("Display");
363 zavai.log.info("Released display");
365 } catch (GLib.Error e) {
366 zavai.log.error(e.message);
372 public class PowerMenu : zavai.Resource, Gtk.Window
374 protected Gtk.VBox vbox;
375 protected ScreenLockButton act_screen_lock;
376 protected SuspendButton act_suspend;
377 protected ShutdownButton act_shutdown;
378 protected RebootButton act_reboot;
379 protected ServiceRequestLink act_backlight_on;
380 protected bool shown;
384 type = Gtk.WindowType.TOPLEVEL;
385 title = "Power Menu";
387 destroy_with_parent = true;
388 set_transient_for(zavai.app);
390 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
391 set_size_request(300, 500);
393 vbox = new Gtk.VBox(false, 0);
396 //destroy += Gtk.main_quit;
397 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
398 //visibility_notify_event += on_visibility;
399 set_skip_pager_hint(true);
400 set_skip_taskbar_hint(true);
401 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
403 act_screen_lock = new ScreenLockButton();
404 vbox.pack_start(act_screen_lock, false, false, 0);
406 act_suspend = new SuspendButton();
407 vbox.pack_start(act_suspend, false, false, 0);
409 act_shutdown = new ShutdownButton();
410 vbox.pack_start(act_shutdown, false, false, 0);
412 act_reboot = new RebootButton();
413 vbox.pack_start(act_reboot, false, false, 0);
415 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
416 vbox.pack_start(act_backlight_on, false, false, 0);
431 // TODO: do more in case it is visible but has no visibility (is covered by others)
443 public void shutdown() {}
447 public class TogglePowerMenu : Gtk.Button
449 public TogglePowerMenu()
451 label = "Toggle power menu";
452 clicked += on_clicked;
453 set_size_request(0, zavai.config.min_button_height);
456 public void on_clicked()
458 zavai.log.info("Toggling power menu");
465 PowerMenu power_menu;
466 BatteryIcon battery_icon;
468 //TogglePowerMenu tpm;
473 backlight = new Backlight();
474 zavai.registry.register_service(backlight);
476 battery_icon = new BatteryIcon();
477 battery_icon.set_visible(true);
479 power_menu = new PowerMenu();
480 zavai.registry.register_resource("powermenu", power_menu);
482 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
483 //tpm = new TogglePowerMenu();
484 //zavai.registry.getmenu("menu.main").add_widget(tpm);
487 raise_icon = new RaiseIcon();
488 raise_icon.set_visible(true);
490 close_or_back = new CloseOrBack();
491 close_or_back.set_visible(true);
493 window_list = new WindowList("Current apps");
494 zavai.registry.register_applet("wm.list", window_list);
495 zavai.registry.getmenu("menu.main").add_applet("wm.list");
498 launcher = new Launcher("Run program");
500 zavai.log.error("Not running launcher: " + e.message);
504 if (launcher != null)
506 zavai.registry.register_applet("wm.launcher", launcher);
507 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");