gdkdisplay-x11.c 78.7 KB
Newer Older
1 2 3
/* GDK - The GIMP Drawing Kit
 * gdkdisplay-x11.c
 * 
4 5
 * Copyright 2001 Sun Microsystems Inc.
 * Copyright (C) 2004 Nokia Corporation
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Erwann Chenede <erwann.chenede@sun.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

25
#include "config.h"
26

27
#include "gdkasync.h"
28
#include "gdkdisplay.h"
29 30
#include "gdkeventsource.h"
#include "gdkeventtranslator.h"
31
#include "gdkinternals.h"
32 33
#include "gdkscreen.h"
#include "gdkinternals.h"
34
#include "gdkdeviceprivate.h"
35
#include "gdkkeysprivate.h"
36
#include "gdkdevicemanager.h"
37
#include "xsettings-client.h"
Matthias Clasen's avatar
Matthias Clasen committed
38 39 40
#include "gdkdisplay-x11.h"
#include "gdkprivate-x11.h"
#include "gdkscreen-x11.h"
41

42 43 44 45 46 47 48
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

49 50 51 52 53 54
#include <X11/Xatom.h>

#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

55 56 57 58
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

59 60
#include <X11/extensions/shape.h>

61 62 63 64 65 66 67 68
#ifdef HAVE_XCOMPOSITE
#include <X11/extensions/Xcomposite.h>
#endif

#ifdef HAVE_XDAMAGE
#include <X11/extensions/Xdamage.h>
#endif

Søren Sandmann's avatar
Søren Sandmann committed
69 70 71 72
#ifdef HAVE_RANDR
#include <X11/extensions/Xrandr.h>
#endif

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
typedef struct _GdkErrorTrap  GdkErrorTrap;

struct _GdkErrorTrap
{
  /* Next sequence when trap was pushed, i.e. first sequence to
   * ignore
   */
  gulong start_sequence;

  /* Next sequence when trap was popped, i.e. first sequence
   * to not ignore. 0 if trap is still active.
   */
  gulong end_sequence;

  /* Most recent error code within the sequence */
  int error_code;
};
90

91 92
static void   gdk_x11_display_dispose            (GObject            *object);
static void   gdk_x11_display_finalize           (GObject            *object);
93

94
static void     gdk_x11_display_event_translator_init (GdkEventTranslatorIface *iface);
95

96
static gboolean gdk_x11_display_translate_event (GdkEventTranslator *translator,
97 98 99 100
                                                 GdkDisplay         *display,
                                                 GdkEvent           *event,
                                                 XEvent             *xevent);

101
#ifdef HAVE_X11R6
102 103 104 105 106
static void gdk_internal_connection_watch (Display  *display,
					   XPointer  arg,
					   gint      fd,
					   gboolean  opening,
					   XPointer *watch_data);
107
#endif /* HAVE_X11R6 */
108

109 110 111 112 113 114 115 116
typedef struct _GdkEventTypeX11 GdkEventTypeX11;

struct _GdkEventTypeX11
{
  gint base;
  gint n_events;
};

117 118 119 120 121 122 123 124
/* Note that we never *directly* use WM_LOCALE_NAME, WM_PROTOCOLS,
 * but including them here has the side-effect of getting them
 * into the internal Xlib cache
 */
static const char *const precache_atoms[] = {
  "UTF8_STRING",
  "WM_CLIENT_LEADER",
  "WM_DELETE_WINDOW",
125
  "WM_ICON_NAME",
126
  "WM_LOCALE_NAME",
127
  "WM_NAME",
128 129
  "WM_PROTOCOLS",
  "WM_TAKE_FOCUS",
130 131 132 133 134
  "WM_WINDOW_ROLE",
  "_NET_ACTIVE_WINDOW",
  "_NET_CURRENT_DESKTOP",
  "_NET_FRAME_EXTENTS",
  "_NET_STARTUP_ID",
135
  "_NET_WM_CM_S0",
136 137 138 139 140 141 142
  "_NET_WM_DESKTOP",
  "_NET_WM_ICON",
  "_NET_WM_ICON_NAME",
  "_NET_WM_NAME",
  "_NET_WM_PID",
  "_NET_WM_PING",
  "_NET_WM_STATE",
143 144 145 146
  "_NET_WM_STATE_ABOVE",
  "_NET_WM_STATE_BELOW",
  "_NET_WM_STATE_FULLSCREEN",
  "_NET_WM_STATE_MODAL",
147 148
  "_NET_WM_STATE_MAXIMIZED_VERT",
  "_NET_WM_STATE_MAXIMIZED_HORZ",
149 150 151
  "_NET_WM_STATE_SKIP_TASKBAR",
  "_NET_WM_STATE_SKIP_PAGER",
  "_NET_WM_STATE_STICKY",
152 153
  "_NET_WM_SYNC_REQUEST",
  "_NET_WM_SYNC_REQUEST_COUNTER",
154 155
  "_NET_WM_WINDOW_TYPE",
  "_NET_WM_WINDOW_TYPE_NORMAL",
156
  "_NET_WM_USER_TIME",
157 158
  "_NET_VIRTUAL_ROOTS",
  "GDK_SELECTION"
159 160
};

161 162
static char *gdk_sm_client_id;

163
G_DEFINE_TYPE_WITH_CODE (GdkX11Display, gdk_x11_display, GDK_TYPE_DISPLAY,
164
                         G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
165
                                                gdk_x11_display_event_translator_init))
166

167

Matthias Clasen's avatar
Matthias Clasen committed
168
static void
169
gdk_x11_display_init (GdkX11Display *display)
Matthias Clasen's avatar
Matthias Clasen committed
170
{
171
  _gdk_x11_display_manager_add_display (gdk_display_manager_get (),
172
                                        GDK_DISPLAY (display));
Matthias Clasen's avatar
Matthias Clasen committed
173
}
174

175
static void
176
gdk_x11_display_event_translator_init (GdkEventTranslatorIface *iface)
177
{
178
  iface->translate_event = gdk_x11_display_translate_event;
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 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
}

static void
do_net_wm_state_changes (GdkWindow *window)
{
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
  GdkWindowState old_state;

  if (GDK_WINDOW_DESTROYED (window) ||
      gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
    return;

  old_state = gdk_window_get_state (window);

  /* For found_sticky to remain TRUE, we have to also be on desktop
   * 0xFFFFFFFF
   */
  if (old_state & GDK_WINDOW_STATE_STICKY)
    {
      if (!(toplevel->have_sticky && toplevel->on_all_desktops))
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_STICKY,
                                     0);
    }
  else
    {
      if (toplevel->have_sticky || toplevel->on_all_desktops)
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_STICKY);
    }

  if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
    {
      if (!toplevel->have_fullscreen)
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_FULLSCREEN,
                                     0);
    }
  else
    {
      if (toplevel->have_fullscreen)
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_FULLSCREEN);
    }

  /* Our "maximized" means both vertical and horizontal; if only one,
   * we don't expose that via GDK
   */
  if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
    {
      if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_MAXIMIZED,
                                     0);
    }
  else
    {
      if (toplevel->have_maxvert && toplevel->have_maxhorz)
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_MAXIMIZED);
    }
}

