gdkdevice-xi.c 19.7 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-xi.h"
23 24 25

#include "gdkwindow.h"
#include "gdkdeviceprivate.h"
26 27
#include "gdkprivate-x11.h"
#include "gdkintl.h"
28
#include "gdkasync.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
#include "gdkx.h"

#define MAX_DEVICE_CLASSES 13

static GQuark quark_window_input_info = 0;

typedef struct
{
  gdouble root_x;
  gdouble root_y;
} GdkWindowInputInfo;

static void gdk_device_xi_constructed  (GObject *object);
static void gdk_device_xi_dispose      (GObject *object);

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

static gboolean gdk_device_xi_get_history (GdkDevice      *device,
                                           GdkWindow      *window,
                                           guint32         start,
                                           guint32         stop,
                                           GdkTimeCoord ***events,
58
                                           gint           *n_events);
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

static void gdk_device_xi_get_state       (GdkDevice       *device,
                                           GdkWindow       *window,
                                           gdouble         *axes,
                                           GdkModifierType *mask);
static void gdk_device_xi_set_window_cursor (GdkDevice *device,
                                             GdkWindow *window,
                                             GdkCursor *cursor);
static void gdk_device_xi_warp              (GdkDevice *device,
                                             GdkScreen *screen,
                                             gint       x,
                                             gint       y);
static gboolean gdk_device_xi_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_xi_grab     (GdkDevice    *device,
                                             GdkWindow    *window,
                                             gboolean      owner_events,
                                             GdkEventMask  event_mask,
                                             GdkWindow    *confine_to,
                                             GdkCursor    *cursor,
                                             guint32       time_);
static void          gdk_device_xi_ungrab   (GdkDevice    *device,
                                             guint32       time_);

static GdkWindow* gdk_device_xi_window_at_position (GdkDevice       *device,
                                                    gint            *win_x,
                                                    gint            *win_y,
                                                    GdkModifierType *mask,
                                                    gboolean         get_toplevel);

static void gdk_device_xi_select_window_events (GdkDevice    *device,
                                                GdkWindow    *window,
                                                GdkEventMask  mask);


G_DEFINE_TYPE (GdkDeviceXI, gdk_device_xi, GDK_TYPE_DEVICE)

enum {
  PROP_0,
  PROP_DEVICE_ID
};

static void
gdk_device_xi_class_init (GdkDeviceXIClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);

  quark_window_input_info = g_quark_from_static_string ("gdk-window-input-info");

  object_class->constructed = gdk_device_xi_constructed;
  object_class->set_property = gdk_device_xi_set_property;
  object_class->get_property = gdk_device_xi_get_property;
  object_class->dispose = gdk_device_xi_dispose;

  device_class->get_history = gdk_device_xi_get_history;
  device_class->get_state = gdk_device_xi_get_state;
  device_class->set_window_cursor = gdk_device_xi_set_window_cursor;
  device_class->warp = gdk_device_xi_warp;
  device_class->query_state = gdk_device_xi_query_state;
  device_class->grab = gdk_device_xi_grab;
  device_class->ungrab = gdk_device_xi_ungrab;
  device_class->window_at_position = gdk_device_xi_window_at_position;
  device_class->select_window_events = gdk_device_xi_select_window_events;

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

static void
gdk_device_xi_init (GdkDeviceXI *device)
{
}

static void
gdk_device_xi_constructed (GObject *object)
{
  GdkDeviceXI *device;
  GdkDisplay *display;

  device = GDK_DEVICE_XI (object);
  display = gdk_device_get_display (GDK_DEVICE (object));

154
  gdk_x11_display_error_trap_push (display);
155 156 157
  device->xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (display),
                                 device->device_id);

158
  if (gdk_x11_display_error_trap_pop (display))
159 160
    g_warning ("Device %s can't be opened",
               gdk_device_get_name (GDK_DEVICE (device)));
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

  if (G_OBJECT_CLASS (gdk_device_xi_parent_class)->constructed)
    G_OBJECT_CLASS (gdk_device_xi_parent_class)->constructed (object);
}

