Advertisement

Input handling with OpenGL and X11

Started by September 12, 2006 12:34 AM
4 comments, last by hairymunky 18 years, 3 months ago
Hey everybody, I want to write a platform independent GUI for my OpenGL-Engine, but I can't find any good ressources about input handling on the net. My Event loop looks like that:
void xMainLoop()
{
  XEvent event;
  KeySym ks;

  while(1)
  {
    if(XPending(dpDisplay))  // While more events are pending, continue processing.
    {
      /* Get the current event from the system queue. */
      XNextEvent(dpDisplay, &event);

      /* Process the incoming event. */
      switch(event.type)
      {
        /* If a key was pressed, get keystroke and called the processing function. */
        case KeyPress:
          ks = XLookupKeysym((XKeyEvent *) &event, 0);
          KEY_PRESSED(ks);
          break;

        /* If the screen was resized, call the appropriate function. */
        case ConfigureNotify:
          ReSizeGLScene(event.xconfigure.width, event.xconfigure.height);
          break;

        /* Process any custom messages. */
        case ClientMessage:
	  //If we're going to quit
          if(event.xclient.data.l[0] == wmDeleteWindow)
          {
 	    PROGRAM_SHUTDOWN;
          }
          break;
	default:break;
      }
    }
    DRAW_PROC();
    // since this is double buffered, swap the buffers to display what just got drawn.
    glXSwapBuffers(dpDisplay, win);
  }
}
Now I have some questions: 1. How can I notify when a key gets released? 2. How to access the actual position of the mouse cursor? 3. How to know whether a mouse button was pressed?
Programming at the Xlib level is a pretty hairy beast. I doubt there's a lot of free stuff on the net: I've got the X11 manuals and they're measured in shelf-feet of space (and they're not yer typical fluff programming books, they're densly packed reference manuals). You may be able to find the source for them from wherever you can download the X11 sources.

I might suggest that unless you're willing to devote the rest of your life to reinventing the wheel as a rouleaux triangle (and if you are, then more power to you), you just grab a copy of the SDL library and use that instead. It is already platform independant, it's tested and supported, and it meshes well with OpenGL.

Stephen M. Webb
Professional Free Software Developer

Advertisement
I know there are tons of libs doing all this stuff like input handling and setting up a window, but I like to do everything on my own.. Yup.. even the wheel [wink]

Well then I'll take a look at the X11 doc, if there's nobody who knows some good tutorials covering exactly that...?!
I check for the following xevents in my input handler, to check mouse and keyboard states:

(where xev is the xevent in my switch statement)
     // mouse movement     case MotionNotify:          x = xev.xmotion.x;          y = xev.xmotion.y;          break;     // mouse button press/release     case ButtonRelease:     case ButtonPress:     {         switch (xev.xbutton.button)         {            // handle mouse button presses here             case 1: // LMB            case 2: // MMB            case 3: // RMB          }          // Maybe other button stuff here          break;      }      // Keyboard input      case KeyPress:      case KeyRelease:      {         char buf;         KeySym ks;         XLookupString(&xev.key, &buf, 1, &ks, NULL);         // ks is now one of X key types (ie XK_F1 or just 'a' for a)         switch (ks)         {             // do keyboard things here         }      }


Hope this helps a wee bit - it took me ages when I first started writing my display class to find resources (I think it was thanks to the NeHe ports that I finally figured it out)
You would need to mask the events you want when creating your window (ie KeyPressMask | ButtonPressMask etc)

[Edited by - hairymunky on September 15, 2006 6:11:11 AM]
Smoke me a kipper..I'll be home for supper ;)
Thx hairymunkey, I'll try the mouse handling as soon as I have time for that...

Are the mouse coordinates relative to the windows left upper corner or of the whole screen?
x & y are relative to the window in which the event occured.
In my framework, the display grabs the pointer (ie the pointer
never leaves my game window). When I catch a motion event, I
use:
XWarpPointer(display, None, mainwin, 0,0, 0,0, mouse_x, mouse_y)

where mouse_x, mouse_y are got from the MotionNotify event,
display is the display the window is running on, and
mainwin is my Window.

Hope that helps

Smoke me a kipper..I'll be home for supper ;)

This topic is closed to new replies.

Advertisement