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.
@@ -0,0 +1,69 @@
1
+ require 'rexml/document'
2
+
3
+ module Wayland
4
+ class Scanner
5
+ def initialize(doc)
6
+ @doc = doc
7
+ end
8
+ attr_reader :doc
9
+
10
+ def mkargs(elements)
11
+ elements.map do |arg|
12
+ ahash = {}
13
+ [ "name", "type", "interface", "enum" ].each do |attr|
14
+ val = arg.attribute attr
15
+ ahash[attr.to_sym] = val.to_s.to_sym if val
16
+ end
17
+ val = arg.attribute "allow-null"
18
+ if val
19
+ sval = val.to_s
20
+ ahash[:allow_null] = (sval == "true")
21
+ end
22
+ ahash
23
+ end
24
+ end
25
+ private :mkargs
26
+
27
+ def scan
28
+ interfaces = {}
29
+
30
+ doc.get_elements("/protocol/interface").each do |e|
31
+ intf = { version: e.attribute("version").to_s.to_i }
32
+ requests = {}
33
+ e.get_elements("request").each_with_index do |req, i|
34
+ requests[req.attribute("name").to_s.to_sym] =
35
+ { opcode: i, args: mkargs(req.get_elements("arg")) }
36
+ end
37
+ intf[:requests] = requests
38
+
39
+ events = []
40
+ e.get_elements("event").each_with_index do |ev, i|
41
+ name = ev.attribute("name").to_s.to_sym
42
+ args = mkargs ev.get_elements("arg")
43
+ ev_spec = { opcode: i, name: name, args: args }
44
+ since = ev.attribute("since")
45
+ ev_spec[:since] = since.to_s.to_i if since
46
+ events << ev_spec
47
+ end
48
+ intf[:events] = events
49
+
50
+ enums = []
51
+ e.get_elements("enum").each do |en|
52
+ name = en.attribute("name").to_s.to_sym
53
+ entries = en.get_elements("entry")
54
+ en_spec = { name: name }
55
+ en_spec[:entries] = entries.map do |ent|
56
+ { name: ent.attribute("name").to_s.to_sym,
57
+ value: Integer(ent.attribute("value").to_s) }
58
+ end
59
+ enums << en_spec
60
+ end
61
+ intf[:enums] = enums
62
+
63
+ name = e.attribute("name").to_s.to_sym
64
+ interfaces[name] = intf
65
+ end
66
+ interfaces
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,131 @@
1
+ require 'fiddle/import'
2
+
3
+ module Wayland
4
+ class SharedMemory
5
+ PROT_READ = 0x1
6
+ PROT_WRITE = 0x2
7
+ PROT_RDWR = PROT_READ | PROT_WRITE
8
+ MAP_SHARED = 0x1
9
+ MAP_PRIVATE = 0x2
10
+ MAP_FAILED = Fiddle::Pointer.new(-1)
11
+ LONG_SIZE = [-1].pack("l!").bytesize * 8
12
+ INT_SIZE = [-1].pack("i!").bytesize * 8
13
+ PTR_SIZE = [-1].pack("j!").bytesize * 8
14
+ module F
15
+ extend Fiddle::Importer
16
+ dlload "librt.so.1", "libc.so.6"
17
+ extern "int shm_open(void *, int, int)"
18
+ extern "int shm_unlink(void *)"
19
+ extern "int ftruncate(int, long)"
20
+ extern "int close(int)"
21
+ extern "void *mmap(void *, size_t, int, int, int, long)"
22
+ extern "int munmap(void *, size_t)"
23
+ end
24
+
25
+ class Tag < Struct.new(:io, :size, :address)
26
+ def destroy
27
+ F.munmap address, size if address
28
+ io.close if io
29
+ self.io = nil
30
+ self.size = nil
31
+ self.address = nil
32
+ end
33
+ end
34
+ @@tags = {}
35
+
36
+ def self.finalizer
37
+ proc {|oid| @@tags.delete(oid)&.destroy }
38
+ end
39
+
40
+ def new_shm(size, mode)
41
+ fd = -1
42
+ oflags = File::Constants::RDWR | File::Constants::CREAT | File::Constants::EXCL
43
+ 20.times do
44
+ r = rand(0x100000000)
45
+ name = "/wayland-ruby-shm-#{$$}-#{'%08x' % r}"
46
+ fd = F.shm_open(name, oflags, mode)
47
+ if fd >= 0
48
+ F.shm_unlink(name)
49
+ break
50
+ end
51
+ end
52
+ if fd < 0
53
+ errno = Fiddle.last_error
54
+ raise "shm_open() failed. errno = #{errno}"
55
+ end
56
+ if F.ftruncate(fd, size) != 0
57
+ F.close fd
58
+ errno = Fiddle.last_error
59
+ raise "ftruncate() failed. errno = #{errno}"
60
+ end
61
+ fd
62
+ end
63
+ private :new_shm
64
+
65
+ def initialize(fd = nil, size: nil, mode: nil,
66
+ prot: PROT_RDWR, flags: MAP_SHARED)
67
+ io = nil
68
+ if size.nil? || size <= 0
69
+ raise ArgumentError, "size must be greater than 0"
70
+ end
71
+ case fd
72
+ when nil
73
+ mode = 0600 unless mode
74
+ fd = new_shm(size, mode)
75
+ io = IO.for_fd fd
76
+ when Integer
77
+ raise ArgumentError, "fd must be greater than 0" if fd < 0
78
+ io = IO.for_fd fd
79
+ when IO
80
+ io = fd
81
+ else
82
+ raise TypeError, "the first argument should be nil, an instance of Integer or IO"
83
+ end
84
+ @tag = Tag.new io, size, nil
85
+ @@tags[self.object_id] = @tag
86
+ ObjectSpace.define_finalizer self, SharedMemory.finalizer
87
+ mmap prot, flags
88
+ end
89
+
90
+ def close
91
+ if @tag.io
92
+ io.close
93
+ @tag.io = nil
94
+ end
95
+ end
96
+
97
+ def mmap(prot, flags)
98
+ raise "io already closed" unless io
99
+ fd = io.to_i
100
+ unless @tag.address
101
+ ptr = MAP_FAILED
102
+ 20.times do
103
+ ptr = F.mmap nil, size, prot, flags, fd, 0
104
+ break unless ptr == MAP_FAILED
105
+ errno = Fiddle.last_error
106
+ next if errno == Errno::EAGAIN
107
+ raise "mmap() failed. errno = #{errno}"
108
+ end
109
+ raise "mmap() failed" if ptr == MAP_FAILED
110
+ @tag.address = ptr.to_i
111
+ end
112
+ end
113
+ private :mmap
114
+
115
+ def address
116
+ @tag.address
117
+ end
118
+
119
+ def size
120
+ @tag.size
121
+ end
122
+
123
+ def io
124
+ @tag.io
125
+ end
126
+
127
+ def fd
128
+ @tag.io&.to_i
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,10 @@
1
+ module Wayland
2
+ class Util
3
+ def self.ifname_to_cname(ifname)
4
+ a = ifname.to_s.split("_").map{|i| i.capitalize }
5
+ mod = a.shift
6
+ c = a.join
7
+ return mod, c
8
+ end
9
+ end
10
+ end