Added directory 'util' for accessibility-related
[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 void get_environment_vars (void);
31
32 static int _festival_init ();
33 static void _festival_say (const char *text, const char *voice, boolean shutup);
34 static void _festival_write (const char *buff, int fd);
35
36 static boolean use_magnifier = FALSE;
37 static boolean use_festival = FALSE;
38 static boolean festival_chatty = FALSE;
39
40 int
41 main(int argc, char **argv)
42 {
43   int i, j;
44   int n_desktops;
45   int n_apps;
46   Accessible *desktop;
47   Accessible *application;
48   AccessibleEventListener *focus_listener;
49   AccessibleEventListener *button_listener;
50
51   if ((argc > 1) && (!strncmp(argv[1],"-h",2)))
52   {
53     printf ("Usage: simple-at\n");
54     printf ("\tEnvironment variables used:\n\t\tFESTIVAL\n\t\tMAGNIFIER\n\t\tFESTIVAL_CHATTY\n");
55     exit(0);
56   }
57
58   SPI_init();
59
60   focus_listener = createEventListener (report_focus_event);
61   button_listener = createEventListener (report_button_press);
62
63   registerGlobalEventListener (focus_listener, "focus:");
64   registerGlobalEventListener (button_listener, "Gtk:GtkWidget:button-press-event");
65
66   n_desktops = getDesktopCount ();
67
68   for (i=0; i<n_desktops; ++i)
69     {
70       desktop = getDesktop (i);
71       fprintf (stderr, "desktop %d name: %s\n", i, Accessible_getName (desktop));
72       n_apps = Accessible_getChildCount (desktop);
73       for (j=0; j<n_apps; ++j)
74         {
75           application = Accessible_getChildAtIndex (desktop, j);
76           fprintf (stderr, "app %d name: %s\n", j, Accessible_getName (application));
77         }
78     }
79
80   get_environment_vars();
81
82   SPI_event_main(FALSE);
83 }
84
85 static void
86 get_environment_vars()
87 {
88   if (getenv ("FESTIVAL"))
89   {
90     use_festival = TRUE;
91     if (getenv ("FESTIVAL_CHATTY"))
92     {
93       festival_chatty = TRUE;
94     }
95   }
96   if (getenv("MAGNIFIER"))
97   {
98     use_magnifier = TRUE;
99   }  
100 }
101
102 void
103 report_focus_event (void *p)
104 {
105   AccessibleEvent *ev = (AccessibleEvent *) p;
106   fprintf (stderr, "%s event from %s\n", ev->type,
107            Accessible_getName (&ev->source));
108   
109   if (use_festival)
110     {
111     if (festival_chatty)            
112       {
113         _festival_say (Accessible_getRole (&ev->source), "voice_don_diphone", TRUE);
114       }
115       _festival_say (Accessible_getName (&ev->source), "voice_kal_diphone", festival_chatty==FALSE);
116     }
117   
118   if (Accessible_isComponent (&ev->source))
119     {
120       long x, y, width, height;
121       AccessibleComponent *component = Accessible_getComponent (&ev->source);
122       AccessibleComponent_getExtents (component, &x, &y, &width, &height,
123                                       COORD_TYPE_SCREEN);
124       fprintf (stderr, "Bounding box: (%ld, %ld) ; (%ld, %ld)\n",
125                x, y, x+width, y+height);
126       if (use_magnifier) {
127               magnifier_set_roi (x, y, width, height);        
128       }
129     }
130 }
131
132 void
133 report_button_press (void *p)
134 {
135   AccessibleEvent *ev = (AccessibleEvent *) p;
136   fprintf (stderr, "%s event from %s\n", ev->type,
137            Accessible_getName (&ev->source));
138 }
139
140 static int _festival_init ()
141 {
142   int fd;
143   struct sockaddr_in name;
144   int tries = 2;
145
146   name.sin_family = AF_INET;
147   name.sin_port = htons (1314);
148   name.sin_addr.s_addr = htonl(INADDR_ANY);
149   fd = socket (PF_INET, SOCK_STREAM, 0);
150
151   while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
152     if (!tries--) {
153       perror ("connect");
154       return -1;
155     }
156   }
157
158   _festival_write ("(audio_mode'async)\n", fd);
159   _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
160   _festival_write ("(Parameter.set 'Duration_Stretch 0.5)\n", fd);
161   return fd;
162 }
163
164 static void _festival_say (const char *text, const char *voice, boolean shutup)
165 {
166   static int fd = 0;
167   gchar *quoted;
168   gchar *p;
169   gchar prefix[50];
170   static gchar voice_spec[32];
171   
172   if (!fd)
173     {
174       fd = _festival_init ();
175     }
176
177   fprintf (stderr, "saying text: %s\n", text);
178   
179   quoted = g_malloc(64+strlen(text)*2);
180
181   sprintf (prefix, "(SayText \"");
182
183   strncpy(quoted, prefix, 10);
184   p = quoted+strlen(prefix);
185   while (*text) {
186     if ( *text == '\\' || *text == '"' )
187       *p = '\\';
188     *p++ = *text++;
189   }
190   *p++ = '"';
191   *p++ = ')';
192   *p++ = '\n';
193   *p = 0;
194
195   if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
196   if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
197     {
198       snprintf (voice_spec, 32, "(%s)\n", voice); 
199       _festival_write (voice_spec, fd);
200       _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
201       _festival_write ("(Parameter.set 'Duration_Stretch 0.5)\n", fd);
202     }
203
204   _festival_write (quoted, fd);
205
206   g_free(quoted);
207 }
208
209 static void _festival_write (const gchar *command_string, int fd)
210 {
211   fprintf(stderr, command_string);
212   if (fd < 0) {
213     perror("socket");
214     return;
215   }
216   write(fd, command_string, strlen(command_string));
217 }
218