gdkdisplay-x11.c 32.3 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 25 26
 *
 * 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.
 */

#include <config.h>

27 28
#include <stdlib.h>
#include <string.h>
29
#include <errno.h>
30 31 32 33 34 35 36 37 38 39 40
#include <unistd.h>

#include <glib.h>
#include "gdkx.h"
#include "gdkdisplay.h"
#include "gdkdisplay-x11.h"
#include "gdkscreen.h"
#include "gdkscreen-x11.h"
#include "gdkinternals.h"
#include "gdkinputprivate.h"
#include "xsettings-client.h"
41
#include "gdkalias.h"
42 43 44 45 46 47 48

#include <X11/Xatom.h>

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

Matthias Clasen's avatar
Matthias Clasen committed
49 50 51 52
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

53 54 55 56 57
#ifdef HAVE_SHAPE_EXT
#include <X11/extensions/shape.h>
#endif


58 59
static void   gdk_display_x11_dispose            (GObject            *object);
static void   gdk_display_x11_finalize           (GObject            *object);
60

61
#ifdef HAVE_X11R6
62 63 64 65 66
static void gdk_internal_connection_watch (Display  *display,
					   XPointer  arg,
					   gint      fd,
					   gboolean  opening,
					   XPointer *watch_data);
67
#endif /* HAVE_X11R6 */
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* 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",
  "WM_LOCALE_NAME",
  "WM_PROTOCOLS",
  "WM_TAKE_FOCUS",
  "_NET_WM_DESKTOP",
  "_NET_WM_ICON",
  "_NET_WM_ICON_NAME",
  "_NET_WM_NAME",
  "_NET_WM_PID",
  "_NET_WM_PING",
  "_NET_WM_STATE",
  "_NET_WM_STATE_STICKY",
  "_NET_WM_STATE_MAXIMIZED_VERT",
  "_NET_WM_STATE_MAXIMIZED_HORZ",
  "_NET_WM_STATE_FULLSCREEN",
91 92
  "_NET_WM_SYNC_REQUEST",
  "_NET_WM_SYNC_REQUEST_COUNTER",
93 94
  "_NET_WM_WINDOW_TYPE",
  "_NET_WM_WINDOW_TYPE_NORMAL",
95
  "_NET_WM_USER_TIME",
96
  "_NET_VIRTUAL_ROOTS"
97 98
};

Matthias Clasen's avatar
Matthias Clasen committed
99
G_DEFINE_TYPE (GdkDisplayX11, _gdk_display_x11, GDK_TYPE_DISPLAY);
100 101

static void
Matthias Clasen's avatar
Matthias Clasen committed
102
_gdk_display_x11_class_init (GdkDisplayX11Class * class)
103
{
104 105 106 107
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  
  object_class->dispose = gdk_display_x11_dispose;
  object_class->finalize = gdk_display_x11_finalize;
108 109
}

Matthias Clasen's avatar
Matthias Clasen committed
110 111 112 113
static void
_gdk_display_x11_init (GdkDisplayX11 *display)
{
}
114

Matthias Clasen's avatar
Matthias Clasen committed
115
/**
Owen Taylor's avatar
Owen Taylor committed
116
 * gdk_display_open:
Matthias Clasen's avatar
Matthias Clasen committed
117 118 119 120 121
 * @display_name: the name of the display to open
 * @returns: a #GdkDisplay, or %NULL if the display
 *  could not be opened.
 *
 * Opens a display.
Matthias Clasen's avatar
Matthias Clasen committed
122 123
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
124
 */
