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 backlight.turn_off();
112 screen_lock_changed(locked);
115 private bool on_power_button_timeout()
117 last_down.tv_sec = 0;
118 last_down.tv_usec = 0;
124 private void on_power_button(Posix.timeval* t, bool pressed)
126 bool short_press = false;
127 bool long_press = false;
131 if (last_down.tv_sec == 0)
134 button_press_timeout = Timeout.add(1500, on_power_button_timeout);
138 long diff = timediff(t, &last_down);
139 long_press = diff >= 1500000;
142 if (last_down.tv_sec == 0)
144 // Ignore: release has been simulated with the timeout
146 if (button_press_timeout != 0)
148 // Cancel the timeout
149 Source.remove(button_press_timeout);
150 button_press_timeout = 0;
152 long diff = timediff(t, &last_down);
158 last_down.tv_sec = 0;
159 last_down.tv_usec = 0;
163 if (long_press) power_long_press();
164 if (short_press) power_short_press();
167 private void on_power_short_press()
170 // Short press: turn on backlight for a bit
173 // Short press: toggle power menu
177 private void on_power_long_press()
180 // Long press: unlock
181 set_screen_lock(false);
183 // Long press: lock screen
184 set_screen_lock(true);
188 public class BatteryIcon : Gtk.StatusIcon
190 public dynamic DBus.Object battery;
191 protected string last_status;
192 protected int last_capacity;
196 battery = zavai.registry.sbus.get_object(
197 "org.freesmartphone.odeviced",
198 "/org/freesmartphone/Device/PowerSupply/battery",
199 "org.freesmartphone.Device.PowerSupply");
201 // activate += on_activate;
203 battery.PowerStatus += on_power_status;
204 battery.Capacity += on_capacity;
206 last_status = battery.GetPowerStatus();
207 last_capacity = battery.GetCapacity();
212 private void on_power_status(dynamic DBus.Object bat, string status)
214 zavai.log.info("New battery status: " + status);
215 last_status = status;
219 private void on_capacity(dynamic DBus.Object bat, int val)
221 stderr.printf("NEW CAPACITY: %d\n", val);
227 private void on_activate()
232 protected void update_icon()
234 string name = zavai.config.icondir + "/battery/";
236 if (last_status == "charging")
237 name += "%02d0_charging_500.png".printf(last_capacity/10);
239 name += "%02d0.png".printf(last_capacity/10);
241 stderr.printf("Loading icon from %s\n", name);
247 public class ScreenLockButton : Gtk.Button
249 public ScreenLockButton()
251 label = "Lock screen";
252 clicked += on_clicked;
253 set_size_request(0, zavai.config.min_button_height);
256 public void on_clicked()
258 zavai.log.info("Locking screen");
259 power.set_screen_lock(true);
264 public class SuspendButton : Gtk.Button
266 public SuspendButton()
269 clicked += on_clicked;
270 set_size_request(0, zavai.config.min_button_height);
273 public void on_clicked()
275 zavai.log.info("Suspending the phone via FSO");
281 public class ShutdownButton : Gtk.Button
283 public ShutdownButton()
286 clicked += on_clicked;
287 set_size_request(0, zavai.config.min_button_height);
290 public void on_clicked()
292 zavai.log.info("Shutting down the phone via FSO");
298 public class RebootButton : Gtk.Button
300 public RebootButton()
303 clicked += on_clicked;
304 set_size_request(0, zavai.config.min_button_height);
307 public void on_clicked()
309 zavai.log.info("Rebooting the phone via FSO");
315 // For a list of dbus services, look in /etc/dbus-1/system.d/
316 public class Backlight: zavai.Service
318 public dynamic DBus.Object usage;
324 usage = zavai.registry.sbus.get_object(
325 "org.freesmartphone.ousaged",
326 "/org/freesmartphone/Usage",
327 "org.freesmartphone.Usage");
330 // Turn the backlight and then let it fade off
333 // There must be a better method
334 //usage.RequestResource("Display");
335 //usage.ReleaseResource("Display");
339 public void turn_on()
341 usage.SetResourcePolicy("Display", "enabled");
342 usage.SetResourcePolicy("Display", "auto");
345 public void turn_off()
347 usage.SetResourcePolicy("Display", "disabled");
348 usage.SetResourcePolicy("Display", "auto");
351 /// Request GPS resource
352 public override void start()
356 usage.RequestResource("Display");
357 zavai.log.info("Acquired display");
359 } catch (GLib.Error e) {
360 zavai.log.error(e.message);
365 // Release usage of GPS
366 public override void stop()
368 if (!started) return;
370 usage.ReleaseResource("Display");
371 zavai.log.info("Released display");
373 } catch (GLib.Error e) {
374 zavai.log.error(e.message);
380 public class PowerMenu : zavai.Resource, Gtk.Window
382 protected Gtk.VBox vbox;
383 protected ScreenLockButton act_screen_lock;
384 protected SuspendButton act_suspend;
385 protected ShutdownButton act_shutdown;
386 protected RebootButton act_reboot;
387 protected ServiceRequestLink act_backlight_on;
388 protected bool shown;
392 type = Gtk.WindowType.TOPLEVEL;
393 title = "Power Menu";
395 destroy_with_parent = true;
396 set_transient_for(zavai.app);
398 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
399 set_size_request(300, 500);
401 vbox = new Gtk.VBox(false, 0);
404 //destroy += Gtk.main_quit;
405 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
406 //visibility_notify_event += on_visibility;
407 set_skip_pager_hint(true);
408 set_skip_taskbar_hint(true);
409 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
411 act_screen_lock = new ScreenLockButton();
412 vbox.pack_start(act_screen_lock, false, false, 0);
414 act_suspend = new SuspendButton();
415 vbox.pack_start(act_suspend, false, false, 0);
417 act_shutdown = new ShutdownButton();
418 vbox.pack_start(act_shutdown, false, false, 0);
420 act_reboot = new RebootButton();
421 vbox.pack_start(act_reboot, false, false, 0);
423 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
424 vbox.pack_start(act_backlight_on, false, false, 0);
439 // TODO: do more in case it is visible but has no visibility (is covered by others)
451 public void shutdown() {}
455 public class TogglePowerMenu : Gtk.Button
457 public TogglePowerMenu()
459 label = "Toggle power menu";
460 clicked += on_clicked;
461 set_size_request(0, zavai.config.min_button_height);
464 public void on_clicked()
466 zavai.log.info("Toggling power menu");
473 PowerMenu power_menu;
474 BatteryIcon battery_icon;
476 //TogglePowerMenu tpm;
481 backlight = new Backlight();
482 zavai.registry.register_service(backlight);
484 battery_icon = new BatteryIcon();
485 battery_icon.set_visible(true);
487 power_menu = new PowerMenu();
488 zavai.registry.register_resource("powermenu", power_menu);
490 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
491 //tpm = new TogglePowerMenu();
492 //zavai.registry.getmenu("menu.main").add_widget(tpm);
495 raise_icon = new RaiseIcon();
496 raise_icon.set_visible(true);
498 close_or_back = new CloseOrBack();
499 close_or_back.set_visible(true);
501 window_list = new WindowList("Current apps");
502 zavai.registry.register_applet("wm.list", window_list);
503 zavai.registry.getmenu("menu.main").add_applet("wm.list");
506 launcher = new Launcher("Run program");
508 zavai.log.error("Not running launcher: " + e.message);
512 if (launcher != null)
514 zavai.registry.register_applet("wm.launcher", launcher);
515 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");