1package server
2
3import (
4 "fmt"
5 "sync"
6 "unsafe"
7
8 "github.com/bnema/purego"
9)
10
11// libwayland-server entry points. Only non-varargs functions are bound.
12var (
13 wlDisplayCreate func() uintptr
14 wlDisplayDestroy func(display uintptr)
15 wlDisplayDestroyClients func(display uintptr)
16 wlDisplayAddSocketFD func(display uintptr, fd int32) int32
17 wlDisplayGetEventLoop func(display uintptr) uintptr
18 wlDisplayFlushClients func(display uintptr)
19 wlEventLoopDispatch func(loop uintptr, timeout int32) int32
20 wlEventLoopGetFD func(loop uintptr) int32
21 wlEventLoopAddFD func(loop uintptr, fd int32, mask uint32, fn, data uintptr) uintptr
22 wlEventSourceRemove func(source uintptr) int32
23 wlGlobalCreate func(display, iface uintptr, version int32, data uintptr, bind uintptr) uintptr
24 wlGlobalRemove func(global uintptr)
25 wlResourceCreate func(client, iface uintptr, version int32, id uint32) uintptr
26 wlResourceSetDispatcher func(resource, dispatcher, impl, data, destroy uintptr)
27 wlResourcePostEventArr func(resource uintptr, opcode uint32, args unsafe.Pointer)
28 wlResourcePostError func(resource uintptr, code uint32, msg uintptr)
29 wlResourceDestroy func(resource uintptr)
30 wlResourceGetID func(resource uintptr) uint32
31 wlResourceGetClient func(resource uintptr) uintptr
32 wlResourceGetVersion func(resource uintptr) int32
33 wlClientGetCredentials func(client uintptr, pid, uid, gid unsafe.Pointer)
34)
35
36var (
37 loadOnce sync.Once
38 loadErr error
39
40 // Shared C callbacks. purego callbacks are never freed, so there is one
41 // of each for the whole process.
42 cbDispatcher uintptr
43 cbDestroy uintptr
44 cbBind uintptr
45 cbWake uintptr
46)
47
48func load() error {
49 loadOnce.Do(func() {
50 lib, err := purego.Dlopen("libwayland-server.so.0", purego.RTLD_NOW|purego.RTLD_GLOBAL)
51 if err != nil {
52 loadErr = fmt.Errorf("purego-libwayland: open libwayland-server.so.0: %w", err)
53 return
54 }
55 defer func() {
56 if r := recover(); r != nil {
57 loadErr = fmt.Errorf("purego-libwayland: %v", r)
58 }
59 }()
60 reg := func(fptr any, name string) { purego.RegisterLibFunc(fptr, lib, name) }
61 reg(&wlDisplayCreate, "wl_display_create")
62 reg(&wlDisplayDestroy, "wl_display_destroy")
63 reg(&wlDisplayDestroyClients, "wl_display_destroy_clients")
64 reg(&wlDisplayAddSocketFD, "wl_display_add_socket_fd")
65 reg(&wlDisplayGetEventLoop, "wl_display_get_event_loop")
66 reg(&wlDisplayFlushClients, "wl_display_flush_clients")
67 reg(&wlEventLoopDispatch, "wl_event_loop_dispatch")
68 reg(&wlEventLoopGetFD, "wl_event_loop_get_fd")
69 reg(&wlEventLoopAddFD, "wl_event_loop_add_fd")
70 reg(&wlEventSourceRemove, "wl_event_source_remove")
71 reg(&wlGlobalCreate, "wl_global_create")
72 reg(&wlGlobalRemove, "wl_global_remove")
73 reg(&wlResourceCreate, "wl_resource_create")
74 reg(&wlResourceSetDispatcher, "wl_resource_set_dispatcher")
75 reg(&wlResourcePostEventArr, "wl_resource_post_event_array")
76 reg(&wlResourcePostError, "wl_resource_post_error")
77 reg(&wlResourceDestroy, "wl_resource_destroy")
78 reg(&wlResourceGetID, "wl_resource_get_id")
79 reg(&wlResourceGetClient, "wl_resource_get_client")
80 reg(&wlResourceGetVersion, "wl_resource_get_version")
81 reg(&wlClientGetCredentials, "wl_client_get_credentials")
82
83 cbDispatcher = purego.NewCallback(dispatch)
84 cbDestroy = purego.NewCallback(destroyed)
85 cbBind = purego.NewCallback(bind)
86 cbWake = purego.NewCallback(wake)
87 })
88 return loadErr
89}