summaryrefslogtreecommitdiff
path: root/cspi/spi_event.c
blob: 32719b77cad9e5451a88238649bd6b031e61b450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
 * Copyright 2001, 2002, 2003 Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <cspi/spi-private.h>

static GSList *_cspi_event_queue = NULL;

/**
 * SPI_freeAccessibleKeySet:
 * @keyset: An AccessibleKeyset to free.
 *
 * Release the memory used by an AccessibleKeySet.
 *
 **/
void
SPI_freeAccessibleKeySet (AccessibleKeySet *keyset)
{
  int i = 0;	
  g_free (keyset->keysyms);
  g_free (keyset->keycodes);
  while (keyset->keystrings [i])
    {
      g_free (keyset->keystrings [i++]);
    }
  g_free (keyset->keystrings);
  g_free (keyset);
}

/**
 * SPI_createAccessibleKeySet:
 * @len: the number of key values in the key set.
 * @keysyms: a UTF-8 string containing symbolic key values to be matched, or NULL if
 *           matching is performed against other key values instead.
 * @keycodes: an array of unsigned short values which are the hardware keycodes
 *           to be matched, or NULL if the keyset is specified solely by keysyms
 *           and/or keystrings.
 * @keystrings: an array of null-terminated character strings which specify key
 *             name values to match, or NULL if the keyset is specified solely by
 *             keycodes and/or keysyms.
 *
 * Create a new #AccessibleKeySet of a specified length.
 * A KeySet is used typically to match key event values, and a matches are made
 * using the following criteria: a match exists with a key event if all non-null
 * i-th members of the keyset match the key event.
 * If both keystring and keysym values are NULL, a keycode value match is
 * forced, thus the match for keysym=0, keycode=0, keystring=NULL is
 * keycode 0.
 *
 * Returns: a pointer to a newly-created #AccessibleKeySet.
 *
 **/
AccessibleKeySet *
SPI_createAccessibleKeySet (int len, const char *keysyms, short *keycodes,
			    const char **keystrings)
{
  AccessibleKeySet *keyset = g_new0 (AccessibleKeySet, 1);
  int i, keysym_len = 0;
  const char *keysym_ptr = keysyms;
  keyset->len = len;
  keyset->keysyms = g_new0 (unsigned long, len);
  keyset->keycodes = g_new0 (unsigned short, len);
  keyset->keystrings = g_new0 (char *, len);
  if (keysyms)
    {
      keysym_len = g_utf8_strlen (keysyms, -1);
    }
  for (i = 0; i < len; ++i)
    {
      if (i < keysym_len)
        {
	  keyset->keysyms [i] = (unsigned long) g_utf8_get_char (keysym_ptr);
	  keysym_ptr = g_utf8_find_next_char (keysym_ptr, NULL);
        }
      else
        {
          keyset->keysyms [i] = 0;
        }
      if (keycodes)
        {
	  keyset->keycodes [i] = keycodes [i];
	}
      if (keystrings)
        {
	  keyset->keystrings [i] = g_strdup (keystrings [i]);
	}
    }
  return keyset;	
}

/**
 * SPI_createAccessibleEventListener:
 * @callback : an #AccessibleEventListenerCB callback function, or NULL.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Create a new #AccessibleEventListener with a specified (in-process) callback function.
 *
 * Returns: a pointer to a newly-created #AccessibleEventListener.
 *
 **/
AccessibleEventListener *
SPI_createAccessibleEventListener (AccessibleEventListenerCB callback,
				   void                     *user_data)
{
  AccessibleEventListener *listener = cspi_event_listener_new ();
  if (callback)
    {
      AccessibleEventListener_addCallback (listener, callback, user_data);
    }
  return listener;
}

/**
 * AccessibleEventListener_addCallback:
 * @listener: the #AccessibleEventListener instance to modify.
 * @callback: an #AccessibleEventListenerCB function pointer.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Add an in-process callback function to an existing AccessibleEventListener.
 * Note that the callback function must live in the same address
 * space as the AccessibleEventListener implementation code, thus one should not
 * use this function to attach callbacks to a 'remote' event listener
 * (that is, one that was not created by a client call to
 * createAccessibleEventListener ();
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleEventListener_addCallback (AccessibleEventListener *listener,
				     AccessibleEventListenerCB callback,
				     void                     *user_data)
{
  cspi_event_listener_add_cb (listener, callback, user_data);
  return TRUE;
}

/**
 * AccessibleEventListener_unref:
 * @listener: a pointer to the #AccessibleEventListener being operated on.
 *
 * Decrements an #AccessibleEventListener's reference count.
 **/
