#include <stdio.h>
#include <X11/Xlib.h>

int main(int argc, char **argv) {
	Display *dpy = XOpenDisplay(NULL);

	int screen = DefaultScreen(dpy);
	Window root = RootWindow(dpy, screen);
	unsigned long black = BlackPixel(dpy, screen);

	Atom wm_protocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
	Atom wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
	Atom _net_wm_ping = XInternAtom(dpy, "_NET_WM_PING", False);

	Window wnd = XCreateSimpleWindow(dpy, root, 0, 0, 200, 200, 0, black, black);
	XSetWMProtocols(dpy, wnd, (Atom[]) { wm_delete_window, _net_wm_ping }, 2);
	XMapWindow(dpy, wnd);

	while(1) {
		XEvent xev;
		XNextEvent(dpy, &xev);
		switch(xev.type) {
			case ClientMessage: {
				if(xev.xclient.message_type == wm_protocols) {
					if(xev.xclient.data.l[0] == wm_delete_window) {
						fputs("Delete Window\n", stderr);
					} else if(xev.xclient.data.l[0] == _net_wm_ping) {
						fputs("Ping\n", stderr);
					}
				}
			}
		}
	}
}
