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