125
GdkDisplay *
Owen Taylor's avatar
Owen Taylor committed
126
gdk_display_open (const gchar *display_name)
127
{
128
  Display *xdisplay;
129 130
  GdkDisplay *display;
  GdkDisplayX11 *display_x11;
131
  GdkWindowAttr attr;
132
  gint argc;
133
  gchar *argv[1];
134
  const char *sm_client_id;
135 136 137 138
  
  XClassHint *class_hint;
  gulong pid;
  gint i;
139
#if defined(HAVE_XFIXES) || defined(HAVE_SHAPE_EXT)
Matthias Clasen's avatar
Matthias Clasen committed
140
  gint ignore;
141
  gint maj, min;
142
#endif
143 144 145 146

  xdisplay = XOpenDisplay (display_name);
  if (!xdisplay)
    return NULL;
147 148 149 150
  
  display = g_object_new (GDK_TYPE_DISPLAY_X11, NULL);
  display_x11 = GDK_DISPLAY_X11 (display);

151
  display_x11->use_xshm = TRUE;
152
  display_x11->xdisplay = xdisplay;
153

154
#ifdef HAVE_X11R6  
155 156
  /* Set up handlers for Xlib internal connections */
  XAddConnectionWatch (xdisplay, gdk_internal_connection_watch, NULL);
157
#endif /* HAVE_X11R6 */
158
  
159 160
  /* initialize the display's screens */ 
  display_x11->screens = g_new (GdkScreen *, ScreenCount (display_x11->xdisplay));
161
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
162
    display_x11->screens[i] = _gdk_x11_screen_new (display, i);
163

164 165 166 167 168 169
  /* We need to initialize events after we have the screen
   * structures in places
   */
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
    _gdk_x11_events_init_screen (display_x11->screens[i]);
  
170 171
  /*set the default screen */
  display_x11->default_screen = display_x11->screens[DefaultScreen (display_x11->xdisplay)];
172 173 174 175 176 177 178 179 180

  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;

181 182
  _gdk_x11_precache_atoms (display, precache_atoms, G_N_ELEMENTS (precache_atoms));

183 184
  display_x11->leader_gdk_window = gdk_window_new (GDK_SCREEN_X11 (display_x11->default_screen)->root_window, 
						   &attr, GDK_WA_X | GDK_WA_Y);
185 186
  (_gdk_x11_window_get_toplevel (display_x11->leader_gdk_window))->is_leader = TRUE;

187 188
  display_x11->leader_window = GDK_WINDOW_XID (display_x11->leader_gdk_window);

189
  display_x11->leader_window_title_set = FALSE;
190

191
  display_x11->have_render = GDK_UNKNOWN;
192

Matthias Clasen's avatar
Matthias Clasen committed
193 194 195 196 197 198 199 200 201 202 203 204 205
#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
206 207 208 209 210 211 212 213 214 215 216 217 218 219
    display_x11->have_xfixes = FALSE;

  display_x11->have_shapes = FALSE;
  display_x11->have_input_shapes = FALSE;
#ifdef HAVE_SHAPE_EXT
  if (XShapeQueryExtension (GDK_DISPLAY_XDISPLAY (display), &ignore, &ignore))
    {
      display_x11->have_shapes = TRUE;
#ifdef ShapeInput	      
      if (XShapeQueryVersion (GDK_DISPLAY_XDISPLAY (display), &maj, &min))
	display_x11->have_input_shapes = (maj == 1 && min >= 1);
#endif
    }
#endif
Matthias Clasen's avatar
Matthias Clasen committed
220

221 222 223 224 225 226 227
  if (_gdk_synchronize)
    XSynchronize (display_x11->xdisplay, True);
  
  class_hint = XAllocClassHint();
  class_hint->res_name = g_get_prgname ();
  
  class_hint->res_class = (char *)gdk_get_program_class ();
228 229 230 231 232 233 234

  /* 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 ();
  
235 236 237 238 239 240
  XmbSetWMProperties (display_x11->xdisplay,
		      display_x11->leader_window,
		      NULL, NULL, argv, argc, NULL, NULL,
		      class_hint);
  XFree (class_hint);

241 242 243 244
  sm_client_id = _gdk_get_sm_client_id ();
  if (sm_client_id)
    _gdk_windowing_display_set_sm_client_id (display, sm_client_id);

245 246 247 248 249
  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);
250 251 252

  /* We don't yet know a valid time. */
  display_x11->user_time = 0;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  
#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,
273 274
                             XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask,
                             XkbNewKeyboardNotifyMask | XkbMapNotifyMask | XkbStateNotifyMask);
275

Matthias Clasen's avatar
Matthias Clasen committed
276 277 278 279 280
	    /* keep this in sync with _gdk_keymap_state_changed() */ 
	    XkbSelectEventDetails (display_x11->xdisplay,
				   XkbUseCoreKbd, XkbStateNotify,
				   XkbGroupLockMask, XkbGroupLockMask);

281 282 283 284 285 286 287 288 289 290 291 292 293 294
	    XkbSetDetectableAutoRepeat (display_x11->xdisplay,
					True,
					&detectable_autorepeat_supported);

	    GDK_NOTE (MISC, g_message ("Detectable autorepeat %s.",
				       detectable_autorepeat_supported ? 
				       "supported" : "not supported"));
	    
	    display_x11->have_xkb_autorepeat = detectable_autorepeat_supported;
          }
      }
  }
#endif

295 296 297 298 299 300 301 302 303 304 305 306 307 308
  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
  
309 310 311 312 313
  _gdk_windowing_image_init (display);
  _gdk_events_init (display);
  _gdk_input_init (display);
  _gdk_dnd_init (display);

314 315
  g_signal_emit_by_name (gdk_display_manager_get(),
			 "display_opened", display);
Owen Taylor's avatar
Owen Taylor committed
316

317 318 319
  return display;
}

320
#ifdef HAVE_X11R6
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
/*
 * 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;
}

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
390
    gdk_remove_connection_handler ((GdkInternalConnection *)*watch_data);
391
}
392
#endif /* HAVE_X11R6 */
393

