rwayland 0.2.1 → 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,21 @@
1
+ require "wayland/util/image_pool"
2
+ require "wayland/cursor_support/cursor"
3
+ require "wayland/cursor_support/xcursor"
4
+
5
+ module Wayland
6
+ module CursorSupport
7
+ module_function
8
+ def load_xcursor(wl_shm, theme, size)
9
+ ibytes = size * size * 4
10
+ ipool = Util::ImagePool.new wl_shm, ibytes
11
+ cursor_theme = CursorTheme.new theme, ipool
12
+ xt = XCursorTheme.load theme, size
13
+ xt.each do |xc|
14
+ xc.each do |xi|
15
+ cursor_theme.add_cursor_image(xc.name, xi.width, xi.height, xi.xhots, xi.yhots, xi.delay, xi.pixels)
16
+ end
17
+ end
18
+ cursor_theme
19
+ end
20
+ end
21
+ end
@@ -49,7 +49,6 @@ module Wayland
49
49
  oid ||= new_id
50
50
  obj = new_object(ifname, oid, mod)
51
51
  add_object obj
52
- call_post_init obj, mod
53
52
  obj
54
53
  end
55
54
 
@@ -61,7 +60,6 @@ module Wayland
61
60
  obj.set_socket socket
62
61
  obj.set_object_manager self
63
62
  add_object obj
64
- call_post_init obj, mod
65
63
  obj
66
64
  end
67
65
 
@@ -97,14 +95,6 @@ module Wayland
97
95
  c
98
96
  end
99
97
 
100
- def call_post_init(obj, mod)
101
- if Array === mod && (mod.size > 1)
102
- obj.post_init *mod.slice(1, mod.size - 1)
103
- else
104
- obj.post_init
105
- end
106
- end
107
-
108
98
  def new_object(ifname, oid, mod)
109
99
  obj = get_impl(ifname, mod).new(oid, @display)
110
100
  @display.create_log obj
@@ -143,6 +143,7 @@ module Wayland
143
143
  else
144
144
  display.socket.sendmsg message, 0, nil
145
145
  end
146
+ display.call_post_init obj, as if obj
146
147
  return obj
147
148
  end
148
149
 
@@ -22,6 +22,8 @@ module Wayland
22
22
  extern "int munmap(void *, size_t)"
23
23
  end
24
24
 
25
+ undef_method :initialize_dup, :initialize_clone, :initialize_copy
26
+
25
27
  class Tag < Struct.new(:io, :size, :address)
26
28
  def destroy
27
29
  F.munmap address, size if address
@@ -53,17 +55,24 @@ module Wayland
53
55
  errno = Fiddle.last_error
54
56
  raise "shm_open() failed. errno = #{errno}"
55
57
  end
58
+ truncate fd, size
59
+ fd
60
+ end
61
+ private :new_shm
62
+
63
+ def truncate(fd, size)
56
64
  if F.ftruncate(fd, size) != 0
57
- F.close fd
58
65
  errno = Fiddle.last_error
59
66
  raise "ftruncate() failed. errno = #{errno}"
60
67
  end
61
- fd
62
68
  end
63
- private :new_shm
69
+ private :truncate
64
70
 
65
71
  def initialize(fd = nil, size: nil, mode: nil,
66
72
  prot: PROT_RDWR, flags: MAP_SHARED)
73
+ @allocated = 0
74
+ @prot = prot
75
+ @flags = flags
67
76
  io = nil
68
77
  if size.nil? || size <= 0
69
78
  raise ArgumentError, "size must be greater than 0"
@@ -71,7 +80,7 @@ module Wayland
71
80
  case fd
72
81
  when nil
73
82
  mode = 0600 unless mode
74
- fd = new_shm(size, mode)
83
+ fd = new_shm size, mode
75
84
  io = IO.for_fd fd
76
85
  when Integer
77
86
  raise ArgumentError, "fd must be greater than 0" if fd < 0
@@ -84,7 +93,17 @@ module Wayland
84
93
  @tag = Tag.new io, size, nil
85
94
  @@tags[self.object_id] = @tag
86
95
  ObjectSpace.define_finalizer self, SharedMemory.finalizer
87
- mmap prot, flags
96
+ mmap
97
+ end
98
+
99
+ def allocate(size, resize: false)
100
+ if size + @allocated > @tag.size
101
+ return nil unless resize
102
+ self.resize size + @allocated
103
+ end
104
+ offset = @allocated
105
+ @allocated += size
106
+ offset
88
107
  end
89
108
 
90
109
  def close
@@ -93,14 +112,16 @@ module Wayland
93
112
  @tag.io = nil
94
113
  end
