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