gdkdevice-core-x11.c 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include "gdkx11device-core.h"
21
#include "gdkdeviceprivate.h"
22

23
#include "gdkinternals.h"
24
#include "gdkwindow.h"
25
#include "gdkprivate-x11.h"
26
#include "gdkasync.h"
27

28 29 30 31 32 33 34 35 36 37
struct _GdkX11DeviceCore
{
  GdkDevice parent_instance;
};

struct _GdkX11DeviceCoreClass
{
  GdkDeviceClass parent_class;
};

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static gboolean gdk_x11_device_core_get_history (GdkDevice       *device,
                                                 GdkWindow       *window,
                                                 guint32          start,
                                                 guint32          stop,
                                                 GdkTimeCoord  ***events,
                                                 gint            *n_events);
static void     gdk_x11_device_core_get_state   (GdkDevice       *device,
                                                 GdkWindow       *window,
                                                 gdouble         *axes,
                                                 GdkModifierType *mask);
static void     gdk_x11_device_core_set_window_cursor (GdkDevice *device,
                                                       GdkWindow *window,
                                                       GdkCursor *cursor);
static void     gdk_x11_device_core_warp (GdkDevice *device,
                                          GdkScreen *screen,
                                          gint       x,
                                          gint       y);
55 56 57 58 59 60 61 62 63
static void gdk_x11_device_core_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);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static GdkGrabStatus gdk_x11_device_core_grab   (GdkDevice     *device,
                                                 GdkWindow     *window,
                                                 gboolean       owner_events,
                                                 GdkEventMask   event_mask,
                                                 GdkWindow     *confine_to,
                                                 GdkCursor     *cursor,
                                                 guint32        time_);
static void          gdk_x11_device_core_ungrab (GdkDevice     *device,
                                                 guint32        time_);
static GdkWindow * gdk_x11_device_core_window_at_position (GdkDevice       *device,
                                                           gint            *win_x,
                                                           gint            *win_y,
                                                           GdkModifierType *mask,
                                                           gboolean         get_toplevel);
static void      gdk_x11_device_core_select_window_events (GdkDevice       *device,
                                                           GdkWindow       *window,
                                                           GdkEventMask     event_mask);

G_DEFINE_TYPE (GdkX11DeviceCore, gdk_x11_device_core, GDK_TYPE_DEVICE)
83 84

static void
85
gdk_x11_device_core_class_init (GdkX11DeviceCoreClass *klass)
86 87 88
{
  GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);

89 90 91 92 93 94 95 96 97
  device_class->get_history = gdk_x11_device_core_get_history;
  device_class->get_state = gdk_x11_device_core_get_state;
  device_class->set_window_cursor = gdk_x11_device_core_set_window_cursor;
  device_class->warp = gdk_x11_device_core_warp;
  device_class->query_state = gdk_x11_device_core_query_state;
  device_class->grab = gdk_x11_device_core_grab;
  device_class->ungrab = gdk_x11_device_core_ungrab;
  device_class->window_at_position = gdk_x11_device_core_window_at_position;
  device_class->select_window_events = gdk_x11_device_core_select_window_events;
98 99 100
}

static void
101
gdk_x11_device_core_init (GdkX11DeviceCore *device_core)
102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
  GdkDevice *device;

  device = GDK_DEVICE (device_core);

  _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
  _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
}

static gboolean
impl_coord_in_window (GdkWindow *window,
		      int        impl_x,
		      int        impl_y)
{
116
  if (impl_x < window->abs_x ||
117
      impl_x >= window->abs_x + window->width)
118 119
    return FALSE;

120
  if (impl_y < window->abs_y ||
121
      impl_y >= window->abs_y + window->height)
122 123 124 125 126 127
    return FALSE;

  return TRUE;
}

static gboolean
128 129 130 131 132 133
gdk_x11_device_core_get_history (GdkDevice      *device,
                                 GdkWindow      *window,
                                 guint32         start,
                                 guint32         stop,
                                 GdkTimeCoord ***events,
                                 gint           *n_events)
