Fixed #331 with graceful x11 window closing

This commit is contained in:
vurtun 2017-01-23 19:33:58 +01:00
parent 2e9c4ee0ab
commit 6b27c20231
3 changed files with 25 additions and 8 deletions

View File

@ -41,6 +41,7 @@ struct XWindow {
XFont *font; XFont *font;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
Atom wm_delete_window;
}; };
static void static void
@ -128,6 +129,9 @@ main(void)
xw.vis, CWEventMask | CWColormap, &xw.swa); xw.vis, CWEventMask | CWColormap, &xw.swa);
XStoreName(xw.dpy, xw.win, "X11"); XStoreName(xw.dpy, xw.win, "X11");
XMapWindow(xw.dpy, xw.win); XMapWindow(xw.dpy, xw.win);
xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
XGetWindowAttributes(xw.dpy, xw.win, &xw.attr); XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
xw.width = (unsigned int)xw.attr.width; xw.width = (unsigned int)xw.attr.width;
xw.height = (unsigned int)xw.attr.height; xw.height = (unsigned int)xw.attr.height;
@ -141,14 +145,15 @@ main(void)
/*set_style(ctx, THEME_RED);*/ /*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/ /*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/ /*set_style(ctx, THEME_DARK);*/
while (running) while (running)
{ {
/* Input */ /* Input */
XEvent evt; XEvent evt;
started = timestamp(); started = timestamp();
nk_input_begin(ctx); nk_input_begin(ctx);
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)){ while (XPending(xw.dpy)) {
XNextEvent(xw.dpy, &evt);
if (evt.type == ClientMessage) goto cleanup;
if (XFilterEvent(&evt, xw.win)) continue; if (XFilterEvent(&evt, xw.win)) continue;
nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt); nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
} }
@ -157,8 +162,7 @@ main(void)
/* GUI */ /* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200), if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE| NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
NK_WINDOW_SCALE_LEFT))
{ {
enum {EASY, HARD}; enum {EASY, HARD};
static int op = EASY; static int op = EASY;
@ -193,6 +197,7 @@ main(void)
sleep_for(DTIME - dt); sleep_for(DTIME - dt);
} }
cleanup:
nk_xfont_del(xw.dpy, xw.font); nk_xfont_del(xw.dpy, xw.font);
nk_xlib_shutdown(); nk_xlib_shutdown();
XUnmapWindow(xw.dpy, xw.win); XUnmapWindow(xw.dpy, xw.win);

View File

@ -1,4 +1,4 @@
/* nuklear - 1.32.0 - public domain */ /* nuklear - v1.32.0 - public domain */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -62,6 +62,7 @@ struct XWindow {
XSetWindowAttributes swa; XSetWindowAttributes swa;
XWindowAttributes attr; XWindowAttributes attr;
GLXFBConfig fbc; GLXFBConfig fbc;
Atom wm_delete_window;
int width, height; int width, height;
}; };
static int gl_err = FALSE; static int gl_err = FALSE;
@ -177,6 +178,8 @@ int main(int argc, char **argv)
XFree(win.vis); XFree(win.vis);
XStoreName(win.dpy, win.win, "Demo"); XStoreName(win.dpy, win.win, "Demo");
XMapWindow(win.dpy, win.win); XMapWindow(win.dpy, win.win);
win.wm_delete_window = XInternAtom(win.dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(win.dpy, win.win, &win.wm_delete_window, 1);
} }
{ {
/* create opengl context */ /* create opengl context */
@ -245,7 +248,9 @@ int main(int argc, char **argv)
/* Input */ /* Input */
XEvent evt; XEvent evt;
nk_input_begin(ctx); nk_input_begin(ctx);
while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)){ while (XPending(win.dpy)) {
XNextEvent(win.dpy, &evt);
if (evt.type == ClientMessage) goto cleanup;
if (XFilterEvent(&evt, win.win)) continue; if (XFilterEvent(&evt, win.win)) continue;
nk_x11_handle_event(&evt); nk_x11_handle_event(&evt);
} }
@ -308,6 +313,7 @@ int main(int argc, char **argv)
glXSwapBuffers(win.dpy, win.win);} glXSwapBuffers(win.dpy, win.win);}
} }
cleanup:
nk_x11_shutdown(); nk_x11_shutdown();
glXMakeCurrent(win.dpy, 0, 0); glXMakeCurrent(win.dpy, 0, 0);
glXDestroyContext(win.dpy, glContext); glXDestroyContext(win.dpy, glContext);

View File

@ -1,4 +1,4 @@
/* nuklear - v1.17 - public domain */ /* nuklear - v1.32.0 - public domain */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
@ -60,6 +60,7 @@ struct XWindow {
XSetWindowAttributes swa; XSetWindowAttributes swa;
XWindowAttributes attr; XWindowAttributes attr;
GLXFBConfig fbc; GLXFBConfig fbc;
Atom wm_delete_window;
int width, height; int width, height;
}; };
static int gl_err = FALSE; static int gl_err = FALSE;
@ -175,6 +176,8 @@ int main(int argc, char **argv)
XFree(win.vis); XFree(win.vis);
XStoreName(win.dpy, win.win, "Demo"); XStoreName(win.dpy, win.win, "Demo");
XMapWindow(win.dpy, win.win); XMapWindow(win.dpy, win.win);
win.wm_delete_window = XInternAtom(win.dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(win.dpy, win.win, &win.wm_delete_window, 1);
} }
{ {
/* create opengl context */ /* create opengl context */
@ -242,7 +245,9 @@ int main(int argc, char **argv)
/* Input */ /* Input */
XEvent evt; XEvent evt;
nk_input_begin(ctx); nk_input_begin(ctx);
while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)){ while (XPending(win.dpy)) {
XNextEvent(win.dpy, &evt);
if (evt.type == ClientMessage) goto cleanup;
if (XFilterEvent(&evt, win.win)) continue; if (XFilterEvent(&evt, win.win)) continue;
nk_x11_handle_event(&evt); nk_x11_handle_event(&evt);
} }
@ -305,6 +310,7 @@ int main(int argc, char **argv)
glXSwapBuffers(win.dpy, win.win);} glXSwapBuffers(win.dpy, win.win);}
} }
cleanup:
nk_x11_shutdown(); nk_x11_shutdown();
glXMakeCurrent(win.dpy, 0, 0); glXMakeCurrent(win.dpy, 0, 0);
glXDestroyContext(win.dpy, glContext); glXDestroyContext(win.dpy, glContext);