47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only Hyprland display inventory. No shell, privilege escalation or writes."""
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def normalize(monitors):
|
|
if not isinstance(monitors, list):
|
|
raise ValueError('Expected monitor array')
|
|
result = []
|
|
for monitor in monitors:
|
|
if not isinstance(monitor, dict) or not isinstance(monitor.get('name'), str):
|
|
continue
|
|
name = monitor['name']
|
|
if not name or len(name) > 128:
|
|
continue
|
|
result.append({
|
|
'name': name,
|
|
'description': str(monitor.get('description') or name)[:256],
|
|
'width': int(monitor.get('width') or 0),
|
|
'height': int(monitor.get('height') or 0),
|
|
'refreshRate': round(float(monitor.get('refreshRate') or 0), 2),
|
|
'scale': float(monitor.get('scale') or 1),
|
|
'transform': int(monitor.get('transform') or 0),
|
|
'focused': bool(monitor.get('focused', False)),
|
|
'x': int(monitor.get('x') or 0),
|
|
'y': int(monitor.get('y') or 0),
|
|
'availableModes': [str(mode)[:80] for mode in monitor.get('availableModes', []) if isinstance(mode, str)][:128],
|
|
})
|
|
return result
|
|
|
|
|
|
def main(args):
|
|
if args != ['list']:
|
|
raise ValueError('Only list is supported')
|
|
output = subprocess.run(['hyprctl', '-j', 'monitors', 'all'], capture_output=True, text=True, timeout=8, check=True)
|
|
return {'ok': True, 'monitors': normalize(json.loads(output.stdout))}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
print(json.dumps(main(sys.argv[1:]), ensure_ascii=False))
|
|
except (ValueError, OSError, subprocess.SubprocessError, json.JSONDecodeError) as error:
|
|
print(json.dumps({'ok': False, 'error': str(error)}, ensure_ascii=False))
|
|
sys.exit(1)
|