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_key_event (void *fp);
31 static void check_property_change (void *fp);
32 static void get_environment_vars (void);
34 static int _festival_init ();
35 static void _festival_say (const char *text, const char *voice, boolean shutup);
36 static void _festival_write (const char *buff, int fd);
38 static boolean use_magnifier = FALSE;
39 static boolean use_festival = FALSE;
40 static boolean festival_chatty = FALSE;
42 static SpiAccessibleEventListener *focus_listener;
43 static SpiAccessibleEventListener *property_listener;
44 static SpiAccessibleEventListener *button_listener;
45 static KeystrokeListener *key_listener;
48 main(int argc, char **argv)
53 SpiAccessible *desktop;
54 SpiAccessible *application;
56 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
58 printf ("Usage: simple-at\n");
59 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
65 focus_listener = createEventListener (report_focus_event);
66 property_listener = createEventListener (check_property_change);
67 button_listener = createEventListener (report_button_press);
68 registerGlobalEventListener (focus_listener, "focus:");
69 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
70 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
71 n_desktops = getDesktopCount ();
73 for (i=0; i<n_desktops; ++i)
75 desktop = getDesktop (i);
76 fprintf (stderr, "desktop %d name: %s\n", i, SpiAccessible_getName (desktop));
77 n_apps = SpiAccessible_getChildCount (desktop);
78 for (j=0; j<n_apps; ++j)
80 application = SpiAccessible_getChildAtIndex (desktop, j);
81 fprintf (stderr, "app %d name: %s\n", j, SpiAccessible_getName (application));
82 SpiAccessible_unref (application);
86 /* prepare the keyboard snoopers */
87 key_listener = createKeystrokeListener(report_key_event);
88 /* will listen only to Alt-key combinations */
89 registerKeystrokeListener(key_listener,
92 (unsigned long) ( KeyPress | KeyRelease),
93 KEYSPI_LISTENER_CANCONSUME);
95 get_environment_vars();
101 get_environment_vars()
103 if (getenv ("FESTIVAL"))
106 if (getenv ("FESTIVAL_CHATTY"))
108 festival_chatty = TRUE;
111 if (getenv("MAGNIFIER"))
113 use_magnifier = TRUE;
118 report_focussed_accessible (SpiAccessible *obj, boolean shutup_previous_speech)
124 _festival_say (SpiAccessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
126 fprintf (stderr, "getting Name\n");
127 _festival_say (SpiAccessible_getName (obj), "voice_kal_diphone",
128 shutup_previous_speech || festival_chatty);
131 if (SpiAccessible_isComponent (obj))
133 long x, y, width, height;
134 SpiAccessibleComponent *component = SpiAccessible_getComponent (obj);
135 SpiAccessibleComponent_getExtents (component, &x, &y, &width, &height,
137 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
138 x, y, x+width, y+height);
140 magnifier_set_roi ((short) 0, x, y, width, height);
143 /* if this is a text object, speak the first sentence. */
144 if (SpiAccessible_isText(obj))
146 SpiAccessibleText *spi_text_interface;
147 long start_offset, end_offset;
148 char *first_sentence = "empty";
149 spi_text_interface = SpiAccessible_getText (obj);
150 fprintf (stderr, "isText...%p %p\n", spi_text_interface, (void *)*spi_text_interface);
151 first_sentence = SpiAccessibleText_getTextAtOffset (
152 spi_text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
153 if (first_sentence) _festival_say(first_sentence, "voice_don_diphone", FALSE);
154 fprintf (stderr, "done reporting on focussed object\n");
159 report_focus_event (void *p)
161 SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
162 fprintf (stderr, "%s event from %s\n", ev->type,
163 SpiAccessible_getName (&ev->source));
164 report_focussed_accessible (&ev->source, TRUE);
168 report_button_press (void *p)
170 SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
171 fprintf (stderr, "%s event from %s\n", ev->type,
172 SpiAccessible_getName (&ev->source));
177 check_property_change (void *p)
179 SpiAccessibleEvent *ev = (SpiAccessibleEvent *) p;
180 SpiAccessibleSelection *selection = SpiAccessible_getSelection (&ev->source);
185 n_selections = (int) SpiAccessibleSelection_getNSelectedChildren (selection);
186 fprintf (stderr, "(Property) %s event from %s, %d selected children\n", ev->type,
187 SpiAccessible_getName (&ev->source), n_selections);
188 /* for now, speak entire selection set */
189 for (i=0; i<n_selections; ++i)
191 SpiAccessible *obj = SpiAccessibleSelection_getSelectedChild (selection, (long) i);
192 g_return_if_fail (obj);
193 fprintf (stderr, "Child %d, name=%s\n", i, SpiAccessible_getName (obj));
194 report_focussed_accessible (obj, i==0);
202 deregisterGlobalEventListenerAll (focus_listener);
203 deregisterGlobalEventListenerAll (property_listener);
204 deregisterGlobalEventListenerAll (button_listener);
205 deregisterKeystrokeListener (key_listener, KEYMASK_ALT );
211 is_command_key (KeyStroke *key)
218 return TRUE; /* not reached */
221 use_magnifier = ! use_magnifier;
225 use_festival = ! use_festival;
233 report_key_event (void *p)
235 KeyStroke *key = (KeyStroke *) p;
236 fprintf(stderr, "KeyEvent %s%c (keycode %d)\n",
237 (key->modifiers & KEYMASK_ALT)?"Alt-":"",
238 ((key->modifiers & KEYMASK_SHIFT)^(key->modifiers & KEYMASK_SHIFTLOCK))?
239 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
241 return is_command_key (key);
244 static int _festival_init ()
247 struct sockaddr_in name;
250 name.sin_family = AF_INET;
251 name.sin_port = htons (1314);
252 name.sin_addr.s_addr = htonl(INADDR_ANY);
253 fd = socket (PF_INET, SOCK_STREAM, 0);
255 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
262 _festival_write ("(audio_mode'async)\n", fd);
263 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
264 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
268 static void _festival_say (const char *text, const char *voice, boolean shutup)
274 static gchar voice_spec[32];
278 fd = _festival_init ();
281 fprintf (stderr, "saying text: %s\n", text);
283 quoted = g_malloc(64+strlen(text)*2);
285 sprintf (prefix, "(SayText \"");
287 strncpy(quoted, prefix, 10);
288 p = quoted+strlen(prefix);
290 if ( *text == '\\' || *text == '"' )
299 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
300 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
302 snprintf (voice_spec, 32, "(%s)\n", voice);
303 _festival_write (voice_spec, fd);
304 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
305 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
308 _festival_write (quoted, fd);
313 static void _festival_write (const gchar *command_string, int fd)
315 fprintf(stderr, command_string);
320 write(fd, command_string, strlen(command_string));