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