Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-window

Source of release 0.6.2

0.6.2 / src / window / linux / cursor.lucb

#==============================================================================================
#
#   cursor - The X11 content cursor
#
#   DESCRIPTION:
#       Cursors from the core cursor font, defined on the window so the server shows them
#       whenever the pointer is over its content. The window owns its current cursor.
#
#==============================================================================================

## The cursor-font glyph for a portable cursor (X11/cursorfont.h).
func x11_cursor_shape(cursor: input.Cursor) -> u32:
    match cursor:
        .text: return 152
        .pointer: return 60
        .resize_horizontal: return 108
        .resize_vertical: return 116
        .grab: return 58
        .grabbing: return 52
        .not_allowed: return 0
        .crosshair: return 34
        _: return 68

func x11_set_cursor(state: State*, cursor: input.Cursor) -> !:
    if state.x_cursor != 0 and state.cursor == cursor:
        return
    let x = try x11_connect()
    let created = try x11_blank_cursor(&x, state) if cursor == input.Cursor.hidden else x.api.create_font_cursor(x.display, x11_cursor_shape(cursor))
    if created == 0:
        error(failed, "X11 could not create a system cursor")
    _ = x.api.define_cursor(x.display, state.x_window, created)
    if state.x_cursor != 0:
        _ = x.api.free_cursor(x.display, state.x_cursor)
    state.x_cursor = created
    state.cursor = cursor
    _ = x.api.flush(x.display)

## A cursor with nothing to show: a one-pixel bitmap that masks itself out.
func x11_blank_cursor(x: const X11Connection*, state: State*) -> u64!:
    var bits: u8[1] = [0]
    let pixmap = x.api.create_bitmap_from_data(x.display, state.x_window, &bits[0], 1, 1)
    if pixmap == 0:
        error(failed, "X11 could not make an empty cursor")
    defer _ = x.api.free_pixmap(x.display, pixmap)
    # XColor: pixel, red, green, blue, flags, pad — zero is black, unseen here.
    var color: u8[16]
    for index in 0..<(usize)16:
        color[index] = 0
    return x.api.create_pixmap_cursor(x.display, pixmap, pixmap, (void*)&color[0], (void*)&color[0], 0, 0)