Shutdown, reboot and suspend via frameworkd
[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         public Power()
34         {
35                 screen_locked = false;
36                 screen_lock_fd = -1;
37
38                 usage = zavai.registry.sbus.get_object(
39                         "org.freesmartphone.ousaged",
40                         "/org/freesmartphone/Usage",
41                         "org.freesmartphone.Usage");
42
43                 zavai.input.power_button.power_button += on_power_button;
44                 zavai.input.power_button.request("zavai.ui.powerbutton.power");
45         }
46
47         public void shutdown()
48         {
49                 zavai.input.power_button.release("zavai.ui.powerbutton.power");
50         }
51
52         public void do_suspend() { usage.Suspend(); }
53         public void do_shutdown() { usage.Shutdown(); }
54         public void do_reboot() { usage.Reboot(); }
55
56         public void set_screen_lock(bool locked)
57         {
58                 if (locked && screen_locked)
59                         return;
60                 if (!locked && !screen_locked)
61                         return;
62
63                 if (locked)
64                 {
65                         screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
66                         if (screen_lock_fd < 0)
67                         {
68                                 zavai.log.error("Cannot open /dev/input/event1");
69                                 return;
70                         }
71
72                         int EVIOCGRAB = 0x40044590;
73                         if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
74                         {
75                                 zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
76                                 Posix.close(screen_lock_fd);
77                                 return;
78                         }
79                 } else {
80                         Posix.close(screen_lock_fd);
81                 }
82                 screen_locked = locked;
83         }
84
85         private void on_power_button(Posix.timeval* time, bool pressed)
86         {
87                 if (!pressed)
88                 {
89                         if (screen_locked)
90                                 set_screen_lock(false);
91                         else
92                         {
93                                 zavai.app.push_applet("menu.power");
94                                 zavai.app.ensure_visible();
95                         }
96                 }
97         }
98 }
99
100 public class BatteryIcon : Gtk.StatusIcon
101 {
102         public dynamic DBus.Object battery;
103         protected string last_status;
104         protected int last_capacity;
105
106         public BatteryIcon()
107         {
108                 battery = zavai.registry.sbus.get_object(
109                         "org.freesmartphone.odeviced",
110                         "/org/freesmartphone/Device/PowerSupply/battery",
111                         "org.freesmartphone.Device.PowerSupply");
112
113                 // activate += on_activate;
114
115                 battery.PowerStatus += on_power_status;
116                 battery.Capacity += on_capacity;
117
118                 last_status = battery.GetPowerStatus();
119                 last_capacity = battery.GetCapacity();
120                 
121                 update_icon();
122         }
123
124         private void on_power_status(dynamic DBus.Object bat, string status)
125         {
126                 zavai.log.info("New battery status: " + status);
127                 last_status = status;
128                 update_icon();
129         }
130
131         private void on_capacity(dynamic DBus.Object bat, int val)
132         {
133 stderr.printf("NEW CAPACITY: %d\n", val);
134                 last_capacity = val;
135                 update_icon();
136         }
137
138         /*
139         private void on_activate()
140         {
141         }
142         */
143
144         protected void update_icon()
145         {
146                 string name = zavai.config.icondir + "/battery/";
147
148                 if (last_status == "charging")
149                         name += "%02d0_charging_500.png".printf(last_capacity/10);
150                 else
151                         name += "%02d0.png".printf(last_capacity/10);
152
153 stderr.printf("Loading icon from %s\n", name);
154
155                 set_from_file(name);
156         }
157 }
158
159 public class ScreenLockButton : Gtk.Button
160 {
161         public ScreenLockButton()
162         {
163                 label = "Lock screen";
164                 clicked += on_clicked;
165                 set_size_request(0, zavai.config.min_button_height);
166         }
167
168         public void on_clicked()
169         {
170                 zavai.log.info("Locking screen");
171                 power.set_screen_lock(true);
172         }
173 }
174
175 public class SuspendButton : Gtk.Button
176 {
177         public SuspendButton()
178         {
179                 label = "Suspend";
180                 clicked += on_clicked;
181                 set_size_request(0, zavai.config.min_button_height);
182         }
183
184         public void on_clicked()
185         {
186                 zavai.log.info("Suspending the phone via FSO");
187                 power.do_suspend();
188         }
189 }
190
191 public class ShutdownButton : Gtk.Button
192 {
193         public ShutdownButton()
194         {
195                 label = "Shut down";
196                 clicked += on_clicked;
197                 set_size_request(0, zavai.config.min_button_height);
198         }
199
200         public void on_clicked()
201         {
202                 zavai.log.info("Shutting down the phone via FSO");
203                 power.do_shutdown();
204         }
205 }
206
207 public class RebootButton : Gtk.Button
208 {
209         public RebootButton()
210         {
211                 label = "Reboot";
212                 clicked += on_clicked;
213                 set_size_request(0, zavai.config.min_button_height);
214         }
215
216         public void on_clicked()
217         {
218                 zavai.log.info("Rebooting the phone via FSO");
219                 power.do_reboot();
220         }
221 }
222
223 Power power;
224 BatteryIcon battery_icon;
225
226 public void init()
227 {
228         power = new Power();
229
230         battery_icon = new BatteryIcon();
231         battery_icon.set_visible(true);
232
233     // Menus
234     var menu_power = new zavai.Menu("Power menu");
235         menu_power.add_widget(new ScreenLockButton());
236         menu_power.add_widget(new SuspendButton());
237         menu_power.add_widget(new ShutdownButton());
238         menu_power.add_widget(new RebootButton());
239     zavai.registry.register_menu("menu.power", menu_power);
240
241     zavai.registry.getmenu("menu.main").add_applet("menu.power");
242
243     /*
244         raise_icon = new RaiseIcon();
245         raise_icon.set_visible(true);
246
247         close_or_back = new CloseOrBack();
248         close_or_back.set_visible(true);
249
250         window_list = new WindowList("Current apps");
251         zavai.registry.register_applet("wm.list", window_list);
252         zavai.registry.getmenu("menu.main").add_applet("wm.list");
253
254         try {
255                 launcher = new Launcher("Run program");
256         } catch (Error e) {
257                 zavai.log.error("Not running launcher: " + e.message);
258                 launcher = null;
259         }
260
261         if (launcher != null)
262         {
263                 zavai.registry.register_applet("wm.launcher", launcher);
264                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
265         }
266     */
267 }
268
269 }
270 }
271 }