nuklear_xlib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2016-2017 Patrick Rudolph <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Based on x11/nuklear_xlib.h.
  23. */
  24. /*
  25. * ==============================================================
  26. *
  27. * API
  28. *
  29. * ===============================================================
  30. */
  31. #ifndef NK_XLIBSHM_H_
  32. #define NK_XLIBSHM_H_
  33. #include <X11/Xlib.h>
  34. NK_API int nk_xlib_init(Display *dpy, Visual *vis, int screen, Window root, unsigned int w, unsigned int h, void **fb, rawfb_pl *pl);
  35. NK_API int nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt, struct rawfb_context *rawfb);
  36. NK_API void nk_xlib_render(Drawable screen);
  37. NK_API void nk_xlib_shutdown(void);
  38. #endif
  39. /*
  40. * ==============================================================
  41. *
  42. * IMPLEMENTATION
  43. *
  44. * ===============================================================
  45. */
  46. #ifdef NK_XLIBSHM_IMPLEMENTATION
  47. #include <X11/Xlib.h>
  48. #include <X11/Xutil.h>
  49. #include <X11/Xresource.h>
  50. #include <X11/Xlocale.h>
  51. #include <X11/extensions/XShm.h>
  52. #include <sys/ipc.h>
  53. #include <sys/shm.h>
  54. static struct {
  55. struct nk_context ctx;
  56. struct XSurface *surf;
  57. Cursor cursor;
  58. Display *dpy;
  59. Window root;
  60. XImage *ximg;
  61. XShmSegmentInfo xsi;
  62. char fallback;
  63. GC gc;
  64. } xlib;
  65. NK_API int
  66. nk_xlib_init(Display *dpy, Visual *vis, int screen, Window root,
  67. unsigned int w, unsigned int h, void **fb, rawfb_pl *pl)
  68. {
  69. unsigned int depth = XDefaultDepth(dpy, screen);
  70. xlib.dpy = dpy;
  71. xlib.root = root;
  72. if (!setlocale(LC_ALL,"")) return 0;
  73. if (!XSupportsLocale()) return 0;
  74. if (!XSetLocaleModifiers("@im=none")) return 0;
  75. /* create invisible cursor */
  76. {static XColor dummy; char data[1] = {0};
  77. Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
  78. if (blank == None) return 0;
  79. xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
  80. XFreePixmap(dpy, blank);}
  81. xlib.fallback = False;
  82. do {/* Initialize shared memory according to:
  83. * https://www.x.org/archive/X11R7.5/doc/Xext/mit-shm.html */
  84. int status;
  85. if (!XShmQueryExtension(dpy)) {
  86. printf("No XShm Extension available.\n");
  87. xlib.fallback = True;
  88. break;
  89. }
  90. xlib.ximg = XShmCreateImage(dpy, vis, depth, ZPixmap, NULL, &xlib.xsi, w, h);
  91. if (!xlib.ximg) {
  92. xlib.fallback = True;
  93. break;
  94. }
  95. xlib.xsi.shmid = shmget(IPC_PRIVATE, xlib.ximg->bytes_per_line * xlib.ximg->height, IPC_CREAT | 0777);
  96. if (xlib.xsi.shmid < 0) {
  97. XDestroyImage(xlib.ximg);
  98. xlib.fallback = True;
  99. break;
  100. }
  101. xlib.xsi.shmaddr = xlib.ximg->data = shmat(xlib.xsi.shmid, NULL, 0);
  102. if ((intptr_t)xlib.xsi.shmaddr < 0) {
  103. XDestroyImage(xlib.ximg);
  104. xlib.fallback = True;
  105. break;
  106. }
  107. xlib.xsi.readOnly = False;
  108. status = XShmAttach(dpy, &xlib.xsi);
  109. if (!status) {
  110. shmdt(xlib.xsi.shmaddr);
  111. XDestroyImage(xlib.ximg);
  112. xlib.fallback = True;
  113. break;
  114. } XSync(dpy, False);
  115. shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
  116. } while(0);
  117. if (xlib.fallback) {
  118. xlib.ximg = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL, w, h, 32, 0);
  119. if (!xlib.ximg) return 0;
  120. xlib.ximg->data = malloc(h * xlib.ximg->bytes_per_line);
  121. if (!xlib.ximg->data)
  122. return 0;
  123. }
  124. xlib.gc = XDefaultGC(dpy, screen);
  125. *fb = xlib.ximg->data;
  126. if (xlib.ximg->red_mask == 0xff0000 &&
  127. xlib.ximg->green_mask == 0xff00 &&
  128. xlib.ximg->blue_mask == 0xff &&
  129. xlib.ximg->bits_per_pixel == 32) {
  130. *pl = PIXEL_LAYOUT_XRGB_8888;
  131. }
  132. else if (xlib.ximg->red_mask == 0xff000000 &&
  133. xlib.ximg->green_mask == 0xff0000 &&
  134. xlib.ximg->blue_mask == 0xff00 &&
  135. xlib.ximg->bits_per_pixel == 32) {
  136. *pl = PIXEL_LAYOUT_RGBX_8888;
  137. }
  138. else {
  139. perror("nk_xlib_init(): Unrecognized pixel layout.\n");
  140. return 0;
  141. }
  142. return 1;
  143. }
  144. NK_API int
  145. nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt, struct rawfb_context *rawfb)
  146. {
  147. /* optional grabbing behavior */
  148. if (rawfb->ctx.input.mouse.grab) {
  149. /* XDefineCursor(xlib.dpy, xlib.root, xlib.cursor); */
  150. rawfb->ctx.input.mouse.grab = 0;
  151. } else if (rawfb->ctx.input.mouse.ungrab) {
  152. XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0,
  153. (int)rawfb->ctx.input.mouse.prev.x, (int)rawfb->ctx.input.mouse.prev.y);
  154. /* XUndefineCursor(xlib.dpy, xlib.root); */
  155. rawfb->ctx.input.mouse.ungrab = 0;
  156. }
  157. if (evt->type == KeyPress || evt->type == KeyRelease)
  158. {
  159. /* Key handler */
  160. int ret, down = (evt->type == KeyPress);
  161. KeySym *code = XGetKeyboardMapping(xlib.dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  162. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(&rawfb->ctx, NK_KEY_SHIFT, down);
  163. else if (*code == XK_Control_L || *code == XK_Control_R) nk_input_key(&rawfb->ctx, NK_KEY_CTRL, down);
  164. else if (*code == XK_Delete) nk_input_key(&rawfb->ctx, NK_KEY_DEL, down);
  165. else if (*code == XK_Return) nk_input_key(&rawfb->ctx, NK_KEY_ENTER, down);
  166. else if (*code == XK_Tab) nk_input_key(&rawfb->ctx, NK_KEY_TAB, down);
  167. else if (*code == XK_Left) nk_input_key(&rawfb->ctx, NK_KEY_LEFT, down);
  168. else if (*code == XK_Right) nk_input_key(&rawfb->ctx, NK_KEY_RIGHT, down);
  169. else if (*code == XK_Up) nk_input_key(&rawfb->ctx, NK_KEY_UP, down);
  170. else if (*code == XK_Down) nk_input_key(&rawfb->ctx, NK_KEY_DOWN, down);
  171. else if (*code == XK_BackSpace) nk_input_key(&rawfb->ctx, NK_KEY_BACKSPACE, down);
  172. else if (*code == XK_Escape) nk_input_key(&rawfb->ctx, NK_KEY_TEXT_RESET_MODE, down);
  173. else if (*code == XK_Page_Up) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_UP, down);
  174. else if (*code == XK_Page_Down) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_DOWN, down);
  175. else if (*code == XK_Home) {
  176. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_START, down);
  177. nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_START, down);
  178. } else if (*code == XK_End) {
  179. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_END, down);
  180. nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_END, down);
  181. } else {
  182. if (*code == 'c' && (evt->xkey.state & ControlMask))
  183. nk_input_key(&rawfb->ctx, NK_KEY_COPY, down);
  184. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  185. nk_input_key(&rawfb->ctx, NK_KEY_PASTE, down);
  186. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  187. nk_input_key(&rawfb->ctx, NK_KEY_CUT, down);
  188. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  189. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_UNDO, down);
  190. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  191. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REDO, down);
  192. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  193. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_LEFT, down);
  194. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  195. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  196. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  197. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_START, down);
  198. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  199. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_END, down);
  200. else {
  201. if (*code == 'i')
  202. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_INSERT_MODE, down);
  203. else if (*code == 'r')
  204. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REPLACE_MODE, down);
  205. if (down) {
  206. char buf[32];
  207. KeySym keysym = 0;
  208. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  209. nk_input_glyph(&rawfb->ctx, buf);
  210. }
  211. }
  212. } XFree(code);
  213. return 1;
  214. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  215. /* Button handler */
  216. int down = (evt->type == ButtonPress);
  217. const int x = evt->xbutton.x, y = evt->xbutton.y;
  218. if (evt->xbutton.button == Button1)
  219. nk_input_button(&rawfb->ctx, NK_BUTTON_LEFT, x, y, down);
  220. if (evt->xbutton.button == Button2)
  221. nk_input_button(&rawfb->ctx, NK_BUTTON_MIDDLE, x, y, down);
  222. else if (evt->xbutton.button == Button3)
  223. nk_input_button(&rawfb->ctx, NK_BUTTON_RIGHT, x, y, down);
  224. else if (evt->xbutton.button == Button4)
  225. nk_input_scroll(&rawfb->ctx, nk_vec2(0, 1.0f));
  226. else if (evt->xbutton.button == Button5)
  227. nk_input_scroll(&rawfb->ctx, nk_vec2(0, -1.0f));
  228. else return 0;
  229. return 1;
  230. } else if (evt->type == MotionNotify) {
  231. /* Mouse motion handler */
  232. const int x = evt->xmotion.x, y = evt->xmotion.y;
  233. nk_input_motion(&rawfb->ctx, x, y);
  234. if (rawfb->ctx.input.mouse.grabbed) {
  235. rawfb->ctx.input.mouse.pos.x = rawfb->ctx.input.mouse.prev.x;
  236. rawfb->ctx.input.mouse.pos.y = rawfb->ctx.input.mouse.prev.y;
  237. XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0, (int)rawfb->ctx.input.mouse.pos.x, (int)rawfb->ctx.input.mouse.pos.y);
  238. } return 1;
  239. } else if (evt->type == Expose || evt->type == ConfigureNotify) {
  240. /* Window resize handler */
  241. void *fb;
  242. rawfb_pl pl;
  243. unsigned int width, height;
  244. XWindowAttributes attr;
  245. XGetWindowAttributes(dpy, win, &attr);
  246. width = (unsigned int)attr.width;
  247. height = (unsigned int)attr.height;
  248. nk_xlib_shutdown();
  249. nk_xlib_init(dpy, XDefaultVisual(dpy, screen), screen, win, width, height, &fb, &pl);
  250. nk_rawfb_resize_fb(rawfb, fb, width, height, width * 4, pl);
  251. } else if (evt->type == KeymapNotify) {
  252. XRefreshKeyboardMapping(&evt->xmapping);
  253. return 1;
  254. } else if (evt->type == LeaveNotify) {
  255. XUndefineCursor(xlib.dpy, xlib.root);
  256. } else if (evt->type == EnterNotify) {
  257. XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
  258. } return 0;
  259. }
  260. NK_API void
  261. nk_xlib_shutdown(void)
  262. {
  263. XFreeCursor(xlib.dpy, xlib.cursor);
  264. if (xlib.fallback) {
  265. free(xlib.ximg->data);
  266. XDestroyImage(xlib.ximg);
  267. } else {
  268. XShmDetach(xlib.dpy, &xlib.xsi);
  269. XDestroyImage(xlib.ximg);
  270. shmdt(xlib.xsi.shmaddr);
  271. shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
  272. } memset(&xlib, 0, sizeof(xlib));
  273. }
  274. NK_API void
  275. nk_xlib_render(Drawable screen)
  276. {
  277. if (xlib.fallback)
  278. XPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
  279. 0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height);
  280. else XShmPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
  281. 0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height, False);
  282. }
  283. #endif