Audio plugin host https://kx.studio/carla
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.

380 lines
9.9KB

  1. /*
  2. Copyright 2012 David Robillard <http://drobilla.net>
  3. Copyright 2011-2012 Ben Loftis, Harrison Consoles
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. /**
  16. @file pugl_x11.c X11 Pugl Implementation.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "pugl_x11.h"
  22. #include "pugl_internal.h"
  23. /**
  24. Attributes for single-buffered RGBA with at least
  25. 4 bits per color and a 16 bit depth buffer.
  26. */
  27. static int attrListSgl[] = {
  28. GLX_RGBA,
  29. GLX_RED_SIZE, 4,
  30. GLX_GREEN_SIZE, 4,
  31. GLX_BLUE_SIZE, 4,
  32. GLX_DEPTH_SIZE, 16,
  33. None
  34. };
  35. /**
  36. Attributes for double-buffered RGBA with at least
  37. 4 bits per color and a 16 bit depth buffer.
  38. */
  39. static int attrListDbl[] = {
  40. GLX_RGBA, GLX_DOUBLEBUFFER,
  41. GLX_RED_SIZE, 4,
  42. GLX_GREEN_SIZE, 4,
  43. GLX_BLUE_SIZE, 4,
  44. GLX_DEPTH_SIZE, 16,
  45. None
  46. };
  47. PuglView*
  48. puglCreate(PuglNativeWindow parent,
  49. const char* title,
  50. int width,
  51. int height,
  52. bool resizable,
  53. bool addToDesktop,
  54. const char* x11Display)
  55. {
  56. PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
  57. PuglInternals* impl = (PuglInternals*)calloc(1, sizeof(PuglInternals));
  58. if (!view || !impl) {
  59. return nullptr;
  60. }
  61. view->impl = impl;
  62. view->width = width;
  63. view->height = height;
  64. impl->display = XOpenDisplay(x11Display);
  65. impl->screen = DefaultScreen(impl->display);
  66. XVisualInfo* vi = glXChooseVisual(impl->display, impl->screen, attrListDbl);
  67. if (!vi) {
  68. vi = glXChooseVisual(impl->display, impl->screen, attrListSgl);
  69. impl->doubleBuffered = False;
  70. printf("singlebuffered rendering will be used, no doublebuffering available\n");
  71. } else {
  72. impl->doubleBuffered = True;
  73. printf("doublebuffered rendering available\n");
  74. }
  75. int glxMajor, glxMinor;
  76. glXQueryVersion(impl->display, &glxMajor, &glxMinor);
  77. printf("GLX-Version %d.%d\n", glxMajor, glxMinor);
  78. impl->ctx = glXCreateContext(impl->display, vi, nullptr, GL_TRUE);
  79. Window xParent = parent
  80. ? (Window)parent
  81. : RootWindow(impl->display, impl->screen);
  82. Colormap cmap = XCreateColormap(
  83. impl->display, xParent, vi->visual, AllocNone);
  84. XSetWindowAttributes attr;
  85. memset(&attr, 0, sizeof(XSetWindowAttributes));
  86. attr.colormap = cmap;
  87. attr.border_pixel = 0;
  88. attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask
  89. | ButtonPressMask | ButtonReleaseMask
  90. | PointerMotionMask | StructureNotifyMask;
  91. impl->win = XCreateWindow(
  92. impl->display, xParent,
  93. 0, 0, (unsigned int)view->width, (unsigned int)view->height, 0, vi->depth, InputOutput, vi->visual,
  94. CWBorderPixel | CWColormap | CWEventMask, &attr);
  95. XSizeHints sizeHints;
  96. memset(&sizeHints, 0, sizeof(sizeHints));
  97. if (!resizable) {
  98. sizeHints.flags = PMinSize|PMaxSize;
  99. sizeHints.min_width = width;
  100. sizeHints.min_height = height;
  101. sizeHints.max_width = width;
  102. sizeHints.max_height = height;
  103. XSetNormalHints(impl->display, impl->win, &sizeHints);
  104. }
  105. if (title) {
  106. XStoreName(impl->display, impl->win, title);
  107. }
  108. if (!parent) {
  109. Atom wmDelete = XInternAtom(impl->display, "WM_DELETE_WINDOW", True);
  110. XSetWMProtocols(impl->display, impl->win, &wmDelete, 1);
  111. }
  112. if (addToDesktop) {
  113. XMapRaised(impl->display, impl->win);
  114. }
  115. if (glXIsDirect(impl->display, impl->ctx)) {
  116. printf("DRI enabled\n");
  117. } else {
  118. printf("no DRI available\n");
  119. }
  120. XFree(vi);
  121. return view;
  122. }
  123. void
  124. puglDestroy(PuglView* view)
  125. {
  126. if (!view) {
  127. return;
  128. }
  129. glXDestroyContext(view->impl->display, view->impl->ctx);
  130. XDestroyWindow(view->impl->display, view->impl->win);
  131. XCloseDisplay(view->impl->display);
  132. free(view->impl);
  133. free(view);
  134. }
  135. static void
  136. puglReshape(PuglView* view, int width, int height)
  137. {
  138. glXMakeCurrent(view->impl->display, view->impl->win, view->impl->ctx);
  139. if (view->reshapeFunc) {
  140. view->reshapeFunc(view, width, height);
  141. } else {
  142. puglDefaultReshape(view, width, height);
  143. }
  144. view->width = width;
  145. view->height = height;
  146. }
  147. static void
  148. puglDisplay(PuglView* view)
  149. {
  150. glXMakeCurrent(view->impl->display, view->impl->win, view->impl->ctx);
  151. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  152. glLoadIdentity();
  153. if (view->displayFunc) {
  154. view->displayFunc(view);
  155. }
  156. glFlush();
  157. if (view->impl->doubleBuffered) {
  158. glXSwapBuffers(view->impl->display, view->impl->win);
  159. }
  160. view->redisplay = false;
  161. }
  162. static PuglKey
  163. keySymToSpecial(KeySym sym)
  164. {
  165. switch (sym) {
  166. case XK_F1: return PUGL_KEY_F1;
  167. case XK_F2: return PUGL_KEY_F2;
  168. case XK_F3: return PUGL_KEY_F3;
  169. case XK_F4: return PUGL_KEY_F4;
  170. case XK_F5: return PUGL_KEY_F5;
  171. case XK_F6: return PUGL_KEY_F6;
  172. case XK_F7: return PUGL_KEY_F7;
  173. case XK_F8: return PUGL_KEY_F8;
  174. case XK_F9: return PUGL_KEY_F9;
  175. case XK_F10: return PUGL_KEY_F10;
  176. case XK_F11: return PUGL_KEY_F11;
  177. case XK_F12: return PUGL_KEY_F12;
  178. case XK_Left: return PUGL_KEY_LEFT;
  179. case XK_Up: return PUGL_KEY_UP;
  180. case XK_Right: return PUGL_KEY_RIGHT;
  181. case XK_Down: return PUGL_KEY_DOWN;
  182. case XK_Page_Up: return PUGL_KEY_PAGE_UP;
  183. case XK_Page_Down: return PUGL_KEY_PAGE_DOWN;
  184. case XK_Home: return PUGL_KEY_HOME;
  185. case XK_End: return PUGL_KEY_END;
  186. case XK_Insert: return PUGL_KEY_INSERT;
  187. case XK_Shift_L: return PUGL_KEY_SHIFT;
  188. case XK_Shift_R: return PUGL_KEY_SHIFT;
  189. case XK_Control_L: return PUGL_KEY_CTRL;
  190. case XK_Control_R: return PUGL_KEY_CTRL;
  191. case XK_Alt_L: return PUGL_KEY_ALT;
  192. case XK_Alt_R: return PUGL_KEY_ALT;
  193. case XK_Super_L: return PUGL_KEY_SUPER;
  194. case XK_Super_R: return PUGL_KEY_SUPER;
  195. }
  196. return (PuglKey)0;
  197. }
  198. static void
  199. setModifiers(PuglView* view, int xstate)
  200. {
  201. view->mods = 0;
  202. view->mods |= (xstate & ShiftMask) ? PUGL_MOD_SHIFT : 0;
  203. view->mods |= (xstate & ControlMask) ? PUGL_MOD_CTRL : 0;
  204. view->mods |= (xstate & Mod1Mask) ? PUGL_MOD_ALT : 0;
  205. view->mods |= (xstate & Mod4Mask) ? PUGL_MOD_SUPER : 0;
  206. }
  207. PuglStatus
  208. puglProcessEvents(PuglView* view)
  209. {
  210. XEvent event;
  211. while (XPending(view->impl->display) > 0) {
  212. XNextEvent(view->impl->display, &event);
  213. switch (event.type) {
  214. case MapNotify:
  215. puglReshape(view, view->width, view->height);
  216. break;
  217. case ConfigureNotify:
  218. if ((event.xconfigure.width != view->width) ||
  219. (event.xconfigure.height != view->height)) {
  220. puglReshape(view,
  221. event.xconfigure.width,
  222. event.xconfigure.height);
  223. }
  224. break;
  225. case Expose:
  226. if (event.xexpose.count != 0) {
  227. break;
  228. }
  229. puglDisplay(view);
  230. view->redisplay = false;
  231. break;
  232. case MotionNotify:
  233. setModifiers(view, (int)event.xmotion.state);
  234. if (view->motionFunc) {
  235. view->motionFunc(view, event.xmotion.x, event.xmotion.y);
  236. }
  237. break;
  238. case ButtonPress:
  239. setModifiers(view, (int)event.xbutton.state);
  240. if (event.xbutton.button >= 4 && event.xbutton.button <= 7) {
  241. if (view->scrollFunc) {
  242. float dx = 0, dy = 0;
  243. switch (event.xbutton.button) {
  244. case 4: dy = 1.0f; break;
  245. case 5: dy = -1.0f; break;
  246. case 6: dx = -1.0f; break;
  247. case 7: dx = 1.0f; break;
  248. }
  249. view->scrollFunc(view, dx, dy);
  250. }
  251. break;
  252. }
  253. // nobreak
  254. case ButtonRelease:
  255. setModifiers(view, (int)event.xbutton.state);
  256. if (view->mouseFunc &&
  257. (event.xbutton.button < 4 || event.xbutton.button > 7)) {
  258. view->mouseFunc(view,
  259. (int)event.xbutton.button, event.type == ButtonPress,
  260. event.xbutton.x, event.xbutton.y);
  261. }
  262. break;
  263. case KeyPress: {
  264. setModifiers(view, (int)event.xkey.state);
  265. KeySym sym;
  266. char str[5];
  267. int n = XLookupString(&event.xkey, str, 4, &sym, nullptr);
  268. PuglKey key = keySymToSpecial(sym);
  269. if (!key && view->keyboardFunc) {
  270. if (n == 1) {
  271. view->keyboardFunc(view, true, (uint32_t)str[0]);
  272. } else {
  273. fprintf(stderr, "warning: Unknown key %X\n", (int)sym);
  274. }
  275. } else if (view->specialFunc) {
  276. view->specialFunc(view, true, key);
  277. }
  278. } break;
  279. case KeyRelease: {
  280. setModifiers(view, (int)event.xkey.state);
  281. bool repeated = false;
  282. if (view->ignoreKeyRepeat &&
  283. XEventsQueued(view->impl->display, QueuedAfterReading)) {
  284. XEvent next;
  285. XPeekEvent(view->impl->display, &next);
  286. if (next.type == KeyPress &&
  287. next.xkey.time == event.xkey.time &&
  288. next.xkey.keycode == event.xkey.keycode) {
  289. XNextEvent(view->impl->display, &event);
  290. repeated = true;
  291. }
  292. }
  293. if (!repeated && view->keyboardFunc) {
  294. KeySym sym = XKeycodeToKeysym(
  295. view->impl->display, (KeyCode)event.xkey.keycode, 0);
  296. PuglKey special = keySymToSpecial(sym);
  297. if (!special) {
  298. view->keyboardFunc(view, false, (uint32_t)sym);
  299. } else if (view->specialFunc) {
  300. view->specialFunc(view, false, special);
  301. }
  302. }
  303. } break;
  304. case ClientMessage:
  305. if (!strcmp(XGetAtomName(view->impl->display,
  306. event.xclient.message_type),
  307. "WM_PROTOCOLS")) {
  308. if (view->closeFunc) {
  309. view->closeFunc(view);
  310. }
  311. }
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. if (view->redisplay) {
  318. puglDisplay(view);
  319. }
  320. return PUGL_SUCCESS;
  321. }
  322. void
  323. puglPostRedisplay(PuglView* view)
  324. {
  325. view->redisplay = true;
  326. }
  327. PuglNativeWindow
  328. puglGetNativeWindow(PuglView* view)
  329. {
  330. return static_cast<PuglNativeWindow>(view->impl->win);
  331. }
  332. PuglInternals*
  333. puglGetInternalsImpl(PuglView* view)
  334. {
  335. return view->impl;
  336. }