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