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