Changes to event notification, to fix bugs associated with the use of oneways.
[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   Accessibility_Accessible_unref (e.source, &ev);
333   
334   CORBA_exception_free (&ev);
335 }
336
337 static void
338 spi_atk_emit_eventv (GObject      *gobject,
339                      unsigned long detail1,
340                      unsigned long detail2,
341                      const char   *format, ...)
342 {
343   va_list             args;
344   Accessibility_Event e;
345   SpiAccessible      *source;
346   AtkObject          *aobject;
347 #ifdef SPI_BRIDGE_DEBUG
348   CORBA_string s;
349 #endif
350   
351   va_start (args, format);
352   
353   if (ATK_IS_IMPLEMENTOR (gobject))
354     {
355       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
356       source  = spi_accessible_new (aobject);
357       g_object_unref (G_OBJECT (aobject));
358     }
359   else if (ATK_IS_OBJECT (gobject))
360     {
361       aobject = ATK_OBJECT (gobject);
362       source  = spi_accessible_new (aobject);
363     }
364   else
365     {
366       aobject = NULL;
367       source  = NULL;
368       g_error ("received property-change event from non-AtkImplementor");
369     }
370
371   if (source != NULL)
372     {
373       e.type = g_strdup_vprintf (format, args);
374       e.source = BONOBO_OBJREF (source);
375       e.detail1 = detail1;
376       e.detail2 = detail2;
377
378 #ifdef SPI_BRIDGE_DEBUG
379       s = Accessibility_Accessible__get_name (BONOBO_OBJREF (source), &ev);
380       g_warning ("Emitting event '%s' (%lu, %lu) on %s",
381                  e.type, e.detail1, e.detail2, s);
382       CORBA_free (s);
383 #endif
384
385       Accessibility_Registry_notifyEvent (registry, &e, &ev);
386       Accessibility_Accessible_unref (e.source, &ev);
387
388       CORBA_exception_free (&ev);
389
390       g_free (e.type);
391     }
392
393   va_end (args);
394
395 }
396
397 static gboolean
398 spi_atk_bridge_property_event_listener (GSignalInvocationHint *signal_hint,
399                                         guint n_param_values,
400                                         const GValue *param_values,
401                                         gpointer data)
402 {
403   AtkPropertyValues *values;
404   GObject *gobject;
405
406 #ifdef SPI_BRIDGE_DEBUG
407   GSignalQuery signal_query;
408   const gchar *name;
409   const gchar *s, *s2;
410   
411   g_signal_query (signal_hint->signal_id, &signal_query);
412   name = signal_query.signal_name;
413
414   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
415   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
416   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
417   fprintf (stderr, "Received (property) signal %s:%s:%s from object %s (gail %s)\n",
418            g_type_name (signal_query.itype), name, values->property_name, s, s2);
419   
420 #endif
421
422   gobject = g_value_get_object (param_values + 0);
423   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
424
425   spi_atk_emit_eventv (gobject, 0, 0, "object:property-change:%s", values->property_name);
426
427   return TRUE;
428 }
429
430 static gboolean
431 spi_atk_bridge_state_event_listener (GSignalInvocationHint *signal_hint,
432                                      guint n_param_values,
433                                      const GValue *param_values,
434                                      gpointer data)
435 {
436   GObject *gobject;
437   gchar *property_name;
438   gchar *type;
439   unsigned long detail1;
440 #ifdef SPI_BRIDGE_DEBUG
441   GSignalQuery signal_query;
442   const gchar *name;
443   
444   g_signal_query (signal_hint->signal_id, &signal_query);
445   name = signal_query.signal_name;
446   fprintf (stderr, "Received (state) signal %s:%s\n",
447            g_type_name (signal_query.itype), name);
448 #endif
449
450   gobject = g_value_get_object (param_values + 0);
451   property_name = g_strdup (g_value_get_string (param_values + 1));
452   detail1 = (g_value_get_boolean (param_values + 2))
453     ? 1 : 0;
454   type = g_strdup_printf ("object:state-changed:%s", property_name);
455   spi_atk_emit_eventv (gobject, 
456                        detail1,
457                        0,
458                        type);
459   g_free (property_name);
460   g_free (type);
461   return TRUE;
462 }
463
464
465 static void
466 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent  *keystroke,
467                                        AtkKeyEventStruct          *event)
468 {
469 #ifdef SPI_DEBUG
470   if (event)
471     {
472       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
473     }
474   else
475 #endif
476   if (!event)
477     {
478       g_print ("WARNING: NULL key event!");
479     }
480   
481   keystroke->id        = (CORBA_long) event->keyval;
482   keystroke->hw_code   = (CORBA_short) event->keycode;
483   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
484   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
485   if (event->string)
486     {
487       keystroke->event_string = CORBA_string_dup (event->string);
488       keystroke->is_text = CORBA_TRUE;
489     }
490   else
491     {
492       keystroke->event_string = CORBA_string_dup ("");
493       keystroke->is_text = CORBA_FALSE;
494     }
495   switch (event->type)
496     {
497     case (ATK_KEY_EVENT_PRESS):
498       keystroke->type = Accessibility_KEY_PRESSED_EVENT;
499       break;
500     case (ATK_KEY_EVENT_RELEASE):
501       keystroke->type = Accessibility_KEY_RELEASED_EVENT;
502       break;
503     default:
504       keystroke->type = 0;
505       break;
506     }
507 #if 0  
508   g_print ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
509            (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
510            (int) keystroke->modifiers,
511            keystroke->event_string, (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
512 #endif
513 }
514
515 static gint
516 spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
517 {
518   CORBA_boolean             result;
519   Accessibility_DeviceEvent key_event;
520   Accessibility_DeviceEventController controller;
521         
522   if (BONOBO_EX (&ev))
523         g_warning ("failure: pre-listener get dec\n");
524
525   controller =
526     Accessibility_Registry_getDeviceEventController (registry, &ev);
527
528   if (BONOBO_EX (&ev))
529     {
530       g_warning ("failure: no deviceeventcontroller found\n");
531       CORBA_exception_free (&ev);
532       result = FALSE;
533     }
534   else
535     {
536
537       spi_init_keystroke_from_atk_key_event (&key_event, event);
538
539       result = Accessibility_DeviceEventController_notifyListenersSync (
540         controller, &key_event, &ev);
541
542       CORBA_exception_free (&ev);
543     }
544
545   return result;
546 }
547
548 static gboolean
549 spi_atk_bridge_signal_listener (GSignalInvocationHint *signal_hint,
550                                 guint n_param_values,
551                                 const GValue *param_values,
552                                 gpointer data)
553 {
554   GObject *gobject;
555   GSignalQuery signal_query;
556   const gchar *name;
557   gint detail1 = 0, detail2 = 0;
558 #ifdef SPI_BRIDGE_DEBUG
559   const gchar *s, *s2;
560 #endif
561   
562   g_signal_query (signal_hint->signal_id, &signal_query);
563
564   name = signal_query.signal_name;
565
566 #ifdef SPI_BRIDGE_DEBUG
567   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
568   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
569   fprintf (stderr, "Received signal %s:%s from object %s (gail %s)\n",
570            g_type_name (signal_query.itype), name, s ? s : "<NULL>" , s2);
571 #endif
572
573   gobject = g_value_get_object (param_values + 0);
574   if (G_VALUE_TYPE (param_values + 1) == G_TYPE_INT)
575     detail1 = g_value_get_int (param_values + 1);
576   if (G_VALUE_TYPE (param_values + 2) == G_TYPE_INT)
577     detail2 = g_value_get_int (param_values + 2);
578   
579   spi_atk_emit_eventv (gobject, detail1, detail2, "object:%s", name);
580
581   return TRUE;
582 }
583
584
585
586 static gboolean
587 spi_atk_bridge_window_event_listener (GSignalInvocationHint *signal_hint,
588                                 guint n_param_values,
589                                 const GValue *param_values,
590                                 gpointer data)
591 {
592   GObject *gobject;
593   GSignalQuery signal_query;
594   const gchar *name;
595 #ifdef SPI_BRIDGE_DEBUG
596   const gchar *s, *s2;
597 #endif
598   
599   g_signal_query (signal_hint->signal_id, &signal_query);
600
601   name = signal_query.signal_name;
602
603 #ifdef SPI_BRIDGE_DEBUG
604   s2 = g_type_name (G_OBJECT_TYPE (g_value_get_object (param_values + 0)));
605   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
606   fprintf (stderr, "Received signal %s:%s from object %s (gail %s)\n",
607            g_type_name (signal_query.itype), name, s ? s : "<NULL>" , s2);
608 #endif
609
610   gobject = g_value_get_object (param_values + 0);
611   spi_atk_emit_eventv (gobject, 0, 0, "window:%s", name);
612
613   return TRUE;
614 }