09e26730a8ed0876038223aff46442ea719a15e4
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_event.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, 2003 Sun Microsystems Inc.,
6  * Copyright 2001, 2002, 2003 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 #include <cspi/spi-private.h>
25 #include <cspi/bonobo/cspi-bonobo-listener.h>
26
27 static GSList *_cspi_event_queue = NULL;
28
29 /**
30  * SPI_freeAccessibleKeySet:
31  * @keyset: An AccessibleKeyset to free.
32  *
33  * Release the memory used by an AccessibleKeySet.
34  *
35  **/
36 void
37 SPI_freeAccessibleKeySet (AccessibleKeySet *keyset)
38 {
39   int i = 0;    
40   g_free (keyset->keysyms);
41   g_free (keyset->keycodes);
42   while (keyset->keystrings [i])
43     {
44       g_free (keyset->keystrings [i++]);
45     }
46   g_free (keyset->keystrings);
47   g_free (keyset);
48 }
49
50 /**
51  * SPI_createAccessibleKeySet:
52  * @len: the number of key values in the key set.
53  * @keysyms: a UTF-8 string containing symbolic key values to be matched, or NULL if
54  *           matching is performed against other key values instead.
55  * @keycodes: an array of unsigned short values which are the hardware keycodes
56  *           to be matched, or NULL if the keyset is specified solely by keysyms
57  *           and/or keystrings.
58  * @keystrings: an array of null-terminated character strings which specify key
59  *             name values to match, or NULL if the keyset is specified solely by
60  *             keycodes and/or keysyms.
61  *
62  * Create a new #AccessibleKeySet of a specified length.
63  * A KeySet is used typically to match key event values, and a matches are made
64  * using the following criteria: a match exists with a key event if all non-null
65  * i-th members of the keyset match the key event.
66  * If both keystring and keysym values are NULL, a keycode value match is
67  * forced, thus the match for keysym=0, keycode=0, keystring=NULL is
68  * keycode 0.
69  *
70  * Returns: a pointer to a newly-created #AccessibleKeySet.
71  *
72  **/
73 AccessibleKeySet *
74 SPI_createAccessibleKeySet (int len, const char *keysyms, short *keycodes,
75                             const char **keystrings)
76 {
77   AccessibleKeySet *keyset = g_new0 (AccessibleKeySet, 1);
78   int i, keysym_len = 0;
79   const char *keysym_ptr = keysyms;
80   keyset->len = len;
81   keyset->keysyms = g_new0 (unsigned long, len);
82   keyset->keycodes = g_new0 (unsigned short, len);
83   keyset->keystrings = g_new0 (char *, len);
84   if (keysyms)
85     {
86       keysym_len = g_utf8_strlen (keysyms, -1);
87     }
88   for (i = 0; i < len; ++i)
89     {
90       if (i < keysym_len)
91         {
92           keyset->keysyms [i] = (unsigned long) g_utf8_get_char (keysym_ptr);
93           keysym_ptr = g_utf8_find_next_char (keysym_ptr, NULL);
94         }
95       else
96         {
97           keyset->keysyms [i] = 0;
98         }
99       if (keycodes)
100         {
101           keyset->keycodes [i] = keycodes [i];
102         }
103       if (keystrings)
104         {
105           keyset->keystrings [i] = g_strdup (keystrings [i]);
106         }
107     }
108   return keyset;        
109 }
110
111 /**
112  * SPI_createAccessibleEventListener:
113  * @callback : an #AccessibleEventListenerCB callback function, or NULL.
114  * @user_data: a pointer to data which will be passed to the callback when invoked.
115  *
116  * Create a new #AccessibleEventListener with a specified (in-process) callback function.
117  *
118  * Returns: a pointer to a newly-created #AccessibleEventListener.
119  *
120  **/
121 AccessibleEventListener *
122 SPI_createAccessibleEventListener (AccessibleEventListenerCB callback,
123                                    void                     *user_data)
124 {
125   AccessibleEventListener *listener = cspi_event_listener_new ();
126   if (callback)
127     {
128       AccessibleEventListener_addCallback (listener, callback, user_data);
129     }
130   return listener;
131 }
132
133 /**
134  * AccessibleEventListener_addCallback:
135  * @listener: the #AccessibleEventListener instance to modify.
136  * @callback: an #AccessibleEventListenerCB function pointer.
137  * @user_data: a pointer to data which will be passed to the callback when invoked.
138  *
139  * Add an in-process callback function to an existing AccessibleEventListener.
140  * Note that the callback function must live in the same address
141  * space as the AccessibleEventListener implementation code, thus one should not
142  * use this function to attach callbacks to a 'remote' event listener
143  * (that is, one that was not created by a client call to
144  * createAccessibleEventListener ();
145  *
146  * Returns: #TRUE if successful, otherwise #FALSE.
147  *
148  **/
149 SPIBoolean
150 AccessibleEventListener_addCallback (AccessibleEventListener *listener,
151                                      AccessibleEventListenerCB callback,
152                                      void                     *user_data)
153 {
154   cspi_event_listener_add_cb (listener, callback, user_data);
155   return TRUE;
156 }
157
158 /**
159  * AccessibleEventListener_unref:
160  * @listener: a pointer to the #AccessibleEventListener being operated on.
161  *
162  * Decrements an #AccessibleEventListener's reference count.
163  **/
164 void
165 AccessibleEventListener_unref (AccessibleEventListener *listener)
166 {
167   cspi_event_listener_unref (listener);
168 }
169
170 /**
171  * AccessibleEventListener_removeCallback:
172  * @listener: the #AccessibleEventListener instance to modify.
173  * @callback: an #AccessibleEventListenerCB function pointer.
174  *
175  * Remove an in-process callback function from an existing AccessibleEventListener.
176  *
177  * Returns: #TRUE if successful, otherwise #FALSE.
178  *
179  **/
180 SPIBoolean
181 AccessibleEventListener_removeCallback (AccessibleEventListener  *listener,
182                                         AccessibleEventListenerCB callback)
183 {
184   cspi_event_listener_remove_cb (listener, callback);
185   return TRUE;
186 }
187
188 /**
189  * SPI_createAccessibleKeystrokeListener:
190  * @callback : an #AccessibleKeystrokeListenerCB callback function, or NULL.
191  * @user_data: a pointer to data which will be passed to the callback when invoked.
192  *
193  * Create a new #AccessibleKeystrokeListener with a specified callback function.
194  *
195  * Returns: a pointer to a newly-created #AccessibleKeystrokeListener.
196  *
197  **/
198 AccessibleKeystrokeListener *
199 SPI_createAccessibleKeystrokeListener (AccessibleKeystrokeListenerCB callback,
200                                        void                         *user_data)
201 {
202   AccessibleDeviceListener *listener = cspi_device_listener_new ();
203   if (callback)
204     {
205       AccessibleDeviceListener_addCallback (listener, callback, user_data);
206     }
207   return listener;
208 }
209
210 /**
211  * AccessibleKeystrokeListener_addCallback:
212  * @listener: the #AccessibleKeystrokeListener instance to modify.
213  * @callback: an #AccessibleKeystrokeListenerCB function pointer.
214  * @user_data: a pointer to data which will be passed to the callback when invoked.
215  *
216  * Add an in-process callback function to an existing #AccessibleKeystrokeListener.
217  *
218  * Returns: #TRUE if successful, otherwise #FALSE.
219  *
220  **/
221 SPIBoolean
222 AccessibleKeystrokeListener_addCallback (AccessibleKeystrokeListener *listener,
223                                          AccessibleKeystrokeListenerCB callback,
224                                          void                         *user_data)
225 {
226   cspi_device_listener_add_cb (listener, callback, user_data);
227   return TRUE;
228 }
229
230 /**
231  * AccessibleKeystrokeListener_removeCallback:
232  * @listener: the #AccessibleKeystrokeListener instance to modify.
233  * @callback: an #AccessibleKeystrokeListenerCB function pointer.
234  *
235  * Remove an in-process callback function from an existing #AccessibleKeystrokeListener.
236  *
237  * Returns: #TRUE if successful, otherwise #FALSE.
238  *
239  **/
240 SPIBoolean
241 AccessibleKeystrokeListener_removeCallback (AccessibleKeystrokeListener *listener,
242                                             AccessibleKeystrokeListenerCB callback)
243 {
244   cspi_device_listener_remove_cb (listener, callback);
245   return TRUE;
246 }
247
248 /**
249  * AccessibleKeystrokeListener_unref:
250  * @listener: a pointer to the #AccessibleKeystrokeListener being operated on.
251  *
252  * Decrements an #AccessibleKeystrokeListener's reference count.
253  **/
254 void
255 AccessibleKeystrokeListener_unref (AccessibleKeystrokeListener *listener)
256 {
257   cspi_device_listener_unref (listener);
258 }
259
260 /**
261  * SPI_createAccessibleDeviceListener:
262  * @callback : an #AccessibleDeviceListenerCB callback function, or NULL.
263  * @user_data: a pointer to data which will be passed to the callback when invoked.
264  *
265  * Create a new #AccessibleDeviceListener with a specified callback function.
266  *
267  * Returns: a pointer to a newly-created #AccessibleDeviceListener.
268  *
269  **/
270 AccessibleDeviceListener *
271 SPI_createAccessibleDeviceListener (AccessibleDeviceListenerCB callback,
272                                        void                         *user_data)
273 {
274   AccessibleDeviceListener *listener = cspi_device_listener_new ();
275   if (callback)
276     {
277       AccessibleDeviceListener_addCallback (listener, callback, user_data);
278     }
279   return listener;
280 }
281
282 /**
283  * AccessibleDeviceListener_addCallback:
284  * @listener: the #AccessibleDeviceListener instance to modify.
285  * @callback: an #AccessibleDeviceListenerCB function pointer.
286  * @user_data: a pointer to data which will be passed to the callback when invoked.
287  *
288  * Add an in-process callback function to an existing #AccessibleDeviceListener.
289  *
290  * Returns: #TRUE if successful, otherwise #FALSE.
291  *
292  **/
293 SPIBoolean
294 AccessibleDeviceListener_addCallback (AccessibleDeviceListener *listener,
295                                          AccessibleDeviceListenerCB callback,
296                                          void                         *user_data)
297 {
298   cspi_device_listener_add_cb (listener, callback, user_data);
299   return TRUE;
300 }
301
302 /**
303  * AccessibleDeviceListener_removeCallback:
304  * @listener: the #AccessibleDeviceListener instance to modify.
305  * @callback: an #AccessibleDeviceListenerCB function pointer.
306  *
307  * Remove an in-process callback function from an existing #AccessibleDeviceListener.
308  *
309  * Returns: #TRUE if successful, otherwise #FALSE.
310  *
311  **/
312 SPIBoolean
313 AccessibleDeviceListener_removeCallback (AccessibleDeviceListener *listener,
314                                             AccessibleDeviceListenerCB callback)
315 {
316   cspi_device_listener_remove_cb (listener, callback);
317   return TRUE;
318 }
319
320 /**
321  * AccessibleDeviceListener_unref:
322  * @listener: a pointer to the #AccessibleDeviceListener being operated on.
323  *
324  * Decrements an #AccessibleDeviceListener's reference count.
325  **/
326 void
327 AccessibleDeviceListener_unref (AccessibleDeviceListener *listener)
328 {
329   cspi_device_listener_unref (listener);
330 }
331
332 static char *
333 cspi_internal_event_get_text (const InternalEvent *e)
334 {
335   CORBA_any *any;
336   g_return_val_if_fail (e, NULL);
337   g_return_val_if_fail (e->data, NULL);
338   any = (CORBA_any *) e->data;
339   if (CORBA_TypeCode_equivalent (any->_type, TC_Accessibility_EventDetails, NULL)) 
340     {
341       Accessibility_EventDetails *details = (Accessibility_EventDetails *)any->_value;
342       return CORBA_string_dup (* (char **) (details->any_data._value));
343     }
344   else if (CORBA_TypeCode_equivalent (any->_type, TC_CORBA_string, NULL)) 
345     {
346       return CORBA_string_dup (* (char **) any->_value);
347     } 
348   else
349     {
350 #ifdef EVENT_CONTEXT_DEBUG
351       fprintf (stderr, "requested string, TC is not TC_CORBA_string! (%u)\n",
352                (unsigned) any->_type);
353 #endif
354       return NULL;
355     }
356 }
357
358 static Accessible *
359 cspi_internal_event_get_object (const InternalEvent *e)
360 {
361   CORBA_any *any;
362
363   g_return_val_if_fail (e, NULL);
364   g_return_val_if_fail (e->data, NULL);
365
366   any = (CORBA_any *) e->data;
367   if (CORBA_TypeCode_equivalent (any->_type, TC_Accessibility_EventDetails, NULL)) 
368     {
369       Accessibility_EventDetails *details = (Accessibility_EventDetails *)any->_value;
370       return cspi_object_take (* (CORBA_Object *) (details->any_data._value));
371     }
372   else if (CORBA_TypeCode_equal (any->_type, TC_CORBA_Object, cspi_ev()))
373     return cspi_object_take (* (CORBA_Object *) any->_value);
374   else 
375     return NULL;
376 }
377
378 static SPIRect *
379 cspi_internal_event_get_rect (const InternalEvent *e)
380 {
381   CORBA_any *any;
382   g_return_val_if_fail (e, NULL);
383   g_return_val_if_fail (e->data, NULL);
384   any = (CORBA_any *) e->data;
385   if (CORBA_TypeCode_equivalent (any->_type, TC_Accessibility_EventDetails, NULL)) 
386     {
387       Accessibility_EventDetails *details = (Accessibility_EventDetails *)any->_value;
388       SPIRect *rect = g_new (SPIRect, 1);
389       Accessibility_BoundingBox *bounds = (Accessibility_BoundingBox *) details->any_data._value;
390       rect->x = bounds->x;
391       rect->y = bounds->y;
392       rect->width = bounds->width;
393       rect->height = bounds->height;
394       return rect;
395     }
396   if (CORBA_TypeCode_equivalent (any->_type, TC_Accessibility_BoundingBox, NULL)) 
397     {
398       SPIRect *rect = g_new (SPIRect, 1);
399       Accessibility_BoundingBox *bounds = (Accessibility_BoundingBox *) any->_value;
400       rect->x = bounds->x;
401       rect->y = bounds->y;
402       rect->width = bounds->width;
403       rect->height = bounds->height;
404       return rect;
405     } 
406   else
407     {
408 #ifdef EVENT_CONTEXT_DEBUG
409       fprintf (stderr, "requested string, TC is not TC_Accessible_RectBounds! (%u)\n",
410                (unsigned) any->_type);
411 #endif
412       return NULL;
413     }
414 }
415
416 /**
417  * AccessibleEvent_getSourceName:
418  *
419  * Get the 'accessible-name' of the object emitting the event.
420  * @event: an #AccessibleEvent to be queried. 
421  *
422  * Returns: The name of the event source, or NULL if the event source cannot be identified
423  *          or does not report a name.
424  */
425 char*        AccessibleEvent_getSourceName (const AccessibleEvent *e)
426 {
427     InternalEvent *ie = (InternalEvent *)e;
428     CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
429     if (any &&
430         CORBA_TypeCode_equivalent (any->_type, 
431                                    TC_Accessibility_EventDetails, NULL))
432       {
433           Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
434           return CORBA_string_dup (details->source_name);
435       }
436     else
437         return NULL;
438 }
439
440 /**
441  * AccessibleEvent_getSourceRole:
442  *
443  * Get the #AccessibleRole of the object emitting the event.
444  * @event: an #AccessibleEvent to be queried. 
445  *
446  * Returns: #AccessibleRole of the event source, or SPI_ROLE_UNKNOWN
447  *          if the event source's role is unknown or unspecified.
448  *          (Some kinds of events, such as 'mouse:' events or
449  *          toolkit events, don't have associated object roles.)
450  */
451 AccessibleRole AccessibleEvent_getSourceRole (const AccessibleEvent *e)
452 {
453     InternalEvent *ie = (InternalEvent *)e;
454     CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
455     if (any &&
456         CORBA_TypeCode_equivalent (any->_type, 
457                                    TC_Accessibility_EventDetails, NULL))
458       {
459           Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
460           return cspi_role_from_spi_role (details->source_role);
461       }
462     else
463         return SPI_ROLE_UNKNOWN;
464 }
465
466 /**
467  * AccessibleEvent_getSourceApplication:
468  *
469  * Get the #Application hosting the object which emitted the event.
470  * @event: an #AccessibleEvent to be queried. 
471  *
472  * Returns: A pointer to the host #Application contining the event source
473  *          component.
474  */
475 AccessibleApplication* AccessibleEvent_getSourceApplication (const AccessibleEvent *e)
476 {
477     InternalEvent *ie = (InternalEvent *)e;
478     CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
479     if (any &&
480         CORBA_TypeCode_equivalent (any->_type, 
481                                    TC_Accessibility_EventDetails, NULL))
482       {
483           Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
484           return  cspi_object_take (details->host_application);
485       }
486     else
487         return NULL;
488 }
489
490 /**
491  * AccessibleEvent_getSourceApplication:
492  *
493  * Get the host #Application, "accessible name", and #AccessibleRole 
494  * of the object which emitted the event.
495  *
496  * @event: an #AccessibleEvent to be queried. 
497  *
498  * Returns: TRUE if the source details were successfully retrieved, 
499  *          FALSE if they were not, either due to error, incomplete data,
500  *          or the fact that the event did not encapsulate the required data.
501  */
502 SPIBoolean   AccessibleEvent_getSourceDetails (const AccessibleEvent *e, 
503                                                char **name, AccessibleRole *role, 
504                                                AccessibleApplication **app)
505 {
506     InternalEvent *ie = (InternalEvent *)e;
507     CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
508     if (any &&
509         CORBA_TypeCode_equivalent (any->_type, 
510                                    TC_Accessibility_EventDetails, NULL))
511       {
512           Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
513           *name = CORBA_string_dup (details->source_name);
514           *role = cspi_role_from_spi_role (details->source_role);
515           *app = cspi_object_take (details->host_application);
516           return TRUE;
517       }
518     else
519       {
520         *name = NULL;
521         *role = SPI_ROLE_UNKNOWN;
522         *app = NULL;
523         return FALSE;
524       }
525 }
526
527 /**
528  * AccessibleTextChangedEvent_getChangeString:
529  * @e: a pointer to the #AccessibleEvent being queried.
530  *
531  * Queries an #AccessibleEvent of type "object:text-changed", 
532  *         returning the text inserted or deleted.
533  *
534  * Returns: a UTF-8 text string indicating the text inserted,
535  *          deleted, or substituted by this event.
536  **/
537 char *
538 AccessibleTextChangedEvent_getChangeString (const AccessibleEvent *e)
539 {
540   const InternalEvent *foo = (InternalEvent *) e;
541   /* TODO: check the event type. */
542   return cspi_internal_event_get_text (foo);
543 }
544
545 /**
546  * AccessibleTextSelectionChangedEvent_getSelectionString:
547  * @e: a pointer to the #AccessibleEvent being queried.
548  *
549  * Queries an #AccessibleEvent of type "object:text-selection-changed", 
550  *         returning the newly added, removed, or modified selection string.
551  *
552  * Returns: a UTF-8 text string indicating the recently changed selection.
553  **/
554 char *
555 AccessibleTextSelectionChangedEvent_getSelectionString (const AccessibleEvent *e)
556 {
557   const InternalEvent *foo = (InternalEvent *) e;
558   /* TODO: check the event type. */
559   return cspi_internal_event_get_text (foo);
560 }
561
562 /**
563  * AccessibleWindowEvent_getTitleString:
564  * @e: a pointer to the #AccessibleEvent being queried.
565  *
566  * Queries an #AccessibleEvent of type "window:", 
567  *         returning the window title.
568  *
569  * Returns: a UTF-8 text string representing the title of the 
570  *         recently changed window.
571  **/
572 char *
573 AccessibleWindowEvent_getTitleString (const AccessibleEvent *e)
574 {
575   const InternalEvent *foo = (InternalEvent *) e;
576   /* TODO: check the event type. */
577   return cspi_internal_event_get_text (foo);
578 }
579
580 /**
581  * AccessibleChildChangedEvent_getChildAccessible:
582  * @e: a pointer to the #AccessibleEvent being queried.
583  *
584  * Queries an #AccessibleEvent of type "object:children_changed"
585  *         to get a reference to the changed #Accessible.
586  *         Note that context #Accessibles are not guaranteed to outlive
587  *         event delivery, in which case this call may return %NULL
588  *         even if the object existed at the time of dispatch.
589  *
590  * Returns: the context #Accessible for the event, or %NULL if
591  *          there is no longer a valid context #Accessible 
592  *          object for the event.
593  **/
594 Accessible *
595 AccessibleChildChangedEvent_getChildAccessible (const AccessibleEvent *e)
596 {
597   const InternalEvent *foo = (InternalEvent *) e;
598   return (Accessible *) cspi_internal_event_get_object (foo);
599 }
600
601 /**
602  * AccessibleParentChangedEvent_getParentAccessible:
603  * @e: a pointer to the #AccessibleEvent being queried.
604  *
605  * Queries an #AccessibleEvent of type "object:property-change:accessible-parent"
606  *         to get a reference to the changed #Accessible.
607  *         Note that context #Accessibles are not guaranteed to outlive
608  *         event delivery, in which case this call may return %NULL
609  *         even if the object existed at the time of dispatch.
610  *
611  * Returns: an #Accessible pointer representing the new parent object.
612  **/
613 Accessible *
614 AccessibleParentChangedEvent_getParentAccessible (const AccessibleEvent *e)
615 {
616   const InternalEvent *foo = (InternalEvent *) e;
617   return (Accessible *) cspi_internal_event_get_object (foo);
618 }
619
620 /**
621  * AccessibleActiveDescendantChangedEvent_getActiveDescendant:
622  * @e: a pointer to the #AccessibleEvent being queried.
623  *
624  * Queries an #AccessibleEvent of type "object:active-descendant-changed"
625  *         to get a reference to the changed #Accessible.
626  *         Note that context #Accessibles are not guaranteed to outlive
627  *         event delivery, in which case this call may return %NULL
628  *         even if the object existed at the time of dispatch.
629  *
630  * Returns: an #Accessible pointer representing the new active descendant.
631  **/
632 Accessible *
633 AccessibleActiveDescendantChangedEvent_getActiveDescendant (const AccessibleEvent *e) 
634 {
635   const InternalEvent *foo = (InternalEvent *) e;
636   return (Accessible *) cspi_internal_event_get_object (foo);
637 }
638
639 /**
640  * AccessibleTableSummaryChangedEvent_getSummaryAccessible:
641  * @e: a pointer to the #AccessibleEvent being queried.
642  *
643  * Queries an #AccessibleEvent of type "object:property-changed:accessible-table-summary"
644  *         to get a reference to the changed #Accessible.
645  *         Note that context #Accessibles are not guaranteed to outlive
646  *         event delivery, in which case this call may return %NULL
647  *         even if the object existed at the time of dispatch.
648  *
649  * Returns: an #Accessible pointer representing the new table summary.
650  **/
651 Accessible *
652 AccessibleTableSummaryChangedEvent_getSummaryAccessible (const AccessibleEvent *e) 
653 {
654   const InternalEvent *foo = (InternalEvent *) e;
655   return (Accessible *) cspi_internal_event_get_object (foo);
656 }
657
658 /**
659  * AccessibleTableHeaderChangedEvent_getHeaderAccessible:
660  * @e: a pointer to the #AccessibleEvent being queried.
661  *
662  * Queries an #AccessibleEvent of type 
663  *         "object:property-changed:accessible-table-row-header" or
664  *         "object:property-changed:accessible-table-column-header"
665  *         to get a reference to the changed #Accessible.
666  *         Note that context #Accessibles are not guaranteed to outlive
667  *         event delivery, in which case this call may return %NULL
668  *         even if the object existed at the time of dispatch.
669  *
670  * Returns: an #Accessible pointer representing the new table header.
671  **/
672 Accessible *
673 AccessibleTableHeaderChangedEvent_getHeaderAccessible (const AccessibleEvent *e)
674 {
675   const InternalEvent *foo = (InternalEvent *) e;
676   return (Accessible *) cspi_internal_event_get_object (foo);
677 }
678
679
680 /**
681  * AccessibleTableCaptionChangedEvent_getCaptionString:
682  * @e: a pointer to the #AccessibleEvent being queried.
683  *
684  * Queries an #AccessibleEvent of type 
685  *         "object:property-changed:accessible-table-caption-object" 
686  *         returning the text in the caption, if present.
687  *
688  * Returns: a UTF-8 text string indicating the text in the caption.
689  **/
690 char *
691 AccessibleTableCaptionChangedEvent_getCaptionString (const AccessibleEvent *e)
692 {
693   const InternalEvent *foo = (InternalEvent *) e;
694   /* TODO: check the event type. */
695   return cspi_internal_event_get_text (foo);
696 }
697
698 /**
699  * AccessibleTableRowDescriptionChangedEvent_getDescriptionString:
700  * @e: a pointer to the #AccessibleEvent being queried.
701  *
702  * Queries an #AccessibleEvent of type 
703  *         "object:property-changed:accessible-table-row-description" 
704  *         returning the new table row description.
705  *
706  * Returns: a UTF-8 text string representing the recently changed
707  *         table row description 
708  **/
709 char *
710 AccessibleTableRowDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
711 {
712   const InternalEvent *foo = (InternalEvent *) e;
713   /* TODO: check the event type. */
714   return cspi_internal_event_get_text (foo);
715 }
716
717 /**
718  * AccessibleTableColumnDescriptionChangedEvent_getDescriptionString:
719  * @e: a pointer to the #AccessibleEvent being queried.
720  *
721  * Queries an #AccessibleEvent of type 
722  *         "object:property-changed:accessible-table-column-description" 
723  *         returning the new table column description.
724  *
725  * Returns: a UTF-8 text string representing the recently changed
726  *         table column description 
727  **/
728 char *
729 AccessibleTableColumnDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
730 {
731   const InternalEvent *foo = (InternalEvent *) e;
732   /* TODO: check the event type. */
733   return cspi_internal_event_get_text (foo);
734 }
735
736 /**
737  * AccessibleDescriptionChangedEvent_getDescriptionString:
738  * @e: a pointer to the #AccessibleEvent being queried.
739  *
740  * Queries an #AccessibleEvent of type 
741  *         "object:property-changed:accessible-description" 
742  *         returning the new description.
743  *
744  * Returns: a UTF-8 text string representing the recently changed
745  *         description 
746  **/
747 char *
748 AccessibleDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
749 {
750   const InternalEvent *foo = (InternalEvent *) e;
751   /* TODO: check the event type. */
752   return cspi_internal_event_get_text (foo);
753 }
754
755 /**
756  * AccessibleBoundsChangedEvent_getNewBounds:
757  * @e: a pointer to the #AccessibleEvent being queried.
758  *
759  * Queries an #AccessibleEvent of type "object:bounds-changed", 
760  *         returning a pointer to an SPIRect structure containing the
761  *         new bounds, or NULL on error.
762  *         The returned structure should be freed with SPI_freeRect when 
763  *         the caller has finished referencing it.
764  *
765  * Returns: a pointer to an SPIRect defining the new object bounds.
766  **/
767 SPIRect *
768 AccessibleBoundsChangedEvent_getNewBounds (const AccessibleEvent *e)
769 {
770   const InternalEvent *foo = (InternalEvent *) e;
771   /* TODO: check the event type. */
772   return cspi_internal_event_get_rect (foo);
773 }
774
775 static gint
776 cspi_event_compare (gconstpointer p1, gconstpointer p2)
777 {
778   const InternalEvent *e1 = p1, *e2 = p2;
779   return (gint) ((long) e2->id  - (long) e1->id);
780 }
781
782 static InternalEvent *
783 cspi_internal_event_lookup (const InternalEvent *e)
784 {
785   InternalEvent *internal = NULL;
786   GSList *p =
787     g_slist_find_custom (_cspi_event_queue, e, cspi_event_compare);
788   if (p)
789     internal = p->data;
790   return internal;
791 }
792
793 static const InternalEvent *
794 cspi_internal_event_check (const AccessibleEvent *e)
795 {
796   InternalEvent *internal = (InternalEvent *) e;
797   if (internal->magic == SPI_INTERNAL_EVENT_MAGIC) 
798     return internal;
799   else
800     return NULL;
801 }
802
803 static InternalEvent *
804 cspi_internal_event_add (const InternalEvent *e)
805 {
806   _cspi_event_queue = g_slist_prepend (_cspi_event_queue, (gpointer) e);
807   return (InternalEvent *) e;
808 }
809
810 static void
811 cspi_internal_event_remove (const InternalEvent *e)
812 {
813   GSList *link = g_slist_find_custom (_cspi_event_queue, e, cspi_event_compare);
814   if (link)
815     _cspi_event_queue = g_slist_remove_link (_cspi_event_queue, link);
816 }
817
818 /**
819  * AccessibleNameChangedEvent_getNameString:
820  * @e: a pointer to the #AccessibleEvent being queried.
821  *
822  * Queries an #AccessibleEvent of type "object:property-change:accessible_name:", 
823  *         returning the name.
824  *
825  * Returns: a UTF-8 text string representing the name of the 
826  *         object which recently changed.
827  **/
828 char *
829 AccessibleNameChangedEvent_getNameString (const AccessibleEvent *e)
830 {
831   const InternalEvent *foo = (InternalEvent *) e;
832   return cspi_internal_event_get_text (foo);
833 }
834
835 /**
836  * AccessibleEvent_ref:
837  * @e: a pointer to the #AccessibleEvent being referenced.
838  *
839  * Increments by 1 the reference count of the event
840  *
841  * Returns: TRUE if the function succeeded; FALSE if the pointer is not a
842  *         valid event.
843  **/
844 SPIBoolean
845 AccessibleEvent_ref (const AccessibleEvent *e)
846 {
847   const InternalEvent *private = cspi_internal_event_check (e);
848   if (private)
849     {
850       InternalEvent *event = cspi_internal_event_lookup (private);
851       /* 
852        * put event in the cache if it's not there already, 
853        * and increment refcount 
854        */
855       if (!event)
856         {
857           event = cspi_internal_event_add (private);
858         }
859       event->ref_count++;
860       return TRUE;
861     }
862   else
863     return FALSE;
864 }
865
866 /**
867  * AccessibleEvent_unref:
868  * @e: a pointer to the #AccessibleEvent being referenced.
869  *
870  * Decrements by 1 the reference count of the event. The event is destroyed
871  * when the reference count recahes zero.
872  *
873  **/
874 void
875 AccessibleEvent_unref (const AccessibleEvent *e)
876 {
877   const InternalEvent *private = cspi_internal_event_check (e);
878   /* decrement refcount and remove if appropriate */
879   if (private)
880     {
881       InternalEvent *event = cspi_internal_event_lookup (private);
882       if (event) 
883         {
884           event->ref_count--;
885           if (event->ref_count < 1)
886             {
887               cspi_internal_event_remove (event);
888               g_free ((gpointer)e->type);
889               Accessible_unref (e->source);
890               CORBA_free (event->data);
891               g_free ((gpointer)e);
892             }
893         }
894     }
895 }