2002-09-13 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, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <stdlib.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <sys/un.h>
30 #undef MAGNIFIER_ENABLED
31 #include "../cspi/spi-private.h" /* A hack for now */
32
33 #define PRINT_TREE
34
35 static void report_focus_event    (const AccessibleEvent *event, void *user_data);
36 static void report_generic_event  (const AccessibleEvent *event, void *user_data);
37 static void report_window_event  (const AccessibleEvent *event, void *user_data);
38 static void report_text_event     (const AccessibleEvent *event, void *user_data);
39 static void report_button_press   (const AccessibleEvent *event, void *user_data);
40 static void check_property_change (const AccessibleEvent *event, void *user_data);
41 static SPIBoolean report_command_key_event  (const AccessibleKeystroke *stroke, void *user_data);
42 static SPIBoolean report_ordinary_key_event (const AccessibleKeystroke *stroke, void *user_data);
43 static void get_environment_vars (void);
44
45 static int _festival_init ();
46 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
47 static void _festival_write (const char *buff, int fd);
48
49 #ifdef PRINT_TREE
50 static void print_accessible_tree (Accessible *accessible, char *prefix);
51 #endif
52
53 #ifdef MAGNIFIER_ENABLED
54 static SPIBoolean use_magnifier = FALSE;
55 #endif
56
57 static SPIBoolean use_festival = FALSE;
58 static SPIBoolean festival_chatty = FALSE;
59 static SPIBoolean name_changed = FALSE;
60
61 static AccessibleEventListener *focus_listener;
62 static AccessibleEventListener *property_listener;
63 static AccessibleEventListener *generic_listener;
64 static AccessibleEventListener *window_listener;
65 static AccessibleEventListener *button_listener;
66 static AccessibleEventListener *text_listener;
67 static AccessibleKeystrokeListener *command_key_listener;
68 static AccessibleKeystrokeListener *ordinary_key_listener;
69 static AccessibleKeySet            *command_keyset;
70
71 int
72 main (int argc, char **argv)
73 {
74   int i, j;
75   int n_desktops;
76   int n_apps;
77   char *s;
78   Accessible *desktop;
79   Accessible *application;
80   const char *modules;
81
82   if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
83     {
84       printf ("Usage: simple-at\n");
85       printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
86       exit (0);
87     }
88
89   modules = g_getenv ("GTK_MODULES");
90   if (!modules || modules [0] == '\0')
91     {
92       putenv ("GTK_MODULES=");
93     }
94   modules = NULL;
95
96   SPI_init ();
97
98   focus_listener = SPI_createAccessibleEventListener (report_focus_event, NULL);
99   property_listener = SPI_createAccessibleEventListener (check_property_change, NULL); 
100   generic_listener = SPI_createAccessibleEventListener (report_generic_event, NULL); 
101   window_listener = SPI_createAccessibleEventListener (report_window_event, NULL); 
102   text_listener = SPI_createAccessibleEventListener (report_text_event, NULL); 
103   button_listener = SPI_createAccessibleEventListener (report_button_press, NULL);
104   SPI_registerGlobalEventListener (focus_listener, "focus:");
105   SPI_registerGlobalEventListener (property_listener, "object:property-change");
106 /* :accessible-selection"); */
107   SPI_registerGlobalEventListener (property_listener, "object:property-change:accessible-name");
108   SPI_registerGlobalEventListener (generic_listener, "object:selection-changed"); 
109   SPI_registerGlobalEventListener (generic_listener, "object:children-changed"); 
110   SPI_registerGlobalEventListener (generic_listener, "object:visible-data-changed"); 
111   SPI_registerGlobalEventListener (generic_listener, "object:text-selection-changed"); 
112   SPI_registerGlobalEventListener (text_listener, "object:text-caret-moved"); 
113   SPI_registerGlobalEventListener (text_listener, "object:text-changed"); 
114   SPI_registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
115   SPI_registerGlobalEventListener (window_listener, "window:minimize");
116   n_desktops = SPI_getDesktopCount ();
117
118   for (i=0; i<n_desktops; ++i)
119     {
120       desktop = SPI_getDesktop (i);
121       s = Accessible_getName (desktop);
122       fprintf (stderr, "desktop %d name: %s\n", i, s);
123       SPI_freeString (s);
124       n_apps = Accessible_getChildCount (desktop);
125       for (j=0; j<n_apps; ++j)
126         {
127           application = Accessible_getChildAtIndex (desktop, j);
128           s = Accessible_getName (application);
129           fprintf (stderr, "app %d name: %s\n", j, s);
130 #ifdef PRINT_TREE
131           print_accessible_tree (application, "*");
132 #endif
133           SPI_freeString (s);
134           Accessible_unref (application);
135         }
136       Accessible_unref (desktop);
137     }
138
139   /* prepare the keyboard snoopers */
140   command_key_listener = SPI_createAccessibleKeystrokeListener (report_command_key_event, NULL);
141   ordinary_key_listener = SPI_createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
142
143   command_keyset = SPI_createAccessibleKeySet (11, "qmf23456789", NULL, NULL);
144   
145   /* will listen only to Control-Alt-q KeyPress events */
146   SPI_registerAccessibleKeystrokeListener(command_key_listener,
147                                           command_keyset,
148                                           SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
149                                           (unsigned long) ( SPI_KEY_PRESSED ),
150                                           SPI_KEYLISTENER_ALL_WINDOWS);
151
152   /* will listen only to CAPSLOCK key events, both press and release */
153   SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
154                                           (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
155                                           SPI_KEYMASK_SHIFTLOCK,
156                                           (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
157                                           SPI_KEYLISTENER_NOSYNC);
158
159   get_environment_vars ();
160
161   SPI_event_main ();
162
163   putenv ("AT_BRIDGE_SHUTDOWN=1");
164
165   return SPI_exit ();
166 }
167
168 static void
169 get_environment_vars (void)
170 {
171   if (g_getenv ("FESTIVAL"))
172     {
173       fprintf (stderr, "Using festival\n");
174       use_festival = TRUE;
175       if (g_getenv ("FESTIVAL_CHATTY"))
176         {
177           festival_chatty = TRUE;
178         }
179     }
180 #ifdef MAGNIFIER_ENABLED
181   if (g_getenv ("MAGNIFIER"))
182     {
183       fprintf (stderr, "Using magnifier\n");
184       use_magnifier = TRUE;
185     }  
186   else
187     {
188       fprintf (stderr, "Not using magnifier\n");
189     }
190 #endif
191
192   if (!use_festival)
193     {
194       fprintf (stderr, "No speech output\n");
195     }
196 }
197
198 #ifdef PRINT_TREE
199 static void
200 print_accessible_tree (Accessible *accessible, char *prefix)
201 {
202         int n_children;
203         int i;
204         char *name;
205         char *role_name;
206         char *parent_name = NULL;
207         char *parent_role = NULL;
208         char child_prefix[100];
209         Accessible *child;
210         Accessible *parent;
211         
212         strncpy (child_prefix, prefix, 98);
213         strcat (child_prefix, "*");
214         parent = Accessible_getParent (accessible);
215         if (parent)
216           {
217                 parent_name = Accessible_getName (parent);
218                 parent_role = Accessible_getRoleName (parent);
219                 Accessible_unref (parent);
220           }
221         name = Accessible_getName (accessible);
222         role_name = Accessible_getRoleName (accessible);
223         fprintf (stdout, "%sAccessible [%s] \"%s\"; parent [%s] %s.\n",
224                  prefix, role_name, name, parent_role, parent_name);
225         SPI_freeString (name);
226         SPI_freeString (role_name);
227         SPI_freeString (parent_name);
228         SPI_freeString (parent_role);
229         n_children = Accessible_getChildCount (accessible);
230         for (i = 0; i < n_children; ++i)
231                 {
232                         child = Accessible_getChildAtIndex (accessible, i);
233                         print_accessible_tree (child, child_prefix);
234                         Accessible_unref (child);
235                 }
236 }
237 #endif
238
239 void
240 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
241 {
242   char *s;
243   int len;
244   long x, y, width, height;
245   /* hack for GUADEC demo, to make sure name changes are spoken */
246   shutup_previous_speech = (shutup_previous_speech && !name_changed);
247
248   if (use_festival)
249     {
250       if (festival_chatty)          
251         {
252           s = Accessible_getRoleName (obj);     
253           _festival_say (s, "voice_don_diphone", shutup_previous_speech);
254           SPI_freeString (s);
255         }
256       fprintf (stderr, "getting Name\n");
257       s = Accessible_getName (obj);
258       _festival_say (s, "voice_kal_diphone",
259                      shutup_previous_speech || festival_chatty);
260       SPI_freeString (s);
261     }
262   
263   if (Accessible_isComponent (obj))
264     {
265       AccessibleComponent *component = Accessible_getComponent (obj);
266       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
267                                       SPI_COORD_TYPE_SCREEN);
268       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
269                x, y, x+width, y+height);
270       if (Accessible_isText (obj))
271         {
272           long x0, y0, xN, yN, w0, h0, wN, hN, nchars;
273           AccessibleText *text = Accessible_getText (obj);
274           nchars = AccessibleText_getCharacterCount (text);
275           if (nchars > 0) 
276             {
277               AccessibleText_getCharacterExtents (text, 0, &x0, &y0, &w0, &h0,
278                                               SPI_COORD_TYPE_SCREEN);
279               AccessibleText_getCharacterExtents (text, nchars-1, &xN, &yN, &wN, &hN, 
280                                                   SPI_COORD_TYPE_SCREEN);
281               x = MIN (x0, xN);
282               width = MAX (x0 + w0, xN + wN) - x;
283               fprintf (stderr, "Text bounding box: (%ld, %ld) ; (%ld, %ld)\n",
284                        x, y, x+width, y+height);
285             }
286         }
287 #ifdef MAGNIFIER_ENABLED
288       if (use_magnifier) {
289         magnifier_set_roi ((short) 0, x, y, width, height);
290       }
291 #endif
292     }
293
294
295   if (Accessible_isValue (obj))
296     {
297       AccessibleValue *value = Accessible_getValue (obj);
298       fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
299                AccessibleValue_getCurrentValue (value),
300                AccessibleValue_getMinimumValue (value),
301                AccessibleValue_getMaximumValue (value));
302     }
303   /* if this is a text object, speak the first sentence. */
304
305   if (Accessible_isText(obj))
306
307   {
308      AccessibleText *text_interface;
309      long start_offset, end_offset;
310      char *first_sentence = "empty";
311      text_interface = Accessible_getText (obj);
312      first_sentence = AccessibleText_getTextAtOffset (
313                text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
314      if (first_sentence && use_festival)
315        {
316          _festival_say(first_sentence, "voice_don_diphone", FALSE);
317          SPI_freeString (first_sentence);
318        }
319      len = AccessibleText_getCharacterCount (text_interface);
320      s = AccessibleText_getText (text_interface, 0, len);
321      fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
322   }
323 }
324
325 void
326 report_focus_event (const AccessibleEvent *event, void *user_data)
327 {
328   char *s;
329
330   g_return_if_fail (event->source != NULL);
331   s = Accessible_getName (event->source);
332   if (s)
333     {
334       fprintf (stderr, "%s event from %s\n", event->type, s);
335       SPI_freeString (s);
336       report_focussed_accessible (event->source, TRUE);
337     }
338   Accessible_getParent (event->source);
339   name_changed = FALSE;
340 }
341
342 void
343 report_generic_event (const AccessibleEvent *event, void *user_data)
344 {
345   fprintf (stderr, "%s event received\n", event->type);
346 }
347
348 void
349 report_window_event (const AccessibleEvent *event, void *user_data)
350 {
351   fprintf (stderr, "%s event received\n", event->type);
352 }
353
354 void
355 report_text_event (const AccessibleEvent *event, void *user_data)
356 {
357   AccessibleText *text = Accessible_getText (event->source);
358   fprintf (stderr, "%s event received\n", event->type);
359 #ifdef MAGNIFIER_ENABLED
360   if (use_magnifier && strcmp (event->type, "object:text-changed"))
361     {
362       long offset = AccessibleText_getCaretOffset (text);
363       long x, y, w, h;
364       fprintf (stderr, "offset %d\n", (int) offset);
365       AccessibleText_getCharacterExtents (text, offset, &x, &y, &w, &h, 
366                                           SPI_COORD_TYPE_SCREEN);
367       fprintf (stderr, "new roi %d %d %d %d\n", (int) x, (int) y, (int) w, (int) h);
368       magnifier_set_roi ((short) 0, x, y, w, h);
369     }
370 #endif
371   if (!strcmp (event->type, "object:text-changed"))
372     {
373       long start, end;
374       char *new_text = AccessibleText_getTextAtOffset (text, (long) event->detail1, SPI_TEXT_BOUNDARY_WORD_START, &start, &end);
375       _festival_say (new_text, "voice_kal_diphone", FALSE);
376       fprintf (stderr, "text changed: %s", new_text ? new_text : "");
377       SPI_freeString (new_text);
378     }
379   else
380     {
381       long start, end;
382       char *word_text = AccessibleText_getTextAtOffset (text, (long) event->detail1, SPI_TEXT_BOUNDARY_WORD_START, &start, &end);
383       char *sentence_text = AccessibleText_getTextAtOffset (text, (long) event->detail1, SPI_TEXT_BOUNDARY_SENTENCE_START, &start, &end);
384       fprintf (stderr, "text changed: word %s; sentence %s at %d",
385                (word_text ? word_text : ""),
386                (sentence_text ? sentence_text : ""),
387                event->detail1);
388       if (word_text) SPI_freeString (word_text);
389       if (sentence_text) SPI_freeString (sentence_text);
390     }
391 }
392
393 void
394 report_button_press (const AccessibleEvent *event, void *user_data)
395 {
396   char *s;
397
398   g_return_if_fail (event->source != NULL);
399
400   s = Accessible_getName (event->source);
401
402   fprintf (stderr, "%s event from %s\n", event->type, s);
403   SPI_freeString (s);
404   s = Accessible_getDescription (event->source);
405   fprintf (stderr, "Object description %s\n", s);
406   SPI_freeString (s);
407 }
408
409 void
410 check_property_change (const AccessibleEvent *event, void *user_data)
411 {
412   AccessibleSelection *selection = Accessible_getSelection (event->source);
413   int n_selections;
414   int i;
415   char *s;
416   fprintf (stderr, "property change event!\n");
417   if (selection)
418   {
419     n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
420     s = Accessible_getName (event->source);
421     fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
422              event->type, s, n_selections);
423     SPI_freeString (s);
424   /* for now, speak entire selection set */
425     for (i=0; i<n_selections; ++i)
426       {
427         Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
428         g_return_if_fail (obj);
429         s = Accessible_getName (obj);
430         fprintf (stderr, "Child %d, name=%s\n", i, s);
431         SPI_freeString (s);
432         report_focussed_accessible (obj, i==0);
433     }
434   }
435   else if (!strcmp (event->type, "object:property-change:accessible-name"))
436     {
437       name_changed = TRUE;          
438       report_focussed_accessible (event->source, TRUE);
439     }
440   else
441     {
442       fprintf (stderr, "Property change %s received\n", event->type);
443     }
444 }
445
446 static void
447 simple_at_exit ()
448 {
449   SPI_deregisterGlobalEventListenerAll (focus_listener);
450   AccessibleEventListener_unref        (focus_listener);
451
452   SPI_deregisterGlobalEventListenerAll (property_listener);
453   AccessibleEventListener_unref        (property_listener);
454
455   SPI_deregisterGlobalEventListenerAll (generic_listener);
456   AccessibleEventListener_unref        (generic_listener);
457
458   SPI_deregisterGlobalEventListenerAll (text_listener);
459   AccessibleEventListener_unref        (text_listener);
460
461   SPI_deregisterGlobalEventListenerAll (button_listener);
462   AccessibleEventListener_unref        (button_listener);
463
464   SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
465   AccessibleKeystrokeListener_unref         (command_key_listener);
466   SPI_freeAccessibleKeySet                  (command_keyset);
467
468   SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFTLOCK);
469   AccessibleKeystrokeListener_unref         (ordinary_key_listener);
470
471   SPI_event_quit ();
472 }
473
474 static SPIBoolean
475 is_command_key (const AccessibleKeystroke *key)
476 {
477   switch (key->keyID)
478     {
479     case 'Q':
480     case 'q':
481             simple_at_exit(); 
482             return TRUE; /* not reached */
483 #ifdef MAGNIFIER_ENABLED
484     case 'M':
485     case 'm':
486             use_magnifier = ! use_magnifier;
487             fprintf (stderr, "%ssing magnifier\n", use_magnifier ? "U" : "Not u");
488             return TRUE;
489 #endif
490     case 'F':
491     case 'f':
492             use_festival = ! use_festival;
493             fprintf (stderr, "%speech output\n", use_festival ? "S" : "No s");
494             return TRUE;
495     default:
496             return FALSE;
497     }
498 }
499
500 static SPIBoolean
501 report_command_key_event (const AccessibleKeystroke *key, void *user_data)
502 {
503   fprintf (stderr, "Command KeyEvent %s%c (keycode %d); string=%s; time=%lx\n",
504           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
505           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
506           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
507           (int) key->keycode,
508           key->keystring,
509           (long int) key->timestamp);
510   return is_command_key (key);
511 }
512
513
514 static SPIBoolean
515 report_ordinary_key_event (const AccessibleKeystroke *key, void *user_data)
516 {
517   fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\tstring=\'%s\'\n\ttime %lx\n",
518            (long) key->keyID,
519            (unsigned int) key->modifiers,
520            (int) key->keycode,
521            key->keystring,
522            (long int) key->timestamp);
523   return FALSE;
524 }
525
526 static int
527 _festival_init ()
528 {
529   int fd;
530   struct sockaddr_in name;
531   int tries = 2;
532
533   name.sin_family = AF_INET;
534   name.sin_port = htons (1314);
535   name.sin_addr.s_addr = htonl(INADDR_ANY);
536   fd = socket (PF_INET, SOCK_STREAM, 0);
537
538   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
539     if (!tries--) {
540       perror ("connect");
541       return -1;
542     }
543   }
544
545   _festival_write ("(audio_mode'async)\n", fd);
546   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
547   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
548   return fd;
549 }
550
551 static void 
552 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
553 {
554   static int fd = 0;
555   gchar *quoted;
556   gchar *p;
557   gchar prefix[50];
558   static gchar voice_spec[32];
559   
560   if (!fd)
561     {
562       fd = _festival_init ();
563     }
564
565   fprintf (stderr, "saying text: %s\n", text);
566   
567   quoted = g_malloc(64+strlen(text)*2);
568
569   sprintf (prefix, "(SayText \"");
570
571   strncpy(quoted, prefix, 10);
572   p = quoted+strlen(prefix);
573   while (*text) {
574     if ( *text == '\\' || *text == '"' )
575       *p = '\\';
576     *p++ = *text++;
577   }
578   *p++ = '"';
579   *p++ = ')';
580   *p++ = '\n';
581   *p = 0;
582
583   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
584   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
585     {
586       snprintf (voice_spec, 32, "(%s)\n", voice); 
587       _festival_write (voice_spec, fd);
588       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
589       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
590     }
591
592   _festival_write (quoted, fd);
593
594   g_free(quoted);
595 }
596
597 static void
598 _festival_write (const gchar *command_string, int fd)
599 {
600   fprintf(stderr, command_string);
601   if (fd < 0) {
602     perror("socket");
603     return;
604   }
605   write(fd, command_string, strlen(command_string));
606 }
607