void
AccessibleEventListener_unref (AccessibleEventListener *listener)
{
  cspi_event_listener_unref (listener);
}

/**
 * AccessibleEventListener_removeCallback:
 * @listener: the #AccessibleEventListener instance to modify.
 * @callback: an #AccessibleEventListenerCB function pointer.
 *
 * Remove an in-process callback function from an existing AccessibleEventListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleEventListener_removeCallback (AccessibleEventListener  *listener,
					AccessibleEventListenerCB callback)
{
  cspi_event_listener_remove_cb (listener, callback);
  return TRUE;
}

/**
 * SPI_createAccessibleKeystrokeListener:
 * @callback : an #AccessibleKeystrokeListenerCB callback function, or NULL.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Create a new #AccessibleKeystrokeListener with a specified callback function.
 *
 * Returns: a pointer to a newly-created #AccessibleKeystrokeListener.
 *
 **/
AccessibleKeystrokeListener *
SPI_createAccessibleKeystrokeListener (AccessibleKeystrokeListenerCB callback,
				       void                         *user_data)
{
  AccessibleDeviceListener *listener = cspi_device_listener_new ();
  if (callback)
    {
      AccessibleDeviceListener_addCallback (listener, callback, user_data);
    }
  return listener;
}

/**
 * AccessibleKeystrokeListener_addCallback:
 * @listener: the #AccessibleKeystrokeListener instance to modify.
 * @callback: an #AccessibleKeystrokeListenerCB function pointer.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Add an in-process callback function to an existing #AccessibleKeystrokeListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleKeystrokeListener_addCallback (AccessibleKeystrokeListener *listener,
					 AccessibleKeystrokeListenerCB callback,
					 void                         *user_data)
{
  cspi_device_listener_add_cb (listener, callback, user_data);
  return TRUE;
}

/**
 * AccessibleKeystrokeListener_removeCallback:
 * @listener: the #AccessibleKeystrokeListener instance to modify.
 * @callback: an #AccessibleKeystrokeListenerCB function pointer.
 *
 * Remove an in-process callback function from an existing #AccessibleKeystrokeListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleKeystrokeListener_removeCallback (AccessibleKeystrokeListener *listener,
					    AccessibleKeystrokeListenerCB callback)
{
  cspi_device_listener_remove_cb (listener, callback);
  return TRUE;
}

/**
 * AccessibleKeystrokeListener_unref:
 * @listener: a pointer to the #AccessibleKeystrokeListener being operated on.
 *
 * Decrements an #AccessibleKeystrokeListener's reference count.
 **/
void
AccessibleKeystrokeListener_unref (AccessibleKeystrokeListener *listener)
{
  cspi_device_listener_unref (listener);
}

/**
 * SPI_createAccessibleDeviceListener:
 * @callback : an #AccessibleDeviceListenerCB callback function, or NULL.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Create a new #AccessibleDeviceListener with a specified callback function.
 *
 * Returns: a pointer to a newly-created #AccessibleDeviceListener.
 *
 **/
AccessibleDeviceListener *
SPI_createAccessibleDeviceListener (AccessibleDeviceListenerCB callback,
				       void                         *user_data)
{
  AccessibleDeviceListener *listener = cspi_device_listener_new ();
  if (callback)
    {
      AccessibleDeviceListener_addCallback (listener, callback, user_data);
    }
  return listener;
}

/**
 * AccessibleDeviceListener_addCallback:
 * @listener: the #AccessibleDeviceListener instance to modify.
 * @callback: an #AccessibleDeviceListenerCB function pointer.
 * @user_data: a pointer to data which will be passed to the callback when invoked.
 *
 * Add an in-process callback function to an existing #AccessibleDeviceListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleDeviceListener_addCallback (AccessibleDeviceListener *listener,
					 AccessibleDeviceListenerCB callback,
					 void                         *user_data)
{
  cspi_device_listener_add_cb (listener, callback, user_data);
  return TRUE;
}

/**
 * AccessibleDeviceListener_removeCallback:
 * @listener: the #AccessibleDeviceListener instance to modify.
 * @callback: an #AccessibleDeviceListenerCB function pointer.
 *
 * Remove an in-process callback function from an existing #AccessibleDeviceListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 *
 **/
