Skip to content

Instantly share code, notes, and snippets.

@m1nuz
Created March 12, 2015 09:37
Show Gist options
  • Select an option

  • Save m1nuz/8f8f10a7f8715b62fe79 to your computer and use it in GitHub Desktop.

Select an option

Save m1nuz/8f8f10a7f8715b62fe79 to your computer and use it in GitHub Desktop.
Simple X11 window
#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;
}
@JuanmaDevG
Copy link

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.

@Buggem
Copy link

Buggem commented Dec 30, 2025

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