| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
# mypy: disable-error-code="misc" |
| 5 |
|
| 6 |
from tests.templates.xdp_utils import Response, ImplSession, init_logger |
| 7 |
|
| 8 |
from collections import namedtuple |
| 9 |
from itertools import count |
| 10 |
from gi.repository import GLib |
| 11 |
from dataclasses import dataclass |
| 12 |
import dbus |
| 13 |
import dbus.service |
| 14 |
import socket |
| 15 |
|
| 16 |
from tests.xdp_utils import SessionPersistenceMode |
| 17 |
|
| 18 |
|
| 19 |
BUS_NAME = "org.freedesktop.impl.portal.Test" |
| 20 |
MAIN_OBJ = "/org/freedesktop/portal/desktop" |
| 21 |
SYSTEM_BUS = False |
| 22 |
MAIN_IFACE = "org.freedesktop.impl.portal.InputCapture" |
| 23 |
VERSION = 2 |
| 24 |
|
| 25 |
|
| 26 |
logger = init_logger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
serials = count() |
| 30 |
|
| 31 |
|
| 32 |
Barrier = namedtuple("Barrier", ["id", "position"]) |
| 33 |
|
| 34 |
|
| 35 |
def make_restore_data(data: str): |
| 36 |
RESTORE_DATA_VERSION = dbus.UInt32(7) # Random-generator generated number, trust me |
| 37 |
return dbus.Struct( |
| 38 |
("GNOME", RESTORE_DATA_VERSION, dbus.String(data, variant_level=1)), |
| 39 |
signature="suv", |
| 40 |
) |
| 41 |
|
| 42 |
|
| 43 |
@dataclass |
| 44 |
class InputcaptureParameters: |
| 45 |
delay: int |
| 46 |
supported_capabilities: int |
| 47 |
capabilities: int |
| 48 |
default_zone: list |
| 49 |
disable_delay: int |
| 50 |
activated_delay: int |
| 51 |
deactivated_delay: int |
| 52 |
zones_changed_delay: int |
| 53 |
force_clipboard_enabled: bool |
| 54 |
allow_persistence: bool |
| 55 |
|
| 56 |
|
| 57 |
def load(mock, parameters={}): |
| 58 |
logger.debug(f"Loading parameters: {parameters}") |
| 59 |
|
| 60 |
assert not hasattr(mock, "inputcapture_params") |
| 61 |
mock.inputcapture_params = InputcaptureParameters( |
| 62 |
delay=parameters.get("delay", 0), |
| 63 |
supported_capabilities=parameters.get("supported_capabilities", 0xF), |
| 64 |
capabilities=parameters.get("capabilities", None), |
| 65 |
default_zone=parameters.get("default-zone", [(1920, 1080, 0, 0)]), |
| 66 |
disable_delay=parameters.get("disable-delay", 0), |
| 67 |
activated_delay=parameters.get("activated-delay", 0), |
| 68 |
deactivated_delay=parameters.get("deactivated-delay", 0), |
| 69 |
zones_changed_delay=parameters.get("zones-changed-delay", 0), |
| 70 |
force_clipboard_enabled=parameters.get("force-clipboard-enabled", False), |
| 71 |
allow_persistence=parameters.get("allow-persistence", True), |
| 72 |
) |
| 73 |
|
| 74 |
mock.current_zones = mock.inputcapture_params.default_zone |
| 75 |
mock.current_zone_set = next(serials) |
| 76 |
|
| 77 |
mock.AddProperties( |
| 78 |
MAIN_IFACE, |
| 79 |
dbus.Dictionary( |
| 80 |
{ |
| 81 |
"version": dbus.UInt32(parameters.get("version", VERSION)), |
| 82 |
"SupportedCapabilities": dbus.UInt32( |
| 83 |
mock.inputcapture_params.supported_capabilities |
| 84 |
), |
| 85 |
} |
| 86 |
), |
| 87 |
) |
| 88 |
|
| 89 |
mock.session_handles = [] |
| 90 |
mock.active_session_handles = [] |
| 91 |
mock.restore_datas = [] |
| 92 |
mock.sessions = {} |
| 93 |
|
| 94 |
|
| 95 |
@dbus.service.method( |
| 96 |
MAIN_IFACE, |
| 97 |
in_signature="osa{sv}", |
| 98 |
out_signature="a{sv}", |
| 99 |
) |
| 100 |
def CreateSession2(self, session_handle, app_id, options): |
| 101 |
try: |
| 102 |
logger.debug(f"CreateSession2({session_handle}, {app_id}, {options})") |
| 103 |
|
| 104 |
assert len(options) == 0 |
| 105 |
|
| 106 |
session = ImplSession(self, BUS_NAME, session_handle, app_id).export() |
| 107 |
self.sessions[session_handle] = session |
| 108 |
self.session_handles.append(session_handle) |
| 109 |
|
| 110 |
return {"session_handle": session_handle} |
| 111 |
except Exception as e: |
| 112 |
logger.critical(e) |
| 113 |
return (2, {}) |
| 114 |
|
| 115 |
|
| 116 |
@dbus.service.method( |
| 117 |
MAIN_IFACE, |
| 118 |
in_signature="oossa{sv}", |
| 119 |
out_signature="ua{sv}", |
| 120 |
) |
| 121 |
def Start(self, handle, session_handle, app_id, parent_window, options): |
| 122 |
try: |
| 123 |
logger.debug(f"Start({session_handle}, {app_id}, {parent_window}, {options})") |
| 124 |
params = self.inputcapture_params |
| 125 |
|
| 126 |
assert session_handle in self.session_handles |
| 127 |
assert "capabilities" in options |
| 128 |
|
| 129 |
# Filter to the subset of supported capabilities |
| 130 |
if params.capabilities is None: |
| 131 |
capabilities = options["capabilities"] |
| 132 |
else: |
| 133 |
capabilities = params.capabilities |
| 134 |
|
| 135 |
capabilities &= params.supported_capabilities |
| 136 |
response = Response(0, {}) |
| 137 |
|
| 138 |
persist_mode = options.get("persist_mode") |
| 139 |
restore_data = options.get("restore_data") |
| 140 |
|
| 141 |
if restore_data: |
| 142 |
try: |
| 143 |
restore_data = next( |
| 144 |
rd for rd in self.restore_datas if rd == restore_data |
| 145 |
) |
| 146 |
except StopIteration: |
| 147 |
raise Exception(f"Invalid restore_data tuple {restore_data}") |
| 148 |
|
| 149 |
if persist_mode == SessionPersistenceMode.NONE: |
| 150 |
pass |
| 151 |
elif persist_mode == SessionPersistenceMode.TRANSIENT: |
| 152 |
if restore_data is None: |
| 153 |
restore_data = make_restore_data("restore-transient") |
| 154 |
elif persist_mode == SessionPersistenceMode.PERSISTENT: |
| 155 |
if restore_data is None: |
| 156 |
restore_data = make_restore_data("restore-persistent") |
| 157 |
elif persist_mode is not None: |
| 158 |
raise Exception(f"Invalid session persistence mode: {persist_mode}") |
| 159 |
|
| 160 |
if restore_data and persist_mode in [ |
| 161 |
SessionPersistenceMode.TRANSIENT, |
| 162 |
SessionPersistenceMode.PERSISTENT, |
| 163 |
]: |
| 164 |
# In this test interface we allow for multiple restore data |
| 165 |
self.restore_datas.append(restore_data) |
| 166 |
response.results["restore_data"] = restore_data |
| 167 |
|
| 168 |
if params.force_clipboard_enabled: |
| 169 |
response.results["clipboard_enabled"] = True |
| 170 |
|
| 171 |
response.results["capabilities"] = dbus.UInt32(capabilities) |
| 172 |
self.active_session_handles.append(session_handle) |
| 173 |
|
| 174 |
logger.debug(f"Start with response {response}") |
| 175 |
|
| 176 |
return response.response, response.results |
| 177 |
except Exception as e: |
| 178 |
logger.critical(e) |
| 179 |
return (2, {}) |
| 180 |
|
| 181 |
|
| 182 |
@dbus.service.method( |
| 183 |
MAIN_IFACE, |
| 184 |
in_signature="oosa{sv}", |
| 185 |
out_signature="ua{sv}", |
| 186 |
) |
| 187 |
def GetZones(self, handle, session_handle, app_id, options): |
| 188 |
try: |
| 189 |
logger.debug(f"GetZones({session_handle}, {options})") |
| 190 |
params = self.inputcapture_params |
| 191 |
|
| 192 |
assert session_handle in self.active_session_handles |
| 193 |
|
| 194 |
response = Response(0, {}) |
| 195 |
response.results["zones"] = params.default_zone |
| 196 |
response.results["zone_set"] = dbus.UInt32( |
| 197 |
self.current_zone_set, variant_level=1 |
| 198 |
) |
| 199 |
logger.debug(f"GetZones with response {response}") |
| 200 |
|
| 201 |
if response.response == 0: |
| 202 |
self.current_zones = response.results["zones"] |
| 203 |
|
| 204 |
return response.response, response.results |
| 205 |
except Exception as e: |
| 206 |
logger.critical(e) |
| 207 |
return (2, {}) |
| 208 |
|
| 209 |
|
| 210 |
@dbus.service.method( |
| 211 |
MAIN_IFACE, |
| 212 |
in_signature="oosa{sv}aa{sv}u", |
| 213 |
out_signature="ua{sv}", |
| 214 |
) |
| 215 |
def SetPointerBarriers( |
| 216 |
self, handle, session_handle, app_id, options, barriers, zone_set |
| 217 |
): |
| 218 |
try: |
| 219 |
logger.debug( |
| 220 |
f"SetPointerBarriers({session_handle}, {options}, {barriers}, {zone_set})" |
| 221 |
) |
| 222 |
|
| 223 |
assert session_handle in self.active_session_handles |
| 224 |
assert zone_set == self.current_zone_set |
| 225 |
|
| 226 |
self.current_barriers = [] |
| 227 |
|
| 228 |
failed_barriers = [] |
| 229 |
|
| 230 |
# Barrier sanity checks: |
| 231 |
for b in barriers: |
| 232 |
id = b["barrier_id"] |
| 233 |
x1, y1, x2, y2 = b["position"] |
| 234 |
if (x1 != x2 and y1 != y2) or (x1 == x2 and y1 == y2): |
| 235 |
logger.debug(f"Barrier {id} is not horizontal or vertical") |
| 236 |
failed_barriers.append(id) |
| 237 |
continue |
| 238 |
|
| 239 |
for z in self.current_zones: |
| 240 |
w, h, x, y = z |
| 241 |
if x1 < x or x1 > x + w: |
| 242 |
continue |
| 243 |
if y1 < y or y1 > y + h: |
| 244 |
continue |
| 245 |
|
| 246 |
# x1/y1 fit into our current zone |
| 247 |
if x2 < x or x2 > x + w or y2 < y or y2 > y + h: |
| 248 |
logger.debug(f"Barrier {id} spans multiple zones") |
| 249 |
elif x1 == x2 and (x1 != x and x1 != x + w): |
| 250 |
logger.debug(f"Barrier {id} is not on vertical edge") |
| 251 |
elif y1 == y2 and (y1 != y and y1 != y + h): |
| 252 |
logger.debug(f"Barrier {id} is not on horizontal edge") |
| 253 |
else: |
| 254 |
self.current_barriers.append(Barrier(id=id, position=b["position"])) |
| 255 |
break |
| 256 |
|
| 257 |
failed_barriers.append(id) |
| 258 |
break |
| 259 |
else: |
| 260 |
logger.debug(f"Barrier {id} does not fit into any zone") |
| 261 |
failed_barriers.append(id) |
| 262 |
continue |
| 263 |
|
| 264 |
response = Response(0, {}) |
| 265 |
response.results["failed_barriers"] = dbus.Array( |
| 266 |
[dbus.UInt32(f) for f in failed_barriers], |
| 267 |
signature="u", |
| 268 |
variant_level=1, |
| 269 |
) |
| 270 |
|
| 271 |
logger.debug(f"SetPointerBarriers with response {response}") |
| 272 |
|
| 273 |
return response.response, response.results |
| 274 |
except Exception as e: |
| 275 |
logger.critical(e) |
| 276 |
return (2, {}) |
| 277 |
|
| 278 |
|
| 279 |
@dbus.service.method( |
| 280 |
MAIN_IFACE, |
| 281 |
in_signature="osa{sv}", |
| 282 |
out_signature="ua{sv}", |
| 283 |
) |
| 284 |
def Enable(self, session_handle, app_id, options): |
| 285 |
try: |
| 286 |
logger.debug(f"Enable({session_handle}, {options})") |
| 287 |
params = self.inputcapture_params |
| 288 |
|
| 289 |
assert session_handle in self.active_session_handles |
| 290 |
|
| 291 |
# for use in the signals |
| 292 |
activation_id = next(serials) |
| 293 |
barrier = self.current_barriers[0] |
| 294 |
pos = (barrier.position[0] + 10, barrier.position[1] + 20) |
| 295 |
|
| 296 |
if params.disable_delay > 0: |
| 297 |
|
| 298 |
def disable(): |
| 299 |
logger.debug("emitting Disabled") |
| 300 |
self.EmitSignal(MAIN_IFACE, "Disabled", "oa{sv}", [session_handle, {}]) |
| 301 |
|
| 302 |
GLib.timeout_add(params.disable_delay, disable) |
| 303 |
|
| 304 |
if params.activated_delay > 0: |
| 305 |
|
| 306 |
def activated(): |
| 307 |
logger.debug("emitting Activated") |
| 308 |
options = { |
| 309 |
"activation_id": dbus.UInt32(activation_id, variant_level=1), |
| 310 |
"barrier_id": dbus.UInt32(barrier.id, variant_level=1), |
| 311 |
"cursor_position": dbus.Struct( |
| 312 |
pos, signature="dd", variant_level=1 |
| 313 |
), |
| 314 |
} |
| 315 |
self.EmitSignal( |
| 316 |
MAIN_IFACE, "Activated", "oa{sv}", [session_handle, options] |
| 317 |
) |
| 318 |
|
| 319 |
GLib.timeout_add(params.activated_delay, activated) |
| 320 |
|
| 321 |
if params.deactivated_delay > 0: |
| 322 |
|
| 323 |
def deactivated(): |
| 324 |
logger.debug("emitting Deactivated") |
| 325 |
options = { |
| 326 |
"activation_id": dbus.UInt32(activation_id, variant_level=1), |
| 327 |
"cursor_position": dbus.Struct( |
| 328 |
pos, signature="dd", variant_level=1 |
| 329 |
), |
| 330 |
} |
| 331 |
self.EmitSignal( |
| 332 |
MAIN_IFACE, "Deactivated", "oa{sv}", [session_handle, options] |
| 333 |
) |
| 334 |
|
| 335 |
GLib.timeout_add(params.deactivated_delay, deactivated) |
| 336 |
|
| 337 |
if params.zones_changed_delay > 0: |
| 338 |
|
| 339 |
def zones_changed(): |
| 340 |
logger.debug("emitting ZonesChanged") |
| 341 |
options = { |
| 342 |
"zone_set": dbus.UInt32(activation_id, variant_level=1), |
| 343 |
} |
| 344 |
self.EmitSignal( |
| 345 |
MAIN_IFACE, "ZonesChanged", "oa{sv}", [session_handle, options] |
| 346 |
) |
| 347 |
|
| 348 |
GLib.timeout_add(params.zones_changed_delay, zones_changed) |
| 349 |
|
| 350 |
except Exception as e: |
| 351 |
logger.critical(e) |
| 352 |
return (2, {}) |
| 353 |
|
| 354 |
|
| 355 |
@dbus.service.method( |
| 356 |
MAIN_IFACE, |
| 357 |
in_signature="osa{sv}", |
| 358 |
out_signature="ua{sv}", |
| 359 |
) |
| 360 |
def Disable(self, session_handle, app_id, options): |
| 361 |
try: |
| 362 |
logger.debug(f"Disable({session_handle}, {options})") |
| 363 |
|
| 364 |
assert session_handle in self.active_session_handles |
| 365 |
except Exception as e: |
| 366 |
logger.critical(e) |
| 367 |
return (2, {}) |
| 368 |
|
| 369 |
|
| 370 |
@dbus.service.method( |
| 371 |
MAIN_IFACE, |
| 372 |
in_signature="osa{sv}", |
| 373 |
out_signature="ua{sv}", |
| 374 |
) |
| 375 |
def Release(self, session_handle, app_id, options): |
| 376 |
try: |
| 377 |
logger.debug(f"Release({session_handle}, {options})") |
| 378 |
|
| 379 |
assert session_handle in self.active_session_handles |
| 380 |
except Exception as e: |
| 381 |
logger.critical(e) |
| 382 |
return (2, {}) |
| 383 |
|
| 384 |
|
| 385 |
@dbus.service.method( |
| 386 |
MAIN_IFACE, |
| 387 |
in_signature="osa{sv}", |
| 388 |
out_signature="h", |
| 389 |
) |
| 390 |
def ConnectToEIS(self, session_handle, app_id, options): |
| 391 |
try: |
| 392 |
logger.debug(f"ConnectToEIS({session_handle}, {options})") |
| 393 |
|
| 394 |
assert session_handle in self.active_session_handles |
| 395 |
|
| 396 |
sockets = socket.socketpair() |
| 397 |
self.eis_socket = sockets[0] |
| 398 |
|
| 399 |
assert self.eis_socket.send(b"HELLO") == 5 |
| 400 |
|
| 401 |
fd = sockets[1] |
| 402 |
|
| 403 |
logger.debug(f"ConnectToEis with fd {fd.fileno()}") |
| 404 |
|
| 405 |
return dbus.types.UnixFd(fd) |
| 406 |
except Exception as e: |
| 407 |
logger.critical(e) |
| 408 |
return -1 |