-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlan_mouse.py
More file actions
229 lines (184 loc) · 6.92 KB
/
lan_mouse.py
File metadata and controls
229 lines (184 loc) · 6.92 KB
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
"""Shared helpers for interacting with the lan-mouse CLI from inside the flatpak sandbox."""
from __future__ import annotations
import functools
import re
import shlex
import shutil
import subprocess
import time
from pathlib import Path
from typing import TypedDict
# Default binary name (searched in PATH)
DEFAULT_BIN: str = "lan-mouse"
# Cache immutable runtime values (these never change while the process is alive)
_IN_FLATPAK: bool = Path("/.flatpak-info").is_file()
_HOME_DIR: Path = Path.home()
class Client(TypedDict):
"""A parsed lan-mouse client entry."""
id: int
host: str
port: int
position: str
active: bool
ips: list[str]
def _host_prefix() -> list[str]:
"""Return the flatpak-spawn --host prefix when running inside the Flatpak sandbox."""
return ["flatpak-spawn", "--host"] if _IN_FLATPAK else []
def _run(args: list[str], timeout: float = 5.0) -> subprocess.CompletedProcess[str]:
"""Run a host command and return the CompletedProcess."""
return subprocess.run(
_host_prefix() + args,
shell=False,
capture_output=True,
text=True,
timeout=timeout,
start_new_session=True,
cwd=_HOME_DIR,
)
def _bin(bin_path: str = "") -> str:
"""Return the lan-mouse binary path, defaulting to 'lan-mouse'."""
return bin_path.strip() or DEFAULT_BIN
def _has_command(name: str) -> bool:
"""Check if a command exists on the host."""
if not _IN_FLATPAK:
return shutil.which(name) is not None
try:
result = _run(["which", name])
return result.returncode == 0
except (subprocess.TimeoutExpired, OSError):
return False
def is_running(bin_path: str = "") -> bool:
"""Check if the lan-mouse process is running."""
try:
name = Path(_bin(bin_path)).name
result = _run(["pgrep", "-x", name])
return result.returncode == 0
except (subprocess.TimeoutExpired, OSError):
return False
# Example line: id 0: 192.168.10.54:4242 (left) active: true, ips: {192.168.10.54}
_CLIENT_RE = re.compile(
r"id\s+(?P<id>\d+):\s+"
r"(?P<host>[^:]+):(?P<port>\d+)\s+"
r"\((?P<position>\w+)\)\s+"
r"active:\s+(?P<active>true|false)"
r"(?:,\s+ips:\s+\{(?P<ips>[^}]*)\})?"
)
def _parse_clients(stdout: str) -> list[Client]:
"""Parse lan-mouse CLI output into a list of :class:`Client` dicts."""
clients: list[Client] = []
for line in stdout.splitlines():
m = _CLIENT_RE.search(line)
if m:
ips_raw: str = m.group("ips") or ""
clients.append(
Client(
id=int(m.group("id")),
host=m.group("host"),
port=int(m.group("port")),
position=m.group("position"),
active=m.group("active") == "true",
ips=[ip.strip() for ip in ips_raw.split(",") if ip.strip()],
)
)
return clients
def list_clients(bin_path: str = "") -> list[Client]:
"""Parse output of ``lan-mouse cli list`` into a list of :class:`Client` dicts.
Returns an empty list if lan-mouse is not running or the command fails.
"""
try:
result = _run([_bin(bin_path), "cli", "list"])
if result.returncode != 0:
return []
except (subprocess.TimeoutExpired, OSError):
return []
return _parse_clients(result.stdout)
class Status(TypedDict):
"""Snapshot of lan-mouse state from a single CLI call."""
running: bool
clients: list[Client]
def get_status(bin_path: str = "") -> Status:
"""Get running state and client list.
Requires BOTH pgrep and a successful CLI response to report running.
This prevents false positives from either source during process
startup/shutdown races (e.g. IPC socket cleanup, auto-restart delay).
"""
if not is_running(bin_path):
return Status(running=False, clients=[])
try:
result = _run([_bin(bin_path), "cli", "list"])
if result.returncode == 0:
return Status(running=True, clients=_parse_clients(result.stdout))
except (subprocess.TimeoutExpired, OSError):
pass
return Status(running=False, clients=[])
def activate(client_id: int, bin_path: str = "") -> bool:
"""Activate a client connection. Returns True on success."""
try:
result = _run([_bin(bin_path), "cli", "activate", str(client_id)])
return result.returncode == 0
except (subprocess.TimeoutExpired, OSError):
return False
def deactivate(client_id: int, bin_path: str = "") -> bool:
"""Deactivate a client connection. Returns True on success."""
try:
result = _run([_bin(bin_path), "cli", "deactivate", str(client_id)])
return result.returncode == 0
except (subprocess.TimeoutExpired, OSError):
return False
@functools.lru_cache(maxsize=4)
def _resolve_launch_cmd(bin_path: str = "") -> str:
"""Determine the best launch command for this system.
Prefers uwsm-app (systemd session integration) if available,
falls back to the bare binary. Result is cached per bin_path since
available commands don't change at runtime.
"""
b = _bin(bin_path)
if _has_command("uwsm-app"):
return f"uwsm-app -- {b}"
return b
def launch(command: str = "", bin_path: str = "") -> None:
"""Start the lan-mouse daemon (fire-and-forget).
Args:
command: Custom launch command. If empty, auto-detects the best method.
bin_path: Custom path to the lan-mouse binary.
"""
args = _host_prefix() + shlex.split(
command.strip() or _resolve_launch_cmd(bin_path)
)
subprocess.Popen(
args,
shell=False,
start_new_session=True,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
cwd=_HOME_DIR,
)
def wait_for_ready(bin_path: str = "", timeout: float = 5.0) -> bool:
"""Wait for lan-mouse to be running and its CLI to respond. Returns True if ready."""
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
if is_running(bin_path):
try:
result = _run([_bin(bin_path), "cli", "list"])
if result.returncode == 0:
return True
except (subprocess.TimeoutExpired, OSError):
pass
time.sleep(0.3)
return False
def kill(bin_path: str = "") -> bool:
"""Kill the lan-mouse daemon and wait for it to exit. Returns True on success."""
try:
name = Path(_bin(bin_path)).name
result = _run(["pkill", "-x", name])
if result.returncode != 0:
return False
for _ in range(20):
if not is_running(bin_path):
return True
_run(["pkill", "-x", name]) # re-kill in case of auto-restart
time.sleep(0.1)
return not is_running(bin_path)
except (subprocess.TimeoutExpired, OSError):
return False