Tweak to michael's patch, makes listener deregistration rules match
[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, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* registry.c: the main accessibility service registry implementation */
25
26 #undef SPI_LISTENER_DEBUG
27 #undef SPI_DEBUG
28 #undef SPI_QUEUE_DEBUG
29
30 #include <config.h>
31 #ifdef SPI_DEBUG
32 #  include <stdio.h>
33 #endif
34
35 #include <bonobo/bonobo-exception.h>
36 #include "../libspi/spi-private.h"
37 #include "registry.h"
38
39 /* Our parent GObject type  */
40 #define PARENT_TYPE SPI_LISTENER_TYPE
41
42 /* A pointer to our parent object class */
43 static SpiListenerClass *spi_registry_parent_class;
44
45 static GQuark _deactivate_quark = 0;
46 static GQuark _activate_quark = 0;
47 static GQuark _state_quark = 0;
48 static GQuark _state_changed_focused_quark = 0;
49
50 int _dbg = 0;
51
52 typedef enum {
53   ETYPE_FOCUS,
54   ETYPE_OBJECT,
55   ETYPE_PROPERTY,
56   ETYPE_WINDOW,
57   ETYPE_TOOLKIT,
58   ETYPE_KEYBOARD,
59   ETYPE_MOUSE,
60   ETYPE_LAST_DEFINED
61 } EventTypeCategory;
62
63 typedef struct {
64   const char *event_name;
65   EventTypeCategory type_cat;
66   GQuark major;  /* from string segment[1] */
67   GQuark minor;  /* from string segment[1]+segment[2] */
68   GQuark detail; /* from string segment[3] (not concatenated) */
69 } EventTypeStruct;
70
71 typedef struct {
72   Accessibility_EventListener listener;
73   GQuark            event_type_quark;
74   EventTypeCategory event_type_cat;
75 } SpiListenerStruct;
76
77 static void
78 spi_registry_set_debug (const char *debug_flag_string)
79 {
80   if (debug_flag_string) 
81     _dbg = (int) g_ascii_strtod (debug_flag_string, NULL);
82 }
83
84 SpiListenerStruct *
85 spi_listener_struct_new (Accessibility_EventListener listener, CORBA_Environment *ev)
86 {
87   SpiListenerStruct *retval = g_malloc (sizeof (SpiListenerStruct));
88   retval->listener = bonobo_object_dup_ref (listener, ev);
89   return retval;
90 }
91
92
93 void
94 spi_listener_struct_free (SpiListenerStruct *ls, CORBA_Environment *ev)
95 {
96   bonobo_object_release_unref (ls->listener, ev);
97   g_free (ls);
98 }
99
100 static void
101 desktop_add_application (SpiDesktop *desktop,
102                          guint index, gpointer data)
103 {
104   BonoboObject *registry = BONOBO_OBJECT (data);
105   Accessibility_Event e;
106   CORBA_Environment ev;
107   Accessibility_Accessible a;
108   
109   CORBA_exception_init (&ev);
110   e.type = "object:children-changed:add";
111   e.source = BONOBO_OBJREF (desktop);
112   e.detail1 = index;
113   e.detail2 = 0;
114   a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop), 
115                                                 index, &ev);
116   /* FIXME
117   spi_init_any_object (&e.any_data, a);
118   */
119   spi_init_any_nil (&e.any_data);
120   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
121                                       &e, &ev);
122   bonobo_object_release_unref (a, &ev);
123   CORBA_exception_free (&ev);
124 }
125
126
127
128 static void
129 desktop_remove_application (SpiDesktop *desktop,
130                             guint index, gpointer data)
131 {
132   BonoboObject *registry = BONOBO_OBJECT (data);
133   Accessibility_Event e;
134   Accessibility_Accessible a;
135   CORBA_Environment ev;
136   
137   CORBA_exception_init (&ev);
138
139   e.type = "object:children-changed:remove";
140   e.source = BONOBO_OBJREF (desktop);
141   e.detail1 = index;
142   e.detail2 = 0;
143   a = Accessibility_Accessible_getChildAtIndex (BONOBO_OBJREF (desktop), 
144                                                 index, &ev);
145   /* FIXME
146   spi_init_any_object (&e.any_data, a);
147   */
148   spi_init_any_nil (&e.any_data);
149   Accessibility_Accessible_unref (a, &ev);
150   Accessibility_Registry_notifyEvent (BONOBO_OBJREF (registry),
151                                       &e, &ev);
152   Accessibility_Desktop_unref (e.source, &ev);
153   CORBA_exception_free (&ev);
154 }
155
156
157 static void
158 spi_registry_object_finalize (GObject *object)
159 {
160   DBG (1, g_warning ("spi_registry_object_finalize called\n"));
161   g_object_unref (SPI_REGISTRY (object)->de_controller);
162
163   G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
164 }
165
166 static long
167 _get_unique_id (void)
168 {
169   static long id = 0;
170
171   return ++id;
172 }
173
174 /**
175  * registerApplication:
176  * @application: a reference to the requesting @Application
177  * return values: void
178  *
179  * Register a new application with the accessibility broker.
180  *
181  **/
182 static void
183 impl_accessibility_registry_register_application (PortableServer_Servant servant,
184                                                   const Accessibility_Application application,
185                                                   CORBA_Environment * ev)
186 {
187   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
188
189 #ifdef SPI_DEBUG
190   fprintf (stderr, "registering app %p\n", application);
191 #endif
192   spi_desktop_add_application (registry->desktop, application);
193
194   Accessibility_Application__set_id (application, _get_unique_id (), ev);
195
196   /*
197    * TODO: change the implementation below to a WM-aware one;
198    * e.g. don't add all apps to the SpiDesktop
199    */
200 }
201
202 #ifdef USE_A_HASH_IN_FUTURE
203 static gint
204 compare_corba_objects (gconstpointer p1, gconstpointer p2)
205 {
206   CORBA_Environment ev;
207   gint retval;
208
209 #ifdef SPI_DEBUG
210   fprintf (stderr, "comparing %p to %p\n",
211            p1, p2);
212 #endif
213   
214   retval = !CORBA_Object_is_equivalent ((CORBA_Object) p1, (CORBA_Object) p2, &ev);
215   return retval;  
216 }
217 #endif
218
219 static void
220 register_with_toolkits (SpiRegistry *spi_registry_bonobo_object, EventTypeStruct *etype, CORBA_Environment *ev)
221 {
222   gint n_desktops;
223   gint n_apps;
224   gint i, j;
225   Accessibility_Desktop desktop;
226   Accessibility_Application app;
227   Accessibility_Registry registry;
228   registry  = BONOBO_OBJREF (spi_registry_bonobo_object);
229
230   /* for each app in each desktop, call ...Application_registerToolkitEventListener */
231
232   n_desktops = Accessibility_Registry_getDesktopCount (registry, ev);
233
234   for (i=0; i<n_desktops; ++i)
235     {
236       desktop = Accessibility_Registry_getDesktop (registry, i, ev);
237       n_apps = Accessibility_Desktop__get_childCount (desktop, ev);
238       for (j=0; j<n_apps; ++j)
239         {
240           app = (Accessibility_Application) Accessibility_Desktop_getChildAtIndex (desktop,
241                                                                                    j,
242                                                                                    ev);
243           Accessibility_Application_registerToolkitEventListener (app,
244                                                                   registry,
245                                                                   CORBA_string_dup (etype->event_name),
246                                                                   ev);
247         }
248     }
249 }
250
251 #ifdef USE_A_HASH_IN_FUTURE
252
253 static gint
254 compare_listener_quarks (gconstpointer p1, gconstpointer p2)
255 {
256         return (((SpiListenerStruct *)p2)->event_type_quark !=
257                 ((SpiListenerStruct *)p1)->event_type_quark);
258 }
259
260 static gint
261 compare_listener_corbaref (gconstpointer p1, gconstpointer p2)
262 {
263   return compare_corba_objects (((SpiListenerStruct *)p2)->listener,
264                                 ((SpiListenerStruct *)p1)->listener);
265 }
266 #endif
267
268 static void
269 parse_event_type (EventTypeStruct *etype, const char *event_name)
270 {
271   gchar **split_string;
272   gchar *s;
273
274   split_string = g_strsplit (event_name, ":", 4);
275   etype->event_name = event_name;
276
277   if (!g_ascii_strncasecmp (event_name, "focus:", 6))
278     {
279       etype->type_cat = ETYPE_FOCUS;
280     }
281   else if (!g_ascii_strncasecmp (event_name, "mouse:", 6))
282     {
283       etype->type_cat = ETYPE_MOUSE;
284     }
285   else if (!g_ascii_strncasecmp (event_name, "object:", 7))
286     {
287       etype->type_cat = ETYPE_OBJECT;
288     }
289   else if (!g_ascii_strncasecmp (event_name, "window:", 7))
290     {
291       etype->type_cat = ETYPE_WINDOW;
292     }
293   else if (!g_ascii_strncasecmp (event_name, "keyboard:", 9))
294     {
295       etype->type_cat = ETYPE_KEYBOARD;
296     }
297   else
298     {
299       etype->type_cat = ETYPE_TOOLKIT;
300     }
301
302   if (split_string[1])
303     {
304       etype->major = g_quark_from_string (split_string[1]);
305       if (split_string[2])
306         {
307           etype->minor = g_quark_from_string (s = g_strconcat (split_string[1], split_string[2], NULL));
308           g_free (s);
309           if (split_string[3])
310             {
311               etype->detail = g_quark_from_string (split_string[3]);
312             }
313           else
314             {
315               etype->detail = g_quark_from_static_string ("");
316             }
317         }
318       else
319         {
320           etype->minor = etype->major;
321           etype->detail = g_quark_from_static_string (""); /*etype->major;*/
322         }
323     }
324   else
325     {
326       etype->major = g_quark_from_static_string ("");
327       etype->minor = etype->major;
328       etype->detail = etype->major;
329     }
330
331   g_strfreev (split_string);
332 }
333
334 /**
335  * deregisterApplication:
336  * @application: a reference to the @Application
337  * to be deregistered.
338  * return values: void
339  *
340  * De-register an application previously registered with the broker.
341  *
342  **/
343 static void
344 impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
345                                                     const Accessibility_Application application,
346                                                     CORBA_Environment * ev)
347 {
348   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
349
350   spi_desktop_remove_application (registry->desktop, application);
351
352 #ifdef SPI_DEBUG
353   fprintf (stderr, "de-registered app %p\n", application);
354 #endif
355 }
356
357 static GList **
358 get_listener_list (SpiRegistry      *registry,
359                    EventTypeCategory cat)
360 {
361   GList **ret;
362   
363   switch (cat)
364     {
365       case ETYPE_OBJECT:
366       case ETYPE_PROPERTY:
367       case ETYPE_FOCUS:
368       case ETYPE_KEYBOARD:
369         ret = &registry->object_listeners;
370         break;
371       case ETYPE_WINDOW:
372         ret = &registry->window_listeners;
373         break;
374       case ETYPE_MOUSE:
375       case ETYPE_TOOLKIT:
376         ret = &registry->toolkit_listeners;
377         break;
378       default:
379         ret = NULL;
380         break;
381     }
382   return ret;
383 }
384
385 /*
386  * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
387  */
388 static void
389 impl_accessibility_registry_register_global_event_listener (
390         PortableServer_Servant      servant,
391         Accessibility_EventListener listener,
392         const CORBA_char           *event_name,
393         CORBA_Environment          *ev)
394 {
395   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant)); 
396   SpiListenerStruct *ls = spi_listener_struct_new (listener, ev);
397   EventTypeStruct etype;
398   GList          **list;
399
400 #ifdef SPI_LISTENER_DEBUG
401   fprintf (stderr, "registering for events of type %s\n", event_name);
402 #endif
403
404   /* parse, check major event type and add listener accordingly */
405   parse_event_type (&etype, event_name);
406   ls->event_type_quark = etype.minor;
407   ls->event_type_cat = etype.type_cat;
408
409   list = get_listener_list (registry, etype.type_cat);
410
411   if (list)
412     {
413       *list = g_list_prepend (*list, ls);
414
415       if (etype.type_cat == ETYPE_TOOLKIT)
416         {
417           register_with_toolkits (registry, &etype, ev);
418         }
419     }
420   else
421     {
422       spi_listener_struct_free (ls, ev);
423     }
424 }
425
426 typedef struct {
427   gboolean                    remove_all;
428   Accessibility_EventListener listener;
429   EventTypeStruct             etype;
430 } RemoveListenerClosure;
431
432 static SpiReEntrantContinue
433 remove_listener_cb (GList * const *list, gpointer user_data)
434 {
435   SpiListenerStruct *ls = (SpiListenerStruct *) (*list)->data;
436   CORBA_Environment  ev;
437   RemoveListenerClosure *cl = user_data;
438
439   CORBA_exception_init (&ev);
440
441   if (cl->remove_all || (((cl->etype.minor == ls->event_type_quark) || 
442                          (cl->etype.major == ls->event_type_quark)) &&
443                          cl->etype.type_cat == ls->event_type_cat ) )
444   {
445     if (CORBA_Object_is_equivalent (ls->listener, cl->listener, &ev))
446       {
447         spi_re_entrant_list_delete_link (list);
448         spi_listener_struct_free (ls, &ev);
449       }
450   }
451
452   CORBA_exception_free (&ev);
453
454   return SPI_RE_ENTRANT_CONTINUE;
455 }
456
457 /*
458  * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
459  */
460 static void
461 impl_accessibility_registry_deregister_global_event_listener_all (
462         PortableServer_Servant      servant,
463         Accessibility_EventListener listener,
464         CORBA_Environment          *ev)
465 {
466   int i;
467   GList **lists[3];
468   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
469   RemoveListenerClosure cl = { 0, };
470
471   lists[0] = &registry->object_listeners;
472   lists[1] = &registry->window_listeners;
473   lists[2] = &registry->toolkit_listeners;
474
475   cl.remove_all = TRUE;
476   cl.listener = listener;
477
478   for (i = 0; i < sizeof (lists) / sizeof (lists[0]); i++)
479     {
480       spi_re_entrant_list_foreach (lists [i], remove_listener_cb, &cl);
481     }
482 }
483
484
485 /*
486  * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
487  */
488 static void
489 impl_accessibility_registry_deregister_global_event_listener (
490         PortableServer_Servant      servant,
491         Accessibility_EventListener listener,
492         const CORBA_char           *event_name,
493         CORBA_Environment          *ev)
494 {
495   SpiRegistry    *registry;
496   RemoveListenerClosure cl = { 0, };
497
498   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
499
500   cl.remove_all = FALSE;
501   parse_event_type (&cl.etype, (char *) event_name);
502   cl.listener = listener;
503
504   spi_re_entrant_list_foreach (get_listener_list (registry, cl.etype.type_cat),
505                                 remove_listener_cb, &cl);
506 }
507
508
509 /**
510  * getDesktopCount:
511  * return values: a short integer indicating the current number of
512  * @Desktops.
513  *
514  * Get the current number of desktops.
515  *
516  **/
517 static short
518 impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
519                                                CORBA_Environment * ev)
520 {
521   /* TODO: implement support for multiple virtual desktops */
522   CORBA_short n_desktops;
523   n_desktops = (CORBA_short) 1;
524   return n_desktops;
525 }
526
527
528 /**
529  * getDesktop:
530  * @n: the index of the requested @Desktop.
531  * return values: a reference to the requested @Desktop.
532  *
533  * Get the nth accessible desktop.
534  *
535  **/
536 static Accessibility_Desktop
537 impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
538                                          const CORBA_short n,
539                                          CORBA_Environment * ev)
540 {
541   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
542
543   /* TODO: implement support for multiple virtual desktops */
544   if (n == 0)
545     {
546       return (Accessibility_Desktop)
547         bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
548     }
549   else
550     {
551       return (Accessibility_Desktop) CORBA_OBJECT_NIL;
552     }
553 }
554
555
556 /**
557  * getDesktopList:
558  * return values: a sequence containing references to
559  * the @Desktops.
560  *
561  * Get a list of accessible desktops.
562  *
563  **/
564 static Accessibility_DesktopSeq *
565 impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
566                                               CORBA_Environment * ev)
567 {
568   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
569   Accessibility_DesktopSeq *desktops;
570
571   desktops = Accessibility_DesktopSeq__alloc ();
572   desktops->_length = desktops->_maximum = 1;
573   desktops->_buffer = Accessibility_DesktopSeq_allocbuf (desktops->_length);
574   desktops->_buffer [0] = bonobo_object_dup_ref (BONOBO_OBJREF (registry->desktop), ev);
575
576   return desktops;
577 }
578
579
580 static Accessibility_DeviceEventController
581 impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
582                                                          CORBA_Environment     *ev)
583 {
584   SpiRegistry *registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
585
586   if (!registry->de_controller)
587     {
588       registry->de_controller = spi_device_event_controller_new (registry);
589     }
590
591   return bonobo_object_dup_ref (BONOBO_OBJREF (registry->de_controller), ev);
592 }
593
594 typedef struct {
595   CORBA_Environment  *ev;
596   Bonobo_Unknown      source;
597   EventTypeStruct     etype;
598   Accessibility_Event e_out;
599 } NotifyContext;
600
601 static SpiReEntrantContinue
602 notify_listeners_cb (GList * const *list, gpointer user_data)
603 {
604   SpiListenerStruct *ls;
605   NotifyContext     *ctx = user_data;
606
607   ls = (*list)->data;
608
609 #ifdef SPI_LISTENER_DEBUG
610   fprintf (stderr, "event quarks: %lx %lx %lx\n", ls->event_type_quark, ctx->etype.major, ctx->etype.minor);
611   fprintf (stderr, "event name: %s\n", ctx->etype.event_name);
612 #endif
613   if ((ls->event_type_quark == ctx->etype.major) ||
614       (ls->event_type_quark == ctx->etype.minor))
615     {
616 #ifdef SPI_DEBUG
617       CORBA_string s;
618       fprintf (stderr, "notifying listener %d\n", 0);
619 /* g_list_index (list, l->data)); */
620       s = Accessibility_Accessible__get_name (ctx->source, ctx->ev);
621       fprintf (stderr, "event source name %s\n", s);
622       CORBA_free (s);
623       if (BONOBO_EX (ctx->ev))
624         {
625           CORBA_exception_free (ctx->ev);
626           return SPI_RE_ENTRANT_CONTINUE;
627         }
628 #endif
629       
630       ctx->e_out.source = ctx->source;
631       
632       if ((*list) && (*list)->data == ls)
633         {
634           Accessibility_EventListener_notifyEvent (
635             (Accessibility_EventListener) ls->listener, &ctx->e_out, ctx->ev);
636           if (ctx->ev->_major != CORBA_NO_EXCEPTION)
637             {
638               DBG (1, g_warning ("Accessibility app error: exception during "
639                         "event notification: %s\n",
640                         CORBA_exception_id (ctx->ev)));
641               CORBA_exception_free (ctx->ev);
642               /* FIXME: check that this item is removed from the list
643                * on system exception by a 'broken' listener */
644             }
645         }
646     }  
647
648   return SPI_RE_ENTRANT_CONTINUE;
649 }
650
651 static void
652 registry_emit_event (SpiRegistry *registry, NotifyContext *ctx)
653 {
654   GList       **list = get_listener_list (registry, ctx->etype.type_cat);
655
656   if (list && *list)
657     {
658     
659       spi_re_entrant_list_foreach (list, notify_listeners_cb, ctx);
660     }
661 }
662
663 static NotifyContext*
664 registry_clone_notify_context (NotifyContext *ctx)
665 {
666   NotifyContext *new_ctx = g_new0 (NotifyContext, 1);
667
668   new_ctx->ev = NULL;
669   new_ctx->source = bonobo_object_dup_ref (ctx->source, NULL); 
670   new_ctx->etype.event_name = CORBA_string_dup (ctx->etype.event_name);
671   new_ctx->etype.type_cat = ctx->etype.type_cat;
672   new_ctx->etype.major = ctx->etype.major;
673   new_ctx->etype.minor = ctx->etype.minor;
674   new_ctx->etype.detail = ctx->etype.detail;
675   new_ctx->e_out.type = CORBA_string_dup (ctx->e_out.type);
676   new_ctx->e_out.source = ctx->e_out.source;
677   new_ctx->e_out.detail1 = ctx->e_out.detail1;
678   new_ctx->e_out.detail2 = ctx->e_out.detail2;
679   CORBA_any__copy (&(new_ctx->e_out.any_data), &(ctx->e_out.any_data));
680   return new_ctx;
681 }
682
683 static void
684 registry_flush_event_queue (SpiRegistry       *registry,
685                             gboolean      discard,
686                             CORBA_Environment *ev)
687 {
688   NotifyContext *q_ctx;
689   while (!g_queue_is_empty (registry->deferred_event_queue)) {
690     q_ctx = g_queue_pop_tail (registry->deferred_event_queue);
691 #ifdef SPI_QUEUE_DEBUG
692     fprintf (stderr, "%s! %s [n=%d] %p\n", (discard ? "discard" : "pop"), 
693              q_ctx->etype.event_name, 
694              (int) registry->deferred_event_queue->length, q_ctx);
695 #endif
696     if (!discard)  {
697       q_ctx->ev = ev;
698       registry_emit_event (registry, q_ctx);
699     }
700     if (discard &&
701         (q_ctx->etype.type_cat == ETYPE_OBJECT) && 
702         (q_ctx->etype.major == _state_quark) &&
703         (q_ctx->etype.minor == _state_changed_focused_quark)) {
704         registry->focus_object = q_ctx->source;
705 #ifdef SPI_QUEUE_DEBUG
706         fprintf (stderr, "discard!: set focus_object %p\n", registry->focus_object);
707 #endif
708     }
709     else {
710       bonobo_object_release_unref (q_ctx->source, NULL);
711     }
712     CORBA_free ((void *)q_ctx->etype.event_name);
713     CORBA_free ((void *)q_ctx->e_out.type);
714     g_free (q_ctx);
715   }
716   registry->is_queueing = FALSE;
717 }
718
719 static gboolean
720 registry_timeout_flush_queue (gpointer data)
721 {
722   SpiRegistry *registry = data;
723   CORBA_Environment ev;
724 #ifdef SPI_QUEUE_DEBUG
725   fprintf (stderr, "timeout! flushing queue...\n");
726 #endif
727   CORBA_exception_init (&ev);
728   registry->queue_handler_id = 0;
729   registry_flush_event_queue (registry, FALSE, &ev);
730   return FALSE;
731 }
732
733 static gboolean
734 registry_discard_on_event (SpiRegistry *registry, NotifyContext *ctx)
735 {
736   gboolean retval = FALSE;
737   NotifyContext *q_ctx = g_queue_peek_tail (registry->deferred_event_queue);
738   if ((q_ctx != NULL) &&
739       (ctx->etype.type_cat == ETYPE_WINDOW) && 
740       (ctx->etype.major == _activate_quark)) {
741     if (CORBA_Object_is_equivalent (ctx->source, q_ctx->source, NULL)) {
742       retval = TRUE;
743     }
744   }
745   return retval;
746 }
747
748 static gboolean
749 registry_reset_on_event (SpiRegistry *registry, NotifyContext *ctx)
750 {
751   return (ctx->etype.type_cat == ETYPE_WINDOW) ? TRUE : FALSE;
752 }
753
754 #ifdef SPI_QUEUE_DEBUG
755 #include <sys/time.h>
756 #endif
757
758 static void
759 registry_start_queue (SpiRegistry *registry)
760 {
761 #ifdef SPI_QUEUE_DEBUG
762     struct timeval tp;
763     gettimeofday (&tp, NULL);
764     fprintf (stderr, "start queueing at %i.%.6i\n", tp.tv_sec, tp.tv_usec);
765 #endif
766     if (registry->queue_handler_id != 0)
767       g_source_remove (registry->queue_handler_id);
768        
769     if (registry->focus_object)
770       {
771 #ifdef SPI_QUEUE_DEBUG
772         fprintf (stderr, "registry_start_queue: release focus_object %p\n", registry->focus_object);
773 #endif
774         bonobo_object_release_unref (registry->focus_object, NULL);
775         registry->focus_object = NULL;
776       }
777     registry->queue_handler_id = g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 
778                                                      registry->exit_notify_timeout,
779                                                      registry_timeout_flush_queue, registry, 
780                                                      NULL);
781     registry->is_queueing = TRUE;
782 }
783
784 static gboolean
785 registry_discard_event (SpiRegistry *registry,  NotifyContext *ctx)
786 {
787   gboolean ret = FALSE;
788
789   if (ctx->etype.type_cat == ETYPE_FOCUS)
790     {
791       if (registry->focus_object)
792         {
793           if (CORBA_Object_is_equivalent (registry->focus_object, ctx->source, NULL))
794             {
795               ret = TRUE;
796             }
797 #ifdef SPI_QUEUE_DEBUG
798           fprintf (stderr, "registry_discard_event: release focus_object %p\n", registry->focus_object);
799 #endif
800           bonobo_object_release_unref (registry->focus_object, NULL);
801           registry->focus_object = NULL;
802         }
803     }
804   return ret; 
805 }
806
807 static gboolean
808 registry_defer_on_event (SpiRegistry *registry, NotifyContext *ctx)
809 {
810   gboolean defer = FALSE;
811   if ((ctx->etype.type_cat == ETYPE_WINDOW) && 
812       (ctx->etype.major == _deactivate_quark)) {
813     defer = TRUE;
814     registry_start_queue (registry);
815   }
816   /* defer all object:state-change events after a window:deactivate */
817   else if ((ctx->etype.type_cat == ETYPE_FOCUS) ||
818            ((ctx->etype.type_cat == ETYPE_OBJECT) && 
819            (ctx->etype.major == _state_quark))) {
820     defer = TRUE;
821   }
822   return defer;
823 }
824
825 static gboolean
826 registry_queue_event (SpiRegistry *registry, NotifyContext *ctx)
827 {
828   NotifyContext *q_ctx = registry_clone_notify_context (ctx);
829 #ifdef SPI_QUEUE_DEBUG
830     if (q_ctx->etype.type_cat != ETYPE_MOUSE)
831       fprintf (stderr, "push! %s %p\n", q_ctx->etype.event_name, q_ctx);
832 #endif    
833   if (registry->is_queueing)
834     {
835       g_queue_push_head (registry->deferred_event_queue, q_ctx);
836
837       return FALSE;
838     }
839   else
840     {
841       bonobo_object_release_unref (q_ctx->source, NULL);
842       CORBA_free ((void *)q_ctx->etype.event_name);
843       CORBA_free ((void *)q_ctx->e_out.type);
844       g_free (q_ctx);
845       return TRUE; 
846     }
847 }
848
849 /**
850  * Dispose of event in one of several ways:
851  * 1) discard;
852  * 2) initiate queuing and push onto queue (below)
853  * 3) push on existing queue to either pop on timeout or on subsequent event
854  * 4) pass-through immediately
855  * 5) pass-through, discarding queued events
856  * 6) emit queued events and then pass through
857  **/
858 static gboolean
859 registry_filter_event (SpiRegistry *registry, NotifyContext *ctx,
860                        CORBA_Environment *ev)
861 {
862   g_assert (ctx != NULL);
863
864   if (registry_discard_event (registry, ctx))
865     return FALSE;
866
867   if (registry_defer_on_event (registry, ctx)) { /* #2, #3 */
868     if (registry->is_queueing) {
869       return registry_queue_event (registry, ctx);
870     }
871     else { /* #4a */
872       return TRUE;
873     }
874   } 
875   else if (registry_reset_on_event (registry, ctx)) { /* #5, #6 */
876     gboolean discard = registry_discard_on_event (registry, ctx);
877 #ifdef SPI_QUEUE_DEBUG
878     fprintf (stderr, "event %s caused reset, discard=%d\n",
879              ctx->etype.event_name, (int) discard);
880       {
881         struct timeval tp;
882         gettimeofday (&tp, NULL);
883         fprintf (stderr, "event at %i.%.6i\n", tp.tv_sec, tp.tv_usec);
884       }
885 #endif    
886     registry_flush_event_queue (registry, discard, ev);
887     return (discard ? FALSE : TRUE);
888   }
889   else { /* #4b */
890     return TRUE;
891   }
892 }
893
894 static void
895 impl_registry_notify_event (PortableServer_Servant     servant,
896                             const Accessibility_Event *e,
897                             CORBA_Environment         *ev)
898 {
899   SpiRegistry  *registry;
900   NotifyContext ctx;
901   static int level = 0;
902
903   level++;
904   registry = SPI_REGISTRY (bonobo_object_from_servant (servant));
905
906   parse_event_type (&ctx.etype, e->type);
907
908   ctx.ev = ev;
909   ctx.e_out = *e;
910   ctx.source = e->source;
911
912 #ifdef SPI_QUEUE_DEBUG
913     if (ctx.etype.type_cat != ETYPE_MOUSE)
914       fprintf (stderr, "filter! %s level: %d\n", ctx.etype.event_name, level);
915 #endif    
916   if (registry_filter_event (registry, &ctx, ev)) {
917 #ifdef SPI_QUEUE_DEBUG
918     if (ctx.etype.type_cat != ETYPE_MOUSE)
919       fprintf (stderr, "emit! %s level: %d\n", ctx.etype.event_name, level);
920 #endif    
921     registry_emit_event (registry, &ctx);
922   }
923   level--;
924 }
925
926 static void
927 spi_registry_class_init (SpiRegistryClass *klass)
928 {
929   GObjectClass * object_class = (GObjectClass *) klass;
930   POA_Accessibility_Registry__epv *epv = &klass->epv;
931
932   spi_registry_parent_class = g_type_class_ref (SPI_LISTENER_TYPE);
933   
934   object_class->finalize = spi_registry_object_finalize;
935
936   klass->parent_class.epv.notifyEvent   = impl_registry_notify_event;
937   
938   epv->registerApplication              = impl_accessibility_registry_register_application;
939   epv->deregisterApplication            = impl_accessibility_registry_deregister_application;
940   epv->registerGlobalEventListener      = impl_accessibility_registry_register_global_event_listener;
941   epv->deregisterGlobalEventListener    = impl_accessibility_registry_deregister_global_event_listener;
942   epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
943   epv->getDeviceEventController         = impl_accessibility_registry_get_device_event_controller;
944   epv->getDesktopCount                  = impl_accessibility_registry_get_desktop_count;
945   epv->getDesktop                       = impl_accessibility_registry_get_desktop;
946   epv->getDesktopList                   = impl_accessibility_registry_get_desktop_list;
947   _deactivate_quark = g_quark_from_static_string ("deactivate");
948   _activate_quark = g_quark_from_static_string ("activate");
949   _state_quark = g_quark_from_static_string ("state-changed");
950   _state_changed_focused_quark = g_quark_from_static_string ("state-changedfocused");
951 }
952
953 static void
954 spi_registry_init (SpiRegistry *registry)
955 {
956   spi_registry_set_debug (g_getenv ("AT_SPI_DEBUG"));
957   /*
958    * TODO: FIXME, this module makes the foolish assumptions that
959    * registryd uses the same display as the apps, and that the
960    * DISPLAY environment variable is set.
961    */
962   gdk_init (NULL, NULL);
963
964   registry->object_listeners = NULL;
965   registry->window_listeners = NULL;
966   registry->toolkit_listeners = NULL;
967   registry->deferred_event_queue = g_queue_new ();
968   registry->exit_notify_timeout = 200;
969   registry->queue_handler_id  = 0;
970   /*
971    * The focus_object is set when a state-change:focused event is discarded
972    * after a window:deactivate event is received because a window:activate
973    * event has been received for the same window within the timeout period.
974    * The focus object is used to suppress a focus event for that object.
975    * It is released when a window:deactivate event is received.
976    */ 
977   registry->focus_object = NULL;
978   registry->desktop = spi_desktop_new ();
979   /* Register callback notification for application addition and removal */
980   g_signal_connect (G_OBJECT (registry->desktop),
981                     "application_added",
982                     G_CALLBACK (desktop_add_application),
983                     registry);
984
985   g_signal_connect (G_OBJECT (registry->desktop),
986                     "application_removed",
987                     G_CALLBACK (desktop_remove_application),
988                     registry);
989
990   registry->de_controller = spi_device_event_controller_new (registry);
991 }
992
993 BONOBO_TYPE_FUNC_FULL (SpiRegistry,
994                        Accessibility_Registry,
995                        PARENT_TYPE,
996                        spi_registry)
997
998 SpiRegistry *
999 spi_registry_new (void)
1000 {
1001   SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
1002   bonobo_object_set_immortal (BONOBO_OBJECT (retval), TRUE);
1003   return retval;
1004 }