SPIBoolean
AccessibleDeviceListener_removeCallback (AccessibleDeviceListener *listener,
					    AccessibleDeviceListenerCB callback)
{
  cspi_device_listener_remove_cb (listener, callback);
  return TRUE;
}

/**
 * AccessibleDeviceListener_unref:
 * @listener: a pointer to the #AccessibleDeviceListener being operated on.
 *
 * Decrements an #AccessibleDeviceListener's reference count.
 **/
void
AccessibleDeviceListener_unref (AccessibleDeviceListener *listener)
{
  cspi_device_listener_unref (listener);
}

static char *
cspi_internal_event_get_text (const InternalEvent *e)
{
  g_return_val_if_fail (e, NULL);
  if (e->event.v_type == EVENT_DATA_STRING)
    {
      return g_strdup (e->event.v.text? e->event.v.text: "");
    }
  return NULL;
}

static Accessible *
cspi_internal_event_get_object (const InternalEvent *e)
{
  g_return_val_if_fail (e, NULL);
  if (e->event.v_type == EVENT_DATA_OBJECT)
    {
      cspi_object_ref (e->event.v.accessible);
      return e->event.v.accessible;
    }
  return NULL;
}

static SPIRect *
cspi_internal_event_get_rect (const InternalEvent *e)
{
  g_return_val_if_fail (e, NULL);
  if (e->event.v_type == EVENT_DATA_RECT)
  {
      SPIRect *rect = g_new (SPIRect, 1);
      if (rect) memcpy (rect, &e->event.v.rect, sizeof(*rect));
      return rect;
    }
  return NULL;
}

/**
 * AccessibleEvent_getSourceName:
 * @e: an #AccessibleEvent to be queried. 
 *
 * Get the 'accessible-name' of the object emitting the event.
 *
 * Returns: The name of the event source, or NULL if the event source cannot be identified
 *          or does not report a name.
 */
char*        AccessibleEvent_getSourceName (const AccessibleEvent *e)
{
  if (e && e->source)
    {
      return Accessible_getName (e->source);
    }
  return NULL;
}

/**
 * AccessibleEvent_getSourceRole:
 * @e: an #AccessibleEvent to be queried. 
 *
 * Get the #AccessibleRole of the object emitting the event.
 *
 * Returns: #AccessibleRole of the event source, or SPI_ROLE_UNKNOWN
 *          if the event source's role is unknown or unspecified.
 *          (Some kinds of events, such as 'mouse:' events or
 *          toolkit events, don't have associated object roles.)
 */
AccessibleRole AccessibleEvent_getSourceRole (const AccessibleEvent *e)
{
  if (e && e->source)
    {
      return Accessible_getRole (e->source);
    }
	return SPI_ROLE_UNKNOWN;
}

/**
 * AccessibleEvent_getSourceApplication:
 * @e: an #AccessibleEvent to be queried. 
 *
 * Get the #Application hosting the object which emitted the event.
 *
 * Returns: A pointer to the host #Application contining the event source
 *          component.
 */
#if 0
AccessibleApplication* AccessibleEvent_getSourceApplication (const AccessibleEvent *e)
{
xyzzy
    InternalEvent *ie = (InternalEvent *)e;
    CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
    if (any &&
	CORBA_TypeCode_equivalent (any->_type, 
				   TC_Accessibility_EventDetails, NULL))
      {
	  Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
	  return  cspi_object_take (details->host_application);
      }
    else
	return NULL;
}
#endif

/**
 * AccessibleEvent_getSourceDetails:
 * @e: an #AccessibleEvent to be queried. 
 * @name: a pointer to a character string which will point to the name of the event source 
 * on successful completion of the call.
 * @role: a pointer to an #AccessibleRole which will point to the role of the event source
 * on successful completion of the call.
 * @app: A pointer to an #AccessibleApplication which points to the host application for this event
 * on successful completion of the call.
 *
 * Get the host #Application, "accessible name", and #AccessibleRole 
 * of the object which emitted the event.
 *
 * Returns: TRUE if the source details were successfully retrieved, 
 *          FALSE if they were not, either due to error, incomplete data,
 *          or the fact that the event did not encapsulate the required data.
 */
