PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/nuklear/demo/x11/nuklear_xlib.h

https://gitlab.com/OpenMusicKontrollers/nk_pugl
C Header | 957 lines | 846 code | 81 blank | 30 comment | 217 complexity | ac1b1585cbcb37d2afe0228091386c06 MD5 | raw file
  1. /*
  2. * Nuklear - v1.40.8 - public domain
  3. * no warrenty implied; use at your own risk.
  4. * authored from 2015-2017 by Micha Mettke
  5. */
  6. /*
  7. * ==============================================================
  8. *
  9. * API
  10. *
  11. * ===============================================================
  12. */
  13. #ifndef NK_XLIB_H_
  14. #define NK_XLIB_H_
  15. #include <X11/Xlib.h>
  16. typedef struct XFont XFont;
  17. NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, unsigned w, unsigned h);
  18. NK_API int nk_xlib_handle_event(Display*, int scrn, Window, XEvent*);
  19. NK_API void nk_xlib_render(Drawable screen, struct nk_color clear);
  20. NK_API void nk_xlib_shutdown(void);
  21. NK_API void nk_xlib_set_font(XFont*);
  22. NK_API void nk_xlib_push_font(XFont*);
  23. NK_API void nk_xlib_paste(nk_handle, struct nk_text_edit*);
  24. NK_API void nk_xlib_copy(nk_handle, const char*, int len);
  25. /* Image */
  26. #ifdef NK_XLIB_INCLUDE_STB_IMAGE
  27. NK_API struct nk_image nk_xsurf_load_image_from_file(char const *filename);
  28. NK_API struct nk_image nk_xsurf_load_image_from_memory(const void *membuf, nk_uint membufSize);
  29. #endif
  30. /* Font */
  31. NK_API XFont* nk_xfont_create(Display *dpy, const char *name);
  32. NK_API void nk_xfont_del(Display *dpy, XFont *font);
  33. #endif
  34. /*
  35. * ==============================================================
  36. *
  37. * IMPLEMENTATION
  38. *
  39. * ===============================================================
  40. */
  41. #ifdef NK_XLIB_IMPLEMENTATION
  42. #include <X11/Xlib.h>
  43. #include <X11/Xutil.h>
  44. #include <X11/Xresource.h>
  45. #include <X11/Xlocale.h>
  46. #include <X11/Xatom.h>
  47. #include <sys/time.h>
  48. #include <unistd.h>
  49. #include <time.h>
  50. #ifdef NK_XLIB_IMPLEMENT_STB_IMAGE
  51. #define STB_IMAGE_IMPLEMENTATION
  52. #endif
  53. #ifdef NK_XLIB_INCLUDE_STB_IMAGE
  54. #include "../../example/stb_image.h"
  55. #endif
  56. #ifndef NK_X11_DOUBLE_CLICK_LO
  57. #define NK_X11_DOUBLE_CLICK_LO 20
  58. #endif
  59. #ifndef NK_X11_DOUBLE_CLICK_HI
  60. #define NK_X11_DOUBLE_CLICK_HI 200
  61. #endif
  62. typedef struct XSurface XSurface;
  63. typedef struct XImageWithAlpha XImageWithAlpha;
  64. struct XFont {
  65. int ascent;
  66. int descent;
  67. int height;
  68. XFontSet set;
  69. XFontStruct *xfont;
  70. struct nk_user_font handle;
  71. };
  72. struct XSurface {
  73. GC gc;
  74. Display *dpy;
  75. int screen;
  76. Window root;
  77. Drawable drawable;
  78. unsigned int w, h;
  79. };
  80. struct XImageWithAlpha {
  81. XImage* ximage;
  82. GC clipMaskGC;
  83. Pixmap clipMask;
  84. };
  85. static struct {
  86. char *clipboard_data;
  87. int clipboard_len;
  88. struct nk_text_edit* clipboard_target;
  89. Atom xa_clipboard;
  90. Atom xa_targets;
  91. Atom xa_text;
  92. Atom xa_utf8_string;
  93. struct nk_context ctx;
  94. struct XSurface *surf;
  95. Cursor cursor;
  96. Display *dpy;
  97. Window root;
  98. long last_button_click;
  99. } xlib;
  100. NK_INTERN long
  101. nk_timestamp(void)
  102. {
  103. struct timeval tv;
  104. if (gettimeofday(&tv, NULL) < 0) return 0;
  105. return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
  106. }
  107. NK_INTERN unsigned long
  108. nk_color_from_byte(const nk_byte *c)
  109. {
  110. unsigned long res = 0;
  111. res |= (unsigned long)c[0] << 16;
  112. res |= (unsigned long)c[1] << 8;
  113. res |= (unsigned long)c[2] << 0;
  114. return (res);
  115. }
  116. NK_INTERN XSurface*
  117. nk_xsurf_create(int screen, unsigned int w, unsigned int h)
  118. {
  119. XSurface *surface = (XSurface*)calloc(1, sizeof(XSurface));
  120. surface->w = w;
  121. surface->h = h;
  122. surface->dpy = xlib.dpy;
  123. surface->screen = screen;
  124. surface->root = xlib.root;
  125. surface->gc = XCreateGC(xlib.dpy, xlib.root, 0, NULL);
  126. XSetLineAttributes(xlib.dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
  127. surface->drawable = XCreatePixmap(xlib.dpy, xlib.root, w, h,
  128. (unsigned int)DefaultDepth(xlib.dpy, screen));
  129. return surface;
  130. }
  131. NK_INTERN void
  132. nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
  133. {
  134. if(!surf) return;
  135. if (surf->w == w && surf->h == h) return;
  136. surf->w = w; surf->h = h;
  137. if(surf->drawable) XFreePixmap(surf->dpy, surf->drawable);
  138. surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
  139. (unsigned int)DefaultDepth(surf->dpy, surf->screen));
  140. }
  141. NK_INTERN void
  142. nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
  143. {
  144. XRectangle clip_rect;
  145. clip_rect.x = (short)(x-1);
  146. clip_rect.y = (short)(y-1);
  147. clip_rect.width = (unsigned short)(w+2);
  148. clip_rect.height = (unsigned short)(h+2);
  149. XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);
  150. }
  151. NK_INTERN void
  152. nk_xsurf_stroke_line(XSurface *surf, short x0, short y0, short x1,
  153. short y1, unsigned int line_thickness, struct nk_color col)
  154. {
  155. unsigned long c = nk_color_from_byte(&col.r);
  156. XSetForeground(surf->dpy, surf->gc, c);
  157. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  158. XDrawLine(surf->dpy, surf->drawable, surf->gc, (int)x0, (int)y0, (int)x1, (int)y1);
  159. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  160. }
  161. NK_INTERN void
  162. nk_xsurf_stroke_rect(XSurface* surf, short x, short y, unsigned short w,
  163. unsigned short h, unsigned short r, unsigned short line_thickness, struct nk_color col)
  164. {
  165. unsigned long c = nk_color_from_byte(&col.r);
  166. XSetForeground(surf->dpy, surf->gc, c);
  167. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  168. if (r == 0) {XDrawRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);return;}
  169. {short xc = x + r;
  170. short yc = y + r;
  171. short wc = (short)(w - 2 * r);
  172. short hc = (short)(h - 2 * r);
  173. XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y, xc+wc, y);
  174. XDrawLine(surf->dpy, surf->drawable, surf->gc, x+w, yc, x+w, yc+hc);
  175. XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y+h, xc+wc, y+h);
  176. XDrawLine(surf->dpy, surf->drawable, surf->gc, x, yc, x, yc+hc);
  177. XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
  178. (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
  179. XDrawArc(surf->dpy, surf->drawable, surf->gc, x, y,
  180. (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
  181. XDrawArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
  182. (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
  183. XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
  184. (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
  185. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  186. }
  187. NK_INTERN void
  188. nk_xsurf_fill_rect(XSurface* surf, short x, short y, unsigned short w,
  189. unsigned short h, unsigned short r, struct nk_color col)
  190. {
  191. unsigned long c = nk_color_from_byte(&col.r);
  192. XSetForeground(surf->dpy, surf->gc, c);
  193. if (r == 0) {XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h); return;}
  194. {short xc = x + r;
  195. short yc = y + r;
  196. short wc = (short)(w - 2 * r);
  197. short hc = (short)(h - 2 * r);
  198. XPoint pnts[12];
  199. pnts[0].x = x;
  200. pnts[0].y = yc;
  201. pnts[1].x = xc;
  202. pnts[1].y = yc;
  203. pnts[2].x = xc;
  204. pnts[2].y = y;
  205. pnts[3].x = xc + wc;
  206. pnts[3].y = y;
  207. pnts[4].x = xc + wc;
  208. pnts[4].y = yc;
  209. pnts[5].x = x + w;
  210. pnts[5].y = yc;
  211. pnts[6].x = x + w;
  212. pnts[6].y = yc + hc;
  213. pnts[7].x = xc + wc;
  214. pnts[7].y = yc + hc;
  215. pnts[8].x = xc + wc;
  216. pnts[8].y = y + h;
  217. pnts[9].x = xc;
  218. pnts[9].y = y + h;
  219. pnts[10].x = xc;
  220. pnts[10].y = yc + hc;
  221. pnts[11].x = x;
  222. pnts[11].y = yc + hc;
  223. XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 12, Convex, CoordModeOrigin);
  224. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
  225. (unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
  226. XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
  227. (unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
  228. XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
  229. (unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
  230. XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
  231. (unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
  232. }
  233. NK_INTERN void
  234. nk_xsurf_fill_triangle(XSurface *surf, short x0, short y0, short x1,
  235. short y1, short x2, short y2, struct nk_color col)
  236. {
  237. XPoint pnts[3];
  238. unsigned long c = nk_color_from_byte(&col.r);
  239. pnts[0].x = (short)x0;
  240. pnts[0].y = (short)y0;
  241. pnts[1].x = (short)x1;
  242. pnts[1].y = (short)y1;
  243. pnts[2].x = (short)x2;
  244. pnts[2].y = (short)y2;
  245. XSetForeground(surf->dpy, surf->gc, c);
  246. XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 3, Convex, CoordModeOrigin);
  247. }
  248. NK_INTERN void
  249. nk_xsurf_stroke_triangle(XSurface *surf, short x0, short y0, short x1,
  250. short y1, short x2, short y2, unsigned short line_thickness, struct nk_color col)
  251. {
  252. unsigned long c = nk_color_from_byte(&col.r);
  253. XSetForeground(surf->dpy, surf->gc, c);
  254. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  255. XDrawLine(surf->dpy, surf->drawable, surf->gc, x0, y0, x1, y1);
  256. XDrawLine(surf->dpy, surf->drawable, surf->gc, x1, y1, x2, y2);
  257. XDrawLine(surf->dpy, surf->drawable, surf->gc, x2, y2, x0, y0);
  258. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  259. }
  260. NK_INTERN void
  261. nk_xsurf_fill_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
  262. struct nk_color col)
  263. {
  264. int i = 0;
  265. #define MAX_POINTS 128
  266. XPoint xpnts[MAX_POINTS];
  267. unsigned long c = nk_color_from_byte(&col.r);
  268. XSetForeground(surf->dpy, surf->gc, c);
  269. for (i = 0; i < count && i < MAX_POINTS; ++i) {
  270. xpnts[i].x = pnts[i].x;
  271. xpnts[i].y = pnts[i].y;
  272. }
  273. XFillPolygon(surf->dpy, surf->drawable, surf->gc, xpnts, count, Convex, CoordModeOrigin);
  274. #undef MAX_POINTS
  275. }
  276. NK_INTERN void
  277. nk_xsurf_stroke_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
  278. unsigned short line_thickness, struct nk_color col)
  279. {
  280. int i = 0;
  281. unsigned long c = nk_color_from_byte(&col.r);
  282. XSetForeground(surf->dpy, surf->gc, c);
  283. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  284. for (i = 1; i < count; ++i)
  285. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i-1].x, pnts[i-1].y, pnts[i].x, pnts[i].y);
  286. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[count-1].x, pnts[count-1].y, pnts[0].x, pnts[0].y);
  287. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  288. }
  289. NK_INTERN void
  290. nk_xsurf_stroke_polyline(XSurface *surf, const struct nk_vec2i *pnts,
  291. int count, unsigned short line_thickness, struct nk_color col)
  292. {
  293. int i = 0;
  294. unsigned long c = nk_color_from_byte(&col.r);
  295. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  296. XSetForeground(surf->dpy, surf->gc, c);
  297. for (i = 0; i < count-1; ++i)
  298. XDrawLine(surf->dpy, surf->drawable, surf->gc, pnts[i].x, pnts[i].y, pnts[i+1].x, pnts[i+1].y);
  299. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  300. }
  301. NK_INTERN void
  302. nk_xsurf_fill_circle(XSurface *surf, short x, short y, unsigned short w,
  303. unsigned short h, struct nk_color col)
  304. {
  305. unsigned long c = nk_color_from_byte(&col.r);
  306. XSetForeground(surf->dpy, surf->gc, c);
  307. XFillArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
  308. (unsigned)w, (unsigned)h, 0, 360 * 64);
  309. }
  310. NK_INTERN void
  311. nk_xsurf_stroke_circle(XSurface *surf, short x, short y, unsigned short w,
  312. unsigned short h, unsigned short line_thickness, struct nk_color col)
  313. {
  314. unsigned long c = nk_color_from_byte(&col.r);
  315. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  316. XSetForeground(surf->dpy, surf->gc, c);
  317. XDrawArc(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y,
  318. (unsigned)w, (unsigned)h, 0, 360 * 64);
  319. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  320. }
  321. NK_INTERN void
  322. nk_xsurf_stroke_curve(XSurface *surf, struct nk_vec2i p1,
  323. struct nk_vec2i p2, struct nk_vec2i p3, struct nk_vec2i p4,
  324. unsigned int num_segments, unsigned short line_thickness, struct nk_color col)
  325. {
  326. unsigned int i_step;
  327. float t_step;
  328. struct nk_vec2i last = p1;
  329. XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
  330. num_segments = NK_MAX(num_segments, 1);
  331. t_step = 1.0f/(float)num_segments;
  332. for (i_step = 1; i_step <= num_segments; ++i_step) {
  333. float t = t_step * (float)i_step;
  334. float u = 1.0f - t;
  335. float w1 = u*u*u;
  336. float w2 = 3*u*u*t;
  337. float w3 = 3*u*t*t;
  338. float w4 = t * t *t;
  339. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  340. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  341. nk_xsurf_stroke_line(surf, last.x, last.y, (short)x, (short)y, line_thickness,col);
  342. last.x = (short)x; last.y = (short)y;
  343. }
  344. XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
  345. }
  346. NK_INTERN void
  347. nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
  348. const char *text, int len, XFont *font, struct nk_color cbg, struct nk_color cfg)
  349. {
  350. int tx, ty;
  351. unsigned long bg = nk_color_from_byte(&cbg.r);
  352. unsigned long fg = nk_color_from_byte(&cfg.r);
  353. XSetForeground(surf->dpy, surf->gc, bg);
  354. XFillRectangle(surf->dpy, surf->drawable, surf->gc, (int)x, (int)y, (unsigned)w, (unsigned)h);
  355. if(!text || !font || !len) return;
  356. tx = (int)x;
  357. ty = (int)y + font->ascent;
  358. XSetForeground(surf->dpy, surf->gc, fg);
  359. if(font->set)
  360. XmbDrawString(surf->dpy,surf->drawable,font->set,surf->gc,tx,ty,(const char*)text,(int)len);
  361. else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
  362. }
  363. #ifdef NK_XLIB_INCLUDE_STB_IMAGE
  364. NK_INTERN struct nk_image
  365. nk_stbi_image_to_xsurf(unsigned char *data, int width, int height, int channels) {
  366. XSurface *surf = xlib.surf;
  367. struct nk_image img;
  368. int bpl = channels;
  369. long i, isize = width*height*channels;
  370. XImageWithAlpha *aimage = (XImageWithAlpha*)calloc( 1, sizeof(XImageWithAlpha) );
  371. int depth = DefaultDepth(surf->dpy, surf->screen);
  372. if (data == NULL) return nk_image_id(0);
  373. if (aimage == NULL) return nk_image_id(0);
  374. switch (depth){
  375. case 24:
  376. bpl = 4;
  377. break;
  378. case 16:
  379. case 15:
  380. bpl = 2;
  381. break;
  382. default:
  383. bpl = 1;
  384. break;
  385. }
  386. /* rgba to bgra */
  387. if (channels >= 3){
  388. for (i=0; i < isize; i += channels) {
  389. unsigned char red = data[i+2];
  390. unsigned char blue = data[i];
  391. data[i] = red;
  392. data[i+2] = blue;
  393. }
  394. }
  395. if (channels == 4){
  396. const unsigned alpha_treshold = 127;
  397. aimage->clipMask = XCreatePixmap(surf->dpy, surf->drawable, width, height, 1);
  398. if( aimage->clipMask ){
  399. aimage->clipMaskGC = XCreateGC(surf->dpy, aimage->clipMask, 0, 0);
  400. XSetForeground(surf->dpy, aimage->clipMaskGC, BlackPixel(surf->dpy, surf->screen));
  401. XFillRectangle(surf->dpy, aimage->clipMask, aimage->clipMaskGC, 0, 0, width, height);
  402. XSetForeground(surf->dpy, aimage->clipMaskGC, WhitePixel(surf->dpy, surf->screen));
  403. for (i=0; i < isize; i += channels){
  404. unsigned char alpha = data[i+3];
  405. int div = i / channels;
  406. int x = div % width;
  407. int y = div / width;
  408. if( alpha > alpha_treshold )
  409. XDrawPoint(surf->dpy, aimage->clipMask, aimage->clipMaskGC, x, y);
  410. }
  411. }
  412. }
  413. aimage->ximage = XCreateImage(surf->dpy,
  414. CopyFromParent, depth,
  415. ZPixmap, 0,
  416. (char*)data,
  417. width, height,
  418. bpl*8, bpl * width);
  419. img = nk_image_ptr( (void*)aimage);
  420. img.h = height;
  421. img.w = width;
  422. return img;
  423. }
  424. NK_API struct nk_image
  425. nk_xsurf_load_image_from_memory(const void *membuf, nk_uint membufSize)
  426. {
  427. int x,y,n;
  428. unsigned char *data;
  429. data = stbi_load_from_memory(membuf, membufSize, &x, &y, &n, 0);
  430. return nk_stbi_image_to_xsurf(data, x, y, n);
  431. }
  432. NK_API struct nk_image
  433. nk_xsurf_load_image_from_file(char const *filename)
  434. {
  435. int x,y,n;
  436. unsigned char *data;
  437. data = stbi_load(filename, &x, &y, &n, 0);
  438. return nk_stbi_image_to_xsurf(data, x, y, n);
  439. }
  440. #endif /* NK_XLIB_INCLUDE_STB_IMAGE */
  441. NK_INTERN void
  442. nk_xsurf_draw_image(XSurface *surf, short x, short y, unsigned short w, unsigned short h,
  443. struct nk_image img, struct nk_color col)
  444. {
  445. XImageWithAlpha *aimage = img.handle.ptr;
  446. if (aimage){
  447. if (aimage->clipMask){
  448. XSetClipMask(surf->dpy, surf->gc, aimage->clipMask);
  449. XSetClipOrigin(surf->dpy, surf->gc, x, y);
  450. }
  451. XPutImage(surf->dpy, surf->drawable, surf->gc, aimage->ximage, 0, 0, x, y, w, h);
  452. XSetClipMask(surf->dpy, surf->gc, None);
  453. }
  454. }
  455. void
  456. nk_xsurf_image_free(struct nk_image* image)
  457. {
  458. XSurface *surf = xlib.surf;
  459. XImageWithAlpha *aimage = image->handle.ptr;
  460. if (!aimage) return;
  461. XDestroyImage(aimage->ximage);
  462. XFreePixmap(surf->dpy, aimage->clipMask);
  463. XFreeGC(surf->dpy, aimage->clipMaskGC);
  464. free(aimage);
  465. }
  466. NK_INTERN void
  467. nk_xsurf_clear(XSurface *surf, unsigned long color)
  468. {
  469. XSetForeground(surf->dpy, surf->gc, color);
  470. XFillRectangle(surf->dpy, surf->drawable, surf->gc, 0, 0, surf->w, surf->h);
  471. }
  472. NK_INTERN void
  473. nk_xsurf_blit(Drawable target, XSurface *surf, unsigned int w, unsigned int h)
  474. {
  475. XCopyArea(surf->dpy, surf->drawable, target, surf->gc, 0, 0, w, h, 0, 0);
  476. }
  477. NK_INTERN void
  478. nk_xsurf_del(XSurface *surf)
  479. {
  480. XFreePixmap(surf->dpy, surf->drawable);
  481. XFreeGC(surf->dpy, surf->gc);
  482. free(surf);
  483. }
  484. NK_API XFont*
  485. nk_xfont_create(Display *dpy, const char *name)
  486. {
  487. int n;
  488. char *def, **missing;
  489. XFont *font = (XFont*)calloc(1, sizeof(XFont));
  490. font->set = XCreateFontSet(dpy, name, &missing, &n, &def);
  491. if(missing) {
  492. while(n--)
  493. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  494. XFreeStringList(missing);
  495. }
  496. if(font->set) {
  497. XFontStruct **xfonts;
  498. char **font_names;
  499. XExtentsOfFontSet(font->set);
  500. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  501. while(n--) {
  502. font->ascent = NK_MAX(font->ascent, (*xfonts)->ascent);
  503. font->descent = NK_MAX(font->descent,(*xfonts)->descent);
  504. xfonts++;
  505. }
  506. } else {
  507. if(!(font->xfont = XLoadQueryFont(dpy, name))
  508. && !(font->xfont = XLoadQueryFont(dpy, "fixed"))) {
  509. free(font);
  510. return 0;
  511. }
  512. font->ascent = font->xfont->ascent;
  513. font->descent = font->xfont->descent;
  514. }
  515. font->height = font->ascent + font->descent;
  516. return font;
  517. }
  518. NK_INTERN float
  519. nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int len)
  520. {
  521. XFont *font = (XFont*)handle.ptr;
  522. XRectangle r;
  523. if(!font || !text)
  524. return 0;
  525. if(font->set) {
  526. XmbTextExtents(font->set, (const char*)text, len, NULL, &r);
  527. return (float)r.width;
  528. } else{
  529. int w = XTextWidth(font->xfont, (const char*)text, len);
  530. return (float)w;
  531. }
  532. }
  533. NK_API void
  534. nk_xfont_del(Display *dpy, XFont *font)
  535. {
  536. if(!font) return;
  537. if(font->set)
  538. XFreeFontSet(dpy, font->set);
  539. else
  540. XFreeFont(dpy, font->xfont);
  541. free(font);
  542. }
  543. NK_API struct nk_context*
  544. nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
  545. unsigned int w, unsigned int h)
  546. {
  547. struct nk_user_font *font = &xfont->handle;
  548. font->userdata = nk_handle_ptr(xfont);
  549. font->height = (float)xfont->height;
  550. font->width = nk_xfont_get_text_width;
  551. xlib.dpy = dpy;
  552. xlib.root = root;
  553. if (!setlocale(LC_ALL,"")) return 0;
  554. if (!XSupportsLocale()) return 0;
  555. if (!XSetLocaleModifiers("@im=none")) return 0;
  556. xlib.xa_clipboard = XInternAtom(dpy, "CLIPBOARD", False);
  557. xlib.xa_targets = XInternAtom(dpy, "TARGETS", False);
  558. xlib.xa_text = XInternAtom(dpy, "TEXT", False);
  559. xlib.xa_utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
  560. /* create invisible cursor */
  561. {static XColor dummy; char data[1] = {0};
  562. Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
  563. if (blank == None) return 0;
  564. xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
  565. XFreePixmap(dpy, blank);}
  566. xlib.surf = nk_xsurf_create(screen, w, h);
  567. nk_init_default(&xlib.ctx, font);
  568. return &xlib.ctx;
  569. }
  570. NK_API void
  571. nk_xlib_set_font(XFont *xfont)
  572. {
  573. struct nk_user_font *font = &xfont->handle;
  574. font->userdata = nk_handle_ptr(xfont);
  575. font->height = (float)xfont->height;
  576. font->width = nk_xfont_get_text_width;
  577. nk_style_set_font(&xlib.ctx, font);
  578. }
  579. NK_API void
  580. nk_xlib_push_font(XFont *xfont)
  581. {
  582. struct nk_user_font *font = &xfont->handle;
  583. font->userdata = nk_handle_ptr(xfont);
  584. font->height = (float)xfont->height;
  585. font->width = nk_xfont_get_text_width;
  586. nk_style_push_font(&xlib.ctx, font);
  587. }
  588. NK_API void
  589. nk_xlib_paste(nk_handle handle, struct nk_text_edit* edit)
  590. {
  591. NK_UNUSED(handle);
  592. /* Paste in X is asynchronous, so can not use a temporary text edit */
  593. NK_ASSERT(edit != &xlib.ctx.text_edit && "Paste not supported for temporary editors");
  594. xlib.clipboard_target = edit;
  595. /* Request the contents of the primary buffer */
  596. XConvertSelection(xlib.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xlib.root, CurrentTime);
  597. }
  598. NK_API void
  599. nk_xlib_copy(nk_handle handle, const char* str, int len)
  600. {
  601. NK_UNUSED(handle);
  602. free(xlib.clipboard_data);
  603. xlib.clipboard_len = 0;
  604. xlib.clipboard_data = malloc((size_t)len);
  605. if (xlib.clipboard_data) {
  606. memcpy(xlib.clipboard_data, str, (size_t)len);
  607. xlib.clipboard_len = len;
  608. XSetSelectionOwner(xlib.dpy, XA_PRIMARY, xlib.root, CurrentTime);
  609. XSetSelectionOwner(xlib.dpy, xlib.xa_clipboard, xlib.root, CurrentTime);
  610. }
  611. }
  612. NK_API int
  613. nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
  614. {
  615. struct nk_context *ctx = &xlib.ctx;
  616. /* optional grabbing behavior */
  617. if (ctx->input.mouse.grab) {
  618. XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
  619. ctx->input.mouse.grab = 0;
  620. } else if (ctx->input.mouse.ungrab) {
  621. XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0,
  622. (int)ctx->input.mouse.prev.x, (int)ctx->input.mouse.prev.y);
  623. XUndefineCursor(xlib.dpy, xlib.root);
  624. ctx->input.mouse.ungrab = 0;
  625. }
  626. if (evt->type == KeyPress || evt->type == KeyRelease)
  627. {
  628. /* Key handler */
  629. int ret, down = (evt->type == KeyPress);
  630. KeySym *code = XGetKeyboardMapping(xlib.surf->dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
  631. if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(ctx, NK_KEY_SHIFT, down);
  632. else if (*code == XK_Delete) nk_input_key(ctx, NK_KEY_DEL, down);
  633. else if (*code == XK_Return) nk_input_key(ctx, NK_KEY_ENTER, down);
  634. else if (*code == XK_Tab) nk_input_key(ctx, NK_KEY_TAB, down);
  635. else if (*code == XK_Left) nk_input_key(ctx, NK_KEY_LEFT, down);
  636. else if (*code == XK_Right) nk_input_key(ctx, NK_KEY_RIGHT, down);
  637. else if (*code == XK_Up) nk_input_key(ctx, NK_KEY_UP, down);
  638. else if (*code == XK_Down) nk_input_key(ctx, NK_KEY_DOWN, down);
  639. else if (*code == XK_BackSpace) nk_input_key(ctx, NK_KEY_BACKSPACE, down);
  640. else if (*code == XK_Escape) nk_input_key(ctx, NK_KEY_TEXT_RESET_MODE, down);
  641. else if (*code == XK_Page_Up) nk_input_key(ctx, NK_KEY_SCROLL_UP, down);
  642. else if (*code == XK_Page_Down) nk_input_key(ctx, NK_KEY_SCROLL_DOWN, down);
  643. else if (*code == XK_Home) {
  644. nk_input_key(ctx, NK_KEY_TEXT_START, down);
  645. nk_input_key(ctx, NK_KEY_SCROLL_START, down);
  646. } else if (*code == XK_End) {
  647. nk_input_key(ctx, NK_KEY_TEXT_END, down);
  648. nk_input_key(ctx, NK_KEY_SCROLL_END, down);
  649. } else {
  650. if (*code == 'c' && (evt->xkey.state & ControlMask))
  651. nk_input_key(ctx, NK_KEY_COPY, down);
  652. else if (*code == 'v' && (evt->xkey.state & ControlMask))
  653. nk_input_key(ctx, NK_KEY_PASTE, down);
  654. else if (*code == 'x' && (evt->xkey.state & ControlMask))
  655. nk_input_key(ctx, NK_KEY_CUT, down);
  656. else if (*code == 'z' && (evt->xkey.state & ControlMask))
  657. nk_input_key(ctx, NK_KEY_TEXT_UNDO, down);
  658. else if (*code == 'r' && (evt->xkey.state & ControlMask))
  659. nk_input_key(ctx, NK_KEY_TEXT_REDO, down);
  660. else if (*code == XK_Left && (evt->xkey.state & ControlMask))
  661. nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down);
  662. else if (*code == XK_Right && (evt->xkey.state & ControlMask))
  663. nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down);
  664. else if (*code == 'b' && (evt->xkey.state & ControlMask))
  665. nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down);
  666. else if (*code == 'e' && (evt->xkey.state & ControlMask))
  667. nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down);
  668. else {
  669. if (*code == 'i')
  670. nk_input_key(ctx, NK_KEY_TEXT_INSERT_MODE, down);
  671. else if (*code == 'r')
  672. nk_input_key(ctx, NK_KEY_TEXT_REPLACE_MODE, down);
  673. if (down) {
  674. char buf[32];
  675. KeySym keysym = 0;
  676. if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
  677. nk_input_glyph(ctx, buf);
  678. }
  679. }
  680. }
  681. XFree(code);
  682. return 1;
  683. } else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
  684. /* Button handler */
  685. int down = (evt->type == ButtonPress);
  686. const int x = evt->xbutton.x, y = evt->xbutton.y;
  687. if (evt->xbutton.button == Button1) {
  688. if (down) { /* Double-Click Button handler */
  689. long dt = nk_timestamp() - xlib.last_button_click;
  690. if (dt > NK_X11_DOUBLE_CLICK_LO && dt < NK_X11_DOUBLE_CLICK_HI)
  691. nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_true);
  692. xlib.last_button_click = nk_timestamp();
  693. } else nk_input_button(ctx, NK_BUTTON_DOUBLE, x, y, nk_false);
  694. nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down);
  695. } else if (evt->xbutton.button == Button2)
  696. nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down);
  697. else if (evt->xbutton.button == Button3)
  698. nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
  699. else if (evt->xbutton.button == Button4)
  700. nk_input_scroll(ctx, nk_vec2(0, 1.0f));
  701. else if (evt->xbutton.button == Button5)
  702. nk_input_scroll(ctx, nk_vec2(0, -1.0f));
  703. else return 0;
  704. return 1;
  705. } else if (evt->type == MotionNotify) {
  706. /* Mouse motion handler */
  707. const int x = evt->xmotion.x, y = evt->xmotion.y;
  708. nk_input_motion(ctx, x, y);
  709. if (ctx->input.mouse.grabbed) {
  710. ctx->input.mouse.pos.x = ctx->input.mouse.prev.x;
  711. ctx->input.mouse.pos.y = ctx->input.mouse.prev.y;
  712. XWarpPointer(xlib.dpy, None, xlib.surf->root, 0, 0, 0, 0, (int)ctx->input.mouse.pos.x, (int)ctx->input.mouse.pos.y);
  713. }
  714. return 1;
  715. } else if (evt->type == Expose || evt->type == ConfigureNotify) {
  716. /* Window resize handler */
  717. unsigned int width, height;
  718. XWindowAttributes attr;
  719. XGetWindowAttributes(dpy, win, &attr);
  720. width = (unsigned int)attr.width;
  721. height = (unsigned int)attr.height;
  722. nk_xsurf_resize(xlib.surf, width, height);
  723. return 1;
  724. } else if (evt->type == KeymapNotify) {
  725. XRefreshKeyboardMapping(&evt->xmapping);
  726. return 1;
  727. } else if (evt->type == SelectionClear) {
  728. free(xlib.clipboard_data);
  729. xlib.clipboard_data = NULL;
  730. xlib.clipboard_len = 0;
  731. return 1;
  732. } else if (evt->type == SelectionRequest) {
  733. XEvent reply;
  734. reply.xselection.type = SelectionNotify;
  735. reply.xselection.requestor = evt->xselectionrequest.requestor;
  736. reply.xselection.selection = evt->xselectionrequest.selection;
  737. reply.xselection.target = evt->xselectionrequest.target;
  738. reply.xselection.property = None; /* Default refuse */
  739. reply.xselection.time = evt->xselectionrequest.time;
  740. if (reply.xselection.target == xlib.xa_targets) {
  741. Atom target_list[4];
  742. target_list[0] = xlib.xa_targets;
  743. target_list[1] = xlib.xa_text;
  744. target_list[2] = xlib.xa_utf8_string;
  745. target_list[3] = XA_STRING;
  746. reply.xselection.property = evt->xselectionrequest.property;
  747. XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
  748. reply.xselection.property, XA_ATOM, 32, PropModeReplace,
  749. (unsigned char*)&target_list, 4);
  750. } else if (xlib.clipboard_data && (reply.xselection.target == xlib.xa_text ||
  751. reply.xselection.target == xlib.xa_utf8_string || reply.xselection.target == XA_STRING)) {
  752. reply.xselection.property = evt->xselectionrequest.property;
  753. XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
  754. reply.xselection.property, reply.xselection.target, 8, PropModeReplace,
  755. (unsigned char*)xlib.clipboard_data, xlib.clipboard_len);
  756. }
  757. XSendEvent(evt->xselection.display, evt->xselectionrequest.requestor, True, 0, &reply);
  758. XFlush(evt->xselection.display);
  759. return 1;
  760. } else if (evt->type == SelectionNotify && xlib.clipboard_target) {
  761. if ((evt->xselection.target != XA_STRING) &&
  762. (evt->xselection.target != xlib.xa_utf8_string) &&
  763. (evt->xselection.target != xlib.xa_text))
  764. return 1;
  765. {Atom actual_type;
  766. int actual_format;
  767. unsigned long pos = 0, len, remain;
  768. unsigned char* data = 0;
  769. do {
  770. XGetWindowProperty(dpy, win, XA_PRIMARY, (int)pos, 1024, False,
  771. AnyPropertyType, &actual_type, &actual_format, &len, &remain, &data);
  772. if (len && data)
  773. nk_textedit_text(xlib.clipboard_target, (char*)data, (int)len);
  774. if (data != 0) XFree(data);
  775. pos += (len * (unsigned long)actual_format) / 32;
  776. } while (remain != 0);}
  777. return 1;
  778. }
  779. return 0;
  780. }
  781. NK_API void
  782. nk_xlib_shutdown(void)
  783. {
  784. nk_xsurf_del(xlib.surf);
  785. nk_free(&xlib.ctx);
  786. XFreeCursor(xlib.dpy, xlib.cursor);
  787. nk_memset(&xlib, 0, sizeof(xlib));
  788. }
  789. NK_API void
  790. nk_xlib_render(Drawable screen, struct nk_color clear)
  791. {
  792. const struct nk_command *cmd;
  793. struct nk_context *ctx = &xlib.ctx;
  794. XSurface *surf = xlib.surf;
  795. nk_xsurf_clear(xlib.surf, nk_color_from_byte(&clear.r));
  796. nk_foreach(cmd, &xlib.ctx)
  797. {
  798. switch (cmd->type) {
  799. case NK_COMMAND_NOP: break;
  800. case NK_COMMAND_SCISSOR: {
  801. const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
  802. nk_xsurf_scissor(surf, s->x, s->y, s->w, s->h);
  803. } break;
  804. case NK_COMMAND_LINE: {
  805. const struct nk_command_line *l = (const struct nk_command_line *)cmd;
  806. nk_xsurf_stroke_line(surf, l->begin.x, l->begin.y, l->end.x,
  807. l->end.y, l->line_thickness, l->color);
  808. } break;
  809. case NK_COMMAND_RECT: {
  810. const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
  811. nk_xsurf_stroke_rect(surf, r->x, r->y, NK_MAX(r->w -r->line_thickness, 0),
  812. NK_MAX(r->h - r->line_thickness, 0), (unsigned short)r->rounding,
  813. r->line_thickness, r->color);
  814. } break;
  815. case NK_COMMAND_RECT_FILLED: {
  816. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
  817. nk_xsurf_fill_rect(surf, r->x, r->y, r->w, r->h,
  818. (unsigned short)r->rounding, r->color);
  819. } break;
  820. case NK_COMMAND_CIRCLE: {
  821. const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
  822. nk_xsurf_stroke_circle(surf, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
  823. } break;
  824. case NK_COMMAND_CIRCLE_FILLED: {
  825. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  826. nk_xsurf_fill_circle(surf, c->x, c->y, c->w, c->h, c->color);
  827. } break;
  828. case NK_COMMAND_TRIANGLE: {
  829. const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
  830. nk_xsurf_stroke_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
  831. t->c.x, t->c.y, t->line_thickness, t->color);
  832. } break;
  833. case NK_COMMAND_TRIANGLE_FILLED: {
  834. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
  835. nk_xsurf_fill_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
  836. t->c.x, t->c.y, t->color);
  837. } break;
  838. case NK_COMMAND_POLYGON: {
  839. const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
  840. nk_xsurf_stroke_polygon(surf, p->points, p->point_count, p->line_thickness,p->color);
  841. } break;
  842. case NK_COMMAND_POLYGON_FILLED: {
  843. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
  844. nk_xsurf_fill_polygon(surf, p->points, p->point_count, p->color);
  845. } break;
  846. case NK_COMMAND_POLYLINE: {
  847. const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
  848. nk_xsurf_stroke_polyline(surf, p->points, p->point_count, p->line_thickness, p->color);
  849. } break;
  850. case NK_COMMAND_TEXT: {
  851. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  852. nk_xsurf_draw_text(surf, t->x, t->y, t->w, t->h,
  853. (const char*)t->string, t->length,
  854. (XFont*)t->font->userdata.ptr,
  855. t->background, t->foreground);
  856. } break;
  857. case NK_COMMAND_CURVE: {
  858. const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
  859. nk_xsurf_stroke_curve(surf, q->begin, q->ctrl[0], q->ctrl[1],
  860. q->end, 22, q->line_thickness, q->color);
  861. } break;
  862. case NK_COMMAND_IMAGE: {
  863. const struct nk_command_image *i = (const struct nk_command_image *)cmd;
  864. nk_xsurf_draw_image(surf, i->x, i->y, i->w, i->h, i->img, i->col);
  865. } break;
  866. case NK_COMMAND_RECT_MULTI_COLOR:
  867. case NK_COMMAND_ARC:
  868. case NK_COMMAND_ARC_FILLED:
  869. case NK_COMMAND_CUSTOM:
  870. default: break;
  871. }
  872. }
  873. nk_clear(ctx);
  874. nk_xsurf_blit(screen, surf, surf->w, surf->h);
  875. }
  876. #endif