idl/Registry.idl : temporarily changed register_Application
[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 typedef enum {
55   ETYPE_FOCUS,
56   ETYPE_WINDOW,
57   ETYPE_TOOLKIT,
58   ETYPE_LAST_DEFINED
59 } EventTypeMajor;
60
61 typedef struct {
62   EventTypeMajor major;
63   char * minor;
64   char * detail;
65 } EventTypeStruct;
66
67
68 /*
69  * Implemented GObject::finalize
70  */
71 static void
72 registry_object_finalize (GObject *object)
73 {
74 /*        Registry *registry = REGISTRY (object); */
75         GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
76
77         printf("registry_object_finalize called\n");
78
79         object_class->finalize (object);
80 }
81
82 /**
83  * registerApplication:
84  * @application: a reference to the requesting @Application
85  * return values: void
86  *
87  * Register a new application with the accessibility broker.
88  *
89  **/
90 static void
91 impl_accessibility_registry_register_application (PortableServer_Servant servant,
92                                                   const Accessibility_Application application,
93                                                   CORBA_Environment * ev)
94 {
95   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
96
97 #ifdef SPI_DEBUG
98   fprintf (stderr, "registering app %p\n", application);
99 #endif
100   ORBit_register_objref (application);
101   registry->desktop->applications = g_list_append (registry->desktop->applications,
102                                                    CORBA_Object_duplicate (application, ev));
103
104   /* TODO: create unique string here (with libuuid call ?) */
105   Accessibility_Application__set_id (application, "test-some-unique-string", ev);
106
107   /*
108    * TODO: change the implementation below to a WM-aware one;
109    * e.g. don't add all apps to the Desktop
110    */
111 }
112
113 static gint
114 compare_object_hash (gconstpointer p1, gconstpointer p2)
115 {
116   CORBA_Environment ev;
117   long long diff = ((CORBA_Object_hash ((CORBA_Object) p2, (CORBA_unsigned_long) 0, &ev)) -
118                     (CORBA_Object_hash ((CORBA_Object) p1, (CORBA_unsigned_long) 0, &ev)));
119   return ((diff < 0) ? -1 : ((diff > 0) ? 1 : 0));
120 }
121
122 static void
123 parse_event_type (EventTypeStruct *etype, char *event_name)
124 {
125   etype->major = ETYPE_FOCUS;
126 }
127
128 /**
129  * deregisterApplication:
130  * @application: a reference to the @Application
131  * to be deregistered.
132  * return values: void
133  *
134  * De-register an application previously registered with the broker.
135  *
136  **/
137 static void
138 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
139                                                     const Accessibility_Application application,
140                                                     CORBA_Environment * ev)
141 {
142   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
143   GList *list = g_list_find_custom (registry->applications, application, compare_object_hash);
144   if (list)
145     {
146       fprintf (stderr, "deregistering application\n");
147       registry->applications = g_list_delete_link (registry->applications, list);
148     }
149 }
150
151 /*
152  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
153  */
154 static void
155 impl_accessibility_registry_register_global_event_listener
156                                             (PortableServer_Servant  servant,
157                                              Accessibility_EventListener listener,
158                                              const CORBA_char *event_name,
159                                              CORBA_Environment      *ev)
160 {
161   /**
162    *  TODO:
163    *
164    *  distinguish between event types
165    *  register with app toolkits only for requested event types
166    *  maintain list of requested types and number of listeners
167    *  find non-strcmp method of matching event types to listeners
168    *
169    **/
170
171   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
172   /* fprintf(stderr, "registering %x/%x\n", listener, *listener); */
173   EventTypeStruct etype;
174   parse_event_type (&etype, event_name);
175
176   /* parse, check major event type */
177   switch (etype.major)
178     {
179     case (ETYPE_FOCUS) :
180       break;
181     case (ETYPE_WINDOW) :
182       break;
183     case (ETYPE_TOOLKIT) :
184       break;
185     default:
186       break;
187     }
188
189   registry->listeners = g_list_append (registry->listeners, CORBA_Object_duplicate(listener, ev));
190   /* fprintf(stderr, "there are now %d listeners registered.\n", g_list_length(registry->listeners)); */
191   /* should use hashtable and CORBA_Object_hash (...) */
192 }
193
194 /*
195  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
196  */
197 static void
198 impl_accessibility_registry_deregister_global_event_listener
199                                                    (PortableServer_Servant  servant,
200                                                     Accessibility_EventListener listener,
201                                                     CORBA_Environment      *ev)
202 {
203   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
204   /* TODO: this won't work since 'listener' is a duplicate ref */
205   registry->listeners = g_list_remove (registry->listeners, listener);
206   /*  fprintf(stderr, "deregister\n"); */
207 }
208
209
210 /**
211  * getDesktopCount:
212  * return values: a short integer indicating the current number of
213  * @Desktops.
214  *
215  * Get the current number of desktops.
216  *
217  **/
218 static short
219 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
220                                                CORBA_Environment * ev)
221 {
222   /* TODO: implement support for multiple virtual desktops */
223   CORBA_short n_desktops;
224   n_desktops = (CORBA_short) 1;
225   return n_desktops;
226 }
227
228 /**
229  * getDesktop:
230  * @n: the index of the requested @Desktop.
231  * return values: a reference to the requested @Desktop.
232  *
233  * Get the nth accessible desktop.
234  *
235  **/
236 static Accessibility_Desktop
237 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
238                                          const CORBA_short n,
239                                          CORBA_Environment * ev)
240 {
241   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
242
243   /* TODO: implement support for multiple virtual desktops */
244   if (n == 0)
245     {
246       return (Accessibility_Desktop)
247         CORBA_Object_duplicate (
248              bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
249     }
250   else
251     {
252       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
253     }
254 }
255
256 /**
257  * getDesktopList:
258  * return values: a sequence containing references to
259  * the @Desktops.
260  *
261  * Get a list of accessible desktops.
262  *
263  **/
264 static Accessibility_DesktopSeq *
265 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
266                                               CORBA_Environment * ev)
267 {
268   /* TODO: implement support for multiple virtual desktops */
269   return (Accessibility_DesktopSeq *) NULL;
270 }
271
272 static CORBA_Object
273 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
274                                                          CORBA_Environment * ev)
275 {
276   /* TODO: not yet implemented! */
277   return CORBA_OBJECT_NIL;
278 }
279
280 static void
281 impl_registry_notify_event (PortableServer_Servant servant,
282                             const Accessibility_Event *e,
283                             CORBA_Environment *ev)
284 {
285   int n;
286   int len;
287   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
288
289   /**
290    *  TODO:
291    *
292    *  distinguish between event types
293    *  find non-strcmp method of matching event types to listeners
294    *
295    **/
296
297   len = g_list_length (registry->listeners);
298   /* fprintf(stderr, "%d listeners registered\n", len); */
299
300   for (n=0; n<len; ++n)
301     {
302 #ifdef SPI_DEBUG
303       fprintf(stderr, "notifying listener #%d\n", n);
304       fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->target, ev));
305 #endif
306       Accessibility_EventListener_notifyEvent (
307                (Accessibility_EventListener) g_list_nth_data (registry->listeners, n),
308                e,
309                ev);
310     }
311 }
312
313 static void
314 registry_class_init (RegistryClass *klass)
315 {
316         GObjectClass * object_class = (GObjectClass *) klass;
317         POA_Accessibility_Registry__epv *epv = &klass->epv;
318
319         registry_parent_class = g_type_class_ref (LISTENER_TYPE);
320
321         object_class->finalize = registry_object_finalize;
322
323         epv->registerApplication = impl_accessibility_registry_register_application;
324         epv->deregisterApplication = impl_accessibility_registry_deregister_application;
325         epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
326         epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
327         epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
328         epv->getDesktop = impl_accessibility_registry_get_desktop;
329         epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
330         epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
331         ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
332 }
333
334 static void
335 registry_init (Registry *registry)
336 {
337   registry->listeners = NULL;
338   registry->applications = NULL;
339   registry->desktop = desktop_new();
340 }
341
342 GType
343 registry_get_type (void)
344 {
345         static GType type = 0;
346
347         if (!type) {
348                 static const GTypeInfo tinfo = {
349                         sizeof (RegistryClass),
350                         (GBaseInitFunc) NULL,
351                         (GBaseFinalizeFunc) NULL,
352                         (GClassInitFunc) registry_class_init,
353                         (GClassFinalizeFunc) NULL,
354                         NULL, /* class data */
355                         sizeof (Registry),
356                         0, /* n preallocs */
357                         (GInstanceInitFunc) registry_init,
358                         NULL /* value table */
359                 };
360                 /*
361                  *   Here we use bonobo_x_type_unique instead of
362                  * gtk_type_unique, this auto-generates a load of
363                  * CORBA structures for us. All derived types must
364                  * use bonobo_x_type_unique.
365                  */
366                 type = bonobo_type_unique (
367                         PARENT_TYPE,
368                         POA_Accessibility_Registry__init,
369                         NULL,
370                         G_STRUCT_OFFSET (RegistryClass, epv),
371                         &tinfo,
372                         "Registry");
373         }
374
375         return type;
376 }
377
378 Registry *
379 registry_new (void)
380 {
381     Registry *retval =
382                REGISTRY (g_object_new (registry_get_type (), NULL));
383     return retval;
384 }