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;
40 screen_locked = false;
42 hide_after_closing_power_menu = false;
45 usage = zavai.registry.sbus.get_object(
46 "org.freesmartphone.ousaged",
47 "/org/freesmartphone/Usage",
48 "org.freesmartphone.Usage");
50 zavai.input.power_button.power_button += on_power_button;
51 zavai.input.power_button.request("zavai.ui.powerbutton.power");
54 public void shutdown()
56 zavai.input.power_button.release("zavai.ui.powerbutton.power");
59 public void do_suspend() { usage.Suspend(); }
60 public void do_shutdown() { usage.Shutdown(); }
61 public void do_reboot() { usage.Reboot(); }
63 public void set_screen_lock(bool locked)
65 if (locked && screen_locked)
67 if (!locked && !screen_locked)
72 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
73 if (screen_lock_fd < 0)
75 zavai.log.error("Cannot open /dev/input/event1");
79 int EVIOCGRAB = 0x40044590;
80 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
82 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
83 Posix.close(screen_lock_fd);
87 Posix.close(screen_lock_fd);
89 screen_locked = locked;
92 private void on_power_button(Posix.timeval* t, bool pressed)
96 last_down = t->tv_sec;
100 time_t now = new time_t();
101 if (now < last_down + 2)
103 // Short press: turn on backlight for a bit
106 // Long press: unlock
107 set_screen_lock(false);
119 public class BatteryIcon : Gtk.StatusIcon
121 public dynamic DBus.Object battery;
122 protected string last_status;
123 protected int last_capacity;
127 battery = zavai.registry.sbus.get_object(
128 "org.freesmartphone.odeviced",
129 "/org/freesmartphone/Device/PowerSupply/battery",
130 "org.freesmartphone.Device.PowerSupply");
132 // activate += on_activate;
134 battery.PowerStatus += on_power_status;
135 battery.Capacity += on_capacity;
137 last_status = battery.GetPowerStatus();
138 last_capacity = battery.GetCapacity();
143 private void on_power_status(dynamic DBus.Object bat, string status)
145 zavai.log.info("New battery status: " + status);
146 last_status = status;
150 private void on_capacity(dynamic DBus.Object bat, int val)
152 stderr.printf("NEW CAPACITY: %d\n", val);
158 private void on_activate()
163 protected void update_icon()
165 string name = zavai.config.icondir + "/battery/";
167 if (last_status == "charging")
168 name += "%02d0_charging_500.png".printf(last_capacity/10);
170 name += "%02d0.png".printf(last_capacity/10);
172 stderr.printf("Loading icon from %s\n", name);
178 public class ScreenLockButton : Gtk.Button
180 public ScreenLockButton()
182 label = "Lock screen";
183 clicked += on_clicked;
184 set_size_request(0, zavai.config.min_button_height);
187 public void on_clicked()
189 zavai.log.info("Locking screen");
190 power.set_screen_lock(true);
195 public class SuspendButton : Gtk.Button
197 public SuspendButton()
200 clicked += on_clicked;
201 set_size_request(0, zavai.config.min_button_height);
204 public void on_clicked()
206 zavai.log.info("Suspending the phone via FSO");
212 public class ShutdownButton : Gtk.Button
214 public ShutdownButton()
217 clicked += on_clicked;
218 set_size_request(0, zavai.config.min_button_height);
221 public void on_clicked()
223 zavai.log.info("Shutting down the phone via FSO");
229 public class RebootButton : Gtk.Button
231 public RebootButton()
234 clicked += on_clicked;
235 set_size_request(0, zavai.config.min_button_height);
238 public void on_clicked()
240 zavai.log.info("Rebooting the phone via FSO");
246 // For a list of dbus services, look in /etc/dbus-1/system.d/
247 public class Backlight: zavai.Service
249 public dynamic DBus.Object usage;
255 usage = zavai.registry.sbus.get_object(
256 "org.freesmartphone.ousaged",
257 "/org/freesmartphone/Usage",
258 "org.freesmartphone.Usage");
261 // Turn the backlight and then let it fade off
264 // There must be a better method
265 usage.RequestResource("Display");
266 usage.ReleaseResource("Display");
269 /// Request GPS resource
270 public override void start()
274 usage.RequestResource("Display");
275 zavai.log.info("Acquired display");
277 } catch (GLib.Error e) {
278 zavai.log.error(e.message);
283 // Release usage of GPS
284 public override void stop()
286 if (!started) return;
288 usage.ReleaseResource("Display");
289 zavai.log.info("Released display");
291 } catch (GLib.Error e) {
292 zavai.log.error(e.message);
298 public class PowerMenu : zavai.Resource, Gtk.Window
300 protected Gtk.VBox vbox;
301 protected ScreenLockButton act_screen_lock;
302 protected SuspendButton act_suspend;
303 protected ShutdownButton act_shutdown;
304 protected RebootButton act_reboot;
305 protected ServiceRequestLink act_backlight_on;
306 protected bool shown;
310 type = Gtk.WindowType.TOPLEVEL;
311 title = "Power Menu";
313 destroy_with_parent = true;
314 set_transient_for(zavai.app);
316 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
317 set_size_request(300, 500);
319 vbox = new Gtk.VBox(false, 0);
322 //destroy += Gtk.main_quit;
323 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
324 //visibility_notify_event += on_visibility;
325 set_skip_pager_hint(true);
326 set_skip_taskbar_hint(true);
327 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
329 act_screen_lock = new ScreenLockButton();
330 vbox.pack_start(act_screen_lock, false, false, 0);
332 act_suspend = new SuspendButton();
333 vbox.pack_start(act_suspend, false, false, 0);
335 act_shutdown = new ShutdownButton();
336 vbox.pack_start(act_shutdown, false, false, 0);
338 act_reboot = new RebootButton();
339 vbox.pack_start(act_reboot, false, false, 0);
341 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
342 vbox.pack_start(act_backlight_on, false, false, 0);
357 // TODO: do more in case it is visible but has no visibility (is covered by others)
369 public void shutdown() {}
373 public class TogglePowerMenu : Gtk.Button
375 public TogglePowerMenu()
377 label = "Toggle power menu";
378 clicked += on_clicked;
379 set_size_request(0, zavai.config.min_button_height);
382 public void on_clicked()
384 zavai.log.info("Toggling power menu");
391 PowerMenu power_menu;
392 BatteryIcon battery_icon;
394 //TogglePowerMenu tpm;
399 backlight = new Backlight();
400 zavai.registry.register_service(backlight);
402 battery_icon = new BatteryIcon();
403 battery_icon.set_visible(true);
405 power_menu = new PowerMenu();
406 zavai.registry.register_resource("powermenu", power_menu);
408 //zavai.registry.getmenu("menu.main").add_applet("menu.power");
409 //tpm = new TogglePowerMenu();
410 //zavai.registry.getmenu("menu.main").add_widget(tpm);
413 raise_icon = new RaiseIcon();
414 raise_icon.set_visible(true);
416 close_or_back = new CloseOrBack();
417 close_or_back.set_visible(true);
419 window_list = new WindowList("Current apps");
420 zavai.registry.register_applet("wm.list", window_list);
421 zavai.registry.getmenu("menu.main").add_applet("wm.list");
424 launcher = new Launcher("Run program");
426 zavai.log.error("Not running launcher: " + e.message);
430 if (launcher != null)
432 zavai.registry.register_applet("wm.launcher", launcher);
433 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");