gdkscreen-x11.c 45.7 KB
Newer Older
1
 /*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * gdkscreen-x11.c
 * 
 * Copyright 2001 Sun Microsystems Inc. 
 *
 * 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.
 */

24
#include "config.h"
25

26
#include "gdkscreen-x11.h"
27

28 29 30 31 32
#include "gdkscreen.h"
#include "gdkdisplay.h"
#include "gdkdisplay-x11.h"
#include "gdkx.h"

33 34 35 36 37
#include <glib.h>

#include <stdlib.h>
#include <string.h>

38 39
#include <X11/Xatom.h>

40 41 42 43 44 45 46
#ifdef HAVE_SOLARIS_XINERAMA
#include <X11/extensions/xinerama.h>
#endif
#ifdef HAVE_XFREE_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif

47 48 49 50
#ifdef HAVE_RANDR
#include <X11/extensions/Xrandr.h>
#endif

Søren Sandmann's avatar
Søren Sandmann committed
51 52 53 54
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

55 56
#include "gdksettings.c"

57
static void         gdk_screen_x11_dispose     (GObject		  *object);
58
static void         gdk_screen_x11_finalize    (GObject		  *object);
59
static void	    init_randr_support	       (GdkScreen	  *screen);
Søren Sandmann's avatar
Søren Sandmann committed
60
static void	    deinit_multihead           (GdkScreen         *screen);
61

62 63 64 65 66
enum
{
  WINDOW_MANAGER_CHANGED,
  LAST_SIGNAL
};
67

68
static guint signals[LAST_SIGNAL] = { 0 };
69

Matthias Clasen's avatar
Matthias Clasen committed
70
G_DEFINE_TYPE (GdkScreenX11, _gdk_screen_x11, GDK_TYPE_SCREEN)
71

72 73 74 75 76 77 78 79
typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;

struct _NetWmSupportedAtoms
{
  Atom *atoms;
  gulong n_atoms;
};

Søren Sandmann's avatar
Søren Sandmann committed
80 81 82 83 84 85 86 87 88 89
struct _GdkX11Monitor
{
  GdkRectangle  geometry;
  XID		output;
  int		width_mm;
  int		height_mm;
  char *	output_name;
  char *	manufacturer;
};

90

Matthias Clasen's avatar
Matthias Clasen committed
91 92 93 94 95
static void
_gdk_screen_x11_init (GdkScreenX11 *screen)
{
}

96 97
static GdkDisplay *
gdk_screen_x11_get_display (GdkScreen *screen)
98
{
99
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
100

101
  return GDK_SCREEN_X11 (screen)->display;
102
}
103 104 105

static gint
gdk_screen_x11_get_width (GdkScreen *screen)
106
{
107
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
108

109
  return WidthOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
110 111
}

112 113
static gint
gdk_screen_x11_get_height (GdkScreen *screen)
114
{
115
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
116

117
  return HeightOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
118 119
}

120 121
static gint
gdk_screen_x11_get_width_mm (GdkScreen *screen)
122
{
123
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);  
124

125
  return WidthMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
126 127
}

128 129
static gint
gdk_screen_x11_get_height_mm (GdkScreen *screen)
130
{
131
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
132

133
  return HeightMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
134 135
}

136 137
static gint
gdk_screen_x11_get_number (GdkScreen *screen)
138
{
139
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
140
  
141
  return GDK_SCREEN_X11 (screen)->screen_num;
142 143
}

144 145
static GdkWindow *
gdk_screen_x11_get_root_window (GdkScreen *screen)
146
{
147
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
148

149
  return GDK_SCREEN_X11 (screen)->root_window;
150 151
}

152 153 154 155 156 157 158 159 160 161 162 163
static void
_gdk_screen_x11_events_uninit (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  if (screen_x11->xsettings_client)
    {
      xsettings_client_destroy (screen_x11->xsettings_client);
      screen_x11->xsettings_client = NULL;
    }
}

164 165 166 167
static void
gdk_screen_x11_dispose (GObject *object)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
168 169 170 171 172 173 174 175 176 177
  int i;

  for (i = 0; i < 32; ++i)
    {
      if (screen_x11->subwindow_gcs[i])
        {
          XFreeGC (screen_x11->xdisplay, screen_x11->subwindow_gcs[i]);
          screen_x11->subwindow_gcs[i] = 0;
        }
    }
178

179
  _gdk_screen_x11_events_uninit (GDK_SCREEN (object));
180

181 182 183
  if (screen_x11->root_window)
    _gdk_window_destroy (screen_x11->root_window, TRUE);

Matthias Clasen's avatar
Matthias Clasen committed
184
  G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->dispose (object);
185 186 187 188 189 190 191 192

  screen_x11->xdisplay = NULL;
  screen_x11->xscreen = NULL;
  screen_x11->screen_num = -1;
  screen_x11->xroot_window = None;
  screen_x11->wmspec_check_window = None;
}

