Added additional documentation and fixed a couple of latent bugs.
[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 <libbonobo.h>
26 #include <orbit/orbit.h>
27 #include <atk/atk.h>
28 #include <atk/atkobject.h>
29 #include <libspi/Accessibility.h>
30 #include "accessible.h"
31 #include "application.h"
32
33 #define SPI_BRIDGE_DEBUG 1
34
35 #define APP_STATIC_BUFF_SZ 64
36
37 typedef struct _ArgStruct ArgStruct;
38
39 struct _ArgStruct {
40   gint c;
41   char **v;
42 };
43
44 static CORBA_Environment ev;
45 static Accessibility_Registry registry;
46 static SpiApplication *this_app;
47
48 static gboolean bridge_register_app (gpointer p);
49 static void bridge_focus_tracker (AtkObject *object);
50 static void bridge_exit_func(void);
51 static gboolean bridge_register_event_listener ();
52 static void register_atk_event_listeners();
53 static gboolean bridge_property_event_listener (GSignalInvocationHint *signal_hint,
54                                                 guint n_param_values,
55                                                 const GValue *param_values,
56                                                 gpointer data);
57 static gboolean bridge_signal_listener (GSignalInvocationHint *signal_hint,
58                                         guint n_param_values,
59                                         const GValue *param_values,
60                                         gpointer data);
61
62 static gint bridge_key_listener (AtkImplementor *atk_impl,
63                                  AtkKeyEventStruct *event,
64                                  gpointer data);
65
66 int
67 gtk_module_init(gint *argc, gchar **argv[])
68 {
69   ArgStruct *args = (ArgStruct *) g_new0(ArgStruct, 1);
70   args->c = *argc;
71   args->v = *argv;
72   g_idle_add (bridge_register_app, args);
73   g_atexit (bridge_exit_func);
74 }
75
76 static gboolean
77 bridge_register_app (gpointer gp)
78 {
79   CORBA_Object oclient;
80   AtkObject *atko;
81   char *obj_id;
82   ArgStruct *args = (ArgStruct *)gp;
83
84   CORBA_exception_init(&ev);
85
86   if (!bonobo_init (&(args->c), args->v))
87     {
88       g_error ("Could not initialize Bonobo");
89     }
90
91   /* Create the accesssible application server object */
92   this_app = spi_application_new(atk_get_root ());
93
94   obj_id = "OAFIID:Accessibility_Registry:proto0.1";
95
96   oclient = bonobo_activation_activate_from_id (obj_id, 0, NULL, &ev);
97   if (ev._major != CORBA_NO_EXCEPTION) {
98     fprintf(stderr,
99             ("Accessibility app error: exception during registry activation from id: %s\n"),
100             CORBA_exception_id(&ev));
101     CORBA_exception_free(&ev);
102     exit(-1);
103   }
104
105   if (CORBA_Object_is_nil (oclient, &ev))
106     {
107       g_error ("Could not locate registry");
108     }
109
110   registry = (Accessibility_Registry) oclient;
111
112   fprintf(stderr, "About to register application\n");
113
114   bonobo_activate ();
115
116   Accessibility_Registry_registerApplication (registry,
117                                               CORBA_Object_duplicate (BONOBO_OBJREF (this_app), &ev),
118                                               &ev);
119
120   register_atk_event_listeners ();
121
122   return FALSE;
123 }
124
125 static void
126 register_atk_event_listeners ()
127 {
128   /* Register for focus event notifications, and register app with central registry  */
129
130 /* kludge to make sure the Atk interface types are registered, otherwise
131    the AtkText signal handlers below won't get registered */
132
133   ATK_TYPE_TEXT;
134
135   atk_add_focus_tracker (bridge_focus_tracker);
136   atk_add_global_event_listener (bridge_property_event_listener, "Gtk:AtkObject:property-change");
137   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkObject:children-changed");
138   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-changed");
139   atk_add_global_event_listener (bridge_signal_listener, "Gtk:AtkText:text-caret-moved");
140   atk_add_key_event_listener    (bridge_key_listener, NULL);
141 }
142
143 static void bridge_exit_func()
144 {
145   fprintf (stderr, "exiting bridge\n");
146   Accessibility_Registry_deregisterApplication (registry,
147                                                 CORBA_Object_duplicate (BONOBO_OBJREF (this_app), &ev),
148                                                 &ev);
149   fprintf (stderr, "bridge exit func complete.\n");
150 }
151
152 static void bridge_focus_tracker (AtkObject *object)
153 {
154   Accessibility_Event *e = Accessibility_Event__alloc();
155   e->type = CORBA_string_dup ("focus:");
156   e->source = CORBA_Object_duplicate (BONOBO_OBJREF (spi_accessible_new (object)), &ev);
157   e->detail1 = 0;
158   e->detail2 = 0;
159   Accessibility_Registry_notifyEvent (registry, e, &ev);
160 }
161
162 static gboolean
163 bridge_property_event_listener (GSignalInvocationHint *signal_hint,
164                                 guint n_param_values,
165                                 const GValue *param_values,
166                                 gpointer data)
167 {
168   Accessibility_Event *e = Accessibility_Event__alloc();
169   Bonobo_Unknown source = NULL;
170   AtkObject *aobject;
171   AtkPropertyValues *values;
172   GObject *gobject;
173   GSignalQuery signal_query;
174   gchar *name;
175   char sbuf[APP_STATIC_BUFF_SZ];
176   
177   g_signal_query (signal_hint->signal_id, &signal_query);
178   name = signal_query.signal_name;
179 #ifdef SPI_BRIDGE_DEBUG
180   fprintf (stderr, "Received (property) signal %s:%s\n",
181            g_type_name (signal_query.itype), name);
182 #endif
183   gobject = g_value_get_object (param_values + 0);
184   values = (AtkPropertyValues*) g_value_get_pointer (param_values + 1);
185
186   /* notify the actual listeners */
187   if (ATK_IS_IMPLEMENTOR (gobject))
188   {
189     aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
190     source = CORBA_Object_duplicate (BONOBO_OBJREF (spi_accessible_new (aobject)), &ev);
191     g_object_unref (G_OBJECT(aobject));
192   }
193   else if (ATK_IS_OBJECT (gobject))
194   {
195     aobject = ATK_OBJECT (gobject);
196     source = CORBA_Object_duplicate (BONOBO_OBJREF (spi_accessible_new (aobject)), &ev);
197   }
198   else
199   {
200     g_error("received property-change event from non-AtkImplementor");
201   }
202   
203   snprintf(sbuf, APP_STATIC_BUFF_SZ, "object:property-change:%s", values->property_name);
204   e->type = CORBA_string_dup (sbuf);
205   e->source = source;
206   e->detail1 = 0;
207   e->detail2 = 0;
208   if (source)
209     Accessibility_Registry_notifyEvent (registry, e, &ev);
210   return TRUE;
211 }
212
213 static gint
214 bridge_key_listener (AtkImplementor *atk_impl, AtkKeyEventStruct *event, gpointer data)
215 {
216   g_print ("bridge key listener!\n");
217 }
218
219 static gboolean
220 bridge_signal_listener (GSignalInvocationHint *signal_hint,
221                         guint n_param_values,
222                         const GValue *param_values,
223                         gpointer data)
224 {
225   Accessibility_Event *e = g_new0(Accessibility_Event, 1);
226   AtkObject *aobject;
227   Bonobo_Unknown source;
228   AtkPropertyValues *values;
229   GObject *gobject;
230   GSignalQuery signal_query;
231   gchar *name;
232   char sbuf[APP_STATIC_BUFF_SZ];
233   
234   g_signal_query (signal_hint->signal_id, &signal_query);
235   name = signal_query.signal_name;
236 #ifdef SPI_BRIDGE_DEBUG
237   fprintf (stderr, "Received (property) signal %s:%s\n",
238            g_type_name (signal_query.itype), name);
239 #endif
240   gobject = g_value_get_object (param_values + 0);
241
242   /* notify the actual listeners */
243   if (ATK_IS_IMPLEMENTOR (gobject))
244   {
245     aobject = atk_implementor_ref_accessible (ATK_IMPLEMENTOR (gobject));
246   }
247   else if (ATK_IS_OBJECT (gobject))
248   {
249     aobject = ATK_OBJECT (gobject);
250     g_object_ref (aobject);
251   }
252   else
253   {
254     g_error("received property-change event from non-AtkImplementor");
255   }
256
257   snprintf(sbuf, APP_STATIC_BUFF_SZ, "%s:%s", name, g_type_name (signal_query.itype));
258   source =  CORBA_Object_duplicate (BONOBO_OBJREF (spi_accessible_new (aobject)), &ev);
259   e->type = CORBA_string_dup (sbuf);
260   e->source = source;
261   e->detail1 = 0;
262   e->detail2 = 0;
263   Accessibility_Registry_notifyEvent (registry, e, &ev);
264   g_object_unref (aobject);
265   return TRUE;
266 }
267
268 static Accessibility_Registry bridge_get_registry ()
269 {
270   return registry;
271 }