2001-12-08 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / libspi / application.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 /* application.c: implements SpiApplication.idl */
24
25 #include <string.h>
26 #include <config.h>
27 #include <atk/atkutil.h>
28 #include <libspi/application.h>
29
30 /* Our parent Gtk object type */
31 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
32
33 /* A pointer to our parent object class */
34 static SpiAccessibleClass *spi_application_parent_class;
35
36 static SpiApplication *the_app;
37
38 /* static methods */
39
40 static void notify_listeners (GList *listeners,
41                               SpiAccessible *source,
42                               Accessibility_Event *e);
43
44 static const char *reverse_lookup_name_for_toolkit_event (char *toolkit_name);
45
46 static const char *
47 lookup_toolkit_event_for_name (const char *generic_name)
48 {
49   char *toolkit_specific_name;
50   SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
51 #ifdef SPI_DEBUG
52   fprintf (stderr, "looking for %s in hash table.\n", generic_name);
53 #endif
54   toolkit_specific_name =
55     (char *) g_hash_table_lookup (klass->toolkit_event_names, generic_name);
56 #ifdef SPI_DEBUG
57   fprintf (stderr, "generic event %s converted to %s\n", generic_name, toolkit_specific_name);
58 #endif
59   return toolkit_specific_name;
60 }
61
62 /*
63  * Implemented GObject::finalize
64  */
65 static void
66 spi_accessible_application_finalize (GObject *object)
67 {
68   GList *l;
69   SpiApplication *application = (SpiApplication *) object;
70   CORBA_Environment ev;
71
72   CORBA_exception_init (&ev);
73
74   for (l = application->toolkit_listeners; l; l = l->next)
75     {
76       CORBA_Object_release ((CORBA_Object) l->data, &ev);
77     }
78
79   CORBA_exception_free (&ev);
80
81   g_list_free (application->toolkit_listeners);
82   application->toolkit_listeners = NULL;
83
84   g_print ("application finalize called\n");
85   (G_OBJECT_CLASS (spi_application_parent_class))->finalize (object);
86 }
87
88 static CORBA_string
89 impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
90                                                  CORBA_Environment     *ev)
91 {
92   return CORBA_string_dup (atk_get_toolkit_name ());
93 }
94
95 static CORBA_string
96 impl_accessibility_application_get_version (PortableServer_Servant servant,
97                                             CORBA_Environment     *ev)
98 {
99   return CORBA_string_dup (atk_get_toolkit_version ());
100 }
101
102 static CORBA_long
103 impl_accessibility_application_get_id (PortableServer_Servant servant,
104                                        CORBA_Environment     *ev)
105 {
106   SpiApplication *application = SPI_APPLICATION (
107     bonobo_object_from_servant (servant));
108
109   return application->id;
110 }
111
112 static void
113 impl_accessibility_application_set_id (PortableServer_Servant servant,
114                                        const CORBA_long id,
115                                        CORBA_Environment *ev)
116 {
117   SpiApplication *application = SPI_APPLICATION (
118     bonobo_object_from_servant (servant));
119
120   application->id = id;
121 }
122
123 static AtkObject *
124 get_atk_object_ref (GObject *gobject)
125 {
126   AtkObject *aobject;
127
128   if (ATK_IS_IMPLEMENTOR (gobject))
129     {
130       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
131     }
132   else if (ATK_IS_OBJECT (gobject))
133     {
134       aobject = ATK_OBJECT (gobject);
135       g_object_ref (G_OBJECT (aobject));
136     }
137   else
138     {
139       aobject = NULL;
140       g_error ("received event from non-AtkImplementor");
141     }
142
143   return aobject;
144 }
145
146 static gboolean
147 spi_application_object_event_listener (GSignalInvocationHint *signal_hint,
148                                        guint                   n_param_values,
149                                        const GValue           *param_values,
150                                        gpointer                data)
151 {
152   Accessibility_Event e;
153   AtkObject     *aobject;
154   SpiAccessible *source;
155   GSignalQuery   signal_query;
156   gchar         *event_name;
157   const char    *generic_name;
158
159   g_return_val_if_fail (the_app != NULL, FALSE);
160   
161   g_signal_query (signal_hint->signal_id, &signal_query);
162
163   /* TODO: move GTK reference out of app.c into bridge */
164   event_name = g_strdup_printf ("Gtk:%s:%s",
165                                 g_type_name (signal_query.itype),
166                                 signal_query.signal_name);
167
168   generic_name = reverse_lookup_name_for_toolkit_event (event_name);
169
170   fprintf (stderr, "Received (object) signal %s maps to '%s'\n",
171            event_name, generic_name);
172
173   g_free (event_name);
174
175   g_return_val_if_fail (generic_name, FALSE);
176
177   aobject = get_atk_object_ref (g_value_get_object (param_values + 0));
178
179   source = spi_accessible_new (aobject);
180   e.type = CORBA_string_dup (generic_name);
181   e.source = CORBA_OBJECT_NIL;
182   e.detail1 = 0;
183   e.detail2 = 0;
184
185   notify_listeners (the_app->toolkit_listeners, source, &e);
186
187   bonobo_object_unref (BONOBO_OBJECT (source));
188
189   g_object_unref (G_OBJECT (aobject));
190
191   return TRUE;
192 }
193
194 static gboolean
195 spi_application_toolkit_event_listener (GSignalInvocationHint *signal_hint,
196                                         guint                  n_param_values,
197                                         const GValue          *param_values,
198                                         gpointer               data)
199 {
200   Accessibility_Event e;
201   AtkObject      *aobject;
202   SpiAccessible *source;
203   GSignalQuery   signal_query;
204   char          *event_name;
205
206   g_return_val_if_fail (the_app != NULL, FALSE);
207
208   g_signal_query (signal_hint->signal_id, &signal_query);
209
210   /* TODO: move GTK reference out of app.c into bridge */
211   event_name = g_strdup_printf ("Gtk:%s:%s",
212                                 g_type_name (signal_query.itype), 
213                                 signal_query.signal_name);
214
215   fprintf (stderr, "Received signal %s\n", event_name);
216
217   aobject = get_atk_object_ref (g_value_get_object (param_values + 0));
218
219   source = spi_accessible_new (aobject);
220   e.type = CORBA_string_dup (event_name);
221   e.source = CORBA_OBJECT_NIL;
222   e.detail1 = 0;
223   e.detail2 = 0;
224   notify_listeners (the_app->toolkit_listeners, source, &e);
225
226   bonobo_object_unref (BONOBO_OBJECT (source));
227   g_object_unref (G_OBJECT (aobject));
228
229   g_free (event_name);
230
231   return TRUE;
232 }
233
234 static void
235 impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
236                                                                 Accessibility_EventListener listener,
237                                                                 const CORBA_char *event_name,
238                                                                 CORBA_Environment *ev)
239 {
240   guint spi_listener_id;
241   spi_listener_id =
242      atk_add_global_event_listener (spi_application_toolkit_event_listener, event_name);
243   the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
244                                               CORBA_Object_duplicate (listener, ev));
245 #ifdef SPI_DEBUG
246   fprintf (stderr, "registered %d for toolkit events named: %s\n",
247            spi_listener_id,
248            event_name);
249 #endif
250 }
251
252 static void
253 impl_accessibility_application_register_object_event_listener (PortableServer_Servant servant,
254                                                                Accessibility_EventListener listener,
255                                                                const CORBA_char *event_name,
256                                                                CORBA_Environment *ev)
257 {
258   guint spi_listener_id = 0;
259   const char *toolkit_specific_event_name =
260           lookup_toolkit_event_for_name (event_name);
261   if (toolkit_specific_event_name)
262   {
263     spi_listener_id =
264        atk_add_global_event_listener (spi_application_object_event_listener,
265                                       toolkit_specific_event_name);
266     the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
267                                               CORBA_Object_duplicate (listener, ev));
268   }
269 #ifdef SPI_DEBUG
270   fprintf (stderr, "registered %d for object events named: %s\n",
271            spi_listener_id,
272            event_name);
273 #endif
274 }
275
276 static void
277 notify_listeners (GList *listeners, SpiAccessible *source, Accessibility_Event *e)
278 {
279   GList *l;
280   CORBA_Environment ev;
281
282   CORBA_exception_init (&ev);
283
284   for (l = listeners; l; l = l->next)
285     {
286       Accessibility_EventListener listener = l->data;
287
288       e->source = bonobo_object_dup_ref (BONOBO_OBJREF (source), &ev);
289
290       Accessibility_EventListener_notifyEvent (listener, e, &ev);
291       /*
292        * when this (oneway) call completes, the CORBA refcount and
293        * Bonobo_Unknown refcount will be decremented by the recipient
294        */
295       CORBA_exception_free (&ev);
296     }
297 }
298
299 static const char *
300 reverse_lookup_name_for_toolkit_event (char *toolkit_specific_name)
301 {
302     const char *generic_name;
303     SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
304 #ifdef SPI_DEBUG
305     fprintf (stderr, "(reverse lookup) looking for %s in hash table.\n", toolkit_specific_name);
306 #endif
307     generic_name =
308             (const char *) g_hash_table_lookup (klass->generic_event_names, toolkit_specific_name);
309 #ifdef SPI_DEBUG
310     fprintf (stderr, "toolkit event %s converted to %s\n", toolkit_specific_name, generic_name);
311 #endif
312     return generic_name;
313 }
314
315 static void
316 init_toolkit_names (GHashTable **generic_event_names, GHashTable **toolkit_event_names)
317 {
318         *toolkit_event_names = g_hash_table_new (g_str_hash, g_str_equal);
319         *generic_event_names = g_hash_table_new (g_str_hash, g_str_equal);
320         g_hash_table_insert (*toolkit_event_names,
321                              "object:property-change",
322                              "Gtk:AtkObject:property-change");
323         g_hash_table_insert (*generic_event_names,
324                              "Gtk:AtkObject:property-change",
325                              "object:property-change");
326 #ifdef SPI_DEBUG
327         fprintf (stderr, "inserted spi_selection_changed hash\n");
328 #endif
329 }
330
331 static void
332 spi_application_class_init (SpiApplicationClass *klass)
333 {
334   GObjectClass * object_class = (GObjectClass *) klass;
335   POA_Accessibility_Application__epv *epv = &klass->epv;
336
337   spi_application_parent_class = g_type_class_ref (SPI_ACCESSIBLE_TYPE);
338
339   object_class->finalize = spi_accessible_application_finalize;
340
341   epv->_get_toolkitName = impl_accessibility_application_get_toolkit_name;
342   epv->_get_version = impl_accessibility_application_get_version;
343   epv->_get_id = impl_accessibility_application_get_id;
344   epv->_set_id = impl_accessibility_application_set_id;
345   epv->registerToolkitEventListener = impl_accessibility_application_register_toolkit_event_listener;
346   init_toolkit_names (&klass->generic_event_names, &klass->toolkit_event_names);
347 }
348
349 static void
350 spi_application_init (SpiApplication *application)
351 {
352   application->toolkit_listeners = NULL;
353   the_app = application;
354 }
355
356 BONOBO_TYPE_FUNC_FULL (SpiApplication,
357                        Accessibility_Application,
358                        PARENT_TYPE, spi_application);
359
360 SpiApplication *
361 spi_application_new (AtkObject *app_root)
362 {
363   SpiApplication *retval = g_object_new (SPI_APPLICATION_TYPE, NULL);
364
365   spi_base_construct (SPI_BASE (retval), app_root);
366
367   return retval;
368 }