2001-11-13 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_registry.c
1
2 /*
3  *
4  * Global functions serviced by the registry
5  *
6  */
7
8 /* static stuff used only by registry C bindings */
9 static GList *key_listeners = NULL;
10 static Display *display = NULL;
11
12 /**
13  * registerGlobalEventListener:
14  * @listener: the #AccessibleEventListener to be registered against an event type.
15  * @callback: a character string indicating the type of events for which
16  *            notification is requested.  Format is
17  *            EventClass:major_type:minor_type:detail
18  *            where all subfields other than EventClass are optional.
19  *            EventClasses include "Focus", "Window", "Mouse",
20  *            and toolkit events (e.g. "Gtk", "AWT").
21  *            Examples: "focus:", "Gtk:GtkWidget:button_press_event".
22  *
23  * NOTE: this string may be UTF-8, but should not contain byte value 56 (ascii ':'),
24  *            except as a delimiter, since non-UTF-8 string delimiting
25  *            functions are used internally.  In general, listening to
26  *            toolkit-specific events is not recommended.
27  *
28  * Add an in-process callback function to an existing SpiAccessibleEventListener.
29  *
30  * Returns: #TRUE if successful, otherwise #FALSE.
31  *
32  **/
33 boolean
34 registerGlobalEventListener (SpiAccessibleEventListener *listener,
35                              char *eventType)
36 {
37   Accessibility_Registry_registerGlobalEventListener (
38                          registry,
39                          (Accessibility_EventListener)
40                             bonobo_object_corba_objref (bonobo_object (listener)),
41                          eventType,
42                          &ev);
43
44   if (ev._major != CORBA_NO_EXCEPTION)
45     {
46     return FALSE;
47     }
48   else
49     {
50       return TRUE;
51     }
52 }
53
54 /**
55  * deregisterGlobalEventListener:
56  * @listener: the #AccessibleEventListener to be registered against an event type.
57  *
58  * deregisters an SpiAccessibleEventListener from the registry, for all event types it may be listening to.
59  *
60  * Returns: #TRUE if successful, otherwise #FALSE.
61  *
62  **/
63 boolean
64 deregisterGlobalEventListenerAll (SpiAccessibleEventListener *listener)
65 {
66   Accessibility_Registry_deregisterGlobalEventListenerAll (
67                          registry,
68                          (Accessibility_EventListener)
69                             CORBA_Object_duplicate (bonobo_object_corba_objref (bonobo_object (listener)), &ev),
70                          &ev);
71
72   if (ev._major != CORBA_NO_EXCEPTION)
73     {
74     return FALSE;
75     }
76   else
77     {
78       return TRUE;
79     }
80 }
81
82 /**
83  * getDesktopCount:
84  *
85  * Get the number of virtual desktops.
86  * NOTE: currently multiple virtual desktops are not implemented, this
87  *       function always returns '1'.
88  *
89  * Returns: an integer indicating the number of active virtual desktops.
90  *
91  **/
92 int
93 getDesktopCount ()
94 {
95   return Accessibility_Registry_getDesktopCount (registry, &ev);
96 }
97
98 /**
99  * getDesktop:
100  * @i: an integer indicating which of the accessible desktops is to be returned.
101  *
102  * Get the virtual desktop indicated by index @i.
103  * NOTE: currently multiple virtual desktops are not implemented, this
104  *       function always returns '1'.
105  *
106  * Returns: a pointer to the 'i-th' virtual desktop's #Accessible representation.
107  *
108  **/
109 SpiAccessible*
110 getDesktop (int n)
111 {
112   return Obj_Add (Accessibility_Registry_getDesktop (registry, (CORBA_short) n, &ev));
113 }
114
115 /**
116  * getDesktopList:
117  * @list: a pointer to an array of #Accessible objects.
118  *
119  * Get the list of virtual desktops.  On return, @list will point
120  *     to a newly-created array of virtual desktop pointers.
121  *     It is the responsibility of the caller to free this array when
122  *     it is no longer needed.
123  *
124  * Not Yet Implemented.
125  *
126  * Returns: an integer indicating how many virtual desktops have been
127  *          placed in the list pointed to by parameter @list.
128  **/
129 int
130 getDesktopList (SpiAccessible **list)
131 {
132   *list = NULL;
133   return 0;
134 }
135
136 static gboolean
137 key_event_source_func (void *p)
138 {
139   GList *listeners = (GList *)p;
140   XEvent *x_event = g_new0 (XEvent, 1);
141   while (XPending (display))
142     {
143       XNextEvent (display, x_event);
144       while (listeners)
145         {
146         /* if the listener mask matches, notify it*/
147           if (1)
148             {
149               ;   
150             }
151         }
152     }
153   return TRUE;
154 }
155
156 void
157 save_this_impl_registerKeystrokeListener (KeystrokeListener *listener, KeyMaskType keymask)
158 {
159   static gboolean initialized = FALSE;
160   static Window grab_window;
161   XEvent *x_event = g_new0(XEvent, 1);
162   key_listeners = g_list_append (key_listeners, listener);
163   if (!initialized)
164     {
165       g_timeout_add_full (G_PRIORITY_HIGH_IDLE, 200, key_event_source_func, key_listeners, NULL);
166       display = XOpenDisplay (getenv ("DISPLAY"));
167       grab_window = DefaultRootWindow (display);
168       XSelectInput (display, grab_window, KeyPress | KeyRelease);
169       initialized = TRUE;
170     }
171   /* */
172   XGrabKey (display,
173             AnyKey,
174             LockMask,
175             grab_window,
176             False,
177             GrabModeAsync,
178             GrabModeAsync);
179   while (0)
180   {
181           XNextEvent (display, x_event);
182           g_print ("foo!\n");
183   }
184 }
185  
186 /**
187  * registerKeystrokeListener:
188  * @listener: a pointer to the #KeystrokeListener for which
189  *            keystroke events are requested.
190  *
191  **/
192 void
193 registerKeystrokeListener (KeystrokeListener *listener,
194                            KeySet *keys,
195                            KeyMaskType modmask,
196                            KeyEventMask eventmask,
197                            KeyListenerSyncType sync_type)
198 {
199   Accessibility_ControllerEventMask *controller_event_mask =
200           Accessibility_ControllerEventMask__alloc();
201   Accessibility_DeviceEventController device_event_controller = 
202           Accessibility_Registry_getDeviceEventController (registry, &ev);
203   Accessibility_KeySet *all_keys = Accessibility_KeySet__alloc();
204   Accessibility_KeyEventTypeSeq *key_events = Accessibility_KeyEventTypeSeq__alloc();
205   Accessibility_KeystrokeListener spi_listener_corba_ref;
206   Accessibility_DeviceEventController_ref (device_event_controller, &ev);
207   controller_event_mask->value = (CORBA_unsigned_long) modmask;
208   controller_event_mask->refcount = (CORBA_unsigned_short) 1;
209
210   spi_listener_corba_ref = (Accessibility_KeystrokeListener)
211           CORBA_Object_duplicate (bonobo_object_corba_objref (bonobo_object (listener)), &ev);
212   
213           Accessibility_DeviceEventController_registerKeystrokeListener (
214           device_event_controller,
215           spi_listener_corba_ref,
216           all_keys,
217           controller_event_mask,
218           key_events,
219           (CORBA_boolean) ((sync_type | KEYSPI_LISTENER_CANCONSUME)!=0),
220           &ev);
221 }
222
223 /**
224  * deregisterKeystrokeListener:
225  * @listener: a pointer to the #KeystrokeListener for which
226  *            keystroke events are requested.
227  *
228  **/
229 void
230 deregisterKeystrokeListener (KeystrokeListener *listener, KeyMaskType keymask)
231 {
232   Accessibility_ControllerEventMask *controller_event_mask =
233           Accessibility_ControllerEventMask__alloc();
234   Accessibility_DeviceEventController device_event_controller = 
235           Accessibility_Registry_getDeviceEventController (registry, &ev);
236   Accessibility_KeySet *all_keys = Accessibility_KeySet__alloc();
237   Accessibility_KeyEventTypeSeq *key_events = Accessibility_KeyEventTypeSeq__alloc();
238   Accessibility_KeystrokeListener spi_listener_corba_ref;
239   Accessibility_DeviceEventController_unref (device_event_controller, &ev);
240   controller_event_mask->value = (CORBA_unsigned_long) keymask;
241   controller_event_mask->refcount = (CORBA_unsigned_short) 1;
242
243   spi_listener_corba_ref = (Accessibility_KeystrokeListener)
244           CORBA_Object_duplicate (bonobo_object_corba_objref (bonobo_object (listener)), &ev);
245   
246   Accessibility_DeviceEventController_deregisterKeystrokeListener (
247           device_event_controller,
248           spi_listener_corba_ref,
249           all_keys,
250           controller_event_mask,
251           key_events,
252           (CORBA_boolean) TRUE,
253           &ev);
254 }
255
256 /**
257  * generateKeyEvent:
258  * @keycode: a #long indicating the keycode of the key event
259  *           being synthesized.
260  * @meta: a #long indicating the key modifiers to be sent
261  *        with the event, if any.
262  *
263  * Synthesize a keyboard event (as if a hardware keyboard event occurred in the
264  * current UI context).
265  * Not Yet Implemented.
266  *
267  **/
268 void
269 generateKeyEvent (long keyval, KeySynthType type)
270 {
271 /* TODO: check current modifier status and
272  *  send keycode to alter, if necessary
273  */
274   Accessibility_DeviceEventController device_event_controller = 
275           Accessibility_Registry_getDeviceEventController (registry, &ev);
276   Accessibility_DeviceEventController_generateKeyEvent (device_event_controller,
277                                                         keyval,
278                                                         (unsigned long) type,
279                                                         &ev);
280 }
281
282 /**
283  * generateMouseEvent:
284  * @x: a #long indicating the screen x coordinate of the mouse event.
285  * @y: a #long indicating the screen y coordinate of the mouse event.
286  * @name: a string indicating which mouse event to be synthesized
287  *        (e.g. "button1", "button2", "mousemove").
288  *
289  * Synthesize a mouse event at a specific screen coordinate.
290  * Not Yet Implemented.
291  *
292  **/
293 void
294 generateMouseEvent (long x, long y, char *name)
295 {
296   ;
297 }
298