static void
gdk_check_wm_desktop_changed (GdkWindow *window)
{
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
  GdkDisplay *display = GDK_WINDOW_DISPLAY (window);

  Atom type;
  gint format;
  gulong nitems;
  gulong bytes_after;
  guchar *data;
  gulong *desktop;

  type = None;
259
  gdk_x11_display_error_trap_push (display);
260 261 262 263 264 265
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
                      GDK_WINDOW_XID (window),
                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
                      0, G_MAXLONG, False, XA_CARDINAL, &type,
                      &format, &nitems,
                      &bytes_after, &data);
266
  gdk_x11_display_error_trap_pop_ignored (display);
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

  if (type != None)
    {
      desktop = (gulong *)data;
      toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
      XFree (desktop);
    }
  else
    toplevel->on_all_desktops = FALSE;

  do_net_wm_state_changes (window);
}

static void
gdk_check_wm_state_changed (GdkWindow *window)
{
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
  GdkDisplay *display = GDK_WINDOW_DISPLAY (window);

  Atom type;
  gint format;
  gulong nitems;
  gulong bytes_after;
  guchar *data;
  Atom *atoms = NULL;
  gulong i;

  gboolean had_sticky = toplevel->have_sticky;

  toplevel->have_sticky = FALSE;
  toplevel->have_maxvert = FALSE;
  toplevel->have_maxhorz = FALSE;
  toplevel->have_fullscreen = FALSE;

  type = None;
302
  gdk_x11_display_error_trap_push (display);
303 304 305 306
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
		      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
		      0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
		      &bytes_after, &data);
307
  gdk_x11_display_error_trap_pop_ignored (display);
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

  if (type != None)
    {
      Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
      Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
      Atom maxhorz_atom	= gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
      Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");

      atoms = (Atom *)data;

      i = 0;
      while (i < nitems)
        {
          if (atoms[i] == sticky_atom)
            toplevel->have_sticky = TRUE;
          else if (atoms[i] == maxvert_atom)
            toplevel->have_maxvert = TRUE;
          else if (atoms[i] == maxhorz_atom)
            toplevel->have_maxhorz = TRUE;
          else if (atoms[i] == fullscreen_atom)
            toplevel->have_fullscreen = TRUE;

          ++i;
        }

      XFree (atoms);
    }

  /* When have_sticky is turned on, we have to check the DESKTOP property
   * as well.
   */
  if (toplevel->have_sticky && !had_sticky)
    gdk_check_wm_desktop_changed (window);
  else
    do_net_wm_state_changes (window);
}

static GdkWindow *
get_event_window (GdkEventTranslator *translator,
                  XEvent             *xevent)
{
  GdkDisplay *display;
  Window xwindow;

  display = (GdkDisplay *) translator;

  switch (xevent->type)
    {
    case DestroyNotify:
      xwindow = xevent->xdestroywindow.window;
      break;
    case UnmapNotify:
      xwindow = xevent->xunmap.window;
      break;
    case MapNotify:
      xwindow = xevent->xmap.window;
      break;
    case ConfigureNotify:
      xwindow = xevent->xconfigure.window;
      break;
    default:
      xwindow = xevent->xany.window;
    }

372
  return gdk_x11_window_lookup_for_display (display, xwindow);
373 374 375
}

