No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x11platform.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "platform.h"
  24. #include "engine.h"
  25. #include "input.h"
  26. #include "logger.h"
  27. #include <cstring>
  28. #include <string>
  29. #include <algorithm>
  30. #include <map>
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #include <X11/Xlib.h>
  34. #include <X11/Xatom.h>
  35. #include <GL/glx.h>
  36. struct X11PlatformPrivate {
  37. Display *display;
  38. XVisualInfo *visual;
  39. GLXContext glxContext;
  40. XIM xim;
  41. XIC xic;
  42. Colormap colormap;
  43. Window window;
  44. Cursor cursor;
  45. Atom atomDeleteWindow;
  46. Atom atomClipboard;
  47. Atom atomTargets;
  48. Atom atomText;
  49. Atom atomCompoundText;
  50. Atom atomUTF8String;
  51. bool visible;
  52. bool focused;
  53. int width;
  54. int height;
  55. std::string clipboardText;
  56. std::map<int, unsigned char> keyMap;
  57. } x11;
  58. void Platform::init()
  59. {
  60. x11.display = NULL;
  61. x11.visual = NULL;
  62. x11.glxContext = NULL;
  63. x11.xim = NULL;
  64. x11.xic = NULL;
  65. x11.colormap = None;
  66. x11.window = None;
  67. x11.cursor = None;
  68. x11.visible = false;
  69. x11.focused = false;
  70. x11.width = 0;
  71. x11.height = 0;
  72. // setup keymap
  73. x11.keyMap[XK_1] = KC_1;
  74. x11.keyMap[XK_2] = KC_2;
  75. x11.keyMap[XK_3] = KC_3;
  76. x11.keyMap[XK_4] = KC_4;
  77. x11.keyMap[XK_5] = KC_5;
  78. x11.keyMap[XK_6] = KC_6;
  79. x11.keyMap[XK_7] = KC_7;
  80. x11.keyMap[XK_8] = KC_8;
  81. x11.keyMap[XK_9] = KC_9;
  82. x11.keyMap[XK_0] = KC_0;
  83. x11.keyMap[XK_BackSpace] = KC_BACK;
  84. x11.keyMap[XK_minus] = KC_MINUS;
  85. x11.keyMap[XK_equal] = KC_EQUALS;
  86. x11.keyMap[XK_space] = KC_SPACE;
  87. x11.keyMap[XK_comma] = KC_COMMA;
  88. x11.keyMap[XK_period] = KC_PERIOD;
  89. x11.keyMap[XK_backslash] = KC_BACKSLASH;
  90. x11.keyMap[XK_slash] = KC_SLASH;
  91. x11.keyMap[XK_bracketleft] = KC_LBRACKET;
  92. x11.keyMap[XK_bracketright] = KC_RBRACKET;
  93. x11.keyMap[XK_Escape] = KC_ESCAPE;
  94. x11.keyMap[XK_Caps_Lock] = KC_CAPITAL;
  95. x11.keyMap[XK_Tab] = KC_TAB;
  96. x11.keyMap[XK_Return] = KC_RETURN;
  97. x11.keyMap[XK_Control_L] = KC_LCONTROL;
  98. x11.keyMap[XK_Control_R] = KC_RCONTROL;
  99. x11.keyMap[XK_colon] = KC_COLON;
  100. x11.keyMap[XK_semicolon] = KC_SEMICOLON;
  101. x11.keyMap[XK_apostrophe] = KC_APOSTROPHE;
  102. x11.keyMap[XK_grave] = KC_GRAVE;
  103. x11.keyMap[XK_b] = KC_B;
  104. x11.keyMap[XK_a] = KC_A;
  105. x11.keyMap[XK_c] = KC_C;
  106. x11.keyMap[XK_d] = KC_D;
  107. x11.keyMap[XK_e] = KC_E;
  108. x11.keyMap[XK_f] = KC_F;
  109. x11.keyMap[XK_g] = KC_G;
  110. x11.keyMap[XK_h] = KC_H;
  111. x11.keyMap[XK_i] = KC_I;
  112. x11.keyMap[XK_j] = KC_J;
  113. x11.keyMap[XK_k] = KC_K;
  114. x11.keyMap[XK_l] = KC_L;
  115. x11.keyMap[XK_m] = KC_M;
  116. x11.keyMap[XK_n] = KC_N;
  117. x11.keyMap[XK_o] = KC_O;
  118. x11.keyMap[XK_p] = KC_P;
  119. x11.keyMap[XK_q] = KC_Q;
  120. x11.keyMap[XK_r] = KC_R;
  121. x11.keyMap[XK_s] = KC_S;
  122. x11.keyMap[XK_t] = KC_T;
  123. x11.keyMap[XK_u] = KC_U;
  124. x11.keyMap[XK_v] = KC_V;
  125. x11.keyMap[XK_w] = KC_W;
  126. x11.keyMap[XK_x] = KC_X;
  127. x11.keyMap[XK_y] = KC_Y;
  128. x11.keyMap[XK_z] = KC_Z;
  129. x11.keyMap[XK_F1] = KC_F1;
  130. x11.keyMap[XK_F2] = KC_F2;
  131. x11.keyMap[XK_F3] = KC_F3;
  132. x11.keyMap[XK_F4] = KC_F4;
  133. x11.keyMap[XK_F5] = KC_F5;
  134. x11.keyMap[XK_F6] = KC_F6;
  135. x11.keyMap[XK_F7] = KC_F7;
  136. x11.keyMap[XK_F8] = KC_F8;
  137. x11.keyMap[XK_F9] = KC_F9;
  138. x11.keyMap[XK_F10] = KC_F10;
  139. x11.keyMap[XK_F11] = KC_F11;
  140. x11.keyMap[XK_F12] = KC_F12;
  141. x11.keyMap[XK_F13] = KC_F13;
  142. x11.keyMap[XK_F14] = KC_F14;
  143. x11.keyMap[XK_F15] = KC_F15;
  144. // keypad
  145. x11.keyMap[XK_KP_0] = KC_NUMPAD0;
  146. x11.keyMap[XK_KP_1] = KC_NUMPAD1;
  147. x11.keyMap[XK_KP_2] = KC_NUMPAD2;
  148. x11.keyMap[XK_KP_3] = KC_NUMPAD3;
  149. x11.keyMap[XK_KP_4] = KC_NUMPAD4;
  150. x11.keyMap[XK_KP_5] = KC_NUMPAD5;
  151. x11.keyMap[XK_KP_6] = KC_NUMPAD6;
  152. x11.keyMap[XK_KP_7] = KC_NUMPAD7;
  153. x11.keyMap[XK_KP_8] = KC_NUMPAD8;
  154. x11.keyMap[XK_KP_9] = KC_NUMPAD9;
  155. x11.keyMap[XK_KP_Add] = KC_ADD;
  156. x11.keyMap[XK_KP_Subtract] = KC_SUBTRACT;
  157. x11.keyMap[XK_KP_Decimal] = KC_DECIMAL;
  158. x11.keyMap[XK_KP_Equal] = KC_NUMPADEQUALS;
  159. x11.keyMap[XK_KP_Divide] = KC_DIVIDE;
  160. x11.keyMap[XK_KP_Multiply] = KC_MULTIPLY;
  161. x11.keyMap[XK_KP_Enter] = KC_NUMPADENTER;
  162. // keypad with numlock off
  163. x11.keyMap[XK_KP_Home] = KC_NUMPAD7;
  164. x11.keyMap[XK_KP_Up] = KC_NUMPAD8;
  165. x11.keyMap[XK_KP_Page_Up] = KC_NUMPAD9;
  166. x11.keyMap[XK_KP_Left] = KC_NUMPAD4;
  167. x11.keyMap[XK_KP_Begin] = KC_NUMPAD5;
  168. x11.keyMap[XK_KP_Right] = KC_NUMPAD6;
  169. x11.keyMap[XK_KP_End] = KC_NUMPAD1;
  170. x11.keyMap[XK_KP_Down] = KC_NUMPAD2;
  171. x11.keyMap[XK_KP_Page_Down] = KC_NUMPAD3;
  172. x11.keyMap[XK_KP_Insert] = KC_NUMPAD0;
  173. x11.keyMap[XK_KP_Delete] = KC_DECIMAL;
  174. x11.keyMap[XK_Up] = KC_UP;
  175. x11.keyMap[XK_Down] = KC_DOWN;
  176. x11.keyMap[XK_Left] = KC_LEFT;
  177. x11.keyMap[XK_Right] = KC_RIGHT;
  178. x11.keyMap[XK_Page_Up] = KC_PGUP;
  179. x11.keyMap[XK_Page_Down] = KC_PGDOWN;
  180. x11.keyMap[XK_Home] = KC_HOME;
  181. x11.keyMap[XK_End] = KC_END;
  182. x11.keyMap[XK_Num_Lock] = KC_NUMLOCK;
  183. x11.keyMap[XK_Print] = KC_SYSRQ;
  184. x11.keyMap[XK_Scroll_Lock] = KC_SCROLL;
  185. x11.keyMap[XK_Pause] = KC_PAUSE;
  186. x11.keyMap[XK_Shift_R] = KC_RSHIFT;
  187. x11.keyMap[XK_Shift_L] = KC_LSHIFT;
  188. x11.keyMap[XK_Alt_R] = KC_RALT;
  189. x11.keyMap[XK_Alt_L] = KC_LALT;
  190. x11.keyMap[XK_Insert] = KC_INSERT;
  191. x11.keyMap[XK_Delete] = KC_DELETE;
  192. x11.keyMap[XK_Super_L] = KC_LWIN;
  193. x11.keyMap[XK_Super_R] = KC_RWIN;
  194. x11.keyMap[XK_Menu] = KC_APPS;
  195. // try to set a latin1 locales otherwise fallback to standard C locale
  196. static char locales[][32] = { "en_US.iso88591", "iso88591", "en_US", "C" };
  197. for(int i=0;i<4;++i) {
  198. if(setlocale(LC_ALL, locales[i]))
  199. break;
  200. }
  201. // open display
  202. x11.display = XOpenDisplay(0);
  203. if(!x11.display)
  204. fatal("Failed to open X display");
  205. // check if GLX is supported on this display
  206. if(!glXQueryExtension(x11.display, 0, 0))
  207. fatal("GLX not supported");
  208. // retrieve GLX version
  209. int glxMajor;
  210. int glxMinor;
  211. if(!glXQueryVersion(x11.display, &glxMajor, &glxMinor))
  212. fatal("Unable to query GLX version");
  213. notice("GLX version %d.%d", glxMajor, glxMinor);
  214. // clipboard related atoms
  215. x11.atomClipboard = XInternAtom(x11.display, "CLIPBOARD", False);
  216. x11.atomTargets = XInternAtom(x11.display, "TARGETS", False);
  217. x11.atomUTF8String = XInternAtom(x11.display, "UTF8_STRING", False);
  218. x11.atomText = XInternAtom(x11.display, "TEXT", False);
  219. x11.atomCompoundText = XInternAtom(x11.display, "COMPOUND_TEXT", False);
  220. }
  221. void Platform::terminate()
  222. {
  223. if(x11.window) {
  224. destroyWindow();
  225. x11.window = None;
  226. }
  227. // close display
  228. if(x11.display) {
  229. XCloseDisplay(x11.display);
  230. x11.display = NULL;
  231. }
  232. }
  233. void Platform::poll()
  234. {
  235. XEvent event, peekevent;
  236. static InputEvent inputEvent;
  237. while(XPending(x11.display) > 0) {
  238. XNextEvent(x11.display, &event);
  239. // call filter because xim will discard KeyPress events when keys still composing
  240. if(XFilterEvent(&event, x11.window))
  241. continue;
  242. // discard events of repeated key releases
  243. if(event.type == KeyRelease && XPending(x11.display)) {
  244. XPeekEvent(x11.display, &peekevent);
  245. if((peekevent.type == KeyPress) &&
  246. (peekevent.xkey.keycode == event.xkey.keycode) &&
  247. ((peekevent.xkey.time-event.xkey.time) < 2))
  248. continue;
  249. }
  250. switch(event.type) {
  251. case ConfigureNotify:
  252. // window resize
  253. if(x11.width != event.xconfigure.width || x11.height != event.xconfigure.height) {
  254. x11.width = event.xconfigure.width;
  255. x11.height = event.xconfigure.height;
  256. g_engine.onResize(x11.width, x11.height);
  257. }
  258. break;
  259. case KeyPress:
  260. case KeyRelease: {
  261. KeySym keysym;
  262. char buf[32];
  263. int len;
  264. inputEvent.key.ctrl = (event.xkey.state & ControlMask);
  265. inputEvent.key.shift = (event.xkey.state & ShiftMask);
  266. inputEvent.key.alt = (event.xkey.state & Mod1Mask);
  267. // fire enter text event
  268. if(event.type == KeyPress && !inputEvent.key.ctrl && !inputEvent.key.alt) {
  269. if(x11.xic) { // with xim we can get latin1 input correctly
  270. Status status;
  271. len = XmbLookupString(x11.xic, &event.xkey, buf, sizeof(buf), &keysym, &status);
  272. } else { // otherwise use XLookupString, but it doesn't work right with dead keys often
  273. static XComposeStatus compose = {NULL, 0};
  274. len = XLookupString(&event.xkey, buf, sizeof(buf), &keysym, &compose);
  275. }
  276. if(len > 0 &&
  277. // for some reason these keys produces characters and we don't want that
  278. keysym != XK_BackSpace &&
  279. keysym != XK_Return &&
  280. keysym != XK_Delete &&
  281. keysym != XK_Escape
  282. ) {
  283. //debug("char: %c code: %d", buf[0], (unsigned char)buf[0]);
  284. inputEvent.type = EV_TEXT_ENTER;
  285. inputEvent.key.keychar = buf[0];
  286. inputEvent.key.keycode = KC_UNKNOWN;
  287. g_engine.onInputEvent(&inputEvent);
  288. }
  289. }
  290. // unmask Shift/Lock to get expected results
  291. event.xkey.state &= ~(ShiftMask | LockMask);
  292. len = XLookupString(&event.xkey, buf, sizeof(buf), &keysym, 0);
  293. // fire key up/down event
  294. if(x11.keyMap.find(keysym) != x11.keyMap.end()) {
  295. inputEvent.key.keycode = x11.keyMap[keysym];
  296. inputEvent.type = (event.type == KeyPress) ? EV_KEY_DOWN : EV_KEY_UP;
  297. inputEvent.key.keychar = (len > 0) ? buf[0] : 0;
  298. g_engine.onInputEvent(&inputEvent);
  299. }
  300. break;
  301. }
  302. case ButtonPress:
  303. case ButtonRelease:
  304. switch(event.xbutton.button) {
  305. case Button1:
  306. inputEvent.type = (event.type == ButtonPress) ? EV_MOUSE_LDOWN : EV_MOUSE_LUP;
  307. break;
  308. case Button3:
  309. inputEvent.type = (event.type == ButtonPress) ? EV_MOUSE_RDOWN : EV_MOUSE_RUP;
  310. break;
  311. case Button2:
  312. inputEvent.type = (event.type == ButtonPress) ? EV_MOUSE_MDOWN : EV_MOUSE_MUP;
  313. break;
  314. case Button4:
  315. inputEvent.type = EV_MOUSE_WHEEL_UP;
  316. break;
  317. case Button5:
  318. inputEvent.type = EV_MOUSE_WHEEL_DOWN;
  319. break;
  320. }
  321. g_engine.onInputEvent(&inputEvent);
  322. break;
  323. case MotionNotify:
  324. inputEvent.type = EV_MOUSE_MOVE;
  325. inputEvent.mouse.x = event.xbutton.x;
  326. inputEvent.mouse.y = event.xbutton.y;
  327. g_engine.onInputEvent(&inputEvent);
  328. break;
  329. case MapNotify:
  330. x11.visible = true;
  331. break;
  332. case UnmapNotify:
  333. x11.visible = false;
  334. break;
  335. case FocusIn:
  336. x11.focused = true;
  337. break;
  338. case FocusOut:
  339. x11.focused = false;
  340. break;
  341. // clipboard data request
  342. case SelectionRequest:
  343. {
  344. XEvent respond;
  345. XSelectionRequestEvent *req = &(event.xselectionrequest);
  346. if(req->target == x11.atomTargets ) {
  347. Atom typeList[] = {x11.atomText, x11.atomCompoundText, x11.atomUTF8String, XA_STRING};
  348. XChangeProperty(x11.display, req->requestor,
  349. req->property, req->target,
  350. 8, PropModeReplace,
  351. (unsigned char *) &typeList,
  352. sizeof(typeList));
  353. respond.xselection.property = req->property;
  354. } else {
  355. XChangeProperty(x11.display,
  356. req->requestor,
  357. req->property, req->target,
  358. 8,
  359. PropModeReplace,
  360. (unsigned char*) x11.clipboardText.c_str(),
  361. x11.clipboardText.size());
  362. respond.xselection.property = req->property;
  363. }
  364. respond.xselection.type = SelectionNotify;
  365. respond.xselection.display = req->display;
  366. respond.xselection.requestor = req->requestor;
  367. respond.xselection.selection = req->selection;
  368. respond.xselection.target = req->target;
  369. respond.xselection.time = req->time;
  370. XSendEvent(x11.display, req->requestor, 0, 0, &respond);
  371. XFlush(x11.display);
  372. break;
  373. }
  374. case ClientMessage:
  375. {
  376. if((Atom)event.xclient.data.l[0] == x11.atomDeleteWindow)
  377. g_engine.onClose();
  378. break;
  379. }
  380. }
  381. }
  382. }
  383. unsigned long Platform::getTicks()
  384. {
  385. static timeval tv;
  386. static unsigned long firstTick = 0;
  387. gettimeofday(&tv, 0);
  388. if(!firstTick)
  389. firstTick = tv.tv_sec;
  390. return ((tv.tv_sec - firstTick) * 1000) + (tv.tv_usec / 1000);
  391. }
  392. void Platform::sleep(unsigned long miliseconds)
  393. {
  394. timespec tv;
  395. tv.tv_sec = miliseconds / 1000;
  396. tv.tv_nsec = (miliseconds % 1000) * 1000000;
  397. nanosleep(&tv, NULL);
  398. }
  399. bool Platform::createWindow(int width, int height, int minWidth, int minHeight)
  400. {
  401. static int attrList[] = {
  402. GLX_USE_GL,
  403. GLX_RGBA,
  404. GLX_DOUBLEBUFFER,
  405. None
  406. };
  407. // choose OpenGL, RGBA, double buffered, visual
  408. x11.visual = glXChooseVisual(x11.display, DefaultScreen(x11.display), attrList);
  409. if(!x11.visual)
  410. fatal("RGBA/Double buffered visual not supported");
  411. // create GLX context
  412. x11.glxContext = glXCreateContext(x11.display, x11.visual, 0, GL_TRUE);
  413. if(!x11.glxContext)
  414. fatal("Unable to create GLX context");
  415. // color map
  416. x11.colormap = XCreateColormap(x11.display,
  417. RootWindow(x11.display, x11.visual->screen),
  418. x11.visual->visual,
  419. AllocNone);
  420. // setup window attributes
  421. XSetWindowAttributes wa;
  422. wa.colormap = x11.colormap;
  423. wa.border_pixel = 0;
  424. wa.event_mask = KeyPressMask | KeyReleaseMask |
  425. ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
  426. ExposureMask | VisibilityChangeMask |
  427. StructureNotifyMask | FocusChangeMask;
  428. // calculate center position
  429. int x = (XDisplayHeight(x11.display, DefaultScreen(x11.display)) - width) / 2;
  430. int y = (XDisplayHeight(x11.display, DefaultScreen(x11.display)) - height) / 2;
  431. // create the window
  432. x11.window = XCreateWindow(x11.display,
  433. RootWindow(x11.display, x11.visual->screen),
  434. x, y,
  435. width, height,
  436. 0,
  437. x11.visual->depth,
  438. InputOutput,
  439. x11.visual->visual,
  440. CWBorderPixel | CWColormap | CWEventMask,
  441. &wa);
  442. if(!x11.window)
  443. fatal("Unable to create X window");
  444. // create input context (to have better key input handling)
  445. if(XSupportsLocale()) {
  446. XSetLocaleModifiers("");
  447. x11.xim = XOpenIM(x11.display, NULL, NULL, NULL);
  448. if(x11.xim) {
  449. x11.xic = XCreateIC(x11.xim,
  450. XNInputStyle,
  451. XIMPreeditNothing | XIMStatusNothing,
  452. XNClientWindow, x11.window, NULL);
  453. if(!x11.xic)
  454. error("Unable to create the input context");
  455. } else
  456. error("Failed to open an input method");
  457. } else
  458. error("X11 does not support the current locale");
  459. if(!x11.xic)
  460. warning("Input of special keys maybe messed up because we couldn't create an input context");
  461. // set window minimum size
  462. XSizeHints xsizehints;
  463. xsizehints.flags = PMinSize;
  464. xsizehints.min_width = minWidth;
  465. xsizehints.min_height= minHeight;
  466. XSetWMSizeHints(x11.display, x11.window, &xsizehints, XA_WM_NORMAL_HINTS);
  467. // handle delete window event
  468. x11.atomDeleteWindow = XInternAtom(x11.display, "WM_DELETE_WINDOW", True);
  469. XSetWMProtocols(x11.display, x11.window, &x11.atomDeleteWindow , 1);
  470. // connect the GLX-context to the window
  471. glXMakeCurrent(x11.display, x11.window, x11.glxContext);
  472. x11.width = width;
  473. x11.height = height;
  474. return true;
  475. }
  476. void Platform::destroyWindow()
  477. {
  478. if(x11.glxContext) {
  479. glXMakeCurrent(x11.display, None, NULL);
  480. glXDestroyContext(x11.display, x11.glxContext);
  481. x11.glxContext = NULL;
  482. }
  483. if(x11.visual) {
  484. XFree(x11.visual);
  485. x11.visual = NULL;
  486. }
  487. if(x11.colormap != None) {
  488. XFreeColormap(x11.display, x11.colormap);
  489. x11.colormap = 0;
  490. }
  491. if(x11.window != None) {
  492. XUnmapWindow(x11.display, x11.window);
  493. XDestroyWindow(x11.display, x11.window);
  494. x11.window = None;
  495. }
  496. if(x11.xic) {
  497. XDestroyIC(x11.xic);
  498. x11.xic = NULL;
  499. }
  500. if(x11.xim) {
  501. XCloseIM(x11.xim);
  502. x11.xim = NULL;
  503. }
  504. }
  505. void Platform::showWindow()
  506. {
  507. XMapWindow(x11.display, x11.window);
  508. }
  509. void Platform::setWindowTitle(const char *title)
  510. {
  511. XStoreName(x11.display, x11.window, title);
  512. XSetIconName(x11.display, x11.window, title);
  513. }
  514. void *Platform::getExtensionProcAddress(const char *ext)
  515. {
  516. return (void*)glXGetProcAddressARB((const GLubyte*)ext);
  517. }
  518. bool Platform::isExtensionSupported(const char *ext)
  519. {
  520. const char *exts = glXQueryExtensionsString(x11.display, DefaultScreen(x11.display));
  521. if(strstr(exts, ext))
  522. return true;
  523. return true;
  524. }
  525. const char *Platform::getTextFromClipboard()
  526. {
  527. Window ownerWindow = XGetSelectionOwner(x11.display, x11.atomClipboard);
  528. if(ownerWindow == x11.window)
  529. return x11.clipboardText.c_str();
  530. std::string clipboard = "";
  531. if(ownerWindow != None) {
  532. XConvertSelection(x11.display, x11.atomClipboard, XA_STRING, 0, ownerWindow, CurrentTime);
  533. XFlush(x11.display);
  534. // hack to wait SelectioNotify event, otherwise we will get wrong clipboard pastes
  535. sleep(100);
  536. // check for data
  537. Atom type;
  538. int format;
  539. unsigned long numItems, bytesLeft, dummy;
  540. unsigned char *data;
  541. XGetWindowProperty(x11.display, ownerWindow,
  542. XA_STRING,
  543. 0, 0, 0,
  544. AnyPropertyType,
  545. &type,
  546. &format,
  547. &numItems,
  548. &bytesLeft,
  549. &data);
  550. if(bytesLeft > 0) {
  551. // get the data get
  552. int result = XGetWindowProperty(x11.display, ownerWindow,
  553. XA_STRING,
  554. 0,
  555. bytesLeft,
  556. 0,
  557. AnyPropertyType,
  558. &type,
  559. &format,
  560. &numItems,
  561. &dummy,
  562. &data);
  563. if(result == Success)
  564. clipboard = (const char*)data;
  565. XFree(data);
  566. }
  567. }
  568. return clipboard.c_str();
  569. }
  570. void Platform::copyToClipboard(const char *text)
  571. {
  572. x11.clipboardText = text;
  573. XSetSelectionOwner(x11.display, x11.atomClipboard, x11.window, CurrentTime);
  574. XFlush(x11.display);
  575. }
  576. void Platform::hideMouseCursor()
  577. {
  578. if(x11.cursor == None) {
  579. char bm[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  580. Pixmap pix = XCreateBitmapFromData(x11.display, x11.window, bm, 8, 8);
  581. XColor black;
  582. memset(&black, 0, sizeof(XColor));
  583. black.flags = DoRed | DoGreen | DoBlue;
  584. x11.cursor = XCreatePixmapCursor(x11.display, pix, pix, &black, &black, 0, 0);
  585. XFreePixmap(x11.display, pix);
  586. }
  587. XDefineCursor(x11.display, x11.window, x11.cursor);
  588. }
  589. void Platform::showMouseCursor()
  590. {
  591. XUndefineCursor(x11.display, x11.window);
  592. if(x11.cursor != None) {
  593. XFreeCursor(x11.display, x11.cursor);
  594. x11.cursor = None;
  595. }
  596. }
  597. void Platform::setVsync(bool enable)
  598. {
  599. typedef GLint (*glSwapIntervalProc)(GLint);
  600. glSwapIntervalProc glSwapInterval = NULL;
  601. if(isExtensionSupported("GLX_MESA_swap_control"))
  602. glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalMESA");
  603. else if(isExtensionSupported("GLX_SGI_swap_control"))
  604. glSwapInterval = (glSwapIntervalProc)getExtensionProcAddress("glXSwapIntervalSGI");
  605. if(glSwapInterval)
  606. glSwapInterval(enable ? 1 : 0);
  607. }
  608. void Platform::swapBuffers()
  609. {
  610. glXSwapBuffers(x11.display, x11.window);
  611. }
  612. bool Platform::isWindowFocused()
  613. {
  614. return x11.focused;
  615. }
  616. bool Platform::isWindowVisible()
  617. {
  618. return x11.visible;
  619. }