193 194 195 196
static void
gdk_screen_x11_finalize (GObject *object)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
197 198 199 200
  gint          i;

  if (screen_x11->root_window)
    g_object_unref (screen_x11->root_window);
201

202 203 204
  /* Visual Part */
  for (i = 0; i < screen_x11->nvisuals; i++)
    g_object_unref (screen_x11->visuals[i]);
205 206
  g_free (screen_x11->visuals);
  g_hash_table_destroy (screen_x11->visual_hash);
207

208
  g_free (screen_x11->window_manager_name);
209

Søren Sandmann's avatar
Søren Sandmann committed
210 211
  deinit_multihead (GDK_SCREEN (object));
  
Matthias Clasen's avatar
Matthias Clasen committed
212
  G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->finalize (object);
213 214
}

215 216
static gint
gdk_screen_x11_get_n_monitors (GdkScreen *screen)
217
{
218
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
Søren Sandmann's avatar
Søren Sandmann committed
219

220
  return GDK_SCREEN_X11 (screen)->n_monitors;
Søren Sandmann's avatar
Søren Sandmann committed
221 222
}

223 224
static gint
gdk_screen_x11_get_primary_monitor (GdkScreen *screen)
225 226 227 228 229 230
{
  g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);

  return GDK_SCREEN_X11 (screen)->primary_monitor;
}

231 232 233
static gint
gdk_screen_x11_get_monitor_width_mm	(GdkScreen *screen,
					 gint       monitor_num)
Søren Sandmann's avatar
Søren Sandmann committed
234
{
235 236 237 238 239 240 241
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
  g_return_val_if_fail (monitor_num >= 0, -1);
  g_return_val_if_fail (monitor_num < screen_x11->n_monitors, -1);

  return screen_x11->monitors[monitor_num].width_mm;
Søren Sandmann's avatar
Søren Sandmann committed
242 243
}

244 245 246
static gint
gdk_screen_x11_get_monitor_height_mm (GdkScreen *screen,
				      gint       monitor_num)
Søren Sandmann's avatar
Søren Sandmann committed
247
{
248 249 250 251 252 253 254
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
  g_return_val_if_fail (monitor_num >= 0, -1);
  g_return_val_if_fail (monitor_num < screen_x11->n_monitors, -1);

  return screen_x11->monitors[monitor_num].height_mm;
Søren Sandmann's avatar
Søren Sandmann committed
255 256
}

257 258 259
static gchar *
gdk_screen_x11_get_monitor_plug_name (GdkScreen *screen,
				      gint       monitor_num)
Søren Sandmann's avatar
Søren Sandmann committed
260
{
261 262 263 264 265 266 267
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
  g_return_val_if_fail (monitor_num >= 0, NULL);
  g_return_val_if_fail (monitor_num < screen_x11->n_monitors, NULL);

  return g_strdup (screen_x11->monitors[monitor_num].output_name);
Søren Sandmann's avatar
Søren Sandmann committed
268 269 270 271 272
}

/**
 * gdk_x11_screen_get_monitor_output:
 * @screen: a #GdkScreen
273
 * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
Søren Sandmann's avatar
Søren Sandmann committed
274 275
 *
 * Gets the XID of the specified output/monitor.
276
 * If the X server does not support version 1.2 of the RANDR
Søren Sandmann's avatar
Søren Sandmann committed
277 278 279 280 281 282 283 284 285 286
 * extension, 0 is returned.
 *
 * Returns: the XID of the monitor
 *
 * Since: 2.14
 */
XID
gdk_x11_screen_get_monitor_output (GdkScreen *screen,
                                   gint       monitor_num)
{
287 288 289 290 291 292 293
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  g_return_val_if_fail (GDK_IS_SCREEN (screen), None);
  g_return_val_if_fail (monitor_num >= 0, None);
  g_return_val_if_fail (monitor_num < screen_x11->n_monitors, None);

  return screen_x11->monitors[monitor_num].output;
294 295
}

296 297 298 299
static void
gdk_screen_x11_get_monitor_geometry (GdkScreen    *screen,
				     gint          monitor_num,
				     GdkRectangle *dest)
300
{
301
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
302

303 304 305 306 307 308
  g_return_if_fail (GDK_IS_SCREEN (screen));
  g_return_if_fail (monitor_num >= 0);
  g_return_if_fail (monitor_num < screen_x11->n_monitors);

  if (dest)
    *dest = screen_x11->monitors[monitor_num].geometry;
309 310
}

311 312
static GdkVisual *
gdk_screen_x11_get_rgba_visual (GdkScreen *screen)
313 314 315 316 317 318 319 320 321 322
{
  GdkScreenX11 *screen_x11;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  screen_x11 = GDK_SCREEN_X11 (screen);

  return screen_x11->rgba_visual;
}

