More of keyboard
[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         private void on_child_quit(Pid pid, int status)
32         {
33                 Process.close_pid(pid);
34                 pid = (Pid)0;
35                 stop();
36         }
37
38         protected override void start()
39         {
40                 string[] args = { "/usr/bin/matchbox-keyboard", null };
41                 int opid;
42                 try {
43                         Gdk.spawn_on_screen(
44                                 Gdk.Screen.get_default(),
45                                 "/",
46                                 args,
47                                 null,
48                                 SpawnFlags.SEARCH_PATH | SpawnFlags.STDOUT_TO_DEV_NULL | SpawnFlags.STDERR_TO_DEV_NULL,
49                                 null,
50                                 out opid);
51                         pid = (Pid)opid;
52                         ChildWatch.add(pid, on_child_quit);
53                         base.start();
54                 } catch (Error e) {
55                         log.error("Running matchbox-keyboard: " + e.message);
56                         pid = (Pid)0;
57                 }
58         }
59
60         protected override void stop()
61         {
62                 if ((int)pid != 0)
63                 {
64                         Posix.kill((int)pid, 15);
65                         pid = (Pid)0;
66                 }
67                 base.stop();
68         }
69 }
70
71 /*
72 class GPSOn(gtk.ToggleAction):
73     states = [_("GPS always on"), _("GPS on when needed")]
74
75     def __init__(self, registry, **kw):
76         self.state = 0
77         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
78
79         self.registry = registry
80         self.set_active(False)
81
82         self.connect("toggled", self.on_toggle)
83
84     def on_toggle(self, *args):
85         self.state = (self.state + 1) % len(self.states)
86         self.set_property("label", self.states[self.state])
87         if self.get_active():
88             self.start()
89         else:
90             self.stop()
91
92     def start(self):
93         self.registry.resource("gps").connect("gps", self)
94
95     def stop(self):
96         self.registry.resource("gps").disconnect("gps", self)
97
98 class GPXTracer(gtk.ToggleAction):
99     states = [_("Start GPX trace"), _("Stop GPX trace")]
100
101     def __init__(self, registry, **kw):
102         self.state = 0
103         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
104
105         self.registry = registry
106         self.set_active(False)
107
108         self.connect("toggled", self.on_toggle)
109
110     def on_toggle(self, *args):
111         self.state = (self.state + 1) % len(self.states)
112         self.set_property("label", self.states[self.state])
113         if self.get_active():
114             self.start()
115         else:
116             self.stop()
117
118     def start(self):
119         zavai.info("GPX trace started")
120         self.registry.resource("gpx").connect("gpx", self)
121
122     def stop(self):
123         zavai.info("GPX trace ended")
124         self.registry.resource("gpx").disconnect("gpx", self)
125
126 public class Waypoint : BigButton
127 {
128     public Waypoint()
129     {
130         set_label("Take waypoint");
131         zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
132         clicked += on_clicked;
133         set_sensitive(zavai.gps.gpx.tracking);
134     }
135
136     protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
137     {
138 stderr.printf("Toggled %d\n", (int)new_state);
139         set_sensitive(new_state);
140     }
141
142     protected void on_clicked()
143     {
144 stderr.printf("Activate\n");
145         zavai.gps.gpx.waypoint();
146     }
147 }
148
149 class GPXAudioTracer(gtk.ToggleAction):
150     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
151
152     def __init__(self, registry, **kw):
153         self.state = 0
154         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
155
156         self.registry = registry
157         self.recorder = zavai.Recorder(registry)
158         self.set_active(False)
159
160         self.connect("toggled", self.on_toggle)
161
162     def shutdown(self):
163         self.recorder.stop()
164         super(GPXAudioTracer, self).shutdown()
165
166     def on_toggle(self, *args):
167         self.state = (self.state + 1) % len(self.states)
168         self.set_property("label", self.states[self.state])
169         if self.get_active():
170             self.start()
171         else:
172             self.stop()
173
174     def start(self):
175         zavai.info("GPX trace started")
176         gpx = self.registry.resource("gpx")
177         gpx.connect("gpx", self)
178         gpx.add_activity_monitor(self.on_gpx_activity_changed)
179
180     def stop(self):
181         zavai.info("GPX trace ended")
182         gpx = self.registry.resource("gpx")
183         gpx.disconnect("gpx", self)
184         self.recorder.stop()
185         gpx.del_activity_monitor(self.on_gpx_activity_changed)
186
187     def on_gpx_activity_changed(self, gpx, state):
188         if state:
189             self.recorder.start(gpx.basename + ".wav")
190         else:
191             self.recorder.stop()
192 */
193
194 /*
195 public class GPSRequestLink : Gtk.ToggleButton
196 {
197         protected string service_name;
198         protected string label_start;
199         protected string label_stop;
200     protected Gtk.StatusIcon status_icon;
201     protected int fix_status = 0;
202
203         public GPSRequestLink()
204         {
205         service_name = "gps";
206         label_start = "Keep GPS on";
207         label_stop = "Stop keeping GPS on";
208                 set_size_request(0, zavai.config.min_button_height);
209                 toggled += on_toggled;
210
211                 set_label(get_active() ? label_stop : label_start);
212
213         //tooltip = "GPS status";
214         try {
215             fix_status = zavai.gps.gps.device.GetFixStatus();
216         } catch (Error e) {
217             fix_status = 0;
218         }
219         zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
220
221         // GPS status icon
222         status_icon = new Gtk.StatusIcon();
223         status_icon.set_visible(true);
224         status_icon.activate += on_status_activate;
225         update_icon();
226         }
227
228     protected void update_icon()
229     {
230         string name;
231         if (fix_status == 2 || fix_status == 3)
232             name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
233         else
234             name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
235 stderr.printf("load icon from %s\n", name);
236         status_icon.set_from_file(name);
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 public void init()
264 {
265     /*
266     registry.register(GPXAudioTracer(registry))
267     registry.register(GPXWaypoint(registry))
268
269     // Apps
270     var useless = new Useless();
271     zavai.registry.register_applet("app.debug.useless", useless);
272     */
273
274     /*
275     var menu_waypoint = new Waypoint();
276     var menu_gpsrequest = new GPSRequestLink();
277                 //label_on = "Stop GPX trace";
278                 //label_off = "Start GPX trace";
279                 //label_on = "Stop GPS monitor";
280                 //label_off = "Start GPS monitor";
281                 //label_on = "Stop GPS position tracking";
282                 //label_off = "Start GPS position tracking";
283                 //label_on = "Stop keeping GPS on";
284                 //label_off = "Keep GPS on";
285
286     // Menus
287     var menu_gps = new zavai.Menu("GPS");
288     //menu_gps.add_applet("app.debug.useless");
289     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
290     menu_gps.add_widget(menu_waypoint);
291     menu_gps.add_widget(menu_gpsrequest);
292
293     zavai.registry.register_menu("menu.gps", menu_gps);
294     zavai.registry.getmenu("menu.main").add_applet("menu.gps");
295     */
296 }
297
298 }
299 }
300 }