]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkscreen-x11.c
Add support for disabling middle click paste
[~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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "config.h"
23
24 #include "gdkscreen-x11.h"
25 #include "gdkdisplay-x11.h"
26 #include "gdkprivate-x11.h"
27
28 #include <glib.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <X11/Xatom.h>
34
35 #ifdef HAVE_SOLARIS_XINERAMA
36 #include <X11/extensions/xinerama.h>
37 #endif
38 #ifdef HAVE_XFREE_XINERAMA
39 #include <X11/extensions/Xinerama.h>
40 #endif
41
42 #ifdef HAVE_RANDR
43 #include <X11/extensions/Xrandr.h>
44 #endif
45
46 #ifdef HAVE_XFIXES
47 #include <X11/extensions/Xfixes.h>
48 #endif
49
50 #include "gdksettings.c"
51
52 static void         gdk_x11_screen_dispose     (GObject           *object);
53 static void         gdk_x11_screen_finalize    (GObject           *object);
54 static void         init_randr_support         (GdkScreen         *screen);
55 static void         deinit_multihead           (GdkScreen         *screen);
56
57 enum
58 {
59   WINDOW_MANAGER_CHANGED,
60   LAST_SIGNAL
61 };
62
63 static guint signals[LAST_SIGNAL] = { 0 };
64
65 G_DEFINE_TYPE (GdkX11Screen, gdk_x11_screen, GDK_TYPE_SCREEN)
66
67 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
68
69 struct _NetWmSupportedAtoms
70 {
71   Atom *atoms;
72   gulong n_atoms;
73 };
74
75 struct _GdkX11Monitor
76 {
77   GdkRectangle  geometry;
78   XID           output;
79   int           width_mm;
80   int           height_mm;
81   char *        output_name;
82   char *        manufacturer;
83 };
84
85
86 static void
87 gdk_x11_screen_init (GdkX11Screen *screen)
88 {
89 }
90
91 static GdkDisplay *
92 gdk_x11_screen_get_display (GdkScreen *screen)
93 {
94   return GDK_X11_SCREEN (screen)->display;
95 }
96
97 static gint
98 gdk_x11_screen_get_width (GdkScreen *screen)
99 {
100   return WidthOfScreen (GDK_X11_SCREEN (screen)->xscreen);
101 }
102
103 static gint
104 gdk_x11_screen_get_height (GdkScreen *screen)
105 {
106   return HeightOfScreen (GDK_X11_SCREEN (screen)->xscreen);
107 }
108
109 static gint
110 gdk_x11_screen_get_width_mm (GdkScreen *screen)
111 {
112   return WidthMMOfScreen (GDK_X11_SCREEN (screen)->xscreen);
113 }
114
115 static gint
116 gdk_x11_screen_get_height_mm (GdkScreen *screen)
117 {
118   return HeightMMOfScreen (GDK_X11_SCREEN (screen)->xscreen);
119 }
120
121 static gint
122 gdk_x11_screen_get_number (GdkScreen *screen)
123 {
124   return GDK_X11_SCREEN (screen)->screen_num;
125 }
126
127 static GdkWindow *
128 gdk_x11_screen_get_root_window (GdkScreen *screen)
129 {
130   return GDK_X11_SCREEN (screen)->root_window;
131 }
132
133 static void
134 _gdk_x11_screen_events_uninit (GdkScreen *screen)
135 {
136   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
137
138   if (x11_screen->xsettings_client)
139     {
140       xsettings_client_destroy (x11_screen->xsettings_client);
141       x11_screen->xsettings_client = NULL;
142     }
143 }
144
145 static void
146 gdk_x11_screen_dispose (GObject *object)
147 {
148   GdkX11Screen *x11_screen = GDK_X11_SCREEN (object);
149   int i;
150
151   for (i = 0; i < 32; ++i)
152     {
153       if (x11_screen->subwindow_gcs[i])
154         {
155           XFreeGC (x11_screen->xdisplay, x11_screen->subwindow_gcs[i]);
156           x11_screen->subwindow_gcs[i] = 0;
157         }
158     }
159
160   _gdk_x11_screen_events_uninit (GDK_SCREEN (object));
161
162   if (x11_screen->root_window)
163     _gdk_window_destroy (x11_screen->root_window, TRUE);
164
165   G_OBJECT_CLASS (gdk_x11_screen_parent_class)->dispose (object);
166
167   x11_screen->xdisplay = NULL;
168   x11_screen->xscreen = NULL;
169   x11_screen->screen_num = -1;
170   x11_screen->xroot_window = None;
171   x11_screen->wmspec_check_window = None;
172 }
173
174 static void
175 gdk_x11_screen_finalize (GObject *object)
176 {
177   GdkX11Screen *x11_screen = GDK_X11_SCREEN (object);
178   gint          i;
179
180   if (x11_screen->root_window)
181     g_object_unref (x11_screen->root_window);
182
183   /* Visual Part */
184   for (i = 0; i < x11_screen->nvisuals; i++)
185     g_object_unref (x11_screen->visuals[i]);
186   g_free (x11_screen->visuals);
187   g_hash_table_destroy (x11_screen->visual_hash);
188
189   g_free (x11_screen->window_manager_name);
190
191   deinit_multihead (GDK_SCREEN (object));
192   
193   G_OBJECT_CLASS (gdk_x11_screen_parent_class)->finalize (object);
194 }
195
196 static gint
197 gdk_x11_screen_get_n_monitors (GdkScreen *screen)
198 {
199   return GDK_X11_SCREEN (screen)->n_monitors;
200 }
201
202 static gint
203 gdk_x11_screen_get_primary_monitor (GdkScreen *screen)
204 {
205   return GDK_X11_SCREEN (screen)->primary_monitor;
206 }
207
208 static gint
209 gdk_x11_screen_get_monitor_width_mm (GdkScreen *screen,
210                                      gint       monitor_num)
211 {
212   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
213
214   return x11_screen->monitors[monitor_num].width_mm;
215 }
216
217 static gint
218 gdk_x11_screen_get_monitor_height_mm (GdkScreen *screen,
219                                       gint       monitor_num)
220 {
221   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
222
223   return x11_screen->monitors[monitor_num].height_mm;
224 }
225
226 static gchar *
227 gdk_x11_screen_get_monitor_plug_name (GdkScreen *screen,
228                                       gint       monitor_num)
229 {
230   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
231
232   return g_strdup (x11_screen->monitors[monitor_num].output_name);
233 }
234
235 /**
236  * gdk_x11_screen_get_monitor_output:
237  * @screen: (type GdkX11Screen): a #GdkScreen
238  * @monitor_num: number of the monitor, between 0 and gdk_screen_get_n_monitors (screen)
239  *
240  * Gets the XID of the specified output/monitor.
241  * If the X server does not support version 1.2 of the RANDR
242  * extension, 0 is returned.
243  *
244  * Returns: the XID of the monitor
245  *
246  * Since: 2.14
247  */
248 XID
249 gdk_x11_screen_get_monitor_output (GdkScreen *screen,
250                                    gint       monitor_num)
251 {
252   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
253
254   g_return_val_if_fail (GDK_IS_SCREEN (screen), None);
255   g_return_val_if_fail (monitor_num >= 0, None);
256   g_return_val_if_fail (monitor_num < x11_screen->n_monitors, None);
257
258   return x11_screen->monitors[monitor_num].output;
259 }
260
261 static void
262 gdk_x11_screen_get_monitor_geometry (GdkScreen    *screen,
263                                      gint          monitor_num,
264                                      GdkRectangle *dest)
265 {
266   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
267
268   if (dest)
269     *dest = x11_screen->monitors[monitor_num].geometry;
270 }
271
272 static int
273 get_current_desktop (GdkScreen *screen)
274 {
275   Display *display;
276   Window win;
277   Atom current_desktop, type;
278   int format;
279   unsigned long n_items, bytes_after;
280   unsigned char *data_return = NULL;
281   int workspace = 0;
282
283   display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));
284   win = XRootWindow (display, GDK_SCREEN_XNUMBER (screen));
285
286   current_desktop = XInternAtom (display, "_NET_CURRENT_DESKTOP", True);
287
288   XGetWindowProperty (display,
289                       win,
290                       current_desktop,
291                       0, G_MAXLONG,
292                       False, XA_CARDINAL,
293                       &type, &format, &n_items, &bytes_after,
294                       &data_return);
295
296   if (type == XA_CARDINAL && format == 32 && n_items > 0)
297     workspace = (int) data_return[0];
298
299   if (data_return)
300     XFree (data_return);
301
302   return workspace;
303 }
304
305 static void
306 get_work_area (GdkScreen    *screen,
307                GdkRectangle *area)
308 {
309   Atom            workarea;
310   Atom            type;
311   Window          win;
312   int             format;
313   gulong          num;
314   gulong          leftovers;
315   gulong          max_len = 4 * 32;
316   guchar         *ret_workarea;
317   long           *workareas;
318   int             result;
319   int             disp_screen;
320   int             desktop;
321   Display        *display;
322
323   display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen));
324   disp_screen = GDK_SCREEN_XNUMBER (screen);
325   workarea = XInternAtom (display, "_NET_WORKAREA", True);
326
327   /* Defaults in case of error */
328   area->x = 0;
329   area->y = 0;
330   area->width = gdk_screen_get_width (screen);
331   area->height = gdk_screen_get_height (screen);
332
333   if (workarea == None)
334     return;
335
336   win = XRootWindow (display, disp_screen);
337   result = XGetWindowProperty (display,
338                                win,
339                                workarea,
340                                0,
341                                max_len,
342                                False,
343                                AnyPropertyType,
344                                &type,
345                                &format,
346                                &num,
347                                &leftovers,
348                                &ret_workarea);
349   if (result != Success ||
350       type == None ||
351       format == 0 ||
352       leftovers ||
353       num % 4 != 0)
354     return;
355
356   desktop = get_current_desktop (screen);
357
358   workareas = (long *) ret_workarea;
359   area->x = workareas[desktop * 4];
360   area->y = workareas[desktop * 4 + 1];
361   area->width = workareas[desktop * 4 + 2];
362   area->height = workareas[desktop * 4 + 3];
363
364   XFree (ret_workarea);
365 }
366
367 static void
368 gdk_x11_screen_get_monitor_workarea (GdkScreen    *screen,
369                                      gint          monitor_num,
370                                      GdkRectangle *dest)
371 {
372   GdkRectangle workarea;
373
374   gdk_x11_screen_get_monitor_geometry (screen, monitor_num, dest);
375
376   /* The EWMH constrains workarea to be a rectangle, so it
377    * can't adequately deal with L-shaped monitor arrangements.
378    * As a workaround, we ignore the workarea for anything
379    * but the primary monitor. Since that is where the 'desktop
380    * chrome' usually lives, this works ok in practice.
381    */
382   if (monitor_num == GDK_X11_SCREEN (screen)->primary_monitor)
383     {
384       get_work_area (screen, &workarea);
385       if (gdk_rectangle_intersect (dest, &workarea, &workarea))
386         *dest = workarea;
387     }
388 }
389
390 static GdkVisual *
391 gdk_x11_screen_get_rgba_visual (GdkScreen *screen)
392 {
393   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
394
395   return x11_screen->rgba_visual;
396 }
397
398 /**
399  * gdk_x11_screen_get_xscreen:
400  * @screen: (type GdkX11Screen): a #GdkScreen
401  *
402  * Returns the screen of a #GdkScreen.
403  *
404  * Returns: (transfer none): an Xlib <type>Screen*</type>
405  *
406  * Since: 2.2
407  */
408 Screen *
409 gdk_x11_screen_get_xscreen (GdkScreen *screen)
410 {
411   return GDK_X11_SCREEN (screen)->xscreen;
412 }
413
414 /**
415  * gdk_x11_screen_get_screen_number:
416  * @screen: (type GdkX11Screen): a #GdkScreen
417  *
418  * Returns the index of a #GdkScreen.
419  *
420  * Returns: the position of @screen among the screens
421  *     of its display
422  *
423  * Since: 2.2
424  */
425 int
426 gdk_x11_screen_get_screen_number (GdkScreen *screen)
427 {
428   return GDK_X11_SCREEN (screen)->screen_num;
429 }
430
431 static gboolean
432 check_is_composited (GdkDisplay *display,
433                      GdkX11Screen *x11_screen)
434 {
435   Atom xselection = gdk_x11_atom_to_xatom_for_display (display, x11_screen->cm_selection_atom);
436   Window xwindow;
437   
438   xwindow = XGetSelectionOwner (GDK_DISPLAY_XDISPLAY (display), xselection);
439
440   return xwindow != None;
441 }
442
443 static GdkAtom
444 make_cm_atom (int screen_number)
445 {
446   gchar *name = g_strdup_printf ("_NET_WM_CM_S%d", screen_number);
447   GdkAtom atom = gdk_atom_intern (name, FALSE);
448   g_free (name);
449   return atom;
450 }
451
452 static void
453 init_monitor_geometry (GdkX11Monitor *monitor,
454                        int x, int y, int width, int height)
455 {
456   monitor->geometry.x = x;
457   monitor->geometry.y = y;
458   monitor->geometry.width = width;
459   monitor->geometry.height = height;
460
461   monitor->output = None;
462   monitor->width_mm = -1;
463   monitor->height_mm = -1;
464   monitor->output_name = NULL;
465   monitor->manufacturer = NULL;
466 }
467
468 static gboolean
469 init_fake_xinerama (GdkScreen *screen)
470 {
471 #ifdef G_ENABLE_DEBUG
472   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
473   XSetWindowAttributes atts;
474   Window win;
475   gint w, h;
476
477   if (!(_gdk_debug_flags & GDK_DEBUG_XINERAMA))
478     return FALSE;
479   
480   /* Fake Xinerama mode by splitting the screen into 4 monitors.
481    * Also draw a little cross to make the monitor boundaries visible.
482    */
483   w = WidthOfScreen (x11_screen->xscreen);
484   h = HeightOfScreen (x11_screen->xscreen);
485
486   x11_screen->n_monitors = 4;
487   x11_screen->monitors = g_new0 (GdkX11Monitor, 4);
488   init_monitor_geometry (&x11_screen->monitors[0], 0, 0, w / 2, h / 2);
489   init_monitor_geometry (&x11_screen->monitors[1], w / 2, 0, w / 2, h / 2);
490   init_monitor_geometry (&x11_screen->monitors[2], 0, h / 2, w / 2, h / 2);
491   init_monitor_geometry (&x11_screen->monitors[3], w / 2, h / 2, w / 2, h / 2);
492   
493   atts.override_redirect = 1;
494   atts.background_pixel = WhitePixel(GDK_SCREEN_XDISPLAY (screen), 
495                                      x11_screen->screen_num);
496   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
497                       x11_screen->xroot_window, 0, h / 2, w, 1, 0, 
498                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
499                                    x11_screen->screen_num),
500                       InputOutput, 
501                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
502                                     x11_screen->screen_num),
503                       CWOverrideRedirect|CWBackPixel, 
504                       &atts);
505   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win); 
506   win = XCreateWindow(GDK_SCREEN_XDISPLAY (screen), 
507                       x11_screen->xroot_window, w/2 , 0, 1, h, 0, 
508                       DefaultDepth(GDK_SCREEN_XDISPLAY (screen), 
509                                    x11_screen->screen_num),
510                       InputOutput, 
511                       DefaultVisual(GDK_SCREEN_XDISPLAY (screen), 
512                                     x11_screen->screen_num),
513                       CWOverrideRedirect|CWBackPixel, 
514                       &atts);
515   XMapRaised(GDK_SCREEN_XDISPLAY (screen), win);
516   return TRUE;
517 #endif
518   
519   return FALSE;
520 }
521
522 static void
523 free_monitors (GdkX11Monitor *monitors,
524                gint           n_monitors)
525 {
526   int i;
527
528   for (i = 0; i < n_monitors; ++i)
529     {
530       g_free (monitors[i].output_name);
531       g_free (monitors[i].manufacturer);
532     }
533
534   g_free (monitors);
535 }
536
537 #ifdef HAVE_RANDR
538 static int
539 monitor_compare_function (GdkX11Monitor *monitor1,
540                           GdkX11Monitor *monitor2)
541 {
542   /* Sort the leftmost/topmost monitors first.
543    * For "cloned" monitors, sort the bigger ones first
544    * (giving preference to taller monitors over wider
545    * monitors)
546    */
547
548   if (monitor1->geometry.x != monitor2->geometry.x)
549     return monitor1->geometry.x - monitor2->geometry.x;
550
551   if (monitor1->geometry.y != monitor2->geometry.y)
552     return monitor1->geometry.y - monitor2->geometry.y;
553
554   if (monitor1->geometry.height != monitor2->geometry.height)
555     return - (monitor1->geometry.height - monitor2->geometry.height);
556
557   if (monitor1->geometry.width != monitor2->geometry.width)
558     return - (monitor1->geometry.width - monitor2->geometry.width);
559
560   return 0;
561 }
562 #endif
563
564 static gboolean
565 init_randr13 (GdkScreen *screen)
566 {
567 #ifdef HAVE_RANDR
568   GdkDisplay *display = gdk_screen_get_display (screen);
569   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
570   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
571   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
572   XRRScreenResources *resources;
573   RROutput primary_output;
574   RROutput first_output = None;
575   int i;
576   GArray *monitors;
577   gboolean randr12_compat = FALSE;
578
579   if (!display_x11->have_randr13)
580       return FALSE;
581
582   resources = XRRGetScreenResourcesCurrent (x11_screen->xdisplay,
583                                             x11_screen->xroot_window);
584   if (!resources)
585     return FALSE;
586
587   monitors = g_array_sized_new (FALSE, TRUE, sizeof (GdkX11Monitor),
588                                 resources->noutput);
589
590   for (i = 0; i < resources->noutput; ++i)
591     {
592       XRROutputInfo *output =
593         XRRGetOutputInfo (dpy, resources, resources->outputs[i]);
594
595       /* Non RandR1.2 X driver have output name "default" */
596       randr12_compat |= !g_strcmp0 (output->name, "default");
597
598       if (output->connection == RR_Disconnected)
599         {
600           XRRFreeOutputInfo (output);
601           continue;
602         }
603
604       if (output->crtc)
605         {
606           GdkX11Monitor monitor;
607           XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, output->crtc);
608
609           monitor.geometry.x = crtc->x;
610           monitor.geometry.y = crtc->y;
611           monitor.geometry.width = crtc->width;
612           monitor.geometry.height = crtc->height;
613
614           monitor.output = resources->outputs[i];
615           monitor.width_mm = output->mm_width;
616           monitor.height_mm = output->mm_height;
617           monitor.output_name = g_strdup (output->name);
618           /* FIXME: need EDID parser */
619           monitor.manufacturer = NULL;
620
621           g_array_append_val (monitors, monitor);
622
623           XRRFreeCrtcInfo (crtc);
624         }
625
626       XRRFreeOutputInfo (output);
627     }
628
629   if (resources->noutput > 0)
630     first_output = resources->outputs[0];
631
632   XRRFreeScreenResources (resources);
633
634   /* non RandR 1.2 X driver doesn't return any usable multihead data */
635   if (randr12_compat)
636     {
637       guint n_monitors = monitors->len;
638
639       free_monitors ((GdkX11Monitor *)g_array_free (monitors, FALSE),
640                      n_monitors);
641
642       return FALSE;
643     }
644
645   g_array_sort (monitors,
646                 (GCompareFunc) monitor_compare_function);
647   x11_screen->n_monitors = monitors->len;
648   x11_screen->monitors = (GdkX11Monitor *)g_array_free (monitors, FALSE);
649
650   x11_screen->primary_monitor = 0;
651
652   primary_output = XRRGetOutputPrimary (x11_screen->xdisplay,
653                                         x11_screen->xroot_window);
654
655   for (i = 0; i < x11_screen->n_monitors; ++i)
656     {
657       if (x11_screen->monitors[i].output == primary_output)
658         {
659           x11_screen->primary_monitor = i;
660           break;
661         }
662
663       /* No RandR1.3+ available or no primary set, fall back to prefer LVDS as primary if present */
664       if (primary_output == None &&
665           g_ascii_strncasecmp (x11_screen->monitors[i].output_name, "LVDS", 4) == 0)
666         {
667           x11_screen->primary_monitor = i;
668           break;
669         }
670
671       /* No primary specified and no LVDS found */
672       if (x11_screen->monitors[i].output == first_output)
673         x11_screen->primary_monitor = i;
674     }
675
676   return x11_screen->n_monitors > 0;
677 #endif
678
679   return FALSE;
680 }
681
682 static gboolean
683 init_solaris_xinerama (GdkScreen *screen)
684 {
685 #ifdef HAVE_SOLARIS_XINERAMA
686   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
687   int screen_no = gdk_screen_get_number (screen);
688   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
689   XRectangle monitors[MAXFRAMEBUFFERS];
690   unsigned char hints[16];
691   gint result;
692   int n_monitors;
693   int i;
694   
695   if (!XineramaGetState (dpy, screen_no))
696     return FALSE;
697
698   result = XineramaGetInfo (dpy, screen_no, monitors, hints, &n_monitors);
699
700   /* Yes I know it should be Success but the current implementation 
701    * returns the num of monitor
702    */
703   if (result == 0)
704     {
705       return FALSE;
706     }
707
708   x11_screen->monitors = g_new0 (GdkX11Monitor, n_monitors);
709   x11_screen->n_monitors = n_monitors;
710
711   for (i = 0; i < n_monitors; i++)
712     {
713       init_monitor_geometry (&x11_screen->monitors[i],
714                              monitors[i].x, monitors[i].y,
715                              monitors[i].width, monitors[i].height);
716     }
717
718   x11_screen->primary_monitor = 0;
719
720   return TRUE;
721 #endif /* HAVE_SOLARIS_XINERAMA */
722
723   return FALSE;
724 }
725
726 static gboolean
727 init_xfree_xinerama (GdkScreen *screen)
728 {
729 #ifdef HAVE_XFREE_XINERAMA
730   Display *dpy = GDK_SCREEN_XDISPLAY (screen);
731   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
732   XineramaScreenInfo *monitors;
733   int i, n_monitors;
734   
735   if (!XineramaIsActive (dpy))
736     return FALSE;
737
738   monitors = XineramaQueryScreens (dpy, &n_monitors);
739   
740   if (n_monitors <= 0 || monitors == NULL)
741     {
742       /* If Xinerama doesn't think we have any monitors, try acting as
743        * though we had no Xinerama. If the "no monitors" condition
744        * is because XRandR 1.2 is currently switching between CRTCs,
745        * we'll be notified again when we have our monitor back,
746        * and can go back into Xinerama-ish mode at that point.
747        */
748       if (monitors)
749         XFree (monitors);
750       
751       return FALSE;
752     }
753
754   x11_screen->n_monitors = n_monitors;
755   x11_screen->monitors = g_new0 (GdkX11Monitor, n_monitors);
756   
757   for (i = 0; i < n_monitors; ++i)
758     {
759       init_monitor_geometry (&x11_screen->monitors[i],
760                              monitors[i].x_org, monitors[i].y_org,
761                              monitors[i].width, monitors[i].height);
762     }
763   
764   XFree (monitors);
765   
766   x11_screen->primary_monitor = 0;
767
768   return TRUE;
769 #endif /* HAVE_XFREE_XINERAMA */
770   
771   return FALSE;
772 }
773
774 static void
775 deinit_multihead (GdkScreen *screen)
776 {
777   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
778
779   free_monitors (x11_screen->monitors, x11_screen->n_monitors);
780
781   x11_screen->n_monitors = 0;
782   x11_screen->monitors = NULL;
783 }
784
785 static gboolean
786 compare_monitor (GdkX11Monitor *m1,
787                  GdkX11Monitor *m2)
788 {
789   if (m1->geometry.x != m2->geometry.x ||
790       m1->geometry.y != m2->geometry.y ||
791       m1->geometry.width != m2->geometry.width ||
792       m1->geometry.height != m2->geometry.height)
793     return FALSE;
794
795   if (m1->width_mm != m2->width_mm ||
796       m1->height_mm != m2->height_mm)
797     return FALSE;
798
799   if (g_strcmp0 (m1->output_name, m2->output_name) != 0)
800     return FALSE;
801
802   if (g_strcmp0 (m1->manufacturer, m2->manufacturer) != 0)
803     return FALSE;
804
805   return TRUE;
806 }
807
808 static gboolean
809 compare_monitors (GdkX11Monitor *monitors1, gint n_monitors1,
810                   GdkX11Monitor *monitors2, gint n_monitors2)
811 {
812   gint i;
813
814   if (n_monitors1 != n_monitors2)
815     return FALSE;
816
817   for (i = 0; i < n_monitors1; i++)
818     {
819       if (!compare_monitor (monitors1 + i, monitors2 + i))
820         return FALSE;
821     }
822
823   return TRUE;
824 }
825
826 static void
827 init_multihead (GdkScreen *screen)
828 {
829   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
830   int opcode, firstevent, firsterror;
831
832   /* There are four different implementations of multihead support: 
833    *
834    *  1. Fake Xinerama for debugging purposes
835    *  2. RandR 1.2
836    *  3. Solaris Xinerama
837    *  4. XFree86/Xorg Xinerama
838    *
839    * We use them in that order.
840    */
841   if (init_fake_xinerama (screen))
842     return;
843
844   if (init_randr13 (screen))
845     return;
846
847   if (XQueryExtension (GDK_SCREEN_XDISPLAY (screen), "XINERAMA",
848                        &opcode, &firstevent, &firsterror))
849     {
850       if (init_solaris_xinerama (screen))
851         return;
852       
853       if (init_xfree_xinerama (screen))
854         return;
855     }
856
857   /* No multihead support of any kind for this screen */
858   x11_screen->n_monitors = 1;
859   x11_screen->monitors = g_new0 (GdkX11Monitor, 1);
860   x11_screen->primary_monitor = 0;
861
862   init_monitor_geometry (x11_screen->monitors, 0, 0,
863                          WidthOfScreen (x11_screen->xscreen),
864                          HeightOfScreen (x11_screen->xscreen));
865 }
866
867 GdkScreen *
868 _gdk_x11_screen_new (GdkDisplay *display,
869                      gint        screen_number) 
870 {
871   GdkScreen *screen;
872   GdkX11Screen *x11_screen;
873   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
874
875   screen = g_object_new (GDK_TYPE_X11_SCREEN, NULL);
876
877   x11_screen = GDK_X11_SCREEN (screen);
878   x11_screen->display = display;
879   x11_screen->xdisplay = display_x11->xdisplay;
880   x11_screen->xscreen = ScreenOfDisplay (display_x11->xdisplay, screen_number);
881   x11_screen->screen_num = screen_number;
882   x11_screen->xroot_window = RootWindow (display_x11->xdisplay,screen_number);
883   x11_screen->wmspec_check_window = None;
884   /* we want this to be always non-null */
885   x11_screen->window_manager_name = g_strdup ("unknown");
886   
887   init_multihead (screen);
888   init_randr_support (screen);
889   
890   _gdk_x11_screen_init_visuals (screen);
891   _gdk_x11_screen_init_root_window (screen);
892   
893   return screen;
894 }
895
896 /*
897  * It is important that we first request the selection
898  * notification, and then setup the initial state of
899  * is_composited to avoid a race condition here.
900  */
901 void
902 _gdk_x11_screen_setup (GdkScreen *screen)
903 {
904   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
905
906   x11_screen->cm_selection_atom = make_cm_atom (x11_screen->screen_num);
907   gdk_display_request_selection_notification (x11_screen->display,
908                                               x11_screen->cm_selection_atom);
909   x11_screen->is_composited = check_is_composited (x11_screen->display, x11_screen);
910 }
911
912 static gboolean
913 gdk_x11_screen_is_composited (GdkScreen *screen)
914 {
915   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
916
917   return x11_screen->is_composited;
918 }
919
920 static void
921 init_randr_support (GdkScreen *screen)
922 {
923   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
924
925   XSelectInput (GDK_SCREEN_XDISPLAY (screen),
926                 x11_screen->xroot_window,
927                 StructureNotifyMask);
928
929 #ifdef HAVE_RANDR
930   if (!GDK_X11_DISPLAY (gdk_screen_get_display (screen))->have_randr12)
931     return;
932
933   XRRSelectInput (GDK_SCREEN_XDISPLAY (screen),
934                   x11_screen->xroot_window,
935                   RRScreenChangeNotifyMask
936                   | RRCrtcChangeNotifyMask
937                   | RROutputPropertyNotifyMask);
938 #endif
939 }
940
941 static void
942 process_monitors_change (GdkScreen *screen)
943 {
944   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
945   gint           n_monitors;
946   gint           primary_monitor;
947   GdkX11Monitor *monitors;
948   gboolean changed;
949
950   primary_monitor = x11_screen->primary_monitor;
951   n_monitors = x11_screen->n_monitors;
952   monitors = x11_screen->monitors;
953
954   x11_screen->n_monitors = 0;
955   x11_screen->monitors = NULL;
956
957   init_multihead (screen);
958
959   changed =
960     !compare_monitors (monitors, n_monitors,
961                        x11_screen->monitors, x11_screen->n_monitors) ||
962     x11_screen->primary_monitor != primary_monitor;
963
964
965   free_monitors (monitors, n_monitors);
966
967   if (changed)
968     g_signal_emit_by_name (screen, "monitors-changed");
969 }
970
971 void
972 _gdk_x11_screen_size_changed (GdkScreen *screen,
973                               XEvent    *event)
974 {
975   gint width, height;
976 #ifdef HAVE_RANDR
977   GdkX11Display *display_x11;
978 #endif
979
980   width = gdk_screen_get_width (screen);
981   height = gdk_screen_get_height (screen);
982
983 #ifdef HAVE_RANDR
984   display_x11 = GDK_X11_DISPLAY (gdk_screen_get_display (screen));
985
986   if (display_x11->have_randr13 && event->type == ConfigureNotify)
987     return;
988
989   XRRUpdateConfiguration (event);
990 #else
991   if (event->type == ConfigureNotify)
992     {
993       XConfigureEvent *rcevent = (XConfigureEvent *) event;
994       Screen        *xscreen = gdk_x11_screen_get_xscreen (screen);
995
996       xscreen->width   = rcevent->width;
997       xscreen->height  = rcevent->height;
998     }
999   else
1000     return;
1001 #endif
1002
1003   process_monitors_change (screen);
1004
1005   if (width != gdk_screen_get_width (screen) ||
1006       height != gdk_screen_get_height (screen))
1007     g_signal_emit_by_name (screen, "size-changed");
1008 }
1009
1010 void
1011 _gdk_x11_screen_window_manager_changed (GdkScreen *screen)
1012 {
1013   g_signal_emit (screen, signals[WINDOW_MANAGER_CHANGED], 0);
1014 }
1015
1016 void
1017 _gdk_x11_screen_process_owner_change (GdkScreen *screen,
1018                                       XEvent *event)
1019 {
1020 #ifdef HAVE_XFIXES
1021   XFixesSelectionNotifyEvent *selection_event = (XFixesSelectionNotifyEvent *)event;
1022   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
1023   Atom xcm_selection_atom = gdk_x11_atom_to_xatom_for_display (x11_screen->display,
1024                                                                x11_screen->cm_selection_atom);
1025
1026   if (selection_event->selection == xcm_selection_atom)
1027     {
1028       gboolean composited = selection_event->owner != None;
1029
1030       if (composited != x11_screen->is_composited)
1031         {
1032           x11_screen->is_composited = composited;
1033
1034           g_signal_emit_by_name (screen, "composited-changed");
1035         }
1036     }
1037 #endif
1038 }
1039
1040 static gchar *
1041 substitute_screen_number (const gchar *display_name,
1042                           gint         screen_number)
1043 {
1044   GString *str;
1045   gchar   *p;
1046
1047   str = g_string_new (display_name);
1048
1049   p = strrchr (str->str, '.');
1050   if (p && p >  strchr (str->str, ':'))
1051     g_string_truncate (str, p - str->str);
1052
1053   g_string_append_printf (str, ".%d", screen_number);
1054
1055   return g_string_free (str, FALSE);
1056 }
1057
1058 static gchar *
1059 gdk_x11_screen_make_display_name (GdkScreen *screen)
1060 {
1061   const gchar *old_display;
1062
1063   old_display = gdk_display_get_name (gdk_screen_get_display (screen));
1064
1065   return substitute_screen_number (old_display,
1066                                    gdk_screen_get_number (screen));
1067 }
1068
1069 static GdkWindow *
1070 gdk_x11_screen_get_active_window (GdkScreen *screen)
1071 {
1072   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
1073   GdkWindow *ret = NULL;
1074   Atom type_return;
1075   gint format_return;
1076   gulong nitems_return;
1077   gulong bytes_after_return;
1078   guchar *data = NULL;
1079
1080   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1081                                             gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
1082     return NULL;
1083
1084   if (XGetWindowProperty (x11_screen->xdisplay, x11_screen->xroot_window,
1085                           gdk_x11_get_xatom_by_name_for_display (x11_screen->display,
1086                                                                  "_NET_ACTIVE_WINDOW"),
1087                           0, 1, False, XA_WINDOW, &type_return,
1088                           &format_return, &nitems_return,
1089                           &bytes_after_return, &data)
1090       == Success)
1091     {
1092       if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
1093         {
1094           Window window = *(Window *) data;
1095
1096           if (window != None)
1097             {
1098               ret = gdk_x11_window_foreign_new_for_display (x11_screen->display,
1099                                                             window);
1100             }
1101         }
1102     }
1103
1104   if (data)
1105     XFree (data);
1106
1107   return ret;
1108 }
1109
1110 static GList *
1111 gdk_x11_screen_get_window_stack (GdkScreen *screen)
1112 {
1113   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
1114   GList *ret = NULL;
1115   Atom type_return;
1116   gint format_return;
1117   gulong nitems_return;
1118   gulong bytes_after_return;
1119   guchar *data = NULL;
1120
1121   if (!gdk_x11_screen_supports_net_wm_hint (screen,
1122                                             gdk_atom_intern_static_string ("_NET_CLIENT_LIST_STACKING")))
1123     return NULL;
1124
1125   if (XGetWindowProperty (x11_screen->xdisplay, x11_screen->xroot_window,
1126                           gdk_x11_get_xatom_by_name_for_display (x11_screen->display,
1127                                                                  "_NET_CLIENT_LIST_STACKING"),
1128                           0, G_MAXLONG, False, XA_WINDOW, &type_return,
1129                           &format_return, &nitems_return,
1130                           &bytes_after_return, &data)
1131       == Success)
1132     {
1133       if ((type_return == XA_WINDOW) && (format_return == 32) &&
1134           (data) && (nitems_return > 0))
1135         {
1136           gulong *stack = (gulong *) data;
1137           GdkWindow *win;
1138           int i;
1139
1140           for (i = 0; i < nitems_return; i++)
1141             {
1142               win = gdk_x11_window_foreign_new_for_display (x11_screen->display,
1143                                                             (Window)stack[i]);
1144
1145               if (win != NULL)
1146                 ret = g_list_append (ret, win);
1147             }
1148         }
1149     }
1150
1151   if (data)
1152     XFree (data);
1153
1154   return ret;
1155 }
1156
1157 static gboolean
1158 check_transform (const gchar *xsettings_name,
1159                  GType        src_type,
1160                  GType        dest_type)
1161 {
1162   if (!g_value_type_transformable (src_type, dest_type))
1163     {
1164       g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
1165                  xsettings_name,
1166                  g_type_name (src_type),
1167                  g_type_name (dest_type));
1168       return FALSE;
1169     }
1170   else
1171     return TRUE;
1172 }
1173
1174 static gboolean
1175 gdk_x11_screen_get_setting (GdkScreen   *screen,
1176                             const gchar *name,
1177                             GValue      *value)
1178 {
1179   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
1180   const char *xsettings_name = NULL;
1181   XSettingsResult result;
1182   XSettingsSetting *setting = NULL;
1183   gboolean success = FALSE;
1184   gint i;
1185   GValue tmp_val = G_VALUE_INIT;
1186
1187   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS(); i++)
1188     if (strcmp (GDK_SETTINGS_GDK_NAME (i), name) == 0)
1189       {
1190         xsettings_name = GDK_SETTINGS_X_NAME (i);
1191         break;
1192       }
1193
1194   if (!xsettings_name)
1195     goto out;
1196
1197   result = xsettings_client_get_setting (x11_screen->xsettings_client,
1198                                          xsettings_name, &setting);
1199   if (result != XSETTINGS_SUCCESS)
1200     goto out;
1201
1202   switch (setting->type)
1203     {
1204     case XSETTINGS_TYPE_INT:
1205       if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
1206         {
1207           g_value_init (&tmp_val, G_TYPE_INT);
1208           g_value_set_int (&tmp_val, setting->data.v_int);
1209           g_value_transform (&tmp_val, value);
1210
1211           success = TRUE;
1212         }
1213       break;
1214     case XSETTINGS_TYPE_STRING:
1215       if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
1216         {
1217           g_value_init (&tmp_val, G_TYPE_STRING);
1218           g_value_set_string (&tmp_val, setting->data.v_string);
1219           g_value_transform (&tmp_val, value);
1220
1221           success = TRUE;
1222         }
1223       break;
1224     case XSETTINGS_TYPE_COLOR:
1225       if (!check_transform (xsettings_name, GDK_TYPE_RGBA, G_VALUE_TYPE (value)))
1226         {
1227           GdkRGBA rgba;
1228
1229           g_value_init (&tmp_val, GDK_TYPE_RGBA);
1230
1231           rgba.red = setting->data.v_color.red / 65535.0;
1232           rgba.green = setting->data.v_color.green / 65535.0;
1233           rgba.blue = setting->data.v_color.blue / 65535.0;
1234           rgba.alpha = setting->data.v_color.alpha / 65535.0;
1235
1236           g_value_set_boxed (&tmp_val, &rgba);
1237
1238           g_value_transform (&tmp_val, value);
1239
1240           success = TRUE;
1241         }
1242       break;
1243     }
1244
1245   g_value_unset (&tmp_val);
1246
1247  out:
1248   if (setting)
1249     xsettings_setting_free (setting);
1250
1251   if (success)
1252     return TRUE;
1253   else
1254     return _gdk_x11_get_xft_setting (screen, name, value);
1255 }
1256
1257 static void
1258 cleanup_atoms(gpointer data)
1259 {
1260   NetWmSupportedAtoms *supported_atoms = data;
1261   if (supported_atoms->atoms)
1262       XFree (supported_atoms->atoms);
1263   g_free (supported_atoms);
1264 }
1265
1266 static Window
1267 get_net_supporting_wm_check (GdkX11Screen *screen,
1268                              Window        window)
1269 {
1270   GdkDisplay *display;
1271   Atom type;
1272   gint format;
1273   gulong n_items;
1274   gulong bytes_after;
1275   guchar *data;
1276   Window value;
1277
1278   display = screen->display;
1279   type = None;
1280   data = NULL;
1281   value = None;
1282
1283   gdk_x11_display_error_trap_push (display);
1284   XGetWindowProperty (screen->xdisplay, window,
1285                       gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
1286                       0, G_MAXLONG, False, XA_WINDOW, &type, &format,
1287                       &n_items, &bytes_after, &data);
1288   gdk_x11_display_error_trap_pop_ignored (display);
1289
1290   if (type == XA_WINDOW)
1291     value = *(Window *)data;
1292
1293   if (data)
1294     XFree (data);
1295
1296   return value;
1297 }
1298
1299 static void
1300 fetch_net_wm_check_window (GdkScreen *screen)
1301 {
1302   GdkX11Screen *x11_screen;
1303   GdkDisplay *display;
1304   Window window;
1305   GTimeVal tv;
1306   gint error;
1307
1308   x11_screen = GDK_X11_SCREEN (screen);
1309   display = x11_screen->display;
1310
1311   g_return_if_fail (GDK_X11_DISPLAY (display)->trusted_client);
1312
1313   if (x11_screen->wmspec_check_window != None)
1314     return; /* already have it */
1315
1316   g_get_current_time (&tv);
1317
1318   if (ABS  (tv.tv_sec - x11_screen->last_wmspec_check_time) < 15)
1319     return; /* we've checked recently */
1320
1321   window = get_net_supporting_wm_check (x11_screen, x11_screen->xroot_window);
1322   if (window == None)
1323     return;
1324
1325   if (window != get_net_supporting_wm_check (x11_screen, window))
1326     return;
1327
1328   gdk_x11_display_error_trap_push (display);
1329
1330   /* Find out if this WM goes away, so we can reset everything. */
1331   XSelectInput (x11_screen->xdisplay, window, StructureNotifyMask);
1332
1333   error = gdk_x11_display_error_trap_pop (display);
1334   if (!error)
1335     {
1336       /* We check the window property again because after XGetWindowProperty()
1337        * and before XSelectInput() the window may have been recycled in such a
1338        * way that XSelectInput() doesn't fail but the window is no longer what
1339        * we want.
1340        */
1341       if (window != get_net_supporting_wm_check (x11_screen, window))
1342         return;
1343
1344       x11_screen->wmspec_check_window = window;
1345       x11_screen->last_wmspec_check_time = tv.tv_sec;
1346       x11_screen->need_refetch_net_supported = TRUE;
1347       x11_screen->need_refetch_wm_name = TRUE;
1348
1349       /* Careful, reentrancy */
1350       _gdk_x11_screen_window_manager_changed (screen);
1351     }
1352 }
1353
1354 /**
1355  * gdk_x11_screen_supports_net_wm_hint:
1356  * @screen: (type GdkX11Screen): the relevant #GdkScreen.
1357  * @property: a property atom.
1358  *
1359  * This function is specific to the X11 backend of GDK, and indicates
1360  * whether the window manager supports a certain hint from the
1361  * Extended Window Manager Hints Specification. You can find this
1362  * specification on
1363  * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
1364  *
1365  * When using this function, keep in mind that the window manager
1366  * can change over time; so you shouldn't use this function in
1367  * a way that impacts persistent application state. A common bug
1368  * is that your application can start up before the window manager
1369  * does when the user logs in, and before the window manager starts
1370  * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
1371  * You can monitor the window_manager_changed signal on #GdkScreen to detect
1372  * a window manager change.
1373  *
1374  * Return value: %TRUE if the window manager supports @property
1375  *
1376  * Since: 2.2
1377  **/
1378 gboolean
1379 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
1380                                      GdkAtom    property)
1381 {
1382   gulong i;
1383   GdkX11Screen *x11_screen;
1384   NetWmSupportedAtoms *supported_atoms;
1385   GdkDisplay *display;
1386
1387   g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
1388
1389   x11_screen = GDK_X11_SCREEN (screen);
1390   display = x11_screen->display;
1391
1392   if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1393     return FALSE;
1394
1395   supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
1396   if (!supported_atoms)
1397     {
1398       supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
1399       g_object_set_data_full (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms, cleanup_atoms);
1400     }
1401
1402   fetch_net_wm_check_window (screen);
1403
1404   if (x11_screen->wmspec_check_window == None)
1405     return FALSE;
1406
1407   if (x11_screen->need_refetch_net_supported)
1408     {
1409       /* WM has changed since we last got the supported list,
1410        * refetch it.
1411        */
1412       Atom type;
1413       gint format;
1414       gulong bytes_after;
1415
1416       x11_screen->need_refetch_net_supported = FALSE;
1417
1418       if (supported_atoms->atoms)
1419         XFree (supported_atoms->atoms);
1420
1421       supported_atoms->atoms = NULL;
1422       supported_atoms->n_atoms = 0;
1423
1424       XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), x11_screen->xroot_window,
1425                           gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
1426                           0, G_MAXLONG, False, XA_ATOM, &type, &format,
1427                           &supported_atoms->n_atoms, &bytes_after,
1428                           (guchar **)&supported_atoms->atoms);
1429
1430       if (type != XA_ATOM)
1431         return FALSE;
1432     }
1433
1434   if (supported_atoms->atoms == NULL)
1435     return FALSE;
1436
1437   i = 0;
1438   while (i < supported_atoms->n_atoms)
1439     {
1440       if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
1441         return TRUE;
1442
1443       ++i;
1444     }
1445
1446   return FALSE;
1447 }
1448
1449 static void
1450 refcounted_grab_server (Display *xdisplay)
1451 {
1452   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
1453
1454   gdk_x11_display_grab (display);
1455 }
1456
1457 static void
1458 refcounted_ungrab_server (Display *xdisplay)
1459 {
1460   GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
1461
1462   gdk_x11_display_ungrab (display);
1463 }
1464
1465 static GdkFilterReturn
1466 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
1467                                    GdkEvent  *event,
1468                                    gpointer   data)
1469 {
1470   GdkX11Screen *screen = data;
1471
1472   if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
1473     return GDK_FILTER_REMOVE;
1474   else
1475     return GDK_FILTER_CONTINUE;
1476 }
1477
1478 static Bool
1479 gdk_xsettings_watch_cb (Window   window,
1480                         Bool     is_start,
1481                         long     mask,
1482                         void    *cb_data)
1483 {
1484   GdkWindow *gdkwin;
1485   GdkScreen *screen = cb_data;
1486
1487   gdkwin = gdk_x11_window_lookup_for_display (gdk_screen_get_display (screen), window);
1488
1489   if (is_start)
1490     {
1491       if (gdkwin)
1492         g_object_ref (gdkwin);
1493       else
1494         {
1495           gdkwin = gdk_x11_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
1496           
1497           /* gdk_window_foreign_new_for_display() can fail and return NULL if the
1498            * window has already been destroyed.
1499            */
1500           if (!gdkwin)
1501             return False;
1502         }
1503
1504       gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
1505     }
1506   else
1507     {
1508       if (!gdkwin)
1509         {
1510           /* gdkwin should not be NULL here, since if starting the watch succeeded
1511            * we have a reference on the window. It might mean that the caller didn't
1512            * remove the watch when it got a DestroyNotify event. Or maybe the
1513            * caller ignored the return value when starting the watch failed.
1514            */
1515           g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
1516           return False;
1517         }
1518       
1519       gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
1520       g_object_unref (gdkwin);
1521     }
1522
1523   return True;
1524 }
1525
1526 static void
1527 gdk_xsettings_notify_cb (const char       *name,
1528                          XSettingsAction   action,
1529                          XSettingsSetting *setting,
1530                          void             *data)
1531 {
1532   GdkEvent new_event;
1533   GdkScreen *screen = data;
1534   GdkX11Screen *x11_screen = data;
1535   int i;
1536
1537   if (x11_screen->xsettings_in_init)
1538     return;
1539   
1540   new_event.type = GDK_SETTING;
1541   new_event.setting.window = gdk_screen_get_root_window (screen);
1542   new_event.setting.send_event = FALSE;
1543   new_event.setting.name = NULL;
1544
1545   for (i = 0; i < GDK_SETTINGS_N_ELEMENTS() ; i++)
1546     if (strcmp (GDK_SETTINGS_X_NAME (i), name) == 0)
1547       {
1548         new_event.setting.name = (char*) GDK_SETTINGS_GDK_NAME (i);
1549         break;
1550       }
1551   
1552   if (!new_event.setting.name)
1553     return;
1554   
1555   switch (action)
1556     {
1557     case XSETTINGS_ACTION_NEW:
1558       new_event.setting.action = GDK_SETTING_ACTION_NEW;
1559       break;
1560     case XSETTINGS_ACTION_CHANGED:
1561       new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
1562       break;
1563     case XSETTINGS_ACTION_DELETED:
1564       new_event.setting.action = GDK_SETTING_ACTION_DELETED;
1565       break;
1566     }
1567
1568   gdk_event_put (&new_event);
1569 }
1570
1571 void
1572 _gdk_x11_screen_init_events (GdkScreen *screen)
1573 {
1574   GdkX11Screen *x11_screen = GDK_X11_SCREEN (screen);
1575
1576   /* Keep a flag to avoid extra notifies that we don't need
1577    */
1578   x11_screen->xsettings_in_init = TRUE;
1579   x11_screen->xsettings_client = xsettings_client_new_with_grab_funcs (x11_screen->xdisplay,
1580                                                                        x11_screen->screen_num,
1581                                                                        gdk_xsettings_notify_cb,
1582                                                                        gdk_xsettings_watch_cb,
1583                                                                        screen,
1584                                                                        refcounted_grab_server,
1585                                                                        refcounted_ungrab_server);
1586   x11_screen->xsettings_in_init = FALSE;
1587 }
1588
1589 /**
1590  * gdk_x11_screen_get_window_manager_name:
1591  * @screen: (type GdkX11Screen): a #GdkScreen
1592  *
1593  * Returns the name of the window manager for @screen.
1594  *
1595  * Return value: the name of the window manager screen @screen, or
1596  * "unknown" if the window manager is unknown. The string is owned by GDK
1597  * and should not be freed.
1598  *
1599  * Since: 2.2
1600  **/
1601 const char*
1602 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
1603 {
1604   GdkX11Screen *x11_screen;
1605   GdkDisplay *display;
1606
1607   x11_screen = GDK_X11_SCREEN (screen);
1608   display = x11_screen->display;
1609
1610   if (!G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
1611     return x11_screen->window_manager_name;
1612
1613   fetch_net_wm_check_window (screen);
1614
1615   if (x11_screen->need_refetch_wm_name)
1616     {
1617       /* Get the name of the window manager */
1618       x11_screen->need_refetch_wm_name = FALSE;
1619
1620       g_free (x11_screen->window_manager_name);
1621       x11_screen->window_manager_name = g_strdup ("unknown");
1622
1623       if (x11_screen->wmspec_check_window != None)
1624         {
1625           Atom type;
1626           gint format;
1627           gulong n_items;
1628           gulong bytes_after;
1629           gchar *name;
1630
1631           name = NULL;
1632
1633           gdk_x11_display_error_trap_push (display);
1634
1635           XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
1636                               x11_screen->wmspec_check_window,
1637                               gdk_x11_get_xatom_by_name_for_display (display,
1638                                                                      "_NET_WM_NAME"),
1639                               0, G_MAXLONG, False,
1640                               gdk_x11_get_xatom_by_name_for_display (display,
1641                                                                      "UTF8_STRING"),
1642                               &type, &format,
1643                               &n_items, &bytes_after,
1644                               (guchar **)&name);
1645
1646           gdk_x11_display_error_trap_pop_ignored (display);
1647
1648           if (name != NULL)
1649             {
1650               g_free (x11_screen->window_manager_name);
1651               x11_screen->window_manager_name = g_strdup (name);
1652               XFree (name);
1653             }
1654         }
1655     }
1656
1657   return GDK_X11_SCREEN (screen)->window_manager_name;
1658 }
1659
1660 static void
1661 gdk_x11_screen_class_init (GdkX11ScreenClass *klass)
1662 {
1663   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1664   GdkScreenClass *screen_class = GDK_SCREEN_CLASS (klass);
1665
1666   object_class->dispose = gdk_x11_screen_dispose;
1667   object_class->finalize = gdk_x11_screen_finalize;
1668
1669   screen_class->get_display = gdk_x11_screen_get_display;
1670   screen_class->get_width = gdk_x11_screen_get_width;
1671   screen_class->get_height = gdk_x11_screen_get_height;
1672   screen_class->get_width_mm = gdk_x11_screen_get_width_mm;
1673   screen_class->get_height_mm = gdk_x11_screen_get_height_mm;
1674   screen_class->get_number = gdk_x11_screen_get_number;
1675   screen_class->get_root_window = gdk_x11_screen_get_root_window;
1676   screen_class->get_n_monitors = gdk_x11_screen_get_n_monitors;
1677   screen_class->get_primary_monitor = gdk_x11_screen_get_primary_monitor;
1678   screen_class->get_monitor_width_mm = gdk_x11_screen_get_monitor_width_mm;
1679   screen_class->get_monitor_height_mm = gdk_x11_screen_get_monitor_height_mm;
1680   screen_class->get_monitor_plug_name = gdk_x11_screen_get_monitor_plug_name;
1681   screen_class->get_monitor_geometry = gdk_x11_screen_get_monitor_geometry;
1682   screen_class->get_monitor_workarea = gdk_x11_screen_get_monitor_workarea;
1683   screen_class->get_system_visual = _gdk_x11_screen_get_system_visual;
1684   screen_class->get_rgba_visual = gdk_x11_screen_get_rgba_visual;
1685   screen_class->is_composited = gdk_x11_screen_is_composited;
1686   screen_class->make_display_name = gdk_x11_screen_make_display_name;
1687   screen_class->get_active_window = gdk_x11_screen_get_active_window;
1688   screen_class->get_window_stack = gdk_x11_screen_get_window_stack;
1689   screen_class->get_setting = gdk_x11_screen_get_setting;
1690   screen_class->visual_get_best_depth = _gdk_x11_screen_visual_get_best_depth;
1691   screen_class->visual_get_best_type = _gdk_x11_screen_visual_get_best_type;
1692   screen_class->visual_get_best = _gdk_x11_screen_visual_get_best;
1693   screen_class->visual_get_best_with_depth = _gdk_x11_screen_visual_get_best_with_depth;
1694   screen_class->visual_get_best_with_type = _gdk_x11_screen_visual_get_best_with_type;
1695   screen_class->visual_get_best_with_both = _gdk_x11_screen_visual_get_best_with_both;
1696   screen_class->query_depths = _gdk_x11_screen_query_depths;
1697   screen_class->query_visual_types = _gdk_x11_screen_query_visual_types;
1698   screen_class->list_visuals = _gdk_x11_screen_list_visuals;
1699
1700   signals[WINDOW_MANAGER_CHANGED] =
1701     g_signal_new (g_intern_static_string ("window-manager-changed"),
1702                   G_OBJECT_CLASS_TYPE (object_class),
1703                   G_SIGNAL_RUN_LAST,
1704                   G_STRUCT_OFFSET (GdkX11ScreenClass, window_manager_changed),
1705                   NULL, NULL,
1706                   g_cclosure_marshal_VOID__VOID,
1707                   G_TYPE_NONE,
1708                   0);
1709 }