95
114
  end
115
+ private :close
96
116
 
97
- def mmap(prot, flags)
98
- raise "io already closed" unless io
99
- fd = io.to_i
117
+ def mmap
100
118
  unless @tag.address
119
+ raise FrozenError, "can't change frozen memory mapping" if frozen?
120
+ raise "io already closed" unless @tag.io
121
+ fd = @tag.io.to_i
101
122
  ptr = MAP_FAILED
102
123
  20.times do
103
- ptr = F.mmap nil, size, prot, flags, fd, 0
124
+ ptr = F.mmap nil, @tag.size, @prot, @flags, fd, 0
104
125
  break unless ptr == MAP_FAILED
105
126
  errno = Fiddle.last_error
106
127
  next if errno == Errno::EAGAIN
@@ -108,10 +129,69 @@ module Wayland
108
129
  end
109
130
  raise "mmap() failed" if ptr == MAP_FAILED
110
131
  @tag.address = ptr.to_i
132
+ @pointer = ptr
111
133
  end
112
134
  end
113
135
  private :mmap
114
136
 
137
+ def munmap()
138
+ if @tag.address
139
+ raise FrozenError, "can't change frozen memory mapping" if frozen?
140
+ F.munmap @tag.address, @tag.size
141
+ @tag.address = nil
142
+ @pointer = nil
143
+ end
144
+ end
145
+ private :munmap
146
+
147
+ def resize(newsize)
148
+ raise RangeError, "new size must be greater than current size" if newsize < size
149
+ return nil if newsize == size
150
+ raise "io already closed" unless @tag.io
151
+ raise FrozenError, "can't change frozen memory mapping" if frozen?
152
+ munmap
153
+ truncate @tag.io.to_i, newsize
154
+ @tag.size = newsize
155
+ mmap
156
+ nil
157
+ end
158
+
159
+ def read(offset, bytes)
160
+ if offset < 0 || offset >= @tag.size
161
+ raise RangeError, "offset exceeds size"
162
+ end
163
+ if bytes <= 0
164
+ raise ArgumentError, "bytes should be greather than 0"
165
+ end
166
+ if offset + bytes > @tag.size
167
+ bytes = offset + bytes - @tag.size
168
+ end
169
+ @pointer[offset, bytes]
170
+ end
171
+
172
+ def write(offset, str, bytes = nil, resize: false)
173
+ bytes = str.bytesize if bytes.nil?
174
+ if offset < 0 || offset >= @tag.size
175
+ raise RangeError, "offset exceeds size"
176
+ end
177
+ if bytes <= 0
178
+ raise ArgumentError, "bytes should be greather than 0"
179
+ end
180
+ if offset + bytes > @tag.size
181
+ raise RangeError, "string exceeds size" unless resize
182
+ newsize = offset + bytes
183
+ self.resize newsize
184
+ end
185
+ @pointer[offset, bytes] = str
186
+ end
187
+
188
+ def freeze
189
+ unless frozen?
190
+ close
191
+ super
192
+ end
193
+ end
194
+
115
195
  def address
116
196
  @tag.address
117
197
  end
@@ -0,0 +1,56 @@
1
+ require "wayland/shared_memory"
2
+
3
+ module Wayland
4
+ module Util
5
+ class ImagePool
6
+ DEFAULT_POOL_SIZE = 4096
7
+ Entry = Struct.new(:offset, :attr, :buffer)
8
+ Attribute = Struct.new(:width, :height, :stride, :format)
9
+
10
+ def initialize(wl_shm, size = nil)
11
+ @size = size || DEFAULT_POOL_SIZE
12
+ @shm = SharedMemory.new size: @size
13
+ @pool = wl_shm.create_pool @shm.fd, @size
14
+ @pool.instance_variable_set :@__shared_meory, @shm
15
+ @images = []
16
+ end
17
+
18
+ def add_image(width, height, stride, format, pixels)
19
+ raise ArgumentError, "invalid size" if width <= 0 || height <= 0
20
+ asize = height * stride
21
+ offset = @shm.allocate asize, resize: true
22
+ if @size < @shm.size
23
+ @size = @shm.size
24
+ @pool.resize @size
25
+ end
26
+ @shm.write offset, pixels, asize
27
+ index = @images.size
28
+ attr = Attribute.new(width, height, stride, format).freeze
29
+ @images << Entry.new(offset, attr, nil)
30
+ index
31
+ end
32
+
33
+ def get_buffer(index, as: nil)
34
+ raise RangeError, "index out of range" if index < 0 || index >= @images.size
35
+ e = @images[index]
36
+ unless e.buffer
37
+ a = e.attr
38
+ e.buffer = @pool.create_buffer e.offset, a.width, a.height, a.stride, a.format, as: as
39
+ end
40
+ e.buffer
41
+ end
42
+
43
+ def attribute(index)
44
+ raise RangeError, "index out of range" if index < 0 || index >= @images.size
45
+ @images[index].attr
46
+ end
47
+
48
+ def pixels(index)
49
+ raise RangeError, "index out of range" if index < 0 || index >= @images.size
50
+ e = @images[index]
51
+ a = attribute(index)
52
+ @shm.read(e.offset, a.height * a.stride)
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/wayland/util.rb CHANGED
@@ -18,13 +18,12 @@ module Wayland
18
18
  # +return+ :: string, str_size
