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