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
|
# SPDX-License-Identifier: MIT
#
# This file is formatted with Python Black
from templates import Request, Response, Session, MockParams
from typing import Dict
from itertools import count
import dbus
import dbus.service
import enum
import logging
logger = logging.getLogger(f"templates.{__name__}")
BUS_NAME = "org.freedesktop.portal.Desktop"
MAIN_OBJ = "/org/freedesktop/portal/desktop"
SYSTEM_BUS = False
MAIN_IFACE = "org.freedesktop.portal.RemoteDesktop"
_restore_tokens = count()
class RDSession(Session):
class State(enum.IntEnum):
CREATED = enum.auto()
SELECTED = enum.auto()
STARTED = enum.auto()
CONNECTED = enum.auto()
@property
def state(self):
try:
return self._session_state
except AttributeError:
self._session_state = RDSession.State.CREATED
return self._session_state
def advance_state(self):
if self.state != RDSession.State.CONNECTED:
self._session_state += 1
def load(mock, parameters):
logger.debug(f"loading {MAIN_IFACE} template")
params = MockParams.get(mock, MAIN_IFACE)
params.delay = 500
params.version = parameters.get("version", 2)
params.response = parameters.get("response", 0)
params.devices = parameters.get("devices", 0b111)
params.sessions: Dict[str, RDSession] = {}
mock.AddProperties(
MAIN_IFACE,
dbus.Dictionary(
{
"version": dbus.UInt32(params.version),
"AvailableDeviceTypes": dbus.UInt32(
parameters.get("device-types", params.devices)
),
}
),
)
@dbus.service.method(
MAIN_IFACE,
sender_keyword="sender",
in_signature="a{sv}",
out_signature="o",
)
def CreateSession(self, options, sender):
try:
logger.debug(f"CreateSession: {options}")
params = MockParams.get(self, MAIN_IFACE)
request = Request(bus_name=self.bus_name, sender=sender, options=options)
session = RDSession(bus_name=self.bus_name, sender=sender, options=options)
params.sessions[session.handle] = session
response = Response(params.response, {"session_handle": session.handle})
request.respond(response, delay=params.delay)
return request.handle
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
sender_keyword="sender",
in_signature="oa{sv}",
out_signature="o",
)
def SelectDevices(self, session_handle, options, sender):
try:
logger.debug(f"SelectDevices: {session_handle} {options}")
params = MockParams.get(self, MAIN_IFACE)
request = Request(bus_name=self.bus_name, sender=sender, options=options)
try:
session = params.sessions[session_handle]
if session.state != RDSession.State.CREATED:
raise dbus.exceptions.DBusException(
f"Session in state {session.state}, expected CREATED",
name="org.freedesktop.DBus.Error.AccessDenied",
)
else:
resp = params.response
if resp == 0:
session.advance_state()
except KeyError:
raise dbus.exceptions.DBusException(
"Invalid session", name="org.freedesktop.DBus.Error.AccessDenied"
)
response = Response(resp, {})
request.respond(response, delay=params.delay)
return request.handle
except Exception as e:
logger.critical(e)
if isinstance(e, dbus.exceptions.DBusException):
raise e
@dbus.service.method(
MAIN_IFACE,
sender_keyword="sender",
in_signature="osa{sv}",
out_signature="o",
)
def Start(self, session_handle, parent_window, options, sender):
try:
logger.debug(f"Start: {session_handle} {options}")
params = MockParams.get(self, MAIN_IFACE)
request = Request(bus_name=self.bus_name, sender=sender, options=options)
results = {
"devices": dbus.UInt32(params.devices),
}
try:
session = params.sessions[session_handle]
if session.state != RDSession.State.SELECTED:
raise dbus.exceptions.DBusException(
f"Session in state {session.state}, expected SELECTED",
name="org.freedesktop.DBus.Error.AccessDenied",
)
else:
resp = params.response
if resp == 0:
session.advance_state()
except KeyError:
raise dbus.exceptions.DBusException(
"Invalid session", name="org.freedesktop.DBus.Error.AccessDenied"
)
response = Response(resp, results)
request.respond(response, delay=params.delay)
return request.handle
except Exception as e:
logger.critical(e)
if isinstance(e, dbus.exceptions.DBusException):
raise e
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}dd",
out_signature="",
)
def NotifyPointerMotion(self, session_handle, options, dx, dy):
try:
logger.debug(f"NotifyPointerMotion: {session_handle} {options} {dx} {dy}")
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}udd",
out_signature="",
)
def NotifyPointerMotionAbsolute(self, session_handle, options, stream, x, y):
try:
logger.debug(
f"NotifyPointerMotionAbsolute: {session_handle} {options} {stream} {x} {y}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}iu",
out_signature="",
)
def NotifyPointerButton(self, session_handle, options, button, state):
try:
logger.debug(
f"NotifyPointerButton: {session_handle} {options} {button} {state}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}dd",
out_signature="",
)
def NotifyPointerAxis(self, session_handle, options, dx, dy):
try:
logger.debug(f"NotifyPointerAxis: {session_handle} {options} {dx} {dx}")
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}ui",
out_signature="",
)
def NotifyPointerAxisDiscrete(self, session_handle, options, axis, steps):
try:
logger.debug(
f"NotifyPointerAxisDiscrete: {session_handle} {options} {axis} {steps}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}iu",
out_signature="",
)
def NotifyKeyboardKeycode(self, session_handle, options, keycode, state):
try:
logger.debug(
f"NotifyKeyboardKeycode: {session_handle} {options} {keycode} {state}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}iu",
out_signature="",
)
def NotifyKeyboardKeysym(self, session_handle, options, keysym, state):
try:
logger.debug(
f"NotifyKeyboardKeysym: {session_handle} {options} {keysym} {state}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}uudd",
out_signature="",
)
def NotifyTouchDown(self, session_handle, options, stream, slot, x, y):
try:
logger.debug(
f"NotifyTouchDown: {session_handle} {options} {stream} {slot} {x} {y}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}uudd",
out_signature="",
)
def NotifyTouchMotion(self, session_handle, options, stream, slot, x, y):
try:
logger.debug(
f"NotifyTouchMotion: {session_handle} {options} {stream} {slot} {x} {y}"
)
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}u",
out_signature="",
)
def NotifyTouchUp(self, session_handle, options, slot):
try:
logger.debug(f"NotifyTouchMotion: {session_handle} {options} {slot}")
except Exception as e:
logger.critical(e)
@dbus.service.method(
MAIN_IFACE,
in_signature="oa{sv}",
out_signature="h",
)
def ConnectToEIS(self, session_handle, options):
try:
logger.debug(f"ConnectToEIS: {session_handle} {options}")
params = MockParams.get(self, MAIN_IFACE)
try:
session = params.sessions[session_handle]
if session.state != RDSession.State.STARTED:
logger.error(f"Session in state {session.state}, expected STARTED")
raise dbus.exceptions.DBusException(
"Session must be started before ConnectToEIS",
name="org.freedesktop.DBus.Error.AccesDenied",
)
except KeyError:
raise dbus.exceptions.DBusException(
"Invalid session", name="org.freedesktop.DBus.Error.AccessDenied"
)
import socket
sockets = socket.socketpair()
# Write some random data down so it'll break anything that actually
# expects the socket to be a real EIS socket, plus it makes it
# easy to check if we connected to the right EIS socket in our tests.
sockets[0].send(b"VANILLA")
fd = sockets[1]
logger.debug(f"ConnectToEIS with fd {fd.fileno()}")
return dbus.types.UnixFd(fd)
except Exception as e:
logger.critical(e)
if isinstance(e, dbus.exceptions.DBusException):
raise e
|