19
19
  #
20
20
  def pad_string(str, zterm)
21
+ str = str.dup
21
22
  len = str.bytesize
22
23
  pad = 4 - (len % 4)
23
24
  zterm = zterm && (str.getbyte(-1) != 0)
24
- if pad == 4 && !zterm
25
- str
26
- else
27
- str.dup.bytesplice(len, pad, ZERO4, 0, pad)
25
+ if pad != 4 || zterm
26
+ str = str.bytesplice(len, pad, ZERO4, 0, pad)
28
27
  end
29
28
  return str, len + (zterm ? 1 : 0)
30
29
  end
@@ -151,8 +151,21 @@ module Wayland
151
151
  socket = UNIXSocket.new(path)
152
152
  object_manager = ObjectManager.new
153
153
  inst = object_manager.create_wl_display socket, as
154
+ call_post_init inst, as
154
155
  inst
155
156
  end
157
+
158
+ def self.call_post_init(obj, mod)
159
+ if Array === mod && (mod.size > 1)
160
+ obj.post_init *mod.slice(1, mod.size - 1)
161
+ else
162
+ obj.post_init
163
+ end
164
+ end
165
+
166
+ def call_post_init(obj, mod)
167
+ self.class.call_post_init obj, mod
168
+ end
156
169
  end
157
170
  end
158
171
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rwayland
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - nmbits
@@ -20,12 +20,22 @@ files:
20
20
  - README.md
21
21
  - bin/scanner.rb
22
22
  - example/01_wayland-book.rb
23
- - example/02_window.rb
24
- - example/rwaylandtk.rb
25
- - example/rwaylandtk/custom_wl_interface.rb
26
- - example/rwaylandtk/toplevel_window.rb
23
+ - example/02_memopad.rb
24
+ - example/03_image_pool.rb
25
+ - example/04_cursor_theme.rb
26
+ - example/miw/point.rb
27
+ - example/miw/rectangle.rb
28
+ - example/miw/size.rb
29
+ - example/miw/wl.rb
30
+ - example/miw/wl/popup_window.rb
31
+ - example/miw/wl/private.rb
32
+ - example/miw/wl/toplevel.rb
33
+ - example/miw/wl/window.rb
27
34
  - lib/wayland.rb
28
35
  - lib/wayland/cairo_support.rb
36
+ - lib/wayland/cursor_support.rb
37
+ - lib/wayland/cursor_support/cursor.rb
38
+ - lib/wayland/cursor_support/xcursor.rb
29
39
  - lib/wayland/free_list.rb
30
40
  - lib/wayland/message_buffer.rb
31
41
  - lib/wayland/object_manager.rb
@@ -33,6 +43,7 @@ files:
33
43
  - lib/wayland/scanner.rb
34
44
  - lib/wayland/shared_memory.rb
35
45
  - lib/wayland/util.rb
46
+ - lib/wayland/util/image_pool.rb
36
47
  - lib/wayland/wayland.yaml
37
48
  - lib/wayland/wl/display.rb
38
49
  - lib/wayland/wl/registry.rb
