2002-09-13 Michael Meeks <michael@ximian.com>
[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 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 #include <cspi/spi-private.h>
25
26
27
28 /**
29  * SPI_freeAccessibleKeySet:
30  * @keyset: An AccessibleKeyset to free.
31  *
32  * Release the memory used by an AccessibleKeySet.
33  *
34  **/
35 void
36 SPI_freeAccessibleKeySet (AccessibleKeySet *keyset)
37 {
38   int i = 0;    
39   g_free (keyset->keysyms);
40   g_free (keyset->keycodes);
41   while (keyset->keystrings [i])
42     {
43       g_free (keyset->keystrings [i++]);
44     }
45   g_free (keyset->keystrings);
46   g_free (keyset);
47 }
48
49 /**
50  * SPI_createAccessibleKeySet:
51  * @len: the number of key values in the key set.
52  * @keysyms: a UTF-8 string containing symbolic key values to be matched, or NULL if
53  *           matching is performed against other key values instead.
54  * @keycodes: an array of unsigned short values which are the hardware keycodes
55  *           to be matched, or NULL if the keyset is specified solely by keysyms
56  *           and/or keystrings.
57  * @keystrings: an array of null-terminated character strings which specify key
58  *             name values to match, or NULL if the keyset is specified solely by
59  *             keycodes and/or keysyms.
60  *
61  * Create a new #AccessibleKeySet of a specified length.
62  * A KeySet is used typically to match key event values, and a matches are made
63  * using the following criteria: a match exists with a key event if all non-null
64  * i-th members of the keyset match the key event.
65  * If both keystring and keysym values are NULL, a keycode value match is
66  * forced, thus the match for keysym=0, keycode=0, keystring=NULL is
67  * keycode 0.
68  *
69  * Returns: a pointer to a newly-created #AccessibleKeySet.
70  *
71  **/
72 AccessibleKeySet *
73 SPI_createAccessibleKeySet (int len, const char *keysyms, short *keycodes,
74                             const char **keystrings)
75 {
76   AccessibleKeySet *keyset = g_new0 (AccessibleKeySet, 1);
77   int i, keysym_len = 0;
78   const char *keysym_ptr = keysyms;
79   keyset->len = len;
80   keyset->keysyms = g_new0 (unsigned long, len);
81   keyset->keycodes = g_new0 (unsigned short, len);
82   keyset->keystrings = g_new0 (char *, len);
83   if (keysyms)
84     {
85       keysym_len = g_utf8_strlen (keysyms, -1);
86     }
87   for (i = 0; i < len; ++i)
88     {
89       if (i < keysym_len)
90         {
91           keyset->keysyms [i] = (unsigned long) g_utf8_get_char (keysym_ptr);
92           keysym_ptr = g_utf8_find_next_char (keysym_ptr, NULL);
93         }
94       else
95         {
96           keyset->keysyms [i] = 0;
97         }
98       if (keycodes)
99         {
100           keyset->keycodes [i] = keycodes [i];
101         }
102       if (keystrings)
103         {
104           keyset->keystrings [i] = g_strdup (keystrings [i]);
105         }
106     }
107   return keyset;        
108 }
109
110 /**
111  * SPI_createAccessibleEventListener:
112  * @callback : an #AccessibleEventListenerCB callback function, or NULL.
113  * @user_data: a pointer to data which will be passed to the callback when invoked.
114  *
115  * Create a new #AccessibleEventListener with a specified (in-process) callback function.
116  *
117  * Returns: a pointer to a newly-created #AccessibleEventListener.
118  *
119  **/
120 AccessibleEventListener *
121 SPI_createAccessibleEventListener (AccessibleEventListenerCB callback,
122                                    void                     *user_data)
123 {
124   AccessibleEventListener *listener = cspi_event_listener_new ();
125   if (callback)
126     {
127       AccessibleEventListener_addCallback (listener, callback, user_data);
128     }
129   return listener;
130 }
131
132 /**
133  * AccessibleEventListener_addCallback:
134  * @listener: the #AccessibleEventListener instance to modify.
135  * @callback: an #AccessibleEventListenerCB function pointer.
136  * @user_data: a pointer to data which will be passed to the callback when invoked.
137  *
138  * Add an in-process callback function to an existing AccessibleEventListener.
139  * Note that the callback function must live in the same address
140  * space as the AccessibleEventListener implementation code, thus one should not
141  * use this function to attach callbacks to a 'remote' event listener
142  * (that is, one that was not created by a client call to
143  * createAccessibleEventListener ();
144  *
145  * Returns: #TRUE if successful, otherwise #FALSE.
146  *
147  **/
148 SPIBoolean
149 AccessibleEventListener_addCallback (AccessibleEventListener *listener,
150                                      AccessibleEventListenerCB callback,
151                                      void                     *user_data)
152 {
153   cspi_event_listener_add_cb (listener, callback, user_data);
154   return TRUE;
155 }
156
157 /**
158  * AccessibleEventListener_unref:
159  * @listener: a pointer to the #AccessibleEventListener being operated on.
160  *
161  * Decrements an #AccessibleEventListener's reference count.
162  **/
163 void
164 AccessibleEventListener_unref (AccessibleEventListener *listener)
165 {
166   cspi_event_listener_unref (listener);
167 }
168
169 /**
170  * AccessibleEventListener_removeCallback:
171  * @listener: the #AccessibleEventListener instance to modify.
172  * @callback: an #AccessibleEventListenerCB function pointer.
173  *
174  * Remove an in-process callback function from an existing AccessibleEventListener.
175  *
176  * Returns: #TRUE if successful, otherwise #FALSE.
177  *
178  **/
179 SPIBoolean
180 AccessibleEventListener_removeCallback (AccessibleEventListener  *listener,
181                                         AccessibleEventListenerCB callback)
182 {
183   cspi_event_listener_remove_cb (listener, callback);
184   return TRUE;
185 }
186
187 /**
188  * SPI_createAccessibleKeystrokeListener:
189  * @callback : an #AccessibleKeystrokeListenerCB callback function, or NULL.
190  * @user_data: a pointer to data which will be passed to the callback when invoked.
191  *
192  * Create a new #AccessibleKeystrokeListener with a specified callback function.
193  *
194  * Returns: a pointer to a newly-created #AccessibleKeystrokeListener.
195  *
196  **/
197 AccessibleKeystrokeListener *
198 SPI_createAccessibleKeystrokeListener (AccessibleKeystrokeListenerCB callback,
199                                        void                         *user_data)
200 {
201   AccessibleKeystrokeListener *listener = cspi_keystroke_listener_new ();
202   if (callback)
203     {
204       AccessibleKeystrokeListener_addCallback (listener, callback, user_data);
205     }
206   return listener;
207 }
208
209 /**
210  * AccessibleKeystrokeListener_addCallback:
211  * @listener: the #AccessibleKeystrokeListener instance to modify.
212  * @callback: an #AccessibleKeystrokeListenerCB function pointer.
213  * @user_data: a pointer to data which will be passed to the callback when invoked.
214  *
215  * Add an in-process callback function to an existing #AccessibleKeystrokeListener.
216  *
217  * Returns: #TRUE if successful, otherwise #FALSE.
218  *
219  **/
220 SPIBoolean
221 AccessibleKeystrokeListener_addCallback (AccessibleKeystrokeListener *listener,
222                                          AccessibleKeystrokeListenerCB callback,
223                                          void                         *user_data)
224 {
225   cspi_keystroke_listener_add_cb (listener, callback, user_data);
226   return TRUE;
227 }
228
229 /**
230  * AccessibleKeystrokeListener_removeCallback:
231  * @listener: the #AccessibleKeystrokeListener instance to modify.
232  * @callback: an #AccessibleKeystrokeListenerCB function pointer.
233  *
234  * Remove an in-process callback function from an existing #AccessibleKeystrokeListener.
235  *
236  * Returns: #TRUE if successful, otherwise #FALSE.
237  *
238  **/
239 SPIBoolean
240 AccessibleKeystrokeListener_removeCallback (AccessibleKeystrokeListener *listener,
241                                             AccessibleKeystrokeListenerCB callback)
242 {
243   cspi_keystroke_listener_remove_cb (listener, callback);
244   return TRUE;
245 }
246
247 /**
248  * AccessibleKeystrokeListener_unref:
249  * @listener: a pointer to the #AccessibleKeystrokeListener being operated on.
250  *
251  * Decrements an #AccessibleKeystrokeListener's reference count.
252  **/
253 void
254 AccessibleKeystrokeListener_unref (AccessibleKeystrokeListener *listener)
255 {
256   cspi_keystroke_listener_unref (listener);
257 }