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
25 namespace powerbutton {
28 public class RaiseIcon : Gtk.StatusIcon
32 activate += on_activate;
33 zavai.app.visibility_changed += on_visibility_changed;
37 private void on_visibility_changed(bool visible)
42 private void on_activate()
44 zavai.app.toggle_visibility();
48 protected void update_icon()
50 string name = zavai.config.icondir + "/";
51 if (!zavai.app.visibility)
52 name += "zavai_on.png";
54 name += "zavai_off.png";
59 public class CloseOrBack : Gtk.StatusIcon
63 activate += on_activate;
64 zavai.app.visibility_changed += on_visibility_changed;
68 private void on_visibility_changed(bool visible)
73 private void on_activate()
75 if (zavai.app.visibility)
80 Gdk.Window w = zavai.app.get_screen().get_active_window();
81 if (w != zavai.app.window)
88 protected void update_icon()
90 string name = zavai.config.icondir + "/";
91 if (!zavai.app.visibility)
92 name += "quit_on.png";
94 name += "quit_off.png";
99 public class WindowList : Applet
101 Wnck.Tasklist selector;
103 public WindowList(string label)
106 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
107 pack_start(selector, true, true, 0);
112 public class CommandButton : Gtk.Button
114 private string command;
116 public CommandButton(string name, string command)
119 this.command = command;
120 clicked += on_clicked;
121 set_size_request(0, zavai.config.min_button_height);
124 public void on_clicked()
126 zavai.log.info("Run program: " + command);
127 string[] args = command.split(" ");
130 Environment.get_home_dir(),
133 SpawnFlags.SEARCH_PATH,
139 private bool screen_locked;
140 private int screen_lock_fd;
142 private void set_screen_lock(bool locked)
144 if (locked && screen_locked)
146 if (!locked && !screen_locked)
151 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
152 if (screen_lock_fd < 0)
154 zavai.log.error("Cannot open /dev/input/event1");
158 int EVIOCGRAB = 0x40044590;
159 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
161 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
162 Posix.close(screen_lock_fd);
166 Posix.close(screen_lock_fd);
168 screen_locked = locked;
171 public class ScreenLockButton : Gtk.Button
173 public ScreenLockButton(string name)
176 clicked += on_clicked;
177 set_size_request(0, zavai.config.min_button_height);
180 public void on_clicked()
182 zavai.log.info("Locking screen");
183 set_screen_lock(true);
187 private void on_power_button(Posix.timeval* time, bool pressed)
192 set_screen_lock(false);
195 zavai.app.push_applet("menu.power");
196 zavai.app.ensure_visible();
201 public class BatteryIcon : Gtk.StatusIcon
203 public dynamic DBus.Object battery;
204 protected string last_status;
205 protected int last_capacity;
209 battery = zavai.registry.sbus.get_object(
210 "org.freesmartphone.odeviced",
211 "/org/freesmartphone/Device/PowerSupply/battery",
212 "org.freesmartphone.Device.PowerSupply");
214 // activate += on_activate;
216 battery.PowerStatus += on_power_status;
217 battery.Capacity += on_capacity;
219 last_status = battery.GetPowerStatus();
220 last_capacity = battery.GetCapacity();
225 private void on_power_status(dynamic DBus.Object bat, string status)
227 zavai.log.info("New battery status: " + status);
228 last_status = status;
232 private void on_capacity(dynamic DBus.Object bat, int val)
234 stderr.printf("NEW CAPACITY: %d\n", val);
240 private void on_activate()
245 protected void update_icon()
247 string name = zavai.config.icondir + "/battery/";
249 if (last_status == "charging")
250 name += "%02d0_charging_500.png".printf(last_capacity/10);
252 name += "%02d0.png".printf(last_capacity/10);
254 stderr.printf("Loading icon from %s\n", name);
260 BatteryIcon battery_icon;
264 screen_locked = false;
267 zavai.input.power_button.power_button += on_power_button;
269 battery_icon = new BatteryIcon();
270 battery_icon.set_visible(true);
273 var menu_power = new zavai.Menu("Power menu");
274 menu_power.add_widget(new ScreenLockButton("Lock screen"));
275 menu_power.add_widget(new CommandButton("Suspend", "apm -s"));
276 menu_power.add_widget(new CommandButton("Shutdown", "shutdown -h now"));
277 menu_power.add_widget(new CommandButton("Reboot", "shutdown -r now"));
278 zavai.registry.register_menu("menu.power", menu_power);
280 zavai.registry.getmenu("menu.main").add_applet("menu.power");
282 zavai.registry.gets("input.power_button").request("powerbutton");
285 raise_icon = new RaiseIcon();
286 raise_icon.set_visible(true);
288 close_or_back = new CloseOrBack();
289 close_or_back.set_visible(true);
291 window_list = new WindowList("Current apps");
292 zavai.registry.register_applet("wm.list", window_list);
293 zavai.registry.getmenu("menu.main").add_applet("wm.list");
296 launcher = new Launcher("Run program");
298 zavai.log.error("Not running launcher: " + e.message);
302 if (launcher != null)
304 zavai.registry.register_applet("wm.launcher", launcher);
305 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");