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

2019-01-17 06:59发布

问题:

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 (glutSetCursor(GLUT_CURSOR_NONE)). Also, the mouse pointer should be hidden no matter the pointer location, not just in its own window.

回答1:

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.



回答2:

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.



回答3:

I'd rather use simpler method:

unclutter -idle 0

You almost do not see cursor, still it is available. To disable mouse:

rmmod psmouse

Or disable mouse module permanently somewhere in /etc/. See your distribution manual.



回答4:

an alternative to unclutter

Unclutter didn't work for me, as it doesn't play well with hardware accelerated surfaces (such as for example those produced by intels' VA-API when decoding video). So I found a program that hid the mouse pointer in a less roundabout way, hhp, and rewrote it in C with minimal dependencies, the result is hhpc. I did this to obviate the need to have haskell to compile it and because hhp sometimes stopped hiding the mouse pointer.

hhpc, relies only on glibc and xlib, so it's easy to build, just do make release. You can get the code and instructions from my repository. It's very memory and CPU efficient (because it does almost nothing).



回答5:

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);


回答6:

There is a -no-cursor option for Xorg 1.7 and later. https://www.x.org/wiki/AdvancedTopicsFAQ/

xinit -- -nocursor or startx -- -nocursor could work.



回答7:

Use xbanish! It "banish the mouse cursor when typing"! Start it with

xbanish &

and enjoy!



回答8:

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
}


回答9:

All right!

I guess this post may be getting a little bit old, but if what I've found can help some of us, I definitely have to post it here ;)

I found myself a clean and simple solution that works fine, without using "xcb" ( for what I tried to achieve, it was a litte over-engineering (..)

So:

All you need is the "xsetroot" command, with appropriate arguments/params:

-> to hide the mouse cursor, you need an extra little file (I called mine "blnk_ptr.xbm")

the content of this file:

#define blnk_ptr_width 1
#define blnk_ptr_height 1
#define blnk_ptr_x_hot 0
#define blnk_ptr_y_hot 0
static unsigned char blnk_ptr_bits[] = {
   0x00 };

Then, we can use the two following commands:

-> to hide the mouse pointer cursor:

xsetroot -cursor blnk_ptr.xbm blnk_ptr.xbm

-> to show the mouse pointer cursor again:

xsetroot -cursor_name left_ptr

( you can use a mouse pointer cursor other than "left_ptr", but this one seems to be widely available across *nix systems (..)

Btw-> I don't know yet how to get the name of the pointer currently used by the system using xsetroot --> I guess I'll [as usual] digg that too, but I'd be happy to have someone who knows the how-to giving me the answer ( It'd be nice ;) )

Enjoy ? ;p



标签: linux mouse x11