#if 0
SPIBoolean   AccessibleEvent_getSourceDetails (const AccessibleEvent *e, 
					       char **name, AccessibleRole *role, 
					       AccessibleApplication **app)
{
    InternalEvent *ie = (InternalEvent *)e;
    CORBA_any *any = ((ie && ie->data) ? (CORBA_any *)ie->data : NULL);
    if (any &&
	CORBA_TypeCode_equivalent (any->_type, 
				   TC_Accessibility_EventDetails, NULL))
      {
	  Accessibility_EventDetails *details = (Accessibility_EventDetails *) any->_value;
	  *name = CORBA_string_dup (details->source_name);
	  *role = cspi_role_from_spi_role (details->source_role);
	  *app = cspi_object_take (details->host_application);
	  return TRUE;
      }
    else
      {
        *name = NULL;
	*role = SPI_ROLE_UNKNOWN;
	*app = NULL;
	return FALSE;
      }
}
#endif

/**
 * AccessibleTextChangedEvent_getChangeString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:text-changed", 
 *         returning the text inserted or deleted.
 *
 * Returns: a UTF-8 text string indicating the text inserted,
 *          deleted, or substituted by this event.
 **/
char *
AccessibleTextChangedEvent_getChangeString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleTextSelectionChangedEvent_getSelectionString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:text-selection-changed", 
 *         returning the newly added, removed, or modified selection string.
 *
 * Returns: a UTF-8 text string indicating the recently changed selection.
 **/
char *
AccessibleTextSelectionChangedEvent_getSelectionString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleWindowEvent_getTitleString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "window:", 
 *         returning the window title.
 *
 * Returns: a UTF-8 text string representing the title of the 
 *         recently changed window.
 **/
char *
AccessibleWindowEvent_getTitleString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleChildChangedEvent_getChildAccessible:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:children_changed"
 *         to get a reference to the changed #Accessible.
 *         Note that context #Accessibles are not guaranteed to outlive
 *         event delivery, in which case this call may return %NULL
 *         even if the object existed at the time of dispatch.
 *
 * Returns: the context #Accessible for the event, or %NULL if
 *          there is no longer a valid context #Accessible 
 *          object for the event.
 **/
Accessible *
AccessibleChildChangedEvent_getChildAccessible (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  return (Accessible *) cspi_internal_event_get_object (foo);
}

/**
 * AccessibleParentChangedEvent_getParentAccessible:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:property-change:accessible-parent"
 *         to get a reference to the changed #Accessible.
 *         Note that context #Accessibles are not guaranteed to outlive
 *         event delivery, in which case this call may return %NULL
 *         even if the object existed at the time of dispatch.
 *
 * Returns: an #Accessible pointer representing the new parent object.
 **/
Accessible *
AccessibleParentChangedEvent_getParentAccessible (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  return (Accessible *) cspi_internal_event_get_object (foo);
}

/**
 * AccessibleActiveDescendantChangedEvent_getActiveDescendant:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:active-descendant-changed"
 *         to get a reference to the changed #Accessible.
 *         Note that context #Accessibles are not guaranteed to outlive
 *         event delivery, in which case this call may return %NULL
 *         even if the object existed at the time of dispatch.
 *
 * Returns: an #Accessible pointer representing the new active descendant.
 **/
Accessible *
AccessibleActiveDescendantChangedEvent_getActiveDescendant (const AccessibleEvent *e) 
{
  const InternalEvent *foo = (InternalEvent *) e;
  return (Accessible *) cspi_internal_event_get_object (foo);
}

/**
 * AccessibleTableSummaryChangedEvent_getSummaryAccessible:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:property-changed:accessible-table-summary"
 *         to get a reference to the changed #Accessible.
 *         Note that context #Accessibles are not guaranteed to outlive
 *         event delivery, in which case this call may return %NULL
 *         even if the object existed at the time of dispatch.
 *
 * Returns: an #Accessible pointer representing the new table summary.
 **/
