izapple2/izapple2sdl/sdlKeyboard.go

125 lines
2.4 KiB
Go
Raw Normal View History

package main
2019-04-13 20:29:31 +02:00
import (
2019-10-06 01:26:00 +02:00
"fmt"
2019-04-13 20:29:31 +02:00
2020-10-03 23:38:26 +02:00
"github.com/ivanizag/izapple2"
2020-10-16 20:41:34 +02:00
"github.com/ivanizag/izapple2/screen"
2019-04-13 20:29:31 +02:00
"github.com/veandco/go-sdl2/sdl"
)
type sdlKeyboard struct {
2020-10-03 23:38:26 +02:00
a *izapple2.Apple2
2020-10-10 17:35:25 +02:00
keyChannel *izapple2.KeyboardChannel
showPages bool
2019-04-13 20:29:31 +02:00
}
2020-10-03 23:38:26 +02:00
func newSDLKeyBoard(a *izapple2.Apple2) *sdlKeyboard {
2019-04-13 20:29:31 +02:00
var k sdlKeyboard
2019-05-05 12:38:24 +02:00
k.a = a
2020-10-10 17:35:25 +02:00
k.keyChannel = izapple2.NewKeyboardChannel(a)
2019-05-10 00:09:15 +02:00
return &k
2019-04-13 20:29:31 +02:00
}
2020-10-10 17:35:25 +02:00
func (k *sdlKeyboard) putText(text string) {
k.keyChannel.PutText(text)
2019-04-13 20:29:31 +02:00
}
func (k *sdlKeyboard) putKey(keyEvent *sdl.KeyboardEvent) {
/*
See "Apple II reference manual", page 5
To get keys as understood by the Apple2 hardware run:
10 A=PEEK(49152)
20 PRINT A, A - 128
30 GOTO 10
2019-04-13 20:29:31 +02:00
*/
if keyEvent.Type != sdl.KEYDOWN {
// Process only key pushes
return
}
key := keyEvent.Keysym
ctrl := key.Mod&sdl.KMOD_CTRL != 0
if ctrl {
if key.Sym >= 'a' && key.Sym <= 'z' {
2020-10-10 17:35:25 +02:00
k.keyChannel.PutChar(uint8(key.Sym) - 97 + 1)
2019-04-13 20:29:31 +02:00
return
}
}
result := uint8(0)
switch key.Sym {
case sdl.K_ESCAPE:
result = 27
case sdl.K_BACKSPACE:
result = 8
2019-04-13 20:29:31 +02:00
case sdl.K_RETURN:
result = 13
case sdl.K_RETURN2:
result = 13
case sdl.K_LEFT:
if ctrl {
result = 31 // Base64A
2020-10-07 09:37:13 +02:00
} else {
result = 8
2019-04-13 20:29:31 +02:00
}
case sdl.K_RIGHT:
result = 21
2019-11-09 00:21:34 +01:00
// Apple //e
2019-04-13 20:29:31 +02:00
case sdl.K_UP:
2019-11-09 00:21:34 +01:00
result = 11 // 31 in the Base64A
2019-04-13 20:29:31 +02:00
case sdl.K_DOWN:
result = 10
2019-11-09 00:21:34 +01:00
case sdl.K_TAB:
result = 9
case sdl.K_DELETE:
result = 127 // 24 in the Base64A
// Base64A clone particularities
case sdl.K_F2:
result = 127 // Base64A
2019-05-05 12:38:24 +02:00
// Control of the emulator
2019-11-21 00:13:53 +01:00
case sdl.K_F1:
if ctrl {
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandReset)
2019-11-21 00:13:53 +01:00
}
2019-05-05 12:38:24 +02:00
case sdl.K_F5:
2019-11-07 23:20:14 +01:00
if ctrl {
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandShowSpeed)
2019-11-07 23:20:14 +01:00
} else {
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandToggleSpeed)
2019-11-07 23:20:14 +01:00
}
case sdl.K_F7:
k.showPages = !k.showPages
2019-05-18 23:40:59 +02:00
case sdl.K_F9:
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandDumpDebugInfo)
case sdl.K_F10:
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandNextCharGenPage)
case sdl.K_F11:
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandToggleCPUTrace)
2019-06-02 22:59:51 +02:00
case sdl.K_F12:
2020-10-10 17:35:25 +02:00
case sdl.K_PRINTSCREEN:
2020-10-16 20:41:34 +02:00
err := screen.SaveSnapshot(k.a, screen.ScreenModeNTSC, "snapshot.png")
2019-10-06 01:26:00 +02:00
if err != nil {
fmt.Printf("Error saving snapshoot: %v.\n.", err)
} else {
fmt.Println("Saving snapshot")
}
2020-03-13 21:29:12 -05:00
case sdl.K_PAUSE:
2020-10-03 23:38:26 +02:00
k.a.SendCommand(izapple2.CommandPauseUnpauseEmulator)
2019-04-13 20:29:31 +02:00
}
// Missing values 91 to 95. Usually control for [\]^_
// On the Base64A it's control for \]./
if result != 0 {
2020-10-10 17:35:25 +02:00
k.keyChannel.PutChar(result)
2019-04-13 20:29:31 +02:00
}
}