1import base64
2import struct
3import zlib
4
5MAX_PIXELS = 3840 * 2160
6
7
8def png(width, height, pixels, *, bgra=False):
9 def chunk(kind, data):
10 return struct.pack("!I", len(data)) + kind + data + struct.pack("!I", zlib.crc32(kind + data))
11 compressor = zlib.compressobj()
12 compressed = bytearray()
13 for row in range(height):
14 pixels_row = bytearray(pixels[row * width * 4:(row + 1) * width * 4])
15 if bgra:
16 pixels_row[0::4], pixels_row[2::4], pixels_row[3::4] = pixels_row[2::4], pixels_row[0::4], b'\xff' * width
17 compressed.extend(compressor.compress(b'\0' + pixels_row))
18 if len(compressed) > 8 * 1024 * 1024:
19 raise ValueError("The screenshot is too large. Lower the guest display resolution, then retry.")
20 compressed.extend(compressor.flush())
21 data = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack("!IIBBBBB", width, height, 8, 6, 0, 0, 0)) + chunk(b"IDAT", compressed) + chunk(b"IEND", b"")
22 return "data:image/png;base64," + base64.b64encode(data).decode()
23
24
25
26class Desktop:
27 def __init__(self, reader, writer, notify):
28 self.reader, self.writer, self.notify = reader, writer, notify
29 self.keys = set()
30 self.pointer = (0, 0)
31 self.buttons = 0
32 self.pixels = bytearray()
33 self.cursor = None
34
35 def resize(self, width, height):
36 if not 0 < width <= 4096 or not 0 < height <= 4096 or width * height > MAX_PIXELS:
37 raise ValueError("The VM screen exceeds the supported 4K display size.")
38 self.width, self.height = width, height
39 self.pixels = bytearray(width * height * 4)
40
41 async def start(self):
42 if not (await self.reader.readexactly(12)).startswith(b"RFB 003."):
43 raise ValueError("The VM screen did not respond. Restart the VM and reconnect.")
44 self.writer.write(b"RFB 003.008\n")
45 count = (await self.reader.readexactly(1))[0]
46 security = await self.reader.readexactly(count)
47 if 1 not in security:
48 raise ValueError("Remove the VM's VNC password in its display settings, then reconnect.")
49 self.writer.write(b"\x01")
50 if await self.reader.readexactly(4) != b"\0\0\0\0":
51 raise ValueError("The VM rejected the screen connection. Restart it and reconnect.")
52 self.writer.write(b"\x01")
53 width, height, _, length = struct.unpack("!HH16sI", await self.reader.readexactly(24))
54 if length > 65536:
55 raise ValueError("The VM screen sent an unsupported desktop name.")
56 await self.reader.readexactly(length)
57 self.resize(width, height)
58 self.writer.write(struct.pack("!B3xBBBBHHHBBB3x", 0, 32, 24, 0, 1, 255, 255, 255, 16, 8, 0))
59 encodings = (0, 1, -223, -224, -239)
60 self.writer.write(struct.pack("!BBH" + "i" * len(encodings), 2, 0, len(encodings), *encodings))
61 self.request(False)
62
63 def request(self, incremental=True):
64 self.writer.write(struct.pack("!BBHHHH", 3, incremental, 0, 0, self.width, self.height))
65
66 def input(self, message):
67 kind = message.get("type")
68 if kind == "key":
69 key, down = message.get("key"), message.get("down")
70 if type(key) is not int or not 0 <= key <= 0xFFFFFFFF or type(down) is not bool:
71 raise ValueError("Unsupported key event.")
72 if down:
73 self.keys.add(key)
74 else:
75 self.keys.discard(key)
76 self.writer.write(struct.pack("!BB2xI", 4, down, key))
77 elif kind == "pointer":
78 x, y, buttons = message.get("x"), message.get("y"), message.get("buttons")
79 if any(type(value) is not int for value in (x, y, buttons)) or not 0 <= buttons <= 255:
80 raise ValueError("Unsupported pointer event.")
81 self.pointer = (max(0, min(x, self.width - 1)), max(0, min(y, self.height - 1)))
82 self.buttons = buttons
83 self.writer.write(struct.pack("!BBHH", 5, buttons, *self.pointer))
84 elif kind == "release":
85 for key in self.keys:
86 self.writer.write(struct.pack("!BB2xI", 4, 0, key))
87 self.keys.clear()
88 self.buttons = 0
89 self.writer.write(struct.pack("!BBHH", 5, 0, *self.pointer))
90 elif kind == "clipboard":
91 text = message.get("text")
92 if not isinstance(text, str) or len(text) > 65536:
93 raise ValueError("Shorten the clipboard text, then send it again.")
94 try:
95 data = text.encode("latin1")
96 except UnicodeEncodeError:
97 raise ValueError("Some clipboard characters aren't supported by this guest.") from None
98 self.writer.write(struct.pack("!B3xI", 6, len(data)) + data)
99 else:
100 raise ValueError("Unsupported input event.")
101
102 async def read(self, present, resized):
103 while True:
104 kind = (await self.reader.readexactly(1))[0]
105 if kind == 0:
106 _, count = struct.unpack("!BH", await self.reader.readexactly(3))
107 dirty = False
108 for _ in range(count):
109 x, y, width, height, encoding = struct.unpack("!HHHHi", await self.reader.readexactly(12))
110 if encoding == -224:
111 break
112 if encoding == -223:
113 self.resize(width, height)
114 resized()
115 continue
116 if encoding == -239:
117 if width * height > 256 * 256:
118 raise ValueError("The VM sent an unsupported cursor size.")
119 pixels = bytearray(await self.reader.readexactly(width * height * 4))
120 mask = await self.reader.readexactly(((width + 7) // 8) * height)
121 for row in range(height):
122 for col in range(width):
123 offset = (row * width + col) * 4
124 pixels[offset], pixels[offset + 2] = pixels[offset + 2], pixels[offset]
125 pixels[offset + 3] = 255 if mask[row * ((width + 7) // 8) + col // 8] & (128 >> (col % 8)) else 0
126 self.cursor = {"type": "cursor", "source": "qemu", "width": width, "height": height, "x": x, "y": y,
127 "frame": 0, "frames": 1, "duration": 100,
128 "image": png(width, height, pixels) if width and height else None}
129 self.notify(self.cursor)
130 continue
131 if x + width > self.width or y + height > self.height:
132 raise ValueError("The VM sent a screen update outside its display.")
133 if encoding == 0:
134 data = await self.reader.readexactly(width * height * 4)
135 elif encoding == 1:
136 source_x, source_y = struct.unpack("!HH", await self.reader.readexactly(4))
137 if source_x + width > self.width or source_y + height > self.height:
138 raise ValueError("The VM sent a screen copy outside its display.")
139 data = b"".join(self.pixels[((source_y + row) * self.width + source_x) * 4:
140 ((source_y + row) * self.width + source_x + width) * 4] for row in range(height))
141 else:
142 raise ValueError("The VM sent an unsupported screen encoding.")
143 for row in range(height):
144 start = ((y + row) * self.width + x) * 4
145 self.pixels[start:start + width * 4] = data[row * width * 4:(row + 1) * width * 4]
146 dirty = dirty or bool(width and height)
147 if dirty:
148 present()
149 self.request()
150 await self.writer.drain()
151 elif kind == 2:
152 pass
153 elif kind == 3:
154 _, length = struct.unpack("!3sI", await self.reader.readexactly(7))
155 if length > 65536:
156 raise ValueError("The VM sent too much clipboard text.")
157 self.notify({"type": "clipboard", "text": (await self.reader.readexactly(length)).decode("latin1")})
158 else:
159 raise ValueError("The VM sent an unsupported screen message.")
160