static void
gdk_device_xi_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  GdkDeviceXI *device = GDK_DEVICE_XI (object);

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

static void
gdk_device_xi_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GdkDeviceXI *device = GDK_DEVICE_XI (object);

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

static void
gdk_device_xi_dispose (GObject *object)
{
  GdkDeviceXI *device_xi;
  GdkDisplay *display;

  device_xi = GDK_DEVICE_XI (object);
  display = gdk_device_get_display (GDK_DEVICE (device_xi));

  if (device_xi->xdevice)
    {
      XCloseDevice (GDK_DISPLAY_XDISPLAY (display), device_xi->xdevice);
      device_xi->xdevice = NULL;
    }

  if (device_xi->axis_data)
    {
      g_free (device_xi->axis_data);
      device_xi->axis_data = NULL;
    }

  G_OBJECT_CLASS (gdk_device_xi_parent_class)->dispose (object);
}

static gboolean
gdk_device_xi_get_history (GdkDevice      *device,
                           GdkWindow      *window,
                           guint32         start,
                           guint32         stop,
                           GdkTimeCoord ***events,
234
                           gint           *n_events)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
{
  GdkTimeCoord **coords;
  XDeviceTimeCoord *device_coords;
  GdkWindow *impl_window;
  GdkDeviceXI *device_xi;
  gint n_events_return;
  gint mode_return;
  gint axis_count_return;
  gint i;

  device_xi = GDK_DEVICE_XI (device);
  impl_window = _gdk_window_get_impl_window (window);

  device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (impl_window),
					  device_xi->xdevice,
					  start, stop,
					  &n_events_return,
                                          &mode_return,
					  &axis_count_return);

  if (!device_coords)
    return FALSE;

258
  *n_events = n_events_return;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 357 358 359 360 361 362 363 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  coords = _gdk_device_allocate_history (device, *n_events);

  for (i = 0; i < *n_events; i++)
    {
      coords[i]->time = device_coords[i].time;
      gdk_device_xi_translate_axes (device, window,
                                    device_coords[i].data,
                                    coords[i]->axes,
                                    NULL, NULL);
    }

  XFreeDeviceMotionEvents (device_coords);

  *events = coords;

  return TRUE;
}

static void
gdk_device_xi_get_state (GdkDevice       *device,
                         GdkWindow       *window,
                         gdouble         *axes,
                         GdkModifierType *mask)
{
  GdkDeviceXI *device_xi;
  XDeviceState *state;
  XInputClass *input_class;
  gint i;

  if (mask)
    gdk_window_get_pointer (window, NULL, NULL, mask);

  device_xi = GDK_DEVICE_XI (device);
  state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
                             device_xi->xdevice);
  input_class = state->data;

  for (i = 0; i < state->num_classes; i++)
    {
      switch (input_class->class)
        {
        case ValuatorClass:
          if (axes)
            gdk_device_xi_translate_axes (device, window,
                                          ((XValuatorState *) input_class)->valuators,
                                          axes, NULL, NULL);
          break;

        case ButtonClass:
          if (mask)
            {
              *mask &= 0xFF;
              if (((XButtonState *)input_class)->num_buttons > 0)
                *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
              /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
               * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
            }
          break;
        }

      input_class = (XInputClass *)(((char *)input_class)+input_class->length);
    }

  XFreeDeviceState (state);
}

static void
gdk_device_xi_set_window_cursor (GdkDevice *device,
                                 GdkWindow *window,
                                 GdkCursor *cursor)
{
}

static void
gdk_device_xi_warp (GdkDevice *device,
                    GdkScreen *screen,
                    gint       x,
                    gint       y)
{
}

