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