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