2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001 Sun Microsystems Inc.
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.
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.
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.
26 #include <sys/socket.h>
27 #include <netinet/in.h>
29 #include "../util/mag_client.h"
30 #include "../cspi/spi-private.h" /* A hack for now */
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_text_event (const AccessibleEvent *event, void *user_data);
37 static void report_button_press (const AccessibleEvent *event, void *user_data);
38 static void check_property_change (const AccessibleEvent *event, void *user_data);
39 static SPIBoolean report_command_key_event (const AccessibleKeystroke *stroke, void *user_data);
40 static SPIBoolean report_ordinary_key_event (const AccessibleKeystroke *stroke, void *user_data);
41 static void get_environment_vars (void);
43 static int _festival_init ();
44 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
45 static void _festival_write (const char *buff, int fd);
48 static void print_accessible_tree (Accessible *accessible, char *prefix);
51 static SPIBoolean use_magnifier = FALSE;
52 static SPIBoolean use_festival = FALSE;
53 static SPIBoolean festival_chatty = FALSE;
55 static AccessibleEventListener *focus_listener;
56 static AccessibleEventListener *property_listener;
57 static AccessibleEventListener *generic_listener;
58 static AccessibleEventListener *button_listener;
59 static AccessibleEventListener *text_listener;
60 static AccessibleKeystrokeListener *command_key_listener;
61 static AccessibleKeystrokeListener *ordinary_key_listener;
62 static AccessibleKeySet *command_keyset;
65 main (int argc, char **argv)
72 Accessible *application;
75 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
77 printf ("Usage: simple-at\n");
78 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
82 modules = g_getenv ("GTK_MODULES");
83 if (!modules || modules [0] == '\0')
85 putenv ("GTK_MODULES=");
91 focus_listener = SPI_createAccessibleEventListener (report_focus_event, NULL);
92 property_listener = SPI_createAccessibleEventListener (check_property_change, NULL);
93 generic_listener = SPI_createAccessibleEventListener (report_generic_event, NULL);
94 text_listener = SPI_createAccessibleEventListener (report_text_event, NULL);
95 button_listener = SPI_createAccessibleEventListener (report_button_press, NULL);
96 SPI_registerGlobalEventListener (focus_listener, "focus:");
97 SPI_registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
98 SPI_registerGlobalEventListener (generic_listener, "object:selection-changed");
99 SPI_registerGlobalEventListener (generic_listener, "object:children-changed");
100 SPI_registerGlobalEventListener (generic_listener, "object:visible-data-changed");
101 SPI_registerGlobalEventListener (generic_listener, "object:text-selection-changed");
102 SPI_registerGlobalEventListener (text_listener, "object:text-caret-moved");
103 SPI_registerGlobalEventListener (generic_listener, "object:text-changed");
104 SPI_registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
105 n_desktops = SPI_getDesktopCount ();
107 for (i=0; i<n_desktops; ++i)
109 desktop = SPI_getDesktop (i);
110 s = Accessible_getName (desktop);
111 fprintf (stderr, "desktop %d name: %s\n", i, s);
113 n_apps = Accessible_getChildCount (desktop);
114 for (j=0; j<n_apps; ++j)
116 application = Accessible_getChildAtIndex (desktop, j);
117 s = Accessible_getName (application);
118 fprintf (stderr, "app %d name: %s\n", j, s);
120 print_accessible_tree (application, "*");
123 Accessible_unref (application);
125 Accessible_unref (desktop);
128 /* prepare the keyboard snoopers */
129 command_key_listener = SPI_createAccessibleKeystrokeListener (report_command_key_event, NULL);
130 ordinary_key_listener = SPI_createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
132 command_keyset = SPI_createAccessibleKeySet (11, "qmf23456789", NULL, NULL);
134 /* will listen only to Control-Alt-q KeyPress events */
135 SPI_registerAccessibleKeystrokeListener(command_key_listener,
137 SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
138 (unsigned long) ( SPI_KEY_PRESSED ),
139 SPI_KEYLISTENER_ALL_WINDOWS);
141 /* will listen only to CAPSLOCK key events, both press and release */
142 SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
143 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
144 SPI_KEYMASK_SHIFTLOCK,
145 (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
146 SPI_KEYLISTENER_NOSYNC);
148 get_environment_vars ();
152 putenv ("AT_BRIDGE_SHUTDOWN=1");
158 get_environment_vars (void)
160 if (g_getenv ("FESTIVAL"))
162 fprintf (stderr, "Using festival\n");
164 if (g_getenv ("FESTIVAL_CHATTY"))
166 festival_chatty = TRUE;
169 if (g_getenv ("MAGNIFIER"))
171 fprintf (stderr, "Using magnifier\n");
172 use_magnifier = TRUE;
176 fprintf (stderr, "Not using magnifier\n");
181 fprintf (stderr, "No speech output\n");
187 print_accessible_tree (Accessible *accessible, char *prefix)
195 char child_prefix[100];
199 strncpy (child_prefix, prefix, 98);
200 strcat (child_prefix, "*");
201 parent = Accessible_getParent (accessible);
204 parent_name = Accessible_getName (parent);
205 parent_role = Accessible_getRoleName (parent);
207 Accessible_unref (parent);
208 name = Accessible_getName (accessible);
209 role_name = Accessible_getRoleName (accessible);
210 fprintf (stdout, "%sAccessible [%s] \"%s\"; parent [%s] %s\n",
211 prefix, role_name, name, parent_role, parent_name);
212 SPI_freeString (name);
213 SPI_freeString (role_name);
214 SPI_freeString (parent_name);
215 n_children = Accessible_getChildCount (accessible);
216 for (i = 0; i < n_children; ++i)
218 child = Accessible_getChildAtIndex (accessible, i);
219 print_accessible_tree (child, child_prefix);
220 Accessible_unref (child);
226 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
230 long x, y, width, height;
236 s = Accessible_getRoleName (obj);
237 _festival_say (s, "voice_don_diphone", shutup_previous_speech);
240 fprintf (stderr, "getting Name\n");
241 s = Accessible_getName (obj);
242 _festival_say (s, "voice_kal_diphone",
243 shutup_previous_speech || festival_chatty);
247 if (Accessible_isComponent (obj))
249 AccessibleComponent *component = Accessible_getComponent (obj);
250 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
251 SPI_COORD_TYPE_SCREEN);
252 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
253 x, y, x+width, y+height);
254 if (Accessible_isText (obj))
256 long x0, y0, xN, yN, w0, h0, wN, hN, nchars;
257 AccessibleText *text = Accessible_getText (obj);
258 nchars = AccessibleText_getCharacterCount (text);
261 AccessibleText_getCharacterExtents (text, 0, &x0, &y0, &w0, &h0,
262 SPI_COORD_TYPE_SCREEN);
263 AccessibleText_getCharacterExtents (text, nchars-1, &xN, &yN, &wN, &hN,
264 SPI_COORD_TYPE_SCREEN);
266 width = MAX (x0 + w0, xN + wN) - x;
267 fprintf (stderr, "Text bounding box: (%ld, %ld) ; (%ld, %ld)\n",
268 x, y, x+width, y+height);
272 magnifier_set_roi ((short) 0, x, y, width, height);
276 if (Accessible_isValue (obj))
278 AccessibleValue *value = Accessible_getValue (obj);
279 fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
280 AccessibleValue_getCurrentValue (value),
281 AccessibleValue_getMinimumValue (value),
282 AccessibleValue_getMaximumValue (value));
284 /* if this is a text object, speak the first sentence. */
286 if (Accessible_isText(obj))
289 AccessibleText *text_interface;
290 long start_offset, end_offset;
291 char *first_sentence = "empty";
292 text_interface = Accessible_getText (obj);
293 first_sentence = AccessibleText_getTextAtOffset (
294 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
295 if (first_sentence && use_festival)
297 _festival_say(first_sentence, "voice_don_diphone", FALSE);
298 SPI_freeString (first_sentence);
300 len = AccessibleText_getCharacterCount (text_interface);
301 s = AccessibleText_getText (text_interface, 0, len);
302 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
307 report_focus_event (const AccessibleEvent *event, void *user_data)
311 g_return_if_fail (event->source != NULL);
313 s = Accessible_getName (event->source);
316 fprintf (stderr, "%s event from %s\n", event->type, s);
318 report_focussed_accessible (event->source, TRUE);
320 Accessible_getParent (event->source);
324 report_generic_event (const AccessibleEvent *event, void *user_data)
326 fprintf (stderr, "%s event received\n", event->type);
330 report_text_event (const AccessibleEvent *event, void *user_data)
332 fprintf (stderr, "%s event received\n", event->type);
335 AccessibleText *text = Accessible_getText (event->source);
336 long offset = AccessibleText_getCaretOffset (text);
338 fprintf (stderr, "offset %d\n", (int) offset);
339 AccessibleText_getCharacterExtents (text, offset, &x, &y, &w, &h,
340 SPI_COORD_TYPE_SCREEN);
341 fprintf (stderr, "new roi %d %d %d %d\n", (int) x, (int) y, (int) w, (int) h);
342 magnifier_set_roi ((short) 0, x, y, w, h);
347 report_button_press (const AccessibleEvent *event, void *user_data)
351 g_return_if_fail (event->source != NULL);
353 s = Accessible_getName (event->source);
355 fprintf (stderr, "%s event from %s\n", event->type, s);
357 s = Accessible_getDescription (event->source);
358 fprintf (stderr, "Object description %s\n", s);
363 check_property_change (const AccessibleEvent *event, void *user_data)
365 AccessibleSelection *selection = Accessible_getSelection (event->source);
371 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
372 s = Accessible_getName (event->source);
373 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
374 event->type, s, n_selections);
376 /* for now, speak entire selection set */
377 for (i=0; i<n_selections; ++i)
379 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
380 g_return_if_fail (obj);
381 s = Accessible_getName (obj);
382 fprintf (stderr, "Child %d, name=%s\n", i, s);
384 report_focussed_accessible (obj, i==0);
392 SPI_deregisterGlobalEventListenerAll (focus_listener);
393 AccessibleEventListener_unref (focus_listener);
395 SPI_deregisterGlobalEventListenerAll (property_listener);
396 AccessibleEventListener_unref (property_listener);
398 SPI_deregisterGlobalEventListenerAll (generic_listener);
399 AccessibleEventListener_unref (generic_listener);
401 SPI_deregisterGlobalEventListenerAll (text_listener);
402 AccessibleEventListener_unref (text_listener);
404 SPI_deregisterGlobalEventListenerAll (button_listener);
405 AccessibleEventListener_unref (button_listener);
407 SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
408 AccessibleKeystrokeListener_unref (command_key_listener);
409 SPI_freeAccessibleKeySet (command_keyset);
411 SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFTLOCK);
412 AccessibleKeystrokeListener_unref (ordinary_key_listener);
418 is_command_key (const AccessibleKeystroke *key)
425 return TRUE; /* not reached */
428 use_magnifier = ! use_magnifier;
429 fprintf (stderr, "%ssing magnifier\n", use_magnifier ? "U" : "Not u");
433 use_festival = ! use_festival;
434 fprintf (stderr, "%speech output\n", use_festival ? "S" : "No s");
442 report_command_key_event (const AccessibleKeystroke *key, void *user_data)
444 fprintf (stderr, "Command KeyEvent %s%c (keycode %d); string=%s; time=%lx\n",
445 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
446 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
447 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
450 (long int) key->timestamp);
451 return is_command_key (key);
456 report_ordinary_key_event (const AccessibleKeystroke *key, void *user_data)
458 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\tstring=\'%s\'\n\ttime %lx\n",
460 (unsigned int) key->modifiers,
463 (long int) key->timestamp);
471 struct sockaddr_in name;
474 name.sin_family = AF_INET;
475 name.sin_port = htons (1314);
476 name.sin_addr.s_addr = htonl(INADDR_ANY);
477 fd = socket (PF_INET, SOCK_STREAM, 0);
479 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
486 _festival_write ("(audio_mode'async)\n", fd);
487 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
488 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
493 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
499 static gchar voice_spec[32];
503 fd = _festival_init ();
506 fprintf (stderr, "saying text: %s\n", text);
508 quoted = g_malloc(64+strlen(text)*2);
510 sprintf (prefix, "(SayText \"");
512 strncpy(quoted, prefix, 10);
513 p = quoted+strlen(prefix);
515 if ( *text == '\\' || *text == '"' )
524 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
525 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
527 snprintf (voice_spec, 32, "(%s)\n", voice);
528 _festival_write (voice_spec, fd);
529 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
530 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
533 _festival_write (quoted, fd);
539 _festival_write (const gchar *command_string, int fd)
541 fprintf(stderr, command_string);
546 write(fd, command_string, strlen(command_string));