github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/xcb/util.go (about) 1 //go:build linux && !android 2 3 package xcb 4 5 import ( 6 "unsafe" 7 8 "golang.org/x/exp/constraints" 9 ) 10 11 /* 12 13 #include <stdlib.h> 14 #include <xcb/xcb.h> 15 #include <xcb/xinput.h> 16 #include <xcb/xproto.h> 17 18 */ 19 import "C" 20 21 func setXiMask(mask *[8]C.uint8_t, bit C.int) { 22 mask[bit>>3] |= 1 << (bit & 7) 23 } 24 25 func hasXiMask(mask []C.uint32_t, bit C.uint16_t) bool { 26 return mask[bit>>3]&(1<<(bit&7)) != 0 27 } 28 29 func (d *Display) internAtom(onlyIfExists bool, name string) C.xcb_atom_t { 30 nameStr := C.CString(name) 31 defer C.free(unsafe.Pointer(nameStr)) 32 cookie := d.l.xcb_intern_atom(d.xcbConn, Cbool[C.uint8_t](onlyIfExists), C.uint16_t(len(name)), nameStr) 33 reply := d.l.xcb_intern_atom_reply(d.xcbConn, cookie, nil) 34 defer C.free(unsafe.Pointer(reply)) 35 return reply.atom 36 } 37 38 func Cbool[T constraints.Integer | constraints.Float](v bool) T { 39 if v { 40 return 1 41 } 42 return 0 43 } 44 45 // https://gitlab.freedesktop.org/xorg/lib/libxi/-/blob/bca3474a8622fde5815260461784282f78a4efb5/src/XExtInt.c#L74 46 func fixed1616ToFloat64(v C.xcb_input_fp1616_t) float64 { 47 return float64(v) * 1.0 / (1 << 16) 48 } 49 50 // https://gitlab.freedesktop.org/xorg/lib/libxi/-/blob/bca3474a8622fde5815260461784282f78a4efb5/src/XExtInt.c#L1700 51 func fixed3232ToFloat64(v C.xcb_input_fp3232_t) float64 { 52 return float64(v.integral) + float64(v.frac)/(1<<32) 53 }