Matthias Clasen's avatar
Matthias Clasen committed
323 324 325
/**
 * gdk_x11_screen_get_xscreen:
 * @screen: a #GdkScreen.
326
 * @returns: (transfer none): an Xlib <type>Screen*</type>
Matthias Clasen's avatar
Matthias Clasen committed
327 328
 *
 * Returns the screen of a #GdkScreen.
Matthias Clasen's avatar
Matthias Clasen committed
329 330
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
331
 */
332 333 334 335 336 337
Screen *
gdk_x11_screen_get_xscreen (GdkScreen *screen)
{
  return GDK_SCREEN_X11 (screen)->xscreen;
}

Matthias Clasen's avatar
Matthias Clasen committed
338 339 340 341 342 343 344
/**
 * gdk_x11_screen_get_screen_number:
 * @screen: a #GdkScreen.
 * @returns: the position of @screen among the screens of
 *   its display.
 *
 * Returns the index of a #GdkScreen.
Matthias Clasen's avatar
Matthias Clasen committed
345 346
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
347
 */
348 349 350 351 352
int
gdk_x11_screen_get_screen_number (GdkScreen *screen)
{
  return GDK_SCREEN_X11 (screen)->screen_num;
}
353

Søren Sandmann's avatar
Søren Sandmann committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
static gboolean
check_is_composited (GdkDisplay *display,
		     GdkScreenX11 *screen_x11)
{
  Atom xselection = gdk_x11_atom_to_xatom_for_display (display, screen_x11->cm_selection_atom);
  Window xwindow;
  
  xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), xselection);

  return xwindow != None;
}

static GdkAtom
make_cm_atom (int screen_number)
{
  gchar *name = g_strdup_printf ("_NET_WM_CM_S%d", screen_number);
  GdkAtom atom = gdk_atom_intern (name, FALSE);
  g_free (name);
  return atom;
}

Søren Sandmann's avatar
Søren Sandmann committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
static void
init_monitor_geometry (GdkX11Monitor *monitor,
		       int x, int y, int width, int height)
{
  monitor->geometry.x = x;
  monitor->geometry.y = y;
  monitor->geometry.width = width;
  monitor->geometry.height = height;

  monitor->output = None;
  monitor->width_mm = -1;
  monitor->height_mm = -1;
  monitor->output_name = NULL;
  monitor->manufacturer = NULL;
}

static gboolean
init_fake_xinerama (GdkScreen *screen)
{
#ifdef G_ENABLE_DEBUG
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  XSetWindowAttributes atts;
  Window win;
  gint w, h;

  if (!(_gdk_debug_flags & GDK_DEBUG_XINERAMA))
    return FALSE;
  
  /* Fake Xinerama mode by splitting the screen into 4 monitors.
   * Also draw a little cross to make the monitor boundaries visible.
   */
  w = WidthOfScreen (screen_x11->xscreen);
  h = HeightOfScreen (screen_x11->xscreen);

  screen_x11->n_monitors = 4;
  screen_x11->monitors = g_new0 (GdkX11Monitor, 4);
  init_monitor_geometry (&screen_x11->monitors[0], 0, 0, w / 2, h / 2);
  init_monitor_geometry (&screen_x11->monitors[1], w / 2, 0, w / 2, h / 2);
  init_monitor_geometry (&screen_x11->monitors[2], 0, h / 2, w / 2, h / 2);
  init_monitor_geometry (&screen_x11->monitors[3], w / 2, h / 2, w / 2, h / 2);
  
  atts.override_redirect = 1;
  atts.background_pixel = WhitePixel(GDK_SCREEN_XDISPLAY (screen), 
				     screen_x11->screen_num);
  win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
		      screen_x11->xroot_window, 0, h / 2, w, 1, 0, 
		      DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
				   screen_x11->screen_num),
		      InputOutput, 
		      DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
				    screen_x11->screen_num),
		      CWOverrideRedirect|CWBackPixel, 
		      &atts);
  XMapRaised(GDK_SCREEN_XDISPLAY (screen), win); 
  win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
		      screen_x11->xroot_window, w/2 , 0, 1, h, 0, 
		      DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
				   screen_x11->screen_num),
		      InputOutput, 
		      DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
				    screen_x11->screen_num),
		      CWOverrideRedirect|CWBackPixel, 
		      &atts);
  XMapRaised(GDK_SCREEN_XDISPLAY (screen), win);
  return TRUE;
#endif
  
  return FALSE;
}

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
static void
free_monitors (GdkX11Monitor *monitors,
               gint           n_monitors)
{
  int i;

  for (i = 0; i < n_monitors; ++i)
    {
      g_free (monitors[i].output_name);
      g_free (monitors[i].manufacturer);
    }

  g_free (monitors);
}

