raiguard/odin-wayland
git clone https://git.raiguard.me/raiguard/odin-wayland
fd8ef50
trunk
1package wayland 2 3import "base:intrinsics" 4import "base:runtime" 5import "core:io" 6import "core:log" 7import "core:math/fixed" 8import "core:os" 9import "core:path/filepath" 10import "core:strconv" 11import "core:strings" 12import "core:sys/linux" 13 14_allocate_object :: proc ( session : ^ Session , kind : Object_Kind ) -> Object_ID { 15id := session . next_id 16session . next_id += 1 17 18session . objects [ id ] = { kind = kind } 19 20return id 21} 22 23_get_object_interface_name :: proc ( session : ^ Session , object_id : Object_ID ) -> string { 24if object_info , ok := session . objects [ object_id ]; ok { 25return object_kind_to_interface_name [ object_info . kind ] 26} else { 27return "[unknown]" 28} 29} 30 31_get_socket_path :: proc ( temp_allocator : runtime . Allocator ) -> ( string , bool ) { 32xdg_runtime_directory , xdg_runtime_directory_ok := os . lookup_env ( "XDG_RUNTIME_DIR" , temp_allocator ) 33if ! xdg_runtime_directory_ok { 34return "" , false 35} 36defer delete ( xdg_runtime_directory , temp_allocator ) 37 38wayland_display := os . lookup_env ( "WAYLAND_DISPLAY" , temp_allocator ) or_else "wayland-0" 39defer delete ( wayland_display , temp_allocator ) 40 41final_path , err := filepath . join ({ xdg_runtime_directory , wayland_display }, temp_allocator ) 42if err != nil { 43log . debug ( "Socket path:" , final_path ) 44} 45 46return final_path , err == nil 47} 48 49_get_socket :: proc ( temp_allocator : runtime . Allocator ) -> ( fd : linux . Fd , ok : bool ) { 50if wayland_socket , ok := os . lookup_env ( "WAYLAND_SOCKET" , temp_allocator ); ok { 51fd_num , fd_ok := strconv . parse_int ( wayland_socket ) 52if fd_ok { 53return linux . Fd ( fd_num ), true 54} else { 55log . error ( "Ignoring bad WAYLAND_SOCKET value '{}'" , wayland_socket ) 56} 57} 58 59socket_fd , socket_errno := linux . socket (. UNIX , . STREAM , {}, nil ) 60if socket_errno != . NONE { 61log . error ( "Failed to create wayland socket:" , socket_errno ) 62return 0 , false 63} 64 65socket_path := _get_socket_path ( temp_allocator ) or_return 66defer delete ( socket_path , temp_allocator ) 67 68socket_address := linux . Sock_Addr_Un { sun_family = . UNIX } 69ensure ( len ( socket_path ) < size_of ( socket_address . sun_path ), "Socket path is too long." ) 70copy ( socket_address . sun_path [:], socket_path ) 71 72connect_errno := linux . connect ( socket_fd , & socket_address ) 73if connect_errno != . NONE { 74log . error ( "Failed to connect to wayland socket:" , connect_errno ) 75return 0 , false 76} 77 78return socket_fd , true 79} 80 81// Stolen from core:strings and modified to work with a string builder as input. 82_string_builder_write_fixed :: proc ( builder : ^ strings . Builder , value : $ T / fixed . Fixed ($ Backing , $ Fraction_Width )) -> ( n : int ) { 83Integer_Width :: 8 * size_of ( Backing ) - Fraction_Width 84 85value := value 86buffer : [ 48 ] byte 87i := 0 88 89if ! intrinsics . type_is_unsigned ( Backing ) && value . i == min ( Backing ) { 90// edge case handling for signed numbers 91buffer [ i ] = '-' 92i += 1 93i += copy ( buffer [ i :], _power_of_two_table [ Integer_Width ]) 94} else { 95if value . i < 0 { 96buffer [ i ] = '-' 97i += 1 98value . i = - value . i 99} 100 101when size_of ( Backing ) < 16 { 102T :: u64 103write_uint :: strconv . write_uint 104} else { 105T :: u128 106write_uint :: strconv . write_u128 107} 108 109integer := T ( value . i ) >> Fraction_Width 110fraction := T ( value . i ) & ( 1 << Fraction_Width - 1 ) 111 112s := write_uint ( buffer [ i :], integer , 10 ) 113i += len ( s ) 114if fraction != 0 { 115buffer [ i ] = '.' 116i += 1 117for fraction > 0 { 118fraction *= 10 119buffer [ i ] = byte ( '0' + ( fraction >> Fraction_Width ) % 10 ) 120i += 1 121fraction &= 1 << Fraction_Width - 1 122} 123} 124} 125 126return strings . write_bytes ( builder , buffer [: i ]) 127} 128 129@( rodata ) 130_power_of_two_table := [ 129 ] string { 131"0.5" , 132"1" , 133"2" , 134"4" , 135"8" , 136"16" , 137"32" , 138"64" , 139"128" , 140"256" , 141"512" , 142"1024" , 143"2048" , 144"4096" , 145"8192" , 146"16384" , 147"32768" , 148"65536" , 149"131072" , 150"262144" , 151"524288" , 152"1048576" , 153"2097152" , 154"4194304" , 155"8388608" , 156"16777216" , 157"33554432" , 158"67108864" , 159"134217728" , 160"268435456" , 161"536870912" , 162"1073741824" , 163"2147483648" , 164"4294967296" , 165"8589934592" , 166"17179869184" , 167"34359738368" , 168"68719476736" , 169"137438953472" , 170"274877906944" , 171"549755813888" , 172"1099511627776" , 173"2199023255552" , 174"4398046511104" , 175"8796093022208" , 176"17592186044416" , 177"35184372088832" , 178"70368744177664" , 179"140737488355328" , 180"281474976710656" , 181"562949953421312" , 182"1125899906842624" , 183"2251799813685248" , 184"4503599627370496" , 185"9007199254740992" , 186"18014398509481984" , 187"36028797018963968" , 188"72057594037927936" , 189"144115188075855872" , 190"288230376151711744" , 191"576460752303423488" , 192"1152921504606846976" , 193"2305843009213693952" , 194"4611686018427387904" , 195"9223372036854775808" , 196"18446744073709551616" , 197"36893488147419103232" , 198"73786976294838206464" , 199"147573952589676412928" , 200"295147905179352825856" , 201"590295810358705651712" , 202"1180591620717411303424" , 203"2361183241434822606848" , 204"4722366482869645213696" , 205"9444732965739290427392" , 206"18889465931478580854784" , 207"37778931862957161709568" , 208"75557863725914323419136" , 209"151115727451828646838272" , 210"302231454903657293676544" , 211"604462909807314587353088" , 212"1208925819614629174706176" , 213"2417851639229258349412352" , 214"4835703278458516698824704" , 215"9671406556917033397649408" , 216"19342813113834066795298816" , 217"38685626227668133590597632" , 218"77371252455336267181195264" , 219"154742504910672534362390528" , 220"309485009821345068724781056" , 221"618970019642690137449562112" , 222"1237940039285380274899124224" , 223"2475880078570760549798248448" , 224"4951760157141521099596496896" , 225"9903520314283042199192993792" , 226"19807040628566084398385987584" , 227"39614081257132168796771975168" , 228"79228162514264337593543950336" , 229"158456325028528675187087900672" , 230"316912650057057350374175801344" , 231"633825300114114700748351602688" , 232"1267650600228229401496703205376" , 233"2535301200456458802993406410752" , 234"5070602400912917605986812821504" , 235"10141204801825835211973625643008" , 236"20282409603651670423947251286016" , 237"40564819207303340847894502572032" , 238"81129638414606681695789005144064" , 239"162259276829213363391578010288128" , 240"324518553658426726783156020576256" , 241"649037107316853453566312041152512" , 242"1298074214633706907132624082305024" , 243"2596148429267413814265248164610048" , 244"5192296858534827628530496329220096" , 245"10384593717069655257060992658440192" , 246"20769187434139310514121985316880384" , 247"41538374868278621028243970633760768" , 248"83076749736557242056487941267521536" , 249"166153499473114484112975882535043072" , 250"332306998946228968225951765070086144" , 251"664613997892457936451903530140172288" , 252"1329227995784915872903807060280344576" , 253"2658455991569831745807614120560689152" , 254"5316911983139663491615228241121378304" , 255"10633823966279326983230456482242756608" , 256"21267647932558653966460912964485513216" , 257"42535295865117307932921825928971026432" , 258"85070591730234615865843651857942052864" , 259"170141183460469231731687303715884105728" , 260}