394 395 396 397 398 399
/**
 * gdk_display_get_name:
 * @display: a #GdkDisplay
 *
 * Gets the name of the display.
 * 
Soren Sandmann's avatar
Soren Sandmann committed
400 401
 * Returns: a string representing the display name. This string is owned
 * by GDK and should not be modified or freed.
Matthias Clasen's avatar
Matthias Clasen committed
402 403
 * 
 * Since: 2.2
404 405 406
 */
G_CONST_RETURN gchar *
gdk_display_get_name (GdkDisplay * display)
407
{
408
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
409
  
410
  return (gchar *) DisplayString (GDK_DISPLAY_X11 (display)->xdisplay);
411 412
}

413 414 415 416 417 418 419
/**
 * gdk_display_get_n_screens:
 * @display: a #GdkDisplay
 *
 * Gets the number of screen managed by the @display.
 * 
 * Returns: number of screens.
Matthias Clasen's avatar
Matthias Clasen committed
420 421
 * 
 * Since: 2.2
422 423 424
 */
gint
gdk_display_get_n_screens (GdkDisplay * display)
425
{
426
  g_return_val_if_fail (GDK_IS_DISPLAY (display), 0);
427
  
428
  return ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay);
429 430
}

431 432 433 434 435 436
/**
 * gdk_display_get_screen:
 * @display: a #GdkDisplay
 * @screen_num: the screen number
 *
 * Returns a screen object for one of the screens of the display.
Matthias Clasen's avatar
Matthias Clasen committed
437
 *
438
 * Returns: the #GdkScreen object
Matthias Clasen's avatar
Matthias Clasen committed
439 440
 *
 * Since: 2.2
441 442 443
 */
GdkScreen *
gdk_display_get_screen (GdkDisplay * display, gint screen_num)
444
{
445 446
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
  g_return_val_if_fail (ScreenCount (GDK_DISPLAY_X11 (display)->xdisplay) > screen_num, NULL);
447
  
448
  return GDK_DISPLAY_X11 (display)->screens[screen_num];
449 450
}

451 452 453 454 455 456 457
/**
 * gdk_display_get_default_screen:
 * @display: a #GdkDisplay
 *
 * Get the default #GdkScreen for @display.
 * 
 * Returns: the default #GdkScreen object for @display
Matthias Clasen's avatar
Matthias Clasen committed
458 459
 *
 * Since: 2.2
460 461 462
 */
GdkScreen *
gdk_display_get_default_screen (GdkDisplay * display)
463
{
464
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
465
  
466
  return GDK_DISPLAY_X11 (display)->default_screen;
467 468 469 470 471 472 473
}

gboolean
_gdk_x11_display_is_root_window (GdkDisplay *display,
				 Window      xroot_window)
{
  GdkDisplayX11 *display_x11;
474
  gint i;
475 476 477 478 479
  
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
  
  display_x11 = GDK_DISPLAY_X11 (display);
  
480
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
481
    {
482 483
      if (GDK_SCREEN_XROOTWIN (display_x11->screens[i]) == xroot_window)
	return TRUE;
484 485 486 487
    }
  return FALSE;
}

Matthias Clasen's avatar
Matthias Clasen committed
488 489 490 491 492
#define XSERVER_TIME_IS_LATER(time1, time2)                        \
  ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
    (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
  )

493 494
/**
 * gdk_display_pointer_ungrab:
Matthias Clasen's avatar
Matthias Clasen committed
495
 * @display: a #GdkDisplay.
496
 * @time_: a timestap (e.g. GDK_CURRENT_TIME).
497 498
 *
 * Release any pointer grab.
Matthias Clasen's avatar
Matthias Clasen committed
499 500
 *
 * Since: 2.2
501 502 503 504 505
 */
