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>
29 #include "../util/mag_client.h"
30 #include "../cspi/spi-private.h" /* A hack for now */
32 static void report_focus_event (AccessibleEvent *event, void *user_data);
33 static void report_generic_event (AccessibleEvent *event, void *user_data);
34 static void report_button_press (AccessibleEvent *event, void *user_data);
35 static void check_property_change (AccessibleEvent *event, void *user_data);
36 static SPIBoolean report_command_key_event (AccessibleKeystroke *stroke, void *user_data);
37 static SPIBoolean report_ordinary_key_event (AccessibleKeystroke *stroke, void *user_data);
38 static void get_environment_vars (void);
40 static int _festival_init ();
41 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
42 static void _festival_write (const char *buff, int fd);
44 static SPIBoolean use_magnifier = FALSE;
45 static SPIBoolean use_festival = FALSE;
46 static SPIBoolean festival_chatty = FALSE;
48 static AccessibleEventListener *focus_listener;
49 static AccessibleEventListener *property_listener;
50 static AccessibleEventListener *generic_listener;
51 static AccessibleEventListener *button_listener;
52 static AccessibleKeystrokeListener *command_key_listener;
53 static AccessibleKeystrokeListener *ordinary_key_listener;
56 main (int argc, char **argv)
62 Accessible *application;
65 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
67 printf ("Usage: simple-at\n");
68 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
74 focus_listener = createAccessibleEventListener (report_focus_event, NULL);
75 property_listener = createAccessibleEventListener (check_property_change, NULL);
76 generic_listener = createAccessibleEventListener (report_generic_event, NULL);
77 button_listener = createAccessibleEventListener (report_button_press, NULL);
78 registerGlobalEventListener (focus_listener, "focus:");
79 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
80 registerGlobalEventListener (generic_listener, "object:selection-changed");
81 registerGlobalEventListener (generic_listener, "object:children-changed");
82 registerGlobalEventListener (generic_listener, "object:visible-data-changed");
83 registerGlobalEventListener (generic_listener, "object:text-selection-changed");
84 registerGlobalEventListener (generic_listener, "object:text-caret-moved");
85 registerGlobalEventListener (generic_listener, "object:text-changed");
86 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
87 n_desktops = getDesktopCount ();
89 for (i=0; i<n_desktops; ++i)
91 desktop = getDesktop (i);
92 s = Accessible_getName (desktop);
93 fprintf (stderr, "desktop %d name: %s\n", i, s);
95 n_apps = Accessible_getChildCount (desktop);
96 for (j=0; j<n_apps; ++j)
98 application = Accessible_getChildAtIndex (desktop, j);
99 s = Accessible_getName (application);
100 fprintf (stderr, "app %d name: %s\n", j, s);
102 Accessible_unref (application);
104 Accessible_unref (desktop);
107 /* prepare the keyboard snoopers */
108 command_key_listener = createAccessibleKeystrokeListener (report_command_key_event, NULL);
109 ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
111 /* will listen only to Alt-key combinations, and only to KeyPress events */
112 registerAccessibleKeystrokeListener(command_key_listener,
113 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
115 (unsigned long) ( KeyPress ),
116 SPI_KEYLISTENER_ALL_WINDOWS);
118 /* will listen only to unshifted key events, both press and release */
119 registerAccessibleKeystrokeListener(ordinary_key_listener,
120 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
121 SPI_KEYMASK_UNMODIFIED,
122 (unsigned long) ( KeyPress | KeyRelease),
123 SPI_KEYLISTENER_NOSYNC);
125 /* will listen only to shifted key events, both press and release */
126 registerAccessibleKeystrokeListener(ordinary_key_listener,
127 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
129 (unsigned long) ( KeyPress | KeyRelease),
130 SPI_KEYLISTENER_NOSYNC);
132 get_environment_vars ();
136 setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);
142 get_environment_vars (void)
144 if (g_getenv ("FESTIVAL"))
146 fprintf (stderr, "Using festival\n");
148 if (g_getenv ("FESTIVAL_CHATTY"))
150 festival_chatty = TRUE;
153 if (g_getenv ("MAGNIFIER"))
155 fprintf (stderr, "Using magnifier\n");
156 use_magnifier = TRUE;
161 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
170 _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
172 fprintf (stderr, "getting Name\n");
173 s = Accessible_getName (obj);
174 _festival_say (s, "voice_kal_diphone",
175 shutup_previous_speech || festival_chatty);
179 if (Accessible_isComponent (obj))
181 long x, y, width, height;
182 AccessibleComponent *component = Accessible_getComponent (obj);
183 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
184 SPI_COORD_TYPE_SCREEN);
185 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
186 x, y, x+width, y+height);
188 magnifier_set_roi ((short) 0, x, y, width, height);
192 if (Accessible_isValue (obj))
194 AccessibleValue *value = Accessible_getValue (obj);
195 fprintf (stderr, "Current value = %f, min = %f; max = %f\n",
196 AccessibleValue_getCurrentValue (value),
197 AccessibleValue_getMinimumValue (value),
198 AccessibleValue_getMaximumValue (value));
200 /* if this is a text object, speak the first sentence. */
202 if (Accessible_isText(obj))
205 AccessibleText *text_interface;
206 long start_offset, end_offset;
207 char *first_sentence = "empty";
208 text_interface = Accessible_getText (obj);
209 first_sentence = AccessibleText_getTextAtOffset (
210 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
213 _festival_say(first_sentence, "voice_don_diphone", FALSE);
214 SPI_freeString (first_sentence);
216 len = AccessibleText_getCharacterCount (text_interface);
217 s = AccessibleText_getText (text_interface, 0, len);
218 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
223 report_focus_event (AccessibleEvent *event, void *user_data)
227 g_return_if_fail (event->source != NULL);
229 s = Accessible_getName (event->source);
230 if (cspi_warn_ev (cspi_ev (), "Report focus event"))
232 fprintf (stderr, "%s event from %s\n", event->type, s);
234 report_focussed_accessible (event->source, TRUE);
236 Accessible_getParent (event->source);
240 report_generic_event (AccessibleEvent *event, void *user_data)
242 fprintf (stderr, "%s event received\n", event->type);
246 report_button_press (AccessibleEvent *event, void *user_data)
250 g_return_if_fail (event->source != NULL);
252 s = Accessible_getName (event->source);
254 fprintf (stderr, "%s event from %s\n", event->type, s);
256 s = Accessible_getDescription (event->source);
257 fprintf (stderr, "Object description %s\n", s);
262 check_property_change (AccessibleEvent *event, void *user_data)
264 AccessibleSelection *selection = Accessible_getSelection (event->source);
270 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
271 s = Accessible_getName (event->source);
272 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
273 event->type, s, n_selections);
275 /* for now, speak entire selection set */
276 for (i=0; i<n_selections; ++i)
278 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
279 g_return_if_fail (obj);
280 s = Accessible_getName (obj);
281 fprintf (stderr, "Child %d, name=%s\n", i, s);
283 report_focussed_accessible (obj, i==0);
291 deregisterGlobalEventListenerAll (focus_listener);
292 AccessibleEventListener_unref (focus_listener);
294 deregisterGlobalEventListenerAll (property_listener);
295 AccessibleEventListener_unref (property_listener);
297 deregisterGlobalEventListenerAll (generic_listener);
298 AccessibleEventListener_unref (generic_listener);
300 deregisterGlobalEventListenerAll (button_listener);
301 AccessibleEventListener_unref (button_listener);
303 deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
304 AccessibleKeystrokeListener_unref (command_key_listener);
306 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
307 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
308 AccessibleKeystrokeListener_unref (ordinary_key_listener);
314 is_command_key (AccessibleKeystroke *key)
321 return TRUE; /* not reached */
324 use_magnifier = ! use_magnifier;
328 use_festival = ! use_festival;
336 report_command_key_event (AccessibleKeystroke *key, void *user_data)
338 fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
339 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
340 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
341 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
343 return is_command_key (key);
348 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
350 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
352 (unsigned int) key->modifiers,
354 (long int) key->timestamp);
362 struct sockaddr_in name;
365 name.sin_family = AF_INET;
366 name.sin_port = htons (1314);
367 name.sin_addr.s_addr = htonl(INADDR_ANY);
368 fd = socket (PF_INET, SOCK_STREAM, 0);
370 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
377 _festival_write ("(audio_mode'async)\n", fd);
378 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
379 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
384 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
390 static gchar voice_spec[32];
394 fd = _festival_init ();
397 fprintf (stderr, "saying text: %s\n", text);
399 quoted = g_malloc(64+strlen(text)*2);
401 sprintf (prefix, "(SayText \"");
403 strncpy(quoted, prefix, 10);
404 p = quoted+strlen(prefix);
406 if ( *text == '\\' || *text == '"' )
415 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
416 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
418 snprintf (voice_spec, 32, "(%s)\n", voice);
419 _festival_write (voice_spec, fd);
420 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
421 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
424 _festival_write (quoted, fd);
430 _festival_write (const gchar *command_string, int fd)
432 fprintf(stderr, command_string);
437 write(fd, command_string, strlen(command_string));