Show clock on double power short 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) power_long_press();
191                 if (short_press) power_short_press(t);
192         }
193
194         private void on_power_short_press(Posix.timeval* t)
195         {
196                 long diff = timediff(t, &last_short_press);
197                 bool combo = diff <= 1000000;
198                 last_short_press = *t;
199
200                 if (screen_locked)
201                 {
202                         // Short press: turn on backlight for a bit
203                         backlight.wiggle();
204                         if (combo)
205                         {
206                                 app.back_to_main();
207                                 app.toggle_visibility();
208                         }
209                 }
210                 else
211                         // Short press: toggle power menu
212                         power_menu.toggle();
213         }
214
215         private void on_power_long_press()
216         {
217                 if (screen_locked)
218                         // Long press: unlock
219                         set_screen_lock(false);
220                 else
221                         // Long press: lock screen
222                         set_screen_lock(true);
223         }
224 }
225
226 public class BatteryIcon : Gtk.StatusIcon
227 {
228         public dynamic DBus.Object battery;
229         protected string last_status;
230         protected int last_capacity;
231
232         public BatteryIcon()
233         {
234                 battery = zavai.registry.sbus.get_object(
235                         "org.freesmartphone.odeviced",
236                         "/org/freesmartphone/Device/PowerSupply/battery",
237                         "org.freesmartphone.Device.PowerSupply");
238
239                 // activate += on_activate;
240
241                 battery.PowerStatus += on_power_status;
242                 battery.Capacity += on_capacity;
243
244                 last_status = battery.GetPowerStatus();
245                 last_capacity = battery.GetCapacity();
246                 
247                 update_icon();
248         }
249
250         private void on_power_status(dynamic DBus.Object bat, string status)
251         {
252                 zavai.log.info("New battery status: " + status);
253                 last_status = status;
254                 update_icon();
255         }
256
257         private void on_capacity(dynamic DBus.Object bat, int val)
258         {
259 stderr.printf("NEW CAPACITY: %d\n", val);
260                 last_capacity = val;
261                 update_icon();
262         }
263
264         /*
265         private void on_activate()
266         {
267         }
268         */
269
270         protected void update_icon()
271         {
272                 string name = zavai.config.icondir + "/battery/";
273
274                 if (last_status == "charging")
275                         name += "%02d0_charging_500.png".printf(last_capacity/10);
276                 else
277                         name += "%02d0.png".printf(last_capacity/10);
278
279 stderr.printf("Loading icon from %s\n", name);
280
281                 set_from_file(name);
282         }
283 }
284
285 public class ScreenLockButton : Gtk.Button
286 {
287         public ScreenLockButton()
288         {
289                 label = "Lock screen";
290                 clicked += on_clicked;
291                 set_size_request(0, zavai.config.min_button_height);
292         }
293
294         public void on_clicked()
295         {
296                 zavai.log.info("Locking screen");
297                 power.set_screen_lock(true);
298                 power_menu.hide_menu();
299         }
300 }
301
302 public class SuspendButton : Gtk.Button
303 {
304         public SuspendButton()
305         {
306                 label = "Suspend";
307                 clicked += on_clicked;
308                 set_size_request(0, zavai.config.min_button_height);
309         }
310
311         public void on_clicked()
312         {
313                 zavai.log.info("Suspending the phone via FSO");
314                 power.do_suspend();
315                 power_menu.hide_menu();
316         }
317 }
318
319 public class ShutdownButton : Gtk.Button
320 {
321         public ShutdownButton()
322         {
323                 label = "Shut down";
324                 clicked += on_clicked;
325                 set_size_request(0, zavai.config.min_button_height);
326         }
327
328         public void on_clicked()
329         {
330                 zavai.log.info("Shutting down the phone via FSO");
331                 power.do_shutdown();
332                 power_menu.hide_menu();
333         }
334 }
335
336 public class RebootButton : Gtk.Button
337 {
338         public RebootButton()
339         {
340                 label = "Reboot";
341                 clicked += on_clicked;
342                 set_size_request(0, zavai.config.min_button_height);
343         }
344
345         public void on_clicked()
346         {
347                 zavai.log.info("Rebooting the phone via FSO");
348                 power.do_reboot();
349                 power_menu.hide_menu();
350         }
351 }
352
353 // For a list of dbus services, look in /etc/dbus-1/system.d/
354 public class Backlight: zavai.Service
355 {
356         public dynamic DBus.Object usage;
357         public dynamic DBus.Object display;
358
359         public Backlight()
360         {
361                 name = "backlight";
362
363                 usage = zavai.registry.sbus.get_object(
364                         "org.freesmartphone.ousaged",
365                         "/org/freesmartphone/Usage",
366                         "org.freesmartphone.Usage");
367
368                 display = zavai.registry.sbus.get_object(
369                         "org.freesmartphone.odeviced",
370                         "/org/freesmartphone/Device/Display/0",
371                         "org.freesmartphone.Device.Display");
372         }
373
374         // Turn the backlight and then let it fade off
375         public void wiggle()
376         {
377                 // There must be a better method
378                 try {
379                         display.SetBacklightPower(true);
380                         //usage.SetResourcePolicy("Display", "auto");
381                         usage.RequestResource("Display");
382                         usage.ReleaseResource("Display");
383                 } catch (Error e) {
384                         zavai.log.error("Requesting/releasing resource Display: " + e.message);
385                 }
386         }
387
388         public void lock_screen()
389         {
390                 if (!started)
391                 {
392                         try {
393                                 display.SetBacklightPower(false);
394                                 /*
395                                 string policy = usage.GetResourcePolicy("Display");
396                                 if (policy == "auto")
397                                 {
398                                         usage.SetResourcePolicy("Display", "disabled");
399                                 }
400                                 */
401                         } catch (GLib.Error e) {
402                                 zavai.log.error(e.message);
403                         }
404                 }
405         }
406
407         public void unlock_screen()
408         {
409                 try {
410                         display.SetBacklightPower(true);
411                         //usage.SetResourcePolicy("Display", "auto");
412                 } catch (GLib.Error e) {
413                         zavai.log.error(e.message);
414                 }
415         }
416
417
418         /// Request GPS resource
419         public override void start()
420         {
421                 if (started) return;
422                 try {
423                         usage.RequestResource("Display");
424                         zavai.log.info("Acquired display");
425                         base.start();
426                 } catch (GLib.Error e) {
427                         zavai.log.error(e.message);
428                 }
429                 base.start();
430         }
431
432         // Release usage of GPS
433         public override void stop()
434         {
435                 if (!started) return;
436                 try {
437                         usage.ReleaseResource("Display");
438                         zavai.log.info("Released display");
439                         base.stop();
440                 } catch (GLib.Error e) {
441                         zavai.log.error(e.message);
442                 }
443                 base.stop();
444         }
445 }
446
447 public class PowerMenu : zavai.Resource, Gtk.Window
448 {
449         protected Gtk.VBox vbox;
450         protected ScreenLockButton act_screen_lock;
451         protected SuspendButton act_suspend;
452         protected ShutdownButton act_shutdown;
453         protected RebootButton act_reboot;
454         protected ServiceRequestLink act_backlight_on;
455         protected bool shown;
456
457         public PowerMenu()
458         {
459                 type = Gtk.WindowType.TOPLEVEL;
460                 title = "Power Menu";
461                 shown = false;
462                 destroy_with_parent = true;
463                 set_transient_for(zavai.app);
464                 set_modal(true);
465                 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
466                 set_size_request(300, 500);
467
468                 vbox = new Gtk.VBox(false, 0);
469                 add(vbox);
470
471                 //destroy += Gtk.main_quit;
472                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
473                 //visibility_notify_event += on_visibility;
474                 set_skip_pager_hint(true);
475                 set_skip_taskbar_hint(true);
476                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
477
478                 act_screen_lock = new ScreenLockButton();
479                 vbox.pack_start(act_screen_lock, false, false, 0);
480
481                 act_suspend = new SuspendButton();
482                 vbox.pack_start(act_suspend, false, false, 0);
483
484                 act_shutdown = new ShutdownButton();
485                 vbox.pack_start(act_shutdown, false, false, 0);
486
487                 act_reboot = new RebootButton();
488                 vbox.pack_start(act_reboot, false, false, 0);
489
490                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
491                 act_backlight_on.toggled += (src) => { this.hide_menu(); };
492                 vbox.pack_start(act_backlight_on, false, false, 0);
493
494                 //vbox.show_all();
495         }
496
497         public void toggle()
498         {
499                 if (!shown)
500                 {
501                         show_all();
502                         show();
503                         visible = true;
504                         present();
505                         shown = true;
506                 } else {
507                         // TODO: do more in case it is visible but has no visibility (is covered by others)
508                         visible = !visible;
509                         if (visible)
510                                 present();
511                 }                               
512         }
513
514         public void hide_menu()
515         {
516                 visible = false;
517         }
518
519         public void shutdown() {}
520 }
521
522 /*
523 public class TogglePowerMenu : Gtk.Button
524 {
525         public TogglePowerMenu()
526         {
527                 label = "Toggle power menu";
528                 clicked += on_clicked;
529                 set_size_request(0, zavai.config.min_button_height);
530         }
531
532         public void on_clicked()
533         {
534                 zavai.log.info("Toggling power menu");
535                 power_menu.toggle();
536         }
537 }
538 */
539
540 Power power;
541 PowerMenu power_menu;
542 BatteryIcon battery_icon;
543 Backlight backlight;
544 //TogglePowerMenu tpm;
545
546 public void init()
547 {
548         power = new Power();
549         backlight = new Backlight();
550         zavai.registry.register_service(backlight);
551
552         battery_icon = new BatteryIcon();
553         battery_icon.set_visible(true);
554
555         power_menu = new PowerMenu();
556         zavai.registry.register_resource("powermenu", power_menu);
557         
558     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
559         //tpm = new TogglePowerMenu();
560     //zavai.registry.getmenu("menu.main").add_widget(tpm);
561
562     /*
563         raise_icon = new RaiseIcon();
564         raise_icon.set_visible(true);
565
566         close_or_back = new CloseOrBack();
567         close_or_back.set_visible(true);
568
569         window_list = new WindowList("Current apps");
570         zavai.registry.register_applet("wm.list", window_list);
571         zavai.registry.getmenu("menu.main").add_applet("wm.list");
572
573         try {
574                 launcher = new Launcher("Run program");
575         } catch (Error e) {
576                 zavai.log.error("Not running launcher: " + e.message);
577                 launcher = null;
578         }
579
580         if (launcher != null)
581         {
582                 zavai.registry.register_applet("wm.launcher", launcher);
583                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
584         }
585     */
586 }
587
588 }
589 }
590 }