vulkan_test2

view src/wsys_x11.c @ 10:8eb3126cc2f1

starting on shaders
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Jun 2018 06:58:58 +0300
parents 1dd2c7398afc
children d34f84bede17
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(cb.reshape) {
232 cb.reshape(win_width, win_height);
233 }
234 pending &= ~RESHAPE;
235 }
237 if(mode == WSYS_BLOCKING) {
238 XNextEvent(dpy, &xev);
239 proc_event(&xev);
240 if(pending & QUIT) return -1;
241 }
243 while(XPending(dpy)) {
244 XNextEvent(dpy, &xev);
245 proc_event(&xev);
246 if(pending & QUIT) return -1;
247 }
249 if(pending & REDISPLAY && win_mapped) {
250 pending &= ~REDISPLAY;
251 if(cb.display) {
252 cb.display();
253 }
254 }
255 return 0;
256 }
258 static void proc_event(XEvent *ev)
259 {
260 switch(ev->type) {
261 case MapNotify:
262 win_mapped = 1;
263 break;
265 case UnmapNotify:
266 win_mapped = 0;
267 break;
269 case ClientMessage:
270 if(ev->xclient.data.l[0] == xa_wm_delete) {
271 pending |= QUIT;
272 }
273 break;
275 case ConfigureNotify:
276 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
277 win_width = ev->xconfigure.width;
278 win_height = ev->xconfigure.height;
279 pending |= RESHAPE;
280 }
281 break;
283 case Expose:
284 if(ev->xexpose.count == 0) {
285 pending |= REDISPLAY;
286 }
287 break;
289 case KeyPress:
290 case KeyRelease:
291 if(cb.keyboard) {
292 KeySym sym;
293 char str[16];
294 XLookupString(&ev->xkey, str, sizeof str, &sym, 0);
295 cb.keyboard(sym & 0xff, ev->type == KeyPress ? 1 : 0);
296 }
297 break;
299 case ButtonPress:
300 case ButtonRelease:
301 if(cb.mouse) {
302 int bn = ev->xbutton.button - Button1;
303 int pressed = ev->type == ButtonPress ? 1 : 0;
304 cb.mouse(bn, pressed, ev->xbutton.x, ev->xbutton.y);
305 }
306 break;
308 case MotionNotify:
309 if(ev->xmotion.state & 0x1f00) {
310 if(cb.motion) {
311 cb.motion(ev->xmotion.x, ev->xmotion.y);
312 }
313 } else {
314 if(cb.passive) {
315 cb.passive(ev->xmotion.x, ev->xmotion.y);
316 }
317 }
318 break;
319 }
320 }