static gboolean
376
gdk_x11_display_translate_event (GdkEventTranslator *translator,
377 378 379 380 381 382 383
                                 GdkDisplay         *display,
                                 GdkEvent           *event,
                                 XEvent             *xevent)
{
  GdkWindow *window;
  GdkWindowImplX11 *window_impl = NULL;
  GdkScreen *screen = NULL;
384
  GdkX11Screen *x11_screen = NULL;
385
  GdkToplevelX11 *toplevel = NULL;
386
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  gboolean return_val;
  Window xwindow = None;

  /* Find the GdkWindow that this event relates to.
   * Basically this means substructure events
   * are reported same as structure events
   */
  window = get_event_window (translator, xevent);

  if (window)
    {
      /* We may receive events such as NoExpose/GraphicsExpose
       * and ShmCompletion for pixmaps
       */
      if (!GDK_IS_WINDOW (window))
        return FALSE;

      screen = GDK_WINDOW_SCREEN (window);
405
      x11_screen = GDK_X11_SCREEN (screen);
406
      toplevel = _gdk_x11_window_get_toplevel (window);
407
      window_impl = GDK_WINDOW_IMPL_X11 (window->impl);
408 409 410 411 412 413 414 415
      xwindow = GDK_WINDOW_XID (window);

      g_object_ref (window);
    }

  event->any.window = window;
  event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;

416
  if (window && GDK_WINDOW_DESTROYED (window))
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    {
      if (xevent->type != DestroyNotify)
	{
	  return_val = FALSE;
	  goto done;
	}
    }

  if (xevent->type == DestroyNotify)
    {
      int i, n;

      n = gdk_display_get_n_screens (display);
      for (i = 0; i < n; i++)
        {
          screen = gdk_display_get_screen (display, i);
433
          x11_screen = GDK_X11_SCREEN (screen);
434

435
          if (x11_screen->wmspec_check_window == xwindow)
436
            {
437 438 439 440
              x11_screen->wmspec_check_window = None;
              x11_screen->last_wmspec_check_time = 0;
              g_free (x11_screen->window_manager_name);
              x11_screen->window_manager_name = g_strdup ("unknown");
441 442 443 444 445 446 447 448 449 450 451 452 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

              /* careful, reentrancy */
              _gdk_x11_screen_window_manager_changed (screen);

              return_val = FALSE;
              goto done;
            }
        }
    }

  /* We do a "manual" conversion of the XEvent to a
   *  GdkEvent. The structures are mostly the same so
   *  the conversion is fairly straightforward. We also
   *  optionally print debugging info regarding events
   *  received.
   */

  return_val = TRUE;

  switch (xevent->type)
    {
    case KeymapNotify:
      GDK_NOTE (EVENTS,
		g_message ("keymap notify"));

      /* Not currently handled */
      return_val = FALSE;
      break;

    case Expose:
      GDK_NOTE (EVENTS,
		g_message ("expose:\t\twindow: %ld  %d	x,y: %d %d  w,h: %d %d%s",
			   xevent->xexpose.window, xevent->xexpose.count,
			   xevent->xexpose.x, xevent->xexpose.y,
			   xevent->xexpose.width, xevent->xexpose.height,
			   event->any.send_event ? " (send)" : ""));

478
      if (window == NULL)
479 480 481 482 483 484 485 486 487 488 489 490 491
        {
          return_val = FALSE;
          break;
        }

      {
	GdkRectangle expose_rect;

	expose_rect.x = xevent->xexpose.x;
	expose_rect.y = xevent->xexpose.y;
	expose_rect.width = xevent->xexpose.width;
	expose_rect.height = xevent->xexpose.height;

492
        _gdk_x11_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
493 494 495 496 497 498 499 500 501 502 503 504 505
        return_val = FALSE;
      }

      break;

    case GraphicsExpose:
      {
	GdkRectangle expose_rect;

        GDK_NOTE (EVENTS,
		  g_message ("graphics expose:\tdrawable: %ld",
			     xevent->xgraphicsexpose.drawable));

506
        if (window == NULL)
507 508 509 510 511 512 513 514 515 516
          {
            return_val = FALSE;
            break;
          }

	expose_rect.x = xevent->xgraphicsexpose.x;
	expose_rect.y = xevent->xgraphicsexpose.y;
	expose_rect.width = xevent->xgraphicsexpose.width;
	expose_rect.height = xevent->xgraphicsexpose.height;

517
        _gdk_x11_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
        return_val = FALSE;
      }
      break;

    case VisibilityNotify:
#ifdef G_ENABLE_DEBUG
      if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
	switch (xevent->xvisibility.state)
	  {
	  case VisibilityFullyObscured:
	    g_message ("visibility notify:\twindow: %ld	 none",
		       xevent->xvisibility.window);
	    break;
	  case VisibilityPartiallyObscured:
	    g_message ("visibility notify:\twindow: %ld	 partial",
		       xevent->xvisibility.window);
	    break;
	  case VisibilityUnobscured:
	    g_message ("visibility notify:\twindow: %ld	 full",
		       xevent->xvisibility.window);
	    break;
	  }
#endif /* G_ENABLE_DEBUG */

542
      if (window == NULL)
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 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
        {
          return_val = FALSE;
          break;
        }

      event->visibility.type = GDK_VISIBILITY_NOTIFY;
      event->visibility.window = window;

      switch (xevent->xvisibility.state)
	{
	case VisibilityFullyObscured:
	  event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
	  break;

	case VisibilityPartiallyObscured:
	  event->visibility.state = GDK_VISIBILITY_PARTIAL;
	  break;

	case VisibilityUnobscured:
	  event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
	  break;
	}

      break;

    case CreateNotify:
      GDK_NOTE (EVENTS,
		g_message ("create notify:\twindow: %ld  x,y: %d %d	w,h: %d %d  b-w: %d  parent: %ld	 ovr: %d",
			   xevent->xcreatewindow.window,
			   xevent->xcreatewindow.x,
			   xevent->xcreatewindow.y,
			   xevent->xcreatewindow.width,
			   xevent->xcreatewindow.height,
			   xevent->xcreatewindow.border_width,
			   xevent->xcreatewindow.parent,
			   xevent->xcreatewindow.override_redirect));
      /* not really handled */
      break;

    case DestroyNotify:
      GDK_NOTE (EVENTS,
		g_message ("destroy notify:\twindow: %ld",
			   xevent->xdestroywindow.window));

      /* Ignore DestroyNotify from SubstructureNotifyMask */
      if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
	{
	  event->any.type = GDK_DESTROY;
	  event->any.window = window;

593
	  return_val = window && !GDK_WINDOW_DESTROYED (window);
594

595
	  if (window && GDK_WINDOW_XID (window) != x11_screen->xroot_window)
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	    gdk_window_destroy_notify (window);
	}
      else
	return_val = FALSE;

      break;

    case UnmapNotify:
      GDK_NOTE (EVENTS,
		g_message ("unmap notify:\t\twindow: %ld",
			   xevent->xmap.window));

      event->any.type = GDK_UNMAP;
      event->any.window = window;

      /* If we are shown (not withdrawn) and get an unmap, it means we
       * were iconified in the X sense. If we are withdrawn, and get
       * an unmap, it means we hid the window ourselves, so we
       * will have already flipped the iconified bit off.
       */
      if (window)
617 618 619 620 621
        {
          if (GDK_WINDOW_IS_MAPPED (window))
            gdk_synthesize_window_state (window,
                                         0,
                                         GDK_WINDOW_STATE_ICONIFIED);
622

623 624
          _gdk_x11_window_grab_check_unmap (window, xevent->xany.serial);
        }
625 626 627 628 629 630 631 632 633 634 635 636

      break;

    case MapNotify:
      GDK_NOTE (EVENTS,
		g_message ("map notify:\t\twindow: %ld",
			   xevent->xmap.window));

      event->any.type = GDK_MAP;
      event->any.window = window;

      /* Unset iconified if it was set */
637
      if (window && (window->state & GDK_WINDOW_STATE_ICONIFIED))
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
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_ICONIFIED,
                                     0);

      break;

    case ReparentNotify:
      GDK_NOTE (EVENTS,
		g_message ("reparent notify:\twindow: %ld  x,y: %d %d  parent: %ld	ovr: %d",
			   xevent->xreparent.window,
			   xevent->xreparent.x,
			   xevent->xreparent.y,
			   xevent->xreparent.parent,
			   xevent->xreparent.override_redirect));

      /* Not currently handled */
      return_val = FALSE;
      break;

    case ConfigureNotify:
      GDK_NOTE (EVENTS,
		g_message ("configure notify:\twindow: %ld  x,y: %d %d	w,h: %d %d  b-w: %d  above: %ld	 ovr: %d%s",
			   xevent->xconfigure.window,
			   xevent->xconfigure.x,
			   xevent->xconfigure.y,
			   xevent->xconfigure.width,
			   xevent->xconfigure.height,
			   xevent->xconfigure.border_width,
			   xevent->xconfigure.above,
			   xevent->xconfigure.override_redirect,
			   !window
			   ? " (discarding)"
670
			   : window->window_type == GDK_WINDOW_CHILD
671 672 673 674 675 676
			   ? " (discarding child)"
			   : xevent->xconfigure.event != xevent->xconfigure.window
			   ? " (discarding substructure)"
			   : ""));
      if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
        {
677 678
	  window->width = xevent->xconfigure.width;
	  window->height = xevent->xconfigure.height;
679 680

	  _gdk_window_update_size (window);
681
	  _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
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
	  _gdk_x11_screen_size_changed (screen, xevent);
        }

#ifdef HAVE_XSYNC
      if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
	{
	  toplevel->current_counter_value = toplevel->pending_counter_value;
	  XSyncIntToValue (&toplevel->pending_counter_value, 0);
	}
#endif

    if (!window ||
	  xevent->xconfigure.event != xevent->xconfigure.window ||
          GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
          GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
	return_val = FALSE;
      else
	{
	  event->configure.type = GDK_CONFIGURE;
	  event->configure.window = window;
	  event->configure.width = xevent->xconfigure.width;
	  event->configure.height = xevent->xconfigure.height;

	  if (!xevent->xconfigure.send_event &&
	      !xevent->xconfigure.override_redirect &&
	      !GDK_WINDOW_DESTROYED (window))
	    {
	      gint tx = 0;
	      gint ty = 0;
	      Window child_window = 0;

713
	      gdk_x11_display_error_trap_push (display);
714 715
	      if (XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
					 GDK_WINDOW_XID (window),
716
					 x11_screen->xroot_window,
717 718 719 720 721 722 723
					 0, 0,
					 &tx, &ty,
					 &child_window))
		{
		  event->configure.x = tx;
		  event->configure.y = ty;
		}
724
	      gdk_x11_display_error_trap_pop_ignored (display);
725 726 727 728 729 730
	    }
	  else
	    {
	      event->configure.x = xevent->xconfigure.x;
	      event->configure.y = xevent->xconfigure.y;
	    }
731 732 733 734
	  window->x = event->configure.x;
	  window->y = event->configure.y;
	  window->width = xevent->xconfigure.width;
	  window->height = xevent->xconfigure.height;
735 736

	  _gdk_window_update_size (window);
737
	  _gdk_x11_window_update_size (GDK_WINDOW_IMPL_X11 (window->impl));
738

739 740 741
          if (window->resize_count >= 1)
            {
              window->resize_count -= 1;
742

743 744 745 746
              if (window->resize_count == 0)
                _gdk_x11_moveresize_configure_done (display, window);
            }
        }
747 748 749 750 751 752 753 754 755 756 757
      break;

    case PropertyNotify:
      GDK_NOTE (EVENTS,
		g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
			   xevent->xproperty.window,
			   xevent->xproperty.atom,
			   "\"",
			   gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
			   "\""));

