Created
March 12, 2015 09:37
-
-
Save m1nuz/8f8f10a7f8715b62fe79 to your computer and use it in GitHub Desktop.
Simple X11 window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdbool.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <X11/Xlib.h> | |
| bool quited = false; | |
| void on_delete(Display * display, Window window) | |
| { | |
| XDestroyWindow(display, window); | |
| quited = true; | |
| } | |
| extern int main(int argc, char *argv[]) | |
| { | |
| Display * display = XOpenDisplay(NULL); | |
| if (NULL == display) { | |
| fprintf(stderr, "Failed to initialize display"); | |
| return EXIT_FAILURE; | |
| } | |
| Window root = DefaultRootWindow(display); | |
| if (None == root) { | |
| fprintf(stderr, "No root window found"); | |
| XCloseDisplay(display); | |
| return EXIT_FAILURE; | |
| } | |
| Window window = XCreateSimpleWindow(display, root, 0, 0, 800, 600, 0, 0, 0xffffffff); | |
| if (None == window) { | |
| fprintf(stderr, "Failed to create window"); | |
| XCloseDisplay(display); | |
| return EXIT_FAILURE; | |
| } | |
| XMapWindow(display, window); | |
| Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); | |
| XSetWMProtocols(display, window, & wm_delete_window, 1); | |
| XEvent event; | |
| while (!quited) { | |
| XNextEvent(display, &event); | |
| switch(event.type) { | |
| case ClientMessage: | |
| if(event.xclient.data.l[0] == wm_delete_window) { | |
| on_delete(event.xclient.display, event.xclient.window); | |
| } | |
| break; | |
| } | |
| } | |
| XCloseDisplay(display); | |
| return 0; | |
| } |
Still useful as ever 10 years later!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot, I instantly called sleep after XMapWindow and nothing happened. I guess this was cause the Expose event didn't have time to be processed.