static void
find_events (GdkDevice    *device,
             GdkEventMask  mask,
             XEventClass  *classes,
             int          *num_classes)
{
  GdkDeviceXI *device_xi;
  XEventClass class;
  gint i;

  device_xi = GDK_DEVICE_XI (device);
  i = 0;

  if (mask & GDK_BUTTON_PRESS_MASK)
    {
      DeviceButtonPress (device_xi->xdevice, device_xi->button_press_type, class);
      if (class != 0)
        classes[i++] = class;

      DeviceButtonPressGrab (device_xi->xdevice, 0, class);
      if (class != 0)
        classes[i++] = class;
    }

  if (mask & GDK_BUTTON_RELEASE_MASK)
    {
      DeviceButtonRelease (device_xi->xdevice, device_xi->button_release_type, class);
      if (class != 0)
        classes[i++] = class;
    }

  if (mask & (GDK_POINTER_MOTION_MASK |
              GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
              GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK))
    {
      /* Make sure device->motionnotify_type is set */
      DeviceMotionNotify (device_xi->xdevice, device_xi->motion_notify_type, class);
      if (class != 0)
	  classes[i++] = class;
      DeviceStateNotify (device_xi->xdevice, device_xi->state_notify_type, class);
      if (class != 0)
	  classes[i++] = class;
    }

  if (mask & GDK_KEY_PRESS_MASK)
    {
      DeviceKeyPress (device_xi->xdevice, device_xi->key_press_type, class);
      if (class != 0)
        classes[i++] = class;
    }

  if (mask & GDK_KEY_RELEASE_MASK)
    {
      DeviceKeyRelease (device_xi->xdevice, device_xi->key_release_type, class);
      if (class != 0)
        classes[i++] = class;
    }

  if (mask & GDK_PROXIMITY_IN_MASK)
    {
      ProximityIn (device_xi->xdevice, device_xi->proximity_in_type, class);
      if (class != 0)
        classes[i++] = class;
    }

  if (mask & GDK_PROXIMITY_OUT_MASK)
    {
      ProximityOut (device_xi->xdevice, device_xi->proximity_out_type, class);
      if (class != 0)
        classes[i++] = class;
    }

  *num_classes = i;
}

static gboolean
gdk_device_xi_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)
{
  return FALSE;
}

static GdkGrabStatus
gdk_device_xi_grab (GdkDevice    *device,
                    GdkWindow    *window,
                    gboolean      owner_events,
                    GdkEventMask  event_mask,
                    GdkWindow    *confine_to,
                    GdkCursor    *cursor,
                    guint32       time_)
{
  XEventClass event_classes[MAX_DEVICE_CLASSES];
  gint status, num_classes;
  GdkDeviceXI *device_xi;
441
  GdkDisplay *display;
442 443

  device_xi = GDK_DEVICE_XI (device);
444
  display = gdk_device_get_display (device);
445 446
  find_events (device, event_mask, event_classes, &num_classes);

447 448 449 450 451 452
#ifdef G_ENABLE_DEBUG
  if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
    status = GrabSuccess;
  else
#endif
  status = XGrabDevice (GDK_DISPLAY_XDISPLAY (display),
453
                        device_xi->xdevice,
454
                        GDK_WINDOW_XID (window),
455 456 457 458 459
                        owner_events,
                        num_classes, event_classes,
                        GrabModeAsync, GrabModeAsync,
                        time_);

460 461
  _gdk_x11_display_update_grab_info (display, device, status);

462 463 464 465 466 467 468 469
  return _gdk_x11_convert_grab_status (status);
}

static void
gdk_device_xi_ungrab (GdkDevice *device,
                      guint32    time_)
{
  GdkDeviceXI *device_xi;
470 471 472
  GdkDisplay *display;
  Display *xdisplay;
  unsigned long serial;
473 474 475

  device_xi = GDK_DEVICE_XI (device);
  display = gdk_device_get_display (device);
476 477 478 479 480
  xdisplay = GDK_DISPLAY_XDISPLAY (display);

  serial = NextRequest (xdisplay);

  XUngrabDevice (xdisplay, device_xi->xdevice, time_);
481

482
  _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
483 484 485 486 487 488 489 490 491 492 493
}

static GdkWindow*
gdk_device_xi_window_at_position (GdkDevice       *device,
                                  gint            *win_x,
                                  gint            *win_y,
                                  GdkModifierType *mask,
                                  gboolean         get_toplevel)
{
  return NULL;
}
494

