]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkscreen-x11.c
+Wed Aug 20 18:16:29 2008 Søren Sandmann <sandmann@redhat.com>
[~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 gboolean
667 init_randr12 (GdkScreen *screen)
668 {
669 #ifdef HAVE_RANDR
670   GdkDisplay *display = gdk_screen_get_display (screen);
671   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
672   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
673   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
674   XRRScreenResources *resources;
675   int i;
676   GArray *monitors;
677   gboolean randr12_compat = FALSE;
678
679   if (!display_x11->have_randr12)
680       return FALSE;
681
682   resources = XRRGetScreenResources (screen_x11->xdisplay,
683                                      screen_x11->xroot_window);
684   if (!resources)
685     return FALSE;
686   
687   monitors = g_array_sized_new (FALSE, TRUE, sizeof (GdkX11Monitor),
688                                 resources->noutput);
689
690   for (i = 0; i < resources->noutput; ++i)
691     {
692       XRROutputInfo *output =
693         XRRGetOutputInfo (dpy, resources, resources->outputs[i]);
694
695       /* Non RandR1.2 X driver have output name "default" */
696       randr12_compat |= !g_strcmp0(output->name, "default");
697
698       if (output->crtc)
699         {
700           GdkX11Monitor monitor;
701           XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, output->crtc);
702
703           monitor.geometry.x = crtc->x;
704           monitor.geometry.y = crtc->y;
705           monitor.geometry.width = crtc->width;
706           monitor.geometry.height = crtc->height;
707
708           /* FIXME: fill this out properly - need EDID parser */
709           monitor.output = resources->outputs[i];
710           monitor.width_mm = -1;
711           monitor.height_mm = -1;
712           monitor.output_name = NULL;
713           monitor.manufacturer = NULL;
714
715           g_array_append_val (monitors, monitor);
716
717           XRRFreeCrtcInfo (crtc);
718         }
719
720       XRRFreeOutputInfo (output);
721     }
722
723   XRRFreeScreenResources (resources);
724
725   /* non RandR 1.2 X driver doesn't return any usable multihead data */
726   if (randr12_compat)
727     {
728       g_array_free (monitors, TRUE);
729       return FALSE;
730     }
731
732   screen_x11->n_monitors = monitors->len;
733   screen_x11->monitors = (GdkX11Monitor *)g_array_free (monitors, FALSE);
734
735   return TRUE;
736 #endif
737   
738   return FALSE;
739 }
740
741 static gboolean
742 init_solaris_xinerama (GdkScreen *screen)
743 {
744 #ifdef HAVE_SOLARIS_XINERAMA
745   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
746   int screen_no = gdk_screen_get_number (screen);
747   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
748   XRectangle monitors[MAXFRAMEBUFFERS];
749   unsigned char hints[16];
750   gint result;
751   int n_monitors;
752   int i;
753   
754   if (!XineramaGetState (dpy, screen_no))
755     return FALSE;
756
757   result = XineramaGetInfo (dpy, screen_no, monitors, hints, &n_monitors);
758
759   /* Yes I know it should be Success but the current implementation 
760    * returns the num of monitor
761    */
762   if (result == 0)
763     {
764       return FALSE;
765     }
766
767   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
768   screen_x11->n_monitors = n_monitors;
769
770   for (i = 0; i < n_monitors; i++)
771     {
772       init_monitor_geometry (&screen_x11->monitors[i],
773                              monitors[i].x, monitors[i].y,
774                              monitors[i].width, monitors[i].height);
775     }
776   
777   return TRUE;
778 #endif /* HAVE_SOLARIS_XINERAMA */
779
780   return FALSE;
781 }
782
783 static gboolean
784 init_xfree_xinerama (GdkScreen *screen)
785 {
786 #ifdef HAVE_XFREE_XINERAMA
787   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
788   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
789   XineramaScreenInfo *monitors;
790   int i, n_monitors;
791   
792   if (!XineramaIsActive (dpy))
793     return FALSE;
794
795   monitors = XineramaQueryScreens (dpy, &n_monitors);
796   
797   if (n_monitors <= 0 || monitors == NULL)
798     {
799       /* If Xinerama doesn't think we have any monitors, try acting as
800        * though we had no Xinerama. If the "no monitors" condition
801        * is because XRandR 1.2 is currently switching between CRTCs,
802        * we'll be notified again when we have our monitor back,
803        * and can go back into Xinerama-ish mode at that point.
804        */
805       if (monitors)
806         XFree (monitors);
807       
808       return FALSE;
809     }
810
811   screen_x11->n_monitors = n_monitors;
812   screen_x11->monitors = g_new0 (GdkX11Monitor, n_monitors);
813   
814   for (i = 0; i < n_monitors; ++i)
815     {
816       init_monitor_geometry (&screen_x11->monitors[i],
817                              monitors[i].x_org, monitors[i].y_org,
818                              monitors[i].width, monitors[i].height);
819     }
820   
821   XFree (monitors);
822   
823   return TRUE;
824 #endif /* HAVE_XFREE_XINERAMA */
825   
826   return FALSE;
827 }
828
829 static void
830 deinit_multihead (GdkScreen *screen)
831 {
832   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
833   int i;
834
835   for (i = 0; i < screen_x11->n_monitors; ++i)
836     {
837       GdkX11Monitor *monitor = get_monitor (screen, i);
838
839       g_free (monitor->output_name);
840       g_free (monitor->manufacturer);
841     }
842
843   g_free (screen_x11->monitors);
844
845   screen_x11->n_monitors = 0;
846   screen_x11->monitors = NULL;
847 }
848
849 static void
850 init_multihead (GdkScreen *screen)
851 {
852   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
853   int opcode, firstevent, firsterror;
854
855   deinit_multihead (screen);
856   
857   /* There are four different implementations of multihead support: 
858    *
859    *  1. Fake Xinerama for debugging purposes
860    *  2. RandR 1.2
861    *  3. Solaris Xinerama
862    *  4. XFree86/Xorg Xinerama
863    *
864    * We use them in that order.
865    */
866   if (init_fake_xinerama (screen))
867     return;
868
869   if (init_randr12 (screen))
870     return;
871
872   if (XQueryExtension (GDK_SCREEN_XDISPLAY (screen), "XINERAMA",
873                        &opcode, &firstevent, &firsterror))
874     {
875       if (init_solaris_xinerama (screen))
876         return;
877       
878       if (init_xfree_xinerama (screen))
879         return;
880     }
881
882   /* No multihead support of any kind for this screen */
883   screen_x11->n_monitors = 1;
884   screen_x11->monitors = g_new0 (GdkX11Monitor, 1);
885
886   init_monitor_geometry (screen_x11->monitors, 0, 0,
887                          WidthOfScreen (screen_x11->xscreen),
888                          HeightOfScreen (screen_x11->xscreen));
889 }
890
891 GdkScreen *
892 _gdk_x11_screen_new (GdkDisplay *display,
893                      gint        screen_number) 
894 {
895   GdkScreen *screen;
896   GdkScreenX11 *screen_x11;
897   GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
898
899   screen = g_object_new (GDK_TYPE_SCREEN_X11, NULL);
900
901   screen_x11 = GDK_SCREEN_X11 (screen);
902   screen_x11->display = display;
903   screen_x11->xdisplay = display_x11->xdisplay;
904   screen_x11->xscreen = ScreenOfDisplay (display_x11->xdisplay, screen_number);
905   screen_x11->screen_num = screen_number;
906   screen_x11->xroot_window = RootWindow (display_x11->xdisplay,screen_number);
907   screen_x11->wmspec_check_window = None;
908   /* we want this to be always non-null */
909   screen_x11->window_manager_name = g_strdup ("unknown");
910   screen_x11->cm_selection_atom = make_cm_atom (screen_number);
911   screen_x11->is_composited = check_is_composited (display, screen_x11);
912   
913   init_multihead (screen);
914   init_randr_support (screen);
915   
916   _gdk_visual_init (screen);
917   _gdk_windowing_window_init (screen);
918   
919   return screen;
920 }
921
922 /**
923  * gdk_screen_is_composited:
924  * @screen: a #GdkScreen
925  * 
926  * Returns whether windows with an RGBA visual can reasonably
927  * be expected to have their alpha channel drawn correctly on
928  * the screen.
929  *
930  * On X11 this function returns whether a compositing manager is
931  * compositing @screen.
932  * 
933  * Return value: Whether windows with RGBA visuals can reasonably be
934  * expected to have their alpha channels drawn correctly on the screen.
935  * 
936  * Since: 2.10
937  **/
938 gboolean
939 gdk_screen_is_composited (GdkScreen *screen)
940 {
941   GdkScreenX11 *screen_x11;
942
943   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
944
945   screen_x11 = GDK_SCREEN_X11 (screen);
946
947   return screen_x11->is_composited;
948 }
949
950 static void
951 init_randr_support (GdkScreen * screen)
952 {
953   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
954   
955   XSelectInput (GDK_SCREEN_XDISPLAY (screen),
956                 screen_x11->xroot_window,
957                 StructureNotifyMask);
958
959 #ifdef HAVE_RANDR
960   XRRSelectInput (GDK_SCREEN_XDISPLAY (screen),
961                   screen_x11->xroot_window,
962                   RRScreenChangeNotifyMask      |
963                   RRCrtcChangeNotifyMask        |
964                   RROutputPropertyNotifyMask);
965 #endif
966 }
967
968 void
969 _gdk_x11_screen_size_changed (GdkScreen *screen,
970                               XEvent    *event)
971 {
972 #ifdef HAVE_RANDR
973   if (!XRRUpdateConfiguration (event))
974     return;
975 #else
976   if (event->type == ConfigureNotify)
977     {
978       XConfigureEvent *rcevent = (XConfigureEvent *) event;
979       Screen        *xscreen = gdk_x11_screen_get_xscreen (screen);
980       
981       xscreen->width   = rcevent->width;
982       xscreen->height  = rcevent->height;
983     }
984   else
985     return;
986 #endif
987   
988   _gdk_x11_screen_process_monitors_change (screen);
989   g_signal_emit_by_name (screen, "size_changed");
990 }
991
992 void
993 _gdk_x11_screen_process_monitors_change (GdkScreen *screen)
994 {
995   init_multihead (screen);
996
997   g_signal_emit_by_name (screen, "monitors_changed");
998 }
999
1000 void
1001 _gdk_x11_screen_window_manager_changed (GdkScreen *screen)
1002 {
1003   g_signal_emit (screen, signals[WINDOW_MANAGER_CHANGED], 0);
1004 }
1005
1006 void
1007 _gdk_x11_screen_process_owner_change (GdkScreen *screen,
1008                                       XEvent *event)
1009 {
1010 #ifdef HAVE_XFIXES
1011   XFixesSelectionNotifyEvent *selection_event = (XFixesSelectionNotifyEvent *)event;
1012   GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
1013   Atom xcm_selection_atom = gdk_x11_atom_to_xatom_for_display (screen_x11->display,
1014                                                                screen_x11->cm_selection_atom);
1015
1016   if (selection_event->selection == xcm_selection_atom)
1017     {
1018       gboolean composited = selection_event->owner != None;
1019
1020       if (composited != screen_x11->is_composited)
1021         {
1022           screen_x11->is_composited = composited;
1023
1024           g_signal_emit_by_name (screen, "composited_changed");
1025         }
1026     }
1027 #endif
1028 }
1029
1030 /**
1031  * _gdk_windowing_substitute_screen_number:
1032  * @display_name : The name of a display, in the form used by 
1033  *                 gdk_display_open (). If %NULL a default value
1034  *                 will be used. On X11, this is derived from the DISPLAY
1035  *                 environment variable.
1036  * @screen_number : The number of a screen within the display
1037  *                  referred to by @display_name.
1038  *
1039  * Modifies a @display_name to make @screen_number the default
1040  * screen when the display is opened.
1041  *
1042  * Return value: a newly allocated string holding the resulting
1043  *   display name. Free with g_free().
1044  */
1045 gchar * 
1046 _gdk_windowing_substitute_screen_number (const gchar *display_name,
1047                                          gint         screen_number)
1048 {
1049   GString *str;
1050   gchar   *p;
1051
1052   if (!display_name)
1053     display_name = getenv ("DISPLAY");
1054
1055   if (!display_name)
1056     return NULL;
1057
1058   str = g_string_new (display_name);
1059
1060   p = strrchr (str->str, '.');
1061   if (p && p >  strchr (str->str, ':'))
1062     g_string_truncate (str, p - str->str);
1063
1064   g_string_append_printf (str, ".%d", screen_number);
1065
1066   return g_string_free (str, FALSE);
1067 }
1068
1069 /**
1070  * gdk_screen_make_display_name:
1071  * @screen: a #GdkScreen
1072  * 
1073  * Determines the name to pass to gdk_display_open() to get
1074  * a #GdkDisplay with this screen as the default screen.
1075  * 
1076  * Return value: a newly allocated string, free with g_free()
1077  *
1078  * Since: 2.2
1079  **/
1080 gchar *
1081 gdk_screen_make_display_name (GdkScreen *screen)
1082 {
1083   const gchar *old_display;
1084
1085   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1086
1087   old_display = gdk_display_get_name (gdk_screen_get_display (screen));
1088
1089   return _gdk_windowing_substitute_screen_number (old_display, 
1090                                                   gdk_screen_get_number (screen));
1091 }
1092
1093 /**
1094  * gdk_screen_get_active_window
1095  * @screen: a #GdkScreen
1096  *
1097  * Returns the screen's currently active window.
1098  *
1099  * On X11, this is done by inspecting the _NET_ACTIVE_WINDOW property
1100  * on the root window, as described in the <ulink
1101  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1102  * Manager Hints</ulink>. If there is no currently currently active
1103  * window, or the window manager does not support the
1104  * _NET_ACTIVE_WINDOW hint, this function returns %NULL.
1105  *
1106  * On other platforms, this function may return %NULL, depending on whether
1107  * it is implementable on that platform.
1108  *
1109  * The returned window should be unrefed using g_object_unref() when
1110  * no longer needed.
1111  *
1112  * Return value: the currently active window, or %NULL.
1113  *
1114  * Since: 2.10
1115  **/
1116 GdkWindow *
1117 gdk_screen_get_active_window (GdkScreen *screen)
1118 {
1119   GdkScreenX11 *screen_x11;
1120   GdkWindow *ret = NULL;
1121   Atom type_return;
1122   gint format_return;
1123   gulong nitems_return;
1124   gulong bytes_after_return;
1125   guchar *data = NULL;
1126
1127   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1128
1129   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1130                                             gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
1131     return NULL;
1132
1133   screen_x11 = GDK_SCREEN_X11 (screen);
1134
1135   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1136                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1137                                                                  "_NET_ACTIVE_WINDOW"),
1138                           0, 1, False, XA_WINDOW, &type_return,
1139                           &format_return, &nitems_return,
1140                           &bytes_after_return, &data)
1141       == Success)
1142     {
1143       if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
1144         {
1145           GdkNativeWindow window = *(GdkNativeWindow *) data;
1146
1147           if (window != None)
1148             {
1149               ret = gdk_window_foreign_new_for_display (screen_x11->display,
1150                                                         *(GdkNativeWindow *) data);
1151             }
1152         }
1153     }
1154
1155   if (data)
1156     XFree (data);
1157
1158   return ret;
1159 }
1160
1161 /**
1162  * gdk_screen_get_window_stack
1163  * @screen: a #GdkScreen
1164  *
1165  * Returns a #GList of #GdkWindow<!-- -->s representing the current
1166  * window stack.
1167  *
1168  * On X11, this is done by inspecting the _NET_CLIENT_LIST_STACKING
1169  * property on the root window, as described in the <ulink
1170  * url="http://www.freedesktop.org/Standards/wm-spec">Extended Window
1171  * Manager Hints</ulink>. If the window manager does not support the
1172  * _NET_CLIENT_LIST_STACKING hint, this function returns %NULL.
1173  *
1174  * On other platforms, this function may return %NULL, depending on whether
1175  * it is implementable on that platform.
1176  *
1177  * The returned list is newly allocated and owns references to the
1178  * windows it contains, so it should be freed using g_list_free() and
1179  * its windows unrefed using g_object_unref() when no longer needed.
1180  *
1181  * Return value: a list of #GdkWindow<!-- -->s for the current window stack,
1182  *               or %NULL.
1183  *
1184  * Since: 2.10
1185  **/
1186 GList *
1187 gdk_screen_get_window_stack (GdkScreen *screen)
1188 {
1189   GdkScreenX11 *screen_x11;
1190   GList *ret = NULL;
1191   Atom type_return;
1192   gint format_return;
1193   gulong nitems_return;
1194   gulong bytes_after_return;
1195   guchar *data = NULL;
1196
1197   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1198
1199   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1200                                             gdk_atom_intern_static_string ("_NET_CLIENT_LIST_STACKING")))
1201     return NULL;
1202
1203   screen_x11 = GDK_SCREEN_X11 (screen);
1204
1205   if (XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
1206                           gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
1207                                                                  "_NET_CLIENT_LIST_STACKING"),
1208                           0, G_MAXLONG, False, XA_WINDOW, &type_return,
1209                           &format_return, &nitems_return,
1210                           &bytes_after_return, &data)
1211       == Success)
1212     {
1213       if ((type_return == XA_WINDOW) && (format_return == 32) &&
1214           (data) && (nitems_return > 0))
1215         {
1216           gulong *stack = (gulong *) data;
1217           GdkWindow *win;
1218           int i;
1219
1220           for (i = 0; i < nitems_return; i++)
1221             {
1222               win = gdk_window_foreign_new_for_display (screen_x11->display,
1223                                                         (GdkNativeWindow)stack[i]);
1224
1225               if (win != NULL)
1226                 ret = g_list_append (ret, win);
1227             }
1228         }
1229     }
1230
1231   if (data)
1232     XFree (data);
1233
1234   return ret;
1235 }
1236
1237 #define __GDK_SCREEN_X11_C__
1238 #include "gdkaliasdef.c"