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"
31 static void report_focus_event (AccessibleEvent *event);
32 static void report_button_press (AccessibleEvent *event);
33 static void check_property_change (AccessibleEvent *event);
34 static SPIBoolean report_command_key_event (AccessibleKeystroke *stroke);
35 static SPIBoolean report_ordinary_key_event (AccessibleKeystroke *stroke);
36 static void get_environment_vars (void);
38 static int _festival_init ();
39 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup);
40 static void _festival_write (const char *buff, int fd);
42 static SPIBoolean use_magnifier = FALSE;
43 static SPIBoolean use_festival = FALSE;
44 static SPIBoolean festival_chatty = FALSE;
46 static AccessibleEventListener *focus_listener;
47 static AccessibleEventListener *property_listener;
48 static AccessibleEventListener *button_listener;
49 static AccessibleKeystrokeListener *command_key_listener;
50 static AccessibleKeystrokeListener *ordinary_key_listener;
53 main(int argc, char **argv)
59 Accessible *application;
62 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
64 printf ("Usage: simple-at\n");
65 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
71 focus_listener = createAccessibleEventListener (report_focus_event);
72 property_listener = createAccessibleEventListener (check_property_change);
73 button_listener = createAccessibleEventListener (report_button_press);
74 registerGlobalEventListener (focus_listener, "focus:");
75 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
76 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
77 n_desktops = getDesktopCount ();
79 for (i=0; i<n_desktops; ++i)
81 desktop = getDesktop (i);
82 s = Accessible_getName (desktop);
83 fprintf (stderr, "desktop %d name: %s\n", i, s);
85 n_apps = Accessible_getChildCount (desktop);
86 for (j=0; j<n_apps; ++j)
88 application = Accessible_getChildAtIndex (desktop, j);
89 s = Accessible_getName (application);
90 fprintf (stderr, "app %d name: %s\n", j, s);
92 Accessible_unref (application);
94 Accessible_unref (desktop);
97 /* prepare the keyboard snoopers */
98 command_key_listener = createAccessibleKeystrokeListener (report_command_key_event);
99 ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event);
101 /* will listen only to Alt-key combinations, and only to KeyPress events */
102 registerAccessibleKeystrokeListener(command_key_listener,
103 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
105 (unsigned long) ( KeyPress ),
106 SPI_KEYLISTENER_ALL_WINDOWS);
108 /* will listen only to unshifted key events, both press and release */
109 registerAccessibleKeystrokeListener(ordinary_key_listener,
110 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
111 SPI_KEYMASK_UNMODIFIED,
112 (unsigned long) ( KeyPress | KeyRelease),
113 SPI_KEYLISTENER_NOSYNC);
115 /* will listen only to shifted key events, both press and release */
116 registerAccessibleKeystrokeListener(ordinary_key_listener,
117 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
119 (unsigned long) ( KeyPress | KeyRelease),
120 SPI_KEYLISTENER_NOSYNC);
122 get_environment_vars();
124 SPI_event_main(TRUE);
128 get_environment_vars()
130 if (getenv ("FESTIVAL"))
133 if (getenv ("FESTIVAL_CHATTY"))
135 festival_chatty = TRUE;
138 if (getenv("MAGNIFIER"))
140 use_magnifier = TRUE;
145 report_focussed_accessible (Accessible *obj, SPIBoolean shutup_previous_speech)
153 _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
155 fprintf (stderr, "getting Name\n");
156 s = Accessible_getName (obj);
157 _festival_say (s, "voice_kal_diphone",
158 shutup_previous_speech || festival_chatty);
162 if (Accessible_isComponent (obj))
164 long x, y, width, height;
165 AccessibleComponent *component = Accessible_getComponent (obj);
166 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
167 SPI_COORD_TYPE_SCREEN);
168 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
169 x, y, x+width, y+height);
171 magnifier_set_roi ((short) 0, x, y, width, height);
174 /* if this is a text object, speak the first sentence. */
176 if (Accessible_isText(obj))
179 AccessibleText *text_interface;
180 long start_offset, end_offset;
181 char *first_sentence = "empty";
182 text_interface = Accessible_getText (obj);
183 first_sentence = AccessibleText_getTextAtOffset (
184 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
187 _festival_say(first_sentence, "voice_don_diphone", FALSE);
188 SPI_freeString (first_sentence);
190 len = AccessibleText_getCharacterCount (text_interface);
191 s = AccessibleText_getText (text_interface, 0, len);
192 fprintf (stderr, "done reporting on focussed object, text=%s\n", s);
197 report_focus_event (AccessibleEvent *event)
199 char *s = Accessible_getName (event->source);
200 fprintf (stderr, "%s event from %s\n", event->type, s);
202 report_focussed_accessible (event->source, TRUE);
206 report_button_press (AccessibleEvent *event)
208 char *s = Accessible_getName (event->source);
209 fprintf (stderr, "%s event from %s\n", event->type, s);
211 s = Accessible_getDescription (event->source);
212 fprintf (stderr, "Object description %s\n", s);
218 check_property_change (AccessibleEvent *event)
220 AccessibleSelection *selection = Accessible_getSelection (event->source);
226 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
227 s = Accessible_getName (event->source);
228 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
229 event->type, s, n_selections);
231 /* for now, speak entire selection set */
232 for (i=0; i<n_selections; ++i)
234 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
235 g_return_if_fail (obj);
236 s = Accessible_getName (obj);
237 fprintf (stderr, "Child %d, name=%s\n", i, s);
239 report_focussed_accessible (obj, i==0);
247 deregisterGlobalEventListenerAll (focus_listener);
248 deregisterGlobalEventListenerAll (property_listener);
249 deregisterGlobalEventListenerAll (button_listener);
251 deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT );
252 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED );
253 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT );
254 AccessibleKeystrokeListener_unref (command_key_listener);
255 AccessibleKeystrokeListener_unref (ordinary_key_listener);
261 is_command_key (AccessibleKeystroke *key)
268 return TRUE; /* not reached */
271 use_magnifier = ! use_magnifier;
275 use_festival = ! use_festival;
283 report_command_key_event (AccessibleKeystroke *key)
285 fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
286 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
287 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
288 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
290 return is_command_key (key);
295 report_ordinary_key_event (AccessibleKeystroke *key)
297 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
299 (unsigned int) key->modifiers,
301 (long int) key->timestamp);
305 static int _festival_init ()
308 struct sockaddr_in name;
311 name.sin_family = AF_INET;
312 name.sin_port = htons (1314);
313 name.sin_addr.s_addr = htonl(INADDR_ANY);
314 fd = socket (PF_INET, SOCK_STREAM, 0);
316 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
323 _festival_write ("(audio_mode'async)\n", fd);
324 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
325 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
329 static void _festival_say (const char *text, const char *voice, SPIBoolean shutup)
335 static gchar voice_spec[32];
339 fd = _festival_init ();
342 fprintf (stderr, "saying text: %s\n", text);
344 quoted = g_malloc(64+strlen(text)*2);
346 sprintf (prefix, "(SayText \"");
348 strncpy(quoted, prefix, 10);
349 p = quoted+strlen(prefix);
351 if ( *text == '\\' || *text == '"' )
360 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
361 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
363 snprintf (voice_spec, 32, "(%s)\n", voice);
364 _festival_write (voice_spec, fd);
365 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
366 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
369 _festival_write (quoted, fd);
374 static void _festival_write (const gchar *command_string, int fd)
376 fprintf(stderr, command_string);
381 write(fd, command_string, strlen(command_string));