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;
129 if (last_down.tv_sec == 0)
132 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
136 long diff = timediff(t, &last_down);
137 long_press = diff >= 1000000;
140 if (last_down.tv_sec == 0)
142 // Ignore: release has been simulated with the timeout
144 if (button_press_timeout != 0)
146 // Cancel the timeout
147 Source.remove(button_press_timeout);
148 button_press_timeout = 0;
150 long diff = timediff(t, &last_down);
156 last_down.tv_sec = 0;
157 last_down.tv_usec = 0;
161 if (long_press) power_long_press();
162 if (short_press) power_short_press();
165 private void on_power_short_press()
168 // Short press: turn on backlight for a bit
171 // Short press: toggle power menu
175 private void on_power_long_press()
178 // Long press: unlock
179 set_screen_lock(false);
181 // Long press: lock screen
182 set_screen_lock(true);
186 public class BatteryIcon : Gtk.StatusIcon
188 public dynamic DBus.Object battery;
189 protected string last_status;
190 protected int last_capacity;
194 battery = zavai.registry.sbus.get_object(
195 "org.freesmartphone.odeviced",
196 "/org/freesmartphone/Device/PowerSupply/battery",
197 "org.freesmartphone.Device.PowerSupply");
199 // activate += on_activate;
201 battery.PowerStatus += on_power_status;
202 battery.Capacity += on_capacity;
204 last_status = battery.GetPowerStatus();
205 last_capacity = battery.GetCapacity();
210 private void on_power_status(dynamic DBus.Object bat, string status)
212 zavai.log.info("New battery status: " + status);
213 last_status = status;
217 private void on_capacity(dynamic DBus.Object bat, int val)
219 stderr.printf("NEW CAPACITY: %d\n", val);
225 private void on_activate()
230 protected void update_icon()
232 string name = zavai.config.icondir + "/battery/";
234 if (last_status == "charging")
235 name += "%02d0_charging_500.png".printf(last_capacity/10);
237 name += "%02d0.png".printf(last_capacity/10);
239 stderr.printf("Loading icon from %s\n", name);
245 public class ScreenLockButton : Gtk.Button
247 public ScreenLockButton()
249 label = "Lock screen";
250 clicked += on_clicked;
251 set_size_request(0, zavai.config.min_button_height);
254 public void on_clicked()
256 zavai.log.info("Locking screen");
257 power.set_screen_lock(true);
262 public class SuspendButton : Gtk.Button
264 public SuspendButton()
267 clicked += on_clicked;
268 set_size_request(0, zavai.config.min_button_height);
271 public void on_clicked()
273 zavai.log.info("Suspending the phone via FSO");
279 public class ShutdownButton : Gtk.Button
281 public ShutdownButton()
284 clicked += on_clicked;
285 set_size_request(0, zavai.config.min_button_height);
288 public void on_clicked()
290 zavai.log.info("Shutting down the phone via FSO");
296 public class RebootButton : Gtk.Button
298 public RebootButton()
301 clicked += on_clicked;
302 set_size_request(0, zavai.config.min_button_height);
305 public void on_clicked()
307 zavai.log.info("Rebooting the phone via FSO");
313 // For a list of dbus services, look in /etc/dbus-1/system.d/
314 public class Backlight: zavai.Service
316 public dynamic DBus.Object usage;
322 usage = zavai.registry.sbus.get_object(
323 "org.freesmartphone.ousaged",
324 "/org/freesmartphone/Usage",
325 "org.freesmartphone.Usage");
328 // Turn the backlight and then let it fade off
331 // There must be a better method
332 usage.RequestResource("Display");
333 usage.ReleaseResource("Display");
336 /// Request GPS resource
337 public override void start()
341 usage.RequestResource("Display");
342 zavai.log.info("Acquired display");
344 } catch (GLib.Error e) {
345 zavai.log.error(e.message);
350 // Release usage of GPS
351 public override void stop()
353 if (!started) return;
355 usage.ReleaseResource("Display");
356 zavai.log.info("Released display");
358 } catch (GLib.Error e) {
359 zavai.log.error(e.message);
365 public class PowerMenu : zavai.Resource, Gtk.Window
367 protected Gtk.VBox vbox;
368 protected ScreenLockButton act_screen_lock;
369 protected SuspendButton act_suspend;
370 protected ShutdownButton act_shutdown;
371 protected RebootButton act_reboot;
372 protected ServiceRequestLink act_backlight_on;
373 protected bool shown;
377 type = Gtk.WindowType.TOPLEVEL;
378 title = "Power Menu";
380 destroy_with_parent = true;
381 set_transient_for(zavai.app);
383 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
384 set_size_request(300, 0);
386 vbox = new Gtk.VBox(false, 0);
389 //destroy += Gtk.main_quit;
390 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
391 //visibility_notify_event += on_visibility;
392 set_skip_pager_hint(true);
393 set_skip_taskbar_hint(true);
394 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
396 act_screen_lock = new ScreenLockButton();
397 vbox.pack_start(act_screen_lock, false, false, 0);
399 act_suspend = new SuspendButton();
400 vbox.pack_start(act_suspend, false, false, 0);
402 act_shutdown = new ShutdownButton();
403 vbox.pack_start(act_shutdown, false, false, 0);
405 act_reboot = new RebootButton();
406 vbox.pack_start(act_reboot, false, false, 0);
408 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
409 vbox.pack_start(act_backlight_on, false, false, 0);
424 // TODO: do more in case it is visible but has no visibility (is covered by others)
436 public void shutdown() {}
440 public class TogglePowerMenu : Gtk.Button
442 public TogglePowerMenu()
444 label = "Toggle power menu";
445 clicked += on_clicked;
446 set_size_request(0, zavai.config.min_button_height);
449 public void on_clicked()
451 zavai.log.info("Toggling power menu");
458 PowerMenu power_menu;
459 BatteryIcon battery_icon;
461 //TogglePowerMenu tpm;
466 backlight = new Backlight();
467 zavai.registry.register_service(backlight);
469 battery_icon = new BatteryIcon();
470 battery_icon.set_visible(true);
472 power_menu = new PowerMenu();
473 zavai.registry.register_resource("powermenu", power_menu);
475 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
476 //tpm = new TogglePowerMenu();
477 //zavai.registry.getmenu("menu.main").add_widget(tpm);
480 raise_icon = new RaiseIcon();
481 raise_icon.set_visible(true);
483 close_or_back = new CloseOrBack();
484 close_or_back.set_visible(true);
486 window_list = new WindowList("Current apps");
487 zavai.registry.register_applet("wm.list", window_list);
488 zavai.registry.getmenu("menu.main").add_applet("wm.list");
491 launcher = new Launcher("Run program");
493 zavai.log.error("Not running launcher: " + e.message);
497 if (launcher != null)
499 zavai.registry.register_applet("wm.launcher", launcher);
500 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");