2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_X11
25 #include "SDL_x11video.h"
26 #include "SDL_x11framebuffer.h"
29 #ifndef NO_SHARED_MEMORY
31 /* Shared memory error handler routine */
33 static int (*X_handler)(Display *, XErrorEvent *) = NULL;
34 static int shm_errhandler(Display *d, XErrorEvent *e)
36 if ( e->error_code == BadAccess ) {
40 return(X_handler(d,e));
43 static SDL_bool have_mitshm(Display *dpy)
45 /* Only use shared memory on local X servers */
46 return X11_XShmQueryExtension(dpy) ? SDL_X11_HAVE_SHM : SDL_FALSE;
49 #endif /* !NO_SHARED_MEMORY */
52 X11_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format,
53 void ** pixels, int *pitch)
55 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
56 Display *display = data->videodata->display;
60 /* Free the old framebuffer surface */
61 X11_DestroyWindowFramebuffer(_this, window);
63 /* Create the graphics context for drawing */
64 gcv.graphics_exposures = False;
65 data->gc = X11_XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
67 return SDL_SetError("Couldn't create graphics context");
70 /* Find out the pixel format and depth */
71 if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
72 return SDL_SetError("Couldn't get window visual information");
75 *format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
76 if (*format == SDL_PIXELFORMAT_UNKNOWN) {
77 return SDL_SetError("Unknown window pixel format");
81 *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
83 /* Create the actual image */
84 #ifndef NO_SHARED_MEMORY
85 if (have_mitshm(display)) {
86 XShmSegmentInfo *shminfo = &data->shminfo;
88 shminfo->shmid = shmget(IPC_PRIVATE, window->h*(*pitch), IPC_CREAT | 0777);
89 if ( shminfo->shmid >= 0 ) {
90 shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
91 shminfo->readOnly = False;
92 if ( shminfo->shmaddr != (char *)-1 ) {
94 X_handler = X11_XSetErrorHandler(shm_errhandler);
95 X11_XShmAttach(display, shminfo);
96 X11_XSync(display, False);
97 X11_XSetErrorHandler(X_handler);
99 shmdt(shminfo->shmaddr);
103 shmctl(shminfo->shmid, IPC_RMID, NULL);
108 data->ximage = X11_XShmCreateImage(display, data->visual,
109 vinfo.depth, ZPixmap,
110 shminfo->shmaddr, shminfo,
111 window->w, window->h);
113 X11_XShmDetach(display, shminfo);
114 X11_XSync(display, False);
115 shmdt(shminfo->shmaddr);
118 data->use_mitshm = SDL_TRUE;
119 *pixels = shminfo->shmaddr;
124 #endif /* not NO_SHARED_MEMORY */
126 *pixels = SDL_malloc(window->h*(*pitch));
127 if (*pixels == NULL) {
128 return SDL_OutOfMemory();
131 data->ximage = X11_XCreateImage(display, data->visual,
132 vinfo.depth, ZPixmap, 0, (char *)(*pixels),
133 window->w, window->h, 32, 0);
136 return SDL_SetError("Couldn't create XImage");
142 X11_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects,
145 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
146 Display *display = data->videodata->display;
149 #ifndef NO_SHARED_MEMORY
150 if (data->use_mitshm) {
151 for (i = 0; i < numrects; ++i) {
157 if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
171 if (x + w > window->w)
173 if (y + h > window->h)
176 X11_XShmPutImage(display, data->xwindow, data->gc, data->ximage,
177 x, y, x, y, w, h, False);
181 #endif /* !NO_SHARED_MEMORY */
183 for (i = 0; i < numrects; ++i) {
189 if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
203 if (x + w > window->w)
205 if (y + h > window->h)
208 X11_XPutImage(display, data->xwindow, data->gc, data->ximage,
213 X11_XSync(display, False);
219 X11_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
221 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
225 /* The window wasn't fully initialized */
229 display = data->videodata->display;
232 XDestroyImage(data->ximage);
234 #ifndef NO_SHARED_MEMORY
235 if (data->use_mitshm) {
236 X11_XShmDetach(display, &data->shminfo);
237 X11_XSync(display, False);
238 shmdt(data->shminfo.shmaddr);
239 data->use_mitshm = SDL_FALSE;
241 #endif /* !NO_SHARED_MEMORY */
246 X11_XFreeGC(display, data->gc);
251 #endif /* SDL_VIDEO_DRIVER_X11 */
253 /* vi: set ts=4 sw=4 expandtab: */