erm/internal/app/darktile/termutil/ansi.go
Ernest Romero Climent 93392fc149
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 09:18:01 +01:00

110 lines
2.5 KiB
Go

package termutil
func (t *Terminal) handleANSI(readChan chan MeasuredRune) (renderRequired bool) {
// if the byte is an escape character, read the next byte to determine which one
r := <-readChan
t.log("ANSI SEQ %c 0x%X", r.Rune, r.Rune)
t.mu.Lock()
defer t.mu.Unlock()
switch r.Rune {
case '[':
return t.handleCSI(readChan)
case ']':
return t.handleOSC(readChan)
case '(':
return t.handleSCS0(readChan) // select character set into G0
case ')':
return t.handleSCS1(readChan) // select character set into G1
case '*':
return swallowHandler(1)(readChan) // character set bullshit
case '+':
return swallowHandler(1)(readChan) // character set bullshit
case '>':
return swallowHandler(0)(readChan) // numeric char selection
case '=':
return swallowHandler(0)(readChan) // alt char selection
case '7':
t.GetActiveBuffer().saveCursor()
case '8':
t.GetActiveBuffer().restoreCursor()
case 'D':
t.GetActiveBuffer().index()
case 'E':
t.GetActiveBuffer().newLineEx(true)
case 'H':
t.GetActiveBuffer().tabSetAtCursor()
case 'M':
t.GetActiveBuffer().reverseIndex()
case 'P': // sixel
t.handleSixel(readChan)
case 'c':
t.GetActiveBuffer().clear()
case '#':
return t.handleScreenState(readChan)
case '^':
return t.handlePrivacyMessage(readChan)
default:
t.log("UNKNOWN ESCAPE SEQUENCE: 0x%X", r.Rune)
return false
}
return true
}
func swallowHandler(size int) func(pty chan MeasuredRune) bool {
return func(pty chan MeasuredRune) bool {
for i := 0; i < size; i++ {
<-pty
}
return false
}
}
func (t *Terminal) handleScreenState(readChan chan MeasuredRune) bool {
b := <-readChan
switch b.Rune {
case '8': // DECALN -- Screen Alignment Pattern
// hide cursor?
buffer := t.GetActiveBuffer()
buffer.resetVerticalMargins(uint(buffer.viewHeight))
buffer.SetScrollOffset(0)
// Fill the whole screen with E's
count := buffer.ViewHeight() * buffer.ViewWidth()
for count > 0 {
buffer.write(MeasuredRune{Rune: 'E', Width: 1})
count--
if count > 0 && !buffer.modes.AutoWrap && count%buffer.ViewWidth() == 0 {
buffer.index()
buffer.carriageReturn()
}
}
// restore cursor
buffer.setPosition(0, 0)
default:
return false
}
return true
}
func (t *Terminal) handlePrivacyMessage(readChan chan MeasuredRune) bool {
isEscaped := false
for {
b := <-readChan
if b.Rune == 0x18 /*CAN*/ || b.Rune == 0x1a /*SUB*/ || (b.Rune == 0x5c /*backslash*/ && isEscaped) {
break
}
if isEscaped {
isEscaped = false
} else if b.Rune == 0x1b {
isEscaped = true
continue
}
}
return false
}