diff --git a/macropad_gui/__main__.py b/macropad_gui/__main__.py index baa4b2c..53dc5d4 100644 --- a/macropad_gui/__main__.py +++ b/macropad_gui/__main__.py @@ -35,8 +35,20 @@ def cmd_doctor(_): return 0 if d.ready else 1 +def _state_for(device): + """The shadow state of the pad that is plugged in, or the default one.""" + if device is None: + return core.State() + state = core.State(device_id=f"{device.vid}:{device.pid}") + if not state.path.exists(): + known = core.KNOWN_LAYOUTS.get((device.vid, device.pid)) + if known: + state.layout = known + return state + + def cmd_state(_): - s = core.State() + s = _state_for(core.diagnose(keyd=False).device) print(f"{DIM}{s.path}{'' if s.path.exists() else ' (not created yet)'}{O}\n") print(f"{DIM}layout: {s.layout.describe()}{O}\n") for c in s.layout.control_ids: @@ -77,9 +89,6 @@ def cmd_actions(a): def cmd_write(a): - if a.control not in core.CONTROL_IDS: - print(f"Unknown control. One of: {', '.join(core.CONTROL_IDS)}") - return 1 try: b = core.Binding(keys=a.keys or "", media=a.media or "", delay=a.delay).validate() @@ -91,7 +100,10 @@ def cmd_write(a): if not d.ready: return cmd_doctor(None) - state = core.State() + state = _state_for(d.device) + if a.control not in state.layout.control_ids: + print(f"Unknown control. One of: {', '.join(state.layout.control_ids)}") + return 1 try: core.write_binding(d.device, a.control, b, state) except core.WriteError as e: