erm/app/darktile/gui/input.go

228 lines
6.8 KiB
Go
Raw Normal View History

2021-07-30 22:29:20 +00:00
package gui
import (
"fmt"
"github.com/d-tsuji/clipboard"
)
var modifiableKeys = map[ebiten.Key]uint8{
ebiten.KeyA: 'A',
ebiten.KeyB: 'B',
ebiten.KeyC: 'C',
ebiten.KeyD: 'D',
ebiten.KeyE: 'E',
ebiten.KeyF: 'F',
ebiten.KeyG: 'G',
ebiten.KeyH: 'H',
ebiten.KeyI: 'I',
ebiten.KeyJ: 'J',
ebiten.KeyK: 'K',
ebiten.KeyL: 'L',
ebiten.KeyM: 'M',
ebiten.KeyN: 'N',
ebiten.KeyO: 'O',
ebiten.KeyP: 'P',
ebiten.KeyQ: 'Q',
ebiten.KeyR: 'R',
ebiten.KeyS: 'S',
ebiten.KeyT: 'T',
ebiten.KeyU: 'U',
ebiten.KeyV: 'V',
ebiten.KeyW: 'W',
ebiten.KeyX: 'X',
ebiten.KeyY: 'Y',
ebiten.KeyZ: 'Z',
}
func (g *GUI) handleInput() error {
Fix terminal state and key state data races (#335) * Update gitignore to ignore .swp * Fix terminal state data races go run -race cmd/darktile/main.go WARNING: DATA RACE Write at 0x00c000864128 by goroutine 23: github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).sgrSequenceHandler() /home/ernestrc/src/darktile/internal/app/darktile/termutil/csi.go:973 +0x12ca github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).handleCSI() /home/ernestrc/src/darktile/internal/app/darktile/termutil/csi.go:62 +0x7e8 github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).handleANSI() /home/ernestrc/src/darktile/internal/app/darktile/termutil/ansi.go:11 +0xc86 github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).process() /home/ernestrc/src/darktile/internal/app/darktile/termutil/terminal.go:206 +0x126 github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).Run·dwrap·10() /home/ernestrc/src/darktile/internal/app/darktile/termutil/terminal.go:171 +0x39 Previous read at 0x00c000864128 by goroutine 22: [failed to restore the stack] Goroutine 23 (running) created at: github.com/liamg/darktile/internal/app/darktile/termutil.(*Terminal).Run() /home/ernestrc/src/darktile/internal/app/darktile/termutil/terminal.go:171 +0x517 github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).Run.func1() /home/ernestrc/src/darktile/internal/app/darktile/gui/gui.go:80 +0xa Goroutine 22 (running) created at: github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw.(*UserInterface).Run() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw/run_notsinglethread.go:37 +0x2c4 github.com/hajimehoshi/ebiten/v2.RunGame() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/run.go:158 +0x1d4 github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).Run() /home/ernestrc/src/darktile/internal/app/darktile/gui/gui.go:99 +0x2de github.com/liamg/darktile/internal/app/darktile/cmd.glob..func2() /home/ernestrc/src/darktile/internal/app/darktile/cmd/root.go:130 +0x13a9 github.com/spf13/cobra.(*Command).execute() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:852 +0xa7d github.com/spf13/cobra.(*Command).ExecuteC() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:960 +0x5da github.com/spf13/cobra.(*Command).Execute() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:897 +0x366 github.com/liamg/darktile/internal/app/darktile/cmd.Execute() /home/ernestrc/src/darktile/internal/app/darktile/cmd/root.go:153 +0x34f main.main() /home/ernestrc/src/darktile/cmd/darktile/main.go:75 +0x24 ================== Found 26 data race(s) * Fix KeyPressed data race go run -race cmd/darktile/main.go WARNING: DATA RACE Write at 0x00c00009fbc0 by goroutine 20: runtime.mapdelete_fast64() /usr/lib/go/src/runtime/map_fast64.go:272 +0x0 github.com/liamg/darktile/internal/app/darktile/gui.(*keyState).RepeatPressed() /home/ernestrc/src/darktile/internal/app/darktile/gui/key_states.go:61 +0x428 github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).handleInput() /home/ernestrc/src/darktile/internal/app/darktile/gui/input.go:109 +0x224 github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).Update() /home/ernestrc/src/darktile/internal/app/darktile/gui/update.go:34 +0x2e github.com/hajimehoshi/ebiten/v2.(*imageDumper).update() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/imagedumper_desktop.go:111 +0x85 github.com/hajimehoshi/ebiten/v2.(*imageDumperGame).Update() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/run.go:115 +0x172 github.com/hajimehoshi/ebiten/v2.(*uiContext).updateImpl() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/uicontext.go:194 +0x12c github.com/hajimehoshi/ebiten/v2.(*uiContext).update() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/uicontext.go:166 +0x88 github.com/hajimehoshi/ebiten/v2.(*uiContext).Update() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/uicontext.go:147 +0x3b github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw.(*UserInterface).loop() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw/ui.go:1036 +0x401 github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw.(*UserInterface).Run.func1() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw/run_notsinglethread.go:53 +0x1b1 Previous read at 0x00c00009fbc0 by goroutine 19: github.com/liamg/darktile/internal/app/darktile/gui.(*keyState).AnythingPressed() /home/ernestrc/src/darktile/internal/app/darktile/gui/key_states.go:33 +0xbc github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).watchForUpdate() /home/ernestrc/src/darktile/internal/app/darktile/gui/gui.go:105 +0x77 github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).Run·dwrap·2() /home/ernestrc/src/darktile/internal/app/darktile/gui/gui.go:97 +0x39 Goroutine 20 (running) created at: github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw.(*UserInterface).Run() /home/ernestrc/src/darktile/vendor/github.com/hajimehoshi/ebiten/v2/internal/uidriver/glfw/run_notsinglethread.go:37 +0x2c4 github.com/hajimehoshi/ebiten/v2.RunGame() Goroutine 19 (running) created at: github.com/liamg/darktile/internal/app/darktile/gui.(*GUI).Run() /home/ernestrc/src/darktile/internal/app/darktile/gui/gui.go:97 +0x2ca github.com/liamg/darktile/internal/app/darktile/cmd.glob..func2() /home/ernestrc/src/darktile/internal/app/darktile/cmd/root.go:130 +0x13a9 github.com/spf13/cobra.(*Command).execute() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:852 +0xa7d github.com/spf13/cobra.(*Command).ExecuteC() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:960 +0x5da github.com/spf13/cobra.(*Command).Execute() /home/ernestrc/src/darktile/vendor/github.com/spf13/cobra/command.go:897 +0x366 github.com/liamg/darktile/internal/app/darktile/cmd.Execute() /home/ernestrc/src/darktile/internal/app/darktile/cmd/root.go:153 +0x34f main.main() /home/ernestrc/src/darktile/cmd/darktile/main.go:75 +0x24
2022-05-10 08:18:01 +00:00
g.terminal.Lock()
defer g.terminal.Unlock()
2021-07-30 22:29:20 +00:00
if err := g.handleMouse(); err != nil {
return err
}
switch true {
case ebiten.IsKeyPressed(ebiten.KeyControl) && ebiten.IsKeyPressed(ebiten.KeyShift):
switch true {
case g.keyState.RepeatPressed(ebiten.KeyC):
content, selection := g.terminal.GetActiveBuffer().GetSelection()
if selection == nil {
return nil
}
return clipboard.Set(content)
case g.keyState.RepeatPressed(ebiten.KeyV):
paste, err := clipboard.Get()
if err != nil {
return err
}
return g.terminal.WriteToPty([]byte(paste))
case g.keyState.RepeatPressed(ebiten.KeyBracketLeft):
g.RequestScreenshot("")
}
case ebiten.IsKeyPressed(ebiten.KeyControl):
for key, ch := range modifiableKeys {
if g.keyState.RepeatPressed(key) {
if ch >= 97 && ch < 123 {
return g.terminal.WriteToPty([]byte{ch - 96})
} else if ch >= 65 && ch < 91 {
return g.terminal.WriteToPty([]byte{ch - 64})
}
}
}
switch true {
case g.keyState.RepeatPressed(ebiten.KeyMinus):
g.fontManager.DecreaseSize()
cellSize := g.fontManager.CharSize()
cols, rows := g.size.X/cellSize.X, g.size.Y/cellSize.Y
if err := g.terminal.SetSize(uint16(rows), uint16(cols)); err != nil {
return err
}
return nil
case g.keyState.RepeatPressed(ebiten.KeyEqual):
g.fontManager.IncreaseSize()
cellSize := g.fontManager.CharSize()
cols, rows := g.size.X/cellSize.X, g.size.Y/cellSize.Y
if err := g.terminal.SetSize(uint16(rows), uint16(cols)); err != nil {
return err
}
return nil
default:
return nil
}
case ebiten.IsKeyPressed(ebiten.KeyAlt):
for key, ch := range modifiableKeys {
if g.keyState.RepeatPressed(key) {
return g.terminal.WriteToPty([]byte{0x1b, ch})
}
}
case g.keyState.RepeatPressed(ebiten.KeyArrowUp):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
return g.terminal.WriteToPty([]byte{
0x1b,
'O',
'A',
})
} else {
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[%sA", g.getModifierStr())))
}
case g.keyState.RepeatPressed(ebiten.KeyArrowDown):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
return g.terminal.WriteToPty([]byte{
0x1b,
'O',
'B',
})
} else {
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[%sB", g.getModifierStr())))
}
case g.keyState.RepeatPressed(ebiten.KeyArrowRight):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
return g.terminal.WriteToPty([]byte{
0x1b,
'O',
'C',
})
} else {
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[%sC", g.getModifierStr())))
}
case g.keyState.RepeatPressed(ebiten.KeyArrowLeft):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
return g.terminal.WriteToPty([]byte{
0x1b,
'O',
'D',
})
} else {
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[%sD", g.getModifierStr())))
}
case g.keyState.RepeatPressed(ebiten.KeyEnter):
if g.terminal.GetActiveBuffer().IsNewLineMode() {
return g.terminal.WriteToPty([]byte{0x0d, 0x0a})
}
return g.terminal.WriteToPty([]byte{0x0d})
case g.keyState.RepeatPressed(ebiten.KeyNumpadEnter):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
g.terminal.WriteToPty([]byte{
0x1b,
'O',
'M',
})
} else {
if g.terminal.GetActiveBuffer().IsNewLineMode() {
if err := g.terminal.WriteToPty([]byte{0x0d, 0x0a}); err != nil {
return err
}
}
return g.terminal.WriteToPty([]byte{0x0d})
}
case g.keyState.RepeatPressed(ebiten.KeyTab):
return g.terminal.WriteToPty([]byte{0x09}) // tab
case g.keyState.RepeatPressed(ebiten.KeyEscape):
2021-07-31 12:56:39 +00:00
g.terminal.GetActiveBuffer().ClearSelection()
g.terminal.GetActiveBuffer().ClearHighlight()
2021-07-30 22:29:20 +00:00
return g.terminal.WriteToPty([]byte{0x1b}) // escape
case g.keyState.RepeatPressed(ebiten.KeyBackspace):
if ebiten.IsKeyPressed(ebiten.KeyAlt) {
return g.terminal.WriteToPty([]byte{0x17}) // ctrl-w/delete word
} else {
return g.terminal.WriteToPty([]byte{0x7f}) //0x7f is DEL
}
case g.keyState.RepeatPressed(ebiten.KeyF1):
return g.terminal.WriteToPty([]byte("\x1bOP"))
case g.keyState.RepeatPressed(ebiten.KeyF2):
return g.terminal.WriteToPty([]byte("\x1bOQ"))
case g.keyState.RepeatPressed(ebiten.KeyF3):
return g.terminal.WriteToPty([]byte("\x1bOR"))
case g.keyState.RepeatPressed(ebiten.KeyF4):
return g.terminal.WriteToPty([]byte("\x1bOS"))
case g.keyState.RepeatPressed(ebiten.KeyF5):
return g.terminal.WriteToPty([]byte("\x1b[15~"))
case g.keyState.RepeatPressed(ebiten.KeyF6):
return g.terminal.WriteToPty([]byte("\x1b[17~"))
case g.keyState.RepeatPressed(ebiten.KeyF7):
return g.terminal.WriteToPty([]byte("\x1b[18~"))
case g.keyState.RepeatPressed(ebiten.KeyF8):
return g.terminal.WriteToPty([]byte("\x1b[19~"))
case g.keyState.RepeatPressed(ebiten.KeyF9):
return g.terminal.WriteToPty([]byte("\x1b[20~"))
case g.keyState.RepeatPressed(ebiten.KeyF10):
return g.terminal.WriteToPty([]byte("\x1b[21~"))
case g.keyState.RepeatPressed(ebiten.KeyF11):
return g.terminal.WriteToPty([]byte("\x1b[22~"))
case g.keyState.RepeatPressed(ebiten.KeyF12):
return g.terminal.WriteToPty([]byte("\x1b[23~"))
case g.keyState.RepeatPressed(ebiten.KeyInsert):
return g.terminal.WriteToPty([]byte("\x1b[2~"))
case g.keyState.RepeatPressed(ebiten.KeyDelete):
return g.terminal.WriteToPty([]byte("\x1b[3~"))
case g.keyState.RepeatPressed(ebiten.KeyHome):
if g.terminal.GetActiveBuffer().IsApplicationCursorKeysModeEnabled() {
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[1%s~", g.getModifierStr())))
} else {
return g.terminal.WriteToPty([]byte("\x1b[H"))
}
case g.keyState.RepeatPressed(ebiten.KeyEnd):
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[4%s~", g.getModifierStr())))
case g.keyState.RepeatPressed(ebiten.KeyPageUp):
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[5%s~", g.getModifierStr())))
case g.keyState.RepeatPressed(ebiten.KeyPageDown):
return g.terminal.WriteToPty([]byte(fmt.Sprintf("\x1b[6%s~", g.getModifierStr())))
default:
input := ebiten.AppendInputChars(nil)
return g.terminal.WriteToPty([]byte(string(input)))
2021-07-30 22:29:20 +00:00
}
return nil
}