Accessible *
AccessibleTableSummaryChangedEvent_getSummaryAccessible (const AccessibleEvent *e) 
{
  const InternalEvent *foo = (InternalEvent *) e;
  return (Accessible *) cspi_internal_event_get_object (foo);
}

/**
 * AccessibleTableHeaderChangedEvent_getHeaderAccessible:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type 
 *         "object:property-changed:accessible-table-row-header" or
 *         "object:property-changed:accessible-table-column-header"
 *         to get a reference to the changed #Accessible.
 *         Note that context #Accessibles are not guaranteed to outlive
 *         event delivery, in which case this call may return %NULL
 *         even if the object existed at the time of dispatch.
 *
 * Returns: an #Accessible pointer representing the new table header.
 **/
Accessible *
AccessibleTableHeaderChangedEvent_getHeaderAccessible (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  return (Accessible *) cspi_internal_event_get_object (foo);
}


/**
 * AccessibleTableCaptionChangedEvent_getCaptionString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type 
 *         "object:property-changed:accessible-table-caption-object" 
 *         returning the text in the caption, if present.
 *
 * Returns: a UTF-8 text string indicating the text in the caption.
 **/
char *
AccessibleTableCaptionChangedEvent_getCaptionString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleTableRowDescriptionChangedEvent_getDescriptionString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type 
 *         "object:property-changed:accessible-table-row-description" 
 *         returning the new table row description.
 *
 * Returns: a UTF-8 text string representing the recently changed
 *         table row description 
 **/
char *
AccessibleTableRowDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleTableColumnDescriptionChangedEvent_getDescriptionString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type 
 *         "object:property-changed:accessible-table-column-description" 
 *         returning the new table column description.
 *
 * Returns: a UTF-8 text string representing the recently changed
 *         table column description 
 **/
char *
AccessibleTableColumnDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleDescriptionChangedEvent_getDescriptionString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type 
 *         "object:property-changed:accessible-description" 
 *         returning the new description.
 *
 * Returns: a UTF-8 text string representing the recently changed
 *         description 
 **/
char *
AccessibleDescriptionChangedEvent_getDescriptionString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleBoundsChangedEvent_getNewBounds:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:bounds-changed", 
 *         returning a pointer to an SPIRect structure containing the
 *         new bounds, or NULL on error.
 *         The returned structure should be freed with SPI_freeRect when 
 *         the caller has finished referencing it.
 *
 * @Since: AT-SPI 1.6
 *
 * Returns: a pointer to an SPIRect defining the new object bounds.
 **/
SPIRect *
AccessibleBoundsChangedEvent_getNewBounds (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  /* TODO: check the event type. */
  return cspi_internal_event_get_rect (foo);
}

static gint
cspi_event_compare (gconstpointer p1, gconstpointer p2)
{
  const InternalEvent *e1 = p1, *e2 = p2;
  return (gint) ((long) e2->id  - (long) e1->id);
}

static InternalEvent *
cspi_internal_event_lookup (const InternalEvent *e)
{
  InternalEvent *internal = NULL;
  GSList *p =
    g_slist_find_custom (_cspi_event_queue, e, cspi_event_compare);
  if (p)
    internal = p->data;
  return internal;
}

static const InternalEvent *
cspi_internal_event_check (const AccessibleEvent *e)
{
  InternalEvent *internal = (InternalEvent *) e;
  if (internal->magic == SPI_INTERNAL_EVENT_MAGIC) 
    return internal;
  else
    return NULL;
}

static InternalEvent *
cspi_internal_event_add (const InternalEvent *e)
{
  _cspi_event_queue = g_slist_prepend (_cspi_event_queue, (gpointer) e);
  return (InternalEvent *) e;
}

static void
cspi_internal_event_remove (const InternalEvent *e)
{
  GSList *link = g_slist_find_custom (_cspi_event_queue, e, cspi_event_compare);
  if (link)
    _cspi_event_queue = g_slist_remove_link (_cspi_event_queue, link);
}

/**
 * AccessibleNameChangedEvent_getNameString:
 * @e: a pointer to the #AccessibleEvent being queried.
 *
 * Queries an #AccessibleEvent of type "object:property-change:accessible_name:", 
 *         returning the name.
 *
 * Returns: a UTF-8 text string representing the name of the 
 *         object which recently changed.
 **/