758
      if (window == NULL)
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
        {
	  return_val = FALSE;
          break;
        }

      /* We compare with the serial of the last time we mapped the
       * window to avoid refetching properties that we set ourselves
       */
      if (toplevel &&
	  xevent->xproperty.serial >= toplevel->map_serial)
	{
	  if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
	    gdk_check_wm_state_changed (window);

	  if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
	    gdk_check_wm_desktop_changed (window);
	}

777
      if (window->event_mask & GDK_PROPERTY_CHANGE_MASK)
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
	{
	  event->property.type = GDK_PROPERTY_NOTIFY;
	  event->property.window = window;
	  event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
	  event->property.time = xevent->xproperty.time;
	  event->property.state = xevent->xproperty.state;
	}
      else
	return_val = FALSE;

      break;

    case SelectionClear:
      GDK_NOTE (EVENTS,
		g_message ("selection clear:\twindow: %ld",
			   xevent->xproperty.window));

795
      if (_gdk_x11_selection_filter_clear_event (&xevent->xselectionclear))
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	{
	  event->selection.type = GDK_SELECTION_CLEAR;
	  event->selection.window = window;
	  event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
	  event->selection.time = xevent->xselectionclear.time;
	}
      else
	return_val = FALSE;

      break;

    case SelectionRequest:
      GDK_NOTE (EVENTS,
		g_message ("selection request:\twindow: %ld",
			   xevent->xproperty.window));

      event->selection.type = GDK_SELECTION_REQUEST;
      event->selection.window = window;
      event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
      event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
      event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
      event->selection.requestor = xevent->xselectionrequest.requestor;
      event->selection.time = xevent->xselectionrequest.time;

      break;

    case SelectionNotify:
      GDK_NOTE (EVENTS,
		g_message ("selection notify:\twindow: %ld",
			   xevent->xproperty.window));

      event->selection.type = GDK_SELECTION_NOTIFY;
      event->selection.window = window;
      event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
      event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
      if (xevent->xselection.property == None)
        event->selection.property = GDK_NONE;
      else
        event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
      event->selection.time = xevent->xselection.time;

      break;

    case ColormapNotify:
      GDK_NOTE (EVENTS,
		g_message ("colormap notify:\twindow: %ld",
			   xevent->xcolormap.window));

      /* Not currently handled */
      return_val = FALSE;
      break;

    case ClientMessage:
      {
	GList *tmp_list;
	GdkFilterReturn result = GDK_FILTER_CONTINUE;
	GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);

	GDK_NOTE (EVENTS,
		  g_message ("client message:\twindow: %ld",
			     xevent->xclient.window));

	tmp_list = display_x11->client_filters;
	while (tmp_list)
	  {
	    GdkClientFilter *filter = tmp_list->data;
	    tmp_list = tmp_list->next;

	    if (filter->type == message_type)
	      {
		result = (*filter->function) (xevent, event, filter->data);
		if (result != GDK_FILTER_CONTINUE)
		  break;
	      }
	  }

	switch (result)
	  {
	  case GDK_FILTER_REMOVE:
	    return_val = FALSE;
	    break;
	  case GDK_FILTER_TRANSLATE:
	    return_val = TRUE;
	    break;
	  case GDK_FILTER_CONTINUE:
	    /* Send unknown ClientMessage's on to Gtk for it to use */
882
            if (window == NULL)
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
              {
                return_val = FALSE;
              }
            else
              {
                event->client.type = GDK_CLIENT_EVENT;
                event->client.window = window;
                event->client.message_type = message_type;
                event->client.data_format = xevent->xclient.format;
                memcpy(&event->client.data, &xevent->xclient.data,
                       sizeof(event->client.data));
              }
            break;
          }
      }

      break;

    case MappingNotify:
      GDK_NOTE (EVENTS,
		g_message ("mapping notify"));

      /* Let XLib know that there is a new keyboard mapping.
       */
      XRefreshKeyboardMapping (&xevent->xmapping);
908
      _gdk_x11_keymap_keys_changed (display);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
      return_val = FALSE;
      break;

    default:
#ifdef HAVE_XFIXES
      if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
	{
	  XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;

	  _gdk_x11_screen_process_owner_change (screen, xevent);
	  
	  event->owner_change.type = GDK_OWNER_CHANGE;
	  event->owner_change.window = window;
	  event->owner_change.owner = selection_notify->owner;
	  event->owner_change.reason = selection_notify->subtype;
	  event->owner_change.selection = 
	    gdk_x11_xatom_to_atom_for_display (display, 
					       selection_notify->selection);
	  event->owner_change.time = selection_notify->timestamp;
	  event->owner_change.selection_time = selection_notify->selection_timestamp;
	  
	  return_val = TRUE;
	}
      else
