Revised key event support to remove some invalid casts, fix bugs relating
[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    (AccessibleEvent *event, void *user_data);
32 static void report_generic_event  (AccessibleEvent *event, void *user_data);
33 static void report_button_press   (AccessibleEvent *event, void *user_data);
34 static void check_property_change (AccessibleEvent *event, void *user_data);
35 static SPIBoolean report_command_key_event  (AccessibleKeystroke *stroke, void *user_data);
36 static SPIBoolean report_ordinary_key_event (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 *spacebar_key_set;
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:at-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   /* will listen only to Alt-key combinations, and only to KeyPress events */
120   SPI_registerAccessibleKeystrokeListener(command_key_listener,
121                                           (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
122                                           SPI_KEYMASK_ALT,
123                                           (unsigned long) ( SPI_KEY_PRESSED ),
124                                           SPI_KEYLISTENER_ALL_WINDOWS);
125   
126   /* will listen only to unshifted key events, both press and release */
127   SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
128                                           (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
129                                           SPI_KEYMASK_UNMODIFIED,
130                                           (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
131                                           SPI_KEYLISTENER_NOSYNC);
132                                       
133   /* will listen only to shifted key events, both press and release */
134   SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
135                                           (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
136                                           SPI_KEYMASK_SHIFT,
137                                           (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
138                                           SPI_KEYLISTENER_NOSYNC);
139
140   spacebar_key_set = SPI_createAccessibleKeySet (1, " ", NULL, NULL);
141   
142   /* will listen only to shift-spacebar events, on release, globally */
143   SPI_registerAccessibleKeystrokeListener(command_key_listener,
144                                           spacebar_key_set,
145                                           SPI_KEYMASK_SHIFT,
146                                           (unsigned long) ( SPI_KEY_RELEASED ),
147                                           SPI_KEYLISTENER_ALL_WINDOWS);
148
149   
150   get_environment_vars ();
151
152   SPI_event_main ();
153
154   putenv ("AT_BRIDGE_SHUTDOWN=1");
155
156   return SPI_exit ();
157 }
158
159 static void
160 get_environment_vars (void)
161 {
162   if (g_getenv ("FESTIVAL"))
163     {
164       fprintf (stderr, "Using festival\n");
165       use_festival = TRUE;
166       if (g_getenv ("FESTIVAL_CHATTY"))
167         {
168           festival_chatty = TRUE;
169         }
170     }
171   if (g_getenv ("MAGNIFIER"))
172     {
173       fprintf (stderr, "Using magnifier\n");
174       use_magnifier = TRUE;
175     }  
176   else
177     {
178       fprintf (stderr, "Not using magnifier\n");
179     }
180
181   if (!use_festival)
182     {
183       fprintf (stderr, "No speech output\n");
184     }
185 }
186
187 void
188 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
189 {
190   char *s;
191   int len;
192
193   if (use_festival)
194     {
195       if (festival_chatty)          
196         {
197           s = Accessible_getRoleName (obj);     
198           _festival_say (s, "voice_don_diphone", shutup_previous_speech);
199           SPI_freeString (s);
200         }
201       fprintf (stderr, "getting Name\n");
202       s = Accessible_getName (obj);
203       _festival_say (s, "voice_kal_diphone",
204                      shutup_previous_speech || festival_chatty);
205       SPI_freeString (s);
206     }
207   
208   if (Accessible_isComponent (obj))
209     {
210       long x, y, width, height;
211       AccessibleComponent *component = Accessible_getComponent (obj);
212       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
213                                       SPI_COORD_TYPE_SCREEN);
214       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
215                x, y, x+width, y+height);
216       if (use_magnifier) {
217               magnifier_set_roi ((short) 0, x, y, width, height);
218       }
219     }
220
221   if (Accessible_isValue (obj))
222     {
223       AccessibleValue *value = Accessible_getValue (obj);
224       fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
225                AccessibleValue_getCurrentValue (value),
226                AccessibleValue_getMinimumValue (value),
227                AccessibleValue_getMaximumValue (value));
228     }
229   /* if this is a text object, speak the first sentence. */
230
231   if (Accessible_isText(obj))
232
233   {
234      AccessibleText *text_interface;
235      long start_offset, end_offset;
236      char *first_sentence = "empty";
237      text_interface = Accessible_getText (obj);
238      first_sentence = AccessibleText_getTextAtOffset (
239                text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
240      if (first_sentence)
241        {
242          _festival_say(first_sentence, "voice_don_diphone", FALSE);
243          SPI_freeString (first_sentence);
244        }
245      len = AccessibleText_getCharacterCount (text_interface);
246      s = AccessibleText_getText (text_interface, 0, len);
247      fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
248   }
249 }
250
251 void
252 report_focus_event (AccessibleEvent *event, void *user_data)
253 {
254   char *s;
255
256   g_return_if_fail (event->source != NULL);
257
258   s = Accessible_getName (event->source);
259   if (s)
260     {
261       fprintf (stderr, "%s event from %s\n", event->type, s);
262       SPI_freeString (s);
263       report_focussed_accessible (event->source, TRUE);
264     }
265   Accessible_getParent (event->source);
266 }
267
268 void
269 report_generic_event (AccessibleEvent *event, void *user_data)
270 {
271   fprintf (stderr, "%s event received\n", event->type);
272 }
273
274 void
275 report_button_press (AccessibleEvent *event, void *user_data)
276 {
277   char *s;
278
279   g_return_if_fail (event->source != NULL);
280
281   s = Accessible_getName (event->source);
282
283   fprintf (stderr, "%s event from %s\n", event->type, s);
284   SPI_freeString (s);
285   s = Accessible_getDescription (event->source);
286   fprintf (stderr, "Object description %s\n", s);
287   SPI_freeString (s);
288 }
289
290 void
291 check_property_change (AccessibleEvent *event, void *user_data)
292 {
293   AccessibleSelection *selection = Accessible_getSelection (event->source);
294   int n_selections;
295   int i;
296   char *s;
297   if (selection)
298   {
299     n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
300     s = Accessible_getName (event->source);
301     fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
302              event->type, s, n_selections);
303     SPI_freeString (s);
304   /* for now, speak entire selection set */
305     for (i=0; i<n_selections; ++i)
306       {
307         Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
308         g_return_if_fail (obj);
309         s = Accessible_getName (obj);
310         fprintf (stderr, "Child %d, name=%s\n", i, s);
311         SPI_freeString (s);
312         report_focussed_accessible (obj, i==0);
313     }
314   }
315 }
316
317 static void
318 simple_at_exit ()
319 {
320   SPI_deregisterGlobalEventListenerAll (focus_listener);
321   AccessibleEventListener_unref        (focus_listener);
322
323   SPI_deregisterGlobalEventListenerAll (property_listener);
324   AccessibleEventListener_unref        (property_listener);
325
326   SPI_deregisterGlobalEventListenerAll (generic_listener);
327   AccessibleEventListener_unref        (generic_listener);
328
329   SPI_deregisterGlobalEventListenerAll (button_listener);
330   AccessibleEventListener_unref        (button_listener);
331
332   SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
333   AccessibleKeystrokeListener_unref         (command_key_listener);
334
335   SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
336   SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
337   AccessibleKeystrokeListener_unref         (ordinary_key_listener);
338
339   SPI_freeAccessibleKeySet (spacebar_key_set);
340   
341   SPI_event_quit ();
342 }
343
344 static SPIBoolean
345 is_command_key (AccessibleKeystroke *key)
346 {
347   switch (key->keyID)
348     {
349     case 'Q':
350     case 'q':
351             simple_at_exit(); 
352             return TRUE; /* not reached */
353     case 'M':
354     case 'm':
355             use_magnifier = ! use_magnifier;
356             return TRUE;
357     case 'F':
358     case 'f':
359             use_festival = ! use_festival;
360             return TRUE;
361     default:
362             return FALSE;
363     }
364 }
365
366 static SPIBoolean
367 report_command_key_event (AccessibleKeystroke *key, void *user_data)
368 {
369   fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
370           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
371           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
372           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
373           (int) key->keycode);
374   return is_command_key (key);
375 }
376
377
378 static SPIBoolean
379 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
380 {
381   fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
382            (long) key->keyID,
383            (unsigned int) key->modifiers,
384            (int) key->keycode,
385            (long int) key->timestamp);
386   return FALSE;
387 }
388
389 static int
390 _festival_init ()
391 {
392   int fd;
393   struct sockaddr_in name;
394   int tries = 2;
395
396   name.sin_family = AF_INET;
397   name.sin_port = htons (1314);
398   name.sin_addr.s_addr = htonl(INADDR_ANY);
399   fd = socket (PF_INET, SOCK_STREAM, 0);
400
401   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
402     if (!tries--) {
403       perror ("connect");
404       return -1;
405     }
406   }
407
408   _festival_write ("(audio_mode'async)\n", fd);
409   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
410   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
411   return fd;
412 }
413
414 static void 
415 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
416 {
417   static int fd = 0;
418   gchar *quoted;
419   gchar *p;
420   gchar prefix[50];
421   static gchar voice_spec[32];
422   
423   if (!fd)
424     {
425       fd = _festival_init ();
426     }
427
428   fprintf (stderr, "saying text: %s\n", text);
429   
430   quoted = g_malloc(64+strlen(text)*2);
431
432   sprintf (prefix, "(SayText \"");
433
434   strncpy(quoted, prefix, 10);
435   p = quoted+strlen(prefix);
436   while (*text) {
437     if ( *text == '\\' || *text == '"' )
438       *p = '\\';
439     *p++ = *text++;
440   }
441   *p++ = '"';
442   *p++ = ')';
443   *p++ = '\n';
444   *p = 0;
445
446   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
447   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
448     {
449       snprintf (voice_spec, 32, "(%s)\n", voice); 
450       _festival_write (voice_spec, fd);
451       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
452       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
453     }
454
455   _festival_write (quoted, fd);
456
457   g_free(quoted);
458 }
459
460 static void
461 _festival_write (const gchar *command_string, int fd)
462 {
463   fprintf(stderr, command_string);
464   if (fd < 0) {
465     perror("socket");
466     return;
467   }
468   write(fd, command_string, strlen(command_string));
469 }
470