1 /*
2 * MIT License
3 *
4 * Copyright (c) 2016-2017 Patrick Rudolph <siro@das-labor.org>
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
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Based on x11/main.c.
25 *
26 */
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <sys/time.h>
35 #include <unistd.h>
36 #include <time.h>
37
38 #define RAWFB_RGBX_8888
39 #define NK_INCLUDE_FIXED_TYPES
40 #define NK_INCLUDE_STANDARD_IO
41 #define NK_INCLUDE_STANDARD_VARARGS
42 #define NK_INCLUDE_DEFAULT_ALLOCATOR
43 #define NK_IMPLEMENTATION
44 #define NK_XLIBSHM_IMPLEMENTATION
45 #define NK_RAWFB_IMPLEMENTATION
46 #define NK_INCLUDE_FONT_BAKING
47 #define NK_INCLUDE_DEFAULT_FONT
48 #define NK_INCLUDE_SOFTWARE_FONT
49
50 #include "../../nuklear.h"
51 #include "nuklear_rawfb.h"
52 #include "nuklear_xlib.h"
53
54 #define DTIME 20
55 #define WINDOW_WIDTH 800
56 #define WINDOW_HEIGHT 600
57
58 #define UNUSED(a) (void)a
59 #define MIN(a,b) ((a) < (b) ? (a) : (b))
60 #define MAX(a,b) ((a) < (b) ? (b) : (a))
61 #define LEN(a) (sizeof(a)/sizeof(a)[0])
62
63 typedef struct XWindow XWindow;
64 struct XWindow {
65 Display *dpy;
66 Window root;
67 Visual *vis;
68 Colormap cmap;
69 XWindowAttributes attr;
70 XSetWindowAttributes swa;
71 Window win;
72 int screen;
73 unsigned int width;
74 unsigned int height;
75 };
76
77 static void
die(const char * fmt,...)78 die(const char *fmt, ...)
79 {
80 va_list ap;
81 va_start(ap, fmt);
82 vfprintf(stderr, fmt, ap);
83 va_end(ap);
84 fputs("\n", stderr);
85 exit(EXIT_FAILURE);
86 }
87
88 static long
timestamp(void)89 timestamp(void)
90 {
91 struct timeval tv;
92 if (gettimeofday(&tv, NULL) < 0) return 0;
93 return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
94 }
95
96 static void
sleep_for(long t)97 sleep_for(long t)
98 {
99 struct timespec req;
100 const time_t sec = (int)(t/1000);
101 const long ms = t - (sec * 1000);
102 req.tv_sec = sec;
103 req.tv_nsec = ms * 1000000L;
104 while(-1 == nanosleep(&req, &req));
105 }
106
107 /* ===============================================================
108 *
109 * EXAMPLE
110 *
111 * ===============================================================*/
112 /* This are some code examples to provide a small overview of what can be
113 * done with this library. To try out an example uncomment the defines */
114 /*#define INCLUDE_ALL */
115 /*#define INCLUDE_STYLE */
116 /*#define INCLUDE_CALCULATOR */
117 /*#define INCLUDE_OVERVIEW */
118 /*#define INCLUDE_NODE_EDITOR */
119
120 #ifdef INCLUDE_ALL
121 #define INCLUDE_STYLE
122 #define INCLUDE_CALCULATOR
123 #define INCLUDE_OVERVIEW
124 #define INCLUDE_NODE_EDITOR
125 #endif
126
127 #ifdef INCLUDE_STYLE
128 #include "../style.c"
129 #endif
130 #ifdef INCLUDE_CALCULATOR
131 #include "../calculator.c"
132 #endif
133 #ifdef INCLUDE_OVERVIEW
134 #include "../overview.c"
135 #endif
136 #ifdef INCLUDE_NODE_EDITOR
137 #include "../node_editor.c"
138 #endif
139
140 /* ===============================================================
141 *
142 * DEMO
143 *
144 * ===============================================================*/
145 int
main(void)146 main(void)
147 {
148 long dt;
149 long started;
150 int running = 1;
151 int status;
152 XWindow xw;
153 struct rawfb_context *rawfb;
154 void *fb = NULL;
155 unsigned char tex_scratch[512 * 512];
156
157 /* X11 */
158 memset(&xw, 0, sizeof xw);
159 xw.dpy = XOpenDisplay(NULL);
160 if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
161
162 xw.root = DefaultRootWindow(xw.dpy);
163 xw.screen = XDefaultScreen(xw.dpy);
164 xw.vis = XDefaultVisual(xw.dpy, xw.screen);
165 xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
166 xw.swa.colormap = xw.cmap;
167 xw.swa.event_mask =
168 ExposureMask | KeyPressMask | KeyReleaseMask |
169 ButtonPress | ButtonReleaseMask| ButtonMotionMask |
170 Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
171 PointerMotionMask | KeymapStateMask | EnterWindowMask | LeaveWindowMask;
172 xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
173 XDefaultDepth(xw.dpy, xw.screen), InputOutput,
174 xw.vis, CWEventMask | CWColormap, &xw.swa);
175
176 XStoreName(xw.dpy, xw.win, "X11");
177 XMapWindow(xw.dpy, xw.win);
178 XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
179 xw.width = (unsigned int)xw.attr.width;
180 xw.height = (unsigned int)xw.attr.height;
181
182 /* Framebuffer emulator */
183 status = nk_xlib_init(xw.dpy, xw.vis, xw.screen, xw.win, xw.width, xw.height, &fb);
184 if (!status || !fb)
185 return 0;
186
187 /* GUI */
188 rawfb = nk_rawfb_init(fb, tex_scratch, xw.width, xw.height, xw.width * 4);
189 if (!rawfb) running = 0;
190
191 #ifdef INCLUDE_STYLE
192 /*set_style(ctx, THEME_WHITE);*/
193 /*set_style(ctx, THEME_RED);*/
194 /*set_style(ctx, THEME_BLUE);*/
195 /*set_style(ctx, THEME_DARK);*/
196 #endif
197
198 while (running) {
199 /* Input */
200 XEvent evt;
201 started = timestamp();
202 nk_input_begin(&rawfb->ctx);
203 while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
204 if (XFilterEvent(&evt, xw.win)) continue;
205 nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt, rawfb);
206 }
207 nk_input_end(&rawfb->ctx);
208
209 /* GUI */
210 if (nk_begin(&rawfb->ctx, "Demo", nk_rect(50, 50, 200, 200),
211 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
212 NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
213 enum {EASY, HARD};
214 static int op = EASY;
215 static int property = 20;
216
217 nk_layout_row_static(&rawfb->ctx, 30, 80, 1);
218 if (nk_button_label(&rawfb->ctx, "button"))
219 fprintf(stdout, "button pressed\n");
220 nk_layout_row_dynamic(&rawfb->ctx, 30, 2);
221 if (nk_option_label(&rawfb->ctx, "easy", op == EASY)) op = EASY;
222 if (nk_option_label(&rawfb->ctx, "hard", op == HARD)) op = HARD;
223 nk_layout_row_dynamic(&rawfb->ctx, 25, 1);
224 nk_property_int(&rawfb->ctx, "Compression:", 0, &property, 100, 10, 1);
225 }
226 nk_end(&rawfb->ctx);
227 if (nk_window_is_closed(&rawfb->ctx, "Demo")) break;
228
229 /* -------------- EXAMPLES ---------------- */
230 #ifdef INCLUDE_CALCULATOR
231 calculator(ctx);
232 #endif
233 #ifdef INCLUDE_OVERVIEW
234 overview(ctx);
235 #endif
236 #ifdef INCLUDE_NODE_EDITOR
237 node_editor(ctx);
238 #endif
239 /* ----------------------------------------- */
240
241 /* Draw framebuffer */
242 nk_rawfb_render(rawfb, nk_rgb(30,30,30), 1);
243
244 /* Emulate framebuffer */
245 XClearWindow(xw.dpy, xw.win);
246 nk_xlib_render(xw.win);
247 XFlush(xw.dpy);
248
249 /* Timing */
250 dt = timestamp() - started;
251 if (dt < DTIME)
252 sleep_for(DTIME - dt);
253 }
254
255 nk_rawfb_shutdown(rawfb);
256 nk_xlib_shutdown();
257 XUnmapWindow(xw.dpy, xw.win);
258 XFreeColormap(xw.dpy, xw.cmap);
259 XDestroyWindow(xw.dpy, xw.win);
260 XCloseDisplay(xw.dpy);
261 return 0;
262 }
263
264