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()
93 zavai.log.info("Suspend was done with ousaged.");
95 zavai.log.error("Suspending phone with ousaged: " + e.message);
102 // From http://lindi.iki.fi/lindi/openmoko/susp
104 gsm_device.Suspend();
106 zavai.log.error("Cannot tell GSM to suspend (but never mind): " + e.message);
108 // amixer -q -d sset "Amp Spk" mute
110 // echo 0 | sudo tee /proc/sysrq-trigger
112 // Limit the scope of state, so that it's
113 // closed before we resume
114 FileStream state = FileStream.open("/sys/power/state", "w");
121 // amixer -q -d sset "Amp Spk" unmute
125 zavai.log.error("Cannot tell GSM to resume (but never mind): " + e.message);
129 zavai.app.run_script("pm-suspend");
131 zavai.log.info("Suspend was done with zavai.");
133 zavai.log.error("Suspending phone: " + e.message);
137 public void do_shutdown()
141 zavai.app.run_script("shutdown -h now");
143 zavai.log.error("Shutting down phone: " + e.message);
146 public void do_reboot()
150 zavai.app.run_script("shutdown -r now");
152 zavai.log.error("Rebooting phone: " + e.message);
156 public void set_screen_lock(bool locked)
158 if (locked && screen_locked)
160 if (!locked && !screen_locked)
165 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
166 if (screen_lock_fd < 0)
168 zavai.log.error("Cannot open /dev/input/event1");
172 // FIXME: X won't see events, but it's still generating interrupts,
174 int EVIOCGRAB = 0x40044590;
175 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
177 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
178 Posix.close(screen_lock_fd);
182 backlight.lock_screen();
184 Posix.close(screen_lock_fd);
185 backlight.unlock_screen();
187 screen_locked = locked;
191 screen_lock_changed(locked);
194 private bool on_power_button_timeout()
196 last_down.tv_sec = 0;
197 last_down.tv_usec = 0;
203 private void on_power_button(Posix.timeval* t, bool pressed)
205 bool short_press = false;
206 bool long_press = false;
210 if (last_down.tv_sec == 0)
213 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
217 long diff = timediff(t, &last_down);
218 long_press = diff >= 1000000;
221 if (last_down.tv_sec == 0)
223 // Ignore: release has been simulated with the timeout
225 if (button_press_timeout != 0)
227 // Cancel the timeout
228 Source.remove(button_press_timeout);
229 button_press_timeout = 0;
231 long diff = timediff(t, &last_down);
237 last_down.tv_sec = 0;
238 last_down.tv_usec = 0;
245 last_short_press.tv_sec = 0;
246 last_short_press.tv_usec = 0;
248 if (short_press) power_short_press(t);
251 private void on_power_short_press(Posix.timeval* t)
253 long diff = timediff(t, &last_short_press);
254 bool combo = screen_locked && (diff <= 5000000);
255 last_short_press = *t;
259 // Short press: turn on backlight for a bit
264 app.toggle_visibility();
268 // Short press: toggle power menu
272 private void on_power_long_press()
275 // Long press: unlock
276 set_screen_lock(false);
278 // Long press: lock screen
279 set_screen_lock(true);
283 public class BatteryIcon : Gtk.StatusIcon
285 public Dkp.Device battery;
287 public BatteryIcon(Dkp.Device dev)
290 battery.changed += on_changed;
292 // 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);
297 private void on_changed(void* obj)
302 protected void update_icon()
304 string name = zavai.config.icondir + "/battery/";
305 Dkp.DeviceState state = (Dkp.DeviceState)battery.state;
307 //stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
308 int capacity = (int)Math.round(battery.percentage/10);
311 case Dkp.DeviceState.CHARGING:
312 name += "%02d0_charging_500.png".printf(capacity);
314 case Dkp.DeviceState.FULLY_CHARGED:
315 name += "100_charging_500.png";
317 case Dkp.DeviceState.UNKNOWN:
318 case Dkp.DeviceState.DISCHARGING:
319 case Dkp.DeviceState.EMPTY:
320 case Dkp.DeviceState.PENDING_CHARGE:
321 case Dkp.DeviceState.PENDING_DISCHARGE:
322 case Dkp.DeviceState.LAST:
323 name += "%02d0.png".printf(capacity);
327 //stderr.printf("Loading icon from %s\n", name);
333 public class ScreenLockButton : Gtk.Button
335 public ScreenLockButton()
337 label = "Lock screen";
338 clicked += on_clicked;
339 set_size_request(0, zavai.config.min_button_height);
342 public void on_clicked()
344 zavai.log.info("Locking screen");
345 power.set_screen_lock(true);
346 power_menu.hide_menu();
350 public class SuspendButton : Gtk.Button
352 public SuspendButton()
355 clicked += on_clicked;
356 set_size_request(0, zavai.config.min_button_height);
359 public void on_clicked()
361 zavai.log.info("Suspending the phone");
363 power_menu.hide_menu();
367 public class ShutdownButton : Gtk.Button
369 public ShutdownButton()
372 clicked += on_clicked;
373 set_size_request(0, zavai.config.min_button_height);
376 public void on_clicked()
378 zavai.log.info("Shutting down the phone");
380 power_menu.hide_menu();
384 public class RebootButton : Gtk.Button
386 public RebootButton()
389 clicked += on_clicked;
390 set_size_request(0, zavai.config.min_button_height);
393 public void on_clicked()
395 zavai.log.info("Rebooting the phone");
397 power_menu.hide_menu();
401 // For a list of dbus services, look in /etc/dbus-1/system.d/
402 public class Backlight: zavai.Service
406 Object(name: "backlight");
409 // Turn the backlight on and then let it fade off
413 zavai.app.run_script(zavai.config.homedir + "/display wiggle");
415 zavai.log.error("Requesting/releasing resource Display: " + e.message);
419 public void lock_screen()
424 zavai.app.run_script(zavai.config.homedir + "/display lock_off");
425 } catch (GLib.Error e) {
426 zavai.log.error(e.message);
431 public void unlock_screen()
434 zavai.app.run_script(zavai.config.homedir + "/display defaults");
435 } catch (GLib.Error e) {
436 zavai.log.error(e.message);
440 public override void start()
444 zavai.app.run_script(zavai.config.homedir + "/display lock_on");
445 zavai.log.info("Acquired display");
447 } catch (GLib.Error e) {
448 zavai.log.error(e.message);
453 public override void stop()
455 if (!started) return;
457 zavai.app.run_script(zavai.config.homedir + "/display defaults");
458 zavai.log.info("Released display");
460 } catch (GLib.Error e) {
461 zavai.log.error(e.message);
467 public class PowerMenu : zavai.Resource, Gtk.Window
469 protected Gtk.VBox vbox;
470 protected ScreenLockButton act_screen_lock;
471 protected SuspendButton act_suspend;
472 protected ShutdownButton act_shutdown;
473 protected RebootButton act_reboot;
474 protected ServiceRequestLink act_backlight_on;
475 protected bool shown;
480 type: Gtk.WindowType.TOPLEVEL,
484 destroy_with_parent = true;
485 set_transient_for(zavai.app);
487 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
488 set_size_request(300, 500);
490 vbox = new Gtk.VBox(false, 0);
493 //destroy += Gtk.main_quit;
494 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
495 //visibility_notify_event += on_visibility;
496 set_skip_pager_hint(true);
497 set_skip_taskbar_hint(true);
498 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
500 act_screen_lock = new ScreenLockButton();
501 vbox.pack_start(act_screen_lock, false, false, 0);
503 act_suspend = new SuspendButton();
504 vbox.pack_start(act_suspend, false, false, 0);
506 act_shutdown = new ShutdownButton();
507 vbox.pack_start(act_shutdown, false, false, 0);
509 act_reboot = new RebootButton();
510 vbox.pack_start(act_reboot, false, false, 0);
512 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
513 act_backlight_on.toggled += (src) => { this.hide_menu(); };
514 vbox.pack_start(act_backlight_on, false, false, 0);
529 // TODO: do more in case it is visible but has no visibility (is covered by others)
536 public void hide_menu()
541 public void shutdown() {}
545 public class TogglePowerMenu : Gtk.Button
547 public TogglePowerMenu()
549 label = "Toggle power menu";
550 clicked += on_clicked;
551 set_size_request(0, zavai.config.min_button_height);
554 public void on_clicked()
556 zavai.log.info("Toggling power menu");
563 PowerMenu power_menu;
564 Gee.ArrayList<BatteryIcon> battery_icons;
566 //TogglePowerMenu tpm;
571 backlight = new Backlight();
572 zavai.registry.register_service(backlight);
575 battery_icons = new Gee.ArrayList<BatteryIcon>();
576 // Enumerate batteries
577 var c = new Dkp.Client();
578 unowned GLib.PtrArray devs = c.enumerate_devices();
579 for (int i = 0; i < devs.len; ++i)
581 Dkp.Device dev = (Dkp.Device)devs.pdata[i];
582 stderr.printf("Found new device %s\n", dev.native_path);
584 stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
585 if (!dev.is_rechargeable) continue;
586 var bi = new BatteryIcon(dev);
587 bi.set_visible(true);
588 battery_icons.add(bi);
591 power_menu = new PowerMenu();
592 zavai.registry.register_resource("powermenu", power_menu);
594 stderr.printf("Creating power menu: %s\n", e.message);
598 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
599 //tpm = new TogglePowerMenu();
600 //zavai.registry.getmenu("menu.main").add_widget(tpm);
603 raise_icon = new RaiseIcon();
604 raise_icon.set_visible(true);
606 close_or_back = new CloseOrBack();
607 close_or_back.set_visible(true);
609 window_list = new WindowList("Current apps");
610 zavai.registry.register_applet("wm.list", window_list);
611 zavai.registry.getmenu("menu.main").add_applet("wm.list");
614 launcher = new Launcher("Run program");
616 zavai.log.error("Not running launcher: " + e.message);
620 if (launcher != null)
622 zavai.registry.register_applet("wm.launcher", launcher);
623 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");