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