Fix a few more warnings
[platform/upstream/at-spi2-core.git] / registryd / deviceeventcontroller-x11.c
1 /* AT-SPI - Assistive Technology Service Provider Interface
2  *
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2003 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 /* deviceeventcontroller-x11.c: X-specific DeviceEventController support */
25
26 #include <config.h>
27
28 #undef  SPI_XKB_DEBUG
29 #undef  SPI_DEBUG
30 #undef  SPI_KEYEVENT_DEBUG
31
32 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35 #include <sys/time.h>
36
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/extensions/XTest.h>
40 #include <X11/XKBlib.h>
41
42 #define XK_MISCELLANY
43 #define XK_LATIN1
44 #include <X11/keysymdef.h>
45
46 #ifdef HAVE_XEVIE
47 #include <X11/Xproto.h>
48 #include <X11/X.h>
49 #include <X11/extensions/Xevie.h>
50 #endif /* HAVE_XEVIE */
51
52 #include <glib.h>
53
54 #include <dbus/dbus.h>
55
56 #include "paths.h"
57 #include "keymasks.h"
58 #include "de-types.h"
59 #include "de-marshaller.h"
60 #include "display.h"
61 #include "event-source.h"
62
63 #include "deviceeventcontroller.h"
64 #include "reentrant-list.h"
65
66 #include "introspection.h"
67
68 KeySym ucs2keysym (long ucs);
69 long keysym2ucs(KeySym keysym); 
70
71 static void spi_dec_x11_emit_modifier_event (SpiDEController *controller,
72                              guint prev_mask,
73                              guint current_mask);
74
75 #define CHECK_RELEASE_DELAY 20
76 #define BIT(c, x)       (c[x/8]&(1<<(x%8)))
77 static guint check_release_handler = 0;
78 static Accessibility_DeviceEvent pressed_event;
79 static void wait_for_release_event (XEvent *event, SpiDEController *controller);
80
81 static int spi_error_code = 0;
82 struct _SpiPoint {
83     gint x;
84     gint y;
85 };
86 typedef struct _SpiPoint SpiPoint;
87 static SpiPoint last_mouse_pos_static = {0, 0}; 
88 static SpiPoint *last_mouse_pos = &last_mouse_pos_static;
89 static unsigned int mouse_mask_state = 0;
90 static unsigned int mouse_button_mask =
91   Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask;
92 static unsigned int key_modifier_mask =
93   Mod1Mask | Mod2Mask | Mod3Mask | Mod4Mask | Mod5Mask | ShiftMask | LockMask | ControlMask | SPI_KEYMASK_NUMLOCK;
94 static unsigned int _numlock_physical_mask = Mod2Mask; /* a guess, will be reset */
95
96 static XModifierKeymap* xmkeymap = NULL;
97
98
99 static int (*x_default_error_handler) (Display *display, XErrorEvent *error_event);
100
101 typedef struct {
102   Display *xevie_display;
103   unsigned int last_press_keycode;
104   unsigned int last_release_keycode;
105   struct timeval last_press_time;
106   struct timeval last_release_time;
107   int have_xkb;
108   int xkb_major_extension_opcode;
109   int xkb_base_event_code;
110   int xkb_base_error_code;
111   unsigned int xkb_latch_mask;
112   unsigned int pending_xkb_mod_relatch_mask;
113   XkbDescPtr xkb_desc;
114   KeyCode reserved_keycode;
115   KeySym reserved_keysym;
116   guint  reserved_reset_timeout;
117 } DEControllerPrivateData;
118
119 static void     spi_controller_register_with_devices          (SpiDEController           *controller);
120 static gboolean spi_device_event_controller_forward_key_event (SpiDEController           *controller,
121                                                                const XEvent              *event);
122
123
124 static SpiDEController *saved_controller;
125
126 static unsigned int
127 keysym_mod_mask (KeySym keysym, KeyCode keycode)
128 {
129         /* we really should use XKB and look directly at the keymap */
130         /* this is very inelegant */
131         Display *display = spi_get_display ();
132         unsigned int mods_rtn = 0;
133         unsigned int retval = 0;
134         KeySym sym_rtn;
135
136         if (XkbLookupKeySym (display, keycode, 0, &mods_rtn, &sym_rtn) &&
137             (sym_rtn == keysym)) {
138                 retval = 0;
139         }
140         else if (XkbLookupKeySym (display, keycode, ShiftMask, &mods_rtn, &sym_rtn) &&
141                  (sym_rtn == keysym)) {
142                 retval = ShiftMask;
143         }
144         else if (XkbLookupKeySym (display, keycode, Mod2Mask, &mods_rtn, &sym_rtn) &&
145                  (sym_rtn == keysym)) {
146                 retval = Mod2Mask;
147         }
148         else if (XkbLookupKeySym (display, keycode, Mod3Mask, &mods_rtn, &sym_rtn) &&
149                  (sym_rtn == keysym)) {
150                 retval = Mod3Mask;
151         }
152         else if (XkbLookupKeySym (display, keycode, 
153                                   ShiftMask | Mod2Mask, &mods_rtn, &sym_rtn) &&
154                  (sym_rtn == keysym)) {
155                 retval = (Mod2Mask | ShiftMask);
156         }
157         else if (XkbLookupKeySym (display, keycode, 
158                                   ShiftMask | Mod3Mask, &mods_rtn, &sym_rtn) &&
159                  (sym_rtn == keysym)) {
160                 retval = (Mod3Mask | ShiftMask);
161         }
162         else if (XkbLookupKeySym (display, keycode, 
163                                   ShiftMask | Mod4Mask, &mods_rtn, &sym_rtn) &&
164                  (sym_rtn == keysym)) {
165                 retval = (Mod4Mask | ShiftMask);
166         }
167         else
168                 retval = 0xFFFF;
169         return retval;
170 }
171
172 static gboolean
173 replace_map_keysym (DEControllerPrivateData *priv, KeyCode keycode, KeySym keysym)
174 {
175 #ifdef HAVE_XKB
176   Display *dpy = spi_get_display ();
177   XkbDescPtr desc;
178   if (!(desc = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd)))
179     {
180       fprintf (stderr, "ERROR getting map\n");
181     }
182   XFlush (dpy);
183   XSync (dpy, False);
184   if (desc && desc->map)
185     {
186       gint offset = desc->map->key_sym_map[keycode].offset;
187       desc->map->syms[offset] = keysym; 
188     }
189   else
190     {
191       fprintf (stderr, "Error changing key map: empty server structure\n");
192     }           
193   XkbSetMap (dpy, XkbAllMapComponentsMask, desc);
194   /**
195    *  FIXME: the use of XkbChangeMap, and the reuse of the priv->xkb_desc structure, 
196    * would be far preferable.
197    * HOWEVER it does not seem to work using XFree 4.3. 
198    **/
199   /*        XkbChangeMap (dpy, priv->xkb_desc, priv->changes); */
200   XFlush (dpy);
201   XSync (dpy, False);
202   XkbFreeKeyboard (desc, 0, TRUE);
203
204   return TRUE;
205 #else
206   return FALSE;
207 #endif
208 }
209
210 static gboolean
211 spi_dec_reset_reserved (gpointer data)
212 {
213   DEControllerPrivateData *priv = data;
214   replace_map_keysym (priv, priv->reserved_keycode, priv->reserved_keysym);
215   priv->reserved_reset_timeout = 0;
216   return FALSE;
217 }
218
219 static gint
220 spi_dec_x11_get_keycode (SpiDEController *controller,
221                           gint keysym,
222                           gchar *key_str,
223                           gboolean fix,
224                           guint *modmask)
225 {
226         KeyCode keycode = 0;
227   if (key_str && key_str[0])
228                 keysym = XStringToKeysym(key_str);
229         keycode = XKeysymToKeycode (spi_get_display (), (KeySym) keysym);
230         if (!keycode && fix)
231         {
232                 DEControllerPrivateData *priv = controller->priv;
233                 /* if there's no keycode available, fix it */
234                 if (replace_map_keysym (priv, priv->reserved_keycode, keysym))
235                 {
236                         keycode = priv->reserved_keycode;
237                         /* 
238                          * queue a timer to restore the old keycode.  Ugly, but required 
239                          * due to races / asynchronous X delivery.   
240                          * Long-term fix is to extend the X keymap here instead of replace entries.
241                          */
242                         priv->reserved_reset_timeout = g_timeout_add (500, spi_dec_reset_reserved, priv);
243                 }               
244                 if (modmask)
245                         *modmask = 0;
246                 return keycode;
247         }
248         if (modmask) 
249                 *modmask = keysym_mod_mask (keysym, keycode);
250         return keycode;
251 }
252
253 static void
254 spi_dec_set_unlatch_pending (SpiDEController *controller, unsigned mask)
255 {
256   DEControllerPrivateData *priv = controller->priv;
257 #ifdef SPI_XKB_DEBUG
258   if (priv->xkb_latch_mask) fprintf (stderr, "unlatch pending! %x\n", 
259                                      priv->xkb_latch_mask);
260 #endif
261   priv->pending_xkb_mod_relatch_mask |= priv->xkb_latch_mask; 
262 }
263  
264 static gboolean
265 spi_dec_button_update_and_emit (SpiDEController *controller,
266                                 guint mask_return)
267 {
268   Accessibility_DeviceEvent mouse_e;
269   gchar event_detail[3];
270   gboolean is_consumed = FALSE;
271
272   if ((mask_return & mouse_button_mask) !=
273       (mouse_mask_state & mouse_button_mask)) 
274     {
275       int button_number = 0;
276       gboolean is_down = False;
277       
278       if (!(mask_return & Button1Mask) &&
279           (mouse_mask_state & Button1Mask)) 
280         {
281           mouse_mask_state &= ~Button1Mask;
282           button_number = 1;
283         } 
284       else if ((mask_return & Button1Mask) &&
285                !(mouse_mask_state & Button1Mask)) 
286         {
287           mouse_mask_state |= Button1Mask;
288           button_number = 1;
289           is_down = True;
290         } 
291       else if (!(mask_return & Button2Mask) &&
292                (mouse_mask_state & Button2Mask)) 
293         {
294           mouse_mask_state &= ~Button2Mask;
295           button_number = 2;
296         } 
297       else if ((mask_return & Button2Mask) &&
298                !(mouse_mask_state & Button2Mask)) 
299         {
300           mouse_mask_state |= Button2Mask;
301           button_number = 2;
302           is_down = True;
303         } 
304       else if (!(mask_return & Button3Mask) &&
305                (mouse_mask_state & Button3Mask)) 
306         {
307           mouse_mask_state &= ~Button3Mask;
308           button_number = 3;
309         } 
310       else if ((mask_return & Button3Mask) &&
311                !(mouse_mask_state & Button3Mask)) 
312         {
313           mouse_mask_state |= Button3Mask;
314           button_number = 3;
315           is_down = True;
316         } 
317       else if (!(mask_return & Button4Mask) &&
318                (mouse_mask_state & Button4Mask)) 
319         {
320           mouse_mask_state &= ~Button4Mask;
321           button_number = 4;
322         } 
323       else if ((mask_return & Button4Mask) &&
324                !(mouse_mask_state & Button4Mask)) 
325         {
326           mouse_mask_state |= Button4Mask;
327           button_number = 4;
328           is_down = True;
329         } 
330       else if (!(mask_return & Button5Mask) &&
331                (mouse_mask_state & Button5Mask)) 
332         {
333           mouse_mask_state &= ~Button5Mask;
334           button_number = 5;
335         }
336       else if ((mask_return & Button5Mask) &&
337                !(mouse_mask_state & Button5Mask)) 
338         {
339           mouse_mask_state |= Button5Mask;
340           button_number = 5;
341           is_down = True;
342         }
343       if (button_number) {
344 #ifdef SPI_DEBUG                  
345         fprintf (stderr, "Button %d %s\n",
346                  button_number, (is_down) ? "Pressed" : "Released");
347 #endif
348         snprintf (event_detail, 3, "%d%c", button_number,
349                   (is_down) ? 'p' : 'r');
350         /* TODO: FIXME distinguish between physical and 
351          * logical buttons 
352          */
353         mouse_e.type      = (is_down) ? 
354           Accessibility_BUTTON_PRESSED_EVENT :
355           Accessibility_BUTTON_RELEASED_EVENT;
356         mouse_e.id        = button_number;
357         mouse_e.hw_code   = button_number;
358         mouse_e.modifiers = (dbus_uint16_t) mouse_mask_state; 
359         mouse_e.timestamp = 0;
360         mouse_e.event_string = "";
361         mouse_e.is_text   = FALSE;
362         is_consumed = 
363           spi_controller_notify_mouselisteners (controller, 
364                                                 &mouse_e);
365         if (!is_consumed)
366           {
367             dbus_uint32_t x = last_mouse_pos->x, y = last_mouse_pos->y;
368             spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, x, y);
369           }
370         else
371           spi_dec_set_unlatch_pending (controller, mask_return);
372       }
373       return TRUE;
374     }
375   else
376     {
377       return FALSE;
378     }
379 }
380
381 static guint
382 spi_dec_x11_mouse_check (SpiDEController *controller, 
383                      int *x, int *y, gboolean *moved)
384 {
385   int win_x_return,win_y_return;
386   unsigned int mask_return;
387   Window root_return, child_return;
388   Display *display = spi_get_display ();
389
390   if (display != NULL)
391     XQueryPointer(display, DefaultRootWindow (display),
392                   &root_return, &child_return,
393                   x, y,
394                   &win_x_return, &win_y_return, &mask_return);
395   /* 
396    * Since many clients grab the pointer, and X goes an automatic
397    * pointer grab on mouse-down, we often must detect mouse button events
398    * by polling rather than via a button grab. 
399    * The while loop (rather than if) is used since it's possible that 
400    * multiple buttons have changed state since we last checked.
401    */
402   if (mask_return != mouse_mask_state) 
403     {
404       while (spi_dec_button_update_and_emit (controller, mask_return));
405     }
406
407   if (*x != last_mouse_pos->x || *y != last_mouse_pos->y) 
408     {
409       // TODO: combine these two signals?
410       dbus_uint32_t ix = *x, iy = *y;
411       spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Abs", "", ix, iy);
412       ix -= last_mouse_pos->x;
413       iy -= last_mouse_pos->y;
414       spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Rel", "", ix, iy);
415       last_mouse_pos->x = *x;
416       last_mouse_pos->y = *y;
417       *moved = True;
418     }
419   else
420     {
421       *moved = False;
422     }
423
424   return mask_return;
425 }
426
427 #ifdef WE_NEED_UGRAB_MOUSE
428 static int
429 spi_dec_ungrab_mouse (gpointer data)
430 {
431         Display *display = spi_get_display ();
432         if (display)
433           {
434             XUngrabButton (spi_get_display (), AnyButton, AnyModifier,
435                            XDefaultRootWindow (spi_get_display ()));
436           }
437         return FALSE;
438 }
439 #endif
440
441 static void
442 spi_dec_init_mouse_listener (SpiDEController *dec)
443 {
444 #ifdef GRAB_BUTTON
445   Display *display = spi_get_display ();
446
447   if (display)
448     {
449       if (XGrabButton (display, AnyButton, AnyModifier,
450                        spi_get_root_window (),
451                        True, ButtonPressMask | ButtonReleaseMask,
452                        GrabModeSync, GrabModeAsync, None, None) != Success) {
453 #ifdef SPI_DEBUG
454         fprintf (stderr, "WARNING: could not grab mouse buttons!\n");
455 #endif
456         ;
457       }
458       XSync (display, False);
459 #ifdef SPI_DEBUG
460       fprintf (stderr, "mouse buttons grabbed\n");
461 #endif
462     }
463 #endif
464 }
465
466 static void
467 spi_dec_clear_unlatch_pending (SpiDEController *controller)
468 {
469   DEControllerPrivateData *priv = controller->priv;
470   priv->xkb_latch_mask = 0;
471 }
472
473 static void
474 spi_device_event_controller_forward_mouse_event (SpiDEController *controller,
475                                                  XEvent *xevent)
476 {
477   Accessibility_DeviceEvent mouse_e;
478   gchar event_detail[3];
479   gboolean is_consumed = FALSE;
480   gboolean xkb_mod_unlatch_occurred;
481   XButtonEvent *xbutton_event = (XButtonEvent *) xevent;
482   dbus_uint32_t ix, iy;
483
484   int button = xbutton_event->button;
485   
486   unsigned int mouse_button_state = xbutton_event->state;
487
488   switch (button)
489     {
490     case 1:
491             mouse_button_state |= Button1Mask;
492             break;
493     case 2:
494             mouse_button_state |= Button2Mask;
495             break;
496     case 3:
497             mouse_button_state |= Button3Mask;
498             break;
499     case 4:
500             mouse_button_state |= Button4Mask;
501             break;
502     case 5:
503             mouse_button_state |= Button5Mask;
504             break;
505     }
506
507   last_mouse_pos->x = ((XButtonEvent *) xevent)->x_root;
508   last_mouse_pos->y = ((XButtonEvent *) xevent)->y_root;
509
510 #ifdef SPI_DEBUG  
511   fprintf (stderr, "mouse button %d %s (%x)\n",
512            xbutton_event->button, 
513            (xevent->type == ButtonPress) ? "Press" : "Release",
514            mouse_button_state);
515 #endif
516   snprintf (event_detail, 3, "%d%c", button,
517             (xevent->type == ButtonPress) ? 'p' : 'r');
518
519   /* TODO: FIXME distinguish between physical and logical buttons */
520   mouse_e.type      = (xevent->type == ButtonPress) ? 
521                       Accessibility_BUTTON_PRESSED_EVENT :
522                       Accessibility_BUTTON_RELEASED_EVENT;
523   mouse_e.id        = button;
524   mouse_e.hw_code   = button;
525   mouse_e.modifiers = (dbus_uint16_t) xbutton_event->state;
526   mouse_e.timestamp = (dbus_uint32_t) xbutton_event->time;
527   mouse_e.event_string = "";
528   mouse_e.is_text   = FALSE;
529   if ((mouse_button_state & mouse_button_mask) != 
530        (mouse_mask_state & mouse_button_mask))
531     { 
532       if ((mouse_mask_state & key_modifier_mask) !=
533           (mouse_button_state & key_modifier_mask))
534         spi_dec_x11_emit_modifier_event (controller, 
535                                      mouse_mask_state, mouse_button_state);
536       mouse_mask_state = mouse_button_state;
537       is_consumed = 
538         spi_controller_notify_mouselisteners (controller, &mouse_e);
539       ix = last_mouse_pos->x;
540       iy = last_mouse_pos->y;
541       spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_MOUSE, "Button", event_detail, ix, iy);
542     }
543
544   xkb_mod_unlatch_occurred = (xevent->type == ButtonPress ||
545                               xevent->type == ButtonRelease);
546   
547   /* if client wants to consume this event, and XKB latch state was
548    *   unset by this button event, we reset it
549    */
550   if (is_consumed && xkb_mod_unlatch_occurred)
551     spi_dec_set_unlatch_pending (controller, mouse_mask_state);
552   
553   XAllowEvents (spi_get_display (),
554                 (is_consumed) ? SyncPointer : ReplayPointer,
555                 CurrentTime);
556 }
557
558 static void
559 global_filter_fn (XEvent *xevent, void *data)
560 {
561   SpiDEController *controller;
562   DEControllerPrivateData *priv;
563   Display *display = spi_get_display ();
564   controller = SPI_DEVICE_EVENT_CONTROLLER (data);
565   priv = controller->priv;
566
567   if (xevent->type == MappingNotify)
568     xmkeymap = NULL;
569
570   if (xevent->type == KeyPress || xevent->type == KeyRelease)
571     {
572       if (priv->xevie_display == NULL)
573         {
574           gboolean is_consumed;
575
576           is_consumed =
577             spi_device_event_controller_forward_key_event (controller, xevent);
578
579           if (is_consumed)
580             {
581               int n_events;
582               int i;
583               XEvent next_event;
584               n_events = XPending (display);
585
586 #ifdef SPI_KEYEVENT_DEBUG
587               g_print ("Number of events pending: %d\n", n_events);
588 #endif
589               for (i = 0; i < n_events; i++)
590                 {
591                   XNextEvent (display, &next_event);
592                   if (next_event.type != KeyPress &&
593                       next_event.type != KeyRelease)
594                         g_warning ("Unexpected event type %d in queue", next_event.type);
595                  }
596
597               XAllowEvents (display, AsyncKeyboard, CurrentTime);
598               if (n_events)
599                 XUngrabKeyboard (display, CurrentTime);
600             }
601           else
602             {
603               if (xevent->type == KeyPress)
604                 wait_for_release_event (xevent, controller);
605               XAllowEvents (display, ReplayKeyboard, CurrentTime);
606             }
607         }
608
609       return;
610     }
611   if (xevent->type == ButtonPress || xevent->type == ButtonRelease)
612     {
613       spi_device_event_controller_forward_mouse_event (controller, xevent);
614     }
615   if (xevent->type == priv->xkb_base_event_code)
616     {
617       XkbAnyEvent * xkb_ev = (XkbAnyEvent *) xevent;
618       /* ugly but probably necessary...*/
619       XSynchronize (display, TRUE);
620
621       if (xkb_ev->xkb_type == XkbStateNotify)
622         {
623           XkbStateNotifyEvent *xkb_snev =
624                   (XkbStateNotifyEvent *) xkb_ev;
625           /* check the mouse, to catch mouse events grabbed by
626            * another client; in case we should revert this XKB delatch 
627            */
628           if (!priv->pending_xkb_mod_relatch_mask)
629             {
630               int x, y;
631               gboolean moved;
632               spi_dec_x11_mouse_check (controller, &x, &y, &moved);
633             }
634           /* we check again, since the previous call may have 
635              changed this flag */
636           if (priv->pending_xkb_mod_relatch_mask)
637             {
638               unsigned int feedback_mask;
639 #ifdef SPI_XKB_DEBUG
640               fprintf (stderr, "relatching %x\n",
641                        priv->pending_xkb_mod_relatch_mask);
642 #endif
643               /* temporarily turn off the latch bell, if it's on */
644               XkbGetControls (display,
645                               XkbAccessXFeedbackMask,
646                               priv->xkb_desc);
647               feedback_mask = priv->xkb_desc->ctrls->ax_options;
648               if (feedback_mask & XkbAX_StickyKeysFBMask)
649               {
650                 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
651                                                  0, False};      
652                 priv->xkb_desc->ctrls->ax_options
653                               &= ~(XkbAX_StickyKeysFBMask);
654                 XkbChangeControls (display, priv->xkb_desc, &changes);
655               }
656               /* TODO: account for lock as well as latch */
657               XkbLatchModifiers (display,
658                                  XkbUseCoreKbd,
659                                  priv->pending_xkb_mod_relatch_mask,
660                                  priv->pending_xkb_mod_relatch_mask);
661               if (feedback_mask & XkbAX_StickyKeysFBMask)
662               { 
663                 XkbControlsChangesRec changes = {XkbAccessXFeedbackMask,
664                                                  0, False};      
665                 priv->xkb_desc->ctrls->ax_options = feedback_mask;
666                 XkbChangeControls (display, priv->xkb_desc, &changes);
667               }
668 #ifdef SPI_XKB_DEBUG
669               fprintf (stderr, "relatched %x\n",
670                        priv->pending_xkb_mod_relatch_mask);
671 #endif
672               priv->pending_xkb_mod_relatch_mask = 0;
673             }
674           else
675             {
676               priv->xkb_latch_mask = xkb_snev->latched_mods;
677             }
678         }
679       XSynchronize (display, FALSE);
680     }
681   
682   return;
683 }
684
685 static int
686 _spi_controller_device_error_handler (Display *display, XErrorEvent *error)
687 {
688   if (error->error_code == BadAccess) 
689     {  
690       g_message ("Could not complete key grab: grab already in use.\n");
691       spi_error_code = BadAccess;
692       return 0;
693     }
694   else 
695     {
696       return (*x_default_error_handler) (display, error);
697     }
698 }
699
700 static void
701 spi_controller_register_with_devices (SpiDEController *controller)
702 {
703   DEControllerPrivateData *priv;
704   int event_base, error_base, major_version, minor_version;
705
706   priv = controller->priv;
707   if (XTestQueryExtension (spi_get_display(), &event_base, &error_base, &major_version, &minor_version))
708     {
709       XTestGrabControl (spi_get_display (), True);
710     }
711
712   /* calls to device-specific implementations and routines go here */
713   /* register with: keyboard hardware code handler */
714   /* register with: (translated) keystroke handler */
715
716   priv->have_xkb = XkbQueryExtension (spi_get_display (),
717                                       &priv->xkb_major_extension_opcode,
718                                       &priv->xkb_base_event_code,
719                                       &priv->xkb_base_error_code, NULL, NULL);
720   if (priv->have_xkb)
721     {
722       gint i;
723       guint64 reserved = 0;
724       priv->xkb_desc = XkbGetMap (spi_get_display (), XkbKeySymsMask, XkbUseCoreKbd);
725       XkbSelectEvents (spi_get_display (),
726                        XkbUseCoreKbd,
727                        XkbStateNotifyMask, XkbStateNotifyMask);     
728       _numlock_physical_mask = XkbKeysymToModifiers (spi_get_display (), 
729                                                      XK_Num_Lock);
730       for (i = priv->xkb_desc->max_key_code; i >= priv->xkb_desc->min_key_code; --i)
731       {
732           if (priv->xkb_desc->map->key_sym_map[i].kt_index[0] == XkbOneLevelIndex)
733           { 
734               if (XkbKeycodeToKeysym (spi_get_display (), i, 0, 0) != 0)
735               {
736                   /* don't use this one if there's a grab client! */
737
738                   /* Runtime errors are generated from these functions,
739                    * that are then quashed. Equivalent to:
740                    * try
741                    *   {Blah}
742                    * except
743                    *   {;}
744                    */
745
746                   spi_x_error_trap ();
747                   XGrabKey (spi_get_display (), i, 0, 
748                             spi_get_root_window (),
749                             TRUE,
750                             GrabModeSync, GrabModeSync);
751                   XSync (spi_get_display (), TRUE);
752                   XUngrabKey (spi_get_display (), i, 0, 
753                               spi_get_root_window ());
754                   if (!spi_x_error_release ())
755                   {
756                       reserved = i;
757                       break;
758                   }
759               }
760           }
761       }
762       if (reserved) 
763       {
764           priv->reserved_keycode = reserved;
765           priv->reserved_keysym = XkbKeycodeToKeysym (spi_get_display (), reserved, 0, 0);
766       }
767       else
768       { 
769           priv->reserved_keycode = XKeysymToKeycode (spi_get_display (), XK_numbersign);
770           priv->reserved_keysym = XK_numbersign;
771       }
772 #ifdef SPI_RESERVED_DEBUG
773       unsigned sym = 0;
774       sym = XKeycodeToKeysym (spi_get_display (), reserved, 0);
775       fprintf (stderr, "%x\n", sym);
776       fprintf (stderr, "setting the reserved keycode to %d (%s)\n", 
777                reserved, 
778                XKeysymToString (XKeycodeToKeysym (spi_get_display (),
779                                                             reserved, 0)));
780 #endif
781     }   
782
783   spi_set_filter (global_filter_fn, controller);
784   spi_set_events (KeyPressMask | KeyReleaseMask);
785
786   x_default_error_handler = XSetErrorHandler (_spi_controller_device_error_handler);
787 }
788
789 static Accessibility_DeviceEvent
790 spi_keystroke_from_x_key_event (XKeyEvent *x_key_event)
791 {
792   Accessibility_DeviceEvent key_event;
793   KeySym keysym;
794   const int cbuf_bytes = 20;
795   char cbuf [21];
796   int nbytes;
797
798   nbytes = XLookupString (x_key_event, cbuf, cbuf_bytes, &keysym, NULL);  
799   key_event.id = (dbus_int32_t)(keysym);
800   key_event.hw_code = (dbus_int16_t) x_key_event->keycode;
801   if (((XEvent *) x_key_event)->type == KeyPress)
802     {
803       key_event.type = Accessibility_KEY_PRESSED_EVENT;
804     }
805   else
806     {
807       key_event.type = Accessibility_KEY_RELEASED_EVENT;
808     } 
809   key_event.modifiers = (dbus_uint16_t)(x_key_event->state);
810   key_event.is_text = FALSE;
811   switch (keysym)
812     {
813       case ' ':
814         key_event.event_string = g_strdup ("space");
815         break;
816       case XK_Tab:
817         key_event.event_string = g_strdup ("Tab");
818         break;
819       case XK_BackSpace:
820         key_event.event_string = g_strdup ("Backspace");
821         break;
822       case XK_Return:
823         key_event.event_string = g_strdup ("Return");
824         break;
825       case XK_Home:
826         key_event.event_string = g_strdup ("Home");
827         break;
828       case XK_Page_Down:
829         key_event.event_string = g_strdup ("Page_Down");
830         break;
831       case XK_Page_Up:
832         key_event.event_string = g_strdup ("Page_Up");
833         break;
834       case XK_F1:
835         key_event.event_string = g_strdup ("F1");
836         break;
837       case XK_F2:
838         key_event.event_string = g_strdup ("F2");
839         break;
840       case XK_F3:
841         key_event.event_string = g_strdup ("F3");
842         break;
843       case XK_F4:
844         key_event.event_string = g_strdup ("F4");
845         break;
846       case XK_F5:
847         key_event.event_string = g_strdup ("F5");
848         break;
849       case XK_F6:
850         key_event.event_string = g_strdup ("F6");
851         break;
852       case XK_F7:
853         key_event.event_string = g_strdup ("F7");
854         break;
855       case XK_F8:
856         key_event.event_string = g_strdup ("F8");
857         break;
858       case XK_F9:
859         key_event.event_string = g_strdup ("F9");
860         break;
861       case XK_F10:
862         key_event.event_string = g_strdup ("F10");
863         break;
864       case XK_F11:
865         key_event.event_string = g_strdup ("F11");
866         break;
867       case XK_F12:
868         key_event.event_string = g_strdup ("F12");
869         break;
870       case XK_End:
871         key_event.event_string = g_strdup ("End");
872         break;
873       case XK_Escape:
874         key_event.event_string = g_strdup ("Escape");
875         break;
876       case XK_Up:
877         key_event.event_string = g_strdup ("Up");
878         break;
879       case XK_Down:
880         key_event.event_string = g_strdup ("Down");
881         break;
882       case XK_Left:
883         key_event.event_string = g_strdup ("Left");
884         break;
885       case XK_Right:
886         key_event.event_string = g_strdup ("Right");
887         break;
888       default:
889         if (nbytes > 0)
890           {
891             gunichar c;
892             cbuf[nbytes] = '\0'; /* OK since length is cbuf_bytes+1 */
893             key_event.event_string = g_strdup (cbuf);
894             c = keysym2ucs (keysym);
895             if (c > 0 && !g_unichar_iscntrl (c))
896               {
897                 key_event.is_text = TRUE; 
898                 /* incorrect for some composed chars? */
899               }
900           }
901         else
902           {
903             key_event.event_string = g_strdup ("");
904           }
905     }
906
907   key_event.timestamp = (dbus_uint32_t) x_key_event->time;
908 #ifdef SPI_KEYEVENT_DEBUG
909   {
910     char *pressed_str  = "pressed";
911     char *released_str = "released";
912     char *state_ptr;
913
914     if (key_event.type == Accessibility_KEY_PRESSED_EVENT)
915       state_ptr = pressed_str;
916     else
917       state_ptr = released_str;
918  
919     fprintf (stderr,
920              "Key %lu %s (%c), modifiers %d; string=%s [%x] %s\n",
921              (unsigned long) keysym,
922              state_ptr,
923              keysym ? (int) keysym : '*',
924              (int) x_key_event->state,
925              key_event.event_string,
926              key_event.event_string[0],
927              (key_event.is_text == TRUE) ? "(text)" : "(not text)");
928   }
929 #endif
930 #ifdef SPI_DEBUG
931   fprintf (stderr, "%s%c\n",
932      (x_key_event->state & Mod1Mask)?"Alt-":"",
933      ((x_key_event->state & ShiftMask)^(x_key_event->state & LockMask))?
934      g_ascii_toupper (keysym) : g_ascii_tolower (keysym));
935   fprintf (stderr, "serial: %x Time: %x\n", x_key_event->serial, x_key_event->time);
936 #endif /* SPI_DEBUG */
937   return key_event;     
938 }
939
940 static gboolean
941 spi_dec_x11_grab_key (SpiDEController *controller,
942                       guint key_val,
943                       Accessibility_ControllerEventMask mod_mask)
944 {
945   XGrabKey (spi_get_display (),
946             key_val,
947             mod_mask,
948             spi_get_root_window (),
949             True,
950             GrabModeSync,
951             GrabModeSync);
952   XSync (spi_get_display (), False);
953   return spi_clear_error_state ();
954 }
955
956 static void
957 spi_dec_x11_ungrab_key (SpiDEController *controller,
958                         guint key_val,
959                         Accessibility_ControllerEventMask mod_mask)
960 {
961   XUngrabKey (spi_get_display (),
962               key_val,
963               mod_mask,
964               spi_get_root_window ());
965 }
966
967 static unsigned int
968 xkb_get_slowkeys_delay (SpiDEController *controller)
969 {
970   unsigned int retval = 0;
971   DEControllerPrivateData *priv = controller->priv;
972 #ifdef HAVE_XKB
973 #ifdef XKB_HAS_GET_SLOW_KEYS_DELAY      
974   retval = XkbGetSlowKeysDelay (spi_get_display (),
975                                 XkbUseCoreKbd, &bounce_delay);
976 #else
977   if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
978     {
979       Status s = XkbGetControls (spi_get_display (),
980                                  XkbAllControlsMask, priv->xkb_desc);
981       if (s == Success)
982         {
983          if (priv->xkb_desc->ctrls->enabled_ctrls & XkbSlowKeysMask)
984                  retval = priv->xkb_desc->ctrls->slow_keys_delay;
985         }
986     }
987 #endif
988 #endif
989 #ifdef SPI_XKB_DEBUG
990         fprintf (stderr, "SlowKeys delay: %d\n", (int) retval);
991 #endif
992         return retval;
993 }
994
995 static unsigned int
996 xkb_get_bouncekeys_delay (SpiDEController *controller)
997 {
998   unsigned int retval = 0;
999   DEControllerPrivateData *priv = controller->priv;
1000 #ifdef HAVE_XKB
1001 #ifdef XKB_HAS_GET_BOUNCE_KEYS_DELAY    
1002   retval = XkbGetBounceKeysDelay (spi_get_display (),
1003                                   XkbUseCoreKbd, &bounce_delay);
1004 #else
1005   if (!(priv->xkb_desc == (XkbDescPtr) BadAlloc || priv->xkb_desc == NULL))
1006     {
1007       Status s = XkbGetControls (spi_get_display (),
1008                                  XkbAllControlsMask, priv->xkb_desc);
1009       if (s == Success)
1010         {
1011           if (priv->xkb_desc->ctrls->enabled_ctrls & XkbBounceKeysMask)
1012                   retval = priv->xkb_desc->ctrls->debounce_delay;
1013         }
1014     }
1015 #endif
1016 #endif
1017 #ifdef SPI_XKB_DEBUG
1018   fprintf (stderr, "BounceKeys delay: %d\n", (int) retval);
1019 #endif
1020   return retval;
1021 }
1022
1023 static gboolean
1024 spi_dec_x11_synth_keycode_press (SpiDEController *controller,
1025                          unsigned int keycode)
1026 {
1027         unsigned int time = CurrentTime;
1028         unsigned int bounce_delay;
1029         unsigned int elapsed_msec;
1030         struct timeval tv;
1031         DEControllerPrivateData *priv = controller->priv;
1032
1033         spi_x_error_trap ();
1034         if (keycode == priv->last_release_keycode)
1035         {
1036                 bounce_delay = xkb_get_bouncekeys_delay (controller); 
1037                 if (bounce_delay)
1038                 {
1039                         gettimeofday (&tv, NULL);
1040                         elapsed_msec =
1041                                 (tv.tv_sec - priv->last_release_time.tv_sec) * 1000
1042                                 + (tv.tv_usec - priv->last_release_time.tv_usec) / 1000;
1043 #ifdef SPI_XKB_DEBUG                    
1044                         fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1045                                  (long) (tv.tv_usec - priv->last_release_time.tv_usec));
1046 #endif
1047 #ifdef THIS_IS_BROKEN
1048                         if (elapsed_msec < bounce_delay)
1049                                 time = bounce_delay - elapsed_msec + 1;
1050 #else
1051                         time = bounce_delay + 10;
1052                         /* fudge for broken XTest */
1053 #endif
1054 #ifdef SPI_XKB_DEBUG                    
1055                         fprintf (stderr, "waiting %d ms\n", time);
1056 #endif
1057                 }
1058         }
1059         XTestFakeKeyEvent (spi_get_display (), keycode, True, time);
1060         priv->last_press_keycode = keycode;
1061         XFlush (spi_get_display ());
1062         XSync (spi_get_display (), False);
1063         gettimeofday (&priv->last_press_time, NULL);
1064         return TRUE;
1065 }
1066
1067 static gboolean
1068 spi_dec_x11_synth_keycode_release (SpiDEController *controller,
1069                            unsigned int keycode)
1070 {
1071         unsigned int time = CurrentTime;
1072         unsigned int slow_delay;
1073         unsigned int elapsed_msec;
1074         struct timeval tv;
1075         DEControllerPrivateData *priv = controller->priv;
1076
1077         spi_x_error_trap ();
1078         if (keycode == priv->last_press_keycode)
1079         {
1080                 slow_delay = xkb_get_slowkeys_delay (controller);
1081                 if (slow_delay)
1082                 {
1083                         gettimeofday (&tv, NULL);
1084                         elapsed_msec =
1085                                 (tv.tv_sec - priv->last_press_time.tv_sec) * 1000
1086                                 + (tv.tv_usec - priv->last_press_time.tv_usec) / 1000;
1087 #ifdef SPI_XKB_DEBUG                    
1088                         fprintf (stderr, "%d ms elapsed (%ld usec)\n", elapsed_msec,
1089                                  (long) (tv.tv_usec - priv->last_press_time.tv_usec));
1090 #endif
1091 #ifdef THIS_IS_BROKEN_DUNNO_WHY
1092                         if (elapsed_msec < slow_delay)
1093                                 time = slow_delay - elapsed_msec + 1;
1094 #else
1095                         time = slow_delay + 10;
1096                         /* our XTest seems broken, we have to add slop as above */
1097 #endif
1098 #ifdef SPI_XKB_DEBUG                    
1099                         fprintf (stderr, "waiting %d ms\n", time);
1100 #endif
1101                 }
1102         }
1103         XTestFakeKeyEvent (spi_get_display (), keycode, False, time);
1104         priv->last_release_keycode = keycode;
1105         XSync (spi_get_display (), False);
1106         gettimeofday (&priv->last_release_time, NULL);
1107         return TRUE;
1108 }
1109
1110 static gboolean
1111 spi_dec_x11_lock_modifiers (SpiDEController *controller, unsigned modifiers)
1112 {
1113     DEControllerPrivateData *priv = controller->priv;
1114     
1115     if (priv->have_xkb) {
1116         return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd, 
1117                                   modifiers, modifiers);
1118     } else {
1119         int mod_index;
1120         if (xmkeymap==NULL)
1121           xmkeymap = XGetModifierMapping(spi_get_display ());
1122         for (mod_index=0;mod_index<8;mod_index++)
1123             if (modifiers & (1<<mod_index))
1124                 spi_dec_x11_synth_keycode_press(controller, xmkeymap->modifiermap[mod_index]);
1125         return TRUE;
1126     }
1127 }
1128
1129 static gboolean
1130 spi_dec_x11_unlock_modifiers (SpiDEController *controller, unsigned modifiers)
1131 {
1132     DEControllerPrivateData *priv = controller->priv;
1133     
1134     if (priv->have_xkb) {
1135         return XkbLockModifiers (spi_get_display (), XkbUseCoreKbd, 
1136                                   modifiers, 0);
1137     } else {
1138         int mod_index;
1139         if (xmkeymap==NULL)
1140           xmkeymap = XGetModifierMapping(spi_get_display ());
1141
1142         for (mod_index=0;mod_index<8;mod_index++)
1143             if (modifiers & (1<<mod_index))
1144                 spi_dec_x11_synth_keycode_release(controller, xmkeymap->modifiermap[mod_index]);
1145         return TRUE;
1146     }
1147 }
1148
1149 static KeySym
1150 keysym_for_unichar (SpiDEController *controller, gunichar unichar)
1151 {
1152         return ucs2keysym ((long) unichar);
1153 }
1154
1155 static gboolean
1156 spi_dec_x11_synth_keystring (SpiDEController *controller, guint synth_type, gint keycode, const char *keystring)
1157 {
1158         /* probably we need to create and inject an XIM handler eventually. */
1159         /* for now, try to match the string to existing 
1160          * keycode+modifier states. 
1161          */
1162         KeySym *keysyms;
1163         gint maxlen = 0;
1164         gunichar unichar = 0;
1165         gint i = 0;
1166         gboolean retval = TRUE;
1167         const gchar *c;
1168         KeySym keysym;
1169
1170         maxlen = strlen (keystring) + 1;
1171         keysyms = g_new0 (KeySym, maxlen);
1172         if (!(keystring && *keystring && g_utf8_validate (keystring, -1, &c))) { 
1173                 retval = FALSE;
1174         } 
1175         else {
1176 #ifdef SPI_DEBUG
1177                 fprintf (stderr, "[keystring synthesis attempted on %s]\n", keystring);
1178 #endif
1179                 while (keystring && (unichar = g_utf8_get_char (keystring))) {
1180                         char bytes[6];
1181                         gint mbytes;
1182                         
1183                         mbytes = g_unichar_to_utf8 (unichar, bytes);
1184                         bytes[mbytes] = '\0';
1185 #ifdef SPI_DEBUG
1186                         fprintf (stderr, "[unichar %s]", bytes);
1187 #endif
1188                         keysym = keysym_for_unichar (controller, unichar);
1189                         if (keysym == NoSymbol) {
1190 #ifdef SPI_DEBUG
1191                                 fprintf (stderr, "no keysym for %s", bytes);
1192 #endif
1193                                 retval = FALSE;
1194                                 break;
1195                         }
1196                         keysyms[i++] = keysym;
1197                         keystring = g_utf8_next_char (keystring); 
1198                 }
1199                 keysyms[i++] = 0;
1200                 XSynchronize (spi_get_display (), TRUE);
1201                 for (i = 0; keysyms[i]; ++i) {
1202                         if (!spi_dec_synth_keysym (controller, keysyms[i])) {
1203 #ifdef SPI_DEBUG
1204                                 fprintf (stderr, "could not synthesize %c\n",
1205                                          (int) keysyms[i]);
1206 #endif
1207                                 retval = FALSE;
1208                                 break;
1209                         }
1210                 }
1211                 XSynchronize (spi_get_display (), FALSE);
1212         }
1213         g_free (keysyms);
1214
1215         if (synth_type == Accessibility_KEY_SYM) {
1216                 keysym = keycode;
1217         }
1218         else {
1219                 keysym = XkbKeycodeToKeysym (spi_get_display (), keycode, 0, 0);
1220         }
1221         if (XkbKeysymToModifiers (spi_get_display (), keysym) == 0)  {
1222                 spi_dec_clear_unlatch_pending (controller);
1223         }
1224         return retval;
1225 }
1226
1227 #ifdef HAVE_XEVIE
1228 static Bool
1229 isEvent(Display *dpy, XEvent *event, char *arg)
1230 {
1231    return TRUE;
1232 }
1233
1234 static gboolean
1235 handle_io (GIOChannel *source,
1236            GIOCondition condition,
1237            gpointer data) 
1238 {
1239   SpiDEController *controller = (SpiDEController *) data;
1240     DEControllerPrivateData *priv = controller->priv;
1241   gboolean is_consumed = FALSE;
1242   XEvent ev;
1243
1244   while (XCheckIfEvent(priv->xevie_display, &ev, isEvent, NULL))
1245     {
1246       if (ev.type == KeyPress || ev.type == KeyRelease)
1247         is_consumed = spi_device_event_controller_forward_key_event (controller, &ev);
1248
1249       if (! is_consumed)
1250         XevieSendEvent(priv->xevie_display, &ev, XEVIE_UNMODIFIED);
1251     }
1252
1253   return TRUE;
1254 }
1255 #endif /* HAVE_XEVIE */
1256
1257 static void
1258 spi_dec_x11_init (SpiDEController *controller)
1259 {
1260   DEControllerPrivateData *priv = controller->priv;     
1261   spi_events_init (spi_get_display());
1262 #ifdef HAVE_XEVIE
1263   GIOChannel *ioc;
1264   int fd;
1265 #endif /* HAVE_XEVIE */
1266
1267   spi_events_init (spi_get_display ());
1268 #ifdef HAVE_XEVIE
1269   priv->xevie_display = XOpenDisplay(NULL);
1270
1271   if (XevieStart(priv->xevie_display) == TRUE)
1272     {
1273 #ifdef SPI_KEYEVENT_DEBUG
1274       fprintf (stderr, "XevieStart() success \n");
1275 #endif
1276       XevieSelectInput(priv->xevie_display, KeyPressMask | KeyReleaseMask);
1277
1278       fd = ConnectionNumber(priv->xevie_display);
1279       ioc = g_io_channel_unix_new (fd);
1280       g_io_add_watch (ioc, G_IO_IN | G_IO_HUP, handle_io, controller);
1281       g_io_channel_unref (ioc);
1282     }
1283   else
1284     {
1285 #ifdef SPI_KEYEVENT_DEBUG
1286       fprintf (stderr, "XevieStart() failed, only one client is allowed to do event int exception\n");
1287 #endif
1288     }
1289 #endif /* HAVE_XEVIE */
1290
1291   gettimeofday (&priv->last_press_time, NULL);
1292   gettimeofday (&priv->last_release_time, NULL);
1293   spi_controller_register_with_devices (controller);
1294
1295   spi_dec_init_mouse_listener (controller);
1296
1297   saved_controller = controller;
1298 }
1299
1300 static void
1301 spi_dec_x11_finalize (SpiDEController *controller)
1302 {
1303                 DEControllerPrivateData *priv = controller->priv;
1304   /* disconnect any special listeners, get rid of outstanding keygrabs */
1305   XUngrabKey (spi_get_display (), AnyKey, AnyModifier, DefaultRootWindow (spi_get_display ()));
1306
1307 #ifdef HAVE_XEVIE
1308   if (priv->xevie_display != NULL)
1309     {
1310       XevieEnd(priv->xevie_display);
1311 #ifdef SPI_KEYEVENT_DEBUG
1312       printf("XevieEnd(dpy) finished \n");
1313 #endif
1314     }
1315 #endif
1316
1317   if (priv->xkb_desc)
1318   XkbFreeKeyboard (priv->xkb_desc, 0, True);
1319   /* TODO: Should free the keymap */
1320 }
1321
1322 static gboolean
1323 spi_device_event_controller_forward_key_event (SpiDEController *controller,
1324                                                const XEvent    *event)
1325 {
1326                 DEControllerPrivateData *priv = controller->priv;
1327   Accessibility_DeviceEvent key_event;
1328   gboolean ret;
1329
1330   g_assert (event->type == KeyPress || event->type == KeyRelease);
1331
1332   key_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1333
1334   if (priv->xevie_display == NULL)
1335     spi_controller_update_key_grabs (controller, &key_event);
1336
1337   /* relay to listeners, and decide whether to consume it or not */
1338   ret = spi_controller_notify_keylisteners (controller, &key_event, TRUE);
1339   g_free(key_event.event_string);
1340   return ret;
1341 }
1342
1343
1344 static gboolean
1345 is_key_released (long code)
1346 {
1347   char keys[32];
1348   int down;
1349
1350   XQueryKeymap (spi_get_display (), keys);
1351   down = BIT (keys, code);
1352   return (down == 0);
1353 }
1354
1355 static gboolean
1356 check_release (gpointer data)
1357 {
1358   gboolean released;
1359   Accessibility_DeviceEvent *event = (Accessibility_DeviceEvent *)data;
1360   KeyCode code = event->hw_code;
1361
1362   released = is_key_released (code);
1363
1364   if (released)
1365     {
1366       check_release_handler = 0;
1367       event->type = Accessibility_KEY_RELEASED_EVENT;
1368       spi_controller_notify_keylisteners (saved_controller, event, TRUE);
1369     }
1370   return (released == 0);
1371 }
1372
1373 static void
1374 wait_for_release_event (XEvent          *event,
1375                                     SpiDEController *controller)
1376 {
1377   pressed_event = spi_keystroke_from_x_key_event ((XKeyEvent *) event);
1378   check_release_handler = g_timeout_add (CHECK_RELEASE_DELAY, check_release, &pressed_event);
1379 }
1380
1381 static void
1382 spi_dec_x11_emit_modifier_event (SpiDEController *controller, guint prev_mask, 
1383                              guint current_mask)
1384 {
1385   dbus_uint32_t d1, d2;
1386
1387 #ifdef SPI_XKB_DEBUG
1388   fprintf (stderr, "MODIFIER CHANGE EVENT! %x to %x\n", 
1389            prev_mask, current_mask);
1390 #endif
1391
1392   /* set bits for the virtual modifiers like NUMLOCK */
1393   if (prev_mask & _numlock_physical_mask) 
1394     prev_mask |= SPI_KEYMASK_NUMLOCK;
1395   if (current_mask & _numlock_physical_mask) 
1396     current_mask |= SPI_KEYMASK_NUMLOCK;
1397
1398   d1 = prev_mask & key_modifier_mask;
1399   d2 = current_mask & key_modifier_mask;
1400       spi_dec_dbus_emit(controller, SPI_DBUS_INTERFACE_EVENT_KEYBOARD, "Modifiers", "", d1, d2);
1401 }
1402
1403 static void
1404 spi_dec_x11_generate_mouse_event (SpiDEController *controller,
1405                                   gint x,
1406                                   gint y,
1407                                   const char *eventName)
1408 {
1409   int button = 0;
1410   gboolean err = FALSE;
1411   Display *display = spi_get_display ();
1412
1413   switch (eventName[0])
1414     {
1415       case 'b':
1416         switch (eventName[1])
1417           {
1418           /* TODO: check number of buttons before parsing */
1419           case '1':
1420                     button = 1;
1421                     break;
1422           case '2':
1423                   button = 2;
1424                   break;
1425           case '3':
1426                   button = 3;
1427                   break;
1428           case '4':
1429                   button = 4;
1430                   break;
1431           case '5':
1432                   button = 5;
1433                   break;
1434           default:
1435                   err = TRUE;
1436           }
1437         if (!err)
1438           {
1439             if (x != -1 && y != -1)
1440               {
1441                 XTestFakeMotionEvent (display, DefaultScreen (display),
1442                                       x, y, 0);
1443               }
1444             XTestFakeButtonEvent (display, button, !(eventName[2] == 'r'), 0);
1445             if (eventName[2] == 'c')
1446               XTestFakeButtonEvent (display, button, FALSE, 1);
1447             else if (eventName[2] == 'd')
1448               {
1449               XTestFakeButtonEvent (display, button, FALSE, 1);
1450               XTestFakeButtonEvent (display, button, TRUE, 2);
1451               XTestFakeButtonEvent (display, button, FALSE, 3);
1452               }
1453           }
1454         break;
1455       case 'r': /* relative motion */ 
1456         XTestFakeRelativeMotionEvent (display, x, y, 0);
1457         break;
1458       case 'a': /* absolute motion */
1459         XTestFakeMotionEvent (display, DefaultScreen (display),
1460                               x, y, 0);
1461         break;
1462     }
1463 }
1464
1465 void
1466 spi_dec_setup_x11 (SpiDEControllerClass *klass)
1467 {
1468   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1469
1470   klass->plat.get_keycode = spi_dec_x11_get_keycode;
1471   klass->plat.mouse_check = spi_dec_x11_mouse_check;
1472   klass->plat.synth_keycode_press = spi_dec_x11_synth_keycode_press;
1473   klass->plat.synth_keycode_release = spi_dec_x11_synth_keycode_release;
1474   klass->plat.lock_modifiers = spi_dec_x11_lock_modifiers;
1475   klass->plat.unlock_modifiers = spi_dec_x11_unlock_modifiers;
1476   klass->plat.synth_keystring = spi_dec_x11_synth_keystring;
1477   klass->plat.grab_key = spi_dec_x11_grab_key;
1478   klass->plat.ungrab_key = spi_dec_x11_ungrab_key;
1479   klass->plat.emit_modifier_event = spi_dec_x11_emit_modifier_event;
1480   klass->plat.generate_mouse_event = spi_dec_x11_generate_mouse_event;
1481
1482   klass->plat.init = spi_dec_x11_init;
1483   klass->plat.finalize = spi_dec_x11_finalize;
1484
1485   g_type_class_add_private (object_class, sizeof (DEControllerPrivateData));
1486 }