2008-12-18 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi-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 /* spi_registry.c: Global functions wrapping the registry */
25
26 #include <cspi/spi-private.h>
27
28 static GArray *desktops;
29
30 /**
31  * SPI_getDesktopCount:
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 int
40 SPI_getDesktopCount ()
41 {
42   return 1;
43 }
44
45 /**
46  * SPI_getDesktop:
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, this
51  *       function always returns '1'.
52  *
53  * Returns: a pointer to the 'i-th' virtual desktop's #Accessible representation.
54  **/
55 Accessible*
56 SPI_getDesktop (int i)
57 {
58   if (i != 0) return NULL;
59   return cspi_ref_accessible (spi_bus_registry, NULL);
60 }
61
62 /**
63  * SPI_getDesktopList:
64  * @desktop_list: a pointer to an array of #Accessible references.
65  *
66  * Get the list of virtual desktops.  On return, @list will point
67  *     to a newly-created, NULL terminated array of virtual desktop
68  *     pointers.
69  *     It is the responsibility of the caller to free this array when
70  *     it is no longer needed.
71  *
72  * Not Yet Implemented : this implementation always returns a single
73  * #Accessible desktop.
74  *
75  * Returns: an integer indicating how many virtual desktops have been
76  *          placed in the list pointed to by parameter @list.
77  **/
78 int
79 SPI_getDesktopList (Accessible ***desktop_list)
80 {
81   Accessible **list;
82
83   list = g_new0 (Accessible *, 2);
84
85   if (!desktop_list) return 1;
86   list [0] = cspi_ref_accessible (spi_bus_registry, NULL);
87
88   *desktop_list = list;
89
90   return 1;
91 }
92
93 /**
94  * SPI_freeDesktopList:
95  * @desktop_list: a pointer to an array of #Accessible objects
96  * as returned from @SPI_getDesktopList
97  * 
98  * This routine frees the memory associated with the list.
99  **/
100 void
101 SPI_freeDesktopList (Accessible **desktop_list)
102 {
103   Accessible **p;
104   
105   for (p = desktop_list; p && *p; p++)
106     {
107       cspi_object_unref (*p);
108     }
109   g_free (desktop_list);
110 }
111
112 /**
113  * SPI_KEYSET_ALL_KEYS:
114  * @SPI_KEYSET_ALL_KEYS: A special value for an AccessibleKeySet type, which tacitly
115  *                       includes all keycodes and keyvals for the specified modifier set.
116  **/
117
118 /**
119  * SPI_registerAccessibleKeystrokeListener:
120  * @listener:  a pointer to the #AccessibleKeystrokeListener for which
121  *             keystroke events are requested.
122  * @keys:      a pointer to the #AccessibleKeySet indicating which
123  *             keystroke events are requested, or #SPI_KEYSET_ALL_KEYS
124  *             to indicate that all keycodes and keyvals for the specified
125  *             modifier set are to be included.
126  * @modmask:   an #AccessibleKeyMaskType mask indicating which
127  *             key event modifiers must be set in combination with @keys,
128  *             events will only be reported for key events for which all
129  *             modifiers in @modmask are set.  If you wish to listen for
130  *             events with multiple modifier combinations you must call
131  *             registerAccessibleKeystrokeListener() once for each combination.
132  * @eventmask: an #AccessibleKeyMaskType mask indicating which
133  *             types of key events are requested (#SPI_KEY_PRESSED, etc.).
134  * @sync_type: a #AccessibleKeyListenerSyncType parameter indicating
135  *             the behavior of the notification/listener transaction.
136  *             
137  * Register a listener for keystroke events, either pre-emptively for
138  *             all windows (SPI_KEYLISTENER_ALL_WINDOWS),
139  *             non-preemptively (SPI_KEYLISTENER_NOSYNC), or
140  *             pre-emptively at the toolkit level (SPI_KEYLISTENER_CANCONSUME).
141  *             If ALL_WINDOWS or CANCONSUME are used, the event is consumed
142  *             upon receipt if one of @listener's callbacks returns #TRUE.
143  *             ( Other sync_type values may be available in the future )
144  *
145  * Returns: #TRUE if successful, otherwise #FALSE.
146  **/
147 SPIBoolean
148 SPI_registerAccessibleKeystrokeListener (AccessibleKeystrokeListener  *listener,
149                                          AccessibleKeySet             *keys,
150                                          AccessibleKeyMaskType         modmask,
151                                          AccessibleKeyEventMask        eventmask,
152                                          AccessibleKeyListenerSyncType sync_type)
153 {
154   gchar *path = cspi_device_listener_get_path (listener);
155   gint                                i;
156   GArray *key_set;
157   dbus_uint32_t key_events = 0;
158   Accessibility_ControllerEventMask   controller_event_mask;
159   Accessibility_EventListenerMode     listener_mode;
160   DBusError error;
161   SPIBoolean                          retval = FALSE;
162
163   if (!listener)
164     {
165       return retval;
166     }
167
168   /* copy the keyval filter values from the C api into the DBind KeySet */
169   if (keys)
170     {
171       key_set = g_array_sized_new (FALSE, TRUE, sizeof (Accessibility_KeyDefinition), keys->len);
172       key_set->len = keys->len;
173       for (i = 0; i < keys->len; ++i)
174         {
175           Accessibility_KeyDefinition *kd =  ((Accessibility_KeyDefinition *) key_set->data) + i;
176           kd->keycode = keys->keycodes[i];
177           kd->keysym = keys->keysyms[i];
178           if (keys->keystrings && keys->keystrings[i])
179             {
180               kd->keystring = keys->keystrings[i];
181             } 
182           else 
183             {
184               kd->keystring = "";
185             }
186         }
187     }
188   else
189     {
190       key_set = g_array_sized_new (FALSE, TRUE, sizeof (Accessibility_KeyDefinition), 0);
191     }
192         
193   /* copy the event filter values from the C api into the DBus key_events */
194   if (eventmask & SPI_KEY_PRESSED)
195     {
196       key_events |= (1 << Accessibility_KEY_PRESSED_EVENT);
197     }
198   if (eventmask & SPI_KEY_RELEASED)
199     {
200       key_events |= (1 << Accessibility_KEY_RELEASED_EVENT);
201     }
202   
203   controller_event_mask = (dbus_uint32_t) modmask;
204
205   listener_mode.synchronous =
206           (dbus_bool_t) ((sync_type & SPI_KEYLISTENER_SYNCHRONOUS)!=0);
207   listener_mode.preemptive =
208           (dbus_bool_t) ((sync_type & SPI_KEYLISTENER_CANCONSUME)!=0);
209   listener_mode.global =
210           (dbus_bool_t) ((sync_type & SPI_KEYLISTENER_ALL_WINDOWS)!=0);
211
212     dbus_error_init (&error);
213     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "registerKeystrokeListener", &error, "oa(iisi)uu(bbb)=>b", path, key_set, controller_event_mask, key_set, &listener_mode, &retval);
214
215   g_array_free (key_set, TRUE);
216   g_free (path);
217
218   return retval;
219 }
220
221 /**
222  * SPI_deregisterAccessibleKeystrokeListener:
223  * @listener: a pointer to the #AccessibleKeystrokeListener for which
224  *            keystroke events are requested.
225  * @modmask:  the key modifier mask for which this listener is to be
226  *            'deregistered' (of type #AccessibleeyMaskType).
227  *
228  * Removes a keystroke event listener from the registry's listener queue,
229  *            ceasing notification of events with modifiers matching @modmask.
230  *
231  * Returns: #TRUE if successful, otherwise #FALSE.
232  **/
233 SPIBoolean
234 SPI_deregisterAccessibleKeystrokeListener (AccessibleKeystrokeListener *listener,
235                                            AccessibleKeyMaskType        modmask)
236 {
237   gchar *path = cspi_device_listener_get_path (listener);
238   Accessibility_ControllerEventMask   controller_event_mask;
239   GArray *key_set;
240   dbus_uint32_t key_events = 0;
241   DBusError error;
242
243   if (!listener)
244     {
245       return FALSE;
246     }
247
248
249   controller_event_mask = (dbus_uint32_t) modmask;
250
251       key_set = g_array_sized_new (FALSE, TRUE, sizeof (Accessibility_KeyDefinition), 0);
252     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "deregisterKeystrokeListener", &error, "oa(iisi)uu", path, &key_set, key_events, controller_event_mask);
253   g_free (path);
254   return TRUE;
255 }
256
257 /**
258  * SPI_registerDeviceEventListener:
259  * @listener:  a pointer to the #AccessibleDeviceListener which requests
260  *             the events.
261  * @eventmask: an #AccessibleDeviceEventMask mask indicating which
262  *             types of key events are requested (#SPI_KEY_PRESSED, etc.).
263  * @filter: Unused parameter.
264  *             
265  * Register a listener for device events, for instance button events.
266  *
267  * Returns: #TRUE if successful, otherwise #FALSE.
268  **/
269 SPIBoolean
270 SPI_registerDeviceEventListener (AccessibleDeviceListener  *listener,
271                                  AccessibleDeviceEventMask  eventmask,
272                                  void                      *filter)
273 {
274   SPIBoolean                          retval = FALSE;
275   dbus_uint32_t event_types = 0;
276   gint                                i;
277   gchar *path = cspi_device_listener_get_path (listener);
278   DBusError error;
279
280   if (!listener)
281     {
282       return retval;
283     }
284
285   /* copy the event filter values from the C api into the CORBA KeyEventTypeSeq */
286   
287   if (eventmask & SPI_BUTTON_PRESSED)
288     {
289       event_types |= (1 << Accessibility_BUTTON_PRESSED_EVENT);
290     }
291   if (eventmask & SPI_BUTTON_RELEASED)
292     {
293       event_types |= (1 << Accessibility_BUTTON_RELEASED_EVENT);
294     }
295
296   dbus_error_init (&error);
297     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "registerDeviceEventListener", &error, "ou=>b", path, event_types, &retval);
298   g_free (path);
299   return retval;
300 }
301
302 /**
303  * SPI_deregisterDeviceEventListener:
304  * @listener: a pointer to the #AccessibleDeviceListener for which
305  *            device events are requested.
306  * @filter: Unused parameter.
307  *
308  * Removes a device event listener from the registry's listener queue,
309  *            ceasing notification of events of the specified type.
310  *
311  * Returns: #TRUE if successful, otherwise #FALSE.
312  **/
313 SPIBoolean
314 SPI_deregisterDeviceEventListener (AccessibleDeviceListener *listener,
315                                    void                     *filter)
316 {
317   dbus_uint32_t event_types = 0;
318   gchar *path = cspi_device_listener_get_path (listener);
319   DBusError error;
320
321   if (!listener)
322     {
323       return FALSE;
324     }
325
326   event_types |= (1 << Accessibility_BUTTON_PRESSED_EVENT);
327   event_types |= (1 << Accessibility_BUTTON_RELEASED_EVENT);
328
329   dbus_error_init (&error);
330     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "deregisterDeviceEventListener", &error, "ou", path, event_types);
331   g_free (path);
332   return TRUE;
333 }
334
335 /**
336  * SPI_generateKeyboardEvent:
337  * @keyval: a long integer indicating the keycode or keysym of the key event
338  *           being synthesized.
339  * @keystring: an (optional) UTF-8 string which, if @keyval is NULL,
340  *           indicates a 'composed' keyboard input string which is 
341  *           being synthesized; this type of keyboard event synthesis does
342  *           not emulate hardware keypresses but injects the string 
343  *           as though a composing input method (such as XIM) were used.
344  * @synth_type: a #AccessibleKeySynthType flag indicating whether @keyval
345  *           is to be interpreted as a keysym rather than a keycode
346  *           (CSPI_KEYSYM), or whether to synthesize
347  *           SPI_KEY_PRESS, SPI_KEY_RELEASE, or both (SPI_KEY_PRESSRELEASE).
348  *
349  * Synthesize a keyboard event (as if a hardware keyboard event occurred in the
350  * current UI context).
351  *
352  * Returns: #TRUE if successful, otherwise #FALSE.
353  **/
354 SPIBoolean
355 SPI_generateKeyboardEvent (long int keyval,
356                            char *keystring,
357                            AccessibleKeySynthType synth_type)
358 {
359   dbus_uint32_t keysynth_type;
360   dbus_int32_t keycode = keyval;
361   DBusError error;
362
363   switch (synth_type)
364     {
365       case SPI_KEY_PRESS:
366           keysynth_type = Accessibility_KEY_PRESS;
367           break;
368       case SPI_KEY_RELEASE:
369           keysynth_type = Accessibility_KEY_RELEASE;
370           break;
371       case SPI_KEY_PRESSRELEASE:
372           keysynth_type = Accessibility_KEY_PRESSRELEASE;
373           break;
374       case SPI_KEY_SYM:
375           keysynth_type = Accessibility_KEY_SYM;
376           break;
377       case SPI_KEY_STRING:
378           keysynth_type = Accessibility_KEY_STRING;
379           break;
380       default:
381           return FALSE;
382     }
383
384   if (!keystring) keystring = "";
385   dbus_error_init (&error);
386     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "generateKeyboardEvent", &error, "isu", keycode, keystring, keysynth_type);
387
388   return TRUE;
389 }
390
391 /**
392  * SPI_generateMouseEvent:
393  * @x: a #long indicating the screen x coordinate of the mouse event.
394  * @y: a #long indicating the screen y coordinate of the mouse event.
395  * @name: a string indicating which mouse event to be synthesized
396  *        (e.g. "b1p", "b1c", "b2r", "rel", "abs").
397  *
398  * Synthesize a mouse event at a specific screen coordinate.
399  * Most AT clients should use the #AccessibleAction interface when
400  * tempted to generate mouse events, rather than this method.
401  * Event names: b1p = button 1 press; b2r = button 2 release;
402  *              b3c = button 3 click; b2d = button 2 double-click;
403  *              abs = absolute motion; rel = relative motion.
404  *
405  * Returns: #TRUE if successful, otherwise #FALSE.
406  **/
407 SPIBoolean
408 SPI_generateMouseEvent (long x, long y, char *name)
409 {
410   dbus_int32_t dbus_x = x, dbus__y = y;
411   DBusError error;
412
413   dbus_error_init (&error);
414     dbind_method_call_reentrant (SPI_bus(), spi_bus_registry, spi_path_dec, spi_interface_dec, "generateMouseEvent", &error, "iis", x, y, name);
415   return TRUE;
416 }
417
418 char *
419 cspi_device_listener_get_path (CSpiDeviceListener *listener)
420 {
421   return g_strdup_printf ("/org/freedesktop/atspi/listeners/%d", listener->id);
422 }