Removed po directory from Makefile.am for now.
[platform/core/uifw/at-spi2-atk.git] / at-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 #undef SPI_BRIDGE_DEBUG
36
37 static CORBA_Environment ev;
38 static Accessibility_Registry registry;
39 static SpiApplication *this_app = NULL;
40
41 static gboolean bridge_idle_init (gpointer user_data);
42 static void bridge_focus_tracker (AtkObject *object);
43 static void bridge_exit_func (void);
44 static void register_atk_event_listeners (void);
45 static gboolean bridge_property_event_listener (GSignalInvocationHint *signal_hint,
46                                                 guint n_param_values,
47                                                 const GValue *param_values,
48                                                 gpointer data);
49 static gboolean bridge_state_event_listener (GSignalInvocationHint *signal_hint,
50                                              guint n_param_values,
51                                              const GValue *param_values,
52                                              gpointer data);
53 static gboolean bridge_signal_listener (GSignalInvocationHint *signal_hint,
54                                         guint n_param_values,
55                                         const GValue *param_values,
56                                         gpointer data);
57
58 static gint bridge_key_listener (AtkKeyEventStruct *event,
59                                  gpointer data);
60
61 int
62 gtk_module_init (gint *argc, gchar **argv[])
63 {
64   CORBA_Environment ev;
65
66   if (!bonobo_init (argc, *argv))
67     {
68       g_error ("Could not initialize Bonobo");
69     }
70
71   CORBA_exception_init(&ev);
72
73   registry = bonobo_activation_activate_from_id (
74           "OAFIID:Accessibility_Registry:proto0.1", 0, NULL, &ev);
75   
76   if (ev._major != CORBA_NO_EXCEPTION)
77     {
78       g_error ("Accessibility app error: exception during "
79                "registry activation from id: %s\n",
80                CORBA_exception_id (&ev));
81       CORBA_exception_free (&ev);
82     }
83
84   if (CORBA_Object_is_nil (registry, &ev))
85     {
86       g_error ("Could not locate registry");
87     }
88
89   bonobo_activate ();
90
91   /* Create the accessible application server object */
92
93   this_app = spi_application_new (atk_get_root ());
94
95   fprintf (stderr, "About to register application\n");
96
97   Accessibility_Registry_registerApplication (registry,
98                                               BONOBO_OBJREF (this_app),
99                                               &ev);
100
101   g_atexit (bridge_exit_func);
102
103   g_idle_add (bridge_idle_init, NULL);
104
105   return 0;
106 }
107
108 static gboolean
109 bridge_idle_init (gpointer user_data)
110 {
111   register_atk_event_listeners ();
112
113   fprintf (stderr, "Application registered & listening\n");
114
115   return FALSE;
116 }
117
118 static void
119 register_atk_event_listeners (void)
120 {
121   /*
122    * kludge to make sure the Atk interface types are registered, otherwise
123    * the AtkText signal handlers below won't get registered
124    */
125   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
126   AtkObject *bo = atk_no_op_object_new (ao);
127   
128   /* Register for focus event notifications, and register app with central registry  */
129
130   atk_add_focus_tracker (bridge_focus_tracker);
131   atk_add_global_event_listener (bridge_property_event_listener, "Gtk:AtkObject:property-change");
132   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkObject:children-changed");
133   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkObject:visible-data-changed");
134   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkSelection:selection-changed");
135   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-selection-changed");
136   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-changed");
137   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-caret-moved");
138   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-inserted");
139   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-reordered");
140   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:row-deleted");
141   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-inserted");
142   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-reordered");
143   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:column-deleted");
144   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkTable:model-changed");
145   atk_add_key_event_listener    (bridge_key_listener, NULL);
146
147   g_object_unref (G_OBJECT (bo));
148   g_object_unref (ao);
149 }
150
151 static void
152 bridge_exit_func (void)
153 {
154   BonoboObject *app = (BonoboObject *) this_app;
155
156   fprintf (stderr, "exiting bridge\n");
157
158   if (!app)
159     {
160       return;
161     }
162   this_app = NULL;
163
164   /*
165    *  FIXME: this may be incorrect for apps that do their own bonobo
166    *  shutdown, until we can explicitly shutdown to get the ordering
167    *  right.
168    */
169   if (!bonobo_is_initialized ())
170     {
171       fprintf (stderr, "Re-initializing bonobo\n");
172       g_assert (bonobo_init (0, NULL));
173       g_assert (bonobo_activate ());
174     }
175   
176   Accessibility_Registry_deregisterApplication (
177           registry, BONOBO_OBJREF (app), &ev);
178
179   bonobo_object_release_unref (registry, &ev);
180   
181   bonobo_object_unref (app);
182   
183   fprintf (stderr, "bridge exit func complete.\n");
184
185   if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
186     {
187       g_assert (!bonobo_debug_shutdown ());
188     }
189 }
190
191 static void
192 bridge_focus_tracker (AtkObject *object)
193 {
194   SpiAccessible *source;
195   Accessibility_Event e;
196
197   source = spi_accessible_new (object);
198
199   e.type = "focus:";
200   e.source = BONOBO_OBJREF (source);
201   e.detail1 = 0;
202   e.detail2 = 0;
203
204   Accessibility_Registry_notifyEvent (registry, &e, &ev);
205
206   CORBA_exception_free (&ev);
207 }
208
209 static void
210 emit_eventv (GObject      *gobject,
211              unsigned long detail1,
212              unsigned long detail2,
213              const char   *format, ...)
214 {
215   va_list             args;
216   Accessibility_Event e;
217   SpiAccessible      *source;
218   AtkObject          *aobject;
219 #ifdef SPI_BRIDGE_DEBUG
220   CORBA_string s;
221 #endif
222   
223   va_start (args, format);
224   
225   if (ATK_IS_IMPLEMENTOR (gobject))
226     {
227       aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
228       source  = spi_accessible_new (aobject);
229       g_object_unref (G_OBJECT (aobject));
230     }
231   else if (ATK_IS_OBJECT (gobject))
232     {
233       aobject = ATK_OBJECT (gobject);
234       source  = spi_accessible_new (aobject);
235     }
236   else
237     {
238       aobject = NULL;
239       source  = NULL;
240       g_error ("received property-change event from non-AtkImplementor");
241     }
242
243   if (source != NULL)
244     {
245       e.type = g_strdup_vprintf (format, args);
246       e.source = BONOBO_OBJREF (source);
247       e.detail1 = detail1;
248       e.detail2 = detail2;
249
250 #ifdef SPI_BRIDGE_DEBUG
251       s = Accessibility_Accessible__get_name (BONOBO_OBJREF (source), &ev);
252       g_warning ("Emitting event '%s' (%lu, %lu) on %s",
253                  e.type, e.detail1, e.detail2, s);
254       CORBA_free (s);
255 #endif
256
257       Accessibility_Registry_notifyEvent (registry, &e, &ev);
258
259       CORBA_exception_free (&ev);
260
261       g_free (e.type);
262     }
263
264   va_end (args);
265 }
266
267 static gboolean
268 bridge_property_event_listener (GSignalInvocationHint *signal_hint,
269                                 guint n_param_values,
270                                 const GValue *param_values,
271                                 gpointer data)
272 {
273   AtkPropertyValues *values;
274   GObject *gobject;
275
276 #ifdef SPI_BRIDGE_DEBUG
277   GSignalQuery signal_query;
278   const gchar *name;
279   gchar *s;
280   
281   g_signal_query (signal_hint->signal_id, &signal_query);
282   name = signal_query.signal_name;
283
284   s = atk_object_get_name (ATK_OBJECT (g_value_get_object (param_values + 0)));
285   fprintf (stderr, "Received (property) signal %s:%s from object %s\n",
286            g_type_name (signal_query.itype), name, s);
287 #endif
288
289   gobject = g_value_get_object (param_values + 0);
290   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
291
292   emit_eventv (gobject, 0, 0, "object:property-change:%s", values->property_name);
293
294   return TRUE;
295 }
296
297 static gboolean
298 bridge_state_event_listener (GSignalInvocationHint *signal_hint,
299                              guint n_param_values,
300                              const GValue *param_values,
301                              gpointer data)
302 {
303   GObject *gobject;
304   AtkPropertyValues *values;
305 #ifdef SPI_BRIDGE_DEBUG
306   GSignalQuery signal_query;
307   const gchar *name;
308   
309   g_signal_query (signal_hint->signal_id, &signal_query);
310   name = signal_query.signal_name;
311   fprintf (stderr, "Received (state) signal %s:%s\n",
312            g_type_name (signal_query.itype), name);
313 #endif
314
315   gobject = g_value_get_object (param_values + 0);
316   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
317
318   emit_eventv (gobject, 
319                (unsigned long) values->old_value.data[0].v_ulong,
320                (unsigned long) values->new_value.data[0].v_ulong,
321                "object:%s:?", values->property_name);
322
323   return TRUE;
324 }
325
326 static void
327 accessibility_init_keystroke_from_atk_key_event (Accessibility_KeyStroke *keystroke,
328                                                  AtkKeyEventStruct       *event)
329 {
330 #ifdef SPI_DEBUG
331   if (event)
332     {
333       g_print ("event %c (%d)\n", (int) event->keyval, (int) event->keycode);
334     }
335   else
336 #endif
337   if (!event)
338     {
339       g_print ("WARNING: NULL key event!");
340     }
341   
342   keystroke->keyID     = (CORBA_long) event->keyval;
343   keystroke->keycode   = (CORBA_short) event->keycode;
344   keystroke->timestamp = (CORBA_unsigned_long) event->timestamp;
345   keystroke->modifiers = (CORBA_unsigned_short) (event->state & 0xFFFF);
346
347   switch (event->type)
348     {
349     case (ATK_KEY_EVENT_PRESS):
350       keystroke->type = Accessibility_KEY_PRESSED;
351       break;
352     case (ATK_KEY_EVENT_RELEASE):
353       keystroke->type = Accessibility_KEY_RELEASED;
354       break;
355     default:
356       keystroke->type = 0;
357       break;
358     }
359 }
360
361 static gint
362 bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
363 {
364   CORBA_boolean           result;
365   Accessibility_KeyStroke key_event;
366   Accessibility_DeviceEventController controller =
367     Accessibility_Registry_getDeviceEventController (registry, &ev);
368
369   if (BONOBO_EX (&ev))
370     {
371       CORBA_exception_free (&ev);
372       result = FALSE;
373     }
374   else
375     {
376
377       accessibility_init_keystroke_from_atk_key_event (&key_event, event);
378
379   /* FIXME: this casting is just totaly bogus */
380       result = Accessibility_DeviceEventController_notifyListenersSync (
381         controller, (Accessibility_DeviceEvent *) &key_event, &ev);
382
383       CORBA_exception_free (&ev);
384     }
385
386   return result;
387 }
388
389 static gboolean
390 bridge_signal_listener (GSignalInvocationHint *signal_hint,
391                         guint n_param_values,
392                         const GValue *param_values,
393                         gpointer data)
394 {
395   GObject *gobject;
396   GSignalQuery signal_query;
397   const gchar *name;
398   
399   g_signal_query (signal_hint->signal_id, &signal_query);
400
401   name = signal_query.signal_name;
402
403 #ifdef SPI_BRIDGE_DEBUG
404   fprintf (stderr, "Received signal %s:%s\n",
405            g_type_name (signal_query.itype), name);
406 #endif
407
408   gobject = g_value_get_object (param_values + 0);
409
410   emit_eventv (gobject, 0, 0, "object:%s", name);
411
412   return TRUE;
413 }