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);
48 screen_locked = false;
50 hide_after_closing_power_menu = false;
52 last_down.tv_usec = 0;
54 usage = zavai.registry.sbus.get_object(
55 "org.freesmartphone.ousaged",
56 "/org/freesmartphone/Usage",
57 "org.freesmartphone.Usage");
59 zavai.input.power_button.power_button += on_power_button;
60 zavai.input.power_button.request("zavai.ui.powerbutton.power");
63 public void shutdown()
65 zavai.input.power_button.release("zavai.ui.powerbutton.power");
68 public void do_suspend() { usage.Suspend(); }
69 public void do_shutdown() { usage.Shutdown(); }
70 public void do_reboot() { usage.Reboot(); }
72 public void set_screen_lock(bool locked)
74 if (locked && screen_locked)
76 if (!locked && !screen_locked)
81 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
82 if (screen_lock_fd < 0)
84 zavai.log.error("Cannot open /dev/input/event1");
88 int EVIOCGRAB = 0x40044590;
89 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
91 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
92 Posix.close(screen_lock_fd);
96 Posix.close(screen_lock_fd);
98 screen_locked = locked;
102 screen_lock_changed(locked);
105 private void on_power_button(Posix.timeval* t, bool pressed)
107 bool short_press = false;
108 bool long_press = false;
110 stderr.printf("EVENT %d\n", (int)pressed);
114 if (last_down.tv_sec == 0)
116 stderr.printf(" FIRST PRESSED\n");
121 long diff = timediff(t, &last_down);
122 stderr.printf(" PRESSED FOR %ld\n", diff);
123 long_press = diff >= 1500000;
126 long diff = timediff(t, &last_down);
127 stderr.printf(" RELEASED AFTER %ld\n", diff);
133 last_down.tv_sec = 0;
134 last_down.tv_usec = 0;
137 stderr.printf(" LP %d SP %d\n", (int)long_press, (int)short_press);
141 // Short press: turn on backlight for a bit
144 // Short press: toggle power menu
146 } else if (long_press) {
148 // Long press: unlock
149 set_screen_lock(false);
151 // Long press: lock screen
152 set_screen_lock(true);
157 public class BatteryIcon : Gtk.StatusIcon
159 public dynamic DBus.Object battery;
160 protected string last_status;
161 protected int last_capacity;
165 battery = zavai.registry.sbus.get_object(
166 "org.freesmartphone.odeviced",
167 "/org/freesmartphone/Device/PowerSupply/battery",
168 "org.freesmartphone.Device.PowerSupply");
170 // activate += on_activate;
172 battery.PowerStatus += on_power_status;
173 battery.Capacity += on_capacity;
175 last_status = battery.GetPowerStatus();
176 last_capacity = battery.GetCapacity();
181 private void on_power_status(dynamic DBus.Object bat, string status)
183 zavai.log.info("New battery status: " + status);
184 last_status = status;
188 private void on_capacity(dynamic DBus.Object bat, int val)
190 stderr.printf("NEW CAPACITY: %d\n", val);
196 private void on_activate()
201 protected void update_icon()
203 string name = zavai.config.icondir + "/battery/";
205 if (last_status == "charging")
206 name += "%02d0_charging_500.png".printf(last_capacity/10);
208 name += "%02d0.png".printf(last_capacity/10);
210 stderr.printf("Loading icon from %s\n", name);
216 public class ScreenLockButton : Gtk.Button
218 public ScreenLockButton()
220 label = "Lock screen";
221 clicked += on_clicked;
222 set_size_request(0, zavai.config.min_button_height);
225 public void on_clicked()
227 zavai.log.info("Locking screen");
228 power.set_screen_lock(true);
233 public class SuspendButton : Gtk.Button
235 public SuspendButton()
238 clicked += on_clicked;
239 set_size_request(0, zavai.config.min_button_height);
242 public void on_clicked()
244 zavai.log.info("Suspending the phone via FSO");
250 public class ShutdownButton : Gtk.Button
252 public ShutdownButton()
255 clicked += on_clicked;
256 set_size_request(0, zavai.config.min_button_height);
259 public void on_clicked()
261 zavai.log.info("Shutting down the phone via FSO");
267 public class RebootButton : Gtk.Button
269 public RebootButton()
272 clicked += on_clicked;
273 set_size_request(0, zavai.config.min_button_height);
276 public void on_clicked()
278 zavai.log.info("Rebooting the phone via FSO");
284 // For a list of dbus services, look in /etc/dbus-1/system.d/
285 public class Backlight: zavai.Service
287 public dynamic DBus.Object usage;
293 usage = zavai.registry.sbus.get_object(
294 "org.freesmartphone.ousaged",
295 "/org/freesmartphone/Usage",
296 "org.freesmartphone.Usage");
299 // Turn the backlight and then let it fade off
302 // There must be a better method
303 usage.RequestResource("Display");
304 usage.ReleaseResource("Display");
307 /// Request GPS resource
308 public override void start()
312 usage.RequestResource("Display");
313 zavai.log.info("Acquired display");
315 } catch (GLib.Error e) {
316 zavai.log.error(e.message);
321 // Release usage of GPS
322 public override void stop()
324 if (!started) return;
326 usage.ReleaseResource("Display");
327 zavai.log.info("Released display");
329 } catch (GLib.Error e) {
330 zavai.log.error(e.message);
336 public class PowerMenu : zavai.Resource, Gtk.Window
338 protected Gtk.VBox vbox;
339 protected ScreenLockButton act_screen_lock;
340 protected SuspendButton act_suspend;
341 protected ShutdownButton act_shutdown;
342 protected RebootButton act_reboot;
343 protected ServiceRequestLink act_backlight_on;
344 protected bool shown;
348 type = Gtk.WindowType.TOPLEVEL;
349 title = "Power Menu";
351 destroy_with_parent = true;
352 set_transient_for(zavai.app);
354 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
355 set_size_request(300, 500);
357 vbox = new Gtk.VBox(false, 0);
360 //destroy += Gtk.main_quit;
361 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
362 //visibility_notify_event += on_visibility;
363 set_skip_pager_hint(true);
364 set_skip_taskbar_hint(true);
365 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
367 act_screen_lock = new ScreenLockButton();
368 vbox.pack_start(act_screen_lock, false, false, 0);
370 act_suspend = new SuspendButton();
371 vbox.pack_start(act_suspend, false, false, 0);
373 act_shutdown = new ShutdownButton();
374 vbox.pack_start(act_shutdown, false, false, 0);
376 act_reboot = new RebootButton();
377 vbox.pack_start(act_reboot, false, false, 0);
379 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
380 vbox.pack_start(act_backlight_on, false, false, 0);
395 // TODO: do more in case it is visible but has no visibility (is covered by others)
407 public void shutdown() {}
411 public class TogglePowerMenu : Gtk.Button
413 public TogglePowerMenu()
415 label = "Toggle power menu";
416 clicked += on_clicked;
417 set_size_request(0, zavai.config.min_button_height);
420 public void on_clicked()
422 zavai.log.info("Toggling power menu");
429 PowerMenu power_menu;
430 BatteryIcon battery_icon;
432 //TogglePowerMenu tpm;
437 backlight = new Backlight();
438 zavai.registry.register_service(backlight);
440 battery_icon = new BatteryIcon();
441 battery_icon.set_visible(true);
443 power_menu = new PowerMenu();
444 zavai.registry.register_resource("powermenu", power_menu);
446 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
447 //tpm = new TogglePowerMenu();
448 //zavai.registry.getmenu("menu.main").add_widget(tpm);
451 raise_icon = new RaiseIcon();
452 raise_icon.set_visible(true);
454 close_or_back = new CloseOrBack();
455 close_or_back.set_visible(true);
457 window_list = new WindowList("Current apps");
458 zavai.registry.register_applet("wm.list", window_list);
459 zavai.registry.getmenu("menu.main").add_applet("wm.list");
462 launcher = new Launcher("Run program");
464 zavai.log.error("Not running launcher: " + e.message);
468 if (launcher != null)
470 zavai.registry.register_applet("wm.launcher", launcher);
471 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");