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 backlight.turn_off_unless_needed();
127 Posix.close(screen_lock_fd);
129 screen_locked = locked;
133 screen_lock_changed(locked);
136 private bool on_power_button_timeout()
138 last_down.tv_sec = 0;
139 last_down.tv_usec = 0;
145 private void on_power_button(Posix.timeval* t, bool pressed)
147 bool short_press = false;
148 bool long_press = false;
152 if (last_down.tv_sec == 0)
155 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
159 long diff = timediff(t, &last_down);
160 long_press = diff >= 1000000;
163 if (last_down.tv_sec == 0)
165 // Ignore: release has been simulated with the timeout
167 if (button_press_timeout != 0)
169 // Cancel the timeout
170 Source.remove(button_press_timeout);
171 button_press_timeout = 0;
173 long diff = timediff(t, &last_down);
179 last_down.tv_sec = 0;
180 last_down.tv_usec = 0;
184 if (long_press) power_long_press();
185 if (short_press) power_short_press();
188 private void on_power_short_press()
191 // Short press: turn on backlight for a bit
194 // Short press: toggle power menu
198 private void on_power_long_press()
201 // Long press: unlock
202 set_screen_lock(false);
204 // Long press: lock screen
205 set_screen_lock(true);
209 public class BatteryIcon : Gtk.StatusIcon
211 public dynamic DBus.Object battery;
212 protected string last_status;
213 protected int last_capacity;
217 battery = zavai.registry.sbus.get_object(
218 "org.freesmartphone.odeviced",
219 "/org/freesmartphone/Device/PowerSupply/battery",
220 "org.freesmartphone.Device.PowerSupply");
222 // activate += on_activate;
224 battery.PowerStatus += on_power_status;
225 battery.Capacity += on_capacity;
227 last_status = battery.GetPowerStatus();
228 last_capacity = battery.GetCapacity();
233 private void on_power_status(dynamic DBus.Object bat, string status)
235 zavai.log.info("New battery status: " + status);
236 last_status = status;
240 private void on_capacity(dynamic DBus.Object bat, int val)
242 stderr.printf("NEW CAPACITY: %d\n", val);
248 private void on_activate()
253 protected void update_icon()
255 string name = zavai.config.icondir + "/battery/";
257 if (last_status == "charging")
258 name += "%02d0_charging_500.png".printf(last_capacity/10);
260 name += "%02d0.png".printf(last_capacity/10);
262 stderr.printf("Loading icon from %s\n", name);
268 public class ScreenLockButton : Gtk.Button
270 public ScreenLockButton()
272 label = "Lock screen";
273 clicked += on_clicked;
274 set_size_request(0, zavai.config.min_button_height);
277 public void on_clicked()
279 zavai.log.info("Locking screen");
280 power.set_screen_lock(true);
281 power_menu.hide_menu();
285 public class SuspendButton : Gtk.Button
287 public SuspendButton()
290 clicked += on_clicked;
291 set_size_request(0, zavai.config.min_button_height);
294 public void on_clicked()
296 zavai.log.info("Suspending the phone via FSO");
298 power_menu.hide_menu();
302 public class ShutdownButton : Gtk.Button
304 public ShutdownButton()
307 clicked += on_clicked;
308 set_size_request(0, zavai.config.min_button_height);
311 public void on_clicked()
313 zavai.log.info("Shutting down the phone via FSO");
315 power_menu.hide_menu();
319 public class RebootButton : Gtk.Button
321 public RebootButton()
324 clicked += on_clicked;
325 set_size_request(0, zavai.config.min_button_height);
328 public void on_clicked()
330 zavai.log.info("Rebooting the phone via FSO");
332 power_menu.hide_menu();
336 // For a list of dbus services, look in /etc/dbus-1/system.d/
337 public class Backlight: zavai.Service
339 public dynamic DBus.Object usage;
340 public dynamic DBus.Object display;
346 usage = zavai.registry.sbus.get_object(
347 "org.freesmartphone.ousaged",
348 "/org/freesmartphone/Usage",
349 "org.freesmartphone.Usage");
351 display = zavai.registry.sbus.get_object(
352 "org.freesmartphone.odeviced",
353 "/org/freesmartphone/Device/Display/0",
354 "org.freesmartphone.Device.Display");
357 // Turn the backlight and then let it fade off
360 // There must be a better method
362 usage.RequestResource("Display");
363 usage.ReleaseResource("Display");
365 zavai.log.error("Requesting/releasing resource Display: " + e.message);
369 public void turn_off_unless_needed()
374 display.SetBacklightPower(false);
375 } catch (GLib.Error e) {
376 zavai.log.error(e.message);
381 /// Request GPS resource
382 public override void start()
386 usage.RequestResource("Display");
387 zavai.log.info("Acquired display");
389 } catch (GLib.Error e) {
390 zavai.log.error(e.message);
395 // Release usage of GPS
396 public override void stop()
398 if (!started) return;
400 usage.ReleaseResource("Display");
401 zavai.log.info("Released display");
403 } catch (GLib.Error e) {
404 zavai.log.error(e.message);
410 public class PowerMenu : zavai.Resource, Gtk.Window
412 protected Gtk.VBox vbox;
413 protected ScreenLockButton act_screen_lock;
414 protected SuspendButton act_suspend;
415 protected ShutdownButton act_shutdown;
416 protected RebootButton act_reboot;
417 protected ServiceRequestLink act_backlight_on;
418 protected bool shown;
422 type = Gtk.WindowType.TOPLEVEL;
423 title = "Power Menu";
425 destroy_with_parent = true;
426 set_transient_for(zavai.app);
428 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
429 set_size_request(300, 500);
431 vbox = new Gtk.VBox(false, 0);
434 //destroy += Gtk.main_quit;
435 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
436 //visibility_notify_event += on_visibility;
437 set_skip_pager_hint(true);
438 set_skip_taskbar_hint(true);
439 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
441 act_screen_lock = new ScreenLockButton();
442 vbox.pack_start(act_screen_lock, false, false, 0);
444 act_suspend = new SuspendButton();
445 vbox.pack_start(act_suspend, false, false, 0);
447 act_shutdown = new ShutdownButton();
448 vbox.pack_start(act_shutdown, false, false, 0);
450 act_reboot = new RebootButton();
451 vbox.pack_start(act_reboot, false, false, 0);
453 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
454 act_backlight_on.toggled += (src) => { this.hide_menu(); };
455 vbox.pack_start(act_backlight_on, false, false, 0);
470 // TODO: do more in case it is visible but has no visibility (is covered by others)
477 public void hide_menu()
482 public void shutdown() {}
486 public class TogglePowerMenu : Gtk.Button
488 public TogglePowerMenu()
490 label = "Toggle power menu";
491 clicked += on_clicked;
492 set_size_request(0, zavai.config.min_button_height);
495 public void on_clicked()
497 zavai.log.info("Toggling power menu");
504 PowerMenu power_menu;
505 BatteryIcon battery_icon;
507 //TogglePowerMenu tpm;
512 backlight = new Backlight();
513 zavai.registry.register_service(backlight);
515 battery_icon = new BatteryIcon();
516 battery_icon.set_visible(true);
518 power_menu = new PowerMenu();
519 zavai.registry.register_resource("powermenu", power_menu);
521 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
522 //tpm = new TogglePowerMenu();
523 //zavai.registry.getmenu("menu.main").add_widget(tpm);
526 raise_icon = new RaiseIcon();
527 raise_icon.set_visible(true);
529 close_or_back = new CloseOrBack();
530 close_or_back.set_visible(true);
532 window_list = new WindowList("Current apps");
533 zavai.registry.register_applet("wm.list", window_list);
534 zavai.registry.getmenu("menu.main").add_applet("wm.list");
537 launcher = new Launcher("Run program");
539 zavai.log.error("Not running launcher: " + e.message);
543 if (launcher != null)
545 zavai.registry.register_applet("wm.launcher", launcher);
546 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");