File:
src/video/x11/SDL_x11framebuffer.c
Checker name:
clang-diagnostic-unused-parameter
Review status:
Unreviewed
/*
37
return 0;
38
}
39
return X_handler(d, e);
40
}
41
42
static SDL_bool have_mitshm(Display *dpy)
43
{
44
/* Only use shared memory on local X servers */
45
return X11_XShmQueryExtension(dpy) ? SDL_X11_HAVE_SHM : SDL_FALSE;
46
}
47
48
/* !NO_SHARED_MEMORY */
49
50
int X11_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormatEnum *format,
51
void **pixels, int *pitch)
52
{
53
SDL_WindowData *data = window->driverdata;
54
Display *display = data->videodata->display;
55
XGCValues gcv;
56
XVisualInfo vinfo;
57
int w, h;
58
59
SDL_GetWindowSizeInPixels(window, &w, &h);
60
61
/* Free the old framebuffer surface */
62
X11_DestroyWindowFramebuffer(_this, window);
63
64
/* Create the graphics context for drawing */
65
gcv.graphics_exposures = False;
66
data->gc = X11_XCreateGC(display, data->xwindow, GCGraphicsExposures, &gcv);
67
if (!data->gc) {
68
return SDL_SetError("Couldn't create graphics context");
69
}
70
71
/* Find out the pixel format and depth */
72
if (X11_GetVisualInfoFromVisual(display, data->visual, &vinfo) < 0) {
73
return SDL_SetError("Couldn't get window visual information");
74
}
75
76
*format = X11_GetPixelFormatFromVisualInfo(display, &vinfo);
77
if (*format == SDL_PIXELFORMAT_UNKNOWN) {
78
return SDL_SetError("Unknown window pixel format");
79
}
80
81
/* Calculate pitch */
82
*pitch = (((w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
83
84
/* Create the actual image */
85
86
if (have_mitshm(display)) {
87
XShmSegmentInfo *shminfo = &data->shminfo;
88
89
shminfo->shmid = shmget(IPC_PRIVATE, (size_t)h * (*pitch), IPC_CREAT | 0777);
90
if (shminfo->shmid >= 0) {
91
shminfo->shmaddr = (char *)shmat(shminfo->shmid, 0, 0);
92
shminfo->readOnly = False;
93
if (shminfo->shmaddr != (char *)-1) {
94
shm_error = False;
95
X_handler = X11_XSetErrorHandler(shm_errhandler);
96
X11_XShmAttach(display, shminfo);
97
X11_XSync(display, False);
98
X11_XSetErrorHandler(X_handler);
99
if (shm_error) {
100
shmdt(shminfo->shmaddr);
101
}
102
} else {
103
shm_error = True;
104
}
105
shmctl(shminfo->shmid, IPC_RMID, NULL);
106
} else {
107
shm_error = True;
108
}
109
if (!shm_error) {
110
data->ximage = X11_XShmCreateImage(display, data->visual,
111
vinfo.depth, ZPixmap,
112
shminfo->shmaddr, shminfo,
113
w, h);
114
if (!data->ximage) {
115
X11_XShmDetach(display, shminfo);
116
X11_XSync(display, False);
117
shmdt(shminfo->shmaddr);
118
} else {
119
/* Done! */
120
data->ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? MSBFirst : LSBFirst;
121
data->use_mitshm = SDL_TRUE;
122
*pixels = shminfo->shmaddr;
123
return 0;
124
}
125
}
126
}
127
/* not NO_SHARED_MEMORY */
128
129
*pixels = SDL_malloc((size_t)h * (*pitch));
130
if (!*pixels) {
131
return -1;
132
}
133
134
data->ximage = X11_XCreateImage(display, data->visual,
135
vinfo.depth, ZPixmap, 0, (char *)(*pixels),
136
w, h, 32, 0);
137
if (!data->ximage) {
138
SDL_free(*pixels);
139
return SDL_SetError("Couldn't create XImage");
140
}
141
data->ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? MSBFirst : LSBFirst;
142
return 0;
143
}
144
145
int X11_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects,
146
int numrects)
147
{
148
SDL_WindowData *data = window->driverdata;
149
Display *display = data->videodata->display;
150
int i;
151
int x, y, w, h;
152
int window_w, window_h;
153
154
SDL_GetWindowSizeInPixels(window, &window_w, &window_h);
155
156
157
if (data->use_mitshm) {
158
for (i = 0; i < numrects; ++i) {
159
x = rects[i].x;
160
y = rects[i].y;
161
w = rects[i].w;
162
h = rects[i].h;
163
164
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
165
/* Clipped? */
166
continue;
167
}
168
if (x < 0) {
169
x += w;
170
w += rects[i].x;
171
}
172
if (y < 0) {
173
y += h;
174
h += rects[i].y;
175
}
176
if (x + w > window_w) {
177
w = window_w - x;
178
}
179
if (y + h > window_h) {
180
h = window_h - y;
181
}
182
183
X11_XShmPutImage(display, data->xwindow, data->gc, data->ximage,
184
x, y, x, y, w, h, False);
185
}
186
} else
187
/* !NO_SHARED_MEMORY */
188
{
189
for (i = 0; i < numrects; ++i) {
190
x = rects[i].x;
191
y = rects[i].y;
192
w = rects[i].w;
193
h = rects[i].h;
194
195
if (w <= 0 || h <= 0 || (x + w) <= 0 || (y + h) <= 0) {
196
/* Clipped? */
197
continue;
198
}
199
if (x < 0) {
200
x += w;
201
w += rects[i].x;
202
}
203
if (y < 0) {
204
y += h;
205
h += rects[i].y;
206
}
207
if (x + w > window_w) {
208
w = window_w - x;
209
}
210
if (y + h > window_h) {
211
h = window_h - y;
212
}
213
214
X11_XPutImage(display, data->xwindow, data->gc, data->ximage,
215
x, y, x, y, w, h);
216
}
217
}
218
219
X11_XSync(display, False);
220
221
return 0;
222
}
223
224
void X11_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
225
{
226
SDL_WindowData *data = window->driverdata;
227
Display *display;
228
229
if (!data) {
230
/* The window wasn't fully initialized */
231
return;
232
}
233
234
display = data->videodata->display;
235
236
if (data->ximage) {
237
XDestroyImage(data->ximage);
238
239
240
if (data->use_mitshm) {
241
X11_XShmDetach(display, &data->shminfo);
242
X11_XSync(display, False);
243
shmdt(data->shminfo.shmaddr);
244
data->use_mitshm = SDL_FALSE;
245
}
246
/* !NO_SHARED_MEMORY */
247
248
data->ximage = NULL;
249
}
250
if (data->gc) {
251
X11_XFreeGC(display, data->gc);
252
data->gc = NULL;
253
}
254
}
255