#endif
#ifdef HAVE_RANDR
      if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
          xevent->type - display_x11->xrandr_event_base == RRNotify)
	{
          if (screen)
            _gdk_x11_screen_size_changed (screen, xevent);
	}
      else
#endif
#if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
944
      if (display_x11->have_xdamage && window && window->composited &&
945 946 947 948 949 950 951
	  xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
	  ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
	{
	  XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
	  XserverRegion repair;
	  GdkRectangle rect;

952 953
	  rect.x = window->x + damage_event->area.x;
	  rect.y = window->y + damage_event->area.y;
954 955 956 957 958 959 960 961 962 963
	  rect.width = damage_event->area.width;
	  rect.height = damage_event->area.height;

	  repair = XFixesCreateRegion (display_x11->xdisplay,
				       &damage_event->area, 1);
	  XDamageSubtract (display_x11->xdisplay,
			   window_impl->damage,
			   repair, None);
	  XFixesDestroyRegion (display_x11->xdisplay, repair);

964 965 966
          if (window->parent != NULL)
           _gdk_x11_window_process_expose (window->parent,
                                           damage_event->serial, &rect);
967 968 969 970 971 972 973 974 975 976 977 978 979 980

	  return_val = TRUE;
	}
      else
#endif
#ifdef HAVE_XKB
      if (xevent->type == display_x11->xkb_event_type)
	{
	  XkbEvent *xkb_event = (XkbEvent *) xevent;

	  switch (xkb_event->any.xkb_type)
	    {
	    case XkbNewKeyboardNotify:
	    case XkbMapNotify:
981
	      _gdk_x11_keymap_keys_changed (display);
982 983 984 985 986

	      return_val = FALSE;
	      break;

	    case XkbStateNotify:
987
	      _gdk_x11_keymap_state_changed (display, xevent);
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	      break;
	    }
	}
      else
#endif
        return_val = FALSE;
    }

 done:
  if (return_val)
    {
      if (event->any.window)
	g_object_ref (event->any.window);
    }
  else
    {
      /* Mark this event as having no resources to be freed */
      event->any.window = NULL;
      event->any.type = GDK_NOTHING;
    }

  if (window)
    g_object_unref (window);

  return return_val;
}

static GdkFilterReturn
gdk_wm_protocols_filter (GdkXEvent *xev,
			 GdkEvent  *event,
			 gpointer data)
{
  XEvent *xevent = (XEvent *)xev;
  GdkWindow *win = event->any.window;
  GdkDisplay *display;
  Atom atom;

  if (!win)
      return GDK_FILTER_REMOVE;

  display = GDK_WINDOW_DISPLAY (win);
  atom = (Atom)xevent->xclient.data.l[0];

  if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
    {
  /* The delete window request specifies a window
   *  to delete. We don't actually destroy the
   *  window because "it is only a request". (The
   *  window might contain vital data that the
   *  program does not want destroyed). Instead
   *  the event is passed along to the program,
   *  which should then destroy the window.
   */
      GDK_NOTE (EVENTS,
		g_message ("delete window:\t\twindow: %ld",
			   xevent->xclient.window));

      event->any.type = GDK_DELETE;

      gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);

      return GDK_FILTER_TRANSLATE;
    }
  else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
    {
      GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);

      /* There is no way of knowing reliably whether we are viewable;
       * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
       */
1058
      if (toplevel && win->accept_focus)
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	_gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
				       RevertToParent,
				       xevent->xclient.data.l[1]);

      return GDK_FILTER_REMOVE;
    }
  else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
	   !_gdk_x11_display_is_root_window (display,
					     xevent->xclient.window))
    {
      XClientMessageEvent xclient = xevent->xclient;

      xclient.window = GDK_WINDOW_XROOTWIN (win);
      XSendEvent (GDK_WINDOW_XDISPLAY (win),
		  xclient.window,
		  False,
		  SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);

      return GDK_FILTER_REMOVE;
    }
  else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
1080
	   GDK_X11_DISPLAY (display)->use_sync)
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    {
      GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
      if (toplevel)
	{
#ifdef HAVE_XSYNC
	  XSyncIntsToValue (&toplevel->pending_counter_value,
			    xevent->xclient.data.l[2],
			    xevent->xclient.data.l[3]);
#endif
	}
      return GDK_FILTER_REMOVE;
    }

  return GDK_FILTER_CONTINUE;
}

static void
1098
gdk_event_init (GdkDisplay *display)
1099
{
1100
  GdkX11Display *display_x11;
1101 1102
  GdkDeviceManager *device_manager;

1103
  display_x11 = GDK_X11_DISPLAY (display);
1104
  display_x11->event_source = gdk_x11_event_source_new (display);
1105

1106 1107
  gdk_x11_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
                                       GDK_EVENT_TRANSLATOR (display));
1108 1109

  device_manager = gdk_display_get_device_manager (display);
1110 1111
  gdk_x11_event_source_add_translator ((GdkEventSource *) display_x11->event_source,
                                        GDK_EVENT_TRANSLATOR (device_manager));
1112 1113

  gdk_display_add_client_message_filter (display,
1114 1115 1116
                                         gdk_atom_intern_static_string ("WM_PROTOCOLS"),
                                         gdk_wm_protocols_filter,
                                         NULL);
1117 1118 1119
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1120
gdk_x11_display_init_input (GdkDisplay *display)
1121
{
1122
  GdkX11Display *display_x11;
1123 1124 1125 1126
  GdkDeviceManager *device_manager;
  GdkDevice *device;
  GList *list, *l;

1127
  display_x11 = GDK_X11_DISPLAY (display);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  device_manager = gdk_display_get_device_manager (display);

  /* For backwards compatibility, just add
   * floating devices that are not keyboards.
   */
  list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);

  for (l = list; l; l = l->next)
    {
      device = l->data;

1139
      if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1140 1141
        continue;

1142 1143
      display_x11->input_devices = g_list_prepend (display_x11->input_devices,
                                                   g_object_ref (l->data));
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    }

  g_list_free (list);

  /* Now set "core" pointer to the first
   * master device that is a pointer.
   */
  list = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);

  for (l = list; l; l = l->next)
    {
1155
      device = l->data;
1156

1157
      if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
1158 1159 1160 1161 1162 1163 1164
        continue;

      display->core_pointer = device;
      break;
    }

  /* Add the core pointer to the devices list */
1165 1166
  display_x11->input_devices = g_list_prepend (display_x11->input_devices,
                                               g_object_ref (display->core_pointer));
1167 1168 1169 1170

  g_list_free (list);
}