void
gdk_display_pointer_ungrab (GdkDisplay *display,
			    guint32     time)
{
506
  Display *xdisplay;
Matthias Clasen's avatar
Matthias Clasen committed
507 508
  GdkDisplayX11 *display_x11;

509
  g_return_if_fail (GDK_IS_DISPLAY (display));
510

Matthias Clasen's avatar
Matthias Clasen committed
511
  display_x11 = GDK_DISPLAY_X11 (display);
512
  xdisplay = GDK_DISPLAY_XDISPLAY (display);
513 514
  
  _gdk_input_ungrab_pointer (display, time);
515 516
  XUngrabPointer (xdisplay, time);
  XFlush (xdisplay);
Matthias Clasen's avatar
Matthias Clasen committed
517 518 519 520 521

  if (time == GDK_CURRENT_TIME || 
      display_x11->pointer_xgrab_time == GDK_CURRENT_TIME ||
      !XSERVER_TIME_IS_LATER (display_x11->pointer_xgrab_time, time))
    display_x11->pointer_xgrab_window = NULL;
522 523 524
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
525 526
 * gdk_display_pointer_is_grabbed:
 * @display: a #GdkDisplay
527 528 529
 *
 * Test if the pointer is grabbed.
 *
Matthias Clasen's avatar
Matthias Clasen committed
530 531 532
 * Returns: %TRUE if an active X pointer grab is in effect
 *
 * Since: 2.2
533 534 535 536 537 538
 */
gboolean
gdk_display_pointer_is_grabbed (GdkDisplay * display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
  
Matthias Clasen's avatar
Matthias Clasen committed
539 540
  return (GDK_DISPLAY_X11 (display)->pointer_xgrab_window != NULL &&
	  !GDK_DISPLAY_X11 (display)->pointer_xgrab_implicit);
541 542 543 544
}

/**
 * gdk_display_keyboard_ungrab:
Matthias Clasen's avatar
Matthias Clasen committed
545 546
 * @display: a #GdkDisplay.
 * @time_: a timestap (e.g #GDK_CURRENT_TIME).
547 548
 *
 * Release any keyboard grab
Matthias Clasen's avatar
Matthias Clasen committed
549 550
 *
 * Since: 2.2
551 552 553 554 555
 */
void
gdk_display_keyboard_ungrab (GdkDisplay *display,
			     guint32     time)
{
556
  Display *xdisplay;
Matthias Clasen's avatar
Matthias Clasen committed
557
  GdkDisplayX11 *display_x11;
558
  
559
  g_return_if_fail (GDK_IS_DISPLAY (display));
560

Matthias Clasen's avatar
Matthias Clasen committed
561
  display_x11 = GDK_DISPLAY_X11 (display);
562 563 564 565
  xdisplay = GDK_DISPLAY_XDISPLAY (display);
  
  XUngrabKeyboard (xdisplay, time);
  XFlush (xdisplay);
566
  
Matthias Clasen's avatar
Matthias Clasen committed
567 568 569 570
  if (time == GDK_CURRENT_TIME || 
      display_x11->keyboard_xgrab_time == GDK_CURRENT_TIME ||
      !XSERVER_TIME_IS_LATER (display_x11->keyboard_xgrab_time, time))
    display_x11->keyboard_xgrab_window = NULL;
571 572 573 574 575 576 577 578
}

/**
 * gdk_display_beep:
 * @display: a #GdkDisplay
 *
 * Emits a short beep on @display
 *
Matthias Clasen's avatar
Matthias Clasen committed
579
 * Since: 2.2
580 581 582 583 584 585 586 587 588 589
 */
void
gdk_display_beep (GdkDisplay * display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  XBell (GDK_DISPLAY_XDISPLAY (display), 0);
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
590 591
 * gdk_display_sync:
 * @display: a #GdkDisplay
592 593 594 595 596
 *
 * Flushes any requests queued for the windowing system and waits until all
 * requests have been handled. This is often used for making sure that the
 * display is synchronized with the current state of the program. Calling
 * gdk_display_sync() before gdk_error_trap_pop() makes sure that any errors
Matthias Clasen's avatar
Matthias Clasen committed
597 598
 * generated from earlier requests are handled before the error trap is 
 * removed.
599
 *
Soren Sandmann's avatar
Soren Sandmann committed
600 601
 * This is most useful for X11. On windowing systems where requests are
 * handled synchronously, this function will do nothing.
Matthias Clasen's avatar
Matthias Clasen committed
602 603
 *
 * Since: 2.2
604 605 606 607 608 609 610 611 612
 */
void
gdk_display_sync (GdkDisplay * display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  XSync (GDK_DISPLAY_XDISPLAY (display), False);
}

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
/**
 * gdk_display_flush:
 * @display: a #GdkDisplay
 *
 * Flushes any requests queued for the windowing system; this happens automatically
 * when the main loop blocks waiting for new events, but if your application
 * is drawing without returning control to the main loop, you may need
 * to call this function explicitely. A common case where this function
 * needs to be called is when an application is executing drawing commands
 * from a thread other than the thread where the main loop is running.
 *
 * This is most useful for X11. On windowing systems where requests are
 * handled synchronously, this function will do nothing.
 *
 * Since: 2.4
 */
void 
gdk_display_flush (GdkDisplay *display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));

  if (!display->closed)
    XFlush (GDK_DISPLAY_XDISPLAY (display));
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
/**
 * gdk_display_get_default_group:
 * @display: a #GdkDisplay
 * 
 * Returns the default group leader window for all toplevel windows
 * on @display. This window is implicitly created by GDK. 
 * See gdk_window_set_group().
 * 
 * Return value: The default group leader window for @display
 *
 * Since: 2.4
 **/
GdkWindow *gdk_display_get_default_group (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

  return GDK_DISPLAY_X11 (display)->leader_gdk_window;
}

Matthias Clasen's avatar
Matthias Clasen committed
657 658 659 660 661 662 663 664
/**
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
665 666
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
667
 **/
668 669 670 671 672 673 674 675 676 677 678 679 680 681
void
gdk_x11_display_grab (GdkDisplay * display)
{
  GdkDisplayX11 *display_x11;
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  display_x11 = GDK_DISPLAY_X11 (display);
  
  if (display_x11->grab_count == 0)
    XGrabServer (display_x11->xdisplay);
  display_x11->grab_count++;
}

Matthias Clasen's avatar
Matthias Clasen committed
682 683 684 685 686 687
/**
 * gdk_x11_display_ungrab:
 * @display: a #GdkDisplay
 * 
 * Ungrab @display after it has been grabbed with 
 * gdk_x11_display_grab(). 
Matthias Clasen's avatar
Matthias Clasen committed
688 689
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
690
 **/
691 692 693 694 695 696 697 698 699 700 701 702
void
gdk_x11_display_ungrab (GdkDisplay * display)
{
  GdkDisplayX11 *display_x11;
  
  g_return_if_fail (GDK_IS_DISPLAY (display));
  
  display_x11 = GDK_DISPLAY_X11 (display);;
  g_return_if_fail (display_x11->grab_count > 0);
  
  display_x11->grab_count--;
  if (display_x11->grab_count == 0)
703 704 705 706
    {
      XUngrabServer (display_x11->xdisplay);
      XFlush (display_x11->xdisplay);
    }
707 708
}

709 710 711
static void
gdk_display_x11_dispose (GObject *object)
{
712 713 714
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
  gint           n_screens;
  gint           i;
715

716
  n_screens = ScreenCount (display_x11->xdisplay);
717

718 719 720
  for (i = 0; i < n_screens; i++)
    _gdk_screen_close (display_x11->screens[i]);

721
  _gdk_events_uninit (GDK_DISPLAY_OBJECT (object));
722

Matthias Clasen's avatar
Matthias Clasen committed
723
  G_OBJECT_CLASS (_gdk_display_x11_parent_class)->dispose (object);
724 725
}

726 727 728 729
static void
gdk_display_x11_finalize (GObject *object)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (object);
730 731 732 733 734 735 736
  gint           n_screens;
  gint           i;

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

737 738 739 740 741 742 743 744 745 746 747
  /* 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);
    }

  /* Atom Hashtable */
  g_hash_table_destroy (display_x11->atom_from_virtual);
  g_hash_table_destroy (display_x11->atom_to_virtual);
748

749 750
  /* Leader Window */
  XDestroyWindow (display_x11->xdisplay, display_x11->leader_window);
751

752
  /* list of filters for client messages */
753
  g_list_foreach (display_x11->client_filters, (GFunc) g_free, NULL);
754
  g_list_free (display_x11->client_filters);
755

756 757 758
  /* List of event window extraction functions */
  g_slist_foreach (display_x11->event_types, (GFunc)g_free, NULL);
  g_slist_free (display_x11->event_types);
759

760 761
  /* input GdkDevice list */
  /* FIXME need to write finalize fct */
762
  g_list_foreach (display_x11->input_devices, (GFunc) g_object_unref, NULL);
763
  g_list_free (display_x11->input_devices);
764

765
  /* input GdkWindow list */
766
  g_list_foreach (display_x11->input_windows, (GFunc) g_object_unref, NULL);
767
  g_list_free (display_x11->input_windows);
768

769
  /* Free all GdkScreens */
770
  n_screens = ScreenCount (display_x11->xdisplay);
771
  for (i = 0; i < ScreenCount (display_x11->xdisplay); i++)
772
    g_object_unref (display_x11->screens[i]);
773
  g_free (display_x11->screens);
774

775
  g_free (display_x11->startup_notification_id);
776

777 778 779
  /* X ID hashtable */
  g_hash_table_destroy (display_x11->xid_ht);

780 781
  XCloseDisplay (display_x11->xdisplay);

Matthias Clasen's avatar
Matthias Clasen committed
782
  G_OBJECT_CLASS (_gdk_display_x11_parent_class)->finalize (object);
783 784 785 786 787 788 789 790 791
}

