-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-mapper.js
More file actions
285 lines (247 loc) · 7.77 KB
/
input-mapper.js
File metadata and controls
285 lines (247 loc) · 7.77 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use strict';
// ─────────────────────────────────────────────────────────────
// InputMapper — unified mapping from input sources (keyboard,
// MIDI, Stream Deck, scenes) to abstract actions.
//
// Usage:
// const mapper = new InputMapper(store);
// mapper.registerAction('display.toggleGrid', () => { … });
// mapper.bindKey('g', 'display.toggleGrid');
// mapper.bindMidi(0, 64, 'display.toggleGrid'); // CC 64 on ch 0
// mapper.trigger('display.toggleGrid'); // manual trigger
//
// The mapper decouples input sources from actions. Keyboard
// shortcuts, MIDI CCs, and scene triggers all go through the
// same action registry. Mappings are user-configurable and
// persisted to localStorage.
// ─────────────────────────────────────────────────────────────
const STORAGE_KEY = 'osc_inputMap';
export class InputMapper {
constructor(store) {
this.store = store;
/** @type {Map<string, Function>} action name → handler */
this._actions = new Map();
/** @type {Map<string, string>} key (lowercase) → action name */
this._keyMap = new Map();
/** @type {Map<string, string>} "ch:cc" → action name */
this._midiMap = new Map();
/** @type {boolean} whether keyboard listener is active */
this._kbActive = false;
/** @type {MIDIAccess|null} */
this._midiAccess = null;
this._loadMappings();
}
// ── Action registry ───────────────────────────────────────
/**
* Register a named action with its handler.
* @param {string} name – dot-separated action path (e.g. 'display.toggleGrid')
* @param {Function} fn – handler to execute
*/
registerAction(name, fn) {
this._actions.set(name, fn);
}
/**
* Register multiple actions at once.
* @param {Object} map – { 'action.name': handler, … }
*/
registerActions(map) {
for (const [name, fn] of Object.entries(map)) {
this._actions.set(name, fn);
}
}
/**
* Execute an action by name.
* @param {string} name
* @param {any} [value] – optional value (e.g. MIDI velocity 0-127)
*/
trigger(name, value) {
const fn = this._actions.get(name);
if (fn) fn(value);
}
/**
* Get all registered action names.
* @returns {string[]}
*/
getActions() {
return Array.from(this._actions.keys());
}
// ── Keyboard mapping ──────────────────────────────────────
/**
* Bind a keyboard key to an action.
* @param {string} key – key value (e.g. 'g', ' ', 'F11')
* @param {string} action – action name
*/
bindKey(key, action) {
this._keyMap.set(key.toLowerCase(), action);
this._saveMappings();
}
/**
* Remove a keyboard binding.
*/
unbindKey(key) {
this._keyMap.delete(key.toLowerCase());
this._saveMappings();
}
/**
* Get the action bound to a key.
*/
getKeyAction(key) {
return this._keyMap.get(key.toLowerCase());
}
/**
* Get all key bindings as { key: action } object.
*/
getKeyBindings() {
const out = {};
for (const [k, v] of this._keyMap) out[k] = v;
return out;
}
/**
* Install the global keyboard listener.
*/
enableKeyboard() {
if (this._kbActive) return;
this._kbActive = true;
this._kbHandler = ev => {
const tag = ev.target.tagName;
if (tag === 'INPUT' || tag === 'SELECT' || tag === 'TEXTAREA') return;
const key = ev.key.toLowerCase();
const action = this._keyMap.get(key);
if (action) {
ev.preventDefault();
this.trigger(action);
}
};
document.addEventListener('keydown', this._kbHandler);
}
/**
* Remove the global keyboard listener.
*/
disableKeyboard() {
if (!this._kbActive) return;
this._kbActive = false;
document.removeEventListener('keydown', this._kbHandler);
this._kbHandler = null;
}
// ── MIDI mapping ──────────────────────────────────────────
/**
* Bind a MIDI CC to an action.
* @param {number} channel – MIDI channel (0-15)
* @param {number} cc – CC number (0-127)
* @param {string} action – action name
*/
bindMidi(channel, cc, action) {
this._midiMap.set(`${channel}:${cc}`, action);
this._saveMappings();
}
/**
* Remove a MIDI CC binding.
*/
unbindMidi(channel, cc) {
this._midiMap.delete(`${channel}:${cc}`);
this._saveMappings();
}
/**
* Get all MIDI bindings as { "ch:cc": action } object.
*/
getMidiBindings() {
const out = {};
for (const [k, v] of this._midiMap) out[k] = v;
return out;
}
/**
* Initialize Web MIDI access (if available).
* Call this after user gesture.
*/
async enableMidi() {
if (this._midiAccess) return;
if (!navigator.requestMIDIAccess) return;
try {
this._midiAccess = await navigator.requestMIDIAccess();
this._midiAccess.inputs.forEach(input => this._connectMidiInput(input));
this._midiAccess.addEventListener('statechange', e => {
if (e.port.type === 'input' && e.port.state === 'connected') {
this._connectMidiInput(e.port);
}
});
} catch (err) {
console.warn('MIDI access denied:', err);
}
}
_connectMidiInput(input) {
input.onmidimessage = ev => {
const [status, cc, value] = ev.data;
// CC message: 0xB0-0xBF
if ((status & 0xF0) === 0xB0) {
const channel = status & 0x0F;
const key = `${channel}:${cc}`;
const action = this._midiMap.get(key);
if (action) this.trigger(action, value);
}
};
}
// ── Persistence ───────────────────────────────────────────
_saveMappings() {
const data = {
keys: Object.fromEntries(this._keyMap),
midi: Object.fromEntries(this._midiMap),
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
}
_loadMappings() {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return;
const data = JSON.parse(raw);
if (data.keys) {
for (const [k, v] of Object.entries(data.keys)) {
this._keyMap.set(k, v);
}
}
if (data.midi) {
for (const [k, v] of Object.entries(data.midi)) {
this._midiMap.set(k, v);
}
}
} catch (_) {}
}
/**
* Install default keyboard bindings (used on first run
* or when user resets to defaults).
*/
installDefaults() {
// Only install if no custom mappings exist
if (this._keyMap.size > 0) return;
const defaults = {
' ': 'playback.toggle',
'escape': 'playback.stop',
'g': 'display.toggleGrid',
'c': 'display.toggleCRT',
'm': 'display.toggleMeasure',
'f': 'display.toggleFullscreen',
'f11': 'display.toggleFullscreen',
'1': 'scope.modeYT',
'2': 'scope.modeXY',
'4': 'scope.modeVS',
'5': 'scope.modeFS',
'r': 'scope.runStop',
's': 'scope.single',
'3': 'scene.toggle',
'tab': 'scene.switchMode',
'?': 'help.toggle',
};
for (const [key, action] of Object.entries(defaults)) {
this._keyMap.set(key, action);
}
this._saveMappings();
}
/**
* Reset all mappings to defaults.
*/
resetToDefaults() {
this._keyMap.clear();
this._midiMap.clear();
localStorage.removeItem(STORAGE_KEY);
this.installDefaults();
}
}