1171 1172 1173 1174
static void
set_sm_client_id (GdkDisplay  *display,
                  const gchar *sm_client_id)
{
1175
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

  if (gdk_display_is_closed (display))
    return;

  if (sm_client_id && strcmp (sm_client_id, ""))
    XChangeProperty (display_x11->xdisplay, display_x11->leader_window,
                     gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"),
                     XA_STRING, 8, PropModeReplace, (guchar *)sm_client_id,
                     strlen (sm_client_id));
  else
    XDeleteProperty (display_x11->xdisplay, display_x11->leader_window,
                     gdk_x11_get_xatom_by_name_for_display (display, "SM_CLIENT_ID"));
}

1190
GdkDisplay *
1191
_gdk_x11_display_open (const gchar *display_name)
1192
{
1193
  Display *xdisplay;
1194
  GdkDisplay *display;
1195
  GdkX11Display *display_x11;
1196
  GdkWindowAttr attr;
1197
  gint argc;
1198
  gchar *argv[1];
1199

1200 1201 1202
  XClassHint *class_hint;
  gulong pid;
  gint i;
1203
  gint ignore;
1204
  gint maj, min;
1205 1206 1207 1208

  xdisplay = XOpenDisplay (display_name);
  if (!xdisplay)
    return NULL;
1209
  
1210 1211
  display = g_object_new (GDK_TYPE_X11_DISPLAY, NULL);
  display_x11 = GDK_X11_DISPLAY (display);
1212

1213
  display_x11->xdisplay = xdisplay;
1214

1215
#ifdef HAVE_X11R6  
1216 1217
  /* Set up handlers for Xlib internal connections */
  XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
1218
#endif /* HAVE_X11R6 */
1219
  
1220 1221
  _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));

Søren Sandmann's avatar
Søren Sandmann committed
1222
  /* RandR must be initialized before we initialize the screens */
1223
  display_x11->have_randr12 = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
1224
  display_x11->have_randr13 = FALSE;
Søren Sandmann's avatar
Søren Sandmann committed
1225 1226 1227 1228 1229
#ifdef HAVE_RANDR
  if (XRRQueryExtension (display_x11->xdisplay,
			 &display_x11->xrandr_event_base, &ignore))
  {
      int major, minor;
1230

Søren Sandmann's avatar
Søren Sandmann committed
1231 1232
      XRRQueryVersion (display_x11->xdisplay, &major, &minor);

1233 1234 1235 1236 1237
      if ((major == 1 && minor >= 2) || major > 1) {
	  display_x11->have_randr12 = TRUE;
	  if (minor >= 3 || major > 1)
	      display_x11->have_randr13 = TRUE;
      }
Matthias Clasen's avatar
Matthias Clasen committed
1238 1239

       gdk_x11_register_standard_event_type (display, display_x11->xrandr_event_base, RRNumberEvents);
Søren Sandmann's avatar
Søren Sandmann committed
1240 1241
  }
#endif
1242

1243 1244
  /* initialize the display's screens */ 
  display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
1245
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1246
    display_x11->screens[i] = _gdk_x11_screen_new (display, i);
1247

1248 1249 1250 1251
  /* We need to initialize events after we have the screen
   * structures in places
   */
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1252
    _gdk_x11_screen_init_events (display_x11->screens[i]);
1253

1254 1255
  /*set the default screen */
  display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
1256

1257
  display->device_manager = _gdk_x11_device_manager_new (display);
1258

1259
  gdk_event_init (display);
1260

1261 1262 1263 1264 1265 1266 1267 1268
  attr.window_type = GDK_WINDOW_TOPLEVEL;
  attr.wclass = GDK_INPUT_OUTPUT;
  attr.x = 10;
  attr.y = 10;
  attr.width = 10;
  attr.height = 10;
  attr.event_mask = 0;

1269
  display_x11->leader_gdk_window = gdk_window_new (GDK_X11_SCREEN (display_x11->default_screen)->root_window, 
1270
						   &attr, GDK_WA_X | GDK_WA_Y);
1271 1272
  (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;

1273 1274
  display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);

1275
  display_x11->leader_window_title_set = FALSE;
1276

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
#ifdef HAVE_XFIXES
  if (XFixesQueryExtension (display_x11->xdisplay, 
			    &display_x11->xfixes_event_base, 
			    &ignore))
    {
      display_x11->have_xfixes = TRUE;

      gdk_x11_register_standard_event_type (display,
					    display_x11->xfixes_event_base, 
					    XFixesNumberEvents);
    }
  else
#endif
1290 1291
    display_x11->have_xfixes = FALSE;

1292 1293 1294
#ifdef HAVE_XCOMPOSITE
  if (XCompositeQueryExtension (display_x11->xdisplay,
				&ignore, &ignore))
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    {
      int major, minor;

      XCompositeQueryVersion (display_x11->xdisplay, &major, &minor);

      /* Prior to Composite version 0.4, composited windows clipped their
       * parents, so you had to use IncludeInferiors to draw to the parent
       * This isn't useful for our purposes, so require 0.4
       */
      display_x11->have_xcomposite = major > 0 || (major == 0 && minor >= 4);
    }
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  else
#endif
    display_x11->have_xcomposite = FALSE;

#ifdef HAVE_XDAMAGE
  if (XDamageQueryExtension (display_x11->xdisplay,
			     &display_x11->xdamage_event_base,
			     &ignore))
    {
      display_x11->have_xdamage = TRUE;

      gdk_x11_register_standard_event_type (display,
					    display_x11->xdamage_event_base,
					    XDamageNumberEvents);
    }
  else
#endif
    display_x11->have_xdamage = FALSE;

1325 1326
  display_x11->have_shapes = FALSE;
  display_x11->have_input_shapes = FALSE;
1327

1328
  if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &display_x11->shape_event_base, &ignore))
1329 1330
    {
      display_x11->have_shapes = TRUE;
1331
#ifdef ShapeInput
1332 1333 1334 1335
      if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
	display_x11->have_input_shapes = (maj == 1 && min >= 1);
#endif
    }
1336

