From 1d11a4467e7f1605d75cc70cef4e8fdc8cf0f550 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 30 May 2026 13:44:43 +0200 Subject: [PATCH] Improved wayland example --- examples/wayland/wayland.q | 43 +++++++++++++++++++------------------- lib/wayland/State.q | 4 ++-- lib/wayland/const.q | 8 ++----- lib/wayland/opcode.q | 6 ++++++ lib/wayland/pad.q | 3 +++ lib/wayland/write.q | 2 +- 6 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 lib/wayland/opcode.q create mode 100644 lib/wayland/pad.q diff --git a/examples/wayland/wayland.q b/examples/wayland/wayland.q index f3fbc23..57a128a 100644 --- a/examples/wayland/wayland.q +++ b/examples/wayland/wayland.q @@ -13,7 +13,7 @@ main() { } state := new(wayland.State) { - id: 1, + id: wayland.wl_display_id, socket: socket } @@ -91,7 +91,7 @@ read(state *wayland.State, buffer string) -> error { pos := 0 loop { - if pos + wayland.headerSize >= n { + if pos + wayland.header_size >= n { return 0 } @@ -104,16 +104,16 @@ handleMessage(state *wayland.State, msg string) -> int { //assert header.size <= msg.len if header.id == state.wl_registry { - name := [msg.ptr + wayland.headerSize as *uint32] - len := [msg.ptr + wayland.headerSize + 4 as *uint32] - start := wayland.headerSize + 8 + name := [msg.ptr + wayland.header_size as *uint32] + len := [msg.ptr + wayland.header_size + 4 as *uint32] + start := wayland.header_size + 8 end := start + len interface := msg[start..end-1] version := [msg.ptr + end as *uint32] handleGlobal(state, name, interface, version) } - if header.id == wayland.displayId && header.opcode == wayland.displayError { + if header.id == wayland.wl_display_id && header.opcode == wayland.wl_display_error { io.writeLine("wl_display::error") } @@ -142,10 +142,10 @@ handleGlobal(state *wayland.State, name uint32, interface string, _version uint3 getRegistry(state *wayland.State, buffer string) -> error { state.wl_registry = wayland.newId(state) - size := wayland.headerSize + 4 + size := wayland.header_size + 4 ptr := buffer.ptr - ptr = wayland.write32(ptr, wayland.displayId) - ptr = wayland.write16(ptr, wayland.displayGetRegistry) + ptr = wayland.write32(ptr, wayland.wl_display_id) + ptr = wayland.write16(ptr, wayland.wl_display_get_registry) ptr = wayland.write16(ptr, size) ptr = wayland.write32(ptr, state.wl_registry) _, err := io.writeTo(state.socket, buffer[..size]) @@ -153,9 +153,8 @@ getRegistry(state *wayland.State, buffer string) -> error { } bindCompositor(state *wayland.State, buffer string, interface string) -> error { - padded := interface.len + 1 - padded = (padded + 3) & -4 - size := wayland.headerSize + 4 + 4 + padded + 4 + 4 + padded := wayland.pad(interface.len + 1) + size := bindSize(padded) if size > buffer.len { return -1 @@ -163,7 +162,7 @@ bindCompositor(state *wayland.State, buffer string, interface string) -> error { ptr := buffer.ptr ptr = wayland.write32(ptr, state.wl_registry) - ptr = wayland.write16(ptr, wayland.registryBind) + ptr = wayland.write16(ptr, wayland.wl_registry_bind) ptr = wayland.write16(ptr, size) ptr = wayland.write32(ptr, state.wl_compositor_name) ptr = wayland.writeString(ptr, interface) @@ -174,9 +173,8 @@ bindCompositor(state *wayland.State, buffer string, interface string) -> error { } bindShm(state *wayland.State, buffer string, interface string) -> error { - padded := interface.len + 1 - padded = (padded + 3) & -4 - size := wayland.headerSize + 4 + 4 + padded + 4 + 4 + padded := wayland.pad(interface.len + 1) + size := bindSize(padded) if size > buffer.len { return -1 @@ -184,7 +182,7 @@ bindShm(state *wayland.State, buffer string, interface string) -> error { ptr := buffer.ptr ptr = wayland.write32(ptr, state.wl_registry) - ptr = wayland.write16(ptr, wayland.registryBind) + ptr = wayland.write16(ptr, wayland.wl_registry_bind) ptr = wayland.write16(ptr, size) ptr = wayland.write32(ptr, state.wl_shm_name) ptr = wayland.writeString(ptr, interface) @@ -195,9 +193,8 @@ bindShm(state *wayland.State, buffer string, interface string) -> error { } bindXdgWmBase(state *wayland.State, buffer string, interface string) -> error { - padded := interface.len + 1 - padded = (padded + 3) & -4 - size := wayland.headerSize + 4 + 4 + padded + 4 + 4 + padded := wayland.pad(interface.len + 1) + size := bindSize(padded) if size > buffer.len { return -1 @@ -205,7 +202,7 @@ bindXdgWmBase(state *wayland.State, buffer string, interface string) -> error { ptr := buffer.ptr ptr = wayland.write32(ptr, state.wl_registry) - ptr = wayland.write16(ptr, wayland.registryBind) + ptr = wayland.write16(ptr, wayland.wl_registry_bind) ptr = wayland.write16(ptr, size) ptr = wayland.write32(ptr, state.xdg_wm_base_name) ptr = wayland.writeString(ptr, interface) @@ -213,4 +210,8 @@ bindXdgWmBase(state *wayland.State, buffer string, interface string) -> error { ptr = wayland.write32(ptr, state.xdg_wm_base) _, err := io.writeTo(state.socket, buffer[..size]) return err +} + +bindSize(padded uint) -> uint16 { + return wayland.header_size + 4 + 4 + padded + 4 + 4 } \ No newline at end of file diff --git a/lib/wayland/State.q b/lib/wayland/State.q index 603db2f..ccd5f6c 100644 --- a/lib/wayland/State.q +++ b/lib/wayland/State.q @@ -8,8 +8,8 @@ State { xdg_wm_base uint32 wl_compositor_name uint32 - wl_shm_name uint32 - xdg_wm_base_name uint32 + wl_shm_name uint32 + xdg_wm_base_name uint32 } newId(state *State) -> uint32 { diff --git a/lib/wayland/const.q b/lib/wayland/const.q index 96193cb..811151b 100644 --- a/lib/wayland/const.q +++ b/lib/wayland/const.q @@ -1,8 +1,4 @@ const { - displayGetRegistry = 1 - registryGlobal = 0 - registryBind = 0 - displayError = 0 - displayId = 1 - headerSize = 8 + header_size = 8 + wl_display_id = 1 } \ No newline at end of file diff --git a/lib/wayland/opcode.q b/lib/wayland/opcode.q new file mode 100644 index 0000000..49a57cf --- /dev/null +++ b/lib/wayland/opcode.q @@ -0,0 +1,6 @@ +const { + wl_display_error = 0 + wl_display_get_registry = 1 + wl_registry_bind = 0 + wl_registry_global = 0 +} \ No newline at end of file diff --git a/lib/wayland/pad.q b/lib/wayland/pad.q new file mode 100644 index 0000000..05f3f3f --- /dev/null +++ b/lib/wayland/pad.q @@ -0,0 +1,3 @@ +pad(length uint) -> uint { + return (length + 3) & -4 +} \ No newline at end of file diff --git a/lib/wayland/write.q b/lib/wayland/write.q index b7664db..81fd5f6 100644 --- a/lib/wayland/write.q +++ b/lib/wayland/write.q @@ -13,7 +13,7 @@ write32(ptr *byte, v uint32) -> *byte { writeString(ptr *byte, s string) -> *byte { len := s.len + 1 ptr = write32(ptr, len as uint32) - padded := (len + 3) & -4 + padded := pad(len) loop i := 0..s.len { ptr[i] = s.ptr[i] -- 2.51.2