/**
 * gdk_x11_lookup_xdisplay:
 * @xdisplay: a pointer to an X Display
 * 
 * Find the #GdkDisplay corresponding to @display, if any exists.
 * 
 * Return value: the #GdkDisplay, if found, otherwise %NULL.
Matthias Clasen's avatar
Matthias Clasen committed
792 793
 *
 * Since: 2.2
794 795 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
 **/
GdkDisplay *
gdk_x11_lookup_xdisplay (Display *xdisplay)
{
  GSList *tmp_list;

  for (tmp_list = _gdk_displays; tmp_list; tmp_list = tmp_list->next)
    {
      if (GDK_DISPLAY_XDISPLAY (tmp_list->data) == xdisplay)
	return tmp_list->data;
    }
  
  return NULL;
}

/**
 * _gdk_x11_display_screen_for_xrootwin:
 * @display: a #Display
 * @xrootwin: window ID for one of of the screen's of the display.
 * 
 * Given the root window ID of one of the screen's of a #GdkDisplay,
 * finds the screen.
 * 
 * Return value: the #GdkScreen corresponding to @xrootwin, or %NULL.
 **/
GdkScreen *
_gdk_x11_display_screen_for_xrootwin (GdkDisplay *display,
				      Window      xrootwin)
{
  gint n_screens, i;

  n_screens = gdk_display_get_n_screens (display);
  for (i = 0; i < n_screens; i++)
    {
      GdkScreen *screen = gdk_display_get_screen (display, i);
      if (GDK_SCREEN_XROOTWIN (screen) == xrootwin)
	return screen;
    }

  return NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
836 837 838 839 840 841
/**
 * gdk_x11_display_get_xdisplay:
 * @display: a #GdkDisplay
 * @returns: an X display.
 *
 * Returns the X display of a #GdkDisplay.
Matthias Clasen's avatar
Matthias Clasen committed
842 843
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
844
 */
845 846 847 848 849
Display *
gdk_x11_display_get_xdisplay (GdkDisplay  *display)
{
  return GDK_DISPLAY_X11 (display)->xdisplay;
}
850 851 852 853 854 855 856

void
_gdk_windowing_set_default_display (GdkDisplay *display)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  const gchar *startup_id;
  
857 858 859 860 861 862 863
  if (!display)
    {
      gdk_display = NULL;
      return;
    }

  gdk_display = GDK_DISPLAY_XDISPLAY (display);
864 865 866 867 868 869 870

  g_free (display_x11->startup_notification_id);
  display_x11->startup_notification_id = NULL;
  
  startup_id = g_getenv ("DESKTOP_STARTUP_ID");
  if (startup_id && *startup_id != '\0')
    {
871 872
      gchar *time_str;

873 874 875 876
      if (!g_utf8_validate (startup_id, -1, NULL))
	g_warning ("DESKTOP_STARTUP_ID contains invalid UTF-8");
      else
	display_x11->startup_notification_id = g_strdup (startup_id);
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894

      /* Find the launch time from the startup_id, if it's there.  Newer spec
       * states that the startup_id is of the form <unique>_TIME<timestamp>
       */
      time_str = g_strrstr (startup_id, "_TIME");
      if (time_str != NULL)
        {
	  gulong retval;
          gchar *end;
          errno = 0;

          /* Skip past the "_TIME" part */
          time_str += 5;

          retval = strtoul (time_str, &end, 0);
          if (end != time_str && errno == 0)
            display_x11->user_time = retval;
        }
895 896
      
      /* Clear the environment variable so it won't be inherited by
897
       * child processes and confuse things.  
898
       */
899
      g_unsetenv ("DESKTOP_STARTUP_ID");
900 901 902 903 904 905 906 907 908

      /* Set the startup id on the leader window so it
       * applies to all windows we create on this display
       */
      XChangeProperty (display_x11->xdisplay,
		       display_x11->leader_window,
		       gdk_x11_get_xatom_by_name_for_display (display, "_NET_STARTUP_ID"),
		       gdk_x11_get_xatom_by_name_for_display (display, "UTF8_STRING"), 8,
		       PropModeReplace,
Matthias Clasen's avatar
Matthias Clasen committed
909
		       (guchar *)startup_id, strlen (startup_id));
910 911 912
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
913
static char*
914 915 916 917 918
escape_for_xmessage (const char *str)
{
  GString *retval;
  const char *p;
  
919
  retval = g_string_new (NULL);
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

  p = str;
  while (*p)
    {
      switch (*p)
        {
        case ' ':
        case '"':
        case '\\':
          g_string_append_c (retval, '\\');
          break;
        }

      g_string_append_c (retval, *p);
      ++p;
    }

  return g_string_free (retval, FALSE);
}

static void
broadcast_xmessage   (GdkDisplay   *display,
                      const char   *message_type,
                      const char   *message_type_begin,
                      const char   *message)
{
  Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
  GdkScreen *screen = gdk_display_get_default_screen (display);
  GdkWindow *root_window = gdk_screen_get_root_window (screen);
  Window xroot_window = GDK_WINDOW_XID (root_window);
  
  Atom type_atom;
  Atom type_atom_begin;
  Window xwindow;

  {
    XSetWindowAttributes attrs;

    attrs.override_redirect = True;
    attrs.event_mask = PropertyChangeMask | StructureNotifyMask;

    xwindow =
      XCreateWindow (xdisplay,
                     xroot_window,
                     -100, -100, 1, 1,
                     0,
                     CopyFromParent,
                     CopyFromParent,
Matthias Clasen's avatar
Matthias Clasen committed
968
                     (Visual *)CopyFromParent,
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
                     CWOverrideRedirect | CWEventMask,
                     &attrs);
  }

  type_atom = gdk_x11_get_xatom_by_name_for_display (display,
                                                     message_type);
  type_atom_begin = gdk_x11_get_xatom_by_name_for_display (display,
                                                           message_type_begin);
  
  {
    XEvent xevent;
    const char *src;
    const char *src_end;
    char *dest;
    char *dest_end;
    
    xevent.xclient.type = ClientMessage;
    xevent.xclient.message_type = type_atom_begin;
    xevent.xclient.display =xdisplay;
    xevent.xclient.window = xwindow;
    xevent.xclient.format = 8;

    src = message;
    src_end = message + strlen (message) + 1; /* +1 to include nul byte */
    
    while (src != src_end)
      {
        dest = &xevent.xclient.data.b[0];
        dest_end = dest + 20;        
        
        while (dest != dest_end &&
               src != src_end)
          {
            *dest = *src;
            ++dest;
            ++src;
          }
1006 1007 1008 1009 1010 1011

	while (dest != dest_end)
	  {
	    *dest = 0;
	    ++dest;
	  }
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
        
        XSendEvent (xdisplay,
                    xroot_window,
                    False,
                    PropertyChangeMask,
                    &xevent);

        xevent.xclient.message_type = type_atom;
      }
  }

  XDestroyWindow (xdisplay, xwindow);
  XFlush (xdisplay);
}

/**
 * gdk_notify_startup_complete:
 * 
 * Indicates to the GUI environment that the application has finished
 * loading. If the applications opens windows, this function is
 * normally called after opening the application's initial set of
 * windows.
 * 
 * GTK+ will call this function automatically after opening the first
Matthias Clasen's avatar
Matthias Clasen committed
1036 1037 1038 1039
 * #GtkWindow unless gtk_window_set_auto_startup_notification() is called 
 * to disable that feature.
 *
 * Since: 2.2
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
 **/
void
gdk_notify_startup_complete (void)
{
  GdkDisplay *display;
  GdkDisplayX11 *display_x11;
  gchar *escaped_id;
  gchar *message;

  display = gdk_display_get_default ();
  if (!display)
    return;
  
  display_x11 = GDK_DISPLAY_X11 (display);

  if (display_x11->startup_notification_id == NULL)
    return;

  escaped_id = escape_for_xmessage (display_x11->startup_notification_id);
  message = g_strdup_printf ("remove: ID=%s", escaped_id);
  g_free (escaped_id);

  broadcast_xmessage (display,
		      "_NET_STARTUP_INFO",
                      "_NET_STARTUP_INFO_BEGIN",
                      message);

  g_free (message);
}
Matthias Clasen's avatar
Matthias Clasen committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128


/**
 * gdk_display_supports_selection_notification:
 * @display: a #GdkDisplay
 * 
 * Returns whether #GdkEventOwnerChange events will be 
 * sent when the owner of a selection changes.
 * 
 * Return value: whether #GdkEventOwnerChange events will 
 *               be sent.
 *
 * Since: 2.6
 **/
gboolean 
gdk_display_supports_selection_notification (GdkDisplay *display)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);

  return display_x11->have_xfixes;
}