1337 1338 1339 1340 1341 1342
  display_x11->trusted_client = TRUE;
  {
    Window root, child;
    int rootx, rooty, winx, winy;
    unsigned int xmask;

1343 1344
    gdk_x11_display_error_trap_push (display);
    XQueryPointer (display_x11->xdisplay,
1345
		   GDK_X11_SCREEN (display_x11->default_screen)->xroot_window,
1346
		   &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
1347
    if (G_UNLIKELY (gdk_x11_display_error_trap_pop (display) == BadWindow))
1348 1349 1350 1351 1352 1353
      {
	g_warning ("Connection to display %s appears to be untrusted. Pointer and keyboard grabs and inter-client communication may not work as expected.", gdk_display_get_name (display));
	display_x11->trusted_client = FALSE;
      }
  }

1354
  if (g_getenv ("GDK_SYNCHRONIZE"))
1355
    XSynchronize (display_x11->xdisplay, True);
1356

1357 1358
  class_hint = XAllocClassHint();
  class_hint->res_name = g_get_prgname ();
1359

1360
  class_hint->res_class = (char *)gdk_get_program_class ();
1361 1362 1363 1364 1365 1366

  /* XmbSetWMProperties sets the RESOURCE_NAME environment variable
   * from argv[0], so we just synthesize an argument array here.
   */
  argc = 1;
  argv[0] = g_get_prgname ();
1367

1368 1369 1370 1371 1372 1373
  XmbSetWMProperties (display_x11->xdisplay,
		      display_x11->leader_window,
		      NULL, NULL, argv, argc, NULL, NULL,
		      class_hint);
  XFree (class_hint);

1374 1375
  if (gdk_sm_client_id)
    set_sm_client_id (display, gdk_sm_client_id);
1376

1377 1378 1379 1380 1381
  pid = getpid ();
  XChangeProperty (display_x11->xdisplay,
		   display_x11->leader_window,
		   gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PID"),
		   XA_CARDINAL, 32, PropModeReplace, (guchar *) & pid, 1);
1382 1383 1384

  /* We don't yet know a valid time. */
  display_x11->user_time = 0;
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
  
#ifdef HAVE_XKB
  {
    gint xkb_major = XkbMajorVersion;
    gint xkb_minor = XkbMinorVersion;
    if (XkbLibraryVersion (&xkb_major, &xkb_minor))
      {
        xkb_major = XkbMajorVersion;
        xkb_minor = XkbMinorVersion;
	    
        if (XkbQueryExtension (display_x11->xdisplay, 
			       NULL, &display_x11->xkb_event_type, NULL,
                               &xkb_major, &xkb_minor))
          {
	    Bool detectable_autorepeat_supported;
	    
	    display_x11->use_xkb = TRUE;

            XkbSelectEvents (display_x11->xdisplay,
                             XkbUseCoreKbd,
1405 1406
                             XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
                             XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
1407

1408
	    /* keep this in sync with _gdk_x11_keymap_state_changed() */
1409 1410
	    XkbSelectEventDetails (display_x11->xdisplay,
				   XkbUseCoreKbd, XkbStateNotify,
Matthias Clasen's avatar
Matthias Clasen committed
1411
				   XkbAllStateComponentsMask,
1412
                                   XkbGroupLockMask|XkbModifierLockMask);
1413

1414 1415 1416 1417 1418
	    XkbSetDetectableAutoRepeat (display_x11->xdisplay,
					True,
					&detectable_autorepeat_supported);

	    GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
1419
				       detectable_autorepeat_supported ?
1420 1421 1422 1423 1424 1425 1426 1427
				       "supported" : "not supported"));
	    
	    display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
          }
      }
  }
#endif

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
  display_x11->use_sync = FALSE;
#ifdef HAVE_XSYNC
  {
    int major, minor;
    int error_base, event_base;
    
    if (XSyncQueryExtension (display_x11->xdisplay,
			     &event_base, &error_base) &&
        XSyncInitialize (display_x11->xdisplay,
                         &major, &minor))
      display_x11->use_sync = TRUE;
  }
#endif
1441

Matthias Clasen's avatar
Matthias Clasen committed
1442 1443
  gdk_x11_display_init_input (display);
  _gdk_x11_display_init_dnd (display);
1444

1445
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1446
    _gdk_x11_screen_setup (display_x11->screens[i]);
1447

1448
  g_signal_emit_by_name (display, "opened");
1449
  g_signal_emit_by_name (gdk_display_manager_get (), "display-opened", display);
Owen Taylor's avatar
Owen Taylor committed
1450

1451 1452 1453
  return display;
}

1454
#ifdef HAVE_X11R6
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
/*
 * XLib internal connection handling
 */
typedef struct _GdkInternalConnection GdkInternalConnection;

struct _GdkInternalConnection
{
  gint	         fd;
  GSource	*source;
  Display	*display;
};

static gboolean
process_internal_connection (GIOChannel  *gioc,
			     GIOCondition cond,
			     gpointer     data)
{
  GdkInternalConnection *connection = (GdkInternalConnection *)data;

  GDK_THREADS_ENTER ();

  XProcessInternalConnection ((Display*)connection->display, connection->fd);

  GDK_THREADS_LEAVE ();

  return TRUE;
}

1483 1484
static gulong
gdk_x11_display_get_next_serial (GdkDisplay *display)
1485 1486 1487 1488 1489
{
  return NextRequest (GDK_DISPLAY_XDISPLAY (display));
}


1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
static GdkInternalConnection *
gdk_add_connection_handler (Display *display,
			    guint    fd)
{
  GIOChannel *io_channel;
  GdkInternalConnection *connection;

  connection = g_new (GdkInternalConnection, 1);

  connection->fd = fd;
  connection->display = display;
  
  io_channel = g_io_channel_unix_new (fd);
  
  connection->source = g_io_create_watch (io_channel, G_IO_IN);
  g_source_set_callback (connection->source,
			 (GSourceFunc)process_internal_connection, connection, NULL);
  g_source_attach (connection->source, NULL);
  
  g_io_channel_unref (io_channel);
  
  return connection;
}

static void
gdk_remove_connection_handler (GdkInternalConnection *connection)
{
  g_source_destroy (connection->source);
  g_free (connection);
}

static void
gdk_internal_connection_watch (Display  *display,
			       XPointer  arg,
			       gint      fd,
			       gboolean  opening,
			       XPointer *watch_data)
{
  if (opening)
    *watch_data = (XPointer)gdk_add_connection_handler (display, fd);
  else
1531
    gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
1532
}
1533
#endif /* HAVE_X11R6 */
1534

1535 1536
static G_CONST_RETURN gchar *
gdk_x11_display_get_name (GdkDisplay *display)
1537
{
1538
  return (gchar *) DisplayString (GDK_X11_DISPLAY (display)->xdisplay);
1539 1540
}

1541 1542
static gint
gdk_x11_display_get_n_screens (GdkDisplay *display)
1543
{
1544
  return ScreenCount (GDK_X11_DISPLAY (display)->xdisplay);
1545 1546
}

1547 1548 1549
static GdkScreen *
gdk_x11_display_get_screen (GdkDisplay *display,
			    gint        screen_num)
1550
{
1551
  g_return_val_if_fail (ScreenCount (GDK_X11_DISPLAY (display)->xdisplay) > screen_num, NULL);
1552

1553
  return GDK_X11_DISPLAY (display)->screens[screen_num];
1554 1555
}

1556 1557
static GdkScreen *
gdk_x11_display_get_default_screen (GdkDisplay *display)
1558
{
1559
  return GDK_X11_DISPLAY (display)->default_screen;
1560 1561 1562 1563 1564 1565
}

