Various bug fixes
[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 AccessibleKeySet type, which tacitly
91  *                       includes all keycodes and keyvals for the specified modifier set.
92  **/
93
94 /**
95  * atspi_register_accessible_keystroke_listener:
96  * @listener:  a pointer to the #AccessibleKeystrokeListener for which
97  *             keystroke events are requested.
98  * @keys:      a pointer to the #AccessibleKeySet indicating which
99  *             keystroke events are requested, or #ATSPI_KEYSET_ALL_KEYS
100  *             to indicate that all keycodes and keyvals for the specified
101  *             modifier set are to be included.
102  * @modmask:   an #AccessibleKeyMaskType mask indicating which
103  *             key event modifiers must be set in combination with @keys,
104  *             events will only be reported for key events for which all
105  *             modifiers in @modmask are set.  If you wish to listen for
106  *             events with multiple modifier combinations you must call
107  *             registerAccessibleKeystrokeListener() once for each combination.
108  * @eventmask: an #AccessibleKeyMaskType mask indicating which
109  *             types of key events are requested (#ATSPI_KEY_PRESSED, etc.).
110  * @sync_type: a #AccessibleKeyListenerSyncType 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_accessible_keystroke_listener (AtspiKeystrokeListener  *listener,
125                                          AtspiKeySet             *keys,
126                                          AtspiKeyMaskType         modmask,
127                                          AtspiKeyEventMask        eventmask,
128                                          AtspiKeyListenerSyncType sync_type, GError **error)
129 {
130   gchar *path = _atspi_device_listener_get_path (listener);
131   gint                                i;
132   GArray *key_set;
133   dbus_uint32_t key_events = 0;
134   AtspiControllerEventMask   controller_event_mask;
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 (keys)
146     {
147       key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), keys->len);
148       key_set->len = keys->len;
149       for (i = 0; i < keys->len; ++i)
150         {
151           AtspiKeyDefinition *kd =  ((AtspiKeyDefinition *) key_set->data) + i;
152           kd->keycode = keys->keycodes[i];
153           kd->keysym = keys->keysyms[i];
154           if (keys->keystrings && keys->keystrings[i])
155             {
156               kd->keystring = keys->keystrings[i];
157             } 
158           else 
159             {
160               kd->keystring = "";
161             }
162         }
163     }
164   else
165     {
166       key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), 0);
167     }
168         
169   /* copy the event filter values from the C api into the DBus key_events */
170   if (eventmask & ATSPI_KEY_PRESSED)
171     {
172       key_events |= (1 << ATSPI_KEY_PRESSED_EVENT);
173     }
174   if (eventmask & ATSPI_KEY_RELEASED)
175     {
176       key_events |= (1 << ATSPI_KEY_RELEASED_EVENT);
177     }
178   
179   controller_event_mask = (dbus_uint32_t) modmask;
180
181   listener_mode.synchronous =
182           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_SYNCHRONOUS)!=0);
183   listener_mode.preemptive =
184           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_CANCONSUME)!=0);
185   listener_mode.global =
186           (dbus_bool_t) ((sync_type & ATSPI_KEYLISTENER_ALL_WINDOWS)!=0);
187
188     dbus_error_init (&d_error);
189     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "RegisterKeystrokeListener", &d_error, "oa(iisi)uu(bbb)=>b", path, key_set, controller_event_mask, key_events, &listener_mode, &retval);
190
191   g_array_free (key_set, TRUE);
192   g_free (path);
193
194   return retval;
195 }
196
197 /**
198  * atspi_deregister_accessible_keystroke_listener:
199  * @listener: a pointer to the #AccessibleKeystrokeListener for which
200  *            keystroke events are requested.
201  * @modmask:  the key modifier mask for which this listener is to be
202  *            'deregistered' (of type #AtspiKeyMaskType).
203  *
204  * Removes a keystroke event listener from the registry's listener queue,
205  *            ceasing notification of events with modifiers matching @modmask.
206  *
207  * Returns: #TRUE if successful, otherwise #FALSE.
208  **/
209 gboolean
210 atspi_deregister_accessible_keystroke_listener (AtspiKeystrokeListener *listener,
211                                            AtspiKeyMaskType        modmask, GError **error)
212 {
213   gchar *path = _atspi_device_listener_get_path (listener);
214   AtspiControllerEventMask   controller_event_mask;
215   GArray *key_set;
216   dbus_uint32_t key_events = 0;
217   DBusError d_error;
218
219   dbus_error_init (&d_error);
220   if (!listener)
221     {
222       return FALSE;
223     }
224
225   controller_event_mask = (dbus_uint32_t) modmask;
226
227       key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), 0);
228     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "DeregisterKeystrokeListener", &d_error, "oa(iisi)uu", path, &key_set, key_events, controller_event_mask);
229   g_free (path);
230   return TRUE;
231 }
232
233 /**
234  * atspi_register_device_event_listener:
235  * @listener:  a pointer to the #AtspiDeviceListener which requests
236  *             the events.
237  * @eventmask: an #AtspiDeviceEventMask mask indicating which
238  *             types of key events are requested (#ATSPI_KEY_PRESSED, etc.).
239  * @filter: Unused parameter.
240  *             
241  * Register a listener for device events, for instance button events.
242  *
243  * Returns: #TRUE if successful, otherwise #FALSE.
244  **/
245 gboolean
246 atspi_register_device_event_listener (AtspiDeviceListener  *listener,
247                                  AtspiDeviceEventMask  event_mask,
248                                  void                      *filter, GError **error)
249 {
250   gboolean                          retval = FALSE;
251   dbus_uint32_t d_event_mask = event_mask;
252   gint                                i;
253   gchar *path = _atspi_device_listener_get_path (listener);
254   DBusError d_error;
255
256   dbus_error_init (&d_error);
257   if (!listener)
258     {
259       return retval;
260     }
261
262     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "RegisterDeviceEventListener", &d_error, "ou=>b", path, d_event_mask, &retval);
263   g_free (path);
264   return retval;
265 }
266
267 /**
268  * atspi_deregister_device_event_listener:
269  * @listener: a pointer to the #AtspiDeviceListener for which
270  *            device events are requested.
271  * @filter: Unused parameter.
272  *
273  * Removes a device event listener from the registry's listener queue,
274  *            ceasing notification of events of the specified type.
275  *
276  * Returns: #TRUE if successful, otherwise #FALSE.
277  **/
278 gboolean
279 atspi_deregister_device_event_listener (AtspiDeviceListener *listener,
280                                    void                     *filter, GError **error)
281 {
282   dbus_uint32_t event_types = 0;
283   gchar *path = _atspi_device_listener_get_path (listener);
284   DBusError d_error;
285
286   dbus_error_init (&d_error);
287
288   if (!listener)
289     {
290       return FALSE;
291     }
292
293   event_types |= (1 << ATSPI_BUTTON_PRESSED_EVENT);
294   event_types |= (1 << ATSPI_BUTTON_RELEASED_EVENT);
295
296     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "DeregisterDeviceEventListener", &d_error, "ou", path, event_types);
297   g_free (path);
298   return TRUE;
299 }
300
301 /**
302  * atspi_generate_keyboard_event:
303  * @keyval: a long integer indicating the keycode or keysym of the key event
304  *           being synthesized.
305  * @keystring: an (optional) UTF-8 string which, if @keyval is NULL,
306  *           indicates a 'composed' keyboard input string which is 
307  *           being synthesized; this type of keyboard event synthesis does
308  *           not emulate hardware keypresses but injects the string 
309  *           as though a composing input method (such as XIM) were used.
310  * @synth_type: a #AccessibleKeySynthType flag indicating whether @keyval
311  *           is to be interpreted as a keysym rather than a keycode
312  *           (ATSPI_KEYSYM), or whether to synthesize
313  *           ATSPI_KEY_PRESS, ATSPI_KEY_RELEASE, or both (ATSPI_KEY_PRESSRELEASE).
314  *
315  * Synthesize a keyboard event (as if a hardware keyboard event occurred in the
316  * current UI context).
317  *
318  * Returns: #TRUE if successful, otherwise #FALSE.
319  **/
320 gboolean
321 atspi_generate_keyboard_event (glong keyval,
322                            const gchar *keystring,
323                            AtspiKeySynthType synth_type, GError **error)
324 {
325   dbus_uint32_t d_synth_type = synth_type;
326   dbus_int32_t d_keyval = keyval;
327   DBusError d_error;
328
329   dbus_error_init (&d_error);
330   if (!keystring) keystring = "";
331     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);
332
333   return TRUE;
334 }
335
336 /**
337  * atspi_generate_mouse_event:
338  * @x: a #long indicating the screen x coordinate of the mouse event.
339  * @y: a #long indicating the screen y coordinate of the mouse event.
340  * @name: a string indicating which mouse event to be synthesized
341  *        (e.g. "b1p", "b1c", "b2r", "rel", "abs").
342  *
343  * Synthesize a mouse event at a specific screen coordinate.
344  * Most AT clients should use the #AccessibleAction interface when
345  * tempted to generate mouse events, rather than this method.
346  * Event names: b1p = button 1 press; b2r = button 2 release;
347  *              b3c = button 3 click; b2d = button 2 double-click;
348  *              abs = absolute motion; rel = relative motion.
349  *
350  * Returns: #TRUE if successful, otherwise #FALSE.
351  **/
352 gboolean
353 atspi_generate_mouse_event (glong x, glong y, const gchar *name, GError **error)
354 {
355   dbus_int32_t dbus_x = x, dbus__y = y;
356   DBusError d_error;
357
358   dbus_error_init (&d_error);
359     dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "GenerateMouseEvent", &d_error, "iis", x, y, name);
360   return TRUE;
361 }