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