vulkan_test2
view src/wsys_x11.c @ 5:cec4b0e7fce8
created swapchain
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 22 Sep 2017 17:48:18 +0300 |
parents | c31c4115d44a |
children | 1dd2c7398afc |
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 /* TODO */
202 }
204 void wsys_redisplay(void)
205 {
206 pending |= REDISPLAY;
207 }
209 void wsys_quit(void)
210 {
211 pending |= QUIT;
212 }
214 int wsys_process_events(int mode)
215 {
216 XEvent xev;
218 if(pending & RESHAPE) {
219 VkSwapchainKHR sc;
220 if(!(sc = vku_create_swapchain(surf, win_width, win_height, 2, VK_PRESENT_MODE_FIFO_KHR, swapchain))) {
221 fprintf(stderr, "Failed to create %dx%d double-buffered swapchain\n", win_width, win_height);
222 return -1;
223 }
224 swapchain = sc;
226 if(cb.reshape) {
227 cb.reshape(win_width, win_height);
228 }
229 pending &= ~RESHAPE;
230 }
232 if(mode == WSYS_BLOCKING) {
233 XNextEvent(dpy, &xev);
234 proc_event(&xev);
235 if(pending & QUIT) return -1;
236 }
238 while(XPending(dpy)) {
239 XNextEvent(dpy, &xev);
240 proc_event(&xev);
241 if(pending & QUIT) return -1;
242 }
244 if(pending & REDISPLAY && win_mapped) {
245 pending &= ~REDISPLAY;
246 if(cb.display) {
247 cb.display();
248 }
249 }
250 return 0;
251 }
253 static void proc_event(XEvent *ev)
254 {
255 switch(ev->type) {
256 case MapNotify:
257 win_mapped = 1;
258 break;
260 case UnmapNotify:
261 win_mapped = 0;
262 break;
264 case ClientMessage:
265 if(ev->xclient.data.l[0] == xa_wm_delete) {
266 pending |= QUIT;
267 }
268 break;
270 case ConfigureNotify:
271 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
272 win_width = ev->xconfigure.width;
273 win_height = ev->xconfigure.height;
274 pending |= RESHAPE;
275 }
276 break;
278 case Expose:
279 if(ev->xexpose.count == 0) {
280 pending |= REDISPLAY;
281 }
282 break;
284 case KeyPress:
285 case KeyRelease:
286 if(cb.keyboard) {
287 KeySym sym;
288 char str[16];
289 XLookupString(&ev->xkey, str, sizeof str, &sym, 0);
290 cb.keyboard(sym & 0xff, ev->type == KeyPress ? 1 : 0);
291 }
292 break;
294 case ButtonPress:
295 case ButtonRelease:
296 if(cb.mouse) {
297 int bn = ev->xbutton.button - Button1;
298 int pressed = ev->type == ButtonPress ? 1 : 0;
299 cb.mouse(bn, pressed, ev->xbutton.x, ev->xbutton.y);
300 }
301 break;
303 case MotionNotify:
304 if(ev->xmotion.state & 0x1f00) {
305 if(cb.motion) {
306 cb.motion(ev->xmotion.x, ev->xmotion.y);
307 }
308 } else {
309 if(cb.passive) {
310 cb.passive(ev->xmotion.x, ev->xmotion.y);
311 }
312 }
313 break;
314 }
315 }