Xlib and GLX: Part 2
Catching the WM close-button[edit]
- /* Tell the window you want to recive the fscking event */
- Atom wm_delete_window = XInternAtom(GLWindow.dpy, "WM_DELETE_WINDOW", false);
- XSetWMProtocols(GLWindow.dpy, GLWindow.win, &GLWindow.wm_delete_window, 1);
Run that code any time after creating the window. Now you can catch the event ClientMessage in your event loop. When that event is caught you need to check if event.xclient.data.l[0] equals GLWindow.wm_delete_window.
Now, if just someone could have told me this instead of me having to dig through hundreds of lines of X code.
Getting keyboard input[edit]
First, make sure your window has been created with the KeyPressMask.
In your X Event look for the KeyPress event. The event.xkey struct contains enought info to be converted to an ascii character using XLookupString. XLookupString returns the numbers of characters written to the buffer. In this case a maximum of 1 can be written. If a non-printable character occurs the lenght will be 0. XLookupString also looks for key modifiers like shift and alt.
- static void poll(){
- XEvent event;
- while ( XPending(GLWindow.dpy) > 0 ){
- XNextEvent(GLWindow.dpy, &event);
- switch (event.type){
- case KeyPress:
- {
- char buf[2];
- int len;
- KeySym keysym_return;
- len = XLookupString(&event.xkey, buf, 1, &keysym_return, NULL);
- if ( len != 0 ){
- printf("Char: %c",buf[0]);
- }
- }
- break;
- default:
- printf("Unhandled event: %d\n",event.type);
- break;
- }
- }
- }