/**
 * gdk_display_request_selection_notification:
 * @display: a #GdkDisplay
 * @selection: the #GdkAtom naming the selection for which
 *             ownership change notification is requested
 * 
 * Request #GdkEventOwnerChange events for ownership changes
 * of the selection named by the given atom.
 * 
 * Return value: whether #GdkEventOwnerChange events will 
 *               be sent.
 *
 * Since: 2.6
 **/
gboolean gdk_display_request_selection_notification  (GdkDisplay *display,
						      GdkAtom     selection)

{
#ifdef HAVE_XFIXES
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  Atom atom;

  if (display_x11->have_xfixes)
    {
      atom = gdk_x11_atom_to_xatom_for_display (display, 
						selection);
      XFixesSelectSelectionInput (display_x11->xdisplay, 
				  display_x11->leader_window,
				  atom,
				  XFixesSetSelectionOwnerNotifyMask |
				  XFixesSelectionWindowDestroyNotifyMask |
				  XFixesSelectionClientCloseNotifyMask);
      return TRUE;
    }
  else
#endif
    return FALSE;
}
1129 1130 1131 1132 1133 1134

/**
 * gdk_display_supports_clipboard_persistence
 * @display: a #GdkDisplay
 *
 * Returns whether the speicifed display supports clipboard
Matthias Clasen's avatar
Matthias Clasen committed
1135
 * persistance; i.e. if it's possible to store the clipboard data after an
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
 * application has quit. On X11 this checks if a clipboard daemon is
 * running.
 *
 * Returns: %TRUE if the display supports clipboard persistance.
 *
 * Since: 2.6
 */
