| 1 | import fcntl |
| 2 | import json |
| 3 | import os |
| 4 | import sys |
| 5 | import tempfile |
| 6 | from pathlib import Path |
| 7 | |
| 8 | def 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 | |
| 23 | def visibility(show): |
| 24 | runtime = Path('/run/cage-desktop') |
| 25 | if not runtime.is_dir(): |
| 26 | return |
| 27 | |
| 28 | |
| 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 | |
| 80 | |
| 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 | |
| 95 | if __name__ == '__main__': |
| 96 | visibility(sys.argv[1] == 'true') |