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;
104 time_t now = new time_t();
105 bool short_press = now < last_down + 2;
109 // Short press: turn on backlight for a bit
112 // Long press: unlock
113 set_screen_lock(false);
118 // Short press: toggle power menu
121 // Long press: lock screen
122 set_screen_lock(true);
129 public class BatteryIcon : Gtk.StatusIcon
131 public dynamic DBus.Object battery;
132 protected string last_status;
133 protected int last_capacity;
137 battery = zavai.registry.sbus.get_object(
138 "org.freesmartphone.odeviced",
139 "/org/freesmartphone/Device/PowerSupply/battery",
140 "org.freesmartphone.Device.PowerSupply");
142 // activate += on_activate;
144 battery.PowerStatus += on_power_status;
145 battery.Capacity += on_capacity;
147 last_status = battery.GetPowerStatus();
148 last_capacity = battery.GetCapacity();
153 private void on_power_status(dynamic DBus.Object bat, string status)
155 zavai.log.info("New battery status: " + status);
156 last_status = status;
160 private void on_capacity(dynamic DBus.Object bat, int val)
162 stderr.printf("NEW CAPACITY: %d\n", val);
168 private void on_activate()
173 protected void update_icon()
175 string name = zavai.config.icondir + "/battery/";
177 if (last_status == "charging")
178 name += "%02d0_charging_500.png".printf(last_capacity/10);
180 name += "%02d0.png".printf(last_capacity/10);
182 stderr.printf("Loading icon from %s\n", name);
188 public class ScreenLockButton : Gtk.Button
190 public ScreenLockButton()
192 label = "Lock screen";
193 clicked += on_clicked;
194 set_size_request(0, zavai.config.min_button_height);
197 public void on_clicked()
199 zavai.log.info("Locking screen");
200 power.set_screen_lock(true);
205 public class SuspendButton : Gtk.Button
207 public SuspendButton()
210 clicked += on_clicked;
211 set_size_request(0, zavai.config.min_button_height);
214 public void on_clicked()
216 zavai.log.info("Suspending the phone via FSO");
222 public class ShutdownButton : Gtk.Button
224 public ShutdownButton()
227 clicked += on_clicked;
228 set_size_request(0, zavai.config.min_button_height);
231 public void on_clicked()
233 zavai.log.info("Shutting down the phone via FSO");
239 public class RebootButton : Gtk.Button
241 public RebootButton()
244 clicked += on_clicked;
245 set_size_request(0, zavai.config.min_button_height);
248 public void on_clicked()
250 zavai.log.info("Rebooting the phone via FSO");
256 // For a list of dbus services, look in /etc/dbus-1/system.d/
257 public class Backlight: zavai.Service
259 public dynamic DBus.Object usage;
265 usage = zavai.registry.sbus.get_object(
266 "org.freesmartphone.ousaged",
267 "/org/freesmartphone/Usage",
268 "org.freesmartphone.Usage");
271 // Turn the backlight and then let it fade off
274 // There must be a better method
275 usage.RequestResource("Display");
276 usage.ReleaseResource("Display");
279 /// Request GPS resource
280 public override void start()
284 usage.RequestResource("Display");
285 zavai.log.info("Acquired display");
287 } catch (GLib.Error e) {
288 zavai.log.error(e.message);
293 // Release usage of GPS
294 public override void stop()
296 if (!started) return;
298 usage.ReleaseResource("Display");
299 zavai.log.info("Released display");
301 } catch (GLib.Error e) {
302 zavai.log.error(e.message);
308 public class PowerMenu : zavai.Resource, Gtk.Window
310 protected Gtk.VBox vbox;
311 protected ScreenLockButton act_screen_lock;
312 protected SuspendButton act_suspend;
313 protected ShutdownButton act_shutdown;
314 protected RebootButton act_reboot;
315 protected ServiceRequestLink act_backlight_on;
316 protected bool shown;
320 type = Gtk.WindowType.TOPLEVEL;
321 title = "Power Menu";
323 destroy_with_parent = true;
324 set_transient_for(zavai.app);
326 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
327 set_size_request(300, 500);
329 vbox = new Gtk.VBox(false, 0);
332 //destroy += Gtk.main_quit;
333 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
334 //visibility_notify_event += on_visibility;
335 set_skip_pager_hint(true);
336 set_skip_taskbar_hint(true);
337 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
339 act_screen_lock = new ScreenLockButton();
340 vbox.pack_start(act_screen_lock, false, false, 0);
342 act_suspend = new SuspendButton();
343 vbox.pack_start(act_suspend, false, false, 0);
345 act_shutdown = new ShutdownButton();
346 vbox.pack_start(act_shutdown, false, false, 0);
348 act_reboot = new RebootButton();
349 vbox.pack_start(act_reboot, false, false, 0);
351 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
352 vbox.pack_start(act_backlight_on, false, false, 0);
367 // TODO: do more in case it is visible but has no visibility (is covered by others)
379 public void shutdown() {}
383 public class TogglePowerMenu : Gtk.Button
385 public TogglePowerMenu()
387 label = "Toggle power menu";
388 clicked += on_clicked;
389 set_size_request(0, zavai.config.min_button_height);
392 public void on_clicked()
394 zavai.log.info("Toggling power menu");
401 PowerMenu power_menu;
402 BatteryIcon battery_icon;
404 //TogglePowerMenu tpm;
409 backlight = new Backlight();
410 zavai.registry.register_service(backlight);
412 battery_icon = new BatteryIcon();
413 battery_icon.set_visible(true);
415 power_menu = new PowerMenu();
416 zavai.registry.register_resource("powermenu", power_menu);
418 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
419 //tpm = new TogglePowerMenu();
420 //zavai.registry.getmenu("menu.main").add_widget(tpm);
423 raise_icon = new RaiseIcon();
424 raise_icon.set_visible(true);
426 close_or_back = new CloseOrBack();
427 close_or_back.set_visible(true);
429 window_list = new WindowList("Current apps");
430 zavai.registry.register_applet("wm.list", window_list);
431 zavai.registry.getmenu("menu.main").add_applet("wm.list");
434 launcher = new Launcher("Run program");
436 zavai.log.error("Not running launcher: " + e.message);
440 if (launcher != null)
442 zavai.registry.register_applet("wm.launcher", launcher);
443 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");