Second attempt at a togglable power menu
[gregoa/zavai.git] / src / app_power.vala
1 /*
2  * app_power - zavai power handling
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace ui {
25 namespace powerbutton {
26
27 public class Power : zavai.Resource, Object
28 {
29         public dynamic DBus.Object usage;
30         public bool screen_locked;
31         private int screen_lock_fd;
32
33         private bool hide_after_closing_power_menu;
34
35         public Power()
36         {
37                 screen_locked = false;
38                 screen_lock_fd = -1;
39                 hide_after_closing_power_menu = false;
40
41                 usage = zavai.registry.sbus.get_object(
42                         "org.freesmartphone.ousaged",
43                         "/org/freesmartphone/Usage",
44                         "org.freesmartphone.Usage");
45
46                 zavai.input.power_button.power_button += on_power_button;
47                 zavai.input.power_button.request("zavai.ui.powerbutton.power");
48         }
49
50         public void shutdown()
51         {
52                 zavai.input.power_button.release("zavai.ui.powerbutton.power");
53         }
54
55         public void do_suspend() { usage.Suspend(); }
56         public void do_shutdown() { usage.Shutdown(); }
57         public void do_reboot() { usage.Reboot(); }
58
59         public void set_screen_lock(bool locked)
60         {
61                 if (locked && screen_locked)
62                         return;
63                 if (!locked && !screen_locked)
64                         return;
65
66                 if (locked)
67                 {
68                         screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
69                         if (screen_lock_fd < 0)
70                         {
71                                 zavai.log.error("Cannot open /dev/input/event1");
72                                 return;
73                         }
74
75                         int EVIOCGRAB = 0x40044590;
76                         if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
77                         {
78                                 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
79                                 Posix.close(screen_lock_fd);
80                                 return;
81                         }
82                 } else {
83                         Posix.close(screen_lock_fd);
84                 }
85                 screen_locked = locked;
86         }
87
88         private void on_power_button(Posix.timeval* time, bool pressed)
89         {
90                 if (!pressed)
91                 {
92                         if (screen_locked)
93                         {
94                                 // TODO: short press: turn on backlight for a bit
95                                 // TODO: long press: unlock
96                                 set_screen_lock(false);
97                         }
98                         else
99                         {
100                                 power_menu.toggle();
101                         }
102                 }
103         }
104 }
105
106 public class BatteryIcon : Gtk.StatusIcon
107 {
108         public dynamic DBus.Object battery;
109         protected string last_status;
110         protected int last_capacity;
111
112         public BatteryIcon()
113         {
114                 battery = zavai.registry.sbus.get_object(
115                         "org.freesmartphone.odeviced",
116                         "/org/freesmartphone/Device/PowerSupply/battery",
117                         "org.freesmartphone.Device.PowerSupply");
118
119                 // activate += on_activate;
120
121                 battery.PowerStatus += on_power_status;
122                 battery.Capacity += on_capacity;
123
124                 last_status = battery.GetPowerStatus();
125                 last_capacity = battery.GetCapacity();
126                 
127                 update_icon();
128         }
129
130         private void on_power_status(dynamic DBus.Object bat, string status)
131         {
132                 zavai.log.info("New battery status: " + status);
133                 last_status = status;
134                 update_icon();
135         }
136
137         private void on_capacity(dynamic DBus.Object bat, int val)
138         {
139 stderr.printf("NEW CAPACITY: %d\n", val);
140                 last_capacity = val;
141                 update_icon();
142         }
143
144         /*
145         private void on_activate()
146         {
147         }
148         */
149
150         protected void update_icon()
151         {
152                 string name = zavai.config.icondir + "/battery/";
153
154                 if (last_status == "charging")
155                         name += "%02d0_charging_500.png".printf(last_capacity/10);
156                 else
157                         name += "%02d0.png".printf(last_capacity/10);
158
159 stderr.printf("Loading icon from %s\n", name);
160
161                 set_from_file(name);
162         }
163 }
164
165 public class ScreenLockButton : Gtk.Button
166 {
167         public ScreenLockButton()
168         {
169                 label = "Lock screen";
170                 clicked += on_clicked;
171                 set_size_request(0, zavai.config.min_button_height);
172         }
173
174         public void on_clicked()
175         {
176                 zavai.log.info("Locking screen");
177                 power.set_screen_lock(true);
178         }
179 }
180
181 public class SuspendButton : Gtk.Button
182 {
183         public SuspendButton()
184         {
185                 label = "Suspend";
186                 clicked += on_clicked;
187                 set_size_request(0, zavai.config.min_button_height);
188         }
189
190         public void on_clicked()
191         {
192                 zavai.log.info("Suspending the phone via FSO");
193                 power.do_suspend();
194         }
195 }
196
197 public class ShutdownButton : Gtk.Button
198 {
199         public ShutdownButton()
200         {
201                 label = "Shut down";
202                 clicked += on_clicked;
203                 set_size_request(0, zavai.config.min_button_height);
204         }
205
206         public void on_clicked()
207         {
208                 zavai.log.info("Shutting down the phone via FSO");
209                 power.do_shutdown();
210         }
211 }
212
213 public class RebootButton : Gtk.Button
214 {
215         public RebootButton()
216         {
217                 label = "Reboot";
218                 clicked += on_clicked;
219                 set_size_request(0, zavai.config.min_button_height);
220         }
221
222         public void on_clicked()
223         {
224                 zavai.log.info("Rebooting the phone via FSO");
225                 power.do_reboot();
226         }
227 }
228
229 // For a list of dbus services, look in /etc/dbus-1/system.d/
230 public class Backlight: zavai.Service
231 {
232         public dynamic DBus.Object usage;
233
234         public Backlight()
235         {
236                 name = "backlight";
237
238                 usage = zavai.registry.sbus.get_object(
239                         "org.freesmartphone.ousaged",
240                         "/org/freesmartphone/Usage",
241                         "org.freesmartphone.Usage");
242         }
243
244         /// Request GPS resource
245         public override void start()
246         {
247                 if (started) return;
248                 try {
249                         usage.RequestResource("Display");
250                         zavai.log.info("Acquired display");
251                         base.start();
252                 } catch (GLib.Error e) {
253                         zavai.log.error(e.message);
254                 }
255                 base.start();
256         }
257
258         // Release usage of GPS
259         public override void stop()
260         {
261                 if (!started) return;
262                 try {
263                         usage.ReleaseResource("Display");
264                         zavai.log.info("Released display");
265                         base.stop();
266                 } catch (GLib.Error e) {
267                         zavai.log.error(e.message);
268                 }
269                 base.stop();
270         }
271 }
272
273 public class PowerMenu : zavai.Resource, Gtk.Window
274 {
275         protected Gtk.VBox vbox;
276         protected ScreenLockButton act_screen_lock;
277         protected SuspendButton act_suspend;
278         protected ShutdownButton act_shutdown;
279         protected RebootButton act_reboot;
280         protected ServiceRequestLink act_backlight_on;
281
282         public PowerMenu()
283         {
284                 type = Gtk.WindowType.POPUP;
285                 title = "Power Menu";
286
287                 vbox = new Gtk.VBox(true, 0);
288                 add(vbox);
289
290                 //destroy += Gtk.main_quit;
291                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
292                 //visibility_notify_event += on_visibility;
293                 set_skip_pager_hint(true);
294                 set_skip_taskbar_hint(true);
295                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
296
297                 act_screen_lock = new ScreenLockButton();
298                 vbox.pack_start(act_screen_lock, false, false, 0);
299
300                 act_suspend = new SuspendButton();
301                 vbox.pack_start(act_suspend, false, false, 0);
302
303                 act_shutdown = new ShutdownButton();
304                 vbox.pack_start(act_shutdown, false, false, 0);
305
306                 act_reboot = new RebootButton();
307                 vbox.pack_start(act_reboot, false, false, 0);
308
309                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
310                 vbox.pack_start(act_backlight_on, false, false, 0);
311         }
312
313         public void toggle()
314         {
315                 // TODO: do more in case it is visible but has no visibility (is covered by others)
316                 visible = !visible;
317                 if (visible)
318                         present();
319         }
320
321         public void shutdown() {}
322 }
323
324
325 Power power;
326 PowerMenu power_menu;
327 BatteryIcon battery_icon;
328 Backlight backlight;
329
330 public void init()
331 {
332         power = new Power();
333         backlight = new Backlight();
334         zavai.registry.register_service(backlight);
335
336         battery_icon = new BatteryIcon();
337         battery_icon.set_visible(true);
338
339         power_menu = new PowerMenu();
340         zavai.registry.register_resource("powermenu", power_menu);
341         
342     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
343
344     /*
345         raise_icon = new RaiseIcon();
346         raise_icon.set_visible(true);
347
348         close_or_back = new CloseOrBack();
349         close_or_back.set_visible(true);
350
351         window_list = new WindowList("Current apps");
352         zavai.registry.register_applet("wm.list", window_list);
353         zavai.registry.getmenu("menu.main").add_applet("wm.list");
354
355         try {
356                 launcher = new Launcher("Run program");
357         } catch (Error e) {
358                 zavai.log.error("Not running launcher: " + e.message);
359                 launcher = null;
360         }
361
362         if (launcher != null)
363         {
364                 zavai.registry.register_applet("wm.launcher", launcher);
365                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
366         }
367     */
368 }
369
370 }
371 }
372 }