nuklear_xlib_gl2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. static int insert_toggle = 0;
  214. /* optional grabbing behavior */
  215. if (ctx->input.mouse.grab) {
  216. XDefineCursor(x11.dpy, x11.win, x11.cursor);
  217. ctx->input.mouse.grab = 0;
  218. } else if (ctx->input.mouse.ungrab) {
  219. XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0,
  220. (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
  221. XUndefineCursor(x11.dpy, x11.win);
  222. ctx->input.mouse.ungrab = 0;
  223. }
  224. if (evt->type == KeyPress || evt->type == KeyRelease)
  225. {
  226. /* Key handler */
  227. int ret, down = (evt->type == KeyPress);
  228. KeySym *code = XGetKeyboardMapping(x11.dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  229. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(ctx, NK_KEY_SHIFT, down);
  230. else if (*code == XK_Control_L || *code == XK_Control_R) nk_input_key(ctx, NK_KEY_CTRL, down);
  231. else if (*code == XK_Delete) nk_input_key(ctx, NK_KEY_DEL, down);
  232. else if (*code == XK_Return || *code == XK_KP_Enter) nk_input_key(ctx, NK_KEY_ENTER, down);
  233. else if (*code == XK_Tab) nk_input_key(ctx, NK_KEY_TAB, down);
  234. else if (*code == XK_Left) nk_input_key(ctx, NK_KEY_LEFT, down);
  235. else if (*code == XK_Right) nk_input_key(ctx, NK_KEY_RIGHT, down);
  236. else if (*code == XK_Up) nk_input_key(ctx, NK_KEY_UP, down);
  237. else if (*code == XK_Down) nk_input_key(ctx, NK_KEY_DOWN, down);
  238. else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  239. else if (*code == XK_Escape) nk_input_key(ctx, NK_KEY_TEXT_RESET_MODE, down);
  240. else if (*code == XK_Page_Up) nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
  241. else if (*code == XK_Page_Down) nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  242. else if (*code == XK_Home) {
  243. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  244. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  245. } else if (*code == XK_End) {
  246. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  247. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  248. } else {
  249. if (*code == 'c' && (evt->xkey.state & ControlMask))
  250. nk_input_key(ctx, NK_KEY_COPY, down);
  251. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  252. nk_input_key(ctx, NK_KEY_PASTE, down);
  253. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  254. nk_input_key(ctx, NK_KEY_CUT, down);
  255. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  256. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down);
  257. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  258. nk_input_key(ctx, NK_KEY_TEXT_REDO, down);
  259. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  260. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  261. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  262. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  263. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  264. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
  265. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  266. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
  267. else if (*code == XK_Insert) {
  268. if (down) insert_toggle = !insert_toggle;
  269. if (insert_toggle) {
  270. nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down);
  271. } else {
  272. nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down);
  273. }
  274. } else {
  275. if (down) {
  276. char buf[32];
  277. KeySym keysym = 0;
  278. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  279. nk_input_glyph(ctx, buf);
  280. }
  281. }
  282. }
  283. XFree(code);
  284. return 1;
  285. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  286. /* Button handler */
  287. int down = (evt->type == ButtonPress);
  288. const int x = evt->xbutton.x, y = evt->xbutton.y;
  289. if (evt->xbutton.button == Button1) {
  290. if (down) { /* Double-Click Button handler */
  291. float dt = nk_get_time() - x11.last_button_click;
  292. if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
  293. nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
  294. x11.last_button_click = nk_get_time();
  295. } else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
  296. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  297. } else if (evt->xbutton.button == Button2)
  298. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  299. else if (evt->xbutton.button == Button3)
  300. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  301. else if (evt->xbutton.button == Button4)
  302. nk_input_scroll(ctx, nk_vec2(0,1.0f));
  303. else if (evt->xbutton.button == Button5)
  304. nk_input_scroll(ctx, nk_vec2(0,-1.0f));
  305. else return 0;
  306. return 1;
  307. } else if (evt->type == MotionNotify) {
  308. /* Mouse motion handler */
  309. const int x = evt->xmotion.x, y = evt->xmotion.y;
  310. nk_input_motion(ctx, x, y);
  311. if (ctx->input.mouse.grabbed) {
  312. ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
  313. ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
  314. XWarpPointer(x11.dpy, None, x11.win, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
  315. }
  316. return 1;
  317. } else if (evt->type == KeymapNotify) {
  318. XRefreshKeyboardMapping(&evt->xmapping);
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. NK_API struct nk_context*
  324. nk_x11_init(Display *dpy, Window win)
  325. {
  326. x11.dpy = dpy;
  327. x11.win = win;
  328. if (!setlocale(LC_ALL,"")) return 0;
  329. if (!XSupportsLocale()) return 0;
  330. if (!XSetLocaleModifiers("@im=none")) return 0;
  331. /* create invisible cursor */
  332. {static XColor dummy; char data[1] = {0};
  333. Pixmap blank = XCreateBitmapFromData(dpy, win, data, 1, 1);
  334. if (blank == None) return 0;
  335. x11.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
  336. XFreePixmap(dpy, blank);}
  337. nk_buffer_init_default(&x11.ogl.cmds);
  338. nk_init_default(&x11.ctx, 0);
  339. x11.time_of_last_frame = nk_get_time();
  340. return &x11.ctx;
  341. }
  342. NK_API void
  343. nk_x11_shutdown(void)
  344. {
  345. struct nk_x11_device *dev = &x11.ogl;
  346. nk_font_atlas_clear(&x11.atlas);
  347. nk_free(&x11.ctx);
  348. glDeleteTextures(1, &dev->font_tex);
  349. nk_buffer_free(&dev->cmds);
  350. XFreeCursor(x11.dpy, x11.cursor);
  351. memset(&x11, 0, sizeof(x11));
  352. }
  353. #endif