char *
AccessibleNameChangedEvent_getNameString (const AccessibleEvent *e)
{
  const InternalEvent *foo = (InternalEvent *) e;
  return cspi_internal_event_get_text (foo);
}

/**
 * AccessibleEvent_ref:
 * @e: a pointer to the #AccessibleEvent being referenced.
 *
 * Increments by 1 the reference count of the event
 *
 * Returns: TRUE if the function succeeded; FALSE if the pointer is not a
 *         valid event.
 **/
SPIBoolean
AccessibleEvent_ref (const AccessibleEvent *e)
{
  const InternalEvent *private = cspi_internal_event_check (e);
  if (private)
    {
      InternalEvent *event = cspi_internal_event_lookup (private);
      /* 
       * put event in the cache if it's not there already, 
       * and increment refcount 
       */
      if (!event)
	{
	  event = cspi_internal_event_add (private);
	}
      event->ref_count++;
      return TRUE;
    }
  else
    return FALSE;
}

/**
 * AccessibleEvent_unref:
 * @e: a pointer to the #AccessibleEvent being referenced.
 *
 * Decrements by 1 the reference count of the event. The event is destroyed
 * when the reference count recahes zero.
 *
 **/
void
AccessibleEvent_unref (const AccessibleEvent *e)
{
  const InternalEvent *private = cspi_internal_event_check (e);
  /* decrement refcount and remove if appropriate */
  if (private)
    {
      InternalEvent *event = cspi_internal_event_lookup (private);
      if (event) 
	{
	  event->ref_count--;
	  if (event->ref_count < 1)
            {
	      cspi_internal_event_remove (event);
              g_free ((gpointer)e->type);
              Accessible_unref (e->source);
	      if (event->event.v_type == EVENT_DATA_OBJECT)
		{
		  Accessible_unref (event->event.v.accessible);
		}
              g_free ((gpointer)e);
            }
	}
    }
}

typedef struct
{
  CSpiEventListener *listener;
  char *event;
  char *detail;
} CSpiEventListenerEntry;

static GList *event_listeners = NULL;

static dbus_bool_t
demarshal_rect (DBusMessageIter *iter, SPIRect *rect)
{
  dbus_int32_t x, y, width, height;
  DBusMessageIter iter_struct;

  dbus_message_iter_recurse (iter, &iter_struct);
  if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
  dbus_message_iter_get_basic (&iter_struct, &x);
  dbus_message_iter_next (&iter_struct);
  if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
  dbus_message_iter_get_basic (&iter_struct, &y);
  dbus_message_iter_next (&iter_struct);
  if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
  dbus_message_iter_get_basic (&iter_struct, &width);
  dbus_message_iter_next (&iter_struct);
  if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
  dbus_message_iter_get_basic (&iter_struct, &height);
  rect->x = x;
  rect->y = y;
  rect->width = width;
  rect->height = height;
  return TRUE;
}

static gboolean
parse_eventType (const char *eventType, char **type, char **detail, char **matchrule)
{
  char *p, *q;
  char *t, *d;

  p = strchr (eventType, ':');
  if (!p) return FALSE;
  p = strchr (p + 1, ':');
  if (!p) p = eventType + strlen (eventType);
  t = g_malloc (p - eventType + 1);
  if (t)
  {
    memcpy (t, eventType, p - eventType);
    t[p - eventType] = '\0';
  }
  else return FALSE;
  if (*p == ':')
  {
    d = g_strdup (p + 1);
    if (!d)
    {
      g_free (t);
      return FALSE;
    }
  }
  else d = NULL;
  if ((p = strchr (t, ':')))
  {
    *p = (p[1] == '\0'? '\0': '_');
  }
  while ((p = strchr (t, '-'))) *p = '_';
  if (matchrule)
  {
    *matchrule = g_strdup_printf ("type='signal',interface='%s',member='%s'", spi_interface_accessible, t);
    if (!*matchrule)
    {
      g_free (t);
      if (d) g_free (d);
      return FALSE;
    }
  }
  if (type) *type = t;
  if (detail) *detail = d;
  return TRUE;
}

static void listener_data_free (CSpiEventListenerEntry *e)
{
  g_free (e->event);
  if (e->detail) g_free (e->detail);
  g_free (e);
}

