]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkscreen-x11.c
After using randr to get a list of monitors, sort the list such that the
[~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 #include "gdkalias.h"
36
37 #include <X11/Xatom.h>
38
39 #ifdef HAVE_SOLARIS_XINERAMA
40 #include <X11/extensions/xinerama.h>
41 #endif
42 #ifdef HAVE_XFREE_XINERAMA
43 #include <X11/extensions/Xinerama.h>
44 #endif
45
46 #ifdef HAVE_RANDR
47 #include <X11/extensions/Xrandr.h>
48 #endif
49
50 #ifdef HAVE_XFIXES
51 #include <X11/extensions/Xfixes.h>
52 #endif
53
54 static void         gdk_screen_x11_dispose     (GObject           *object);
55 static void         gdk_screen_x11_finalize    (GObject           *object);
56 static void         init_randr_support         (GdkScreen         *screen);
57 static void         deinit_multihead           (GdkScreen         *screen);
58
59 enum
60 {
61   WINDOW_MANAGER_CHANGED,
62   LAST_SIGNAL
63 };
64
65 static guint signals[LAST_SIGNAL] = { 0 };
66
67 G_DEFINE_TYPE (GdkScreenX11, _gdk_screen_x11, GDK_TYPE_SCREEN)
68
69 struct _GdkX11Monitor
70 {
71   GdkRectangle  geometry;
72   XID           output;
73   int           width_mm;
74   int           height_mm;
75   char *        output_name;
76   char *        manufacturer;
77 };
78
79 static void
80 _gdk_screen_x11_class_init (GdkScreenX11Class *klass)
81 {
82   GObjectClass *object_class = G_OBJECT_CLASS (klass);
83   
84   object_class->dispose = gdk_screen_x11_dispose;
85   object_class->finalize = gdk_screen_x11_finalize;
86
87   signals[WINDOW_MANAGER_CHANGED] =
88     g_signal_new (g_intern_static_string ("window_manager_changed"),
89                   G_OBJECT_CLASS_TYPE (object_class),
90                   G_SIGNAL_RUN_LAST,
91                   G_STRUCT_OFFSET (GdkScreenX11Class, window_manager_changed),
92                   NULL, NULL,
93                   g_cclosure_marshal_VOID__VOID,
94                   G_TYPE_NONE,
95                   0);
96 }
97
98 static void
99 _gdk_screen_x11_init (GdkScreenX11 *screen)
100 {
101 }
102
103 /**
104  * gdk_screen_get_display:
105  * @screen: a #GdkScreen
106  *
107  * Gets the display to which the @screen belongs.
108  * 
109  * Returns: the display to which @screen belongs
110  *
111  * Since: 2.2
112  **/
113 GdkDisplay *
114 gdk_screen_get_display (GdkScreen *screen)
115 {
116   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
117
118   return GDK_SCREEN_X11 (screen)->display;
119 }
120 /**
121  * gdk_screen_get_width:
122  * @screen: a #GdkScreen
123  *
124  * Gets the width of @screen in pixels
125  * 
126  * Returns: the width of @screen in pixels.
127  *
128  * Since: 2.2
129  **/
130 gint
131 gdk_screen_get_width (GdkScreen *screen)
132 {
133   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
134
135   return WidthOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
136 }
137
138 /**
139  * gdk_screen_get_height:
140  * @screen: a #GdkScreen
141  *
142  * Gets the height of @screen in pixels
143  * 
144  * Returns: the height of @screen in pixels.
145  *
146  * Since: 2.2
147  **/
148 gint
149 gdk_screen_get_height (GdkScreen *screen)
150 {
151   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
152
153   return HeightOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
154 }
155
156 /**
157  * gdk_screen_get_width_mm:
158  * @screen: a #GdkScreen
159  *
160  * Gets the width of @screen in millimeters. 
161  * Note that on some X servers this value will not be correct.
162  * 
163  * Returns: the width of @screen in millimeters.
164  *
165  * Since: 2.2
166  **/
167 gint
168 gdk_screen_get_width_mm (GdkScreen *screen)
169 {
170   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);  
171
172   return WidthMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
173 }
174
175 /**
176  * gdk_screen_get_height_mm:
177  * @screen: a #GdkScreen
178  *
179  * Returns the height of @screen in millimeters. 
180  * Note that on some X servers this value will not be correct.
181  * 
182  * Returns: the heigth of @screen in millimeters.
183  *
184  * Since: 2.2
185  **/
186 gint
187 gdk_screen_get_height_mm (GdkScreen *screen)
188 {
189   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
190
191   return HeightMMOfScreen (GDK_SCREEN_X11 (screen)->xscreen);
192 }
193
194 /**
195  * gdk_screen_get_number:
196  * @screen: a #GdkScreen
197  *
198  * Gets the index of @screen among the screens in the display
199  * to which it belongs. (See gdk_screen_get_display())
200  * 
201  * Returns: the index
202  *
203  * Since: 2.2
204  **/
205 gint
206 gdk_screen_get_number (GdkScreen *screen)
207 {
208   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);  
209   
210   return GDK_SCREEN_X11 (screen)->screen_num;
211 }
212
213 /**
214  * gdk_screen_get_root_window:
215  * @screen: a #GdkScreen
216  *
217  * Gets the root window of @screen. 
218  * 
219  * Returns: the root window
220  *
221  * Since: 2.2
222  **/
223 GdkWindow *
224 gdk_screen_get_root_window (GdkScreen *screen)
225 {
226   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
227
228   return GDK_SCREEN_X11 (screen)->root_window;
229 }
230
231 /**
232  * gdk_screen_get_default_colormap:
233  * @screen: a #GdkScreen
234  *
235  * Gets the default colormap for @screen.
236  * 
237  * Returns: the default #GdkColormap.
238  *
239  * Since: 2.2
240  **/
241 GdkColormap *
242 gdk_screen_get_default_colormap (GdkScreen *screen)
243 {
244   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
245
246   return GDK_SCREEN_X11 (screen)->default_colormap;
247 }
248
249 /**
250  * gdk_screen_set_default_colormap:
251  * @screen: a #GdkScreen
252  * @colormap: a #GdkColormap
253  *
254  * Sets the default @colormap for @screen.
255  *
256  * Since: 2.2
257  **/
258 void
259 gdk_screen_set_default_colormap (GdkScreen   *screen,
260                                  GdkColormap *colormap)
261 {
262   GdkColormap *old_colormap;
263   
264   g_return_if_fail (GDK_IS_SCREEN (screen));
265   g_return_if_fail (GDK_IS_COLORMAP (colormap));
266
267   old_colormap = GDK_SCREEN_X11 (screen)->default_colormap;
268
269   GDK_SCREEN_X11 (screen)->default_colormap = g_object_ref (colormap);
270   
271   if (old_colormap)
272     g_object_unref (old_colormap);
273 }
274
275 static void
276 gdk_screen_x11_dispose (GObject *object)
277 {
278   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
279
280   _gdk_x11_events_uninit_screen (GDK_SCREEN (object));
281
282   if (screen_x11->default_colormap)
283     {
284       g_object_unref (screen_x11->default_colormap);
285       screen_x11->default_colormap = NULL;
286     }
287
288   if (screen_x11->system_colormap)
289     {
290       g_object_unref (screen_x11->system_colormap);
291       screen_x11->system_colormap = NULL;
292     }
293
294   if (screen_x11->rgba_colormap)
295     {
296       g_object_unref (screen_x11->rgba_colormap);
297       screen_x11->rgba_colormap = NULL;
298     }
299
300   if (screen_x11->root_window)
301     _gdk_window_destroy (screen_x11->root_window, TRUE);
302
303   G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->dispose (object);
304
305   screen_x11->xdisplay = NULL;
306   screen_x11->xscreen = NULL;
307   screen_x11->screen_num = -1;
308   screen_x11->xroot_window = None;
309   screen_x11->wmspec_check_window = None;
310 }
311
312 static void
313 gdk_screen_x11_finalize (GObject *object)
314 {
315   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (object);
316   gint          i;
317
318   if (screen_x11->root_window)
319     g_object_unref (screen_x11->root_window);
320
321   if (screen_x11->renderer)
322     g_object_unref (screen_x11->renderer);
323
324   /* Visual Part */
325   for (i = 0; i < screen_x11->nvisuals; i++)
326     g_object_unref (screen_x11->visuals[i]);
327   g_free (screen_x11->visuals);
328   g_hash_table_destroy (screen_x11->visual_hash);
329
330   g_free (screen_x11->window_manager_name);
331
332   g_hash_table_destroy (screen_x11->colormap_hash);
333
334   deinit_multihead (GDK_SCREEN (object));
335   
336   G_OBJECT_CLASS (_gdk_screen_x11_parent_class)->finalize (object);
337 }
338
339 /**
340  * gdk_screen_get_n_monitors:
341  * @screen: a #GdkScreen.
342  *
343  * Returns the number of monitors which @screen consists of.
344  *
345  * Returns: number of monitors which @screen consists of.
346  *
347  * Since: 2.2
348  **/
349 gint 
350 gdk_screen_get_n_monitors (GdkScreen *screen)
351 {
352   g_return_val_if_fail (GDK_IS_SCREEN (screen), 0);
353   
354   return GDK_SCREEN_X11 (screen)->n_monitors;
355 }
356
357 static GdkX11Monitor *
358 get_monitor (GdkScreen *screen,
359              int        monitor_num)
360 {
361   GdkScreenX11 *screen_x11;
362
363   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
364   
365   screen_x11 = GDK_SCREEN_X11 (screen);
366   
367   g_return_val_if_fail (monitor_num < screen_x11->n_monitors, NULL);
368   g_return_val_if_fail (monitor_num >= 0, NULL);
369   
370   return &(screen_x11->monitors[monitor_num]);
371 }
372
373 /**
374  * gdk_screen_get_monitor_width_mm:
375  * @screen: a #GdkScreen
376  * @monitor_num: number of the monitor
377  *
378  * Gets the width in millimeters of the specified monitor, if available.
379  *
380  * Returns the width of the monitor, or -1 if not available
381  *
382  * Since: 2.14
383  */
384 gint
385 gdk_screen_get_monitor_width_mm (GdkScreen *screen,
386                                  gint       monitor_num)
387 {
388   return get_monitor (screen, monitor_num)->width_mm;
389 }
390
391 /**
392  * gdk_screen_get_monitor_height_mm:
393  * @screen: a #GdkScreen
394  * @monitor_num: number of the monitor
395  *
396  * Gets the height in millimeters of the specified monitor. 
397  *
398  * Returns: the height of the monitor, or -1 if not available
399  *
400  * Since: 2.14
401  */
402 gint
403 gdk_screen_get_monitor_height_mm (GdkScreen *screen,
404                                   gint       monitor_num)
405 {
406   return get_monitor (screen, monitor_num)->height_mm;
407 }
408
409 /**
410  * gdk_screen_get_monitor_plug_name:
411  * @screen: a #GdkScreen
412  * @monitor_num: number of the monitor
413  *
414  * Returns the output name of the specified monitor. 
415  * Usually something like VGA, DVI, or TV, not the actual
416  * product name of the display device.
417  * 
418  * Returns: a newly-allocated string containing the name of the monitor,
419  *   or %NULL if the name cannot be determined
420  *
421  * Since: 2.14
422  */
423 gchar *
424 gdk_screen_get_monitor_plug_name (GdkScreen *screen,
425                                   gint       monitor_num)
426 {
427   return g_strdup (get_monitor (screen, monitor_num)->output_name);
428 }
429
430 /**
431  * gdk_x11_screen_get_monitor_output:
432  * @screen: a #GdkScreen
433  * @monitor_num: number of the monitor 
434  *
435  * Gets the XID of the specified output/monitor.
436  * If the X server does not support version 1.2 of the RANDR 
437  * extension, 0 is returned.
438  *
439  * Returns: the XID of the monitor
440  *
441  * Since: 2.14
442  */
443 XID
444 gdk_x11_screen_get_monitor_output (GdkScreen *screen,
445                                    gint       monitor_num)
446 {
447   return get_monitor (screen, monitor_num)->output;
448 }
449
450 /**
451  * gdk_screen_get_monitor_geometry:
452  * @screen : a #GdkScreen.
453  * @monitor_num: the monitor number. 
454  * @dest : a #GdkRectangle to be filled with the monitor geometry
455  *
456  * Retrieves the #GdkRectangle representing the size and position of 
457  * the individual monitor within the entire screen area.
458  * 
459  * Note that the size of the entire screen area can be retrieved via 
460  * gdk_screen_get_width() and gdk_screen_get_height().
461  *
462  * Since: 2.2
463  **/
464 void 
465 gdk_screen_get_monitor_geometry (GdkScreen    *screen,
466                                  gint          monitor_num,
467                                  GdkRectangle *dest)
468 {
469   if (dest) 
470     {
471       GdkX11Monitor *monitor = get_monitor (screen, monitor_num);
472
473       *dest = monitor->geometry;
474     }
475 }
476
477 /**
478  * gdk_screen_get_rgba_colormap:
479  * @screen: a #GdkScreen.
480  * 
481  * Gets a colormap to use for creating windows or pixmaps with an
482  * alpha channel. The windowing system on which GTK+ is running
483  * may not support this capability, in which case %NULL will
484  * be returned. Even if a non-%NULL value is returned, its
485  * possible that the window's alpha channel won't be honored
486  * when displaying the window on the screen: in particular, for
487  * X an appropriate windowing manager and compositing manager
488  * must be running to provide appropriate display.
489  *
490  * This functionality is not implemented in the Windows backend.
491  *
492  * For setting an overall opacity for a top-level window, see
493  * gdk_window_set_opacity().
494
495  * Return value: a colormap to use for windows with an alpha channel
496  *   or %NULL if the capability is not available.
497  *
498  * Since: 2.8
499  **/
500 GdkColormap *
501 gdk_screen_get_rgba_colormap (GdkScreen *screen)
502 {
503   GdkScreenX11 *screen_x11;
504
505   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
506
507   screen_x11 = GDK_SCREEN_X11 (screen);
508
509   if (!screen_x11->rgba_visual)
510     return NULL;
511
512   if (!screen_x11->rgba_colormap)
513     screen_x11->rgba_colormap = gdk_colormap_new (screen_x11->rgba_visual,
514                                                   FALSE);
515   
516   return screen_x11->rgba_colormap;
517 }
518
519 /**
520  * gdk_screen_get_rgba_visual:
521  * @screen: a #GdkScreen
522  * 
523  * Gets a visual to use for creating windows or pixmaps with an
524  * alpha channel. See the docs for gdk_screen_get_rgba_colormap()
525  * for caveats.
526  * 
527  * Return value: a visual to use for windows with an alpha channel
528  *   or %NULL if the capability is not available.
529  *
530  * Since: 2.8
531  **/
532 GdkVisual *
533 gdk_screen_get_rgba_visual (GdkScreen *screen)
534 {
535   GdkScreenX11 *screen_x11;
536
537   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
538
539   screen_x11 = GDK_SCREEN_X11 (screen);
540
541   return screen_x11->rgba_visual;
542 }
543
544 /**
545  * gdk_x11_screen_get_xscreen:
546  * @screen: a #GdkScreen.
547  * @returns: an Xlib <type>Screen*</type>
548  *
549  * Returns the screen of a #GdkScreen.
550  *
551  * Since: 2.2
552  */
553 Screen *
554 gdk_x11_screen_get_xscreen (GdkScreen *screen)
555 {
556   return GDK_SCREEN_X11 (screen)->xscreen;
557 }
558
559 /**
560  * gdk_x11_screen_get_screen_number:
561  * @screen: a #GdkScreen.
562  * @returns: the position of @screen among the screens of
563  *   its display.
564  *
565  * Returns the index of a #GdkScreen.
566  *
567  * Since: 2.2
568  */
569 int
570 gdk_x11_screen_get_screen_number (GdkScreen *screen)
571 {
572   return GDK_SCREEN_X11 (screen)->screen_num;
573 }
574
575 static gboolean
576 check_is_composited (GdkDisplay *display,
577                      GdkScreenX11 *screen_x11)
578 {
579   Atom xselection = gdk_x11_atom_to_xatom_for_display (display, screen_x11->cm_selection_atom);
580   Window xwindow;
581   
582   xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), xselection);
583
584   return xwindow != None;
585 }
586
587 static GdkAtom
588 make_cm_atom (int screen_number)
589 {
590   gchar *name = g_strdup_printf ("_NET_WM_CM_S%d", screen_number);
591   GdkAtom atom = gdk_atom_intern (name, FALSE);
592   g_free (name);
593   return atom;
594 }
595
596 static void
597 init_monitor_geometry (GdkX11Monitor *monitor,
598                        int x, int y, int width, int height)
599 {
600   monitor->geometry.x = x;
601   monitor->geometry.y = y;
602   monitor->geometry.width = width;
603   monitor->geometry.height = height;
604
605   monitor->output = None;
606   monitor->width_mm = -1;
607   monitor->height_mm = -1;
608   monitor->output_name = NULL;
609   monitor->manufacturer = NULL;
610 }
611
612 static gboolean
613 init_fake_xinerama (GdkScreen *screen)
614 {
615 #ifdef G_ENABLE_DEBUG
616   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
617   XSetWindowAttributes atts;
618   Window win;
619   gint w, h;
620
621   if (!(_gdk_debug_flags & GDK_DEBUG_XINERAMA))
622     return FALSE;
623   
624   /* Fake Xinerama mode by splitting the screen into 4 monitors.
625    * Also draw a little cross to make the monitor boundaries visible.
626    */
627   w = WidthOfScreen (screen_x11->xscreen);
628   h = HeightOfScreen (screen_x11->xscreen);
629
630   screen_x11->n_monitors = 4;
631   screen_x11->monitors = g_new0 (GdkX11Monitor, 4);
632   init_monitor_geometry (&screen_x11->monitors[0], 0, 0, w / 2, h / 2);
633   init_monitor_geometry (&screen_x11->monitors[1], w / 2, 0, w / 2, h / 2);
634   init_monitor_geometry (&screen_x11->monitors[2], 0, h / 2, w / 2, h / 2);
635   init_monitor_geometry (&screen_x11->monitors[3], w / 2, h / 2, w / 2, h / 2);
636   
637   atts.override_redirect = 1;
638   atts.background_pixel = WhitePixel(GDK_SCREEN_XDISPLAY (screen), 
639                                      screen_x11->screen_num);
640   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
641                       screen_x11->xroot_window, 0, h / 2, w, 1, 0, 
642                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
643                                    screen_x11->screen_num),
644                       InputOutput, 
645                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
646                                     screen_x11->screen_num),
647                       CWOverrideRedirect|CWBackPixel, 
648                       &atts);
649   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win); 
650   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
651                       screen_x11->xroot_window, w/2 , 0, 1, h, 0, 
652                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
653                                    screen_x11->screen_num),
654                       InputOutput, 
655                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
656                                     screen_x11->screen_num),
657                       CWOverrideRedirect|CWBackPixel, 
658                       &atts);
659   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win);
660   return TRUE;
661 #endif
662   
663   return FALSE;
664 }
665
666 static int
667 monitor_compare_function (GdkX11Monitor *monitor1,
668                           GdkX11Monitor *monitor2)
669 {
670   /* Sort the leftmost/topmost monitors first.
671    * For "cloned" monitors, sort the bigger ones first
672    * (giving preference to taller monitors over wider
673    * monitors)
674    */
675
676   if (monitor1->geometry.x != monitor2->geometry.x)
677     return monitor1->geometry.x - monitor2->geometry.x;
678
679   if (monitor1->geometry.y != monitor2->geometry.y)
680     return monitor1->geometry.y - monitor2->geometry.y;
681
682   if (monitor1->geometry.height != monitor2->geometry.height)
683     return - (monitor1->geometry.height - monitor2->geometry.height);
684
685   if (monitor1->geometry.width != monitor2->geometry.width)
686     return - (monitor1->geometry.width - monitor2->geometry.width);
687
688   return 0;
689 }
690
691 static gboolean
692 init_randr13 (GdkScreen *screen)
693 {
694 #ifdef HAVE_RANDR
695   GdkDisplay *display = gdk_screen_get_display (screen);
696   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
697   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
698   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
699   XRRScreenResources *resources;
700   int i;
701   GArray *monitors;
702   gboolean randr12_compat = FALSE;
703
704   if (!display_x11->have_randr13)
705       return FALSE;
706
707   resources = XRRGetScreenResourcesCurrent (screen_x11->xdisplay,
708                                             screen_x11->xroot_window);
709   if (!resources)
710     return FALSE;
711   
712   monitors = g_array_sized_new (FALSE, TRUE, sizeof (GdkX11Monitor),
713                                 resources->noutput);
714
715   for (i = 0; i < resources->noutput; ++i)
716     {
717       XRROutputInfo *output =
718         XRRGetOutputInfo (dpy, resources, resources->outputs[i]);
719
720       /* Non RandR1.2 X driver have output name "default" */
721       randr12_compat |= !g_strcmp0(output->name, "default");
722
723       if (output->connection == RR_Disconnected)
724         continue;
725
726       if (output->crtc)
727         {
728           GdkX11Monitor monitor;
729           XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, output->crtc);
730
731           monitor.geometry.x = crtc->x;
732           monitor.geometry.y = crtc->y;
733           monitor.geometry.width = crtc->width;
734           monitor.geometry.height = crtc->height;
735
736           monitor.output = resources->outputs[i];
737           monitor.width_mm = output->mm_width;
738           monitor.height_mm = output->mm_height;
739           monitor.output_name = g_strdup (output->name);
740           /* FIXME: need EDID parser */
741           monitor.manufacturer = NULL;
742
743           g_array_append_val (monitors, monitor);
744
745           XRRFreeCrtcInfo (crtc);
746         }
747
748       XRRFreeOutputInfo (output);
749     }
750
751   XRRFreeScreenResources (resources);
752
753   /* non RandR 1.2 X driver doesn't return any usable multihead data */
754   if (randr12_compat)
755     {
756       g_array_free (monitors, TRUE);
757       return FALSE;
758     }
759
760   g_array_sort (monitors,
761                 (GCompareFunc) monitor_compare_function);
762   screen_x11->n_monitors = monitors->len;
763   screen_x11->monitors = (GdkX11Monitor *)g_array_free (monitors, FALSE);
764
765   return screen_x11->n_monitors > 0;
766 #endif
767   
768   return FALSE;
769 }
770
771 static gboolean
772 init_solaris_xinerama (GdkScreen *screen)
773 {
774 #ifdef HAVE_SOLARIS_XINERAMA
775   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
776   int screen_no = gdk_screen_get_number (screen);
777   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
778   XRectangle monitors[MAXFRAMEBUFFERS];
779   unsigned char hints[16];
780   gint result;
781   int n_monitors;
782   int i;
783   
784   if (!XineramaGetState (dpy, screen_no))
785     return FALSE;
786
787   result = XineramaGetInfo (dpy, screen_no, monitors, hints, &n_monitors);
788
789   /* Yes I know it should be Success but the current implementation 
790    * returns the num of monitor
791    */
792   if (result == 0)
793     {
794       return FALSE;
795     }
796
797   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
798   screen_x11->n_monitors = n_monitors;
799
800   for (i = 0; i < n_monitors; i++)
801     {
802       init_monitor_geometry (&screen_x11->monitors[i],
803                              monitors[i].x, monitors[i].y,
804                              monitors[i].width, monitors[i].height);
805     }
806   
807   return TRUE;
808 #endif /* HAVE_SOLARIS_XINERAMA */
809
810   return FALSE;
811 }
812
813 static gboolean
814 init_xfree_xinerama (GdkScreen *screen)
815 {
816 #ifdef HAVE_XFREE_XINERAMA
817   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
818   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
819   XineramaScreenInfo *monitors;
820   int i, n_monitors;
821   
822   if (!XineramaIsActive (dpy))
823     return FALSE;
824
825   monitors = XineramaQueryScreens (dpy, &n_monitors);
826   
827   if (n_monitors <= 0 || monitors == NULL)
828     {
829       /* If Xinerama doesn't think we have any monitors, try acting as
830        * though we had no Xinerama. If the "no monitors" condition
831        * is because XRandR 1.2 is currently switching between CRTCs,
832        * we'll be notified again when we have our monitor back,
833        * and can go back into Xinerama-ish mode at that point.
834        */
835       if (monitors)
836         XFree (monitors);
837       
838       return FALSE;
839     }
840
841   screen_x11->n_monitors = n_monitors;
842   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
843   
844   for (i = 0; i < n_monitors; ++i)
845     {
846       init_monitor_geometry (&screen_x11->monitors[i],
847                              monitors[i].x_org, monitors[i].y_org,
848                              monitors[i].width, monitors[i].height);
849     }
850   
851   XFree (monitors);
852   
853   return TRUE;
854 #endif /* HAVE_XFREE_XINERAMA */
855   
856   return FALSE;
857 }
858
859 static void
860 deinit_multihead (GdkScreen *screen)
861 {
862   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
863   int i;
864
865   for (i = 0; i < screen_x11->n_monitors; ++i)
866     {
867       GdkX11Monitor *monitor = get_monitor (screen, i);
868
869       g_free (monitor->output_name);
870       g_free (monitor->manufacturer);
871     }
872
873   g_free (screen_x11->monitors);
874
875   screen_x11->n_monitors = 0;
876   screen_x11->monitors = NULL;
877 }
878
879 static void
880 init_multihead (GdkScreen *screen)
881 {
882   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
883   int opcode, firstevent, firsterror;
884
885   deinit_multihead (screen);
886   
887   /* There are four different implementations of multihead support: 
888    *
889    *  1. Fake Xinerama for debugging purposes
890    *  2. RandR 1.2
891    *  3. Solaris Xinerama
892    *  4. XFree86/Xorg Xinerama
893    *
894    * We use them in that order.
895    */
896   if (init_fake_xinerama (screen))
897     return;
898
899   if (init_randr13 (screen))
900     return;
901
902   if (XQueryExtension (GDK_SCREEN_XDISPLAY (screen), "XINERAMA",
903                        &opcode, &firstevent, &firsterror))
904     {
905       if (init_solaris_xinerama (screen))
906         return;
907       
908       if (init_xfree_xinerama (screen))
909         return;
910     }
911
912   /* No multihead support of any kind for this screen */
913   screen_x11->n_monitors = 1;
914   screen_x11->monitors = g_new0 (GdkX11Monitor, 1);
915
916   init_monitor_geometry (screen_x11->monitors, 0, 0,
917                          WidthOfScreen (screen_x11->xscreen),
918                          HeightOfScreen (screen_x11->xscreen));
919 }
920
921 GdkScreen *
922 _gdk_x11_screen_new (GdkDisplay *display,
923                      gint        screen_number) 
924 {
925   GdkScreen *screen;
926   GdkScreenX11 *screen_x11;
927   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
928
929   screen = g_object_new (GDK_TYPE_SCREEN_X11, NULL);
930
931   screen_x11 = GDK_SCREEN_X11 (screen);
932   screen_x11->display = display;
933   screen_x11->xdisplay = display_x11->xdisplay;
934   screen_x11->xscreen = ScreenOfDisplay (display_x11->xdisplay, screen_number);
935   screen_x11->screen_num = screen_number;
936   screen_x11->xroot_window = RootWindow (display_x11->xdisplay,screen_number);
937   screen_x11->wmspec_check_window = None;
938   /* we want this to be always non-null */
939   screen_x11->window_manager_name = g_strdup ("unknown");
940   screen_x11->cm_selection_atom = make_cm_atom (screen_number);
941   screen_x11->is_composited = check_is_composited (display, screen_x11);
942   
943   init_multihead (screen);
944   init_randr_support (screen);
945   
946   _gdk_visual_init (screen);
947   _gdk_windowing_window_init (screen);
948   
949   return screen;
950 }
951
952 /**
953  * gdk_screen_is_composited:
954  * @screen: a #GdkScreen
955  * 
956  * Returns whether windows with an RGBA visual can reasonably
957  * be expected to have their alpha channel drawn correctly on
958  * the screen.
959  *
960  * On X11 this function returns whether a compositing manager is
961  * compositing @screen.
962  * 
963  * Return value: Whether windows with RGBA visuals can reasonably be
964  * expected to have their alpha channels drawn correctly on the screen.
965  * 
966  * Since: 2.10
967  **/
968 gboolean
969 gdk_screen_is_composited (GdkScreen *screen)
970 {
971   GdkScreenX11 *screen_x11;
972
973   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
974
975   screen_x11 = GDK_SCREEN_X11 (screen);
976
977   return screen_x11->is_composited;
978 }
979
980 static void
981 init_randr_support (GdkScreen * screen)
982 {
983   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
984   
985   XSelectInput (GDK_SCREEN_XDISPLAY (screen),
986                 screen_x11->xroot_window,
987                 StructureNotifyMask);
988
989 #ifdef HAVE_RANDR
990   XRRSelectInput (GDK_SCREEN_XDISPLAY (screen),
991                   screen_x11->xroot_window,
992                   RRScreenChangeNotifyMask      |
993                   RRCrtcChangeNotifyMask        |
994                   RROutputPropertyNotifyMask);
995 #endif
996 }
997
998 void
999 _gdk_x11_screen_size_changed (GdkScreen *screen,
1000                               XEvent    *event)
1001 {
1002   gint width, height;
1003
1004   width = gdk_screen_get_width (screen);
1005   height = gdk_screen_get_height (screen);
1006
1007 #ifdef HAVE_RANDR
1008   if (!XRRUpdateConfiguration (event))
1009     return;
1010 #else
1011   if (event->type == ConfigureNotify)
1012     {
1013       XConfigureEvent *rcevent = (XConfigureEvent *) event;
1014       Screen        *xscreen = gdk_x11_screen_get_xscreen (screen);
1015       
1016       xscreen->width   = rcevent->width;
1017       xscreen->height  = rcevent->height;
1018     }
1019   else
1020     return;
1021 #endif
1022
1023   if (width == gdk_screen_get_width (screen) && 
1024       height == gdk_screen_get_height (screen))
1025     return;
1026
1027   _gdk_x11_screen_process_monitors_change (screen);
1028
1029   g_signal_emit_by_name (screen, "size-changed");
1030 }
1031
1032 void
1033 _gdk_x11_screen_process_monitors_change (GdkScreen *screen)
1034 {
1035   init_multihead (screen);
1036
1037   g_signal_emit_by_name (screen, "monitors-changed");
1038 }
1039
1040 void
1041 _gdk_x11_screen_window_manager_changed (GdkScreen *screen)
1042 {
1043   g_signal_emit (screen, signals[WINDOW_MANAGER_CHANGED], 0);
1044 }
1045
1046 void
1047 _gdk_x11_screen_process_owner_change (GdkScreen *screen,
1048                                       XEvent *event)
1049 {
1050 #ifdef HAVE_XFIXES
1051   XFixesSelectionNotifyEvent *selection_event = (XFixesSelectionNotifyEvent *)event;
1052   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1053   Atom xcm_selection_atom = gdk_x11_atom_to_xatom_for_display (screen_x11->display,
1054                                                                screen_x11->cm_selection_atom);
1055
1056   if (selection_event->selection == xcm_selection_atom)
1057     {
1058       gboolean composited = selection_event->owner != None;
1059
1060       if (composited != screen_x11->is_composited)
1061         {
1062           screen_x11->is_composited = composited;
1063
1064           g_signal_emit_by_name (screen, "composited_changed");
1065         }
1066     }
1067 #endif
1068 }
1069
1070 /**
1071  * _gdk_windowing_substitute_screen_number:
1072  * @display_name : The name of a display, in the form used by 
1073  *                 gdk_display_open (). If %NULL a default value
1074  *                 will be used. On X11, this is derived from the DISPLAY
1075  *                 environment variable.
1076  * @screen_number : The number of a screen within the display
1077  *                  referred to by @display_name.
1078  *
1079  * Modifies a @display_name to make @screen_number the default
1080  * screen when the display is opened.
1081  *
1082  * Return value: a newly allocated string holding the resulting
1083  *   display name. Free with g_free().
1084  */
1085 gchar * 
1086 _gdk_windowing_substitute_screen_number (const gchar *display_name,
1087                                          gint         screen_number)
1088 {
1089   GString *str;
1090   gchar   *p;
1091
1092   if (!display_name)
1093     display_name = getenv ("DISPLAY");
1094
1095   if (!display_name)
1096     return NULL;
1097
1098   str = g_string_new (display_name);
1099
1100   p = strrchr (str->str, '.');
1101   if (p && p >  strchr (str->str, ':'))
1102     g_string_truncate (str, p - str->str);
1103
1104   g_string_append_printf (str, ".%d", screen_number);
1105
1106   return g_string_free (str, FALSE);
1107 }
1108
1109 /**
1110  * gdk_screen_make_display_name:
1111  * @screen: a #GdkScreen
1112  * 
1113  * Determines the name to pass to gdk_display_open() to get
1114  * a #GdkDisplay with this screen as the default screen.
1115  * 
1116  * Return value: a newly allocated string, free with g_free()
1117  *
1118  * Since: 2.2
1119  **/
1120 gchar *
1121 gdk_screen_make_display_name (GdkScreen *screen)
1122 {
1123   const gchar *old_display;
1124
1125   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1126
1127   old_display = gdk_display_get_name (gdk_screen_get_display (screen));
1128
1129   return _gdk_windowing_substitute_screen_number (old_display, 
1130                                                   gdk_screen_get_number (screen));
1131 }
1132
1133 /**
1134  * gdk_screen_get_active_window
1135  * @screen: a #GdkScreen
1136  *
1137  * Returns the screen's currently active window.
1138  *
1139  * On X11, this is done by inspecting the _NET_ACTIVE_WINDOW property
1140  * on the root window, as described in the <ulink
1141  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1142  * Manager Hints</ulink>. If there is no currently currently active
1143  * window, or the window manager does not support the
1144  * _NET_ACTIVE_WINDOW hint, this function returns %NULL.
1145  *
1146  * On other platforms, this function may return %NULL, depending on whether
1147  * it is implementable on that platform.
1148  *
1149  * The returned window should be unrefed using g_object_unref() when
1150  * no longer needed.
1151  *
1152  * Return value: the currently active window, or %NULL.
1153  *
1154  * Since: 2.10
1155  **/
1156 GdkWindow *
1157 gdk_screen_get_active_window (GdkScreen *screen)
1158 {
1159   GdkScreenX11 *screen_x11;
1160   GdkWindow *ret = NULL;
1161   Atom type_return;
1162   gint format_return;
1163   gulong nitems_return;
1164   gulong bytes_after_return;
1165   guchar *data = NULL;
1166
1167   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1168
1169   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1170                                             gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
1171     return NULL;
1172
1173   screen_x11 = GDK_SCREEN_X11 (screen);
1174
1175   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1176                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1177                                                                  "_NET_ACTIVE_WINDOW"),
1178                           0, 1, False, XA_WINDOW, &type_return,
1179                           &format_return, &nitems_return,
1180                           &bytes_after_return, &data)
1181       == Success)
1182     {
1183       if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
1184         {
1185           GdkNativeWindow window = *(GdkNativeWindow *) data;
1186
1187           if (window != None)
1188             {
1189               ret = gdk_window_foreign_new_for_display (screen_x11->display,
1190                                                         *(GdkNativeWindow *) data);
1191             }
1192         }
1193     }
1194
1195   if (data)
1196     XFree (data);
1197
1198   return ret;
1199 }
1200
1201 /**
1202  * gdk_screen_get_window_stack
1203  * @screen: a #GdkScreen
1204  *
1205  * Returns a #GList of #GdkWindow<!-- -->s representing the current
1206  * window stack.
1207  *
1208  * On X11, this is done by inspecting the _NET_CLIENT_LIST_STACKING
1209  * property on the root window, as described in the <ulink
1210  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1211  * Manager Hints</ulink>. If the window manager does not support the
1212  * _NET_CLIENT_LIST_STACKING hint, this function returns %NULL.
1213  *
1214  * On other platforms, this function may return %NULL, depending on whether
1215  * it is implementable on that platform.
1216  *
1217  * The returned list is newly allocated and owns references to the
1218  * windows it contains, so it should be freed using g_list_free() and
1219  * its windows unrefed using g_object_unref() when no longer needed.
1220  *
1221  * Return value: a list of #GdkWindow<!-- -->s for the current window stack,
1222  *               or %NULL.
1223  *
1224  * Since: 2.10
1225  **/
1226 GList *
1227 gdk_screen_get_window_stack (GdkScreen *screen)
1228 {
1229   GdkScreenX11 *screen_x11;
1230   GList *ret = NULL;
1231   Atom type_return;
1232   gint format_return;
1233   gulong nitems_return;
1234   gulong bytes_after_return;
1235   guchar *data = NULL;
1236
1237   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1238
1239   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1240                                             gdk_atom_intern_static_string ("_NET_CLIENT_LIST_STACKING")))
1241     return NULL;
1242
1243   screen_x11 = GDK_SCREEN_X11 (screen);
1244
1245   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1246                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1247                                                                  "_NET_CLIENT_LIST_STACKING"),
1248                           0, G_MAXLONG, False, XA_WINDOW, &type_return,
1249                           &format_return, &nitems_return,
1250                           &bytes_after_return, &data)
1251       == Success)
1252     {
1253       if ((type_return == XA_WINDOW) && (format_return == 32) &&
1254           (data) && (nitems_return > 0))
1255         {
1256           gulong *stack = (gulong *) data;
1257           GdkWindow *win;
1258           int i;
1259
1260           for (i = 0; i < nitems_return; i++)
1261             {
1262               win = gdk_window_foreign_new_for_display (screen_x11->display,
1263                                                         (GdkNativeWindow)stack[i]);
1264
1265               if (win != NULL)
1266                 ret = g_list_append (ret, win);
1267             }
1268         }
1269     }
1270
1271   if (data)
1272     XFree (data);
1273
1274   return ret;
1275 }
1276
1277 #define __GDK_SCREEN_X11_C__
1278 #include "gdkaliasdef.c"