Collection of tools useful for audio production
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.

385 lines
11KB

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