gpu

source of pkg::alloy::window::wayland_protocol

1// The Wayland protocols the window speaks, read from their XML while2// compiling. libwayland marshals each message by the signature and the3// argument types a `wl_interface` gives it. The tables here describe4// those as plain rows, every name and signature in `NAMES`, since a5// compile-time result holds no address; `Protocol::build` lays them out6// as the C structures when a window opens.78import std::string;9import std::vector;10import std::option;11import std::text;12import std::macros;13import std::ffi;1415const WAYLAND = #read_file("protocols/wayland.xml");16const XDG_SHELL = #read_file("protocols/xdg-shell.xml");17const XDG_DECORATION = #read_file("protocols/xdg-decoration-unstable-v1.xml");18const VIEWPORTER = #read_file("protocols/viewporter.xml");19const FRACTIONAL_SCALE = #read_file("protocols/fractional-scale-v1.xml");20const CURSOR_SHAPE = #read_file("protocols/cursor-shape-v1.xml");21const TEXT_INPUT = #read_file("protocols/text-input-unstable-v3.xml");2223pub type InterfaceRow = struct {24    pub name: u32, // where its name starts in NAMES25    pub version: u32,26    pub messages: u32, // its first request in MESSAGES, its events after them27    pub request_count: u32,28    pub event_count: u32,29};3031pub type MessageRow = struct {32    pub name: u32,33    pub signature: u32,34    pub types: u32, // its first argument's in TYPES35};3637// Each name and signature, ending with a NUL.38pub const NAMES: [u8] = copy tables().names.view();39pub const INTERFACES: [InterfaceRow] = copy tables().interfaces.view();40pub const MESSAGES: [MessageRow] = copy tables().messages.view();41// The interface each object or new_id argument is.42pub const TYPES: [Option<u32>] = copy tables().types.view();4344// The interface of that name, as an index into INTERFACES.45pub fn find_interface(name: &[u8]) -> u32 {46    for ([..INTERFACES.length()]) |k| {47        if (name_at(&NAMES, INTERFACES[k].name) == name) {48            return k to u32;49        }50    }51    panic "no such Wayland interface";52}5354// The opcode of the interface's request of that name.55pub fn request(owner: u32, name: &[u8]) -> u32 {56    const row = &INTERFACES[owner to u64];57    for ([..row.request_count to u64]) |k| {58        if (name_at(&NAMES, MESSAGES[row.messages to u64 + k].name) == name) {59            return k to u32;60        }61    }62    panic "no such Wayland request";63}6465// The interface's event of that name, as an index into MESSAGES.66pub fn event(owner: u32, name: &[u8]) -> u32 {67    const row = &INTERFACES[owner to u64];68    const first = row.messages + row.request_count;69    for ([..row.event_count to u64]) |k| {70        if (name_at(&NAMES, MESSAGES[first to u64 + k].name) == name) {71            return first + k to u32;72        }73    }74    panic "no such Wayland event";75}7677// The name starting at the offset, without its NUL.78pub fn name_at(names: &[u8], offset: u32) -> &[u8] {79    var end = offset to u64;80    while (names[end] != 0) {81        end += 1;82    }83    return &names[offset to u64..end];84}8586// The C structures libwayland reads: a `wl_interface` of five words for87// each row, a `wl_message` of three for each message, and the array of88// argument types each message points into.89pub type Protocol = struct {90    interfaces: *var [u64],91    messages: *var [u64],92    types: *var [u64],93};9495pub fn Protocol::build() -> Protocol {96    var p = Protocol {97        .interfaces = new [0 to u64 : INTERFACES.length() * 5],98        .messages = new [0 to u64 : MESSAGES.length() * 3],99        .types = new [0 to u64 : TYPES.length() + 1]100    };101    const names = c_pointer::of(&NAMES[..]) to u64;102    for ([..TYPES.length()]) |k| {103        if (TYPES[k] is ::Some |index|) {104            p.types[k] = p.address(index to u64) to u64;105        }106    }107    const messages = c_pointer::of(&p.messages[..]) to u64;108    const types = c_pointer::of(&p.types[..]) to u64;109    for ([..MESSAGES.length()]) |k| {110        const row = &MESSAGES[k];111        p.messages[k * 3] = names + row.name to u64;112        p.messages[k * 3 + 1] = names + row.signature to u64;113        p.messages[k * 3 + 2] = types + row.types to u64 * 8;114    }115    for ([..INTERFACES.length()]) |k| {116        const row = &INTERFACES[k];117        const requests = messages + row.messages to u64 * 24;118        p.interfaces[k * 5] = names + row.name to u64;119        p.interfaces[k * 5 + 1] = row.version to u64 | (row.request_count to u64 << 32);120        p.interfaces[k * 5 + 2] = requests;121        p.interfaces[k * 5 + 3] = row.event_count to u64;122        p.interfaces[k * 5 + 4] = requests + row.request_count to u64 * 24;123    }124    return move p;125}126127// The `wl_interface` of the interface.128pub fn address(self p: &Protocol, index: u64) -> c_pointer {129    return c_pointer::of(&p.interfaces[index * 5..index * 5 + 5]);130}131132// The index into MESSAGES of a `wl_message` libwayland passes back.133pub fn message_index(self p: &Protocol, message: c_pointer) -> u64 {134    return (message - c_pointer::of(&p.messages[..])) to u64 / 24;135}136137// A request or event being read.138type Pending = struct {139    name: u32,140    request: bool,141    signature: String,142    types: Vector<Option<u32>>, // each argument's interface in `references`143};144145type Tables = struct {146    names: String,147    interfaces: Vector<InterfaceRow>,148    messages: Vector<MessageRow>,149    types: Vector<Option<u32>>, // by name in `references` until resolved150    // the interfaces arguments name, resolved once every file is read151    references: String,152    // the interface being read153    requests: Vector<Pending>,154    events: Vector<Pending>,155    reading: Option<Pending>,156};157158fn tables() -> Tables {159    var t = Tables {160        .names = String::empty(),161        .interfaces = Vector<InterfaceRow>::empty(),162        .messages = Vector<MessageRow>::empty(),163        .types = Vector<Option<u32>>::empty(),164        .references = String::empty(),165        .requests = Vector<Pending>::empty(),166        .events = Vector<Pending>::empty(),167        .reading = ::None168    };169    const texts: [&[u8] : 7] = [170        &WAYLAND,171        &XDG_SHELL,172        &XDG_DECORATION,173        &VIEWPORTER,174        &FRACTIONAL_SCALE,175        &CURSOR_SHAPE,176        &TEXT_INPUT177    ];178    for (texts) |&text| {179        t.read(&text);180    }181    t.resolve();182    return move t;183}184185// Each tag of the XML, comments skipped.186fn read(self t: &var Tables, text: &[u8]) {187    var at: u64 = 0;188    while (text.find_byte('<', at) is ::Some |start|) {189        if (text[start..].starts_with(&"<!--")) {190            at = if (text.find(&"-->", start) is ::Some |end|) end + 3 else text.length();191            continue;192        }193        const end = if (text.find_byte('>', start) is ::Some |e|) e else text.length();194        t.tag(&text[start + 1..end]);195        at = end + 1;196    }197}198199fn tag(self t: &var Tables, tag: &[u8]) {200    const name = &tag_name(&tag);201    match (name) {202        "interface" {203            t.interfaces.push(InterfaceRow {204                .name = add(&var t.names, &attribute(&tag, &"name")),205                .version = number(&attribute(&tag, &"version")),206                .messages = 0,207                .request_count = 0,208                .event_count = 0209            });210        }211        "/interface" {212            t.finish_interface();213        }214        "request", "event" {215            const since = number(&attribute(&tag, &"since"));216            t.reading = ::Some(Pending {217                .name = add(&var t.names, &attribute(&tag, &"name")),218                .request = name == "request",219                .signature = if (since > 1) String::from(since to i64) else String::empty(),220                .types = Vector<Option<u32>>::empty()221            });222            if (tag.ends_with(&"/")) {223                t.finish_message();224            }225        }226        "/request", "/event" {227            t.finish_message();228        }229        "arg" {230            t.argument(&tag);231        }232        else {}233    }234}235236fn argument(self t: &var Tables, tag: &[u8]) {237    const kind = &attribute(&tag, &"type");238    const named = &attribute(&tag, &"interface");239    const message = if (t.reading is ::Some |&var m|) &var m else {240        panic "a Wayland argument outside a message";241    };242    if (attribute(&tag, &"allow-null") == "true") {243        message.signature.push('?');244    }245    if (kind == "new_id" && named.length() == 0) {246        // the interface's name and version, then the object247        message.signature.append(&"sun");248        message.types.push(::None);249        message.types.push(::None);250        message.types.push(::None);251        return;252    }253    message.signature.push(signature_code(&kind));254    if ((kind == "object" || kind == "new_id") && named.length() != 0) {255        message.types.push(::Some(add(&var t.references, &named)));256    } else {257        message.types.push(::None);258    }259}260261fn signature_code(kind: &[u8]) -> u8 {262    return match (kind) {263        "int" 'i';264        "uint" 'u';265        "fixed" 'f';266        "string" 's';267        "object" 'o';268        "new_id" 'n';269        "array" 'a';270        "fd" 'h';271        else {272            panic "an unknown Wayland argument type";273        }274    };275}276277fn finish_message(self t: &var Tables) {278    if (t.reading is ::Some |move m|) {279        t.reading = ::None;280        if (m.request) {281            t.requests.push(move m);282        } else {283            t.events.push(move m);284        }285    }286}287288fn finish_interface(self t: &var Tables) {289    const first = t.messages.length() to u32;290    const request_count = t.requests.length() to u32;291    const event_count = t.events.length() to u32;292    for (t.requests.view()) |&m| {293        t.add_message(&m);294    }295    for (t.events.view()) |&m| {296        t.add_message(&m);297    }298    t.requests.clear();299    t.events.clear();300    var row = &var t.interfaces.get_var(t.interfaces.length() - 1);301    row.messages = first;302    row.request_count = request_count;303    row.event_count = event_count;304}305306fn add_message(self t: &var Tables, m: &Pending) {307    t.messages.push(MessageRow {308        .name = m.name,309        .signature = add(&var t.names, &m.signature.view()),310        .types = t.types.length() to u32311    });312    t.types.append(&m.types.view());313}314315// Each argument's interface named in references to its index; none for316// an interface of a protocol not read.317fn resolve(self t: &var Tables) {318    for ([..t.types.length()]) |k| {319        if (t.types.get(k) is ::Some |reference|) {320            t.types.get_var(k) = t.index_of(reference);321        }322    }323}324325fn index_of(self t: &Tables, reference: u32) -> Option<u32> {326    const name = &name_at(&t.references.view(), reference);327    for ([..t.interfaces.length()]) |k| {328        if (name_at(&t.names.view(), t.interfaces.get(k).name) == name) {329            return ::Some(k to u32);330        }331    }332    return ::None;333}334335// Adds the text and a NUL, giving where it starts.336fn add(names: &var String, text: &[u8]) -> u32 {337    const at = names.length() to u32;338    names.append(&text);339    names.push(0);340    return at;341}342343fn number(text: &[u8]) -> u32 {344    if (text.parse_integer() is ::Some |n|) {345        return n to u32;346    }347    return 0;348}349350// The tag's name, `/interface` for a closing tag.351fn tag_name(tag: &[u8]) -> &[u8] {352    var end: u64 = 0;353    while (end < tag.length() && !is_space(tag[end]) && tag[end] != '/') {354        end += 1;355    }356    if (end == 0 && tag.length() > 0 && tag[0] == '/') {357        end = 1;358        while (end < tag.length() && !is_space(tag[end])) {359            end += 1;360        }361    }362    return &tag[..end];363}364365// The value of the tag's attribute of that name, empty when it has none.366fn attribute(tag: &[u8], name: &[u8]) -> &[u8] {367    var at = tag_name(&tag).length();368    while (at < tag.length()) {369        while (at < tag.length() && is_space(tag[at])) {370            at += 1;371        }372        const start = at;373        while (at < tag.length() && tag[at] != '=') {374            at += 1;375        }376        if (at + 1 >= tag.length() || tag[at + 1] != '"') {377            return &"";378        }379        const key = &tag[start..at];380        const value = at + 2;381        var end = value;382        while (end < tag.length() && tag[end] != '"') {383            end += 1;384        }385        if (key == name) {386            return &tag[value..end];387        }388        at = end + 1;389    }390    return &"";391}392393fn is_space(b: u8) -> bool {394    return b == ' ' || b == '\n' || b == '\t' || b == '\r';395}