nuklear_xlib_gl2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Nuklear - 1.32.0 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2016 by Micha Mettke
  5. */
  6. /*
  7. * ==============================================================
  8. *
  9. * API
  10. *
  11. * ===============================================================
  12. */
  13. #ifndef NK_XLIB_GL2_H_
  14. #define NK_XLIB_GL2_H_
  15. #include <X11/Xlib.h>
  16. NK_API struct nk_context* nk_x11_init(Display *dpy, Window win);
  17. NK_API void nk_x11_font_stash_begin(struct nk_font_atlas **atlas);
  18. NK_API void nk_x11_font_stash_end(void);
  19. NK_API int nk_x11_handle_event(XEvent *evt);
  20. NK_API void nk_x11_render(enum nk_anti_aliasing, int max_vertex_buffer, int max_element_buffer);
  21. NK_API void nk_x11_shutdown(void);
  22. #endif
  23. /*
  24. * ==============================================================
  25. *
  26. * IMPLEMENTATION
  27. *
  28. * ===============================================================
  29. */
  30. #ifdef NK_XLIB_GL2_IMPLEMENTATION
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/time.h>
  35. #include <unistd.h>
  36. #include <time.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/Xutil.h>
  39. #include <X11/Xresource.h>
  40. #include <X11/Xlocale.h>
  41. #include <GL/gl.h>
  42. #ifndef NK_X11_DOUBLE_CLICK_LO
  43. #define NK_X11_DOUBLE_CLICK_LO 0.02
  44. #endif
  45. #ifndef NK_X11_DOUBLE_CLICK_HI
  46. #define NK_X11_DOUBLE_CLICK_HI 0.20
  47. #endif
  48. struct nk_x11_vertex {
  49. float position[2];
  50. float uv[2];
  51. nk_byte col[4];
  52. };
  53. struct nk_x11_device {
  54. struct nk_buffer cmds;
  55. struct nk_draw_null_texture tex_null;
  56. GLuint font_tex;
  57. };
  58. static struct nk_x11 {
  59. struct nk_x11_device ogl;
  60. struct nk_context ctx;
  61. struct nk_font_atlas atlas;
  62. Cursor cursor;
  63. Display *dpy;
  64. Window win;
  65. double last_button_click;
  66. double time_of_last_frame;
  67. } x11;
  68. NK_INTERN double
  69. nk_get_time(void)
  70. {
  71. struct timeval tv;
  72. if (gettimeofday(&tv, NULL) < 0) return 0;
  73. return ((double)tv.tv_sec + (double)tv.tv_usec/1000000);
  74. }
  75. NK_INTERN void
  76. nk_x11_device_upload_atlas(const void *image, int width, int height)
  77. {
  78. struct nk_x11_device *dev = &x11.ogl;
  79. glGenTextures(1, &dev->font_tex);
  80. glBindTexture(GL_TEXTURE_2D, dev->font_tex);
  81. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  82. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  83. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
  84. GL_RGBA, GL_UNSIGNED_BYTE, image);
  85. }
  86. NK_API void
  87. nk_x11_render(enum nk_anti_aliasing AA, int max_vertex_buffer, int max_element_buffer)
  88. {
  89. /* setup global state */
  90. struct nk_x11_device *dev = &x11.ogl;
  91. int width, height;
  92. XWindowAttributes attr;
  93. double now = nk_get_time();
  94. x11.ctx.delta_time_seconds = now - x11.time_of_last_frame;
  95. x11.time_of_last_frame = now;
  96. NK_UNUSED(max_vertex_buffer);
  97. NK_UNUSED(max_element_buffer);
  98. XGetWindowAttributes(x11.dpy, x11.win, &attr);
  99. width = attr.width;
  100. height = attr.height;
  101. glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT|GL_TRANSFORM_BIT);
  102. glDisable(GL_CULL_FACE);
  103. glDisable(GL_DEPTH_TEST);
  104. glEnable(GL_SCISSOR_TEST);
  105. glEnable(GL_BLEND);
  106. glEnable(GL_TEXTURE_2D);
  107. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  108. /* setup viewport/project */
  109. glViewport(0,0,(GLsizei)width,(GLsizei)height);
  110. glMatrixMode(GL_PROJECTION);
  111. glPushMatrix();
  112. glLoadIdentity();
  113. glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
  114. glMatrixMode(GL_MODELVIEW);
  115. glPushMatrix();
  116. glLoadIdentity();
  117. glEnableClientState(GL_VERTEX_ARRAY);
  118. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  119. glEnableClientState(GL_COLOR_ARRAY);
  120. {
  121. GLsizei vs = sizeof(struct nk_x11_vertex);
  122. size_t vp = offsetof(struct nk_x11_vertex, position);
  123. size_t vt = offsetof(struct nk_x11_vertex, uv);
  124. size_t vc = offsetof(struct nk_x11_vertex, col);
  125. /* convert from command queue into draw list and draw to screen */
  126. const struct nk_draw_command *cmd;
  127. const nk_draw_index *offset = NULL;
  128. struct nk_buffer vbuf, ebuf;
  129. /* fill convert configuration */
  130. struct nk_convert_config config;
  131. static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  132. {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_x11_vertex, position)},
  133. {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct nk_x11_vertex, uv)},
  134. {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct nk_x11_vertex, col)},
  135. {NK_VERTEX_LAYOUT_END}
  136. };
  137. memset(&config, 0, sizeof(config));
  138. config.vertex_layout = vertex_layout;
  139. config.vertex_size = sizeof(struct nk_x11_vertex);
  140. config.vertex_alignment = NK_ALIGNOF(struct nk_x11_vertex);
  141. config.tex_null = dev->tex_null;
  142. config.circle_segment_count = 22;
  143. config.curve_segment_count = 22;
  144. config.arc_segment_count = 22;
  145. config.global_alpha = 1.0f;
  146. config.shape_AA = AA;
  147. config.line_AA = AA;
  148. /* convert shapes into vertexes */
  149. nk_buffer_init_default(&vbuf);
  150. nk_buffer_init_default(&ebuf);
  151. nk_convert(&x11.ctx, &dev->cmds, &vbuf, &ebuf, &config);
  152. /* setup vertex buffer pointer */
  153. {const void *vertices = nk_buffer_memory_const(&vbuf);
  154. glVertexPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vp));
  155. glTexCoordPointer(2, GL_FLOAT, vs, (const void*)((const nk_byte*)vertices + vt));
  156. glColorPointer(4, GL_UNSIGNED_BYTE, vs, (const void*)((const nk_byte*)vertices + vc));}
  157. /* iterate over and execute each draw command */
  158. offset = (const nk_draw_index*)nk_buffer_memory_const(&ebuf);
  159. nk_draw_foreach(cmd, &x11.ctx, &dev->cmds)
  160. {
  161. if (!cmd->elem_count) continue;
  162. glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
  163. glScissor(
  164. (GLint)(cmd->clip_rect.x),
  165. (GLint)((height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h))),
  166. (GLint)(cmd->clip_rect.w),
  167. (GLint)(cmd->clip_rect.h));
  168. glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset);
  169. offset += cmd->elem_count;
  170. }
  171. nk_clear(&x11.ctx);
  172. nk_buffer_clear(&dev->cmds);
  173. nk_buffer_free(&vbuf);
  174. nk_buffer_free(&ebuf);
  175. }
  176. /* default OpenGL state */
  177. glDisableClientState(GL_VERTEX_ARRAY);
  178. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  179. glDisableClientState(GL_COLOR_ARRAY);
  180. glDisable(GL_CULL_FACE);
  181. glDisable(GL_DEPTH_TEST);
  182. glDisable(GL_SCISSOR_TEST);
  183. glDisable(GL_BLEND);
  184. glDisable(GL_TEXTURE_2D);
  185. glBindTexture(GL_TEXTURE_2D, 0);
  186. glMatrixMode(GL_MODELVIEW);
  187. glPopMatrix();
  188. glMatrixMode(GL_PROJECTION);
  189. glPopMatrix();
  190. glPopAttrib();
  191. }
  192. NK_API void
  193. nk_x11_font_stash_begin(struct nk_font_atlas **atlas)
  194. {
  195. nk_font_atlas_init_default(&x11.atlas);
  196. nk_font_atlas_begin(&x11.atlas);
  197. *atlas = &x11.atlas;
  198. }
  199. NK_API void
  200. nk_x11_font_stash_end(void)
  201. {
  202. const void *image; int w, h;
  203. image = nk_font_atlas_bake(&x11.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
  204. nk_x11_device_upload_atlas(image, w, h);
  205. nk_font_atlas_end(&x11.atlas, nk_handle_id((int)x11.ogl.font_tex), &x11.ogl.tex_null);
  206. if (x11.atlas.default_font)
  207. nk_style_set_font(&x11.ctx, &x11.atlas.default_font->handle);
  208. }
  209. NK_API int
  210. nk_x11_handle_event(XEvent *evt)
  211. {
  212. struct nk_context *ctx = &x11.ctx;
  213. /* optional grabbing behavior */
  214. if (ctx->input.mouse.grab) {
  215. XDefineCursor(x11.dpy, x11.win, x11.cursor);
  216. ctx->input.mouse.grab = 0;
  217. } else if (ctx->input.mouse.ungrab) {
  218. XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0,
  219. (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
  220. XUndefineCursor(x11.dpy, x11.win);
  221. ctx->input.mouse.ungrab = 0;
  222. }
  223. if (evt->type == KeyPress || evt->type == KeyRelease)
  224. {
  225. /* Key handler */
  226. int ret, down = (evt->type == KeyPress);
  227. KeySym *code = XGetKeyboardMapping(x11.dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  228. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(ctx, NK_KEY_SHIFT, down);
  229. else if (*code == XK_Control_L || *code == XK_Control_R) nk_input_key(ctx, NK_KEY_CTRL, down);
  230. else if (*code == XK_Delete) nk_input_key(ctx, NK_KEY_DEL, down);
  231. else if (*code == XK_Return || *code == XK_KP_Enter) nk_input_key(ctx, NK_KEY_ENTER, down);
  232. else if (*code == XK_Tab) nk_input_key(ctx, NK_KEY_TAB, down);
  233. else if (*code == XK_Left) nk_input_key(ctx, NK_KEY_LEFT, down);
  234. else if (*code == XK_Right) nk_input_key(ctx, NK_KEY_RIGHT, down);
  235. else if (*code == XK_Up) nk_input_key(ctx, NK_KEY_UP, down);
  236. else if (*code == XK_Down) nk_input_key(ctx, NK_KEY_DOWN, down);
  237. else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  238. else if (*code == XK_space && !down) nk_input_char(ctx, ' ');
  239. else if (*code == XK_Page_Up) nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
  240. else if (*code == XK_Page_Down) nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  241. else if (*code == XK_Home) {
  242. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  243. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  244. } else if (*code == XK_End) {
  245. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  246. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  247. } else {
  248. if (*code == 'c' && (evt->xkey.state & ControlMask))
  249. nk_input_key(ctx, NK_KEY_COPY, down);
  250. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  251. nk_input_key(ctx, NK_KEY_PASTE, down);
  252. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  253. nk_input_key(ctx, NK_KEY_CUT, down);
  254. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  255. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down);
  256. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  257. nk_input_key(ctx, NK_KEY_TEXT_REDO, down);
  258. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  259. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  260. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  261. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  262. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  263. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
  264. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  265. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
  266. else {
  267. if (*code == 'i')
  268. nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down);
  269. else if (*code == 'r')
  270. nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down);
  271. if (down) {
  272. char buf[32];
  273. KeySym keysym = 0;
  274. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  275. nk_input_glyph(ctx, buf);
  276. }
  277. }
  278. }
  279. XFree(code);
  280. return 1;
  281. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  282. /* Button handler */
  283. int down = (evt->type == ButtonPress);
  284. const int x = evt->xbutton.x, y = evt->xbutton.y;
  285. if (evt->xbutton.button == Button1) {
  286. if (down) { /* Double-Click Button handler */
  287. float dt = nk_get_time() - x11.last_button_click;
  288. if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
  289. nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
  290. x11.last_button_click = nk_get_time();
  291. } else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
  292. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  293. } else if (evt->xbutton.button == Button2)
  294. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  295. else if (evt->xbutton.button == Button3)
  296. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  297. else if (evt->xbutton.button == Button4)
  298. nk_input_scroll(ctx, nk_vec2(0,1.0f));
  299. else if (evt->xbutton.button == Button5)
  300. nk_input_scroll(ctx, nk_vec2(0,-1.0f));
  301. else return 0;
  302. return 1;
  303. } else if (evt->type == MotionNotify) {
  304. /* Mouse motion handler */
  305. const int x = evt->xmotion.x, y = evt->xmotion.y;
  306. nk_input_motion(ctx, x, y);
  307. if (ctx->input.mouse.grabbed) {
  308. ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
  309. ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
  310. XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
  311. }
  312. return 1;
  313. } else if (evt->type == KeymapNotify) {
  314. XRefreshKeyboardMapping(&evt->xmapping);
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. NK_API struct nk_context*
  320. nk_x11_init(Display *dpy, Window win)
  321. {
  322. x11.dpy = dpy;
  323. x11.win = win;
  324. if (!setlocale(LC_ALL,"")) return 0;
  325. if (!XSupportsLocale()) return 0;
  326. if (!XSetLocaleModifiers("@im=none")) return 0;
  327. /* create invisible cursor */
  328. {static XColor dummy; char data[1] = {0};
  329. Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
  330. if (blank == None) return 0;
  331. x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
  332. XFreePixmap(dpy, blank);}
  333. nk_buffer_init_default(&x11.ogl.cmds);
  334. nk_init_default(&x11.ctx, 0);
  335. x11.time_of_last_frame = nk_get_time();
  336. return &x11.ctx;
  337. }
  338. NK_API void
  339. nk_x11_shutdown(void)
  340. {
  341. struct nk_x11_device *dev = &x11.ogl;
  342. nk_font_atlas_clear(&x11.atlas);
  343. nk_free(&x11.ctx);
  344. glDeleteTextures(1, &dev->font_tex);
  345. nk_buffer_free(&dev->cmds);
  346. XFreeCursor(x11.dpy, x11.cursor);
  347. memset(&x11, 0, sizeof(x11));
  348. }
  349. #endif