rwayland 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ require 'wayland'
2
+ require 'wayland/shared_memory'
3
+ require 'wayland/cairo_support'
4
+ require 'wayland/xkb_support'
5
+
6
+ module MiW
7
+ module Wl
8
+ module Private
9
+ class WlSurface < Wayland::Wl::Surface
10
+ def post_init(window)
11
+ @window = window
12
+ end
13
+ attr_reader :window
14
+
15
+ def attach(buffer)
16
+ @buffer = buffer
17
+ buffer.attached
18
+ super buffer, 0, 0
19
+ end
20
+
21
+ def commit
22
+ @buffer&.committed
23
+ super
24
+ end
25
+ end
26
+
27
+ class XdgSurface < Wayland::Xdg::Surface
28
+ def post_init(window)
29
+ @window = window
30
+ end
31
+
32
+ def configure(serial)
33
+ ack_configure serial
34
+ @window.on_xdg_surface_configure(serial)
35
+ end
36
+ end
37
+
38
+ class XdgPopup < Wayland::Xdg::Popup
39
+ def post_init(window)
40
+ @window = window
41
+ end
42
+
43
+ def configure(x, y, width, height)
44
+ @x = x
45
+ @y = y
46
+ @width = width
47
+ @height = height
48
+ end
49
+ attr_reader :x, :y, :width, :height
50
+
51
+ def popup_done
52
+ @window.on_xdg_popup_popup_done
53
+ end
54
+
55
+ def repositioned(token)
56
+ @window.on_xdg_popup_repositioned token
57
+ end
58
+ end
59
+
60
+ class WlShmPool < Wayland::Wl::ShmPool
61
+ def post_init(width, height, bpp, shared_memory)
62
+ @width = width
63
+ @height = height
64
+ @bpp = bpp
65
+ @shared_memory = shared_memory
66
+ end
67
+ attr_reader :width, :height, :bpp, :shared_memory
68
+
69
+ def stride
70
+ @width * @bpp
71
+ end
72
+
73
+ def self.create(display, width, height, bpp)
74
+ stride = width * bpp
75
+ size = stride * height
76
+ shm = Wayland::SharedMemory.new size: size
77
+ pool = display[:wl_shm].create_pool shm.fd, size, as: [self, width, height, bpp, shm]
78
+ end
79
+ end
80
+
81
+ class WlBuffer < Wayland::Wl::Buffer
82
+ def post_init(width, height, format, pool)
83
+ @width = width
84
+ @height = height
85
+ @format = format
86
+ @pool = pool
87
+ end
88
+ attr_reader :width, :height, :format, :pool
89
+
90
+ def destroy_now
91
+ unless @destroyed
92
+ @destroyed = true
93
+ destroy
94
+ end
95
+ end
96
+
97
+ def attached
98
+ @attached = true
99
+ end
100
+
101
+ def committed
102
+ @committed = true
103
+ end
104
+
105
+ def release
106
+ @attached = false
107
+ @committed = false
108
+ destroy_now if @destroy_on_release
109
+ end
110
+
111
+ def busy?
112
+ @attached && @committed
113
+ end
114
+
115
+ def lasy_destroy
116
+ @destroy_on_release = true
117
+ destroy_now unless busy?
118
+ end
119
+
120
+ def cairo_image_surface
121
+ @cairo_surface ||=
122
+ Wayland::CairoSupport.create_image_surface_for_shared_memory(@pool.shared_memory, 0, 0,
123
+ @width, @height, @pool.stride)
124
+ end
125
+
126
+ def self.create(pool, width, height, format)
127
+ pool.create_buffer(0, width, height, pool.stride, format,
128
+ as: [self, width, height, format, pool])
129
+ end
130
+ end
131
+
132
+ class WlPointer < Wayland::Wl::Pointer
133
+ def enter(serial, surface, x, y)
134
+ @surface = surface
135
+ surface.window.on_wl_pointer_enter serial, x, y
136
+ end
137
+
138
+ def leave(serial, surface)
139
+ surface.window.on_wl_pointer_leave serial
140
+ @surface = nil
141
+ end
142
+
143
+ def motion(time, x, y)
144
+ @surface.window.on_wl_pointer_motion time, x, y if @surface
145
+ end
146
+
147
+ def button(serial, button, time, state)
148
+ @surface.window.on_wl_pointer_button serial, button, time, state if @surface
149
+ end
150
+
151
+ def axis(time, ax, value)
152
+ @surface.window.on_wl_pointer_axis time, ax, value if @surface
153
+ end
154
+ end
155
+
156
+ class WlKeyboard < Wayland::Wl::Keyboard
157
+ def keymap(fmt, fd, size)
158
+ if fmt != Wayland::Wl::Keyboard[:keymap_format].xkb_v1
159
+ raise "unknonw keymap format received"
160
+ end
161
+ shm = Wayland::SharedMemory.new fd, size: size, prot: Wayland::SharedMemory::PROT_READ
162
+ @xkb_ctx = Wayland::Xkb::Context.new
163
+ @xkb_keymap = @xkb_ctx.keymap_new_from_string(shm.address,
164
+ Wayland::XkbSupport::XKB_KEYMAP_FORMAT_TEXT_V1,
165
+ Wayland::XkbSupport::XKB_KEYMAP_COMPILE_NO_FLAGS)
166
+ @xkb_state = @xkb_keymap.state_new
167
+ end
168
+
169
+ def enter(serial, surface, keys)
170
+ @surface = surface
171
+ if keys
172
+ keys.each_byte do |b|
173
+ s = @xkb_state.key_get_one_sym(b + 8)
174
+ end
175
+ end
176
+ end
177
+
178
+ def leave(serial, surface)
179
+ end
180
+
181
+ def key(serial, time, key, state)
182
+ s = @xkb_state.key_get_one_sym(key + 8)
183
+ # buff = "\0" * 128
184
+ # len, buffer = @xkb_state.key_get_utf8(key + 8, buff)
185
+ # data = buff.byteslice(0, len)
186
+ @surface.window&.on_wl_keyboard_key(serial, time, s, state)
187
+ end
188
+
189
+ def modifiers(serial, mods_depressed, mods_latched, mods_locked, group)
190
+ @xkb_state.update_mask(mods_depressed, mods_latched, mods_locked, 0, 0, group)
191
+ end
192
+ end
193
+
194
+ class WlSeat < Wayland::Wl::Seat
195
+ def capabilities(caps)
196
+ if (caps & Wayland::Wl::Seat[:capability][:pointer] != 0)
197
+ get_pointer as: WlPointer
198
+ end
199
+ if (caps & Wayland::Wl::Seat[:capability][:keyboard] != 0)
200
+ get_keyboard as: WlKeyboard
201
+ end
202
+ end
203
+ end
204
+
205
+ class FrameCB < Wayland::Wl::Callback
206
+ def post_init(window)
207
+ @window = window
208
+ end
209
+
210
+ def done(data)
211
+ @window.on_framecb_done data
212
+ end
213
+ end
214
+
215
+ class XdgToplevel < Wayland::Xdg::Toplevel
216
+ def post_init(window)
217
+ @window = window
218
+ end
219
+
220
+ def configure(width, height, states)
221
+ @width = width
222
+ @height = height
223
+ @states = states&.unpack("L*") || []
224
+ end
225
+
226
+ def configure_bounds(width, height)
227
+ @bounds_width = width
228
+ @bounds_height = height
229
+ end
230
+ attr_reader :width, :height, :states,
231
+ :bounds_width, :bounds_height
232
+ end
233
+ end # Private
234
+ end # Wl
235
+ end