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>
28 #include "../util/mag_client.h"
29 #include "../cspi/spi-private.h" /* A hack for now */
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);
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);
43 static SPIBoolean use_magnifier = FALSE;
44 static SPIBoolean use_festival = FALSE;
45 static SPIBoolean festival_chatty = FALSE;
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;
56 main (int argc, char **argv)
63 Accessible *application;
66 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
68 printf ("Usage: simple-at\n");
69 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
73 modules = g_getenv ("GTK_MODULES");
74 if (!modules || modules [0] == '\0')
76 putenv ("GTK_MODULES=gail:at-bridge");
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 ();
97 for (i=0; i<n_desktops; ++i)
99 desktop = SPI_getDesktop (i);
100 s = Accessible_getName (desktop);
101 fprintf (stderr, "desktop %d name: %s\n", i, s);
103 n_apps = Accessible_getChildCount (desktop);
104 for (j=0; j<n_apps; ++j)
106 application = Accessible_getChildAtIndex (desktop, j);
107 s = Accessible_getName (application);
108 fprintf (stderr, "app %d name: %s\n", j, s);
110 Accessible_unref (application);
112 Accessible_unref (desktop);
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);
119 /* will listen only to Control-Alt-key combinations, and only to KeyPress events */
120 SPI_registerAccessibleKeystrokeListener(command_key_listener,
121 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
122 SPI_KEYMASK_ALT | SPI_KEYMASK_CONTROL,
123 (unsigned long) ( SPI_KEY_PRESSED ),
124 SPI_KEYLISTENER_ALL_WINDOWS);
126 /* will listen only to shifted key events, both press and release */
127 SPI_registerAccessibleKeystrokeListener(ordinary_key_listener,
128 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
130 (unsigned long) ( SPI_KEY_PRESSED | SPI_KEY_RELEASED ),
131 SPI_KEYLISTENER_NOSYNC);
133 spacebar_key_set = SPI_createAccessibleKeySet (1, " ", NULL, NULL);
135 /* will listen only to shift-spacebar events, on release, globally */
136 SPI_registerAccessibleKeystrokeListener(command_key_listener,
139 (unsigned long) ( SPI_KEY_RELEASED ),
140 SPI_KEYLISTENER_ALL_WINDOWS);
143 get_environment_vars ();
147 putenv ("AT_BRIDGE_SHUTDOWN=1");
153 get_environment_vars (void)
155 if (g_getenv ("FESTIVAL"))
157 fprintf (stderr, "Using festival\n");
159 if (g_getenv ("FESTIVAL_CHATTY"))
161 festival_chatty = TRUE;
164 if (g_getenv ("MAGNIFIER"))
166 fprintf (stderr, "Using magnifier\n");
167 use_magnifier = TRUE;
171 fprintf (stderr, "Not using magnifier\n");
176 fprintf (stderr, "No speech output\n");
181 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
190 s = Accessible_getRoleName (obj);
191 _festival_say (s, "voice_don_diphone", shutup_previous_speech);
194 fprintf (stderr, "getting Name\n");
195 s = Accessible_getName (obj);
196 _festival_say (s, "voice_kal_diphone",
197 shutup_previous_speech || festival_chatty);
201 if (Accessible_isComponent (obj))
203 long x, y, width, height;
204 AccessibleComponent *component = Accessible_getComponent (obj);
205 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
206 SPI_COORD_TYPE_SCREEN);
207 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
208 x, y, x+width, y+height);
210 magnifier_set_roi ((short) 0, x, y, width, height);
214 if (Accessible_isValue (obj))
216 AccessibleValue *value = Accessible_getValue (obj);
217 fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
218 AccessibleValue_getCurrentValue (value),
219 AccessibleValue_getMinimumValue (value),
220 AccessibleValue_getMaximumValue (value));
222 /* if this is a text object, speak the first sentence. */
224 if (Accessible_isText(obj))
227 AccessibleText *text_interface;
228 long start_offset, end_offset;
229 char *first_sentence = "empty";
230 text_interface = Accessible_getText (obj);
231 first_sentence = AccessibleText_getTextAtOffset (
232 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
235 _festival_say(first_sentence, "voice_don_diphone", FALSE);
236 SPI_freeString (first_sentence);
238 len = AccessibleText_getCharacterCount (text_interface);
239 s = AccessibleText_getText (text_interface, 0, len);
240 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
245 report_focus_event (AccessibleEvent *event, void *user_data)
249 g_return_if_fail (event->source != NULL);
251 s = Accessible_getName (event->source);
254 fprintf (stderr, "%s event from %s\n", event->type, s);
256 report_focussed_accessible (event->source, TRUE);
258 Accessible_getParent (event->source);
262 report_generic_event (AccessibleEvent *event, void *user_data)
264 fprintf (stderr, "%s event received\n", event->type);
268 report_button_press (AccessibleEvent *event, void *user_data)
272 g_return_if_fail (event->source != NULL);
274 s = Accessible_getName (event->source);
276 fprintf (stderr, "%s event from %s\n", event->type, s);
278 s = Accessible_getDescription (event->source);
279 fprintf (stderr, "Object description %s\n", s);
284 check_property_change (AccessibleEvent *event, void *user_data)
286 AccessibleSelection *selection = Accessible_getSelection (event->source);
292 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
293 s = Accessible_getName (event->source);
294 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
295 event->type, s, n_selections);
297 /* for now, speak entire selection set */
298 for (i=0; i<n_selections; ++i)
300 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
301 g_return_if_fail (obj);
302 s = Accessible_getName (obj);
303 fprintf (stderr, "Child %d, name=%s\n", i, s);
305 report_focussed_accessible (obj, i==0);
313 SPI_deregisterGlobalEventListenerAll (focus_listener);
314 AccessibleEventListener_unref (focus_listener);
316 SPI_deregisterGlobalEventListenerAll (property_listener);
317 AccessibleEventListener_unref (property_listener);
319 SPI_deregisterGlobalEventListenerAll (generic_listener);
320 AccessibleEventListener_unref (generic_listener);
322 SPI_deregisterGlobalEventListenerAll (button_listener);
323 AccessibleEventListener_unref (button_listener);
325 SPI_deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
326 AccessibleKeystrokeListener_unref (command_key_listener);
328 SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
329 SPI_deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
330 AccessibleKeystrokeListener_unref (ordinary_key_listener);
332 SPI_freeAccessibleKeySet (spacebar_key_set);
338 is_command_key (AccessibleKeystroke *key)
345 return TRUE; /* not reached */
348 use_magnifier = ! use_magnifier;
349 fprintf (stderr, "%ssing magnifier\n", use_magnifier ? "U" : "Not u");
353 use_festival = ! use_festival;
354 fprintf (stderr, "%speech output\n", use_festival ? "S" : "No s");
362 report_command_key_event (AccessibleKeystroke *key, void *user_data)
364 fprintf (stderr, "Command KeyEvent %s%c (keycode %d); string=%s; time=%lx\n",
365 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
366 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
367 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
370 (long int) key->timestamp);
371 return is_command_key (key);
376 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
378 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\tstring=\'%s\'\n\ttime %lx\n",
380 (unsigned int) key->modifiers,
383 (long int) key->timestamp);
391 struct sockaddr_in name;
394 name.sin_family = AF_INET;
395 name.sin_port = htons (1314);
396 name.sin_addr.s_addr = htonl(INADDR_ANY);
397 fd = socket (PF_INET, SOCK_STREAM, 0);
399 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
406 _festival_write ("(audio_mode'async)\n", fd);
407 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
408 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
413 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
419 static gchar voice_spec[32];
423 fd = _festival_init ();
426 fprintf (stderr, "saying text: %s\n", text);
428 quoted = g_malloc(64+strlen(text)*2);
430 sprintf (prefix, "(SayText \"");
432 strncpy(quoted, prefix, 10);
433 p = quoted+strlen(prefix);
435 if ( *text == '\\' || *text == '"' )
444 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
445 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
447 snprintf (voice_spec, 32, "(%s)\n", voice);
448 _festival_write (voice_spec, fd);
449 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
450 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
453 _festival_write (quoted, fd);
459 _festival_write (const gchar *command_string, int fd)
461 fprintf(stderr, command_string);
466 write(fd, command_string, strlen(command_string));