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