-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
328 lines (278 loc) · 8.32 KB
/
context.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
322
323
324
325
326
327
328
/***********************************************************************************
* This file is part of samurai-render-go
* https://github.com/Samudevv/samurai-render-go
***********************************************************************************
* Copyright (c) 2023 Jonas Pucher
*
* This software is provided ‘as-is’, without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
************************************************************************************/
package samure
/*
#include <samure/context.h>
#include <samure/backends/opengl.h>
#include "wrappers.h"
*/
import "C"
import (
"unsafe"
)
const (
RenderStateAlways = C.SAMURE_RENDER_STATE_ALWAYS
RenderStateNone = C.SAMURE_RENDER_STATE_NONE
RenderStateOnce = C.SAMURE_RENDER_STATE_ONCE
)
type Context struct {
Handle *C.struct_samure_context
}
type ContextConfig struct {
PointerInteraction bool
KeyboardInteraction bool
TouchInteraction bool
MaxUpdateFrequency int
NotCreateOutputLayerSurfaces bool
NotRequestFrame bool
ForceClientCursors bool
GL OpenGLConfig
App App
}
func CreateContextConfig(a App) *ContextConfig {
return &ContextConfig{
GL: DefaultOpenGLConfig(),
App: a,
}
}
type OpenGLConfig struct {
RedSize int
GreenSize int
BlueSize int
AlphaSize int
Samples int
DepthSize int
MajorVersion int
MinorVersion int
ProfileMask int
Debug int
ColorSpace int
RenderBuffer int
}
func DefaultOpenGLConfig() OpenGLConfig {
return OpenGLConfig{
RedSize: 8,
GreenSize: 8,
BlueSize: 8,
AlphaSize: 8,
MajorVersion: 1,
ProfileMask: 0x00000001,
ColorSpace: 0x308A,
RenderBuffer: 0x3084,
}
}
func (cfg ContextConfig) convertToC() C.struct_samure_context_config {
var c C.struct_samure_context_config
c.backend = C.SAMURE_BACKEND_NONE
if cfg.PointerInteraction {
c.pointer_interaction = 1
}
if cfg.KeyboardInteraction {
c.keyboard_interaction = 1
}
if cfg.TouchInteraction {
c.touch_interaction = 1
}
c.max_update_frequency = C.uint32_t(cfg.MaxUpdateFrequency)
if cfg.NotCreateOutputLayerSurfaces {
c.not_create_output_layer_surfaces = 1
}
if cfg.NotRequestFrame {
c.not_request_frame = 1
}
if cfg.ForceClientCursors {
c.force_client_cursors = 1
}
c.on_event = C.samure_event_callback(C.globalOnEvent)
c.on_render = C.samure_render_callback(C.globalOnRender)
c.on_update = C.samure_update_callback(C.globalOnUpdate)
c.user_data = unsafe.Pointer(uintptr(AddGlobalApp(cfg.App)))
c.gl = cfg.GL.convertToC()
return c
}
func (cfg OpenGLConfig) convertToC() *C.struct_samure_opengl_config {
ptr := (*C.struct_samure_opengl_config)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_samure_opengl_config{}))))
if ptr == nil {
return nil
}
ptr.red_size = C.int(cfg.RedSize)
ptr.green_size = C.int(cfg.GreenSize)
ptr.blue_size = C.int(cfg.BlueSize)
ptr.alpha_size = C.int(cfg.AlphaSize)
ptr.samples = C.int(cfg.Samples)
ptr.depth_size = C.int(cfg.DepthSize)
ptr.major_version = C.int(cfg.MajorVersion)
ptr.minor_version = C.int(cfg.MinorVersion)
ptr.profile_mask = C.int(cfg.ProfileMask)
ptr.debug = C.int(cfg.Debug)
ptr.color_space = C.int(cfg.ColorSpace)
ptr.render_buffer = C.int(cfg.RenderBuffer)
return ptr
}
func CreateContextWithBackend(cfg *ContextConfig, bak Backend) (Context, error) {
var c C.struct_samure_context_config
if cfg != nil {
c = cfg.convertToC()
} else {
c.backend = C.SAMURE_BACKEND_NONE
}
not_create_output_layer_surfaces := c.not_create_output_layer_surfaces
c.not_create_output_layer_surfaces = 1
ctx_rs := C.samure_create_context(&c)
if ctx_rs.error != ErrorNone {
return Context{}, NewError(uint64(ctx_rs.error))
}
ctx := Context{ctx_rs.result}
if err := bak.Init(ctx); err != nil {
ctx.Destroy()
return Context{nil}, err
}
bakIdx := AddGlobalBackend(bak)
wrapBak := C.create_wrapper_backend(C.int(bakIdx))
ctx.Handle.backend = &wrapBak.base
if not_create_output_layer_surfaces == 0 {
ctx.Handle.config.not_create_output_layer_surfaces = 0
if err := C.samure_context_create_output_layer_surfaces(ctx.Handle); err != ErrorNone {
ctx.Destroy()
return Context{nil}, NewError(uint64(err))
}
}
return Context{ctx_rs.result}, nil
}
func (ctx Context) Destroy() {
C.samure_destroy_context(ctx.Handle)
}
func (ctx Context) LenOutputs() int {
return int(ctx.Handle.num_outputs)
}
func (ctx Context) Output(idx int) Output {
return Output{
*(**C.struct_samure_output)(unsafe.Pointer(uintptr(unsafe.Pointer(ctx.Handle.outputs)) + unsafe.Sizeof(&C.struct_samure_output{})*uintptr(idx))),
}
}
func (ctx Context) LenSeats() int {
return int(ctx.Handle.num_seats)
}
func (ctx Context) Seat(idx int) Seat {
return Seat{
*(**C.struct_samure_seat)(unsafe.Pointer(uintptr(unsafe.Pointer(ctx.Handle.seats)) + unsafe.Sizeof(&C.struct_samure_seat{})*uintptr(idx))),
}
}
func (ctx Context) Run() {
C.samure_context_run(ctx.Handle)
}
func (ctx Context) SetRunning(v bool) {
if v {
ctx.Handle.running = 1
} else {
ctx.Handle.running = 0
}
}
func (ctx Context) SetRenderState(v int) {
C.samure_context_set_render_state(ctx.Handle, uint32(v))
}
func (ctx Context) CreateOutputLayerSurfaces() error {
err := C.samure_context_create_output_layer_surfaces(ctx.Handle)
if err != ErrorNone {
return NewError(uint64(err))
}
return nil
}
func (ctx Context) SetPointerInteraction(enable bool) {
var cEnable C.int
if enable {
cEnable = 1
}
C.samure_context_set_pointer_interaction(ctx.Handle, cEnable)
}
func (ctx Context) SetInputRegions(rs []Rect) {
var cRs *C.struct_samure_rect
cRs = (*C.struct_samure_rect)(C.malloc(C.size_t(unsafe.Sizeof(*cRs) * uintptr(len(rs)))))
if cRs == nil {
return
}
for i, r := range rs {
cR := (*C.struct_samure_rect)(unsafe.Pointer(uintptr(unsafe.Pointer(cRs)) + unsafe.Sizeof(*cRs)*uintptr(i)))
cR.x = C.int32_t(r.X)
cR.y = C.int32_t(r.Y)
cR.w = C.int32_t(r.W)
cR.h = C.int32_t(r.H)
}
C.samure_context_set_input_regions(ctx.Handle, cRs, C.size_t(len(rs)))
C.free(unsafe.Pointer(cRs))
}
func (ctx Context) SetKeyboardInteraction(enable bool) {
var cEnable C.int
if enable {
cEnable = 1
}
C.samure_context_set_keyboard_interaction(ctx.Handle, cEnable)
}
func (ctx Context) ProcessEvents() {
C.samure_context_process_events(ctx.Handle)
}
func (ctx Context) RenderLayerSurface(sfc LayerSurface, o Rect) {
C.samure_context_render_layer_surface(ctx.Handle, sfc.Handle, o.convertToC())
}
func (ctx Context) RenderOutput(o Output) {
C.samure_context_render_output(ctx.Handle, o.Handle)
}
func (ctx Context) Update(deltaTime float64) {
C.samure_context_update(ctx.Handle, C.double(deltaTime))
}
func (ctx Context) SetPointerShape(shape int) {
C.samure_context_set_pointer_shape(ctx.Handle, C.uint32_t(shape))
}
func (ctx Context) Flush() {
C.wl_display_flush(ctx.Handle.display)
}
func (ctx Context) Display() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.display)
}
func (ctx Context) Shm() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.shm)
}
func (ctx Context) Compositor() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.compositor)
}
func (ctx Context) LayerShell() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.layer_shell)
}
func (ctx Context) OutputManager() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.output_manager)
}
func (ctx Context) ScreencopyManager() unsafe.Pointer {
return unsafe.Pointer(ctx.Handle.screencopy_manager)
}
func (ctx Context) GetOutputRect() Rect {
r := C.samure_context_get_output_rect(ctx.Handle)
return Rect{
X: int(r.x),
Y: int(r.y),
W: int(r.w),
H: int(r.h),
}
}