How to set mouse cursor on X11 in a C application

2019-05-10 14:07发布

问题:

I've got a rather large and rather old C application that has been ported to Linux. I'm in charge of getting mouse cursors to work correctly, but having some issues. I was able to convert most of the cursors we require to using the standard cursors provided by XFontCursor by using something like:

gCursorTable[waitCurs] = XCreateFontCursor(gDisplay, XC_watch);
...
XDefineCursor(gDisplay, WHostWindow(w), gCursorTable[cursor]);
XFlush(gDisplay);

This is fine for cursors which have analogs in the extremely limited list of (useful) cursors that XFontCursor provides, but there are other built in themed cursors that I'd like to set. For example, I'd like to be able to set the cursor to bd_double_arrow (which is included in every cursor theme and is the standard diagonal sizing cursor for windows) in my app, but you obviously can't do that with XCreateFontCursor. This seems pretty basic, but for the life of me I can't find any description on how to do it.

I just want to know how other X11 apps are setting cursors, because they are obviously getting them from a global theme and not just using XCreateFontCursor.

回答1:

The easiest way to use themed cursors is with the Xcursor library.

#include <X11/Xcursor/Xcursor.h>
...
Cursor c = XcursorLibraryLoadCursor(dpy, "sb_v_double_arrow");
XDefineCursor (dpy, w, c);

The names are standard cursor names from X11/cursorfont.h, sans XC_. If the theme has extra cursors such as bd_double_arrow, these names can also be used (but not all themes have them!)

If a theme does not have a replacement for some core X cursor, the library will fall back to the core cursor.



回答2:

After clicking some links on that page: try XCreatePixmapCursor. From its description, it looks like you can create any 2-color cursor you want.



标签: c cursor mouse x11