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