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