gboolean
_gdk_x11_display_is_root_window (GdkDisplay *display,
				 Window      xroot_window)
{
1566
  GdkX11Display *display_x11;
1567
  gint i;
1568

1569
  display_x11 = GDK_X11_DISPLAY (display);
1570

1571
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1572
    {
1573 1574
      if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
	return TRUE;
1575 1576 1577 1578
    }
  return FALSE;
}

1579 1580 1581 1582 1583
struct XPointerUngrabInfo {
  GdkDisplay *display;
  guint32 time;
};

1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
static void
device_grab_update_callback (GdkDisplay *display,
                             gpointer    data,
                             gulong      serial)
{
  GdkDevice *device = data;

  _gdk_display_device_grab_update (display, device, NULL, serial);
}

1594 1595 1596 1597 1598
#define XSERVER_TIME_IS_LATER(time1, time2)                        \
  ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
    (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
  )

1599
void
1600 1601 1602
_gdk_x11_display_update_grab_info (GdkDisplay *display,
                                   GdkDevice  *device,
                                   gint        status)
1603
{
1604
  if (status == GrabSuccess)
1605
    _gdk_x11_roundtrip_async (display, device_grab_update_callback, device);
1606
}
1607

1608 1609 1610 1611 1612 1613 1614
void
_gdk_x11_display_update_grab_info_ungrab (GdkDisplay *display,
                                          GdkDevice  *device,
                                          guint32     time,
                                          gulong      serial)
{
  GdkDeviceGrabInfo *grab;
1615

1616
  XFlush (GDK_DISPLAY_XDISPLAY (display));
1617

1618
  grab = _gdk_display_get_last_device_grab (display, device);
1619
  if (grab &&
1620
      (time == GDK_CURRENT_TIME ||
1621
       grab->time == GDK_CURRENT_TIME ||
1622
       !XSERVER_TIME_IS_LATER (grab->time, time)))
1623
    {
1624
      grab->serial_end = serial;
1625
      _gdk_x11_roundtrip_async (display, device_grab_update_callback, device);
1626
    }
1627 1628
}

1629 1630
static void
gdk_x11_display_beep (GdkDisplay *display)
1631
{
1632
#ifdef HAVE_XKB
Matthias Clasen's avatar
Matthias Clasen committed
1633
  XkbBell (GDK_DISPLAY_XDISPLAY (display), None, 0, None);
1634 1635 1636
#else
  XBell (GDK_DISPLAY_XDISPLAY (display), 0);
#endif
1637 1638
}

1639 1640
static void
gdk_x11_display_sync (GdkDisplay *display)
1641 1642 1643 1644
{
  XSync (GDK_DISPLAY_XDISPLAY (display), False);
}

1645 1646
static void
gdk_x11_display_flush (GdkDisplay *display)
1647 1648 1649 1650 1651
{
  if (!display->closed)
    XFlush (GDK_DISPLAY_XDISPLAY (display));
}

1652 1653 1654 1655 1656 1657
static gboolean
gdk_x11_display_has_pending (GdkDisplay *display)
{
  return XPending (GDK_DISPLAY_XDISPLAY (display));
}

1658 1659
static GdkWindow *
gdk_x11_display_get_default_group (GdkDisplay *display)
1660 1661 1662
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

1663
  return GDK_X11_DISPLAY (display)->leader_gdk_window;
1664 1665
}

Matthias Clasen's avatar
Matthias Clasen committed
1666 1667 1668 1669 1670 1671 1672 1673
/**
 * gdk_x11_display_grab:
 * @display: a #GdkDisplay 
 * 
 * Call XGrabServer() on @display. 
 * To ungrab the display again, use gdk_x11_display_ungrab(). 
 *
 * gdk_x11_display_grab()/gdk_x11_display_ungrab() calls can be nested.
1674 1675
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
1676
 **/
1677
void
1678
gdk_x11_display_grab (GdkDisplay *display)
1679
{
1680
  GdkX11Display *display_x11;
1681 1682 1683
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
1684
  display_x11 = GDK_X11_DISPLAY (display);
1685 1686 1687 1688 1689 1690
  
  if (display_x11->grab_count == 0)
    XGrabServer (display_x11->xdisplay);
  display_x11->grab_count++;
}

Matthias Clasen's avatar
Matthias Clasen committed
1691 1692 1693 1694 1695 1696
/**
 * gdk_x11_display_ungrab:
 * @display: a #GdkDisplay
 * 
 * Ungrab @display after it has been grabbed with 
 * gdk_x11_display_grab(). 
1697 1698
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
1699
 **/
1700
void
1701
gdk_x11_display_ungrab (GdkDisplay *display)
1702
{
1703
  GdkX11Display *display_x11;
1704 1705 1706
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
1707
  display_x11 = GDK_X11_DISPLAY (display);;
1708 1709 1710 1711
  g_return_if_fail (display_x11->grab_count > 0);
  
  display_x11->grab_count--;
  if (display_x11->grab_count == 0)
1712 1713 1714 1715
    {
      XUngrabServer (display_x11->xdisplay);
      XFlush (display_x11->xdisplay);
    }
1716 1717
}

1718
static void
1719
gdk_x11_display_dispose (GObject *object)
1720
{
1721
  GdkDisplay *display = GDK_DISPLAY (object);
1722
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (object);
1723
  gint           i;
1724

1725 1726
  _gdk_x11_display_manager_remove_display (gdk_display_manager_get (), display);

1727 1728
  g_list_foreach (display_x11->input_devices, (GFunc) g_object_run_dispose, NULL);

1729
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
1730 1731
    _gdk_screen_close (display_x11->screens[i]);

1732 1733 1734 1735 1736 1737
  if (display_x11->event_source)
    {
      g_source_destroy (display_x11->event_source);
      g_source_unref (display_x11->event_source);
      display_x11->event_source = NULL;
    }
1738

1739
  G_OBJECT_CLASS (gdk_x11_display_parent_class)->dispose (object);
1740 1741
}

1742
static void
1743
gdk_x11_display_finalize (GObject *object)
1744
{
1745
  GdkX11Display *display_x11 = GDK_X11_DISPLAY (object);
1746 1747 1748 1749 1750 1751
  gint           i;

  /* Keymap */
  if (display_x11->keymap)
    g_object_unref (display_x11->keymap);

1752 1753 1754 1755 1756 1757 1758 1759
  /* Free motif Dnd */
  if (display_x11->motif_target_lists)
    {
      for (i = 0; i < display_x11->motif_n_target_lists; i++)
        g_list_free (display_x11->motif_target_lists[i]);
      g_free (display_x11->motif_target_lists);
    }

1760
  _gdk_x11_cursor_display_finalize (GDK_DISPLAY (display_x11));
1761

1762 1763 1764
  /* Atom Hashtable */
  g_hash_table_destroy (display_x11->atom_from_virtual);
  g_hash_table_destroy (display_x11->atom_to_virtual);
1765

1766 1767
  /* Leader Window */
  XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);