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;
64 usage = zavai.registry.sbus.get_object(
65 "org.freesmartphone.ousaged",
66 "/org/freesmartphone/Usage",
67 "org.freesmartphone.Usage");
68 gsm_device = zavai.registry.sbus.get_object(
69 "org.freesmartphone.ogsmd",
70 "/org/freesmartphone/GSM/Device",
71 "org.freesmartphone.Resource");
74 zavai.input.power_button.power_button += on_power_button;
75 zavai.input.power_button.request("zavai.ui.powerbutton.power");
77 power_short_press += on_power_short_press;
78 power_long_press += on_power_long_press;
79 zavai.registry.register(this);
82 public void shutdown()
84 zavai.input.power_button.release("zavai.ui.powerbutton.power");
87 public void do_suspend()
96 zavai.log.info("Suspend was done with ousaged.");
98 zavai.log.error("Suspending phone with ousaged: " + e.message);
105 // From http://lindi.iki.fi/lindi/openmoko/susp
107 gsm_device.Suspend();
109 zavai.log.error("Cannot tell GSM to suspend (but never mind): " + e.message);
111 // amixer -q -d sset "Amp Spk" mute
113 // echo 0 | sudo tee /proc/sysrq-trigger
115 // Limit the scope of state, so that it's
116 // closed before we resume
117 FileStream state = FileStream.open("/sys/power/state", "w");
124 // amixer -q -d sset "Amp Spk" unmute
128 zavai.log.error("Cannot tell GSM to resume (but never mind): " + e.message);
132 zavai.config.run_script("pm-suspend");
134 zavai.log.info("Suspend was done with zavai.");
136 zavai.log.error("Suspending phone: " + e.message);
140 public void do_shutdown()
144 zavai.config.run_script("shutdown -h now");
146 zavai.log.error("Shutting down phone: " + e.message);
149 public void do_reboot()
153 zavai.config.run_script("shutdown -r now");
155 zavai.log.error("Rebooting phone: " + e.message);
159 public void set_screen_lock(bool locked)
161 if (locked && screen_locked)
163 if (!locked && !screen_locked)
168 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
169 if (screen_lock_fd < 0)
171 zavai.log.error("Cannot open /dev/input/event1");
175 // FIXME: X won't see events, but it's still generating interrupts,
177 if (Posix.ioctl(screen_lock_fd, LinuxInput.Evio.CGRAB, locked ? 1 : 0) != 0)
179 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
180 Posix.close(screen_lock_fd);
184 backlight.lock_screen();
186 Posix.close(screen_lock_fd);
187 backlight.unlock_screen();
189 screen_locked = locked;
193 screen_lock_changed(locked);
196 private bool on_power_button_timeout()
198 last_down.tv_sec = 0;
199 last_down.tv_usec = 0;
205 private void on_power_button(Posix.timeval* t, bool pressed)
207 bool short_press = false;
208 bool long_press = false;
212 if (last_down.tv_sec == 0)
215 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
219 long diff = timediff(t, &last_down);
220 long_press = diff >= 1000000;
223 if (last_down.tv_sec == 0)
225 // Ignore: release has been simulated with the timeout
227 if (button_press_timeout != 0)
229 // Cancel the timeout
230 Source.remove(button_press_timeout);
231 button_press_timeout = 0;
233 long diff = timediff(t, &last_down);
239 last_down.tv_sec = 0;
240 last_down.tv_usec = 0;
247 last_short_press.tv_sec = 0;
248 last_short_press.tv_usec = 0;
250 if (short_press) power_short_press(t);
253 private void on_power_short_press(Posix.timeval* t)
255 long diff = timediff(t, &last_short_press);
256 bool combo = screen_locked && (diff <= 5000000);
257 last_short_press = *t;
261 // Short press: turn on backlight for a bit
266 app.toggle_visibility();
270 // Short press: toggle power menu
274 private void on_power_long_press()
277 // Long press: unlock
278 set_screen_lock(false);
280 // Long press: lock screen
281 set_screen_lock(true);
286 public class BatteryIcon : Gtk.StatusIcon
288 public Dkp.Device battery;
290 public BatteryIcon(Dkp.Device dev)
293 battery.changed += on_changed;
295 // 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);
300 private void on_changed(void* obj)
305 protected void update_icon()
307 string name = zavai.config.icondir + "/battery/";
308 Dkp.DeviceState state = (Dkp.DeviceState)battery.state;
310 //stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
311 int capacity = (int)Math.round(battery.percentage/10);
314 case Dkp.DeviceState.CHARGING:
315 name += "%02d0_charging_500.png".printf(capacity);
317 case Dkp.DeviceState.FULLY_CHARGED:
318 name += "100_charging_500.png";
320 case Dkp.DeviceState.UNKNOWN:
321 case Dkp.DeviceState.DISCHARGING:
322 case Dkp.DeviceState.EMPTY:
323 case Dkp.DeviceState.PENDING_CHARGE:
324 case Dkp.DeviceState.PENDING_DISCHARGE:
325 case Dkp.DeviceState.LAST:
326 name += "%02d0.png".printf(capacity);
330 //stderr.printf("Loading icon from %s\n", name);
334 public static List<BatteryIcon> create_icons()
336 List<BatteryIcon> battery_icons = new List<BatteryIcon>();
338 // Enumerate batteries
339 var c = new Dkp.Client();
340 unowned GLib.PtrArray devs = c.enumerate_devices();
341 for (int i = 0; i < devs.len; ++i)
343 Dkp.Device dev = (Dkp.Device)devs.pdata[i];
344 stderr.printf("Found new device %s\n", dev.native_path);
346 stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
347 if (!dev.is_rechargeable) continue;
348 var bi = new BatteryIcon(dev);
349 bi.set_visible(true);
350 battery_icons.append(bi);
353 return battery_icons;
357 public class BatteryIcon : Gtk.StatusIcon
361 zavai.power.power.changed += on_changed;
362 zavai.power.power.request("zavai.ui.batteryicon");
364 // 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);
369 private void on_changed()
374 protected void update_icon()
376 string name = zavai.config.icondir + "/battery/";
378 //stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
379 int capacity = (int)Math.round(zavai.power.power.percentage/10);
380 switch (zavai.power.power.state)
382 case zavai.power.Power.State.CHARGING:
383 name += "%02d0_charging_500.png".printf(capacity);
385 case zavai.power.Power.State.FULLY_CHARGED:
386 name += "100_charging_500.png";
389 name += "%02d0.png".printf(capacity);
393 //stderr.printf("Loading icon from %s\n", name);
397 public static List<BatteryIcon> create_icons()
399 List<BatteryIcon> battery_icons = new List<BatteryIcon>();
400 var bi = new BatteryIcon();
401 battery_icons.append(bi);
402 bi.set_visible(true);
403 return battery_icons;
408 public class ScreenLockButton : Gtk.Button
410 public ScreenLockButton()
412 label = "Lock screen";
413 clicked += on_clicked;
414 set_size_request(0, zavai.config.min_button_height);
417 public void on_clicked()
419 zavai.log.info("Locking screen");
420 power.set_screen_lock(true);
421 power_menu.hide_menu();
425 public class SuspendButton : Gtk.Button
427 public SuspendButton()
430 clicked += on_clicked;
431 set_size_request(0, zavai.config.min_button_height);
434 public void on_clicked()
436 zavai.log.info("Suspending the phone");
438 power_menu.hide_menu();
442 public class ShutdownButton : Gtk.Button
444 public ShutdownButton()
447 clicked += on_clicked;
448 set_size_request(0, zavai.config.min_button_height);
451 public void on_clicked()
453 zavai.log.info("Shutting down the phone");
455 power_menu.hide_menu();
459 public class RebootButton : Gtk.Button
461 public RebootButton()
464 clicked += on_clicked;
465 set_size_request(0, zavai.config.min_button_height);
468 public void on_clicked()
470 zavai.log.info("Rebooting the phone");
472 power_menu.hide_menu();
476 // For a list of dbus services, look in /etc/dbus-1/system.d/
477 public class Backlight: zavai.Service
481 Object(name: "backlight");
484 // Turn the backlight on and then let it fade off
488 zavai.config.find_and_run_script("display", "wiggle");
490 zavai.log.error("Requesting/releasing resource Display: " + e.message);
494 public void lock_screen()
499 zavai.config.find_and_run_script("display", "lock_off");
500 } catch (GLib.Error e) {
501 zavai.log.error(e.message);
506 public void unlock_screen()
509 zavai.config.find_and_run_script("display", "defaults");
510 } catch (GLib.Error e) {
511 zavai.log.error(e.message);
515 public override void start()
519 zavai.config.find_and_run_script("display", "lock_on");
520 zavai.log.info("Acquired display");
522 } catch (GLib.Error e) {
523 zavai.log.error(e.message);
528 public override void stop()
530 if (!started) return;
532 zavai.config.find_and_run_script("display", "defaults");
533 zavai.log.info("Released display");
535 } catch (GLib.Error e) {
536 zavai.log.error(e.message);
542 public class BrightnessAdjustment : Gtk.Adjustment
544 public BrightnessAdjustment()
547 upper = Omhacks.Screen.Brightness.get_max();
548 value = Omhacks.Screen.Brightness.get();
550 page_increment = upper/10;
551 page_size = upper/10;
552 value_changed += on_value_changed;
556 zavai.config.backlight_max/2,
557 0, zavai.config.backlight_max,
558 1, zavai.config.backlight_max/10, zavai.config.backlight_max/10);
562 protected void on_value_changed()
564 Omhacks.Screen.Brightness.set((int)value);
568 public class PowerMenu : zavai.Resource, Gtk.Window
570 protected Gtk.VBox vbox;
571 protected Gtk.HBox hbox;
572 protected ScreenLockButton act_screen_lock;
573 protected SuspendButton act_suspend;
574 protected ShutdownButton act_shutdown;
575 protected RebootButton act_reboot;
576 protected ServiceRequestLink act_backlight_on;
577 protected Gtk.VScrollbar bscroll;
578 protected bool shown;
583 type: Gtk.WindowType.TOPLEVEL,
587 destroy_with_parent = true;
588 set_transient_for(zavai.app);
590 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
591 set_size_request(300, 500);
593 hbox = new Gtk.HBox(false, 0);
596 vbox = new Gtk.VBox(false, 0);
597 hbox.pack_start(vbox, true, true, 0);
599 bscroll = new Gtk.VScrollbar(brightness);
600 bscroll.inverted = true;
601 hbox.pack_start(bscroll, false, false, 0);
603 //destroy += Gtk.main_quit;
604 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
605 //visibility_notify_event += on_visibility;
606 set_skip_pager_hint(true);
607 set_skip_taskbar_hint(true);
608 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
610 act_screen_lock = new ScreenLockButton();
611 vbox.pack_start(act_screen_lock, false, false, 0);
613 act_suspend = new SuspendButton();
614 vbox.pack_start(act_suspend, false, false, 0);
616 act_shutdown = new ShutdownButton();
617 vbox.pack_start(act_shutdown, false, false, 0);
619 act_reboot = new RebootButton();
620 vbox.pack_start(act_reboot, false, false, 0);
622 act_backlight_on = new ServiceRequestLink(backlight, "Keep backlight on", "Let backlight fade");
623 act_backlight_on.toggled += (src) => { this.hide_menu(); };
624 vbox.pack_start(act_backlight_on, false, false, 0);
627 zavai.registry.register(this);
640 // TODO: do more in case it is visible but has no visibility (is covered by others)
647 public void hide_menu()
652 public void shutdown() {}
656 public class TogglePowerMenu : Gtk.Button
658 public TogglePowerMenu()
660 label = "Toggle power menu";
661 clicked += on_clicked;
662 set_size_request(0, zavai.config.min_button_height);
665 public void on_clicked()
667 zavai.log.info("Toggling power menu");
674 PowerMenu power_menu;
675 List<BatteryIcon> battery_icons;
677 BrightnessAdjustment brightness;
678 //TogglePowerMenu tpm;
683 backlight = new Backlight();
684 brightness = new BrightnessAdjustment();
687 battery_icons = BatteryIcon.create_icons();
689 stderr.printf("Creating power menu: %s\n", e.message);
692 power_menu = new PowerMenu();
694 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
695 //tpm = new TogglePowerMenu();
696 //zavai.registry.getmenu("menu.main").add_widget(tpm);
699 raise_icon = new RaiseIcon();
700 raise_icon.set_visible(true);
702 close_or_back = new CloseOrBack();
703 close_or_back.set_visible(true);
705 window_list = new WindowList("Current apps");
706 zavai.registry.register_applet("wm.list", window_list);
707 zavai.registry.getmenu("menu.main").add_applet("wm.list");
710 launcher = new Launcher("Run program");
712 zavai.log.error("Not running launcher: " + e.message);
716 if (launcher != null)
718 zavai.registry.register_applet("wm.launcher", launcher);
719 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");