vulkan_test2

view src/wsys_x11.c @ 13:d34f84bede17

pipeline madness
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Jun 2018 08:00:57 +0300
parents 20eb42197ab8
children 9fb6c24691ea
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include "wsys.h"
6 #include "vku.h"
8 struct callbacks {
9 void (*display)(void);
10 void (*reshape)(int, int);
11 void (*keyboard)(int, int);
12 void (*mouse)(int, int, int, int);
13 void (*motion)(int, int);
14 void (*passive)(int, int);
15 };
17 enum {
18 QUIT = 1,
19 RESHAPE = 2,
20 REDISPLAY = 4
21 };
23 static void proc_event(XEvent *ev);
25 static Display *dpy;
26 static Window win;
27 static VkSurfaceKHR surf;
28 static VkSwapchainKHR swapchain;
29 static Atom xa_wm_delete;
30 static int win_width, win_height;
31 static int win_mapped;
32 static unsigned int evmask = StructureNotifyMask | ExposureMask;
33 static unsigned int pending;
34 static struct callbacks cb;
36 int wsys_create_window(int xsz, int ysz)
37 {
38 int i, scr, num_visuals;
39 Window root_win;
40 Visual *vis = 0;
41 XSetWindowAttributes xattr;
42 unsigned int xattr_mask;
43 XVisualInfo *vinf, vinf_match;
45 if(!(dpy = XOpenDisplay(0))) {
46 fprintf(stderr, "failed to open connection to the X server\n");
47 return -1;
48 }
49 scr = DefaultScreen(dpy);
50 root_win = RootWindow(dpy, scr);
52 xa_wm_delete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
54 vinf_match.screen = scr;
55 vinf_match.depth = 24;
56 vinf_match.class = TrueColor;
58 if(!(vinf = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask | VisualClassMask, &vinf_match, &num_visuals))) {
59 fprintf(stderr, "failed to retrieve matching visuals\n");
60 XCloseDisplay(dpy);
61 return -1;
62 }
64 for(i=0; i<num_visuals; i++) {
65 if(vku_xlib_usable_visual(dpy, vinf[i].visualid)) {
66 vis = vinf[i].visual;
67 break;
68 }
69 }
70 if(!vis) {
71 fprintf(stderr, "failed to find approprate visual\n");
72 XFree(vinf);
73 XCloseDisplay(dpy);
74 return -1;
75 }
77 xattr.background_pixel = xattr.border_pixel = BlackPixel(dpy, scr);
78 xattr.colormap = XCreateColormap(dpy, root_win, vis, AllocNone);
79 xattr_mask = CWBackPixel | CWBorderPixel | CWColormap;
81 if(!(win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, 24, InputOutput, vis, xattr_mask, &xattr))) {
82 fprintf(stderr, "failed to create X window\n");
83 XFree(vinf);
84 XCloseDisplay(dpy);
85 return -1;
86 }
87 XFree(vinf);
89 XSelectInput(dpy, win, evmask);
90 XSetWMProtocols(dpy, win, &xa_wm_delete, 1);
92 if(!(surf = vku_xlib_create_surface(dpy, win))) {
93 fprintf(stderr, "failed to create vulkan surface for the window\n");
94 XDestroyWindow(dpy, win);
95 XCloseDisplay(dpy);
96 return -1;
97 }
98 /* swapchain gets created before the first reshape invocation */
100 wsys_set_window_title("X11 window");
101 XMapWindow(dpy, win);
103 win_width = xsz;
104 win_height = ysz;
105 pending = RESHAPE | REDISPLAY;
107 return 0;
108 }
110 void wsys_destroy_window(void)
111 {
112 if(dpy) {
113 if(win) {
114 XDestroyWindow(dpy, win);
115 win = 0;
116 }
117 XCloseDisplay(dpy);
118 dpy = 0;
119 }
120 }
122 void wsys_get_window_size(int *xsz, int *ysz)
123 {
124 *xsz = win_width;
125 *ysz = win_height;
126 }
128 void wsys_set_window_title(const char *title)
129 {
130 XTextProperty text;
131 XStringListToTextProperty((char**)&title, 1, &text);
132 XSetWMName(dpy, win, &text);
133 XSetWMIconName(dpy, win, &text);
134 XFree(text.value);
135 }
137 void wsys_display_callback(void (*func)(void))
138 {
139 cb.display = func;
140 }
142 void wsys_reshape_callback(void (*func)(int, int))
143 {
144 cb.reshape = func;
145 }
147 void wsys_keyboard_callback(void (*func)(int, int))
148 {
149 cb.keyboard = func;
150 if(func) {
151 evmask |= KeyPressMask | KeyReleaseMask;
152 } else {
153 evmask &= ~(KeyPressMask | KeyReleaseMask);
154 }
155 if(win) {
156 XSelectInput(dpy, win, evmask);
157 }
158 }
160 void wsys_mouse_callback(void (*func)(int, int, int, int))
161 {
162 cb.mouse = func;
163 if(func) {
164 evmask |= ButtonPressMask | ButtonReleaseMask;
165 } else {
166 evmask &= ~(ButtonPressMask | ButtonReleaseMask);
167 }
168 if(win) {
169 XSelectInput(dpy, win, evmask);
170 }
171 }
173 void wsys_motion_callback(void (*func)(int, int))
174 {
175 cb.motion = func;
176 if(func) {
177 evmask |= ButtonMotionMask;
178 } else {
179 evmask &= ~ButtonMotionMask;
180 }
181 if(win) {
182 XSelectInput(dpy, win, evmask);
183 }
184 }
186 void wsys_passive_motion_callback(void (*func)(int, int))
187 {
188 cb.passive = func;
189 if(func) {
190 evmask |= PointerMotionMask;
191 } else {
192 evmask &= ~PointerMotionMask;
193 }
194 if(win) {
195 XSelectInput(dpy, win, evmask);
196 }
197 }
199 void wsys_swap_buffers(void)
200 {
201 vku_present(swapchain, next_swapchain_image);
202 next_swapchain_image = vku_get_next_image(swapchain);
203 }
205 void wsys_redisplay(void)
206 {
207 pending |= REDISPLAY;
208 }
210 void wsys_quit(void)
211 {
212 pending |= QUIT;
213 }
215 int wsys_process_events(int mode)
216 {
217 XEvent xev;
219 if(pending & RESHAPE) {
220 VkSwapchainKHR sc;
221 if(!(sc = vku_create_swapchain(surf, win_width, win_height, 2, VK_PRESENT_MODE_FIFO_KHR, swapchain))) {
222 fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", win_width, win_height);
223 return -1;
224 }
225 swapchain = sc;
227 free(swapchain_images);
228 swapchain_images = vku_get_swapchain_images(sc, 0);
229 next_swapchain_image = vku_get_next_image(swapchain);
231 if(!vkrpass) {
232 if(!(vkrpass = vku_create_renderpass(VK_FORMAT_R8G8B8_UNORM, VK_FORMAT_UNDEFINED))) {
233 abort();
234 }
235 }
237 if(cb.reshape) {
238 cb.reshape(win_width, win_height);
239 }
240 pending &= ~RESHAPE;
241 }
243 if(mode == WSYS_BLOCKING) {
244 XNextEvent(dpy, &xev);
245 proc_event(&xev);
246 if(pending & QUIT) return -1;
247 }
249 while(XPending(dpy)) {
250 XNextEvent(dpy, &xev);
251 proc_event(&xev);
252 if(pending & QUIT) return -1;
253 }
255 if(pending & REDISPLAY && win_mapped) {
256 pending &= ~REDISPLAY;
257 if(cb.display) {
258 cb.display();
259 }
260 }
261 return 0;
262 }
264 static void proc_event(XEvent *ev)
265 {
266 switch(ev->type) {
267 case MapNotify:
268 win_mapped = 1;
269 break;
271 case UnmapNotify:
272 win_mapped = 0;
273 break;
275 case ClientMessage:
276 if(ev->xclient.data.l[0] == xa_wm_delete) {
277 pending |= QUIT;
278 }
279 break;
281 case ConfigureNotify:
282 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
283 win_width = ev->xconfigure.width;
284 win_height = ev->xconfigure.height;
285 pending |= RESHAPE;
286 }
287 break;
289 case Expose:
290 if(ev->xexpose.count == 0) {
291 pending |= REDISPLAY;
292 }
293 break;
295 case KeyPress:
296 case KeyRelease:
297 if(cb.keyboard) {
298 KeySym sym;
299 char str[16];
300 XLookupString(&ev->xkey, str, sizeof str, &sym, 0);
301 cb.keyboard(sym & 0xff, ev->type == KeyPress ? 1 : 0);
302 }
303 break;
305 case ButtonPress:
306 case ButtonRelease:
307 if(cb.mouse) {
308 int bn = ev->xbutton.button - Button1;
309 int pressed = ev->type == ButtonPress ? 1 : 0;
310 cb.mouse(bn, pressed, ev->xbutton.x, ev->xbutton.y);
311 }
312 break;
314 case MotionNotify:
315 if(ev->xmotion.state & 0x1f00) {
316 if(cb.motion) {
317 cb.motion(ev->xmotion.x, ev->xmotion.y);
318 }
319 } else {
320 if(cb.passive) {
321 cb.passive(ev->xmotion.x, ev->xmotion.y);
322 }
323 }
324 break;
325 }
326 }