gdkdevice-xi2.c 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gdkdevice-xi2.h"
23

24
#include "gdkintl.h"
25
#include "gdkasync.h"
Matthias Clasen's avatar
Matthias Clasen committed
26
#include "gdkprivate-x11.h"
27

28
#include <X11/extensions/XInput2.h>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

struct _GdkDeviceXI2Private
{
  int device_id;
};

static void gdk_device_xi2_get_property (GObject      *object,
                                         guint         prop_id,
                                         GValue       *value,
                                         GParamSpec   *pspec);
static void gdk_device_xi2_set_property (GObject      *object,
                                         guint         prop_id,
                                         const GValue *value,
                                         GParamSpec   *pspec);

static void gdk_device_xi2_get_state (GdkDevice       *device,
                                      GdkWindow       *window,
                                      gdouble         *axes,
                                      GdkModifierType *mask);
static void gdk_device_xi2_set_window_cursor (GdkDevice *device,
                                              GdkWindow *window,
                                              GdkCursor *cursor);
static void gdk_device_xi2_warp (GdkDevice *device,
                                 GdkScreen *screen,
                                 gint       x,
                                 gint       y);
static gboolean gdk_device_xi2_query_state (GdkDevice        *device,
                                            GdkWindow        *window,
                                            GdkWindow       **root_window,
                                            GdkWindow       **child_window,
                                            gint             *root_x,
                                            gint             *root_y,
                                            gint             *win_x,
                                            gint             *win_y,
                                            GdkModifierType  *mask);

static GdkGrabStatus gdk_device_xi2_grab   (GdkDevice     *device,
                                            GdkWindow     *window,
                                            gboolean       owner_events,
                                            GdkEventMask   event_mask,
                                            GdkWindow     *confine_to,
                                            GdkCursor     *cursor,
                                            guint32        time_);
static void          gdk_device_xi2_ungrab (GdkDevice     *device,
                                            guint32        time_);

static GdkWindow * gdk_device_xi2_window_at_position (GdkDevice       *device,
                                                      gint            *win_x,
                                                      gint            *win_y,
                                                      GdkModifierType *mask,
                                                      gboolean         get_toplevel);
static void  gdk_device_xi2_select_window_events (GdkDevice    *device,
                                                  GdkWindow    *window,
                                                  GdkEventMask  event_mask);


G_DEFINE_TYPE (GdkDeviceXI2, gdk_device_xi2, GDK_TYPE_DEVICE)

enum {
  PROP_0,
  PROP_DEVICE_ID
};

static void
gdk_device_xi2_class_init (GdkDeviceXI2Class *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);

  object_class->get_property = gdk_device_xi2_get_property;
  object_class->set_property = gdk_device_xi2_set_property;

  device_class->get_state = gdk_device_xi2_get_state;
  device_class->set_window_cursor = gdk_device_xi2_set_window_cursor;
  device_class->warp = gdk_device_xi2_warp;
  device_class->query_state = gdk_device_xi2_query_state;
  device_class->grab = gdk_device_xi2_grab;
  device_class->ungrab = gdk_device_xi2_ungrab;
  device_class->window_at_position = gdk_device_xi2_window_at_position;
  device_class->select_window_events = gdk_device_xi2_select_window_events;

  g_object_class_install_property (object_class,
				   PROP_DEVICE_ID,
				   g_param_spec_int ("device-id",
                                                     P_("Device ID"),
                                                     P_("Device identifier"),
                                                     0, G_MAXINT, 0,
                                                     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

  g_type_class_add_private (object_class, sizeof (GdkDeviceXI2Private));
}

static void
gdk_device_xi2_init (GdkDeviceXI2 *device)
{
  GdkDeviceXI2Private *priv;

  device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
                                                     GDK_TYPE_DEVICE_XI2,
                                                     GdkDeviceXI2Private);
}

