1 /* nuklear - v1.32.0 - public domain */
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <limits.h>
8 #include <math.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include <time.h>
12 
13 #define NK_INCLUDE_FIXED_TYPES
14 #define NK_INCLUDE_STANDARD_IO
15 #define NK_INCLUDE_STANDARD_VARARGS
16 #define NK_INCLUDE_DEFAULT_ALLOCATOR
17 #define NK_IMPLEMENTATION
18 #define NK_XLIB_IMPLEMENTATION
19 #include "../../nuklear.h"
20 #include "nuklear_xlib.h"
21 
22 #define DTIME           20
23 #define WINDOW_WIDTH    800
24 #define WINDOW_HEIGHT   600
25 
26 typedef struct XWindow XWindow;
27 struct XWindow {
28     Display *dpy;
29     Window root;
30     Visual *vis;
31     Colormap cmap;
32     XWindowAttributes attr;
33     XSetWindowAttributes swa;
34     Window win;
35     int screen;
36     XFont *font;
37     unsigned int width;
38     unsigned int height;
39     Atom wm_delete_window;
40 };
41 
42 static void
die(const char * fmt,...)43 die(const char *fmt, ...)
44 {
45     va_list ap;
46     va_start(ap, fmt);
47     vfprintf(stderr, fmt, ap);
48     va_end(ap);
49     fputs("\n", stderr);
50     exit(EXIT_FAILURE);
51 }
52 
53 static long
timestamp(void)54 timestamp(void)
55 {
56     struct timeval tv;
57     if (gettimeofday(&tv, NULL) < 0) return 0;
58     return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
59 }
60 
61 static void
sleep_for(long t)62 sleep_for(long t)
63 {
64     struct timespec req;
65     const time_t sec = (int)(t/1000);
66     const long ms = t - (sec * 1000);
67     req.tv_sec = sec;
68     req.tv_nsec = ms * 1000000L;
69     while(-1 == nanosleep(&req, &req));
70 }
71 
72 /* ===============================================================
73  *
74  *                          EXAMPLE
75  *
76  * ===============================================================*/
77 /* This are some code examples to provide a small overview of what can be
78  * done with this library. To try out an example uncomment the defines */
79 /*#define INCLUDE_ALL */
80 /*#define INCLUDE_STYLE */
81 /*#define INCLUDE_CALCULATOR */
82 /*#define INCLUDE_OVERVIEW */
83 /*#define INCLUDE_NODE_EDITOR */
84 
85 #ifdef INCLUDE_ALL
86   #define INCLUDE_STYLE
87   #define INCLUDE_CALCULATOR
88   #define INCLUDE_OVERVIEW
89   #define INCLUDE_NODE_EDITOR
90 #endif
91 
92 #ifdef INCLUDE_STYLE
93   #include "../style.c"
94 #endif
95 #ifdef INCLUDE_CALCULATOR
96   #include "../calculator.c"
97 #endif
98 #ifdef INCLUDE_OVERVIEW
99   #include "../overview.c"
100 #endif
101 #ifdef INCLUDE_NODE_EDITOR
102   #include "../node_editor.c"
103 #endif
104 
105 /* ===============================================================
106  *
107  *                          DEMO
108  *
109  * ===============================================================*/
110 int
main(void)111 main(void)
112 {
113     long dt;
114     long started;
115     int running = 1;
116     XWindow xw;
117     struct nk_context *ctx;
118 
119     /* X11 */
120     memset(&xw, 0, sizeof xw);
121     xw.dpy = XOpenDisplay(NULL);
122     if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
123     xw.root = DefaultRootWindow(xw.dpy);
124     xw.screen = XDefaultScreen(xw.dpy);
125     xw.vis = XDefaultVisual(xw.dpy, xw.screen);
126     xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
127 
128     xw.swa.colormap = xw.cmap;
129     xw.swa.event_mask =
130         ExposureMask | KeyPressMask | KeyReleaseMask |
131         ButtonPress | ButtonReleaseMask| ButtonMotionMask |
132         Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
133         PointerMotionMask | KeymapStateMask;
134     xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
135         XDefaultDepth(xw.dpy, xw.screen), InputOutput,
136         xw.vis, CWEventMask | CWColormap, &xw.swa);
137 
138     XStoreName(xw.dpy, xw.win, "X11");
139     XMapWindow(xw.dpy, xw.win);
140     xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
141     XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
142     XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
143     xw.width = (unsigned int)xw.attr.width;
144     xw.height = (unsigned int)xw.attr.height;
145 
146     /* GUI */
147     xw.font = nk_xfont_create(xw.dpy, "fixed");
148     ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
149 
150     #ifdef INCLUDE_STYLE
151     /*set_style(ctx, THEME_WHITE);*/
152     /*set_style(ctx, THEME_RED);*/
153     /*set_style(ctx, THEME_BLUE);*/
154     /*set_style(ctx, THEME_DARK);*/
155     #endif
156 
157     while (running)
158     {
159         /* Input */
160         XEvent evt;
161         started = timestamp();
162         nk_input_begin(ctx);
163         while (XPending(xw.dpy)) {
164             XNextEvent(xw.dpy, &evt);
165             if (evt.type == ClientMessage) goto cleanup;
166             if (XFilterEvent(&evt, xw.win)) continue;
167             nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
168         }
169         nk_input_end(ctx);
170 
171         /* GUI */
172         if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
173             NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
174             NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
175         {
176             enum {EASY, HARD};
177             static int op = EASY;
178             static int property = 20;
179 
180             nk_layout_row_static(ctx, 30, 80, 1);
181             if (nk_button_label(ctx, "button"))
182                 fprintf(stdout, "button pressed\n");
183             nk_layout_row_dynamic(ctx, 30, 2);
184             if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
185             if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
186             nk_layout_row_dynamic(ctx, 25, 1);
187             nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
188         }
189         nk_end(ctx);
190         if (nk_window_is_hidden(ctx, "Demo")) break;
191 
192         /* -------------- EXAMPLES ---------------- */
193         #ifdef INCLUDE_CALCULATOR
194           calculator(ctx);
195         #endif
196         #ifdef INCLUDE_OVERVIEW
197           overview(ctx);
198         #endif
199         #ifdef INCLUDE_NODE_EDITOR
200           node_editor(ctx);
201         #endif
202         /* ----------------------------------------- */
203 
204         /* Draw */
205         XClearWindow(xw.dpy, xw.win);
206         nk_xlib_render(xw.win, nk_rgb(30,30,30));
207         XFlush(xw.dpy);
208 
209         /* Timing */
210         dt = timestamp() - started;
211         if (dt < DTIME)
212             sleep_for(DTIME - dt);
213     }
214 
215 cleanup:
216     nk_xfont_del(xw.dpy, xw.font);
217     nk_xlib_shutdown();
218     XUnmapWindow(xw.dpy, xw.win);
219     XFreeColormap(xw.dpy, xw.cmap);
220     XDestroyWindow(xw.dpy, xw.win);
221     XCloseDisplay(xw.dpy);
222     return 0;
223 }
224 
225