Wiggle screen on unlock
[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 power {
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         // Timestamp of the past power button pressed even (0 if the button has
33         // been released)
34         private time_t last_down;
35
36         private bool hide_after_closing_power_menu;
37
38         public signal void screen_lock_changed(bool state);
39
40         public Power()
41         {
42                 screen_locked = false;
43                 screen_lock_fd = -1;
44                 hide_after_closing_power_menu = false;
45                 last_down = 0;
46
47                 usage = zavai.registry.sbus.get_object(
48                         "org.freesmartphone.ousaged",
49                         "/org/freesmartphone/Usage",
50                         "org.freesmartphone.Usage");
51
52                 zavai.input.power_button.power_button += on_power_button;
53                 zavai.input.power_button.request("zavai.ui.powerbutton.power");
54         }
55
56         public void shutdown()
57         {
58                 zavai.input.power_button.release("zavai.ui.powerbutton.power");
59         }
60
61         public void do_suspend() { usage.Suspend(); }
62         public void do_shutdown() { usage.Shutdown(); }
63         public void do_reboot() { usage.Reboot(); }
64
65         public void set_screen_lock(bool locked)
66         {
67                 if (locked && screen_locked)
68                         return;
69                 if (!locked && !screen_locked)
70                         return;
71
72                 if (locked)
73                 {
74                         screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
75                         if (screen_lock_fd < 0)
76                         {
77                                 zavai.log.error("Cannot open /dev/input/event1");
78                                 return;
79                         }
80
81                         int EVIOCGRAB = 0x40044590;
82                         if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
83                         {
84                                 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
85                                 Posix.close(screen_lock_fd);
86                                 return;
87                         }
88                 } else {
89                         Posix.close(screen_lock_fd);
90                 }
91                 screen_locked = locked;
92                 if (!locked)
93                         backlight.wiggle();
94
95                 screen_lock_changed(locked);
96         }
97
98         private void on_power_button(Posix.timeval* t, bool pressed)
99         {
100                 if (pressed)
101                 {
102                         last_down = t->tv_sec;
103                 } else {
104                         if (screen_locked)
105                         {
106                                 time_t now = new time_t();
107                                 if (now < last_down + 2)
108                                 {
109                                         // Short press: turn on backlight for a bit
110                                         backlight.wiggle();
111                                 } else {
112                                         // Long press: unlock
113                                         set_screen_lock(false);
114                                 }
115                         }
116                         else
117                         {
118                                 power_menu.toggle();
119                         }
120                         last_down = 0;
121                 }
122         }
123 }
124
125 public class BatteryIcon : Gtk.StatusIcon
126 {
127         public dynamic DBus.Object battery;
128         protected string last_status;
129         protected int last_capacity;
130
131         public BatteryIcon()
132         {
133                 battery = zavai.registry.sbus.get_object(
134                         "org.freesmartphone.odeviced",
135                         "/org/freesmartphone/Device/PowerSupply/battery",
136                         "org.freesmartphone.Device.PowerSupply");
137
138                 // activate += on_activate;
139
140                 battery.PowerStatus += on_power_status;
141                 battery.Capacity += on_capacity;
142
143                 last_status = battery.GetPowerStatus();
144                 last_capacity = battery.GetCapacity();
145                 
146                 update_icon();
147         }
148
149         private void on_power_status(dynamic DBus.Object bat, string status)
150         {
151                 zavai.log.info("New battery status: " + status);
152                 last_status = status;
153                 update_icon();
154         }
155
156         private void on_capacity(dynamic DBus.Object bat, int val)
157         {
158 stderr.printf("NEW CAPACITY: %d\n", val);
159                 last_capacity = val;
160                 update_icon();
161         }
162
163         /*
164         private void on_activate()
165         {
166         }
167         */
168
169         protected void update_icon()
170         {
171                 string name = zavai.config.icondir + "/battery/";
172
173                 if (last_status == "charging")
174                         name += "%02d0_charging_500.png".printf(last_capacity/10);
175                 else
176                         name += "%02d0.png".printf(last_capacity/10);
177
178 stderr.printf("Loading icon from %s\n", name);
179
180                 set_from_file(name);
181         }
182 }
183
184 public class ScreenLockButton : Gtk.Button
185 {
186         public ScreenLockButton()
187         {
188                 label = "Lock screen";
189                 clicked += on_clicked;
190                 set_size_request(0, zavai.config.min_button_height);
191         }
192
193         public void on_clicked()
194         {
195                 zavai.log.info("Locking screen");
196                 power.set_screen_lock(true);
197                 power_menu.hide();
198         }
199 }
200
201 public class SuspendButton : Gtk.Button
202 {
203         public SuspendButton()
204         {
205                 label = "Suspend";
206                 clicked += on_clicked;
207                 set_size_request(0, zavai.config.min_button_height);
208         }
209
210         public void on_clicked()
211         {
212                 zavai.log.info("Suspending the phone via FSO");
213                 power.do_suspend();
214                 power_menu.hide();
215         }
216 }
217
218 public class ShutdownButton : Gtk.Button
219 {
220         public ShutdownButton()
221         {
222                 label = "Shut down";
223                 clicked += on_clicked;
224                 set_size_request(0, zavai.config.min_button_height);
225         }
226
227         public void on_clicked()
228         {
229                 zavai.log.info("Shutting down the phone via FSO");
230                 power.do_shutdown();
231                 power_menu.hide();
232         }
233 }
234
235 public class RebootButton : Gtk.Button
236 {
237         public RebootButton()
238         {
239                 label = "Reboot";
240                 clicked += on_clicked;
241                 set_size_request(0, zavai.config.min_button_height);
242         }
243
244         public void on_clicked()
245         {
246                 zavai.log.info("Rebooting the phone via FSO");
247                 power.do_reboot();
248                 power_menu.hide();
249         }
250 }
251
252 // For a list of dbus services, look in /etc/dbus-1/system.d/
253 public class Backlight: zavai.Service
254 {
255         public dynamic DBus.Object usage;
256
257         public Backlight()
258         {
259                 name = "backlight";
260
261                 usage = zavai.registry.sbus.get_object(
262                         "org.freesmartphone.ousaged",
263                         "/org/freesmartphone/Usage",
264                         "org.freesmartphone.Usage");
265         }
266
267         // Turn the backlight and then let it fade off
268         public void wiggle()
269         {
270                 // There must be a better method
271                 usage.RequestResource("Display");
272                 usage.ReleaseResource("Display");
273         }
274
275         /// Request GPS resource
276         public override void start()
277         {
278                 if (started) return;
279                 try {
280                         usage.RequestResource("Display");
281                         zavai.log.info("Acquired display");
282                         base.start();
283                 } catch (GLib.Error e) {
284                         zavai.log.error(e.message);
285                 }
286                 base.start();
287         }
288
289         // Release usage of GPS
290         public override void stop()
291         {
292                 if (!started) return;
293                 try {
294                         usage.ReleaseResource("Display");
295                         zavai.log.info("Released display");
296                         base.stop();
297                 } catch (GLib.Error e) {
298                         zavai.log.error(e.message);
299                 }
300                 base.stop();
301         }
302 }
303
304 public class PowerMenu : zavai.Resource, Gtk.Window
305 {
306         protected Gtk.VBox vbox;
307         protected ScreenLockButton act_screen_lock;
308         protected SuspendButton act_suspend;
309         protected ShutdownButton act_shutdown;
310         protected RebootButton act_reboot;
311         protected ServiceRequestLink act_backlight_on;
312         protected bool shown;
313
314         public PowerMenu()
315         {
316                 type = Gtk.WindowType.TOPLEVEL;
317                 title = "Power Menu";
318                 shown = false;
319                 destroy_with_parent = true;
320                 set_transient_for(zavai.app);
321                 set_modal(true);
322                 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
323                 set_size_request(300, 500);
324
325                 vbox = new Gtk.VBox(false, 0);
326                 add(vbox);
327
328                 //destroy += Gtk.main_quit;
329                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
330                 //visibility_notify_event += on_visibility;
331                 set_skip_pager_hint(true);
332                 set_skip_taskbar_hint(true);
333                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
334
335                 act_screen_lock = new ScreenLockButton();
336                 vbox.pack_start(act_screen_lock, false, false, 0);
337
338                 act_suspend = new SuspendButton();
339                 vbox.pack_start(act_suspend, false, false, 0);
340
341                 act_shutdown = new ShutdownButton();
342                 vbox.pack_start(act_shutdown, false, false, 0);
343
344                 act_reboot = new RebootButton();
345                 vbox.pack_start(act_reboot, false, false, 0);
346
347                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
348                 vbox.pack_start(act_backlight_on, false, false, 0);
349
350                 //vbox.show_all();
351         }
352
353         public void toggle()
354         {
355                 if (!shown)
356                 {
357                         show_all();
358                         show();
359                         visible = true;
360                         present();
361                         shown = true;
362                 } else {
363                         // TODO: do more in case it is visible but has no visibility (is covered by others)
364                         visible = !visible;
365                         if (visible)
366                                 present();
367                 }                               
368         }
369
370         public void hide()
371         {
372                 visible = false;
373         }
374
375         public void shutdown() {}
376 }
377
378 /*
379 public class TogglePowerMenu : Gtk.Button
380 {
381         public TogglePowerMenu()
382         {
383                 label = "Toggle power menu";
384                 clicked += on_clicked;
385                 set_size_request(0, zavai.config.min_button_height);
386         }
387
388         public void on_clicked()
389         {
390                 zavai.log.info("Toggling power menu");
391                 power_menu.toggle();
392         }
393 }
394 */
395
396 Power power;
397 PowerMenu power_menu;
398 BatteryIcon battery_icon;
399 Backlight backlight;
400 //TogglePowerMenu tpm;
401
402 public void init()
403 {
404         power = new Power();
405         backlight = new Backlight();
406         zavai.registry.register_service(backlight);
407
408         battery_icon = new BatteryIcon();
409         battery_icon.set_visible(true);
410
411         power_menu = new PowerMenu();
412         zavai.registry.register_resource("powermenu", power_menu);
413         
414     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
415         //tpm = new TogglePowerMenu();
416     //zavai.registry.getmenu("menu.main").add_widget(tpm);
417
418     /*
419         raise_icon = new RaiseIcon();
420         raise_icon.set_visible(true);
421
422         close_or_back = new CloseOrBack();
423         close_or_back.set_visible(true);
424
425         window_list = new WindowList("Current apps");
426         zavai.registry.register_applet("wm.list", window_list);
427         zavai.registry.getmenu("menu.main").add_applet("wm.list");
428
429         try {
430                 launcher = new Launcher("Run program");
431         } catch (Error e) {
432                 zavai.log.error("Not running launcher: " + e.message);
433                 launcher = null;
434         }
435
436         if (launcher != null)
437         {
438                 zavai.registry.register_applet("wm.launcher", launcher);
439                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
440         }
441     */
442 }
443
444 }
445 }
446 }