460
#ifdef HAVE_RANDR
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
static int
monitor_compare_function (GdkX11Monitor *monitor1,
                          GdkX11Monitor *monitor2)
{
  /* Sort the leftmost/topmost monitors first.
   * For "cloned" monitors, sort the bigger ones first
   * (giving preference to taller monitors over wider
   * monitors)
   */

  if (monitor1->geometry.x != monitor2->geometry.x)
    return monitor1->geometry.x - monitor2->geometry.x;

  if (monitor1->geometry.y != monitor2->geometry.y)
    return monitor1->geometry.y - monitor2->geometry.y;

  if (monitor1->geometry.height != monitor2->geometry.height)
    return - (monitor1->geometry.height - monitor2->geometry.height);

  if (monitor1->geometry.width != monitor2->geometry.width)
    return - (monitor1->geometry.width - monitor2->geometry.width);

  return 0;
}
485
#endif
486

Matthias Clasen's avatar
Matthias Clasen committed
487 488 489 490 491 492 493 494 495
static gboolean
init_randr13 (GdkScreen *screen)
{
#ifdef HAVE_RANDR
  GdkDisplay *display = gdk_screen_get_display (screen);
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  Display *dpy = GDK_SCREEN_XDISPLAY (screen);
  XRRScreenResources *resources;
496
  RROutput primary_output;
497
  RROutput first_output = None;
Matthias Clasen's avatar
Matthias Clasen committed
498 499 500 501 502 503 504 505 506 507 508
  int i;
  GArray *monitors;
  gboolean randr12_compat = FALSE;

  if (!display_x11->have_randr13)
      return FALSE;

  resources = XRRGetScreenResourcesCurrent (screen_x11->xdisplay,
				            screen_x11->xroot_window);
  if (!resources)
    return FALSE;
509

Matthias Clasen's avatar
Matthias Clasen committed
510 511 512 513 514 515 516 517 518
  monitors = g_array_sized_new (FALSE, TRUE, sizeof (GdkX11Monitor),
                                resources->noutput);

  for (i = 0; i < resources->noutput; ++i)
    {
      XRROutputInfo *output =
	XRRGetOutputInfo (dpy, resources, resources->outputs[i]);

      /* Non RandR1.2 X driver have output name "default" */
519
      randr12_compat |= !g_strcmp0 (output->name, "default");
Matthias Clasen's avatar
Matthias Clasen committed
520

Matthias Clasen's avatar
Matthias Clasen committed
521
      if (output->connection == RR_Disconnected)
522 523 524 525
        {
          XRRFreeOutputInfo (output);
          continue;
        }
Matthias Clasen's avatar
Matthias Clasen committed
526

Matthias Clasen's avatar
Matthias Clasen committed
527 528 529 530 531 532 533 534 535 536 537
      if (output->crtc)
	{
	  GdkX11Monitor monitor;
	  XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, output->crtc);

	  monitor.geometry.x = crtc->x;
	  monitor.geometry.y = crtc->y;
	  monitor.geometry.width = crtc->width;
	  monitor.geometry.height = crtc->height;

	  monitor.output = resources->outputs[i];
Matthias Clasen's avatar
Matthias Clasen committed
538 539 540 541
	  monitor.width_mm = output->mm_width;
	  monitor.height_mm = output->mm_height;
	  monitor.output_name = g_strdup (output->name);
	  /* FIXME: need EDID parser */
Matthias Clasen's avatar
Matthias Clasen committed
542 543 544 545 546 547 548 549 550 551
	  monitor.manufacturer = NULL;

	  g_array_append_val (monitors, monitor);

          XRRFreeCrtcInfo (crtc);
	}

      XRRFreeOutputInfo (output);
    }

552 553 554
  if (resources->noutput > 0)
    first_output = resources->outputs[0];

Matthias Clasen's avatar
Matthias Clasen committed
555 556 557 558 559
  XRRFreeScreenResources (resources);

  /* non RandR 1.2 X driver doesn't return any usable multihead data */
  if (randr12_compat)
    {
560 561 562 563 564
      guint n_monitors = monitors->len;

      free_monitors ((GdkX11Monitor *)g_array_free (monitors, FALSE),
		     n_monitors);

Matthias Clasen's avatar
Matthias Clasen committed
565 566 567
      return FALSE;
    }

568 569
  g_array_sort (monitors,
                (GCompareFunc) monitor_compare_function);
Matthias Clasen's avatar
Matthias Clasen committed
570 571 572
  screen_x11->n_monitors = monitors->len;
  screen_x11->monitors = (GdkX11Monitor *)g_array_free (monitors, FALSE);