/**
 * SPI_registerGlobalEventListener:
 * @listener: the #AccessibleEventListener to be registered against an
 *            event type.
 * @eventType: a character string indicating the type of events for which
 *            notification is requested.  Format is
 *            EventClass:major_type:minor_type:detail
 *            where all subfields other than EventClass are optional.
 *            EventClasses include "object", "window", "mouse",
 *            and toolkit events (e.g. "Gtk", "AWT").
 *            Examples: "focus:", "Gtk:GtkWidget:button_press_event".
 *
 * Legal object event types:
 *
 *    (property change events)
 *
 *            object:property-change
 *            object:property-change:accessible-name
 *            object:property-change:accessible-description
 *            object:property-change:accessible-parent
 *            object:property-change:accessible-value
 *            object:property-change:accessible-role
 *            object:property-change:accessible-table-caption
 *            object:property-change:accessible-table-column-description
 *            object:property-change:accessible-table-column-header
 *            object:property-change:accessible-table-row-description
 *            object:property-change:accessible-table-row-header
 *            object:property-change:accessible-table-summary
 *
 *    (other object events)
 *
 *            object:state-changed 
 *            object:children-changed
 *            object:visible-data-changed
 *            object:selection-changed
 *            object:text-selection-changed
 *            object:text-changed
 *            object:text-caret-moved
 *            object:row-inserted
 *            object:row-reordered
 *            object:row-deleted
 *            object:column-inserted
 *            object:column-reordered
 *            object:column-deleted
 *            object:model-changed
 *            object:active-descendant-changed
 *
 *  (window events)
 *
 *            window:minimize
 *            window:maximize
 *            window:restore
 *            window:close
 *            window:create
 *            window:reparent
 *            window:desktop-create
 *            window:desktop-destroy
 *            window:activate
 *            window:deactivate
 *            window:raise
 *            window:lower
 *            window:move
 *            window:resize
 *            window:shade
 *            window:unshade
 *            window:restyle
 *
 *  (other events)
 *
 *            focus:
 *            mouse:abs
 *            mouse:rel
 *            mouse:b1p
 *            mouse:b1r
 *            mouse:b2p
 *            mouse:b2r
 *            mouse:b3p
 *            mouse:b3r
 *
 * NOTE: this string may be UTF-8, but should not contain byte value 56
 *            (ascii ':'), except as a delimiter, since non-UTF-8 string
 *            delimiting functions are used internally.
 *            In general, listening to
 *            toolkit-specific events is not recommended.
 *
 * Add an in-process callback function to an existing AccessibleEventListener.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 **/
SPIBoolean
SPI_registerGlobalEventListener (AccessibleEventListener *listener,
				 const char              *eventType)
{
  CSpiEventListenerEntry *e;
  char *matchrule;
  DBusError error;
  GList *new_list;

  if (!listener)
    {
      return FALSE;
    }

  e = g_new (CSpiEventListenerEntry, 1);
  if (!e) return FALSE;
  e->listener = listener;
  parse_eventType (eventType, &e->event, &e->detail, &matchrule);
  new_list = g_list_prepend (event_listeners, e);
  if (!new_list)
  {
    listener_data_free (e);
    return FALSE;
  }
  event_listeners = new_list;
  dbus_error_init (&error);
  dbus_bus_add_match (cspi_bus(), matchrule, &error);
  if (error.message)
  {
    g_warning ("Adding match: %s", error.message);
  }
  return TRUE;
}

/**
 * SPI_deregisterGlobalEventListenerAll:
 * @listener: the #AccessibleEventListener to be registered against
 *            an event type.
 *
 * deregisters an AccessibleEventListener from the registry, for all
 *            event types it may be listening to. Use
 *            AccessibleEventListener_unref to release the
 *            listener reference.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 **/
SPIBoolean
SPI_deregisterGlobalEventListenerAll (AccessibleEventListener *listener)
{
  GList *l;

  if (!listener)
    {
      return FALSE;
    }

  for (l = event_listeners; l;)
  {
    CSpiEventListenerEntry *e = l->data;
    if (e->listener == listener)
    {
      listener_data_free (e);
      l = g_list_remove (l, e);
    }
    else l = g_list_next (l);
  }
  return TRUE;
}

