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

Latest commit

 

History

History
390 lines (341 loc) · 11.3 KB

SDL_x11video.c

File metadata and controls

390 lines (341 loc) · 11.3 KB
 
Jul 26, 2006
Jul 26, 2006
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
Jul 26, 2006
Jul 26, 2006
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
Jul 12, 2010
Jul 12, 2010
24
25
#include <unistd.h> /* For getpid() and readlink() */
Jul 26, 2006
Jul 26, 2006
26
27
28
29
30
31
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "SDL_x11video.h"
Jun 30, 2010
Jun 30, 2010
32
#include "SDL_x11shape.h"
Dec 1, 2010
Dec 1, 2010
33
#include "SDL_x11touch.h"
Aug 25, 2008
Aug 25, 2008
34
Jul 30, 2009
Jul 30, 2009
35
36
37
#if SDL_VIDEO_DRIVER_PANDORA
#include "SDL_x11opengles.h"
#endif
Jul 26, 2006
Jul 26, 2006
38
39
40
41
42
/* Initialization/Query functions */
static int X11_VideoInit(_THIS);
static void X11_VideoQuit(_THIS);
Jul 27, 2006
Jul 27, 2006
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
/* Find out what class name we should use */
static char *
get_classname()
{
char *spot;
#if defined(__LINUX__) || defined(__FREEBSD__)
char procfile[1024];
char linkfile[1024];
int linksize;
#endif
/* First allow environment variable override */
spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
if (spot) {
return SDL_strdup(spot);
}
/* Next look at the application's executable name */
#if defined(__LINUX__) || defined(__FREEBSD__)
#if defined(__LINUX__)
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
#elif defined(__FREEBSD__)
SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
getpid());
#else
#error Where can we find the executable name?
#endif
linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
if (linksize > 0) {
linkfile[linksize] = '\0';
spot = SDL_strrchr(linkfile, '/');
if (spot) {
return SDL_strdup(spot + 1);
} else {
return SDL_strdup(linkfile);
}
}
#endif /* __LINUX__ || __FREEBSD__ */
/* Finally use the default we've used forever */
return SDL_strdup("SDL_App");
}
Jul 26, 2006
Jul 26, 2006
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* X11 driver bootstrap functions */
static int
X11_Available(void)
{
Display *display = NULL;
if (SDL_X11_LoadSymbols()) {
display = XOpenDisplay(NULL);
if (display != NULL) {
XCloseDisplay(display);
}
SDL_X11_UnloadSymbols();
}
return (display != NULL);
}
static void
X11_DeleteDevice(SDL_VideoDevice * device)
{
SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
if (data->display) {
XCloseDisplay(data->display);
}
Mar 7, 2008
Mar 7, 2008
109
SDL_free(data->windowlist);
Jul 26, 2006
Jul 26, 2006
110
SDL_free(device->driverdata);
Jul 30, 2009
Jul 30, 2009
111
112
113
#if SDL_VIDEO_DRIVER_PANDORA
SDL_free(device->gles_data);
#endif
Jul 26, 2006
Jul 26, 2006
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
SDL_free(device);
SDL_X11_UnloadSymbols();
}
static SDL_VideoDevice *
X11_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
SDL_VideoData *data;
const char *display = NULL; /* Use the DISPLAY environment variable */
if (!SDL_X11_LoadSymbols()) {
return NULL;
}
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
Mar 6, 2008
Mar 6, 2008
132
133
134
if (!device) {
SDL_OutOfMemory();
return NULL;
Jul 26, 2006
Jul 26, 2006
135
}
Mar 6, 2008
Mar 6, 2008
136
137
data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
if (!data) {
Jul 26, 2006
Jul 26, 2006
138
SDL_OutOfMemory();
Mar 6, 2008
Mar 6, 2008
139
SDL_free(device);
Jul 26, 2006
Jul 26, 2006
140
141
142
143
return NULL;
}
device->driverdata = data;
Jul 30, 2009
Jul 30, 2009
144
145
146
147
148
149
150
151
#if SDL_VIDEO_DRIVER_PANDORA
device->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
if (!device->gles_data) {
SDL_OutOfMemory();
return NULL;
}
#endif
Jul 26, 2006
Jul 26, 2006
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* FIXME: Do we need this?
if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
(SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
local_X11 = 1;
} else {
local_X11 = 0;
}
*/
data->display = XOpenDisplay(display);
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
/* On Tru64 if linking without -lX11, it fails and you get following message.
* Xlib: connection to ":0.0" refused by server
* Xlib: XDM authorization key matches an existing client!
*
* It succeeds if retrying 1 second later
* or if running xhost +localhost on shell.
*/
if (data->display == NULL) {
SDL_Delay(1000);
data->display = XOpenDisplay(display);
}
#endif
if (data->display == NULL) {
SDL_free(device);
SDL_SetError("Couldn't open X11 display");
return NULL;
}
#ifdef X11_DEBUG
XSynchronize(data->display, True);
#endif
/* Set the function pointers */
device->VideoInit = X11_VideoInit;
device->VideoQuit = X11_VideoQuit;
device->GetDisplayModes = X11_GetDisplayModes;
device->SetDisplayMode = X11_SetDisplayMode;
Jul 10, 2007
Jul 10, 2007
188
189
device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
Jan 12, 2009
Jan 12, 2009
190
device->SuspendScreenSaver = X11_SuspendScreenSaver;
Jul 27, 2006
Jul 27, 2006
191
device->PumpEvents = X11_PumpEvents;
Jul 26, 2006
Jul 26, 2006
192
193
194
195
device->CreateWindow = X11_CreateWindow;
device->CreateWindowFrom = X11_CreateWindowFrom;
device->SetWindowTitle = X11_SetWindowTitle;
Jan 2, 2009
Jan 2, 2009
196
device->SetWindowIcon = X11_SetWindowIcon;
Jul 26, 2006
Jul 26, 2006
197
198
199
200
201
202
203
204
205
206
207
device->SetWindowPosition = X11_SetWindowPosition;
device->SetWindowSize = X11_SetWindowSize;
device->ShowWindow = X11_ShowWindow;
device->HideWindow = X11_HideWindow;
device->RaiseWindow = X11_RaiseWindow;
device->MaximizeWindow = X11_MaximizeWindow;
device->MinimizeWindow = X11_MinimizeWindow;
device->RestoreWindow = X11_RestoreWindow;
device->SetWindowGrab = X11_SetWindowGrab;
device->DestroyWindow = X11_DestroyWindow;
device->GetWindowWMInfo = X11_GetWindowWMInfo;
Jun 30, 2010
Jun 30, 2010
208
209
210
device->shape_driver.CreateShaper = X11_CreateShaper;
device->shape_driver.SetWindowShape = X11_SetWindowShape;
device->shape_driver.ResizeWindowShape = X11_ResizeWindowShape;
Jan 25, 2011
Jan 25, 2011
211
#if SDL_VIDEO_OPENGL_GLX
Jul 26, 2006
Jul 26, 2006
212
213
device->GL_LoadLibrary = X11_GL_LoadLibrary;
device->GL_GetProcAddress = X11_GL_GetProcAddress;
Feb 9, 2009
Feb 9, 2009
214
device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
Jul 26, 2006
Jul 26, 2006
215
216
217
218
219
220
221
device->GL_CreateContext = X11_GL_CreateContext;
device->GL_MakeCurrent = X11_GL_MakeCurrent;
device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
device->GL_SwapWindow = X11_GL_SwapWindow;
device->GL_DeleteContext = X11_GL_DeleteContext;
#endif
Jul 30, 2009
Jul 30, 2009
222
223
224
225
226
227
228
229
230
231
232
#if SDL_VIDEO_DRIVER_PANDORA
device->GL_LoadLibrary = X11_GLES_LoadLibrary;
device->GL_GetProcAddress = X11_GLES_GetProcAddress;
device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
device->GL_CreateContext = X11_GLES_CreateContext;
device->GL_MakeCurrent = X11_GLES_MakeCurrent;
device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
device->GL_SwapWindow = X11_GLES_SwapWindow;
device->GL_DeleteContext = X11_GLES_DeleteContext;
#endif
Jul 26, 2006
Jul 26, 2006
233
Jul 12, 2010
Jul 12, 2010
234
235
236
237
device->SetClipboardText = X11_SetClipboardText;
device->GetClipboardText = X11_GetClipboardText;
device->HasClipboardText = X11_HasClipboardText;
Jul 26, 2006
Jul 26, 2006
238
239
240
241
242
243
244
245
246
247
device->free = X11_DeleteDevice;
return device;
}
VideoBootStrap X11_bootstrap = {
"x11", "SDL X11 video driver",
X11_Available, X11_CreateDevice
};
Jul 20, 2010
Jul 20, 2010
248
249
250
251
252
253
254
255
256
257
static int (*handler) (Display *, XErrorEvent *) = NULL;
static int
X11_CheckWindowManagerErrorHandler(Display * d, XErrorEvent * e)
{
if (e->error_code == BadWindow) {
return (0);
} else {
return (handler(d, e));
}
}
Jul 26, 2006
Jul 26, 2006
258
Jul 14, 2010
Jul 14, 2010
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
static void
X11_CheckWindowManager(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Display *display = data->display;
Atom _NET_SUPPORTING_WM_CHECK;
int status, real_format;
Atom real_type;
unsigned long items_read, items_left;
unsigned char *propdata;
Window wm_window = 0;
#ifdef DEBUG_WINDOW_MANAGER
char *wm_name;
#endif
Jul 20, 2010
Jul 20, 2010
274
275
276
277
/* Set up a handler to gracefully catch errors */
XSync(display, False);
handler = XSetErrorHandler(X11_CheckWindowManagerErrorHandler);
Jul 14, 2010
Jul 14, 2010
278
279
280
281
282
_NET_SUPPORTING_WM_CHECK = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
status = XGetWindowProperty(display, DefaultRootWindow(display), _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
if (status == Success && items_read) {
wm_window = ((Window*)propdata)[0];
}
Jul 20, 2010
Jul 20, 2010
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
if (propdata) {
XFree(propdata);
}
if (wm_window) {
status = XGetWindowProperty(display, wm_window, _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
if (status != Success || !items_read || wm_window != ((Window*)propdata)[0]) {
wm_window = None;
}
if (propdata) {
XFree(propdata);
}
}
/* Reset the error handler, we're done checking */
XSync(display, False);
XSetErrorHandler(handler);
Jul 14, 2010
Jul 14, 2010
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
if (!wm_window) {
#ifdef DEBUG_WINDOW_MANAGER
printf("Couldn't get _NET_SUPPORTING_WM_CHECK property\n");
#endif
return;
}
data->net_wm = SDL_TRUE;
#ifdef DEBUG_WINDOW_MANAGER
wm_name = X11_GetWindowTitle(_this, wm_window);
printf("Window manager: %s\n", wm_name);
SDL_free(wm_name);
#endif
}
Jul 26, 2006
Jul 26, 2006
315
316
317
318
int
X11_VideoInit(_THIS)
{
Jul 27, 2006
Jul 27, 2006
319
320
321
322
323
324
325
326
327
328
329
330
331
332
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
/* Get the window class name, usually the name of the application */
data->classname = get_classname();
/* Open a connection to the X input manager */
#ifdef X_HAVE_UTF8_STRING
if (SDL_X11_HAVE_UTF8) {
data->im =
XOpenIM(data->display, NULL, data->classname, data->classname);
}
#endif
/* Look up some useful Atoms */
Jul 14, 2010
Jul 14, 2010
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#define GET_ATOM(X) data->X = XInternAtom(data->display, #X, False)
GET_ATOM(WM_DELETE_WINDOW);
GET_ATOM(_NET_WM_STATE);
GET_ATOM(_NET_WM_STATE_HIDDEN);
GET_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
GET_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
GET_ATOM(_NET_WM_STATE_FULLSCREEN);
GET_ATOM(_NET_WM_NAME);
GET_ATOM(_NET_WM_ICON_NAME);
GET_ATOM(_NET_WM_ICON);
GET_ATOM(UTF8_STRING);
/* Detect the window manager */
X11_CheckWindowManager(_this);
Jul 27, 2006
Jul 27, 2006
347
Dec 4, 2009
Dec 4, 2009
348
349
350
if (X11_InitModes(_this) < 0) {
return -1;
}
Jul 26, 2006
Jul 26, 2006
351
Jan 8, 2008
Jan 8, 2008
352
353
354
if (X11_InitKeyboard(_this) != 0) {
return -1;
}
Jul 26, 2006
Jul 26, 2006
355
356
X11_InitMouse(_this);
May 31, 2010
May 31, 2010
357
X11_InitTouch(_this);
Jul 26, 2006
Jul 26, 2006
358
359
360
361
362
363
return 0;
}
void
X11_VideoQuit(_THIS)
{
Jul 27, 2006
Jul 27, 2006
364
365
366
367
368
369
370
371
372
373
374
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
if (data->classname) {
SDL_free(data->classname);
}
#ifdef X_HAVE_UTF8_STRING
if (data->im) {
XCloseIM(data->im);
}
#endif
Jul 26, 2006
Jul 26, 2006
375
376
377
X11_QuitModes(_this);
X11_QuitKeyboard(_this);
X11_QuitMouse(_this);
May 31, 2010
May 31, 2010
378
X11_QuitTouch(_this);
Jul 26, 2006
Jul 26, 2006
379
380
}
Jan 2, 2009
Jan 2, 2009
381
SDL_bool
Jun 26, 2010
Jun 26, 2010
382
X11_UseDirectColorVisuals(void)
Jan 2, 2009
Jan 2, 2009
383
{
Jan 10, 2009
Jan 10, 2009
384
385
386
387
/* Once we implement DirectColor colormaps and gamma ramp support...
return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
*/
return SDL_FALSE;
Jan 2, 2009
Jan 2, 2009
388
389
}
Jul 26, 2006
Jul 26, 2006
390
/* vim: set ts=4 sw=4 expandtab: */