How do you hide the mouse pointer under Linux/X11?

前端 未结 10 2086
小蘑菇
小蘑菇 2020-12-05 00:44

How do I hide the mouse pointer under X11? I would like to use the built in libraries in order to do this and not something like SDL (SDL_ShowCursor(0)) or glut (glutSetCur

相关标签:
10条回答
  • 2020-12-05 00:59

    I ended up using XDefineCursor like ephemient mentioned. The control application changed the default root window cursor and the other applications (which are under my control) inherited it.

    Code specifics look like:

    // Hide the cursor
    
    if (NULL==(display=XOpenDisplay(NULL))) 
    {
       printf("Unable to open NULL display\n");
       exit(1);
    }
    window = DefaultRootWindow(display);
    
    Cursor invisibleCursor;
    Pixmap bitmapNoData;
    XColor black;
    static char noData[] = { 0,0,0,0,0,0,0,0 };
    black.red = black.green = black.blue = 0;
    
    bitmapNoData = XCreateBitmapFromData(display, window, noData, 8, 8);
    invisibleCursor = XCreatePixmapCursor(display, bitmapNoData, bitmapNoData, 
                                         &black, &black, 0, 0);
    XDefineCursor(display,window, invisibleCursor);
    XFreeCursor(display, invisibleCursor);
    XFreePixmap(display, bitmapNoData);
    

    In order to hide the cursor and then after I'm done

    // Restore the X left facing cursor
    Cursor cursor;
    cursor=XCreateFontCursor(display,XC_left_ptr);
    XDefineCursor(display, window, cursor);
    XFreeCursor(display, cursor);
    

    To restore X's left handed cursor (Since it's the root window and I don't want it to stay invisible. I'm not sure, but I might also be able to use

    XUndefineCursor(display, window);
    
    0 讨论(0)
  • 2020-12-05 01:00

    You can create and set an invisible cursor theme. This trick is used by maemo, because it's rather pointless to have a cursor on a touchscreen device.

    Sadly, the ability to change the global cursor theme at runtime is not uniform across X11 applications and toolkits. You can change the server resource Xcursor.theme, and nobody will notice (generally it's only queried at startup); you can inform xsettings which only seems to affect Gtk+ programs; KDE uses some sort of communication through properties on the root window; etc.

    At least changing the cursor for your own application is as easy as XDefineCursor, and if you do that on the root window, some applications might follow along.

    0 讨论(0)
  • 2020-12-05 01:00

    This is my solution. It places the cursor where you can't see it (in my case, in the bottom-left corner) - then, it disables the mouse, so you can't move it.

    Note You could parse data from xrandr, or put that data in an environmental on login, and then use it; that way, you won't have to have it hard coded. But, as for me, I never change my screen resolution, so 768 is OK :)

    setmouse () {
       DISPLAY=":0" xinput $1 `DISPLAY=":0" xinput | grep Mouse |
               tr -d " " | tr "\t" " " |
               cut -d" " -f2 | cut -d"=" -f2`
    }
    
    offmouse () {
       DISPLAY=":0" xdotool mousemove 0 768 # use xrandr to find out
       setmouse disable
    }
    
    onmouse () {
       setmouse enable
    }
    
    0 讨论(0)
  • 2020-12-05 01:10

    Here's a description how unclutter utility does it.

    Unclutter is a program which runs permanently in the background of an X11 session. It checks on the X11 pointer (cursor) position every few seconds, and when it finds it has not moved (and no buttons are pressed on the mouse, and the cursor is not in the root window) it creates a small sub-window as a child of the window the cursor is in. The new window installs a cursor of size 1x1 but a mask of all 0, ie an invisible cursor. This allows you to see all the text in an xterm or xedit, for example. The human factors crowd would agree it should make things less distracting.

    Once created, the program waits for the pointer to leave the window and then destroys it, restoring the original situation. Button events are passed transparently through to the parent window. They will usually cause the cursor to reappear because an active grab will be made by the program while the button is down, so the pointer will apparently leave the window, even though its x y position doesnt change.

    0 讨论(0)
提交回复
热议问题