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