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