跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

linux-desktop-visibility.py

96 行 · 4.3 KB历史下载
1import fcntl
2import json
3import os
4import sys
5import tempfile
6from pathlib import Path
7
8def owner(window, pid_atom):
9 from Xlib import X
10 prop = window.get_full_property(pid_atom, X.AnyPropertyType)
11 if prop is None or len(prop.value) != 1:
12 return None
13 pid = int(prop.value[0])
14 try:
15 if Path(f'/proc/{pid}/exe').resolve() != Path('/usr/lib/claude-desktop/claude-desktop'):
16 return None
17 fields = Path(f'/proc/{pid}/stat').read_text().split(') ', 1)[1].split()
18 return [pid, fields[19]]
19 except (OSError, ValueError):
20 return None
21
22
23def visibility(show):
24 runtime = Path('/run/cage-desktop')
25 if not runtime.is_dir():
26 return
27 # Startup and legacy unmanaged sessions must remain viewable before the
28 # managed session is ready. Previously hidden windows can always restore.
29 if not (runtime / 'keyring-ready').exists() and not (runtime / 'hidden-windows.json').exists():
30 return
31 from Xlib import X, display, error, protocol
32 os.environ.update(DISPLAY=':78', XAUTHORITY=str(runtime / 'Xauthority'))
33 os.umask(0o077)
34 with (runtime / 'visibility.lock').open('a') as lock:
35 fcntl.flock(lock, fcntl.LOCK_EX)
36 connection = display.Display()
37 try:
38 root = connection.screen().root
39 pid_atom = connection.intern_atom('_NET_WM_PID')
40 saved_file = runtime / 'hidden-windows.json'
41 saved = json.loads(saved_file.read_text()) if saved_file.exists() else {'windows': {}, 'active': 0}
42 if show:
43 mapped = []
44 for key, identity in saved['windows'].items():
45 window = connection.create_resource_object('window', int(key))
46 try:
47 if owner(window, pid_atom) != identity:
48 continue
49 window.map()
50 mapped.append(window.id)
51 except error.XError:
52 continue
53 connection.sync()
54 target = saved['active'] if saved['active'] in mapped else (mapped[-1] if mapped else 0)
55 if target:
56 root.send_event(protocol.event.ClientMessage(window=target, client_type=connection.intern_atom('_NET_ACTIVE_WINDOW'),
57 data=(32, [2, X.CurrentTime, 0, 0, 0])),
58 event_mask=X.SubstructureRedirectMask | X.SubstructureNotifyMask)
59 connection.sync()
60 saved_file.unlink(missing_ok=True)
61 return
62 clients = root.get_full_property(connection.intern_atom('_NET_CLIENT_LIST'), X.AnyPropertyType)
63 active = root.get_full_property(connection.intern_atom('_NET_ACTIVE_WINDOW'), X.AnyPropertyType)
64 windows = []
65 for wid in clients.value if clients is not None else []:
66 window = connection.create_resource_object('window', int(wid))
67 try:
68 identity = owner(window, pid_atom)
69 if identity is None or window.get_attributes().map_state != X.IsViewable:
70 continue
71 saved['windows'][str(window.id)] = identity
72 windows.append(window)
73 except error.XError:
74 continue
75 if not windows:
76 return
77 if active is not None and len(active.value) == 1 and str(int(active.value[0])) in saved['windows']:
78 saved['active'] = int(active.value[0])
79 # Save the owned windows before withdrawing them from the window manager.
80 # Their IDs disappear from _NET_CLIENT_LIST while they are hidden.
81 with tempfile.NamedTemporaryFile(mode='w', dir=runtime, prefix='hidden-windows-', delete=False) as pending:
82 json.dump(saved, pending)
83 Path(pending.name).replace(saved_file)
84 for window in windows:
85 try:
86 if owner(window, pid_atom) == saved['windows'][str(window.id)]:
87 window.unmap()
88 except error.XError:
89 continue
90 connection.sync()
91 finally:
92 connection.close()
93
94
95if __name__ == '__main__':
96 visibility(sys.argv[1] == 'true')