/project/jni/sdl-1.3/src/video/x11/SDL_x11video.c

https://github.com/aichunyu/FFPlayer · C · 399 lines · 298 code · 50 blank · 51 comment · 45 complexity · eb6544ecba998f5ab854310c0cbc12f1 MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_config.h"
  19. #if SDL_VIDEO_DRIVER_X11
  20. #include <unistd.h> /* For getpid() and readlink() */
  21. #include "SDL_video.h"
  22. #include "SDL_mouse.h"
  23. #include "../SDL_sysvideo.h"
  24. #include "../SDL_pixels_c.h"
  25. #include "SDL_x11video.h"
  26. #include "SDL_x11framebuffer.h"
  27. #include "SDL_x11shape.h"
  28. #include "SDL_x11touch.h"
  29. #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  30. #include "SDL_x11opengles.h"
  31. #endif
  32. /* Initialization/Query functions */
  33. static int X11_VideoInit(_THIS);
  34. static void X11_VideoQuit(_THIS);
  35. /* Find out what class name we should use */
  36. static char *
  37. get_classname()
  38. {
  39. char *spot;
  40. #if defined(__LINUX__) || defined(__FREEBSD__)
  41. char procfile[1024];
  42. char linkfile[1024];
  43. int linksize;
  44. #endif
  45. /* First allow environment variable override */
  46. spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
  47. if (spot) {
  48. return SDL_strdup(spot);
  49. }
  50. /* Next look at the application's executable name */
  51. #if defined(__LINUX__) || defined(__FREEBSD__)
  52. #if defined(__LINUX__)
  53. SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
  54. #elif defined(__FREEBSD__)
  55. SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
  56. getpid());
  57. #else
  58. #error Where can we find the executable name?
  59. #endif
  60. linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
  61. if (linksize > 0) {
  62. linkfile[linksize] = '\0';
  63. spot = SDL_strrchr(linkfile, '/');
  64. if (spot) {
  65. return SDL_strdup(spot + 1);
  66. } else {
  67. return SDL_strdup(linkfile);
  68. }
  69. }
  70. #endif /* __LINUX__ || __FREEBSD__ */
  71. /* Finally use the default we've used forever */
  72. return SDL_strdup("SDL_App");
  73. }
  74. /* X11 driver bootstrap functions */
  75. static int
  76. X11_Available(void)
  77. {
  78. Display *display = NULL;
  79. if (SDL_X11_LoadSymbols()) {
  80. display = XOpenDisplay(NULL);
  81. if (display != NULL) {
  82. XCloseDisplay(display);
  83. }
  84. SDL_X11_UnloadSymbols();
  85. }
  86. return (display != NULL);
  87. }
  88. static void
  89. X11_DeleteDevice(SDL_VideoDevice * device)
  90. {
  91. SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
  92. if (data->display) {
  93. XCloseDisplay(data->display);
  94. }
  95. SDL_free(data->windowlist);
  96. SDL_free(device->driverdata);
  97. #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  98. SDL_free(device->gles_data);
  99. #endif
  100. SDL_free(device);
  101. SDL_X11_UnloadSymbols();
  102. }
  103. static SDL_VideoDevice *
  104. X11_CreateDevice(int devindex)
  105. {
  106. SDL_VideoDevice *device;
  107. SDL_VideoData *data;
  108. const char *display = NULL; /* Use the DISPLAY environment variable */
  109. if (!SDL_X11_LoadSymbols()) {
  110. return NULL;
  111. }
  112. /* Initialize all variables that we clean on shutdown */
  113. device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
  114. if (!device) {
  115. SDL_OutOfMemory();
  116. return NULL;
  117. }
  118. data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
  119. if (!data) {
  120. SDL_OutOfMemory();
  121. SDL_free(device);
  122. return NULL;
  123. }
  124. device->driverdata = data;
  125. #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  126. device->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
  127. if (!device->gles_data) {
  128. SDL_OutOfMemory();
  129. return NULL;
  130. }
  131. #endif
  132. /* FIXME: Do we need this?
  133. if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
  134. (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
  135. local_X11 = 1;
  136. } else {
  137. local_X11 = 0;
  138. }
  139. */
  140. data->display = XOpenDisplay(display);
  141. #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
  142. /* On Tru64 if linking without -lX11, it fails and you get following message.
  143. * Xlib: connection to ":0.0" refused by server
  144. * Xlib: XDM authorization key matches an existing client!
  145. *
  146. * It succeeds if retrying 1 second later
  147. * or if running xhost +localhost on shell.
  148. */
  149. if (data->display == NULL) {
  150. SDL_Delay(1000);
  151. data->display = XOpenDisplay(display);
  152. }
  153. #endif
  154. if (data->display == NULL) {
  155. SDL_free(device);
  156. SDL_SetError("Couldn't open X11 display");
  157. return NULL;
  158. }
  159. #ifdef X11_DEBUG
  160. XSynchronize(data->display, True);
  161. #endif
  162. /* Set the function pointers */
  163. device->VideoInit = X11_VideoInit;
  164. device->VideoQuit = X11_VideoQuit;
  165. device->GetDisplayModes = X11_GetDisplayModes;
  166. device->SetDisplayMode = X11_SetDisplayMode;
  167. device->SuspendScreenSaver = X11_SuspendScreenSaver;
  168. device->PumpEvents = X11_PumpEvents;
  169. device->CreateWindow = X11_CreateWindow;
  170. device->CreateWindowFrom = X11_CreateWindowFrom;
  171. device->SetWindowTitle = X11_SetWindowTitle;
  172. device->SetWindowIcon = X11_SetWindowIcon;
  173. device->SetWindowPosition = X11_SetWindowPosition;
  174. device->SetWindowSize = X11_SetWindowSize;
  175. device->ShowWindow = X11_ShowWindow;
  176. device->HideWindow = X11_HideWindow;
  177. device->RaiseWindow = X11_RaiseWindow;
  178. device->MaximizeWindow = X11_MaximizeWindow;
  179. device->MinimizeWindow = X11_MinimizeWindow;
  180. device->RestoreWindow = X11_RestoreWindow;
  181. device->SetWindowFullscreen = X11_SetWindowFullscreen;
  182. device->SetWindowGammaRamp = X11_SetWindowGammaRamp;
  183. device->SetWindowGrab = X11_SetWindowGrab;
  184. device->DestroyWindow = X11_DestroyWindow;
  185. device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
  186. device->UpdateWindowFramebuffer = X11_UpdateWindowFramebuffer;
  187. device->DestroyWindowFramebuffer = X11_DestroyWindowFramebuffer;
  188. device->GetWindowWMInfo = X11_GetWindowWMInfo;
  189. device->shape_driver.CreateShaper = X11_CreateShaper;
  190. device->shape_driver.SetWindowShape = X11_SetWindowShape;
  191. device->shape_driver.ResizeWindowShape = X11_ResizeWindowShape;
  192. #if SDL_VIDEO_OPENGL_GLX
  193. device->GL_LoadLibrary = X11_GL_LoadLibrary;
  194. device->GL_GetProcAddress = X11_GL_GetProcAddress;
  195. device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
  196. device->GL_CreateContext = X11_GL_CreateContext;
  197. device->GL_MakeCurrent = X11_GL_MakeCurrent;
  198. device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
  199. device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
  200. device->GL_SwapWindow = X11_GL_SwapWindow;
  201. device->GL_DeleteContext = X11_GL_DeleteContext;
  202. #endif
  203. #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
  204. device->GL_LoadLibrary = X11_GLES_LoadLibrary;
  205. device->GL_GetProcAddress = X11_GLES_GetProcAddress;
  206. device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
  207. device->GL_CreateContext = X11_GLES_CreateContext;
  208. device->GL_MakeCurrent = X11_GLES_MakeCurrent;
  209. device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
  210. device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
  211. device->GL_SwapWindow = X11_GLES_SwapWindow;
  212. device->GL_DeleteContext = X11_GLES_DeleteContext;
  213. #endif
  214. device->SetClipboardText = X11_SetClipboardText;
  215. device->GetClipboardText = X11_GetClipboardText;
  216. device->HasClipboardText = X11_HasClipboardText;
  217. device->free = X11_DeleteDevice;
  218. return device;
  219. }
  220. VideoBootStrap X11_bootstrap = {
  221. "x11", "SDL X11 video driver",
  222. X11_Available, X11_CreateDevice
  223. };
  224. static int (*handler) (Display *, XErrorEvent *) = NULL;
  225. static int
  226. X11_CheckWindowManagerErrorHandler(Display * d, XErrorEvent * e)
  227. {
  228. if (e->error_code == BadWindow) {
  229. return (0);
  230. } else {
  231. return (handler(d, e));
  232. }
  233. }
  234. static void
  235. X11_CheckWindowManager(_THIS)
  236. {
  237. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  238. Display *display = data->display;
  239. Atom _NET_SUPPORTING_WM_CHECK;
  240. int status, real_format;
  241. Atom real_type;
  242. unsigned long items_read, items_left;
  243. unsigned char *propdata;
  244. Window wm_window = 0;
  245. #ifdef DEBUG_WINDOW_MANAGER
  246. char *wm_name;
  247. #endif
  248. /* Set up a handler to gracefully catch errors */
  249. XSync(display, False);
  250. handler = XSetErrorHandler(X11_CheckWindowManagerErrorHandler);
  251. _NET_SUPPORTING_WM_CHECK = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
  252. status = XGetWindowProperty(display, DefaultRootWindow(display), _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
  253. if (status == Success && items_read) {
  254. wm_window = ((Window*)propdata)[0];
  255. }
  256. if (propdata) {
  257. XFree(propdata);
  258. }
  259. if (wm_window) {
  260. status = XGetWindowProperty(display, wm_window, _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
  261. if (status != Success || !items_read || wm_window != ((Window*)propdata)[0]) {
  262. wm_window = None;
  263. }
  264. if (propdata) {
  265. XFree(propdata);
  266. }
  267. }
  268. /* Reset the error handler, we're done checking */
  269. XSync(display, False);
  270. XSetErrorHandler(handler);
  271. if (!wm_window) {
  272. #ifdef DEBUG_WINDOW_MANAGER
  273. printf("Couldn't get _NET_SUPPORTING_WM_CHECK property\n");
  274. #endif
  275. return;
  276. }
  277. data->net_wm = SDL_TRUE;
  278. #ifdef DEBUG_WINDOW_MANAGER
  279. wm_name = X11_GetWindowTitle(_this, wm_window);
  280. printf("Window manager: %s\n", wm_name);
  281. SDL_free(wm_name);
  282. #endif
  283. }
  284. int
  285. X11_VideoInit(_THIS)
  286. {
  287. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  288. /* Get the window class name, usually the name of the application */
  289. data->classname = get_classname();
  290. /* Get the process PID to be associated to the window */
  291. data->pid = getpid();
  292. /* Open a connection to the X input manager */
  293. #ifdef X_HAVE_UTF8_STRING
  294. if (SDL_X11_HAVE_UTF8) {
  295. data->im =
  296. XOpenIM(data->display, NULL, data->classname, data->classname);
  297. }
  298. #endif
  299. /* Look up some useful Atoms */
  300. #define GET_ATOM(X) data->X = XInternAtom(data->display, #X, False)
  301. GET_ATOM(WM_DELETE_WINDOW);
  302. GET_ATOM(_NET_WM_STATE);
  303. GET_ATOM(_NET_WM_STATE_HIDDEN);
  304. GET_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
  305. GET_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
  306. GET_ATOM(_NET_WM_STATE_FULLSCREEN);
  307. GET_ATOM(_NET_WM_NAME);
  308. GET_ATOM(_NET_WM_ICON_NAME);
  309. GET_ATOM(_NET_WM_ICON);
  310. GET_ATOM(UTF8_STRING);
  311. /* Detect the window manager */
  312. X11_CheckWindowManager(_this);
  313. if (X11_InitModes(_this) < 0) {
  314. return -1;
  315. }
  316. if (X11_InitKeyboard(_this) != 0) {
  317. return -1;
  318. }
  319. X11_InitMouse(_this);
  320. X11_InitTouch(_this);
  321. return 0;
  322. }
  323. void
  324. X11_VideoQuit(_THIS)
  325. {
  326. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  327. if (data->classname) {
  328. SDL_free(data->classname);
  329. }
  330. #ifdef X_HAVE_UTF8_STRING
  331. if (data->im) {
  332. XCloseIM(data->im);
  333. }
  334. #endif
  335. X11_QuitModes(_this);
  336. X11_QuitKeyboard(_this);
  337. X11_QuitMouse(_this);
  338. X11_QuitTouch(_this);
  339. }
  340. SDL_bool
  341. X11_UseDirectColorVisuals(void)
  342. {
  343. return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
  344. }
  345. #endif /* SDL_VIDEO_DRIVER_X11 */
  346. /* vim: set ts=4 sw=4 expandtab: */