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,338 @@
1
+ require "miw/rectangle"
2
+ require "miw/size"
3
+ require "miw/wl"
4
+ require "miw/wl/private"
5
+
6
+ module MiW
7
+ module Wl
8
+ class Window
9
+
10
+ PIXEL_BLOCK_SIZE = 256
11
+
12
+ # Initializer.
13
+ #
14
+ # +client+ :: client
15
+ # +width+ :: client width
16
+ # +height+ :: client height
17
+ #
18
+ def initialize(client, width, height)
19
+ raise ArgumentError, "width and height should be greater than 0" if width <= 0 || height <= 0
20
+ @init_size = Size.new(width.to_i, height.to_i)
21
+ @states = 0
22
+ @client = client
23
+ @display = Wl.display_instance
24
+ @wl_surface = @display[:wl_compositor].create_surface as: [Private::WlSurface, self]
25
+ @xdg_surface = @display[:xdg_wm_base].get_xdg_surface @wl_surface, as: [Private::XdgSurface, self]
26
+ end
27
+ attr_reader :states
28
+ attr_reader :surface_geometry
29
+ attr_reader :invalid_rect
30
+
31
+ def client_size
32
+ @client_geometry&.size || Size.new(0, 0)
33
+ end
34
+
35
+ # Byte per pixel.
36
+ #
37
+ def bpp; 4 end # pseudo
38
+
39
+ # True if wl_shm_pool should be (re)allocated.
40
+ #
41
+ private def pool_realloc?
42
+ return true if @wl_shm_pool.nil?
43
+ return true if @wl_buffur&.busy?
44
+ return true if width > @wl_shm_pool.width
45
+ return true if height > @wl_shm_pool.height
46
+ pblks = (@wl_shm_pool.width + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE
47
+ if pblks > 1
48
+ wblks = (width + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE
49
+ return true if wblks <= pblks / 2
50
+ end
51
+ pblks = (@wl_shm_pool.height + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE
52
+ if pblks > 1
53
+ wblks = (height + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE
54
+ return true if wblks <= pblks / 2
55
+ end
56
+ return false
57
+ end
58
+
59
+ # (Re)allocate a wl_shm_pool instance.
60
+ #
61
+ private def realloc_pool
62
+ if pool_realloc?
63
+ return nil if width == 0 || height == 0 #TODO
64
+ pw = (width + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE * PIXEL_BLOCK_SIZE
65
+ ph = (height + PIXEL_BLOCK_SIZE - 1) / PIXEL_BLOCK_SIZE * PIXEL_BLOCK_SIZE
66
+ @wl_shm_pool&.destroy
67
+ @wl_shm_pool = Private::WlShmPool.create @display, pw, ph, bpp
68
+ end
69
+ @wl_shm_pool
70
+ end
71
+
72
+ # True if wl_buffer should be (re)allocated.
73
+ #
74
+ private def buffer_realloc?
75
+ return false if width == 0 || height == 0
76
+ return true if @wl_buffer.nil?
77
+ return true if @wl_shm_pool != @wl_buffer.pool
78
+ return true if @wl_buffer.width != width || @wl_buffer.height != height
79
+ return true if @wl_buffer.busy?
80
+ return false
81
+ end
82
+
83
+ # (Re)allocate wl_buffer.
84
+ #
85
+ private def realloc_buffer
86
+ pool = realloc_pool
87
+ if buffer_realloc?
88
+ @wl_buffer&.lasy_destroy
89
+ @wl_buffer = Private::WlBuffer.create pool, width, height, Wayland::Wl::Shm[:format].argb8888
90
+ @invalid_rect = Rectangle.new(0, 0, width, height)
91
+ end
92
+ @wl_buffer
93
+ end
94
+
95
+ # Request wl_surface#commit.
96
+ #
97
+ def commit_now
98
+ @wl_surface.commit
99
+ @need_commit = false
100
+ end
101
+
102
+ #
103
+ #
104
+ def lasy_commit
105
+ @need_commit = true
106
+ end
107
+
108
+ def commit_if_needed
109
+ commit_now if @need_commit
110
+ end
111
+
112
+ # Request wl_surface#frame.
113
+ #
114
+ def lasy_update
115
+ if invalid? && !@frame_requested
116
+ @frame_requested = true
117
+ @wl_surface.frame as: [Private::FrameCB, self]
118
+ lasy_commit
119
+ end
120
+ end
121
+
122
+ def update
123
+ if invalid?
124
+ realloc_buffer
125
+ draw
126
+ @wl_surface.attach @wl_buffer
127
+ @wl_surface.damage_buffer(@invalid_rect.x, @invalid_rect.y,
128
+ @invalid_rect.width, @invalid_rect.height)
129
+ @invalid_rect = nil
130
+ lasy_commit
131
+ commit_now
132
+ end
133
+ end
134
+
135
+ def draw
136
+ cairo = Cairo::Context.new @wl_buffer.cairo_image_surface
137
+ cairo.save { draw_nonclient cairo }
138
+ cairo.save { draw_client cairo }
139
+ end
140
+
141
+ private def draw_client(cairo)
142
+ rect = convert_to_client @invalid_rect.dup.intersect(@client_geometry)
143
+ if rect.width > 0 && rect.height > 0
144
+ cairo.translate @client_geometry.x, @client_geometry.y
145
+ cairo.rectangle 0, 0, @client_geometry.width, @client_geometry.height
146
+ cairo.clip
147
+ @client.draw cairo, rect
148
+ end
149
+ end
150
+
151
+ private def draw_nonclient(cairo)
152
+ end
153
+
154
+ def invalid?
155
+ @invalid_rect&.valid?
156
+ end
157
+
158
+ # Mark rectangle area as invalid. Request wl_surfae#frame.
159
+ #
160
+ # +x+ :: left
161
+ # +y+ :: top
162
+ # +w+ :: width
163
+ # +h+ :: height
164
+ def invalidate(x, y, w, h)
165
+ if @invalid_rect
166
+ @invalid_rect.union x, y, w, h
167
+ else
168
+ @invalid_rect = Rectangle.new(x, y, w, h)
169
+ end
170
+ end
171
+
172
+ def invalidate_all
173
+ invalidate(0, 0, width, height)
174
+ end
175
+
176
+ def invalidate_client(cx, cy, w, h)
177
+ cr = Rectangle.new(cx, cy, w, h)
178
+ wr = convert_to_window(cr).intersect @client_geometry
179
+ invalidate wr.x, wr.y, wr.width, wr.height if wr.valid?
180
+ end
181
+
182
+ # event hooks
183
+
184
+ ## FrameCB
185
+
186
+ def on_framecb_done(time)
187
+ @frame_requested = false
188
+ update
189
+ end
190
+
191
+ ## WlPointer
192
+
193
+ def on_wl_pointer_enter(serial, x, y)
194
+ @serial = serial
195
+ @pointer_x = x
196
+ @pointer_y = y
197
+ cx, cy = convert_to_client x, y
198
+ @client.pointer_enter cx, cy
199
+ lasy_update
200
+ commit_if_needed
201
+ end
202
+
203
+ def on_wl_pointer_leave(serial)
204
+ @serial = serial
205
+ @client.pointer_leave
206
+ lasy_update
207
+ commit_if_needed
208
+ end
209
+
210
+ def on_wl_pointer_motion(time, x, y)
211
+ @poitner_x = x
212
+ @poitner_y = y
213
+ cx, cy = convert_to_client x, y
214
+ @client.pointer_motion time, cx, cy
215
+ lasy_update
216
+ commit_if_needed
217
+ end
218
+
219
+ def on_wl_pointer_button(serial, time, button, state)
220
+ @serial = serial
221
+ @client.pointer_button(time, button, state)
222
+ lasy_update
223
+ commit_if_needed
224
+ end
225
+
226
+ def on_wl_pointer_axis(time, axis, value)
227
+ end
228
+
229
+ def set_default_cursor(serial, loc)
230
+ name = case loc
231
+ when :top_left
232
+ "nw-resize"
233
+ when :top
234
+ "n-resize"
235
+ when :top_right
236
+ "ne-resize"
237
+ when :left
238
+ "w-resize"
239
+ when :right
240
+ "e-resize"
241
+ when :bottom_left
242
+ "sw-resize"
243
+ when :bottom
244
+ "s-resize"
245
+ when :bottom_right
246
+ "se-resize"
247
+ else
248
+ "arrow"
249
+ end
250
+ surface, hx, hy = MiW::Wl.cursor_surface name
251
+ if surface && @prev_name != name
252
+ @prev_name = name
253
+ @display[:wl_pointer].set_cursor serial, surface, hx, hy
254
+ end
255
+ end
256
+
257
+ def reset_cursor
258
+ @prev_name = nil
259
+ end
260
+
261
+ def on_wl_keyboard_key(serial, time, keysym, state)
262
+ @client.keyboard_key(time, keysym, state)
263
+ lasy_update
264
+ commit_if_needed
265
+ end
266
+ # convert
267
+
268
+ def convert_to_client(wx, wy = nil)
269
+ if wy.nil?
270
+ wx.dup.offset_by -@client_geometry.x, -@client_geometry.y
271
+ else
272
+ return wx - @client_geometry.x, wy - @client_geometry.y
273
+ end
274
+ end
275
+
276
+ def convert_to_window(cx, cy = nil)
277
+ if cy.nil?
278
+ cx.dup.offset_by @client_geometry.x, @client_geometry.y
279
+ else
280
+ return cx + @client_geometry.x, cy + @client_geometry.y
281
+ end
282
+ end
283
+
284
+ def pulse
285
+ @client&.pulse
286
+ end
287
+
288
+ def hit_test(x, y)
289
+ :client
290
+ end
291
+
292
+ # control methods
293
+
294
+ def show
295
+ end
296
+
297
+ def hide
298
+ end
299
+
300
+ def minimize
301
+ end
302
+
303
+ def title=(title)
304
+ end
305
+
306
+ def title
307
+ end
308
+
309
+ def surface
310
+ end
311
+
312
+ def sync
313
+ end
314
+
315
+ def size
316
+ end
317
+
318
+ def move_to
319
+ end
320
+
321
+ def resize_to(width, height)
322
+ @surface_geometry ||= Rectangle.new(0, 0, 0, 0)
323
+ @surface_geometry.resize_to width, height
324
+ end
325
+
326
+ def width
327
+ @surface_geometry.width
328
+ end
329
+
330
+ def height
331
+ @surface_geometry.height
332
+ end
333
+
334
+ def quit_requested
335
+ end
336
+ end
337
+ end
338
+ end
data/example/miw/wl.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'wayland'
2
+ require 'wayland/cursor_support'
3
+ require 'miw/wl/private'
4
+ require 'logger'
5
+
6
+ module MiW
7
+ module Wl
8
+ def self.init(**config)
9
+ Wayland::Protocol.load_yaml File.join(File.dirname(__FILE__), "xdg-decoration-unstable-v1.yaml")
10
+ @display = Wayland::Wl::Display.connect
11
+ # @display.set_logger Logger.new(STDOUT)
12
+ registry = @display.get_registry
13
+ registry.set_global_module(wl_seat: Private::WlSeat)
14
+ @display.roundtrip
15
+ load_cursor_theme config[:cusor_theme], config[:cursor_size]
16
+ end
17
+
18
+ def self.load_cursor_theme(theme, size)
19
+ wl_shm = @display[:wl_shm]
20
+ @cursor_surfaces = Hash.new
21
+ @cursor_theme = Wayland::CursorSupport.load_xcursor wl_shm, theme, size
22
+ %w(nw-resize n-resize ne-resize w-resize arrow e-resize sw-resize s-resize se-resize).each do |name|
23
+ surface = @display[:wl_compositor].create_surface
24
+ image = cursor_theme[name][0]
25
+ wl_buffer = image.wl_buffer
26
+ surface.attach wl_buffer, 0, 0
27
+ surface.commit
28
+ @cursor_surfaces[name] = [surface, image.hotspot_x, image.hotspot_y]
29
+ end
30
+ end
31
+
32
+ def self.cursor_surface(name)
33
+ @cursor_surfaces[name]
34
+ end
35
+
36
+ def self.cursor_theme
37
+ @cursor_theme
38
+ end
39
+
40
+ def self.main_loop
41
+ tm0 = Time.now
42
+ loop do
43
+ @display.dispatch 0.02
44
+ tm1 = Time.now
45
+ if tm1 - tm0 > 0.02
46
+ @display.get_objects_by_ifname(:wl_surface).each do |s|
47
+ if s.respond_to? :window
48
+ s.window&.pulse
49
+ end
50
+ end
51
+ tm0 = tm1
52
+ end
53
+ end
54
+ end
55
+
56
+ def self.display_instance
57
+ @display
58
+ end
59
+ end
60
+ end
@@ -9,8 +9,14 @@ module Wayland
9
9
  cairo_so = $".find {|f| File.basename(f) == "cairo.so" }
10
10
  raise "cairo.so not found" unless cairo_so
11
11
  dlload cairo_so
12
- extern "void *rb_cairo_surface_to_ruby_object(void *)"
13
12
  extern "void *cairo_image_surface_create_for_data(void *, int, int, int, int)"
13
+ addr = handler.sym "rb_cairo_surface_to_ruby_object"
14
+ RB_CAIRO_SURFACE_TO_RUBY_OBJECT =
15
+ Fiddle::Function.new(addr, [Fiddle::TYPE_VOIDP], Fiddle::TYPE_UINTPTR_T, need_gvl: true)
16
+ module_function
17
+ def rb_cairo_surface_to_ruby_object(adr)
18
+ RB_CAIRO_SURFACE_TO_RUBY_OBJECT.call adr
19
+ end
14
20
  end
15
21
 
16
22
  module_function
@@ -0,0 +1,81 @@
1
+ module Wayland
2
+ module CursorSupport
3
+ class CursorImage
4
+ def initialize(cursor, index, hotspot_x, hotspot_y, delay)
5
+ @cursor = cursor
6
+ @index = index
7
+ @hotspot_x = hotspot_x
8
+ @hotspot_y = hotspot_y
9
+ @delay = delay
10
+ end
11
+ attr_reader :cursor, :hotspot_x, :hotspot_y, :delay
12
+
13
+ def wl_buffer(as: nil)
14
+ @cursor.theme.image_pool.get_buffer(@index, as: as)
15
+ end
16
+
17
+ def width
18
+ @cursor.theme.image_pool.attribute(@index).width
19
+ end
20
+
21
+ def height
22
+ @cursor.theme.image_pool.attribute(@index).height
23
+ end
24
+
25
+ def format
26
+ @cursor.theme.image_pool.attribute(@index).format
27
+ end
28
+
29
+ def pixels
30
+ @cursor.theme.image_pool.pixels(@index)
31
+ end
32
+ end
33
+
34
+ class Cursor
35
+ def initialize(theme, name)
36
+ @theme = theme
37
+ @name = name
38
+ @images = []
39
+ end
40
+ attr_reader :name, :size
41
+ attr_accessor :theme
42
+
43
+ def count
44
+ @images.size
45
+ end
46
+
47
+ def [](index)
48
+ @images[index]
49
+ end
50
+
51
+ def add_image(image)
52
+ @images << image
53
+ end
54
+ end
55
+
56
+ class CursorTheme
57
+ def initialize(name, image_pool)
58
+ @name = name
59
+ @image_pool = image_pool
60
+ @cursors = {}
61
+ end
62
+ attr_reader :name, :image_pool
63
+
64
+ def [](name)
65
+ @cursors[name]
66
+ end
67
+
68
+ def names
69
+ @cursors.keys
70
+ end
71
+
72
+ def add_cursor_image(name, width, height, hotspot_x, hotspot_y, delay, pixels)
73
+ cursor = (@cursors[name] ||= Cursor.new(self, name))
74
+ index = @image_pool.add_image width, height, width * 4,
75
+ Wayland::Wl::Shm[:format].argb8888, pixels
76
+ image = CursorImage.new cursor, index, hotspot_x, hotspot_y, delay
77
+ cursor.add_image image
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,227 @@
1
+ module Wayland
2
+ module CursorSupport
3
+ class XCursor
4
+ XCURSOR_IMAGE_TYPE = 0xfffd0002
5
+ XCURSOR_IMAGE_VERSION = 1
6
+ XCURSOR_IMAGE_MAX_SIZE = 0x7fff
7
+
8
+ class Image < Struct.new(:version, :width, :height, :xhots, :yhots, :delay, :size, :pixels)
9
+ end
10
+
11
+ class FileHeader < Struct.new(:magic, :header, :version, :ntoc, :tocs)
12
+ def each_toc(&blk)
13
+ if block_given?
14
+ self.tocs.each &blk
15
+ else
16
+ self.tocs.each
17
+ end
18
+ end
19
+
20
+ def each_image_toc
21
+ if block_given?
22
+ each_toc{|toc| yield toc if toc.image? }
23
+ else
24
+ self.to_enum __callee__
25
+ end
26
+ end
27
+
28
+ def best_size(size)
29
+ each_image_toc.inject 0 do |r, toc|
30
+ toc_size = toc.subtype
31
+ toc.image? && (toc_size - size).abs < (toc_size - r).abs ? toc_size : r
32
+ end
33
+ end
34
+ end
35
+
36
+ class Toc < Struct.new(:type, :subtype, :position)
37
+ def seek(io)
38
+ io.seek position, :SET
39
+ end
40
+
41
+ def image?
42
+ self.type == XCURSOR_IMAGE_TYPE
43
+ end
44
+ end
45
+
46
+ class ChunkHeader < Struct.new(:header, :type, :subtype, :version)
47
+ def image?
48
+ self.type == XCURSOR_IMAGE_TYPE
49
+ end
50
+ end
51
+
52
+ def initialize(name, version, size, cursor_images)
53
+ @name = name
54
+ @version = version
55
+ @size = size
56
+ @cursor_images = cursor_images
57
+ end
58
+ attr_reader :name, :version, :size
59
+
60
+ def cursor_image(i)
61
+ @cursor_images[i]
62
+ end
63
+
64
+ def each(&blk)
65
+ @cursor_images.each &blk
66
+ end
67
+
68
+ def count_image
69
+ @cursor_images.size
70
+ end
71
+
72
+ def self.load_path(path, size)
73
+ cursor_images = []
74
+ version = nil
75
+ best_size = 0
76
+ File.open(path) do |io|
77
+ file_header = read_file_header(io)
78
+ version = file_header.version
79
+ best_size = file_header.best_size size
80
+ file_header.each_image_toc do |toc|
81
+ cursor_images << read_image(io, toc).freeze if toc.subtype == best_size
82
+ end
83
+ end
84
+ self.new File.basename(path), version, best_size, cursor_images
85
+ end
86
+
87
+ def self.readu32le(io)
88
+ s = io.read(4)
89
+ raise "Cannot read 4B" if s.nil? || s.bytesize != 4
90
+ s.unpack1 "L<"
91
+ end
92
+
93
+ def self.read_file_header(io)
94
+ io.seek 0, :SET
95
+ header = FileHeader.new
96
+ header.magic = io.read(4)
97
+ raise "Not a xcursor file" unless header.magic == "Xcur"
98
+ header.header = readu32le(io)
99
+ header.version = readu32le(io)
100
+ header.ntoc = readu32le(io)
101
+ header.tocs = []
102
+ io.seek header.header, :SET
103
+ header.ntoc.times do |i|
104
+ toc = Toc.new
105
+ toc.type = readu32le(io)
106
+ toc.subtype = readu32le(io)
107
+ toc.position = readu32le(io)
108
+ header.tocs << toc
109
+ end
110
+ header
111
+ end
112
+
113
+ def self.read_chunk_header(io, toc)
114
+ ch = ChunkHeader.new
115
+ io.seek toc.position, IO::SEEK_SET
116
+ ch.header = readu32le(io)
117
+ ch.type = readu32le(io)
118
+ ch.subtype = readu32le(io)
119
+ ch.version = readu32le(io)
120
+ if ch.type != toc.type || ch.subtype != toc.subtype
121
+ raise "type or subtype mismatch"
122
+ end
123
+ ch
124
+ end
125
+
126
+ def self.read_image(io, toc)
127
+ ch = read_chunk_header(io, toc)
128
+ im = Image.new XCURSOR_IMAGE_VERSION, # version
129
+ readu32le(io), # width
130
+ readu32le(io), # height
131
+ readu32le(io), # xhots
132
+ readu32le(io), # yhots
133
+ readu32le(io) # delay
134
+ if im.width > XCURSOR_IMAGE_MAX_SIZE || im.height > XCURSOR_IMAGE_MAX_SIZE ||
135
+ im.width == 0 || im.height == 0 || im.xhots > im.width || im.yhots > im.height
136
+ raise "Invalid file"
137
+ end
138
+ if ch.version < im.version
139
+ im.version = ch.version
140
+ end
141
+ im.size = ch.subtype
142
+ im.pixels = io.read im.width * im.height * 4
143
+ im
144
+ end
145
+ end # XCursor
146
+
147
+ class XCursorTheme
148
+ XCURSORPATH = %w(~/.icons /usr/share/icons /usr/share/pixmaps ~/.cursors /usr/share/cursors/xorg-x11 /usr/X11R6/lib/X11/icons)
149
+ XDG_DATA_HOME_FALLBACK = "~/.local/share"
150
+ def initialize(name, size, xcursors)
151
+ @name = name
152
+ @size = size
153
+ @xcursors = xcursors
154
+ end
155
+ attr_reader :name, :size
156
+
157
+ def [](name)
158
+ @xcursors[name]
159
+ end
160
+
161
+ def each(&blk)
162
+ @xcursors.each_value &blk
163
+ end
164
+
165
+ def self.library_path
166
+ env_var = ENV["XCURSOR_PATH"]
167
+ return [env_var] if env_var
168
+
169
+ env_var = ENV["XDG_DATA_HOME"]
170
+ if env_var.nil? || !env_var.start_with?('/')
171
+ env_var = XDG_DATA_HOME_FALLBACK
172
+ end
173
+ dir = File.join env_var, "icons"
174
+ return [dir].concat XCURSORPATH
175
+ end
176
+
177
+ def self.theme_inherits(path)
178
+ ans = nil
179
+ File.open path do |io|
180
+ io.each do |line|
181
+ next unless /^Inherits\s*\=\s*(.*)/ =~ line
182
+ ans = $1.split(/[\;\,]/).map{|i| i.strip }
183
+ break
184
+ end
185
+ end
186
+ ans
187
+ end
188
+
189
+ def self.load_all_cursors(pathes, theme, size, xcursors = {})
190
+ inherits = nil
191
+ pathes.each do |path|
192
+ dir = File.expand_path theme, path
193
+ wildcard = File.join dir, "cursors", "*"
194
+ Dir.glob wildcard do |file|
195
+ if File.readable? file
196
+ begin
197
+ xcursor = XCursor.load_path(file, size)
198
+ xcursors[xcursor.name] ||= xcursor
199
+ rescue => e
200
+ end
201
+ end
202
+ end
203
+ index_theme = File.join dir, "index.theme"
204
+ if inherits.nil? && File.readable?(index_theme)
205
+ inherits = theme_inherits index_theme
206
+ end
207
+ end
208
+ if inherits
209
+ inherits.each{|itheme| load_all_cursors pathes, itheme, size, xcursors }
210
+ end
211
+ xcursors
212
+ end
213
+
214
+ def self.load(theme, size)
215
+ theme = "default" unless theme
216
+ theme = theme.sub(/\:.*/, '')
217
+ pathes = library_path
218
+ if pathes.size > 0
219
+ xcursors = load_all_cursors(pathes, theme, size)
220
+ end
221
+ if xcursors.size > 0
222
+ return self.new(theme, size, xcursors)
223
+ end
224
+ end
225
+ end # XCursorTheme
226
+ end # CursorSupport
227
+ end # Wayland