rwayland 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d3ec36656cef46c8e78c6c3e149ace1b3e5a8fc2382803fc2380f781614c5ef3
4
+ data.tar.gz: 22ba78b2816bc819284bdb1c759d4191df68d6fad9d217955f6d847456dd5b1b
5
+ SHA512:
6
+ metadata.gz: 5acb97e1d7e9e08b0d3ab4ae9df6042837d5da0baeb1165b3bb854ba4313440f857c455ae3e642b61003195a314f6f471c2292676649e598f3fc7a909c22cab6
7
+ data.tar.gz: 360b2b3eea6ea36b91538057e947f34a157664534791a6a67b143939df2c7906fb107cbe2dd6f827d8b922c339d4ee197cfdee478c02dd90caaca60d1af8ff55
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 nmbits
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # README
2
+
3
+ ## Name
4
+
5
+ rwayland
6
+
7
+ ## Description
8
+
9
+ Ruby bindings for Wayland protocol.
10
+
11
+ ## Dependencies
12
+
13
+ * Ruby >= 3.3
14
+
15
+ ## Recommendations
16
+
17
+ * rcairo
18
+ * pango
19
+
20
+ ## Installation
21
+
22
+ ### RubyGems
23
+
24
+ ```
25
+ $ gem install rwayland
26
+ ```
27
+
28
+ ## Documents
29
+
30
+ There is no document for this library. Please check
31
+ example/01-wayland-book.rb file which is a translated version of
32
+ example code listed in The Wayland Book (https://wayland-book.com/) to
33
+ grasp usage.
data/bin/scanner.rb ADDED
@@ -0,0 +1,8 @@
1
+ if __FILE__ == $0
2
+ require 'wayland/scanner'
3
+ require 'yaml'
4
+ doc = REXML::Document.new ARGF
5
+ scanner = Wayland::Scanner.new doc
6
+ interfaces = scanner.scan
7
+ print YAML.dump(interfaces)
8
+ end
@@ -0,0 +1,94 @@
1
+ require 'wayland'
2
+ require 'wayland/shared_memory'
3
+ require 'wayland/xkb_support'
4
+ require 'logger'
5
+
6
+ module WaylandBook
7
+
8
+ module CBMixin
9
+ def post_init(state)
10
+ @state = state
11
+ end
12
+
13
+ def done(time)
14
+ cb = @state.wl_surface.frame as: [CBMixin, @state]
15
+ if @state.last_frame != 0
16
+ elapsed = time - @state.last_frame
17
+ @state.offset += elapsed / 1000.0 * 24
18
+ end
19
+ buffer = WaylandBook.draw_frame @state
20
+ @state.wl_surface.attach buffer, 0, 0
21
+ @state.wl_surface.damage_buffer 0, 0, 0x7fffffff, 0x7fffffff
22
+ @state.wl_surface.commit
23
+ @state.last_frame = time
24
+ end
25
+ end
26
+
27
+ module_function
28
+
29
+ def draw_frame(state)
30
+ display = state.wl_display
31
+
32
+ width = 640
33
+ height = 480
34
+ stride = width * 4
35
+ size = stride * height
36
+
37
+ shared_memory = Wayland::SharedMemory.new size: size
38
+
39
+ data = Fiddle::Pointer.new shared_memory.address
40
+ pool = display[:wl_shm].create_pool(shared_memory.fd, size)
41
+ buffer = pool.create_buffer(0, width, height, stride, Wayland::Wl::Shm[:format].xrgb8888)
42
+ pool.destroy
43
+ shared_memory.close
44
+
45
+ offset = state.offset.to_i % 8
46
+
47
+ ff6666ee = [0xff6666ee].pack("L")
48
+ ffeeeeee = [0xffeeeeee].pack("L")
49
+
50
+ height.times do |y|
51
+ width.times do |x|
52
+ if (((x + offset) + (y + offset) / 8 * 8) % 16 < 8)
53
+ data[y * stride + x * 4, 4] = ff6666ee
54
+ else
55
+ data[y * stride + x * 4, 4] = ffeeeeee
56
+ end
57
+ end
58
+ end
59
+ buffer.on(:release) { buffer.destroy }
60
+ buffer
61
+ end
62
+
63
+ def main
64
+ display = Wayland::Wl::Display.connect
65
+ display.set_logger Logger.new(STDOUT)
66
+ display.get_registry
67
+ display.roundtrip
68
+
69
+ compositor = display[:wl_compositor]
70
+ wl_surface = compositor.create_surface
71
+
72
+ state = Struct.new(:wl_display, :wl_surface, :offset, :last_frame).new(display, wl_surface, 0, 0)
73
+
74
+ xdg_surface = display[:xdg_wm_base].get_xdg_surface wl_surface
75
+ xdg_surface.on :configure do |serial|
76
+ xdg_surface.ack_configure serial
77
+ buffer = draw_frame(state)
78
+ wl_surface.attach buffer, 0, 0
79
+ wl_surface.commit
80
+ end
81
+
82
+ xdg_toplevel = xdg_surface.get_toplevel
83
+ xdg_toplevel.set_title("Example Client")
84
+ wl_surface.commit
85
+
86
+ cb = wl_surface.frame as: [CBMixin, state]
87
+
88
+ loop { display.dispatch }
89
+ end
90
+ end
91
+
92
+ if __FILE__ == $0
93
+ WaylandBook.main
94
+ end
@@ -0,0 +1,170 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'rwaylandtk'
4
+ require 'pango'
5
+ require 'logger'
6
+
7
+ class MyApplicationWindow < RWaylandTk::ToplevelWindow
8
+
9
+ TITLE_BAR_SIZE = 20
10
+ RESIZER_SIZE = 20
11
+
12
+ def initialize(width, height)
13
+ super
14
+ set_title("Example Window", "Example App")
15
+ @text = File.read __FILE__
16
+ @ty = TITLE_BAR_SIZE
17
+ @cursor = 0
18
+ end
19
+
20
+ def mouse_moved(x, y)
21
+ @px = x
22
+ @py = y
23
+ end
24
+
25
+ def mouse_down(button, time, serial)
26
+ seat = @display[:wl_seat]
27
+ if @py < TITLE_BAR_SIZE
28
+ @xdg_toplevel.move(seat, serial)
29
+ elsif @px > width - RESIZER_SIZE && @py > height - RESIZER_SIZE
30
+ @xdg_toplevel.resize(seat, serial, Wayland::Xdg::Toplevel[:resize_edge].bottom_right)
31
+ else
32
+ lx = @px
33
+ ly = @py - @ty
34
+ ans = content_pango_layout.xy_to_index lx * Pango::SCALE, ly * Pango::SCALE
35
+ inside, index, trailing = ans
36
+ index
37
+ @cursor = index
38
+ invalidate
39
+ end
40
+ end
41
+
42
+ def axis(time, axis, value)
43
+ if axis == 0
44
+ @ty -= value * 2
45
+ end
46
+ invalidate
47
+ end
48
+
49
+ def content_pango_layout
50
+ context = cairo_context
51
+ if context && !@pango_layout
52
+ @pango_layout = context.create_pango_layout
53
+ @pango_layout.font_description = "monospace 10"
54
+ end
55
+ @pango_layout
56
+ end
57
+
58
+ def title_bar_pango_layout
59
+ context = cairo_context
60
+ if context && !@title_bar_pango_layout
61
+ @title_bar_pango_layout = context.create_pango_layout
62
+ @title_bar_pango_layout.font_description = "monospace 10"
63
+ @title_bar_pango_layout.text = title
64
+ end
65
+ @title_bar_pango_layout
66
+ end
67
+
68
+ def cursor_up
69
+ if @cursor > 0
70
+ @pd ||= 0
71
+ rn0 = @text.rindex("\n", @cursor - 1) || 0
72
+ if rn0 > 0
73
+ d = [@cursor - rn0, @pd].max
74
+ rn1 = @text.rindex("\n", rn0 - 1) || 0
75
+ if rn1 + d < rn0
76
+ @pd = 0
77
+ @cursor = rn1 + d
78
+ else
79
+ @pd = d
80
+ @cursor = rn0
81
+ end
82
+ invalidate
83
+ end
84
+ end
85
+ end
86
+
87
+ def cursor_down
88
+ @pd ||= 0
89
+ rn0 = @text.rindex("\n", @cursor - 1) || 0
90
+ d = [@cursor - rn0, @pd].max
91
+ rn1 = @text.index("\n", @cursor)
92
+ if rn1
93
+ rn2 = @text.index("\n", rn1 + 1) || @text.size
94
+ if rn1 + d < rn2
95
+ @pd = 0
96
+ @cursor = rn1 + d
97
+ else
98
+ @pd = d
99
+ @cursor = rn2
100
+ end
101
+ end
102
+ end
103
+
104
+ def key_down(key)
105
+ case key
106
+ when 0x20..0x7e
107
+ @text.insert @cursor, key.chr
108
+ @cursor += 1
109
+ when 0xff51 # LEFT
110
+ @cursor -= 1 if @cursor > 0
111
+ when 0xff52 # UP
112
+ cursor_up
113
+ when 0xff53 # RIGHT
114
+ @cursor += 1 if @cursor < @text.size
115
+ when 0xff54 # DOWN
116
+ cursor_down
117
+ else
118
+ p [key.to_s(16)]
119
+ end
120
+ end
121
+
122
+ def draw
123
+ context = cairo_context
124
+ context.save do
125
+ # background
126
+ context.set_source_color [0.8, 0.8, 0.8, 1.0]
127
+ context.rectangle 0, 0, width, height
128
+ context.fill
129
+
130
+ layout = content_pango_layout
131
+ layout.text = @text
132
+
133
+ # Cursor
134
+ pos = layout.index_to_pos(@cursor)
135
+ context.set_source_color [0, 0.9, 0, 1]
136
+ w = pos.width > 0 ? pos.width / Pango::SCALE : 10
137
+ h = pos.height > 0 ? pos.height / Pango::SCALE : 10
138
+ context.rectangle pos.x / Pango::SCALE, pos.y / Pango::SCALE + @ty, w, h
139
+ context.fill
140
+
141
+ # Text
142
+ context.move_to 0, @ty
143
+ context.set_source_color [0.2, 0.3, 0.2, 1]
144
+ context.show_pango_layout layout
145
+
146
+ # title bar
147
+ title_bar_layout = title_bar_pango_layout
148
+ tw, th = title_bar_layout.pixel_size
149
+ tx = (width - tw) / 2
150
+ ty = (TITLE_BAR_SIZE - th) / 2
151
+ context.set_source_color [0, 0, 0, 0.5]
152
+ context.rectangle 0, 0, width, TITLE_BAR_SIZE
153
+ context.fill
154
+ context.move_to tx, ty
155
+ context.set_source_color [0.9, 0.9, 0.9, 1]
156
+ context.show_pango_layout title_bar_layout
157
+
158
+ # resizer
159
+ context.set_source_color [0, 0, 0, 0.5]
160
+ context.rectangle width - RESIZER_SIZE, height - RESIZER_SIZE, RESIZER_SIZE, RESIZER_SIZE
161
+ context.fill
162
+ end
163
+ end
164
+ end
165
+
166
+ if __FILE__ == $0
167
+ RWaylandTk.init
168
+ w = MyApplicationWindow.new(640, 480).show
169
+ RWaylandTk.main_loop
170
+ end
@@ -0,0 +1,177 @@
1
+ require 'wayland'
2
+ require 'wayland/cairo_support'
3
+ require 'wayland/xkb_support'
4
+
5
+ module RWaylandTk
6
+ module CustomWaylandInterface
7
+ class WlSurface < Wayland::Wl::Surface
8
+ def post_init(window)
9
+ @window = window
10
+ end
11
+ attr_reader :window
12
+ end
13
+
14
+ class XdgToplevel < Wayland::Xdg::Toplevel
15
+ def post_init(window)
16
+ @window = window
17
+ end
18
+
19
+ def configure(width, height, state)
20
+ @window._resize(width, height)
21
+ end
22
+ end
23
+
24
+ class XdgSurface < Wayland::Xdg::Surface
25
+ def post_init(window)
26
+ @window = window
27
+ end
28
+
29
+ def configure(serial)
30
+ @window._configure serial
31
+ ack_configure serial
32
+ end
33
+ end
34
+
35
+ class WlBuffer < Wayland::Wl::Buffer
36
+ def post_init(width, height, stride, format, shared_memory)
37
+ @width = width
38
+ @height = height
39
+ @stride = stride
40
+ @format = format
41
+ @shared_memory = shared_memory
42
+ end
43
+ attr_reader :width, :height, :stride, :format
44
+
45
+ def destroy_on_release
46
+ @destroy_on_release = true
47
+ destroy if @attached == false
48
+ end
49
+
50
+ def attached
51
+ @attached = true
52
+ end
53
+
54
+ def release
55
+ @attached = false
56
+ destroy if @destroy_on_release
57
+ end
58
+
59
+ def attached?
60
+ @attached
61
+ end
62
+
63
+ def cairo_surface
64
+ @cairo_surface ||=
65
+ Wayland::CairoSupport.create_image_surface_for_shared_memory(@shared_memory, 0, 0,
66
+ @width, @height, @stride)
67
+ end
68
+
69
+ def cairo_context
70
+ @cairo_context ||= Cairo::Context.new cairo_surface
71
+ end
72
+ end
73
+
74
+ module WlPointer
75
+ def enter(serial, surface, x, y)
76
+ @surface = surface
77
+ surface.window._mouse_enter x, y
78
+ end
79
+
80
+ def leave(serial, surface)
81
+ surface.window._mouse_leave
82
+ @surface = nil
83
+ end
84
+
85
+ def motion(time, x, y)
86
+ @surface&.window._mouse_moved x, y
87
+ end
88
+
89
+ def button(serial, button, time, state)
90
+ case state
91
+ when Wayland::Wl::Pointer[:button_state].pressed
92
+ @surface&.window._mouse_down(button, time, serial)
93
+ when Wayland::Wl::Pointer[:button_state].released
94
+ @surface&.window._mouse_up(button, time, serial)
95
+ end
96
+ end
97
+
98
+ def axis(time, axis, value)
99
+ @surface&.window._axis(time, axis, value)
100
+ end
101
+ end
102
+
103
+ module WlKeyboard
104
+ def keymap(fmt, fd, size)
105
+ if fmt != Wayland::Wl::Keyboard[:keymap_format].xkb_v1
106
+ raise "unknonw keymap format received"
107
+ end
108
+ shm = Wayland::SharedMemory.new fd, size: size, prot: Wayland::SharedMemory::PROT_READ
109
+ @xkb_ctx = Wayland::Xkb::Context.new
110
+ @xkb_keymap = @xkb_ctx.keymap_new_from_string(shm.address,
111
+ Wayland::XkbSupport::XKB_KEYMAP_FORMAT_TEXT_V1,
112
+ Wayland::XkbSupport::XKB_KEYMAP_COMPILE_NO_FLAGS)
113
+ @xkb_state = @xkb_keymap.state_new
114
+ shm.close
115
+ end
116
+
117
+ def enter(serial, surface, keys)
118
+ @surface = surface
119
+ if keys
120
+ keys.each_byte do |b|
121
+ s = @xkb_state.key_get_one_sym(b + 8)
122
+ end
123
+ end
124
+ end
125
+
126
+ def leave(serial, surface)
127
+ end
128
+
129
+ def key(serial, time, key, state)
130
+ s = @xkb_state.key_get_one_sym(key + 8)
131
+ # buff = "\0" * 128
132
+ # len, buffer = @xkb_state.key_get_utf8(key + 8, buff)
133
+ # data = buff.byteslice(0, len)
134
+ if state == 1
135
+ @surface.window&._key_down s
136
+ else
137
+ @surface.window&._key_up s
138
+ end
139
+ end
140
+
141
+ def modifiers(serial, mods_depressed, mods_latched, mods_locked, group)
142
+ @xkb_state.update_mask(mods_depressed, mods_latched, mods_locked, 0, 0, group)
143
+ end
144
+ end
145
+
146
+ module WlSeat
147
+ def capabilities(caps)
148
+ if (caps & Wayland::Wl::Seat[:capability][:pointer] != 0)
149
+ get_pointer as: WlPointer
150
+ end
151
+ if (caps & Wayland::Wl::Seat[:capability][:keyboard] != 0)
152
+ get_keyboard as: WlKeyboard
153
+ end
154
+ end
155
+ end
156
+
157
+ module WlRegistry
158
+ def global(name, interface, version)
159
+ if interface == "wl_seat"
160
+ bind name, interface, version, :wl_seat, as: WlSeat
161
+ else
162
+ super
163
+ end
164
+ end
165
+ end
166
+
167
+ class FrameCB < Wayland::Wl::Callback
168
+ def post_init(window)
169
+ @window = window
170
+ end
171
+
172
+ def done(callback_data)
173
+ @window._frame_done
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,194 @@
1
+ require 'rwaylandtk/custom_wl_interface'
2
+
3
+ module RWaylandTk
4
+
5
+ class ToplevelWindow
6
+
7
+ include CustomWaylandInterface
8
+
9
+ def initialize(width, height)
10
+ @display = RWaylandTk.display_instance
11
+ @width = width
12
+ @height = height
13
+ @valid = false
14
+ @wl_surface = @display[:wl_compositor].create_surface as: [WlSurface, self]
15
+ @xdg_surface = @display[:xdg_wm_base].get_xdg_surface @wl_surface, as: [XdgSurface, self]
16
+ @xdg_toplevel = @xdg_surface.get_toplevel as: [XdgToplevel, self]
17
+ end
18
+ attr_reader :width, :height
19
+
20
+ # User hook
21
+
22
+ def mouse_enter(x, y); end
23
+ def mouse_leave; end
24
+ def mouse_moved(x, y); end
25
+ def mouse_up(button, time, serial); end
26
+ def mouse_down(button, time, serial); end
27
+ def axis(time, axis, value) ; end
28
+ def resized(width, height); end
29
+ def key_down(data); end
30
+ def key_up(data); end
31
+
32
+ BGCOLOR = [1.0, 1.0, 1.0, 1.0]
33
+
34
+ def draw
35
+ cairo = cairo_context
36
+ cairo.save do
37
+ cairo.set_source_color BGCOLOR
38
+ cairo.rectangle 0, 0, width, height
39
+ cairo.fill
40
+ end
41
+ end
42
+
43
+ # Hook
44
+
45
+ def _mouse_enter(x, y)
46
+ # p [:mouse_enter, x, y]
47
+ mouse_enter x, y
48
+ # update_if_needed
49
+ end
50
+
51
+ def _mouse_leave
52
+ # p [:mouse_leave]
53
+ mouse_leave
54
+ # update_if_needed
55
+ end
56
+
57
+ def _mouse_moved(x, y)
58
+ mouse_moved x, y
59
+ update_if_needed
60
+ end
61
+
62
+ def _mouse_up(button, time, serial)
63
+ mouse_up(button, time, serial)
64
+ update_if_needed
65
+ end
66
+
67
+ def _mouse_down(button, time, serial)
68
+ mouse_down(button, time, serial)
69
+ update_if_needed
70
+ end
71
+
72
+ def _axis(time, axis, value)
73
+ axis(time, axis, value)
74
+ update_if_needed
75
+ end
76
+
77
+ def _resize(width, height)
78
+ if width > 0 && height > 0
79
+ @width = width
80
+ @height = height
81
+ resized width, height
82
+ update_if_needed
83
+ end
84
+ end
85
+
86
+ def _frame_done
87
+ @frame_requested = false
88
+ realloc_buffer
89
+ draw
90
+ _expose
91
+ end
92
+
93
+ def _expose
94
+ @wl_surface.attach @wl_buffer, 0, 0
95
+ @wl_buffer.attached
96
+ @wl_surface.damage_buffer 0, 0, 0x7fffffff, 0x7fffffff
97
+ @wl_surface.commit
98
+ validate
99
+ end
100
+
101
+ def _configure(serial)
102
+ end
103
+
104
+ def _key_down(data)
105
+ key_down(data)
106
+ update_if_needed
107
+ end
108
+
109
+ def _key_up(data)
110
+ key_up(data)
111
+ update_if_needed
112
+ end
113
+
114
+ # API
115
+
116
+ def cairo_context
117
+ @wl_buffer.cairo_context
118
+ end
119
+
120
+ def request_frame
121
+ unless @frame_requested
122
+ @wl_surface.frame as: [FrameCB, self]
123
+ @wl_surface.commit
124
+ @frame_requested = true
125
+ end
126
+ end
127
+
128
+ def update_if_needed
129
+ return if valid?
130
+ request_frame
131
+ end
132
+
133
+ def set_title(title, app_id, commit = true)
134
+ @xdg_toplevel.set_title title
135
+ @xdg_toplevel.set_app_id app_id
136
+ @wl_surface.commit if commit
137
+ @title = title
138
+ @app_id = app_id
139
+ end
140
+ attr_reader :title, :app_id
141
+
142
+ def bpp; 4 end # pseudo
143
+
144
+ def buffer_realloc?
145
+ return false if width == 0 || height == 0
146
+ return true unless @wl_buffer
147
+ return true if @wl_buffer.attached?
148
+ return false if @wl_buffer.width == width && @wl_buffer.height == height
149
+ return true
150
+ end
151
+
152
+ def realloc_buffer
153
+ if buffer_realloc?
154
+ stride = width * bpp
155
+ size = stride * height
156
+ shm = Wayland::SharedMemory.new size: size
157
+ pool = @display[:wl_shm].create_pool shm.fd, size
158
+ format = Wayland::Wl::Shm[:format].xrgb8888
159
+ buffer = pool.create_buffer(0, width, height, stride, format,
160
+ as: [WlBuffer, width, height, stride, format, shm])
161
+ pool.destroy
162
+ shm.close
163
+ @wl_buffer.destroy_on_release if @wl_buffer
164
+ cairo = buffer.cairo_context
165
+ cairo.save do
166
+ cairo.set_source_color BGCOLOR
167
+ cairo.rectangle 0, 0, width, height
168
+ cairo.fill
169
+ end
170
+ @wl_buffer = buffer
171
+ end
172
+ @wl_buffer
173
+ end
174
+
175
+ def invalidate
176
+ @valid = false
177
+ end
178
+
179
+ def validate
180
+ @valid = false
181
+ end
182
+
183
+ def valid?
184
+ @valid
185
+ end
186
+
187
+ def show
188
+ invalidate
189
+ realloc_buffer
190
+ draw
191
+ _expose
192
+ end
193
+ end
194
+ end