pyinputcapture 0.3.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/.github/workflows/ci.yml +7 -3
- pyinputcapture-0.4.0/.github/workflows/release.yml +104 -0
- pyinputcapture-0.4.0/.release-please-manifest.json +3 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/CHANGELOG.md +7 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/Cargo.lock +1 -1
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/Cargo.toml +1 -1
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/PKG-INFO +1 -1
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/README.md +37 -1
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/pyproject.toml +1 -1
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/python/pyinputcapture/__init__.py +18 -2
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/python/pyinputcapture/pyinputcapture.pyi +49 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/src/lib.rs +88 -82
- pyinputcapture-0.4.0/src/remote_desktop.rs +372 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/tests/test_input_capture.py +21 -1
- pyinputcapture-0.3.0/.github/workflows/release.yml +0 -59
- pyinputcapture-0.3.0/.release-please-manifest.json +0 -3
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/.gitignore +0 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/LICENSE +0 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/Makefile +0 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/python/pyinputcapture/_async.py +0 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/python/pyinputcapture/py.typed +0 -0
- {pyinputcapture-0.3.0 → pyinputcapture-0.4.0}/release-please-config.json +0 -0
|
@@ -10,9 +10,9 @@ on:
|
|
|
10
10
|
release:
|
|
11
11
|
type: boolean
|
|
12
12
|
default: false
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
checkout-ref:
|
|
14
|
+
type: string
|
|
15
|
+
default: ""
|
|
16
16
|
|
|
17
17
|
permissions:
|
|
18
18
|
contents: read
|
|
@@ -28,6 +28,8 @@ jobs:
|
|
|
28
28
|
timeout-minutes: 20
|
|
29
29
|
steps:
|
|
30
30
|
- uses: actions/checkout@v4
|
|
31
|
+
with:
|
|
32
|
+
ref: ${{ inputs.checkout-ref }}
|
|
31
33
|
- uses: dtolnay/rust-toolchain@stable
|
|
32
34
|
- uses: actions/setup-python@v5
|
|
33
35
|
with:
|
|
@@ -66,6 +68,8 @@ jobs:
|
|
|
66
68
|
python-version: ["3.11", "3.12", "3.13"]
|
|
67
69
|
steps:
|
|
68
70
|
- uses: actions/checkout@v4
|
|
71
|
+
with:
|
|
72
|
+
ref: ${{ inputs.checkout-ref }}
|
|
69
73
|
- uses: actions/setup-python@v5
|
|
70
74
|
with:
|
|
71
75
|
python-version: ${{ matrix.python-version }}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
inputs:
|
|
8
|
+
tag:
|
|
9
|
+
description: Tag to build and publish
|
|
10
|
+
required: true
|
|
11
|
+
default: v0.3.0
|
|
12
|
+
|
|
13
|
+
permissions:
|
|
14
|
+
contents: write
|
|
15
|
+
pull-requests: write
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
release-please:
|
|
19
|
+
if: github.event_name == 'push'
|
|
20
|
+
runs-on: ubuntu-latest
|
|
21
|
+
outputs:
|
|
22
|
+
releases_created: ${{ steps.release.outputs.releases_created }}
|
|
23
|
+
tag_name: ${{ steps.release.outputs.tag_name }}
|
|
24
|
+
steps:
|
|
25
|
+
- uses: googleapis/release-please-action@v4
|
|
26
|
+
id: release
|
|
27
|
+
with:
|
|
28
|
+
# Cargo.toml and python/pyinputcapture/__init__.py are kept in sync
|
|
29
|
+
# via "extra-files" in release-please-config.json.
|
|
30
|
+
config-file: release-please-config.json
|
|
31
|
+
manifest-file: .release-please-manifest.json
|
|
32
|
+
|
|
33
|
+
build:
|
|
34
|
+
needs: release-please
|
|
35
|
+
if: needs.release-please.outputs.releases_created == 'true'
|
|
36
|
+
uses: ./.github/workflows/ci.yml
|
|
37
|
+
with:
|
|
38
|
+
release: true
|
|
39
|
+
checkout-ref: ${{ needs.release-please.outputs.tag_name }}
|
|
40
|
+
|
|
41
|
+
publish:
|
|
42
|
+
needs: [release-please, build]
|
|
43
|
+
if: needs.release-please.outputs.releases_created == 'true'
|
|
44
|
+
runs-on: ubuntu-latest
|
|
45
|
+
steps:
|
|
46
|
+
- uses: actions/checkout@v4
|
|
47
|
+
with:
|
|
48
|
+
ref: ${{ needs.release-please.outputs.tag_name }}
|
|
49
|
+
|
|
50
|
+
- uses: actions/download-artifact@v4
|
|
51
|
+
with:
|
|
52
|
+
pattern: wheels-*
|
|
53
|
+
merge-multiple: true
|
|
54
|
+
path: dist
|
|
55
|
+
|
|
56
|
+
- name: Build sdist
|
|
57
|
+
uses: PyO3/maturin-action@v1
|
|
58
|
+
with:
|
|
59
|
+
command: sdist
|
|
60
|
+
args: --out dist
|
|
61
|
+
|
|
62
|
+
- name: Publish to PyPI
|
|
63
|
+
uses: PyO3/maturin-action@v1
|
|
64
|
+
env:
|
|
65
|
+
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
66
|
+
with:
|
|
67
|
+
command: upload
|
|
68
|
+
args: --non-interactive --skip-existing dist/*
|
|
69
|
+
|
|
70
|
+
manual-build:
|
|
71
|
+
if: github.event_name == 'workflow_dispatch'
|
|
72
|
+
uses: ./.github/workflows/ci.yml
|
|
73
|
+
with:
|
|
74
|
+
release: true
|
|
75
|
+
checkout-ref: ${{ inputs.tag }}
|
|
76
|
+
|
|
77
|
+
manual-publish:
|
|
78
|
+
needs: manual-build
|
|
79
|
+
if: github.event_name == 'workflow_dispatch'
|
|
80
|
+
runs-on: ubuntu-latest
|
|
81
|
+
steps:
|
|
82
|
+
- uses: actions/checkout@v4
|
|
83
|
+
with:
|
|
84
|
+
ref: ${{ inputs.tag }}
|
|
85
|
+
|
|
86
|
+
- uses: actions/download-artifact@v4
|
|
87
|
+
with:
|
|
88
|
+
pattern: wheels-*
|
|
89
|
+
merge-multiple: true
|
|
90
|
+
path: dist
|
|
91
|
+
|
|
92
|
+
- name: Build sdist
|
|
93
|
+
uses: PyO3/maturin-action@v1
|
|
94
|
+
with:
|
|
95
|
+
command: sdist
|
|
96
|
+
args: --out dist
|
|
97
|
+
|
|
98
|
+
- name: Publish to PyPI
|
|
99
|
+
uses: PyO3/maturin-action@v1
|
|
100
|
+
env:
|
|
101
|
+
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
102
|
+
with:
|
|
103
|
+
command: upload
|
|
104
|
+
args: --non-interactive --skip-existing dist/*
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.0](https://github.com/fizzi01/pyinputcapture/compare/v0.3.0...v0.4.0) (2026-11-27)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add RemoteDesktopPortal with persistent grants ([#7](https://github.com/fizzi01/pyinputcapture/issues/7)) ([0ed1dd7](https://github.com/fizzi01/pyinputcapture/commit/0ed1dd738aab92cf3e7dd54b65c7be2565e906fc))
|
|
9
|
+
|
|
3
10
|
## [0.3.0](https://github.com/fizzi01/pyinputcapture/compare/v0.2.0...v0.3.0) (2026-08-01)
|
|
4
11
|
|
|
5
12
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Python bindings for the [XDG InputCapture portal](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.InputCapture.html) on Wayland, built with [ashpd](https://github.com/bilelmoussaoui/ashpd) + [PyO3](https://pyo3.rs).
|
|
4
4
|
|
|
5
|
-
Capture pointer input across monitor boundaries via `libei`. Provides both blocking and async APIs.
|
|
5
|
+
Capture pointer input across monitor boundaries via `libei`. Provides both blocking and async APIs. Also wraps the RemoteDesktop portal for input emulation with a grant that survives restarts.
|
|
6
6
|
|
|
7
7
|
## Requirements
|
|
8
8
|
|
|
@@ -149,6 +149,42 @@ for when a client abuts only part of an edge — and returns the new
|
|
|
149
149
|
`ValueError` naming its index. It reuses the live session on purpose:
|
|
150
150
|
re-running `setup` is what hangs the GNOME portal.
|
|
151
151
|
|
|
152
|
+
## RemoteDesktop (input emulation with a persistent grant)
|
|
153
|
+
|
|
154
|
+
`RemoteDesktopPortal` opens a pointer-only RemoteDesktop session and returns
|
|
155
|
+
the EIS fd for emulating input with libei. With `persist=True` the portal keeps
|
|
156
|
+
the grant until the user revokes it, and the returned `restore_token` reopens
|
|
157
|
+
the session with no dialog next time.
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
from pyinputcapture import RemoteDesktopPortal, register_host_app
|
|
161
|
+
|
|
162
|
+
register_host_app("org.example.App") # before any other portal object
|
|
163
|
+
rd = RemoteDesktopPortal()
|
|
164
|
+
eis_fd, token = rd.setup(restore_token=saved_token, persist=True, timeout=300)
|
|
165
|
+
save(token) # single use: always keep the latest one
|
|
166
|
+
# … emulate pointer input on eis_fd with libei …
|
|
167
|
+
rd.close()
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
| Function / Method | Description |
|
|
171
|
+
|------------------------------------------------------------|-------------------------------------------------------------------|
|
|
172
|
+
| `register_host_app(app_id)` | Register the app id grants are stored under (x-d-p >= 1.19.4) |
|
|
173
|
+
| `RemoteDesktopPortal.setup(restore_token, persist, timeout)` | Open the session. Returns `(eis_fd, restore_token)` |
|
|
174
|
+
| `RemoteDesktopPortal.close()` | End the session, or the wait for it, from any thread |
|
|
175
|
+
| `RemoteDesktopPortal.last_error` | Portal task's last error, or `None` |
|
|
176
|
+
|
|
177
|
+
`setup` raises `PermissionError` when the portal refuses (a saved token is then
|
|
178
|
+
worthless), `TimeoutError` when the dialog is not answered in time, and
|
|
179
|
+
`RuntimeError` otherwise. `timeout` is capped at one hour.
|
|
180
|
+
|
|
181
|
+
**Register before anything else.** The portal fixes a host process's app id on
|
|
182
|
+
the first call over its D-Bus connection, and every portal object here shares
|
|
183
|
+
that connection. Grants are keyed by app id, so it must name the app's
|
|
184
|
+
`.desktop` file and stay the same across launches. Without a Registry
|
|
185
|
+
(xdg-desktop-portal < 1.19.4) `register_host_app` raises `RuntimeError` and the
|
|
186
|
+
portal falls back to the id of the process's systemd scope.
|
|
187
|
+
|
|
152
188
|
## Development
|
|
153
189
|
|
|
154
190
|
```bash
|
|
@@ -4,7 +4,7 @@ build-backend = "maturin"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pyinputcapture"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.4.0"
|
|
8
8
|
description = "Python bindings for the XDG InputCapture portal (Wayland cursor capture via ashpd)"
|
|
9
9
|
requires-python = ">=3.11"
|
|
10
10
|
license = { text = "GPL-3.0-or-later" }
|
|
@@ -23,15 +23,31 @@ Quick start (async)::
|
|
|
23
23
|
# …
|
|
24
24
|
await portal.release(cursor_x=960.0, cursor_y=540.0)
|
|
25
25
|
await portal.close()
|
|
26
|
+
|
|
27
|
+
RemoteDesktop (input emulation via libei, grant kept across runs)::
|
|
28
|
+
|
|
29
|
+
from pyinputcapture import RemoteDesktopPortal, register_host_app
|
|
30
|
+
|
|
31
|
+
register_host_app("org.example.App") # before any other portal object
|
|
32
|
+
rd = RemoteDesktopPortal()
|
|
33
|
+
eis_fd, token = rd.setup(restore_token=saved_token)
|
|
34
|
+
# … store `token`, emulate input on eis_fd with libei …
|
|
35
|
+
rd.close()
|
|
26
36
|
"""
|
|
27
37
|
|
|
28
|
-
from pyinputcapture.pyinputcapture import
|
|
38
|
+
from pyinputcapture.pyinputcapture import (
|
|
39
|
+
InputCapturePortal,
|
|
40
|
+
RemoteDesktopPortal,
|
|
41
|
+
register_host_app,
|
|
42
|
+
)
|
|
29
43
|
from pyinputcapture._async import AsyncInputCapturePortal
|
|
30
44
|
|
|
31
|
-
__version__ = "0.
|
|
45
|
+
__version__ = "0.4.0" # x-release-please-version
|
|
32
46
|
|
|
33
47
|
__all__ = [
|
|
34
48
|
"InputCapturePortal",
|
|
35
49
|
"AsyncInputCapturePortal",
|
|
50
|
+
"RemoteDesktopPortal",
|
|
51
|
+
"register_host_app",
|
|
36
52
|
"__version__",
|
|
37
53
|
]
|
|
@@ -105,3 +105,52 @@ class InputCapturePortal:
|
|
|
105
105
|
def close(self) -> None:
|
|
106
106
|
"""Close the session and shut down the background task."""
|
|
107
107
|
...
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class RemoteDesktopPortal:
|
|
111
|
+
"""Wayland RemoteDesktop portal, pointer devices only, for libei emulation.
|
|
112
|
+
|
|
113
|
+
One object, one session: `setup` once, `close` (or drop) to end it.
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
def __init__(self) -> None: ...
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def last_error(self) -> str | None:
|
|
120
|
+
"""The portal task's last error, or None. Cleared by `setup()`."""
|
|
121
|
+
...
|
|
122
|
+
|
|
123
|
+
def setup(
|
|
124
|
+
self,
|
|
125
|
+
restore_token: str | None = None,
|
|
126
|
+
persist: bool = True,
|
|
127
|
+
timeout: float | None = 300.0,
|
|
128
|
+
) -> tuple[int, str | None]:
|
|
129
|
+
"""Open the session and connect to EIS. Returns (eis_fd, restore_token).
|
|
130
|
+
|
|
131
|
+
Blocks, with the GIL released, until the portal grants the session: at
|
|
132
|
+
once when `restore_token` is still valid, otherwise when the user
|
|
133
|
+
answers the dialog. The returned token is the one to present next time
|
|
134
|
+
(single use: always store the latest).
|
|
135
|
+
|
|
136
|
+
Raises PermissionError when the portal refuses (a saved token is then
|
|
137
|
+
worthless), TimeoutError past `timeout`, RuntimeError otherwise.
|
|
138
|
+
`close()` from another thread ends the wait. `timeout` is capped at
|
|
139
|
+
one hour; None waits indefinitely.
|
|
140
|
+
"""
|
|
141
|
+
...
|
|
142
|
+
|
|
143
|
+
def close(self) -> None:
|
|
144
|
+
"""End the session, or the wait for it. Never blocks."""
|
|
145
|
+
...
|
|
146
|
+
|
|
147
|
+
def register_host_app(app_id: str) -> None:
|
|
148
|
+
"""Tell the portal which app this non-sandboxed process is.
|
|
149
|
+
|
|
150
|
+
Grants are stored per app id. Call before any other portal object in the
|
|
151
|
+
process. Repeating the same id is a no-op; a different one raises
|
|
152
|
+
ValueError, as does an invalid id. Raises RuntimeError when the portal has
|
|
153
|
+
no Registry (xdg-desktop-portal < 1.19.4) and TimeoutError when it does not
|
|
154
|
+
answer; the process can carry on unregistered.
|
|
155
|
+
"""
|
|
156
|
+
...
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
//! `activation_id` is written **last** with `Release` ordering so that
|
|
11
11
|
//! a Python `Acquire` load that sees the new ID is guaranteed to also
|
|
12
12
|
//! see the corresponding barrier_id and cursor position.
|
|
13
|
+
//!
|
|
14
|
+
//! `RemoteDesktopPortal` (see `remote_desktop`) shares the runtime and the
|
|
15
|
+
//! D-Bus connection, which is what lets one `register_host_app` call cover
|
|
16
|
+
//! every portal object in the process.
|
|
13
17
|
|
|
14
18
|
use std::future::Future;
|
|
15
19
|
use std::num::NonZeroU32;
|
|
@@ -25,28 +29,25 @@ use pyo3::exceptions::{PyRuntimeError, PyTypeError, PyValueError};
|
|
|
25
29
|
use pyo3::prelude::*;
|
|
26
30
|
use tokio::sync::{mpsc, oneshot};
|
|
27
31
|
|
|
32
|
+
mod remote_desktop;
|
|
33
|
+
|
|
28
34
|
/// A poisoned lock only means some other thread panicked while holding it; the
|
|
29
35
|
/// data behind it is a plain snapshot of portal geometry, so there is nothing
|
|
30
36
|
/// to salvage or invalidate. Taking the inner value keeps a panic in one Python
|
|
31
37
|
/// thread from turning every later call into an error.
|
|
32
|
-
fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
|
|
38
|
+
pub(crate) fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
|
|
33
39
|
m.lock().unwrap_or_else(|e| e.into_inner())
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
/// The one tokio runtime of the process, shared by every portal object.
|
|
37
43
|
///
|
|
38
|
-
///
|
|
39
|
-
///
|
|
40
|
-
///
|
|
41
|
-
///
|
|
42
|
-
/// and that connection's zbus tasks live on whichever runtime created it first.
|
|
43
|
-
///
|
|
44
|
-
/// With a runtime per portal object, dropping one object shut its runtime down
|
|
45
|
-
/// and killed those tasks while the now-dead connection stayed in ashpd's
|
|
46
|
-
/// static. A process-global connection needs a process-global runtime to carry it.
|
|
44
|
+
/// Load-bearing: `ashpd` caches the D-Bus session connection in a process-global
|
|
45
|
+
/// static, and that connection's zbus tasks live on whichever runtime created it
|
|
46
|
+
/// first. A per-object runtime shut down on `Drop` kills them, after which every
|
|
47
|
+
/// portal request in the process is accepted and never answered.
|
|
47
48
|
static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
|
|
48
49
|
|
|
49
|
-
fn runtime() -> PyResult<&'static tokio::runtime::Runtime> {
|
|
50
|
+
pub(crate) fn runtime() -> PyResult<&'static tokio::runtime::Runtime> {
|
|
50
51
|
if let Some(rt) = RUNTIME.get() {
|
|
51
52
|
return Ok(rt);
|
|
52
53
|
}
|
|
@@ -60,47 +61,40 @@ fn runtime() -> PyResult<&'static tokio::runtime::Runtime> {
|
|
|
60
61
|
Ok(RUNTIME.get_or_init(|| rt))
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
/// Slack
|
|
64
|
-
///
|
|
65
|
-
/// The task's bound exists only so a request nobody will ever answer ends
|
|
66
|
-
/// instead of parking a worker forever - it must not be what *reports* the
|
|
67
|
-
/// timeout, because the task's error reaches Python only as a late `last_error`.
|
|
68
|
-
/// Python's own `wait_for_setup` has to expire first.
|
|
64
|
+
/// Slack over the caller's deadline, so Python's own wait reports the timeout
|
|
65
|
+
/// first: the task's error only reaches it as a late `last_error`.
|
|
69
66
|
const SETUP_DEADLINE_MARGIN: Duration = Duration::from_secs(5);
|
|
70
67
|
|
|
71
|
-
///
|
|
72
|
-
///
|
|
73
|
-
///
|
|
74
|
-
///
|
|
75
|
-
|
|
76
|
-
///
|
|
77
|
-
/// `None` - the caller asked to wait indefinitely - stays unbounded, as before.
|
|
78
|
-
struct SetupBudget {
|
|
68
|
+
/// Bounds the whole pre-`Ok` phase of a session, all of which waits on the
|
|
69
|
+
/// portal and the first step of which waits on a human. The task outlives the
|
|
70
|
+
/// object that spawned it, so an unanswered dialog would otherwise park a
|
|
71
|
+
/// worker for the life of the process. `None` stays unbounded.
|
|
72
|
+
pub(crate) struct SetupBudget {
|
|
79
73
|
until: Option<Instant>,
|
|
80
74
|
}
|
|
81
75
|
|
|
82
76
|
impl SetupBudget {
|
|
83
|
-
fn new(limit: Option<Duration>) -> Self {
|
|
77
|
+
pub(crate) fn new(limit: Option<Duration>) -> Self {
|
|
84
78
|
Self {
|
|
85
79
|
until: limit.map(|d| Instant::now() + d + SETUP_DEADLINE_MARGIN),
|
|
86
80
|
}
|
|
87
81
|
}
|
|
88
82
|
|
|
89
83
|
/// Await `fut`, failing with a named error once the budget is spent.
|
|
90
|
-
async fn guard<T
|
|
84
|
+
pub(crate) async fn guard<T, E: From<String>>(
|
|
91
85
|
&self,
|
|
92
86
|
what: &str,
|
|
93
|
-
fut: impl Future<Output = Result<T,
|
|
94
|
-
) -> Result<T,
|
|
87
|
+
fut: impl Future<Output = Result<T, E>>,
|
|
88
|
+
) -> Result<T, E> {
|
|
95
89
|
let Some(until) = self.until else {
|
|
96
90
|
return fut.await;
|
|
97
91
|
};
|
|
98
92
|
let left = until.saturating_duration_since(Instant::now());
|
|
99
93
|
match tokio::time::timeout(left, fut).await {
|
|
100
94
|
Ok(result) => result,
|
|
101
|
-
Err(_) => Err(format!(
|
|
95
|
+
Err(_) => Err(E::from(format!(
|
|
102
96
|
"{what}: the portal did not answer within the setup deadline"
|
|
103
|
-
)),
|
|
97
|
+
))),
|
|
104
98
|
}
|
|
105
99
|
}
|
|
106
100
|
}
|
|
@@ -283,7 +277,7 @@ fn build_segment_barriers(
|
|
|
283
277
|
/// Only call this with an fd that was produced by `into_raw_fd` on this side
|
|
284
278
|
/// and was never handed out: taking ownership back is only sound while nothing
|
|
285
279
|
/// else can close it.
|
|
286
|
-
fn close_orphan_fd(raw_fd: i32) {
|
|
280
|
+
pub(crate) fn close_orphan_fd(raw_fd: i32) {
|
|
287
281
|
// SAFETY: per the invariant above, this is the sole remaining owner.
|
|
288
282
|
drop(unsafe { OwnedFd::from_raw_fd(raw_fd) });
|
|
289
283
|
}
|
|
@@ -393,11 +387,9 @@ async fn portal_session(
|
|
|
393
387
|
})
|
|
394
388
|
.await?;
|
|
395
389
|
|
|
396
|
-
// Create session. On GNOME this is what raises the permission dialog
|
|
397
|
-
// stays pending
|
|
398
|
-
//
|
|
399
|
-
// above it is what keeps a dialog nobody answers from parking this task
|
|
400
|
-
// forever (it now outlives the object that spawned it).
|
|
390
|
+
// Create session. On GNOME this is what raises the permission dialog and
|
|
391
|
+
// stays pending until the user answers it, so the Python caller must not
|
|
392
|
+
// hold the GIL across it, and the budget is what bounds it.
|
|
401
393
|
let (session, _caps) = budget
|
|
402
394
|
.guard("create_session", async {
|
|
403
395
|
ic.create_session(
|
|
@@ -441,9 +433,8 @@ async fn live_session(
|
|
|
441
433
|
active_edges: Option<Vec<String>>,
|
|
442
434
|
budget: &SetupBudget,
|
|
443
435
|
) -> Result<(), String> {
|
|
444
|
-
//
|
|
445
|
-
//
|
|
446
|
-
// not be able to park on any of it.
|
|
436
|
+
// Still inside the budget: every step up to the `Ok` handed back to Python
|
|
437
|
+
// waits on the portal, and none of them may park the task.
|
|
447
438
|
let zones_resp = budget
|
|
448
439
|
.guard("zones", async {
|
|
449
440
|
ic.zones(session)
|
|
@@ -627,6 +618,12 @@ const MAX_TIMEOUT_SECS: f64 = 60.0;
|
|
|
627
618
|
/// `setup(timeout=0)` is a caller asking to fail fast, and answering that with
|
|
628
619
|
/// an unbounded wait is the worst possible reading.
|
|
629
620
|
fn deadline_from(timeout: Option<f64>) -> PyResult<Option<Duration>> {
|
|
621
|
+
deadline_capped(timeout, MAX_TIMEOUT_SECS)
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/// `deadline_from` with a caller-chosen ceiling, for waits that are a human
|
|
625
|
+
/// answering a dialog rather than a portal answering a request.
|
|
626
|
+
pub(crate) fn deadline_capped(timeout: Option<f64>, max_secs: f64) -> PyResult<Option<Duration>> {
|
|
630
627
|
match timeout {
|
|
631
628
|
None => Ok(None),
|
|
632
629
|
Some(secs) if secs.is_nan() => Err(PyValueError::new_err(
|
|
@@ -637,22 +634,30 @@ fn deadline_from(timeout: Option<f64>) -> PyResult<Option<Duration>> {
|
|
|
637
634
|
pass None to wait indefinitely"
|
|
638
635
|
))),
|
|
639
636
|
Some(secs) if secs.is_infinite() => Ok(None),
|
|
640
|
-
Some(secs) => Ok(Some(Duration::from_secs_f64(secs.min(
|
|
637
|
+
Some(secs) => Ok(Some(Duration::from_secs_f64(secs.min(max_secs)))),
|
|
641
638
|
}
|
|
642
639
|
}
|
|
643
640
|
|
|
641
|
+
/// How a wait for the portal task's answer ended.
|
|
642
|
+
pub(crate) enum Waited<T> {
|
|
643
|
+
Answer(T),
|
|
644
|
+
TimedOut(Duration),
|
|
645
|
+
/// The task dropped its sender without answering.
|
|
646
|
+
Gone,
|
|
647
|
+
}
|
|
648
|
+
|
|
644
649
|
/// Wait for the portal task's answer without pinning the interpreter.
|
|
645
650
|
///
|
|
646
651
|
/// The wait runs in short slices with the GIL released; between slices the GIL
|
|
647
652
|
/// comes back just long enough to run Python's signal handlers. Without that,
|
|
648
653
|
/// a wait with no deadline is un-interruptible: the handler for Ctrl-C only
|
|
649
654
|
/// runs when the thread executes bytecode, so the process has to be killed.
|
|
650
|
-
fn
|
|
655
|
+
pub(crate) fn wait_for<T: Send>(
|
|
651
656
|
py: Python<'_>,
|
|
652
657
|
handle: &tokio::runtime::Handle,
|
|
653
|
-
mut
|
|
658
|
+
mut rx: oneshot::Receiver<T>,
|
|
654
659
|
deadline: Option<Duration>,
|
|
655
|
-
) -> PyResult<
|
|
660
|
+
) -> PyResult<Waited<T>> {
|
|
656
661
|
const SLICE: Duration = Duration::from_millis(100);
|
|
657
662
|
let started = Instant::now();
|
|
658
663
|
|
|
@@ -661,10 +666,7 @@ fn wait_for_setup(
|
|
|
661
666
|
Some(limit) => {
|
|
662
667
|
let left = limit.saturating_sub(started.elapsed());
|
|
663
668
|
if left.is_zero() {
|
|
664
|
-
return
|
|
665
|
-
"portal setup timed out after {}s (permission dialog unanswered?)",
|
|
666
|
-
limit.as_secs_f64()
|
|
667
|
-
)));
|
|
669
|
+
return Ok(Waited::TimedOut(limit));
|
|
668
670
|
}
|
|
669
671
|
left.min(SLICE)
|
|
670
672
|
}
|
|
@@ -672,12 +674,12 @@ fn wait_for_setup(
|
|
|
672
674
|
};
|
|
673
675
|
|
|
674
676
|
let outcome = py.allow_threads(|| {
|
|
675
|
-
handle.block_on(async { tokio::time::timeout(slice, &mut
|
|
677
|
+
handle.block_on(async { tokio::time::timeout(slice, &mut rx).await })
|
|
676
678
|
});
|
|
677
679
|
|
|
678
680
|
match outcome {
|
|
679
|
-
Ok(Ok(
|
|
680
|
-
Ok(Err(_)) => return
|
|
681
|
+
Ok(Ok(answer)) => return Ok(Waited::Answer(answer)),
|
|
682
|
+
Ok(Err(_)) => return Ok(Waited::Gone),
|
|
681
683
|
// Slice expired with no answer: let Python raise a pending
|
|
682
684
|
// KeyboardInterrupt, then keep waiting.
|
|
683
685
|
Err(_) => py.check_signals()?,
|
|
@@ -685,6 +687,22 @@ fn wait_for_setup(
|
|
|
685
687
|
}
|
|
686
688
|
}
|
|
687
689
|
|
|
690
|
+
fn wait_for_setup(
|
|
691
|
+
py: Python<'_>,
|
|
692
|
+
handle: &tokio::runtime::Handle,
|
|
693
|
+
setup_rx: oneshot::Receiver<Result<SetupResult, String>>,
|
|
694
|
+
deadline: Option<Duration>,
|
|
695
|
+
) -> PyResult<SetupResult> {
|
|
696
|
+
match wait_for(py, handle, setup_rx, deadline)? {
|
|
697
|
+
Waited::Answer(result) => result.map_err(PyRuntimeError::new_err),
|
|
698
|
+
Waited::TimedOut(limit) => Err(PyRuntimeError::new_err(format!(
|
|
699
|
+
"portal setup timed out after {}s (permission dialog unanswered?)",
|
|
700
|
+
limit.as_secs_f64()
|
|
701
|
+
))),
|
|
702
|
+
Waited::Gone => Err(PyRuntimeError::new_err("portal setup channel closed")),
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
688
706
|
/// Wayland InputCapture portal (ashpd). All methods are blocking, but they
|
|
689
707
|
/// release the GIL while they wait (see `send_simple_cmd`), so a Python program
|
|
690
708
|
/// stays responsive even while a permission dialog is on screen.
|
|
@@ -701,12 +719,8 @@ struct InputCapturePortal {
|
|
|
701
719
|
cmd_tx: Mutex<Option<mpsc::Sender<Cmd>>>,
|
|
702
720
|
shared: Arc<SharedActivation>,
|
|
703
721
|
state: Arc<Mutex<SessionState>>,
|
|
704
|
-
/// Last error the portal task reported
|
|
705
|
-
///
|
|
706
|
-
/// The task can only fail *after* `setup()` has returned - a late answer to
|
|
707
|
-
/// a dialog, a session that died - and that reason used to go to stderr,
|
|
708
|
-
/// which a caller that points fd 2 at /dev/null (to silence libei's dispatch
|
|
709
|
-
/// spam) discards. Then the only account of the failure was gone.
|
|
722
|
+
/// Last error the portal task reported. It can only fail after `setup()`
|
|
723
|
+
/// returned, and stderr may well be pointed at /dev/null by the caller.
|
|
710
724
|
last_error: Arc<Mutex<Option<String>>>,
|
|
711
725
|
}
|
|
712
726
|
|
|
@@ -784,11 +798,10 @@ impl InputCapturePortal {
|
|
|
784
798
|
let result = match wait_for_setup(py, rt.handle(), setup_rx, deadline) {
|
|
785
799
|
Ok(result) => result,
|
|
786
800
|
Err(e) => {
|
|
787
|
-
// Dropping `cmd_tx`
|
|
788
|
-
//
|
|
789
|
-
// session it
|
|
790
|
-
//
|
|
791
|
-
// ends on its own rather than parking on an unanswered dialog.
|
|
801
|
+
// Dropping `cmd_tx` closes the command channel, which the task
|
|
802
|
+
// reads as a Close; `portal_session` then closes whatever
|
|
803
|
+
// session it created. The task needs no tracking - `SetupBudget`
|
|
804
|
+
// bounds it.
|
|
792
805
|
drop(cmd_tx);
|
|
793
806
|
return Err(e);
|
|
794
807
|
}
|
|
@@ -801,9 +814,8 @@ impl InputCapturePortal {
|
|
|
801
814
|
|
|
802
815
|
/// The portal task's last error, or `None`.
|
|
803
816
|
///
|
|
804
|
-
/// The
|
|
805
|
-
///
|
|
806
|
-
/// nowhere else to go. Cleared at the start of every `setup()`.
|
|
817
|
+
/// The only channel for a failure the task hits after `setup()` returned or
|
|
818
|
+
/// gave up. Cleared at the start of every `setup()`.
|
|
807
819
|
#[getter]
|
|
808
820
|
fn last_error(&self) -> Option<String> {
|
|
809
821
|
lock(&self.last_error).clone()
|
|
@@ -976,13 +988,9 @@ impl InputCapturePortal {
|
|
|
976
988
|
|
|
977
989
|
/// Asking the session to close is all a drop does.
|
|
978
990
|
///
|
|
979
|
-
/// It
|
|
980
|
-
///
|
|
981
|
-
///
|
|
982
|
-
/// which every later portal request in the process was accepted and never
|
|
983
|
-
/// answered. Never dropping the runtime also settles, for free, the problem the
|
|
984
|
-
/// old code worked around - dropping a multi-thread runtime under the GIL blocks
|
|
985
|
-
/// on workers that may be parked on a portal reply, freezing the interpreter.
|
|
991
|
+
/// It must not touch the runtime, which is shared by the whole process (see
|
|
992
|
+
/// `RUNTIME`). Never dropping it also keeps a drop under the GIL from blocking
|
|
993
|
+
/// on a worker parked on a portal reply.
|
|
986
994
|
impl Drop for InputCapturePortal {
|
|
987
995
|
fn drop(&mut self) {
|
|
988
996
|
if let Some(tx) = lock(&self.cmd_tx).take() {
|
|
@@ -995,6 +1003,8 @@ impl Drop for InputCapturePortal {
|
|
|
995
1003
|
#[pymodule]
|
|
996
1004
|
fn pyinputcapture(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
997
1005
|
m.add_class::<InputCapturePortal>()?;
|
|
1006
|
+
m.add_class::<remote_desktop::RemoteDesktopPortal>()?;
|
|
1007
|
+
m.add_function(wrap_pyfunction!(remote_desktop::register_host_app, m)?)?;
|
|
998
1008
|
Ok(())
|
|
999
1009
|
}
|
|
1000
1010
|
|
|
@@ -1499,10 +1509,8 @@ mod tests {
|
|
|
1499
1509
|
|
|
1500
1510
|
#[test]
|
|
1501
1511
|
fn runtime_is_one_per_process() {
|
|
1502
|
-
//
|
|
1503
|
-
//
|
|
1504
|
-
// tasks has to be process-global too. A second runtime here would mean a
|
|
1505
|
-
// portal object able to outlive the connection every other object uses.
|
|
1512
|
+
// ashpd's D-Bus connection is process-global, so the runtime carrying
|
|
1513
|
+
// its zbus tasks has to be too.
|
|
1506
1514
|
let a = runtime().unwrap();
|
|
1507
1515
|
let b = runtime().unwrap();
|
|
1508
1516
|
assert!(std::ptr::eq(a, b), "every portal object shares one runtime");
|
|
@@ -1533,9 +1541,8 @@ mod tests {
|
|
|
1533
1541
|
|
|
1534
1542
|
#[test]
|
|
1535
1543
|
fn budget_ends_a_request_nobody_answers() {
|
|
1536
|
-
// The task outlives the portal object
|
|
1537
|
-
//
|
|
1538
|
-
// the process.
|
|
1544
|
+
// The task outlives the portal object, so a dialog nobody ever answers
|
|
1545
|
+
// must end it instead of parking a worker for the life of the process.
|
|
1539
1546
|
let rt = tokio::runtime::Builder::new_current_thread()
|
|
1540
1547
|
.enable_time()
|
|
1541
1548
|
.start_paused(true)
|
|
@@ -1557,9 +1564,8 @@ mod tests {
|
|
|
1557
1564
|
|
|
1558
1565
|
#[test]
|
|
1559
1566
|
fn budget_leaves_the_caller_room_to_report_first() {
|
|
1560
|
-
// Python's
|
|
1561
|
-
//
|
|
1562
|
-
// caller would get the generic channel-closed instead of the timeout.
|
|
1567
|
+
// Python's wait must expire first: the task's error only surfaces as a
|
|
1568
|
+
// late `last_error`, so winning the race would hide the real timeout.
|
|
1563
1569
|
assert!(SETUP_DEADLINE_MARGIN > Duration::ZERO);
|
|
1564
1570
|
}
|
|
1565
1571
|
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
//! RemoteDesktop portal: an input-only session whose grant survives restarts.
|
|
2
|
+
//!
|
|
3
|
+
//! The session asks for pointer devices only and hands back the EIS fd, so the
|
|
4
|
+
//! caller emulates input with libei. With `persist=true` the grant is kept by
|
|
5
|
+
//! the portal until the user revokes it, and the `restore_token` returned by
|
|
6
|
+
//! `setup` reopens it with no dialog next time. The token is single use:
|
|
7
|
+
//! always store the latest one.
|
|
8
|
+
|
|
9
|
+
use std::os::fd::IntoRawFd;
|
|
10
|
+
use std::sync::{Arc, Mutex};
|
|
11
|
+
use std::time::Duration;
|
|
12
|
+
|
|
13
|
+
use ashpd::desktop::remote_desktop::{DeviceType, RemoteDesktop};
|
|
14
|
+
use ashpd::desktop::{PersistMode, Session};
|
|
15
|
+
use pyo3::exceptions::{PyPermissionError, PyRuntimeError, PyTimeoutError, PyValueError};
|
|
16
|
+
use pyo3::prelude::*;
|
|
17
|
+
use tokio::sync::oneshot;
|
|
18
|
+
|
|
19
|
+
use crate::{close_orphan_fd, deadline_capped, lock, runtime, wait_for, SetupBudget, Waited};
|
|
20
|
+
|
|
21
|
+
/// Ceiling for the consent wait. A dialog is a human answering, so the
|
|
22
|
+
/// InputCapture ceiling (60 s) is far too short.
|
|
23
|
+
const MAX_CONSENT_SECS: f64 = 3600.0;
|
|
24
|
+
|
|
25
|
+
/// `Register` is a plain method call, answered at once by any live portal.
|
|
26
|
+
const REGISTER_TIMEOUT: Duration = Duration::from_secs(10);
|
|
27
|
+
|
|
28
|
+
/// Why no session was had. Only `Denied` (the portal answered the request
|
|
29
|
+
/// with "cancelled" or "other") says a saved token is no longer good.
|
|
30
|
+
#[derive(Debug, Clone, PartialEq)]
|
|
31
|
+
enum RdFailure {
|
|
32
|
+
Denied(String),
|
|
33
|
+
Failed(String),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl RdFailure {
|
|
37
|
+
fn message(&self) -> &str {
|
|
38
|
+
match self {
|
|
39
|
+
RdFailure::Denied(m) | RdFailure::Failed(m) => m,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
impl From<String> for RdFailure {
|
|
45
|
+
fn from(message: String) -> Self {
|
|
46
|
+
RdFailure::Failed(message)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn classify(step: &str, e: ashpd::Error) -> RdFailure {
|
|
51
|
+
match e {
|
|
52
|
+
ashpd::Error::Response(r) => RdFailure::Denied(format!("{step}: {r}")),
|
|
53
|
+
other => RdFailure::Failed(format!("{step}: {other}")),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#[derive(Debug)]
|
|
58
|
+
struct RdSetup {
|
|
59
|
+
eis_raw_fd: i32,
|
|
60
|
+
restore_token: Option<String>,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
type SetupTx = oneshot::Sender<Result<RdSetup, RdFailure>>;
|
|
64
|
+
|
|
65
|
+
fn aborted() -> RdFailure {
|
|
66
|
+
RdFailure::Failed("session closed before it was granted".to_string())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// Report a failure to the waiting caller unless the result already went out.
|
|
70
|
+
async fn rd_task(
|
|
71
|
+
setup_tx: SetupTx,
|
|
72
|
+
close_rx: oneshot::Receiver<()>,
|
|
73
|
+
budget: SetupBudget,
|
|
74
|
+
restore_token: Option<String>,
|
|
75
|
+
persist: bool,
|
|
76
|
+
) -> Result<(), String> {
|
|
77
|
+
let mut slot = Some(setup_tx);
|
|
78
|
+
let result = rd_session(&mut slot, close_rx, &budget, restore_token, persist).await;
|
|
79
|
+
if let Err(e) = &result {
|
|
80
|
+
if let Some(tx) = slot.take() {
|
|
81
|
+
tx.send(Err(e.clone())).ok();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
result.map_err(|e| e.message().to_string())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async fn rd_session(
|
|
88
|
+
slot: &mut Option<SetupTx>,
|
|
89
|
+
mut close_rx: oneshot::Receiver<()>,
|
|
90
|
+
budget: &SetupBudget,
|
|
91
|
+
restore_token: Option<String>,
|
|
92
|
+
persist: bool,
|
|
93
|
+
) -> Result<(), RdFailure> {
|
|
94
|
+
// Every await before the grant also listens for `close`: the caller gives
|
|
95
|
+
// up on a timeout or a stop, and the dialog must go away with it.
|
|
96
|
+
let rd = tokio::select! {
|
|
97
|
+
r = budget.guard("RemoteDesktop::new", async {
|
|
98
|
+
RemoteDesktop::new().await.map_err(|e| classify("RemoteDesktop::new", e))
|
|
99
|
+
}) => r?,
|
|
100
|
+
_ = &mut close_rx => return Err(aborted()),
|
|
101
|
+
};
|
|
102
|
+
let session = tokio::select! {
|
|
103
|
+
r = budget.guard("create_session", async {
|
|
104
|
+
rd.create_session().await.map_err(|e| classify("create_session", e))
|
|
105
|
+
}) => r?,
|
|
106
|
+
_ = &mut close_rx => return Err(aborted()),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
let granted = tokio::select! {
|
|
110
|
+
r = start(&rd, &session, budget, restore_token.as_deref(), persist) => r,
|
|
111
|
+
_ = &mut close_rx => Err(aborted()),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
let result = match granted {
|
|
115
|
+
Ok(setup) => {
|
|
116
|
+
let tx = slot.take().ok_or_else(aborted)?;
|
|
117
|
+
if let Err(Ok(unsent)) = tx.send(Ok(setup)) {
|
|
118
|
+
// The caller gave up after the grant: nobody owns the fd.
|
|
119
|
+
close_orphan_fd(unsent.eis_raw_fd);
|
|
120
|
+
} else {
|
|
121
|
+
// Held open until the owner closes it or goes away.
|
|
122
|
+
close_rx.await.ok();
|
|
123
|
+
}
|
|
124
|
+
Ok(())
|
|
125
|
+
}
|
|
126
|
+
Err(e) => Err(e),
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// Dropping the proxy leaves the session alive on the compositor side.
|
|
130
|
+
session.close().await.ok();
|
|
131
|
+
result
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async fn start(
|
|
135
|
+
rd: &RemoteDesktop<'_>,
|
|
136
|
+
session: &Session<'_, RemoteDesktop<'_>>,
|
|
137
|
+
budget: &SetupBudget,
|
|
138
|
+
restore_token: Option<&str>,
|
|
139
|
+
persist: bool,
|
|
140
|
+
) -> Result<RdSetup, RdFailure> {
|
|
141
|
+
let mode = if persist {
|
|
142
|
+
PersistMode::ExplicitlyRevoked
|
|
143
|
+
} else {
|
|
144
|
+
PersistMode::DoNot
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
budget
|
|
148
|
+
.guard("select_devices", async {
|
|
149
|
+
rd.select_devices(session, DeviceType::Pointer.into(), restore_token, mode)
|
|
150
|
+
.await
|
|
151
|
+
.map_err(|e| classify("select_devices", e))?
|
|
152
|
+
.response()
|
|
153
|
+
.map_err(|e| classify("select_devices", e))
|
|
154
|
+
})
|
|
155
|
+
.await?;
|
|
156
|
+
|
|
157
|
+
// The dialog, when there is one, is up for the whole of this step.
|
|
158
|
+
let selected = budget
|
|
159
|
+
.guard("start", async {
|
|
160
|
+
rd.start(session, None)
|
|
161
|
+
.await
|
|
162
|
+
.map_err(|e| classify("start", e))?
|
|
163
|
+
.response()
|
|
164
|
+
.map_err(|e| classify("start", e))
|
|
165
|
+
})
|
|
166
|
+
.await?;
|
|
167
|
+
|
|
168
|
+
let fd = budget
|
|
169
|
+
.guard("connect_to_eis", async {
|
|
170
|
+
rd.connect_to_eis(session)
|
|
171
|
+
.await
|
|
172
|
+
.map_err(|e| RdFailure::Failed(format!("connect_to_eis: {e}")))
|
|
173
|
+
})
|
|
174
|
+
.await?;
|
|
175
|
+
|
|
176
|
+
Ok(RdSetup {
|
|
177
|
+
eis_raw_fd: fd.into_raw_fd(),
|
|
178
|
+
restore_token: selected.restore_token().map(str::to_owned),
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Wayland RemoteDesktop portal, pointer devices only, for libei emulation.
|
|
183
|
+
///
|
|
184
|
+
/// One object, one session: `setup` once, `close` (or drop) to end it.
|
|
185
|
+
#[pyclass]
|
|
186
|
+
pub(crate) struct RemoteDesktopPortal {
|
|
187
|
+
/// Present from `setup` until `close`; sending or dropping it ends the
|
|
188
|
+
/// session, including one still waiting on its dialog.
|
|
189
|
+
close_tx: Mutex<Option<oneshot::Sender<()>>>,
|
|
190
|
+
used: Mutex<bool>,
|
|
191
|
+
last_error: Arc<Mutex<Option<String>>>,
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
impl RemoteDesktopPortal {
|
|
195
|
+
fn end_session(&self) {
|
|
196
|
+
if let Some(tx) = lock(&self.close_tx).take() {
|
|
197
|
+
tx.send(()).ok();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#[pymethods]
|
|
203
|
+
impl RemoteDesktopPortal {
|
|
204
|
+
#[new]
|
|
205
|
+
fn new() -> PyResult<Self> {
|
|
206
|
+
runtime()?;
|
|
207
|
+
Ok(Self {
|
|
208
|
+
close_tx: Mutex::new(None),
|
|
209
|
+
used: Mutex::new(false),
|
|
210
|
+
last_error: Arc::new(Mutex::new(None)),
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/// Open the session and connect to EIS.
|
|
215
|
+
/// Returns `(eis_fd, restore_token)`.
|
|
216
|
+
///
|
|
217
|
+
/// Blocks, with the GIL released, until the portal grants the session:
|
|
218
|
+
/// at once when `restore_token` is still valid, otherwise when the user
|
|
219
|
+
/// answers the dialog. Raises `PermissionError` when the portal refuses
|
|
220
|
+
/// (the token, if any, is then worthless), `TimeoutError` past `timeout`,
|
|
221
|
+
/// `RuntimeError` for anything else. `close()` from another thread ends
|
|
222
|
+
/// the wait.
|
|
223
|
+
#[pyo3(signature = (restore_token=None, persist=true, timeout=300.0))]
|
|
224
|
+
fn setup(
|
|
225
|
+
&self,
|
|
226
|
+
py: Python<'_>,
|
|
227
|
+
restore_token: Option<String>,
|
|
228
|
+
persist: bool,
|
|
229
|
+
timeout: Option<f64>,
|
|
230
|
+
) -> PyResult<(i32, Option<String>)> {
|
|
231
|
+
let deadline = deadline_capped(timeout, MAX_CONSENT_SECS)?;
|
|
232
|
+
{
|
|
233
|
+
let mut used = lock(&self.used);
|
|
234
|
+
if *used {
|
|
235
|
+
return Err(PyRuntimeError::new_err("already set up"));
|
|
236
|
+
}
|
|
237
|
+
*used = true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
let (setup_tx, setup_rx) = oneshot::channel();
|
|
241
|
+
let (close_tx, close_rx) = oneshot::channel();
|
|
242
|
+
*lock(&self.close_tx) = Some(close_tx);
|
|
243
|
+
*lock(&self.last_error) = None;
|
|
244
|
+
let last_error = self.last_error.clone();
|
|
245
|
+
|
|
246
|
+
let rt = runtime()?;
|
|
247
|
+
rt.spawn(async move {
|
|
248
|
+
let budget = SetupBudget::new(deadline);
|
|
249
|
+
if let Err(e) = rd_task(setup_tx, close_rx, budget, restore_token, persist).await {
|
|
250
|
+
*lock(&last_error) = Some(e);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
let outcome = wait_for(py, rt.handle(), setup_rx, deadline);
|
|
255
|
+
let result = match outcome {
|
|
256
|
+
Ok(Waited::Answer(Ok(setup))) => return Ok((setup.eis_raw_fd, setup.restore_token)),
|
|
257
|
+
Ok(Waited::Answer(Err(RdFailure::Denied(m)))) => Err(PyPermissionError::new_err(m)),
|
|
258
|
+
Ok(Waited::Answer(Err(RdFailure::Failed(m)))) => Err(PyRuntimeError::new_err(m)),
|
|
259
|
+
Ok(Waited::TimedOut(limit)) => Err(PyTimeoutError::new_err(format!(
|
|
260
|
+
"permission dialog not answered within {}s",
|
|
261
|
+
limit.as_secs_f64()
|
|
262
|
+
))),
|
|
263
|
+
Ok(Waited::Gone) => Err(PyRuntimeError::new_err(
|
|
264
|
+
"portal task ended without an answer",
|
|
265
|
+
)),
|
|
266
|
+
Err(e) => Err(e),
|
|
267
|
+
};
|
|
268
|
+
self.end_session();
|
|
269
|
+
result
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/// The portal task's last error, or `None`. Cleared by `setup()`.
|
|
273
|
+
#[getter]
|
|
274
|
+
fn last_error(&self) -> Option<String> {
|
|
275
|
+
lock(&self.last_error).clone()
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/// End the session, or the wait for it. Never blocks.
|
|
279
|
+
fn close(&self) -> PyResult<()> {
|
|
280
|
+
self.end_session();
|
|
281
|
+
Ok(())
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
impl Drop for RemoteDesktopPortal {
|
|
286
|
+
fn drop(&mut self) {
|
|
287
|
+
self.end_session();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
static REGISTERED: Mutex<Option<String>> = Mutex::new(None);
|
|
292
|
+
|
|
293
|
+
/// Tell the portal which app this (non-sandboxed) process is.
|
|
294
|
+
///
|
|
295
|
+
/// Grants are stored per app id, and a host process otherwise gets one from
|
|
296
|
+
/// its systemd scope, which differs between a menu launch and a terminal. Must
|
|
297
|
+
/// precede every other portal call in the process; repeating the same id is a
|
|
298
|
+
/// no-op, a different one raises `ValueError`. Needs xdg-desktop-portal
|
|
299
|
+
/// >= 1.19.4: older ones raise `RuntimeError`, and the caller carries on
|
|
300
|
+
/// unregistered. A sandboxed process returns at once.
|
|
301
|
+
#[pyfunction]
|
|
302
|
+
pub(crate) fn register_host_app(py: Python<'_>, app_id: &str) -> PyResult<()> {
|
|
303
|
+
{
|
|
304
|
+
let done = lock(®ISTERED);
|
|
305
|
+
if let Some(prev) = done.as_deref() {
|
|
306
|
+
if prev == app_id {
|
|
307
|
+
return Ok(());
|
|
308
|
+
}
|
|
309
|
+
return Err(PyValueError::new_err(format!(
|
|
310
|
+
"already registered as {prev:?}"
|
|
311
|
+
)));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
let id = ashpd::AppID::try_from(app_id)
|
|
315
|
+
.map_err(|e| PyValueError::new_err(format!("invalid app id {app_id:?}: {e}")))?;
|
|
316
|
+
|
|
317
|
+
let rt = runtime()?;
|
|
318
|
+
py.allow_threads(|| {
|
|
319
|
+
rt.block_on(async {
|
|
320
|
+
tokio::time::timeout(REGISTER_TIMEOUT, ashpd::register_host_app(id)).await
|
|
321
|
+
})
|
|
322
|
+
})
|
|
323
|
+
.map_err(|_| PyTimeoutError::new_err("Registry.Register did not answer"))?
|
|
324
|
+
.map_err(|e| PyRuntimeError::new_err(format!("Registry.Register: {e}")))?;
|
|
325
|
+
|
|
326
|
+
*lock(®ISTERED) = Some(app_id.to_owned());
|
|
327
|
+
Ok(())
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
#[cfg(test)]
|
|
331
|
+
mod tests {
|
|
332
|
+
use super::*;
|
|
333
|
+
|
|
334
|
+
#[test]
|
|
335
|
+
fn a_portal_response_is_a_denial() {
|
|
336
|
+
let e = ashpd::Error::Response(ashpd::desktop::ResponseError::Cancelled);
|
|
337
|
+
assert!(matches!(classify("start", e), RdFailure::Denied(m) if m.starts_with("start: ")));
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#[test]
|
|
341
|
+
fn anything_else_is_a_failure() {
|
|
342
|
+
let e = ashpd::Error::RequiresVersion(2, 1);
|
|
343
|
+
assert!(matches!(
|
|
344
|
+
classify("connect_to_eis", e),
|
|
345
|
+
RdFailure::Failed(_)
|
|
346
|
+
));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
#[test]
|
|
350
|
+
fn a_budget_timeout_is_a_failure_not_a_denial() {
|
|
351
|
+
let rt = tokio::runtime::Builder::new_current_thread()
|
|
352
|
+
.enable_time()
|
|
353
|
+
.start_paused(true)
|
|
354
|
+
.build()
|
|
355
|
+
.unwrap();
|
|
356
|
+
let err = rt.block_on(async {
|
|
357
|
+
SetupBudget::new(Some(Duration::ZERO))
|
|
358
|
+
.guard("start", async {
|
|
359
|
+
std::future::pending::<Result<(), RdFailure>>().await
|
|
360
|
+
})
|
|
361
|
+
.await
|
|
362
|
+
.unwrap_err()
|
|
363
|
+
});
|
|
364
|
+
assert!(matches!(err, RdFailure::Failed(m) if m.contains("setup deadline")));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
#[test]
|
|
368
|
+
fn consent_wait_is_not_capped_at_the_input_capture_ceiling() {
|
|
369
|
+
let d = deadline_capped(Some(300.0), MAX_CONSENT_SECS).unwrap();
|
|
370
|
+
assert_eq!(d, Some(Duration::from_secs(300)));
|
|
371
|
+
}
|
|
372
|
+
}
|
|
@@ -8,7 +8,11 @@ import pytest
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
try:
|
|
11
|
-
from pyinputcapture import
|
|
11
|
+
from pyinputcapture import (
|
|
12
|
+
InputCapturePortal,
|
|
13
|
+
RemoteDesktopPortal,
|
|
14
|
+
register_host_app,
|
|
15
|
+
)
|
|
12
16
|
|
|
13
17
|
HAS_MODULE = True
|
|
14
18
|
except ImportError:
|
|
@@ -324,3 +328,19 @@ class TestReleaseSignature:
|
|
|
324
328
|
portal = InputCapturePortal()
|
|
325
329
|
with pytest.raises(RuntimeError, match="not set up"):
|
|
326
330
|
portal.release(cursor_x=1.0)
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
class TestRemoteDesktopWithoutPortal:
|
|
334
|
+
def test_create_and_close(self):
|
|
335
|
+
rd = RemoteDesktopPortal()
|
|
336
|
+
assert rd.last_error is None
|
|
337
|
+
rd.close()
|
|
338
|
+
rd.close()
|
|
339
|
+
|
|
340
|
+
def test_zero_timeout_is_rejected(self):
|
|
341
|
+
with pytest.raises(ValueError):
|
|
342
|
+
RemoteDesktopPortal().setup(timeout=0)
|
|
343
|
+
|
|
344
|
+
def test_invalid_app_id_is_rejected_before_any_dbus_call(self):
|
|
345
|
+
with pytest.raises(ValueError, match="invalid app id"):
|
|
346
|
+
register_host_app("not an app id")
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
name: Release
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches: [main]
|
|
6
|
-
|
|
7
|
-
permissions:
|
|
8
|
-
contents: write
|
|
9
|
-
pull-requests: write
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
release-please:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
outputs:
|
|
15
|
-
release_created: ${{ steps.release.outputs.release_created }}
|
|
16
|
-
steps:
|
|
17
|
-
- uses: googleapis/release-please-action@v4
|
|
18
|
-
id: release
|
|
19
|
-
with:
|
|
20
|
-
# Cargo.toml and python/pyinputcapture/__init__.py are kept in sync
|
|
21
|
-
# via "extra-files" in release-please-config.json.
|
|
22
|
-
config-file: release-please-config.json
|
|
23
|
-
manifest-file: .release-please-manifest.json
|
|
24
|
-
|
|
25
|
-
build:
|
|
26
|
-
needs: release-please
|
|
27
|
-
if: needs.release-please.outputs.release_created
|
|
28
|
-
uses: ./.github/workflows/ci.yml
|
|
29
|
-
with:
|
|
30
|
-
release: true
|
|
31
|
-
|
|
32
|
-
publish:
|
|
33
|
-
needs: [release-please, build]
|
|
34
|
-
if: needs.release-please.outputs.release_created
|
|
35
|
-
runs-on: ubuntu-latest
|
|
36
|
-
steps:
|
|
37
|
-
- uses: actions/checkout@v4
|
|
38
|
-
|
|
39
|
-
- uses: actions/download-artifact@v4
|
|
40
|
-
with:
|
|
41
|
-
pattern: wheels-*
|
|
42
|
-
merge-multiple: true
|
|
43
|
-
path: dist
|
|
44
|
-
github-token: ${{ github.token }}
|
|
45
|
-
run-id: ${{ needs.build.outputs.run_id }}
|
|
46
|
-
|
|
47
|
-
- name: Build sdist
|
|
48
|
-
uses: PyO3/maturin-action@v1
|
|
49
|
-
with:
|
|
50
|
-
command: sdist
|
|
51
|
-
args: --out dist
|
|
52
|
-
|
|
53
|
-
- name: Publish to PyPI
|
|
54
|
-
uses: PyO3/maturin-action@v1
|
|
55
|
-
env:
|
|
56
|
-
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
57
|
-
with:
|
|
58
|
-
command: upload
|
|
59
|
-
args: --non-interactive --skip-existing dist/*
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|