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