raiguard/odin-wayland

git clone https://git.raiguard.me/raiguard/odin-wayland

Caleb HeuerMove generated -> mockgenfd8ef50

trunk
1.6 KiB55 linesraw
1#+build linux
2package wayland
3
4import "base:intrinsics"
5import "core:fmt"
6import "core:io"
7import "core:sys/linux"
8
9_marshal_raw :: proc(stream: io.Stream, value: $T) -> io.Error
10	where T == u32 || T == i32 || T == Message_Header
11{
12	data := transmute([size_of(T)]byte) value
13	io.write(stream, data[:]) or_return
14	return nil
15}
16
17_marshal_string :: proc(stream: io.Stream, value: string) -> io.Error {
18	length := len(value) + 1 // +1 for null terminator
19	_marshal_raw(stream, u32(length)) or_return
20	io.write(stream, transmute([]byte) value) or_return
21	io.write(stream, {0}) or_return // null terminator
22	_marshal_padding(stream, length) or_return
23	return nil
24}
25
26_marshal_fixed :: proc(stream: io.Stream, value: Fixed) -> io.Error {
27	return _marshal_raw(stream, value.i)
28}
29
30_marshal_primitive :: proc(stream: io.Stream, primitive: Message_Primitive) -> io.Error {
31	switch value in primitive {
32	case u32:
33		return _marshal_raw(stream, value)
34	case i32:
35		return _marshal_raw(stream, value)
36	case Object_ID:
37		return _marshal_raw(stream, u32(value))
38	case Fixed:
39		return _marshal_fixed(stream, value)
40	case string:
41		return _marshal_string(stream, value)
42	case []byte, linux.Fd:
43		unimplemented(fmt.aprint(primitive))
44	}
45	panic(fmt.aprint("Invalid union variant passed to _marshal_primitive:", primitive))
46}
47
48// This is simply moving the cursor forward by remainder bytes - there really ought to be an io.discard() for streams...
49_marshal_padding :: proc(stream: io.Stream, written_length: int) -> io.Error {
50	@(static, rodata)
51	padding_buf: [4]byte
52	remainder := (4 - (written_length % 4)) % 4
53	io.write(stream, padding_buf[:remainder]) or_return
54	return nil
55}