573 574 575 576 577 578 579 580
  screen_x11->primary_monitor = 0;

  primary_output = XRRGetOutputPrimary (screen_x11->xdisplay,
                                        screen_x11->xroot_window);

  for (i = 0; i < screen_x11->n_monitors; ++i)
    {
      if (screen_x11->monitors[i].output == primary_output)
581 582 583 584
	{
	  screen_x11->primary_monitor = i;
	  break;
	}
585 586 587 588

      /* No RandR1.3+ available or no primary set, fall back to prefer LVDS as primary if present */
      if (primary_output == None &&
          g_ascii_strncasecmp (screen_x11->monitors[i].output_name, "LVDS", 4) == 0)
589 590 591 592 593 594 595 596
	{
	  screen_x11->primary_monitor = i;
	  break;
	}

      /* No primary specified and no LVDS found */
      if (screen_x11->monitors[i].output == first_output)
	screen_x11->primary_monitor = i;
597 598
    }

599
  return screen_x11->n_monitors > 0;
Matthias Clasen's avatar
Matthias Clasen committed
600
#endif
601

Matthias Clasen's avatar
Matthias Clasen committed
602 603 604
  return FALSE;
}

Søren Sandmann's avatar
Søren Sandmann committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static gboolean
init_solaris_xinerama (GdkScreen *screen)
{
#ifdef HAVE_SOLARIS_XINERAMA
  Display *dpy = GDK_SCREEN_XDISPLAY (screen);
  int screen_no = gdk_screen_get_number (screen);
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  XRectangle monitors[MAXFRAMEBUFFERS];
  unsigned char hints[16];
  gint result;
  int n_monitors;
  int i;
  
  if (!XineramaGetState (dpy, screen_no))
    return FALSE;

  result = XineramaGetInfo (dpy, screen_no, monitors, hints, &n_monitors);

  /* Yes I know it should be Success but the current implementation 
   * returns the num of monitor
   */
  if (result == 0)
    {
628
      return FALSE;
Søren Sandmann's avatar
Søren Sandmann committed
629 630
    }

631 632
  screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
  screen_x11->n_monitors = n_monitors;
Søren Sandmann's avatar
Søren Sandmann committed
633

634
  for (i = 0; i < n_monitors; i++)
Søren Sandmann's avatar
Søren Sandmann committed
635 636
    {
      init_monitor_geometry (&screen_x11->monitors[i],
637 638
			     monitors[i].x, monitors[i].y,
			     monitors[i].width, monitors[i].height);
Søren Sandmann's avatar
Søren Sandmann committed
639
    }
640 641 642

  screen_x11->primary_monitor = 0;

Søren Sandmann's avatar
Søren Sandmann committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
  return TRUE;
#endif /* HAVE_SOLARIS_XINERAMA */

  return FALSE;
}

static gboolean
init_xfree_xinerama (GdkScreen *screen)
{
#ifdef HAVE_XFREE_XINERAMA
  Display *dpy = GDK_SCREEN_XDISPLAY (screen);
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  XineramaScreenInfo *monitors;
  int i, n_monitors;
  
  if (!XineramaIsActive (dpy))
    return FALSE;

  monitors = XineramaQueryScreens (dpy, &n_monitors);
  
  if (n_monitors <= 0 || monitors == NULL)
    {
      /* If Xinerama doesn't think we have any monitors, try acting as
       * though we had no Xinerama. If the "no monitors" condition
       * is because XRandR 1.2 is currently switching between CRTCs,
       * we'll be notified again when we have our monitor back,
       * and can go back into Xinerama-ish mode at that point.
       */
      if (monitors)
	XFree (monitors);
      
      return FALSE;
    }

  screen_x11->n_monitors = n_monitors;
  screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
  
  for (i = 0; i < n_monitors; ++i)
    {
      init_monitor_geometry (&screen_x11->monitors[i],
			     monitors[i].x_org, monitors[i].y_org,
			     monitors[i].width, monitors[i].height);
    }
  
  XFree (monitors);
  
689 690
  screen_x11->primary_monitor = 0;

Søren Sandmann's avatar
Søren Sandmann committed
691 692 693 694 695 696
  return TRUE;
#endif /* HAVE_XFREE_XINERAMA */
  
  return FALSE;
}

697 698 699 700 701 702
static void
deinit_multihead (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  free_monitors (screen_x11->monitors, screen_x11->n_monitors);
Søren Sandmann's avatar
Søren Sandmann committed
703 704 705 706 707

  screen_x11->n_monitors = 0;
  screen_x11->monitors = NULL;
}

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
static gboolean
compare_monitor (GdkX11Monitor *m1,
                 GdkX11Monitor *m2)
{
  if (m1->geometry.x != m2->geometry.x ||
      m1->geometry.y != m2->geometry.y ||
      m1->geometry.width != m2->geometry.width ||
      m1->geometry.height != m2->geometry.height)
    return FALSE;

  if (m1->width_mm != m2->width_mm ||
      m1->height_mm != m2->height_mm)
    return FALSE;

  if (g_strcmp0 (m1->output_name, m2->output_name) != 0)
    return FALSE;

  if (g_strcmp0 (m1->manufacturer, m2->manufacturer) != 0)
    return FALSE;

  return TRUE;
}

