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 boolean report_command_key_event (AccessibleKeystroke *stroke);
35 static boolean 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, boolean shutup);
40 static void _festival_write (const char *buff, int fd);
42 static boolean use_magnifier = FALSE;
43 static boolean use_festival = FALSE;
44 static boolean 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;
61 if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
63 printf ("Usage: simple-at\n");
64 printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
70 focus_listener = createAccessibleEventListener (report_focus_event);
71 property_listener = createAccessibleEventListener (check_property_change);
72 button_listener = createAccessibleEventListener (report_button_press);
73 registerGlobalEventListener (focus_listener, "focus:");
74 registerGlobalEventListener (property_listener, "object:property-change:accessible-selection");
75 registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
76 n_desktops = getDesktopCount ();
78 for (i=0; i<n_desktops; ++i)
80 desktop = getDesktop (i);
81 fprintf (stderr, "desktop %d name: %s\n", i, Accessible_getName (desktop));
82 n_apps = Accessible_getChildCount (desktop);
83 for (j=0; j<n_apps; ++j)
85 application = Accessible_getChildAtIndex (desktop, j);
86 fprintf (stderr, "app %d name: %s\n", j, Accessible_getName (application));
87 Accessible_unref (application);
89 Accessible_unref (desktop);
92 /* prepare the keyboard snoopers */
93 command_key_listener = createAccessibleKeystrokeListener (report_command_key_event);
94 ordinary_key_listener = createAccessibleKeystrokeListener (report_ordinary_key_event);
96 /* will listen only to Alt-key combinations, and only to KeyPress events */
97 registerAccessibleKeystrokeListener(command_key_listener,
98 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
100 (unsigned long) ( KeyPress ),
101 SPI_KEYLISTENER_ALL_WINDOWS);
103 /* will listen only to unshifted key events, both press and release */
104 registerAccessibleKeystrokeListener(ordinary_key_listener,
105 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
106 SPI_KEYMASK_UNMODIFIED,
107 (unsigned long) ( KeyPress | KeyRelease),
108 SPI_KEYLISTENER_NOSYNC);
110 /* will listen only to shifted key events, both press and release */
111 registerAccessibleKeystrokeListener(ordinary_key_listener,
112 (AccessibleKeySet *) SPI_KEYSET_ALL_KEYS,
114 (unsigned long) ( KeyPress | KeyRelease),
115 SPI_KEYLISTENER_NOSYNC);
117 get_environment_vars();
119 SPI_event_main(TRUE);
123 get_environment_vars()
125 if (getenv ("FESTIVAL"))
128 if (getenv ("FESTIVAL_CHATTY"))
130 festival_chatty = TRUE;
133 if (getenv("MAGNIFIER"))
135 use_magnifier = TRUE;
140 report_focussed_accessible (Accessible *obj, boolean shutup_previous_speech)
146 _festival_say (Accessible_getRole (obj), "voice_don_diphone", shutup_previous_speech);
148 fprintf (stderr, "getting Name\n");
149 _festival_say (Accessible_getName (obj), "voice_kal_diphone",
150 shutup_previous_speech || festival_chatty);
153 if (Accessible_isComponent (obj))
155 long x, y, width, height;
156 AccessibleComponent *component = Accessible_getComponent (obj);
157 AccessibleComponent_getExtents (component, &x, &y, &width, &height,
158 SPI_COORD_TYPE_SCREEN);
159 fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
160 x, y, x+width, y+height);
162 magnifier_set_roi ((short) 0, x, y, width, height);
165 /* if this is a text object, speak the first sentence. */
167 if (Accessible_isText(obj))
170 AccessibleText *text_interface;
171 long start_offset, end_offset;
172 char *first_sentence = "empty";
173 text_interface = Accessible_getText (obj);
174 fprintf (stderr, "isText...%p\n", text_interface);
175 first_sentence = AccessibleText_getTextAtOffset (
176 text_interface, (long) 0, SPI_TEXT_BOUNDARY_SENTENCE_START, &start_offset, &end_offset);
177 if (first_sentence) _festival_say(first_sentence, "voice_don_diphone", FALSE);
178 fprintf (stderr, "done reporting on focussed object\n");
183 report_focus_event (AccessibleEvent *event)
185 fprintf (stderr, "%s event from %s\n", event->type,
186 Accessible_getName (event->source));
187 report_focussed_accessible (event->source, TRUE);
191 report_button_press (AccessibleEvent *event)
193 fprintf (stderr, "%s event from %s\n", event->type,
194 Accessible_getName (event->source));
195 fprintf (stderr, "Object description %s\n",
196 Accessible_getDescription (event->source));
201 check_property_change (AccessibleEvent *event)
203 AccessibleSelection *selection = Accessible_getSelection (event->source);
208 n_selections = (int) AccessibleSelection_getNSelectedChildren (selection);
209 fprintf (stderr, "(Property) %s event from %s, %d selected children\n",
210 event->type, Accessible_getName (event->source), n_selections);
211 /* for now, speak entire selection set */
212 for (i=0; i<n_selections; ++i)
214 Accessible *obj = AccessibleSelection_getSelectedChild (selection, (long) i);
215 g_return_if_fail (obj);
216 fprintf (stderr, "Child %d, name=%s\n", i, Accessible_getName (obj));
217 report_focussed_accessible (obj, i==0);
225 deregisterGlobalEventListenerAll (focus_listener);
226 deregisterGlobalEventListenerAll (property_listener);
227 deregisterGlobalEventListenerAll (button_listener);
229 deregisterAccessibleKeystrokeListener (command_key_listener, SPI_KEYMASK_ALT );
230 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_UNMODIFIED );
231 deregisterAccessibleKeystrokeListener (ordinary_key_listener, SPI_KEYMASK_SHIFT );
232 AccessibleKeystrokeListener_unref (command_key_listener);
233 AccessibleKeystrokeListener_unref (ordinary_key_listener);
239 is_command_key (AccessibleKeystroke *key)
246 return TRUE; /* not reached */
249 use_magnifier = ! use_magnifier;
253 use_festival = ! use_festival;
261 report_command_key_event (AccessibleKeystroke *key)
263 fprintf (stderr, "Command KeyEvent %s%c (keycode %d)\n",
264 (key->modifiers & SPI_KEYMASK_ALT)?"Alt-":"",
265 ((key->modifiers & SPI_KEYMASK_SHIFT)^(key->modifiers & SPI_KEYMASK_SHIFTLOCK))?
266 (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
268 return is_command_key (key);
273 report_ordinary_key_event (AccessibleKeystroke *key)
275 fprintf (stderr, "Received key event:\tsym %ld\n\tmods %x\n\tcode %d\n\ttime %ld\n",
277 (unsigned int) key->modifiers,
279 (long int) key->timestamp);
283 static int _festival_init ()
286 struct sockaddr_in name;
289 name.sin_family = AF_INET;
290 name.sin_port = htons (1314);
291 name.sin_addr.s_addr = htonl(INADDR_ANY);
292 fd = socket (PF_INET, SOCK_STREAM, 0);
294 while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
301 _festival_write ("(audio_mode'async)\n", fd);
302 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
303 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
307 static void _festival_say (const char *text, const char *voice, boolean shutup)
313 static gchar voice_spec[32];
317 fd = _festival_init ();
320 fprintf (stderr, "saying text: %s\n", text);
322 quoted = g_malloc(64+strlen(text)*2);
324 sprintf (prefix, "(SayText \"");
326 strncpy(quoted, prefix, 10);
327 p = quoted+strlen(prefix);
329 if ( *text == '\\' || *text == '"' )
338 if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
339 if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
341 snprintf (voice_spec, 32, "(%s)\n", voice);
342 _festival_write (voice_spec, fd);
343 _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
344 _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
347 _festival_write (quoted, fd);
352 static void _festival_write (const gchar *command_string, int fd)
354 fprintf(stderr, command_string);
359 write(fd, command_string, strlen(command_string));