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;
93 screen_lock_changed(locked);
96 private void on_power_button(Posix.timeval* t, bool pressed)
100 last_down = t->tv_sec;
104 time_t now = new time_t();
105 if (now < last_down + 2)
107 // Short press: turn on backlight for a bit
110 // Long press: unlock
111 set_screen_lock(false);
123 public class BatteryIcon : Gtk.StatusIcon
125 public dynamic DBus.Object battery;
126 protected string last_status;
127 protected int last_capacity;
131 battery = zavai.registry.sbus.get_object(
132 "org.freesmartphone.odeviced",
133 "/org/freesmartphone/Device/PowerSupply/battery",
134 "org.freesmartphone.Device.PowerSupply");
136 // activate += on_activate;
138 battery.PowerStatus += on_power_status;
139 battery.Capacity += on_capacity;
141 last_status = battery.GetPowerStatus();
142 last_capacity = battery.GetCapacity();
147 private void on_power_status(dynamic DBus.Object bat, string status)
149 zavai.log.info("New battery status: " + status);
150 last_status = status;
154 private void on_capacity(dynamic DBus.Object bat, int val)
156 stderr.printf("NEW CAPACITY: %d\n", val);
162 private void on_activate()
167 protected void update_icon()
169 string name = zavai.config.icondir + "/battery/";
171 if (last_status == "charging")
172 name += "%02d0_charging_500.png".printf(last_capacity/10);
174 name += "%02d0.png".printf(last_capacity/10);
176 stderr.printf("Loading icon from %s\n", name);
182 public class ScreenLockButton : Gtk.Button
184 public ScreenLockButton()
186 label = "Lock screen";
187 clicked += on_clicked;
188 set_size_request(0, zavai.config.min_button_height);
191 public void on_clicked()
193 zavai.log.info("Locking screen");
194 power.set_screen_lock(true);
199 public class SuspendButton : Gtk.Button
201 public SuspendButton()
204 clicked += on_clicked;
205 set_size_request(0, zavai.config.min_button_height);
208 public void on_clicked()
210 zavai.log.info("Suspending the phone via FSO");
216 public class ShutdownButton : Gtk.Button
218 public ShutdownButton()
221 clicked += on_clicked;
222 set_size_request(0, zavai.config.min_button_height);
225 public void on_clicked()
227 zavai.log.info("Shutting down the phone via FSO");
233 public class RebootButton : Gtk.Button
235 public RebootButton()
238 clicked += on_clicked;
239 set_size_request(0, zavai.config.min_button_height);
242 public void on_clicked()
244 zavai.log.info("Rebooting the phone via FSO");
250 // For a list of dbus services, look in /etc/dbus-1/system.d/
251 public class Backlight: zavai.Service
253 public dynamic DBus.Object usage;
259 usage = zavai.registry.sbus.get_object(
260 "org.freesmartphone.ousaged",
261 "/org/freesmartphone/Usage",
262 "org.freesmartphone.Usage");
265 // Turn the backlight and then let it fade off
268 // There must be a better method
269 usage.RequestResource("Display");
270 usage.ReleaseResource("Display");
273 /// Request GPS resource
274 public override void start()
278 usage.RequestResource("Display");
279 zavai.log.info("Acquired display");
281 } catch (GLib.Error e) {
282 zavai.log.error(e.message);
287 // Release usage of GPS
288 public override void stop()
290 if (!started) return;
292 usage.ReleaseResource("Display");
293 zavai.log.info("Released display");
295 } catch (GLib.Error e) {
296 zavai.log.error(e.message);
302 public class PowerMenu : zavai.Resource, Gtk.Window
304 protected Gtk.VBox vbox;
305 protected ScreenLockButton act_screen_lock;
306 protected SuspendButton act_suspend;
307 protected ShutdownButton act_shutdown;
308 protected RebootButton act_reboot;
309 protected ServiceRequestLink act_backlight_on;
310 protected bool shown;
314 type = Gtk.WindowType.TOPLEVEL;
315 title = "Power Menu";
317 destroy_with_parent = true;
318 set_transient_for(zavai.app);
320 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
321 set_size_request(300, 500);
323 vbox = new Gtk.VBox(false, 0);
326 //destroy += Gtk.main_quit;
327 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
328 //visibility_notify_event += on_visibility;
329 set_skip_pager_hint(true);
330 set_skip_taskbar_hint(true);
331 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
333 act_screen_lock = new ScreenLockButton();
334 vbox.pack_start(act_screen_lock, false, false, 0);
336 act_suspend = new SuspendButton();
337 vbox.pack_start(act_suspend, false, false, 0);
339 act_shutdown = new ShutdownButton();
340 vbox.pack_start(act_shutdown, false, false, 0);
342 act_reboot = new RebootButton();
343 vbox.pack_start(act_reboot, false, false, 0);
345 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
346 vbox.pack_start(act_backlight_on, false, false, 0);
361 // TODO: do more in case it is visible but has no visibility (is covered by others)
373 public void shutdown() {}
377 public class TogglePowerMenu : Gtk.Button
379 public TogglePowerMenu()
381 label = "Toggle power menu";
382 clicked += on_clicked;
383 set_size_request(0, zavai.config.min_button_height);
386 public void on_clicked()
388 zavai.log.info("Toggling power menu");
395 PowerMenu power_menu;
396 BatteryIcon battery_icon;
398 //TogglePowerMenu tpm;
403 backlight = new Backlight();
404 zavai.registry.register_service(backlight);
406 battery_icon = new BatteryIcon();
407 battery_icon.set_visible(true);
409 power_menu = new PowerMenu();
410 zavai.registry.register_resource("powermenu", power_menu);
412 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
413 //tpm = new TogglePowerMenu();
414 //zavai.registry.getmenu("menu.main").add_widget(tpm);
417 raise_icon = new RaiseIcon();
418 raise_icon.set_visible(true);
420 close_or_back = new CloseOrBack();
421 close_or_back.set_visible(true);
423 window_list = new WindowList("Current apps");
424 zavai.registry.register_applet("wm.list", window_list);
425 zavai.registry.getmenu("menu.main").add_applet("wm.list");
428 launcher = new Launcher("Run program");
430 zavai.log.error("Not running launcher: " + e.message);
434 if (launcher != null)
436 zavai.registry.register_applet("wm.launcher", launcher);
437 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");