495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
static void
gdk_device_xi_select_window_events (GdkDevice    *device,
                                    GdkWindow    *window,
                                    GdkEventMask  event_mask)
{
  XEventClass event_classes[MAX_DEVICE_CLASSES];
  GdkDeviceXI *device_xi;
  gint num_classes;

  event_mask |= (GDK_PROXIMITY_IN_MASK |
                 GDK_PROXIMITY_OUT_MASK);

  device_xi = GDK_DEVICE_XI (device);
  find_events (device, event_mask, event_classes, &num_classes);

  XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (window),
511
			 GDK_WINDOW_XID (window),
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 575
			 event_classes, num_classes);

  if (event_mask)
    {
      GdkWindowInputInfo *info;

      info = g_new0 (GdkWindowInputInfo, 1);
      g_object_set_qdata_full (G_OBJECT (window),
                               quark_window_input_info,
                               info,
                               (GDestroyNotify) g_free);
    }
  else
    g_object_set_qdata (G_OBJECT (window),
                        quark_window_input_info,
                        NULL);
}

void
gdk_device_xi_update_window_info (GdkWindow *window)
{
  GdkWindowInputInfo *info;
  gint root_x, root_y;

  info = g_object_get_qdata (G_OBJECT (window),
                             quark_window_input_info);

  if (!info)
    return;

  gdk_window_get_origin (window, &root_x, &root_y);
  info->root_x = (gdouble) root_x;
  info->root_y = (gdouble) root_y;
}

static gboolean
gdk_device_xi_get_window_info (GdkWindow *window,
                               gdouble   *root_x,
                               gdouble   *root_y)
{
  GdkWindowInputInfo *info;

  info = g_object_get_qdata (G_OBJECT (window),
                             quark_window_input_info);

  if (!info)
    return FALSE;

  *root_x = info->root_x;
  *root_y = info->root_y;

  return TRUE;
}

void
gdk_device_xi_update_axes (GdkDevice *device,
                           gint       axes_count,
                           gint       first_axis,
                           gint      *axis_data)
{
  GdkDeviceXI *device_xi;
  int i;

  device_xi = GDK_DEVICE_XI (device);
576 577
  g_return_if_fail (first_axis >= 0 &&
                    first_axis + axes_count <= gdk_device_get_n_axes (device));
578 579

  if (!device_xi->axis_data)
580
    device_xi->axis_data = g_new0 (gint, gdk_device_get_n_axes (device));
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

  for (i = 0; i < axes_count; i++)
    device_xi->axis_data[first_axis + i] = axis_data[i];
}

void
gdk_device_xi_translate_axes (GdkDevice *device,
                              GdkWindow *window,
                              gint      *axis_data,
                              gdouble   *axes,
                              gdouble   *x,
                              gdouble   *y)
{
  GdkDeviceXI *device_xi;
  GdkWindow *impl_window;
  gdouble root_x, root_y;
  gdouble temp_x, temp_y;
598
  gint n_axes;
599 600 601 602 603 604 605 606 607
  gint i;

  device_xi = GDK_DEVICE_XI (device);
  impl_window = _gdk_window_get_impl_window (window);
  temp_x = temp_y = 0;

  if (!gdk_device_xi_get_window_info (impl_window, &root_x, &root_y))
    return;

608 609 610
  n_axes = gdk_device_get_n_axes (device);

  for (i = 0; i < n_axes; i++)
611 612 613 614 615 616 617 618 619
    {
      GdkAxisUse use;

      use = _gdk_device_get_axis_use (device, i);

      switch (use)
        {
        case GDK_AXIS_X:
        case GDK_AXIS_Y:
620
          if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            _gdk_device_translate_window_coord (device, window,
                                                i, axis_data[i],
                                                &axes[i]);
          else
            _gdk_device_translate_screen_coord (device, window,
                                                root_x, root_y,
                                                i, axis_data[i],
                                                &axes[i]);
          if (use == GDK_AXIS_X)
            temp_x = axes[i];
          else if (use == GDK_AXIS_Y)
            temp_y = axes[i];

          break;
        default:
          _gdk_device_translate_axis (device, i, axis_data[i], &axes[i]);
          break;
        }
    }

  if (x)
    *x = temp_x;

  if (y)
    *y = temp_y;
}