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_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);
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);
49 static void print_accessible_tree (Accessible *accessible, char *prefix);
52 static SPIBoolean use_magnifier = FALSE;
53 static SPIBoolean use_festival = FALSE;
54 static SPIBoolean festival_chatty = FALSE;
55 static SPIBoolean name_changed = FALSE;
57 static AccessibleEventListener *focus_listener;
58 static AccessibleEventListener *property_listener;
59 static AccessibleEventListener *generic_listener;
60 static AccessibleEventListener *window_listener;
61 static AccessibleEventListener *button_listener;
62 static AccessibleEventListener *text_listener;
63 static AccessibleKeystrokeListener *command_key_listener;
64 static AccessibleKeystrokeListener *ordinary_key_listener;
65 static AccessibleKeySet *command_keyset;
68 main (int argc, char **argv)
75 Accessible *application;
78 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
80 printf ("Usage: simple-at\n");
81 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
85 modules = g_getenv ("GTK_MODULES");
86 if (!modules || modules [0] == '\0')
88 putenv ("GTK_MODULES=");
94 focus_listener = SPI_createAccessibleEventListener (report_focus_event, NULL);
95 property_listener = SPI_createAccessibleEventListener (check_property_change, NULL);
96 generic_listener = SPI_createAccessibleEventListener (report_generic_event, NULL);
97 window_listener = SPI_createAccessibleEventListener (report_window_event, NULL);
98 text_listener = SPI_createAccessibleEventListener (report_text_event, NULL);
99 button_listener = SPI_createAccessibleEventListener (report_button_press, NULL);
100 SPI_registerGlobalEventListener (focus_listener, "focus:");
101 SPI_registerGlobalEventListener (property_listener, "object:property-change");
102 /* :accessible-selection"); */
103 SPI_registerGlobalEventListener (property_listener, "object:property-change:accessible-name");
104 SPI_registerGlobalEventListener (generic_listener, "object:selection-changed");
105 SPI_registerGlobalEventListener (generic_listener, "object:children-changed");
106 SPI_registerGlobalEventListener (generic_listener, "object:visible-data-changed");
107 SPI_registerGlobalEventListener (generic_listener, "object:text-selection-changed");
108 SPI_registerGlobalEventListener (text_listener, "object:text-caret-moved");
109 SPI_registerGlobalEventListener (generic_listener, "object:text-changed");
110 SPI_registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
111 SPI_registerGlobalEventListener (window_listener, "Gtk:GtkWidget:window-state-event");
112 n_desktops = SPI_getDesktopCount ();
114 for (i=0; i<n_desktops; ++i)
116 desktop = SPI_getDesktop (i);
117 s = Accessible_getName (desktop);
118 fprintf (stderr, "desktop %d name: %s\n", i, s);
120 n_apps = Accessible_getChildCount (desktop);
121 for (j=0; j<n_apps; ++j)
123 application = Accessible_getChildAtIndex (desktop, j);
124 s = Accessible_getName (application);
125 fprintf (stderr, "app %d name: %s\n", j, s);
127 print_accessible_tree (application, "*");
130 Accessible_unref (application);
132 Accessible_unref (desktop);
135 /* prepare the keyboard snoopers */
136 command_key_listener = SPI_createAccessibleKeystrokeListener (report_command_key_event, NULL);
137 ordinary_key_listener = SPI_createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
139 command_keyset = SPI_createAccessibleKeySet (11, "qmf23456789", NULL, NULL);
141 /* will listen only to Control-Alt-q KeyPress events */
142 SPI_registerAccessibleKeystrokeListener(command_key_listener,
144 SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
145 (unsigned long) ( SPI_KEY_PRESSED ),
146 SPI_KEYLISTENER_ALL_WINDOWS);
148 /* will listen only to CAPSLOCK key events, both press and release */
149 SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
150 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
151 SPI_KEYMASK_SHIFTLOCK,
152 (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
153 SPI_KEYLISTENER_NOSYNC);
155 get_environment_vars ();
159 putenv ("AT_BRIDGE_SHUTDOWN=1");
165 get_environment_vars (void)
167 if (g_getenv ("FESTIVAL"))
169 fprintf (stderr, "Using festival\n");
171 if (g_getenv ("FESTIVAL_CHATTY"))
173 festival_chatty = TRUE;
176 if (g_getenv ("MAGNIFIER"))
178 fprintf (stderr, "Using magnifier\n");
179 use_magnifier = TRUE;
183 fprintf (stderr, "Not using magnifier\n");
188 fprintf (stderr, "No speech output\n");
194 print_accessible_tree (Accessible *accessible, char *prefix)
202 char child_prefix[100];
206 strncpy (child_prefix, prefix, 98);
207 strcat (child_prefix, "*");
208 parent = Accessible_getParent (accessible);
211 parent_name = Accessible_getName (parent);
212 parent_role = Accessible_getRoleName (parent);
214 Accessible_unref (parent);
215 name = Accessible_getName (accessible);
216 role_name = Accessible_getRoleName (accessible);
217 fprintf (stdout, "%sAccessible [%s] \"%s\"; parent [%s] %s\n",
218 prefix, role_name, name, parent_role, parent_name);
219 SPI_freeString (name);
220 SPI_freeString (role_name);
221 SPI_freeString (parent_name);
222 n_children = Accessible_getChildCount (accessible);
223 for (i = 0; i < n_children; ++i)
225 child = Accessible_getChildAtIndex (accessible, i);
226 print_accessible_tree (child, child_prefix);
227 Accessible_unref (child);
233 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
237 long x, y, width, height;
238 /* hack for GUADEC demo, to make sure name changes are spoken */
239 shutup_previous_speech = (shutup_previous_speech && !name_changed);
245 s = Accessible_getRoleName (obj);
246 _festival_say (s, "voice_don_diphone", shutup_previous_speech);
249 fprintf (stderr, "getting Name\n");
250 s = Accessible_getName (obj);
251 _festival_say (s, "voice_kal_diphone",
252 shutup_previous_speech || festival_chatty);
256 if (Accessible_isComponent (obj))
258 AccessibleComponent *component = Accessible_getComponent (obj);
259 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
260 SPI_COORD_TYPE_SCREEN);
261 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
262 x, y, x+width, y+height);
263 if (Accessible_isText (obj))
265 long x0, y0, xN, yN, w0, h0, wN, hN, nchars;
266 AccessibleText *text = Accessible_getText (obj);
267 nchars = AccessibleText_getCharacterCount (text);
270 AccessibleText_getCharacterExtents (text, 0, &x0, &y0, &w0, &h0,
271 SPI_COORD_TYPE_SCREEN);
272 AccessibleText_getCharacterExtents (text, nchars-1, &xN, &yN, &wN, &hN,
273 SPI_COORD_TYPE_SCREEN);
275 width = MAX (x0 + w0, xN + wN) - x;
276 fprintf (stderr, "Text bounding box: (%ld, %ld) ; (%ld, %ld)\n",
277 x, y, x+width, y+height);
281 magnifier_set_roi ((short) 0, x, y, width, height);
285 if (Accessible_isValue (obj))
287 AccessibleValue *value = Accessible_getValue (obj);
288 fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
289 AccessibleValue_getCurrentValue (value),
290 AccessibleValue_getMinimumValue (value),
291 AccessibleValue_getMaximumValue (value));
293 /* if this is a text object, speak the first sentence. */
295 if (Accessible_isText(obj))
298 AccessibleText *text_interface;
299 long start_offset, end_offset;
300 char *first_sentence = "empty";
301 text_interface = Accessible_getText (obj);
302 first_sentence = AccessibleText_getTextAtOffset (
303 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
304 if (first_sentence && use_festival)
306 _festival_say(first_sentence, "voice_don_diphone", FALSE);
307 SPI_freeString (first_sentence);
309 len = AccessibleText_getCharacterCount (text_interface);
310 s = AccessibleText_getText (text_interface, 0, len);
311 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
316 report_focus_event (const AccessibleEvent *event, void *user_data)
320 g_return_if_fail (event->source != NULL);
321 s = Accessible_getName (event->source);
324 fprintf (stderr, "%s event from %s\n", event->type, s);
326 report_focussed_accessible (event->source, TRUE);
328 Accessible_getParent (event->source);
329 name_changed = FALSE;
333 report_generic_event (const AccessibleEvent *event, void *user_data)
335 fprintf (stderr, "%s event received\n", event->type);
339 report_window_event (const AccessibleEvent *event, void *user_data)
341 fprintf (stderr, "%s event received\n", event->type);
345 report_text_event (const AccessibleEvent *event, void *user_data)
347 AccessibleText *text = Accessible_getText (event->source);
348 fprintf (stderr, "%s event received\n", event->type);
349 if (use_magnifier && strcmp (event->type, "object:text-changed"))
351 long offset = AccessibleText_getCaretOffset (text);
353 fprintf (stderr, "offset %d\n", (int) offset);
354 AccessibleText_getCharacterExtents (text, offset, &x, &y, &w, &h,
355 SPI_COORD_TYPE_SCREEN);
356 fprintf (stderr, "new roi %d %d %d %d\n", (int) x, (int) y, (int) w, (int) h);
357 magnifier_set_roi ((short) 0, x, y, w, h);
359 if (!strcmp (event->type, "object:text-changed"))
362 char *new_text = AccessibleText_getTextAtOffset (text, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start, &end);
363 _festival_say (new_text, "voice_kal_diphone", FALSE);
364 SPI_freeString (new_text);
369 report_button_press (const AccessibleEvent *event, void *user_data)
373 g_return_if_fail (event->source != NULL);
375 s = Accessible_getName (event->source);
377 fprintf (stderr, "%s event from %s\n", event->type, s);
379 s = Accessible_getDescription (event->source);
380 fprintf (stderr, "Object description %s\n", s);
385 check_property_change (const AccessibleEvent *event, void *user_data)
387 AccessibleSelection *selection = Accessible_getSelection (event->source);
391 fprintf (stderr, "property change event!\n");
394 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
395 s = Accessible_getName (event->source);
396 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
397 event->type, s, n_selections);
399 /* for now, speak entire selection set */
400 for (i=0; i<n_selections; ++i)
402 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
403 g_return_if_fail (obj);
404 s = Accessible_getName (obj);
405 fprintf (stderr, "Child %d, name=%s\n", i, s);
407 report_focussed_accessible (obj, i==0);
410 else if (!strcmp (event->type, "object:property-change:accessible-name"))
413 report_focussed_accessible (event->source, TRUE);
417 fprintf (stderr, "Property change %s received\n", event->type);
424 SPI_deregisterGlobalEventListenerAll (focus_listener);
425 AccessibleEventListener_unref (focus_listener);
427 SPI_deregisterGlobalEventListenerAll (property_listener);
428 AccessibleEventListener_unref (property_listener);
430 SPI_deregisterGlobalEventListenerAll (generic_listener);
431 AccessibleEventListener_unref (generic_listener);
433 SPI_deregisterGlobalEventListenerAll (text_listener);
434 AccessibleEventListener_unref (text_listener);
436 SPI_deregisterGlobalEventListenerAll (button_listener);
437 AccessibleEventListener_unref (button_listener);
439 SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL);
440 AccessibleKeystrokeListener_unref (command_key_listener);
441 SPI_freeAccessibleKeySet (command_keyset);
443 SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFTLOCK);
444 AccessibleKeystrokeListener_unref (ordinary_key_listener);
450 is_command_key (const AccessibleKeystroke *key)
457 return TRUE; /* not reached */
460 use_magnifier = ! use_magnifier;
461 fprintf (stderr, "%ssing magnifier\n", use_magnifier ? "U" : "Not u");
465 use_festival = ! use_festival;
466 fprintf (stderr, "%speech output\n", use_festival ? "S" : "No s");
474 report_command_key_event (const AccessibleKeystroke *key, void *user_data)
476 fprintf (stderr, "Command KeyEvent %s%c (keycode %d); string=%s; time=%lx\n",
477 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
478 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
479 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
482 (long int) key->timestamp);
483 return is_command_key (key);
488 report_ordinary_key_event (const AccessibleKeystroke *key, void *user_data)
490 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\tstring=\'%s\'\n\ttime %lx\n",
492 (unsigned int) key->modifiers,
495 (long int) key->timestamp);
503 struct sockaddr_in name;
506 name.sin_family = AF_INET;
507 name.sin_port = htons (1314);
508 name.sin_addr.s_addr = htonl(INADDR_ANY);
509 fd = socket (PF_INET, SOCK_STREAM, 0);
511 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
518 _festival_write ("(audio_mode'async)\n", fd);
519 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
520 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
525 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
531 static gchar voice_spec[32];
535 fd = _festival_init ();
538 fprintf (stderr, "saying text: %s\n", text);
540 quoted = g_malloc(64+strlen(text)*2);
542 sprintf (prefix, "(SayText \"");
544 strncpy(quoted, prefix, 10);
545 p = quoted+strlen(prefix);
547 if ( *text == '\\' || *text == '"' )
556 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
557 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
559 snprintf (voice_spec, 32, "(%s)\n", voice);
560 _festival_write (voice_spec, fd);
561 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
562 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
565 _festival_write (quoted, fd);
571 _festival_write (const gchar *command_string, int fd)
573 fprintf(stderr, command_string);
578 write(fd, command_string, strlen(command_string));