package uv
import (
xwindows
)
const (
flagCtrlAt = 1 << iota
flagCtrlI
flagCtrlM
flagCtrlOpenBracket
flagBackspace
flagFind
flagSelect
flagFKeys
)
type LegacyKeyEncoding uint32
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagCtrlAt
} else {
&^= flagCtrlAt
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagCtrlI
} else {
&^= flagCtrlI
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagCtrlM
} else {
&^= flagCtrlM
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagCtrlOpenBracket
} else {
&^= flagCtrlOpenBracket
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagBackspace
} else {
&^= flagBackspace
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagFind
} else {
&^= flagFind
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagSelect
} else {
&^= flagSelect
}
return
}
func ( LegacyKeyEncoding) ( bool) LegacyKeyEncoding {
if {
|= flagFKeys
} else {
&^= flagFKeys
}
return
}
type EventDecoder struct {
Legacy LegacyKeyEncoding
UseTerminfo bool
lastCks uint32
}
func ( *EventDecoder) ( []byte) ( int, Event) {
if len() == 0 {
return 0, nil
}
switch := [0]; {
case ansi.ESC:
if len() == 1 {
return 1, KeyPressEvent{Code: KeyEscape}
}
switch := [1]; {
case 'O':
return .parseSs3()
case 'P':
return .parseDcs()
case '[':
return .parseCsi()
case ']':
return .parseOsc()
case '_':
return .parseApc()
case '^':
return .parseStTerminated(ansi.PM, '^', nil)()
case 'X':
return .parseStTerminated(ansi.SOS, 'X', nil)()
default:
, := .([1:])
if , := .(KeyPressEvent); {
.Text = ""
.Mod |= ModAlt
return + 1,
}
return 1, KeyPressEvent{Code: KeyEscape}
}
case ansi.SS3:
return .parseSs3()
case ansi.DCS:
return .parseDcs()
case ansi.CSI:
return .parseCsi()
case ansi.OSC:
return .parseOsc()
case ansi.APC:
return .parseApc()
case ansi.PM:
return .parseStTerminated(ansi.PM, '^', nil)()
case ansi.SOS:
return .parseStTerminated(ansi.SOS, 'X', nil)()
default:
if <= ansi.US || == ansi.DEL || == ansi.SP {
return 1, .parseControl()
} else if >= ansi.PAD && <= ansi.APC {
:= rune() - 0x40
return 1, KeyPressEvent{Code: , Mod: ModCtrl | ModAlt}
}
return .parseUtf8()
}
}
func ( *EventDecoder) ( []byte) (int, Event) {
if len() == 2 && [0] == ansi.ESC {
return 2, KeyPressEvent{Code: rune([1]), Mod: ModAlt}
}
var ansi.Cmd
var [parser.MaxParamsSize]ansi.Param
var int
var int
if [] == ansi.CSI || [] == ansi.ESC {
++
}
if < len() && [-1] == ansi.ESC && [] == '[' {
++
}
if < len() && [] >= '<' && [] <= '?' {
|= ansi.Cmd([]) << parser.PrefixShift
}
var int
for = 0; < len() && < len() && [] >= 0x30 && [] <= 0x3F; , = +1, +1 {
if [] >= '0' && [] <= '9' {
if [] == parser.MissingParam {
[] = 0
}
[] *= 10
[] += ansi.Param([]) - '0'
}
if [] == ':' {
[] |= parser.HasMoreFlag
}
if [] == ';' || [] == ':' {
++
if < len() {
[] = parser.MissingParam
}
}
}
if > 0 && < len() {
++
}
var byte
for ; < len() && [] >= 0x20 && [] <= 0x2F; ++ {
= []
}
|= ansi.Cmd() << parser.IntermedShift
if >= len() || [] < 0x40 || [] > 0x7E {
if == '$' && [-1] == '$' {
:= slices.Clone([:-1])
, := .(append(, '~'))
if , := .(KeyPressEvent); {
.Mod |= ModShift
return ,
}
}
return , UnknownEvent([:])
}
|= ansi.Cmd([])
++
:= ansi.Params([:])
switch {
case 'y' | '?'<<parser.PrefixShift | '$'<<parser.IntermedShift:
, , := .Param(0, -1)
if ! || == -1 {
break
}
, , := .Param(1, 0)
if ! {
break
}
return , ModeReportEvent{Mode: ansi.DECMode(), Value: ansi.ModeSetting()}
case 'c' | '?'<<parser.PrefixShift:
return , parsePrimaryDevAttrs()
case 'c' | '>'<<parser.PrefixShift:
return , parseSecondaryDevAttrs()
case 'u' | '?'<<parser.PrefixShift:
, , := .Param(0, -1)
return , KeyboardEnhancementsEvent{}
case 'R' | '?'<<parser.PrefixShift:
, , := .Param(0, 1)
, , := .Param(1, 1)
if ! {
break
}
return , CursorPositionEvent{Y: - 1, X: - 1}
case 'm' | '<'<<parser.PrefixShift, 'M' | '<'<<parser.PrefixShift:
if == 3 {
return , parseSGRMouseEvent(, )
}
case 'm' | '>'<<parser.PrefixShift:
, , := .Param(0, 0)
if ! || != 4 {
break
}
, , := .Param(1, -1)
if ! || == -1 {
break
}
return , ModifyOtherKeysEvent{}
case 'n' | '?'<<parser.PrefixShift:
, , := .Param(0, -1)
, , := .Param(1, -1)
switch {
case 997:
switch {
case 1:
return , DarkColorSchemeEvent{}
case 2:
return , LightColorSchemeEvent{}
}
}
case 'I':
return , FocusEvent{}
case 'O':
return , BlurEvent{}
case 'R':
, , := .Param(0, 1)
, , := .Param(1, 1)
if == 2 && && {
:= CursorPositionEvent{Y: - 1, X: - 1}
if == 1 && -1 <= int(ModMeta|ModShift|ModAlt|ModCtrl) {
return , MultiEvent{KeyPressEvent{Code: KeyF3, Mod: KeyMod( - 1)}, }
}
return ,
}
if != 0 {
break
}
fallthrough
case 'a', 'b', 'c', 'd', 'A', 'B', 'C', 'D', 'E', 'F', 'H', 'P', 'Q', 'S', 'Z':
var KeyPressEvent
switch {
case 'a', 'b', 'c', 'd':
= KeyPressEvent{Code: KeyUp + rune(-'a'), Mod: ModShift}
case 'A', 'B', 'C', 'D':
= KeyPressEvent{Code: KeyUp + rune(-'A')}
case 'E':
= KeyPressEvent{Code: KeyBegin}
case 'F':
= KeyPressEvent{Code: KeyEnd}
case 'H':
= KeyPressEvent{Code: KeyHome}
case 'P', 'Q', 'R', 'S':
= KeyPressEvent{Code: KeyF1 + rune(-'P')}
case 'Z':
= KeyPressEvent{Code: KeyTab, Mod: ModShift}
}
, , := .Param(0, 1)
, , := .Param(1, 1)
if > 2 && ![1].HasMore() || != 1 {
break
}
if > 1 && == 1 && != -1 {
.Mod |= KeyMod( - 1)
}
return , parseKittyKeyboardExt(, )
case 'M':
if +3 > len() {
return , UnknownCsiEvent([:])
}
return + 3, parseX10MouseEvent(append([:], [:+3]...))
case 'y' | '$'<<parser.IntermedShift:
, , := .Param(0, -1)
if ! || == -1 {
break
}
, , := .Param(1, 0)
if ! {
break
}
return , ModeReportEvent{Mode: ansi.ANSIMode(), Value: ansi.ModeSetting()}
case 'u':
if == 0 {
return , UnknownCsiEvent([:])
}
return , parseKittyKeyboard()
case '_':
if != 6 {
return , UnknownCsiEvent([:])
}
, , := .Param(0, 0)
, , := .Param(1, 0)
, , := .Param(2, 0)
, , := .Param(3, 0)
, , := .Param(4, 0)
, , := .Param(5, 0)
:= .parseWin32InputKeyEvent(
uint16(),
uint16(),
rune(),
== 1,
uint32(),
max(1, uint16()),
)
return ,
case '@', '^', '~':
if == 0 {
return , UnknownCsiEvent([:])
}
, , := .Param(0, 0)
switch {
case '~':
switch {
case 27:
if != 3 {
return , UnknownCsiEvent([:])
}
return , parseXTermModifyOtherKeys()
case 200:
return , PasteStartEvent{}
case 201:
return , PasteEndEvent{}
}
}
switch {
case 1, 2, 3, 4, 5, 6, 7, 8,
11, 12, 13, 14, 15,
17, 18, 19, 20, 21,
23, 24, 25, 26,
28, 29, 31, 32, 33, 34:
var KeyPressEvent
switch {
case 1:
if .Legacy&flagFind != 0 {
= KeyPressEvent{Code: KeyFind}
} else {
= KeyPressEvent{Code: KeyHome}
}
case 2:
= KeyPressEvent{Code: KeyInsert}
case 3:
= KeyPressEvent{Code: KeyDelete}
case 4:
if .Legacy&flagSelect != 0 {
= KeyPressEvent{Code: KeySelect}
} else {
= KeyPressEvent{Code: KeyEnd}
}
case 5:
= KeyPressEvent{Code: KeyPgUp}
case 6:
= KeyPressEvent{Code: KeyPgDown}
case 7:
= KeyPressEvent{Code: KeyHome}
case 8:
= KeyPressEvent{Code: KeyEnd}
case 11, 12, 13, 14, 15:
= KeyPressEvent{Code: KeyF1 + rune(-11)}
case 17, 18, 19, 20, 21:
= KeyPressEvent{Code: KeyF6 + rune(-17)}
case 23, 24, 25, 26:
= KeyPressEvent{Code: KeyF11 + rune(-23)}
case 28, 29:
= KeyPressEvent{Code: KeyF15 + rune(-28)}
case 31, 32, 33, 34:
= KeyPressEvent{Code: KeyF17 + rune(-31)}
}
, , := .Param(1, -1)
if > 1 && != -1 {
.Mod |= KeyMod( - 1)
}
switch {
case '~':
return , parseKittyKeyboardExt(, )
case '^':
.Mod |= ModCtrl
case '@':
.Mod |= ModCtrl | ModShift
}
return ,
}
case 't':
, , := .Param(0, 0)
if ! {
break
}
switch {
case 4:
if == 3 {
, , := .Param(1, 0)
, , := .Param(2, 0)
if ! || ! {
break
}
return , PixelSizeEvent{Width: , Height: }
}
case 6:
if == 3 {
, , := .Param(1, 0)
, , := .Param(2, 0)
if ! || ! {
break
}
return , CellSizeEvent{Width: , Height: }
}
case 8:
if == 3 {
, , := .Param(1, 0)
, , := .Param(2, 0)
if ! || ! {
break
}
return , WindowSizeEvent{Width: , Height: }
}
case 48:
if == 5 {
, , := .Param(1, 0)
, , := .Param(2, 0)
, , := .Param(3, 0)
, , := .Param(4, 0)
if ! || ! || ! || ! {
break
}
return , MultiEvent{
WindowSizeEvent{Width: , Height: },
PixelSizeEvent{Width: , Height: },
}
}
}
var WindowOpEvent
.Op =
for := 1; < ; ++ {
, , := .Param(, 0)
if {
.Args = append(.Args, )
}
}
return ,
}
return , UnknownCsiEvent([:])
}
func ( *EventDecoder) ( []byte) (int, Event) {
if len() == 2 && [0] == ansi.ESC {
return 2, KeyPressEvent{Code: unicode.ToLower(rune([1])), Mod: ModShift | ModAlt}
}
var int
if [] == ansi.SS3 || [] == ansi.ESC {
++
}
if < len() && [-1] == ansi.ESC && [] == 'O' {
++
}
var int
for ; < len() && [] >= '0' && [] <= '9'; ++ {
*= 10
+= int([]) - '0'
}
if >= len() || [] < 0x21 || [] > 0x7E {
return , UnknownEvent([:])
}
:= []
++
var KeyPressEvent
switch {
case 'a', 'b', 'c', 'd':
= KeyPressEvent{Code: KeyUp + rune(-'a'), Mod: ModCtrl}
case 'A', 'B', 'C', 'D':
= KeyPressEvent{Code: KeyUp + rune(-'A')}
case 'E':
= KeyPressEvent{Code: KeyBegin}
case 'F':
= KeyPressEvent{Code: KeyEnd}
case 'H':
= KeyPressEvent{Code: KeyHome}
case 'P', 'Q', 'R', 'S':
= KeyPressEvent{Code: KeyF1 + rune(-'P')}
case 'M':
= KeyPressEvent{Code: KeyKpEnter}
case 'X':
= KeyPressEvent{Code: KeyKpEqual}
case 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y':
= KeyPressEvent{Code: KeyKpMultiply + rune(-'j')}
default:
return , UnknownSs3Event([:])
}
if > 0 {
.Mod |= KeyMod( - 1)
}
return ,
}
func ( *EventDecoder) ( []byte) (int, Event) {
:= func() KeyPressEvent {
return KeyPressEvent{Code: rune([1]), Mod: ModAlt}
}
if len() == 2 && [0] == ansi.ESC {
return 2, ()
}
var int
if [] == ansi.OSC || [] == ansi.ESC {
++
}
if < len() && [-1] == ansi.ESC && [] == ']' {
++
}
var , int
:= -1
for ; < len() && [] >= '0' && [] <= '9'; ++ {
if == -1 {
= 0
} else {
*= 10
}
+= int([]) - '0'
}
if < len() && [] == ';' {
++
=
}
for ; < len(); ++ {
if slices.Contains([]byte{ansi.BEL, ansi.ESC, ansi.ST, ansi.CAN, ansi.SUB}, []) {
break
}
}
if >= len() {
return , UnknownEvent([:])
}
=
++
switch [-1] {
case ansi.CAN, ansi.SUB:
return , ignoredEvent([:])
case ansi.ESC:
if >= len() || [] != '\\' {
if == -1 || ( == 0 && == 2) {
return 2, ()
}
return , ignoredEvent([:])
}
++
}
if <= {
return , UnknownEvent([:])
}
:= string([:])
switch {
case 10:
return , ForegroundColorEvent{ansi.XParseColor()}
case 11:
return , BackgroundColorEvent{ansi.XParseColor()}
case 12:
return , CursorColorEvent{ansi.XParseColor()}
case 52:
:= strings.Split(, ";")
if len() != 2 || len([0]) < 1 {
return , ClipboardEvent{}
}
:= [1]
, := base64.StdEncoding.DecodeString()
if != nil {
return , ClipboardEvent{Content: [1]}
}
:= ClipboardSelection([0][0])
return , ClipboardEvent{Selection: , Content: string()}
}
return , UnknownOscEvent([:])
}
func ( *EventDecoder) (, byte, func([]byte) Event) func([]byte) (int, Event) {
:= func( []byte) (int, Event) {
switch {
case ansi.SOS:
return 2, KeyPressEvent{Code: unicode.ToLower(rune([1])), Mod: ModShift | ModAlt}
case ansi.PM, ansi.APC:
return 2, KeyPressEvent{Code: rune([1]), Mod: ModAlt}
}
return 0, nil
}
return func( []byte) (int, Event) {
if len() == 2 && [0] == ansi.ESC {
return ()
}
var int
if [] == || [] == ansi.ESC {
++
}
if < len() && [-1] == ansi.ESC && [] == {
++
}
:=
for ; < len(); ++ {
if slices.Contains([]byte{ansi.ESC, ansi.ST, ansi.CAN, ansi.SUB}, []) {
break
}
}
if >= len() {
return , UnknownEvent([:])
}
:=
++
switch [-1] {
case ansi.CAN, ansi.SUB:
return , ignoredEvent([:])
case ansi.ESC:
if >= len() || [] != '\\' {
if == {
return ()
}
return , ignoredEvent([:])
}
++
}
if != nil {
if := ([:]); != nil {
return ,
}
}
switch {
case ansi.PM:
return , UnknownPmEvent([:])
case ansi.SOS:
return , UnknownSosEvent([:])
case ansi.APC:
return , UnknownApcEvent([:])
default:
return , UnknownEvent([:])
}
}
}
func ( *EventDecoder) ( []byte) (int, Event) {
if len() == 2 && [0] == ansi.ESC {
return 2, KeyPressEvent{Code: unicode.ToLower(rune([1])), Mod: ModShift | ModAlt}
}
var [16]ansi.Param
var int
var ansi.Cmd
var int
if [] == ansi.DCS || [] == ansi.ESC {
++
}
if < len() && [-1] == ansi.ESC && [] == 'P' {
++
}
if < len() && [] >= '<' && [] <= '?' {
|= ansi.Cmd([]) << parser.PrefixShift
}
var int
for = 0; < len() && < len() && [] >= 0x30 && [] <= 0x3F; , = +1, +1 {
if [] >= '0' && [] <= '9' {
if [] == parser.MissingParam {
[] = 0
}
[] *= 10
[] += ansi.Param([]) - '0'
}
if [] == ':' {
[] |= parser.HasMoreFlag
}
if [] == ';' || [] == ':' {
++
if < len() {
[] = parser.MissingParam
}
}
}
if > 0 && < len() {
++
}
var byte
for := 0; < len() && [] >= 0x20 && [] <= 0x2F; , = +1, +1 {
= []
}
|= ansi.Cmd() << parser.IntermedShift
if >= len() || [] < 0x40 || [] > 0x7E {
return , UnknownEvent([:])
}
|= ansi.Cmd([])
++
:=
for ; < len(); ++ {
if [] == ansi.ST || [] == ansi.ESC {
break
}
}
if >= len() {
return , UnknownEvent([:])
}
:=
++
if < len() && [-1] == ansi.ESC && [] == '\\' {
++
}
:= ansi.Params([:])
switch {
case 'r' | '+'<<parser.IntermedShift:
, , := .Param(0, 0)
switch {
case 1:
:= parseTermcap([:])
return ,
}
case '|' | '>'<<parser.PrefixShift:
return , TerminalVersionEvent{string([:])}
case '|' | '!'<<parser.IntermedShift:
return , parseTertiaryDevAttrs([:])
}
return , UnknownDcsEvent([:])
}
func ( *EventDecoder) ( []byte) (int, Event) {
if len() == 2 && [0] == ansi.ESC {
return 2, KeyPressEvent{Code: rune([1]), Mod: ModAlt}
}
return .parseStTerminated(ansi.APC, '_', func( []byte) Event {
if len() == 0 {
return nil
}
switch [0] {
case 'G':
var KittyGraphicsEvent
:= bytes.Split([1:], []byte{';'})
.Options.UnmarshalText([0])
if len() > 1 {
.Payload = [1]
}
return
}
return nil
})()
}
func ( *EventDecoder) ( []byte) (int, Event) {
if len() == 0 {
return 0, nil
}
:= [0]
if <= ansi.US || == ansi.DEL {
return 1, .parseControl()
} else if > ansi.US && < ansi.DEL {
:= rune()
:= KeyPressEvent{Code: , Text: string()}
if unicode.IsUpper() {
.Code = unicode.ToLower()
.ShiftedCode =
.Mod |= ModShift
}
return 1,
}
, := utf8.DecodeRune()
if == utf8.RuneError {
return 1, UnknownEvent([0])
}
, , , := uniseg.FirstGraphemeCluster(, -1)
:= string()
for := range {
if > 0 {
= KeyExtended
break
}
}
return len(), KeyPressEvent{Code: , Text: }
}
func ( *EventDecoder) ( byte) Event {
switch {
case ansi.NUL:
if .Legacy&flagCtrlAt != 0 {
return KeyPressEvent{Code: '@', Mod: ModCtrl}
}
return KeyPressEvent{Code: KeySpace, Mod: ModCtrl}
case ansi.BS:
return KeyPressEvent{Code: 'h', Mod: ModCtrl}
case ansi.HT:
if .Legacy&flagCtrlI != 0 {
return KeyPressEvent{Code: 'i', Mod: ModCtrl}
}
return KeyPressEvent{Code: KeyTab}
case ansi.CR:
if .Legacy&flagCtrlM != 0 {
return KeyPressEvent{Code: 'm', Mod: ModCtrl}
}
return KeyPressEvent{Code: KeyEnter}
case ansi.ESC:
if .Legacy&flagCtrlOpenBracket != 0 {
return KeyPressEvent{Code: '[', Mod: ModCtrl}
}
return KeyPressEvent{Code: KeyEscape}
case ansi.DEL:
if .Legacy&flagBackspace != 0 {
return KeyPressEvent{Code: KeyDelete}
}
return KeyPressEvent{Code: KeyBackspace}
case ansi.SP:
return KeyPressEvent{Code: KeySpace, Text: " "}
default:
if >= ansi.SOH && <= ansi.SUB {
:= rune( + 0x60)
return KeyPressEvent{Code: , Mod: ModCtrl}
} else if >= ansi.FS && <= ansi.US {
:= rune( + 0x40)
return KeyPressEvent{Code: , Mod: ModCtrl}
}
return UnknownEvent()
}
}
func ( ansi.Params) Event {
, , := .Param(1, 1)
, , := .Param(2, 1)
:= KeyMod( - 1)
:= rune()
switch {
case ansi.BS:
return KeyPressEvent{Mod: , Code: KeyBackspace}
case ansi.HT:
return KeyPressEvent{Mod: , Code: KeyTab}
case ansi.CR:
return KeyPressEvent{Mod: , Code: KeyEnter}
case ansi.ESC:
return KeyPressEvent{Mod: , Code: KeyEscape}
case ansi.DEL:
return KeyPressEvent{Mod: , Code: KeyBackspace}
}
:= KeyPressEvent{Code: , Mod: }
if .Mod <= ModShift {
.Text = string()
}
return
}
var kittyKeyMap = map[int]Key{
ansi.BS: {Code: KeyBackspace},
ansi.HT: {Code: KeyTab},
ansi.CR: {Code: KeyEnter},
ansi.ESC: {Code: KeyEscape},
ansi.DEL: {Code: KeyBackspace},
57344: {Code: KeyEscape},
57345: {Code: KeyEnter},
57346: {Code: KeyTab},
57347: {Code: KeyBackspace},
57348: {Code: KeyInsert},
57349: {Code: KeyDelete},
57350: {Code: KeyLeft},
57351: {Code: KeyRight},
57352: {Code: KeyUp},
57353: {Code: KeyDown},
57354: {Code: KeyPgUp},
57355: {Code: KeyPgDown},
57356: {Code: KeyHome},
57357: {Code: KeyEnd},
57358: {Code: KeyCapsLock},
57359: {Code: KeyScrollLock},
57360: {Code: KeyNumLock},
57361: {Code: KeyPrintScreen},
57362: {Code: KeyPause},
57363: {Code: KeyMenu},
57364: {Code: KeyF1},
57365: {Code: KeyF2},
57366: {Code: KeyF3},
57367: {Code: KeyF4},
57368: {Code: KeyF5},
57369: {Code: KeyF6},
57370: {Code: KeyF7},
57371: {Code: KeyF8},
57372: {Code: KeyF9},
57373: {Code: KeyF10},
57374: {Code: KeyF11},
57375: {Code: KeyF12},
57376: {Code: KeyF13},
57377: {Code: KeyF14},
57378: {Code: KeyF15},
57379: {Code: KeyF16},
57380: {Code: KeyF17},
57381: {Code: KeyF18},
57382: {Code: KeyF19},
57383: {Code: KeyF20},
57384: {Code: KeyF21},
57385: {Code: KeyF22},
57386: {Code: KeyF23},
57387: {Code: KeyF24},
57388: {Code: KeyF25},
57389: {Code: KeyF26},
57390: {Code: KeyF27},
57391: {Code: KeyF28},
57392: {Code: KeyF29},
57393: {Code: KeyF30},
57394: {Code: KeyF31},
57395: {Code: KeyF32},
57396: {Code: KeyF33},
57397: {Code: KeyF34},
57398: {Code: KeyF35},
57399: {Code: KeyKp0},
57400: {Code: KeyKp1},
57401: {Code: KeyKp2},
57402: {Code: KeyKp3},
57403: {Code: KeyKp4},
57404: {Code: KeyKp5},
57405: {Code: KeyKp6},
57406: {Code: KeyKp7},
57407: {Code: KeyKp8},
57408: {Code: KeyKp9},
57409: {Code: KeyKpDecimal},
57410: {Code: KeyKpDivide},
57411: {Code: KeyKpMultiply},
57412: {Code: KeyKpMinus},
57413: {Code: KeyKpPlus},
57414: {Code: KeyKpEnter},
57415: {Code: KeyKpEqual},
57416: {Code: KeyKpSep},
57417: {Code: KeyKpLeft},
57418: {Code: KeyKpRight},
57419: {Code: KeyKpUp},
57420: {Code: KeyKpDown},
57421: {Code: KeyKpPgUp},
57422: {Code: KeyKpPgDown},
57423: {Code: KeyKpHome},
57424: {Code: KeyKpEnd},
57425: {Code: KeyKpInsert},
57426: {Code: KeyKpDelete},
57427: {Code: KeyKpBegin},
57428: {Code: KeyMediaPlay},
57429: {Code: KeyMediaPause},
57430: {Code: KeyMediaPlayPause},
57431: {Code: KeyMediaReverse},
57432: {Code: KeyMediaStop},
57433: {Code: KeyMediaFastForward},
57434: {Code: KeyMediaRewind},
57435: {Code: KeyMediaNext},
57436: {Code: KeyMediaPrev},
57437: {Code: KeyMediaRecord},
57438: {Code: KeyLowerVol},
57439: {Code: KeyRaiseVol},
57440: {Code: KeyMute},
57441: {Code: KeyLeftShift},
57442: {Code: KeyLeftCtrl},
57443: {Code: KeyLeftAlt},
57444: {Code: KeyLeftSuper},
57445: {Code: KeyLeftHyper},
57446: {Code: KeyLeftMeta},
57447: {Code: KeyRightShift},
57448: {Code: KeyRightCtrl},
57449: {Code: KeyRightAlt},
57450: {Code: KeyRightSuper},
57451: {Code: KeyRightHyper},
57452: {Code: KeyRightMeta},
57453: {Code: KeyIsoLevel3Shift},
57454: {Code: KeyIsoLevel5Shift},
}
func () {
kittyKeyMap[ansi.NUL] = Key{Code: KeySpace, Mod: ModCtrl}
for := ansi.SOH; <= ansi.SUB; ++ {
if , := kittyKeyMap[]; ! {
kittyKeyMap[] = Key{Code: rune( + 0x60), Mod: ModCtrl}
}
}
for := ansi.FS; <= ansi.US; ++ {
if , := kittyKeyMap[]; ! {
kittyKeyMap[] = Key{Code: rune( + 0x40), Mod: ModCtrl}
}
}
}
const (
kittyShift = 1 << iota
kittyAlt
kittyCtrl
kittySuper
kittyHyper
kittyMeta
kittyCapsLock
kittyNumLock
)
func ( int) KeyMod {
var KeyMod
if &kittyShift != 0 {
|= ModShift
}
if &kittyAlt != 0 {
|= ModAlt
}
if &kittyCtrl != 0 {
|= ModCtrl
}
if &kittySuper != 0 {
|= ModSuper
}
if &kittyHyper != 0 {
|= ModHyper
}
if &kittyMeta != 0 {
|= ModMeta
}
if &kittyCapsLock != 0 {
|= ModCapsLock
}
if &kittyNumLock != 0 {
|= ModNumLock
}
return
}
func ( ansi.Params) ( Event) {
var bool
var Key
var int
var int
for , := range {
switch {
case 0:
switch {
case 0:
var bool
:= .Param(1)
, = kittyKeyMap[]
if ! {
:= rune()
if !utf8.ValidRune() {
= utf8.RuneError
}
.Code =
}
case 2:
if := rune(.Param(1)); unicode.IsPrint() {
.BaseCode =
}
fallthrough
case 1:
if := rune(.Param(1)); unicode.IsPrint() {
.ShiftedCode =
}
}
case 1:
switch {
case 0:
:= .Param(1)
if > 1 {
.Mod = fromKittyMod( - 1)
if .Mod > ModShift {
.Text = ""
}
}
case 1:
switch .Param(1) {
case 2:
.IsRepeat = true
case 3:
= true
}
case 2:
}
case 2:
if := .Param(0); != 0 {
.Text += string(rune())
}
}
++
if !.HasMore() {
++
= 0
}
}
:= .Mod
&^= ModNumLock
:= <= ModShift || == ModCapsLock || == (ModShift|ModCapsLock)
:= .Code >= KeyKpEqual && .Code <= KeyKpSep
if len(.Text) == 0 && && {
switch {
case .Code >= KeyKp0 && .Code <= KeyKp9:
.Text = string('0' + .Code - KeyKp0)
case .Code == KeyKpEqual:
.Text = "="
case .Code == KeyKpMultiply:
.Text = "*"
case .Code == KeyKpPlus:
.Text = "+"
case .Code == KeyKpMinus:
.Text = "-"
case .Code == KeyKpDecimal:
.Text = "."
case .Code == KeyKpDivide:
.Text = "/"
case .Code == KeyKpSep:
.Text = ","
}
}
if len(.Text) == 0 && unicode.IsPrint(.Code) && {
if == 0 {
.Text = string(.Code)
} else {
:= unicode.ToLower
if .Contains(ModShift) || .Contains(ModCapsLock) {
= unicode.ToUpper
}
if .ShiftedCode != 0 {
.Text = string(.ShiftedCode)
} else {
.Text = string((.Code))
}
}
}
if {
return KeyReleaseEvent()
}
return KeyPressEvent()
}
func ( ansi.Params, KeyPressEvent) Event {
if len() > 2 &&
[1].HasMore() {
switch [2].Param(1) {
case 2:
.IsRepeat = true
case 3:
return KeyReleaseEvent()
}
}
return
}
func ( ansi.Params) Event {
:= make(PrimaryDeviceAttributesEvent, len())
for , := range {
if !.HasMore() {
[] = .Param(0)
}
}
return
}
func ( ansi.Params) Event {
:= make(SecondaryDeviceAttributesEvent, len())
for , := range {
if !.HasMore() {
[] = .Param(0)
}
}
return
}
func ( []byte) Event {
, := hex.DecodeString(string())
if != nil {
return UnknownDcsEvent(fmt.Sprintf("\x1bP!|%s\x1b\\", ))
}
return TertiaryDeviceAttributesEvent()
}
func ( ansi.Cmd, ansi.Params) Event {
, , := .Param(1, 1)
if ! {
= 1
}
, , := .Param(2, 1)
if ! {
= 1
}
:= .Final() == 'm'
, , := .Param(0, 0)
, , , := parseMouseButton()
--
--
:= Mouse{X: , Y: , Button: , Mod: }
if isWheel(.Button) {
return MouseWheelEvent()
} else if ! && {
return MouseReleaseEvent()
} else if {
return MouseMotionEvent()
}
return MouseClickEvent()
}
const x10MouseByteOffset = 32
func ( []byte) Event {
:= [3:6]
:= int([0])
if >= x10MouseByteOffset {
-= x10MouseByteOffset
}
, , , := parseMouseButton()
:= int([1]) - x10MouseByteOffset - 1
:= int([2]) - x10MouseByteOffset - 1
:= Mouse{X: , Y: , Button: , Mod: }
if isWheel(.Button) {
return MouseWheelEvent()
} else if {
return MouseMotionEvent()
} else if {
return MouseReleaseEvent()
}
return MouseClickEvent()
}
func ( int) ( KeyMod, MouseButton, bool, bool) {
const (
= 0b0000_0100
= 0b0000_1000
= 0b0001_0000
= 0b0010_0000
= 0b0100_0000
= 0b1000_0000
= 0b0000_0011
)
if & != 0 {
|= ModAlt
}
if & != 0 {
|= ModCtrl
}
if & != 0 {
|= ModShift
}
if & != 0 {
= MouseBackward + MouseButton(&)
} else if & != 0 {
= MouseWheelUp + MouseButton(&)
} else {
= MouseLeft + MouseButton(&)
if & == {
= MouseNone
= true
}
}
if & != 0 && !isWheel() {
= true
}
return
}
func ( MouseButton) bool {
return >= MouseWheelUp && <= MouseWheelRight
}
type shiftable interface {
~uint | ~uint16 | ~uint32 | ~uint64
}
func [ shiftable]( ) {
if > 0xff {
>>= 8
}
return
}
func ( color.Color) string {
if == nil {
return ""
}
, , , := .RGBA()
return fmt.Sprintf("#%02x%02x%02x", shift(), shift(), shift())
}
func (, , float64) (, float64) {
if > {
=
=
} else {
=
=
}
if > {
=
} else if < {
=
}
return ,
}
func ( float64) float64 {
return math.Round(*1000) / 1000
}
func (, , uint8) (, , float64) {
:= float64() / 255
:= float64() / 255
:= float64() / 255
, := getMaxMin(, , )
:= -
= ( + ) / 2
if == 0 {
= 0
= 0
} else {
switch {
case :
= 60 * (math.Mod((-)/, 6))
case :
= 60 * ((( - ) / ) + 2)
case :
= 60 * ((( - ) / ) + 4)
}
if < 0 {
+= 360
}
= / (1 - math.Abs((2*)-1))
}
return , round(), round()
}
func ( color.Color) bool {
if == nil {
return true
}
, , , := .RGBA()
, , := rgbToHSL(uint8(>>8), uint8(>>8), uint8(>>8))
return < 0.5
}
func ( []byte) CapabilityEvent {
if len() == 0 {
return CapabilityEvent{""}
}
var strings.Builder
:= bytes.Split(, []byte{';'})
for , := range {
:= bytes.SplitN(, []byte{'='}, 2)
if len() == 0 {
return CapabilityEvent{""}
}
, := hex.DecodeString(string([0]))
if != nil || len() == 0 {
continue
}
var []byte
if len() > 1 {
, = hex.DecodeString(string([1]))
if != nil {
continue
}
}
if .Len() > 0 {
.WriteByte(';')
}
.WriteString(string())
if len() > 0 {
.WriteByte('=')
.WriteString(string())
}
}
return CapabilityEvent{.String()}
}
func ( *EventDecoder) ( uint16, uint16, rune, bool, uint32, uint16) ( Event) {
defer func() {
if > 1 {
var MultiEvent
for := 0; < int(); ++ {
= append(, )
}
=
}
}()
defer func() {
if != 0 {
.lastCks =
}
}()
var Key
switch {
case == 0:
if {
return KeyPressEvent{Code: 0, BaseCode: , Mod: translateControlKeyState()}
}
return KeyReleaseEvent{Code: 0, BaseCode: , Mod: translateControlKeyState()}
case == xwindows.VK_BACK:
.BaseCode = KeyBackspace
case == xwindows.VK_TAB:
.BaseCode = KeyTab
case == xwindows.VK_RETURN:
.BaseCode = KeyEnter
case == xwindows.VK_SHIFT:
if &xwindows.SHIFT_PRESSED != 0 {
if &xwindows.ENHANCED_KEY != 0 {
.BaseCode = KeyRightShift
} else {
.BaseCode = KeyLeftShift
}
} else if .lastCks&xwindows.SHIFT_PRESSED != 0 {
if .lastCks&xwindows.ENHANCED_KEY != 0 {
.BaseCode = KeyRightShift
} else {
.BaseCode = KeyLeftShift
}
}
case == xwindows.VK_CONTROL:
if &xwindows.LEFT_CTRL_PRESSED != 0 {
.BaseCode = KeyLeftCtrl
} else if &xwindows.RIGHT_CTRL_PRESSED != 0 {
.BaseCode = KeyRightCtrl
} else if .lastCks&xwindows.LEFT_CTRL_PRESSED != 0 {
.BaseCode = KeyLeftCtrl
} else if .lastCks&xwindows.RIGHT_CTRL_PRESSED != 0 {
.BaseCode = KeyRightCtrl
}
case == xwindows.VK_MENU:
if &xwindows.LEFT_ALT_PRESSED != 0 {
.BaseCode = KeyLeftAlt
} else if &xwindows.RIGHT_ALT_PRESSED != 0 {
.BaseCode = KeyRightAlt
} else if .lastCks&xwindows.LEFT_ALT_PRESSED != 0 {
.BaseCode = KeyLeftAlt
} else if .lastCks&xwindows.RIGHT_ALT_PRESSED != 0 {
.BaseCode = KeyRightAlt
}
case == xwindows.VK_PAUSE:
.BaseCode = KeyPause
case == xwindows.VK_CAPITAL:
.BaseCode = KeyCapsLock
case == xwindows.VK_ESCAPE:
.BaseCode = KeyEscape
case == xwindows.VK_SPACE:
.BaseCode = KeySpace
case == xwindows.VK_PRIOR:
.BaseCode = KeyPgUp
case == xwindows.VK_NEXT:
.BaseCode = KeyPgDown
case == xwindows.VK_END:
.BaseCode = KeyEnd
case == xwindows.VK_HOME:
.BaseCode = KeyHome
case == xwindows.VK_LEFT:
.BaseCode = KeyLeft
case == xwindows.VK_UP:
.BaseCode = KeyUp
case == xwindows.VK_RIGHT:
.BaseCode = KeyRight
case == xwindows.VK_DOWN:
.BaseCode = KeyDown
case == xwindows.VK_SELECT:
.BaseCode = KeySelect
case == xwindows.VK_SNAPSHOT:
.BaseCode = KeyPrintScreen
case == xwindows.VK_INSERT:
.BaseCode = KeyInsert
case == xwindows.VK_DELETE:
.BaseCode = KeyDelete
case >= '0' && <= '9':
.BaseCode = rune()
case >= 'A' && <= 'Z':
.BaseCode = rune() + 32
case == xwindows.VK_LWIN:
.BaseCode = KeyLeftSuper
case == xwindows.VK_RWIN:
.BaseCode = KeyRightSuper
case == xwindows.VK_APPS:
.BaseCode = KeyMenu
case >= xwindows.VK_NUMPAD0 && <= xwindows.VK_NUMPAD9:
.BaseCode = rune(-xwindows.VK_NUMPAD0) + KeyKp0
.Text = string('0' + (rune() - xwindows.VK_NUMPAD0))
case == xwindows.VK_MULTIPLY:
.BaseCode = KeyKpMultiply
.Text = "*"
case == xwindows.VK_ADD:
.BaseCode = KeyKpPlus
.Text = "+"
case == xwindows.VK_SEPARATOR:
.BaseCode = KeyKpComma
.Text = ","
case == xwindows.VK_SUBTRACT:
.BaseCode = KeyKpMinus
.Text = "-"
case == xwindows.VK_DECIMAL:
.BaseCode = KeyKpDecimal
.Text = "."
case == xwindows.VK_DIVIDE:
.BaseCode = KeyKpDivide
.Text = "/"
case >= xwindows.VK_F1 && <= xwindows.VK_F24:
.BaseCode = rune(-xwindows.VK_F1) + KeyF1
case == xwindows.VK_NUMLOCK:
.BaseCode = KeyNumLock
case == xwindows.VK_SCROLL:
.BaseCode = KeyScrollLock
case == xwindows.VK_LSHIFT:
.BaseCode = KeyLeftShift
case == xwindows.VK_RSHIFT:
.BaseCode = KeyRightShift
case == xwindows.VK_LCONTROL:
.BaseCode = KeyLeftCtrl
case == xwindows.VK_RCONTROL:
.BaseCode = KeyRightCtrl
case == xwindows.VK_LMENU:
.BaseCode = KeyLeftAlt
case == xwindows.VK_RMENU:
.BaseCode = KeyRightAlt
case == xwindows.VK_VOLUME_MUTE:
.BaseCode = KeyMute
case == xwindows.VK_VOLUME_DOWN:
.BaseCode = KeyLowerVol
case == xwindows.VK_VOLUME_UP:
.BaseCode = KeyRaiseVol
case == xwindows.VK_MEDIA_NEXT_TRACK:
.BaseCode = KeyMediaNext
case == xwindows.VK_MEDIA_PREV_TRACK:
.BaseCode = KeyMediaPrev
case == xwindows.VK_MEDIA_STOP:
.BaseCode = KeyMediaStop
case == xwindows.VK_MEDIA_PLAY_PAUSE:
.BaseCode = KeyMediaPlayPause
case == xwindows.VK_OEM_1:
.BaseCode = ';'
case == xwindows.VK_OEM_PLUS:
.BaseCode = '+'
case == xwindows.VK_OEM_COMMA:
.BaseCode = ','
case == xwindows.VK_OEM_MINUS:
.BaseCode = '-'
case == xwindows.VK_OEM_PERIOD:
.BaseCode = '.'
case == xwindows.VK_OEM_2:
.BaseCode = '/'
case == xwindows.VK_OEM_3:
.BaseCode = '`'
case == xwindows.VK_OEM_4:
.BaseCode = '['
case == xwindows.VK_OEM_5:
.BaseCode = '\\'
case == xwindows.VK_OEM_6:
.BaseCode = ']'
case == xwindows.VK_OEM_7:
.BaseCode = '\''
}
const = xwindows.LEFT_CTRL_PRESSED | xwindows.RIGHT_ALT_PRESSED
:= & ==
&^= xwindows.NUMLOCK_ON
&^= xwindows.SCROLLLOCK_ON
.Code = .BaseCode
if !unicode.IsControl() {
.Code =
if unicode.IsPrint(.Code) && ( == 0 ||
== xwindows.SHIFT_PRESSED ||
== xwindows.CAPSLOCK_ON ||
== (xwindows.SHIFT_PRESSED|xwindows.CAPSLOCK_ON) ||
) {
.Text = string(.Code)
}
}
.Mod = translateControlKeyState()
= ensureKeyCase(, )
if {
return KeyPressEvent()
}
return KeyReleaseEvent()
}
func ( Key, uint32) Key {
if len(.Text) == 0 {
return
}
:= &xwindows.SHIFT_PRESSED != 0
:= &xwindows.CAPSLOCK_ON != 0
if || {
if unicode.IsLower(.Code) {
.ShiftedCode = unicode.ToUpper(.Code)
.Text = string(.ShiftedCode)
}
} else {
if unicode.IsUpper(.Code) {
.ShiftedCode = unicode.ToLower(.Code)
.Text = string(.ShiftedCode)
}
}
return
}
func ( uint32) ( KeyMod) {
if &xwindows.LEFT_CTRL_PRESSED != 0 || &xwindows.RIGHT_CTRL_PRESSED != 0 {
|= ModCtrl
}
if &xwindows.LEFT_ALT_PRESSED != 0 || &xwindows.RIGHT_ALT_PRESSED != 0 {
|= ModAlt
}
if &xwindows.SHIFT_PRESSED != 0 {
|= ModShift
}
if &xwindows.CAPSLOCK_ON != 0 {
|= ModCapsLock
}
if &xwindows.NUMLOCK_ON != 0 {
|= ModNumLock
}
if &xwindows.SCROLLLOCK_ON != 0 {
|= ModScrollLock
}
return
}