Xlib透明鼠标,Busy鼠标,普通鼠标范例

利用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;
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值