-
Notifications
You must be signed in to change notification settings - Fork 17
/
keyboard.go
425 lines (368 loc) · 10 KB
/
keyboard.go
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Copyright (c) 2010 Andrea Fazzi
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package spectrum
import (
"sync"
"time"
)
type rowState struct {
row, state byte
}
type Cmd_KeyPress struct {
logicalKeyCode uint
done chan bool
}
type Cmd_SendLoad struct {
romType RomType
}
type Keyboard struct {
speccy *Spectrum48k
keyStates [8]byte
mutex sync.RWMutex
CommandChannel chan interface{}
}
func NewKeyboard() *Keyboard {
keyboard := &Keyboard{}
keyboard.reset()
keyboard.CommandChannel = make(chan interface{})
return keyboard
}
func (keyboard *Keyboard) init(speccy *Spectrum48k) {
keyboard.speccy = speccy
go keyboard.commandLoop()
}
func (keyboard *Keyboard) delayAfterKeyDown() {
// Sleep for 1 frame
time.Sleep(1e9 / time.Duration(keyboard.speccy.GetCurrentFPS()))
}
func (keyboard *Keyboard) delayAfterKeyUp() {
// Sleep for 10 frames
time.Sleep(10 * 1e9 / time.Duration(keyboard.speccy.GetCurrentFPS()))
}
func (keyboard *Keyboard) commandLoop() {
evtLoop := keyboard.speccy.app.NewEventLoop()
for {
select {
case <-evtLoop.Pause:
evtLoop.Pause <- 0
case <-evtLoop.Terminate:
// Terminate this Go routine
if evtLoop.App().Verbose {
evtLoop.App().PrintfMsg("keyboard command loop: exit")
}
evtLoop.Terminate <- 0
return
case untyped_cmd := <-keyboard.CommandChannel:
switch cmd := untyped_cmd.(type) {
case Cmd_KeyPress:
keyboard.KeyDown(cmd.logicalKeyCode)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(cmd.logicalKeyCode)
keyboard.delayAfterKeyUp()
cmd.done <- true
case Cmd_SendLoad:
if cmd.romType == ROM_OPENSE {
// Sleep for 30 frames
time.Sleep(30e9 / time.Duration(keyboard.speccy.GetCurrentFPS()))
// l o a d
for _, keycode := range []uint{KEY_L, KEY_O, KEY_A, KEY_D} {
keyboard.KeyDown(keycode)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(keycode)
keyboard.delayAfterKeyUp()
}
// " "
keyboard.KeyDown(KEY_SymbolShift)
{
keyboard.KeyDown(KEY_P)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_P)
keyboard.delayAfterKeyUp()
keyboard.KeyDown(KEY_P)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_P)
keyboard.delayAfterKeyUp()
}
keyboard.KeyUp(KEY_SymbolShift)
keyboard.KeyDown(KEY_Enter)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_Enter)
} else {
// LOAD
keyboard.KeyDown(KEY_J)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_J)
keyboard.delayAfterKeyUp()
// " "
keyboard.KeyDown(KEY_SymbolShift)
{
keyboard.KeyDown(KEY_P)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_P)
keyboard.delayAfterKeyUp()
keyboard.KeyDown(KEY_P)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_P)
keyboard.delayAfterKeyUp()
}
keyboard.KeyUp(KEY_SymbolShift)
keyboard.KeyDown(KEY_Enter)
keyboard.delayAfterKeyDown()
keyboard.KeyUp(KEY_Enter)
}
}
}
}
}
func (k *Keyboard) reset() {
// Initialize 'k.keyStates'
for row := uint(0); row < 8; row++ {
k.SetKeyState(row, 0xff)
}
}
func (keyboard *Keyboard) GetKeyState(row uint) byte {
keyboard.mutex.RLock()
keyState := keyboard.keyStates[row]
keyboard.mutex.RUnlock()
return keyState
}
func (keyboard *Keyboard) SetKeyState(row uint, state byte) {
keyboard.mutex.Lock()
keyboard.keyStates[row] = state
keyboard.mutex.Unlock()
}
func (keyboard *Keyboard) KeyDown(logicalKeyCode uint) {
keyCode, ok := keyCodes[logicalKeyCode]
if ok {
keyboard.mutex.Lock()
keyboard.keyStates[keyCode.row] &= ^(keyCode.mask)
keyboard.mutex.Unlock()
}
}
func (keyboard *Keyboard) KeyUp(logicalKeyCode uint) {
keyCode, ok := keyCodes[logicalKeyCode]
if ok {
keyboard.mutex.Lock()
keyboard.keyStates[keyCode.row] |= (keyCode.mask)
keyboard.mutex.Unlock()
}
}
func (keyboard *Keyboard) KeyPress(logicalKeyCode uint) chan bool {
done := make(chan bool)
keyboard.CommandChannel <- Cmd_KeyPress{logicalKeyCode, done}
return done
}
func (keyboard *Keyboard) KeyPressSequence(logicalKeyCodes ...uint) chan bool {
done := make(chan bool, len(logicalKeyCodes))
for _, keyCode := range logicalKeyCodes {
keyboard.CommandChannel <- Cmd_KeyPress{keyCode, done}
}
return done
}
// Logical key codes
const (
KEY_1 = iota
KEY_2
KEY_3
KEY_4
KEY_5
KEY_6
KEY_7
KEY_8
KEY_9
KEY_0
KEY_Q
KEY_W
KEY_E
KEY_R
KEY_T
KEY_Y
KEY_U
KEY_I
KEY_O
KEY_P
KEY_A
KEY_S
KEY_D
KEY_F
KEY_G
KEY_H
KEY_J
KEY_K
KEY_L
KEY_Enter
KEY_CapsShift
KEY_Z
KEY_X
KEY_C
KEY_V
KEY_B
KEY_N
KEY_M
KEY_SymbolShift
KEY_Space
)
type keyCell struct {
row, mask byte
}
var keyCodes = map[uint]keyCell{
KEY_1: keyCell{row: 3, mask: 0x01},
KEY_2: keyCell{row: 3, mask: 0x02},
KEY_3: keyCell{row: 3, mask: 0x04},
KEY_4: keyCell{row: 3, mask: 0x08},
KEY_5: keyCell{row: 3, mask: 0x10},
KEY_6: keyCell{row: 4, mask: 0x10},
KEY_7: keyCell{row: 4, mask: 0x08},
KEY_8: keyCell{row: 4, mask: 0x04},
KEY_9: keyCell{row: 4, mask: 0x02},
KEY_0: keyCell{row: 4, mask: 0x01},
KEY_Q: keyCell{row: 2, mask: 0x01},
KEY_W: keyCell{row: 2, mask: 0x02},
KEY_E: keyCell{row: 2, mask: 0x04},
KEY_R: keyCell{row: 2, mask: 0x08},
KEY_T: keyCell{row: 2, mask: 0x10},
KEY_Y: keyCell{row: 5, mask: 0x10},
KEY_U: keyCell{row: 5, mask: 0x08},
KEY_I: keyCell{row: 5, mask: 0x04},
KEY_O: keyCell{row: 5, mask: 0x02},
KEY_P: keyCell{row: 5, mask: 0x01},
KEY_A: keyCell{row: 1, mask: 0x01},
KEY_S: keyCell{row: 1, mask: 0x02},
KEY_D: keyCell{row: 1, mask: 0x04},
KEY_F: keyCell{row: 1, mask: 0x08},
KEY_G: keyCell{row: 1, mask: 0x10},
KEY_H: keyCell{row: 6, mask: 0x10},
KEY_J: keyCell{row: 6, mask: 0x08},
KEY_K: keyCell{row: 6, mask: 0x04},
KEY_L: keyCell{row: 6, mask: 0x02},
KEY_Enter: keyCell{row: 6, mask: 0x01},
KEY_CapsShift: keyCell{row: 0, mask: 0x01},
KEY_Z: keyCell{row: 0, mask: 0x02},
KEY_X: keyCell{row: 0, mask: 0x04},
KEY_C: keyCell{row: 0, mask: 0x08},
KEY_V: keyCell{row: 0, mask: 0x10},
KEY_B: keyCell{row: 7, mask: 0x10},
KEY_N: keyCell{row: 7, mask: 0x08},
KEY_M: keyCell{row: 7, mask: 0x04},
KEY_SymbolShift: keyCell{row: 7, mask: 0x02},
KEY_Space: keyCell{row: 7, mask: 0x01},
}
var SDL_KeyMap = map[string][]uint{
"0": []uint{KEY_0},
"1": []uint{KEY_1},
"2": []uint{KEY_2},
"3": []uint{KEY_3},
"4": []uint{KEY_4},
"5": []uint{KEY_5},
"6": []uint{KEY_6},
"7": []uint{KEY_7},
"8": []uint{KEY_8},
"9": []uint{KEY_9},
"a": []uint{KEY_A},
"b": []uint{KEY_B},
"c": []uint{KEY_C},
"d": []uint{KEY_D},
"e": []uint{KEY_E},
"f": []uint{KEY_F},
"g": []uint{KEY_G},
"h": []uint{KEY_H},
"i": []uint{KEY_I},
"j": []uint{KEY_J},
"k": []uint{KEY_K},
"l": []uint{KEY_L},
"m": []uint{KEY_M},
"n": []uint{KEY_N},
"o": []uint{KEY_O},
"p": []uint{KEY_P},
"q": []uint{KEY_Q},
"r": []uint{KEY_R},
"s": []uint{KEY_S},
"t": []uint{KEY_T},
"u": []uint{KEY_U},
"v": []uint{KEY_V},
"w": []uint{KEY_W},
"x": []uint{KEY_X},
"y": []uint{KEY_Y},
"z": []uint{KEY_Z},
"return": []uint{KEY_Enter},
"space": []uint{KEY_Space},
"left shift": []uint{KEY_CapsShift},
"right shift": []uint{KEY_CapsShift},
"left ctrl": []uint{KEY_SymbolShift},
"right ctrl": []uint{KEY_SymbolShift},
//"escape": []uint{KEY_CapsShift, KEY_1},
//"caps lock": []uint{KEY_CapsShift, KEY_2}, // FIXME: SDL never sends the sdl.KEYUP event
"left": []uint{KEY_CapsShift, KEY_5},
"down": []uint{KEY_CapsShift, KEY_6},
"up": []uint{KEY_CapsShift, KEY_7},
"right": []uint{KEY_CapsShift, KEY_8},
"backspace": []uint{KEY_CapsShift, KEY_0},
"-": []uint{KEY_SymbolShift, KEY_J},
//"_": []uint{KEY_SymbolShift, KEY_0},
"=": []uint{KEY_SymbolShift, KEY_L},
//"+": []uint{KEY_SymbolShift, KEY_K},
"[": []uint{KEY_SymbolShift, KEY_8}, // Maps to "("
"]": []uint{KEY_SymbolShift, KEY_9}, // Maps to ")"
";": []uint{KEY_SymbolShift, KEY_O},
//":": []uint{KEY_SymbolShift, KEY_Z},
"'": []uint{KEY_SymbolShift, KEY_7},
//"\"": []uint{KEY_SymbolShift, KEY_P},
",": []uint{KEY_SymbolShift, KEY_N},
".": []uint{KEY_SymbolShift, KEY_M},
"/": []uint{KEY_SymbolShift, KEY_V},
//"<": []uint{KEY_SymbolShift, KEY_R},
//">": []uint{KEY_SymbolShift, KEY_T},
//"?": []uint{KEY_SymbolShift, KEY_C},
// Keypad
"[0]": []uint{KEY_0},
"[1]": []uint{KEY_1},
"[2]": []uint{KEY_2},
"[3]": []uint{KEY_3},
"[4]": []uint{KEY_4},
"[5]": []uint{KEY_5},
"[6]": []uint{KEY_6},
"[7]": []uint{KEY_7},
"[8]": []uint{KEY_8},
"[9]": []uint{KEY_9},
"[*]": []uint{KEY_SymbolShift, KEY_B},
"[-]": []uint{KEY_SymbolShift, KEY_J},
"[+]": []uint{KEY_SymbolShift, KEY_K},
"[/]": []uint{KEY_SymbolShift, KEY_V},
}
func init() {
if len(keyCodes) != 40 {
panic("invalid keyboard specification")
}
// Make sure we are able to press every button on the Spectrum keyboard
used := make(map[uint]bool)
for logicalKeyCode := range keyCodes {
used[logicalKeyCode] = false
}
for _, seq := range SDL_KeyMap {
if len(seq) == 1 {
used[seq[0]] = true
}
}
for _, isUsed := range used {
if !isUsed {
panic("some key is missing in the SDL keymap")
}
}
}