1 /* xutil.c - utility functions for X
3 * Raster graphics library
5 * Copyright (c) 1997 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <X11/Xutil.h>
45 static int (*oldErrorHandler
)();
48 errorHandler(Display
*dpy
, XErrorEvent
*err
)
51 if(err
->error_code
!=BadAccess
)
52 (*oldErrorHandler
)(dpy
, err
);
62 RCreateXImage(RContext
*context
, int depth
, unsigned width
, unsigned height
)
65 Visual
*visual
= context
->visual
;
67 rximg
= malloc(sizeof(RXImage
));
69 RErrorCode
= RERR_NOMEMORY
;
74 rximg
->image
= XCreateImage(context
->dpy
, visual
, depth
,
75 ZPixmap
, 0, NULL
, width
, height
, 8, 0);
78 RErrorCode
= RERR_XERROR
;
81 rximg
->image
->data
= malloc(rximg
->image
->bytes_per_line
*height
);
82 if (!rximg
->image
->data
) {
83 XDestroyImage(rximg
->image
);
85 RErrorCode
= RERR_NOMEMORY
;
90 if (!context
->attribs
->use_shared_memory
) {
93 rximg
->image
= XCreateImage(context
->dpy
, visual
, depth
,
94 ZPixmap
, 0, NULL
, width
, height
, 8, 0);
97 RErrorCode
= RERR_XERROR
;
100 rximg
->image
->data
= malloc(rximg
->image
->bytes_per_line
*height
);
101 if (!rximg
->image
->data
) {
102 XDestroyImage(rximg
->image
);
104 RErrorCode
= RERR_NOMEMORY
;
108 rximg
->is_shared
= 1;
110 rximg
->info
.readOnly
= False
;
112 rximg
->image
= XShmCreateImage(context
->dpy
, visual
, depth
,
113 ZPixmap
, NULL
, &rximg
->info
, width
,
116 rximg
->info
.shmid
= shmget(IPC_PRIVATE
,
117 rximg
->image
->bytes_per_line
*height
,
119 if (rximg
->info
.shmid
< 0) {
120 context
->attribs
->use_shared_memory
= 0;
121 perror("wrlib:could not allocate shared memory segment");
122 XDestroyImage(rximg
->image
);
123 goto retry_without_shm
;
126 rximg
->info
.shmaddr
= shmat(rximg
->info
.shmid
, 0, 0);
127 if ((int)rximg
->info
.shmaddr
< 0) {
128 context
->attribs
->use_shared_memory
= 0;
129 if (shmctl(rximg
->info
.shmid
, IPC_RMID
, 0) < 0)
130 perror("wrlib:shmctl");
131 perror("wrlib:could not allocate shared memory");
132 XDestroyImage(rximg
->image
);
133 goto retry_without_shm
;
137 XSync(context
->dpy
, False
);
138 oldErrorHandler
= XSetErrorHandler(errorHandler
);
139 XShmAttach(context
->dpy
, &rximg
->info
);
140 XSync(context
->dpy
, False
);
141 XSetErrorHandler(oldErrorHandler
);
143 rximg
->image
->data
= rximg
->info
.shmaddr
;
144 /* rximg->image->obdata = &(rximg->info);*/
147 context
->attribs
->use_shared_memory
= 0;
148 XDestroyImage(rximg
->image
);
149 if (shmdt(rximg
->info
.shmaddr
) < 0)
150 perror("wrlib:shmdt");
151 if (shmctl(rximg
->info
.shmid
, IPC_RMID
, 0) < 0)
152 perror("wrlib:shmctl");
153 printf("wrlib:error attaching shared memory segment to XImage\n");
154 goto retry_without_shm
;
164 RDestroyXImage(RContext
*context
, RXImage
*rximage
)
167 XDestroyImage(rximage
->image
);
170 if (rximage
->is_shared
) {
171 XShmDetach(context
->dpy
, &rximage
->info
);
172 XDestroyImage(rximage
->image
);
173 if (shmdt(rximage
->info
.shmaddr
) < 0)
174 perror("wrlib:shmdt");
175 if (shmctl(rximage
->info
.shmid
, IPC_RMID
, 0) < 0)
176 perror("wrlib:shmctl");
178 XDestroyImage(rximage
->image
);
185 RPutXImage(RContext
*context
, Drawable d
, GC gc
, RXImage
*ximage
, int src_x
,
186 int src_y
, int dest_x
, int dest_y
,
187 unsigned int width
, unsigned int height
)
190 XPutImage(context
->dpy
, d
, gc
, ximage
->image
, src_x
, src_y
, dest_x
,
191 dest_y
, width
, height
);
193 if (ximage
->is_shared
) {
194 XShmPutImage(context
->dpy
, d
, gc
, ximage
->image
, src_x
, src_y
,
195 dest_x
, dest_y
, width
, height
, False
);
197 XPutImage(context
->dpy
, d
, gc
, ximage
->image
, src_x
, src_y
, dest_x
,
198 dest_y
, width
, height
);
200 XFlush(context
->dpy
);
207 R_CreateXImageMappedPixmap(RContext
*context
, RXImage
*rximage
)
211 pix
= XShmCreatePixmap(context
->dpy
, context
->drawable
,
212 rximage
->image
->data
, &rximage
->info
,
213 rximage
->image
->width
, rximage
->image
->height
,
214 rximage
->image
->depth
);