Sign in to follow this  
Aluthreney

X11: Locking cursor in the center of the window

Recommended Posts

I'm developing a game using an engine called Gameplay 3D on Ubuntu and while trying to use the engine's "captureMouse" function I learned that this particular function has not yet been defined for the linux distro. I've already told them about my need for this function to be defined, but luckily the engine is open source; so for quite a few hours now I've been trying to get the cursor to lock in the center of the screen.

 

This is what I've been able to do so far:

- using this function

 

 

XWarpPointer(__display, None, __window, 0, 0, 0, 0, getDisplayWidth()/2, getDisplayHeight/2);

I've been able to move the cursor onto the center of the window, but if I move the mouse the cursor will leave the center. This seemed like a step in the right directing so I kept looking for another function for actually locked the cursor in place

 

- using this code:

 


 

/* vars to make blank cursor */
Pixmap blank;
XColor dummy;
char data[1] = {0};
Cursor cursor;

/* make a blank cursor */
blank = XCreateBitmapFromData (dpy, win, data, 1, 1);
if(blank == None) fprintf(stderr, "error: out of memory.\n");
cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
XFreePixmap (dpy, blank);
Con_Printf("Cursor blanked.\n")

found here: Link; I was able to make an invisible with the help of this function:

 

 

 

XGrabPointer(__display, __window, true, 0, GrabModeAsync, GrabModeASnyc, __window, cursor, CurrentTime)

Unfortunatly the XGrabPointer function didn't lock the pointer in place. Instead it just disabled the cursor from leaving the window, ei.: the cursor wouldn't move past the edges of the window.

 

So right now I have an invisible cursor, confined to the application window, that jumps the center (of the window) when I run the program. My question: How do I lock the cursor in place (at the center)?

Share this post


Link to post
Share on other sites
I don't think there is a function to lock the cursor in position (shame on me if I'm wrong), but you can call XWarpPointer every time you receive a mouse event, which has the same effect.

sorry for plusing your answer smile.png its not exactly the way its done. XWarpPointer() would move the cursor and produce the mouse event itself, performance falls drastically for no good reason. its better to call XWarpPointer() after the event queue is empty:

while (XPending(display)) {
    XNextEvent(display, &event);
    // process you events
}

XWarpPointer(...);

or you can look through the freeglut sources to find out how its implemented for X11.

Edited by santa01

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this