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