libXcursor support (themed cursors).
This commit is contained in:
committed by
Michael Stapelberg
parent
3bab222aa7
commit
69fc6449dc
24
src/main.c
24
src/main.c
@ -2,6 +2,7 @@
|
||||
* vim:ts=4:sw=4:expandtab
|
||||
*/
|
||||
#include <ev.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#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));
|
||||
|
||||
|
2
src/x.c
2
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);
|
||||
|
||||
|
55
src/xcb.c
55
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
42
src/xcursor.c
Normal file
42
src/xcursor.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <assert.h>
|
||||
#include <X11/Xcursor/Xcursor.h>
|
||||
#include <X11/cursorfont.h>
|
||||
|
||||
#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];
|
||||
}
|
Reference in New Issue
Block a user