Imported version 2.7.91
[platform/core/uifw/at-spi2-core.git] / atspi / atspi-registry.c
1
2 /*
3  * AT-SPI - Assistive Technology Service Provider Interface
4  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5  *
6  * Copyright 2001, 2002 Sun Microsystems Inc.,
7  * Copyright 2001, 2002 Ximian, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /* atspi_registry.c: Global functions wrapping the registry */
26
27 #include "atspi-private.h"
28
29 typedef struct
30 {
31   AtspiDeviceListener *listener;
32   GArray             *key_set;
33   AtspiKeyMaskType         modmask;
34   AtspiKeyEventMask        event_types;
35   gint sync_type;
36 } DeviceListenerEntry;
37
38 static GList *device_listeners;
39
40 /**
41  * atspi_get_desktop_count:
42  *
43  * Gets the number of virtual desktops.
44  * NOTE: multiple virtual desktops are not implemented yet; as a 
45  * consequence, this function always returns 1.
46  *
47  * Returns: a #gint indicating the number of active virtual desktops.
48  **/
49 gint
50 atspi_get_desktop_count ()
51 {
52   return 1;
53 }
54
55 /**
56  * atspi_get_desktop:
57  * @i: a #gint indicating which of the accessible desktops is to be returned.
58  *
59  * Gets the virtual desktop indicated by index @i.
60  * NOTE: currently multiple virtual desktops are not implemented;
61  * as a consequence, any @i value different from 0 will not return a
62  * virtual desktop - instead it will return NULL.
63  *
64  * Returns: (transfer full): a pointer to the @i-th virtual desktop's
65  * #AtspiAccessible representation.
66  **/
67 AtspiAccessible*
68 atspi_get_desktop (gint i)
69 {
70   if (i != 0) return NULL;
71   return _atspi_ref_accessible (atspi_bus_registry, atspi_path_root);
72 }
73
74 /**
75  * atspi_get_desktop_list:
76  *
77  * Gets the list of virtual desktops.  On return, @list will point
78  *     to a newly-created, NULL terminated array of virtual desktop
79  *     pointers.
80  *     It is the responsibility of the caller to free this array when
81  *     it is no longer needed.
82  * NOTE: currently multiple virtual desktops are not implemented;
83  * this implementation always returns a #Garray with a single
84  * #AtspiAccessible desktop.
85  *
86  * Returns: (transfer full): a #GArray of desktops.
87  **/
88 GArray *
89 atspi_get_desktop_list ()
90 {
91   GArray *array = g_array_new (TRUE, TRUE, sizeof (AtspiAccessible *));
92   AtspiAccessible *desktop;
93
94   desktop = _atspi_ref_accessible (atspi_bus_registry, atspi_path_root);
95   if (array)
96     g_array_index (array, AtspiAccessible *, 0) = desktop;
97   return array;
98 }
99
100 static gboolean
101 notify_keystroke_listener (DeviceListenerEntry *e)
102 {
103   gchar *path = _atspi_device_listener_get_path (e->listener);
104   dbus_uint32_t d_modmask = e->modmask;
105   dbus_uint32_t d_event_types = e->event_types;
106   AtspiEventListenerMode     listener_mode;
107   gboolean                          retval = FALSE;
108   DBusError d_error;
109
110   listener_mode.synchronous =
111           (dbus_bool_t) ((e->sync_type & ATSPI_KEYLISTENER_SYNCHRONOUS)!=0);
112   listener_mode.preemptive =
113           (dbus_bool_t) ((e->sync_type & ATSPI_KEYLISTENER_CANCONSUME)!=0);
114   listener_mode.global =
115           (dbus_bool_t) ((e->sync_type & ATSPI_KEYLISTENER_ALL_WINDOWS)!=0);
116
117   dbus_error_init (&d_error);
118   dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry,
119                                atspi_path_dec, atspi_interface_dec,
120                                "RegisterKeystrokeListener", &d_error,
121                                "oa(iisi)uu(bbb)=>b", path, e->key_set,
122                                d_modmask, d_event_types, &listener_mode,
123                                &retval);
124   if (dbus_error_is_set (&d_error))
125     {
126       g_warning ("RegisterKeystrokeListener failed: %s", d_error.message);
127       dbus_error_free (&d_error);
128     }
129
130   g_free (path);
131
132   return retval;
133 }
134
135 static void
136 device_listener_entry_free (DeviceListenerEntry *e)
137 {
138   g_array_free (e->key_set, TRUE);
139   g_free (e);
140 }
141
142 static void
143 unregister_listener (gpointer data, GObject *object)
144 {
145   GList *l;
146   AtspiDeviceListener *listener = ATSPI_DEVICE_LISTENER (object);
147
148   for (l = device_listeners; l;)
149     {
150       DeviceListenerEntry *e = l->data;
151       if (e->listener == listener)
152         {
153           GList *next = l->next;
154           device_listener_entry_free (e);
155           device_listeners = g_list_delete_link (device_listeners, l);
156           l = next;
157         }
158       else
159         l = l->next;
160     }
161 }
162
163 /**
164  * atspi_register_keystroke_listener:
165  * @listener:  a pointer to the #AtspiDeviceListener for which
166  *             keystroke events are requested.
167  * @key_set: (element-type AtspiKeyDefinition) (allow-none): a pointer to the
168  *        #AtspiKeyDefinition array indicating which keystroke events are
169  *        requested, or NULL
170  *        to indicate that all keycodes and keyvals for the specified
171  *        modifier set are to be included.
172  * @modmask:   an #AtspiKeyMaskType mask indicating which
173  *             key event modifiers must be set in combination with @keys,
174  *             events will only be reported for key events for which all
175  *             modifiers in @modmask are set.  If you wish to listen for
176  *             events with multiple modifier combinations, you must call
177  *             #atspi_register_keystroke_listener once for each
178  *             combination.
179  * @event_types: an #AtspiKeyMaskType mask indicating which
180  *             types of key events are requested (%ATSPI_KEY_PRESSED etc.).
181  * @sync_type: an #AtspiKeyListenerSyncType parameter indicating
182  *             the behavior of the notification/listener transaction.
183  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
184  *             
185  * Registers a listener for keystroke events, either pre-emptively for
186  *             all windows (%ATSPI_KEYLISTENER_ALL_WINDOWS),
187  *             non-preemptively (%ATSPI_KEYLISTENER_NOSYNC), or
188  *             pre-emptively at the toolkit level (%ATSPI_KEYLISTENER_CANCONSUME).
189  *             If ALL_WINDOWS or CANCONSUME are used, the event is consumed
190  *             upon receipt if one of @listener's callbacks returns %TRUE 
191  *             (other sync_type values may be available in the future).
192  *
193  * Returns: %TRUE if successful, otherwise %FALSE.
194  **/
195 gboolean
196 atspi_register_keystroke_listener (AtspiDeviceListener  *listener,
197                                          GArray             *key_set,
198                                          AtspiKeyMaskType         modmask,
199                                          AtspiKeyEventMask        event_types,
200                                          gint sync_type, GError **error)
201 {
202   DeviceListenerEntry *e;
203
204   g_return_val_if_fail (listener != NULL, FALSE);
205
206   e = g_new0 (DeviceListenerEntry, 1);
207   e->listener = listener;
208   e->modmask = modmask;
209   e->event_types = event_types;
210   e->sync_type = sync_type;
211   if (key_set)
212     {
213       gint i;
214       e->key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition),
215                                       key_set->len);
216       e->key_set->len = key_set->len;
217       for (i = 0; i < key_set->len; i++)
218         {
219           AtspiKeyDefinition *kd =  ((AtspiKeyDefinition *) key_set->data) + i;
220           AtspiKeyDefinition *d_kd =  ((AtspiKeyDefinition *) e->key_set->data) + i;
221           d_kd->keycode = kd->keycode;
222           d_kd->keysym = kd->keysym;
223           if (kd->keystring)
224             {
225               d_kd->keystring = kd->keystring;
226             } 
227           else 
228             {
229               d_kd->keystring = "";
230             }
231         }
232     }
233   else
234     {
235       e->key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), 0);
236     }
237
238   g_object_weak_ref (G_OBJECT (listener), unregister_listener, NULL);
239   device_listeners = g_list_prepend (device_listeners, e);
240   return notify_keystroke_listener (e);
241 }
242
243 /**
244  * atspi_deregister_keystroke_listener:
245  * @listener: a pointer to the #AtspiDeviceListener for which
246  *            keystroke events are requested.
247  * @key_set: (element-type AtspiKeyDefinition) (allow-none): a pointer to the
248  *        #AtspiKeyDefinition array indicating which keystroke events are
249  *        requested, or %NULL
250  *        to indicate that all keycodes and keyvals for the specified
251  *        modifier set are to be included.
252  * @modmask:  the key modifier mask for which this listener is to be
253  *            'deregistered' (of type #AtspiKeyMaskType).
254  * @event_types: an #AtspiKeyMaskType mask indicating which
255  *             types of key events were requested (%ATSPI_KEY_PRESSED, etc.).
256  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
257  *
258  * Removes a keystroke event listener from the registry's listener queue,
259  *            ceasing notification of events with modifiers matching @modmask.
260  *
261  * Returns: %TRUE if successful, otherwise %FALSE.
262  **/
263 gboolean
264 atspi_deregister_keystroke_listener (AtspiDeviceListener *listener,
265                                      GArray              *key_set,
266                                      AtspiKeyMaskType     modmask,
267                                      AtspiKeyEventMask    event_types,
268                                      GError             **error)
269 {
270   GArray *d_key_set;
271   gchar *path = _atspi_device_listener_get_path (listener);
272   gint i;
273   dbus_uint32_t d_modmask = modmask;
274   dbus_uint32_t d_event_types = event_types;
275   DBusError d_error;
276   GList *l;
277
278   dbus_error_init (&d_error);
279   if (!listener)
280     {
281       return FALSE;
282     }
283
284   /* copy the keyval filter values from the C api into the DBind KeySet */
285   if (key_set)
286     {
287       d_key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), key_set->len);
288       d_key_set->len = key_set->len;
289       for (i = 0; i < key_set->len; ++i)
290         {
291           AtspiKeyDefinition *kd =  ((AtspiKeyDefinition *) key_set->data) + i;
292           AtspiKeyDefinition *d_kd =  ((AtspiKeyDefinition *) d_key_set->data) + i;
293           d_kd->keycode = kd->keycode;
294           d_kd->keysym = kd->keysym;
295           if (kd->keystring)
296             {
297               d_kd->keystring = kd->keystring;
298             } 
299           else 
300             {
301               d_kd->keystring = "";
302             }
303         }
304     }
305   else
306     {
307       d_key_set = g_array_sized_new (FALSE, TRUE, sizeof (AtspiKeyDefinition), 0);
308     }
309
310   dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry,
311                                atspi_path_dec, atspi_interface_dec,
312                                "DeregisterKeystrokeListener", &d_error,
313                                "oa(iisi)uu", path, d_key_set, d_modmask,
314                                d_event_types);
315   if (dbus_error_is_set (&d_error))
316     {
317       g_warning ("DeregisterKeystrokeListener failed: %s", d_error.message);
318       dbus_error_free (&d_error);
319     }
320
321   unregister_listener (listener, NULL);
322   for (l = device_listeners; l;)
323     {
324       /* TODO: This code is all wrong / doesn't match what is in
325  *       deviceeventcontroller.c. It would be nice to deprecate these methods
326  *       in favor of methods that return an ID for the registration that can
327  *       be passed to a deregister function, for instance. */
328       DeviceListenerEntry *e = l->data;
329       if (e->modmask == modmask && e->event_types == event_types)
330         {
331           GList *next = l->next;
332           device_listener_entry_free (e);
333           device_listeners = g_list_delete_link (device_listeners, l);
334           l = next;
335         }
336       else
337         l = l->next;
338     }
339   g_array_free (d_key_set, TRUE);
340   g_free (path);
341   return TRUE;
342 }
343
344 /**
345  * atspi_register_device_event_listener:
346  * @listener:  a pointer to the #AtspiDeviceListener which requests
347  *             the events.
348  * @event_types: an #AtspiDeviceEventMask mask indicating which
349  *             types of key events are requested (%ATSPI_KEY_PRESSED, etc.).
350  * @filter: Unused parameter.
351  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
352  *             
353  * Registers a listener for device events, for instance button events.
354  *
355  * Returns: %TRUE if successful, otherwise %FALSE.
356  **/
357 gboolean
358 atspi_register_device_event_listener (AtspiDeviceListener  *listener,
359                                  AtspiDeviceEventMask  event_types,
360                                  void                      *filter, GError **error)
361 {
362   gboolean                          retval = FALSE;
363   dbus_uint32_t d_event_types = event_types;
364   gchar *path = _atspi_device_listener_get_path (listener);
365   DBusError d_error;
366
367   dbus_error_init (&d_error);
368   if (!listener)
369     {
370       return retval;
371     }
372
373     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);
374     if (dbus_error_is_set (&d_error))
375       {
376         g_warning ("RegisterDeviceEventListener failed: %s", d_error.message);
377         dbus_error_free (&d_error);
378       }
379
380   g_free (path);
381   return retval;
382 }
383
384 /**
385  * atspi_deregister_device_event_listener:
386  * @listener: a pointer to the #AtspiDeviceListener for which
387  *            device events are requested.
388  * @filter: Unused parameter.
389  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
390  *
391  * Removes a device event listener from the registry's listener queue,
392  *            ceasing notification of events of the specified type.
393  *
394  * Returns: %TRUE if successful, otherwise %FALSE.
395  **/
396 gboolean
397 atspi_deregister_device_event_listener (AtspiDeviceListener *listener,
398                                    void                     *filter, GError **error)
399 {
400   dbus_uint32_t event_types = 0;
401   gchar *path = _atspi_device_listener_get_path (listener);
402   DBusError d_error;
403
404   dbus_error_init (&d_error);
405
406   if (!listener)
407     {
408       return FALSE;
409     }
410
411   event_types |= (1 << ATSPI_BUTTON_PRESSED_EVENT);
412   event_types |= (1 << ATSPI_BUTTON_RELEASED_EVENT);
413
414   dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry, atspi_path_dec, atspi_interface_dec, "DeregisterDeviceEventListener", &d_error, "ou", path, event_types);
415   if (dbus_error_is_set (&d_error))
416     {
417       g_warning ("DeregisterDeviceEventListener failed: %s", d_error.message);
418       dbus_error_free (&d_error);
419     }
420
421   g_free (path);
422   return TRUE;
423 }
424
425 /**
426  * atspi_generate_keyboard_event:
427  * @keyval: a #gint indicating the keycode or keysym of the key event
428  *           being synthesized.
429  * @keystring: (allow-none): an (optional) UTF-8 string which, if
430  *           @synth_type is %ATSPI_KEY_STRING, indicates a 'composed'
431  *           keyboard input string being synthesized; this type of
432  *           keyboard event synthesis does not emulate hardware
433  *           keypresses but injects the string as though a composing
434  *           input method (such as XIM) were used.
435  * @synth_type: an #AtspiKeySynthType flag indicating whether @keyval
436  *           is to be interpreted as a keysym rather than a keycode
437  *           (%ATSPI_KEY_SYM) or a string (%ATSPI_KEY_STRING), or
438  *           whether to synthesize %ATSPI_KEY_PRESS,
439  *           %ATSPI_KEY_RELEASE, or both (%ATSPI_KEY_PRESSRELEASE).
440  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
441  *
442  * Synthesizes a keyboard event (as if a hardware keyboard event occurred in the
443  * current UI context).
444  *
445  * Returns: %TRUE if successful, otherwise %FALSE.
446  **/
447 gboolean
448 atspi_generate_keyboard_event (glong keyval,
449                            const gchar *keystring,
450                            AtspiKeySynthType synth_type, GError **error)
451 {
452   dbus_uint32_t d_synth_type = synth_type;
453   dbus_int32_t d_keyval = keyval;
454   DBusError d_error;
455
456   dbus_error_init (&d_error);
457   if (!keystring)
458     keystring = "";
459   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);
460   if (dbus_error_is_set (&d_error))
461     {
462       g_warning ("GenerateKeyboardEvent failed: %s", d_error.message);
463       dbus_error_free (&d_error);
464     }
465
466   return TRUE;
467 }
468
469 /**
470  * atspi_generate_mouse_event:
471  * @x: a #glong indicating the screen x coordinate of the mouse event.
472  * @y: a #glong indicating the screen y coordinate of the mouse event.
473  * @name: a string indicating which mouse event to be synthesized
474  *        (e.g. "b1p", "b1c", "b2r", "rel", "abs").
475  * @error: (allow-none): a pointer to a %NULL #GError pointer, or %NULL
476  *
477  * Synthesizes a mouse event at a specific screen coordinate.
478  * Most AT clients should use the #AccessibleAction interface when
479  * tempted to generate mouse events, rather than this method.
480  * Event names: b1p = button 1 press; b2r = button 2 release;
481  *              b3c = button 3 click; b2d = button 2 double-click;
482  *              abs = absolute motion; rel = relative motion.
483  *
484  * Returns: %TRUE if successful, otherwise %FALSE.
485  **/
486 gboolean
487 atspi_generate_mouse_event (glong x, glong y, const gchar *name, GError **error)
488 {
489   dbus_int32_t d_x = x, d_y = y;
490   DBusError d_error;
491
492   dbus_error_init (&d_error);
493   dbind_method_call_reentrant (_atspi_bus(), atspi_bus_registry,
494                                atspi_path_dec, atspi_interface_dec,
495                                "GenerateMouseEvent", &d_error, "iis",
496                                d_x, d_y, name);
497   if (dbus_error_is_set (&d_error))
498     {
499       g_warning ("GenerateMouseEvent failed: %s", d_error.message);
500       dbus_error_free (&d_error);
501     }
502
503   return TRUE;
504 }
505
506 AtspiKeyDefinition *
507 atspi_key_definition_copy (AtspiKeyDefinition *src)
508 {
509   AtspiKeyDefinition *dst;
510
511   dst = g_new0 (AtspiKeyDefinition, 1);
512   dst->keycode = src->keycode;
513   dst->keysym = src->keysym;
514   if (src->keystring)
515     dst->keystring = g_strdup (src->keystring);
516   dst->unused = src->unused;
517   return dst;
518 }
519
520 void
521 atspi_key_definition_free (AtspiKeyDefinition *kd)
522 {
523   if (kd->keystring)
524     g_free (kd->keystring);
525   g_free (kd);
526 }
527
528 void
529 _atspi_reregister_device_listeners ()
530 {
531   GList *l;
532   DeviceListenerEntry *e;
533
534   for (l = device_listeners; l; l = l->next)
535     {
536       e = l->data;
537       notify_keystroke_listener (e);
538     }
539 }
540 G_DEFINE_BOXED_TYPE (AtspiKeyDefinition, atspi_key_definition, atspi_key_definition_copy, atspi_key_definition_free)