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"
59 typedef struct _GdkImagePrivateX11 GdkImagePrivateX11;
61 struct _GdkImagePrivateX11
69 static GList *image_list = NULL;
70 static gpointer parent_class = NULL;
72 static void gdk_x11_image_destroy (GdkImage *image);
73 static void gdk_image_init (GdkImage *image);
74 static void gdk_image_class_init (GdkImageClass *klass);
75 static void gdk_image_finalize (GObject *object);
77 #define PRIVATE_DATA(image) ((GdkImagePrivateX11 *) GDK_IMAGE (image)->windowing_data)
80 gdk_image_get_type (void)
82 static GType object_type = 0;
86 static const GTypeInfo object_info =
88 sizeof (GdkImageClass),
90 (GBaseFinalizeFunc) NULL,
91 (GClassInitFunc) gdk_image_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
96 (GInstanceInitFunc) gdk_image_init,
99 object_type = g_type_register_static (G_TYPE_OBJECT,
108 gdk_image_init (GdkImage *image)
110 image->windowing_data = g_new0 (GdkImagePrivateX11, 1);
114 gdk_image_class_init (GdkImageClass *klass)
116 GObjectClass *object_class = G_OBJECT_CLASS (klass);
118 parent_class = g_type_class_peek_parent (klass);
120 object_class->finalize = gdk_image_finalize;
124 gdk_image_finalize (GObject *object)
126 GdkImage *image = GDK_IMAGE (object);
128 gdk_x11_image_destroy (image);
130 G_OBJECT_CLASS (parent_class)->finalize (object);
135 _gdk_image_exit (void)
141 image = image_list->data;
142 gdk_x11_image_destroy (image);
147 * gdk_image_new_bitmap:
148 * @visual: the #GdkVisual to use for the image.
149 * @data: the pixel data.
150 * @width: the width of the image in pixels.
151 * @height: the height of the image in pixels.
153 * Creates a new #GdkImage with a depth of 1 from the given data.
154 * <warning><para>THIS FUNCTION IS INCREDIBLY BROKEN. The passed-in data must
155 * be allocated by malloc() (NOT g_malloc()) and will be freed when the
156 * image is freed.</para></warning>
158 * Return value: a new #GdkImage.
161 gdk_image_new_bitmap(GdkVisual *visual, gpointer data, gint width, gint height)
166 GdkImagePrivateX11 *private;
168 image = g_object_new (gdk_image_get_type (), NULL);
169 private = PRIVATE_DATA (image);
170 private->screen = gdk_visual_get_screen (visual);
171 display = GDK_SCREEN_DISPLAY (private->screen);
173 image->type = GDK_IMAGE_NORMAL;
174 image->visual = visual;
175 image->width = width;
176 image->height = height;
178 image->bits_per_pixel = 1;
180 private->ximage = NULL;
183 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
184 private->ximage = XCreateImage (GDK_SCREEN_XDISPLAY (private->screen),
185 xvisual, 1, XYBitmap,
186 0, NULL, width, height, 8, 0);
189 private->ximage->data = data;
190 private->ximage->bitmap_bit_order = MSBFirst;
191 private->ximage->byte_order = MSBFirst;
192 image->byte_order = MSBFirst;
193 image->mem = private->ximage->data;
194 image->bpl = private->ximage->bytes_per_line;
197 } /* gdk_image_new_bitmap() */
200 _gdk_windowing_image_init (GdkDisplay *display)
202 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
204 if (display_x11->use_xshm)
207 Display *xdisplay = display_x11->xdisplay;
208 int major, minor, event_base;
211 if (XShmQueryExtension (xdisplay) &&
212 XShmQueryVersion (xdisplay, &major, &minor, &pixmaps))
214 display_x11->have_shm_pixmaps = pixmaps;
215 event_base = XShmGetEventBase (xdisplay);
217 gdk_x11_register_standard_event_type (display,
218 event_base, ShmNumberEvents);
222 display_x11->use_xshm = FALSE;
227 _gdk_image_new_for_depth (GdkScreen *screen,
235 GdkImagePrivateX11 *private;
237 XShmSegmentInfo *x_shm_info;
239 Visual *xvisual = NULL;
240 GdkDisplayX11 *display_x11;
241 GdkScreenX11 *screen_x11;
243 g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL);
244 g_return_val_if_fail (visual || depth != -1, NULL);
245 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
247 screen_x11 = GDK_SCREEN_X11 (screen);
248 display_x11 = GDK_DISPLAY_X11 (screen_x11->display);
251 depth = visual->depth;
255 case GDK_IMAGE_FASTEST:
256 image = _gdk_image_new_for_depth (screen, GDK_IMAGE_SHARED,
257 visual, width, height, depth);
259 image = _gdk_image_new_for_depth (screen, GDK_IMAGE_NORMAL,
260 visual, width, height, depth);
264 image = g_object_new (gdk_image_get_type (), NULL);
266 private = PRIVATE_DATA (image);
268 private->screen = screen;
271 image->visual = visual;
272 image->width = width;
273 image->height = height;
274 image->depth = depth;
277 xvisual = ((GdkVisualPrivate*) visual)->xvisual;
281 case GDK_IMAGE_SHARED:
283 if (display_x11->use_xshm)
285 private->x_shm_info = g_new (XShmSegmentInfo, 1);
286 x_shm_info = private->x_shm_info;
287 x_shm_info->shmid = -1;
288 x_shm_info->shmaddr = (char*) -1;
290 private->ximage = XShmCreateImage (screen_x11->xdisplay, xvisual, depth,
291 ZPixmap, NULL, x_shm_info, width, height);
292 if (private->ximage == NULL)
294 g_warning ("XShmCreateImage failed");
295 display_x11->use_xshm = FALSE;
300 x_shm_info->shmid = shmget (IPC_PRIVATE,
301 private->ximage->bytes_per_line * private->ximage->height,
304 if (x_shm_info->shmid == -1)
306 /* EINVAL indicates, most likely, that the segment we asked for
307 * is bigger than SHMMAX, so we don't treat it as a permanent
308 * error. ENOSPC and ENOMEM may also indicate this, but
309 * more likely are permanent errors.
313 g_warning ("shmget failed: error %d (%s)", errno, g_strerror (errno));
314 display_x11->use_xshm = FALSE;
320 x_shm_info->readOnly = False;
321 x_shm_info->shmaddr = shmat (x_shm_info->shmid, NULL, 0);
322 private->ximage->data = x_shm_info->shmaddr;
324 if (x_shm_info->shmaddr == (char*) -1)
326 g_warning ("shmat failed: error %d (%s)", errno, g_strerror (errno));
327 /* Failure in shmat is almost certainly permanent. Most likely error is
328 * EMFILE, which would mean that we've exceeded the per-process
331 display_x11->use_xshm = FALSE;
335 gdk_error_trap_push ();
337 XShmAttach (screen_x11->xdisplay, x_shm_info);
338 XSync (screen_x11->xdisplay, False);
340 if (gdk_error_trap_pop ())
342 /* this is the common failure case so omit warning */
343 display_x11->use_xshm = FALSE;
347 /* We mark the segment as destroyed so that when
348 * the last process detaches, it will be deleted.
349 * There is a small possibility of leaking if
350 * we die in XShmAttach. In theory, a signal handler
353 shmctl (x_shm_info->shmid, IPC_RMID, NULL);
356 image_list = g_list_prepend (image_list, image);
362 case GDK_IMAGE_NORMAL:
363 private->ximage = XCreateImage (screen_x11->xdisplay, xvisual, depth,
364 ZPixmap, 0, NULL, width, height, 32, 0);
366 /* Use malloc, not g_malloc here, because X will call free()
369 private->ximage->data = malloc (private->ximage->bytes_per_line *
370 private->ximage->height);
371 if (!private->ximage->data)
375 case GDK_IMAGE_FASTEST:
376 g_assert_not_reached ();
381 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
382 image->mem = private->ximage->data;
383 image->bpl = private->ximage->bytes_per_line;
384 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
385 image->bits_per_pixel = private->ximage->bits_per_pixel;
394 XDestroyImage (private->ximage);
395 private->ximage = NULL;
398 if (private->x_shm_info)
400 x_shm_info = private->x_shm_info;
402 if (x_shm_info->shmaddr != (char *)-1)
403 shmdt (x_shm_info->shmaddr);
404 if (x_shm_info->shmid != -1)
405 shmctl (x_shm_info->shmid, IPC_RMID, NULL);
408 private->x_shm_info = NULL;
411 g_object_unref (image);
417 _gdk_x11_image_get_shm_pixmap (GdkImage *image)
419 GdkImagePrivateX11 *private = PRIVATE_DATA (image);
420 GdkDisplay *display = GDK_SCREEN_DISPLAY (private->screen);
426 /* Future: do we need one of these per-screen per-image? ShmPixmaps
427 * are the same for every screen, but can they be shared? Not a concern
428 * right now since we tie images to a particular screen.
430 if (!private->shm_pixmap && image->type == GDK_IMAGE_SHARED &&
431 GDK_DISPLAY_X11 (display)->have_shm_pixmaps)
432 private->shm_pixmap = XShmCreatePixmap (GDK_SCREEN_XDISPLAY (private->screen),
433 GDK_SCREEN_XROOTWIN (private->screen),
434 image->mem, private->x_shm_info,
435 image->width, image->height, image->depth);
437 return private->shm_pixmap;
444 get_full_image (GdkDrawable *drawable,
451 GdkImagePrivateX11 *private;
452 GdkDrawableImplX11 *impl;
455 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
457 ximage = XGetImage (GDK_SCREEN_XDISPLAY (impl->screen),
459 src_x, src_y, width, height,
465 image = g_object_new (gdk_image_get_type (), NULL);
467 private = PRIVATE_DATA (image);
469 private->screen = impl->screen;
470 private->ximage = ximage;
472 image->type = GDK_IMAGE_NORMAL;
473 image->visual = gdk_drawable_get_visual (drawable); /* could be NULL */
474 image->width = width;
475 image->height = height;
476 image->depth = gdk_drawable_get_depth (drawable);
478 image->mem = private->ximage->data;
479 image->bpl = private->ximage->bytes_per_line;
480 image->bits_per_pixel = private->ximage->bits_per_pixel;
481 image->bpp = (private->ximage->bits_per_pixel + 7) / 8;
482 image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST;
488 _gdk_x11_copy_to_image (GdkDrawable *drawable,
497 GdkImagePrivateX11 *private;
498 GdkDrawableImplX11 *impl;
504 GdkRectangle window_rect;
505 Pixmap shm_pixmap = None;
506 gboolean success = TRUE;
508 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_X11 (drawable), NULL);
509 g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL);
511 visual = gdk_drawable_get_visual (drawable);
512 impl = GDK_DRAWABLE_IMPL_X11 (drawable);
513 display = gdk_drawable_get_display (drawable);
514 xdisplay = gdk_x11_display_get_xdisplay (display);
521 #define UNGRAB() G_STMT_START { \
523 gdk_x11_display_ungrab (display); \
524 have_grab = FALSE; } \
527 if (!image && !GDK_IS_WINDOW_IMPL_X11 (drawable))
528 return get_full_image (drawable, src_x, src_y, width, height);
530 if (image && image->type == GDK_IMAGE_SHARED)
532 shm_pixmap = _gdk_x11_image_get_shm_pixmap (image);
538 /* Again easy, we can just XCopyArea, and don't have to worry about clipping
540 values.subwindow_mode = IncludeInferiors;
541 xgc = XCreateGC (xdisplay, impl->xid, GCSubwindowMode, &values);
543 XCopyArea (xdisplay, impl->xid, shm_pixmap, xgc,
544 src_x, src_y, width, height, dest_x, dest_y);
545 XSync (xdisplay, FALSE);
547 XFreeGC (xdisplay, xgc);
553 /* Now the general case - we may have to worry about clipping to the screen
554 * bounds, in which case we'll have to grab the server and only get a piece
557 if (GDK_IS_WINDOW_IMPL_X11 (drawable))
559 GdkRectangle screen_rect;
563 gdk_x11_display_grab (display);
565 /* Translate screen area into window coordinates */
566 XTranslateCoordinates (xdisplay,
567 GDK_SCREEN_XROOTWIN (impl->screen),
570 &screen_rect.x, &screen_rect.y,
573 screen_rect.width = gdk_screen_get_width (impl->screen);
574 screen_rect.height = gdk_screen_get_height (impl->screen);
576 gdk_error_trap_push ();
581 gdk_window_get_geometry (GDK_WINDOW (impl->wrapper),
587 /* compute intersection of screen and window, in window
590 if (gdk_error_trap_pop () ||
591 !gdk_rectangle_intersect (&window_rect, &screen_rect,
599 gdk_drawable_get_size (drawable,
601 &window_rect.height);
609 /* window_rect specifies the part of drawable which we can get from
610 * the server in window coordinates.
611 * For pixmaps this is all of the pixmap, for windows it is just
614 if (!gdk_rectangle_intersect (&req, &window_rect, &req))
617 gdk_error_trap_push ();
620 req.x == src_x && req.y == src_y && req.width == width && req.height == height)
622 image = get_full_image (drawable, src_x, src_y, width, height);
628 gboolean created_image = FALSE;
632 image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL,
633 visual, width, height,
634 gdk_drawable_get_depth (drawable));
635 created_image = TRUE;
638 private = PRIVATE_DATA (image);
640 /* In the ShmImage but no ShmPixmap case, we could use XShmGetImage when
641 * we are getting the entire image.
643 if (XGetSubImage (xdisplay, impl->xid,
644 req.x, req.y, req.width, req.height,
647 dest_x + req.x - src_x, dest_y + req.y - src_y) == None)
650 g_object_unref (image);
656 gdk_error_trap_pop ();
662 gdk_x11_display_ungrab (display);
666 if (success && !image)
668 /* We "succeeded", but could get no content for the image so return junk */
669 image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL,
670 visual, width, height,
671 gdk_drawable_get_depth (drawable));
678 gdk_image_get_pixel (GdkImage *image,
683 GdkImagePrivateX11 *private;
685 g_return_val_if_fail (GDK_IS_IMAGE (image), 0);
686 g_return_val_if_fail (x >= 0 && x < image->width, 0);
687 g_return_val_if_fail (y >= 0 && y < image->height, 0);
689 private = PRIVATE_DATA (image);
691 if (!private->screen->closed)
692 pixel = XGetPixel (private->ximage, x, y);
700 gdk_image_put_pixel (GdkImage *image,
705 GdkImagePrivateX11 *private;
707 g_return_if_fail (GDK_IS_IMAGE (image));
708 g_return_if_fail (x >= 0 && x < image->width);
709 g_return_if_fail (y >= 0 && y < image->height);
711 private = PRIVATE_DATA (image);
713 if (!private->screen->closed)
714 pixel = XPutPixel (private->ximage, x, y, pixel);
718 gdk_x11_image_destroy (GdkImage *image)
720 GdkImagePrivateX11 *private;
722 XShmSegmentInfo *x_shm_info;
725 g_return_if_fail (GDK_IS_IMAGE (image));
727 private = PRIVATE_DATA (image);
729 if (private == NULL) /* This means that _gdk_image_exit() destroyed the
730 * image already, and now we're called a second
731 * time from _finalize()
735 if (private->ximage) /* Deal with failure of creation */
739 case GDK_IMAGE_NORMAL:
740 if (!private->screen->closed)
741 XDestroyImage (private->ximage);
744 case GDK_IMAGE_SHARED:
746 if (!private->screen->closed)
748 gdk_display_sync (GDK_SCREEN_DISPLAY (private->screen));
750 if (private->shm_pixmap)
751 XFreePixmap (GDK_SCREEN_XDISPLAY (private->screen), private->shm_pixmap);
753 XShmDetach (GDK_SCREEN_XDISPLAY (private->screen), private->x_shm_info);
754 XDestroyImage (private->ximage);
757 image_list = g_list_remove (image_list, image);
759 x_shm_info = private->x_shm_info;
760 shmdt (x_shm_info->shmaddr);
762 g_free (private->x_shm_info);
763 private->x_shm_info = NULL;
766 g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support");
770 case GDK_IMAGE_FASTEST:
771 g_assert_not_reached ();
776 image->windowing_data = NULL;
780 * gdk_x11_image_get_xdisplay:
781 * @image: a #GdkImage.
783 * Returns the display of a #GdkImage.
785 * Return value: an Xlib <type>Display*</type>.
788 gdk_x11_image_get_xdisplay (GdkImage *image)
790 GdkImagePrivateX11 *private;
792 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
794 private = PRIVATE_DATA (image);
796 return GDK_SCREEN_XDISPLAY (private->screen);
800 * gdk_x11_image_get_ximage:
801 * @image: a #GdkImage.
803 * Returns the X image belonging to a #GdkImage.
805 * Return value: an <type>XImage*</type>.
808 gdk_x11_image_get_ximage (GdkImage *image)
810 GdkImagePrivateX11 *private;
812 g_return_val_if_fail (GDK_IS_IMAGE (image), NULL);
814 private = PRIVATE_DATA (image);
816 if (private->screen->closed)
819 return private->ximage;
823 _gdk_windowing_get_bits_for_depth (GdkDisplay *display,
826 XPixmapFormatValues *formats;
829 formats = XListPixmapFormats (GDK_DISPLAY_XDISPLAY (display), &count);
831 for (i = 0; i < count; i++)
832 if (formats[i].depth == depth)
834 gint result = formats[i].bits_per_pixel;
839 g_assert_not_reached ();
844 #define __GDK_IMAGE_X11_C__
845 #include "gdkaliasdef.c"