-
Notifications
You must be signed in to change notification settings - Fork 1
/
wayland.py
454 lines (333 loc) · 15.4 KB
/
wayland.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
from __future__ import annotations
import os
import ctypes
import socket
import logging as _logging
import itertools as _itertools
from array import array as _array
from types import FunctionType
# from inspect import signature, Parameter
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
__version__ = 0.3
logger = _logging.getLogger('wayland')
logger.addHandler(_logging.StreamHandler())
##################################
# Data types and structures
##################################
class Array(ctypes.Structure):
_pack_ = True
_fields_ = [('length', ctypes.c_uint32), # size 4
('value', ctypes.c_char * 28)] # size 28
def __init__(self, text: str):
assert len(text) <= 28
super().__init__(len(text), text.encode())
def __repr__(self):
return f"{self.__class__.__name__}(len={self.length}, text='{self.value.decode()}')"
class String(ctypes.Structure):
_fields_ = [('length', ctypes.c_uint32), # size 4
('value', ctypes.c_char * 27), # size 27
('null', ctypes.c_byte)] # size 1
def __init__(self, text: str):
assert len(text) <= 27
super().__init__(len(text) + 1, text.encode())
def __repr__(self):
return f"{self.__class__.__name__}(len={self.length}, text='{self.value.decode()}')"
class Fixed(ctypes.Structure):
_fields_ = [('_value', ctypes.c_uint32)] # size 32
def __init__(self, value):
v = (int(value) << 8) + int((value % 1.0) * 256)
super().__init__(v)
def __int__(self):
return int((self._value >> 8) + (self._value & 0xff) / 256.0)
def __float__(self):
return (self._value >> 8) + (self._value & 0xff) / 256.0
def __repr__(self):
return f"{self.__class__.__name__}({float(self)})"
class Header(ctypes.Structure):
_pack_ = True
_fields_ = [('id', ctypes.c_uint32), # size 4
('opcode', ctypes.c_uint16), # size 2
('size', ctypes.c_uint16)] # size 2
def __add__(self, other: bytes) -> bytes:
return bytes(self) + other # type: ignore
def __repr__(self) -> str:
return f"Header(id={self.id}, opcode={self.opcode}, size={self.size})"
_argument_types = {
'int': ctypes.c_int32,
'uint': ctypes.c_uint32,
'fixed': Fixed,
'string': String,
'object': ctypes.c_uint32,
'new_id': ctypes.c_uint32,
'array': Array,
'fd': ctypes.c_int32
}
# _python_types = {
# 'int': int,
# 'uint': int,
# 'fixed': float,
# 'string': str,
# 'object': int,
# 'new_id': int,
# 'array': str,
# 'fd': int
# }
class _ObjectSpace:
pass
##################################
# Wayland abstractions
##################################
class Argument:
def __init__(self, parent, element):
self._parent = parent
self._element = element
self.name = element.get('name')
self.type_name = element.get('type')
self.type = _argument_types[self.type_name]
# self.python_type = _python_types[self.type_name]
self.summary = element.get('summary')
def __call__(self, value) -> bytes:
# TODO: This tries to convert the argument value (a class) into bytes:
print(self.name, value, self.type)
return bytes(self.type(value))
def __repr__(self) -> str:
return f"{self.name}({self.type_name}={self.type.__name__})"
class Enum:
def __init__(self, interface, element):
self._interface = interface
self._element = element
self.name = element.get('name')
self.description = getattr(element.find('description'), 'text', "")
self.summary = element.find('description').get('summary') if self.description else ""
self.bitfield = element.get('bitfield', 'false')
self._entries = {}
self._summaries = {}
for entry in element.findall('entry'):
name = entry.get('name')
value = int(entry.get('value'), base=0)
summary = entry.get('summary')
self._entries[value] = name
self._summaries[name] = summary
# TODO: item access, including bitfield
def __repr__(self):
return f"{self.__class__.__name__}('{self.name}')"
class Event:
def __init__(self, interface, element, opcode):
self._interface = interface
self._element = element
self.opcode = opcode
self.name = element.get('name')
self.description = getattr(element.find('description'), 'text', "")
self.summary = element.find('description').get('summary') if self.description else ""
self.arguments = [Argument(self, element) for element in element.findall('arg')]
def __repr__(self):
return f"{self.name}(opcode={self.opcode}, args=[{', '.join((f'{a}' for a in self.arguments))}])"
class _RequestBase:
"""Request base class"""
arguments: list
def __init__(self, interface, element, opcode):
self._interface = interface
self._client = interface.protocol.client
self.opcode = opcode
self.name = element.get('name')
self.description = getattr(element.find('description'), 'text', "")
self.summary = element.find('description').get('summary') if self.description else ""
def _send(self, bytestring, *fds):
# Headers are 8 bytes
size = ctypes.sizeof(Header) + len(bytestring)
header = Header(id=self._interface.id, opcode=self.opcode, size=size)
request = header + bytestring
print(f"{self.name} sent message: {request}, {header}")
self._client.send_request(self, request, *fds)
def __repr__(self):
return f"{self.name}(opcode={self.opcode}, args=[{', '.join((f'{a}' for a in self.arguments))}])"
# class Request:
#
# def __init__(self, interface, element, opcode, arguments):
# self._client = interface.protocol.client
# self.opcode = opcode
# self.arguments = arguments
#
# self.name = element.get('name')
# self.description = getattr(element.find('description'), 'text', "")
# self.summary = element.find('description').get('summary') if self.description else ""
#
# def _send(self, bytestring):
# # TODO: Complete this method
#
# # Headers are 8 bytes
# size = 8 + len(bytestring)
# # header = Header(id=???, opcode=self.opcode, size=size)
# #
# # request = header +
# #
# # self._client.send_request(self, request, *fds)
#
# # def __call__(self, *args) -> bytes:
# # return b''.join(self.arguments[i](value) for i, value in enumerate(args))
#
# def __repr__(self):
# return f"{self.name}(opcode={self.opcode}, args=[{', '.join((f'{a}' for a in self.arguments))}])"
class _InterfaceBase:
"""Interface base class"""
_element: Element
protocol: Protocol
opcode: int
def __init__(self, oid: int):
self.id = oid
self.version = int(self._element.get('version'), 0)
self.description = getattr(self._element.find('description'), 'text', "")
self.summary = self._element.find('description').get('summary') if self.description else ""
# TODO: do enums have opcodes?
self._enums = [Enum(self, element) for element in self._element.findall('enum')]
self._events = [Event(self, element, opc) for opc, element in enumerate(self._element.findall('event'))]
for request in self._create_requests():
setattr(self, request.name, request)
def _create_requests(self):
"""Dynamically create `request` methods
This method parses the xml element for `request` definitions,
and dynamically creates callable Reqest classes from them. These
Request instances are then assigned by name to the Interface,
allowing them to be called like a normal Python methods.
"""
for i, element in enumerate(self._element.findall('request')):
request_name = element.get('name')
# Arguments are callable objects that type cast and return bytes:
arguments = [Argument(self, arg) for arg in element.findall('arg')]
# Create a dynamic __call__ method with correct signature:
signature = "self, " + ", ".join(arg.name for arg in arguments)
call_string = " + ".join(f"arguments[{i}]({arg.name})" for i, arg in enumerate(arguments))
source = f"def {request_name}({signature}):\n self._send({call_string})"
# Final source code should look something like:
#
# def request_name(self, argument1, argument2):
# self._send(arguments[0](argument1) + arguments[1](argument2))
# print(source, '\n')
compiled_code = compile(source=source, filename="<string>", mode="exec")
method = FunctionType(compiled_code.co_consts[0], locals(), request_name)
# Create a dynamic Request class which includes the custom __call__ method:
request_class = type(request_name, (_RequestBase,), {'__call__': method, 'arguments': arguments})
yield request_class(interface=self, element=element, opcode=i)
# def _call(self, *args) -> bytes:
# return b''.join(self.arguments[_i](value) for _i, value in enumerate(args))
#
# # request = Request(interface=self, element=element, opcode=i, arguments=arguments)
# request_class = type(request_name, (Request,), {'__call__': _call})
#
# sig = signature(_call)
# parameters = [Parameter(name=arg.name, kind=Parameter.VAR_POSITIONAL) for arg in arguments]
# _call.__signature__ = sig.replace(parameters=parameters)
#
# yield request_class(interface=self, element=element, opcode=i, arguments=arguments)
def __repr__(self):
return f"{self.__class__.__name__}(opcode={self.opcode}, id={self.id})"
class Protocol:
def __init__(self, client: Client, filename: str):
"""A representaion of a Wayland Protocol
Given a Wayland Protocol .xml file, all Interfaces classes will
be dynamically generated at runtime.
"""
self.client = client
self._root = ElementTree.parse(filename).getroot()
self.name = self._root.get('name')
self.copyright = getattr(self._root.find('copyright'), 'text', "")
self._interface_classes = {}
# Iterate over all defined interfaces, and dynamically create
# custom Interface classes using the _InterfaceBase class.
# Opcodes are determined by enumeration order.
for i, element in enumerate(self._root.findall('interface')):
name = element.get('name')
interface_class = type(name, (_InterfaceBase,), {'protocol': self, '_element': element, 'opcode': i})
self._interface_classes[name] = interface_class
def create_interface(self, name, oid):
if name not in self._interface_classes:
raise NameError(f"This Protocol does not define an interface named '{name}'.\n"
f"Valid interface names are : {list(self._interface_classes)}")
return self._interface_classes[name](oid=oid)
@property
def interface_names(self):
return list(self._interface_classes)
def __repr__(self):
return f"{self.__class__.__name__}('{self.name}')"
class Client:
"""Wayland Client
The Client class establishes a connection to the Wayland domain socket.
As per the Wayland specification, the `WAYLAND_DISPLAY` environmental
variable is queried for the endpoint name. If this is an absolute path,
it is used as-is. If not, the final socket path will be made by joining
the `XDG_RUNTIME_DIR` + `WAYLAND_DISPLAY` environmental variables.
To create an instance of this class, at least one Wayland Protocol file
must be provided. Protocol files are XML, and are generally found under
the `/usr/share/wayland/` directory. At a minimum, the base Wayland
protocol file (`wayland.xml`) is required.
When instantiated, the Client automatically creates the main Display
(`wl_display`) interface, which is available as `Client.wl_display`.
"""
def __init__(self, *protocols: str):
"""Create a Wayland Client connection.
:param protocols: one or more protocol xml file paths.
"""
assert protocols, ("At a minimum you must provide at least a wayland.xml "
"protocol file, commonly '/usr/share/wayland/wayland.xml'.")
endpoint = os.environ.get('WAYLAND_DISPLAY', default='wayland-0')
if os.path.isabs(endpoint):
path = endpoint
else:
_runtime_dir = os.environ.get('XDG_RUNTIME_DIR', default='/run/user/1000')
path = os.path.join(_runtime_dir, endpoint)
if not os.path.exists(path):
raise FileNotFoundError(f"Wayland endpoint not found: {path}")
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
self._sock.setblocking(False)
self._sock.connect(path)
self._protocols = dict()
self.protocols = _ObjectSpace()
for filename in protocols:
if not os.path.exists(filename):
raise FileNotFoundError(f"Protocol file was not found: {filename}")
protocol = Protocol(client=self, filename=filename)
self._protocols[protocol.name] = protocol
# Temporary addition for easy access in the REPL:
setattr(self.protocols, protocol.name, protocol)
assert 'wayland' in self._protocols, "You must provide at minimum a wayland.xml protocol file."
# Client side object ID generator:
self._oid_generator = _itertools.cycle(range(1, 0xfeffffff))
# A mapping of oids to interfaces:
self._objects = {}
# Create a global wl_display object:
self.wl_display = self.create_interface(protocol='wayland', interface='wl_display')
def _get_next_object_id(self) -> int:
"""Get the next available object ID
"""
oid = next(self._oid_generator)
while oid in self._objects:
oid = next(self._oid_generator)
return oid
def create_interface(self, protocol: str, interface: str):
protocol_class = self._protocols[protocol]
object_id = self._get_next_object_id()
interface_instance = protocol_class.create_interface(name=interface, oid=object_id)
self._objects[object_id] = interface_instance
return interface_instance
def send_request(self, request, *fds):
self._sock.sendmsg([request], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, _array("i", fds))])
def fileno(self):
"""The fileno of the socket object
This method exists to allow the class
to be "selectable" (see the `select` module).
"""
return self._sock.fileno()
def select(self):
# TODO: receive events from the server
# (data, ancdata, msg_flags, address)
data, ancdata, msg_flags, _ = self._sock.recvmsg(1024, socket.CMSG_SPACE(64))
print("data, ancdata, msg_flags: ", data, ancdata, msg_flags)
def __del__(self):
if hasattr(self, '_sock'):
self._sock.close()
def __repr__(self):
return f"{self.__class__.__name__}(socket='{self._sock.getpeername()}')"
# TODO: remove testing code:
if __name__ == '__main__':
client = Client('/usr/share/wayland/wayland.xml')