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