static void
gdk_device_xi2_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GdkDeviceXI2Private *priv;

  priv = GDK_DEVICE_XI2 (object)->priv;

  switch (prop_id)
    {
    case PROP_DEVICE_ID:
      g_value_set_int (value, priv->device_id);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_device_xi2_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GdkDeviceXI2Private *priv;

  priv = GDK_DEVICE_XI2 (object)->priv;

  switch (prop_id)
    {
    case PROP_DEVICE_ID:
      priv->device_id = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdk_device_xi2_get_state (GdkDevice       *device,
                          GdkWindow       *window,
                          gdouble         *axes,
                          GdkModifierType *mask)
{
  GdkDeviceXI2Private *priv;
  GdkDisplay *display;
  XIDeviceInfo *info;
  gint i, j, ndevices;

  priv = GDK_DEVICE_XI2 (device)->priv;
  display = gdk_device_get_display (device);

  if (axes)
    {
      info = XIQueryDevice(GDK_DISPLAY_XDISPLAY (display),
                           priv->device_id, &ndevices);

      for (i = 0, j = 0; i < info->num_classes; i++)
        {
          XIAnyClassInfo *class_info = info->classes[i];
          GdkAxisUse use;
          gdouble value;

          if (class_info->type != XIValuatorClass)
            continue;

          value = ((XIValuatorClassInfo *) class_info)->value;
          use = _gdk_device_get_axis_use (device, j);

          switch (use)
            {
            case GDK_AXIS_X:
            case GDK_AXIS_Y:
            case GDK_AXIS_IGNORE:
209
              if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
              else
                {
                  gint root_x, root_y;

                  /* FIXME: Maybe root coords chaching should happen here */
                  gdk_window_get_origin (window, &root_x, &root_y);
                  _gdk_device_translate_screen_coord (device, window,
                                                      root_x, root_y,
                                                      j, value,
                                                      &axes[j]);
                }
              break;
            default:
              _gdk_device_translate_axis (device, j, value, &axes[j]);
              break;
            }

          j++;
        }

      XIFreeDeviceInfo (info);
    }

  if (mask)
    gdk_device_xi2_query_state (device, window,
                                NULL, NULL,
                                NULL, NULL,
                                NULL, NULL,
                                mask);
}

static void
gdk_device_xi2_set_window_cursor (GdkDevice *device,
                                  GdkWindow *window,
                                  GdkCursor *cursor)
{
  GdkDeviceXI2Private *priv;
  GdkCursorPrivate *cursor_private;

  priv = GDK_DEVICE_XI2 (device)->priv;

  /* Non-master devices don't have a cursor */
  if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
    return;

  if (cursor)
    {
      cursor_private = (GdkCursorPrivate*) cursor;

      XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
                      priv->device_id,
262
                      GDK_WINDOW_XID (window),
263 264 265 266 267
                      cursor_private->xcursor);
    }
  else
    XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
                      priv->device_id,
268
                      GDK_WINDOW_XID (window));
269 270 271 272 273 274 275 276 277 278 279 280
}

static void
gdk_device_xi2_warp (GdkDevice *device,
                     GdkScreen *screen,
                     gint       x,
                     gint       y)
{
  GdkDeviceXI2Private *priv;
  Window dest;

  priv = GDK_DEVICE_XI2 (device)->priv;
281
  dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

  XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
                 priv->device_id,
                 None, dest,
                 0, 0, 0, 0, x, y);
}

