From 69fc6449dc96045e6fecd31b211b928017fc4f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Tarl=C3=A1=20Cardoso=20Lemos?= Date: Fri, 26 Nov 2010 21:26:51 -0200 Subject: [PATCH] libXcursor support (themed cursors). --- DEPENDS | 1 + Makefile | 2 +- common.mk | 1 + debian/control | 2 +- include/all.h | 1 + include/i3.h | 4 ++-- include/xcb.h | 3 ++- include/xcursor.h | 17 +++++++++++++++ src/main.c | 24 +++++++++++++++++++++ src/x.c | 2 +- src/xcb.c | 55 ++++++++++++++++++++++++++--------------------- src/xcursor.c | 42 ++++++++++++++++++++++++++++++++++++ 12 files changed, 123 insertions(+), 31 deletions(-) create mode 100644 include/xcursor.h create mode 100644 src/xcursor.c diff --git a/DEPENDS b/DEPENDS index b7a6fefb..5ca6d8c4 100644 --- a/DEPENDS +++ b/DEPENDS @@ -11,6 +11,7 @@ mentioned below until a fix is provided. * yajl (the IPC interface uses JSON to serialize data) * asciidoc >= 8.3.0 for docs/hacking-howto * asciidoc, xmlto, docbook-xml for man/i3.man + * libxcursor * Xlib, the one that comes with your X-Server * x11-utils for xmessage (only for displaying the welcome message, so this is mainly interesting for distributors) diff --git a/Makefile b/Makefile index 8aaf20cf..97e0dd80 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ include $(TOPDIR)/common.mk # Depend on the object files of all source-files in src/*.c and on all header files AUTOGENERATED:=src/cfgparse.tab.c src/cfgparse.yy.c src/cmdparse.tab.c src/cmdparse.yy.c -FILES:=src/ipc.c src/main.c src/log.c src/util.c src/tree.c src/xcb.c src/manage.c src/workspace.c src/x.c src/floating.c src/click.c src/config.c src/handlers.c src/randr.c src/xinerama.c src/con.c src/load_layout.c src/render.c src/window.c src/match.c +FILES:=src/ipc.c src/main.c src/log.c src/util.c src/tree.c src/xcb.c src/manage.c src/workspace.c src/x.c src/floating.c src/click.c src/config.c src/handlers.c src/randr.c src/xinerama.c src/con.c src/load_layout.c src/render.c src/window.c src/match.c src/xcursor.c FILES:=$(FILES:.c=.o) HEADERS:=$(filter-out include/loglevels.h,$(wildcard include/*.h)) diff --git a/common.mk b/common.mk index 49298049..9ac6dcd2 100644 --- a/common.mk +++ b/common.mk @@ -33,6 +33,7 @@ LDFLAGS += -lxcb-xinerama LDFLAGS += -lxcb-randr LDFLAGS += -lxcb LDFLAGS += -lyajl +LDFLAGS += -lXcursor LDFLAGS += -lX11 LDFLAGS += -lev LDFLAGS += -L/usr/local/lib -L/usr/pkg/lib diff --git a/debian/control b/debian/control index 8dde5b80..39446178 100644 --- a/debian/control +++ b/debian/control @@ -3,7 +3,7 @@ Section: utils Priority: extra Maintainer: Michael Stapelberg DM-Upload-Allowed: yes -Build-Depends: debhelper (>= 5), libx11-dev, libxcb-aux0-dev (>= 0.3.3), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-randr0-dev, libxcb-event1-dev (>= 0.3.3), libxcb-property1-dev (>= 0.3.3), libxcb-atom1-dev (>= 0.3.3), libxcb-icccm1-dev (>= 0.3.3), asciidoc (>= 8.4.4), xmlto, docbook-xml, pkg-config, libev-dev, flex, bison, libyajl-dev +Build-Depends: debhelper (>= 5), libx11-dev, libxcb-aux0-dev (>= 0.3.3), libxcb-keysyms1-dev, libxcb-xinerama0-dev (>= 1.1), libxcb-randr0-dev, libxcb-event1-dev (>= 0.3.3), libxcb-property1-dev (>= 0.3.3), libxcb-atom1-dev (>= 0.3.3), libxcb-icccm1-dev (>= 0.3.3), libxcursor-dev, asciidoc (>= 8.4.4), xmlto, docbook-xml, pkg-config, libev-dev, flex, bison, libyajl-dev Standards-Version: 3.8.3 Homepage: http://i3.zekjur.net/ diff --git a/include/all.h b/include/all.h index 2096016d..1625f16c 100644 --- a/include/all.h +++ b/include/all.h @@ -51,5 +51,6 @@ #include "window.h" #include "match.h" #include "cmdparse.h" +#include "xcursor.h" #endif diff --git a/include/i3.h b/include/i3.h index e437943e..ed8cacb6 100644 --- a/include/i3.h +++ b/include/i3.h @@ -26,7 +26,7 @@ extern xcb_connection_t *conn; extern xcb_key_symbols_t *keysyms; extern char **start_argv; -extern Display *xkbdpy; +extern Display *xlibdpy, *xkbdpy; extern int xkb_current_group; extern TAILQ_HEAD(bindings_head, Binding) *bindings; extern TAILQ_HEAD(autostarts_head, Autostart) autostarts; @@ -35,7 +35,7 @@ extern SLIST_HEAD(stack_wins_head, Stack_Window) stack_wins; extern xcb_event_handlers_t evenths; extern xcb_property_handlers_t prophs; extern uint8_t root_depth; -extern bool xkb_supported; +extern bool xcursor_supported, xkb_supported; extern xcb_atom_t atoms[NUM_ATOMS]; extern xcb_window_t root; diff --git a/include/xcb.h b/include/xcb.h index 3752e9f4..4b01d900 100644 --- a/include/xcb.h +++ b/include/xcb.h @@ -12,6 +12,7 @@ #define _XCB_H #include "data.h" +#include "xcursor.h" #define _NET_WM_STATE_REMOVE 0 #define _NET_WM_STATE_ADD 1 @@ -94,7 +95,7 @@ uint32_t get_colorpixel(char *hex); * */ xcb_window_t create_window(xcb_connection_t *conn, Rect r, uint16_t window_class, - int cursor, bool map, uint32_t mask, uint32_t *values); + enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values); /** * Changes a single value in the graphic context (so one doesn’t have to diff --git a/include/xcursor.h b/include/xcursor.h new file mode 100644 index 00000000..4b5326d5 --- /dev/null +++ b/include/xcursor.h @@ -0,0 +1,17 @@ +#ifndef _XCURSOR_CURSOR_H +#define _XCURSOR_CURSOR_H + +#include + +enum xcursor_cursor_t { + XCURSOR_CURSOR_POINTER = 0, + XCURSOR_CURSOR_RESIZE_HORIZONTAL, + XCURSOR_CURSOR_RESIZE_VERTICAL, + XCURSOR_CURSOR_MAX +}; + +extern void xcursor_load_cursors(); +extern Cursor xcursor_get_cursor(enum xcursor_cursor_t c); +extern int xcursor_get_xcb_cursor(enum xcursor_cursor_t c); + +#endif diff --git a/src/main.c b/src/main.c index 2d52ef8a..121100b5 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ * vim:ts=4:sw=4:expandtab */ #include +#include #include #include "all.h" @@ -23,6 +24,9 @@ uint8_t root_depth; xcb_key_symbols_t *keysyms; +/* Those are our connections to X11 for use with libXcursor and XKB */ +Display *xlibdpy, *xkbdpy; + /* The list of key bindings */ struct bindings_head *bindings; @@ -32,6 +36,10 @@ struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts); /* The list of assignments */ struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments); +/* We hope that those are supported and set them to true */ +bool xcursor_supported = true; +bool xkb_supported = true; + /* * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb. * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop @@ -195,6 +203,22 @@ int main(int argc, char *argv[]) { REQUEST_ATOM(_NET_ACTIVE_WINDOW); REQUEST_ATOM(_NET_WORKAREA); + /* Initialize the Xlib connection */ + xlibdpy = xkbdpy = XOpenDisplay(NULL); + + /* Try to load the X cursors and initialize the XKB extension */ + if (xlibdpy == NULL) { + ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n"); + xcursor_supported = false; + xkb_supported = false; + } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) { + ELOG("Could not set FD_CLOEXEC on xkbdpy\n"); + return 1; + } else { + xcursor_load_cursors(); + /*init_xkb();*/ + } + memset(&evenths, 0, sizeof(xcb_event_handlers_t)); memset(&prophs, 0, sizeof(xcb_property_handlers_t)); diff --git a/src/x.c b/src/x.c index 2deb37c1..ca292b2c 100644 --- a/src/x.c +++ b/src/x.c @@ -82,7 +82,7 @@ void x_con_init(Con *con) { values[1] = FRAME_EVENT_MASK; Rect dims = { -15, -15, 10, 10 }; - con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, -1, false, mask, values); + con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values); con->gc = xcb_generate_id(conn); xcb_create_gc(conn, con->gc, con->frame, 0, 0); diff --git a/src/xcb.c b/src/xcb.c index 78b7a3b1..765c1894 100644 --- a/src/xcb.c +++ b/src/xcb.c @@ -80,40 +80,45 @@ uint32_t get_colorpixel(char *hex) { * for errors. * */ -xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, int cursor, - bool map, uint32_t mask, uint32_t *values) { - xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root; - xcb_window_t result = xcb_generate_id(conn); - xcb_cursor_t cursor_id = xcb_generate_id(conn); +xcb_window_t create_window(xcb_connection_t *conn, Rect dims, uint16_t window_class, + enum xcursor_cursor_t cursor, bool map, uint32_t mask, uint32_t *values) { + xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root; + xcb_window_t result = xcb_generate_id(conn); + xcb_cursor_t cursor_id = xcb_generate_id(conn); - /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */ - uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT); + /* If the window class is XCB_WINDOW_CLASS_INPUT_ONLY, depth has to be 0 */ + uint16_t depth = (window_class == XCB_WINDOW_CLASS_INPUT_ONLY ? 0 : XCB_COPY_FROM_PARENT); - xcb_create_window(conn, - depth, - result, /* the window id */ - root, /* parent == root */ - dims.x, dims.y, dims.width, dims.height, /* dimensions */ - 0, /* border = 0, we draw our own */ - window_class, - XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */ - mask, - values); + xcb_create_window(conn, + depth, + result, /* the window id */ + root, /* parent == root */ + dims.x, dims.y, dims.width, dims.height, /* dimensions */ + 0, /* border = 0, we draw our own */ + window_class, + XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */ + mask, + values); - /* Set the cursor */ + /* Set the cursor */ + if (xcursor_supported) { + mask = XCB_CW_CURSOR; + values[0] = xcursor_get_cursor(cursor); + xcb_change_window_attributes(conn, result, mask, values); + } else { i3Font *cursor_font = load_font(conn, "cursor"); + int xcb_cursor = xcursor_get_xcb_cursor(cursor); xcb_create_glyph_cursor(conn, cursor_id, cursor_font->id, cursor_font->id, - (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor), - (cursor == -1 ? XCB_CURSOR_LEFT_PTR : cursor) + 1, - 0, 0, 0, 65535, 65535, 65535); + xcb_cursor, xcb_cursor + 1, 0, 0, 0, 65535, 65535, 65535); xcb_change_window_attributes(conn, result, XCB_CW_CURSOR, &cursor_id); xcb_free_cursor(conn, cursor_id); + } - /* Map the window (= make it visible) */ - if (map) - xcb_map_window(conn, result); + /* Map the window (= make it visible) */ + if (map) + xcb_map_window(conn, result); - return result; + return result; } /* diff --git a/src/xcursor.c b/src/xcursor.c new file mode 100644 index 00000000..2e21aab1 --- /dev/null +++ b/src/xcursor.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#include "i3.h" +#include "xcb.h" +#include "xcursor.h" + +static Cursor cursors[XCURSOR_CURSOR_MAX]; + +static const int xcb_cursors[XCURSOR_CURSOR_MAX] = { + XCB_CURSOR_LEFT_PTR, + XCB_CURSOR_SB_H_DOUBLE_ARROW, + XCB_CURSOR_SB_V_DOUBLE_ARROW +}; + +static Cursor load_cursor(const char *name, int font) +{ + Cursor c = XcursorLibraryLoadCursor(xlibdpy, name); + if (c == None) + c = XCreateFontCursor(xlibdpy, font); + return c; +} + +void xcursor_load_cursors() +{ + cursors[XCURSOR_CURSOR_POINTER] = load_cursor("left_ptr", XC_left_ptr); + cursors[XCURSOR_CURSOR_RESIZE_HORIZONTAL] = load_cursor("sb_h_double_arrow", XC_sb_h_double_arrow); + cursors[XCURSOR_CURSOR_RESIZE_VERTICAL] = load_cursor("sb_v_double_arrow", XC_sb_v_double_arrow); +} + +Cursor xcursor_get_cursor(enum xcursor_cursor_t c) +{ + assert(c >= 0 && c < XCURSOR_CURSOR_MAX); + return cursors[c]; +} + +int xcursor_get_xcb_cursor(enum xcursor_cursor_t c) +{ + assert(c >= 0 && c < XCURSOR_CURSOR_MAX); + return xcb_cursors[c]; +}