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