static gboolean
compare_monitors (GdkX11Monitor *monitors1, gint n_monitors1,
                  GdkX11Monitor *monitors2, gint n_monitors2)
{
  gint i;

  if (n_monitors1 != n_monitors2)
    return FALSE;

  for (i = 0; i < n_monitors1; i++)
    {
      if (!compare_monitor (monitors1 + i, monitors2 + i))
        return FALSE;
    }

  return TRUE;
}

Søren Sandmann's avatar
Søren Sandmann committed
749 750 751 752 753 754 755 756 757 758 759 760 761
static void
init_multihead (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  int opcode, firstevent, firsterror;

  /* There are four different implementations of multihead support: 
   *
   *  1. Fake Xinerama for debugging purposes
   *  2. RandR 1.2
   *  3. Solaris Xinerama
   *  4. XFree86/Xorg Xinerama
   *
Matthias Clasen's avatar
Matthias Clasen committed
762
   * We use them in that order.
Søren Sandmann's avatar
Søren Sandmann committed
763 764 765 766
   */
  if (init_fake_xinerama (screen))
    return;

Matthias Clasen's avatar
Matthias Clasen committed
767 768 769
  if (init_randr13 (screen))
    return;

Søren Sandmann's avatar
Søren Sandmann committed
770 771 772 773 774 775 776 777 778 779 780 781 782
  if (XQueryExtension (GDK_SCREEN_XDISPLAY (screen), "XINERAMA",
		       &opcode, &firstevent, &firsterror))
    {
      if (init_solaris_xinerama (screen))
	return;
      
      if (init_xfree_xinerama (screen))
	return;
    }

  /* No multihead support of any kind for this screen */
  screen_x11->n_monitors = 1;
  screen_x11->monitors = g_new0 (GdkX11Monitor, 1);
783
  screen_x11->primary_monitor = 0;
Søren Sandmann's avatar
Søren Sandmann committed
784 785 786 787 788 789

  init_monitor_geometry (screen_x11->monitors, 0, 0,
			 WidthOfScreen (screen_x11->xscreen),
			 HeightOfScreen (screen_x11->xscreen));
}

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
GdkScreen *
_gdk_x11_screen_new (GdkDisplay *display,
		     gint	 screen_number) 
{
  GdkScreen *screen;
  GdkScreenX11 *screen_x11;
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);

  screen = g_object_new (GDK_TYPE_SCREEN_X11, NULL);

  screen_x11 = GDK_SCREEN_X11 (screen);
  screen_x11->display = display;
  screen_x11->xdisplay = display_x11->xdisplay;
  screen_x11->xscreen = ScreenOfDisplay (display_x11->xdisplay, screen_number);
  screen_x11->screen_num = screen_number;
  screen_x11->xroot_window = RootWindow (display_x11->xdisplay,screen_number);
  screen_x11->wmspec_check_window = None;
807 808 809
  /* we want this to be always non-null */
  screen_x11->window_manager_name = g_strdup ("unknown");
  
Søren Sandmann's avatar
Søren Sandmann committed
810
  init_multihead (screen);
811
  init_randr_support (screen);
812
  
813
  _gdk_x11_visual_init (screen);
814
  _gdk_windowing_window_init (screen);
Søren Sandmann's avatar
Søren Sandmann committed
815
  
816 817 818
  return screen;
}

819 820 821 822 823 824 825 826 827 828 829
/*
 * It is important that we first request the selection
 * notification, and then setup the initial state of
 * is_composited to avoid a race condition here.
 */
void
_gdk_x11_screen_setup (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

  screen_x11->cm_selection_atom = make_cm_atom (screen_x11->screen_num);
Michael Meeks's avatar
Michael Meeks committed
830
  gdk_display_request_selection_notification (screen_x11->display,
831 832 833 834
					      screen_x11->cm_selection_atom);
  screen_x11->is_composited = check_is_composited (screen_x11->display, screen_x11);
}

835 836
static gboolean
gdk_screen_x11_is_composited (GdkScreen *screen)
Søren Sandmann's avatar
Søren Sandmann committed
837 838 839 840 841 842 843 844 845 846
{
  GdkScreenX11 *screen_x11;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);

  screen_x11 = GDK_SCREEN_X11 (screen);

  return screen_x11->is_composited;
}

