2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include <cairo-xlib.h>
34 #include <string.h> /* for memcpy() */
36 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
41 #include <X11/extensions/XShm.h>
44 #include "gdkprivate-x11.h"
45 #include "gdkdrawable-x11.h"
46 #include "gdkpixmap-x11.h"
47 #include "gdkscreen-x11.h"
48 #include "gdkdisplay-x11.h"
51 static cairo_surface_t *gdk_x11_ref_cairo_surface (GdkDrawable *drawable);
53 static void gdk_x11_set_colormap (GdkDrawable *drawable,
54 GdkColormap *colormap);
56 static GdkColormap* gdk_x11_get_colormap (GdkDrawable *drawable);
57 static gint gdk_x11_get_depth (GdkDrawable *drawable);
58 static GdkScreen * gdk_x11_get_screen (GdkDrawable *drawable);
59 static GdkVisual* gdk_x11_get_visual (GdkDrawable *drawable);
61 static void gdk_drawable_impl_x11_finalize (GObject *object);
63 static const cairo_user_data_key_t gdk_x11_cairo_key;
65 G_DEFINE_TYPE (GdkDrawableImplX11, _gdk_drawable_impl_x11, GDK_TYPE_DRAWABLE)
68 _gdk_drawable_impl_x11_class_init (GdkDrawableImplX11Class *klass)
70 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
71 GObjectClass *object_class = G_OBJECT_CLASS (klass);
73 object_class->finalize = gdk_drawable_impl_x11_finalize;
75 drawable_class->ref_cairo_surface = gdk_x11_ref_cairo_surface;
77 drawable_class->set_colormap = gdk_x11_set_colormap;
78 drawable_class->get_colormap = gdk_x11_get_colormap;
80 drawable_class->get_depth = gdk_x11_get_depth;
81 drawable_class->get_screen = gdk_x11_get_screen;
82 drawable_class->get_visual = gdk_x11_get_visual;
86 _gdk_drawable_impl_x11_init (GdkDrawableImplX11 *impl)
91 gdk_drawable_impl_x11_finalize (GObject *object)
93 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
95 G_OBJECT_CLASS (_gdk_drawable_impl_x11_parent_class)->finalize (object);
99 * _gdk_x11_drawable_finish:
100 * @drawable: a #GdkDrawableImplX11.
102 * Performs necessary cleanup prior to freeing a pixmap or
103 * destroying a window.
106 _gdk_x11_drawable_finish (GdkDrawable *drawable)
108 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
112 XRenderFreePicture (GDK_SCREEN_XDISPLAY (impl->screen),
114 impl->picture = None;
117 if (impl->cairo_surface)
119 cairo_surface_finish (impl->cairo_surface);
120 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
126 * _gdk_x11_drawable_update_size:
127 * @drawable: a #GdkDrawableImplX11.
129 * Updates the state of the drawable (in particular the drawable's
130 * cairo surface) when its size has changed.
133 _gdk_x11_drawable_update_size (GdkDrawable *drawable)
135 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
137 if (impl->cairo_surface)
141 gdk_drawable_get_size (drawable, &width, &height);
142 cairo_xlib_surface_set_size (impl->cairo_surface, width, height);
147 try_pixmap (Display *xdisplay,
151 Pixmap pixmap = XCreatePixmap (xdisplay,
152 RootWindow (xdisplay, screen),
154 XFreePixmap (xdisplay, pixmap);
158 _gdk_x11_have_render (GdkDisplay *display)
160 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
161 GdkDisplayX11 *x11display = GDK_DISPLAY_X11 (display);
163 if (x11display->have_render == GDK_UNKNOWN)
165 int event_base, error_base;
166 x11display->have_render =
167 XRenderQueryExtension (xdisplay, &event_base, &error_base)
170 if (x11display->have_render == GDK_YES)
173 * Sun advertises RENDER, but fails to support 32-bit pixmaps.
174 * That is just no good. Therefore, we check all screens
175 * for proper support.
179 for (screen = 0; screen < ScreenCount (xdisplay); screen++)
182 int *depths = XListDepths (xdisplay, screen, &count);
183 gboolean has_8 = FALSE, has_32 = FALSE;
189 for (i = 0; i < count; i++)
193 else if (depths[i] == 32)
199 /* At this point, we might have a false positive;
200 * buggy versions of Xinerama only report depths for
201 * which there is an associated visual; so we actually
202 * go ahead and try create pixmaps.
204 if (!(has_8 && has_32))
206 gdk_error_trap_push ();
208 try_pixmap (xdisplay, screen, 8);
210 try_pixmap (xdisplay, screen, 32);
211 XSync (xdisplay, False);
212 if (gdk_error_trap_pop () == 0)
219 if (!(has_8 && has_32))
221 g_warning ("The X server advertises that RENDER support is present,\n"
222 "but fails to supply the necessary pixmap support. In\n"
223 "other words, it is buggy.");
224 x11display->have_render = GDK_NO;
231 return x11display->have_render == GDK_YES;
234 /*****************************************************
235 * X11 specific implementations of generic functions *
236 *****************************************************/
239 gdk_x11_get_colormap (GdkDrawable *drawable)
241 GdkDrawableImplX11 *impl;
243 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
245 return impl->colormap;
249 gdk_x11_set_colormap (GdkDrawable *drawable,
250 GdkColormap *colormap)
252 GdkDrawableImplX11 *impl;
254 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
256 if (impl->colormap == colormap)
260 g_object_unref (impl->colormap);
261 impl->colormap = colormap;
263 g_object_ref (impl->colormap);
267 gdk_x11_get_depth (GdkDrawable *drawable)
269 /* This is a bit bogus but I'm not sure the other way is better */
271 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
275 get_impl_drawable (GdkDrawable *drawable)
277 if (GDK_IS_WINDOW (drawable))
278 return ((GdkWindowObject *)drawable)->impl;
279 else if (GDK_IS_PIXMAP (drawable))
280 return ((GdkPixmapObject *)drawable)->impl;
283 g_warning (G_STRLOC " drawable is not a pixmap or window");
289 gdk_x11_get_screen (GdkDrawable *drawable)
291 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
292 return GDK_DRAWABLE_IMPL_X11 (drawable)->screen;
294 return GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen;
298 gdk_x11_get_visual (GdkDrawable *drawable)
300 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_X11 (drawable)->wrapper);
304 * gdk_x11_drawable_get_xdisplay:
305 * @drawable: a #GdkDrawable.
307 * Returns the display of a #GdkDrawable.
309 * Return value: an Xlib <type>Display*</type>.
312 gdk_x11_drawable_get_xdisplay (GdkDrawable *drawable)
314 if (GDK_IS_DRAWABLE_IMPL_X11 (drawable))
315 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (drawable)->screen);
317 return GDK_SCREEN_XDISPLAY (GDK_DRAWABLE_IMPL_X11 (get_impl_drawable (drawable))->screen);
321 * gdk_x11_drawable_get_xid:
322 * @drawable: a #GdkDrawable.
324 * Returns the X resource (window or pixmap) belonging to a #GdkDrawable.
326 * Return value: the ID of @drawable's X resource.
329 gdk_x11_drawable_get_xid (GdkDrawable *drawable)
333 if (GDK_IS_WINDOW (drawable))
335 GdkWindow *window = (GdkWindow *)drawable;
337 /* Try to ensure the window has a native window */
338 if (!_gdk_window_has_impl (window))
340 gdk_window_ensure_native (window);
342 /* We sync here to ensure the window is created in the Xserver when
343 * this function returns. This is required because the returned XID
344 * for this window must be valid immediately, even with another
345 * connection to the Xserver */
346 gdk_display_sync (gdk_drawable_get_display (window));
349 if (!GDK_WINDOW_IS_X11 (window))
351 g_warning (G_STRLOC " drawable is not a native X11 window");
355 impl = ((GdkWindowObject *)drawable)->impl;
357 else if (GDK_IS_PIXMAP (drawable))
358 impl = ((GdkPixmapObject *)drawable)->impl;
361 g_warning (G_STRLOC " drawable is not a pixmap or window");
365 return ((GdkDrawableImplX11 *)impl)->xid;
369 gdk_x11_window_get_drawable_impl (GdkWindow *window)
371 return ((GdkWindowObject *)window)->impl;
374 gdk_x11_pixmap_get_drawable_impl (GdkPixmap *pixmap)
376 return ((GdkPixmapObject *)pixmap)->impl;
381 list_formats (XRenderPictFormat *pf)
387 XRenderPictFormat *pf = XRenderFindFormat (impl->xdisplay, 0, NULL, i);
390 g_print ("%2d R-%#06x/%#06x G-%#06x/%#06x B-%#06x/%#06x A-%#06x/%#06x\n",
395 pf->direct.greenMask,
399 pf->direct.alphaMask);
408 _gdk_x11_convert_to_format (guchar *src_buf,
412 GdkX11FormatType dest_format,
413 GdkByteOrder dest_byteorder,
419 for (i=0; i < height; i++)
423 case GDK_X11_FORMAT_EXACT_MASK:
425 memcpy (dest_buf + i * dest_rowstride,
426 src_buf + i * src_rowstride,
430 case GDK_X11_FORMAT_ARGB_MASK:
432 guchar *row = src_buf + i * src_rowstride;
433 if (((gsize)row & 3) != 0)
436 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
437 guchar *end = p + 4 * width;
441 *q = (p[3] << 24) | (p[0] << 16) | (p[1] << 8) | p[2];
448 guint32 *p = (guint32 *)row;
449 guint32 *q = (guint32 *)(dest_buf + i * dest_rowstride);
450 guint32 *end = p + width;
452 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
453 if (dest_byteorder == GDK_LSB_FIRST)
459 *q = ( (*p & 0xff00ff00) |
460 ((*p & 0x000000ff) << 16) |
461 ((*p & 0x00ff0000) >> 16));
472 *q = (((*p & 0xff000000) >> 24) |
473 ((*p & 0x00ffffff) << 8));
478 #else /* G_BYTE_ORDER == G_BIG_ENDIAN */
479 if (dest_byteorder == GDK_LSB_FIRST)
485 *q = ( (*p & 0x00ff00ff) |
486 ((*p & 0x0000ff00) << 16) |
487 ((*p & 0xff000000) >> 16));
498 *q = (((*p & 0xffffff00) >> 8) |
499 ((*p & 0x000000ff) << 24));
504 #endif /* G_BYTE_ORDER*/
508 case GDK_X11_FORMAT_ARGB:
510 guchar *p = (src_buf + i * src_rowstride);
511 guchar *q = (dest_buf + i * dest_rowstride);
512 guchar *end = p + 4 * width;
515 #define MULT(d,c,a,t) G_STMT_START { t = c * a; d = ((t >> 8) + t) >> 8; } G_STMT_END
517 if (dest_byteorder == GDK_LSB_FIRST)
521 MULT(q[0], p[2], p[3], t1);
522 MULT(q[1], p[1], p[3], t2);
523 MULT(q[2], p[0], p[3], t3);
534 MULT(q[1], p[0], p[3], t1);
535 MULT(q[2], p[1], p[3], t2);
536 MULT(q[3], p[2], p[3], t3);
544 case GDK_X11_FORMAT_NONE:
545 g_assert_not_reached ();
552 gdk_x11_cairo_surface_destroy (void *data)
554 GdkDrawableImplX11 *impl = data;
556 impl->cairo_surface = NULL;
560 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
564 cairo_xlib_surface_set_size (surface, width, height);
568 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
572 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
575 visual = gdk_drawable_get_visual (drawable);
577 return cairo_xlib_surface_create (GDK_SCREEN_XDISPLAY (impl->screen),
579 GDK_VISUAL_XVISUAL (visual),
581 else if (gdk_drawable_get_depth (drawable) == 1)
582 return cairo_xlib_surface_create_for_bitmap (GDK_SCREEN_XDISPLAY (impl->screen),
584 GDK_SCREEN_XSCREEN (impl->screen),
588 g_warning ("Using Cairo rendering requires the drawable argument to\n"
589 "have a specified colormap. All windows have a colormap,\n"
590 "however, pixmaps only have colormap by default if they\n"
591 "were created with a non-NULL window argument. Otherwise\n"
592 "a colormap must be set on them with gdk_drawable_set_colormap");
598 static cairo_surface_t *
599 gdk_x11_ref_cairo_surface (GdkDrawable *drawable)
601 GdkDrawableImplX11 *impl = GDK_DRAWABLE_IMPL_X11 (drawable);
603 if (GDK_IS_WINDOW_IMPL_X11 (drawable) &&
604 GDK_WINDOW_DESTROYED (impl->wrapper))
607 if (!impl->cairo_surface)
611 gdk_drawable_get_size (impl->wrapper, &width, &height);
613 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, width, height);
615 if (impl->cairo_surface)
616 cairo_surface_set_user_data (impl->cairo_surface, &gdk_x11_cairo_key,
617 drawable, gdk_x11_cairo_surface_destroy);
620 cairo_surface_reference (impl->cairo_surface);
622 return impl->cairo_surface;