i3
src/xcursor.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * xcursor.c: libXcursor support for themed cursors.
00008  *
00009  */
00010 #include <assert.h>
00011 #include <X11/Xcursor/Xcursor.h>
00012 #include <X11/cursorfont.h>
00013 
00014 #include "i3.h"
00015 #include "xcb.h"
00016 #include "xcursor.h"
00017 
00018 static Cursor cursors[XCURSOR_CURSOR_MAX];
00019 
00020 static const int xcb_cursors[XCURSOR_CURSOR_MAX] = {
00021     XCB_CURSOR_LEFT_PTR,
00022     XCB_CURSOR_SB_H_DOUBLE_ARROW,
00023     XCB_CURSOR_SB_V_DOUBLE_ARROW,
00024     XCB_CURSOR_WATCH
00025 };
00026 
00027 static Cursor load_cursor(const char *name) {
00028     Cursor c = XcursorLibraryLoadCursor(xlibdpy, name);
00029     if (c == None)
00030         xcursor_supported = false;
00031     return c;
00032 }
00033 
00034 void xcursor_load_cursors(void) {
00035     cursors[XCURSOR_CURSOR_POINTER] = load_cursor("left_ptr");
00036     cursors[XCURSOR_CURSOR_RESIZE_HORIZONTAL] = load_cursor("sb_h_double_arrow");
00037     cursors[XCURSOR_CURSOR_RESIZE_VERTICAL] = load_cursor("sb_v_double_arrow");
00038     cursors[XCURSOR_CURSOR_WATCH] = load_cursor("watch");
00039 }
00040 
00041 /*
00042  * Sets the cursor of the root window to the 'pointer' cursor.
00043  *
00044  * This function is called when i3 is initialized, because with some login
00045  * managers, the root window will not have a cursor otherwise.
00046  *
00047  * We have a separate xcursor function to use the same X11 connection as the
00048  * xcursor_load_cursors() function. If we mix the Xlib and the XCB connection,
00049  * races might occur (even though we flush the Xlib connection).
00050  *
00051  */
00052 void xcursor_set_root_cursor(int cursor_id) {
00053     XSetWindowAttributes attributes;
00054     attributes.cursor = xcursor_get_cursor(cursor_id);
00055     XChangeWindowAttributes(xlibdpy, DefaultRootWindow(xlibdpy), CWCursor, &attributes);
00056     XFlush(xlibdpy);
00057 }
00058 
00059 Cursor xcursor_get_cursor(enum xcursor_cursor_t c) {
00060     assert(c >= 0 && c < XCURSOR_CURSOR_MAX);
00061     return cursors[c];
00062 }
00063 
00064 int xcursor_get_xcb_cursor(enum xcursor_cursor_t c) {
00065     assert(c >= 0 && c < XCURSOR_CURSOR_MAX);
00066     return xcb_cursors[c];
00067 }