Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set mouse cursor on X11 in a C application

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.

like image 529
Mordred Avatar asked Feb 16 '23 06:02

Mordred


1 Answers

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.

like image 142
n. 1.8e9-where's-my-share m. Avatar answered Feb 24 '23 11:02

n. 1.8e9-where's-my-share m.