static gboolean
gdk_device_xi2_query_state (GdkDevice        *device,
                            GdkWindow        *window,
                            GdkWindow       **root_window,
                            GdkWindow       **child_window,
                            gint             *root_x,
                            gint             *root_y,
                            gint             *win_x,
                            gint             *win_y,
                            GdkModifierType  *mask)
{
  GdkDisplay *display;
301
  GdkScreen *default_screen;
302 303 304 305 306 307 308 309 310 311 312
  GdkDeviceXI2Private *priv;
  Window xroot_window, xchild_window;
  gdouble xroot_x, xroot_y, xwin_x, xwin_y;
  XIButtonState button_state;
  XIModifierState mod_state;
  XIGroupState group_state;

  if (!window || GDK_WINDOW_DESTROYED (window))
    return FALSE;

  priv = GDK_DEVICE_XI2 (device)->priv;
313
  display = gdk_window_get_display (window);
314
  default_screen = gdk_display_get_default_screen (display);
315

316
  if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
317
    {
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
      if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
                           priv->device_id,
                           GDK_WINDOW_XID (window),
                           &xroot_window,
                           &xchild_window,
                           &xroot_x,
                           &xroot_y,
                           &xwin_x,
                           &xwin_y,
                           &button_state,
                           &mod_state,
                           &group_state))
        return FALSE;
    }
  else
    {
      XSetWindowAttributes attributes;
      Display *xdisplay;
      Window xwindow, w;

      /* FIXME: untrusted clients not multidevice-safe */
      xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
      xwindow = GDK_SCREEN_XROOTWIN (default_screen);

      w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
                         CopyFromParent, InputOnly, CopyFromParent,
                         0, &attributes);
      XIQueryPointer (xdisplay, priv->device_id,
                      w,
                      &xroot_window,
                      &xchild_window,
                      &xroot_x,
                      &xroot_y,
                      &xwin_x,
                      &xwin_y,
                      &button_state,
                      &mod_state,
                      &group_state);
      XDestroyWindow (xdisplay, w);
357 358 359
    }

  if (root_window)
360
    *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
361 362

  if (child_window)
363
    *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

  if (root_x)
    *root_x = (gint) xroot_x;

  if (root_y)
    *root_y = (gint) xroot_y;

  if (win_x)
    *win_x = (gint) xwin_x;

  if (win_y)
    *win_y = (gint) xwin_y;

  if (mask)
    *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);

  return TRUE;
}

static GdkGrabStatus
gdk_device_xi2_grab (GdkDevice    *device,
                     GdkWindow    *window,
                     gboolean      owner_events,
                     GdkEventMask  event_mask,
                     GdkWindow    *confine_to,
                     GdkCursor    *cursor,
                     guint32       time_)
{
  GdkDeviceXI2Private *priv;
  GdkDisplay *display;
  XIEventMask mask;
  Window xwindow;
  Cursor xcursor;
397
  gint status;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  priv = GDK_DEVICE_XI2 (device)->priv;
  display = gdk_device_get_display (device);

  /* FIXME: confine_to is actually unused */

  xwindow = GDK_WINDOW_XID (window);

  if (!cursor)
    xcursor = None;
  else
    {
      _gdk_x11_cursor_update_theme (cursor);
      xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
    }

  mask.deviceid = priv->device_id;
  mask.mask = gdk_device_xi2_translate_event_mask (event_mask, &mask.mask_len);

417 418 419 420 421
#ifdef G_ENABLE_DEBUG
  if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
    status = GrabSuccess;
  else
#endif
422 423 424 425 426 427 428 429 430 431 432
  status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
                         priv->device_id,
                         xwindow,
                         time_,
                         xcursor,
                         GrabModeAsync, GrabModeAsync,
                         owner_events,
                         &mask);

  g_free (mask.mask);

433 434
  _gdk_x11_display_update_grab_info (display, device, status);

435 436 437 438 439 440 441 442 443
  return _gdk_x11_convert_grab_status (status);
}

static void
gdk_device_xi2_ungrab (GdkDevice *device,
                       guint32    time_)
{
  GdkDeviceXI2Private *priv;
  GdkDisplay *display;
444
  gulong serial;
445 446 447

  priv = GDK_DEVICE_XI2 (device)->priv;
  display = gdk_device_get_display (device);
448 449 450
  serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));

  XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), priv->device_id, time_);
451

452
  _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
}

