-
Notifications
You must be signed in to change notification settings - Fork 22
/
wlroots.go
321 lines (264 loc) · 6.88 KB
/
wlroots.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package wlroots
import (
"errors"
"unsafe"
)
// #cgo pkg-config: wlroots wayland-server
// #cgo CFLAGS: -D_GNU_SOURCE -DWLR_USE_UNSTABLE
// #include <stdlib.h>
// #include <time.h>
// #include <wayland-server-core.h>
// #include <wlr/backend.h>
// #include <wlr/types/wlr_compositor.h>
// #include <wlr/types/wlr_subcompositor.h>
// #include <wlr/types/wlr_data_device.h>
// #include <wlr/types/wlr_output_layout.h>
// #include <wlr/types/wlr_xcursor_manager.h>
// #include <wlr/types/wlr_xdg_shell.h>
// #include <wlr/render/wlr_texture.h>
// #include <wlr/types/wlr_linux_dmabuf_v1.h>
// #include <wlr/types/wlr_matrix.h>
// #include <wlr/util/box.h>
// #include <wlr/util/edges.h>
import "C"
type XCursorManager struct {
p *C.struct_wlr_xcursor_manager
}
func NewXCursorManager(name string, size int) XCursorManager {
p := C.wlr_xcursor_manager_create(C.CString(name), C.uint(size))
return XCursorManager{p: p}
}
func (m XCursorManager) Destroy() {
C.wlr_xcursor_manager_destroy(m.p)
}
func (m XCursorManager) Load(scale float32) {
C.wlr_xcursor_manager_load(m.p, C.float(scale))
}
type Edges uint32
const (
EdgeNone Edges = C.WLR_EDGE_NONE
EdgeTop Edges = C.WLR_EDGE_TOP
EdgeBottom Edges = C.WLR_EDGE_BOTTOM
EdgeLeft Edges = C.WLR_EDGE_LEFT
EdgeRight Edges = C.WLR_EDGE_RIGHT
)
type Texture struct {
p *C.struct_wlr_texture
}
func (t Texture) Destroy() {
C.wlr_texture_destroy(t.p)
}
func (t Texture) Nil() bool {
return t.p == nil
}
type OutputLayout struct {
p *C.struct_wlr_output_layout
}
type OutputLayoutOutput struct {
p *C.struct_wlr_output_layout_output
}
func NewOutputLayout() OutputLayout {
return OutputLayoutCerate()
}
func OutputLayoutCerate() OutputLayout {
p := C.wlr_output_layout_create()
man.track(unsafe.Pointer(p), &p.events.destroy)
return OutputLayout{p: p}
}
func (l OutputLayout) Destroy() {
C.wlr_output_layout_destroy(l.p)
}
func (l OutputLayout) AddOutputAuto(output Output) OutputLayoutOutput {
p := C.wlr_output_layout_add_auto(l.p, output.p)
return OutputLayoutOutput{p: p}
}
func (l OutputLayout) Coords(output Output) (x float64, y float64) {
var ox, oy C.double
C.wlr_output_layout_output_coords(l.p, output.p, &ox, &oy)
return float64(ox), float64(oy)
}
type Matrix [9]float32
func (m *Matrix) ProjectBox(box *GeoBox, transform uint32, rotation float32, projection *Matrix) {
cm := m.toC()
b := box.toC()
pm := projection.toC()
C.wlr_matrix_project_box(&cm[0], &b, C.enum_wl_output_transform(transform), C.float(rotation), &pm[0])
m.fromC(&cm)
}
func (m *Matrix) toC() [9]C.float {
var cm [9]C.float
for i := range m {
cm[i] = C.float(m[i])
}
return cm
}
func (m *Matrix) fromC(cm *[9]C.float) {
for i := range cm {
m[i] = float32(cm[i])
}
}
type DMABuf struct {
p *C.struct_wlr_linux_dmabuf_v1
}
func NewDMABuf(display Display, renderer Renderer) DMABuf {
p := C.wlr_linux_dmabuf_v1_create_with_renderer(display.p, 4, renderer.p)
man.track(unsafe.Pointer(p), &p.events.destroy)
return DMABuf{p: p}
}
func (b DMABuf) OnDestroy(cb func(DMABuf)) {
man.add(unsafe.Pointer(b.p), &b.p.events.destroy, func(unsafe.Pointer) {
cb(b)
})
}
type Display struct {
p *C.struct_wl_display
}
func DisplayCreate() {
NewDisplay()
}
func NewDisplay() Display {
p := C.wl_display_create()
d := Display{p: p}
d.OnDestroy(func(Display) {
man.delete(unsafe.Pointer(p))
})
return d
}
func (d Display) NewBackend() (Backend, error) {
return d.BackendAutocreate()
}
func (d Display) BackendAutocreate() (Backend, error) {
p := C.wlr_backend_autocreate(d.p, nil)
if p == nil {
return Backend{}, errors.New("failed to create wlr_backend")
}
man.track(unsafe.Pointer(p), &p.events.destroy)
return Backend{p: p}, nil
}
func (d Display) NewSubCompositor() SubCompositor {
return d.SubCompositorCreate()
}
func (d Display) SubCompositorCreate() SubCompositor {
p := C.wlr_subcompositor_create(d.p)
man.track(unsafe.Pointer(p), &p.events.destroy)
return SubCompositor{p: p}
}
func (d Display) NewCompositor(version int, renderer Renderer) Compositor {
return d.CompositorCreate(version, renderer)
}
func (d Display) CompositorCreate(version int, renderer Renderer) Compositor {
p := C.wlr_compositor_create(d.p, C.uint(version), renderer.p)
man.track(unsafe.Pointer(p), &p.events.destroy)
return Compositor{p: p}
}
func (d Display) NewDataDeviceManager() DataDeviceManager {
return d.DataDeviceManagerCreate()
}
func (d Display) DataDeviceManagerCreate() DataDeviceManager {
p := C.wlr_data_device_manager_create(d.p)
man.track(unsafe.Pointer(p), &p.events.destroy)
return DataDeviceManager{p: p}
}
func (d Display) NewSeat(name string) Seat {
return d.SeatCreate(name)
}
func (d Display) SeatCreate(name string) Seat {
s := C.CString(name)
p := C.wlr_seat_create(d.p, s)
C.free(unsafe.Pointer(s))
man.track(unsafe.Pointer(p), &p.events.destroy)
return Seat{p: p}
}
func (d Display) NewXDGShell(version int) XDGShell {
return d.XDGShellCreate(version)
}
func (d Display) XDGShellCreate(version int) XDGShell {
p := C.wlr_xdg_shell_create(d.p, C.uint(version))
man.track(unsafe.Pointer(p), &p.events.destroy)
return XDGShell{p: p}
}
func (d Display) Destroy() {
C.wl_display_destroy(d.p)
}
func (d Display) OnDestroy(cb func(Display)) {
l := man.add(unsafe.Pointer(d.p), nil, func(data unsafe.Pointer) {
cb(d)
})
C.wl_display_add_destroy_listener(d.p, l.p)
}
func (d Display) Run() {
C.wl_display_run(d.p)
}
func (d Display) Terminate() {
C.wl_display_terminate(d.p)
}
func (d Display) EventLoop() EventLoop {
p := C.wl_display_get_event_loop(d.p)
evl := EventLoop{p: p}
evl.OnDestroy(func(EventLoop) {
man.delete(unsafe.Pointer(p))
})
return evl
}
func (d Display) AddSocketAuto() (string, error) {
socket := C.wl_display_add_socket_auto(d.p)
if socket == nil {
return "", errors.New("can't auto add wayland socket")
}
return C.GoString(socket), nil
}
func (d Display) FlushClients() {
C.wl_display_flush_clients(d.p)
}
func (d Display) DestroyClients() {
C.wl_display_destroy_clients(d.p)
}
type DataDeviceManager struct {
p *C.struct_wlr_data_device_manager
}
func (m DataDeviceManager) OnDestroy(cb func(DataDeviceManager)) {
man.add(unsafe.Pointer(m.p), &m.p.events.destroy, func(unsafe.Pointer) {
cb(m)
})
}
type Color struct {
R, G, B, A float32
}
func (c *Color) Set(r, g, b, a float32) {
c.R = r
c.G = g
c.B = b
c.A = a
}
func (c *Color) toC() [4]C.float {
return [...]C.float{
C.float(c.R),
C.float(c.G),
C.float(c.B),
C.float(c.A),
}
}
type GeoBox struct {
p *C.struct_wlr_box
X, Y, Width, Height int
}
func (b *GeoBox) Set(x, y, width, height int) {
b.X = x
b.Y = y
b.Width = width
b.Height = height
}
func (b *GeoBox) toC() C.struct_wlr_box {
return C.struct_wlr_box{
x: C.int(b.X),
y: C.int(b.Y),
width: C.int(b.Width),
height: C.int(b.Height),
}
}
func (b *GeoBox) fromC(cb *C.struct_wlr_box) {
b.X = int(cb.x)
b.Y = int(cb.y)
b.Width = int(cb.width)
b.Height = int(cb.height)
}