Added docs for SPI_freeString, and used it to stop memory leak of strings
[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 <cspi/spi.h>
29 #include "../util/mag_client.h"
30
31 static void report_focus_event    (AccessibleEvent *event);
32 static void report_button_press   (AccessibleEvent *event);
33 static void check_property_change (AccessibleEvent *event);
34 static boolean report_command_key_event  (AccessibleKeystroke *stroke);
35 static boolean report_ordinary_key_event (AccessibleKeystroke *stroke);
36 static void get_environment_vars (void);
37
38 static int _festival_init ();
39 static void _festival_say (const char *text, const char *voice, boolean shutup);
40 static void _festival_write (const char *buff, int fd);
41
42 static boolean use_magnifier = FALSE;
43 static boolean use_festival = FALSE;
44 static boolean festival_chatty = FALSE;
45
46 static AccessibleEventListener *focus_listener;
47 static AccessibleEventListener *property_listener;
48 static AccessibleEventListener *button_listener;
49 static AccessibleKeystrokeListener *command_key_listener;
50 static AccessibleKeystrokeListener *ordinary_key_listener;
51
52 int
53 main(int argc, char **argv)
54 {
55   int i, j;
56   int n_desktops;
57   int n_apps;
58   Accessible *desktop;
59   Accessible *application;
60   char *s;
61
62   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
63   {
64     printf ("Usage: simple-at\n");
65     printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
66     exit(0);
67   }
68
69   SPI_init();
70
71   focus_listener = createAccessibleEventListener (report_focus_event);
72   property_listener = createAccessibleEventListener (check_property_change); 
73   button_listener = createAccessibleEventListener (report_button_press);
74   registerGlobalEventListener (focus_listener, "focus:");
75   registerGlobalEventListener (property_listener, "object:property-change:accessible-selection"); 
76   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
77   n_desktops = getDesktopCount ();
78
79   for (i=0; i<n_desktops; ++i)
80     {
81       desktop = getDesktop (i);
82       s = Accessible_getName (desktop);
83       fprintf (stderr, "desktop %d name: %s\n", i, s);
84       SPI_freeString (s);
85       n_apps = Accessible_getChildCount (desktop);
86       for (j=0; j<n_apps; ++j)
87         {
88           application = Accessible_getChildAtIndex (desktop, j);
89           s = Accessible_getName (application);
90           fprintf (stderr, "app %d name: %s\n", j, s);
91           SPI_freeString (s);
92           Accessible_unref (application);
93         }
94       Accessible_unref (desktop);
95     }
96
97   /* prepare the keyboard snoopers */
98   command_key_listener = createAccessibleKeystrokeListener (report_command_key_event);
99   ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event);
100   
101   /* will listen only to Alt-key combinations, and only to KeyPress events */
102   registerAccessibleKeystrokeListener(command_key_listener,
103                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
104                                       SPI_KEYMASK_ALT,
105                                       (unsigned long) ( KeyPress ),
106                                       SPI_KEYLISTENER_ALL_WINDOWS);
107   
108   /* will listen only to unshifted key events, both press and release */
109   registerAccessibleKeystrokeListener(ordinary_key_listener,
110                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
111                                       SPI_KEYMASK_UNMODIFIED,
112                                       (unsigned long) ( KeyPress | KeyRelease),
113                                       SPI_KEYLISTENER_NOSYNC);
114                                       
115   /* will listen only to shifted key events, both press and release */
116   registerAccessibleKeystrokeListener(ordinary_key_listener,
117                                       (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
118                                       SPI_KEYMASK_SHIFT,
119                                       (unsigned long) ( KeyPress | KeyRelease),
120                                       SPI_KEYLISTENER_NOSYNC);
121
122   get_environment_vars();
123
124   SPI_event_main(TRUE);
125 }
126
127 static void
128 get_environment_vars()
129 {
130   if (getenv ("FESTIVAL"))
131   {
132     use_festival = TRUE;
133     if (getenv ("FESTIVAL_CHATTY"))
134     {
135       festival_chatty = TRUE;
136     }
137   }
138   if (getenv("MAGNIFIER"))
139   {
140     use_magnifier = TRUE;
141   }  
142 }
143
144 void
145 report_focussed_accessible (Accessible *obj, boolean shutup_previous_speech)
146 {
147   char *s;
148   int len;
149   if (use_festival)
150     {
151     if (festival_chatty)            
152       {
153         _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
154       }
155       fprintf (stderr, "getting Name\n");
156       s = Accessible_getName (obj);
157       _festival_say (s, "voice_kal_diphone",
158                      shutup_previous_speech || festival_chatty);
159       SPI_freeString (s);
160     }
161   
162   if (Accessible_isComponent (obj))
163     {
164       long x, y, width, height;
165       AccessibleComponent *component = Accessible_getComponent (obj);
166       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
167                                       SPI_COORD_TYPE_SCREEN);
168       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
169                x, y, x+width, y+height);
170       if (use_magnifier) {
171               magnifier_set_roi ((short) 0, x, y, width, height);
172       }
173     }
174   /* if this is a text object, speak the first sentence. */
175
176   if (Accessible_isText(obj))
177
178   {
179      AccessibleText *text_interface;
180      long start_offset, end_offset;
181      char *first_sentence = "empty";
182      text_interface = Accessible_getText (obj);
183      first_sentence = AccessibleText_getTextAtOffset (
184                text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
185      if (first_sentence)
186        {
187          _festival_say(first_sentence, "voice_don_diphone", FALSE);
188          SPI_freeString (first_sentence);
189        }
190      len = AccessibleText_getCharacterCount (text_interface);
191      s = AccessibleText_getText (text_interface, 0, len);
192      fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
193   }
194 }
195
196 void
197 report_focus_event (AccessibleEvent *event)
198 {
199   char *s = Accessible_getName (event->source);
200   fprintf (stderr, "%s event from %s\n", event->type, s);
201   SPI_freeString (s);
202   report_focussed_accessible (event->source, TRUE);
203 }
204
205 void
206 report_button_press (AccessibleEvent *event)
207 {
208   char *s = Accessible_getName (event->source);
209   fprintf (stderr, "%s event from %s\n", event->type, s);
210   SPI_freeString (s);
211   s = Accessible_getDescription (event->source);
212   fprintf (stderr, "Object description %s\n", s);
213   SPI_freeString (s);
214 }
215
216
217 void
218 check_property_change (AccessibleEvent *event)
219 {
220   AccessibleSelection *selection = Accessible_getSelection (event->source);
221   int n_selections;
222   int i;
223   char *s;
224   if (selection)
225   {
226     n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
227     s = Accessible_getName (event->source);
228     fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
229              event->type, s, n_selections);
230     SPI_freeString (s);
231   /* for now, speak entire selection set */
232     for (i=0; i<n_selections; ++i)
233       {
234         Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
235         g_return_if_fail (obj);
236         s = Accessible_getName (obj);
237         fprintf (stderr, "Child %d, name=%s\n", i, s);
238         SPI_freeString (s);
239         report_focussed_accessible (obj, i==0);
240     }
241   }
242 }
243
244 static void
245 simple_at_exit()
246 {
247   deregisterGlobalEventListenerAll (focus_listener);
248   deregisterGlobalEventListenerAll (property_listener);
249   deregisterGlobalEventListenerAll (button_listener);
250
251   deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT );
252   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED );
253   deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT );
254   AccessibleKeystrokeListener_unref (command_key_listener);
255   AccessibleKeystrokeListener_unref (ordinary_key_listener);
256   
257   SPI_exit ();
258 }
259
260 static boolean
261 is_command_key (AccessibleKeystroke *key)
262 {
263   switch (key->keyID)
264     {
265     case 'Q':
266     case 'q':
267             simple_at_exit(); 
268             return TRUE; /* not reached */
269     case 'M':
270     case 'm':
271             use_magnifier = ! use_magnifier;
272             return TRUE;
273     case 'F':
274     case 'f':
275             use_festival = ! use_festival;
276             return TRUE;
277     default:
278             return FALSE;
279     }
280 }
281
282 static boolean
283 report_command_key_event (AccessibleKeystroke *key)
284 {
285   fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
286           (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
287           ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
288           (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
289           (int) key->keycode);
290   return is_command_key (key);
291 }
292
293
294 static boolean
295 report_ordinary_key_event (AccessibleKeystroke *key)
296 {
297   fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
298            (long) key->keyID,
299            (unsigned int) key->modifiers,
300            (int) key->keycode,
301            (long int) key->timestamp);
302   return FALSE;
303 }
304
305 static int _festival_init ()
306 {
307   int fd;
308   struct sockaddr_in name;
309   int tries = 2;
310
311   name.sin_family = AF_INET;
312   name.sin_port = htons (1314);
313   name.sin_addr.s_addr = htonl(INADDR_ANY);
314   fd = socket (PF_INET, SOCK_STREAM, 0);
315
316   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
317     if (!tries--) {
318       perror ("connect");
319       return -1;
320     }
321   }
322
323   _festival_write ("(audio_mode'async)\n", fd);
324   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
325   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
326   return fd;
327 }
328
329 static void _festival_say (const char *text, const char *voice, boolean shutup)
330 {
331   static int fd = 0;
332   gchar *quoted;
333   gchar *p;
334   gchar prefix[50];
335   static gchar voice_spec[32];
336   
337   if (!fd)
338     {
339       fd = _festival_init ();
340     }
341
342   fprintf (stderr, "saying text: %s\n", text);
343   
344   quoted = g_malloc(64+strlen(text)*2);
345
346   sprintf (prefix, "(SayText \"");
347
348   strncpy(quoted, prefix, 10);
349   p = quoted+strlen(prefix);
350   while (*text) {
351     if ( *text == '\\' || *text == '"' )
352       *p = '\\';
353     *p++ = *text++;
354   }
355   *p++ = '"';
356   *p++ = ')';
357   *p++ = '\n';
358   *p = 0;
359
360   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
361   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
362     {
363       snprintf (voice_spec, 32, "(%s)\n", voice); 
364       _festival_write (voice_spec, fd);
365       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
366       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
367     }
368
369   _festival_write (quoted, fd);
370
371   g_free(quoted);
372 }
373
374 static void _festival_write (const gchar *command_string, int fd)
375 {
376   fprintf(stderr, command_string);
377   if (fd < 0) {
378     perror("socket");
379     return;
380   }
381   write(fd, command_string, strlen(command_string));
382 }
383