ad2e69356febcb58de88a778549fad60f156bced
[gregoa/zavai.git] / src / app_keyboard.vala
1 /*
2  * app_keyboard - zavai keyboard show/hide functions
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 kbd {
26
27 public class Keyboard : Service
28 {
29         protected Pid pid;
30
31         public Keyboard()
32         {
33                 name = "keyboard";
34         }
35
36         private void on_child_quit(Pid pid, int status)
37         {
38                 Process.close_pid(pid);
39                 pid = (Pid)0;
40                 stop();
41         }
42
43         protected override void start()
44         {
45                 string[] args = { "/usr/bin/matchbox-keyboard", null };
46                 int opid;
47                 try {
48                         Gdk.spawn_on_screen(
49                                 Gdk.Screen.get_default(),
50                                 "/",
51                                 args,
52                                 null,
53                                 SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
54                                 null,
55                                 out opid);
56                         pid = (Pid)opid;
57                         ChildWatch.add(pid, on_child_quit);
58                         base.start();
59                 } catch (Error e) {
60                         log.error("Running matchbox-keyboard: " + e.message);
61                         pid = (Pid)0;
62                 }
63         }
64
65         protected override void stop()
66         {
67                 if ((int)pid != 0)
68                 {
69                         Posix.kill((int)pid, 15);
70                         pid = (Pid)0;
71                 }
72                 base.stop();
73         }
74 }
75
76 /*
77 class GPSOn(gtk.ToggleAction):
78     states = [_("GPS always on"), _("GPS on when needed")]
79
80     def __init__(self, registry, **kw):
81         self.state = 0
82         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
83
84         self.registry = registry
85         self.set_active(False)
86
87         self.connect("toggled", self.on_toggle)
88
89     def on_toggle(self, *args):
90         self.state = (self.state + 1) % len(self.states)
91         self.set_property("label", self.states[self.state])
92         if self.get_active():
93             self.start()
94         else:
95             self.stop()
96
97     def start(self):
98         self.registry.resource("gps").connect("gps", self)
99
100     def stop(self):
101         self.registry.resource("gps").disconnect("gps", self)
102
103 class GPXTracer(gtk.ToggleAction):
104     states = [_("Start GPX trace"), _("Stop GPX trace")]
105
106     def __init__(self, registry, **kw):
107         self.state = 0
108         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
109
110         self.registry = registry
111         self.set_active(False)
112
113         self.connect("toggled", self.on_toggle)
114
115     def on_toggle(self, *args):
116         self.state = (self.state + 1) % len(self.states)
117         self.set_property("label", self.states[self.state])
118         if self.get_active():
119             self.start()
120         else:
121             self.stop()
122
123     def start(self):
124         zavai.info("GPX trace started")
125         self.registry.resource("gpx").connect("gpx", self)
126
127     def stop(self):
128         zavai.info("GPX trace ended")
129         self.registry.resource("gpx").disconnect("gpx", self)
130
131 public class Waypoint : BigButton
132 {
133     public Waypoint()
134     {
135         set_label("Take waypoint");
136         zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
137         clicked += on_clicked;
138         set_sensitive(zavai.gps.gpx.tracking);
139     }
140
141     protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
142     {
143 stderr.printf("Toggled %d\n", (int)new_state);
144         set_sensitive(new_state);
145     }
146
147     protected void on_clicked()
148     {
149 stderr.printf("Activate\n");
150         zavai.gps.gpx.waypoint();
151     }
152 }
153
154 class GPXAudioTracer(gtk.ToggleAction):
155     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
156
157     def __init__(self, registry, **kw):
158         self.state = 0
159         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
160
161         self.registry = registry
162         self.recorder = zavai.Recorder(registry)
163         self.set_active(False)
164
165         self.connect("toggled", self.on_toggle)
166
167     def shutdown(self):
168         self.recorder.stop()
169         super(GPXAudioTracer, self).shutdown()
170
171     def on_toggle(self, *args):
172         self.state = (self.state + 1) % len(self.states)
173         self.set_property("label", self.states[self.state])
174         if self.get_active():
175             self.start()
176         else:
177             self.stop()
178
179     def start(self):
180         zavai.info("GPX trace started")
181         gpx = self.registry.resource("gpx")
182         gpx.connect("gpx", self)
183         gpx.add_activity_monitor(self.on_gpx_activity_changed)
184
185     def stop(self):
186         zavai.info("GPX trace ended")
187         gpx = self.registry.resource("gpx")
188         gpx.disconnect("gpx", self)
189         self.recorder.stop()
190         gpx.del_activity_monitor(self.on_gpx_activity_changed)
191
192     def on_gpx_activity_changed(self, gpx, state):
193         if state:
194             self.recorder.start(gpx.basename + ".wav")
195         else:
196             self.recorder.stop()
197 */
198
199 public class KeyboardIcon : Gtk.StatusIcon
200 {
201         bool requested = false;
202
203         public KeyboardIcon()
204         {
205                 activate += on_activate;
206                 update_icon();
207         }
208
209         private void on_activate()
210         {
211                 requested = !requested;
212                 update_icon();
213                 if (requested)
214                         keyboard.request("keyboardicon");
215                 else
216                         keyboard.release("keyboardicon");
217         }
218
219         protected void update_icon()
220         {
221                 string name = zavai.config.icondir + "/" + (requested ? "kbd_on.png" : "kbd_off.png");
222                 stderr.printf("load icon from %s\n", name);
223                 set_from_file(name);
224         }
225 }
226
227 /*
228 public class GPSRequestLink : Gtk.ToggleButton
229 {
230         public GPSRequestLink()
231         {
232         // GPS status icon
233         status_icon = new Gtk.StatusIcon();
234         status_icon.set_visible(true);
235         status_icon.activate += on_status_activate;
236         update_icon();
237         }
238
239     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
240     {
241         this.fix_status = fix_status;
242         update_icon();
243     }
244
245         private void on_toggled(Gtk.Button src)
246         {
247                 Service s = zavai.registry.gets(service_name);
248                 if (get_active())
249                         s.request("servicerequestlink");
250                 else
251                         s.release("servicerequestlink");
252                 set_label(get_active() ? label_stop : label_start);
253         update_icon();
254         }
255
256     private void on_status_activate()
257     {
258         set_active(!get_active());
259     }
260 }
261 */
262
263 Keyboard keyboard;
264 KeyboardIcon icon;
265
266 public void init()
267 {
268 stderr.printf("INIT KBD\n");
269         keyboard = new Keyboard();
270         zavai.registry.register_service(keyboard);
271         icon = new KeyboardIcon();
272         icon.set_visible(true);
273 }
274
275 }
276 }
277 }