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_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 *button_listener;
50 static AccessibleKeystrokeListener *command_key_listener;
51 static AccessibleKeystrokeListener *ordinary_key_listener;
54 main (int argc, char **argv)
60 Accessible *application;
63 if ((argc > 1) && (!strncmp (argv[1], "-h", 2)))
65 printf ("Usage: simple-at\n");
66 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
72 focus_listener = createAccessibleEventListener (report_focus_event, NULL);
73 property_listener = createAccessibleEventListener (check_property_change, NULL);
74 button_listener = createAccessibleEventListener (report_button_press, NULL);
75 registerGlobalEventListener (focus_listener, "focus:");
76 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
77 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
78 n_desktops = getDesktopCount ();
80 for (i=0; i<n_desktops; ++i)
82 desktop = getDesktop (i);
83 s = Accessible_getName (desktop);
84 fprintf (stderr, "desktop %d name: %s\n", i, s);
86 n_apps = Accessible_getChildCount (desktop);
87 for (j=0; j<n_apps; ++j)
89 application = Accessible_getChildAtIndex (desktop, j);
90 s = Accessible_getName (application);
91 fprintf (stderr, "app %d name: %s\n", j, s);
93 Accessible_unref (application);
95 Accessible_unref (desktop);
98 /* prepare the keyboard snoopers */
99 command_key_listener = createAccessibleKeystrokeListener (report_command_key_event, NULL);
100 ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event, NULL);
102 /* will listen only to Alt-key combinations, and only to KeyPress events */
103 registerAccessibleKeystrokeListener(command_key_listener,
104 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
106 (unsigned long) ( KeyPress ),
107 SPI_KEYLISTENER_ALL_WINDOWS);
109 /* will listen only to unshifted key events, both press and release */
110 registerAccessibleKeystrokeListener(ordinary_key_listener,
111 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
112 SPI_KEYMASK_UNMODIFIED,
113 (unsigned long) ( KeyPress | KeyRelease),
114 SPI_KEYLISTENER_NOSYNC);
116 /* will listen only to shifted key events, both press and release */
117 registerAccessibleKeystrokeListener(ordinary_key_listener,
118 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
120 (unsigned long) ( KeyPress | KeyRelease),
121 SPI_KEYLISTENER_NOSYNC);
123 get_environment_vars ();
127 setenv ("AT_BRIDGE_SHUTDOWN", "1", TRUE);
133 get_environment_vars (void)
135 if (g_getenv ("FESTIVAL"))
137 fprintf (stderr, "Using festival\n");
139 if (g_getenv ("FESTIVAL_CHATTY"))
141 festival_chatty = TRUE;
144 if (g_getenv ("MAGNIFIER"))
146 fprintf (stderr, "Using magnifier\n");
147 use_magnifier = TRUE;
152 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
157 g_warning ("Report focused !");
163 _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
165 fprintf (stderr, "getting Name\n");
166 s = Accessible_getName (obj);
167 _festival_say (s, "voice_kal_diphone",
168 shutup_previous_speech || festival_chatty);
172 if (Accessible_isComponent (obj))
174 long x, y, width, height;
175 AccessibleComponent *component = Accessible_getComponent (obj);
176 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
177 SPI_COORD_TYPE_SCREEN);
178 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
179 x, y, x+width, y+height);
181 magnifier_set_roi ((short) 0, x, y, width, height);
184 /* if this is a text object, speak the first sentence. */
186 if (Accessible_isText(obj))
189 AccessibleText *text_interface;
190 long start_offset, end_offset;
191 char *first_sentence = "empty";
192 text_interface = Accessible_getText (obj);
193 first_sentence = AccessibleText_getTextAtOffset (
194 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
197 _festival_say(first_sentence, "voice_don_diphone", FALSE);
198 SPI_freeString (first_sentence);
200 len = AccessibleText_getCharacterCount (text_interface);
201 s = AccessibleText_getText (text_interface, 0, len);
202 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
207 report_focus_event (AccessibleEvent *event, void *user_data)
211 g_warning ("report focus event");
213 g_return_if_fail (event->source != NULL);
215 s = Accessible_getName (event->source);
216 if (cspi_warn_ev (cspi_ev (), "Foobar"))
218 fprintf (stderr, "%s event from %s\n", event->type, s);
220 report_focussed_accessible (event->source, TRUE);
222 Accessible_getParent (event->source);
226 report_button_press (AccessibleEvent *event, void *user_data)
230 g_return_if_fail (event->source != NULL);
232 s = Accessible_getName (event->source);
234 fprintf (stderr, "%s event from %s\n", event->type, s);
236 s = Accessible_getDescription (event->source);
237 fprintf (stderr, "Object description %s\n", s);
242 check_property_change (AccessibleEvent *event, void *user_data)
244 AccessibleSelection *selection = Accessible_getSelection (event->source);
250 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
251 s = Accessible_getName (event->source);
252 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
253 event->type, s, n_selections);
255 /* for now, speak entire selection set */
256 for (i=0; i<n_selections; ++i)
258 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
259 g_return_if_fail (obj);
260 s = Accessible_getName (obj);
261 fprintf (stderr, "Child %d, name=%s\n", i, s);
263 report_focussed_accessible (obj, i==0);
271 deregisterGlobalEventListenerAll (focus_listener);
272 AccessibleEventListener_unref (focus_listener);
274 deregisterGlobalEventListenerAll (property_listener);
275 AccessibleEventListener_unref (property_listener);
277 deregisterGlobalEventListenerAll (button_listener);
278 AccessibleEventListener_unref (button_listener);
280 deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT);
281 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED);
282 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT);
283 AccessibleKeystrokeListener_unref (command_key_listener);
284 AccessibleKeystrokeListener_unref (ordinary_key_listener);
290 is_command_key (AccessibleKeystroke *key)
297 return TRUE; /* not reached */
300 use_magnifier = ! use_magnifier;
304 use_festival = ! use_festival;
312 report_command_key_event (AccessibleKeystroke *key, void *user_data)
314 fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
315 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
316 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
317 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
319 return is_command_key (key);
324 report_ordinary_key_event (AccessibleKeystroke *key, void *user_data)
326 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
328 (unsigned int) key->modifiers,
330 (long int) key->timestamp);
338 struct sockaddr_in name;
341 name.sin_family = AF_INET;
342 name.sin_port = htons (1314);
343 name.sin_addr.s_addr = htonl(INADDR_ANY);
344 fd = socket (PF_INET, SOCK_STREAM, 0);
346 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
353 _festival_write ("(audio_mode'async)\n", fd);
354 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
355 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
360 _festival_say (const char *text, const char *voice, SPIBoolean shutup)
366 static gchar voice_spec[32];
370 fd = _festival_init ();
373 fprintf (stderr, "saying text: %s\n", text);
375 quoted = g_malloc(64+strlen(text)*2);
377 sprintf (prefix, "(SayText \"");
379 strncpy(quoted, prefix, 10);
380 p = quoted+strlen(prefix);
382 if ( *text == '\\' || *text == '"' )
391 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
392 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
394 snprintf (voice_spec, 32, "(%s)\n", voice);
395 _festival_write (voice_spec, fd);
396 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
397 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
400 _festival_write (quoted, fd);
406 _festival_write (const gchar *command_string, int fd)
408 fprintf(stderr, command_string);
413 write(fd, command_string, strlen(command_string));