First attempt at hiding power menu if power button is pressed again
[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 sec
95                                 // TODO: long press: unlock
96                                 set_screen_lock(false);
97                         }
98                         else
99                         {
100                                 if (zavai.app.current_name == "menu.power" && zavai.app.visibility)
101                                 {
102                                         // Hide or back
103                                         zavai.app.back();
104                                         if (hide_after_closing_power_menu)
105                                                 zavai.app.hide();
106                                 } else if (zavai.app.current_name == "menu.power") {
107                                         // Unhide
108                                         zavai.app.ensure_visible();
109                                 } else {
110                                         zavai.app.push_applet("menu.power");
111                                         hide_after_closing_power_menu = !zavai.app.visibility;
112                                         zavai.app.ensure_visible();
113                                 }
114                         }
115                 }
116         }
117 }
118
119 public class BatteryIcon : Gtk.StatusIcon
120 {
121         public dynamic DBus.Object battery;
122         protected string last_status;
123         protected int last_capacity;
124
125         public BatteryIcon()
126         {
127                 battery = zavai.registry.sbus.get_object(
128                         "org.freesmartphone.odeviced",
129                         "/org/freesmartphone/Device/PowerSupply/battery",
130                         "org.freesmartphone.Device.PowerSupply");
131
132                 // activate += on_activate;
133
134                 battery.PowerStatus += on_power_status;
135                 battery.Capacity += on_capacity;
136
137                 last_status = battery.GetPowerStatus();
138                 last_capacity = battery.GetCapacity();
139                 
140                 update_icon();
141         }
142
143         private void on_power_status(dynamic DBus.Object bat, string status)
144         {
145                 zavai.log.info("New battery status: " + status);
146                 last_status = status;
147                 update_icon();
148         }
149
150         private void on_capacity(dynamic DBus.Object bat, int val)
151         {
152 stderr.printf("NEW CAPACITY: %d\n", val);
153                 last_capacity = val;
154                 update_icon();
155         }
156
157         /*
158         private void on_activate()
159         {
160         }
161         */
162
163         protected void update_icon()
164         {
165                 string name = zavai.config.icondir + "/battery/";
166
167                 if (last_status == "charging")
168                         name += "%02d0_charging_500.png".printf(last_capacity/10);
169                 else
170                         name += "%02d0.png".printf(last_capacity/10);
171
172 stderr.printf("Loading icon from %s\n", name);
173
174                 set_from_file(name);
175         }
176 }
177
178 public class ScreenLockButton : Gtk.Button
179 {
180         public ScreenLockButton()
181         {
182                 label = "Lock screen";
183                 clicked += on_clicked;
184                 set_size_request(0, zavai.config.min_button_height);
185         }
186
187         public void on_clicked()
188         {
189                 zavai.log.info("Locking screen");
190                 power.set_screen_lock(true);
191         }
192 }
193
194 public class SuspendButton : Gtk.Button
195 {
196         public SuspendButton()
197         {
198                 label = "Suspend";
199                 clicked += on_clicked;
200                 set_size_request(0, zavai.config.min_button_height);
201         }
202
203         public void on_clicked()
204         {
205                 zavai.log.info("Suspending the phone via FSO");
206                 power.do_suspend();
207         }
208 }
209
210 public class ShutdownButton : Gtk.Button
211 {
212         public ShutdownButton()
213         {
214                 label = "Shut down";
215                 clicked += on_clicked;
216                 set_size_request(0, zavai.config.min_button_height);
217         }
218
219         public void on_clicked()
220         {
221                 zavai.log.info("Shutting down the phone via FSO");
222                 power.do_shutdown();
223         }
224 }
225
226 public class RebootButton : Gtk.Button
227 {
228         public RebootButton()
229         {
230                 label = "Reboot";
231                 clicked += on_clicked;
232                 set_size_request(0, zavai.config.min_button_height);
233         }
234
235         public void on_clicked()
236         {
237                 zavai.log.info("Rebooting the phone via FSO");
238                 power.do_reboot();
239         }
240 }
241
242 // For a list of dbus services, look in /etc/dbus-1/system.d/
243 public class Backlight: zavai.Service
244 {
245         public dynamic DBus.Object usage;
246
247         public Backlight()
248         {
249                 name = "backlight";
250
251                 usage = zavai.registry.sbus.get_object(
252                         "org.freesmartphone.ousaged",
253                         "/org/freesmartphone/Usage",
254                         "org.freesmartphone.Usage");
255         }
256
257         /// Request GPS resource
258         public override void start()
259         {
260                 if (started) return;
261                 try {
262                         usage.RequestResource("Display");
263                         zavai.log.info("Acquired display");
264                         base.start();
265                 } catch (GLib.Error e) {
266                         zavai.log.error(e.message);
267                 }
268                 base.start();
269         }
270
271         // Release usage of GPS
272         public override void stop()
273         {
274                 if (!started) return;
275                 try {
276                         usage.ReleaseResource("Display");
277                         zavai.log.info("Released display");
278                         base.stop();
279                 } catch (GLib.Error e) {
280                         zavai.log.error(e.message);
281                 }
282                 base.stop();
283         }
284 }
285
286
287 Power power;
288 BatteryIcon battery_icon;
289 Backlight backlight;
290
291 public void init()
292 {
293         power = new Power();
294         backlight = new Backlight();
295         zavai.registry.register_service(backlight);
296
297         battery_icon = new BatteryIcon();
298         battery_icon.set_visible(true);
299
300     // Menus
301     var menu_power = new zavai.Menu("Power menu");
302         menu_power.add_widget(new ScreenLockButton());
303         menu_power.add_widget(new SuspendButton());
304         menu_power.add_widget(new ShutdownButton());
305         menu_power.add_widget(new RebootButton());
306     menu_power.add_service_toggle("backlight", "Keep backlight on", "Let backlight fade");
307     zavai.registry.register_menu("menu.power", menu_power);
308
309
310     //zavai.registry.getmenu("menu.main").add_applet("menu.power");
311
312     /*
313         raise_icon = new RaiseIcon();
314         raise_icon.set_visible(true);
315
316         close_or_back = new CloseOrBack();
317         close_or_back.set_visible(true);
318
319         window_list = new WindowList("Current apps");
320         zavai.registry.register_applet("wm.list", window_list);
321         zavai.registry.getmenu("menu.main").add_applet("wm.list");
322
323         try {
324                 launcher = new Launcher("Run program");
325         } catch (Error e) {
326                 zavai.log.error("Not running launcher: " + e.message);
327                 launcher = null;
328         }
329
330         if (launcher != null)
331         {
332                 zavai.registry.register_applet("wm.launcher", launcher);
333                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
334         }
335     */
336 }
337
338 }
339 }
340 }