Second attempt at locking the screen
[gregoa/zavai.git] / src / app_powerbutton.vala
1 /*
2  * app_powerbutton - zavai power button 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 /*
28 public class RaiseIcon : Gtk.StatusIcon
29 {
30         public RaiseIcon()
31         {
32                 activate += on_activate;
33                 zavai.app.visibility_changed += on_visibility_changed;
34                 update_icon();
35         }
36
37         private void on_visibility_changed(bool visible)
38         {
39                 update_icon();
40         }
41
42         private void on_activate()
43         {
44                 zavai.app.toggle_visibility();
45                 update_icon();
46         }
47
48         protected void update_icon()
49         {
50                 string name = zavai.config.icondir + "/";
51                 if (!zavai.app.visibility)
52                         name += "zavai_on.png";
53                 else
54                         name += "zavai_off.png";
55                 set_from_file(name);
56         }
57 }
58
59 public class CloseOrBack : Gtk.StatusIcon
60 {
61         public CloseOrBack()
62         {
63                 activate += on_activate;
64                 zavai.app.visibility_changed += on_visibility_changed;
65                 update_icon();
66         }
67
68         private void on_visibility_changed(bool visible)
69         {
70                 update_icon();
71         }
72
73         private void on_activate()
74         {
75                 if (zavai.app.visibility)
76                 {
77                         // Back
78                 } else {
79                         // Close current app
80                         Gdk.Window w = zavai.app.get_screen().get_active_window();
81                         if (w != zavai.app.window)
82                         {
83                                 w.destroy();
84                         }
85                 }
86         }
87
88         protected void update_icon()
89         {
90                 string name = zavai.config.icondir + "/";
91                 if (!zavai.app.visibility)
92                         name += "quit_on.png";
93                 else
94                         name += "quit_off.png";
95                 set_from_file(name);
96         }
97 }
98
99 public class WindowList : Applet
100 {
101         Wnck.Tasklist selector;
102
103         public WindowList(string label)
104         {
105                 _label = label;
106                 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
107                 pack_start(selector, true, true, 0);
108         }
109 }
110 */
111
112 public class CommandButton : Gtk.Button
113 {
114         private string command;
115
116         public CommandButton(string name, string command)
117         {
118                 label = name;
119                 this.command = command;
120                 clicked += on_clicked;
121                 set_size_request(0, zavai.config.min_button_height);
122         }
123
124         public void on_clicked()
125         {
126                 zavai.log.info("Run program: " + command);
127                 string[] args = command.split(" ");
128                 Pid pid;
129                 Process.spawn_async(
130                         Environment.get_home_dir(),
131                         args,
132                         null,
133                         SpawnFlags.SEARCH_PATH,
134                         null,
135                         out pid);
136         }
137 }
138
139 private bool screen_locked;
140 private int screen_lock_fd;
141
142 private void set_screen_lock(bool locked)
143 {
144         if (locked && screen_locked)
145                 return;
146         if (!locked && !screen_locked)
147                 return;
148
149         if (locked)
150         {
151                 screen_lock_fd = Posix.open("/dev/input/event1", Posix.O_RDWR | Posix.O_NONBLOCK);
152                 if (screen_lock_fd < 0)
153                 {
154                         zavai.log.error("Cannot open /dev/input/event1");
155                         return;
156                 }
157
158                 int EVIOCGRAB = 0x40044590;
159                 if (Posix.ioctl(screen_lock_fd, EVIOCGRAB, locked ? 1 : 0) != 0)
160                 {
161                         zavai.log.error("Cannot EVIOCGRAB /dev/input/event1");
162                         Posix.close(screen_lock_fd);
163                         return;
164                 }
165         } else {
166                 Posix.close(screen_lock_fd);
167         }
168         screen_locked = locked;
169 }
170
171 public class ScreenLockButton : Gtk.Button
172 {
173         public ScreenLockButton(string name)
174         {
175                 label = name;
176                 clicked += on_clicked;
177                 set_size_request(0, zavai.config.min_button_height);
178         }
179
180         public void on_clicked()
181         {
182                 zavai.log.info("Locking screen");
183                 set_screen_lock(true);
184         }
185 }
186
187 private void on_power_button(Posix.timeval* time, bool pressed)
188 {
189         if (!pressed)
190         {
191                 if (screen_locked)
192                         set_screen_lock(false);
193                 else
194                         zavai.app.push_applet("menu.power");
195         }
196 }
197
198 /*
199 RaiseIcon raise_icon;
200 CloseOrBack close_or_back;
201 WindowList window_list;
202 Launcher launcher;
203 */
204
205 public void init()
206 {
207         screen_locked = false;
208         screen_lock_fd = -1;
209
210         zavai.input.power_button.power_button += on_power_button;
211
212     // Menus
213     var menu_power = new zavai.Menu("Power menu");
214         menu_power.add_widget(new ScreenLockButton("Lock screen"));
215         menu_power.add_widget(new CommandButton("Suspend", "apm -s"));
216         menu_power.add_widget(new CommandButton("Shutdown", "shutdown -h now"));
217         menu_power.add_widget(new CommandButton("Reboot", "shutdown -r now"));
218     zavai.registry.register_menu("menu.power", menu_power);
219
220     zavai.registry.getmenu("menu.main").add_applet("menu.power");
221
222     zavai.registry.gets("input.power_button").request("powerbutton");
223
224     /*
225         raise_icon = new RaiseIcon();
226         raise_icon.set_visible(true);
227
228         close_or_back = new CloseOrBack();
229         close_or_back.set_visible(true);
230
231         window_list = new WindowList("Current apps");
232         zavai.registry.register_applet("wm.list", window_list);
233         zavai.registry.getmenu("menu.main").add_applet("wm.list");
234
235         try {
236                 launcher = new Launcher("Run program");
237         } catch (Error e) {
238                 zavai.log.error("Not running launcher: " + e.message);
239                 launcher = null;
240         }
241
242         if (launcher != null)
243         {
244                 zavai.registry.register_applet("wm.launcher", launcher);
245                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
246         }
247     */
248 }
249
250 }
251 }
252 }