1 /* GDK - The GIMP Drawing Kit
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/.
30 #include <sys/types.h>
32 #if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H)
42 #include <X11/Xutil.h>
45 #include <X11/extensions/XShm.h>
50 #include "gdk.h" /* For gdk_error_trap_* / gdk_flush_* */
53 #include "gdkprivate.h"
54 #include "gdkprivate-x11.h"
55 #include "gdkdisplay-x11.h"
56 #include "gdkscreen-x11.h"
58 typedef struct _GdkImagePrivateX11 GdkImagePrivateX11;
60 struct _GdkImagePrivateX11
68 static GList *image_list = NULL;
69 static gpointer parent_class = NULL;
71 static void gdk_x11_image_destroy (GdkImage *image);
72 static void gdk_image_init (GdkImage *image);
73 static void gdk_image_class_init (GdkImageClass *klass);
74 static void gdk_image_finalize (GObject *object);
76 #define PRIVATE_DATA(image) ((GdkImagePrivateX11 *) GDK_IMAGE (image)->windowing_data)
79 gdk_image_get_type (void)
81 static GType object_type = 0;
85 static const GTypeInfo object_info =
87 sizeof (GdkImageClass),
89 (GBaseFinalizeFunc) NULL,
90 (GClassInitFunc) gdk_image_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
95 (GInstanceInitFunc) gdk_image_init,
98 object_type = g_type_register_static (G_TYPE_OBJECT,
107 gdk_image_init (GdkImage *image)
109 image->windowing_data = g_new0 (GdkImagePrivateX11, 1);
113 gdk_image_class_init (GdkImageClass *klass)
115 GObjectClass *object_class = G_OBJECT_CLASS (klass);
117 parent_class = g_type_class_peek_parent (klass);
119 object_class->finalize = gdk_image_finalize;
123 gdk_image_finalize (GObject *object)
125 GdkImage *image = GDK_IMAGE (object);
127 gdk_x11_image_destroy (image);
129 G_OBJECT_CLASS (parent_class)->finalize (object);
134 _gdk_image_exit (void)
140 image = image_list->data;
141 gdk_x11_image_destroy (image);
146 * gdk_image_new_bitmap:
147 * @visual: the #GdkVisual to use for the image.
148 * @data: the pixel data.
149 * @width: the width of the image in pixels.
150 * @height: the height of the image in pixels.
152 * Creates a new #GdkImage with a depth of 1 from the given data.
153 * <warning><para>THIS FUNCTION IS INCREDIBLY BROKEN. The passed-in data must
154 * be allocated by malloc() (NOT g_malloc()) and will be freed when the
155 * image is freed.</para></warning>
157 * Return value: a new #GdkImage.
160 gdk_image_new_bitmap(GdkVisual *visual, gpointer data, gint width, gint height)
165 GdkImagePrivateX11 *private;
167 image = g_object_new (gdk_image_get_type (), NULL);
168 private = PRIVATE_DATA (image);
169 private->screen = gdk_visual_get_screen (visual);
170 display = GDK_SCREEN_DISPLAY (private->screen);
172 image->type = GDK_IMAGE_NORMAL;
173 image->visual = visual;
174 image->width = width;
175 image->height = height;
177 image->bits_per_pixel = 1;
179 private->ximage = NULL;
182 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
183 private->ximage = XCreateImage (GDK_SCREEN_XDISPLAY (private->screen),
184 xvisual, 1, XYBitmap,
185 0, 0, width, height, 8, 0);
188 private->ximage->data = data;
189 private->ximage->bitmap_bit_order = MSBFirst;
190 private->ximage->byte_order = MSBFirst;
191 image->byte_order = MSBFirst;
192 image->mem = private->ximage->data;
193 image->bpl = private->ximage->bytes_per_line;
196 } /* gdk_image_new_bitmap() */
199 * Desc: query the server for support for the MIT_SHM extension
200 * Return: 0 = not available
201 * 1 = shared XImage support available
202 * 2 = shared Pixmap support available also
205 gdk_image_check_xshm(Display *display)
211 if (XShmQueryExtension (display) &&
212 XShmQueryVersion (display, &major, &minor, &pixmaps))
213 return pixmaps ? 2 : 1;
219 _gdk_windowing_image_init (GdkDisplay *display)
221 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
223 if (display_x11->use_xshm)
225 gint res = gdk_image_check_xshm (GDK_DISPLAY_XDISPLAY (display));
228 display_x11->use_xshm = False;
230 display_x11->have_shm_pixmaps = (res == 2);
235 _gdk_image_new_for_depth (GdkScreen *screen,
243 GdkImagePrivateX11 *private;
245 XShmSegmentInfo *x_shm_info;
247 Visual *xvisual = NULL;
248 GdkDisplayX11 *display_x11;
249 GdkScreenX11 *screen_x11;
251 g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
252 g_return_val_if_fail (visual || depth != -1, NULL);
253 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
255 screen_x11 = GDK_SCREEN_X11 (screen);
256 display_x11 = GDK_DISPLAY_X11 (screen_x11->display);
259 depth = visual->depth;
263 case GDK_IMAGE_FASTEST:
264 image = _gdk_image_new_for_depth (screen, GDK_IMAGE_SHARED,
265 visual, width, height, depth);
267 image = _gdk_image_new_for_depth (screen, GDK_IMAGE_NORMAL,
268 visual, width, height, depth);
272 image = g_object_new (gdk_image_get_type (), NULL);
274 private = PRIVATE_DATA (image);
276 private->screen = screen;
279 image->visual = visual;
280 image->width = width;
281 image->height = height;
282 image->depth = depth;
285 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
289 case GDK_IMAGE_SHARED:
291 if (display_x11->use_xshm)
293 private->x_shm_info = g_new (XShmSegmentInfo, 1);
294 x_shm_info = private->x_shm_info;
295 x_shm_info->shmid = -1;
296 x_shm_info->shmaddr = (char*) -1;
298 private->ximage = XShmCreateImage (screen_x11->xdisplay, xvisual, depth,
299 ZPixmap, NULL, x_shm_info, width, height);
300 if (private->ximage == NULL)
302 g_warning ("XShmCreateImage failed");
303 display_x11->use_xshm = FALSE;
308 x_shm_info->shmid = shmget (IPC_PRIVATE,
309 private->ximage->bytes_per_line * private->ximage->height,
312 if (x_shm_info->shmid == -1)
314 /* EINVAL indicates, most likely, that the segment we asked for
315 * is bigger than SHMMAX, so we don't treat it as a permanent
316 * error. ENOSPC and ENOMEM may also indicate this, but
317 * more likely are permanent errors.
321 g_warning ("shmget failed: error %d (%s)", errno, g_strerror (errno));
322 display_x11->use_xshm = FALSE;
328 x_shm_info->readOnly = False;
329 x_shm_info->shmaddr = shmat (x_shm_info->shmid, 0, 0);
330 private->ximage->data = x_shm_info->shmaddr;
332 if (x_shm_info->shmaddr == (char*) -1)
334 g_warning ("shmat failed: error %d (%s)", errno, g_strerror (errno));
335 /* Failure in shmat is almost certainly permanent. Most likely error is
336 * EMFILE, which would mean that we've exceeded the per-process
339 display_x11->use_xshm = FALSE;
343 gdk_error_trap_push ();
345 XShmAttach (screen_x11->xdisplay, x_shm_info);
346 XSync (screen_x11->xdisplay, False);
348 if (gdk_error_trap_pop ())
350 /* this is the common failure case so omit warning */
351 display_x11->use_xshm = FALSE;
355 /* We mark the segment as destroyed so that when
356 * the last process detaches, it will be deleted.
357 * There is a small possibility of leaking if
358 * we die in XShmAttach. In theory, a signal handler
361 shmctl (x_shm_info->shmid, IPC_RMID, 0);
364 image_list = g_list_prepend (image_list, image);
370 case GDK_IMAGE_NORMAL:
371 private->ximage = XCreateImage (screen_x11->xdisplay, xvisual, depth,
372 ZPixmap, 0, 0, width, height, 32, 0);
374 /* Use malloc, not g_malloc here, because X will call free()
377 private->ximage->data = malloc (private->ximage->bytes_per_line *
378 private->ximage->height);
379 if (!private->ximage->data)
383 case GDK_IMAGE_FASTEST:
384 g_assert_not_reached ();
389 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
390 image->mem = private->ximage->data;
391 image->bpl = private->ximage->bytes_per_line;
392 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
393 image->bits_per_pixel = private->ximage->bits_per_pixel;
402 XDestroyImage (private->ximage);
403 private->ximage = NULL;
406 if (private->x_shm_info)
408 x_shm_info = private->x_shm_info;
410 if (x_shm_info->shmaddr != (char *)-1)
411 shmdt (x_shm_info->shmaddr);
412 if (x_shm_info->shmid != -1)
413 shmctl (x_shm_info->shmid, IPC_RMID, 0);
416 private->x_shm_info = NULL;
419 g_object_unref (image);
425 _gdk_x11_image_get_shm_pixmap (GdkImage *image)
427 GdkImagePrivateX11 *private = PRIVATE_DATA (image);
428 GdkDisplay *display = GDK_SCREEN_DISPLAY (private->screen);
434 /* Future: do we need one of these per-screen per-image? ShmPixmaps
435 * are the same for every screen, but can they be shared? Not a concern
436 * right now since we tie images to a particular screen.
438 if (!private->shm_pixmap && image->type == GDK_IMAGE_SHARED &&
439 GDK_DISPLAY_X11 (display)->have_shm_pixmaps)
440 private->shm_pixmap = XShmCreatePixmap (GDK_SCREEN_XDISPLAY (private->screen),
441 GDK_SCREEN_XROOTWIN (private->screen),
442 image->mem, private->x_shm_info,
443 image->width, image->height, image->depth);
445 return private->shm_pixmap;
452 get_full_image (GdkDrawable *drawable,
459 GdkImagePrivateX11 *private;
460 GdkDrawableImplX11 *impl;
463 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
465 ximage = XGetImage (GDK_SCREEN_XDISPLAY (impl->screen),
467 src_x, src_y, width, height,
473 image = g_object_new (gdk_image_get_type (), NULL);
475 private = PRIVATE_DATA (image);
477 private->screen = impl->screen;
478 private->ximage = ximage;
480 image->type = GDK_IMAGE_NORMAL;
481 image->visual = gdk_drawable_get_visual (drawable); /* could be NULL */
482 image->width = width;
483 image->height = height;
484 image->depth = gdk_drawable_get_depth (drawable);
486 image->mem = private->ximage->data;
487 image->bpl = private->ximage->bytes_per_line;
488 image->bits_per_pixel = private->ximage->bits_per_pixel;
489 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
490 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
496 _gdk_x11_copy_to_image (GdkDrawable *drawable,
505 GdkImagePrivateX11 *private;
506 GdkDrawableImplX11 *impl;
512 GdkRectangle window_rect;
513 Pixmap shm_pixmap = None;
514 gboolean success = TRUE;
516 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_X11 (drawable), NULL);
517 g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL);
519 visual = gdk_drawable_get_visual (drawable);
520 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
521 display = gdk_drawable_get_display (drawable);
522 xdisplay = gdk_x11_display_get_xdisplay (display);
529 #define UNGRAB() G_STMT_START { \
531 gdk_x11_display_ungrab (display); \
532 have_grab = FALSE; } \
535 if (!image && !GDK_IS_WINDOW_IMPL_X11 (drawable))
536 return get_full_image (drawable, src_x, src_y, width, height);
538 if (image && image->type == GDK_IMAGE_SHARED)
540 shm_pixmap = _gdk_x11_image_get_shm_pixmap (image);
546 /* Again easy, we can just XCopyArea, and don't have to worry about clipping
548 values.subwindow_mode = IncludeInferiors;
549 xgc = XCreateGC (xdisplay, impl->xid, GCSubwindowMode, &values);
551 XCopyArea (xdisplay, impl->xid, shm_pixmap, xgc,
552 src_x, src_y, width, height, dest_x, dest_y);
553 XSync (xdisplay, FALSE);
555 XFreeGC (xdisplay, xgc);
561 /* Now the general case - we may have to worry about clipping to the screen
562 * bounds, in which case we'll have to grab the server and only get a piece
565 if (GDK_IS_WINDOW_IMPL_X11 (drawable))
567 GdkRectangle screen_rect;
571 gdk_x11_display_grab (display);
573 /* Translate screen area into window coordinates */
574 XTranslateCoordinates (xdisplay,
575 GDK_SCREEN_XROOTWIN (impl->screen),
578 &screen_rect.x, &screen_rect.y,
581 screen_rect.width = gdk_screen_get_width (impl->screen);
582 screen_rect.height = gdk_screen_get_height (impl->screen);
584 gdk_error_trap_push ();
589 gdk_window_get_geometry (GDK_WINDOW (impl->wrapper),
595 /* compute intersection of screen and window, in window
598 if (gdk_error_trap_pop () ||
599 !gdk_rectangle_intersect (&window_rect, &screen_rect,
607 gdk_drawable_get_size (drawable,
609 &window_rect.height);
617 /* window_rect specifies the part of drawable which we can get from
618 * the server in window coordinates.
619 * For pixmaps this is all of the pixmap, for windows it is just
622 if (!gdk_rectangle_intersect (&req, &window_rect, &req))
625 gdk_error_trap_push ();
628 req.x == src_x && req.y == src_y && req.width == width && req.height == height)
630 image = get_full_image (drawable, src_x, src_y, width, height);
636 gboolean created_image = FALSE;
640 image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL,
641 visual, width, height,
642 gdk_drawable_get_depth (drawable));
643 created_image = TRUE;
646 private = PRIVATE_DATA (image);
648 /* In the ShmImage but no ShmPixmap case, we could use XShmGetImage when
649 * we are getting the entire image.
651 if (XGetSubImage (xdisplay, impl->xid,
652 req.x, req.y, req.width, req.height,
655 dest_x + req.x - src_x, dest_y + req.y - src_y) == None)
658 g_object_unref (image);
668 gdk_x11_display_ungrab (display);
672 gdk_error_trap_pop ();
674 if (success && !image)
676 /* We "succeeded", but could get no content for the image so return junk */
677 image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL,
678 visual, width, height,
679 gdk_drawable_get_depth (drawable));
686 gdk_image_get_pixel (GdkImage *image,
691 GdkImagePrivateX11 *private;
693 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
694 g_return_val_if_fail (x >= 0 && x < image->width, 0);
695 g_return_val_if_fail (y >= 0 && y < image->height, 0);
697 private = PRIVATE_DATA (image);
699 if (!private->screen->closed)
700 pixel = XGetPixel (private->ximage, x, y);
708 gdk_image_put_pixel (GdkImage *image,
713 GdkImagePrivateX11 *private;
715 g_return_if_fail (GDK_IS_IMAGE (image));
716 g_return_if_fail (x >= 0 && x < image->width);
717 g_return_if_fail (y >= 0 && y < image->height);
719 private = PRIVATE_DATA (image);
721 if (!private->screen->closed)
722 pixel = XPutPixel (private->ximage, x, y, pixel);
726 gdk_x11_image_destroy (GdkImage *image)
728 GdkImagePrivateX11 *private;
730 XShmSegmentInfo *x_shm_info;
733 g_return_if_fail (GDK_IS_IMAGE (image));
735 private = PRIVATE_DATA (image);
737 if (private == NULL) /* This means that _gdk_image_exit() destroyed the
738 * image already, and now we're called a second
739 * time from _finalize()
743 if (private->ximage) /* Deal with failure of creation */
747 case GDK_IMAGE_NORMAL:
748 if (!private->screen->closed)
749 XDestroyImage (private->ximage);
752 case GDK_IMAGE_SHARED:
754 if (!private->screen->closed)
756 gdk_display_sync (GDK_SCREEN_DISPLAY (private->screen));
758 if (private->shm_pixmap)
759 XFreePixmap (GDK_SCREEN_XDISPLAY (private->screen), private->shm_pixmap);
761 XShmDetach (GDK_SCREEN_XDISPLAY (private->screen), private->x_shm_info);
762 XDestroyImage (private->ximage);
765 image_list = g_list_remove (image_list, image);
767 x_shm_info = private->x_shm_info;
768 shmdt (x_shm_info->shmaddr);
770 g_free (private->x_shm_info);
771 private->x_shm_info = NULL;
774 g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support");
778 case GDK_IMAGE_FASTEST:
779 g_assert_not_reached ();
784 image->windowing_data = NULL;
788 * gdk_x11_image_get_xdisplay:
789 * @image: a #GdkImage.
791 * Returns the display of a #GdkImage.
793 * Return value: an Xlib <type>Display*</type>.
796 gdk_x11_image_get_xdisplay (GdkImage *image)
798 GdkImagePrivateX11 *private;
800 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
802 private = PRIVATE_DATA (image);
804 return GDK_SCREEN_XDISPLAY (private->screen);
808 * gdk_x11_image_get_ximage:
809 * @image: a #GdkImage.
811 * Returns the X image belonging to a #GdkImage.
813 * Return value: an <type>XImage*</type>.
816 gdk_x11_image_get_ximage (GdkImage *image)
818 GdkImagePrivateX11 *private;
820 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
822 private = PRIVATE_DATA (image);
824 if (private->screen->closed)
827 return private->ximage;
831 _gdk_windowing_get_bits_for_depth (GdkDisplay *display,
834 XPixmapFormatValues *formats;
837 formats = XListPixmapFormats (GDK_DISPLAY_XDISPLAY (display), &count);
839 for (i = 0; i < count; i++)
840 if (formats[i].depth == depth)
842 gint result = formats[i].bits_per_pixel;
847 g_assert_not_reached ();