Removed workaround, sent patches to devkit-power instead
[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         public Dkp.Device battery;
237
238         public BatteryIcon(Dkp.Device dev)
239         {
240                 battery = dev;
241                 battery.changed += on_changed;
242
243                 stderr.printf("New battery icon for %s online %s perc %f isrec %s tte %lld ttf %lld\n", dev.native_path, dev.online ? "yes" : "no", dev.percentage, dev.is_rechargeable ? "yes" : "no", dev.time_to_empty, dev.time_to_full);
244
245                 /*
246                 battery = zavai.registry.sbus.get_object(
247                         "org.freesmartphone.odeviced",
248                         "/org/freesmartphone/Device/PowerSupply/battery",
249                         "org.freesmartphone.Device.PowerSupply");
250                 */
251
252                 // activate += on_activate;
253
254                 /*
255                 battery.PowerStatus += on_power_status;
256                 battery.Capacity += on_capacity;
257
258                 last_status = battery.GetPowerStatus();
259                 last_capacity = battery.GetCapacity();
260                 */
261                 
262                 update_icon();
263         }
264
265         private void on_changed(void* obj)
266         {
267                 update_icon();
268         }
269
270         /*
271         private void on_power_status(dynamic DBus.Object bat, string status)
272         {
273                 zavai.log.info("New battery status: " + status);
274                 last_status = status;
275                 update_icon();
276         }
277
278         private void on_capacity(dynamic DBus.Object bat, int val)
279         {
280 stderr.printf("NEW CAPACITY: %d\n", val);
281                 last_capacity = val;
282                 update_icon();
283         }
284         */
285
286         /*
287         private void on_activate()
288         {
289         }
290         */
291
292         protected void update_icon()
293         {
294                 string name = zavai.config.icondir + "/battery/";
295                 bool charging = false;
296                 Dkp.DeviceState state = (Dkp.DeviceState)battery.state;
297
298 stderr.printf("New battery status: %s\n", Dkp.Device.state_to_text(state));
299                 int capacity = (int)Math.round(battery.percentage/10);
300                 switch (state)
301                 {
302                         case Dkp.DeviceState.CHARGING:
303                                 name += "%02d0_charging_500.png".printf(capacity);
304                                 break;
305                         case Dkp.DeviceState.FULLY_CHARGED:
306                                 name += "100_charging_500.png";
307                                 break;
308                         case Dkp.DeviceState.UNKNOWN:
309                         case Dkp.DeviceState.DISCHARGING:
310                         case Dkp.DeviceState.EMPTY:
311                         case Dkp.DeviceState.PENDING_CHARGE:
312                         case Dkp.DeviceState.PENDING_DISCHARGE:
313                         case Dkp.DeviceState.LAST:
314                                 name += "%02d0.png".printf(capacity);
315                                 break;
316                 }
317
318 stderr.printf("Loading icon from %s\n", name);
319
320                 set_from_file(name);
321         }
322 }
323
324 public class ScreenLockButton : Gtk.Button
325 {
326         public ScreenLockButton()
327         {
328                 label = "Lock screen";
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("Locking screen");
336                 power.set_screen_lock(true);
337                 power_menu.hide_menu();
338         }
339 }
340
341 public class SuspendButton : Gtk.Button
342 {
343         public SuspendButton()
344         {
345                 label = "Suspend";
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("Suspending the phone via FSO");
353                 power.do_suspend();
354                 power_menu.hide_menu();
355         }
356 }
357
358 public class ShutdownButton : Gtk.Button
359 {
360         public ShutdownButton()
361         {
362                 label = "Shut down";
363                 clicked += on_clicked;
364                 set_size_request(0, zavai.config.min_button_height);
365         }
366
367         public void on_clicked()
368         {
369                 zavai.log.info("Shutting down the phone via FSO");
370                 power.do_shutdown();
371                 power_menu.hide_menu();
372         }
373 }
374
375 public class RebootButton : Gtk.Button
376 {
377         public RebootButton()
378         {
379                 label = "Reboot";
380                 clicked += on_clicked;
381                 set_size_request(0, zavai.config.min_button_height);
382         }
383
384         public void on_clicked()
385         {
386                 zavai.log.info("Rebooting the phone via FSO");
387                 power.do_reboot();
388                 power_menu.hide_menu();
389         }
390 }
391
392 // For a list of dbus services, look in /etc/dbus-1/system.d/
393 public class Backlight: zavai.Service
394 {
395         public dynamic DBus.Object usage;
396
397         public Backlight()
398         {
399                 name = "backlight";
400
401                 usage = zavai.registry.sbus.get_object(
402                         "org.freesmartphone.ousaged",
403                         "/org/freesmartphone/Usage",
404                         "org.freesmartphone.Usage");
405         }
406
407         // Turn the backlight and then let it fade off
408         public void wiggle()
409         {
410                 // There must be a better method
411                 try {
412                         //display.SetBacklightPower(true);
413                         //usage.SetResourcePolicy("Display", "auto");
414                         //usage.RequestResource("Display");
415                         //usage.ReleaseResource("Display");
416                         zavai.app.run_script(zavai.config.homedir + "/display wiggle");
417                 } catch (Error e) {
418                         zavai.log.error("Requesting/releasing resource Display: " + e.message);
419                 }
420         }
421
422         public void lock_screen()
423         {
424                 if (!started)
425                 {
426                         try {
427                                 //display.SetBacklightPower(false);
428                                 zavai.app.run_script(zavai.config.homedir + "/display lock_off");
429                                 /*
430                                 string policy = usage.GetResourcePolicy("Display");
431                                 if (policy == "auto")
432                                 {
433                                         usage.SetResourcePolicy("Display", "disabled");
434                                 }
435                                 */
436                         } catch (GLib.Error e) {
437                                 zavai.log.error(e.message);
438                         }
439                 }
440         }
441
442         public void unlock_screen()
443         {
444                 try {
445                         //display.SetBacklightPower(true);
446                         zavai.app.run_script(zavai.config.homedir + "/display defaults");
447                         //usage.SetResourcePolicy("Display", "auto");
448                 } catch (GLib.Error e) {
449                         zavai.log.error(e.message);
450                 }
451         }
452
453
454         /// Request GPS resource
455         public override void start()
456         {
457                 if (started) return;
458                 try {
459                         //usage.RequestResource("Display");
460                         zavai.app.run_script(zavai.config.homedir + "/display lock_on");
461                         zavai.log.info("Acquired display");
462                         base.start();
463                 } catch (GLib.Error e) {
464                         zavai.log.error(e.message);
465                 }
466                 base.start();
467         }
468
469         // Release usage of GPS
470         public override void stop()
471         {
472                 if (!started) return;
473                 try {
474                         //usage.ReleaseResource("Display");
475                         zavai.app.run_script(zavai.config.homedir + "/display defaults");
476                         zavai.log.info("Released display");
477                         base.stop();
478                 } catch (GLib.Error e) {
479                         zavai.log.error(e.message);
480                 }
481                 base.stop();
482         }
483 }
484
485 public class PowerMenu : zavai.Resource, Gtk.Window
486 {
487         protected Gtk.VBox vbox;
488         protected ScreenLockButton act_screen_lock;
489         protected SuspendButton act_suspend;
490         protected ShutdownButton act_shutdown;
491         protected RebootButton act_reboot;
492         protected ServiceRequestLink act_backlight_on;
493         protected bool shown;
494
495         public PowerMenu()
496         {
497                 type = Gtk.WindowType.TOPLEVEL;
498                 title = "Power Menu";
499                 shown = false;
500                 destroy_with_parent = true;
501                 set_transient_for(zavai.app);
502                 set_modal(true);
503                 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
504                 set_size_request(300, 500);
505
506                 vbox = new Gtk.VBox(false, 0);
507                 add(vbox);
508
509                 //destroy += Gtk.main_quit;
510                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
511                 //visibility_notify_event += on_visibility;
512                 set_skip_pager_hint(true);
513                 set_skip_taskbar_hint(true);
514                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
515
516                 act_screen_lock = new ScreenLockButton();
517                 vbox.pack_start(act_screen_lock, false, false, 0);
518
519                 act_suspend = new SuspendButton();
520                 vbox.pack_start(act_suspend, false, false, 0);
521
522                 act_shutdown = new ShutdownButton();
523                 vbox.pack_start(act_shutdown, false, false, 0);
524
525                 act_reboot = new RebootButton();
526                 vbox.pack_start(act_reboot, false, false, 0);
527
528                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
529                 act_backlight_on.toggled += (src) => { this.hide_menu(); };
530                 vbox.pack_start(act_backlight_on, false, false, 0);
531
532                 //vbox.show_all();
533         }
534
535         public void toggle()
536         {
537                 if (!shown)
538                 {
539                         show_all();
540                         show();
541                         visible = true;
542                         present();
543                         shown = true;
544                 } else {
545                         // TODO: do more in case it is visible but has no visibility (is covered by others)
546                         visible = !visible;
547                         if (visible)
548                                 present();
549                 }                               
550         }
551
552         public void hide_menu()
553         {
554                 visible = false;
555         }
556
557         public void shutdown() {}
558 }
559
560 /*
561 public class TogglePowerMenu : Gtk.Button
562 {
563         public TogglePowerMenu()
564         {
565                 label = "Toggle power menu";
566                 clicked += on_clicked;
567                 set_size_request(0, zavai.config.min_button_height);
568         }
569
570         public void on_clicked()
571         {
572                 zavai.log.info("Toggling power menu");
573                 power_menu.toggle();
574         }
575 }
576 */
577
578 Power power;
579 PowerMenu power_menu;
580 Gee.ArrayList<BatteryIcon> battery_icons;
581 Backlight backlight;
582 //TogglePowerMenu tpm;
583
584 public void init()
585 {
586         power = new Power();
587         backlight = new Backlight();
588         zavai.registry.register_service(backlight);
589         
590         battery_icons = new Gee.ArrayList<BatteryIcon>();
591         // Enumerate batteries
592         var c = new Dkp.Client();
593         unowned GLib.PtrArray devs = c.enumerate_devices();
594         for (int i = 0; i < devs.len; ++i)
595         {
596                 Dkp.Device dev = (Dkp.Device)devs.pdata[i];
597                 stderr.printf("Found new device %s\n", dev.native_path);
598                 dev.print();
599                 stderr.printf("Rechargeable: %s\n", dev.is_rechargeable ? "yes" : "no");
600                 if (!dev.is_rechargeable) continue;
601                 var bi = new BatteryIcon(dev);
602                 bi.set_visible(true);
603                 battery_icons.add(bi);
604         }
605
606         power_menu = new PowerMenu();
607         zavai.registry.register_resource("powermenu", power_menu);
608         
609     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
610         //tpm = new TogglePowerMenu();
611     //zavai.registry.getmenu("menu.main").add_widget(tpm);
612
613     /*
614         raise_icon = new RaiseIcon();
615         raise_icon.set_visible(true);
616
617         close_or_back = new CloseOrBack();
618         close_or_back.set_visible(true);
619
620         window_list = new WindowList("Current apps");
621         zavai.registry.register_applet("wm.list", window_list);
622         zavai.registry.getmenu("menu.main").add_applet("wm.list");
623
624         try {
625                 launcher = new Launcher("Run program");
626         } catch (Error e) {
627                 zavai.log.error("Not running launcher: " + e.message);
628                 launcher = null;
629         }
630
631         if (launcher != null)
632         {
633                 zavai.registry.register_applet("wm.launcher", launcher);
634                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
635         }
636     */
637 }
638
639 }
640 }
641 }