847 848 849 850 851 852 853 854
static void
init_randr_support (GdkScreen * screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  
  XSelectInput (GDK_SCREEN_XDISPLAY (screen),
		screen_x11->xroot_window,
		StructureNotifyMask);
Søren Sandmann's avatar
Søren Sandmann committed
855 856 857 858 859 860 861 862

#ifdef HAVE_RANDR
  XRRSelectInput (GDK_SCREEN_XDISPLAY (screen),
		  screen_x11->xroot_window,
		  RRScreenChangeNotifyMask	|
		  RRCrtcChangeNotifyMask	|
		  RROutputPropertyNotifyMask);
#endif
863 864
}

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
static void
process_monitors_change (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  gint		 n_monitors;
  GdkX11Monitor	*monitors;
  gboolean changed;

  n_monitors = screen_x11->n_monitors;
  monitors = screen_x11->monitors;

  screen_x11->n_monitors = 0;
  screen_x11->monitors = NULL;

  init_multihead (screen);

  changed = !compare_monitors (monitors, n_monitors,
                               screen_x11->monitors, screen_x11->n_monitors);

  free_monitors (monitors, n_monitors);

  if (changed)
    g_signal_emit_by_name (screen, "monitors-changed");
}

890 891 892 893
void
_gdk_x11_screen_size_changed (GdkScreen *screen,
			      XEvent    *event)
{
894
  gint width, height;
895
#ifdef HAVE_RANDR
896
  GdkDisplayX11 *display_x11;
897
#endif
898 899 900 901

  width = gdk_screen_get_width (screen);
  height = gdk_screen_get_height (screen);

902
#ifdef HAVE_RANDR
903 904 905
  display_x11 = GDK_DISPLAY_X11 (gdk_screen_get_display (screen));

  if (display_x11->have_randr13 && event->type == ConfigureNotify)
906 907 908 909
    {
      g_signal_emit_by_name (screen, "monitors-changed");
      return;
    }
910 911

  XRRUpdateConfiguration (event);
912 913 914 915 916
#else
  if (event->type == ConfigureNotify)
    {
      XConfigureEvent *rcevent = (XConfigureEvent *) event;
      Screen	    *xscreen = gdk_x11_screen_get_xscreen (screen);
917

918 919 920 921 922 923
      xscreen->width   = rcevent->width;
      xscreen->height  = rcevent->height;
    }
  else
    return;
#endif
924

925
  process_monitors_change (screen);
Søren Sandmann's avatar
Søren Sandmann committed
926

927 928 929
  if (width != gdk_screen_get_width (screen) ||
      height != gdk_screen_get_height (screen))
    g_signal_emit_by_name (screen, "size-changed");
Søren Sandmann's avatar
Søren Sandmann committed
930 931
}

932 933 934
void
_gdk_x11_screen_window_manager_changed (GdkScreen *screen)
{
935
  g_signal_emit (screen, signals[WINDOW_MANAGER_CHANGED], 0);
936 937
}

Søren Sandmann's avatar
Søren Sandmann committed
938 939 940 941
void
_gdk_x11_screen_process_owner_change (GdkScreen *screen,
				      XEvent *event)
{
Matthias Clasen's avatar
Matthias Clasen committed
942
#ifdef HAVE_XFIXES
Søren Sandmann's avatar
Søren Sandmann committed
943 944 945 946 947 948 949 950 951 952 953 954 955
  XFixesSelectionNotifyEvent *selection_event = (XFixesSelectionNotifyEvent *)event;
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
  Atom xcm_selection_atom = gdk_x11_atom_to_xatom_for_display (screen_x11->display,
							       screen_x11->cm_selection_atom);

  if (selection_event->selection == xcm_selection_atom)
    {
      gboolean composited = selection_event->owner != None;

      if (composited != screen_x11->is_composited)
	{
	  screen_x11->is_composited = composited;

956
	  g_signal_emit_by_name (screen, "composited-changed");
Søren Sandmann's avatar
Søren Sandmann committed
957 958
	}
    }
Matthias Clasen's avatar
Matthias Clasen committed
959
#endif
Søren Sandmann's avatar
Søren Sandmann committed
960 961
}

962 963 964
static gchar *
substitute_screen_number (const gchar *display_name,
                          gint         screen_number)
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
{
  GString *str;
  gchar   *p;

  str = g_string_new (display_name);

  p = strrchr (str->str, '.');
  if (p && p >	strchr (str->str, ':'))
    g_string_truncate (str, p - str->str);

  g_string_append_printf (str, ".%d", screen_number);

  return g_string_free (str, FALSE);
}

980 981
static gchar *
gdk_screen_x11_make_display_name (GdkScreen *screen)
982 983 984
{
  const gchar *old_display;

985
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
986

987
  old_display = gdk_display_get_name (gdk_screen_get_display (screen));
988

989 990
  return substitute_screen_number (old_display,
                                   gdk_screen_get_number (screen));
991
}
992

