98d351ab4937471e4935d856ad74d346c198dcf2
[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 public class Power : zavai.Resource, Object
28 {
29         public dynamic DBus.Object usage;
30         public bool screen_locked;
31         private int screen_lock_fd;
32
33         private bool hide_after_closing_power_menu;
34
35         public Power()
36         {
37                 screen_locked = false;
38                 screen_lock_fd = -1;
39                 hide_after_closing_power_menu = false;
40
41                 usage = zavai.registry.sbus.get_object(
42                         "org.freesmartphone.ousaged",
43                         "/org/freesmartphone/Usage",
44                         "org.freesmartphone.Usage");
45
46                 zavai.input.power_button.power_button += on_power_button;
47                 zavai.input.power_button.request("zavai.ui.powerbutton.power");
48         }
49
50         public void shutdown()
51         {
52                 zavai.input.power_button.release("zavai.ui.powerbutton.power");
53         }
54
55         public void do_suspend() { usage.Suspend(); }
56         public void do_shutdown() { usage.Shutdown(); }
57         public void do_reboot() { usage.Reboot(); }
58
59         public void set_screen_lock(bool locked)
60         {
61                 if (locked && screen_locked)
62                         return;
63                 if (!locked && !screen_locked)
64                         return;
65
66                 if (locked)
67                 {
68                         screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
69                         if (screen_lock_fd < 0)
70                         {
71                                 zavai.log.error("Cannot open /dev/input/event1");
72                                 return;
73                         }
74
75                         int EVIOCGRAB = 0x40044590;
76                         if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
77                         {
78                                 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
79                                 Posix.close(screen_lock_fd);
80                                 return;
81                         }
82                 } else {
83                         Posix.close(screen_lock_fd);
84                 }
85                 screen_locked = locked;
86         }
87
88         private void on_power_button(Posix.timeval* time, bool pressed)
89         {
90                 if (!pressed)
91                 {
92                         if (screen_locked)
93                         {
94                                 // TODO: short press: turn on backlight for a bit
95                                 // TODO: long press: unlock
96                                 set_screen_lock(false);
97                         }
98                         else
99                         {
100                                 power_menu.toggle();
101                         }
102                 }
103         }
104 }
105
106 public class BatteryIcon : Gtk.StatusIcon
107 {
108         public dynamic DBus.Object battery;
109         protected string last_status;
110         protected int last_capacity;
111
112         public BatteryIcon()
113         {
114                 battery = zavai.registry.sbus.get_object(
115                         "org.freesmartphone.odeviced",
116                         "/org/freesmartphone/Device/PowerSupply/battery",
117                         "org.freesmartphone.Device.PowerSupply");
118
119                 // activate += on_activate;
120
121                 battery.PowerStatus += on_power_status;
122                 battery.Capacity += on_capacity;
123
124                 last_status = battery.GetPowerStatus();
125                 last_capacity = battery.GetCapacity();
126                 
127                 update_icon();
128         }
129
130         private void on_power_status(dynamic DBus.Object bat, string status)
131         {
132                 zavai.log.info("New battery status: " + status);
133                 last_status = status;
134                 update_icon();
135         }
136
137         private void on_capacity(dynamic DBus.Object bat, int val)
138         {
139 stderr.printf("NEW CAPACITY: %d\n", val);
140                 last_capacity = val;
141                 update_icon();
142         }
143
144         /*
145         private void on_activate()
146         {
147         }
148         */
149
150         protected void update_icon()
151         {
152                 string name = zavai.config.icondir + "/battery/";
153
154                 if (last_status == "charging")
155                         name += "%02d0_charging_500.png".printf(last_capacity/10);
156                 else
157                         name += "%02d0.png".printf(last_capacity/10);
158
159 stderr.printf("Loading icon from %s\n", name);
160
161                 set_from_file(name);
162         }
163 }
164
165 public class ScreenLockButton : Gtk.Button
166 {
167         public ScreenLockButton()
168         {
169                 label = "Lock screen";
170                 clicked += on_clicked;
171                 set_size_request(0, zavai.config.min_button_height);
172         }
173
174         public void on_clicked()
175         {
176                 zavai.log.info("Locking screen");
177                 power.set_screen_lock(true);
178                 power_menu.hide();
179         }
180 }
181
182 public class SuspendButton : Gtk.Button
183 {
184         public SuspendButton()
185         {
186                 label = "Suspend";
187                 clicked += on_clicked;
188                 set_size_request(0, zavai.config.min_button_height);
189         }
190
191         public void on_clicked()
192         {
193                 zavai.log.info("Suspending the phone via FSO");
194                 power.do_suspend();
195                 power_menu.hide();
196         }
197 }
198
199 public class ShutdownButton : Gtk.Button
200 {
201         public ShutdownButton()
202         {
203                 label = "Shut down";
204                 clicked += on_clicked;
205                 set_size_request(0, zavai.config.min_button_height);
206         }
207
208         public void on_clicked()
209         {
210                 zavai.log.info("Shutting down the phone via FSO");
211                 power.do_shutdown();
212                 power_menu.hide();
213         }
214 }
215
216 public class RebootButton : Gtk.Button
217 {
218         public RebootButton()
219         {
220                 label = "Reboot";
221                 clicked += on_clicked;
222                 set_size_request(0, zavai.config.min_button_height);
223         }
224
225         public void on_clicked()
226         {
227                 zavai.log.info("Rebooting the phone via FSO");
228                 power.do_reboot();
229                 power_menu.hide();
230         }
231 }
232
233 // For a list of dbus services, look in /etc/dbus-1/system.d/
234 public class Backlight: zavai.Service
235 {
236         public dynamic DBus.Object usage;
237
238         public Backlight()
239         {
240                 name = "backlight";
241
242                 usage = zavai.registry.sbus.get_object(
243                         "org.freesmartphone.ousaged",
244                         "/org/freesmartphone/Usage",
245                         "org.freesmartphone.Usage");
246         }
247
248         /// Request GPS resource
249         public override void start()
250         {
251                 if (started) return;
252                 try {
253                         usage.RequestResource("Display");
254                         zavai.log.info("Acquired display");
255                         base.start();
256                 } catch (GLib.Error e) {
257                         zavai.log.error(e.message);
258                 }
259                 base.start();
260         }
261
262         // Release usage of GPS
263         public override void stop()
264         {
265                 if (!started) return;
266                 try {
267                         usage.ReleaseResource("Display");
268                         zavai.log.info("Released display");
269                         base.stop();
270                 } catch (GLib.Error e) {
271                         zavai.log.error(e.message);
272                 }
273                 base.stop();
274         }
275 }
276
277 public class PowerMenu : zavai.Resource, Gtk.Window
278 {
279         protected Gtk.VBox vbox;
280         protected ScreenLockButton act_screen_lock;
281         protected SuspendButton act_suspend;
282         protected ShutdownButton act_shutdown;
283         protected RebootButton act_reboot;
284         protected ServiceRequestLink act_backlight_on;
285         protected bool shown;
286
287         public PowerMenu()
288         {
289                 type = Gtk.WindowType.TOPLEVEL;
290                 title = "Power Menu";
291                 shown = false;
292                 destroy_with_parent = true;
293                 set_transient_for(zavai.app);
294                 set_modal(true);
295                 set_position(Gtk.WindowPosition.CENTER_ON_PARENT);
296                 set_size_request(300, 500);
297
298                 vbox = new Gtk.VBox(false, 0);
299                 add(vbox);
300
301                 //destroy += Gtk.main_quit;
302                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
303                 //visibility_notify_event += on_visibility;
304                 set_skip_pager_hint(true);
305                 set_skip_taskbar_hint(true);
306                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
307
308                 act_screen_lock = new ScreenLockButton();
309                 vbox.pack_start(act_screen_lock, false, false, 0);
310
311                 act_suspend = new SuspendButton();
312                 vbox.pack_start(act_suspend, false, false, 0);
313
314                 act_shutdown = new ShutdownButton();
315                 vbox.pack_start(act_shutdown, false, false, 0);
316
317                 act_reboot = new RebootButton();
318                 vbox.pack_start(act_reboot, false, false, 0);
319
320                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
321                 vbox.pack_start(act_backlight_on, false, false, 0);
322
323                 //vbox.show_all();
324         }
325
326         public void toggle()
327         {
328                 if (!shown)
329                 {
330                         show_all();
331                         show();
332                         visible = true;
333                         present();
334                         shown = true;
335                 } else {
336                         // TODO: do more in case it is visible but has no visibility (is covered by others)
337                         visible = !visible;
338                         if (visible)
339                                 present();
340                 }                               
341         }
342
343         public void hide()
344         {
345                 visible = false;
346         }
347
348         public void shutdown() {}
349 }
350
351 /*
352 public class TogglePowerMenu : Gtk.Button
353 {
354         public TogglePowerMenu()
355         {
356                 label = "Toggle power menu";
357                 clicked += on_clicked;
358                 set_size_request(0, zavai.config.min_button_height);
359         }
360
361         public void on_clicked()
362         {
363                 zavai.log.info("Toggling power menu");
364                 power_menu.toggle();
365         }
366 }
367 */
368
369 Power power;
370 PowerMenu power_menu;
371 BatteryIcon battery_icon;
372 Backlight backlight;
373 //TogglePowerMenu tpm;
374
375 public void init()
376 {
377         power = new Power();
378         backlight = new Backlight();
379         zavai.registry.register_service(backlight);
380
381         battery_icon = new BatteryIcon();
382         battery_icon.set_visible(true);
383
384         power_menu = new PowerMenu();
385         zavai.registry.register_resource("powermenu", power_menu);
386         
387     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
388         //tpm = new TogglePowerMenu();
389     //zavai.registry.getmenu("menu.main").add_widget(tpm);
390
391     /*
392         raise_icon = new RaiseIcon();
393         raise_icon.set_visible(true);
394
395         close_or_back = new CloseOrBack();
396         close_or_back.set_visible(true);
397
398         window_list = new WindowList("Current apps");
399         zavai.registry.register_applet("wm.list", window_list);
400         zavai.registry.getmenu("menu.main").add_applet("wm.list");
401
402         try {
403                 launcher = new Launcher("Run program");
404         } catch (Error e) {
405                 zavai.log.error("Not running launcher: " + e.message);
406                 launcher = null;
407         }
408
409         if (launcher != null)
410         {
411                 zavai.registry.register_applet("wm.launcher", launcher);
412                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
413         }
414     */
415 }
416
417 }
418 }
419 }