static GdkWindow *
gdk_device_xi2_window_at_position (GdkDevice       *device,
                                   gint            *win_x,
                                   gint            *win_y,
                                   GdkModifierType *mask,
                                   gboolean         get_toplevel)
{
  GdkDeviceXI2Private *priv;
  GdkDisplay *display;
  GdkScreen *screen;
  Display *xdisplay;
  GdkWindow *window;
  Window xwindow, root, child, last = None;
  gdouble xroot_x, xroot_y, xwin_x, xwin_y;
  XIButtonState button_state;
  XIModifierState mod_state;
  XIGroupState group_state;

  priv = GDK_DEVICE_XI2 (device)->priv;
  display = gdk_device_get_display (device);
  screen = gdk_display_get_default_screen (display);

  /* This function really only works if the mouse pointer is held still
   * during its operation. If it moves from one leaf window to another
   * than we'll end up with inaccurate values for win_x, win_y
   * and the result.
   */
  gdk_x11_display_grab (display);

  xdisplay = GDK_SCREEN_XDISPLAY (screen);
  xwindow = GDK_SCREEN_XROOTWIN (screen);

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
    {
      XIQueryPointer (xdisplay,
                      priv->device_id,
                      xwindow,
                      &root, &child,
                      &xroot_x, &xroot_y,
                      &xwin_x, &xwin_y,
                      &button_state,
                      &mod_state,
                      &group_state);

      if (root == xwindow)
        xwindow = child;
      else
        xwindow = root;
    }
504
  else
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    {
      gint i, screens, width, height;
      GList *toplevels, *list;
      Window pointer_window, root, child;

      /* FIXME: untrusted clients case not multidevice-safe */
      pointer_window = None;
      screens = gdk_display_get_n_screens (display);

      for (i = 0; i < screens; ++i)
        {
          screen = gdk_display_get_screen (display, i);
          toplevels = gdk_screen_get_toplevel_windows (screen);
          for (list = toplevels; list != NULL; list = g_list_next (list))
            {
              window = GDK_WINDOW (list->data);
              xwindow = GDK_WINDOW_XID (window);
              gdk_x11_display_error_trap_push (display);
              XIQueryPointer (xdisplay,
                              priv->device_id,
                              xwindow,
                              &root, &child,
                              &xroot_x, &xroot_y,
                              &xwin_x, &xwin_y,
                              &button_state,
                              &mod_state,
                              &group_state);
              if (gdk_x11_display_error_trap_pop (display))
                continue;
              if (child != None)
                {
                  pointer_window = child;
                  break;
                }
              gdk_window_get_geometry (window, NULL, NULL, &width, &height);
              if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
                {
                  /* A childless toplevel, or below another window? */
                  XSetWindowAttributes attributes;
                  Window w;

                  w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
                                     CopyFromParent, InputOnly, CopyFromParent,
                                     0, &attributes);
                  XMapWindow (xdisplay, w);
                  XIQueryPointer (xdisplay,
                                  priv->device_id,
                                  xwindow,
                                  &root, &child,
                                  &xroot_x, &xroot_y,
                                  &xwin_x, &xwin_y,
                                  &button_state,
                                  &mod_state,
                                  &group_state);
                  XDestroyWindow (xdisplay, w);
                  if (child == w)
                    {
                      pointer_window = xwindow;
                      break;
                    }
                }
            }

          g_list_free (toplevels);
          if (pointer_window != None)
            break;
        }

      xwindow = pointer_window;
    }
575 576 577 578

  while (xwindow)
    {
      last = xwindow;
579
      gdk_x11_display_error_trap_push (display);
580 581 582 583 584 585 586 587 588
      XIQueryPointer (xdisplay,
                      priv->device_id,
                      xwindow,
                      &root, &xwindow,
                      &xroot_x, &xroot_y,
                      &xwin_x, &xwin_y,
                      &button_state,
                      &mod_state,
                      &group_state);
589 590
      if (gdk_x11_display_error_trap_pop (display))
        break;
591 592

      if (get_toplevel && last != root &&
593
          (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
594 595 596 597 598 599 600 601 602
          GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
        {
          xwindow = last;
          break;
        }
    }

  gdk_x11_display_ungrab (display);

603
  window = gdk_x11_window_lookup_for_display (display, last);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

  if (win_x)
    *win_x = (window) ? (gint) xwin_x : -1;

  if (win_y)
    *win_y = (window) ? (gint) xwin_y : -1;

  if (mask)
    *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);

  return window;
}