134 135 136 137 138 139 140 141
{
  XTimeCoord *xcoords;
  GdkTimeCoord **coords;
  GdkWindow *impl_window;
  int tmp_n_events;
  int i, j;

  impl_window = _gdk_window_get_impl_window (window);
142 143
  xcoords = XGetMotionEvents (GDK_WINDOW_XDISPLAY (window),
                              GDK_WINDOW_XID (impl_window),
144 145 146 147 148 149 150 151 152 153 154
                              start, stop, &tmp_n_events);
  if (!xcoords)
    return FALSE;

  coords = _gdk_device_allocate_history (device, tmp_n_events);

  for (i = 0, j = 0; i < tmp_n_events; i++)
    {
      if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
        {
          coords[j]->time = xcoords[i].time;
155 156
          coords[j]->axes[0] = xcoords[i].x - window->abs_x;
          coords[j]->axes[1] = xcoords[i].y - window->abs_y;
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
          j++;
        }
    }

  XFree (xcoords);

  /* free the events we allocated too much */
  for (i = j; i < tmp_n_events; i++)
    {
      g_free (coords[i]);
      coords[i] = NULL;
    }

  tmp_n_events = j;

  if (tmp_n_events == 0)
    {
      gdk_device_free_history (coords, tmp_n_events);
      return FALSE;
    }

  if (n_events)
    *n_events = tmp_n_events;

  if (events)
    *events = coords;
  else if (coords)
    gdk_device_free_history (coords, tmp_n_events);

  return TRUE;
}

static void
190 191 192 193
gdk_x11_device_core_get_state (GdkDevice       *device,
                               GdkWindow       *window,
                               gdouble         *axes,
                               GdkModifierType *mask)
194 195 196
{
  gint x_int, y_int;

197
  gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
198 199 200 201 202 203 204 205 206

  if (axes)
    {
      axes[0] = x_int;
      axes[1] = y_int;
    }
}

static void
207 208 209
gdk_x11_device_core_set_window_cursor (GdkDevice *device,
                                       GdkWindow *window,
                                       GdkCursor *cursor)
210 211 212 213 214 215
{
  Cursor xcursor;

  if (!cursor)
    xcursor = None;
  else
216
    xcursor = gdk_x11_cursor_get_xcursor (cursor);
217 218 219 220 221 222 223

  XDefineCursor (GDK_WINDOW_XDISPLAY (window),
                 GDK_WINDOW_XID (window),
                 xcursor);
}

static void
224 225 226 227
gdk_x11_device_core_warp (GdkDevice *device,
                          GdkScreen *screen,
                          gint       x,
                          gint       y)
228 229 230 231 232
{
  Display *xdisplay;
  Window dest;

  xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
233
  dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
234 235 236 237

  XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
}

238
static void
239 240 241 242 243 244 245 246 247
gdk_x11_device_core_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)
248 249
{
  GdkDisplay *display;
250
  GdkScreen *default_screen;
251 252 253 254
  Window xroot_window, xchild_window;
  int xroot_x, xroot_y, xwin_x, xwin_y;
  unsigned int xmask;

255
  display = gdk_window_get_display (window);
256
  default_screen = gdk_display_get_default_screen (display);
257

Benjamin Otte's avatar
Benjamin Otte committed
258 259 260 261 262 263 264 265
  if (!GDK_X11_DISPLAY (display)->trusted_client ||
      !XQueryPointer (GDK_WINDOW_XDISPLAY (window),
                      GDK_WINDOW_XID (window),
                      &xroot_window,
                      &xchild_window,
                      &xroot_x, &xroot_y,
                      &xwin_x, &xwin_y,
                      &xmask))
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    {
      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);
      XQueryPointer (xdisplay, w,
                     &xroot_window,
                     &xchild_window,
281 282
                     &xroot_x, &xroot_y,
                     &xwin_x, &xwin_y,
283 284
                     &xmask);
      XDestroyWindow (xdisplay, w);
285 286 287
    }

  if (root_window)
288
    *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
289 290

  if (child_window)
