利用Xlib相关库实现代码:
/** *gcc -lX11 -lXext xcursorBusy.c -o xcursorBusy *xcursorBusy *xcursorBusy resort 2>logfile */ #include <X11/Xlib.h> #include <X11/Xatom.h> #include <X11/cursorfont.h> #include <stdio.h> #include <stdlib.h> static const unsigned char xlib_spinning_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xfc, 0x3b, 0x00, 0x00, 0x7c, 0x38, 0x00, 0x00, 0x6c, 0x54, 0x00, 0x00, 0xc4, 0xdc, 0x00, 0x00, 0xc0, 0x44, 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; static const unsigned char xlib_spinning_mask_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, 0xfe, 0x3b, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xee, 0xff, 0x01, 0x00, 0xe4, 0xff, 0x00, 0x00, 0xc0, 0x7f, 0x00, 0x00, 0xc0, 0x7f, 0x00, 0x00, 0x80, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; Cursor cursorCreateSpinning (Display * dpy) { Pixmap cursor; Pixmap mask; XColor fg, bg; Cursor xcursor; fg.pixel = 0; fg.red = 0; fg.green = 0; fg.blue = 0; fg.flags = 0xf; bg.pixel = 0xffffffff; bg.red = 0xffff; bg.green = 0xffff; bg.blue = 0xffff; bg.flags = 0xf; cursor = XCreatePixmapFromBitmapData (dpy, DefaultRootWindow(dpy), (char *) xlib_spinning_bits, 32, 32, 0xffffffff, 0x0, 1); mask = XCreatePixmapFromBitmapData (dpy, DefaultRootWindow(dpy), (char *) xlib_spinning_mask_bits, 32, 32, 0xffffffff, 0x0, 1); xcursor = XCreatePixmapCursor (dpy, cursor, mask, &fg, &bg, 2, 2); XFreePixmap (dpy, mask); XFreePixmap (dpy, cursor); return xcursor; } int main(int argc, char **argv) { Display *d; Window w; Cursor busy_ptr; char *dn = getenv ("DISPLAY"); fprintf (stderr, "got display %s\n", dn); d = XOpenDisplay( dn ); if (d == NULL) { fprintf (stderr, "%s display open error!", dn); } else { fprintf (stderr, "display open normal\n"); } w = RootWindow (d, DefaultScreen (d)); fprintf (stderr, "%s get root window %d\n", dn, DefaultScreen (d)); #if 1 // busy cursor/normal cursor if (argc > 1) { // resort default cursor by WM // busy_ptr = 0; or // XUndefineCursor (d, w); goto cursor_out; busy_ptr = XCreateFontCursor (d, XC_left_ptr); // or X_Cursor fprintf (stderr, "resort default cursor\n"); } else { busy_ptr = cursorCreateSpinning (d); fprintf (stderr, "create busy cursor\n"); } XDefineCursor(d, w, busy_ptr); XFreeCursor(d, busy_ptr); #else // transparent cursor Pixmap bm_no; Colormap cmap; Cursor no_ptr; XColor black, dummy; static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0}; cmap = DefaultColormap(d, DefaultScreen(d)); XAllocNamedColor(d, cmap, "black", &black, &dummy); bm_no = XCreateBitmapFromData(d, w, bm_no_data, 8, 8); no_ptr = XCreatePixmapCursor(d, bm_no, bm_no, &black, &black, 0, 0); XDefineCursor(d, w, no_ptr); XFreeCursor(d, no_ptr); if (bm_no != None) XFreePixmap(d, bm_no); XFreeColors(d, cmap, &black.pixel, 1, 0); #endif XMapWindow( d, w ); XSync(d, False); //XFlush( d ); fprintf (stderr, "let cursor loop\n"); #if 0 while( 1 ){ XEvent xe; XNextEvent(d, &xe); switch (xe.type) { case KeyPress: case ButtonPress: fprintf (stderr, "cursor exit\n"); goto cursor_out; default: fprintf (stderr, "catch event %i\n", xe.type); } } #endif cursor_out: fprintf (stderr, "release resource\n"); XDestroyWindow(d, w); XCloseDisplay(d); return 0; }