Removed references to bonobo_x_object, which is now redundant with bonobo_object...
[platform/core/uifw/at-spi2-atk.git] / registryd / registry.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 /*
24  * registry.c: test for accessibility implementation
25  *
26  */
27
28 #ifdef SPI_DEBUG
29 #include <stdio.h>
30 #endif
31 #include <config.h>
32 #include <bonobo/Bonobo.h>
33
34 /*
35  * This pulls the CORBA definitions for the "Accessibility::Registry" server
36  */
37 #include <libspi/Accessibility.h>
38
39 /*
40  * This pulls the definition for the BonoboObject (GType)
41  */
42 #include "registry.h"
43
44 /*
45  * Our parent GObject type
46  */
47 #define PARENT_TYPE LISTENER_TYPE
48
49 /*
50  * A pointer to our parent object class
51  */
52 static ListenerClass *registry_parent_class;
53
54 /*
55  * Implemented GObject::finalize
56  */
57 static void
58 registry_object_finalize (GObject *object)
59 {
60 /*        Registry *registry = REGISTRY (object); */
61         GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
62
63         printf("registry_object_finalize called\n");
64
65         object_class->finalize (object);
66 }
67
68 /**
69  * registerApplication:
70  * @application: a reference to the requesting @Application
71  * return values: void
72  *
73  * Register a new application with the accessibility broker.
74  *
75  **/
76 static void
77 impl_accessibility_registry_register_application (PortableServer_Servant servant,
78                                                   const Accessibility_Application application,
79                                                   CORBA_Environment * ev)
80 {
81   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
82
83 #ifdef SPI_DEBUG
84   fprintf (stderr, "registering app %p\n", application);
85 #endif
86
87   registry->desktop->applications = g_list_append (registry->desktop->applications, CORBA_Object_duplicate (application, ev));
88   /*
89    * TODO: change the implementation below to a WM-aware one;
90    * e.g. don't add all apps to the Desktop
91    */
92   /*  registry->desktop->applications = registry->applications;*/
93 }
94
95 /**
96  * deregisterApplication:
97  * @application: a reference to the @Application
98  * to be deregistered.
99  * return values: void
100  *
101  * De-register an application previously registered with the broker.
102  *
103  **/
104 static void
105 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
106                                                     const Accessibility_Application application,
107                                                     CORBA_Environment * ev)
108 {
109   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
110   registry->applications = g_list_remove (registry->applications, application);
111 }
112
113 /*
114  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
115  */
116 static void
117 impl_accessibility_registry_register_global_event_listener
118                                             (PortableServer_Servant  servant,
119                                              Accessibility_EventListener listener,
120                                              const CORBA_char *event_name,
121                                              CORBA_Environment      *ev)
122 {
123   /**
124    *  TODO:
125    *
126    *  distinguish between event types
127    *  register with app toolkits only for requested event types
128    *  maintain list of requested types and number of listeners
129    *  find non-strcmp method of matching event types to listeners
130    *
131    **/
132
133   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
134   /* fprintf(stderr, "registering %x/%x\n", listener, *listener); */
135   registry->listeners = g_list_append (registry->listeners, CORBA_Object_duplicate(listener, ev));
136   /* fprintf(stderr, "there are now %d listeners registered.\n", g_list_length(registry->listeners)); */
137   /* should use hashtable and CORBA_Object_hash (...) */
138 }
139
140 /*
141  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
142  */
143 static void
144 impl_accessibility_registry_deregister_global_event_listener
145                                                    (PortableServer_Servant  servant,
146                                                     Accessibility_EventListener listener,
147                                                     CORBA_Environment      *ev)
148 {
149   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
150   /* TODO: this won't work since 'listener' is a duplicate ref */
151   registry->listeners = g_list_remove (registry->listeners, listener);
152   /*  fprintf(stderr, "deregister\n"); */
153 }
154
155
156 /**
157  * getDesktopCount:
158  * return values: a short integer indicating the current number of
159  * @Desktops.
160  *
161  * Get the current number of desktops.
162  *
163  **/
164 static short
165 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
166                                                CORBA_Environment * ev)
167 {
168   /* TODO: implement support for multiple virtual desktops */
169   CORBA_short n_desktops;
170   n_desktops = (CORBA_short) 1;
171   return n_desktops;
172 }
173
174 /**
175  * getDesktop:
176  * @n: the index of the requested @Desktop.
177  * return values: a reference to the requested @Desktop.
178  *
179  * Get the nth accessible desktop.
180  *
181  **/
182 static Accessibility_Desktop
183 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
184                                          const CORBA_short n,
185                                          CORBA_Environment * ev)
186 {
187   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
188
189   /* TODO: implement support for multiple virtual desktops */
190   if (n == 0)
191     {
192       return (Accessibility_Desktop)
193         CORBA_Object_duplicate (
194              bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
195     }
196   else
197     {
198       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
199     }
200 }
201
202 /**
203  * getDesktopList:
204  * return values: a sequence containing references to
205  * the @Desktops.
206  *
207  * Get a list of accessible desktops.
208  *
209  **/
210 static Accessibility_DesktopSeq *
211 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
212                                               CORBA_Environment * ev)
213 {
214   /* TODO: implement support for multiple virtual desktops */
215   return (Accessibility_DesktopSeq *) NULL;
216 }
217
218 static CORBA_Object
219 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
220                                                          CORBA_Environment * ev)
221 {
222   /* TODO: not yet implemented! */
223   return CORBA_OBJECT_NIL;
224 }
225
226 static void
227 impl_registry_notify_event (PortableServer_Servant servant,
228                             const Accessibility_Event *e,
229                             CORBA_Environment *ev)
230 {
231   int n;
232   int len;
233   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
234
235   /**
236    *  TODO:
237    *
238    *  distinguish between event types
239    *  find non-strcmp method of matching event types to listeners
240    *
241    **/
242
243   len = g_list_length (registry->listeners);
244   /* fprintf(stderr, "%d listeners registered\n", len); */
245
246   for (n=0; n<len; ++n)
247     {
248 #ifdef SPI_DEBUG
249       fprintf(stderr, "notifying listener #%d\n", n);
250       fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->target, ev));
251 #endif
252       Accessibility_EventListener_notifyEvent (
253                (Accessibility_EventListener) g_list_nth_data (registry->listeners, n),
254                e,
255                ev);
256     }
257 }
258
259 static void
260 registry_class_init (RegistryClass *klass)
261 {
262         GObjectClass * object_class = (GObjectClass *) klass;
263         POA_Accessibility_Registry__epv *epv = &klass->epv;
264
265         registry_parent_class = g_type_class_ref (LISTENER_TYPE);
266
267         object_class->finalize = registry_object_finalize;
268
269         epv->registerApplication = impl_accessibility_registry_register_application;
270         epv->deregisterApplication = impl_accessibility_registry_deregister_application;
271         epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
272         epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
273         epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
274         epv->getDesktop = impl_accessibility_registry_get_desktop;
275         epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
276         epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
277         ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
278 }
279
280 static void
281 registry_init (Registry *registry)
282 {
283   registry->listeners = NULL;
284   registry->applications = NULL;
285   registry->desktop = desktop_new();
286 }
287
288 GType
289 registry_get_type (void)
290 {
291         static GType type = 0;
292
293         if (!type) {
294                 static const GTypeInfo tinfo = {
295                         sizeof (RegistryClass),
296                         (GBaseInitFunc) NULL,
297                         (GBaseFinalizeFunc) NULL,
298                         (GClassInitFunc) registry_class_init,
299                         (GClassFinalizeFunc) NULL,
300                         NULL, /* class data */
301                         sizeof (Registry),
302                         0, /* n preallocs */
303                         (GInstanceInitFunc) registry_init,
304                         NULL /* value table */
305                 };
306                 /*
307                  *   Here we use bonobo_x_type_unique instead of
308                  * gtk_type_unique, this auto-generates a load of
309                  * CORBA structures for us. All derived types must
310                  * use bonobo_x_type_unique.
311                  */
312                 type = bonobo_type_unique (
313                         PARENT_TYPE,
314                         POA_Accessibility_Registry__init,
315                         NULL,
316                         G_STRUCT_OFFSET (RegistryClass, epv),
317                         &tinfo,
318                         "Registry");
319         }
320
321         return type;
322 }
323
324 Registry *
325 registry_new (void)
326 {
327     Registry *retval =
328                REGISTRY (g_object_new (registry_get_type (), NULL));
329     return retval;
330 }