draw

source of pkg::alloy::window::xkb

1// The keyboard on Wayland: the compositor sends its keymap in xkbcommon's2// text format, which libxkbcommon turns into keys, text and modifiers for3// each key code (loaded while running, like libwayland-client).45import std::ffi;6import std::option;7import std::string;89import window;10import loading;1112type Functions = struct {13    context_new: extern (i32) -> c_pointer,14    keymap_new_from_buffer: extern (c_pointer, c_pointer, c_size, i32, i32) -> c_pointer,15    keymap_unref: extern (c_pointer),16    keymap_key_repeats: extern (c_pointer, u32) -> i32,17    keymap_key_get_syms_by_level: extern (c_pointer, u32, u32, u32, c_pointer) -> i32,18    state_new: extern (c_pointer) -> c_pointer,19    state_unref: extern (c_pointer),20    state_update_mask: extern (c_pointer, u32, u32, u32, u32, u32, u32) -> i32,21    state_key_get_one_sym: extern (c_pointer, u32) -> u32,22    state_key_get_utf8: extern (c_pointer, u32, c_pointer, c_size) -> i32,23    state_key_get_layout: extern (c_pointer, u32) -> u32,24    state_mod_name_is_active: extern (c_pointer, c_pointer, i32) -> i32,25    keysym_to_utf32: extern (u32) -> u32,26};2728pub type Keyboard = struct {29    functions: Functions,30    context: c_pointer,31    keymap: c_pointer, // 0 until the compositor sends one32    state: c_pointer,33    held: Modifiers, // as the last update left them34};3536pub fn Keyboard::load() -> Option<Keyboard> {37    const library = if (Library::open(&"libxkbcommon.so.0") is ::Some |l|) l else {38        return ::None;39    };40    const functions = Functions {41        .context_new = function(&library, &"xkb_context_new"),42        .keymap_new_from_buffer = function(&library, &"xkb_keymap_new_from_buffer"),43        .keymap_unref = function(&library, &"xkb_keymap_unref"),44        .keymap_key_repeats = function(&library, &"xkb_keymap_key_repeats"),45        .keymap_key_get_syms_by_level = function(&library, &"xkb_keymap_key_get_syms_by_level"),46        .state_new = function(&library, &"xkb_state_new"),47        .state_unref = function(&library, &"xkb_state_unref"),48        .state_update_mask = function(&library, &"xkb_state_update_mask"),49        .state_key_get_one_sym = function(&library, &"xkb_state_key_get_one_sym"),50        .state_key_get_utf8 = function(&library, &"xkb_state_key_get_utf8"),51        .state_key_get_layout = function(&library, &"xkb_state_key_get_layout"),52        .state_mod_name_is_active = function(&library, &"xkb_state_mod_name_is_active"),53        .keysym_to_utf32 = function(&library, &"xkb_keysym_to_utf32")54    };55    const context = functions.context_new(0);56    if (context == 0) {57        return ::None;58    }59    return ::Some(Keyboard {60        .functions = functions,61        .context = context,62        .keymap = 0,63        .state = 0,64        .held = Modifiers::none()65    });66}6768// Takes the keymap's text, in the format xkbcommon's version 1.69pub fn set_keymap(self k: &var Keyboard, text: &[u8]) {70    var length = text.length();71    if (length > 0 && text[length - 1] == 0) {72        length -= 1;73    }74    const keymap = k.functions.keymap_new_from_buffer(75        k.context,76        c_pointer::of(&text),77        length to c_size,78        1,79        080    );81    if (keymap == 0) {82        return;83    }84    k.release();85    k.keymap = keymap;86    k.state = k.functions.state_new(keymap);87    k.held = k.read_modifiers();88}8990pub fn release(self k: &var Keyboard) {91    if (k.state != 0) {92        k.functions.state_unref(k.state);93        k.functions.keymap_unref(k.keymap);94        k.state = 0;95        k.keymap = 0;96    }97}9899pub fn update_modifiers(100    self k: &var Keyboard,101    depressed: u32,102    latched: u32,103    locked: u32,104    group: u32105) {106    if (k.state != 0) {107        k.functions.state_update_mask(k.state, depressed, latched, locked, 0, 0, group);108    }109    k.held = k.read_modifiers();110}111112pub fn modifiers(self k: &Keyboard) -> Modifiers => k.held;113114fn read_modifiers(self k: &Keyboard) -> Modifiers {115    return Modifiers {116        .shift = k.active(&"Shift\0"),117        .control = k.active(&"Control\0"),118        .alt = k.active(&"Mod1\0"),119        .super = k.active(&"Mod4\0")120    };121}122123fn active(self k: &Keyboard, name: &[u8]) -> bool {124    // XKB_STATE_MODS_EFFECTIVE125    return k.state != 0126        && k.functions.state_mod_name_is_active(k.state, c_pointer::of(&name), 8) == 1;127}128129// The key of the Linux key code a wl_keyboard event gives.130pub fn key(self k: &Keyboard, code: u32) -> Key {131    if (k.state == 0) {132        return Key::Unknown(code);133    }134    const keycode = code + 8;135    const symbol = k.functions.state_key_get_one_sym(k.state, keycode);136    if (named_key(symbol) is ::Some |named|) {137        return named;138    }139    // the character of the key alone, with no Shift: its first level140    const layout = k.functions.state_key_get_layout(k.state, keycode);141    var symbols: c_pointer = 0;142    const count = k.functions.keymap_key_get_syms_by_level(143        k.keymap,144        keycode,145        layout,146        0,147        c_pointer::from(&var symbols)148    );149    const base = if (count > 0) symbols.view<u32>(1)[0] else symbol;150    const character = k.functions.keysym_to_utf32(base);151    if (character != 0) {152        return Key::Character(lower(character));153    }154    return Key::Unknown(symbol);155}156157// What the key types with the modifiers held, as UTF-8: nothing for keys158// writing no text and while Control or Alt is held.159pub fn text(self k: &Keyboard, code: u32) -> String {160    var out = String::empty();161    if (k.state == 0 || k.active(&"Control\0") || k.active(&"Mod1\0")) {162        return move out;163    }164    var buffer: [u8 : 64] = [0 : 64];165    const length = k.functions.state_key_get_utf8(166        k.state,167        code + 8,168        c_pointer::of(&buffer[..]),169        64170    );171    if (length <= 0 || length >= 64) {172        return move out;173    }174    for (buffer[..length to u64]) |byte| {175        if (byte >= 32 && byte != 127) {176            out.push(byte);177        }178    }179    return move out;180}181182pub fn repeats(self k: &Keyboard, code: u32) -> bool {183    return k.keymap != 0 && k.functions.keymap_key_repeats(k.keymap, code + 8) == 1;184}185186fn lower(character: u32) -> u32 {187    if (character >= 'A' && character <= 'Z') {188        return character + 32;189    }190    return character;191}192193// The key an xkbcommon key symbol names, when it writes no character.194fn named_key(symbol: u32) -> Option<Key> {195    if (symbol >= 0xFFBE && symbol <= 0xFFD5) {196        return ::Some(Key::Function((symbol - 0xFFBE + 1) to u8));197    }198    return match (symbol) {199        0xFF0D, 0xFF8D ::Some(Key::Enter); // Return, KP_Enter200        0xFF1B ::Some(Key::Escape);201        0xFF08 ::Some(Key::Backspace);202        0xFF09, 0xFE20 ::Some(Key::Tab); // Tab, ISO_Left_Tab203        0xFFFF ::Some(Key::Delete);204        0xFF63 ::Some(Key::Insert);205        0xFF51 ::Some(Key::Left);206        0xFF53 ::Some(Key::Right);207        0xFF52 ::Some(Key::Up);208        0xFF54 ::Some(Key::Down);209        0xFF50 ::Some(Key::Home);210        0xFF57 ::Some(Key::End);211        0xFF55 ::Some(Key::PageUp);212        0xFF56 ::Some(Key::PageDown);213        0xFFE1, 0xFFE2 ::Some(Key::Shift);214        0xFFE3, 0xFFE4 ::Some(Key::Control);215        0xFFE9, 0xFFEA ::Some(Key::Alt);216        0xFFEB, 0xFFEC ::Some(Key::Super);217        0xFFE5 ::Some(Key::CapsLock);218        0xFF67 ::Some(Key::Menu);219        else ::None;220    };221}