]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkscreen-x11.c
gdk: Remove GdkPixmap
[~andy/gtk] / gdk / x11 / gdkscreen-x11.c
1  /*
2  * gdkscreen-x11.c
3  * 
4  * Copyright 2001 Sun Microsystems Inc. 
5  *
6  * Erwann Chenede <erwann.chenede@sun.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <glib.h>
30 #include "gdkscreen.h"
31 #include "gdkscreen-x11.h"
32 #include "gdkdisplay.h"
33 #include "gdkdisplay-x11.h"
34 #include "gdkx.h"
35
36 #include <X11/Xatom.h>
37
38 #ifdef HAVE_SOLARIS_XINERAMA
39 #include <X11/extensions/xinerama.h>
40 #endif
41 #ifdef HAVE_XFREE_XINERAMA
42 #include <X11/extensions/Xinerama.h>
43 #endif
44
45 #ifdef HAVE_RANDR
46 #include <X11/extensions/Xrandr.h>
47 #endif
48
49 #ifdef HAVE_XFIXES
50 #include <X11/extensions/Xfixes.h>
51 #endif
52
53 #include "gdksettings.c"
54
55 static void         gdk_screen_x11_dispose     (GObject           *object);
56 static void         gdk_screen_x11_finalize    (GObject           *object);
57 static void         init_randr_support         (GdkScreen         *screen);
58 static void         deinit_multihead           (GdkScreen         *screen);
59
60 enum
61 {
62   WINDOW_MANAGER_CHANGED,
63   LAST_SIGNAL
64 };
65
66 static guint signals[LAST_SIGNAL] = { 0 };
67
68 G_DEFINE_TYPE (GdkScreenX11, _gdk_screen_x11, GDK_TYPE_SCREEN)
69
70 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
71
72 struct _NetWmSupportedAtoms
73 {
74   Atom *atoms;
75   gulong n_atoms;
76 };
77
78 struct _GdkX11Monitor
79 {
80   GdkRectangle  geometry;
81   XID           output;
82   int           width_mm;
83   int           height_mm;
84   char *        output_name;
85   char *        manufacturer;
86 };
87
88 static void
89 _gdk_screen_x11_class_init (GdkScreenX11Class *klass)
90 {
91   GObjectClass *object_class = G_OBJECT_CLASS (klass);
92   
93   object_class->dispose = gdk_screen_x11_dispose;
94   object_class->finalize = gdk_screen_x11_finalize;
95
96   signals[WINDOW_MANAGER_CHANGED] =
97     g_signal_new (g_intern_static_string ("window_manager_changed"),
98                   G_OBJECT_CLASS_TYPE (object_class),
99                   G_SIGNAL_RUN_LAST,
100                   G_STRUCT_OFFSET (GdkScreenX11Class, window_manager_changed),
101                   NULL, NULL,
102                   g_cclosure_marshal_VOID__VOID,
103                   G_TYPE_NONE,
104                   0);
105 }
106
107 static void
108 _gdk_screen_x11_init (GdkScreenX11 *screen)
109 {
110 }
111
112 /**
113  * gdk_screen_get_display:
114  * @screen: a #GdkScreen
115  *
116  * Gets the display to which the @screen belongs.
117  * 
118  * Returns: the display to which @screen belongs
119  *
120  * Since: 2.2
121  **/
122 GdkDisplay *
123 gdk_screen_get_display (GdkScreen *screen)
124 {
125   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
126
127   return GDK_SCREEN_X11 (screen)->display;
128 }
129 /**
130  * gdk_screen_get_width:
131  * @screen: a #GdkScreen
132  *
133  * Gets the width of @screen in pixels
134  * 
135  * Returns: the width of @screen in pixels.
136  *
137  * Since: 2.2
138  **/
139 gint
140 gdk_screen_get_width (GdkScreen *screen)
141 {
142   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
143
144   return WidthOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
145 }
146
147 /**
148  * gdk_screen_get_height:
149  * @screen: a #GdkScreen
150  *
151  * Gets the height of @screen in pixels
152  * 
153  * Returns: the height of @screen in pixels.
154  *
155  * Since: 2.2
156  **/
157 gint
158 gdk_screen_get_height (GdkScreen *screen)
159 {
160   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
161
162   return HeightOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
163 }
164
165 /**
166  * gdk_screen_get_width_mm:
167  * @screen: a #GdkScreen
168  *
169  * Gets the width of @screen in millimeters. 
170  * Note that on some X servers this value will not be correct.
171  * 
172  * Returns: the width of @screen in millimeters.
173  *
174  * Since: 2.2
175  **/
176 gint
177 gdk_screen_get_width_mm (GdkScreen *screen)
178 {
179   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);  
180
181   return WidthMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
182 }
183
184 /**
185  * gdk_screen_get_height_mm:
186  * @screen: a #GdkScreen
187  *
188  * Returns the height of @screen in millimeters. 
189  * Note that on some X servers this value will not be correct.
190  * 
191  * Returns: the heigth of @screen in millimeters.
192  *
193  * Since: 2.2
194  **/
195 gint
196 gdk_screen_get_height_mm (GdkScreen *screen)
197 {
198   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
199
200   return HeightMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
201 }
202
203 /**
204  * gdk_screen_get_number:
205  * @screen: a #GdkScreen
206  *
207  * Gets the index of @screen among the screens in the display
208  * to which it belongs. (See gdk_screen_get_display())
209  * 
210  * Returns: the index
211  *
212  * Since: 2.2
213  **/
214 gint
215 gdk_screen_get_number (GdkScreen *screen)
216 {
217   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
218   
219   return GDK_SCREEN_X11 (screen)->screen_num;
220 }
221
222 /**
223  * gdk_screen_get_root_window:
224  * @screen: a #GdkScreen
225  *
226  * Gets the root window of @screen.
227  *
228  * Returns: (transfer none): the root window
229  *
230  * Since: 2.2
231  **/
232 GdkWindow *
233 gdk_screen_get_root_window (GdkScreen *screen)
234 {
235   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
236
237   return GDK_SCREEN_X11 (screen)->root_window;
238 }
239
240 /**
241  * gdk_screen_get_default_colormap:
242  * @screen: a #GdkScreen
243  *
244  * Gets the default colormap for @screen.
245  * 
246  * Returns: (transfer none): the default #GdkColormap.
247  *
248  * Since: 2.2
249  **/
250 GdkColormap *
251 gdk_screen_get_default_colormap (GdkScreen *screen)
252 {
253   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
254
255   return GDK_SCREEN_X11 (screen)->default_colormap;
256 }
257
258 /**
259  * gdk_screen_set_default_colormap:
260  * @screen: a #GdkScreen
261  * @colormap: a #GdkColormap
262  *
263  * Sets the default @colormap for @screen.
264  *
265  * Since: 2.2
266  **/
267 void
268 gdk_screen_set_default_colormap (GdkScreen   *screen,
269                                  GdkColormap *colormap)
270 {
271   GdkColormap *old_colormap;
272   
273   g_return_if_fail (GDK_IS_SCREEN (screen));
274   g_return_if_fail (GDK_IS_COLORMAP (colormap));
275
276   old_colormap = GDK_SCREEN_X11 (screen)->default_colormap;
277
278   GDK_SCREEN_X11 (screen)->default_colormap = g_object_ref (colormap);
279   
280   if (old_colormap)
281     g_object_unref (old_colormap);
282 }
283
284 static void
285 _gdk_screen_x11_events_uninit (GdkScreen *screen)
286 {
287   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
288
289   if (screen_x11->xsettings_client)
290     {
291       xsettings_client_destroy (screen_x11->xsettings_client);
292       screen_x11->xsettings_client = NULL;
293     }
294 }
295
296 static void
297 gdk_screen_x11_dispose (GObject *object)
298 {
299   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
300   int i;
301
302   for (i = 0; i < 32; ++i)
303     {
304       if (screen_x11->subwindow_gcs[i])
305         {
306           XFreeGC (screen_x11->xdisplay, screen_x11->subwindow_gcs[i]);
307           screen_x11->subwindow_gcs[i] = 0;
308         }
309     }
310
311   _gdk_screen_x11_events_uninit (GDK_SCREEN (object));
312
313   if (screen_x11->default_colormap)
314     {
315       g_object_unref (screen_x11->default_colormap);
316       screen_x11->default_colormap = NULL;
317     }
318
319   if (screen_x11->system_colormap)
320     {
321       g_object_unref (screen_x11->system_colormap);
322       screen_x11->system_colormap = NULL;
323     }
324
325   if (screen_x11->rgba_colormap)
326     {
327       g_object_unref (screen_x11->rgba_colormap);
328       screen_x11->rgba_colormap = NULL;
329     }
330
331   if (screen_x11->root_window)
332     _gdk_window_destroy (screen_x11->root_window, TRUE);
333
334   G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->dispose (object);
335
336   screen_x11->xdisplay = NULL;
337   screen_x11->xscreen = NULL;
338   screen_x11->screen_num = -1;
339   screen_x11->xroot_window = None;
340   screen_x11->wmspec_check_window = None;
341 }
342
343 static void
344 gdk_screen_x11_finalize (GObject *object)
345 {
346   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
347   gint          i;
348
349   if (screen_x11->root_window)
350     g_object_unref (screen_x11->root_window);
351
352   /* Visual Part */
353   for (i = 0; i < screen_x11->nvisuals; i++)
354     g_object_unref (screen_x11->visuals[i]);
355   g_free (screen_x11->visuals);
356   g_hash_table_destroy (screen_x11->visual_hash);
357
358   g_free (screen_x11->window_manager_name);
359
360   g_hash_table_destroy (screen_x11->colormap_hash);
361
362   deinit_multihead (GDK_SCREEN (object));
363   
364   G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->finalize (object);
365 }
366
367 /**
368  * gdk_screen_get_n_monitors:
369  * @screen: a #GdkScreen
370  *
371  * Returns the number of monitors which @screen consists of.
372  *
373  * Returns: number of monitors which @screen consists of
374  *
375  * Since: 2.2
376  */
377 gint
378 gdk_screen_get_n_monitors (GdkScreen *screen)
379 {
380   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
381
382   return GDK_SCREEN_X11 (screen)->n_monitors;
383 }
384
385 /**
386  * gdk_screen_get_primary_monitor:
387  * @screen: a #GdkScreen.
388  *
389  * Gets the primary monitor for @screen.  The primary monitor
390  * is considered the monitor where the 'main desktop' lives.
391  * While normal application windows typically allow the window
392  * manager to place the windows, specialized desktop applications
393  * such as panels should place themselves on the primary monitor.
394  *
395  * If no primary monitor is configured by the user, the return value
396  * will be 0, defaulting to the first monitor.
397  *
398  * Returns: An integer index for the primary monitor, or 0 if none is configured.
399  *
400  * Since: 2.20
401  */
402 gint
403 gdk_screen_get_primary_monitor (GdkScreen *screen)
404 {
405   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
406
407   return GDK_SCREEN_X11 (screen)->primary_monitor;
408 }
409
410 /**
411  * gdk_screen_get_monitor_width_mm:
412  * @screen: a #GdkScreen
413  * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
414  *
415  * Gets the width in millimeters of the specified monitor, if available.
416  *
417  * Returns: the width of the monitor, or -1 if not available
418  *
419  * Since: 2.14
420  */
421 gint
422 gdk_screen_get_monitor_width_mm (GdkScreen *screen,
423                                  gint       monitor_num)
424 {
425   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
426
427   g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
428   g_return_val_if_fail (monitor_num >= 0, -1);
429   g_return_val_if_fail (monitor_num < screen_x11->n_monitors, -1);
430
431   return screen_x11->monitors[monitor_num].width_mm;
432 }
433
434 /**
435  * gdk_screen_get_monitor_height_mm:
436  * @screen: a #GdkScreen
437  * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
438  *
439  * Gets the height in millimeters of the specified monitor.
440  *
441  * Returns: the height of the monitor, or -1 if not available
442  *
443  * Since: 2.14
444  */
445 gint
446 gdk_screen_get_monitor_height_mm (GdkScreen *screen,
447                                   gint       monitor_num)
448 {
449   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
450
451   g_return_val_if_fail (GDK_IS_SCREEN (screen), -1);
452   g_return_val_if_fail (monitor_num >= 0, -1);
453   g_return_val_if_fail (monitor_num < screen_x11->n_monitors, -1);
454
455   return screen_x11->monitors[monitor_num].height_mm;
456 }
457
458 /**
459  * gdk_screen_get_monitor_plug_name:
460  * @screen: a #GdkScreen
461  * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
462  *
463  * Returns the output name of the specified monitor.
464  * Usually something like VGA, DVI, or TV, not the actual
465  * product name of the display device.
466  *
467  * Returns: a newly-allocated string containing the name of the monitor,
468  *   or %NULL if the name cannot be determined
469  *
470  * Since: 2.14
471  */
472 gchar *
473 gdk_screen_get_monitor_plug_name (GdkScreen *screen,
474                                   gint       monitor_num)
475 {
476   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
477
478   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
479   g_return_val_if_fail (monitor_num >= 0, NULL);
480   g_return_val_if_fail (monitor_num < screen_x11->n_monitors, NULL);
481
482   return g_strdup (screen_x11->monitors[monitor_num].output_name);
483 }
484
485 /**
486  * gdk_x11_screen_get_monitor_output:
487  * @screen: a #GdkScreen
488  * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
489  *
490  * Gets the XID of the specified output/monitor.
491  * If the X server does not support version 1.2 of the RANDR
492  * extension, 0 is returned.
493  *
494  * Returns: the XID of the monitor
495  *
496  * Since: 2.14
497  */
498 XID
499 gdk_x11_screen_get_monitor_output (GdkScreen *screen,
500                                    gint       monitor_num)
501 {
502   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
503
504   g_return_val_if_fail (GDK_IS_SCREEN (screen), None);
505   g_return_val_if_fail (monitor_num >= 0, None);
506   g_return_val_if_fail (monitor_num < screen_x11->n_monitors, None);
507
508   return screen_x11->monitors[monitor_num].output;
509 }
510
511 /**
512  * gdk_screen_get_monitor_geometry:
513  * @screen: a #GdkScreen
514  * @monitor_num: the monitor number, between 0 and gdk_screen_get_n_monitors (screen)
515  * @dest: a #GdkRectangle to be filled with the monitor geometry
516  *
517  * Retrieves the #GdkRectangle representing the size and position of
518  * the individual monitor within the entire screen area.
519  *
520  * Note that the size of the entire screen area can be retrieved via
521  * gdk_screen_get_width() and gdk_screen_get_height().
522  *
523  * Since: 2.2
524  */
525 void
526 gdk_screen_get_monitor_geometry (GdkScreen    *screen,
527                                  gint          monitor_num,
528                                  GdkRectangle *dest)
529 {
530   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
531
532   g_return_if_fail (GDK_IS_SCREEN (screen));
533   g_return_if_fail (monitor_num >= 0);
534   g_return_if_fail (monitor_num < screen_x11->n_monitors);
535
536   if (dest)
537     *dest = screen_x11->monitors[monitor_num].geometry;
538 }
539
540 /**
541  * gdk_screen_get_rgba_colormap:
542  * @screen: a #GdkScreen.
543  * 
544  * Gets a colormap to use for creating windows with an alpha
545  * channel. The windowing system on which GTK+ is running
546  * may not support this capability, in which case %NULL will
547  * be returned. Even if a non-%NULL value is returned, its
548  * possible that the window's alpha channel won't be honored
549  * when displaying the window on the screen: in particular, for
550  * X an appropriate windowing manager and compositing manager
551  * must be running to provide appropriate display.
552  *
553  * This functionality is not implemented in the Windows backend.
554  *
555  * For setting an overall opacity for a top-level window, see
556  * gdk_window_set_opacity().
557
558  * Return value: (transfer none): a colormap to use for windows with
559  *     an alpha channel or %NULL if the capability is not available.
560  *
561  * Since: 2.8
562  **/
563 GdkColormap *
564 gdk_screen_get_rgba_colormap (GdkScreen *screen)
565 {
566   GdkScreenX11 *screen_x11;
567
568   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
569
570   screen_x11 = GDK_SCREEN_X11 (screen);
571
572   if (!screen_x11->rgba_visual)
573     return NULL;
574
575   if (!screen_x11->rgba_colormap)
576     screen_x11->rgba_colormap = gdk_colormap_new (screen_x11->rgba_visual,
577                                                   FALSE);
578   
579   return screen_x11->rgba_colormap;
580 }
581
582 /**
583  * gdk_screen_get_rgba_visual:
584  * @screen: a #GdkScreen
585  * 
586  * Gets a visual to use for creating windows with an alpha channel.
587  * See the docs for gdk_screen_get_rgba_colormap() for caveats.
588  * 
589  * Return value: (transfer none): a visual to use for windows with an
590  *     alpha channel or %NULL if the capability is not available.
591  *
592  * Since: 2.8
593  **/
594 GdkVisual *
595 gdk_screen_get_rgba_visual (GdkScreen *screen)
596 {
597   GdkScreenX11 *screen_x11;
598
599   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
600
601   screen_x11 = GDK_SCREEN_X11 (screen);
602
603   return screen_x11->rgba_visual;
604 }
605
606 /**
607  * gdk_x11_screen_get_xscreen:
608  * @screen: a #GdkScreen.
609  * @returns: (transfer none): an Xlib <type>Screen*</type>
610  *
611  * Returns the screen of a #GdkScreen.
612  *
613  * Since: 2.2
614  */
615 Screen *
616 gdk_x11_screen_get_xscreen (GdkScreen *screen)
617 {
618   return GDK_SCREEN_X11 (screen)->xscreen;
619 }
620
621 /**
622  * gdk_x11_screen_get_screen_number:
623  * @screen: a #GdkScreen.
624  * @returns: the position of @screen among the screens of
625  *   its display.
626  *
627  * Returns the index of a #GdkScreen.
628  *
629  * Since: 2.2
630  */
631 int
632 gdk_x11_screen_get_screen_number (GdkScreen *screen)
633 {
634   return GDK_SCREEN_X11 (screen)->screen_num;
635 }
636
637 static gboolean
638 check_is_composited (GdkDisplay *display,
639                      GdkScreenX11 *screen_x11)
640 {
641   Atom xselection = gdk_x11_atom_to_xatom_for_display (display, screen_x11->cm_selection_atom);
642   Window xwindow;
643   
644   xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), xselection);
645
646   return xwindow != None;
647 }
648
649 static GdkAtom
650 make_cm_atom (int screen_number)
651 {
652   gchar *name = g_strdup_printf ("_NET_WM_CM_S%d", screen_number);
653   GdkAtom atom = gdk_atom_intern (name, FALSE);
654   g_free (name);
655   return atom;
656 }
657
658 static void
659 init_monitor_geometry (GdkX11Monitor *monitor,
660                        int x, int y, int width, int height)
661 {
662   monitor->geometry.x = x;
663   monitor->geometry.y = y;
664   monitor->geometry.width = width;
665   monitor->geometry.height = height;
666
667   monitor->output = None;
668   monitor->width_mm = -1;
669   monitor->height_mm = -1;
670   monitor->output_name = NULL;
671   monitor->manufacturer = NULL;
672 }
673
674 static gboolean
675 init_fake_xinerama (GdkScreen *screen)
676 {
677 #ifdef G_ENABLE_DEBUG
678   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
679   XSetWindowAttributes atts;
680   Window win;
681   gint w, h;
682
683   if (!(_gdk_debug_flags & GDK_DEBUG_XINERAMA))
684     return FALSE;
685   
686   /* Fake Xinerama mode by splitting the screen into 4 monitors.
687    * Also draw a little cross to make the monitor boundaries visible.
688    */
689   w = WidthOfScreen (screen_x11->xscreen);
690   h = HeightOfScreen (screen_x11->xscreen);
691
692   screen_x11->n_monitors = 4;
693   screen_x11->monitors = g_new0 (GdkX11Monitor, 4);
694   init_monitor_geometry (&screen_x11->monitors[0], 0, 0, w / 2, h / 2);
695   init_monitor_geometry (&screen_x11->monitors[1], w / 2, 0, w / 2, h / 2);
696   init_monitor_geometry (&screen_x11->monitors[2], 0, h / 2, w / 2, h / 2);
697   init_monitor_geometry (&screen_x11->monitors[3], w / 2, h / 2, w / 2, h / 2);
698   
699   atts.override_redirect = 1;
700   atts.background_pixel = WhitePixel(GDK_SCREEN_XDISPLAY (screen), 
701                                      screen_x11->screen_num);
702   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
703                       screen_x11->xroot_window, 0, h / 2, w, 1, 0, 
704                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
705                                    screen_x11->screen_num),
706                       InputOutput, 
707                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
708                                     screen_x11->screen_num),
709                       CWOverrideRedirect|CWBackPixel, 
710                       &atts);
711   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win); 
712   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
713                       screen_x11->xroot_window, w/2 , 0, 1, h, 0, 
714                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
715                                    screen_x11->screen_num),
716                       InputOutput, 
717                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
718                                     screen_x11->screen_num),
719                       CWOverrideRedirect|CWBackPixel, 
720                       &atts);
721   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win);
722   return TRUE;
723 #endif
724   
725   return FALSE;
726 }
727
728 static void
729 free_monitors (GdkX11Monitor *monitors,
730                gint           n_monitors)
731 {
732   int i;
733
734   for (i = 0; i < n_monitors; ++i)
735     {
736       g_free (monitors[i].output_name);
737       g_free (monitors[i].manufacturer);
738     }
739
740   g_free (monitors);
741 }
742
743 #ifdef HAVE_RANDR
744 static int
745 monitor_compare_function (GdkX11Monitor *monitor1,
746                           GdkX11Monitor *monitor2)
747 {
748   /* Sort the leftmost/topmost monitors first.
749    * For "cloned" monitors, sort the bigger ones first
750    * (giving preference to taller monitors over wider
751    * monitors)
752    */
753
754   if (monitor1->geometry.x != monitor2->geometry.x)
755     return monitor1->geometry.x - monitor2->geometry.x;
756
757   if (monitor1->geometry.y != monitor2->geometry.y)
758     return monitor1->geometry.y - monitor2->geometry.y;
759
760   if (monitor1->geometry.height != monitor2->geometry.height)
761     return - (monitor1->geometry.height - monitor2->geometry.height);
762
763   if (monitor1->geometry.width != monitor2->geometry.width)
764     return - (monitor1->geometry.width - monitor2->geometry.width);
765
766   return 0;
767 }
768 #endif
769
770 static gboolean
771 init_randr13 (GdkScreen *screen)
772 {
773 #ifdef HAVE_RANDR
774   GdkDisplay *display = gdk_screen_get_display (screen);
775   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
776   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
777   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
778   XRRScreenResources *resources;
779   RROutput primary_output;
780   RROutput first_output = None;
781   int i;
782   GArray *monitors;
783   gboolean randr12_compat = FALSE;
784
785   if (!display_x11->have_randr13)
786       return FALSE;
787
788   resources = XRRGetScreenResourcesCurrent (screen_x11->xdisplay,
789                                             screen_x11->xroot_window);
790   if (!resources)
791     return FALSE;
792
793   monitors = g_array_sized_new (FALSE, TRUE, sizeof (GdkX11Monitor),
794                                 resources->noutput);
795
796   for (i = 0; i < resources->noutput; ++i)
797     {
798       XRROutputInfo *output =
799         XRRGetOutputInfo (dpy, resources, resources->outputs[i]);
800
801       /* Non RandR1.2 X driver have output name "default" */
802       randr12_compat |= !g_strcmp0 (output->name, "default");
803
804       if (output->connection == RR_Disconnected)
805         {
806           XRRFreeOutputInfo (output);
807           continue;
808         }
809
810       if (output->crtc)
811         {
812           GdkX11Monitor monitor;
813           XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, output->crtc);
814
815           monitor.geometry.x = crtc->x;
816           monitor.geometry.y = crtc->y;
817           monitor.geometry.width = crtc->width;
818           monitor.geometry.height = crtc->height;
819
820           monitor.output = resources->outputs[i];
821           monitor.width_mm = output->mm_width;
822           monitor.height_mm = output->mm_height;
823           monitor.output_name = g_strdup (output->name);
824           /* FIXME: need EDID parser */
825           monitor.manufacturer = NULL;
826
827           g_array_append_val (monitors, monitor);
828
829           XRRFreeCrtcInfo (crtc);
830         }
831
832       XRRFreeOutputInfo (output);
833     }
834
835   if (resources->noutput > 0)
836     first_output = resources->outputs[0];
837
838   XRRFreeScreenResources (resources);
839
840   /* non RandR 1.2 X driver doesn't return any usable multihead data */
841   if (randr12_compat)
842     {
843       guint n_monitors = monitors->len;
844
845       free_monitors ((GdkX11Monitor *)g_array_free (monitors, FALSE),
846                      n_monitors);
847
848       return FALSE;
849     }
850
851   g_array_sort (monitors,
852                 (GCompareFunc) monitor_compare_function);
853   screen_x11->n_monitors = monitors->len;
854   screen_x11->monitors = (GdkX11Monitor *)g_array_free (monitors, FALSE);
855
856   screen_x11->primary_monitor = 0;
857
858   primary_output = XRRGetOutputPrimary (screen_x11->xdisplay,
859                                         screen_x11->xroot_window);
860
861   for (i = 0; i < screen_x11->n_monitors; ++i)
862     {
863       if (screen_x11->monitors[i].output == primary_output)
864         {
865           screen_x11->primary_monitor = i;
866           break;
867         }
868
869       /* No RandR1.3+ available or no primary set, fall back to prefer LVDS as primary if present */
870       if (primary_output == None &&
871           g_ascii_strncasecmp (screen_x11->monitors[i].output_name, "LVDS", 4) == 0)
872         {
873           screen_x11->primary_monitor = i;
874           break;
875         }
876
877       /* No primary specified and no LVDS found */
878       if (screen_x11->monitors[i].output == first_output)
879         screen_x11->primary_monitor = i;
880     }
881
882   return screen_x11->n_monitors > 0;
883 #endif
884
885   return FALSE;
886 }
887
888 static gboolean
889 init_solaris_xinerama (GdkScreen *screen)
890 {
891 #ifdef HAVE_SOLARIS_XINERAMA
892   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
893   int screen_no = gdk_screen_get_number (screen);
894   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
895   XRectangle monitors[MAXFRAMEBUFFERS];
896   unsigned char hints[16];
897   gint result;
898   int n_monitors;
899   int i;
900   
901   if (!XineramaGetState (dpy, screen_no))
902     return FALSE;
903
904   result = XineramaGetInfo (dpy, screen_no, monitors, hints, &n_monitors);
905
906   /* Yes I know it should be Success but the current implementation 
907    * returns the num of monitor
908    */
909   if (result == 0)
910     {
911       return FALSE;
912     }
913
914   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
915   screen_x11->n_monitors = n_monitors;
916
917   for (i = 0; i < n_monitors; i++)
918     {
919       init_monitor_geometry (&screen_x11->monitors[i],
920                              monitors[i].x, monitors[i].y,
921                              monitors[i].width, monitors[i].height);
922     }
923
924   screen_x11->primary_monitor = 0;
925
926   return TRUE;
927 #endif /* HAVE_SOLARIS_XINERAMA */
928
929   return FALSE;
930 }
931
932 static gboolean
933 init_xfree_xinerama (GdkScreen *screen)
934 {
935 #ifdef HAVE_XFREE_XINERAMA
936   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
937   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
938   XineramaScreenInfo *monitors;
939   int i, n_monitors;
940   
941   if (!XineramaIsActive (dpy))
942     return FALSE;
943
944   monitors = XineramaQueryScreens (dpy, &n_monitors);
945   
946   if (n_monitors <= 0 || monitors == NULL)
947     {
948       /* If Xinerama doesn't think we have any monitors, try acting as
949        * though we had no Xinerama. If the "no monitors" condition
950        * is because XRandR 1.2 is currently switching between CRTCs,
951        * we'll be notified again when we have our monitor back,
952        * and can go back into Xinerama-ish mode at that point.
953        */
954       if (monitors)
955         XFree (monitors);
956       
957       return FALSE;
958     }
959
960   screen_x11->n_monitors = n_monitors;
961   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
962   
963   for (i = 0; i < n_monitors; ++i)
964     {
965       init_monitor_geometry (&screen_x11->monitors[i],
966                              monitors[i].x_org, monitors[i].y_org,
967                              monitors[i].width, monitors[i].height);
968     }
969   
970   XFree (monitors);
971   
972   screen_x11->primary_monitor = 0;
973
974   return TRUE;
975 #endif /* HAVE_XFREE_XINERAMA */
976   
977   return FALSE;
978 }
979
980 static void
981 deinit_multihead (GdkScreen *screen)
982 {
983   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
984
985   free_monitors (screen_x11->monitors, screen_x11->n_monitors);
986
987   screen_x11->n_monitors = 0;
988   screen_x11->monitors = NULL;
989 }
990
991 static gboolean
992 compare_monitor (GdkX11Monitor *m1,
993                  GdkX11Monitor *m2)
994 {
995   if (m1->geometry.x != m2->geometry.x ||
996       m1->geometry.y != m2->geometry.y ||
997       m1->geometry.width != m2->geometry.width ||
998       m1->geometry.height != m2->geometry.height)
999     return FALSE;
1000
1001   if (m1->width_mm != m2->width_mm ||
1002       m1->height_mm != m2->height_mm)
1003     return FALSE;
1004
1005   if (g_strcmp0 (m1->output_name, m2->output_name) != 0)
1006     return FALSE;
1007
1008   if (g_strcmp0 (m1->manufacturer, m2->manufacturer) != 0)
1009     return FALSE;
1010
1011   return TRUE;
1012 }
1013
1014 static gboolean
1015 compare_monitors (GdkX11Monitor *monitors1, gint n_monitors1,
1016                   GdkX11Monitor *monitors2, gint n_monitors2)
1017 {
1018   gint i;
1019
1020   if (n_monitors1 != n_monitors2)
1021     return FALSE;
1022
1023   for (i = 0; i < n_monitors1; i++)
1024     {
1025       if (!compare_monitor (monitors1 + i, monitors2 + i))
1026         return FALSE;
1027     }
1028
1029   return TRUE;
1030 }
1031
1032 static void
1033 init_multihead (GdkScreen *screen)
1034 {
1035   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1036   int opcode, firstevent, firsterror;
1037
1038   /* There are four different implementations of multihead support: 
1039    *
1040    *  1. Fake Xinerama for debugging purposes
1041    *  2. RandR 1.2
1042    *  3. Solaris Xinerama
1043    *  4. XFree86/Xorg Xinerama
1044    *
1045    * We use them in that order.
1046    */
1047   if (init_fake_xinerama (screen))
1048     return;
1049
1050   if (init_randr13 (screen))
1051     return;
1052
1053   if (XQueryExtension (GDK_SCREEN_XDISPLAY (screen), "XINERAMA",
1054                        &opcode, &firstevent, &firsterror))
1055     {
1056       if (init_solaris_xinerama (screen))
1057         return;
1058       
1059       if (init_xfree_xinerama (screen))
1060         return;
1061     }
1062
1063   /* No multihead support of any kind for this screen */
1064   screen_x11->n_monitors = 1;
1065   screen_x11->monitors = g_new0 (GdkX11Monitor, 1);
1066   screen_x11->primary_monitor = 0;
1067
1068   init_monitor_geometry (screen_x11->monitors, 0, 0,
1069                          WidthOfScreen (screen_x11->xscreen),
1070                          HeightOfScreen (screen_x11->xscreen));
1071 }
1072
1073 GdkScreen *
1074 _gdk_x11_screen_new (GdkDisplay *display,
1075                      gint        screen_number) 
1076 {
1077   GdkScreen *screen;
1078   GdkScreenX11 *screen_x11;
1079   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
1080
1081   screen = g_object_new (GDK_TYPE_SCREEN_X11, NULL);
1082
1083   screen_x11 = GDK_SCREEN_X11 (screen);
1084   screen_x11->display = display;
1085   screen_x11->xdisplay = display_x11->xdisplay;
1086   screen_x11->xscreen = ScreenOfDisplay (display_x11->xdisplay, screen_number);
1087   screen_x11->screen_num = screen_number;
1088   screen_x11->xroot_window = RootWindow (display_x11->xdisplay,screen_number);
1089   screen_x11->wmspec_check_window = None;
1090   /* we want this to be always non-null */
1091   screen_x11->window_manager_name = g_strdup ("unknown");
1092   
1093   init_multihead (screen);
1094   init_randr_support (screen);
1095   
1096   _gdk_visual_init (screen);
1097   _gdk_windowing_window_init (screen);
1098   
1099   return screen;
1100 }
1101
1102 /*
1103  * It is important that we first request the selection
1104  * notification, and then setup the initial state of
1105  * is_composited to avoid a race condition here.
1106  */
1107 void
1108 _gdk_x11_screen_setup (GdkScreen *screen)
1109 {
1110   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1111
1112   screen_x11->cm_selection_atom = make_cm_atom (screen_x11->screen_num);
1113   gdk_display_request_selection_notification (screen_x11->display,
1114                                               screen_x11->cm_selection_atom);
1115   screen_x11->is_composited = check_is_composited (screen_x11->display, screen_x11);
1116 }
1117
1118 /**
1119  * gdk_screen_is_composited:
1120  * @screen: a #GdkScreen
1121  * 
1122  * Returns whether windows with an RGBA visual can reasonably
1123  * be expected to have their alpha channel drawn correctly on
1124  * the screen.
1125  *
1126  * On X11 this function returns whether a compositing manager is
1127  * compositing @screen.
1128  * 
1129  * Return value: Whether windows with RGBA visuals can reasonably be
1130  * expected to have their alpha channels drawn correctly on the screen.
1131  * 
1132  * Since: 2.10
1133  **/
1134 gboolean
1135 gdk_screen_is_composited (GdkScreen *screen)
1136 {
1137   GdkScreenX11 *screen_x11;
1138
1139   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
1140
1141   screen_x11 = GDK_SCREEN_X11 (screen);
1142
1143   return screen_x11->is_composited;
1144 }
1145
1146 static void
1147 init_randr_support (GdkScreen * screen)
1148 {
1149   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1150   
1151   XSelectInput (GDK_SCREEN_XDISPLAY (screen),
1152                 screen_x11->xroot_window,
1153                 StructureNotifyMask);
1154
1155 #ifdef HAVE_RANDR
1156   XRRSelectInput (GDK_SCREEN_XDISPLAY (screen),
1157                   screen_x11->xroot_window,
1158                   RRScreenChangeNotifyMask      |
1159                   RRCrtcChangeNotifyMask        |
1160                   RROutputPropertyNotifyMask);
1161 #endif
1162 }
1163
1164 static void
1165 process_monitors_change (GdkScreen *screen)
1166 {
1167   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1168   gint           n_monitors;
1169   GdkX11Monitor *monitors;
1170   gboolean changed;
1171
1172   n_monitors = screen_x11->n_monitors;
1173   monitors = screen_x11->monitors;
1174
1175   screen_x11->n_monitors = 0;
1176   screen_x11->monitors = NULL;
1177
1178   init_multihead (screen);
1179
1180   changed = !compare_monitors (monitors, n_monitors,
1181                                screen_x11->monitors, screen_x11->n_monitors);
1182
1183   free_monitors (monitors, n_monitors);
1184
1185   if (changed)
1186     g_signal_emit_by_name (screen, "monitors-changed");
1187 }
1188
1189 void
1190 _gdk_x11_screen_size_changed (GdkScreen *screen,
1191                               XEvent    *event)
1192 {
1193   gint width, height;
1194 #ifdef HAVE_RANDR
1195   GdkDisplayX11 *display_x11;
1196 #endif
1197
1198   width = gdk_screen_get_width (screen);
1199   height = gdk_screen_get_height (screen);
1200
1201 #ifdef HAVE_RANDR
1202   display_x11 = GDK_DISPLAY_X11 (gdk_screen_get_display (screen));
1203
1204   if (display_x11->have_randr13 && event->type == ConfigureNotify)
1205     {
1206       g_signal_emit_by_name (screen, "monitors-changed");
1207       return;
1208     }
1209
1210   XRRUpdateConfiguration (event);
1211 #else
1212   if (event->type == ConfigureNotify)
1213     {
1214       XConfigureEvent *rcevent = (XConfigureEvent *) event;
1215       Screen        *xscreen = gdk_x11_screen_get_xscreen (screen);
1216
1217       xscreen->width   = rcevent->width;
1218       xscreen->height  = rcevent->height;
1219     }
1220   else
1221     return;
1222 #endif
1223
1224   process_monitors_change (screen);
1225
1226   if (width != gdk_screen_get_width (screen) ||
1227       height != gdk_screen_get_height (screen))
1228     g_signal_emit_by_name (screen, "size-changed");
1229 }
1230
1231 void
1232 _gdk_x11_screen_window_manager_changed (GdkScreen *screen)
1233 {
1234   g_signal_emit (screen, signals[WINDOW_MANAGER_CHANGED], 0);
1235 }
1236
1237 void
1238 _gdk_x11_screen_process_owner_change (GdkScreen *screen,
1239                                       XEvent *event)
1240 {
1241 #ifdef HAVE_XFIXES
1242   XFixesSelectionNotifyEvent *selection_event = (XFixesSelectionNotifyEvent *)event;
1243   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1244   Atom xcm_selection_atom = gdk_x11_atom_to_xatom_for_display (screen_x11->display,
1245                                                                screen_x11->cm_selection_atom);
1246
1247   if (selection_event->selection == xcm_selection_atom)
1248     {
1249       gboolean composited = selection_event->owner != None;
1250
1251       if (composited != screen_x11->is_composited)
1252         {
1253           screen_x11->is_composited = composited;
1254
1255           g_signal_emit_by_name (screen, "composited-changed");
1256         }
1257     }
1258 #endif
1259 }
1260
1261 /**
1262  * _gdk_windowing_substitute_screen_number:
1263  * @display_name: The name of a display, in the form used by 
1264  *                gdk_display_open (). If %NULL a default value
1265  *                will be used. On X11, this is derived from the DISPLAY
1266  *                environment variable.
1267  * @screen_number: The number of a screen within the display
1268  *                 referred to by @display_name.
1269  *
1270  * Modifies a @display_name to make @screen_number the default
1271  * screen when the display is opened.
1272  *
1273  * Return value: a newly allocated string holding the resulting
1274  *   display name. Free with g_free().
1275  */
1276 gchar * 
1277 _gdk_windowing_substitute_screen_number (const gchar *display_name,
1278                                          gint         screen_number)
1279 {
1280   GString *str;
1281   gchar   *p;
1282
1283   if (!display_name)
1284     display_name = getenv ("DISPLAY");
1285
1286   if (!display_name)
1287     return NULL;
1288
1289   str = g_string_new (display_name);
1290
1291   p = strrchr (str->str, '.');
1292   if (p && p >  strchr (str->str, ':'))
1293     g_string_truncate (str, p - str->str);
1294
1295   g_string_append_printf (str, ".%d", screen_number);
1296
1297   return g_string_free (str, FALSE);
1298 }
1299
1300 /**
1301  * gdk_screen_make_display_name:
1302  * @screen: a #GdkScreen
1303  * 
1304  * Determines the name to pass to gdk_display_open() to get
1305  * a #GdkDisplay with this screen as the default screen.
1306  * 
1307  * Return value: a newly allocated string, free with g_free()
1308  *
1309  * Since: 2.2
1310  **/
1311 gchar *
1312 gdk_screen_make_display_name (GdkScreen *screen)
1313 {
1314   const gchar *old_display;
1315
1316   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1317
1318   old_display = gdk_display_get_name (gdk_screen_get_display (screen));
1319
1320   return _gdk_windowing_substitute_screen_number (old_display, 
1321                                                   gdk_screen_get_number (screen));
1322 }
1323
1324 /**
1325  * gdk_screen_get_active_window
1326  * @screen: a #GdkScreen
1327  *
1328  * Returns the screen's currently active window.
1329  *
1330  * On X11, this is done by inspecting the _NET_ACTIVE_WINDOW property
1331  * on the root window, as described in the <ulink
1332  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1333  * Manager Hints</ulink>. If there is no currently currently active
1334  * window, or the window manager does not support the
1335  * _NET_ACTIVE_WINDOW hint, this function returns %NULL.
1336  *
1337  * On other platforms, this function may return %NULL, depending on whether
1338  * it is implementable on that platform.
1339  *
1340  * The returned window should be unrefed using g_object_unref() when
1341  * no longer needed.
1342  *
1343  * Return value: the currently active window, or %NULL.
1344  *
1345  * Since: 2.10
1346  **/
1347 GdkWindow *
1348 gdk_screen_get_active_window (GdkScreen *screen)
1349 {
1350   GdkScreenX11 *screen_x11;
1351   GdkWindow *ret = NULL;
1352   Atom type_return;
1353   gint format_return;
1354   gulong nitems_return;
1355   gulong bytes_after_return;
1356   guchar *data = NULL;
1357
1358   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1359
1360   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1361                                             gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
1362     return NULL;
1363
1364   screen_x11 = GDK_SCREEN_X11 (screen);
1365
1366   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1367                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1368                                                                  "_NET_ACTIVE_WINDOW"),
1369                           0, 1, False, XA_WINDOW, &type_return,
1370                           &format_return, &nitems_return,
1371                           &bytes_after_return, &data)
1372       == Success)
1373     {
1374       if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
1375         {
1376           GdkNativeWindow window = *(GdkNativeWindow *) data;
1377
1378           if (window != None)
1379             {
1380               ret = gdk_window_foreign_new_for_display (screen_x11->display,
1381                                                         *(GdkNativeWindow *) data);
1382             }
1383         }
1384     }
1385
1386   if (data)
1387     XFree (data);
1388
1389   return ret;
1390 }
1391
1392 /**
1393  * gdk_screen_get_window_stack:
1394  * @screen: a #GdkScreen
1395  *
1396  * Returns a #GList of #GdkWindow<!-- -->s representing the current
1397  * window stack.
1398  *
1399  * On X11, this is done by inspecting the _NET_CLIENT_LIST_STACKING
1400  * property on the root window, as described in the <ulink
1401  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1402  * Manager Hints</ulink>. If the window manager does not support the
1403  * _NET_CLIENT_LIST_STACKING hint, this function returns %NULL.
1404  *
1405  * On other platforms, this function may return %NULL, depending on whether
1406  * it is implementable on that platform.
1407  *
1408  * The returned list is newly allocated and owns references to the
1409  * windows it contains, so it should be freed using g_list_free() and
1410  * its windows unrefed using g_object_unref() when no longer needed.
1411  *
1412  * Return value: (transfer full) (element-type GdkWindow):
1413  *     a list of #GdkWindow<!-- -->s for the current window stack,
1414  *               or %NULL.
1415  *
1416  * Since: 2.10
1417  **/
1418 GList *
1419 gdk_screen_get_window_stack (GdkScreen *screen)
1420 {
1421   GdkScreenX11 *screen_x11;
1422   GList *ret = NULL;
1423   Atom type_return;
1424   gint format_return;
1425   gulong nitems_return;
1426   gulong bytes_after_return;
1427   guchar *data = NULL;
1428
1429   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1430
1431   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1432                                             gdk_atom_intern_static_string ("_NET_CLIENT_LIST_STACKING")))
1433     return NULL;
1434
1435   screen_x11 = GDK_SCREEN_X11 (screen);
1436
1437   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1438                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1439                                                                  "_NET_CLIENT_LIST_STACKING"),
1440                           0, G_MAXLONG, False, XA_WINDOW, &type_return,
1441                           &format_return, &nitems_return,
1442                           &bytes_after_return, &data)
1443       == Success)
1444     {
1445       if ((type_return == XA_WINDOW) && (format_return == 32) &&
1446           (data) && (nitems_return > 0))
1447         {
1448           gulong *stack = (gulong *) data;
1449           GdkWindow *win;
1450           int i;
1451
1452           for (i = 0; i < nitems_return; i++)
1453             {
1454               win = gdk_window_foreign_new_for_display (screen_x11->display,
1455                                                         (GdkNativeWindow)stack[i]);
1456
1457               if (win != NULL)
1458                 ret = g_list_append (ret, win);
1459             }
1460         }
1461     }
1462
1463   if (data)
1464     XFree (data);
1465
1466   return ret;
1467 }
1468
1469 /* Sends a ClientMessage to all toplevel client windows */
1470 static gboolean
1471 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
1472                                               XEvent     *xev,
1473                                               guint32     xid,
1474                                               guint       level)
1475 {
1476   Atom type = None;
1477   int format;
1478   unsigned long nitems, after;
1479   unsigned char *data;
1480   Window *ret_children, ret_root, ret_parent;
1481   unsigned int ret_nchildren;
1482   gboolean send = FALSE;
1483   gboolean found = FALSE;
1484   gboolean result = FALSE;
1485   int i;
1486
1487   gdk_error_trap_push ();
1488
1489   if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
1490                           gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
1491                           0, 0, False, AnyPropertyType,
1492                           &type, &format, &nitems, &after, &data) != Success)
1493     goto out;
1494
1495   if (type)
1496     {
1497       send = TRUE;
1498       XFree (data);
1499     }
1500   else
1501     {
1502       /* OK, we're all set, now let's find some windows to send this to */
1503       if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
1504                       &ret_root, &ret_parent,
1505                       &ret_children, &ret_nchildren))
1506         goto out;
1507
1508       for(i = 0; i < ret_nchildren; i++)
1509         if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
1510           found = TRUE;
1511
1512       XFree (ret_children);
1513     }
1514
1515   if (send || (!found && (level == 1)))
1516     {
1517       xev->xclient.window = xid;
1518       _gdk_send_xevent (display, xid, False, NoEventMask, xev);
1519     }
1520
1521   result = send || found;
1522
1523  out:
1524   gdk_error_trap_pop_ignored ();
1525
1526   return result;
1527 }
1528
1529 /**
1530  * gdk_screen_broadcast_client_message:
1531  * @screen: the #GdkScreen where the event will be broadcasted.
1532  * @event: the #GdkEvent.
1533  *
1534  * On X11, sends an X ClientMessage event to all toplevel windows on
1535  * @screen.
1536  *
1537  * Toplevel windows are determined by checking for the WM_STATE property,
1538  * as described in the Inter-Client Communication Conventions Manual (ICCCM).
1539  * If no windows are found with the WM_STATE property set, the message is
1540  * sent to all children of the root window.
1541  *
1542  * On Windows, broadcasts a message registered with the name
1543  * GDK_WIN32_CLIENT_MESSAGE to all top-level windows. The amount of
1544  * data is limited to one long, i.e. four bytes.
1545  *
1546  * Since: 2.2
1547  */
1548
1549 void
1550 gdk_screen_broadcast_client_message (GdkScreen *screen,
1551                                      GdkEvent  *event)
1552 {
1553   XEvent sev;
1554   GdkWindow *root_window;
1555
1556   g_return_if_fail (event != NULL);
1557
1558   root_window = gdk_screen_get_root_window (screen);
1559
1560   /* Set up our event to send, with the exception of its target window */
1561   sev.xclient.type = ClientMessage;
1562   sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
1563   sev.xclient.format = event->client.data_format;
1564   memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
1565   sev.xclient.message_type =
1566     gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
1567                                        event->client.message_type);
1568
1569   gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
1570                                                 &sev,
1571                                                 GDK_WINDOW_XID (root_window),
1572                                                 0);
1573 }
1574
1575 static gboolean
1576 check_transform (const gchar *xsettings_name,
1577                  GType        src_type,
1578                  GType        dest_type)
1579 {
1580   if (!g_value_type_transformable (src_type, dest_type))
1581     {
1582       g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
1583                  xsettings_name,
1584                  g_type_name (src_type),
1585                  g_type_name (dest_type));
1586       return FALSE;
1587     }
1588   else
1589     return TRUE;
1590 }
1591
1592 /**
1593  * gdk_screen_get_setting:
1594  * @screen: the #GdkScreen where the setting is located
1595  * @name: the name of the setting
1596  * @value: location to store the value of the setting
1597  *
1598  * Retrieves a desktop-wide setting such as double-click time
1599  * for the #GdkScreen @screen.
1600  *
1601  * FIXME needs a list of valid settings here, or a link to
1602  * more information.
1603  *
1604  * Returns: %TRUE if the setting existed and a value was stored
1605  *   in @value, %FALSE otherwise.
1606  *
1607  * Since: 2.2
1608  **/
1609 gboolean
1610 gdk_screen_get_setting (GdkScreen   *screen,
1611                         const gchar *name,
1612                         GValue      *value)
1613 {
1614
1615   const char *xsettings_name = NULL;
1616   XSettingsResult result;
1617   XSettingsSetting *setting = NULL;
1618   GdkScreenX11 *screen_x11;
1619   gboolean success = FALSE;
1620   gint i;
1621   GValue tmp_val = { 0, };
1622
1623   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
1624
1625   screen_x11 = GDK_SCREEN_X11 (screen);
1626
1627   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS(); i++)
1628     if (strcmp (GDK_SETTINGS_GDK_NAME (i), name) == 0)
1629       {
1630         xsettings_name = GDK_SETTINGS_X_NAME (i);
1631         break;
1632       }
1633
1634   if (!xsettings_name)
1635     goto out;
1636
1637   result = xsettings_client_get_setting (screen_x11->xsettings_client,
1638                                          xsettings_name, &setting);
1639   if (result != XSETTINGS_SUCCESS)
1640     goto out;
1641
1642   switch (setting->type)
1643     {
1644     case XSETTINGS_TYPE_INT:
1645       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
1646         {
1647           g_value_init (&tmp_val, G_TYPE_INT);
1648           g_value_set_int (&tmp_val, setting->data.v_int);
1649           g_value_transform (&tmp_val, value);
1650
1651           success = TRUE;
1652         }
1653       break;
1654     case XSETTINGS_TYPE_STRING:
1655       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
1656         {
1657           g_value_init (&tmp_val, G_TYPE_STRING);
1658           g_value_set_string (&tmp_val, setting->data.v_string);
1659           g_value_transform (&tmp_val, value);
1660
1661           success = TRUE;
1662         }
1663       break;
1664     case XSETTINGS_TYPE_COLOR:
1665       if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
1666         {
1667           GdkColor color;
1668
1669           g_value_init (&tmp_val, GDK_TYPE_COLOR);
1670
1671           color.pixel = 0;
1672           color.red = setting->data.v_color.red;
1673           color.green = setting->data.v_color.green;
1674           color.blue = setting->data.v_color.blue;
1675
1676           g_value_set_boxed (&tmp_val, &color);
1677
1678           g_value_transform (&tmp_val, value);
1679
1680           success = TRUE;
1681         }
1682       break;
1683     }
1684
1685   g_value_unset (&tmp_val);
1686
1687  out:
1688   if (setting)
1689     xsettings_setting_free (setting);
1690
1691   if (success)
1692     return TRUE;
1693   else
1694     return _gdk_x11_get_xft_setting (screen, name, value);
1695 }
1696
1697 static void
1698 cleanup_atoms(gpointer data)
1699 {
1700   NetWmSupportedAtoms *supported_atoms = data;
1701   if (supported_atoms->atoms)
1702       XFree (supported_atoms->atoms);
1703   g_free (supported_atoms);
1704 }
1705
1706 static void
1707 fetch_net_wm_check_window (GdkScreen *screen)
1708 {
1709   GdkScreenX11 *screen_x11;
1710   GdkDisplay *display;
1711   Atom type;
1712   gint format;
1713   gulong n_items;
1714   gulong bytes_after;
1715   guchar *data;
1716   Window *xwindow;
1717   GTimeVal tv;
1718   gint error;
1719
1720   screen_x11 = GDK_SCREEN_X11 (screen);
1721   display = screen_x11->display;
1722
1723   g_return_if_fail (GDK_DISPLAY_X11 (display)->trusted_client);
1724   
1725   g_get_current_time (&tv);
1726
1727   if (ABS  (tv.tv_sec - screen_x11->last_wmspec_check_time) < 15)
1728     return; /* we've checked recently */
1729
1730   screen_x11->last_wmspec_check_time = tv.tv_sec;
1731
1732   data = NULL;
1733   XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1734                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
1735                       0, G_MAXLONG, False, XA_WINDOW, &type, &format,
1736                       &n_items, &bytes_after, &data);
1737   
1738   if (type != XA_WINDOW)
1739     {
1740       if (data)
1741         XFree (data);
1742       return;
1743     }
1744
1745   xwindow = (Window *)data;
1746
1747   if (screen_x11->wmspec_check_window == *xwindow)
1748     {
1749       XFree (xwindow);
1750       return;
1751     }
1752
1753   gdk_error_trap_push ();
1754
1755   /* Find out if this WM goes away, so we can reset everything. */
1756   XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
1757
1758   error = gdk_error_trap_pop ();
1759   if (!error)
1760     {
1761       screen_x11->wmspec_check_window = *xwindow;
1762       screen_x11->need_refetch_net_supported = TRUE;
1763       screen_x11->need_refetch_wm_name = TRUE;
1764
1765       /* Careful, reentrancy */
1766       _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
1767     }
1768   else if (error == BadWindow)
1769     {
1770       /* Leftover property, try again immediately, new wm may be starting up */
1771       screen_x11->last_wmspec_check_time = 0;
1772     }
1773
1774   XFree (xwindow);
1775 }
1776
1777 /**
1778  * gdk_x11_screen_supports_net_wm_hint:
1779  * @screen: the relevant #GdkScreen.
1780  * @property: a property atom.
1781  *
1782  * This function is specific to the X11 backend of GDK, and indicates
1783  * whether the window manager supports a certain hint from the
1784  * Extended Window Manager Hints Specification. You can find this
1785  * specification on
1786  * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
1787  *
1788  * When using this function, keep in mind that the window manager
1789  * can change over time; so you shouldn't use this function in
1790  * a way that impacts persistent application state. A common bug
1791  * is that your application can start up before the window manager
1792  * does when the user logs in, and before the window manager starts
1793  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
1794  * You can monitor the window_manager_changed signal on #GdkScreen to detect
1795  * a window manager change.
1796  *
1797  * Return value: %TRUE if the window manager supports @property
1798  *
1799  * Since: 2.2
1800  **/
1801 gboolean
1802 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
1803                                      GdkAtom    property)
1804 {
1805   gulong i;
1806   GdkScreenX11 *screen_x11;
1807   NetWmSupportedAtoms *supported_atoms;
1808   GdkDisplay *display;
1809
1810   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
1811
1812   screen_x11 = GDK_SCREEN_X11 (screen);
1813   display = screen_x11->display;
1814
1815   if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
1816     return FALSE;
1817
1818   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
1819   if (!supported_atoms)
1820     {
1821       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
1822       g_object_set_data_full (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms, cleanup_atoms);
1823     }
1824
1825   fetch_net_wm_check_window (screen);
1826
1827   if (screen_x11->wmspec_check_window == None)
1828     return FALSE;
1829
1830   if (screen_x11->need_refetch_net_supported)
1831     {
1832       /* WM has changed since we last got the supported list,
1833        * refetch it.
1834        */
1835       Atom type;
1836       gint format;
1837       gulong bytes_after;
1838
1839       screen_x11->need_refetch_net_supported = FALSE;
1840
1841       if (supported_atoms->atoms)
1842         XFree (supported_atoms->atoms);
1843
1844       supported_atoms->atoms = NULL;
1845       supported_atoms->n_atoms = 0;
1846
1847       XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
1848                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
1849                           0, G_MAXLONG, False, XA_ATOM, &type, &format,
1850                           &supported_atoms->n_atoms, &bytes_after,
1851                           (guchar **)&supported_atoms->atoms);
1852
1853       if (type != XA_ATOM)
1854         return FALSE;
1855     }
1856
1857   if (supported_atoms->atoms == NULL)
1858     return FALSE;
1859
1860   i = 0;
1861   while (i < supported_atoms->n_atoms)
1862     {
1863       if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
1864         return TRUE;
1865
1866       ++i;
1867     }
1868
1869   return FALSE;
1870 }
1871
1872 /**
1873  * gdk_net_wm_supports:
1874  * @property: a property atom.
1875  *
1876  * This function is specific to the X11 backend of GDK, and indicates
1877  * whether the window manager for the default screen supports a certain
1878  * hint from the Extended Window Manager Hints Specification. See
1879  * gdk_x11_screen_supports_net_wm_hint() for complete details.
1880  *
1881  * Return value: %TRUE if the window manager supports @property
1882  **/
1883 gboolean
1884 gdk_net_wm_supports (GdkAtom property)
1885 {
1886   return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
1887 }
1888
1889 static void
1890 refcounted_grab_server (Display *xdisplay)
1891 {
1892   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
1893
1894   gdk_x11_display_grab (display);
1895 }
1896
1897 static void
1898 refcounted_ungrab_server (Display *xdisplay)
1899 {
1900   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
1901
1902   gdk_x11_display_ungrab (display);
1903 }
1904
1905 static GdkFilterReturn
1906 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
1907                                    GdkEvent  *event,
1908                                    gpointer   data)
1909 {
1910   GdkScreenX11 *screen = data;
1911
1912   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
1913     return GDK_FILTER_REMOVE;
1914   else
1915     return GDK_FILTER_CONTINUE;
1916 }
1917
1918 static Bool
1919 gdk_xsettings_watch_cb (Window   window,
1920                         Bool     is_start,
1921                         long     mask,
1922                         void    *cb_data)
1923 {
1924   GdkWindow *gdkwin;
1925   GdkScreen *screen = cb_data;
1926
1927   gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
1928
1929   if (is_start)
1930     {
1931       if (gdkwin)
1932         g_object_ref (gdkwin);
1933       else
1934         {
1935           gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
1936           
1937           /* gdk_window_foreign_new_for_display() can fail and return NULL if the
1938            * window has already been destroyed.
1939            */
1940           if (!gdkwin)
1941             return False;
1942         }
1943
1944       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
1945     }
1946   else
1947     {
1948       if (!gdkwin)
1949         {
1950           /* gdkwin should not be NULL here, since if starting the watch succeeded
1951            * we have a reference on the window. It might mean that the caller didn't
1952            * remove the watch when it got a DestroyNotify event. Or maybe the
1953            * caller ignored the return value when starting the watch failed.
1954            */
1955           g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
1956           return False;
1957         }
1958       
1959       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
1960       g_object_unref (gdkwin);
1961     }
1962
1963   return True;
1964 }
1965
1966 static void
1967 gdk_xsettings_notify_cb (const char       *name,
1968                          XSettingsAction   action,
1969                          XSettingsSetting *setting,
1970                          void             *data)
1971 {
1972   GdkEvent new_event;
1973   GdkScreen *screen = data;
1974   GdkScreenX11 *screen_x11 = data;
1975   int i;
1976
1977   if (screen_x11->xsettings_in_init)
1978     return;
1979   
1980   new_event.type = GDK_SETTING;
1981   new_event.setting.window = gdk_screen_get_root_window (screen);
1982   new_event.setting.send_event = FALSE;
1983   new_event.setting.name = NULL;
1984
1985   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS() ; i++)
1986     if (strcmp (GDK_SETTINGS_X_NAME (i), name) == 0)
1987       {
1988         new_event.setting.name = (char*) GDK_SETTINGS_GDK_NAME (i);
1989         break;
1990       }
1991   
1992   if (!new_event.setting.name)
1993     return;
1994   
1995   switch (action)
1996     {
1997     case XSETTINGS_ACTION_NEW:
1998       new_event.setting.action = GDK_SETTING_ACTION_NEW;
1999       break;
2000     case XSETTINGS_ACTION_CHANGED:
2001       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2002       break;
2003     case XSETTINGS_ACTION_DELETED:
2004       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2005       break;
2006     }
2007
2008   gdk_event_put (&new_event);
2009 }
2010
2011 void
2012 _gdk_screen_x11_events_init (GdkScreen *screen)
2013 {
2014   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
2015
2016   /* Keep a flag to avoid extra notifies that we don't need
2017    */
2018   screen_x11->xsettings_in_init = TRUE;
2019   screen_x11->xsettings_client = xsettings_client_new_with_grab_funcs (screen_x11->xdisplay,
2020                                                                        screen_x11->screen_num,
2021                                                                        gdk_xsettings_notify_cb,
2022                                                                        gdk_xsettings_watch_cb,
2023                                                                        screen,
2024                                                                        refcounted_grab_server,
2025                                                                        refcounted_ungrab_server);
2026   screen_x11->xsettings_in_init = FALSE;
2027 }
2028
2029 /**
2030  * gdk_x11_screen_get_window_manager_name:
2031  * @screen: a #GdkScreen
2032  *
2033  * Returns the name of the window manager for @screen.
2034  *
2035  * Return value: the name of the window manager screen @screen, or
2036  * "unknown" if the window manager is unknown. The string is owned by GDK
2037  * and should not be freed.
2038  *
2039  * Since: 2.2
2040  **/
2041 const char*
2042 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2043 {
2044   GdkScreenX11 *screen_x11;
2045
2046   screen_x11 = GDK_SCREEN_X11 (screen);
2047
2048   if (!G_LIKELY (GDK_DISPLAY_X11 (screen_x11->display)->trusted_client))
2049     return screen_x11->window_manager_name;
2050
2051   fetch_net_wm_check_window (screen);
2052
2053   if (screen_x11->need_refetch_wm_name)
2054     {
2055       /* Get the name of the window manager */
2056       screen_x11->need_refetch_wm_name = FALSE;
2057
2058       g_free (screen_x11->window_manager_name);
2059       screen_x11->window_manager_name = g_strdup ("unknown");
2060
2061       if (screen_x11->wmspec_check_window != None)
2062         {
2063           Atom type;
2064           gint format;
2065           gulong n_items;
2066           gulong bytes_after;
2067           gchar *name;
2068
2069           name = NULL;
2070
2071           gdk_error_trap_push ();
2072
2073           XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2074                               screen_x11->wmspec_check_window,
2075                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2076                                                                      "_NET_WM_NAME"),
2077                               0, G_MAXLONG, False,
2078                               gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2079                                                                      "UTF8_STRING"),
2080                               &type, &format,
2081                               &n_items, &bytes_after,
2082                               (guchar **)&name);
2083
2084           gdk_error_trap_pop_ignored ();
2085
2086           if (name != NULL)
2087             {
2088               g_free (screen_x11->window_manager_name);
2089               screen_x11->window_manager_name = g_strdup (name);
2090               XFree (name);
2091             }
2092         }
2093     }
2094
2095   return GDK_SCREEN_X11 (screen)->window_manager_name;
2096 }