gboolean
gdk_display_supports_clipboard_persistence (GdkDisplay *display)
{
  /* It might make sense to cache this */
  return XGetSelectionOwner (GDK_DISPLAY_X11 (display)->xdisplay,
			     gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER")) != None;
}

/**
 * gdk_display_store_clipboard
 * @display:          a #GdkDisplay
 * @clipboard_window: a #GdkWindow belonging to the clipboard owner
Matthias Clasen's avatar
Matthias Clasen committed
1155 1156
 * @time_:            a timestamp
 * @targets:	      an array of targets that should be saved, or %NULL 
1157
 *                    if all available targets should be saved.
Matthias Clasen's avatar
Matthias Clasen committed
1158
 * @n_targets:        length of the @targets array
1159
 *
1160
 * Issues a request to the clipboard manager to store the
1161 1162 1163
 * clipboard data. On X11, this is a special program that works
 * according to the freedesktop clipboard specification, available at
 * <ulink url="http://www.freedesktop.org/Standards/clipboard-manager-spec">
Matthias Clasen's avatar
Matthias Clasen committed
1164
 * http://www.freedesktop.org/Standards/clipboard-manager-spec</ulink>.
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
 *
 * Since: 2.6
 */
void
gdk_display_store_clipboard (GdkDisplay *display,
			     GdkWindow  *clipboard_window,
			     guint32     time_,
			     GdkAtom    *targets,
			     gint        n_targets)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  Atom clipboard_manager, save_targets;
  
  clipboard_manager = gdk_x11_get_xatom_by_name_for_display (display, "CLIPBOARD_MANAGER");
  save_targets = gdk_x11_get_xatom_by_name_for_display (display, "SAVE_TARGETS");

  gdk_error_trap_push ();

  if (XGetSelectionOwner (display_x11->xdisplay, clipboard_manager) != None)
    {
      Atom property_name = None;
      Atom *xatoms;
      int i;
      
      if (n_targets > 0)
	{
	  property_name = gdk_x11_atom_to_xatom_for_display (display, _gdk_selection_property);

	  xatoms = g_new (Atom, n_targets);
	  for (i = 0; i < n_targets; i++)
	    xatoms[i] = gdk_x11_atom_to_xatom_for_display (display, targets[i]);

	  XChangeProperty (display_x11->xdisplay, GDK_WINDOW_XID (clipboard_window),
			   property_name, XA_ATOM,
			   32, PropModeReplace, (guchar *)xatoms, n_targets);
	  g_free (xatoms);

	}
      
      XConvertSelection (display_x11->xdisplay,
			 clipboard_manager, save_targets, property_name,
			 GDK_WINDOW_XID (clipboard_window), time_);
      
    }
  gdk_error_trap_pop ();

}
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

