]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdkimage-x11.c
Removed features.h test.
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include <config.h>
20
21 /* gcc -ansi -pedantic on GNU/Linux causes warnings and errors
22  * unless this is defined:
23  * warning: #warning "Files using this header must be compiled with _SVID_SOURCE or _XOPEN_SOURCE"
24  */
25 #ifndef _XOPEN_SOURCE
26 #  define _XOPEN_SOURCE 1
27 #endif
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 "gdk.h"
49 #include "gdkprivate.h"
50
51
52 static void gdk_image_put_normal (GdkDrawable *drawable,
53                                   GdkGC       *gc,
54                                   GdkImage    *image,
55                                   gint         xsrc,
56                                   gint         ysrc,
57                                   gint         xdest,
58                                   gint         ydest,
59                                   gint         width,
60                                   gint         height);
61 static void gdk_image_put_shared (GdkDrawable *drawable,
62                                   GdkGC       *gc,
63                                   GdkImage    *image,
64                                   gint         xsrc,
65                                   gint         ysrc,
66                                   gint         xdest,
67                                   gint         ydest,
68                                   gint         width,
69                                   gint         height);
70
71
72 static GList *image_list = NULL;
73
74
75 void
76 gdk_image_exit (void)
77 {
78   GdkImage *image;
79
80   while (image_list)
81     {
82       image = image_list->data;
83       gdk_image_destroy (image);
84     }
85 }
86
87 GdkImage *
88 gdk_image_new_bitmap(GdkVisual *visual, gpointer data, gint w, gint h)
89 /*
90  * Desc: create a new bitmap image
91  */
92 {
93         Visual *xvisual;
94         GdkImage *image;
95         GdkImagePrivate *private;
96         private = g_new(GdkImagePrivate, 1);
97         image = (GdkImage *) private;
98         private->xdisplay = gdk_display;
99         private->image_put = gdk_image_put_normal;
100         image->type = GDK_IMAGE_NORMAL;
101         image->visual = visual;
102         image->width = w;
103         image->height = h;
104         image->depth = 1;
105         xvisual = ((GdkVisualPrivate*) visual)->xvisual;
106         private->ximage = XCreateImage(private->xdisplay, xvisual, 1, XYBitmap,
107                                        0, 0, w ,h, 8, 0);
108         private->ximage->data = data;
109         private->ximage->bitmap_bit_order = MSBFirst;
110         private->ximage->byte_order = MSBFirst;
111         image->byte_order = MSBFirst;
112         image->mem =  private->ximage->data;
113         image->bpl = private->ximage->bytes_per_line;
114         image->bpp = 1;
115         return(image);
116 } /* gdk_image_new_bitmap() */
117
118 static int
119 gdk_image_check_xshm(Display *display)
120 /* 
121  * Desc: query the server for support for the MIT_SHM extension
122  * Return:  0 = not available
123  *          1 = shared XImage support available
124  *          2 = shared Pixmap support available also
125  */
126 {
127 #ifdef USE_SHM
128   int major, minor, ignore;
129   Bool pixmaps;
130   
131   if (XQueryExtension(display, "MIT-SHM", &ignore, &ignore, &ignore)) 
132     {
133       if (XShmQueryVersion(display, &major, &minor, &pixmaps )==True) 
134         {
135           return (pixmaps==True) ? 2 : 1;
136         }
137     }
138 #endif /* USE_SHM */
139   return 0;
140 }
141
142 void
143 gdk_image_init (void)
144 {
145   if (gdk_use_xshm)
146     {
147       if (!gdk_image_check_xshm (gdk_display))
148         {
149           gdk_use_xshm = False;
150         }
151     }
152 }
153
154 GdkImage*
155 gdk_image_new (GdkImageType  type,
156                GdkVisual    *visual,
157                gint          width,
158                gint          height)
159 {
160   GdkImage *image;
161   GdkImagePrivate *private;
162 #ifdef USE_SHM
163   XShmSegmentInfo *x_shm_info;
164 #endif /* USE_SHM */
165   Visual *xvisual;
166
167   switch (type)
168     {
169     case GDK_IMAGE_FASTEST:
170       image = gdk_image_new (GDK_IMAGE_SHARED, visual, width, height);
171
172       if (!image)
173         image = gdk_image_new (GDK_IMAGE_NORMAL, visual, width, height);
174       break;
175
176     default:
177       private = g_new (GdkImagePrivate, 1);
178       image = (GdkImage*) private;
179
180       private->xdisplay = gdk_display;
181       private->image_put = NULL;
182
183       image->type = type;
184       image->visual = visual;
185       image->width = width;
186       image->height = height;
187       image->depth = visual->depth;
188
189       xvisual = ((GdkVisualPrivate*) visual)->xvisual;
190
191       switch (type)
192         {
193         case GDK_IMAGE_SHARED:
194 #ifdef USE_SHM
195           if (gdk_use_xshm)
196             {
197               private->image_put = gdk_image_put_shared;
198
199               private->x_shm_info = g_new (XShmSegmentInfo, 1);
200               x_shm_info = private->x_shm_info;
201
202               private->ximage = XShmCreateImage (private->xdisplay, xvisual, visual->depth,
203                                                  ZPixmap, NULL, x_shm_info, width, height);
204               if (private->ximage == NULL)
205                 {
206                   g_warning ("XShmCreateImage failed");
207                   
208                   g_free (image);
209                   gdk_use_xshm = False;
210                   return NULL;
211                 }
212
213               x_shm_info->shmid = shmget (IPC_PRIVATE,
214                                           private->ximage->bytes_per_line * private->ximage->height,
215                                           IPC_CREAT | 0777);
216
217               if (x_shm_info->shmid == -1)
218                 {
219                   g_warning ("shmget failed!");
220
221                   XDestroyImage (private->ximage);
222                   g_free (private->x_shm_info);
223                   g_free (image);
224
225                   gdk_use_xshm = False;
226                   return NULL;
227                 }
228
229               x_shm_info->readOnly = False;
230               x_shm_info->shmaddr = shmat (x_shm_info->shmid, 0, 0);
231               private->ximage->data = x_shm_info->shmaddr;
232
233               if (x_shm_info->shmaddr == (char*) -1)
234                 {
235                   g_warning ("shmat failed!");
236
237                   XDestroyImage (private->ximage);
238                   shmctl (x_shm_info->shmid, IPC_RMID, 0);
239                   
240                   g_free (private->x_shm_info);
241                   g_free (image);
242
243                   return NULL;
244                 }
245
246               gdk_error_code = 0;
247               gdk_error_warnings = 0;
248
249               XShmAttach (private->xdisplay, x_shm_info);
250               XSync (private->xdisplay, False);
251
252               gdk_error_warnings = 1;
253               if (gdk_error_code == -1)
254                 {
255                   /* this is the common failure case so omit warning */
256                   XDestroyImage (private->ximage);
257                   shmdt (x_shm_info->shmaddr);
258                   shmctl (x_shm_info->shmid, IPC_RMID, 0);
259                   
260                   g_free (private->x_shm_info);
261                   g_free (image);
262
263                   gdk_use_xshm = False;
264                   return NULL;
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, 0);                
274
275               if (image)
276                 image_list = g_list_prepend (image_list, image);
277             }
278           else
279             {
280               g_free (image);
281               return NULL;
282             }
283           break;
284 #else /* USE_SHM */
285           g_free (image);
286           return NULL;
287 #endif /* USE_SHM */
288         case GDK_IMAGE_NORMAL:
289           private->image_put = gdk_image_put_normal;
290
291           private->ximage = XCreateImage (private->xdisplay, xvisual, visual->depth,
292                                           ZPixmap, 0, 0, width, height, 32, 0);
293
294           /* Use malloc, not g_malloc here, because X will call free()
295            * on this data
296            */
297           private->ximage->data = malloc (private->ximage->bytes_per_line *
298                                           private->ximage->height);
299           break;
300
301         case GDK_IMAGE_FASTEST:
302           g_assert_not_reached ();
303         }
304
305       if (image)
306         {
307           image->byte_order = private->ximage->byte_order;
308           image->mem = private->ximage->data;
309           image->bpl = private->ximage->bytes_per_line;
310
311           switch (private->ximage->bits_per_pixel)
312             {
313             case 8:
314               image->bpp = 1;
315               break;
316             case 16:
317               image->bpp = 2;
318               break;
319             case 24:
320               image->bpp = 3;
321               break;
322             case 32:
323               image->bpp = 4;
324               break;
325             }
326         }
327     }
328
329   return image;
330 }
331
332 GdkImage*
333 gdk_image_get (GdkWindow *window,
334                gint       x,
335                gint       y,
336                gint       width,
337                gint       height)
338 {
339   GdkImage *image;
340   GdkImagePrivate *private;
341   GdkWindowPrivate *win_private;
342
343   g_return_val_if_fail (window != NULL, NULL);
344
345   win_private = (GdkWindowPrivate *) window;
346   if (win_private->destroyed)
347     return NULL;
348
349   private = g_new (GdkImagePrivate, 1);
350   image = (GdkImage*) private;
351
352   private->xdisplay = gdk_display;
353   private->image_put = gdk_image_put_normal;
354   private->ximage = XGetImage (private->xdisplay,
355                                win_private->xwindow,
356                                x, y, width, height,
357                                AllPlanes, ZPixmap);
358
359   image->type = GDK_IMAGE_NORMAL;
360   image->visual = gdk_window_get_visual (window);
361   image->width = width;
362   image->height = height;
363   image->depth = private->ximage->depth;
364
365   image->mem = private->ximage->data;
366   image->bpl = private->ximage->bytes_per_line;
367   image->bpp = private->ximage->bits_per_pixel;
368   image->byte_order = private->ximage->byte_order;
369
370   return image;
371 }
372
373 guint32
374 gdk_image_get_pixel (GdkImage *image,
375                      gint x,
376                      gint y)
377 {
378   guint32 pixel;
379   GdkImagePrivate *private;
380
381   g_return_val_if_fail (image != NULL, 0);
382
383   private = (GdkImagePrivate *) image;
384
385   pixel = XGetPixel (private->ximage, x, y);
386
387   return pixel;
388 }
389
390 void
391 gdk_image_put_pixel (GdkImage *image,
392                      gint x,
393                      gint y,
394                      guint32 pixel)
395 {
396   GdkImagePrivate *private;
397
398   g_return_if_fail (image != NULL);
399
400   private = (GdkImagePrivate *) image;
401
402   pixel = XPutPixel (private->ximage, x, y, pixel);
403 }
404
405 void
406 gdk_image_destroy (GdkImage *image)
407 {
408   GdkImagePrivate *private;
409 #ifdef USE_SHM
410   XShmSegmentInfo *x_shm_info;
411 #endif /* USE_SHM */
412
413   g_return_if_fail (image != NULL);
414
415   private = (GdkImagePrivate*) image;
416   switch (image->type)
417     {
418     case GDK_IMAGE_NORMAL:
419       XDestroyImage (private->ximage);
420       break;
421
422     case GDK_IMAGE_SHARED:
423 #ifdef USE_SHM
424       gdk_flush();
425
426       XShmDetach (private->xdisplay, private->x_shm_info);
427       XDestroyImage (private->ximage);
428
429       x_shm_info = private->x_shm_info;
430       shmdt (x_shm_info->shmaddr);
431       
432       g_free (private->x_shm_info);
433
434       image_list = g_list_remove (image_list, image);
435 #else /* USE_SHM */
436       g_error ("trying to destroy shared memory image when gdk was compiled without shared memory support");
437 #endif /* USE_SHM */
438       break;
439
440     case GDK_IMAGE_FASTEST:
441       g_assert_not_reached ();
442     }
443
444   g_free (image);
445 }
446
447 static void
448 gdk_image_put_normal (GdkDrawable *drawable,
449                       GdkGC       *gc,
450                       GdkImage    *image,
451                       gint         xsrc,
452                       gint         ysrc,
453                       gint         xdest,
454                       gint         ydest,
455                       gint         width,
456                       gint         height)
457 {
458   GdkWindowPrivate *drawable_private;
459   GdkImagePrivate *image_private;
460   GdkGCPrivate *gc_private;
461
462   g_return_if_fail (drawable != NULL);
463   g_return_if_fail (image != NULL);
464   g_return_if_fail (gc != NULL);
465
466   drawable_private = (GdkWindowPrivate*) drawable;
467   if (drawable_private->destroyed)
468     return;
469   image_private = (GdkImagePrivate*) image;
470   gc_private = (GdkGCPrivate*) gc;
471
472   g_return_if_fail (image->type == GDK_IMAGE_NORMAL);
473
474   XPutImage (drawable_private->xdisplay, drawable_private->xwindow,
475              gc_private->xgc, image_private->ximage,
476              xsrc, ysrc, xdest, ydest, width, height);
477 }
478
479 static void
480 gdk_image_put_shared (GdkDrawable *drawable,
481                       GdkGC       *gc,
482                       GdkImage    *image,
483                       gint         xsrc,
484                       gint         ysrc,
485                       gint         xdest,
486                       gint         ydest,
487                       gint         width,
488                       gint         height)
489 {
490 #ifdef USE_SHM
491   GdkWindowPrivate *drawable_private;
492   GdkImagePrivate *image_private;
493   GdkGCPrivate *gc_private;
494
495   g_return_if_fail (drawable != NULL);
496   g_return_if_fail (image != NULL);
497   g_return_if_fail (gc != NULL);
498
499   drawable_private = (GdkWindowPrivate*) drawable;
500   if (drawable_private->destroyed)
501     return;
502   image_private = (GdkImagePrivate*) image;
503   gc_private = (GdkGCPrivate*) gc;
504
505   g_return_if_fail (image->type == GDK_IMAGE_SHARED);
506
507   XShmPutImage (drawable_private->xdisplay, drawable_private->xwindow,
508                 gc_private->xgc, image_private->ximage,
509                 xsrc, ysrc, xdest, ydest, width, height, False);
510 #else /* USE_SHM */
511   g_error ("trying to draw shared memory image when gdk was compiled without shared memory support");
512 #endif /* USE_SHM */
513 }