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