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.

637 lines
15KB

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