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