| 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 import Response, 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 |
|
| 17 |
BUS_NAME = "org.freedesktop.impl.portal.Test" |
| 18 |
MAIN_OBJ = "/org/freedesktop/portal/desktop" |
| 19 |
SYSTEM_BUS = False |
| 20 |
MAIN_IFACE = "org.freedesktop.impl.portal.InputCapture" |
| 21 |
VERSION = 1 |
| 22 |
|
| 23 |
|
| 24 |
logger = init_logger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
serials = count() |
| 28 |
|
| 29 |
|
| 30 |
Barrier = namedtuple("Barrier", ["id", "position"]) |
| 31 |
|
| 32 |
|
| 33 |
@dataclass |
| 34 |
class InputcaptureParameters: |
| 35 |
delay: int |
| 36 |
supported_capabilities: int |
| 37 |
capabilities: int |
| 38 |
default_zone: list |
| 39 |
disable_delay: int |
| 40 |
activated_delay: int |
| 41 |
deactivated_delay: int |
| 42 |
zones_changed_delay: int |
| 43 |
|
| 44 |
|
| 45 |
def load(mock, parameters={}): |
| 46 |
logger.debug(f"Loading parameters: {parameters}") |
| 47 |
|
| 48 |
assert not hasattr(mock, "inputcapture_params") |
| 49 |
mock.inputcapture_params = InputcaptureParameters( |
| 50 |
delay=parameters.get("delay", 0), |
| 51 |
supported_capabilities=parameters.get("supported_capabilities", 0xF), |
| 52 |
capabilities=parameters.get("capabilities", None), |
| 53 |
default_zone=parameters.get("default-zone", [(1920, 1080, 0, 0)]), |
| 54 |
disable_delay=parameters.get("disable-delay", 0), |
| 55 |
activated_delay=parameters.get("activated-delay", 0), |
| 56 |
deactivated_delay=parameters.get("deactivated-delay", 0), |
| 57 |
zones_changed_delay=parameters.get("zones-changed-delay", 0), |
| 58 |
) |
| 59 |
|
| 60 |
mock.current_zones = mock.inputcapture_params.default_zone |
| 61 |
mock.current_zone_set = next(serials) |
| 62 |
|
| 63 |
mock.AddProperties( |
| 64 |
MAIN_IFACE, |
| 65 |
dbus.Dictionary( |
| 66 |
{ |
| 67 |
"version": dbus.UInt32(parameters.get("version", VERSION)), |
| 68 |
"SupportedCapabilities": dbus.UInt32( |
| 69 |
mock.inputcapture_params.supported_capabilities |
| 70 |
), |
| 71 |
} |
| 72 |
), |
| 73 |
) |
| 74 |
|
| 75 |
mock.active_session_handles = [] |
| 76 |
|
| 77 |
|
| 78 |
@dbus.service.method( |
| 79 |
MAIN_IFACE, |
| 80 |
in_signature="oossa{sv}", |
| 81 |
out_signature="ua{sv}", |
| 82 |
) |
| 83 |
def CreateSession(self, handle, session_handle, app_id, parent_window, options): |
| 84 |
try: |
| 85 |
logger.debug(f"CreateSession({parent_window}, {options})") |
| 86 |
params = self.inputcapture_params |
| 87 |
|
| 88 |
assert "capabilities" in options |
| 89 |
|
| 90 |
# Filter to the subset of supported capabilities |
| 91 |
if params.capabilities is None: |
| 92 |
capabilities = options["capabilities"] |
| 93 |
else: |
| 94 |
capabilities = params.capabilities |
| 95 |
|
| 96 |
capabilities &= params.supported_capabilities |
| 97 |
response = Response(0, {}) |
| 98 |
|
| 99 |
response.results["capabilities"] = dbus.UInt32(capabilities) |
| 100 |
self.active_session_handles.append(session_handle) |
| 101 |
|
| 102 |
logger.debug(f"CreateSession with response {response}") |
| 103 |
|
| 104 |
return response.response, response.results |
| 105 |
except Exception as e: |
| 106 |
logger.critical(e) |
| 107 |
return (2, {}) |
| 108 |
|
| 109 |
|
| 110 |
@dbus.service.method( |
| 111 |
MAIN_IFACE, |
| 112 |
in_signature="oosa{sv}", |
| 113 |
out_signature="ua{sv}", |
| 114 |
) |
| 115 |
def GetZones(self, handle, session_handle, app_id, options): |
| 116 |
try: |
| 117 |
logger.debug(f"GetZones({session_handle}, {options})") |
| 118 |
params = self.inputcapture_params |
| 119 |
|
| 120 |
assert session_handle in self.active_session_handles |
| 121 |
|
| 122 |
response = Response(0, {}) |
| 123 |
response.results["zones"] = params.default_zone |
| 124 |
response.results["zone_set"] = dbus.UInt32( |
| 125 |
self.current_zone_set, variant_level=1 |
| 126 |
) |
| 127 |
logger.debug(f"GetZones with response {response}") |
| 128 |
|
| 129 |
if response.response == 0: |
| 130 |
self.current_zones = response.results["zones"] |
| 131 |
|
| 132 |
return response.response, response.results |
| 133 |
except Exception as e: |
| 134 |
logger.critical(e) |
| 135 |
return (2, {}) |
| 136 |
|
| 137 |
|
| 138 |
@dbus.service.method( |
| 139 |
MAIN_IFACE, |
| 140 |
in_signature="oosa{sv}aa{sv}u", |
| 141 |
out_signature="ua{sv}", |
| 142 |
) |
| 143 |
def SetPointerBarriers( |
| 144 |
self, handle, session_handle, app_id, options, barriers, zone_set |
| 145 |
): |
| 146 |
try: |
| 147 |
logger.debug( |
| 148 |
f"SetPointerBarriers({session_handle}, {options}, {barriers}, {zone_set})" |
| 149 |
) |
| 150 |
|
| 151 |
assert session_handle in self.active_session_handles |
| 152 |
assert zone_set == self.current_zone_set |
| 153 |
|
| 154 |
self.current_barriers = [] |
| 155 |
|
| 156 |
failed_barriers = [] |
| 157 |
|
| 158 |
# Barrier sanity checks: |
| 159 |
for b in barriers: |
| 160 |
id = b["barrier_id"] |
| 161 |
x1, y1, x2, y2 = b["position"] |
| 162 |
if (x1 != x2 and y1 != y2) or (x1 == x2 and y1 == y2): |
| 163 |
logger.debug(f"Barrier {id} is not horizontal or vertical") |
| 164 |
failed_barriers.append(id) |
| 165 |
continue |
| 166 |
|
| 167 |
for z in self.current_zones: |
| 168 |
w, h, x, y = z |
| 169 |
if x1 < x or x1 > x + w: |
| 170 |
continue |
| 171 |
if y1 < y or y1 > y + h: |
| 172 |
continue |
| 173 |
|
| 174 |
# x1/y1 fit into our current zone |
| 175 |
if x2 < x or x2 > x + w or y2 < y or y2 > y + h: |
| 176 |
logger.debug(f"Barrier {id} spans multiple zones") |
| 177 |
elif x1 == x2 and (x1 != x and x1 != x + w): |
| 178 |
logger.debug(f"Barrier {id} is not on vertical edge") |
| 179 |
elif y1 == y2 and (y1 != y and y1 != y + h): |
| 180 |
logger.debug(f"Barrier {id} is not on horizontal edge") |
| 181 |
else: |
| 182 |
self.current_barriers.append(Barrier(id=id, position=b["position"])) |
| 183 |
break |
| 184 |
|
| 185 |
failed_barriers.append(id) |
| 186 |
break |
| 187 |
else: |
| 188 |
logger.debug(f"Barrier {id} does not fit into any zone") |
| 189 |
failed_barriers.append(id) |
| 190 |
continue |
| 191 |
|
| 192 |
response = Response(0, {}) |
| 193 |
response.results["failed_barriers"] = dbus.Array( |
| 194 |
[dbus.UInt32(f) for f in failed_barriers], |
| 195 |
signature="u", |
| 196 |
variant_level=1, |
| 197 |
) |
| 198 |
|
| 199 |
logger.debug(f"SetPointerBarriers with response {response}") |
| 200 |
|
| 201 |
return response.response, response.results |
| 202 |
except Exception as e: |
| 203 |
logger.critical(e) |
| 204 |
return (2, {}) |
| 205 |
|
| 206 |
|
| 207 |
@dbus.service.method( |
| 208 |
MAIN_IFACE, |
| 209 |
in_signature="osa{sv}", |
| 210 |
out_signature="ua{sv}", |
| 211 |
) |
| 212 |
def Enable(self, session_handle, app_id, options): |
| 213 |
try: |
| 214 |
logger.debug(f"Enable({session_handle}, {options})") |
| 215 |
params = self.inputcapture_params |
| 216 |
|
| 217 |
assert session_handle in self.active_session_handles |
| 218 |
|
| 219 |
# for use in the signals |
| 220 |
activation_id = next(serials) |
| 221 |
barrier = self.current_barriers[0] |
| 222 |
pos = (barrier.position[0] + 10, barrier.position[1] + 20) |
| 223 |
|
| 224 |
if params.disable_delay > 0: |
| 225 |
|
| 226 |
def disable(): |
| 227 |
logger.debug("emitting Disabled") |
| 228 |
self.EmitSignal(MAIN_IFACE, "Disabled", "oa{sv}", [session_handle, {}]) |
| 229 |
|
| 230 |
GLib.timeout_add(params.disable_delay, disable) |
| 231 |
|
| 232 |
if params.activated_delay > 0: |
| 233 |
|
| 234 |
def activated(): |
| 235 |
logger.debug("emitting Activated") |
| 236 |
options = { |
| 237 |
"activation_id": dbus.UInt32(activation_id, variant_level=1), |
| 238 |
"barrier_id": dbus.UInt32(barrier.id, variant_level=1), |
| 239 |
"cursor_position": dbus.Struct( |
| 240 |
pos, signature="dd", variant_level=1 |
| 241 |
), |
| 242 |
} |
| 243 |
self.EmitSignal( |
| 244 |
MAIN_IFACE, "Activated", "oa{sv}", [session_handle, options] |
| 245 |
) |
| 246 |
|
| 247 |
GLib.timeout_add(params.activated_delay, activated) |
| 248 |
|
| 249 |
if params.deactivated_delay > 0: |
| 250 |
|
| 251 |
def deactivated(): |
| 252 |
logger.debug("emitting Deactivated") |
| 253 |
options = { |
| 254 |
"activation_id": dbus.UInt32(activation_id, variant_level=1), |
| 255 |
"cursor_position": dbus.Struct( |
| 256 |
pos, signature="dd", variant_level=1 |
| 257 |
), |
| 258 |
} |
| 259 |
self.EmitSignal( |
| 260 |
MAIN_IFACE, "Deactivated", "oa{sv}", [session_handle, options] |
| 261 |
) |
| 262 |
|
| 263 |
GLib.timeout_add(params.deactivated_delay, deactivated) |
| 264 |
|
| 265 |
if params.zones_changed_delay > 0: |
| 266 |
|
| 267 |
def zones_changed(): |
| 268 |
logger.debug("emitting ZonesChanged") |
| 269 |
options = { |
| 270 |
"zone_set": dbus.UInt32(activation_id, variant_level=1), |
| 271 |
} |
| 272 |
self.EmitSignal( |
| 273 |
MAIN_IFACE, "ZonesChanged", "oa{sv}", [session_handle, options] |
| 274 |
) |
| 275 |
|
| 276 |
GLib.timeout_add(params.zones_changed_delay, zones_changed) |
| 277 |
|
| 278 |
except Exception as e: |
| 279 |
logger.critical(e) |
| 280 |
return (2, {}) |
| 281 |
|
| 282 |
|
| 283 |
@dbus.service.method( |
| 284 |
MAIN_IFACE, |
| 285 |
in_signature="osa{sv}", |
| 286 |
out_signature="ua{sv}", |
| 287 |
) |
| 288 |
def Disable(self, session_handle, app_id, options): |
| 289 |
try: |
| 290 |
logger.debug(f"Disable({session_handle}, {options})") |
| 291 |
|
| 292 |
assert session_handle in self.active_session_handles |
| 293 |
except Exception as e: |
| 294 |
logger.critical(e) |
| 295 |
return (2, {}) |
| 296 |
|
| 297 |
|
| 298 |
@dbus.service.method( |
| 299 |
MAIN_IFACE, |
| 300 |
in_signature="osa{sv}", |
| 301 |
out_signature="ua{sv}", |
| 302 |
) |
| 303 |
def Release(self, session_handle, app_id, options): |
| 304 |
try: |
| 305 |
logger.debug(f"Release({session_handle}, {options})") |
| 306 |
|
| 307 |
assert session_handle in self.active_session_handles |
| 308 |
except Exception as e: |
| 309 |
logger.critical(e) |
| 310 |
return (2, {}) |
| 311 |
|
| 312 |
|
| 313 |
@dbus.service.method( |
| 314 |
MAIN_IFACE, |
| 315 |
in_signature="osa{sv}", |
| 316 |
out_signature="h", |
| 317 |
) |
| 318 |
def ConnectToEIS(self, session_handle, app_id, options): |
| 319 |
try: |
| 320 |
logger.debug(f"ConnectToEIS({session_handle}, {options})") |
| 321 |
|
| 322 |
assert session_handle in self.active_session_handles |
| 323 |
|
| 324 |
sockets = socket.socketpair() |
| 325 |
self.eis_socket = sockets[0] |
| 326 |
|
| 327 |
assert self.eis_socket.send(b"HELLO") == 5 |
| 328 |
|
| 329 |
fd = sockets[1] |
| 330 |
|
| 331 |
logger.debug(f"ConnectToEis with fd {fd.fileno()}") |
| 332 |
|
| 333 |
return dbus.types.UnixFd(fd) |
| 334 |
except Exception as e: |
| 335 |
logger.critical(e) |
| 336 |
return -1 |