]> git.sur5r.net Git - i3/i3/blob - src/xcursor.c
Merge branch 'master' into next
[i3/i3] / src / xcursor.c
1 #undef I3__FILE__
2 #define I3__FILE__ "xcursor.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * xcursor.c: libXcursor support for themed cursors.
10  *
11  */
12 #include <assert.h>
13 #include <X11/Xcursor/Xcursor.h>
14 #include <X11/cursorfont.h>
15
16 #include "i3.h"
17 #include "xcb.h"
18 #include "xcursor.h"
19
20 static Cursor cursors[XCURSOR_CURSOR_MAX];
21
22 static const int xcb_cursors[XCURSOR_CURSOR_MAX] = {
23     XCB_CURSOR_LEFT_PTR,
24     XCB_CURSOR_SB_H_DOUBLE_ARROW,
25     XCB_CURSOR_SB_V_DOUBLE_ARROW,
26     XCB_CURSOR_WATCH
27 };
28
29 static Cursor load_cursor(const char *name) {
30     Cursor c = XcursorLibraryLoadCursor(xlibdpy, name);
31     if (c == None)
32         xcursor_supported = false;
33     return c;
34 }
35
36 void xcursor_load_cursors(void) {
37     cursors[XCURSOR_CURSOR_POINTER] = load_cursor("left_ptr");
38     cursors[XCURSOR_CURSOR_RESIZE_HORIZONTAL] = load_cursor("sb_h_double_arrow");
39     cursors[XCURSOR_CURSOR_RESIZE_VERTICAL] = load_cursor("sb_v_double_arrow");
40     cursors[XCURSOR_CURSOR_WATCH] = load_cursor("watch");
41 }
42
43 /*
44  * Sets the cursor of the root window to the 'pointer' cursor.
45  *
46  * This function is called when i3 is initialized, because with some login
47  * managers, the root window will not have a cursor otherwise.
48  *
49  * We have a separate xcursor function to use the same X11 connection as the
50  * xcursor_load_cursors() function. If we mix the Xlib and the XCB connection,
51  * races might occur (even though we flush the Xlib connection).
52  *
53  */
54 void xcursor_set_root_cursor(int cursor_id) {
55     XSetWindowAttributes attributes;
56     attributes.cursor = xcursor_get_cursor(cursor_id);
57     XChangeWindowAttributes(xlibdpy, DefaultRootWindow(xlibdpy), CWCursor, &attributes);
58     XFlush(xlibdpy);
59 }
60
61 Cursor xcursor_get_cursor(enum xcursor_cursor_t c) {
62     assert(c >= 0 && c < XCURSOR_CURSOR_MAX);
63     return cursors[c];
64 }
65
66 int xcursor_get_xcb_cursor(enum xcursor_cursor_t c) {
67     assert(c >= 0 && c < XCURSOR_CURSOR_MAX);
68     return xcb_cursors[c];
69 }