DISTRHO Plugin Framework
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.

602 lines
15KB

  1. /*
  2. Copyright 2012-2014 David Robillard <http://drobilla.net>
  3. Copyright 2011-2012 Ben Loftis, Harrison Consoles
  4. Copyright 2013,2015 Robin Gareus <robin@gareus.org>
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /**
  17. @file pugl_x11.c X11 Pugl Implementation.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <GL/gl.h>
  23. #include <GL/glx.h>
  24. #include <X11/Xatom.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <X11/keysym.h>
  28. #include "pugl_internal.h"
  29. #ifndef DGL_FILE_BROWSER_DISABLED
  30. #define SOFD_HAVE_X11
  31. #include "../sofd/libsofd.h"
  32. #include "../sofd/libsofd.c"
  33. #endif
  34. /* work around buggy re-parent & focus issues on some systems
  35. * where no keyboard events are passed through even if the
  36. * app has mouse-focus and all other events are working.
  37. */
  38. //#define PUGL_GRAB_FOCUS
  39. /* show messages during initalization
  40. */
  41. //#define PUGL_VERBOSE
  42. struct PuglInternalsImpl {
  43. Display* display;
  44. int screen;
  45. Window win;
  46. GLXContext ctx;
  47. Bool doubleBuffered;
  48. };
  49. /**
  50. Attributes for single-buffered RGBA with at least
  51. 4 bits per color and a 16 bit depth buffer.
  52. */
  53. static int attrListSgl[] = {
  54. GLX_RGBA,
  55. GLX_RED_SIZE, 4,
  56. GLX_GREEN_SIZE, 4,
  57. GLX_BLUE_SIZE, 4,
  58. GLX_DEPTH_SIZE, 16,
  59. GLX_ARB_multisample, 1,
  60. None
  61. };
  62. /**
  63. Attributes for double-buffered RGBA with at least
  64. 4 bits per color and a 16 bit depth buffer.
  65. */
  66. static int attrListDbl[] = {
  67. GLX_RGBA,
  68. GLX_DOUBLEBUFFER, True,
  69. GLX_RED_SIZE, 4,
  70. GLX_GREEN_SIZE, 4,
  71. GLX_BLUE_SIZE, 4,
  72. GLX_DEPTH_SIZE, 16,
  73. GLX_ARB_multisample, 1,
  74. None
  75. };
  76. /**
  77. Attributes for double-buffered RGBA with multi-sampling
  78. (antialiasing)
  79. */
  80. static int attrListDblMS[] = {
  81. GLX_RGBA,
  82. GLX_DOUBLEBUFFER, True,
  83. GLX_RED_SIZE, 4,
  84. GLX_GREEN_SIZE, 4,
  85. GLX_BLUE_SIZE, 4,
  86. GLX_ALPHA_SIZE, 4,
  87. GLX_DEPTH_SIZE, 16,
  88. GLX_SAMPLE_BUFFERS, 1,
  89. GLX_SAMPLES, 4,
  90. None
  91. };
  92. PuglInternals*
  93. puglInitInternals(void)
  94. {
  95. return (PuglInternals*)calloc(1, sizeof(PuglInternals));
  96. }
  97. void
  98. puglEnterContext(PuglView* view)
  99. {
  100. glXMakeCurrent(view->impl->display, view->impl->win, view->impl->ctx);
  101. }
  102. void
  103. puglLeaveContext(PuglView* view, bool flush)
  104. {
  105. if (flush) {
  106. glFlush();
  107. if (view->impl->doubleBuffered) {
  108. glXSwapBuffers(view->impl->display, view->impl->win);
  109. }
  110. }
  111. glXMakeCurrent(view->impl->display, None, NULL);
  112. }
  113. int
  114. puglCreateWindow(PuglView* view, const char* title)
  115. {
  116. PuglInternals* impl = (PuglInternals*)calloc(1, sizeof(PuglInternals));
  117. if (!impl) {
  118. return 1;
  119. }
  120. view->impl = impl;
  121. impl->display = XOpenDisplay(NULL);
  122. if (!impl->display) {
  123. free(impl);
  124. return 1;
  125. }
  126. impl->screen = DefaultScreen(impl->display);
  127. impl->doubleBuffered = True;
  128. XVisualInfo* vi = glXChooseVisual(impl->display, impl->screen, attrListDblMS);
  129. if (!vi) {
  130. vi = glXChooseVisual(impl->display, impl->screen, attrListDbl);
  131. #ifdef PUGL_VERBOSE
  132. printf("puGL: multisampling (antialiasing) is not available\n");
  133. #endif
  134. }
  135. if (!vi) {
  136. vi = glXChooseVisual(impl->display, impl->screen, attrListSgl);
  137. impl->doubleBuffered = False;
  138. }
  139. if (!vi) {
  140. XCloseDisplay(impl->display);
  141. free(impl);
  142. return 1;
  143. }
  144. #ifdef PUGL_VERBOSE
  145. int glxMajor, glxMinor;
  146. glXQueryVersion(impl->display, &glxMajor, &glxMinor);
  147. printf("puGL: GLX-Version : %d.%d\n", glxMajor, glxMinor);
  148. #endif
  149. impl->ctx = glXCreateContext(impl->display, vi, 0, GL_TRUE);
  150. if (!impl->ctx) {
  151. XCloseDisplay(impl->display);
  152. free(impl);
  153. return 1;
  154. }
  155. Window xParent = view->parent
  156. ? (Window)view->parent
  157. : RootWindow(impl->display, impl->screen);
  158. Colormap cmap = XCreateColormap(
  159. impl->display, xParent, vi->visual, AllocNone);
  160. XSetWindowAttributes attr;
  161. memset(&attr, 0, sizeof(XSetWindowAttributes));
  162. attr.background_pixel = BlackPixel(impl->display, impl->screen);
  163. attr.border_pixel = BlackPixel(impl->display, impl->screen);
  164. attr.colormap = cmap;
  165. attr.event_mask = (ExposureMask | StructureNotifyMask |
  166. EnterWindowMask | LeaveWindowMask |
  167. KeyPressMask | KeyReleaseMask |
  168. ButtonPressMask | ButtonReleaseMask |
  169. PointerMotionMask | FocusChangeMask);
  170. impl->win = XCreateWindow(
  171. impl->display, xParent,
  172. 0, 0, view->width, view->height, 0, vi->depth, InputOutput, vi->visual,
  173. CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr);
  174. if (!impl->win) {
  175. XCloseDisplay(impl->display);
  176. free(impl);
  177. return 1;
  178. }
  179. puglUpdateGeometryConstraints(view, view->min_width, view->min_height, view->min_width != view->width);
  180. XResizeWindow(view->impl->display, view->impl->win, view->width, view->height);
  181. if (title) {
  182. XStoreName(impl->display, impl->win, title);
  183. }
  184. if (view->transient_parent > 0) {
  185. XSetTransientForHint(impl->display, impl->win, (Window)view->transient_parent);
  186. }
  187. if (view->parent) {
  188. XMapRaised(impl->display, impl->win);
  189. } else {
  190. Atom wmDelete = XInternAtom(impl->display, "WM_DELETE_WINDOW", True);
  191. XSetWMProtocols(impl->display, impl->win, &wmDelete, 1);
  192. }
  193. #ifdef PUGL_VERBOSE
  194. if (glXIsDirect(impl->display, impl->ctx)) {
  195. printf("puGL: DRI enabled (to disable, set LIBGL_ALWAYS_INDIRECT=1\n");
  196. } else {
  197. printf("puGL: No DRI available\n");
  198. }
  199. #endif
  200. XFree(vi);
  201. return 0;
  202. }
  203. void
  204. puglDestroy(PuglView* view)
  205. {
  206. if (!view) {
  207. return;
  208. }
  209. #ifndef DGL_FILE_BROWSER_DISABLED
  210. x_fib_close(view->impl->display);
  211. #endif
  212. glXDestroyContext(view->impl->display, view->impl->ctx);
  213. XDestroyWindow(view->impl->display, view->impl->win);
  214. XCloseDisplay(view->impl->display);
  215. free(view->impl);
  216. free(view);
  217. }
  218. void
  219. puglShowWindow(PuglView* view)
  220. {
  221. XMapRaised(view->impl->display, view->impl->win);
  222. }
  223. void
  224. puglHideWindow(PuglView* view)
  225. {
  226. XUnmapWindow(view->impl->display, view->impl->win);
  227. }
  228. static void
  229. puglReshape(PuglView* view, int width, int height)
  230. {
  231. puglEnterContext(view);
  232. if (view->reshapeFunc) {
  233. view->reshapeFunc(view, width, height);
  234. } else {
  235. puglDefaultReshape(width, height);
  236. }
  237. puglLeaveContext(view, false);
  238. view->width = width;
  239. view->height = height;
  240. }
  241. static void
  242. puglDisplay(PuglView* view)
  243. {
  244. puglEnterContext(view);
  245. view->redisplay = false;
  246. if (view->displayFunc) {
  247. view->displayFunc(view);
  248. }
  249. puglLeaveContext(view, true);
  250. }
  251. static void
  252. puglResize(PuglView* view)
  253. {
  254. int set_hints = 1;
  255. view->pending_resize = false;
  256. if (!view->resizeFunc) { return; }
  257. /* ask the plugin about the new size */
  258. view->resizeFunc(view, &view->width, &view->height, &set_hints);
  259. if (set_hints) {
  260. XSizeHints sizeHints;
  261. memset(&sizeHints, 0, sizeof(sizeHints));
  262. sizeHints.flags = PMinSize|PMaxSize;
  263. sizeHints.min_width = view->width;
  264. sizeHints.min_height = view->height;
  265. sizeHints.max_width = view->user_resizable ? 4096 : view->width;
  266. sizeHints.max_height = view->user_resizable ? 4096 : view->height;
  267. XSetWMNormalHints(view->impl->display, view->impl->win, &sizeHints);
  268. }
  269. XResizeWindow(view->impl->display, view->impl->win, view->width, view->height);
  270. XFlush(view->impl->display);
  271. #ifdef PUGL_VERBOSE
  272. printf("puGL: window resize (%dx%d)\n", view->width, view->height);
  273. #endif
  274. /* and call Reshape in glX context */
  275. puglReshape(view, view->width, view->height);
  276. }
  277. static PuglKey
  278. keySymToSpecial(KeySym sym)
  279. {
  280. switch (sym) {
  281. case XK_F1: return PUGL_KEY_F1;
  282. case XK_F2: return PUGL_KEY_F2;
  283. case XK_F3: return PUGL_KEY_F3;
  284. case XK_F4: return PUGL_KEY_F4;
  285. case XK_F5: return PUGL_KEY_F5;
  286. case XK_F6: return PUGL_KEY_F6;
  287. case XK_F7: return PUGL_KEY_F7;
  288. case XK_F8: return PUGL_KEY_F8;
  289. case XK_F9: return PUGL_KEY_F9;
  290. case XK_F10: return PUGL_KEY_F10;
  291. case XK_F11: return PUGL_KEY_F11;
  292. case XK_F12: return PUGL_KEY_F12;
  293. case XK_Left: return PUGL_KEY_LEFT;
  294. case XK_Up: return PUGL_KEY_UP;
  295. case XK_Right: return PUGL_KEY_RIGHT;
  296. case XK_Down: return PUGL_KEY_DOWN;
  297. case XK_Page_Up: return PUGL_KEY_PAGE_UP;
  298. case XK_Page_Down: return PUGL_KEY_PAGE_DOWN;
  299. case XK_Home: return PUGL_KEY_HOME;
  300. case XK_End: return PUGL_KEY_END;
  301. case XK_Insert: return PUGL_KEY_INSERT;
  302. case XK_Shift_L: return PUGL_KEY_SHIFT;
  303. case XK_Shift_R: return PUGL_KEY_SHIFT;
  304. case XK_Control_L: return PUGL_KEY_CTRL;
  305. case XK_Control_R: return PUGL_KEY_CTRL;
  306. case XK_Alt_L: return PUGL_KEY_ALT;
  307. case XK_Alt_R: return PUGL_KEY_ALT;
  308. case XK_Super_L: return PUGL_KEY_SUPER;
  309. case XK_Super_R: return PUGL_KEY_SUPER;
  310. }
  311. return (PuglKey)0;
  312. }
  313. static void
  314. setModifiers(PuglView* view, unsigned xstate, unsigned xtime)
  315. {
  316. view->event_timestamp_ms = xtime;
  317. view->mods = 0;
  318. view->mods |= (xstate & ShiftMask) ? PUGL_MOD_SHIFT : 0;
  319. view->mods |= (xstate & ControlMask) ? PUGL_MOD_CTRL : 0;
  320. view->mods |= (xstate & Mod1Mask) ? PUGL_MOD_ALT : 0;
  321. view->mods |= (xstate & Mod4Mask) ? PUGL_MOD_SUPER : 0;
  322. }
  323. static void
  324. dispatchKey(PuglView* view, XEvent* event, bool press)
  325. {
  326. KeySym sym;
  327. char str[5];
  328. PuglKey special;
  329. const int n = XLookupString(&event->xkey, str, 4, &sym, NULL);
  330. if (sym == XK_Escape && view->closeFunc && !press && !view->parent) {
  331. view->closeFunc(view);
  332. view->redisplay = false;
  333. return;
  334. }
  335. if (n == 0 && sym == 0) {
  336. goto send_event;
  337. return;
  338. }
  339. if (n > 1) {
  340. fprintf(stderr, "warning: Unsupported multi-byte key %X\n", (int)sym);
  341. goto send_event;
  342. return;
  343. }
  344. special = keySymToSpecial(sym);
  345. if (special && view->specialFunc) {
  346. if (view->specialFunc(view, press, special) == 0) {
  347. return;
  348. }
  349. } else if (!special && view->keyboardFunc) {
  350. if (view->keyboardFunc(view, press, str[0]) == 0) {
  351. return;
  352. }
  353. }
  354. send_event:
  355. if (view->parent != 0) {
  356. event->xkey.time = 0; // purposefully set an invalid time, used for feedback detection on bad hosts
  357. event->xany.window = view->parent;
  358. XSendEvent(view->impl->display, view->parent, False, NoEventMask, event);
  359. }
  360. }
  361. PuglStatus
  362. puglProcessEvents(PuglView* view)
  363. {
  364. XEvent event;
  365. while (XPending(view->impl->display) > 0) {
  366. XNextEvent(view->impl->display, &event);
  367. #ifndef DGL_FILE_BROWSER_DISABLED
  368. if (x_fib_handle_events(view->impl->display, &event)) {
  369. const int status = x_fib_status();
  370. if (status > 0) {
  371. char* const filename = x_fib_filename();
  372. x_fib_close(view->impl->display);
  373. if (view->fileSelectedFunc) {
  374. view->fileSelectedFunc(view, filename);
  375. }
  376. free(filename);
  377. } else if (status < 0) {
  378. x_fib_close(view->impl->display);
  379. if (view->fileSelectedFunc) {
  380. view->fileSelectedFunc(view, NULL);
  381. }
  382. }
  383. break;
  384. }
  385. #endif
  386. if (event.xany.window != view->impl->win &&
  387. (view->parent == 0 || event.xany.window != (Window)view->parent)) {
  388. continue;
  389. }
  390. if ((event.type == KeyPress || event.type == KeyRelease) && event.xkey.time == 0) {
  391. continue;
  392. }
  393. switch (event.type) {
  394. case UnmapNotify:
  395. if (view->motionFunc) {
  396. view->motionFunc(view, -1, -1);
  397. }
  398. break;
  399. case MapNotify:
  400. puglReshape(view, view->width, view->height);
  401. break;
  402. case ConfigureNotify:
  403. if ((event.xconfigure.width != view->width) ||
  404. (event.xconfigure.height != view->height)) {
  405. puglReshape(view,
  406. event.xconfigure.width,
  407. event.xconfigure.height);
  408. }
  409. break;
  410. case Expose:
  411. if (event.xexpose.count != 0) {
  412. break;
  413. }
  414. puglDisplay(view);
  415. break;
  416. case MotionNotify:
  417. setModifiers(view, event.xmotion.state, event.xmotion.time);
  418. if (view->motionFunc) {
  419. view->motionFunc(view, event.xmotion.x, event.xmotion.y);
  420. }
  421. break;
  422. case ButtonPress:
  423. setModifiers(view, event.xbutton.state, event.xbutton.time);
  424. if (event.xbutton.button >= 4 && event.xbutton.button <= 7) {
  425. if (view->scrollFunc) {
  426. float dx = 0, dy = 0;
  427. switch (event.xbutton.button) {
  428. case 4: dy = 1.0f; break;
  429. case 5: dy = -1.0f; break;
  430. case 6: dx = -1.0f; break;
  431. case 7: dx = 1.0f; break;
  432. }
  433. view->scrollFunc(view, event.xbutton.x, event.xbutton.y, dx, dy);
  434. }
  435. break;
  436. }
  437. // nobreak
  438. case ButtonRelease:
  439. setModifiers(view, event.xbutton.state, event.xbutton.time);
  440. if (view->mouseFunc &&
  441. (event.xbutton.button < 4 || event.xbutton.button > 7)) {
  442. view->mouseFunc(view,
  443. event.xbutton.button, event.type == ButtonPress,
  444. event.xbutton.x, event.xbutton.y);
  445. }
  446. break;
  447. case KeyPress:
  448. setModifiers(view, event.xkey.state, event.xkey.time);
  449. dispatchKey(view, &event, true);
  450. break;
  451. case KeyRelease: {
  452. setModifiers(view, event.xkey.state, event.xkey.time);
  453. bool repeated = false;
  454. if (view->ignoreKeyRepeat &&
  455. XEventsQueued(view->impl->display, QueuedAfterReading)) {
  456. XEvent next;
  457. XPeekEvent(view->impl->display, &next);
  458. if (next.type == KeyPress &&
  459. next.xkey.time == event.xkey.time &&
  460. next.xkey.keycode == event.xkey.keycode) {
  461. XNextEvent(view->impl->display, &event);
  462. repeated = true;
  463. }
  464. }
  465. if (!repeated) {
  466. dispatchKey(view, &event, false);
  467. }
  468. } break;
  469. case ClientMessage: {
  470. char* type = XGetAtomName(view->impl->display,
  471. event.xclient.message_type);
  472. if (!strcmp(type, "WM_PROTOCOLS")) {
  473. if (view->closeFunc) {
  474. view->closeFunc(view);
  475. view->redisplay = false;
  476. }
  477. }
  478. XFree(type);
  479. } break;
  480. #ifdef PUGL_GRAB_FOCUS
  481. case EnterNotify:
  482. XSetInputFocus(view->impl->display, view->impl->win, RevertToPointerRoot, CurrentTime);
  483. break;
  484. #endif
  485. default:
  486. break;
  487. }
  488. }
  489. if (view->pending_resize) {
  490. puglResize(view);
  491. }
  492. if (view->redisplay) {
  493. puglDisplay(view);
  494. }
  495. return PUGL_SUCCESS;
  496. }
  497. void
  498. puglPostRedisplay(PuglView* view)
  499. {
  500. view->redisplay = true;
  501. }
  502. void
  503. puglPostResize(PuglView* view)
  504. {
  505. view->pending_resize = true;
  506. }
  507. PuglNativeWindow
  508. puglGetNativeWindow(PuglView* view)
  509. {
  510. return view->impl->win;
  511. }
  512. int
  513. puglUpdateGeometryConstraints(PuglView* view, int min_width, int min_height, bool aspect)
  514. {
  515. XSizeHints sizeHints;
  516. memset(&sizeHints, 0, sizeof(sizeHints));
  517. sizeHints.flags = PMinSize|PMaxSize;
  518. sizeHints.min_width = min_width;
  519. sizeHints.min_height = min_height;
  520. sizeHints.max_width = view->user_resizable ? 4096 : min_width;
  521. sizeHints.max_height = view->user_resizable ? 4096 : min_height;
  522. if (aspect) {
  523. sizeHints.flags |= PAspect;
  524. sizeHints.min_aspect.x = min_width;
  525. sizeHints.min_aspect.y = min_height;
  526. sizeHints.max_aspect.x = min_width;
  527. sizeHints.max_aspect.y = min_height;
  528. }
  529. XSetWMNormalHints(view->impl->display, view->impl->win, &sizeHints);
  530. return 0;
  531. }