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_* */
52 #include "gdkprivate.h"
53 #include "gdkprivate-x11.h"
55 typedef struct _GdkImagePrivateX11 GdkImagePrivateX11;
57 struct _GdkImagePrivateX11
65 static GList *image_list = NULL;
66 static gpointer parent_class = NULL;
67 static gboolean have_shm_pixmaps;
69 static void gdk_x11_image_destroy (GdkImage *image);
70 static void gdk_image_init (GdkImage *image);
71 static void gdk_image_class_init (GdkImageClass *klass);
72 static void gdk_image_finalize (GObject *object);
74 #define PRIVATE_DATA(image) ((GdkImagePrivateX11 *) GDK_IMAGE (image)->windowing_data)
77 gdk_image_get_type (void)
79 static GType object_type = 0;
83 static const GTypeInfo object_info =
85 sizeof (GdkImageClass),
87 (GBaseFinalizeFunc) NULL,
88 (GClassInitFunc) gdk_image_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
93 (GInstanceInitFunc) gdk_image_init,
96 object_type = g_type_register_static (G_TYPE_OBJECT,
105 gdk_image_init (GdkImage *image)
107 image->windowing_data = g_new0 (GdkImagePrivateX11, 1);
111 gdk_image_class_init (GdkImageClass *klass)
113 GObjectClass *object_class = G_OBJECT_CLASS (klass);
115 parent_class = g_type_class_peek_parent (klass);
117 object_class->finalize = gdk_image_finalize;
121 gdk_image_finalize (GObject *object)
123 GdkImage *image = GDK_IMAGE (object);
125 gdk_x11_image_destroy (image);
127 G_OBJECT_CLASS (parent_class)->finalize (object);
132 _gdk_image_exit (void)
138 image = image_list->data;
139 gdk_x11_image_destroy (image);
144 gdk_image_new_bitmap(GdkVisual *visual, gpointer data, gint w, gint h)
146 * Desc: create a new bitmap image
151 GdkImagePrivateX11 *private;
152 image = g_object_new (gdk_image_get_type (), NULL);
153 private = PRIVATE_DATA (image);
154 private->xdisplay = gdk_display;
155 image->type = GDK_IMAGE_NORMAL;
156 image->visual = visual;
160 image->bits_per_pixel = 1;
161 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
162 private->ximage = XCreateImage(private->xdisplay, xvisual, 1, XYBitmap,
164 private->ximage->data = data;
165 private->ximage->bitmap_bit_order = MSBFirst;
166 private->ximage->byte_order = MSBFirst;
167 image->byte_order = MSBFirst;
168 image->mem = private->ximage->data;
169 image->bpl = private->ximage->bytes_per_line;
172 } /* gdk_image_new_bitmap() */
175 gdk_image_check_xshm(Display *display)
177 * Desc: query the server for support for the MIT_SHM extension
178 * Return: 0 = not available
179 * 1 = shared XImage support available
180 * 2 = shared Pixmap support available also
184 int major, minor, ignore;
187 if (XQueryExtension(display, "MIT-SHM", &ignore, &ignore, &ignore))
189 if (XShmQueryVersion(display, &major, &minor, &pixmaps )==True)
191 return (pixmaps==True) ? 2 : 1;
199 _gdk_windowing_image_init (void)
203 gint res = gdk_image_check_xshm (gdk_display);
206 _gdk_use_xshm = FALSE;
208 have_shm_pixmaps = (res == 2);
213 _gdk_image_new_for_depth (GdkImageType type,
220 GdkImagePrivateX11 *private;
222 XShmSegmentInfo *x_shm_info;
224 Visual *xvisual = NULL;
226 g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
227 g_return_val_if_fail (visual || depth != -1, NULL);
230 depth = visual->depth;
234 case GDK_IMAGE_FASTEST:
235 image = _gdk_image_new_for_depth (GDK_IMAGE_SHARED, visual, width, height, depth);
238 image = _gdk_image_new_for_depth (GDK_IMAGE_NORMAL, visual, width, height, depth);
242 image = g_object_new (gdk_image_get_type (), NULL);
244 private = PRIVATE_DATA (image);
246 private->xdisplay = gdk_display;
249 image->visual = visual;
250 image->width = width;
251 image->height = height;
252 image->depth = depth;
255 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
259 case GDK_IMAGE_SHARED:
263 private->x_shm_info = g_new (XShmSegmentInfo, 1);
264 x_shm_info = private->x_shm_info;
265 x_shm_info->shmid = -1;
266 x_shm_info->shmaddr = (char*) -1;
268 private->ximage = XShmCreateImage (private->xdisplay, xvisual, depth,
269 ZPixmap, NULL, x_shm_info, width, height);
270 if (private->ximage == NULL)
272 g_warning ("XShmCreateImage failed");
273 _gdk_use_xshm = FALSE;
278 x_shm_info->shmid = shmget (IPC_PRIVATE,
279 private->ximage->bytes_per_line * private->ximage->height,
282 if (x_shm_info->shmid == -1)
284 /* EINVAL indicates, most likely, that the segment we asked for
285 * is bigger than SHMMAX, so we don't treat it as a permanent
286 * error. ENOSPC and ENOMEM may also indicate this, but
287 * more likely are permanent errors.
291 g_warning ("shmget failed: error %d (%s)", errno, g_strerror (errno));
292 _gdk_use_xshm = FALSE;
298 x_shm_info->readOnly = False;
299 x_shm_info->shmaddr = shmat (x_shm_info->shmid, 0, 0);
300 private->ximage->data = x_shm_info->shmaddr;
302 if (x_shm_info->shmaddr == (char*) -1)
304 g_warning ("shmat failed: error %d (%s)", errno, g_strerror (errno));
305 /* Failure in shmat is almost certainly permanent. Most likely error is
306 * EMFILE, which would mean that we've exceeded the per-process
309 _gdk_use_xshm = FALSE;
313 gdk_error_trap_push ();
315 XShmAttach (private->xdisplay, x_shm_info);
316 XSync (private->xdisplay, False);
318 if (gdk_error_trap_pop ())
320 /* this is the common failure case so omit warning */
321 _gdk_use_xshm = FALSE;
325 /* We mark the segment as destroyed so that when
326 * the last process detaches, it will be deleted.
327 * There is a small possibility of leaking if
328 * we die in XShmAttach. In theory, a signal handler
331 shmctl (x_shm_info->shmid, IPC_RMID, 0);
334 image_list = g_list_prepend (image_list, image);
340 case GDK_IMAGE_NORMAL:
341 private->ximage = XCreateImage (private->xdisplay, xvisual, depth,
342 ZPixmap, 0, 0, width, height, 32, 0);
344 /* Use malloc, not g_malloc here, because X will call free()
347 private->ximage->data = malloc (private->ximage->bytes_per_line *
348 private->ximage->height);
349 if (!private->ximage->data)
353 case GDK_IMAGE_FASTEST:
354 g_assert_not_reached ();
359 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
360 image->mem = private->ximage->data;
361 image->bpl = private->ximage->bytes_per_line;
362 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
363 image->bits_per_pixel = private->ximage->bits_per_pixel;
372 XDestroyImage (private->ximage);
373 private->ximage = NULL;
376 if (private->x_shm_info)
378 x_shm_info = private->x_shm_info;
380 if (x_shm_info->shmaddr != (char *)-1)
381 shmdt (x_shm_info->shmaddr);
382 if (x_shm_info->shmid != -1)
383 shmctl (x_shm_info->shmid, IPC_RMID, 0);
386 private->x_shm_info = NULL;
389 g_object_unref (image);
395 _gdk_x11_image_get_shm_pixmap (GdkImage *image)
397 GdkImagePrivateX11 *private = PRIVATE_DATA (image);
400 /* Future: do we need one of these per-screen per-image? ShmPixmaps
401 * are the same for every screen, but can they be shared?
403 if (!private->shm_pixmap && image->type == GDK_IMAGE_SHARED && have_shm_pixmaps)
404 private->shm_pixmap = XShmCreatePixmap (private->xdisplay, _gdk_root_window,
405 image->mem, private->x_shm_info,
406 image->width, image->height, image->depth);
408 return private->shm_pixmap;
415 gdk_image_new (GdkImageType type,
420 return _gdk_image_new_for_depth (type, visual, width, height, -1);
424 get_full_image (GdkDrawable *drawable,
431 GdkImagePrivateX11 *private;
432 GdkDrawableImplX11 *impl;
435 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
437 ximage = XGetImage (impl->xdisplay,
439 src_x, src_y, width, height,
445 image = g_object_new (gdk_image_get_type (), NULL);
447 private = PRIVATE_DATA (image);
449 private->xdisplay = gdk_display;
450 private->ximage = ximage;
452 image->type = GDK_IMAGE_NORMAL;
453 image->visual = gdk_drawable_get_visual (drawable); /* could be NULL */
454 image->width = width;
455 image->height = height;
456 image->depth = gdk_drawable_get_depth (drawable);
458 image->mem = private->ximage->data;
459 image->bpl = private->ximage->bytes_per_line;
460 image->bits_per_pixel = private->ximage->bits_per_pixel;
461 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
462 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
468 _gdk_x11_copy_to_image (GdkDrawable *drawable,
477 GdkImagePrivateX11 *private;
478 GdkDrawableImplX11 *impl;
482 GdkRectangle window_rect;
483 Pixmap shm_pixmap = None;
484 gboolean success = TRUE;
486 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_X11 (drawable), NULL);
487 g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL);
489 visual = gdk_drawable_get_visual (drawable);
490 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
494 #define UNGRAB() G_STMT_START { \
496 gdk_x11_ungrab_server (); \
497 XFlush (impl->xdisplay); \
498 have_grab = FALSE; } \
501 if (!image && !GDK_IS_WINDOW_IMPL_X11 (drawable))
502 return get_full_image (drawable, src_x, src_y, width, height);
504 if (image && image->type == GDK_IMAGE_SHARED)
506 shm_pixmap = _gdk_x11_image_get_shm_pixmap (image);
512 /* Again easy, we can just XCopyArea, and don't have to worry about clipping
514 values.subwindow_mode = IncludeInferiors;
515 xgc = XCreateGC (impl->xdisplay, impl->xid, GCSubwindowMode, &values);
517 XCopyArea (impl->xdisplay, impl->xid, shm_pixmap, xgc,
518 src_x, src_y, width, height, dest_x, dest_y);
519 XSync (impl->xdisplay, FALSE);
521 XFreeGC (impl->xdisplay, xgc);
527 /* Now the general case - we may have to worry about clipping to the screen
528 * bounds, in which case we'll have to grab the server and only get a piece
531 if (GDK_IS_WINDOW_IMPL_X11 (drawable))
533 GdkRectangle screen_rect;
537 gdk_x11_grab_server ();
539 /* Translate screen area into window coordinates */
540 XTranslateCoordinates (gdk_display,
544 &screen_rect.x, &screen_rect.y,
547 screen_rect.width = gdk_screen_width ();
548 screen_rect.height = gdk_screen_height ();
550 gdk_error_trap_push ();
555 gdk_window_get_geometry (GDK_WINDOW (impl->wrapper),
561 /* compute intersection of screen and window, in window
564 if (gdk_error_trap_pop () ||
565 !gdk_rectangle_intersect (&window_rect, &screen_rect,
573 gdk_drawable_get_size (drawable,
575 &window_rect.height);
583 /* window_rect specifies the part of drawable which we can get from
584 * the server in window coordinates.
585 * For pixmaps this is all of the pixmap, for windows it is just
588 if (!gdk_rectangle_intersect (&req, &window_rect, &req))
591 gdk_error_trap_push ();
594 req.x == src_x && req.y == src_y && req.width == width && req.height == height)
596 image = get_full_image (drawable, src_x, src_y, width, height);
602 gboolean created_image = FALSE;
606 image = _gdk_image_new_for_depth (GDK_IMAGE_NORMAL, visual, width, height,
607 gdk_drawable_get_depth (drawable));
608 created_image = TRUE;
611 private = PRIVATE_DATA (image);
613 /* In the ShmImage but no ShmPixmap case, we could use XShmGetImage when
614 * we are getting the entire image.
616 if (XGetSubImage (impl->xdisplay,
618 req.x, req.y, req.width, req.height,
621 dest_x + req.x - src_x, dest_y + req.y - src_y) == None)
624 g_object_unref (image);
634 gdk_x11_ungrab_server ();
635 XFlush (impl->xdisplay);
639 gdk_error_trap_pop ();
641 if (success && !image)
643 /* We "succeeded", but could get no content for the image so return junk */
644 image = _gdk_image_new_for_depth (GDK_IMAGE_NORMAL, visual, width, height,
645 gdk_drawable_get_depth (drawable));
652 gdk_image_get_pixel (GdkImage *image,
657 GdkImagePrivateX11 *private;
659 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
661 private = PRIVATE_DATA (image);
663 pixel = XGetPixel (private->ximage, x, y);
669 gdk_image_put_pixel (GdkImage *image,
674 GdkImagePrivateX11 *private;
676 g_return_if_fail (GDK_IS_IMAGE (image));
678 private = PRIVATE_DATA (image);
680 pixel = XPutPixel (private->ximage, x, y, pixel);
684 gdk_x11_image_destroy (GdkImage *image)
686 GdkImagePrivateX11 *private;
688 XShmSegmentInfo *x_shm_info;
691 g_return_if_fail (GDK_IS_IMAGE (image));
693 private = PRIVATE_DATA (image);
695 if (private == NULL) /* This means that _gdk_image_exit() destroyed the
696 * image already, and now we're called a second
697 * time from _finalize()
701 if (private->ximage) /* Deal with failure of creation */
705 case GDK_IMAGE_NORMAL:
706 XDestroyImage (private->ximage);
709 case GDK_IMAGE_SHARED:
713 if (private->shm_pixmap)
714 XFreePixmap (private->xdisplay, private->shm_pixmap);
716 image_list = g_list_remove (image_list, image);
717 XShmDetach (private->xdisplay, private->x_shm_info);
718 XDestroyImage (private->ximage);
720 x_shm_info = private->x_shm_info;
721 shmdt (x_shm_info->shmaddr);
723 g_free (private->x_shm_info);
724 private->x_shm_info = NULL;
727 g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support");
731 case GDK_IMAGE_FASTEST:
732 g_assert_not_reached ();
737 image->windowing_data = NULL;
741 gdk_x11_image_get_xdisplay (GdkImage *image)
743 GdkImagePrivateX11 *private;
745 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
747 private = PRIVATE_DATA (image);
749 return private->xdisplay;
753 gdk_x11_image_get_ximage (GdkImage *image)
755 GdkImagePrivateX11 *private;
757 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
759 private = PRIVATE_DATA (image);
761 return private->ximage;
765 _gdk_windowing_get_bits_for_depth (gint depth)
767 XPixmapFormatValues *formats;
770 formats = XListPixmapFormats (gdk_display, &count);
772 for (i = 0; i < count; i++)
773 if (formats[i].depth == depth)
775 gint result = formats[i].bits_per_pixel;
780 g_assert_not_reached ();