static void
gdk_device_xi2_select_window_events (GdkDevice    *device,
                                     GdkWindow    *window,
                                     GdkEventMask  event_mask)
{
  GdkDeviceXI2Private *priv;
  XIEventMask evmask;

  priv = GDK_DEVICE_XI2 (device)->priv;

  evmask.deviceid = priv->device_id;
  evmask.mask = gdk_device_xi2_translate_event_mask (event_mask, &evmask.mask_len);

  XISelectEvents (GDK_WINDOW_XDISPLAY (window),
631
                  GDK_WINDOW_XID (window),
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
                  &evmask, 1);

  g_free (evmask.mask);
}

guchar *
gdk_device_xi2_translate_event_mask (GdkEventMask  event_mask,
                                     int          *len)
{
  guchar *mask;

  *len = XIMaskLen (XI_LASTEVENT);
  mask = g_new0 (guchar, *len);

  if (event_mask & GDK_POINTER_MOTION_MASK ||
      event_mask & GDK_POINTER_MOTION_HINT_MASK)
    XISetMask (mask, XI_Motion);

  if (event_mask & GDK_BUTTON_MOTION_MASK ||
      event_mask & GDK_BUTTON1_MOTION_MASK ||
      event_mask & GDK_BUTTON2_MOTION_MASK ||
      event_mask & GDK_BUTTON3_MOTION_MASK)
    {
      XISetMask (mask, XI_ButtonPress);
      XISetMask (mask, XI_ButtonRelease);
      XISetMask (mask, XI_Motion);
    }

  if (event_mask & GDK_SCROLL_MASK)
    {
      XISetMask (mask, XI_ButtonPress);
      XISetMask (mask, XI_ButtonRelease);
    }

  if (event_mask & GDK_BUTTON_PRESS_MASK)
    XISetMask (mask, XI_ButtonPress);

  if (event_mask & GDK_BUTTON_RELEASE_MASK)
    XISetMask (mask, XI_ButtonRelease);

  if (event_mask & GDK_KEY_PRESS_MASK)
    XISetMask (mask, XI_KeyPress);

  if (event_mask & GDK_KEY_RELEASE_MASK)
    XISetMask (mask, XI_KeyRelease);

  if (event_mask & GDK_ENTER_NOTIFY_MASK)
    XISetMask (mask, XI_Enter);

  if (event_mask & GDK_LEAVE_NOTIFY_MASK)
    XISetMask (mask, XI_Leave);

  if (event_mask & GDK_FOCUS_CHANGE_MASK)
    {
      XISetMask (mask, XI_FocusIn);
      XISetMask (mask, XI_FocusOut);
    }

  return mask;
}

guint
gdk_device_xi2_translate_state (XIModifierState *mods_state,
                                XIButtonState   *buttons_state)
{
  guint state = 0;

  if (mods_state)
    state = (guint) mods_state->effective;

  if (buttons_state)
    {
      gint len, i;

      /* We're only interested in the first 5 buttons */
      len = MIN (5, buttons_state->mask_len * 8);

      for (i = 0; i < len; i++)
        {
          if (!XIMaskIsSet (buttons_state->mask, i))
            continue;

          switch (i)
            {
            case 1:
              state |= GDK_BUTTON1_MASK;
              break;
            case 2:
              state |= GDK_BUTTON2_MASK;
              break;
            case 3:
              state |= GDK_BUTTON3_MASK;
              break;
            case 4:
              state |= GDK_BUTTON4_MASK;
              break;
            case 5:
              state |= GDK_BUTTON5_MASK;
              break;
            default:
              break;
            }
        }
    }

  return state;
}