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