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