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 dynamic DBus.Object gsm_device;
37 public bool screen_locked;
38 private int screen_lock_fd;
39 // Timestamp of the past power button pressed even (0 if the button has
41 private Posix.timeval last_down;
42 private Posix.timeval last_short_press;
44 private bool hide_after_closing_power_menu;
46 public signal void screen_lock_changed(bool state);
48 public signal void power_short_press(Posix.timeval* t);
49 public signal void power_long_press();
50 private uint button_press_timeout;
54 screen_locked = false;
56 hide_after_closing_power_menu = false;
58 last_down.tv_usec = 0;
59 last_short_press.tv_sec = 0;
60 last_short_press.tv_usec = 0;
61 button_press_timeout = 0;
63 usage = zavai.registry.sbus.get_object(
64 "org.freesmartphone.ousaged",
65 "/org/freesmartphone/Usage",
66 "org.freesmartphone.Usage");
67 gsm_device = zavai.registry.sbus.get_object(
68 "org.freesmartphone.ogsmd",
69 "/org/freesmartphone/GSM/Device",
70 "org.freesmartphone.Resource");
72 zavai.input.power_button.power_button += on_power_button;
73 zavai.input.power_button.request("zavai.ui.powerbutton.power");
75 power_short_press += on_power_short_press;
76 power_long_press += on_power_long_press;
79 public void shutdown()
81 zavai.input.power_button.release("zavai.ui.powerbutton.power");
84 public void do_suspend()
92 zavai.log.info("Suspend was done with ousaged.");
94 zavai.log.error("Suspending phone with ousaged: " + e.message);
99 // From http://lindi.iki.fi/lindi/openmoko/susp
101 gsm_device.Suspend();
103 zavai.log.error("Cannot tell GSM to suspend (but never mind): " + e.message);
105 // amixer -q -d sset "Amp Spk" mute
107 // echo 0 | sudo tee /proc/sysrq-trigger
109 // Limit the scope of state, so that it's
110 // closed before we resume
111 FileStream state = FileStream.open("/sys/power/state", "w");
118 // amixer -q -d sset "Amp Spk" unmute
122 zavai.log.error("Cannot tell GSM to resume (but never mind): " + e.message);
125 zavai.log.info("Suspend was done with zavai.");
128 public void do_shutdown()
132 zavai.app.run_script("shutdown -h now");
134 zavai.log.error("Shutting down phone: " + e.message);
137 public void do_reboot()
141 zavai.app.run_script("shutdown -r now");
143 zavai.log.error("Rebooting phone: " + e.message);
147 public void set_screen_lock(bool locked)
149 if (locked && screen_locked)
151 if (!locked && !screen_locked)
156 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
157 if (screen_lock_fd < 0)
159 zavai.log.error("Cannot open /dev/input/event1");
163 // FIXME: X won't see events, but it's still generating interrupts,
165 int EVIOCGRAB = 0x40044590;
166 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
168 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
169 Posix.close(screen_lock_fd);
173 backlight.lock_screen();
175 Posix.close(screen_lock_fd);
176 backlight.unlock_screen();
178 screen_locked = locked;
182 screen_lock_changed(locked);
185 private bool on_power_button_timeout()
187 last_down.tv_sec = 0;
188 last_down.tv_usec = 0;
194 private void on_power_button(Posix.timeval* t, bool pressed)
196 bool short_press = false;
197 bool long_press = false;
201 if (last_down.tv_sec == 0)
204 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
208 long diff = timediff(t, &last_down);
209 long_press = diff >= 1000000;
212 if (last_down.tv_sec == 0)
214 // Ignore: release has been simulated with the timeout
216 if (button_press_timeout != 0)
218 // Cancel the timeout
219 Source.remove(button_press_timeout);
220 button_press_timeout = 0;
222 long diff = timediff(t, &last_down);
228 last_down.tv_sec = 0;
229 last_down.tv_usec = 0;
236 last_short_press.tv_sec = 0;
237 last_short_press.tv_usec = 0;
239 if (short_press) power_short_press(t);
242 private void on_power_short_press(Posix.timeval* t)
244 long diff = timediff(t, &last_short_press);
245 bool combo = screen_locked && (diff <= 5000000);
246 last_short_press = *t;
250 // Short press: turn on backlight for a bit
255 app.toggle_visibility();
259 // Short press: toggle power menu
263 private void on_power_long_press()
266 // Long press: unlock
267 set_screen_lock(false);
269 // Long press: lock screen
270 set_screen_lock(true);
274 public class BatteryIcon : Gtk.StatusIcon
276 public Dkp.Device battery;
278 public BatteryIcon(Dkp.Device dev)
281 battery.changed += on_changed;
283 stderr.printf("New battery icon for %s online %s perc %f isrec %s tte %lld ttf %lld\n", dev.native_path, dev.online ? "yes" : "no", dev.percentage, dev.is_rechargeable ? "yes" : "no", dev.time_to_empty, dev.time_to_full);
288 private void on_changed(void* obj)
293 protected void update_icon()
295 string name = zavai.config.icondir + "/battery/";
296 Dkp.DeviceState state = (Dkp.DeviceState)battery.state;
298 stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
299 int capacity = (int)Math.round(battery.percentage/10);
302 case Dkp.DeviceState.CHARGING:
303 name += "%02d0_charging_500.png".printf(capacity);
305 case Dkp.DeviceState.FULLY_CHARGED:
306 name += "100_charging_500.png";
308 case Dkp.DeviceState.UNKNOWN:
309 case Dkp.DeviceState.DISCHARGING:
310 case Dkp.DeviceState.EMPTY:
311 case Dkp.DeviceState.PENDING_CHARGE:
312 case Dkp.DeviceState.PENDING_DISCHARGE:
313 case Dkp.DeviceState.LAST:
314 name += "%02d0.png".printf(capacity);
318 stderr.printf("Loading icon from %s\n", name);
324 public class ScreenLockButton : Gtk.Button
326 public ScreenLockButton()
328 label = "Lock screen";
329 clicked += on_clicked;
330 set_size_request(0, zavai.config.min_button_height);
333 public void on_clicked()
335 zavai.log.info("Locking screen");
336 power.set_screen_lock(true);
337 power_menu.hide_menu();
341 public class SuspendButton : Gtk.Button
343 public SuspendButton()
346 clicked += on_clicked;
347 set_size_request(0, zavai.config.min_button_height);
350 public void on_clicked()
352 zavai.log.info("Suspending the phone");
354 power_menu.hide_menu();
358 public class ShutdownButton : Gtk.Button
360 public ShutdownButton()
363 clicked += on_clicked;
364 set_size_request(0, zavai.config.min_button_height);
367 public void on_clicked()
369 zavai.log.info("Shutting down the phone");
371 power_menu.hide_menu();
375 public class RebootButton : Gtk.Button
377 public RebootButton()
380 clicked += on_clicked;
381 set_size_request(0, zavai.config.min_button_height);
384 public void on_clicked()
386 zavai.log.info("Rebooting the phone");
388 power_menu.hide_menu();
392 // For a list of dbus services, look in /etc/dbus-1/system.d/
393 public class Backlight: zavai.Service
400 // Turn the backlight on and then let it fade off
404 zavai.app.run_script(zavai.config.homedir + "/display wiggle");
406 zavai.log.error("Requesting/releasing resource Display: " + e.message);
410 public void lock_screen()
415 zavai.app.run_script(zavai.config.homedir + "/display lock_off");
416 } catch (GLib.Error e) {
417 zavai.log.error(e.message);
422 public void unlock_screen()
425 zavai.app.run_script(zavai.config.homedir + "/display defaults");
426 } catch (GLib.Error e) {
427 zavai.log.error(e.message);
431 public override void start()
435 zavai.app.run_script(zavai.config.homedir + "/display lock_on");
436 zavai.log.info("Acquired display");
438 } catch (GLib.Error e) {
439 zavai.log.error(e.message);
444 public override void stop()
446 if (!started) return;
448 zavai.app.run_script(zavai.config.homedir + "/display defaults");
449 zavai.log.info("Released display");
451 } catch (GLib.Error e) {
452 zavai.log.error(e.message);
458 public class PowerMenu : zavai.Resource, Gtk.Window
460 protected Gtk.VBox vbox;
461 protected ScreenLockButton act_screen_lock;
462 protected SuspendButton act_suspend;
463 protected ShutdownButton act_shutdown;
464 protected RebootButton act_reboot;
465 protected ServiceRequestLink act_backlight_on;
466 protected bool shown;
470 type = Gtk.WindowType.TOPLEVEL;
471 title = "Power Menu";
473 destroy_with_parent = true;
474 set_transient_for(zavai.app);
476 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
477 set_size_request(300, 500);
479 vbox = new Gtk.VBox(false, 0);
482 //destroy += Gtk.main_quit;
483 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
484 //visibility_notify_event += on_visibility;
485 set_skip_pager_hint(true);
486 set_skip_taskbar_hint(true);
487 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
489 act_screen_lock = new ScreenLockButton();
490 vbox.pack_start(act_screen_lock, false, false, 0);
492 act_suspend = new SuspendButton();
493 vbox.pack_start(act_suspend, false, false, 0);
495 act_shutdown = new ShutdownButton();
496 vbox.pack_start(act_shutdown, false, false, 0);
498 act_reboot = new RebootButton();
499 vbox.pack_start(act_reboot, false, false, 0);
501 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
502 act_backlight_on.toggled += (src) => { this.hide_menu(); };
503 vbox.pack_start(act_backlight_on, false, false, 0);
518 // TODO: do more in case it is visible but has no visibility (is covered by others)
525 public void hide_menu()
530 public void shutdown() {}
534 public class TogglePowerMenu : Gtk.Button
536 public TogglePowerMenu()
538 label = "Toggle power menu";
539 clicked += on_clicked;
540 set_size_request(0, zavai.config.min_button_height);
543 public void on_clicked()
545 zavai.log.info("Toggling power menu");
552 PowerMenu power_menu;
553 Gee.ArrayList<BatteryIcon> battery_icons;
555 //TogglePowerMenu tpm;
560 backlight = new Backlight();
561 zavai.registry.register_service(backlight);
563 battery_icons = new Gee.ArrayList<BatteryIcon>();
564 // Enumerate batteries
565 var c = new Dkp.Client();
566 unowned GLib.PtrArray devs = c.enumerate_devices();
567 for (int i = 0; i < devs.len; ++i)
569 Dkp.Device dev = (Dkp.Device)devs.pdata[i];
570 stderr.printf("Found new device %s\n", dev.native_path);
572 stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
573 if (!dev.is_rechargeable) continue;
574 var bi = new BatteryIcon(dev);
575 bi.set_visible(true);
576 battery_icons.add(bi);
579 power_menu = new PowerMenu();
580 zavai.registry.register_resource("powermenu", power_menu);
582 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
583 //tpm = new TogglePowerMenu();
584 //zavai.registry.getmenu("menu.main").add_widget(tpm);
587 raise_icon = new RaiseIcon();
588 raise_icon.set_visible(true);
590 close_or_back = new CloseOrBack();
591 close_or_back.set_visible(true);
593 window_list = new WindowList("Current apps");
594 zavai.registry.register_applet("wm.list", window_list);
595 zavai.registry.getmenu("menu.main").add_applet("wm.list");
598 launcher = new Launcher("Run program");
600 zavai.log.error("Not running launcher: " + e.message);
604 if (launcher != null)
606 zavai.registry.register_applet("wm.launcher", launcher);
607 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");