2001-12-10 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 "../util/mag_client.h"
29 #include "../cspi/spi-private.h" /* A hack for now */
30
31 static void report_focus_event    (AccessibleEvent *event, void *user_data);
32 static void report_generic_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 *generic_listener;
50 static AccessibleEventListener *button_listener;
51 static AccessibleKeystrokeListener *command_key_listener;
52 static AccessibleKeystrokeListener *ordinary_key_listener;
53
54 int
55 main (int argc, char **argv)
56 {
57   int i, j;
58   int n_desktops;
59   int n_apps;
60   char *s;
61   Accessible *desktop;
62   Accessible *application;
63   const char *modules;
64
65   if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
66     {
67       printf ("Usage: simple-at\n");
68       printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
69       exit (0);
70     }
71
72   modules = g_getenv ("GTK_MODULES");
73   if (!modules || modules [0] == '\0')
74     {
75       putenv ("GTK_MODULES=gail:at-bridge");
76     }
77   modules = NULL;
78
79   SPI_init ();
80
81   focus_listener = createAccessibleEventListener (report_focus_event, NULL);
82   property_listener = createAccessibleEventListener (check_property_change, NULL); 
83   generic_listener = createAccessibleEventListener (report_generic_event, NULL); 
84   button_listener = createAccessibleEventListener (report_button_press, NULL);
85   registerGlobalEventListener (focus_listener, "focus:");
86   registerGlobalEventListener (property_listener, "object:property-change:accessible-selection"); 
87   registerGlobalEventListener (generic_listener, "object:selection-changed"); 
88   registerGlobalEventListener (generic_listener, "object:children-changed"); 
89   registerGlobalEventListener (generic_listener, "object:visible-data-changed"); 
90   registerGlobalEventListener (generic_listener, "object:text-selection-changed"); 
91   registerGlobalEventListener (generic_listener, "object:text-caret-moved"); 
92   registerGlobalEventListener (generic_listener, "object:text-changed"); 
93   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
94   n_desktops = getDesktopCount ();
95
96   for (i=0; i<n_desktops; ++i)
97     {
98       desktop = getDesktop (i);
99       s = Accessible_getName (desktop);
100       fprintf (stderr, "desktop %d name: %s\n", i, s);
101       SPI_freeString (s);
102       n_apps = Accessible_getChildCount (desktop);
103       for (j=0; j<n_apps; ++j)
104         {
105           application = Accessible_getChildAtIndex (desktop, j);
106           s = Accessible_getName (application);
107           fprintf (stderr, "app %d name: %s\n", j, s);
108           SPI_freeString (s);
109           Accessible_unref (application);
110         }
111       Accessible_unref (desktop);
112     }
113
114   /* prepare the keyboard snoopers */
115   command_key_listener = createAccessibleKeystrokeListener (report_command_key_event, NULL);
116   ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
117   
118   /* will listen only to Alt-key combinations, and only to KeyPress events */
119   registerAccessibleKeystrokeListener(command_key_listener,
120                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
121                                       SPI_KEYMASK_ALT,
122                                       (unsigned long) ( KeyPress ),
123                                       SPI_KEYLISTENER_ALL_WINDOWS);
124   
125   /* will listen only to unshifted key events, both press and release */
126   registerAccessibleKeystrokeListener(ordinary_key_listener,
127                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
128                                       SPI_KEYMASK_UNMODIFIED,
129                                       (unsigned long) ( KeyPress | KeyRelease),
130                                       SPI_KEYLISTENER_NOSYNC);
131                                       
132   /* will listen only to shifted key events, both press and release */
133   registerAccessibleKeystrokeListener(ordinary_key_listener,
134                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
135                                       SPI_KEYMASK_SHIFT,
136                                       (unsigned long) ( KeyPress | KeyRelease),
137                                       SPI_KEYLISTENER_NOSYNC);
138
139   get_environment_vars ();
140
141   SPI_event_main ();
142
143   setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);
144
145   return SPI_exit ();
146 }
147
148 static void
149 get_environment_vars (void)
150 {
151   if (g_getenv ("FESTIVAL"))
152     {
153       fprintf (stderr, "Using festival\n");
154       use_festival = TRUE;
155       if (g_getenv ("FESTIVAL_CHATTY"))
156         {
157           festival_chatty = TRUE;
158         }
159     }
160   if (g_getenv ("MAGNIFIER"))
161     {
162       fprintf (stderr, "Using magnifier\n");
163       use_magnifier = TRUE;
164     }  
165   else
166     {
167       fprintf (stderr, "Not using magnifier\n");
168     }
169
170   if (!use_festival)
171     {
172       fprintf (stderr, "No speech output\n");
173     }
174 }
175
176 void
177 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
178 {
179   char *s;
180   int len;
181
182   if (use_festival)
183     {
184       if (festival_chatty)          
185         {
186           _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
187         }
188       fprintf (stderr, "getting Name\n");
189       s = Accessible_getName (obj);
190       _festival_say (s, "voice_kal_diphone",
191                      shutup_previous_speech || festival_chatty);
192       SPI_freeString (s);
193     }
194   
195   if (Accessible_isComponent (obj))
196     {
197       long x, y, width, height;
198       AccessibleComponent *component = Accessible_getComponent (obj);
199       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
200                                       SPI_COORD_TYPE_SCREEN);
201       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
202                x, y, x+width, y+height);
203       if (use_magnifier) {
204               magnifier_set_roi ((short) 0, x, y, width, height);
205       }
206     }
207
208   if (Accessible_isValue (obj))
209     {
210       AccessibleValue *value = Accessible_getValue (obj);
211       fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
212                AccessibleValue_getCurrentValue (value),
213                AccessibleValue_getMinimumValue (value),
214                AccessibleValue_getMaximumValue (value));
215     }
216   /* if this is a text object, speak the first sentence. */
217
218   if (Accessible_isText(obj))
219
220   {
221      AccessibleText *text_interface;
222      long start_offset, end_offset;
223      char *first_sentence = "empty";
224      text_interface = Accessible_getText (obj);
225      first_sentence = AccessibleText_getTextAtOffset (
226                text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
227      if (first_sentence)
228        {
229          _festival_say(first_sentence, "voice_don_diphone", FALSE);
230          SPI_freeString (first_sentence);
231        }
232      len = AccessibleText_getCharacterCount (text_interface);
233      s = AccessibleText_getText (text_interface, 0, len);
234      fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
235   }
236 }
237
238 void
239 report_focus_event (AccessibleEvent *event, void *user_data)
240 {
241   char *s;
242
243   g_return_if_fail (event->source != NULL);
244
245   s = Accessible_getName (event->source);
246   if (s)
247     {
248       fprintf (stderr, "%s event from %s\n", event->type, s);
249       SPI_freeString (s);
250       report_focussed_accessible (event->source, TRUE);
251     }
252   Accessible_getParent (event->source);
253 }
254
255 void
256 report_generic_event (AccessibleEvent *event, void *user_data)
257 {
258   fprintf (stderr, "%s event received\n", event->type);
259 }
260
261 void
262 report_button_press (AccessibleEvent *event, void *user_data)
263 {
264   char *s;
265
266   g_return_if_fail (event->source != NULL);
267
268   s = Accessible_getName (event->source);
269
270   fprintf (stderr, "%s event from %s\n", event->type, s);
271   SPI_freeString (s);
272   s = Accessible_getDescription (event->source);
273   fprintf (stderr, "Object description %s\n", s);
274   SPI_freeString (s);
275 }
276
277 void
278 check_property_change (AccessibleEvent *event, void *user_data)
279 {
280   AccessibleSelection *selection = Accessible_getSelection (event->source);
281   int n_selections;
282   int i;
283   char *s;
284   if (selection)
285   {
286     n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
287     s = Accessible_getName (event->source);
288     fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
289              event->type, s, n_selections);
290     SPI_freeString (s);
291   /* for now, speak entire selection set */
292     for (i=0; i<n_selections; ++i)
293       {
294         Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
295         g_return_if_fail (obj);
296         s = Accessible_getName (obj);
297         fprintf (stderr, "Child %d, name=%s\n", i, s);
298         SPI_freeString (s);
299         report_focussed_accessible (obj, i==0);
300     }
301   }
302 }
303
304 static void
305 simple_at_exit ()
306 {
307   deregisterGlobalEventListenerAll (focus_listener);
308   AccessibleEventListener_unref    (focus_listener);
309
310   deregisterGlobalEventListenerAll (property_listener);
311   AccessibleEventListener_unref    (property_listener);
312
313   deregisterGlobalEventListenerAll (generic_listener);
314   AccessibleEventListener_unref    (generic_listener);
315
316   deregisterGlobalEventListenerAll (button_listener);
317   AccessibleEventListener_unref    (button_listener);
318
319   deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
320   AccessibleKeystrokeListener_unref (command_key_listener);
321
322   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
323   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
324   AccessibleKeystrokeListener_unref (ordinary_key_listener);
325   
326   SPI_event_quit ();
327 }
328
329 static SPIBoolean
330 is_command_key (AccessibleKeystroke *key)
331 {
332   switch (key->keyID)
333     {
334     case 'Q':
335     case 'q':
336             simple_at_exit(); 
337             return TRUE; /* not reached */
338     case 'M':
339     case 'm':
340             use_magnifier = ! use_magnifier;
341             return TRUE;
342     case 'F':
343     case 'f':
344             use_festival = ! use_festival;
345             return TRUE;
346     default:
347             return FALSE;
348     }
349 }
350
351 static SPIBoolean
352 report_command_key_event (AccessibleKeystroke *key, void *user_data)
353 {
354   fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
355           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
356           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
357           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
358           (int) key->keycode);
359   return is_command_key (key);
360 }
361
362
363 static SPIBoolean
364 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
365 {
366   fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
367            (long) key->keyID,
368            (unsigned int) key->modifiers,
369            (int) key->keycode,
370            (long int) key->timestamp);
371   return FALSE;
372 }
373
374 static int
375 _festival_init ()
376 {
377   int fd;
378   struct sockaddr_in name;
379   int tries = 2;
380
381   name.sin_family = AF_INET;
382   name.sin_port = htons (1314);
383   name.sin_addr.s_addr = htonl(INADDR_ANY);
384   fd = socket (PF_INET, SOCK_STREAM, 0);
385
386   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
387     if (!tries--) {
388       perror ("connect");
389       return -1;
390     }
391   }
392
393   _festival_write ("(audio_mode'async)\n", fd);
394   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
395   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
396   return fd;
397 }
398
399 static void 
400 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
401 {
402   static int fd = 0;
403   gchar *quoted;
404   gchar *p;
405   gchar prefix[50];
406   static gchar voice_spec[32];
407   
408   if (!fd)
409     {
410       fd = _festival_init ();
411     }
412
413   fprintf (stderr, "saying text: %s\n", text);
414   
415   quoted = g_malloc(64+strlen(text)*2);
416
417   sprintf (prefix, "(SayText \"");
418
419   strncpy(quoted, prefix, 10);
420   p = quoted+strlen(prefix);
421   while (*text) {
422     if ( *text == '\\' || *text == '"' )
423       *p = '\\';
424     *p++ = *text++;
425   }
426   *p++ = '"';
427   *p++ = ')';
428   *p++ = '\n';
429   *p = 0;
430
431   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
432   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
433     {
434       snprintf (voice_spec, 32, "(%s)\n", voice); 
435       _festival_write (voice_spec, fd);
436       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
437       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
438     }
439
440   _festival_write (quoted, fd);
441
442   g_free(quoted);
443 }
444
445 static void
446 _festival_write (const gchar *command_string, int fd)
447 {
448   fprintf(stderr, command_string);
449   if (fd < 0) {
450     perror("socket");
451     return;
452   }
453   write(fd, command_string, strlen(command_string));
454 }
455