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.

734 lines
18KB

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