Fixed 'make dist', and added:
[platform/core/uifw/at-spi2-atk.git] / test / simple-at.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <stdlib.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include "spi.h"
27
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 get_environment_vars (void);
32
33 static int _festival_init ();
34 static void _festival_say (const char *text, const char *voice, boolean shutup);
35 static void _festival_write (const char *buff, int fd);
36
37 static boolean use_magnifier = FALSE;
38 static boolean use_festival = FALSE;
39 static boolean festival_chatty = FALSE;
40
41 int
42 main(int argc, char **argv)
43 {
44   int i, j;
45   int n_desktops;
46   int n_apps;
47   Accessible *desktop;
48   Accessible *application;
49   AccessibleEventListener *focus_listener;
50   AccessibleEventListener *button_listener;
51   KeystrokeListener *key_listener;
52
53   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
54   {
55     printf ("Usage: simple-at\n");
56     printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
57     exit(0);
58   }
59
60   SPI_init();
61
62   focus_listener = createEventListener (report_focus_event);
63   button_listener = createEventListener (report_button_press);
64
65   registerGlobalEventListener (focus_listener, "focus:");
66   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
67
68   n_desktops = getDesktopCount ();
69
70   for (i=0; i<n_desktops; ++i)
71     {
72       desktop = getDesktop (i);
73       fprintf (stderr, "desktop %d name: %s\n", i, Accessible_getName (desktop));
74       n_apps = Accessible_getChildCount (desktop);
75       for (j=0; j<n_apps; ++j)
76         {
77           application = Accessible_getChildAtIndex (desktop, j);
78           fprintf (stderr, "app %d name: %s\n", j, Accessible_getName (application));
79         }
80     }
81
82   /* prepare the keyboard snoopers */
83   /* key_listener = createKeystrokeListener(report_key_event);
84      registerKeystrokeListener(key_listener, KEYMASK_SHIFT); */
85
86   get_environment_vars();
87
88   SPI_event_main(FALSE);
89 }
90
91 static void
92 get_environment_vars()
93 {
94   if (getenv ("FESTIVAL"))
95   {
96     use_festival = TRUE;
97     if (getenv ("FESTIVAL_CHATTY"))
98     {
99       festival_chatty = TRUE;
100     }
101   }
102   if (getenv("MAGNIFIER"))
103   {
104     use_magnifier = TRUE;
105   }  
106 }
107
108 void
109 report_focus_event (void *p)
110 {
111   AccessibleEvent *ev = (AccessibleEvent *) p;
112   fprintf (stderr, "%s event from %s\n", ev->type,
113            Accessible_getName (&ev->source));
114   
115   if (use_festival)
116     {
117     if (festival_chatty)            
118       {
119         _festival_say (Accessible_getRole (&ev->source), "voice_don_diphone", TRUE);
120       }
121       _festival_say (Accessible_getName (&ev->source), "voice_kal_diphone", festival_chatty==FALSE);
122     }
123   
124   if (Accessible_isComponent (&ev->source))
125     {
126       long x, y, width, height;
127       AccessibleComponent *component = Accessible_getComponent (&ev->source);
128       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
129                                       COORD_TYPE_SCREEN);
130       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
131                x, y, x+width, y+height);
132       if (use_magnifier) {
133               magnifier_set_roi (x, y, width, height);        
134       }
135     }
136   if (Accessible_isText(&ev->source))
137     /* if this is a text object, speak the first sentence. */
138   {
139      AccessibleText *text_interface = Accessible_getText (&ev->source);
140      long start_offset, end_offset;
141      char *first_sentence = "";
142      first_sentence = AccessibleText_getTextAtOffset (
143              text_interface, (long) 0, TEXT_BOUNDARY_WORD_END,
144              &start_offset, &end_offset); 
145      _festival_say(first_sentence, "voice_don_diphone", festival_chatty==FALSE);
146   }
147 }
148
149 void
150 report_button_press (void *p)
151 {
152   AccessibleEvent *ev = (AccessibleEvent *) p;
153   fprintf (stderr, "%s event from %s\n", ev->type,
154            Accessible_getName (&ev->source));
155 }
156
157 static boolean
158 report_key_event (void *p)
159 {
160   KeyStroke *key = (KeyStroke *) p;
161   fprintf (stderr, ".");
162   return FALSE;
163 }
164
165 static int _festival_init ()
166 {
167   int fd;
168   struct sockaddr_in name;
169   int tries = 2;
170
171   name.sin_family = AF_INET;
172   name.sin_port = htons (1314);
173   name.sin_addr.s_addr = htonl(INADDR_ANY);
174   fd = socket (PF_INET, SOCK_STREAM, 0);
175
176   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
177     if (!tries--) {
178       perror ("connect");
179       return -1;
180     }
181   }
182
183   _festival_write ("(audio_mode'async)\n", fd);
184   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
185   _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
186   return fd;
187 }
188
189 static void _festival_say (const char *text, const char *voice, boolean shutup)
190 {
191   static int fd = 0;
192   gchar *quoted;
193   gchar *p;
194   gchar prefix[50];
195   static gchar voice_spec[32];
196   
197   if (!fd)
198     {
199       fd = _festival_init ();
200     }
201
202   fprintf (stderr, "saying text: %s\n", text);
203   
204   quoted = g_malloc(64+strlen(text)*2);
205
206   sprintf (prefix, "(SayText \"");
207
208   strncpy(quoted, prefix, 10);
209   p = quoted+strlen(prefix);
210   while (*text) {
211     if ( *text == '\\' || *text == '"' )
212       *p = '\\';
213     *p++ = *text++;
214   }
215   *p++ = '"';
216   *p++ = ')';
217   *p++ = '\n';
218   *p = 0;
219
220   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
221   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
222     {
223       snprintf (voice_spec, 32, "(%s)\n", voice); 
224       _festival_write (voice_spec, fd);
225       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
226       _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
227     }
228
229   _festival_write (quoted, fd);
230
231   g_free(quoted);
232 }
233
234 static void _festival_write (const gchar *command_string, int fd)
235 {
236   fprintf(stderr, command_string);
237   if (fd < 0) {
238     perror("socket");
239     return;
240   }
241   write(fd, command_string, strlen(command_string));
242 }
243