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