| 1 |
/* GDK - The GIMP Drawing Kit |
| 2 |
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
| 3 |
* |
| 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. |
| 8 |
* |
| 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. |
| 13 |
* |
| 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. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 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/. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "config.h" |
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <sys/types.h> |
| 31 |
|
| 32 |
#if defined (HAVE_IPC_H) && defined (HAVE_SHM_H) && defined (HAVE_XSHM_H) |
| 33 |
#define USE_SHM |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef USE_SHM |
| 37 |
#include <sys/ipc.h> |
| 38 |
#include <sys/shm.h> |
| 39 |
#endif /* USE_SHM */ |
| 40 |
|
| 41 |
#include <X11/Xlib.h> |
| 42 |
#include <X11/Xutil.h> |
| 43 |
|
| 44 |
#ifdef USE_SHM |
| 45 |
#include <X11/extensions/XShm.h> |
| 46 |
#endif /* USE_SHM */ |
| 47 |
|
| 48 |
#include <errno.h> |
| 49 |
|
| 50 |
#include "gdk.h" /* For gdk_error_trap_* / gdk_flush_* */ |
| 51 |
#include "gdkx.h" |
| 52 |
#include "gdkimage.h" |
| 53 |
#include "gdkprivate.h" |
| 54 |
#include "gdkprivate-x11.h" |
| 55 |
#include "gdkdisplay-x11.h" |
| 56 |
#include "gdkscreen-x11.h" |
| 57 |
#include "gdkalias.h" |
| 58 |
|
| 59 |
typedef struct _GdkImagePrivateX11 GdkImagePrivateX11; |
| 60 |
|
| 61 |
struct _GdkImagePrivateX11 |
| 62 |
{ |
| 63 |
XImage *ximage; |
| 64 |
GdkScreen *screen; |
| 65 |
gpointer x_shm_info; |
| 66 |
Pixmap shm_pixmap; |
| 67 |
}; |
| 68 |
|
| 69 |
static GList *image_list = NULL; |
| 70 |
|
| 71 |
static void gdk_x11_image_destroy (GdkImage *image); |
| 72 |
static void gdk_image_finalize (GObject *object); |
| 73 |
|
| 74 |
#define PRIVATE_DATA(image) ((GdkImagePrivateX11 *) GDK_IMAGE (image)->windowing_data) |
| 75 |
|
| 76 |
G_DEFINE_TYPE (GdkImage, gdk_image, G_TYPE_OBJECT) |
| 77 |
|
| 78 |
static void |
| 79 |
gdk_image_init (GdkImage *image) |
| 80 |
{ |
| 81 |
image->windowing_data = G_TYPE_INSTANCE_GET_PRIVATE (image, |
| 82 |
GDK_TYPE_IMAGE, |
| 83 |
GdkImagePrivateX11); |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
gdk_image_class_init (GdkImageClass *klass) |
| 88 |
{ |
| 89 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 90 |
|
| 91 |
object_class->finalize = gdk_image_finalize; |
| 92 |
|
| 93 |
g_type_class_add_private (object_class, sizeof (GdkImagePrivateX11)); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
gdk_image_finalize (GObject *object) |
| 98 |
{ |
| 99 |
GdkImage *image = GDK_IMAGE (object); |
| 100 |
|
| 101 |
gdk_x11_image_destroy (image); |
| 102 |
|
| 103 |
G_OBJECT_CLASS (gdk_image_parent_class)->finalize (object); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
void |
| 108 |
_gdk_image_exit (void) |
| 109 |
{ |
| 110 |
GdkImage *image; |
| 111 |
|
| 112 |
while (image_list) |
| 113 |
{ |
| 114 |
image = image_list->data; |
| 115 |
gdk_x11_image_destroy (image); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* gdk_image_new_bitmap: |
| 121 |
* @visual: the #GdkVisual to use for the image. |
| 122 |
* @data: the pixel data. |
| 123 |
* @width: the width of the image in pixels. |
| 124 |
* @height: the height of the image in pixels. |
| 125 |
* |
| 126 |
* Creates a new #GdkImage with a depth of 1 from the given data. |
| 127 |
* <warning><para>THIS FUNCTION IS INCREDIBLY BROKEN. The passed-in data must |
| 128 |
* be allocated by malloc() (NOT g_malloc()) and will be freed when the |
| 129 |
* image is freed.</para></warning> |
| 130 |
* |
| 131 |
* Return value: a new #GdkImage. |
| 132 |
**/ |
| 133 |
GdkImage * |
| 134 |
gdk_image_new_bitmap (GdkVisual *visual, |
| 135 |
gpointer data, |
| 136 |
gint width, |
| 137 |
gint height) |
| 138 |
{ |
| 139 |
Visual *xvisual; |
| 140 |
GdkImage *image; |
| 141 |
GdkDisplay *display; |
| 142 |
GdkImagePrivateX11 *private; |
| 143 |
|
| 144 |
image = g_object_new (gdk_image_get_type (), NULL); |
| 145 |
private = PRIVATE_DATA (image); |
| 146 |
private->screen = gdk_visual_get_screen (visual); |
| 147 |
display = GDK_SCREEN_DISPLAY (private->screen); |
| 148 |
|
| 149 |
image->type = GDK_IMAGE_NORMAL; |
| 150 |
image->visual = visual; |
| 151 |
image->width = width; |
| 152 |
image->height = height; |
| 153 |
image->depth = 1; |
| 154 |
image->bits_per_pixel = 1; |
| 155 |
if (display->closed) |
| 156 |
private->ximage = NULL; |
| 157 |
else |
| 158 |
{ |
| 159 |
xvisual = ((GdkVisualPrivate*) visual)->xvisual; |
| 160 |
private->ximage = XCreateImage (GDK_SCREEN_XDISPLAY (private->screen), |
| 161 |
xvisual, 1, XYBitmap, |
| 162 |
0, NULL, width, height, 8, 0); |
| 163 |
private->ximage->data = data; |
| 164 |
private->ximage->bitmap_bit_order = MSBFirst; |
| 165 |
private->ximage->byte_order = MSBFirst; |
| 166 |
} |
| 167 |
|
| 168 |
image->byte_order = MSBFirst; |
| 169 |
image->mem = private->ximage->data; |
| 170 |
image->bpl = private->ximage->bytes_per_line; |
| 171 |
image->bpp = 1; |
| 172 |
return image; |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
_gdk_windowing_image_init (GdkDisplay *display) |
| 177 |
{ |
| 178 |
GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display); |
| 179 |
|
| 180 |
if (display_x11->use_xshm) |
| 181 |
{ |
| 182 |
#ifdef USE_SHM |
| 183 |
Display *xdisplay = display_x11->xdisplay; |
| 184 |
int major, minor, event_base; |
| 185 |
Bool pixmaps; |
| 186 |
|
| 187 |
if (XShmQueryExtension (xdisplay) && |
| 188 |
XShmQueryVersion (xdisplay, &major, &minor, &pixmaps)) |
| 189 |
{ |
| 190 |
display_x11->have_shm_pixmaps = pixmaps; |
| 191 |
event_base = XShmGetEventBase (xdisplay); |
| 192 |
|
| 193 |
gdk_x11_register_standard_event_type (display, |
| 194 |
event_base, ShmNumberEvents); |
| 195 |
} |
| 196 |
else |
| 197 |
#endif /* USE_SHM */ |
| 198 |
display_x11->use_xshm = FALSE; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
GdkImage* |
| 203 |
_gdk_image_new_for_depth (GdkScreen *screen, |
| 204 |
GdkImageType type, |
| 205 |
GdkVisual *visual, |
| 206 |
gint width, |
| 207 |
gint height, |
| 208 |
gint depth) |
| 209 |
{ |
| 210 |
GdkImage *image; |
| 211 |
GdkImagePrivateX11 *private; |
| 212 |
#ifdef USE_SHM |
| 213 |
XShmSegmentInfo *x_shm_info; |
| 214 |
#endif /* USE_SHM */ |
| 215 |
Visual *xvisual = NULL; |
| 216 |
GdkDisplayX11 *display_x11; |
| 217 |
GdkScreenX11 *screen_x11; |
| 218 |
|
| 219 |
g_return_val_if_fail (!visual || GDK_IS_VISUAL (visual), NULL); |
| 220 |
g_return_val_if_fail (visual || depth != -1, NULL); |
| 221 |
g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL); |
| 222 |
|
| 223 |
screen_x11 = GDK_SCREEN_X11 (screen); |
| 224 |
display_x11 = GDK_DISPLAY_X11 (screen_x11->display); |
| 225 |
|
| 226 |
if (visual) |
| 227 |
depth = visual->depth; |
| 228 |
|
| 229 |
switch (type) |
| 230 |
{ |
| 231 |
case GDK_IMAGE_FASTEST: |
| 232 |
image = _gdk_image_new_for_depth (screen, GDK_IMAGE_SHARED, |
| 233 |
visual, width, height, depth); |
| 234 |
if (!image) |
| 235 |
image = _gdk_image_new_for_depth (screen, GDK_IMAGE_NORMAL, |
| 236 |
visual, width, height, depth); |
| 237 |
break; |
| 238 |
|
| 239 |
default: |
| 240 |
image = g_object_new (gdk_image_get_type (), NULL); |
| 241 |
|
| 242 |
private = PRIVATE_DATA (image); |
| 243 |
|
| 244 |
private->screen = screen; |
| 245 |
|
| 246 |
image->type = type; |
| 247 |
image->visual = visual; |
| 248 |
image->width = width; |
| 249 |
image->height = height; |
| 250 |
image->depth = depth; |
| 251 |
|
| 252 |
if (visual) |
| 253 |
xvisual = ((GdkVisualPrivate*) visual)->xvisual; |
| 254 |
|
| 255 |
switch (type) |
| 256 |
{ |
| 257 |
case GDK_IMAGE_SHARED: |
| 258 |
#ifdef USE_SHM |
| 259 |
if (display_x11->use_xshm) |
| 260 |
{ |
| 261 |
private->x_shm_info = g_new (XShmSegmentInfo, 1); |
| 262 |
x_shm_info = private->x_shm_info; |
| 263 |
x_shm_info->shmid = -1; |
| 264 |
x_shm_info->shmaddr = (char*) -1; |
| 265 |
|
| 266 |
private->ximage = XShmCreateImage (screen_x11->xdisplay, xvisual, depth, |
| 267 |
ZPixmap, NULL, x_shm_info, width, height); |
| 268 |
if (private->ximage == NULL) |
| 269 |
{ |
| 270 |
g_warning ("XShmCreateImage failed"); |
| 271 |
display_x11->use_xshm = FALSE; |
| 272 |
|
| 273 |
goto error; |
| 274 |
} |
| 275 |
|
| 276 |
x_shm_info->shmid = shmget (IPC_PRIVATE, |
| 277 |
private->ximage->bytes_per_line * private->ximage->height, |
| 278 |
IPC_CREAT | 0600); |
| 279 |
|
| 280 |
if (x_shm_info->shmid == -1) |
| 281 |
{ |
| 282 |
/* EINVAL indicates, most likely, that the segment we asked for |
| 283 |
* is bigger than SHMMAX, so we don't treat it as a permanent |
| 284 |
* error. ENOSPC and ENOMEM may also indicate this, but |
| 285 |
* more likely are permanent errors. |
| 286 |
*/ |
| 287 |
if (errno != EINVAL) |
| 288 |
{ |
| 289 |
g_warning ("shmget failed: error %d (%s)", errno, g_strerror (errno)); |
| 290 |
display_x11->use_xshm = FALSE; |
| 291 |
} |
| 292 |
|
| 293 |
goto error; |
| 294 |
} |
| 295 |
|
| 296 |
x_shm_info->readOnly = False; |
| 297 |
x_shm_info->shmaddr = shmat (x_shm_info->shmid, NULL, 0); |
| 298 |
private->ximage->data = x_shm_info->shmaddr; |
| 299 |
|
| 300 |
if (x_shm_info->shmaddr == (char*) -1) |
| 301 |
{ |
| 302 |
g_warning ("shmat failed: error %d (%s)", errno, g_strerror (errno)); |
| 303 |
/* Failure in shmat is almost certainly permanent. Most likely error is |
| 304 |
* EMFILE, which would mean that we've exceeded the per-process |
| 305 |
* Shm segment limit. |
| 306 |
*/ |
| 307 |
display_x11->use_xshm = FALSE; |
| 308 |
goto error; |
| 309 |
} |
| 310 |
|
| 311 |
gdk_error_trap_push (); |
| 312 |
|
| 313 |
XShmAttach (screen_x11->xdisplay, x_shm_info); |
| 314 |
XSync (screen_x11->xdisplay, False); |
| 315 |
|
| 316 |
if (gdk_error_trap_pop ()) |
| 317 |
{ |
| 318 |
/* this is the common failure case so omit warning */ |
| 319 |
display_x11->use_xshm = FALSE; |
| 320 |
goto error; |
| 321 |
} |
| 322 |
|
| 323 |
/* We mark the segment as destroyed so that when |
| 324 |
* the last process detaches, it will be deleted. |
| 325 |
* There is a small possibility of leaking if |
| 326 |
* we die in XShmAttach. In theory, a signal handler |
| 327 |
* could be set up. |
| 328 |
*/ |
| 329 |
shmctl (x_shm_info->shmid, IPC_RMID, NULL); |
| 330 |
|
| 331 |
if (image) |
| 332 |
image_list = g_list_prepend (image_list, image); |
| 333 |
} |
| 334 |
else |
| 335 |
#endif /* USE_SHM */ |
| 336 |
goto error; |
| 337 |
break; |
| 338 |
case GDK_IMAGE_NORMAL: |
| 339 |
private->ximage = XCreateImage (screen_x11->xdisplay, xvisual, depth, |
| 340 |
ZPixmap, 0, NULL, width, height, 32, 0); |
| 341 |
|
| 342 |
/* Use malloc, not g_malloc here, because X will call free() |
| 343 |
* on this data |
| 344 |
*/ |
| 345 |
private->ximage->data = malloc (private->ximage->bytes_per_line * |
| 346 |
private->ximage->height); |
| 347 |
if (!private->ximage->data) |
| 348 |
goto error; |
| 349 |
break; |
| 350 |
|
| 351 |
case GDK_IMAGE_FASTEST: |
| 352 |
g_assert_not_reached (); |
| 353 |
} |
| 354 |
|
| 355 |
if (image) |
| 356 |
{ |
| 357 |
image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST; |
| 358 |
image->mem = private->ximage->data; |
| 359 |
image->bpl = private->ximage->bytes_per_line; |
| 360 |
image->bpp = (private->ximage->bits_per_pixel + 7) / 8; |
| 361 |
image->bits_per_pixel = private->ximage->bits_per_pixel; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
return image; |
| 366 |
|
| 367 |
error: |
| 368 |
if (private->ximage) |
| 369 |
{ |
| 370 |
XDestroyImage (private->ximage); |
| 371 |
private->ximage = NULL; |
| 372 |
} |
| 373 |
#ifdef USE_SHM |
| 374 |
if (private->x_shm_info) |
| 375 |
{ |
| 376 |
x_shm_info = private->x_shm_info; |
| 377 |
|
| 378 |
if (x_shm_info->shmaddr != (char *)-1) |
| 379 |
shmdt (x_shm_info->shmaddr); |
| 380 |
if (x_shm_info->shmid != -1) |
| 381 |
shmctl (x_shm_info->shmid, IPC_RMID, NULL); |
| 382 |
|
| 383 |
g_free (x_shm_info); |
| 384 |
private->x_shm_info = NULL; |
| 385 |
} |
| 386 |
#endif /* USE_SHM */ |
| 387 |
g_object_unref (image); |
| 388 |
|
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
Pixmap |
| 393 |
_gdk_x11_image_get_shm_pixmap (GdkImage *image) |
| 394 |
{ |
| 395 |
GdkImagePrivateX11 *private = PRIVATE_DATA (image); |
| 396 |
GdkDisplay *display = GDK_SCREEN_DISPLAY (private->screen); |
| 397 |
|
| 398 |
if (display->closed) |
| 399 |
return None; |
| 400 |
|
| 401 |
#ifdef USE_SHM |
| 402 |
/* Future: do we need one of these per-screen per-image? ShmPixmaps |
| 403 |
* are the same for every screen, but can they be shared? Not a concern |
| 404 |
* right now since we tie images to a particular screen. |
| 405 |
*/ |
| 406 |
if (!private->shm_pixmap && image->type == GDK_IMAGE_SHARED && |
| 407 |
GDK_DISPLAY_X11 (display)->have_shm_pixmaps) |
| 408 |
private->shm_pixmap = XShmCreatePixmap (GDK_SCREEN_XDISPLAY (private->screen), |
| 409 |
GDK_SCREEN_XROOTWIN (private->screen), |
| 410 |
image->mem, private->x_shm_info, |
| 411 |
image->width, image->height, image->depth); |
| 412 |
|
| 413 |
return private->shm_pixmap; |
| 414 |
#else |
| 415 |
return None; |
| 416 |
#endif |
| 417 |
} |
| 418 |
|
| 419 |
static GdkImage* |
| 420 |
get_full_image (GdkDrawable *drawable, |
| 421 |
gint src_x, |
| 422 |
gint src_y, |
| 423 |
gint width, |
| 424 |
gint height) |
| 425 |
{ |
| 426 |
GdkImage *image; |
| 427 |
GdkImagePrivateX11 *private; |
| 428 |
GdkDrawableImplX11 *impl; |
| 429 |
XImage *ximage; |
| 430 |
|
| 431 |
impl = GDK_DRAWABLE_IMPL_X11 (drawable); |
| 432 |
|
| 433 |
ximage = XGetImage (GDK_SCREEN_XDISPLAY (impl->screen), |
| 434 |
impl->xid, |
| 435 |
src_x, src_y, width, height, |
| 436 |
AllPlanes, ZPixmap); |
| 437 |
|
| 438 |
if (!ximage) |
| 439 |
return NULL; |
| 440 |
|
| 441 |
image = g_object_new (gdk_image_get_type (), NULL); |
| 442 |
|
| 443 |
private = PRIVATE_DATA (image); |
| 444 |
|
| 445 |
private->screen = impl->screen; |
| 446 |
private->ximage = ximage; |
| 447 |
|
| 448 |
image->type = GDK_IMAGE_NORMAL; |
| 449 |
image->visual = gdk_drawable_get_visual (drawable); /* could be NULL */ |
| 450 |
image->width = width; |
| 451 |
image->height = height; |
| 452 |
image->depth = gdk_drawable_get_depth (drawable); |
| 453 |
|
| 454 |
image->mem = private->ximage->data; |
| 455 |
image->bpl = private->ximage->bytes_per_line; |
| 456 |
image->bits_per_pixel = private->ximage->bits_per_pixel; |
| 457 |
image->bpp = (private->ximage->bits_per_pixel + 7) / 8; |
| 458 |
image->byte_order = (private->ximage->byte_order == LSBFirst) ? GDK_LSB_FIRST : GDK_MSB_FIRST; |
| 459 |
|
| 460 |
return image; |
| 461 |
} |
| 462 |
|
| 463 |
GdkImage* |
| 464 |
_gdk_x11_copy_to_image (GdkDrawable *drawable, |
| 465 |
GdkImage *image, |
| 466 |
gint src_x, |
| 467 |
gint src_y, |
| 468 |
gint dest_x, |
| 469 |
gint dest_y, |
| 470 |
gint width, |
| 471 |
gint height) |
| 472 |
{ |
| 473 |
GdkImagePrivateX11 *private; |
| 474 |
GdkDrawableImplX11 *impl; |
| 475 |
GdkVisual *visual; |
| 476 |
GdkDisplay *display; |
| 477 |
Display *xdisplay; |
| 478 |
gboolean have_grab; |
| 479 |
GdkRectangle req; |
| 480 |
GdkRectangle window_rect; |
| 481 |
Pixmap shm_pixmap = None; |
| 482 |
gboolean success = TRUE; |
| 483 |
|
| 484 |
g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_X11 (drawable), NULL); |
| 485 |
g_return_val_if_fail (image != NULL || (dest_x == 0 && dest_y == 0), NULL); |
| 486 |
|
| 487 |
visual = gdk_drawable_get_visual (drawable); |
| 488 |
impl = GDK_DRAWABLE_IMPL_X11 (drawable); |
| 489 |
display = gdk_drawable_get_display (drawable); |
| 490 |
xdisplay = gdk_x11_display_get_xdisplay (display); |
| 491 |
|
| 492 |
if (display->closed) |
| 493 |
return NULL; |
| 494 |
|
| 495 |
have_grab = FALSE; |
| 496 |
|
| 497 |
#define UNGRAB() G_STMT_START { \ |
| 498 |
if (have_grab) { \ |
| 499 |
gdk_x11_display_ungrab (display); \ |
| 500 |
have_grab = FALSE; } \ |
| 501 |
} G_STMT_END |
| 502 |
|
| 503 |
if (!image && !GDK_IS_WINDOW_IMPL_X11 (drawable)) |
| 504 |
return get_full_image (drawable, src_x, src_y, width, height); |
| 505 |
|
| 506 |
if (image && image->type == GDK_IMAGE_SHARED) |
| 507 |
{ |
| 508 |
shm_pixmap = _gdk_x11_image_get_shm_pixmap (image); |
| 509 |
if (shm_pixmap) |
| 510 |
{ |
| 511 |
GC xgc; |
| 512 |
XGCValues values; |
| 513 |
|
| 514 |
/* Again easy, we can just XCopyArea, and don't have to worry about clipping |
| 515 |
*/ |
| 516 |
values.subwindow_mode = IncludeInferiors; |
| 517 |
xgc = XCreateGC (xdisplay, impl->xid, GCSubwindowMode, &values); |
| 518 |
|
| 519 |
XCopyArea (xdisplay, impl->xid, shm_pixmap, xgc, |
| 520 |
src_x, src_y, width, height, dest_x, dest_y); |
| 521 |
XSync (xdisplay, FALSE); |
| 522 |
|
| 523 |
XFreeGC (xdisplay, xgc); |
| 524 |
|
| 525 |
return image; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
/* Now the general case - we may have to worry about clipping to the screen |
| 530 |
* bounds, in which case we'll have to grab the server and only get a piece |
| 531 |
* of the window. |
| 532 |
*/ |
| 533 |
if (GDK_IS_WINDOW_IMPL_X11 (drawable)) |
| 534 |
{ |
| 535 |
GdkRectangle screen_rect; |
| 536 |
Window child; |
| 537 |
|
| 538 |
have_grab = TRUE; |
| 539 |
gdk_x11_display_grab (display); |
| 540 |
|
| 541 |
/* Translate screen area into window coordinates */ |
| 542 |
XTranslateCoordinates (xdisplay, |
| 543 |
GDK_SCREEN_XROOTWIN (impl->screen), |
| 544 |
impl->xid, |
| 545 |
0, 0, |
| 546 |
&screen_rect.x, &screen_rect.y, |
| 547 |
&child); |
| 548 |
|
| 549 |
screen_rect.width = gdk_screen_get_width (impl->screen); |
| 550 |
screen_rect.height = gdk_screen_get_height (impl->screen); |
| 551 |
|
| 552 |
gdk_error_trap_push (); |
| 553 |
|
| 554 |
window_rect.x = 0; |
| 555 |
window_rect.y = 0; |
| 556 |
|
| 557 |
gdk_window_get_geometry (GDK_WINDOW (impl->wrapper), |
| 558 |
NULL, NULL, |
| 559 |
&window_rect.width, |
| 560 |
&window_rect.height, |
| 561 |
NULL); |
| 562 |
|
| 563 |
/* compute intersection of screen and window, in window |
| 564 |
* coordinates |
| 565 |
*/ |
| 566 |
if (gdk_error_trap_pop () || |
| 567 |
!gdk_rectangle_intersect (&window_rect, &screen_rect, |
| 568 |
&window_rect)) |
| 569 |
goto out; |
| 570 |
} |
| 571 |
else |
| 572 |
{ |
| 573 |
window_rect.x = 0; |
| 574 |
window_rect.y = 0; |
| 575 |
gdk_drawable_get_size (drawable, |
| 576 |
&window_rect.width, |
| 577 |
&window_rect.height); |
| 578 |
} |
| 579 |
|
| 580 |
req.x = src_x; |
| 581 |
req.y = src_y; |
| 582 |
req.width = width; |
| 583 |
req.height = height; |
| 584 |
|
| 585 |
/* window_rect specifies the part of drawable which we can get from |
| 586 |
* the server in window coordinates. |
| 587 |
* For pixmaps this is all of the pixmap, for windows it is just |
| 588 |
* the onscreen part. |
| 589 |
*/ |
| 590 |
if (!gdk_rectangle_intersect (&req, &window_rect, &req)) |
| 591 |
goto out; |
| 592 |
|
| 593 |
gdk_error_trap_push (); |
| 594 |
|
| 595 |
if (!image && |
| 596 |
req.x == src_x && req.y == src_y && req.width == width && req.height == height) |
| 597 |
{ |
| 598 |
image = get_full_image (drawable, src_x, src_y, width, height); |
| 599 |
if (!image) |
| 600 |
success = FALSE; |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
gboolean created_image = FALSE; |
| 605 |
|
| 606 |
if (!image) |
| 607 |
{ |
| 608 |
image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL, |
| 609 |
visual, width, height, |
| 610 |
gdk_drawable_get_depth (drawable)); |
| 611 |
created_image = TRUE; |
| 612 |
} |
| 613 |
|
| 614 |
private = PRIVATE_DATA (image); |
| 615 |
|
| 616 |
/* In the ShmImage but no ShmPixmap case, we could use XShmGetImage when |
| 617 |
* we are getting the entire image. |
| 618 |
*/ |
| 619 |
if (XGetSubImage (xdisplay, impl->xid, |
| 620 |
req.x, req.y, req.width, req.height, |
| 621 |
AllPlanes, ZPixmap, |
| 622 |
private->ximage, |
| 623 |
dest_x + req.x - src_x, dest_y + req.y - src_y) == None) |
| 624 |
{ |
| 625 |
if (created_image) |
| 626 |
g_object_unref (image); |
| 627 |
image = NULL; |
| 628 |
success = FALSE; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
gdk_error_trap_pop (); |
| 633 |
|
| 634 |
out: |
| 635 |
|
| 636 |
if (have_grab) |
| 637 |
{ |
| 638 |
gdk_x11_display_ungrab (display); |
| 639 |
have_grab = FALSE; |
| 640 |
} |
| 641 |
|
| 642 |
if (success && !image) |
| 643 |
{ |
| 644 |
/* We "succeeded", but could get no content for the image so return junk */ |
| 645 |
image = _gdk_image_new_for_depth (impl->screen, GDK_IMAGE_NORMAL, |
| 646 |
visual, width, height, |
| 647 |
gdk_drawable_get_depth (drawable)); |
| 648 |
} |
| 649 |
|
| 650 |
return image; |
| 651 |
} |
| 652 |
|
| 653 |
guint32 |
| 654 |
gdk_image_get_pixel (GdkImage *image, |
| 655 |
gint x, |
| 656 |
gint y) |
| 657 |
{ |
| 658 |
guint32 pixel; |
| 659 |
GdkImagePrivateX11 *private; |
| 660 |
|
| 661 |
g_return_val_if_fail (GDK_IS_IMAGE (image), 0); |
| 662 |
g_return_val_if_fail (x >= 0 && x < image->width, 0); |
| 663 |
g_return_val_if_fail (y >= 0 && y < image->height, 0); |
| 664 |
|
| 665 |
private = PRIVATE_DATA (image); |
| 666 |
|
| 667 |
if (!private->screen->closed) |
| 668 |
pixel = XGetPixel (private->ximage, x, y); |
| 669 |
else |
| 670 |
pixel = 0; |
| 671 |
|
| 672 |
return pixel; |
| 673 |
} |
| 674 |
|
| 675 |
void |
| 676 |
gdk_image_put_pixel (GdkImage *image, |
| 677 |
gint x, |
| 678 |
gint y, |
| 679 |
guint32 pixel) |
| 680 |
{ |
| 681 |
GdkImagePrivateX11 *private; |
| 682 |
|
| 683 |
g_return_if_fail (GDK_IS_IMAGE (image)); |
| 684 |
g_return_if_fail (x >= 0 && x < image->width); |
| 685 |
g_return_if_fail (y >= 0 && y < image->height); |
| 686 |
|
| 687 |
private = PRIVATE_DATA (image); |
| 688 |
|
| 689 |
if (!private->screen->closed) |
| 690 |
pixel = XPutPixel (private->ximage, x, y, pixel); |
| 691 |
} |
| 692 |
|
| 693 |
static void |
| 694 |
gdk_x11_image_destroy (GdkImage *image) |
| 695 |
{ |
| 696 |
GdkImagePrivateX11 *private; |
| 697 |
#ifdef USE_SHM |
| 698 |
XShmSegmentInfo *x_shm_info; |
| 699 |
#endif /* USE_SHM */ |
| 700 |
|
| 701 |
g_return_if_fail (GDK_IS_IMAGE (image)); |
| 702 |
|
| 703 |
private = PRIVATE_DATA (image); |
| 704 |
|
| 705 |
if (private->ximage) /* Deal with failure of creation */ |
| 706 |
{ |
| 707 |
switch (image->type) |
| 708 |
{ |
| 709 |
case GDK_IMAGE_NORMAL: |
| 710 |
if (!private->screen->closed) |
| 711 |
XDestroyImage (private->ximage); |
| 712 |
break; |
| 713 |
|
| 714 |
case GDK_IMAGE_SHARED: |
| 715 |
#ifdef USE_SHM |
| 716 |
if (!private->screen->closed) |
| 717 |
{ |
| 718 |
gdk_display_sync (GDK_SCREEN_DISPLAY (private->screen)); |
| 719 |
|
| 720 |
if (private->shm_pixmap) |
| 721 |
XFreePixmap (GDK_SCREEN_XDISPLAY (private->screen), private->shm_pixmap); |
| 722 |
|
| 723 |
XShmDetach (GDK_SCREEN_XDISPLAY (private->screen), private->x_shm_info); |
| 724 |
XDestroyImage (private->ximage); |
| 725 |
} |
| 726 |
|
| 727 |
image_list = g_list_remove (image_list, image); |
| 728 |
|
| 729 |
x_shm_info = private->x_shm_info; |
| 730 |
shmdt (x_shm_info->shmaddr); |
| 731 |
|
| 732 |
g_free (private->x_shm_info); |
| 733 |
private->x_shm_info = NULL; |
| 734 |
|
| 735 |
#else /* USE_SHM */ |
| 736 |
g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support"); |
| 737 |
#endif /* USE_SHM */ |
| 738 |
break; |
| 739 |
|
| 740 |
case GDK_IMAGE_FASTEST: |
| 741 |
g_assert_not_reached (); |
| 742 |
} |
| 743 |
|
| 744 |
private->ximage = NULL; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
* gdk_x11_image_get_xdisplay: |
| 750 |
* @image: a #GdkImage. |
| 751 |
* |
| 752 |
* Returns the display of a #GdkImage. |
| 753 |
* |
| 754 |
* Return value: an Xlib <type>Display*</type>. |
| 755 |
**/ |
| 756 |
Display * |
| 757 |
gdk_x11_image_get_xdisplay (GdkImage *image) |
| 758 |
{ |
| 759 |
GdkImagePrivateX11 *private; |
| 760 |
|
| 761 |
g_return_val_if_fail (GDK_IS_IMAGE (image), NULL); |
| 762 |
|
| 763 |
private = PRIVATE_DATA (image); |
| 764 |
|
| 765 |
return GDK_SCREEN_XDISPLAY (private->screen); |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* gdk_x11_image_get_ximage: |
| 770 |
* @image: a #GdkImage. |
| 771 |
* |
| 772 |
* Returns the X image belonging to a #GdkImage. |
| 773 |
* |
| 774 |
* Return value: an <type>XImage*</type>. |
| 775 |
**/ |
| 776 |
XImage * |
| 777 |
gdk_x11_image_get_ximage (GdkImage *image) |
| 778 |
{ |
| 779 |
GdkImagePrivateX11 *private; |
| 780 |
|
| 781 |
g_return_val_if_fail (GDK_IS_IMAGE (image), NULL); |
| 782 |
|
| 783 |
private = PRIVATE_DATA (image); |
| 784 |
|
| 785 |
if (private->screen->closed) |
| 786 |
return NULL; |
| 787 |
else |
| 788 |
return private->ximage; |
| 789 |
} |
| 790 |
|
| 791 |
gint |
| 792 |
_gdk_windowing_get_bits_for_depth (GdkDisplay *display, |
| 793 |
gint depth) |
| 794 |
{ |
| 795 |
XPixmapFormatValues *formats; |
| 796 |
gint count, i; |
| 797 |
|
| 798 |
formats = XListPixmapFormats (GDK_DISPLAY_XDISPLAY (display), &count); |
| 799 |
|
| 800 |
for (i = 0; i < count; i++) |
| 801 |
if (formats[i].depth == depth) |
| 802 |
{ |
| 803 |
gint result = formats[i].bits_per_pixel; |
| 804 |
XFree (formats); |
| 805 |
return result; |
| 806 |
} |
| 807 |
|
| 808 |
g_assert_not_reached (); |
| 809 |
return -1; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
#define __GDK_IMAGE_X11_C__ |
| 814 |
#include "gdkaliasdef.c" |