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;
41 private Posix.timeval last_short_press;
43 private bool hide_after_closing_power_menu;
45 public signal void screen_lock_changed(bool state);
47 public signal void power_short_press(Posix.timeval* t);
48 public signal void power_long_press();
49 private uint button_press_timeout;
53 screen_locked = false;
55 hide_after_closing_power_menu = false;
57 last_down.tv_usec = 0;
58 last_short_press.tv_sec = 0;
59 last_short_press.tv_usec = 0;
60 button_press_timeout = 0;
62 usage = zavai.registry.sbus.get_object(
63 "org.freesmartphone.ousaged",
64 "/org/freesmartphone/Usage",
65 "org.freesmartphone.Usage");
67 zavai.input.power_button.power_button += on_power_button;
68 zavai.input.power_button.request("zavai.ui.powerbutton.power");
70 power_short_press += on_power_short_press;
71 power_long_press += on_power_long_press;
74 public void shutdown()
76 zavai.input.power_button.release("zavai.ui.powerbutton.power");
79 public void do_suspend()
84 zavai.log.error("Suspending phone: " + e.message);
87 public void do_shutdown()
91 zavai.app.run_script("shutdown -h now");
93 zavai.log.error("Shutting down phone: " + e.message);
96 public void do_reboot()
100 zavai.app.run_script("shutdown -r now");
102 zavai.log.error("Rebooting phone: " + e.message);
106 public void set_screen_lock(bool locked)
108 if (locked && screen_locked)
110 if (!locked && !screen_locked)
115 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
116 if (screen_lock_fd < 0)
118 zavai.log.error("Cannot open /dev/input/event1");
122 // FIXME: X won't see events, but it's still generating interrupts,
124 int EVIOCGRAB = 0x40044590;
125 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
127 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
128 Posix.close(screen_lock_fd);
132 backlight.lock_screen();
134 Posix.close(screen_lock_fd);
135 backlight.unlock_screen();
137 screen_locked = locked;
141 screen_lock_changed(locked);
144 private bool on_power_button_timeout()
146 last_down.tv_sec = 0;
147 last_down.tv_usec = 0;
153 private void on_power_button(Posix.timeval* t, bool pressed)
155 bool short_press = false;
156 bool long_press = false;
160 if (last_down.tv_sec == 0)
163 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
167 long diff = timediff(t, &last_down);
168 long_press = diff >= 1000000;
171 if (last_down.tv_sec == 0)
173 // Ignore: release has been simulated with the timeout
175 if (button_press_timeout != 0)
177 // Cancel the timeout
178 Source.remove(button_press_timeout);
179 button_press_timeout = 0;
181 long diff = timediff(t, &last_down);
187 last_down.tv_sec = 0;
188 last_down.tv_usec = 0;
195 last_short_press.tv_sec = 0;
196 last_short_press.tv_usec = 0;
198 if (short_press) power_short_press(t);
201 private void on_power_short_press(Posix.timeval* t)
203 long diff = timediff(t, &last_short_press);
204 bool combo = screen_locked && (diff <= 5000000);
205 last_short_press = *t;
209 // Short press: turn on backlight for a bit
214 app.toggle_visibility();
218 // Short press: toggle power menu
222 private void on_power_long_press()
225 // Long press: unlock
226 set_screen_lock(false);
228 // Long press: lock screen
229 set_screen_lock(true);
233 public class BatteryIcon : Gtk.StatusIcon
235 public dynamic DBus.Object battery;
236 protected string last_status;
237 protected int last_capacity;
241 battery = zavai.registry.sbus.get_object(
242 "org.freesmartphone.odeviced",
243 "/org/freesmartphone/Device/PowerSupply/battery",
244 "org.freesmartphone.Device.PowerSupply");
246 // activate += on_activate;
248 battery.PowerStatus += on_power_status;
249 battery.Capacity += on_capacity;
251 last_status = battery.GetPowerStatus();
252 last_capacity = battery.GetCapacity();
257 private void on_power_status(dynamic DBus.Object bat, string status)
259 zavai.log.info("New battery status: " + status);
260 last_status = status;
264 private void on_capacity(dynamic DBus.Object bat, int val)
266 stderr.printf("NEW CAPACITY: %d\n", val);
272 private void on_activate()
277 protected void update_icon()
279 string name = zavai.config.icondir + "/battery/";
281 if (last_status == "charging")
282 name += "%02d0_charging_500.png".printf(last_capacity/10);
284 name += "%02d0.png".printf(last_capacity/10);
286 stderr.printf("Loading icon from %s\n", name);
292 public class ScreenLockButton : Gtk.Button
294 public ScreenLockButton()
296 label = "Lock screen";
297 clicked += on_clicked;
298 set_size_request(0, zavai.config.min_button_height);
301 public void on_clicked()
303 zavai.log.info("Locking screen");
304 power.set_screen_lock(true);
305 power_menu.hide_menu();
309 public class SuspendButton : Gtk.Button
311 public SuspendButton()
314 clicked += on_clicked;
315 set_size_request(0, zavai.config.min_button_height);
318 public void on_clicked()
320 zavai.log.info("Suspending the phone via FSO");
322 power_menu.hide_menu();
326 public class ShutdownButton : Gtk.Button
328 public ShutdownButton()
331 clicked += on_clicked;
332 set_size_request(0, zavai.config.min_button_height);
335 public void on_clicked()
337 zavai.log.info("Shutting down the phone via FSO");
339 power_menu.hide_menu();
343 public class RebootButton : Gtk.Button
345 public RebootButton()
348 clicked += on_clicked;
349 set_size_request(0, zavai.config.min_button_height);
352 public void on_clicked()
354 zavai.log.info("Rebooting the phone via FSO");
356 power_menu.hide_menu();
360 // For a list of dbus services, look in /etc/dbus-1/system.d/
361 public class Backlight: zavai.Service
363 public dynamic DBus.Object usage;
369 usage = zavai.registry.sbus.get_object(
370 "org.freesmartphone.ousaged",
371 "/org/freesmartphone/Usage",
372 "org.freesmartphone.Usage");
375 // Turn the backlight and then let it fade off
378 // There must be a better method
380 //display.SetBacklightPower(true);
381 //usage.SetResourcePolicy("Display", "auto");
382 //usage.RequestResource("Display");
383 //usage.ReleaseResource("Display");
384 zavai.app.run_script(zavai.config.homedir + "/display wiggle");
386 zavai.log.error("Requesting/releasing resource Display: " + e.message);
390 public void lock_screen()
395 //display.SetBacklightPower(false);
396 zavai.app.run_script(zavai.config.homedir + "/display lock_off");
398 string policy = usage.GetResourcePolicy("Display");
399 if (policy == "auto")
401 usage.SetResourcePolicy("Display", "disabled");
404 } catch (GLib.Error e) {
405 zavai.log.error(e.message);
410 public void unlock_screen()
413 //display.SetBacklightPower(true);
414 zavai.app.run_script(zavai.config.homedir + "/display defaults");
415 //usage.SetResourcePolicy("Display", "auto");
416 } catch (GLib.Error e) {
417 zavai.log.error(e.message);
422 /// Request GPS resource
423 public override void start()
427 //usage.RequestResource("Display");
428 zavai.app.run_script(zavai.config.homedir + "/display lock_on");
429 zavai.log.info("Acquired display");
431 } catch (GLib.Error e) {
432 zavai.log.error(e.message);
437 // Release usage of GPS
438 public override void stop()
440 if (!started) return;
442 //usage.ReleaseResource("Display");
443 zavai.app.run_script(zavai.config.homedir + "/display defaults");
444 zavai.log.info("Released display");
446 } catch (GLib.Error e) {
447 zavai.log.error(e.message);
453 public class PowerMenu : zavai.Resource, Gtk.Window
455 protected Gtk.VBox vbox;
456 protected ScreenLockButton act_screen_lock;
457 protected SuspendButton act_suspend;
458 protected ShutdownButton act_shutdown;
459 protected RebootButton act_reboot;
460 protected ServiceRequestLink act_backlight_on;
461 protected bool shown;
465 type = Gtk.WindowType.TOPLEVEL;
466 title = "Power Menu";
468 destroy_with_parent = true;
469 set_transient_for(zavai.app);
471 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
472 set_size_request(300, 500);
474 vbox = new Gtk.VBox(false, 0);
477 //destroy += Gtk.main_quit;
478 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
479 //visibility_notify_event += on_visibility;
480 set_skip_pager_hint(true);
481 set_skip_taskbar_hint(true);
482 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
484 act_screen_lock = new ScreenLockButton();
485 vbox.pack_start(act_screen_lock, false, false, 0);
487 act_suspend = new SuspendButton();
488 vbox.pack_start(act_suspend, false, false, 0);
490 act_shutdown = new ShutdownButton();
491 vbox.pack_start(act_shutdown, false, false, 0);
493 act_reboot = new RebootButton();
494 vbox.pack_start(act_reboot, false, false, 0);
496 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
497 act_backlight_on.toggled += (src) => { this.hide_menu(); };
498 vbox.pack_start(act_backlight_on, false, false, 0);
513 // TODO: do more in case it is visible but has no visibility (is covered by others)
520 public void hide_menu()
525 public void shutdown() {}
529 public class TogglePowerMenu : Gtk.Button
531 public TogglePowerMenu()
533 label = "Toggle power menu";
534 clicked += on_clicked;
535 set_size_request(0, zavai.config.min_button_height);
538 public void on_clicked()
540 zavai.log.info("Toggling power menu");
547 PowerMenu power_menu;
548 BatteryIcon battery_icon;
550 //TogglePowerMenu tpm;
555 backlight = new Backlight();
556 zavai.registry.register_service(backlight);
558 battery_icon = new BatteryIcon();
559 battery_icon.set_visible(true);
561 power_menu = new PowerMenu();
562 zavai.registry.register_resource("powermenu", power_menu);
564 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
565 //tpm = new TogglePowerMenu();
566 //zavai.registry.getmenu("menu.main").add_widget(tpm);
569 raise_icon = new RaiseIcon();
570 raise_icon.set_visible(true);
572 close_or_back = new CloseOrBack();
573 close_or_back.set_visible(true);
575 window_list = new WindowList("Current apps");
576 zavai.registry.register_applet("wm.list", window_list);
577 zavai.registry.getmenu("menu.main").add_applet("wm.list");
580 launcher = new Launcher("Run program");
582 zavai.log.error("Not running launcher: " + e.message);
586 if (launcher != null)
588 zavai.registry.register_applet("wm.launcher", launcher);
589 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");