github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/wayland/client.go (about) 1 //go:build linux && !android 2 3 package wayland 4 5 /* 6 7 #cgo LDFLAGS: -ldl 8 9 #include <dlfcn.h> 10 #include "wayland-cursor.h" 11 12 struct wl_display; 13 struct wl_proxy; 14 15 struct wl_display *gamen_wl_display_connect(void *fp, const char *name) { 16 typedef struct wl_display *(*proc_wl_display_connect)(const char *name); 17 return ((proc_wl_display_connect)fp)(name); 18 } 19 20 void gamen_wl_display_disconnect(void *fp, struct wl_display *display) { 21 typedef void (*proc_wl_display_disconnect)(struct wl_display *display); 22 ((proc_wl_display_disconnect)fp)(display); 23 } 24 25 int gamen_wl_display_roundtrip(void *fp, struct wl_display *display) { 26 typedef int (*proc_wl_display_roundtrip)(struct wl_display *display); 27 return ((proc_wl_display_roundtrip)fp)(display); 28 } 29 30 int gamen_wl_display_prepare_read(void *fp, struct wl_display *display) { 31 typedef int (*proc_wl_display_prepare_read)(struct wl_display *display); 32 return ((proc_wl_display_prepare_read)fp)(display); 33 } 34 35 int gamen_wl_display_dispatch_pending(void *fp, struct wl_display *display) { 36 typedef int (*proc_wl_display_dispatch_pending)(struct wl_display *display); 37 return ((proc_wl_display_dispatch_pending)fp)(display); 38 } 39 40 int gamen_wl_display_flush(void *fp, struct wl_display *display) { 41 typedef int (*proc_wl_display_flush)(struct wl_display *display); 42 return ((proc_wl_display_flush)fp)(display); 43 } 44 45 int gamen_wl_display_get_fd(void *fp, struct wl_display *display) { 46 typedef int (*proc_wl_display_get_fd)(struct wl_display *display); 47 return ((proc_wl_display_get_fd)fp)(display); 48 } 49 50 void gamen_wl_display_cancel_read(void *fp, struct wl_display *display) { 51 typedef void (*proc_wl_display_cancel_read)(struct wl_display *display); 52 ((proc_wl_display_cancel_read)fp)(display); 53 } 54 55 int gamen_wl_display_read_events(void *fp, struct wl_display *display) { 56 typedef int (*proc_wl_display_read_events)(struct wl_display *display); 57 return ((proc_wl_display_read_events)fp)(display); 58 } 59 60 void gamen_wl_proxy_destroy(void *fp, struct wl_proxy *proxy) { 61 typedef void (*proc_wl_proxy_destroy)(struct wl_proxy *proxy); 62 ((proc_wl_proxy_destroy)fp)(proxy); 63 } 64 65 66 struct wl_cursor_theme *gamen_wl_cursor_theme_load(void *fp, const char *name, int size, struct wl_shm *shm) { 67 typedef struct wl_cursor_theme *(*proc_wl_cursor_theme_load)(const char *name, int size, struct wl_shm *shm); 68 return ((proc_wl_cursor_theme_load)fp)(name, size, shm); 69 } 70 71 struct wl_cursor *gamen_wl_cursor_theme_get_cursor(void *fp, struct wl_cursor_theme *theme, const char *name) { 72 typedef struct wl_cursor *(*proc_wl_cursor_theme_get_cursor)(struct wl_cursor_theme *theme, const char *name); 73 return ((proc_wl_cursor_theme_get_cursor)fp)(theme, name); 74 } 75 76 void gamen_wl_cursor_theme_destroy(void *fp, struct wl_cursor_theme *theme) { 77 typedef void (*proc_wl_cursor_theme_destroy)(struct wl_cursor_theme *theme); 78 ((proc_wl_cursor_theme_destroy)fp)(theme); 79 } 80 81 struct wl_buffer *gamen_wl_cursor_image_get_buffer(void *fp, struct wl_cursor_image *image) { 82 typedef struct wl_buffer *(*proc_wl_cursor_image_get_buffer)(struct wl_cursor_image *image); 83 return ((proc_wl_cursor_image_get_buffer)fp)(image); 84 } 85 86 int gamen_wl_cursor_frame_and_duration(void *fp, struct wl_cursor *cursor, uint32_t time, uint32_t *duration) { 87 typedef int (*proc_wl_cursor_frame_and_duration)(struct wl_cursor *cursor, uint32_t time, uint32_t *duration); 88 return ((proc_wl_cursor_frame_and_duration)fp)(cursor, time, duration); 89 } 90 91 */ 92 import "C" 93 import ( 94 "errors" 95 "unsafe" 96 ) 97 98 type wl_library struct { 99 libWaylandClientHandle unsafe.Pointer 100 libWaylandCursorHandle unsafe.Pointer 101 102 wl_display_connect_handle unsafe.Pointer 103 wl_display_roundtrip_handle unsafe.Pointer 104 wl_display_disconnect_handle unsafe.Pointer 105 wl_display_prepare_read_handle unsafe.Pointer 106 wl_display_dispatch_pending_handle unsafe.Pointer 107 wl_display_flush_handle unsafe.Pointer 108 wl_display_get_fd_handle unsafe.Pointer 109 wl_display_cancel_read_handle unsafe.Pointer 110 wl_display_read_events_handle unsafe.Pointer 111 wl_proxy_add_listener_handle unsafe.Pointer 112 wl_proxy_destroy_handle unsafe.Pointer 113 wl_proxy_marshal_flags unsafe.Pointer 114 wl_proxy_get_version unsafe.Pointer 115 116 wl_cursor_theme_load_handle unsafe.Pointer 117 wl_cursor_theme_get_cursor_handle unsafe.Pointer 118 wl_cursor_theme_destroy_handle unsafe.Pointer 119 wl_cursor_image_get_buffer_handle unsafe.Pointer 120 wl_cursor_frame_and_duration_handle unsafe.Pointer 121 } 122 123 func loadSym(handle unsafe.Pointer, symbol *C.char) (unsafe.Pointer, error) { 124 C.dlerror() 125 fp := C.dlsym(handle, symbol) 126 if fp == nil { 127 err := C.dlerror() 128 if err != nil { 129 return nil, errors.New(C.GoString(err)) 130 } 131 } 132 return fp, nil 133 } 134 135 func open_wl_library() (*wl_library, error) { 136 C.dlerror() 137 138 l := &wl_library{} 139 140 l.libWaylandClientHandle = C.dlopen((*C.char)(unsafe.Pointer(&([]byte("libwayland-client.so.0\x00"))[0])), C.RTLD_LAZY) 141 if l.libWaylandClientHandle == nil { 142 err := C.dlerror() 143 if err != nil { 144 l.close() 145 return nil, errors.New(C.GoString(err)) 146 } 147 } 148 149 l.libWaylandCursorHandle = C.dlopen((*C.char)(unsafe.Pointer(&([]byte("libwayland-cursor.so.0\x00"))[0])), C.RTLD_LAZY) 150 if l.libWaylandCursorHandle == nil { 151 err := C.dlerror() 152 if err != nil { 153 l.close() 154 return nil, errors.New(C.GoString(err)) 155 } 156 } 157 158 var err error 159 l.wl_display_connect_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_connect\x00"))[0]))) 160 if err != nil { 161 l.close() 162 return nil, err 163 } 164 l.wl_display_roundtrip_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_roundtrip\x00"))[0]))) 165 if err != nil { 166 l.close() 167 return nil, err 168 } 169 l.wl_display_disconnect_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_disconnect\x00"))[0]))) 170 if err != nil { 171 l.close() 172 return nil, err 173 } 174 l.wl_display_prepare_read_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_prepare_read\x00"))[0]))) 175 if err != nil { 176 l.close() 177 return nil, err 178 } 179 l.wl_display_dispatch_pending_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_dispatch_pending\x00"))[0]))) 180 if err != nil { 181 l.close() 182 return nil, err 183 } 184 l.wl_display_flush_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_flush\x00"))[0]))) 185 if err != nil { 186 l.close() 187 return nil, err 188 } 189 l.wl_display_get_fd_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_get_fd\x00"))[0]))) 190 if err != nil { 191 l.close() 192 return nil, err 193 } 194 l.wl_display_cancel_read_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_cancel_read\x00"))[0]))) 195 if err != nil { 196 l.close() 197 return nil, err 198 } 199 l.wl_display_read_events_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_display_read_events\x00"))[0]))) 200 if err != nil { 201 l.close() 202 return nil, err 203 } 204 l.wl_proxy_add_listener_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_proxy_add_listener\x00"))[0]))) 205 if err != nil { 206 l.close() 207 return nil, err 208 } 209 l.wl_proxy_destroy_handle, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_proxy_destroy\x00"))[0]))) 210 if err != nil { 211 l.close() 212 return nil, err 213 } 214 l.wl_proxy_marshal_flags, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_proxy_marshal_flags\x00"))[0]))) 215 if err != nil { 216 l.close() 217 return nil, err 218 } 219 l.wl_proxy_get_version, err = loadSym(l.libWaylandClientHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_proxy_get_version\x00"))[0]))) 220 if err != nil { 221 l.close() 222 return nil, err 223 } 224 225 l.wl_cursor_theme_load_handle, err = loadSym(l.libWaylandCursorHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_cursor_theme_load\x00"))[0]))) 226 if err != nil { 227 l.close() 228 return nil, err 229 } 230 l.wl_cursor_theme_get_cursor_handle, err = loadSym(l.libWaylandCursorHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_cursor_theme_get_cursor\x00"))[0]))) 231 if err != nil { 232 l.close() 233 return nil, err 234 } 235 l.wl_cursor_theme_destroy_handle, err = loadSym(l.libWaylandCursorHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_cursor_theme_destroy\x00"))[0]))) 236 if err != nil { 237 l.close() 238 return nil, err 239 } 240 l.wl_cursor_image_get_buffer_handle, err = loadSym(l.libWaylandCursorHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_cursor_image_get_buffer\x00"))[0]))) 241 if err != nil { 242 l.close() 243 return nil, err 244 } 245 l.wl_cursor_frame_and_duration_handle, err = loadSym(l.libWaylandCursorHandle, (*C.char)(unsafe.Pointer(&([]byte("wl_cursor_frame_and_duration\x00"))[0]))) 246 if err != nil { 247 l.close() 248 return nil, err 249 } 250 251 return l, nil 252 } 253 254 func (l *wl_library) close() { 255 if l.libWaylandCursorHandle != nil { 256 C.dlclose(l.libWaylandCursorHandle) 257 l.libWaylandCursorHandle = nil 258 } 259 if l.libWaylandClientHandle != nil { 260 C.dlclose(l.libWaylandClientHandle) 261 l.libWaylandClientHandle = nil 262 } 263 } 264 265 func (l *wl_library) wl_display_connect(name *C.char) *C.struct_wl_display { 266 return C.gamen_wl_display_connect(l.wl_display_connect_handle, name) 267 } 268 func (l *wl_library) wl_display_roundtrip(display *C.struct_wl_display) C.int { 269 return C.gamen_wl_display_roundtrip(l.wl_display_roundtrip_handle, display) 270 } 271 func (l *wl_library) wl_display_disconnect(display *C.struct_wl_display) { 272 C.gamen_wl_display_disconnect(l.wl_display_disconnect_handle, display) 273 } 274 func (l *wl_library) wl_display_prepare_read(display *C.struct_wl_display) C.int { 275 return C.gamen_wl_display_prepare_read(l.wl_display_prepare_read_handle, display) 276 } 277 func (l *wl_library) wl_display_dispatch_pending(display *C.struct_wl_display) C.int { 278 return C.gamen_wl_display_dispatch_pending(l.wl_display_dispatch_pending_handle, display) 279 } 280 func (l *wl_library) wl_display_flush(display *C.struct_wl_display) (C.int, error) { 281 r, err := C.gamen_wl_display_flush(l.wl_display_flush_handle, display) 282 return r, err 283 } 284 func (l *wl_library) wl_display_get_fd(display *C.struct_wl_display) C.int { 285 return C.gamen_wl_display_get_fd(l.wl_display_get_fd_handle, display) 286 } 287 func (l *wl_library) wl_display_cancel_read(display *C.struct_wl_display) { 288 C.gamen_wl_display_cancel_read(l.wl_display_cancel_read_handle, display) 289 } 290 func (l *wl_library) wl_display_read_events(display *C.struct_wl_display) C.int { 291 return C.gamen_wl_display_read_events(l.wl_display_read_events_handle, display) 292 } 293 func (l *wl_library) wl_proxy_destroy(proxy *C.struct_wl_proxy) { 294 C.gamen_wl_proxy_destroy(l.wl_proxy_destroy_handle, proxy) 295 } 296 297 func (l *wl_library) wl_cursor_theme_load(name *C.char, size C.int, shm *C.struct_wl_shm) *C.struct_wl_cursor_theme { 298 return C.gamen_wl_cursor_theme_load(l.wl_cursor_theme_load_handle, name, size, shm) 299 } 300 301 func (l *wl_library) wl_cursor_theme_get_cursor(theme *C.struct_wl_cursor_theme, name *C.char) *C.struct_wl_cursor { 302 return C.gamen_wl_cursor_theme_get_cursor(l.wl_cursor_theme_get_cursor_handle, theme, name) 303 } 304 305 func (l *wl_library) wl_cursor_theme_destroy(theme *C.struct_wl_cursor_theme) { 306 C.gamen_wl_cursor_theme_destroy(l.wl_cursor_theme_destroy_handle, theme) 307 } 308 309 func (l *wl_library) wl_cursor_image_get_buffer(image *C.struct_wl_cursor_image) *C.struct_wl_buffer { 310 return C.gamen_wl_cursor_image_get_buffer(l.wl_cursor_image_get_buffer_handle, image) 311 } 312 313 func (l *wl_library) wl_cursor_frame_and_duration(cursor *C.struct_wl_cursor, time C.uint32_t, duration *C.uint32_t) C.int { 314 return C.gamen_wl_cursor_frame_and_duration(l.wl_cursor_frame_and_duration_handle, cursor, time, duration) 315 }