-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_manager.py
More file actions
246 lines (190 loc) · 6.54 KB
/
config_manager.py
File metadata and controls
246 lines (190 loc) · 6.54 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
"""
config_manager.py
=================
Handles saving and loading user preferences.
Config is stored at: ~/.config/onscreen-keys/config.json
Schema:
{
"selected_keys": ["enter", "dot", "comma", ...],
"spawn_anchor": "center",
"positions": {
"enter": {"x": 100, "y": 200},
...
}
}
"""
import json
import os
import threading
import copy
# ─── Config paths ────────────────────────────────────────────────────
CONFIG_DIR = os.path.expanduser("~/.config/onscreen-keys")
CONFIG_FILE = os.path.join(CONFIG_DIR, "config.json")
# ─── Default empty config ────────────────────────────────────────────
DEFAULT_CONFIG = {
"selected_keys": [],
"spawn_anchor": "center",
"positions": {},
}
# Valid values for unsaved floating key spawn anchor.
VALID_SPAWN_ANCHORS = {
"center",
"top_left",
"top_right",
"bottom_left",
"bottom_right",
}
# ─── Debouncing and cache state ──────────────────────────────────────
_cached_config = None
_save_timer = None
_config_lock = threading.Lock()
def _ensure_config_dir():
"""Create the config directory if it doesn't exist."""
os.makedirs(CONFIG_DIR, exist_ok=True)
def load_config():
"""
Load config from disk or memory cache.
Returns the config dict. If the file doesn't exist or is corrupt,
returns a fresh default config without crashing.
"""
global _cached_config
with _config_lock:
if _cached_config is not None:
return copy.deepcopy(_cached_config)
# Fall through to load from disk without holding the lock
# (I/O operations don't require thread safety)
_cached_config = None
try:
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data.get("selected_keys"), list):
data["selected_keys"] = []
if data.get("spawn_anchor") not in VALID_SPAWN_ANCHORS:
data["spawn_anchor"] = DEFAULT_CONFIG["spawn_anchor"]
if not isinstance(data.get("positions"), dict):
data["positions"] = {}
with _config_lock:
_cached_config = data
return copy.deepcopy(data)
except (json.JSONDecodeError, IOError, OSError) as e:
print(f"[config_manager] Warning: Could not load config: {e}")
print("[config_manager] Using default config.")
with _config_lock:
_cached_config = dict(DEFAULT_CONFIG)
return copy.deepcopy(_cached_config)
def save_config(config, debounce=False):
"""
Save config to disk, with optional debouncing to avoid excessive I/O.
Args:
config: dict with "selected_keys" list and "positions" dict.
debounce: if True, delays writing to disk to batch changes.
"""
global _cached_config, _save_timer
with _config_lock:
_cached_config = copy.deepcopy(config)
if debounce:
if _save_timer is not None:
_save_timer.cancel()
_save_timer = threading.Timer(0.5, _write_to_disk)
_save_timer.daemon = True
_save_timer.start()
else:
if _save_timer is not None:
_save_timer.cancel()
_save_timer = None
_write_to_disk()
def _write_to_disk():
global _cached_config
config_to_save = None
with _config_lock:
if _cached_config is None:
return
config_to_save = copy.deepcopy(_cached_config)
_ensure_config_dir()
tmp_file = CONFIG_FILE + ".tmp"
try:
with open(tmp_file, "w", encoding="utf-8") as f:
json.dump(config_to_save, f, indent=2)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_file, CONFIG_FILE)
except (IOError, OSError) as e:
print(f"[config_manager] Error: Could not save config: {e}")
def flush_config():
"""Immediately write any pending debounced changes to disk."""
global _save_timer
if _save_timer is not None:
_save_timer.cancel()
_save_timer = None
_write_to_disk()
def save_selected_keys(key_ids):
"""
Convenience: update just the selected keys in the config.
Args:
key_ids: list of key ID strings.
"""
config = load_config()
config["selected_keys"] = list(key_ids)
save_config(config)
def save_selected_keys_and_spawn_anchor(key_ids, anchor):
"""
Convenience: update selected keys and spawn anchor in one write.
Args:
key_ids: list of key ID strings.
anchor: one of VALID_SPAWN_ANCHORS.
"""
if anchor not in VALID_SPAWN_ANCHORS:
anchor = DEFAULT_CONFIG["spawn_anchor"]
config = load_config()
config["selected_keys"] = list(key_ids)
config["spawn_anchor"] = anchor
save_config(config)
def save_spawn_anchor(anchor, clear_positions=False):
"""
Convenience: persist default spawn anchor for unsaved keys.
Args:
anchor: one of VALID_SPAWN_ANCHORS.
clear_positions: if True, discard dragged positions so the new
spawn setting applies on the next activation.
"""
if anchor not in VALID_SPAWN_ANCHORS:
anchor = DEFAULT_CONFIG["spawn_anchor"]
config = load_config()
config["spawn_anchor"] = anchor
if clear_positions:
config["positions"] = {}
save_config(config)
def get_spawn_anchor():
"""
Return persisted spawn anchor, falling back to default.
"""
config = load_config()
anchor = config.get("spawn_anchor")
if anchor not in VALID_SPAWN_ANCHORS:
return DEFAULT_CONFIG["spawn_anchor"]
return anchor
def save_key_position(key_id, x, y):
"""
Convenience: update the saved position for a single key button.
Args:
key_id: the key's unique ID string.
x: screen x coordinate.
y: screen y coordinate.
"""
config = load_config()
config["positions"][key_id] = {"x": x, "y": y}
save_config(config, debounce=True)
def get_saved_position(key_id):
"""
Get the saved position for a key button.
Args:
key_id: the key's unique ID.
Returns:
(x, y) tuple, or None if no saved position.
"""
config = load_config()
pos = config.get("positions", {}).get(key_id)
if pos and "x" in pos and "y" in pos:
return (pos["x"], pos["y"])
return None