/**
 * gdk_x11_display_get_user_time:
 * @display: a #GdkDisplay
 *
 * Returns the timestamp of the last user interaction on 
 * @display. The timestamp is taken from events caused
 * by user interaction such as key presses or pointer 
 * movements. See gdk_x11_window_set_user_time().
 *
 * Returns: the timestamp of the last user interaction 
 *
 * Since: 2.8
 */
guint32
gdk_x11_display_get_user_time (GdkDisplay *display)
{
  return GDK_DISPLAY_X11 (display)->user_time;
}
1231

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
/**
 * gdk_display_supports_shapes:
 * @display: a #GdkDisplay
 *
 * Returns %TRUE if gdk_window_shape_combine_mask() can
 * be used to create shaped windows on @display.
 *
 * Returns: %TRUE if shaped windows are supported 
 *
 * Since: 2.10
 */
gboolean 
gdk_display_supports_shapes (GdkDisplay *display)
{
  return GDK_DISPLAY_X11 (display)->have_shapes;
}

/**
 * gdk_display_supports_input_shapes:
 * @display: a #GdkDisplay
 *
 * Returns %TRUE if gdk_window_input_shape_combine_mask() can
 * be used to modify the input shape of windows on @display.
 *
 * Returns: %TRUE if windows with modified input shape are supported 
 *
 * Since: 2.10
 */
gboolean 
gdk_display_supports_input_shapes (GdkDisplay *display)
{
  return GDK_DISPLAY_X11 (display)->have_input_shapes;
}


1267 1268
#define __GDK_DISPLAY_X11_C__
#include "gdkaliasdef.c"