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