Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
executable file
·
262 lines (227 loc) · 7.43 KB

SDL_x11framebuffer.c

File metadata and controls

executable file
·
262 lines (227 loc) · 7.43 KB
 
Oct 12, 2011
Oct 12, 2011
1
2
/*
Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
Oct 12, 2011
Oct 12, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#if SDL_VIDEO_DRIVER_X11
#include "SDL_x11video.h"
#include "SDL_x11framebuffer.h"
#ifndef NO_SHARED_MEMORY
/* Shared memory error handler routine */
static int shm_error;
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
static int shm_errhandler(Display *d, XErrorEvent *e)
{
if ( e->error_code == BadAccess ) {
shm_error = True;
return(0);
} else
return(X_handler(d,e));
}
static SDL_bool have_mitshm(void)
{
/* Only use shared memory on local X servers */
if ( (SDL_strncmp(XDisplayName(NULL), ":", 1) == 0) ||
(SDL_strncmp(XDisplayName(NULL), "unix:", 5) == 0) ) {
return SDL_X11_HAVE_SHM;
}
return SDL_FALSE;
}
#endif /* !NO_SHARED_MEMORY */
int
X11_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format,
void ** pixels, int *pitch)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
XGCValues gcv;
XVisualInfo vinfo;
/* Free the old framebuffer surface */
X11_DestroyWindowFramebuffer(_this, window);
/* Create the graphics context for drawing */
gcv.graphics_exposures = False;
data->gc = XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
if (!data->gc) {
SDL_SetError("Couldn't create graphics context");
return -1;
}
/* Find out the pixel format and depth */
if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
SDL_SetError("Couldn't get window visual information");
return -1;
}
*format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
if (*format == SDL_PIXELFORMAT_UNKNOWN) {
SDL_SetError("Unknown window pixel format");
return -1;
}
/* Calculate pitch */
*pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
/* Create the actual image */
#ifndef NO_SHARED_MEMORY
if (have_mitshm()) {
XShmSegmentInfo *shminfo = &data->shminfo;
shminfo->shmid = shmget(IPC_PRIVATE, window->h*(*pitch), IPC_CREAT | 0777);
if ( shminfo->shmid >= 0 ) {
shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
shminfo->readOnly = False;
if ( shminfo->shmaddr != (char *)-1 ) {
shm_error = False;
X_handler = XSetErrorHandler(shm_errhandler);
XShmAttach(display, shminfo);
XSync(display, True);
XSetErrorHandler(X_handler);
if ( shm_error )
shmdt(shminfo->shmaddr);
} else {
shm_error = True;
}
shmctl(shminfo->shmid, IPC_RMID, NULL);
} else {
shm_error = True;
}
if (!shm_error) {
data->ximage = XShmCreateImage(display, data->visual,
vinfo.depth, ZPixmap,
shminfo->shmaddr, shminfo,
window->w, window->h);
if (!data->ximage) {
XShmDetach(display, shminfo);
XSync(display, False);
shmdt(shminfo->shmaddr);
} else {
/* Done! */
data->use_mitshm = SDL_TRUE;
*pixels = shminfo->shmaddr;
return 0;
}
}
}
#endif /* not NO_SHARED_MEMORY */
*pixels = SDL_malloc(window->h*(*pitch));
if (*pixels == NULL) {
SDL_OutOfMemory();
return -1;
}
data->ximage = XCreateImage(display, data->visual,
vinfo.depth, ZPixmap, 0, (char *)(*pixels),
window->w, window->h, 32, 0);
if (!data->ximage) {
SDL_free(*pixels);
SDL_SetError("Couldn't create XImage");
return -1;
}
return 0;
}
int
X11_UpdateWindowFramebuffer(_THIS, SDL_Window * window, SDL_Rect * rects,
int numrects)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Display *display = data->videodata->display;
int i;
Jul 5, 2012
Jul 5, 2012
157
int x, y, w ,h;
Oct 12, 2011
Oct 12, 2011
158
159
160
#ifndef NO_SHARED_MEMORY
if (data->use_mitshm) {
for (i = 0; i < numrects; ++i) {
Jul 5, 2012
Jul 5, 2012
161
162
163
164
x = rects[i].x;
y = rects[i].y;
w = rects[i].w;
h = rects[i].h;
Oct 12, 2011
Oct 12, 2011
165
Jul 5, 2012
Jul 5, 2012
166
167
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
/* Clipped? */
Oct 12, 2011
Oct 12, 2011
168
169
continue;
}
Jul 5, 2012
Jul 5, 2012
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
if (x < 0)
{
x += w;
w += rects[i].x;
}
if (y < 0)
{
y += h;
h += rects[i].y;
}
if (x + w > window->w)
w = window->w - x;
if (y + h > window->h)
h = window->h - y;
Oct 12, 2011
Oct 12, 2011
185
XShmPutImage(display, data->xwindow, data->gc, data->ximage,
Jul 5, 2012
Jul 5, 2012
186
x, y, x, y, w, h, False);
Oct 12, 2011
Oct 12, 2011
187
188
189
190
191
192
}
}
else
#endif /* !NO_SHARED_MEMORY */
{
for (i = 0; i < numrects; ++i) {
Jul 5, 2012
Jul 5, 2012
193
194
195
196
x = rects[i].x;
y = rects[i].y;
w = rects[i].w;
h = rects[i].h;
Oct 12, 2011
Oct 12, 2011
197
Jul 5, 2012
Jul 5, 2012
198
199
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
/* Clipped? */
Oct 12, 2011
Oct 12, 2011
200
201
continue;
}
Jul 5, 2012
Jul 5, 2012
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if (x < 0)
{
x += w;
w += rects[i].x;
}
if (y < 0)
{
y += h;
h += rects[i].y;
}
if (x + w > window->w)
w = window->w - x;
if (y + h > window->h)
h = window->h - y;
Oct 12, 2011
Oct 12, 2011
217
XPutImage(display, data->xwindow, data->gc, data->ximage,
Jul 5, 2012
Jul 5, 2012
218
x, y, x, y, w, h);
Oct 12, 2011
Oct 12, 2011
219
220
}
}
Oct 13, 2011
Oct 13, 2011
221
Oct 12, 2011
Oct 12, 2011
222
XSync(display, False);
Oct 13, 2011
Oct 13, 2011
223
224
return 0;
Oct 12, 2011
Oct 12, 2011
225
226
227
228
229
230
}
void
X11_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
Jan 8, 2012
Jan 8, 2012
231
232
233
234
235
236
237
238
Display *display;
if (!data) {
/* The window wasn't fully initialized */
return;
}
display = data->videodata->display;
Oct 12, 2011
Oct 12, 2011
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
if (data->ximage) {
XDestroyImage(data->ximage);
#ifndef NO_SHARED_MEMORY
if (data->use_mitshm) {
XShmDetach(display, &data->shminfo);
XSync(display, False);
shmdt(data->shminfo.shmaddr);
data->use_mitshm = SDL_FALSE;
}
#endif /* !NO_SHARED_MEMORY */
data->ximage = NULL;
}
if (data->gc) {
XFreeGC(display, data->gc);
data->gc = NULL;
}
}
#endif /* SDL_VIDEO_DRIVER_X11 */
/* vi: set ts=4 sw=4 expandtab: */