data/example/02_window.rb DELETED
@@ -1,180 +0,0 @@
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 = server_decoration ? 0 : 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
- return unless @py
27
- seat = @display[:wl_seat]
28
- consumed = false
29
- unless server_decoration
30
- if @py < TITLE_BAR_SIZE
31
- @xdg_toplevel.move(seat, serial)
32
- consumed = true
33
- elsif @px > width - RESIZER_SIZE && @py > height - RESIZER_SIZE
34
- @xdg_toplevel.resize(seat, serial, Wayland::Xdg::Toplevel[:resize_edge].bottom_right)
35
- consumed = true
36
- end
37
- end
38
- unless consumed
39
- lx = @px
40
- ly = @py - @ty
41
- ans = content_pango_layout.xy_to_index lx * Pango::SCALE, ly * Pango::SCALE
42
- inside, index, trailing = ans
43
- index
44
- @cursor = index
45
- invalidate
46
- end
47
- end
48
-
49
- def axis(time, axis, value)
50
- if axis == 0
51
- @ty -= value * 2
52
- end
53
- invalidate
54
- end
55
-
56
- def content_pango_layout
57
- context = cairo_context
58
- if context && !@pango_layout
59
- @pango_layout = context.create_pango_layout
60
- @pango_layout.font_description = "monospace 10"
61
- end
62
- @pango_layout
63
- end
64
-
65
- def title_bar_pango_layout
66
- context = cairo_context
67
- if context && !@title_bar_pango_layout
68
- @title_bar_pango_layout = context.create_pango_layout
69
- @title_bar_pango_layout.font_description = "monospace 10"
70
- @title_bar_pango_layout.text = title
71
- end
72
- @title_bar_pango_layout
73
- end
74
-
75
- def cursor_up
76
- if @cursor > 0
77
- @pd ||= 0
78
- rn0 = @text.rindex("\n", @cursor - 1) || 0
79
- if rn0 > 0
80
- d = [@cursor - rn0, @pd].max
81
- rn1 = @text.rindex("\n", rn0 - 1) || 0
82
- if rn1 + d < rn0
83
- @pd = 0
84
- @cursor = rn1 + d
85
- else
86
- @pd = d
87
- @cursor = rn0
88
- end
89
- invalidate
90
- end
91
- end
92
- end
93
-
94
- def cursor_down
95
- @pd ||= 0
96
- rn0 = @text.rindex("\n", @cursor - 1) || 0
97
- d = [@cursor - rn0, @pd].max
98
- rn1 = @text.index("\n", @cursor)
99
- if rn1
100
- rn2 = @text.index("\n", rn1 + 1) || @text.size
101
- if rn1 + d < rn2
102
- @pd = 0
103
- @cursor = rn1 + d
104
- else
105
- @pd = d
106
- @cursor = rn2
107
- end
108
- end
109
- end
110
-
111
- def key_down(key)
112
- case key
113
- when 0x20..0x7e
114
- @text.insert @cursor, key.chr
115
- @cursor += 1
116
- when 0xff51 # LEFT
117
- @cursor -= 1 if @cursor > 0
118
- when 0xff52 # UP
119
- cursor_up
120
- when 0xff53 # RIGHT
121
- @cursor += 1 if @cursor < @text.size
122
- when 0xff54 # DOWN
123
- cursor_down
124
- else
125
- p [key.to_s(16)]
126
- end
127
- end
128
-
129
- def draw
130
- context = cairo_context
131
- context.save do
132
- # background
133
- context.set_source_color [0.8, 0.8, 0.8, 1.0]
134
- context.rectangle 0, 0, width, height
135
- context.fill
136
-
137
- layout = content_pango_layout
138
- layout.text = @text
139
-
140
- # Cursor
141
- pos = layout.index_to_pos(@cursor)
142
- context.set_source_color [0, 0.9, 0, 1]
143
- w = pos.width > 0 ? pos.width / Pango::SCALE : 10
144
- h = pos.height > 0 ? pos.height / Pango::SCALE : 10
145
- context.rectangle pos.x / Pango::SCALE, pos.y / Pango::SCALE + @ty, w, h
146
- context.fill
147
-
148
- # Text
149
- context.move_to 0, @ty
150
- context.set_source_color [0.2, 0.3, 0.2, 1]
151
- context.show_pango_layout layout
152
-
153
- unless server_decoration
154
- # title bar
155
- title_bar_layout = title_bar_pango_layout
156
- tw, th = title_bar_layout.pixel_size
157
- tx = (width - tw) / 2
158
- ty = (TITLE_BAR_SIZE - th) / 2
159
- context.set_source_color [0, 0, 0, 0.5]
160
- context.rectangle 0, 0, width, TITLE_BAR_SIZE
161
- context.fill
162
- context.move_to tx, ty
163
- context.set_source_color [0.9, 0.9, 0.9, 1]
164
- context.show_pango_layout title_bar_layout
165
-
166
- # resizer
167
- context.set_source_color [0, 0, 0, 0.5]
168
- context.rectangle width - RESIZER_SIZE, height - RESIZER_SIZE, RESIZER_SIZE, RESIZER_SIZE
169
- context.fill
170
- end
171
- end
172
- end
173
- end
174
-
175
- if __FILE__ == $0
176
- Wayland::Protocol.load_yaml File.join(File.dirname(__FILE__), "xdg-decoration-unstable-v1.yaml")
177
- RWaylandTk.init
178
- w = MyApplicationWindow.new(640, 480).show
179
- RWaylandTk.main_loop
180
- end