bug 643384 - atspi_register_keystroke_listener() should take a bitmask not AtspiKeyLi...
[platform/upstream/at-spi2-core.git] / atspi / atspi-registry.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /* atspi_registry.c: Global functions wrapping the registry */
25
26 #include "atspi-private.h"
27
28 /**
29  * atspi_get_desktop_count:
30  *
31  * Get the number of virtual desktops.
32  * NOTE: currently multiple virtual desktops are not implemented, this
33  *       function always returns '1'.
34  *
35  * Returns: an integer indicating the number of active virtual desktops.
36  **/
37 gint
38 atspi_get_desktop_count ()
39 {
40   return 1;
41 }
42
43 /**
44  * atspi_get_desktop:
45  * @i: an integer indicating which of the accessible desktops is to be returned.
46  *
47  * Get the virtual desktop indicated by index @i.
48  * NOTE: currently multiple virtual desktops are not implemented.
49  *
50  * Returns: (transfer full): a pointer to the 'i-th' virtual desktop's
51  * #AtspiAccessible representation.
52  **/
53 AtspiAccessible*
54 atspi_get_desktop (gint i)
55 {
56   if (i != 0) return NULL;
57   return _atspi_ref_accessible (atspi_bus_registry, atspi_path_root);
58 }
59
60 /**
61  * atspi_get_desktop_list:
62  *
63  * Get the list of virtual desktops.  On return, @list will point
64  *     to a newly-created, NULL terminated array of virtual desktop
65  *     pointers.
66  *     It is the responsibility of the caller to free this array when
67  *     it is no longer needed.
68  *
69  * Not Yet Implemented : this implementation always returns a single
70  * #Accessible desktop.
71  *
72  * Returns: (transfer full): a #GArray of desktops.
73  **/
74 GArray *
75 atspi_get_desktop_list ()
76 {
77   GArray *array = g_array_new (TRUE, TRUE, sizeof (AtspiAccessible *));
78   AtspiAccessible *desktop;
79
80   desktop = _atspi_ref_accessible (atspi_bus_registry, atspi_path_root);
81   if (array)
82     g_array_index (array, AtspiAccessible *, 0) = desktop;
83   return array;
84 }
85
86 /**
87  * ATSPI_KEYSET_ALL_KEYS:
88  * @ATSPI_KEYSET_ALL_KEYS: A special value for an AtspiKeySet type, which tacitly
89  *                       includes all keycodes and keyvals for the specified modifier set.
90  **/
91
92 /**
93  * atspi_register_keystroke_listener:
94  * @listener:  a pointer to the #AtspiDeviceListener for which
95  *             keystroke events are requested.
96  * @key_set: (element-type AtspiKeyDefinition): a pointer to the
97  *        #AtspiKeyDefinition array indicating which keystroke events are
98  *        requested, or #ATSPI_KEYSET_ALL_KEYS
99  *        to indicate that all keycodes and keyvals for the specified
100  *        modifier set are to be included.
101  * @modmask:   an #AtspiKeyMaskType mask indicating which
102  *             key event modifiers must be set in combination with @keys,
103  *             events will only be reported for key events for which all
104  *             modifiers in @modmask are set.  If you wish to listen for
105  *             events with multiple modifier combinations you must call
106  *             register_keystroke_listener() once for each
107  *             combination.
108  * @event_types: an #AtspiKeyMaskType mask indicating which
109  *             types of key events are requested (#ATSPI_KEY_PRESSED, etc.).
110  * @sync_type: a #AtspiKeyListenerSyncType parameter indicating
111  *             the behavior of the notification/listener transaction.
112  *             
113  * Register a listener for keystroke events, either pre-emptively for
114  *             all windows (ATSPI_KEYLISTENER_ALL_WINDOWS),
115  *             non-preemptively (ATSPI_KEYLISTENER_NOSYNC), or
116  *             pre-emptively at the toolkit level (ATSPI_KEYLISTENER_CANCONSUME).
117  *             If ALL_WINDOWS or CANCONSUME are used, the event is consumed
118  *             upon receipt if one of @listener's callbacks returns #TRUE.
119  *             ( Other sync_type values may be available in the future )
120  *
121  * Returns: #TRUE if successful, otherwise #FALSE.
122  **/
123 gboolean
124 atspi_register_keystroke_listener (AtspiDeviceListener  *listener,
125                                          GArray             *key_set,
126                                          AtspiKeyMaskType         modmask,
127                                          AtspiKeyEventMask        event_types,
128                                          gint sync_type, GError **error)
129 {
130   GArray *d_key_set;
131   gchar *path = _atspi_device_listener_get_path (listener);
132   gint                                i;
133   dbus_uint32_t d_modmask = modmask;
134   dbus_uint32_t d_event_types = event_types;
135   AtspiEventListenerMode     listener_mode;
136   gboolean                          retval = FALSE;
137   DBusError d_error;
138
139   if (!listener)
140     {
141       return retval;
142     }
143
144   /* copy the keyval filter values from the C api into the DBind KeySet */
145   if (key_set)
146     {
147       d_key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), key_set->len);
148       d_key_set->len = key_set->len;
149       for (i = 0; i < key_set->len; ++i)
150         {
151           AtspiKeyDefinition *kd =  ((AtspiKeyDefinition *) key_set->data) + i;
152           AtspiKeyDefinition *d_kd =  ((AtspiKeyDefinition *) d_key_set->data) + i;
153           d_kd->keycode = kd->keycode;
154           d_kd->keysym = kd->keysym;
155           if (kd->keystring)
156             {
157               d_kd->keystring = kd->keystring;
158             } 
159           else 
160             {
161               d_kd->keystring = "";
162             }
163         }
164     }
165   else
166     {
167       d_key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), 0);
168     }
169         
170   listener_mode.synchronous =
171           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_SYNCHRONOUS)!=0);
172   listener_mode.preemptive =
173           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_CANCONSUME)!=0);
174   listener_mode.global =
175           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_ALL_WINDOWS)!=0);
176
177     dbus_error_init (&d_error);
178     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "RegisterKeystrokeListener", &d_error, "oa(iisi)uu(bbb)=>b", path, d_key_set, d_modmask, d_event_types, &listener_mode, &retval);
179
180   g_array_free (d_key_set, TRUE);
181   g_free (path);
182
183   return retval;
184 }
185
186 /**
187  * atspi_deregister_keystroke_listener:
188  * @listener: a pointer to the #AtspiDeviceListener for which
189  *            keystroke events are requested.
190  * @key_set: (element-type AtspiKeyDefinition): a pointer to the
191  *        #AtspiKeyDefinition array indicating which keystroke events are
192  *        requested, or #ATSPI_KEYSET_ALL_KEYS
193  *        to indicate that all keycodes and keyvals for the specified
194  *        modifier set are to be included.
195  * @modmask:  the key modifier mask for which this listener is to be
196  *            'deregistered' (of type #AtspiKeyMaskType).
197  * @event_types: an #AtspiKeyMaskType mask indicating which
198  *             types of key events were requested (#ATSPI_KEY_PRESSED, etc.).
199  *
200  * Removes a keystroke event listener from the registry's listener queue,
201  *            ceasing notification of events with modifiers matching @modmask.
202  *
203  * Returns: #TRUE if successful, otherwise #FALSE.
204  **/
205 gboolean
206 atspi_deregister_keystroke_listener (AtspiDeviceListener *listener,
207                                      GArray              *key_set,
208                                      AtspiKeyMaskType     modmask,
209                                      AtspiKeyEventMask    event_types,
210                                      GError             **error)
211 {
212   gchar *path = _atspi_device_listener_get_path (listener);
213   dbus_uint32_t d_modmask = modmask;
214   dbus_uint32_t d_event_types = event_types;
215   DBusError d_error;
216
217   dbus_error_init (&d_error);
218   if (!listener)
219     {
220       return FALSE;
221     }
222
223   dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry,
224                                atspi_path_dec, atspi_interface_dec,
225                                "DeregisterKeystrokeListener", &d_error,
226                                "oa(iisi)uu", path, &key_set, d_modmask,
227                                d_event_types);
228   g_free (path);
229   return TRUE;
230 }
231
232 /**
233  * atspi_register_device_event_listener:
234  * @listener:  a pointer to the #AtspiDeviceListener which requests
235  *             the events.
236  * @event_types: an #AtspiDeviceEventMask mask indicating which
237  *             types of key events are requested (#ATSPI_KEY_PRESSED, etc.).
238  * @filter: Unused parameter.
239  *             
240  * Register a listener for device events, for instance button events.
241  *
242  * Returns: #TRUE if successful, otherwise #FALSE.
243  **/
244 gboolean
245 atspi_register_device_event_listener (AtspiDeviceListener  *listener,
246                                  AtspiDeviceEventMask  event_types,
247                                  void                      *filter, GError **error)
248 {
249   gboolean                          retval = FALSE;
250   dbus_uint32_t d_event_types = event_types;
251   gchar *path = _atspi_device_listener_get_path (listener);
252   DBusError d_error;
253
254   dbus_error_init (&d_error);
255   if (!listener)
256     {
257       return retval;
258     }
259
260     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "RegisterDeviceEventListener", &d_error, "ou=>b", path, d_event_types, &retval);
261   g_free (path);
262   return retval;
263 }
264
265 /**
266  * atspi_deregister_device_event_listener:
267  * @listener: a pointer to the #AtspiDeviceListener for which
268  *            device events are requested.
269  * @filter: Unused parameter.
270  *
271  * Removes a device event listener from the registry's listener queue,
272  *            ceasing notification of events of the specified type.
273  *
274  * Returns: #TRUE if successful, otherwise #FALSE.
275  **/
276 gboolean
277 atspi_deregister_device_event_listener (AtspiDeviceListener *listener,
278                                    void                     *filter, GError **error)
279 {
280   dbus_uint32_t event_types = 0;
281   gchar *path = _atspi_device_listener_get_path (listener);
282   DBusError d_error;
283
284   dbus_error_init (&d_error);
285
286   if (!listener)
287     {
288       return FALSE;
289     }
290
291   event_types |= (1 << ATSPI_BUTTON_PRESSED_EVENT);
292   event_types |= (1 << ATSPI_BUTTON_RELEASED_EVENT);
293
294     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "DeregisterDeviceEventListener", &d_error, "ou", path, event_types);
295   g_free (path);
296   return TRUE;
297 }
298
299 /**
300  * atspi_generate_keyboard_event:
301  * @keyval: a long integer indicating the keycode or keysym of the key event
302  *           being synthesized.
303  * @keystring: an (optional) UTF-8 string which, if @keyval is NULL,
304  *           indicates a 'composed' keyboard input string which is 
305  *           being synthesized; this type of keyboard event synthesis does
306  *           not emulate hardware keypresses but injects the string 
307  *           as though a composing input method (such as XIM) were used.
308  * @synth_type: a #AtspiKeySynthType flag indicating whether @keyval
309  *           is to be interpreted as a keysym rather than a keycode
310  *           (ATSPI_KEYSYM), or whether to synthesize
311  *           ATSPI_KEY_PRESS, ATSPI_KEY_RELEASE, or both (ATSPI_KEY_PRESSRELEASE).
312  *
313  * Synthesize a keyboard event (as if a hardware keyboard event occurred in the
314  * current UI context).
315  *
316  * Returns: #TRUE if successful, otherwise #FALSE.
317  **/
318 gboolean
319 atspi_generate_keyboard_event (glong keyval,
320                            const gchar *keystring,
321                            AtspiKeySynthType synth_type, GError **error)
322 {
323   dbus_uint32_t d_synth_type = synth_type;
324   dbus_int32_t d_keyval = keyval;
325   DBusError d_error;
326
327   dbus_error_init (&d_error);
328   if (!keystring) keystring = "";
329     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "GenerateKeyboardEvent", &d_error, "isu", d_keyval, keystring, d_synth_type);
330
331   return TRUE;
332 }
333
334 /**
335  * atspi_generate_mouse_event:
336  * @x: a #long indicating the screen x coordinate of the mouse event.
337  * @y: a #long indicating the screen y coordinate of the mouse event.
338  * @name: a string indicating which mouse event to be synthesized
339  *        (e.g. "b1p", "b1c", "b2r", "rel", "abs").
340  *
341  * Synthesize a mouse event at a specific screen coordinate.
342  * Most AT clients should use the #AccessibleAction interface when
343  * tempted to generate mouse events, rather than this method.
344  * Event names: b1p = button 1 press; b2r = button 2 release;
345  *              b3c = button 3 click; b2d = button 2 double-click;
346  *              abs = absolute motion; rel = relative motion.
347  *
348  * Returns: #TRUE if successful, otherwise #FALSE.
349  **/
350 gboolean
351 atspi_generate_mouse_event (glong x, glong y, const gchar *name, GError **error)
352 {
353   dbus_int32_t d_x = x, d_y = y;
354   DBusError d_error;
355
356   dbus_error_init (&d_error);
357     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry,
358                                  atspi_path_dec, atspi_interface_dec,
359                                  "GenerateMouseEvent", &d_error, "iis",
360                                  d_x, d_y, name);
361   return TRUE;
362 }
363
364 AtspiKeyDefinition *
365 atspi_key_definition_copy (AtspiKeyDefinition *src)
366 {
367   AtspiKeyDefinition *dst;
368
369   dst = g_new0 (AtspiKeyDefinition, 1);
370   if (!dst)
371     return NULL;
372   dst->keycode = src->keycode;
373   dst->keysym = src->keysym;
374   if (src->keystring)
375     dst->keystring = g_strdup (src->keystring);
376   dst->unused = src->unused;
377   return dst;
378 }
379
380 void
381 atspi_key_definition_free (AtspiKeyDefinition *kd)
382 {
383   if (kd->keystring)
384     g_free (kd->keystring);
385   g_free (kd);
386 }
387
388 G_DEFINE_BOXED_TYPE (AtspiKeyDefinition, atspi_key_definition, atspi_key_definition_copy, atspi_key_definition_free)