2001-11-13 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 /*
24  * application.c: implements SpiApplication.idl
25  *
26  */
27 #include <string.h>
28 #include <config.h>
29 #include <bonobo/Bonobo.h>
30 #include <atk/atkutil.h>
31
32 /*
33  * This pulls the CORBA definitions for the "Accessibility::Accessible" server
34  */
35 #include <libspi/Accessibility.h>
36
37 /*
38  * This pulls the definition for the BonoboObject (GObject Type)
39  */
40 #include "application.h"
41
42 /*
43  * Our parent Gtk object type
44  */
45 #define PARENT_TYPE SPI_ACCESSIBLE_TYPE
46
47 /*
48  * A pointer to our parent object class
49  */
50 static SpiAccessibleClass *spi_application_parent_class;
51
52 static SpiApplication *the_app;
53
54 /* static methods */
55
56 static void notify_listeners (GList *listeners,
57                               Accessibility_Event *e,
58                               CORBA_Environment *ev);
59
60 static char* lookup_toolkit_event_for_name (char *generic_name);
61
62 static char* reverse_lookup_name_for_toolkit_event (char *toolkit_name);
63
64 /*
65  * Implemented GObject::finalize
66  */
67 static void
68 spi_accessible_application_finalize (GObject *object)
69 {
70   /* TODO: any necessary cleanup */
71   (G_OBJECT_CLASS (spi_application_parent_class))->finalize (object);
72 }
73
74 static CORBA_string
75 impl_accessibility_application_get_toolkit_name (PortableServer_Servant servant,
76                                                  CORBA_Environment *ev)
77 {
78   CORBA_char *retval;
79   SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
80   retval = CORBA_string_dup (atk_get_toolkit_name ());
81   return retval;
82 }
83
84 static CORBA_string
85 impl_accessibility_application_get_version (PortableServer_Servant servant,
86                                             CORBA_Environment *ev)
87 {
88   CORBA_char *retval;
89   SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
90   retval = CORBA_string_dup (atk_get_toolkit_version ());
91   return retval;
92 }
93
94 static CORBA_long
95 impl_accessibility_application_get_id (PortableServer_Servant servant,
96                                        CORBA_Environment *ev)
97 {
98   CORBA_long retval;
99   SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
100   retval = (CORBA_long) application->id;
101   return retval;
102 }
103
104 static void
105 impl_accessibility_application_set_id (PortableServer_Servant servant,
106                                        const CORBA_long id,
107                                        CORBA_Environment *ev)
108 {
109   SpiApplication *application = SPI_APPLICATION (bonobo_object_from_servant (servant));
110   application->id = id;
111 }
112
113 #define APP_STATIC_BUFF_SZ 64
114
115 static gboolean
116 spi_application_object_event_listener (GSignalInvocationHint *signal_hint,
117                                    guint n_param_values,
118                                    const GValue *param_values,
119                                    gpointer data)
120 {
121   Accessibility_Event *e = Accessibility_Event__alloc();
122   AtkObject *aobject;
123   GObject *gobject;
124   SpiAccessible *source;
125   CORBA_Environment ev;
126   GSignalQuery signal_query;
127   gchar *name;
128   char sbuf[APP_STATIC_BUFF_SZ];
129   char *generic_name;
130   
131   g_signal_query (signal_hint->signal_id, &signal_query);
132   name = signal_query.signal_name;
133   fprintf (stderr, "Received (object) signal %s:%s\n",
134            g_type_name (signal_query.itype), name);
135
136   /* TODO: move GTK dependency out of app.c into bridge */
137   snprintf(sbuf, APP_STATIC_BUFF_SZ, "Gtk:%s:%s", g_type_name (signal_query.itype), name);
138
139   generic_name = reverse_lookup_name_for_toolkit_event (sbuf);
140   gobject = g_value_get_object (param_values + 0);
141
142   /* notify the actual listeners */
143   if (ATK_IS_IMPLEMENTOR (gobject))
144   {
145     aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
146   }
147   else if (ATK_IS_OBJECT (gobject))
148   {
149     aobject = ATK_OBJECT (gobject);
150     g_object_ref (G_OBJECT (aobject));
151   }
152   else
153   {
154     g_error("received event from non-AtkImplementor");
155   }
156
157   g_return_val_if_fail (generic_name, FALSE);
158   if (generic_name)
159     {
160         source = spi_accessible_new (aobject);
161         e->type = CORBA_string_dup (generic_name);
162         e->source = BONOBO_OBJREF (source);
163         /*
164          * no need to dup this ref, since it's inprocess               
165          * and will be dup'ed by (inprocess) notify_listeners() call below
166          */
167         e->detail1 = 0;
168         e->detail2 = 0;
169         if (the_app) notify_listeners (the_app->toolkit_listeners, e, &ev);
170         /* unref because the in-process notify has called b_o_dup_ref (e->source) */
171         bonobo_object_release_unref (e->source, &ev); 
172     }
173   /* and, decrement the refcount on atkobject, incremented moments ago:
174    *  the call to spi_accessible_new() above should have added an extra ref */
175   g_object_unref (G_OBJECT (aobject));
176
177   return TRUE;
178 }
179
180
181 static gboolean
182 spi_application_toolkit_event_listener (GSignalInvocationHint *signal_hint,
183                                     guint n_param_values,
184                                     const GValue *param_values,
185                                     gpointer data)
186 {
187   Accessibility_Event *e = Accessibility_Event__alloc();
188   AtkObject *aobject;
189   GObject *gobject;
190   SpiAccessible *source;
191   CORBA_Environment ev;
192   GSignalQuery signal_query;
193   gchar *name;
194   char sbuf[APP_STATIC_BUFF_SZ];
195
196   g_signal_query (signal_hint->signal_id, &signal_query);
197   name = signal_query.signal_name;
198   fprintf (stderr, "Received signal %s:%s\n", g_type_name (signal_query.itype), name);
199
200   /* TODO: move GTK dependency out of app.c into bridge */
201   snprintf(sbuf, APP_STATIC_BUFF_SZ, "Gtk:%s:%s", g_type_name (signal_query.itype), name);
202
203   gobject = g_value_get_object (param_values + 0);
204   /* notify the actual listeners */
205   if (ATK_IS_IMPLEMENTOR (gobject))
206     {
207       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
208       source = spi_accessible_new (aobject);
209       e->type = CORBA_string_dup (sbuf);
210       e->source = BONOBO_OBJREF (source);
211       e->detail1 = 0;
212       e->detail2 = 0;
213       if (the_app) notify_listeners (the_app->toolkit_listeners, e, &ev);
214       bonobo_object_unref (source);
215       g_object_unref (G_OBJECT (aobject));
216     }
217   return TRUE;
218 }
219
220 static void
221 impl_accessibility_application_register_toolkit_event_listener (PortableServer_Servant servant,
222                                                                 Accessibility_EventListener listener,
223                                                                 const CORBA_char *event_name,
224                                                                 CORBA_Environment *ev)
225 {
226   guint spi_listener_id;
227   spi_listener_id =
228      atk_add_global_event_listener (spi_application_toolkit_event_listener, event_name);
229   the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
230                                               CORBA_Object_duplicate (listener, ev));
231 #ifdef SPI_DEBUG
232   fprintf (stderr, "registered %d for toolkit events named: %s\n",
233            spi_listener_id,
234            event_name);
235 #endif
236 }
237
238 static void
239 impl_accessibility_application_register_object_event_listener (PortableServer_Servant servant,
240                                                                Accessibility_EventListener listener,
241                                                                const CORBA_char *event_name,
242                                                                CORBA_Environment *ev)
243 {
244   guint spi_listener_id;
245   char *toolkit_specific_event_name = lookup_toolkit_event_for_name (event_name);
246   if (toolkit_specific_event_name)
247   {
248     spi_listener_id =
249        atk_add_global_event_listener (spi_application_object_event_listener,
250                                       CORBA_string_dup (toolkit_specific_event_name));
251     the_app->toolkit_listeners = g_list_append (the_app->toolkit_listeners,
252                                               CORBA_Object_duplicate (listener, ev));
253   }
254 #ifdef SPI_DEBUG
255   fprintf (stderr, "registered %d for object events named: %s\n",
256            spi_listener_id,
257            event_name);
258 #endif
259 }
260
261 static void
262 notify_listeners (GList *listeners, Accessibility_Event *e, CORBA_Environment *ev)
263 {
264     int n_listeners=0;
265     int i;
266     if (listeners) n_listeners = g_list_length (listeners);
267
268     for (i=0; i<n_listeners; ++i) {
269         Accessibility_EventListener listener;
270         e->source = bonobo_object_dup_ref (e->source, ev); 
271         listener = (Accessibility_EventListener) g_list_nth_data (listeners, i);
272         Accessibility_EventListener_notifyEvent (listener, e, ev);
273         /*
274          * when this (oneway) call completes, the CORBA refcount and
275          * Bonobo_Unknown refcount will be decremented by the recipient
276          */
277     }
278 }
279
280 static char *
281 lookup_toolkit_event_for_name (char *generic_name)
282 {
283     char *toolkit_specific_name;
284     SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
285 #ifdef SPI_DEBUG
286     fprintf (stderr, "looking for %s in hash table.\n", generic_name);
287 #endif
288     toolkit_specific_name =
289             (char *) g_hash_table_lookup (klass->toolkit_event_names, generic_name);
290 #ifdef SPI_DEBUG
291     fprintf (stderr, "generic event %s converted to %s\n", generic_name, toolkit_specific_name);
292 #endif
293     return toolkit_specific_name;
294 }
295
296 static char *
297 reverse_lookup_name_for_toolkit_event (char *toolkit_specific_name)
298 {
299     char *generic_name;
300     SpiApplicationClass *klass = g_type_class_peek (SPI_APPLICATION_TYPE);
301 #ifdef SPI_DEBUG
302     fprintf (stderr, "(reverse lookup) looking for %s in hash table.\n", toolkit_specific_name);
303 #endif
304     generic_name =
305             (char *) g_hash_table_lookup (klass->generic_event_names, toolkit_specific_name);
306 #ifdef SPI_DEBUG
307     fprintf (stderr, "toolkit event %s converted to %s\n", toolkit_specific_name, generic_name);
308 #endif
309     return generic_name;
310 }
311
312 static void
313 init_toolkit_names (GHashTable **generic_event_names, GHashTable **toolkit_event_names)
314 {
315         *toolkit_event_names = g_hash_table_new (g_str_hash, g_str_equal);
316         *generic_event_names = g_hash_table_new (g_str_hash, g_str_equal);
317         g_hash_table_insert (*toolkit_event_names,
318                              "object:property-change",
319                              "Gtk:AtkObject:property-change");
320         g_hash_table_insert (*generic_event_names,
321                              "Gtk:AtkObject:property-change",
322                              "object:property-change");
323 #ifdef SPI_DEBUG
324         fprintf (stderr, "inserted spi_selection_changed hash\n");
325 #endif
326 }
327
328 static void
329 spi_application_class_init (SpiApplicationClass *klass)
330 {
331   GObjectClass * object_class = (GObjectClass *) klass;
332   POA_Accessibility_Application__epv *epv = &klass->epv;
333
334   spi_application_parent_class = g_type_class_ref (SPI_ACCESSIBLE_TYPE);
335
336   object_class->finalize = spi_accessible_application_finalize;
337
338   epv->_get_toolkitName = impl_accessibility_application_get_toolkit_name;
339   epv->_get_version = impl_accessibility_application_get_version;
340   epv->_get_id = impl_accessibility_application_get_id;
341   epv->_set_id = impl_accessibility_application_set_id;
342   epv->registerToolkitEventListener = impl_accessibility_application_register_toolkit_event_listener;
343   init_toolkit_names (&klass->generic_event_names, &klass->toolkit_event_names);
344 }
345
346 static void
347 spi_application_init (SpiApplication  *application)
348 {
349   SPI_ACCESSIBLE (application)->atko = g_object_new (atk_object_get_type(), NULL);
350   application->toolkit_listeners = (GList *) NULL;
351   the_app = application;
352 }
353
354 GType
355 spi_application_get_type (void)
356 {
357         static GType type = 0;
358
359         if (!type) {
360                 static const GTypeInfo tinfo = {
361                         sizeof (SpiApplicationClass),
362                         (GBaseInitFunc) NULL,
363                         (GBaseFinalizeFunc) NULL,
364                         (GClassInitFunc) spi_application_class_init,
365                         (GClassFinalizeFunc) NULL,
366                         NULL, /* class data */
367                         sizeof (SpiApplication),
368                         0, /* n preallocs */
369                         (GInstanceInitFunc) spi_application_init,
370                         NULL /* value table */
371                 };
372                 /*
373                  * Bonobo_type_unique auto-generates a load of
374                  * CORBA structures for us. All derived types must
375                  * use bonobo_type_unique.
376                  */
377                 type = bonobo_type_unique (
378                         PARENT_TYPE,
379                         POA_Accessibility_Application__init,
380                         NULL,
381                         G_STRUCT_OFFSET (SpiApplicationClass, epv),
382                         &tinfo,
383                         "SpiApplication");
384         }
385
386         return type;
387 }
388
389 SpiApplication *
390 spi_application_new (AtkObject *app_root)
391 {
392     SpiApplication *retval =
393                SPI_APPLICATION (g_object_new (spi_application_get_type (), NULL));
394     SPI_ACCESSIBLE (retval)->atko = app_root;
395     g_object_ref (G_OBJECT (app_root));
396     return retval;
397 }