forked from mathoudebine/turing-smart-screen-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmultiscreen.py
More file actions
247 lines (202 loc) · 6.73 KB
/
Copy pathmultiscreen.py
File metadata and controls
247 lines (202 loc) · 6.73 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
"""
Multi-screen manager for Turing Smart Screen.
Cycles through configured screens using keyboard shortcuts.
Screens and keybindings are configured in multiscreen.yaml.
Usage:
python3 multiscreen.py
"""
import os
import signal
import subprocess
import sys
import time
import threading
from pathlib import Path
import re
import yaml
from pynput import keyboard
MAIN_DIR = Path(__file__).resolve().parent
CONFIG_PATH = MAIN_DIR / "config.yaml"
MULTISCREEN_CONFIG_PATH = MAIN_DIR / "multiscreen.yaml"
PYTHON = sys.executable
current_process = None
current_index = 0
switch_lock = threading.Lock()
screens = []
keybindings = {}
def load_multiscreen_config():
global screens, keybindings
with open(MULTISCREEN_CONFIG_PATH) as f:
data = yaml.safe_load(f)
screens = data.get("screens", [])
raw_bindings = data.get("keybindings", {})
keybindings = {}
for action, combo_str in raw_bindings.items():
keys = parse_combo(combo_str)
if keys:
keybindings[action] = keys
if not screens:
print("[multiscreen] ERROR: No screens defined in multiscreen.yaml")
sys.exit(1)
def parse_combo(combo_str: str) -> frozenset:
parts = [p.strip().lower() for p in combo_str.split("+")]
key_set = set()
for p in parts:
mapped = KEY_MAP.get(p)
if mapped:
key_set.add(mapped)
else:
try:
key_set.add(keyboard.KeyCode.from_char(p))
except Exception:
print(f"[multiscreen] WARNING: Unknown key '{p}' in combo '{combo_str}'")
return frozenset(key_set)
KEY_MAP = {
"ctrl": "ctrl",
"ctrl_l": "ctrl",
"ctrl_r": "ctrl",
"shift": "shift",
"shift_l": "shift",
"shift_r": "shift",
"alt": "alt",
"alt_l": "alt",
"alt_r": "alt",
"home": keyboard.Key.home,
"end": keyboard.Key.end,
"page_up": keyboard.Key.page_up,
"page_down": keyboard.Key.page_down,
"insert": keyboard.Key.insert,
"delete": keyboard.Key.delete,
"f1": keyboard.Key.f1,
"f2": keyboard.Key.f2,
"f3": keyboard.Key.f3,
"f4": keyboard.Key.f4,
"f5": keyboard.Key.f5,
"f6": keyboard.Key.f6,
"f7": keyboard.Key.f7,
"f8": keyboard.Key.f8,
"f9": keyboard.Key.f9,
"f10": keyboard.Key.f10,
"f11": keyboard.Key.f11,
"f12": keyboard.Key.f12,
"up": keyboard.Key.up,
"down": keyboard.Key.down,
"left": keyboard.Key.left,
"right": keyboard.Key.right,
"space": keyboard.Key.space,
"tab": keyboard.Key.tab,
"enter": keyboard.Key.enter,
"esc": keyboard.Key.esc,
"escape": keyboard.Key.esc,
}
def normalize_key(key):
if key in (keyboard.Key.ctrl_l, keyboard.Key.ctrl_r):
return "ctrl"
if key in (keyboard.Key.shift_l, keyboard.Key.shift_r):
return "shift"
if key in (keyboard.Key.alt_l, keyboard.Key.alt_r, keyboard.Key.alt_gr):
return "alt"
return key
def set_theme(theme_name: str):
with open(CONFIG_PATH) as f:
content = f.read()
content = re.sub(r"(THEME:\s*)(\S+)", rf"\g<1>{theme_name}", content, count=1)
with open(CONFIG_PATH, "w") as f:
f.write(content)
def start_monitor(index: int):
global current_process, current_index
screen = screens[index]
theme = screen["theme"]
name = screen.get("name", theme)
set_theme(theme)
current_index = index
env = os.environ.copy()
current_process = subprocess.Popen(
[PYTHON, str(MAIN_DIR / "main.py")],
cwd=str(MAIN_DIR),
env=env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
print(f"[multiscreen] Started: {name} (PID {current_process.pid})")
def stop_monitor():
global current_process
if current_process and current_process.poll() is None:
current_process.send_signal(signal.SIGTERM)
try:
current_process.wait(timeout=8)
except subprocess.TimeoutExpired:
current_process.kill()
current_process.wait()
print(f"[multiscreen] Stopped PID {current_process.pid}")
current_process = None
def switch_to(index: int):
with switch_lock:
if index == current_index and current_process and current_process.poll() is None:
return
index = index % len(screens)
name = screens[index].get("name", screens[index]["theme"])
print(f"[multiscreen] Switching to: {name}")
stop_monitor()
time.sleep(1)
start_monitor(index)
pressed_keys = set()
def get_pressed_normalized():
return frozenset(normalize_key(k) for k in pressed_keys)
def on_press(key):
pressed_keys.add(key)
current = get_pressed_normalized()
for action, combo in keybindings.items():
if combo.issubset(current):
if action == "next":
target = (current_index + 1) % len(screens)
threading.Thread(target=switch_to, args=(target,), daemon=True).start()
elif action == "prev":
target = (current_index - 1) % len(screens)
threading.Thread(target=switch_to, args=(target,), daemon=True).start()
elif action.startswith("screen_"):
try:
idx = int(action.split("_", 1)[1])
if 0 <= idx < len(screens):
threading.Thread(target=switch_to, args=(idx,), daemon=True).start()
except ValueError:
pass
break
def on_release(key):
pressed_keys.discard(key)
def main():
load_multiscreen_config()
print("[multiscreen] Turing Smart Screen - Multi-screen Manager")
print(f"[multiscreen] {len(screens)} screen(s) configured:")
for i, s in enumerate(screens):
print(f" [{i}] {s.get('name', s['theme'])} (theme: {s['theme']})")
print()
print("[multiscreen] Keybindings:")
for action, combo_str in keybindings.items():
raw = None
with open(MULTISCREEN_CONFIG_PATH) as f:
raw_data = yaml.safe_load(f)
raw = raw_data.get("keybindings", {}).get(action, "")
print(f" {action}: {raw}")
print()
print("[multiscreen] CTRL+C to exit")
print()
start_monitor(0)
listener = keyboard.Listener(on_press=on_press, on_release=on_release)
listener.start()
try:
while True:
if current_process and current_process.poll() is not None:
print("[multiscreen] Monitor process died, restarting...")
time.sleep(2)
start_monitor(current_index)
time.sleep(1)
except KeyboardInterrupt:
print("\n[multiscreen] Shutting down...")
stop_monitor()
set_theme(screens[0]["theme"])
listener.stop()
print("[multiscreen] Bye!")
if __name__ == "__main__":
main()