2001-12-08 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / test / simple-at.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <cspi/spi.h>
29 #include "../util/mag_client.h"
30 #include "../cspi/spi-private.h" /* A hack for now */
31
32 static void report_focus_event    (AccessibleEvent *event, void *user_data);
33 static void report_button_press   (AccessibleEvent *event, void *user_data);
34 static void check_property_change (AccessibleEvent *event, void *user_data);
35 static SPIBoolean report_command_key_event  (AccessibleKeystroke *stroke, void *user_data);
36 static SPIBoolean report_ordinary_key_event (AccessibleKeystroke *stroke, void *user_data);
37 static void get_environment_vars (void);
38
39 static int _festival_init ();
40 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
41 static void _festival_write (const char *buff, int fd);
42
43 static SPIBoolean use_magnifier = FALSE;
44 static SPIBoolean use_festival = FALSE;
45 static SPIBoolean festival_chatty = FALSE;
46
47 static AccessibleEventListener *focus_listener;
48 static AccessibleEventListener *property_listener;
49 static AccessibleEventListener *button_listener;
50 static AccessibleKeystrokeListener *command_key_listener;
51 static AccessibleKeystrokeListener *ordinary_key_listener;
52
53 int
54 main (int argc, char **argv)
55 {
56   int i, j;
57   int n_desktops;
58   int n_apps;
59   Accessible *desktop;
60   Accessible *application;
61   char *s;
62
63   if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
64   {
65     printf ("Usage: simple-at\n");
66     printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
67     exit (0);
68   }
69
70   SPI_init ();
71
72   focus_listener = createAccessibleEventListener (report_focus_event, NULL);
73   property_listener = createAccessibleEventListener (check_property_change, NULL); 
74   button_listener = createAccessibleEventListener (report_button_press, NULL);
75   registerGlobalEventListener (focus_listener, "focus:");
76   registerGlobalEventListener (property_listener, "object:property-change:accessible-selection"); 
77   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
78   n_desktops = getDesktopCount ();
79
80   for (i=0; i<n_desktops; ++i)
81     {
82       desktop = getDesktop (i);
83       s = Accessible_getName (desktop);
84       fprintf (stderr, "desktop %d name: %s\n", i, s);
85       SPI_freeString (s);
86       n_apps = Accessible_getChildCount (desktop);
87       for (j=0; j<n_apps; ++j)
88         {
89           application = Accessible_getChildAtIndex (desktop, j);
90           s = Accessible_getName (application);
91           fprintf (stderr, "app %d name: %s\n", j, s);
92           SPI_freeString (s);
93           Accessible_unref (application);
94         }
95       Accessible_unref (desktop);
96     }
97
98   /* prepare the keyboard snoopers */
99   command_key_listener = createAccessibleKeystrokeListener (report_command_key_event, NULL);
100   ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
101   
102   /* will listen only to Alt-key combinations, and only to KeyPress events */
103   registerAccessibleKeystrokeListener(command_key_listener,
104                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
105                                       SPI_KEYMASK_ALT,
106                                       (unsigned long) ( KeyPress ),
107                                       SPI_KEYLISTENER_ALL_WINDOWS);
108   
109   /* will listen only to unshifted key events, both press and release */
110   registerAccessibleKeystrokeListener(ordinary_key_listener,
111                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
112                                       SPI_KEYMASK_UNMODIFIED,
113                                       (unsigned long) ( KeyPress | KeyRelease),
114                                       SPI_KEYLISTENER_NOSYNC);
115                                       
116   /* will listen only to shifted key events, both press and release */
117   registerAccessibleKeystrokeListener(ordinary_key_listener,
118                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
119                                       SPI_KEYMASK_SHIFT,
120                                       (unsigned long) ( KeyPress | KeyRelease),
121                                       SPI_KEYLISTENER_NOSYNC);
122
123   get_environment_vars ();
124
125   SPI_event_main ();
126
127   setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);
128
129   return SPI_exit ();
130 }
131
132 static void
133 get_environment_vars (void)
134 {
135   if (g_getenv ("FESTIVAL"))
136     {
137       fprintf (stderr, "Using festival\n");
138       use_festival = TRUE;
139       if (g_getenv ("FESTIVAL_CHATTY"))
140         {
141           festival_chatty = TRUE;
142         }
143     }
144   if (g_getenv ("MAGNIFIER"))
145     {
146       fprintf (stderr, "Using magnifier\n");
147       use_magnifier = TRUE;
148     }  
149 }
150
151 void
152 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
153 {
154   char *s;
155   int len;
156
157   g_warning ("Report focused !");
158
159   if (use_festival)
160     {
161     if (festival_chatty)            
162       {
163         _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
164       }
165       fprintf (stderr, "getting Name\n");
166       s = Accessible_getName (obj);
167       _festival_say (s, "voice_kal_diphone",
168                      shutup_previous_speech || festival_chatty);
169       SPI_freeString (s);
170     }
171   
172   if (Accessible_isComponent (obj))
173     {
174       long x, y, width, height;
175       AccessibleComponent *component = Accessible_getComponent (obj);
176       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
177                                       SPI_COORD_TYPE_SCREEN);
178       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
179                x, y, x+width, y+height);
180       if (use_magnifier) {
181               magnifier_set_roi ((short) 0, x, y, width, height);
182       }
183     }
184   /* if this is a text object, speak the first sentence. */
185
186   if (Accessible_isText(obj))
187
188   {
189      AccessibleText *text_interface;
190      long start_offset, end_offset;
191      char *first_sentence = "empty";
192      text_interface = Accessible_getText (obj);
193      first_sentence = AccessibleText_getTextAtOffset (
194                text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
195      if (first_sentence)
196        {
197          _festival_say(first_sentence, "voice_don_diphone", FALSE);
198          SPI_freeString (first_sentence);
199        }
200      len = AccessibleText_getCharacterCount (text_interface);
201      s = AccessibleText_getText (text_interface, 0, len);
202      fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
203   }
204 }
205
206 void
207 report_focus_event (AccessibleEvent *event, void *user_data)
208 {
209   char *s;
210
211   g_warning ("report focus event");
212
213   g_return_if_fail (event->source != NULL);
214
215   s = Accessible_getName (event->source);
216   if (cspi_warn_ev (cspi_ev (), "Foobar"))
217     {
218       fprintf (stderr, "%s event from %s\n", event->type, s);
219       SPI_freeString (s);
220       report_focussed_accessible (event->source, TRUE);
221     }
222   Accessible_getParent (event->source);
223 }
224
225 void
226 report_button_press (AccessibleEvent *event, void *user_data)
227 {
228   char *s;
229
230   g_return_if_fail (event->source != NULL);
231
232   s = Accessible_getName (event->source);
233
234   fprintf (stderr, "%s event from %s\n", event->type, s);
235   SPI_freeString (s);
236   s = Accessible_getDescription (event->source);
237   fprintf (stderr, "Object description %s\n", s);
238   SPI_freeString (s);
239 }
240
241 void
242 check_property_change (AccessibleEvent *event, void *user_data)
243 {
244   AccessibleSelection *selection = Accessible_getSelection (event->source);
245   int n_selections;
246   int i;
247   char *s;
248   if (selection)
249   {
250     n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
251     s = Accessible_getName (event->source);
252     fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
253              event->type, s, n_selections);
254     SPI_freeString (s);
255   /* for now, speak entire selection set */
256     for (i=0; i<n_selections; ++i)
257       {
258         Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
259         g_return_if_fail (obj);
260         s = Accessible_getName (obj);
261         fprintf (stderr, "Child %d, name=%s\n", i, s);
262         SPI_freeString (s);
263         report_focussed_accessible (obj, i==0);
264     }
265   }
266 }
267
268 static void
269 simple_at_exit ()
270 {
271   deregisterGlobalEventListenerAll (focus_listener);
272   AccessibleEventListener_unref    (focus_listener);
273
274   deregisterGlobalEventListenerAll (property_listener);
275   AccessibleEventListener_unref    (property_listener);
276
277   deregisterGlobalEventListenerAll (button_listener);
278   AccessibleEventListener_unref    (button_listener);
279
280   deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
281   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
282   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
283   AccessibleKeystrokeListener_unref (command_key_listener);
284   AccessibleKeystrokeListener_unref (ordinary_key_listener);
285   
286   SPI_event_quit ();
287 }
288
289 static SPIBoolean
290 is_command_key (AccessibleKeystroke *key)
291 {
292   switch (key->keyID)
293     {
294     case 'Q':
295     case 'q':
296             simple_at_exit(); 
297             return TRUE; /* not reached */
298     case 'M':
299     case 'm':
300             use_magnifier = ! use_magnifier;
301             return TRUE;
302     case 'F':
303     case 'f':
304             use_festival = ! use_festival;
305             return TRUE;
306     default:
307             return FALSE;
308     }
309 }
310
311 static SPIBoolean
312 report_command_key_event (AccessibleKeystroke *key, void *user_data)
313 {
314   fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
315           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
316           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
317           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
318           (int) key->keycode);
319   return is_command_key (key);
320 }
321
322
323 static SPIBoolean
324 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
325 {
326   fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
327            (long) key->keyID,
328            (unsigned int) key->modifiers,
329            (int) key->keycode,
330            (long int) key->timestamp);
331   return FALSE;
332 }
333
334 static int
335 _festival_init ()
336 {
337   int fd;
338   struct sockaddr_in name;
339   int tries = 2;
340
341   name.sin_family = AF_INET;
342   name.sin_port = htons (1314);
343   name.sin_addr.s_addr = htonl(INADDR_ANY);
344   fd = socket (PF_INET, SOCK_STREAM, 0);
345
346   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
347     if (!tries--) {
348       perror ("connect");
349       return -1;
350     }
351   }
352
353   _festival_write ("(audio_mode'async)\n", fd);
354   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
355   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
356   return fd;
357 }
358
359 static void 
360 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
361 {
362   static int fd = 0;
363   gchar *quoted;
364   gchar *p;
365   gchar prefix[50];
366   static gchar voice_spec[32];
367   
368   if (!fd)
369     {
370       fd = _festival_init ();
371     }
372
373   fprintf (stderr, "saying text: %s\n", text);
374   
375   quoted = g_malloc(64+strlen(text)*2);
376
377   sprintf (prefix, "(SayText \"");
378
379   strncpy(quoted, prefix, 10);
380   p = quoted+strlen(prefix);
381   while (*text) {
382     if ( *text == '\\' || *text == '"' )
383       *p = '\\';
384     *p++ = *text++;
385   }
386   *p++ = '"';
387   *p++ = ')';
388   *p++ = '\n';
389   *p = 0;
390
391   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
392   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
393     {
394       snprintf (voice_spec, 32, "(%s)\n", voice); 
395       _festival_write (voice_spec, fd);
396       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
397       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
398     }
399
400   _festival_write (quoted, fd);
401
402   g_free(quoted);
403 }
404
405 static void
406 _festival_write (const gchar *command_string, int fd)
407 {
408   fprintf(stderr, command_string);
409   if (fd < 0) {
410     perror("socket");
411     return;
412   }
413   write(fd, command_string, strlen(command_string));
414 }
415