raiguard/odin-wayland
git clone https://git.raiguard.me/raiguard/odin-wayland
fd8ef50
trunk
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 10where T == u32 || T == i32 || T == Message_Header 11{ 12data := transmute ([ size_of ( T )] byte ) value 13io . write ( stream , data [:]) or_return 14return nil 15} 16 17_marshal_string :: proc ( stream : io . Stream , value : string ) -> io . Error { 18length := len ( value ) + 1 // +1 for null terminator 19_marshal_raw ( stream , u32 ( length )) or_return 20io . write ( stream , transmute ([] byte ) value ) or_return 21io . write ( stream , { 0 }) or_return // null terminator 22_marshal_padding ( stream , length ) or_return 23return nil 24} 25 26_marshal_fixed :: proc ( stream : io . Stream , value : Fixed ) -> io . Error { 27return _marshal_raw ( stream , value . i ) 28} 29 30_marshal_primitive :: proc ( stream : io . Stream , primitive : Message_Primitive ) -> io . Error { 31switch value in primitive { 32case u32 : 33return _marshal_raw ( stream , value ) 34case i32 : 35return _marshal_raw ( stream , value ) 36case Object_ID : 37return _marshal_raw ( stream , u32 ( value )) 38case Fixed : 39return _marshal_fixed ( stream , value ) 40case string : 41return _marshal_string ( stream , value ) 42case [] byte , linux . Fd : 43unimplemented ( fmt . aprint ( primitive )) 44} 45panic ( 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 ) 51padding_buf : [ 4 ] byte 52remainder := ( 4 - ( written_length % 4 )) % 4 53io . write ( stream , padding_buf [: remainder ]) or_return 54return nil 55}