/**
 * SPI_deregisterGlobalEventListener:
 * @listener: the #AccessibleEventListener registered against an event type.
 * @eventType: a string specifying the event type for which this
 *             listener is to be deregistered.
 *
 * deregisters an AccessibleEventListener from the registry, for a specific
 *             event type.
 *
 * Returns: #TRUE if successful, otherwise #FALSE.
 **/
SPIBoolean
SPI_deregisterGlobalEventListener (AccessibleEventListener *listener,
				   const char              *eventType)
{
  char *type, *detail, *matchrule;
  GList *l;

  if (!parse_eventType (eventType, &type, &detail, &matchrule))
  {
    return FALSE;
  }
  if (!listener)
    {
      return FALSE;
    }

  for (l = event_listeners; l;)
  {
    CSpiEventListenerEntry *e = l->data;
    if (e->listener == listener && !strcmp (e->event, type) && (e->detail == detail || !strcmp (e->detail, detail)))
    {
      DBusError error;
      listener_data_free (e);
      l = g_list_remove (l, e);
      dbus_error_init (&error);
      dbus_bus_remove_match (cspi_bus(), matchrule, &error);
    }
    else l = g_list_next (l);
  }
  g_free (type);
  if (detail) g_free (detail);
  g_free (matchrule);
  return TRUE;
}

void
cspi_dispatch_event (AccessibleEvent *e)
{
  char *event, *detail;
  GList *l;

  parse_eventType (e->type, &event, &detail, NULL);
  for (l = event_listeners; l; l = g_list_next (l))
  {
    CSpiEventListenerEntry *entry = l->data;
    if (!strcmp (event, entry->event) &&
        (entry->detail == NULL || !strcmp (detail, entry->detail)))
    {
      CSpiEventListenerClass *klass = CSPI_EVENT_LISTENER_GET_CLASS (entry->listener);
      if (klass->event) (*klass->event)(entry->listener, e);
    }
  }
  if (detail) g_free (detail);
  g_free (event);
}

DBusHandlerResult
cspi_dbus_handle_event (DBusConnection *bus, DBusMessage *message, void *data)
{
  char *detail = NULL;
  const char *event = dbus_message_get_member (message);
  DBusMessageIter iter, iter_variant;
  dbus_message_iter_init (message, &iter);
  AccessibleEvent e;
  dbus_int32_t detail1, detail2;
  char *p;

  g_return_if_fail (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING);
  dbus_message_iter_get_basic (&iter, &detail);
  dbus_message_iter_next (&iter);
  g_return_if_fail (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_INT32);
  dbus_message_iter_get_basic (&iter, &detail1);
  e.detail1 = detail1;
  dbus_message_iter_next (&iter);
  g_return_if_fail (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_INT32);
  dbus_message_iter_get_basic (&iter, &detail2);
  e.detail2 = detail2;
  dbus_message_iter_next (&iter);
printf("event: %s %s\n", event, detail);
  e.type = g_strdup (event);
  p = strchr (e.type, '_');
  if (p) *p = ':';
  p = g_strconcat (e.type, ":", detail, NULL);
  if (p)
  {
    g_free (e.type);
    e.type = p;
  }
    while ((p = strchr (p, '_'))) *p = '-';
  e.source = cspi_ref_accessible (dbus_message_get_sender(message), dbus_message_get_path(message));
  dbus_message_iter_recurse (&iter, &iter_variant);
  switch (dbus_message_iter_get_arg_type (&iter_variant))
  {
    case DBUS_TYPE_OBJECT_PATH:
    {
      char *p;
      dbus_message_iter_get_basic (&iter_variant, &p);
      e.v_type = EVENT_DATA_OBJECT;
      e.v.accessible = cspi_ref_accessible (dbus_message_get_sender(message), p);
      break;
    }
    case DBUS_TYPE_STRING:
    {
      char *p;
      dbus_message_iter_get_basic (&iter_variant, &p);
      e.v_type = EVENT_DATA_STRING;
      e.v.text = g_strdup (p);
      break;
    }
    case DBUS_TYPE_STRUCT:
    {
      if (demarshal_rect (&iter_variant, &e.v.rect))
      {
	e.v_type = EVENT_DATA_RECT;
      }
      break;
    }
  default:
    break;
  }
  cspi_dispatch_event (&e);
  return DBUS_HANDLER_RESULT_HANDLED;
}