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 public class Power : zavai.Resource, Object
29 public dynamic DBus.Object usage;
30 public bool screen_locked;
31 private int screen_lock_fd;
32 // Timestamp of the past power button pressed even (0 if the button has
34 private time_t last_down;
36 private bool hide_after_closing_power_menu;
38 public signal void screen_lock_changed(bool state);
42 screen_locked = false;
44 hide_after_closing_power_menu = false;
47 usage = zavai.registry.sbus.get_object(
48 "org.freesmartphone.ousaged",
49 "/org/freesmartphone/Usage",
50 "org.freesmartphone.Usage");
52 zavai.input.power_button.power_button += on_power_button;
53 zavai.input.power_button.request("zavai.ui.powerbutton.power");
56 public void shutdown()
58 zavai.input.power_button.release("zavai.ui.powerbutton.power");
61 public void do_suspend() { usage.Suspend(); }
62 public void do_shutdown() { usage.Shutdown(); }
63 public void do_reboot() { usage.Reboot(); }
65 public void set_screen_lock(bool locked)
67 if (locked && screen_locked)
69 if (!locked && !screen_locked)
74 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
75 if (screen_lock_fd < 0)
77 zavai.log.error("Cannot open /dev/input/event1");
81 int EVIOCGRAB = 0x40044590;
82 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
84 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
85 Posix.close(screen_lock_fd);
89 Posix.close(screen_lock_fd);
91 screen_locked = locked;
95 screen_lock_changed(locked);
98 private void on_power_button(Posix.timeval* t, bool pressed)
102 last_down = t->tv_sec;
106 time_t now = new time_t();
107 if (now < last_down + 2)
109 // Short press: turn on backlight for a bit
112 // Long press: unlock
113 set_screen_lock(false);
125 public class BatteryIcon : Gtk.StatusIcon
127 public dynamic DBus.Object battery;
128 protected string last_status;
129 protected int last_capacity;
133 battery = zavai.registry.sbus.get_object(
134 "org.freesmartphone.odeviced",
135 "/org/freesmartphone/Device/PowerSupply/battery",
136 "org.freesmartphone.Device.PowerSupply");
138 // activate += on_activate;
140 battery.PowerStatus += on_power_status;
141 battery.Capacity += on_capacity;
143 last_status = battery.GetPowerStatus();
144 last_capacity = battery.GetCapacity();
149 private void on_power_status(dynamic DBus.Object bat, string status)
151 zavai.log.info("New battery status: " + status);
152 last_status = status;
156 private void on_capacity(dynamic DBus.Object bat, int val)
158 stderr.printf("NEW CAPACITY: %d\n", val);
164 private void on_activate()
169 protected void update_icon()
171 string name = zavai.config.icondir + "/battery/";
173 if (last_status == "charging")
174 name += "%02d0_charging_500.png".printf(last_capacity/10);
176 name += "%02d0.png".printf(last_capacity/10);
178 stderr.printf("Loading icon from %s\n", name);
184 public class ScreenLockButton : Gtk.Button
186 public ScreenLockButton()
188 label = "Lock screen";
189 clicked += on_clicked;
190 set_size_request(0, zavai.config.min_button_height);
193 public void on_clicked()
195 zavai.log.info("Locking screen");
196 power.set_screen_lock(true);
201 public class SuspendButton : Gtk.Button
203 public SuspendButton()
206 clicked += on_clicked;
207 set_size_request(0, zavai.config.min_button_height);
210 public void on_clicked()
212 zavai.log.info("Suspending the phone via FSO");
218 public class ShutdownButton : Gtk.Button
220 public ShutdownButton()
223 clicked += on_clicked;
224 set_size_request(0, zavai.config.min_button_height);
227 public void on_clicked()
229 zavai.log.info("Shutting down the phone via FSO");
235 public class RebootButton : Gtk.Button
237 public RebootButton()
240 clicked += on_clicked;
241 set_size_request(0, zavai.config.min_button_height);
244 public void on_clicked()
246 zavai.log.info("Rebooting the phone via FSO");
252 // For a list of dbus services, look in /etc/dbus-1/system.d/
253 public class Backlight: zavai.Service
255 public dynamic DBus.Object usage;
261 usage = zavai.registry.sbus.get_object(
262 "org.freesmartphone.ousaged",
263 "/org/freesmartphone/Usage",
264 "org.freesmartphone.Usage");
267 // Turn the backlight and then let it fade off
270 // There must be a better method
271 usage.RequestResource("Display");
272 usage.ReleaseResource("Display");
275 /// Request GPS resource
276 public override void start()
280 usage.RequestResource("Display");
281 zavai.log.info("Acquired display");
283 } catch (GLib.Error e) {
284 zavai.log.error(e.message);
289 // Release usage of GPS
290 public override void stop()
292 if (!started) return;
294 usage.ReleaseResource("Display");
295 zavai.log.info("Released display");
297 } catch (GLib.Error e) {
298 zavai.log.error(e.message);
304 public class PowerMenu : zavai.Resource, Gtk.Window
306 protected Gtk.VBox vbox;
307 protected ScreenLockButton act_screen_lock;
308 protected SuspendButton act_suspend;
309 protected ShutdownButton act_shutdown;
310 protected RebootButton act_reboot;
311 protected ServiceRequestLink act_backlight_on;
312 protected bool shown;
316 type = Gtk.WindowType.TOPLEVEL;
317 title = "Power Menu";
319 destroy_with_parent = true;
320 set_transient_for(zavai.app);
322 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
323 set_size_request(300, 500);
325 vbox = new Gtk.VBox(false, 0);
328 //destroy += Gtk.main_quit;
329 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
330 //visibility_notify_event += on_visibility;
331 set_skip_pager_hint(true);
332 set_skip_taskbar_hint(true);
333 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
335 act_screen_lock = new ScreenLockButton();
336 vbox.pack_start(act_screen_lock, false, false, 0);
338 act_suspend = new SuspendButton();
339 vbox.pack_start(act_suspend, false, false, 0);
341 act_shutdown = new ShutdownButton();
342 vbox.pack_start(act_shutdown, false, false, 0);
344 act_reboot = new RebootButton();
345 vbox.pack_start(act_reboot, false, false, 0);
347 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
348 vbox.pack_start(act_backlight_on, false, false, 0);
363 // TODO: do more in case it is visible but has no visibility (is covered by others)
375 public void shutdown() {}
379 public class TogglePowerMenu : Gtk.Button
381 public TogglePowerMenu()
383 label = "Toggle power menu";
384 clicked += on_clicked;
385 set_size_request(0, zavai.config.min_button_height);
388 public void on_clicked()
390 zavai.log.info("Toggling power menu");
397 PowerMenu power_menu;
398 BatteryIcon battery_icon;
400 //TogglePowerMenu tpm;
405 backlight = new Backlight();
406 zavai.registry.register_service(backlight);
408 battery_icon = new BatteryIcon();
409 battery_icon.set_visible(true);
411 power_menu = new PowerMenu();
412 zavai.registry.register_resource("powermenu", power_menu);
414 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
415 //tpm = new TogglePowerMenu();
416 //zavai.registry.getmenu("menu.main").add_widget(tpm);
419 raise_icon = new RaiseIcon();
420 raise_icon.set_visible(true);
422 close_or_back = new CloseOrBack();
423 close_or_back.set_visible(true);
425 window_list = new WindowList("Current apps");
426 zavai.registry.register_applet("wm.list", window_list);
427 zavai.registry.getmenu("menu.main").add_applet("wm.list");
430 launcher = new Launcher("Run program");
432 zavai.log.error("Not running launcher: " + e.message);
436 if (launcher != null)
438 zavai.registry.register_applet("wm.launcher", launcher);
439 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");