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