How do you exit X11 program without Error
The solution to this problem is straightforward:
You must use the right structure member with the XDestroyWindow() function.
Due to the implementation standard of the X11 event structures, they're very similar each other. Every structure begins with the 'type' member, and the first members are practically always the same.
Now assume:
int = 4 bytes
Bool = 4 bytes
unsigned long = 8 bytes
Display* = 8 bytes
Window = 4 bytes
If you call XDestroyWindow() with e.xdestroywindow.window, you are going to be 28 bytes away from the beginning of the event structure, while if you use e.xclient.window, you would be 24 bytes away.
Since you're going to call XDestroyWindow() with a wrong Window argument, it will fail. Instead if you call it using e.xdestroywindow.event (which is 24 bytes away from the beginning of the event structure), the address would be right and the function would work gracefully.
If you take a look yourself at the Xlib.h file, you'll notice that the two structures have the window element positioned differently.
Stated this, remember that Xlib has been developed for years and many programmers every day work with it, so if there is a mysterious error, it's probably not within Xlib. As a last hint I want to tell you: if you want to get farther with Xlib programming, always take the header files as the primary reference, followed by the system manual, then all the rest.
The only error with your code in the end is:
XDestroyWindow(display,e.xdestroywindow.window);
Which must be changed to this:
XDestroyWindow(display,e.xclient.window);
Instead the usage of switch is good, and is the most implemented, with no issues on the X11 code.
NOTE: I've tested your code myself, by changing that line only, and then doing various tests, printing the result. The XDestroyWindow() line is for sure the only error.
Just call XDestroyWindow()
right before XCloseDisplay()
.
Edit:
Sorry, I didn't understand the XSetWMProtocols thing. Now I've read up on it. I think you're accessing the wrong member of the event union.
XDestroyWindow(display,e.xdestroywindow.window);
Should probably be:
XDestroyWindow(display,e.xclient.window);