Added docs for SPI_freeString, and used it to stop memory leak of strings
[platform/core/uifw/at-spi2-atk.git] / test / 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 <stdio.h>
24 #include <stdlib.h>
25 #include <libbonobo.h>
26 #include <libspi/Accessibility.h>
27 #include "accessible.h"
28 #include "listener.h"
29 #include "desktop.h"
30
31 void
32 check_ev (CORBA_Environment *ev, char *desc)
33 {
34         if (ev->_major != CORBA_NO_EXCEPTION) {
35                 fprintf(stderr,
36                 ("Accessibility app error: exception during CORBA call %s (%s)\n"),
37                         CORBA_exception_id(ev), desc);
38                 CORBA_exception_free(ev);
39                 exit(-1);
40         }
41 }
42
43 int
44 main(int argc, char **argv)
45 {
46         CORBA_Environment ev;
47         CORBA_Object oclient;
48         char *obj_id;
49         CORBA_long i;
50         CORBA_short n_desktops;
51         CORBA_long j;
52         CORBA_long n_apps;
53         CORBA_string s;
54         Accessibility_Desktop desktop;
55         Accessibility_Application app;
56         Accessibility_Registry registry;
57         SpiListener *listener;
58
59         CORBA_exception_init(&ev);
60
61         if (!bonobo_init (&argc, argv))
62           {
63             g_error ("Could not initialize Bonobo");
64           }
65
66         obj_id = "OAFIID:Accessibility_Registry:proto0.1";
67
68         oclient = bonobo_activation_activate_from_id (obj_id, 0, NULL, &ev);
69         if (ev._major != CORBA_NO_EXCEPTION) {
70                 fprintf(stderr,
71                 ("Accessibility app error: exception during registry activation from id: %s\n"),
72                         CORBA_exception_id(&ev));
73                 CORBA_exception_free(&ev);
74                 exit(-1);
75         }
76
77         if (CORBA_Object_is_nil (oclient, &ev))
78           {
79             g_error ("Could not locate registry");
80           }
81
82         bonobo_activate ();
83
84         listener = spi_listener_new ();
85
86         registry = (Accessibility_Registry) oclient;
87
88         Accessibility_Registry_registerGlobalEventListener
89                                    (registry,
90                                     (Accessibility_EventListener)
91                                          bonobo_object_corba_objref (bonobo_object (listener)),
92                                     "focus:",
93                                     &ev);
94         check_ev (&ev, "register:focus");
95         Accessibility_Registry_registerGlobalEventListener
96                                    (registry,
97                                     (Accessibility_EventListener)
98                                          bonobo_object_corba_objref (bonobo_object (listener)),
99                                     "Gtk:GtkWidget:button-press-event",
100                                     &ev);
101         check_ev (&ev, "register:button_press");
102         fprintf (stderr, "AT callback registered.\n");
103
104             n_desktops = Accessibility_Registry_getDesktopCount (registry, &ev);
105
106             for (i=0; i<n_desktops; ++i)
107               {
108                 desktop = Accessibility_Registry_getDesktop (registry, i, &ev);
109                 s = Accessibility_Desktop__get_name (desktop, &ev);
110                 fprintf (stderr, "desktop %d name: %s\n", i, s);
111                 CORBA_free (s);  
112                 check_ev (&ev, "desktop:name");
113                 n_apps = Accessibility_Desktop__get_childCount (desktop, &ev);
114                 check_ev (&ev, "desktop:childCount");
115                 fprintf (stderr, "desktop has %d apps:\n", n_apps);
116                 for (j=0; j<n_apps; ++j)
117                   {
118                     app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop, j, &ev);
119                     check_ev (&ev, "desktop:getChildAtIndex");
120                     s = Accessibility_Application__get_name (app, &ev);
121                     fprintf (stderr, "app %d name: %s\n", j, s);
122                     CORBA_free (s);
123                     check_ev (&ev, "app:getName");
124                   }
125               }
126
127             fprintf (stderr, "finished querying desktop(s).\n");
128             bonobo_main ();
129                /* needed by at because it is a server ? */
130         exit(0);
131 }