291
    *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

  if (root_x)
    *root_x = xroot_x;

  if (root_y)
    *root_y = xroot_y;

  if (win_x)
    *win_x = xwin_x;

  if (win_y)
    *win_y = xwin_y;

  if (mask)
    *mask = xmask;
}

static GdkGrabStatus
310 311 312 313 314 315 316
gdk_x11_device_core_grab (GdkDevice    *device,
                          GdkWindow    *window,
                          gboolean      owner_events,
                          GdkEventMask  event_mask,
                          GdkWindow    *confine_to,
                          GdkCursor    *cursor,
                          guint32       time_)
317 318 319
{
  GdkDisplay *display;
  Window xwindow, xconfine_to;
320
  gint status;
321 322 323 324 325 326 327 328 329 330 331 332 333

  display = gdk_device_get_display (device);

  xwindow = GDK_WINDOW_XID (window);

  if (confine_to)
    confine_to = _gdk_window_get_impl_window (confine_to);

  if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
    xconfine_to = None;
  else
    xconfine_to = GDK_WINDOW_XID (confine_to);

334 335 336 337 338
#ifdef G_ENABLE_DEBUG
  if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
    status = GrabSuccess;
  else
#endif
339
  if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    {
      /* Device is a keyboard */
      status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
                              xwindow,
                              owner_events,
                              GrabModeAsync, GrabModeAsync,
                              time_);
    }
  else
    {
      Cursor xcursor;
      guint xevent_mask;
      gint i;

      /* Device is a pointer */
      if (!cursor)
        xcursor = None;
      else
        {
          _gdk_x11_cursor_update_theme (cursor);
360
          xcursor = gdk_x11_cursor_get_xcursor (cursor);
361 362 363 364
        }

      xevent_mask = 0;

365
      for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
366 367
        {
          if (event_mask & (1 << (i + 1)))
368
            xevent_mask |= _gdk_x11_event_mask_table[i];
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        }

      /* We don't want to set a native motion hint mask, as we're emulating motion
       * hints. If we set a native one we just wouldn't get any events.
       */
      xevent_mask &= ~PointerMotionHintMask;

      status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
                             xwindow,
                             owner_events,
                             xevent_mask,
                             GrabModeAsync, GrabModeAsync,
                             xconfine_to,
                             xcursor,
                             time_);
    }

386 387
  _gdk_x11_display_update_grab_info (display, device, status);

388 389 390 391
  return _gdk_x11_convert_grab_status (status);
}

static void
392 393
gdk_x11_device_core_ungrab (GdkDevice *device,
                            guint32    time_)
394 395
{
  GdkDisplay *display;
396
  gulong serial;
397 398

  display = gdk_device_get_display (device);
399
  serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
400

401
  if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
402 403 404
    XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
  else
    XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
405 406

  _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
407 408 409
}

static GdkWindow *
410 411 412 413 414
gdk_x11_device_core_window_at_position (GdkDevice       *device,
                                        gint            *win_x,
                                        gint            *win_y,
                                        GdkModifierType *mask,
                                        gboolean         get_toplevel)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
{
  GdkDisplay *display;
  GdkScreen *screen;
  Display *xdisplay;
  GdkWindow *window;
  Window xwindow, root, child, last;
  int xroot_x, xroot_y, xwin_x, xwin_y;
  unsigned int xmask;

  last = None;
  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);

438
  if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
439 440 441 442 443 444
    {
      XQueryPointer (xdisplay, xwindow,
                     &root, &child,
                     &xroot_x, &xroot_y,
                     &xwin_x, &xwin_y,
                     &xmask);
445

446 447 448 449 450
      if (root == xwindow)
        xwindow = child;
      else
       xwindow = root;
    }
451
  else
