raiguard/odin-wayland

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

Caleb HeuerMove generated -> mockgenfd8ef50

trunk
1.6 KiB56 linesraw
1#+build linux
2package wayland
3
4import "base:runtime"
5import "core:io"
6
7_unmarshal_raw :: proc(stream: io.Stream, $Value_Type: typeid) -> (value: Value_Type, error: io.Error)
8	where Value_Type == u32 || Value_Type == i32 || Value_Type == Object_ID || Value_Type == Message_Header
9{
10	VALUE_LEN :: size_of(Value_Type)
11	bytes: [VALUE_LEN]byte
12	n_read := io.read(stream, bytes[:]) or_return
13	if n_read < VALUE_LEN {
14		return {}, .Unexpected_EOF
15	}
16	return transmute(Value_Type) bytes, nil
17}
18
19_unmarshal_fixed :: proc(stream: io.Stream) -> (value: Fixed, error: io.Error) {
20	raw_value := _unmarshal_raw(stream, u32) or_return
21	return Fixed{i=raw_value}, nil
22}
23
24_unmarshal_string :: proc(stream: io.Stream, allocator: runtime.Allocator) -> (str: string, error: io.Error) {
25	str_len := _unmarshal_raw(stream, u32) or_return
26	if str_len == 0 {
27		return
28	}
29	// -1 because the string ends will a null terminator.
30	buf := make([]byte, str_len - 1, allocator)
31	n_read := io.read(stream, buf) or_return
32	if n_read < len(buf) {
33		error = .Unexpected_EOF
34		return
35	}
36
37	// Pad to 4 bytes
38	@(static)
39	padding_buf: [4]byte
40	remainder := (4 - (str_len % 4)) % 4
41	// +1 for unread null terminator.
42	io.read(stream, padding_buf[:remainder+1])
43
44	return string(buf), nil
45}
46
47_unmarshal_to :: proc(destination: ^$T, stream: io.Stream, allocator: runtime.Allocator) -> io.Error {
48	when T == u32 || T == i32 || T == Object_ID || T == Message_Header {
49		destination^ = _unmarshal_raw(stream, T) or_return
50	} else when T == Fixed {
51		destination^ = _unmarshal_fixed(stream) or_return
52	} else when T == string {
53		destination^ = _unmarshal_string(stream, allocator) or_return
54	}
55	return nil
56}