993 994
static GdkWindow *
gdk_screen_x11_get_active_window (GdkScreen *screen)
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
{
  GdkScreenX11 *screen_x11;
  GdkWindow *ret = NULL;
  Atom type_return;
  gint format_return;
  gulong nitems_return;
  gulong bytes_after_return;
  guchar *data = NULL;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  if (!gdk_x11_screen_supports_net_wm_hint (screen,
                                            gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
    return NULL;

  screen_x11 = GDK_SCREEN_X11 (screen);

  if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
	                  gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
			                                         "_NET_ACTIVE_WINDOW"),
		          0, 1, False, XA_WINDOW, &type_return,
		          &format_return, &nitems_return,
                          &bytes_after_return, &data)
      == Success)
    {
      if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
        {
          GdkNativeWindow window = *(GdkNativeWindow *) data;

          if (window != None)
            {
1026 1027
              ret = gdk_x11_window_foreign_new_for_display (screen_x11->display,
                                                        *(Window *) data);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
            }
        }
    }

  if (data)
    XFree (data);

  return ret;
}

1038 1039
static GList *
gdk_screen_x11_get_window_stack (GdkScreen *screen)
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
{
  GdkScreenX11 *screen_x11;
  GList *ret = NULL;
  Atom type_return;
  gint format_return;
  gulong nitems_return;
  gulong bytes_after_return;
  guchar *data = NULL;

  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);

  if (!gdk_x11_screen_supports_net_wm_hint (screen,
                                            gdk_atom_intern_static_string ("_NET_CLIENT_LIST_STACKING")))
    return NULL;

  screen_x11 = GDK_SCREEN_X11 (screen);

  if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
	                  gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
			                                         "_NET_CLIENT_LIST_STACKING"),
		          0, G_MAXLONG, False, XA_WINDOW, &type_return,
		          &format_return, &nitems_return,
                          &bytes_after_return, &data)
      == Success)
    {
      if ((type_return == XA_WINDOW) && (format_return == 32) &&
          (data) && (nitems_return > 0))
        {
1068
          gulong *stack = (gulong *) data;
1069 1070 1071 1072 1073
          GdkWindow *win;
          int i;

          for (i = 0; i < nitems_return; i++)
            {
1074 1075
              win = gdk_x11_window_foreign_new_for_display (screen_x11->display,
                                                            (Window)stack[i]);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

              if (win != NULL)
                ret = g_list_append (ret, win);
            }
        }
    }

  if (data)
    XFree (data);

  return ret;
}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
/* Sends a ClientMessage to all toplevel client windows */
static gboolean
gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
					      XEvent     *xev,
					      guint32     xid,
					      guint       level)
{
  Atom type = None;
  int format;
  unsigned long nitems, after;
  unsigned char *data;
  Window *ret_children, ret_root, ret_parent;
  unsigned int ret_nchildren;
  gboolean send = FALSE;
  gboolean found = FALSE;
  gboolean result = FALSE;
  int i;

1107
  gdk_x11_display_error_trap_push (display);
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

  if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
			  gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
			  0, 0, False, AnyPropertyType,
			  &type, &format, &nitems, &after, &data) != Success)
    goto out;

  if (type)
    {
      send = TRUE;
      XFree (data);
    }
  else
    {
      /* OK, we're all set, now let's find some windows to send this to */
      if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
		      &ret_root, &ret_parent,
		      &ret_children, &ret_nchildren))
	goto out;

      for(i = 0; i < ret_nchildren; i++)
	if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
	  found = TRUE;

      XFree (ret_children);
    }

  if (send || (!found && (level == 1)))
    {
      xev->xclient.window = xid;
      _gdk_send_xevent (display, xid, False, NoEventMask, xev);
    }

  result = send || found;

 out:
1144
  gdk_x11_display_error_trap_pop_ignored (display);
1145 1146 1147 1148

  return result;
}

1149 1150 1151
static void
gdk_screen_x11_broadcast_client_message (GdkScreen *screen,
					 GdkEvent  *event)
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 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
{
  XEvent sev;
  GdkWindow *root_window;

  g_return_if_fail (event != NULL);

  root_window = gdk_screen_get_root_window (screen);

  /* Set up our event to send, with the exception of its target window */
  sev.xclient.type = ClientMessage;
  sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
  sev.xclient.format = event->client.data_format;
  memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
  sev.xclient.message_type =
    gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
				       event->client.message_type);

  gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
						&sev,
						GDK_WINDOW_XID (root_window),
						0);
}

static gboolean
check_transform (const gchar *xsettings_name,
		 GType        src_type,
		 GType        dest_type)
{
  if (!g_value_type_transformable (src_type, dest_type))
    {
      g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
		 xsettings_name,
		 g_type_name (src_type),
		 g_type_name (dest_type));
      return FALSE;
    }
  else
    return TRUE;
}

1192 1193 1194 1195
static gboolean
gdk_screen_x11_get_setting (GdkScreen   *screen,
			    const gchar *name,
			    GValue      *value)
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 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 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333