2002-03-13 Michael Meeks <michael@ximian.com>
[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 void     spi_atk_bridge_exit_func               (void);
42 static void     spi_atk_register_event_listeners       (void);
43 static gboolean spi_atk_bridge_idle_init               (gpointer               user_data);
44 static void     spi_atk_bridge_focus_tracker           (AtkObject             *object);
45 static gboolean spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
46                                                         guint                  n_param_values,
47                                                         const GValue          *param_values,
48                                                         gpointer               data);
49 static gboolean spi_atk_bridge_signal_listener         (GSignalInvocationHint *signal_hint,
50                                                         guint                  n_param_values,
51                                                         const GValue          *param_values,
52                                                         gpointer               data);
53 static gint     spi_atk_bridge_key_listener            (AtkKeyEventStruct     *event,
54                                                         gpointer               data);
55
56 /* For automatic libgnome init */
57 extern void gnome_accessibility_module_init     (void);
58 extern void gnome_accessibility_module_shutdown (void);
59
60 static int     atk_bridge_initialized = FALSE;
61 static guint   atk_bridge_focus_tracker_id = 0;
62 static guint   atk_bridge_key_event_listener_id = 0;
63 static guint   idle_init_id = 0;
64 static GArray *listener_ids = NULL;
65
66 /*
67  *   These exported symbols are hooked by gnome-program
68  * to provide automatic module initialization and shutdown.
69  */
70 extern void gnome_accessibility_module_init     (void);
71 extern void gnome_accessibility_module_shutdown (void);
72
73 static int
74 atk_bridge_init (gint *argc, gchar **argv[])
75 {
76   CORBA_Environment ev;
77
78   if (atk_bridge_initialized)
79     {
80       return 0;
81     }
82   atk_bridge_initialized = TRUE;
83
84   if (!bonobo_init (argc, argv ? *argv : NULL))
85     {
86       g_error ("Could not initialize Bonobo");
87     }
88
89   CORBA_exception_init(&ev);
90
91   registry = bonobo_activation_activate_from_id (
92           "OAFIID:Accessibility_Registry:proto0.1", 0, NULL, &ev);
93   
94   if (ev._major != CORBA_NO_EXCEPTION)
95     {
96       g_error ("Accessibility app error: exception during "
97                "registry activation from id: %s\n",
98                CORBA_exception_id (&ev));
99       CORBA_exception_free (&ev);
100     }
101
102   if (registry == CORBA_OBJECT_NIL)
103     {
104       g_error ("Could not locate registry");
105     }
106
107   bonobo_activate ();
108
109   /* Create the accessible application server object */
110
111   this_app = spi_application_new (atk_get_root ());
112
113   fprintf (stderr, "About to register application\n");
114
115   Accessibility_Registry_registerApplication (registry,
116                                               BONOBO_OBJREF (this_app),
117                                               &ev);
118
119   g_atexit (spi_atk_bridge_exit_func);
120
121   idle_init_id = g_idle_add (spi_atk_bridge_idle_init, NULL);
122
123   return 0;
124 }
125
126 int
127 gtk_module_init (gint *argc, gchar **argv[])
128 {
129         return atk_bridge_init (argc, argv);
130 }
131
132 static gboolean
133 spi_atk_bridge_idle_init (gpointer user_data)
134 {
135   idle_init_id = 0;
136
137   spi_atk_register_event_listeners ();
138
139   fprintf (stderr, "Application registered & listening\n");
140
141   return FALSE;
142 }
143
144 static void
145 add_signal_listener (const char *signal_name)
146 {
147   guint id;
148
149   id = atk_add_global_event_listener (
150     spi_atk_bridge_signal_listener, signal_name);
151
152   g_array_append_val (listener_ids, id);
153 }
154
155 static void
156 spi_atk_register_event_listeners (void)
157 {
158   /*
159    * kludge to make sure the Atk interface types are registered, otherwise
160    * the AtkText signal handlers below won't get registered
161    */
162   guint      id;
163   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
164   AtkObject *bo = atk_no_op_object_new (ao);
165   
166   /* Register for focus event notifications, and register app with central registry  */
167
168   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
169
170   atk_bridge_focus_tracker_id = atk_add_focus_tracker (spi_atk_bridge_focus_tracker);
171
172   id = atk_add_global_event_listener (spi_atk_bridge_property_event_listener,
173                                       "Gtk:AtkObject:property-change");
174   g_array_append_val (listener_ids, id);
175
176   add_signal_listener ("Gtk:AtkObject:children-changed");
177   add_signal_listener ("Gtk:AtkObject:visible-data-changed");
178   add_signal_listener ("Gtk:AtkSelection:selection-changed");
179   add_signal_listener ("Gtk:AtkText:text-selection-changed");
180   add_signal_listener ("Gtk:AtkText:text-changed");
181   add_signal_listener ("Gtk:AtkText:text-caret-moved");
182   add_signal_listener ("Gtk:AtkTable:row-inserted");
183   add_signal_listener ("Gtk:AtkTable:row-reordered");
184   add_signal_listener ("Gtk:AtkTable:row-deleted");
185   add_signal_listener ("Gtk:AtkTable:column-inserted");
186   add_signal_listener ("Gtk:AtkTable:column-reordered");
187   add_signal_listener ("Gtk:AtkTable:column-deleted");
188   add_signal_listener ("Gtk:AtkTable:model-changed");
189 /*
190  * May add the following listeners to implement preemptive key listening for GTK+
191  *
192  * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-press-event");
193  * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-release-event");
194  */
195   atk_bridge_key_event_listener_id = atk_add_key_event_listener (
196     spi_atk_bridge_key_listener, NULL);
197   
198   g_object_unref (G_OBJECT (bo));
199   g_object_unref (ao);
200 }
201
202 static void
203 deregister_application (BonoboObject *app)
204 {
205   Accessibility_Registry_deregisterApplication (
206           registry, BONOBO_OBJREF (app), &ev);
207
208   registry = bonobo_object_release_unref (registry, &ev);
209   
210   app = bonobo_object_unref (app);
211 }
212
213 static void
214 spi_atk_bridge_exit_func (void)
215 {
216   BonoboObject *app = (BonoboObject *) this_app;
217
218   fprintf (stderr, "exiting bridge\n");
219
220   if (!app)
221     {
222       return;
223     }
224   this_app = NULL;
225
226   /*
227    *  FIXME: this may be incorrect for apps that do their own bonobo
228    *  shutdown, until we can explicitly shutdown to get the ordering
229    *  right.
230    */
231   if (!bonobo_is_initialized ())
232     {
233       fprintf (stderr, "Re-initializing bonobo\n");
234       g_assert (bonobo_init (0, NULL));
235       g_assert (bonobo_activate ());
236     }
237   
238   deregister_application (app);
239
240   fprintf (stderr, "bridge exit func complete.\n");
241
242   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
243     {
244       g_assert (!bonobo_debug_shutdown ());
245     }
246 }
247
248 void
249 gnome_accessibility_module_init (void)
250 {
251   atk_bridge_init (NULL, NULL);
252
253   g_print("Atk Accessibilty bridge initialized\n");
254 }
255
256 void
257 gnome_accessibility_module_shutdown (void)
258 {
259   BonoboObject *app = (BonoboObject *) this_app;
260
261   if (!atk_bridge_initialized)
262     {
263       return;
264     }
265   atk_bridge_initialized = FALSE;
266   this_app = NULL;
267
268   g_print("Atk Accessibilty bridge shutdown\n");
269
270   if (idle_init_id)
271     {
272       g_source_remove (idle_init_id);
273       idle_init_id = 0;
274     }
275   else
276     {
277       int     i;
278       GArray *ids = listener_ids;
279
280       listener_ids = NULL;
281       atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
282       
283       for (i = 0; ids && i < ids->len; i++)
284         {
285           atk_remove_global_event_listener (g_array_index (ids, guint, i));
286         }
287
288       atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
289     }
290
291   deregister_application (app);
292 }
293
294 static void
295 spi_atk_bridge_focus_tracker (AtkObject *object)
296 {
297   SpiAccessible *source;
298   Accessibility_Event e;
299
300   source = spi_accessible_new (object);
301
302   e.type = "focus:";
303   e.source = BONOBO_OBJREF (source);
304   e.detail1 = 0;
305   e.detail2 = 0;
306
307   Accessibility_Registry_notifyEvent (registry, &e, &ev);
308
309   CORBA_exception_free (&ev);
310 }
311
312 static void
313 spi_atk_emit_eventv (GObject      *gobject,
314                      unsigned long detail1,
315                      unsigned long detail2,
316                      const char   *format, ...)
317 {
318   va_list             args;
319   Accessibility_Event e;
320   SpiAccessible      *source;
321   AtkObject          *aobject;
322 #ifdef SPI_BRIDGE_DEBUG
323   CORBA_string s;
324 #endif
325   
326   va_start (args, format);
327   
328   if (ATK_IS_IMPLEMENTOR (gobject))
329     {
330       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
331       source  = spi_accessible_new (aobject);
332       g_object_unref (G_OBJECT (aobject));
333     }
334   else if (ATK_IS_OBJECT (gobject))
335     {
336       aobject = ATK_OBJECT (gobject);
337       source  = spi_accessible_new (aobject);
338     }
339   else
340     {
341       aobject = NULL;
342       source  = NULL;
343       g_error ("received property-change event from non-AtkImplementor");
344     }
345
346   if (source != NULL)
347     {
348       e.type = g_strdup_vprintf (format, args);
349       e.source = BONOBO_OBJREF (source);
350       e.detail1 = detail1;
351       e.detail2 = detail2;
352
353 #ifdef SPI_BRIDGE_DEBUG
354       s = Accessibility_Accessible__get_name (BONOBO_OBJREF (source), &ev);
355       g_warning ("Emitting event '%s' (%lu, %lu) on %s",
356                  e.type, e.detail1, e.detail2, s);
357       CORBA_free (s);
358 #endif
359
360       Accessibility_Registry_notifyEvent (registry, &e, &ev);
361
362       CORBA_exception_free (&ev);
363
364       g_free (e.type);
365     }
366
367   va_end (args);
368 }
369
370 static gboolean
371 spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
372                                         guint n_param_values,
373                                         const GValue *param_values,
374                                         gpointer data)
375 {
376   AtkPropertyValues *values;
377   GObject *gobject;
378
379 #ifdef SPI_BRIDGE_DEBUG
380   GSignalQuery signal_query;
381   const gchar *name;
382   gchar *s, *s2;
383   
384   g_signal_query (signal_hint->signal_id, &signal_query);
385   name = signal_query.signal_name;
386
387   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
388   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
389   fprintf (stderr, "Received (property) signal %s:%s from object %s (gail %s)\n",
390            g_type_name (signal_query.itype), name, s, s2);
391 #endif
392
393   gobject = g_value_get_object (param_values + 0);
394   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
395
396   spi_atk_emit_eventv (gobject, 0, 0, "object:property-change:%s", values->property_name);
397
398   return TRUE;
399 }
400
401 #if THIS_WILL_EVER_BE_USED
402 static gboolean
403 spi_atk_bridge_state_event_listener (GSignalInvocationHint *signal_hint,
404                                      guint n_param_values,
405                                      const GValue *param_values,
406                                      gpointer data)
407 {
408   GObject *gobject;
409   AtkPropertyValues *values;
410 #ifdef SPI_BRIDGE_DEBUG
411   GSignalQuery signal_query;
412   const gchar *name;
413   
414   g_signal_query (signal_hint->signal_id, &signal_query);
415   name = signal_query.signal_name;
416   fprintf (stderr, "Received (state) signal %s:%s\n",
417            g_type_name (signal_query.itype), name);
418 #endif
419
420   gobject = g_value_get_object (param_values + 0);
421   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
422
423   spi_atk_emit_eventv (gobject, 
424                        (unsigned long) values->old_value.data[0].v_ulong,
425                        (unsigned long) values->new_value.data[0].v_ulong,
426                        "object:%s:?", values->property_name);
427   
428   return TRUE;
429 }
430 #endif
431
432 static void
433 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent  *keystroke,
434                                        AtkKeyEventStruct          *event)
435 {
436 #ifdef SPI_DEBUG
437   if (event)
438     {
439       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
440     }
441   else
442 #endif
443   if (!event)
444     {
445       g_print ("WARNING: NULL key event!");
446     }
447   
448   keystroke->id        = (CORBA_long) event->keyval;
449   keystroke->hw_code   = (CORBA_short) event->keycode;
450   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
451   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
452   if (event->string)
453     {
454       keystroke->event_string = CORBA_string_dup (event->string);
455       keystroke->is_text = CORBA_TRUE;
456     }
457   else
458     {
459       keystroke->event_string = CORBA_string_dup ("");
460       keystroke->is_text = CORBA_FALSE;
461     }
462   switch (event->type)
463     {
464     case (ATK_KEY_EVENT_PRESS):
465       keystroke->type = Accessibility_KEY_PRESSED;
466       break;
467     case (ATK_KEY_EVENT_RELEASE):
468       keystroke->type = Accessibility_KEY_RELEASED;
469       break;
470     default:
471       keystroke->type = 0;
472       break;
473     }
474 #if 0  
475   g_print ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
476            (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
477            (int) keystroke->modifiers,
478            keystroke->event_string, (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
479 #endif
480 }
481
482 static gint
483 spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
484 {
485   CORBA_boolean             result;
486   Accessibility_DeviceEvent key_event;
487   Accessibility_DeviceEventController controller =
488     Accessibility_Registry_getDeviceEventController (registry, &ev);
489
490   if (BONOBO_EX (&ev))
491     {
492       g_warning ("failure: no deviceeventcontroller found\n");
493       CORBA_exception_free (&ev);
494       result = FALSE;
495     }
496   else
497     {
498
499       spi_init_keystroke_from_atk_key_event (&key_event, event);
500
501       result = Accessibility_DeviceEventController_notifyListenersSync (
502         controller, &key_event, &ev);
503
504       CORBA_exception_free (&ev);
505     }
506
507   return result;
508 }
509
510 static gboolean
511 spi_atk_bridge_signal_listener (GSignalInvocationHint *signal_hint,
512                                 guint n_param_values,
513                                 const GValue *param_values,
514                                 gpointer data)
515 {
516   GObject *gobject;
517   GSignalQuery signal_query;
518   const gchar *name;
519   gint detail1 = 0, detail2 = 0;
520 #ifdef SPI_BRIDGE_DEBUG
521   gchar *s, *s2;
522 #endif
523   
524   g_signal_query (signal_hint->signal_id, &signal_query);
525
526   name = signal_query.signal_name;
527
528 #ifdef SPI_BRIDGE_DEBUG
529   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
530   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
531   fprintf (stderr, "Received signal %s:%s from object %s (gail %s)\n",
532            g_type_name (signal_query.itype), name, s, s2);
533 #endif
534
535   gobject = g_value_get_object (param_values + 0);
536   if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
537     detail1 = g_value_get_int (param_values + 1);
538   if (G_VALUE_TYPE (param_values + 2) == G_TYPE_INT)
539     detail2 = g_value_get_int (param_values + 2);
540   
541   spi_atk_emit_eventv (gobject, detail1, detail2, "object:%s", name);
542
543   return TRUE;
544 }