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.
24 #include <sys/socket.h>
28 static void report_focus_event (void *fp);
29 static void report_button_press (void *fp);
30 static boolean report_command_key_event (void *fp);
31 static boolean report_ordinary_key_event (void *fp);
32 static void check_property_change (void *fp);
33 static void get_environment_vars (void);
35 static int _festival_init ();
36 static void _festival_say (const char *text, const char *voice, boolean shutup);
37 static void _festival_write (const char *buff, int fd);
39 static boolean use_magnifier = FALSE;
40 static boolean use_festival = FALSE;
41 static boolean festival_chatty = FALSE;
43 static AccessibleEventListener *focus_listener;
44 static AccessibleEventListener *property_listener;
45 static AccessibleEventListener *button_listener;
46 static AccessibleKeystrokeListener *command_key_listener;
47 static AccessibleKeystrokeListener *ordinary_key_listener;
50 main(int argc, char **argv)
56 Accessible *application;
58 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
60 printf ("Usage: simple-at\n");
61 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
67 focus_listener = createAccessibleEventListener (report_focus_event);
68 property_listener = createAccessibleEventListener (check_property_change);
69 button_listener = createAccessibleEventListener (report_button_press);
70 registerGlobalEventListener (focus_listener, "focus:");
71 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
72 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
73 n_desktops = getDesktopCount ();
75 for (i=0; i<n_desktops; ++i)
77 desktop = getDesktop (i);
78 fprintf (stderr, "desktop %d name: %s\n", i, Accessible_getName (desktop));
79 n_apps = Accessible_getChildCount (desktop);
80 for (j=0; j<n_apps; ++j)
82 application = Accessible_getChildAtIndex (desktop, j);
83 fprintf (stderr, "app %d name: %s\n", j, Accessible_getName (application));
84 Accessible_unref (application);
88 /* prepare the keyboard snoopers */
89 command_key_listener = createAccessibleKeystrokeListener(report_command_key_event);
90 ordinary_key_listener = createAccessibleKeystrokeListener(report_ordinary_key_event);
92 /* will listen only to Alt-key combinations, and only to KeyPress events */
93 registerAccessibleKeystrokeListener(command_key_listener,
94 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
96 (unsigned long) ( KeyPress ),
97 SPI_KEYLISTENER_ALL_WINDOWS);
99 /* will listen only to unshifted key events, both press and release */
100 registerAccessibleKeystrokeListener(ordinary_key_listener,
101 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
102 SPI_KEYMASK_UNMODIFIED,
103 (unsigned long) ( KeyPress | KeyRelease),
104 SPI_KEYLISTENER_NOSYNC);
106 /* will listen only to shifted key events, both press and release */
107 registerAccessibleKeystrokeListener(ordinary_key_listener,
108 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
110 (unsigned long) ( KeyPress | KeyRelease),
111 SPI_KEYLISTENER_NOSYNC);
113 get_environment_vars();
115 SPI_event_main(TRUE);
119 get_environment_vars()
121 if (getenv ("FESTIVAL"))
124 if (getenv ("FESTIVAL_CHATTY"))
126 festival_chatty = TRUE;
129 if (getenv("MAGNIFIER"))
131 use_magnifier = TRUE;
136 report_focussed_accessible (Accessible *obj, boolean shutup_previous_speech)
142 _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
144 fprintf (stderr, "getting Name\n");
145 _festival_say (Accessible_getName (obj), "voice_kal_diphone",
146 shutup_previous_speech || festival_chatty);
149 if (Accessible_isComponent (obj))
151 long x, y, width, height;
152 AccessibleComponent *component = Accessible_getComponent (obj);
153 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
154 SPI_COORD_TYPE_SCREEN);
155 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
156 x, y, x+width, y+height);
158 magnifier_set_roi ((short) 0, x, y, width, height);
161 /* if this is a text object, speak the first sentence. */
163 if (Accessible_isText(obj))
166 AccessibleText *text_interface;
167 long start_offset, end_offset;
168 char *first_sentence = "empty";
169 text_interface = Accessible_getText (obj);
170 fprintf (stderr, "isText...%p %p\n", text_interface, (void *)*text_interface);
171 first_sentence = AccessibleText_getTextAtOffset (
172 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
173 if (first_sentence) _festival_say(first_sentence, "voice_don_diphone", FALSE);
174 fprintf (stderr, "done reporting on focussed object\n");
179 report_focus_event (void *p)
181 AccessibleEvent *ev = (AccessibleEvent *) p;
182 fprintf (stderr, "%s event from %s\n", ev->type,
183 Accessible_getName (&ev->source));
184 report_focussed_accessible (&ev->source, TRUE);
188 report_button_press (void *p)
190 AccessibleEvent *ev = (AccessibleEvent *) p;
191 fprintf (stderr, "%s event from %s\n", ev->type,
192 Accessible_getName (&ev->source));
193 fprintf (stderr, "Object description %s\n",
194 Accessible_getDescription (&ev->source));
199 check_property_change (void *p)
201 AccessibleEvent *ev = (AccessibleEvent *) p;
202 AccessibleSelection *selection = Accessible_getSelection (&ev->source);
207 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
208 fprintf (stderr, "(Property) %s event from %s, %d selected children\n", ev->type,
209 Accessible_getName (&ev->source), n_selections);
210 /* for now, speak entire selection set */
211 for (i=0; i<n_selections; ++i)
213 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
214 g_return_if_fail (obj);
215 fprintf (stderr, "Child %d, name=%s\n", i, Accessible_getName (obj));
216 report_focussed_accessible (obj, i==0);
224 deregisterGlobalEventListenerAll (focus_listener);
225 deregisterGlobalEventListenerAll (property_listener);
226 deregisterGlobalEventListenerAll (button_listener);
227 deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT );
228 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED );
229 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT );
235 is_command_key (AccessibleKeyStroke *key)
242 return TRUE; /* not reached */
245 use_magnifier = ! use_magnifier;
249 use_festival = ! use_festival;
257 report_command_key_event (void *p)
259 AccessibleKeyStroke *key = (AccessibleKeyStroke *) p;
260 fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
261 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
262 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
263 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
265 return is_command_key (key);
270 report_ordinary_key_event (void *p)
272 AccessibleKeyStroke *key = (AccessibleKeyStroke *) p;
273 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
275 (unsigned int) key->modifiers,
277 (long int) key->timestamp);
280 static int _festival_init ()
283 struct sockaddr_in name;
286 name.sin_family = AF_INET;
287 name.sin_port = htons (1314);
288 name.sin_addr.s_addr = htonl(INADDR_ANY);
289 fd = socket (PF_INET, SOCK_STREAM, 0);
291 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
298 _festival_write ("(audio_mode'async)\n", fd);
299 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
300 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
304 static void _festival_say (const char *text, const char *voice, boolean shutup)
310 static gchar voice_spec[32];
314 fd = _festival_init ();
317 fprintf (stderr, "saying text: %s\n", text);
319 quoted = g_malloc(64+strlen(text)*2);
321 sprintf (prefix, "(SayText \"");
323 strncpy(quoted, prefix, 10);
324 p = quoted+strlen(prefix);
326 if ( *text == '\\' || *text == '"' )
335 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
336 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
338 snprintf (voice_spec, 32, "(%s)\n", voice);
339 _festival_write (voice_spec, fd);
340 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
341 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
344 _festival_write (quoted, fd);
349 static void _festival_write (const gchar *command_string, int fd)
351 fprintf(stderr, command_string);
356 write(fd, command_string, strlen(command_string));