452
    {
453
      gint width, height;
454 455 456 457 458 459 460 461
      GList *toplevels, *list;
      Window pointer_window, root, child;
      int rootx = -1, rooty = -1;
      int winx, winy;
      unsigned int xmask;

      /* FIXME: untrusted clients case not multidevice-safe */
      pointer_window = None;
462 463 464
      screen = gdk_display_get_screen (display, 0);
      toplevels = gdk_screen_get_toplevel_windows (screen);
      for (list = toplevels; list != NULL; list = g_list_next (list))
465
        {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
          window = GDK_WINDOW (list->data);
          xwindow = GDK_WINDOW_XID (window);
          gdk_x11_display_error_trap_push (display);
          XQueryPointer (xdisplay, xwindow,
                         &root, &child,
                         &rootx, &rooty,
                         &winx, &winy,
                         &xmask);
          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 (winx >= 0 && winy >= 0 && winx < width && winy < height)
483
            {
484 485 486 487 488 489 490 491
              /* A childless toplevel, or below another window? */
              XSetWindowAttributes attributes;
              Window w;

              w = XCreateWindow (xdisplay, xwindow, winx, winy, 1, 1, 0,
                                 CopyFromParent, InputOnly, CopyFromParent,
                                 0, &attributes);
              XMapWindow (xdisplay, w);
492
              XQueryPointer (xdisplay, xwindow,
493 494 495 496
                             &root, &child,
                             &rootx, &rooty,
                             &winx, &winy,
                             &xmask);
497 498
              XDestroyWindow (xdisplay, w);
              if (child == w)
499
                {
500
                  pointer_window = xwindow;
501 502 503 504 505
                  break;
                }
            }
        }

506 507
      g_list_free (toplevels);

508 509
      xwindow = pointer_window;
    }
510 511 512 513

  while (xwindow)
    {
      last = xwindow;
514
      gdk_x11_display_error_trap_push (display);
515 516 517 518 519
      XQueryPointer (xdisplay, xwindow,
                     &root, &xwindow,
                     &xroot_x, &xroot_y,
                     &xwin_x, &xwin_y,
                     &xmask);
520 521
      if (gdk_x11_display_error_trap_pop (display))
        break;
522 523

      if (get_toplevel && last != root &&
524
          (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
525
          window->window_type != GDK_WINDOW_FOREIGN)
526 527 528 529 530 531 532 533
        {
          xwindow = last;
          break;
        }
    }

  gdk_x11_display_ungrab (display);

534
  window = gdk_x11_window_lookup_for_display (display, last);
535 536 537 538 539 540 541 542 543 544 545 546 547 548

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

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

  if (mask)
    *mask = xmask;

  return window;
}

static void
549 550 551
gdk_x11_device_core_select_window_events (GdkDevice    *device,
                                         GdkWindow    *window,
                                         GdkEventMask  event_mask)
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
{
  GdkEventMask filter_mask, window_mask;
  guint xmask = 0;
  gint i;

  window_mask = gdk_window_get_events (window);
  filter_mask = (GDK_POINTER_MOTION_MASK &
                 GDK_POINTER_MOTION_HINT_MASK &
                 GDK_BUTTON_MOTION_MASK &
                 GDK_BUTTON1_MOTION_MASK &
                 GDK_BUTTON2_MOTION_MASK &
                 GDK_BUTTON3_MOTION_MASK &
                 GDK_BUTTON_PRESS_MASK &
                 GDK_BUTTON_RELEASE_MASK &
                 GDK_KEY_PRESS_MASK &
                 GDK_KEY_RELEASE_MASK &
                 GDK_ENTER_NOTIFY_MASK &
                 GDK_LEAVE_NOTIFY_MASK &
                 GDK_FOCUS_CHANGE_MASK &
                 GDK_PROXIMITY_IN_MASK &
                 GDK_PROXIMITY_OUT_MASK &
                 GDK_SCROLL_MASK);

  /* Filter out non-device events */
  event_mask &= filter_mask;

  /* Unset device events on window mask */
  window_mask &= ~(filter_mask);

  /* Combine masks */
  event_mask |= window_mask;

584
  for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
585 586
    {
      if (event_mask & (1 << (i + 1)))
587
        xmask |= _gdk_x11_event_mask_table[i];
588 589 590 591 592 593
    }

  if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
    xmask |= StructureNotifyMask | PropertyChangeMask;

  XSelectInput (GDK_WINDOW_XDISPLAY (window),
594
                GDK_WINDOW_XID (window),
595 596
                xmask);
}