-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullcursor.c
150 lines (122 loc) · 4.4 KB
/
nullcursor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* nullcursor - simple utility which makes X11 root window cursor invisible
*
* Copyright © 2010 Mikhail Gusarov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <err.h>
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/xcb_aux.h>
static void
err_xcb(int retcode, const char *msg, xcb_generic_error_t *xerr)
{
err(retcode, "%s: %d", msg, xerr->error_code);
}
static xcb_pixmap_t
create_1x1x1_pixmap(xcb_connection_t *conn, xcb_screen_t *screen)
{
uint32_t pixmap_id = xcb_generate_id(conn);
xcb_void_cookie_t c
= xcb_create_pixmap(conn, 1, pixmap_id, screen->root, 1, 1);
xcb_generic_error_t *e = xcb_request_check(conn, c);
if (e)
err_xcb(1, "xcb_create_pixmap", e);
return pixmap_id;
}
static xcb_gc_t
create_gc_for_pixmap(xcb_connection_t *conn, xcb_pixmap_t pixmap)
{
xcb_gc_t gc_id = xcb_generate_id(conn);
xcb_void_cookie_t c = xcb_create_gc (conn, gc_id, pixmap, 0, 0);
xcb_generic_error_t *e = xcb_request_check(conn, c);
if (e)
err_xcb(1, "xcb_create_gc", e);
return gc_id;
}
static void
fill_pixmap(xcb_connection_t *conn, xcb_pixmap_t pixmap,
xcb_gc_t gc, uint32_t pixel)
{
xcb_void_cookie_t c = xcb_put_image(conn, XCB_IMAGE_FORMAT_XY_BITMAP,
pixmap, gc, 1, 1, 0, 0, 0, 1, 4,
(uint8_t*)&pixel);
xcb_generic_error_t* e = xcb_request_check(conn, c);
if (e)
err_xcb(1, "xcb_put_image", e);
}
static xcb_rgb_t
query_white_color(xcb_connection_t *conn, xcb_screen_t *screen)
{
uint32_t pixels[1] = { screen->white_pixel };
xcb_query_colors_cookie_t qcc =
xcb_query_colors(conn, screen->default_colormap, 1, pixels);
xcb_generic_error_t *e;
xcb_query_colors_reply_t *colors_rep =
xcb_query_colors_reply(conn, qcc, &e);
if (e)
err_xcb(1, "xcb_query_colors", e);
return xcb_query_colors_colors(colors_rep)[0];
}
static xcb_cursor_t
create_null_cursor(xcb_connection_t *conn, xcb_screen_t *screen)
{
xcb_pixmap_t pixmap = create_1x1x1_pixmap(conn, screen);
xcb_gc_t gc = create_gc_for_pixmap(conn, pixmap);
fill_pixmap(conn, pixmap, gc, screen->white_pixel);
xcb_rgb_t color = query_white_color(conn, screen);
uint32_t cursor_id = xcb_generate_id(conn);
xcb_void_cookie_t c
= xcb_create_cursor(conn, cursor_id, pixmap, pixmap,
color.red, color.green, color.blue,
color.red, color.green, color.blue, 0, 0);
xcb_generic_error_t *e = xcb_request_check(conn, c);
if (e)
err_xcb(1, "xcb_create_cursor", e);
xcb_free_gc_checked(conn, gc);
xcb_free_pixmap_checked(conn, pixmap);
return cursor_id;
}
static void
set_root_cursor(xcb_connection_t *conn, xcb_screen_t *screen,
xcb_cursor_t cursor)
{
uint32_t cw_mask = XCB_CW_CURSOR;
uint32_t cw_list[] = { cursor };
xcb_void_cookie_t c
= xcb_change_window_attributes(conn, screen->root, cw_mask, cw_list);
xcb_generic_error_t *e = xcb_request_check(conn, c);
if (e)
err_xcb(1, "xcb_change_window_attributes", e);
}
int
main(int argc, char *argv[])
{
int default_screen;
xcb_connection_t *conn;
xcb_screen_t *screen;
conn = xcb_connect(NULL, &default_screen);
if (xcb_connection_has_error(conn))
errx(1, "nullcursor: cannot open display %s", getenv("DISPLAY"));
if (!(screen = xcb_aux_get_screen(conn, default_screen)))
errx(1, "nullcursor: cannot obtain default screen");
xcb_cursor_t cursor = create_null_cursor(conn, screen);
set_root_cursor(conn, screen, cursor);
xcb_free_cursor_checked(conn, cursor);
xcb_disconnect(conn);
return 0;
}