1edd502bc88baf79f4d40027b8e22eb8e163d52e
[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 powerbutton {
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.POPUP;
290                 title = "Power Menu";
291                 shown = false;
292
293                 vbox = new Gtk.VBox(true, 0);
294                 add(vbox);
295
296                 //destroy += Gtk.main_quit;
297                 //set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
298                 //visibility_notify_event += on_visibility;
299                 set_skip_pager_hint(true);
300                 set_skip_taskbar_hint(true);
301                 set_type_hint(Gdk.WindowTypeHint.POPUP_MENU);
302
303                 act_screen_lock = new ScreenLockButton();
304                 vbox.pack_start(act_screen_lock, false, false, 0);
305
306                 act_suspend = new SuspendButton();
307                 vbox.pack_start(act_suspend, false, false, 0);
308
309                 act_shutdown = new ShutdownButton();
310                 vbox.pack_start(act_shutdown, false, false, 0);
311
312                 act_reboot = new RebootButton();
313                 vbox.pack_start(act_reboot, false, false, 0);
314
315                 act_backlight_on = new ServiceRequestLink("backlight", "Keep backlight on", "Let backlight fade");
316                 vbox.pack_start(act_backlight_on, false, false, 0);
317
318                 vbox.show_all();
319         }
320
321         public void toggle()
322         {
323                 if (!shown)
324                 {
325                         show_all();
326                         shown = true;
327                 } else {
328                         // TODO: do more in case it is visible but has no visibility (is covered by others)
329                         visible = !visible;
330                         if (visible)
331                                 present();
332                 }                               
333         }
334
335         public void hide()
336         {
337                 visible = false;
338         }
339
340         public void shutdown() {}
341 }
342
343
344 Power power;
345 PowerMenu power_menu;
346 BatteryIcon battery_icon;
347 Backlight backlight;
348
349 public void init()
350 {
351         power = new Power();
352         backlight = new Backlight();
353         zavai.registry.register_service(backlight);
354
355         battery_icon = new BatteryIcon();
356         battery_icon.set_visible(true);
357
358         power_menu = new PowerMenu();
359         zavai.registry.register_resource("powermenu", power_menu);
360         
361     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
362
363     /*
364         raise_icon = new RaiseIcon();
365         raise_icon.set_visible(true);
366
367         close_or_back = new CloseOrBack();
368         close_or_back.set_visible(true);
369
370         window_list = new WindowList("Current apps");
371         zavai.registry.register_applet("wm.list", window_list);
372         zavai.registry.getmenu("menu.main").add_applet("wm.list");
373
374         try {
375                 launcher = new Launcher("Run program");
376         } catch (Error e) {
377                 zavai.log.error("Not running launcher: " + e.message);
378                 launcher = null;
379         }
380
381         if (launcher != null)
382         {
383                 zavai.registry.register_applet("wm.launcher", launcher);
384                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
385         }
386     */
387 }
388
389 }
390 }
391 }