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