nuklear_xlib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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, struct 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 <string.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <X11/Xlib.h>
  51. #include <X11/Xutil.h>
  52. #include <X11/Xresource.h>
  53. #include <X11/Xlocale.h>
  54. #include <X11/extensions/XShm.h>
  55. #include <sys/ipc.h>
  56. #include <sys/shm.h>
  57. static struct {
  58. struct nk_context ctx;
  59. struct XSurface *surf;
  60. Cursor cursor;
  61. Display *dpy;
  62. Window root;
  63. XImage *ximg;
  64. XShmSegmentInfo xsi;
  65. char fallback;
  66. GC gc;
  67. } xlib;
  68. NK_API int
  69. nk_xlib_init(Display *dpy, Visual *vis, int screen, Window root,
  70. unsigned int w, unsigned int h, void **fb, struct rawfb_pl *pl)
  71. {
  72. unsigned int depth = XDefaultDepth(dpy, screen);
  73. xlib.dpy = dpy;
  74. xlib.root = root;
  75. if (!setlocale(LC_ALL,"")) return 0;
  76. if (!XSupportsLocale()) return 0;
  77. if (!XSetLocaleModifiers("@im=none")) return 0;
  78. /* create invisible cursor */
  79. {static XColor dummy; char data[1] = {0};
  80. Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
  81. if (blank == None) return 0;
  82. xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
  83. XFreePixmap(dpy, blank);}
  84. xlib.fallback = False;
  85. do {/* Initialize shared memory according to:
  86. * https://www.x.org/archive/X11R7.5/doc/Xext/mit-shm.html */
  87. int status;
  88. if (!XShmQueryExtension(dpy)) {
  89. printf("No XShm Extension available.\n");
  90. xlib.fallback = True;
  91. break;
  92. }
  93. xlib.ximg = XShmCreateImage(dpy, vis, depth, ZPixmap, NULL, &xlib.xsi, w, h);
  94. if (!xlib.ximg) {
  95. xlib.fallback = True;
  96. break;
  97. }
  98. xlib.xsi.shmid = shmget(IPC_PRIVATE, xlib.ximg->bytes_per_line * xlib.ximg->height, IPC_CREAT | 0777);
  99. if (xlib.xsi.shmid < 0) {
  100. XDestroyImage(xlib.ximg);
  101. xlib.fallback = True;
  102. break;
  103. }
  104. xlib.xsi.shmaddr = xlib.ximg->data = shmat(xlib.xsi.shmid, NULL, 0);
  105. if ((intptr_t)xlib.xsi.shmaddr < 0) {
  106. XDestroyImage(xlib.ximg);
  107. xlib.fallback = True;
  108. break;
  109. }
  110. xlib.xsi.readOnly = False;
  111. status = XShmAttach(dpy, &xlib.xsi);
  112. if (!status) {
  113. shmdt(xlib.xsi.shmaddr);
  114. XDestroyImage(xlib.ximg);
  115. xlib.fallback = True;
  116. break;
  117. } XSync(dpy, False);
  118. shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
  119. } while(0);
  120. if (xlib.fallback) {
  121. xlib.ximg = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL, w, h, 32, 0);
  122. if (!xlib.ximg) return 0;
  123. xlib.ximg->data = malloc(h * xlib.ximg->bytes_per_line);
  124. if (!xlib.ximg->data)
  125. return 0;
  126. }
  127. xlib.gc = XDefaultGC(dpy, screen);
  128. *fb = xlib.ximg->data;
  129. pl->bytesPerPixel = xlib.ximg->bits_per_pixel / 8;
  130. pl->rshift = __builtin_ctzl(xlib.ximg->red_mask);
  131. pl->gshift = __builtin_ctzl(xlib.ximg->green_mask);
  132. pl->bshift = __builtin_ctzl(xlib.ximg->blue_mask);
  133. pl->ashift = 0;
  134. pl->rloss = 8 - __builtin_popcount(xlib.ximg->red_mask);
  135. pl->gloss = 8 - __builtin_popcount(xlib.ximg->green_mask);
  136. pl->bloss = 8 - __builtin_popcount(xlib.ximg->blue_mask);
  137. pl->aloss = 8;
  138. return 1;
  139. }
  140. NK_API int
  141. nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt, struct rawfb_context *rawfb)
  142. {
  143. /* optional grabbing behavior */
  144. if (rawfb->ctx.input.mouse.grab) {
  145. /* XDefineCursor(xlib.dpy, xlib.root, xlib.cursor); */
  146. rawfb->ctx.input.mouse.grab = 0;
  147. } else if (rawfb->ctx.input.mouse.ungrab) {
  148. XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0,
  149. (int)rawfb->ctx.input.mouse.prev.x, (int)rawfb->ctx.input.mouse.prev.y);
  150. /* XUndefineCursor(xlib.dpy, xlib.root); */
  151. rawfb->ctx.input.mouse.ungrab = 0;
  152. }
  153. if (evt->type == KeyPress || evt->type == KeyRelease)
  154. {
  155. /* Key handler */
  156. int ret, down = (evt->type == KeyPress);
  157. KeySym *code = XGetKeyboardMapping(xlib.dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  158. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(&rawfb->ctx, NK_KEY_SHIFT, down);
  159. else if (*code == XK_Control_L || *code == XK_Control_R) nk_input_key(&rawfb->ctx, NK_KEY_CTRL, down);
  160. else if (*code == XK_Delete) nk_input_key(&rawfb->ctx, NK_KEY_DEL, down);
  161. else if (*code == XK_Return) nk_input_key(&rawfb->ctx, NK_KEY_ENTER, down);
  162. else if (*code == XK_Tab) nk_input_key(&rawfb->ctx, NK_KEY_TAB, down);
  163. else if (*code == XK_Left) nk_input_key(&rawfb->ctx, NK_KEY_LEFT, down);
  164. else if (*code == XK_Right) nk_input_key(&rawfb->ctx, NK_KEY_RIGHT, down);
  165. else if (*code == XK_Up) nk_input_key(&rawfb->ctx, NK_KEY_UP, down);
  166. else if (*code == XK_Down) nk_input_key(&rawfb->ctx, NK_KEY_DOWN, down);
  167. else if (*code == XK_BackSpace) nk_input_key(&rawfb->ctx, NK_KEY_BACKSPACE, down);
  168. else if (*code == XK_Escape) nk_input_key(&rawfb->ctx, NK_KEY_TEXT_RESET_MODE, down);
  169. else if (*code == XK_Page_Up) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_UP, down);
  170. else if (*code == XK_Page_Down) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_DOWN, down);
  171. else if (*code == XK_Home) {
  172. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_START, down);
  173. nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_START, down);
  174. } else if (*code == XK_End) {
  175. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_END, down);
  176. nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_END, down);
  177. } else {
  178. if (*code == 'c' && (evt->xkey.state & ControlMask))
  179. nk_input_key(&rawfb->ctx, NK_KEY_COPY, down);
  180. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  181. nk_input_key(&rawfb->ctx, NK_KEY_PASTE, down);
  182. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  183. nk_input_key(&rawfb->ctx, NK_KEY_CUT, down);
  184. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  185. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_UNDO, down);
  186. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  187. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REDO, down);
  188. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  189. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_LEFT, down);
  190. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  191. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  192. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  193. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_START, down);
  194. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  195. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_END, down);
  196. else {
  197. if (*code == 'i')
  198. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_INSERT_MODE, down);
  199. else if (*code == 'r')
  200. nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REPLACE_MODE, down);
  201. if (down) {
  202. char buf[32];
  203. KeySym keysym = 0;
  204. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  205. nk_input_glyph(&rawfb->ctx, buf);
  206. }
  207. }
  208. } XFree(code);
  209. return 1;
  210. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  211. /* Button handler */
  212. int down = (evt->type == ButtonPress);
  213. const int x = evt->xbutton.x, y = evt->xbutton.y;
  214. if (evt->xbutton.button == Button1)
  215. nk_input_button(&rawfb->ctx, NK_BUTTON_LEFT, x, y, down);
  216. if (evt->xbutton.button == Button2)
  217. nk_input_button(&rawfb->ctx, NK_BUTTON_MIDDLE, x, y, down);
  218. else if (evt->xbutton.button == Button3)
  219. nk_input_button(&rawfb->ctx, NK_BUTTON_RIGHT, x, y, down);
  220. else if (evt->xbutton.button == Button4)
  221. nk_input_scroll(&rawfb->ctx, nk_vec2(0, 1.0f));
  222. else if (evt->xbutton.button == Button5)
  223. nk_input_scroll(&rawfb->ctx, nk_vec2(0, -1.0f));
  224. else return 0;
  225. return 1;
  226. } else if (evt->type == MotionNotify) {
  227. /* Mouse motion handler */
  228. const int x = evt->xmotion.x, y = evt->xmotion.y;
  229. nk_input_motion(&rawfb->ctx, x, y);
  230. if (rawfb->ctx.input.mouse.grabbed) {
  231. rawfb->ctx.input.mouse.pos.x = rawfb->ctx.input.mouse.prev.x;
  232. rawfb->ctx.input.mouse.pos.y = rawfb->ctx.input.mouse.prev.y;
  233. XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0, (int)rawfb->ctx.input.mouse.pos.x, (int)rawfb->ctx.input.mouse.pos.y);
  234. } return 1;
  235. } else if (evt->type == Expose || evt->type == ConfigureNotify) {
  236. /* Window resize handler */
  237. void *fb;
  238. struct rawfb_pl pl;
  239. unsigned int width, height;
  240. XWindowAttributes attr;
  241. XGetWindowAttributes(dpy, win, &attr);
  242. width = (unsigned int)attr.width;
  243. height = (unsigned int)attr.height;
  244. nk_xlib_shutdown();
  245. nk_xlib_init(dpy, XDefaultVisual(dpy, screen), screen, win, width, height, &fb, &pl);
  246. nk_rawfb_resize_fb(rawfb, fb, width, height, width * 4, pl);
  247. } else if (evt->type == KeymapNotify) {
  248. XRefreshKeyboardMapping(&evt->xmapping);
  249. return 1;
  250. } else if (evt->type == LeaveNotify) {
  251. XUndefineCursor(xlib.dpy, xlib.root);
  252. } else if (evt->type == EnterNotify) {
  253. XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
  254. } return 0;
  255. }
  256. NK_API void
  257. nk_xlib_shutdown(void)
  258. {
  259. XFreeCursor(xlib.dpy, xlib.cursor);
  260. if (xlib.fallback) {
  261. free(xlib.ximg->data);
  262. XDestroyImage(xlib.ximg);
  263. } else {
  264. XShmDetach(xlib.dpy, &xlib.xsi);
  265. XDestroyImage(xlib.ximg);
  266. shmdt(xlib.xsi.shmaddr);
  267. shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
  268. } memset(&xlib, 0, sizeof(xlib));
  269. }
  270. NK_API void
  271. nk_xlib_render(Drawable screen)
  272. {
  273. if (xlib.fallback)
  274. XPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
  275. 0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height);
  276. else XShmPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
  277. 0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height, False);
  278. }
  279. #endif