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