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