e4e8f7ebacc4de8c5a5ec843a41125b06ed1a4ff
[platform/core/uifw/at-spi2-atk.git] / atk-bridge / bridge.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 <stdarg.h>
26 #include <libbonobo.h>
27 #include <orbit/orbit.h>
28 #include <atk/atk.h>
29 #include <atk/atkobject.h>
30 #include <atk/atknoopobject.h>
31 #include <libspi/Accessibility.h>
32 #include "accessible.h"
33 #include "application.h"
34
35 #undef SPI_BRIDGE_DEBUG
36
37 static CORBA_Environment ev;
38 static Accessibility_Registry registry;
39 static SpiApplication *this_app = NULL;
40
41 static gboolean bridge_idle_init (gpointer user_data);
42 static void bridge_focus_tracker (AtkObject *object);
43 static void bridge_exit_func (void);
44 static void register_atk_event_listeners (void);
45 static gboolean bridge_property_event_listener (GSignalInvocationHint *signal_hint,
46                                                 guint n_param_values,
47                                                 const GValue *param_values,
48                                                 gpointer data);
49 static gboolean bridge_state_event_listener (GSignalInvocationHint *signal_hint,
50                                              guint n_param_values,
51                                              const GValue *param_values,
52                                              gpointer data);
53 static gboolean bridge_signal_listener (GSignalInvocationHint *signal_hint,
54                                         guint n_param_values,
55                                         const GValue *param_values,
56                                         gpointer data);
57
58 static gint bridge_key_listener (AtkKeyEventStruct *event,
59                                  gpointer data);
60
61 int
62 gtk_module_init (gint *argc, gchar **argv[])
63 {
64   CORBA_Environment ev;
65
66   if (!bonobo_init (argc, *argv))
67     {
68       g_error ("Could not initialize Bonobo");
69     }
70
71   CORBA_exception_init(&ev);
72
73   registry = bonobo_activation_activate_from_id (
74           "OAFIID:Accessibility_Registry:proto0.1", 0, NULL, &ev);
75   
76   if (ev._major != CORBA_NO_EXCEPTION)
77     {
78       g_error ("Accessibility app error: exception during "
79                "registry activation from id: %s\n",
80                CORBA_exception_id (&ev));
81       CORBA_exception_free (&ev);
82     }
83
84   if (CORBA_Object_is_nil (registry, &ev))
85     {
86       g_error ("Could not locate registry");
87     }
88
89   bonobo_activate ();
90
91   /* Create the accessible application server object */
92
93   this_app = spi_application_new (atk_get_root ());
94
95   fprintf (stderr, "About to register application\n");
96
97   Accessibility_Registry_registerApplication (registry,
98                                               BONOBO_OBJREF (this_app),
99                                               &ev);
100
101   g_atexit (bridge_exit_func);
102
103   g_idle_add (bridge_idle_init, NULL);
104
105   return 0;
106 }
107
108 static gboolean
109 bridge_idle_init (gpointer user_data)
110 {
111   register_atk_event_listeners ();
112
113   fprintf (stderr, "Application registered & listening\n");
114
115   return FALSE;
116 }
117
118 static void
119 register_atk_event_listeners (void)
120 {
121   /*
122    * kludge to make sure the Atk interface types are registered, otherwise
123    * the AtkText signal handlers below won't get registered
124    */
125   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
126   AtkObject *bo = atk_no_op_object_new (ao);
127   
128   /* Register for focus event notifications, and register app with central registry  */
129
130   atk_add_focus_tracker (bridge_focus_tracker);
131   atk_add_global_event_listener (bridge_property_event_listener, "Gtk:AtkObject:property-change");
132   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkObject:children-changed");
133   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkObject:visible-data-changed");
134   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkSelection:selection-changed");
135   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-selection-changed");
136   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-changed");
137   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-caret-moved");
138   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-inserted");
139   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-reordered");
140   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-deleted");
141   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-inserted");
142   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-reordered");
143   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-deleted");
144   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:model-changed");
145   atk_add_key_event_listener    (bridge_key_listener, NULL);
146
147   g_object_unref (G_OBJECT (bo));
148   g_object_unref (ao);
149 }
150
151 static void
152 bridge_exit_func (void)
153 {
154   BonoboObject *app = (BonoboObject *) this_app;
155
156   fprintf (stderr, "exiting bridge\n");
157
158   if (!app)
159     {
160       return;
161     }
162   this_app = NULL;
163
164   /*
165    *  FIXME: this may be incorrect for apps that do their own bonobo
166    *  shutdown, until we can explicitly shutdown to get the ordering
167    *  right.
168    */
169   if (!bonobo_is_initialized ())
170     {
171       fprintf (stderr, "Re-initializing bonobo\n");
172       g_assert (bonobo_init (0, NULL));
173       g_assert (bonobo_activate ());
174     }
175   
176   Accessibility_Registry_deregisterApplication (
177           registry, BONOBO_OBJREF (app), &ev);
178
179   bonobo_object_release_unref (registry, &ev);
180   
181   bonobo_object_unref (app);
182   
183   fprintf (stderr, "bridge exit func complete.\n");
184
185   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
186     {
187       g_assert (!bonobo_debug_shutdown ());
188     }
189 }
190
191 static void
192 bridge_focus_tracker (AtkObject *object)
193 {
194   SpiAccessible *source;
195   Accessibility_Event e;
196
197   source = spi_accessible_new (object);
198
199   e.type = "focus:";
200   e.source = BONOBO_OBJREF (source);
201   e.detail1 = 0;
202   e.detail2 = 0;
203
204   Accessibility_Registry_notifyEvent (registry, &e, &ev);
205
206   CORBA_exception_free (&ev);
207 }
208
209 static void
210 emit_eventv (GObject      *gobject,
211              unsigned long detail1,
212              unsigned long detail2,
213              const char   *format, ...)
214 {
215   va_list             args;
216   Accessibility_Event e;
217   SpiAccessible      *source;
218   AtkObject          *aobject;
219
220   va_start (args, format);
221   
222   if (ATK_IS_IMPLEMENTOR (gobject))
223     {
224       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
225       source  = spi_accessible_new (aobject);
226       g_object_unref (G_OBJECT (aobject));
227     }
228   else if (ATK_IS_OBJECT (gobject))
229     {
230       aobject = ATK_OBJECT (gobject);
231       source  = spi_accessible_new (aobject);
232     }
233   else
234     {
235       aobject = NULL;
236       source  = NULL;
237       g_error ("received property-change event from non-AtkImplementor");
238     }
239
240   if (source != NULL)
241     {
242       e.type = g_strdup_vprintf (format, args);
243       e.source = BONOBO_OBJREF (source);
244       e.detail1 = detail1;
245       e.detail2 = detail2;
246
247 #ifdef SPI_BRIDGE_DEBUG
248       g_warning ("Emitting event '%s' (%d, %d) on %p",
249                  e.type, e.detail1, e.detail2, source);
250 #endif
251
252       Accessibility_Registry_notifyEvent (registry, &e, &ev);
253
254       CORBA_exception_free (&ev);
255
256       g_free (e.type);
257     }
258
259   va_end (args);
260 }
261
262 static gboolean
263 bridge_property_event_listener (GSignalInvocationHint *signal_hint,
264                                 guint n_param_values,
265                                 const GValue *param_values,
266                                 gpointer data)
267 {
268   AtkPropertyValues *values;
269   GObject *gobject;
270
271 #ifdef SPI_BRIDGE_DEBUG
272   GSignalQuery signal_query;
273   const gchar *name;
274   
275   g_signal_query (signal_hint->signal_id, &signal_query);
276   name = signal_query.signal_name;
277
278   fprintf (stderr, "Received (property) signal %s:%s\n",
279            g_type_name (signal_query.itype), name);
280 #endif
281
282   gobject = g_value_get_object (param_values + 0);
283   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
284
285   emit_eventv (gobject, 0, 0, "object:property-change:%s", values->property_name);
286
287   return TRUE;
288 }
289
290 static gboolean
291 bridge_state_event_listener (GSignalInvocationHint *signal_hint,
292                              guint n_param_values,
293                              const GValue *param_values,
294                              gpointer data)
295 {
296   GObject *gobject;
297   AtkPropertyValues *values;
298 #ifdef SPI_BRIDGE_DEBUG
299   GSignalQuery signal_query;
300   const gchar *name;
301   
302   g_signal_query (signal_hint->signal_id, &signal_query);
303   name = signal_query.signal_name;
304   fprintf (stderr, "Received (state) signal %s:%s\n",
305            g_type_name (signal_query.itype), name);
306 #endif
307
308   gobject = g_value_get_object (param_values + 0);
309   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
310
311   emit_eventv (gobject, 
312                (unsigned long) values->old_value.data[0].v_ulong,
313                (unsigned long) values->new_value.data[0].v_ulong,
314                "object:%s:?", values->property_name);
315
316   return TRUE;
317 }
318
319 static void
320 accessibility_init_keystroke_from_atk_key_event (Accessibility_KeyStroke *keystroke,
321                                                  AtkKeyEventStruct       *event)
322 {
323 #ifdef SPI_DEBUG
324   if (event)
325     {
326       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
327     }
328   else
329 #endif
330   if (!event)
331     {
332       g_print ("WARNING: NULL key event!");
333     }
334   
335   keystroke->keyID     = (CORBA_long) event->keyval;
336   keystroke->keycode   = (CORBA_short) event->keycode;
337   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
338   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
339
340   switch (event->type)
341     {
342     case (ATK_KEY_EVENT_PRESS):
343       keystroke->type = Accessibility_KEY_PRESSED;
344       break;
345     case (ATK_KEY_EVENT_RELEASE):
346       keystroke->type = Accessibility_KEY_RELEASED;
347       break;
348     default:
349       keystroke->type = 0;
350       break;
351     }
352 }
353
354 static gint
355 bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
356 {
357   CORBA_boolean           result;
358   Accessibility_KeyStroke key_event;
359   Accessibility_DeviceEventController controller =
360     Accessibility_Registry_getDeviceEventController (registry, &ev);
361
362   if (BONOBO_EX (&ev))
363     {
364       CORBA_exception_free (&ev);
365       result = FALSE;
366     }
367   else
368     {
369
370       accessibility_init_keystroke_from_atk_key_event (&key_event, event);
371
372   /* FIXME: this casting is just totaly bogus */
373       result = Accessibility_DeviceEventController_notifyListenersSync (
374         controller, (Accessibility_DeviceEvent *) &key_event, &ev);
375
376       CORBA_exception_free (&ev);
377     }
378
379   return result;
380 }
381
382 static gboolean
383 bridge_signal_listener (GSignalInvocationHint *signal_hint,
384                         guint n_param_values,
385                         const GValue *param_values,
386                         gpointer data)
387 {
388   GObject *gobject;
389   GSignalQuery signal_query;
390   const gchar *name;
391   
392   g_signal_query (signal_hint->signal_id, &signal_query);
393
394   name = signal_query.signal_name;
395
396 #ifdef SPI_BRIDGE_DEBUG
397   fprintf (stderr, "Received signal %s:%s\n",
398            g_type_name (signal_query.itype), name);
399 #endif
400
401   gobject = g_value_get_object (param_values + 0);
402
403   emit_eventv (gobject, 0, 0, "%s:%s", name, g_type_name (signal_query.itype));
404
405   return TRUE;
406 }