Alternate way to blank screen on lock
[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 // Compute a-b in microseconds
28 static long timediff(Posix.timeval* a, Posix.timeval* b)
29 {
30         return (a->tv_sec - b->tv_sec) * 1000000 + (a->tv_usec - b->tv_usec);
31 }
32
33 public class Power : zavai.Resource, Object
34 {
35         public dynamic DBus.Object usage;
36         public bool screen_locked;
37         private int screen_lock_fd;
38         // Timestamp of the past power button pressed even (0 if the button has
39         // been released)
40         private Posix.timeval last_down;
41
42         private bool hide_after_closing_power_menu;
43
44         public signal void screen_lock_changed(bool state);
45
46         public signal void power_short_press();
47         public signal void power_long_press();
48         private uint button_press_timeout;
49
50         public Power()
51         {
52                 screen_locked = false;
53                 screen_lock_fd = -1;
54                 hide_after_closing_power_menu = false;
55                 last_down.tv_sec = 0;
56                 last_down.tv_usec = 0;
57                 button_press_timeout = 0;
58
59                 usage = zavai.registry.sbus.get_object(
60                         "org.freesmartphone.ousaged",
61                         "/org/freesmartphone/Usage",
62                         "org.freesmartphone.Usage");
63
64                 zavai.input.power_button.power_button += on_power_button;
65                 zavai.input.power_button.request("zavai.ui.powerbutton.power");
66
67                 power_short_press += on_power_short_press;
68                 power_long_press += on_power_long_press;
69         }
70
71         public void shutdown()
72         {
73                 zavai.input.power_button.release("zavai.ui.powerbutton.power");
74         }
75
76         public void do_suspend()
77         {
78                 try {
79                         usage.Suspend();
80                 } catch (Error e) {
81                         zavai.log.error("Suspending phone: " + e.message);
82                 }
83         }
84         public void do_shutdown()
85         {
86                 try {
87                         usage.Shutdown();
88                 } catch (Error e) {
89                         zavai.log.error("Shutting down phone: " + e.message);
90                 }
91         }
92         public void do_reboot()
93         {
94                 try {
95                         usage.Reboot();
96                 } catch (Error e) {
97                         zavai.log.error("Rebooting phone: " + e.message);
98                 }
99         }
100
101         public void set_screen_lock(bool locked)
102         {
103                 if (locked && screen_locked)
104                         return;
105                 if (!locked && !screen_locked)
106                         return;
107
108                 if (locked)
109                 {
110                         screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
111                         if (screen_lock_fd < 0)
112                         {
113                                 zavai.log.error("Cannot open /dev/input/event1");
114                                 return;
115                         }
116
117                         // FIXME: X won't see events, but it's still generating interrupts,
118                         // isn't it?
119                         int EVIOCGRAB = 0x40044590;
120                         if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
121                         {
122                                 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
123                                 Posix.close(screen_lock_fd);
124                                 return;
125                         }
126
127                         backlight.lock_screen();
128                 } else {
129                         Posix.close(screen_lock_fd);
130                         backlight.unlock_screen();
131                 }
132                 screen_locked = locked;
133                 if (!locked)
134                         backlight.wiggle();
135
136                 screen_lock_changed(locked);
137         }
138
139         private bool on_power_button_timeout()
140         {
141                 last_down.tv_sec = 0;
142                 last_down.tv_usec = 0;
143                 power_long_press();
144                 // Do not reschedule
145                 return false;
146         }
147
148         private void on_power_button(Posix.timeval* t, bool pressed)
149         {
150                 bool short_press = false;
151                 bool long_press = false;
152
153                 if (pressed)
154                 {
155                         if (last_down.tv_sec == 0)
156                         {
157                                 last_down = *t;
158                                 button_press_timeout = Timeout.add(1000, on_power_button_timeout);
159                         }
160                         else
161                         {
162                                 long diff = timediff(t, &last_down);
163                                 long_press = diff >= 1000000;
164                         }
165                 } else {
166                         if (last_down.tv_sec == 0)
167                         {
168                                 // Ignore: release has been simulated with the timeout
169                         } else {
170                                 if (button_press_timeout != 0)
171                                 {
172                                         // Cancel the timeout
173                                         Source.remove(button_press_timeout);
174                                         button_press_timeout = 0;
175                                 }
176                                 long diff = timediff(t, &last_down);
177                                 if (diff >= 1000000)
178                                         long_press = true;
179                                 else
180                                         short_press = true;
181
182                                 last_down.tv_sec = 0;
183                                 last_down.tv_usec = 0;
184                         }
185                 }
186
187                 if (long_press) power_long_press();
188                 if (short_press) power_short_press();
189         }
190
191         private void on_power_short_press()
192         {
193                 if (screen_locked)
194                         // Short press: turn on backlight for a bit
195                         backlight.wiggle();
196                 else
197                         // Short press: toggle power menu
198                         power_menu.toggle();
199         }
200
201         private void on_power_long_press()
202         {
203                 if (screen_locked)
204                         // Long press: unlock
205                         set_screen_lock(false);
206                 else
207                         // Long press: lock screen
208                         set_screen_lock(true);
209         }
210 }
211
212 public class BatteryIcon : Gtk.StatusIcon
213 {
214         public dynamic DBus.Object battery;
215         protected string last_status;
216         protected int last_capacity;
217
218         public BatteryIcon()
219         {
220                 battery = zavai.registry.sbus.get_object(
221                         "org.freesmartphone.odeviced",
222                         "/org/freesmartphone/Device/PowerSupply/battery",
223                         "org.freesmartphone.Device.PowerSupply");
224
225                 // activate += on_activate;
226
227                 battery.PowerStatus += on_power_status;
228                 battery.Capacity += on_capacity;
229
230                 last_status = battery.GetPowerStatus();
231                 last_capacity = battery.GetCapacity();
232                 
233                 update_icon();
234         }
235
236         private void on_power_status(dynamic DBus.Object bat, string status)
237         {
238                 zavai.log.info("New battery status: " + status);
239                 last_status = status;
240                 update_icon();
241         }
242
243         private void on_capacity(dynamic DBus.Object bat, int val)
244         {
245 stderr.printf("NEW CAPACITY: %d\n", val);
246                 last_capacity = val;
247                 update_icon();
248         }
249
250         /*
251         private void on_activate()
252         {
253         }
254         */
255
256         protected void update_icon()
257         {
258                 string name = zavai.config.icondir + "/battery/";
259
260                 if (last_status == "charging")
261                         name += "%02d0_charging_500.png".printf(last_capacity/10);
262                 else
263                         name += "%02d0.png".printf(last_capacity/10);
264
265 stderr.printf("Loading icon from %s\n", name);
266
267                 set_from_file(name);
268         }
269 }
270
271 public class ScreenLockButton : Gtk.Button
272 {
273         public ScreenLockButton()
274         {
275                 label = "Lock screen";
276                 clicked += on_clicked;
277                 set_size_request(0, zavai.config.min_button_height);
278         }
279
280         public void on_clicked()
281         {
282                 zavai.log.info("Locking screen");
283                 power.set_screen_lock(true);
284                 power_menu.hide_menu();
285         }
286 }
287
288 public class SuspendButton : Gtk.Button
289 {
290         public SuspendButton()
291         {
292                 label = "Suspend";
293                 clicked += on_clicked;
294                 set_size_request(0, zavai.config.min_button_height);
295         }
296
297         public void on_clicked()
298         {
299                 zavai.log.info("Suspending the phone via FSO");
300                 power.do_suspend();
301                 power_menu.hide_menu();
302         }
303 }
304
305 public class ShutdownButton : Gtk.Button
306 {
307         public ShutdownButton()
308         {
309                 label = "Shut down";
310                 clicked += on_clicked;
311                 set_size_request(0, zavai.config.min_button_height);
312         }
313
314         public void on_clicked()
315         {
316                 zavai.log.info("Shutting down the phone via FSO");
317                 power.do_shutdown();
318                 power_menu.hide_menu();
319         }
320 }
321
322 public class RebootButton : Gtk.Button
323 {
324         public RebootButton()
325         {
326                 label = "Reboot";
327                 clicked += on_clicked;
328                 set_size_request(0, zavai.config.min_button_height);
329         }
330
331         public void on_clicked()
332         {
333                 zavai.log.info("Rebooting the phone via FSO");
334                 power.do_reboot();
335                 power_menu.hide_menu();
336         }
337 }
338
339 // For a list of dbus services, look in /etc/dbus-1/system.d/
340 public class Backlight: zavai.Service
341 {
342         public dynamic DBus.Object usage;
343         public dynamic DBus.Object display;
344
345         public Backlight()
346         {
347                 name = "backlight";
348
349                 usage = zavai.registry.sbus.get_object(
350                         "org.freesmartphone.ousaged",
351                         "/org/freesmartphone/Usage",
352                         "org.freesmartphone.Usage");
353
354                 display = zavai.registry.sbus.get_object(
355                         "org.freesmartphone.odeviced",
356                         "/org/freesmartphone/Device/Display/0",
357                         "org.freesmartphone.Device.Display");
358         }
359
360         // Turn the backlight and then let it fade off
361         public void wiggle()
362         {
363                 // There must be a better method
364                 try {
365                         display.SetBacklightPower(true);
366                         //usage.SetResourcePolicy("Display", "auto");
367                         usage.RequestResource("Display");
368                         usage.ReleaseResource("Display");
369                 } catch (Error e) {
370                         zavai.log.error("Requesting/releasing resource Display: " + e.message);
371                 }
372         }
373
374         public void lock_screen()
375         {
376                 if (!started)
377                 {
378                         try {
379                                 display.SetBacklightPower(false);
380                                 /*
381                                 string policy = usage.GetResourcePolicy("Display");
382                                 if (policy == "auto")
383                                 {
384                                         usage.SetResourcePolicy("Display", "disabled");
385                                 }
386                                 */
387                         } catch (GLib.Error e) {
388                                 zavai.log.error(e.message);
389                         }
390                 }
391         }
392
393         public void unlock_screen()
394         {
395                 try {
396                         display.SetBacklightPower(true);
397                         //usage.SetResourcePolicy("Display", "auto");
398                 } catch (GLib.Error e) {
399                         zavai.log.error(e.message);
400                 }
401         }
402
403
404         /// Request GPS resource
405         public override void start()
406         {
407                 if (started) return;
408                 try {
409                         usage.RequestResource("Display");
410                         zavai.log.info("Acquired display");
411                         base.start();
412                 } catch (GLib.Error e) {
413                         zavai.log.error(e.message);
414                 }
415                 base.start();
416         }
417
418         // Release usage of GPS
419         public override void stop()
420         {
421                 if (!started) return;
422                 try {
423                         usage.ReleaseResource("Display");
424                         zavai.log.info("Released display");
425                         base.stop();
426                 } catch (GLib.Error e) {
427                         zavai.log.error(e.message);
428                 }
429                 base.stop();
430         }
431 }
432
433 public class PowerMenu : zavai.Resource, Gtk.Window
434 {
435         protected Gtk.VBox vbox;
436         protected ScreenLockButton act_screen_lock;
437         protected SuspendButton act_suspend;
438         protected ShutdownButton act_shutdown;
439         protected RebootButton act_reboot;
440         protected ServiceRequestLink act_backlight_on;
441         protected bool shown;
442
443         public PowerMenu()
444         {
445                 type = Gtk.WindowType.TOPLEVEL;
446                 title = "Power Menu";
447                 shown = false;
448                 destroy_with_parent = true;
449                 set_transient_for(zavai.app);
450                 set_modal(true);
451                 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
452                 set_size_request(300, 500);
453
454                 vbox = new Gtk.VBox(false, 0);
455                 add(vbox);
456
457                 //destroy += Gtk.main_quit;
458                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
459                 //visibility_notify_event += on_visibility;
460                 set_skip_pager_hint(true);
461                 set_skip_taskbar_hint(true);
462                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
463
464                 act_screen_lock = new ScreenLockButton();
465                 vbox.pack_start(act_screen_lock, false, false, 0);
466
467                 act_suspend = new SuspendButton();
468                 vbox.pack_start(act_suspend, false, false, 0);
469
470                 act_shutdown = new ShutdownButton();
471                 vbox.pack_start(act_shutdown, false, false, 0);
472
473                 act_reboot = new RebootButton();
474                 vbox.pack_start(act_reboot, false, false, 0);
475
476                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
477                 act_backlight_on.toggled += (src) => { this.hide_menu(); };
478                 vbox.pack_start(act_backlight_on, false, false, 0);
479
480                 //vbox.show_all();
481         }
482
483         public void toggle()
484         {
485                 if (!shown)
486                 {
487                         show_all();
488                         show();
489                         visible = true;
490                         present();
491                         shown = true;
492                 } else {
493                         // TODO: do more in case it is visible but has no visibility (is covered by others)
494                         visible = !visible;
495                         if (visible)
496                                 present();
497                 }                               
498         }
499
500         public void hide_menu()
501         {
502                 visible = false;
503         }
504
505         public void shutdown() {}
506 }
507
508 /*
509 public class TogglePowerMenu : Gtk.Button
510 {
511         public TogglePowerMenu()
512         {
513                 label = "Toggle power menu";
514                 clicked += on_clicked;
515                 set_size_request(0, zavai.config.min_button_height);
516         }
517
518         public void on_clicked()
519         {
520                 zavai.log.info("Toggling power menu");
521                 power_menu.toggle();
522         }
523 }
524 */
525
526 Power power;
527 PowerMenu power_menu;
528 BatteryIcon battery_icon;
529 Backlight backlight;
530 //TogglePowerMenu tpm;
531
532 public void init()
533 {
534         power = new Power();
535         backlight = new Backlight();
536         zavai.registry.register_service(backlight);
537
538         battery_icon = new BatteryIcon();
539         battery_icon.set_visible(true);
540
541         power_menu = new PowerMenu();
542         zavai.registry.register_resource("powermenu", power_menu);
543         
544     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
545         //tpm = new TogglePowerMenu();
546     //zavai.registry.getmenu("menu.main").add_widget(tpm);
547
548     /*
549         raise_icon = new RaiseIcon();
550         raise_icon.set_visible(true);
551
552         close_or_back = new CloseOrBack();
553         close_or_back.set_visible(true);
554
555         window_list = new WindowList("Current apps");
556         zavai.registry.register_applet("wm.list", window_list);
557         zavai.registry.getmenu("menu.main").add_applet("wm.list");
558
559         try {
560                 launcher = new Launcher("Run program");
561         } catch (Error e) {
562                 zavai.log.error("Not running launcher: " + e.message);
563                 launcher = null;
564         }
565
566         if (launcher != null)
567         {
568                 zavai.registry.register_applet("wm.launcher", launcher);
569                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
570         }
571     */
572 }
573
574 }
575 }
576 }