// Copyright 2024 The TCell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !(js && wasm)
// +build !js !wasm

package tcell

import (
	
	
	
	
	
	
	
	
	
	

	
	

	
)

// NewTerminfoScreen returns a Screen that uses the stock TTY interface
// and POSIX terminal control, combined with a terminfo description taken from
// the $TERM environment variable.  It returns an error if the terminal
// is not supported for any reason.
//
// For terminals that do not support dynamic resize events, the $LINES
// $COLUMNS environment variables can be set to the actual window size,
// otherwise defaults taken from the terminal database are used.
func () (Screen, error) {
	return NewTerminfoScreenFromTty(nil)
}

// LookupTerminfo attempts to find a definition for the named $TERM falling
// back to attempting to parse the output from infocmp.
func ( string) ( *terminfo.Terminfo,  error) {
	,  = terminfo.LookupTerminfo()
	if  != nil {
		,  = loadDynamicTerminfo()
		if  != nil {
			return nil, 
		}
		terminfo.AddTerminfo()
	}

	return
}

// NewTerminfoScreenFromTtyTerminfo returns a Screen using a custom Tty
// implementation  and custom terminfo specification.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
// If passed terminfo is nil, then TERM environment variable is queried for
// terminal specification.
func ( Tty,  *terminfo.Terminfo) ( Screen,  error) {
	if  == nil {
		,  = LookupTerminfo(os.Getenv("TERM"))
		if  != nil {
			return
		}
	}

	 := &tScreen{ti: , tty: }

	.keyexist = make(map[Key]bool)
	.keycodes = make(map[string]*tKeyCode)
	if len(.Mouse) > 0 {
		.mouse = []byte(.Mouse)
	}
	.prepareKeys()
	.buildAcsMap()
	.resizeQ = make(chan bool, 1)
	.fallback = make(map[rune]string)
	for ,  := range RuneFallbacks {
		.fallback[] = 
	}

	return &baseScreen{screenImpl: }, nil
}

// NewTerminfoScreenFromTty returns a Screen using a custom Tty implementation.
// If the passed in tty is nil, then a reasonable default (typically /dev/tty)
// is presumed, at least on UNIX hosts. (Windows hosts will typically fail this
// call altogether.)
func ( Tty) (Screen, error) {
	return NewTerminfoScreenFromTtyTerminfo(, nil)
}

// tKeyCode represents a combination of a key code and modifiers.
type tKeyCode struct {
	key Key
	mod ModMask
}

// tScreen represents a screen backed by a terminfo implementation.
type tScreen struct {
	ti           *terminfo.Terminfo
	tty          Tty
	h            int
	w            int
	fini         bool
	cells        CellBuffer
	buffering    bool // true if we are collecting writes to buf instead of sending directly to out
	buf          bytes.Buffer
	curstyle     Style
	style        Style
	resizeQ      chan bool
	quit         chan struct{}
	keyexist     map[Key]bool
	keycodes     map[string]*tKeyCode
	keychan      chan []byte
	keytimer     *time.Timer
	keyexpire    time.Time
	cx           int
	cy           int
	mouse        []byte
	clear        bool
	cursorx      int
	cursory      int
	acs          map[rune]string
	charset      string
	encoder      transform.Transformer
	decoder      transform.Transformer
	fallback     map[rune]string
	colors       map[Color]Color
	palette      []Color
	truecolor    bool
	escaped      bool
	buttondn     bool
	finiOnce     sync.Once
	enablePaste  string
	disablePaste string
	enterUrl     string
	exitUrl      string
	setWinSize   string
	enableFocus  string
	disableFocus string
	doubleUnder  string
	curlyUnder   string
	dottedUnder  string
	dashedUnder  string
	underColor   string
	underRGB     string
	underFg      string
	cursorStyles map[CursorStyle]string
	cursorStyle  CursorStyle
	cursorColor  Color
	cursorRGB    string
	cursorFg     string
	saved        *term.State
	stopQ        chan struct{}
	eventQ       chan Event
	running      bool
	wg           sync.WaitGroup
	mouseFlags   MouseFlags
	pasteEnabled bool
	focusEnabled bool
	setTitle     string
	saveTitle    string
	restoreTitle string
	title        string
	setClipboard string

	sync.Mutex
}

func ( *tScreen) () error {
	if  := .initialize();  != nil {
		return 
	}

	.keychan = make(chan []byte, 10)
	.keytimer = time.NewTimer(time.Millisecond * 50)
	.charset = "UTF-8"

	.charset = getCharset()
	if  := GetEncoding(.charset);  != nil {
		.encoder = .NewEncoder()
		.decoder = .NewDecoder()
	} else {
		return ErrNoCharset
	}
	 := .ti

	// environment overrides
	 := .Columns
	 := .Lines
	if ,  := strconv.Atoi(os.Getenv("LINES"));  != 0 {
		 = 
	}
	if ,  := strconv.Atoi(os.Getenv("COLUMNS"));  != 0 {
		 = 
	}
	if .ti.SetFgBgRGB != "" || .ti.SetFgRGB != "" || .ti.SetBgRGB != "" {
		.truecolor = true
	}
	// A user who wants to have his themes honored can
	// set this environment variable.
	if os.Getenv("TCELL_TRUECOLOR") == "disable" {
		.truecolor = false
	}
	 := .nColors()
	if  > 256 {
		 = 256 // clip to reasonable limits
	}
	.colors = make(map[Color]Color, )
	.palette = make([]Color, )
	for  := 0;  < ; ++ {
		.palette[] = Color() | ColorValid
		// identity map for our builtin colors
		.colors[Color()|ColorValid] = Color() | ColorValid
	}

	.quit = make(chan struct{})
	.eventQ = make(chan Event, 10)

	.Lock()
	.cx = -1
	.cy = -1
	.style = StyleDefault
	.cells.Resize(, )
	.cursorx = -1
	.cursory = -1
	.resize()
	.Unlock()

	if  := .engage();  != nil {
		return 
	}

	return nil
}

func ( *tScreen) ( Key,  ModMask,  string) {
	if  != "" {
		// Do not override codes that already exist
		if ,  := .keycodes[]; ! {
			.keyexist[] = true
			.keycodes[] = &tKeyCode{key: , mod: }
		}
	}
}

func ( *tScreen) ( Key,  Key,  ModMask,  string) {
	if  != "" {
		// Do not override codes that already exist
		if ,  := .keycodes[]; ! || .key ==  {
			.keyexist[] = true
			.keycodes[] = &tKeyCode{key: , mod: }
		}
	}
}

func ( *tScreen) ( Key,  string) {

	if strings.HasPrefix(, "\x1b[") && strings.HasSuffix(, "~") {

		// Drop the trailing ~
		 = [:len()-1]

		// These suffixes are calculated assuming Xterm style modifier suffixes.
		// Please see https://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf for
		// more information (specifically "PC-Style Function Keys").
		.prepareKeyModReplace(, +12, ModShift, +";2~")
		.prepareKeyModReplace(, +48, ModAlt, +";3~")
		.prepareKeyModReplace(, +60, ModAlt|ModShift, +";4~")
		.prepareKeyModReplace(, +24, ModCtrl, +";5~")
		.prepareKeyModReplace(, +36, ModCtrl|ModShift, +";6~")
		.prepareKeyMod(, ModAlt|ModCtrl, +";7~")
		.prepareKeyMod(, ModShift|ModAlt|ModCtrl, +";8~")
		.prepareKeyMod(, ModMeta, +";9~")
		.prepareKeyMod(, ModMeta|ModShift, +";10~")
		.prepareKeyMod(, ModMeta|ModAlt, +";11~")
		.prepareKeyMod(, ModMeta|ModAlt|ModShift, +";12~")
		.prepareKeyMod(, ModMeta|ModCtrl, +";13~")
		.prepareKeyMod(, ModMeta|ModCtrl|ModShift, +";14~")
		.prepareKeyMod(, ModMeta|ModCtrl|ModAlt, +";15~")
		.prepareKeyMod(, ModMeta|ModCtrl|ModAlt|ModShift, +";16~")
	} else if strings.HasPrefix(, "\x1bO") && len() == 3 {
		 = [2:]
		.prepareKeyModReplace(, +12, ModShift, "\x1b[1;2"+)
		.prepareKeyModReplace(, +48, ModAlt, "\x1b[1;3"+)
		.prepareKeyModReplace(, +24, ModCtrl, "\x1b[1;5"+)
		.prepareKeyModReplace(, +36, ModCtrl|ModShift, "\x1b[1;6"+)
		.prepareKeyModReplace(, +60, ModAlt|ModShift, "\x1b[1;4"+)
		.prepareKeyMod(, ModAlt|ModCtrl, "\x1b[1;7"+)
		.prepareKeyMod(, ModShift|ModAlt|ModCtrl, "\x1b[1;8"+)
		.prepareKeyMod(, ModMeta, "\x1b[1;9"+)
		.prepareKeyMod(, ModMeta|ModShift, "\x1b[1;10"+)
		.prepareKeyMod(, ModMeta|ModAlt, "\x1b[1;11"+)
		.prepareKeyMod(, ModMeta|ModAlt|ModShift, "\x1b[1;12"+)
		.prepareKeyMod(, ModMeta|ModCtrl, "\x1b[1;13"+)
		.prepareKeyMod(, ModMeta|ModCtrl|ModShift, "\x1b[1;14"+)
		.prepareKeyMod(, ModMeta|ModCtrl|ModAlt, "\x1b[1;15"+)
		.prepareKeyMod(, ModMeta|ModCtrl|ModAlt|ModShift, "\x1b[1;16"+)
	}
}

func ( *tScreen) () {
	if .ti.Modifiers != terminfo.ModifiersXTerm {
		return
	}
	.prepareKeyModXTerm(KeyRight, .ti.KeyRight)
	.prepareKeyModXTerm(KeyLeft, .ti.KeyLeft)
	.prepareKeyModXTerm(KeyUp, .ti.KeyUp)
	.prepareKeyModXTerm(KeyDown, .ti.KeyDown)
	.prepareKeyModXTerm(KeyInsert, .ti.KeyInsert)
	.prepareKeyModXTerm(KeyDelete, .ti.KeyDelete)
	.prepareKeyModXTerm(KeyPgUp, .ti.KeyPgUp)
	.prepareKeyModXTerm(KeyPgDn, .ti.KeyPgDn)
	.prepareKeyModXTerm(KeyHome, .ti.KeyHome)
	.prepareKeyModXTerm(KeyEnd, .ti.KeyEnd)
	.prepareKeyModXTerm(KeyF1, .ti.KeyF1)
	.prepareKeyModXTerm(KeyF2, .ti.KeyF2)
	.prepareKeyModXTerm(KeyF3, .ti.KeyF3)
	.prepareKeyModXTerm(KeyF4, .ti.KeyF4)
	.prepareKeyModXTerm(KeyF5, .ti.KeyF5)
	.prepareKeyModXTerm(KeyF6, .ti.KeyF6)
	.prepareKeyModXTerm(KeyF7, .ti.KeyF7)
	.prepareKeyModXTerm(KeyF8, .ti.KeyF8)
	.prepareKeyModXTerm(KeyF9, .ti.KeyF9)
	.prepareKeyModXTerm(KeyF10, .ti.KeyF10)
	.prepareKeyModXTerm(KeyF11, .ti.KeyF11)
	.prepareKeyModXTerm(KeyF12, .ti.KeyF12)
}

func ( *tScreen) () {
	// Another workaround for lack of reporting in terminfo.
	// We assume if the terminal has a mouse entry, that it
	// offers bracketed paste.  But we allow specific overrides
	// via our terminal database.
	if .ti.EnablePaste != "" {
		.enablePaste = .ti.EnablePaste
		.disablePaste = .ti.DisablePaste
		.prepareKey(keyPasteStart, .ti.PasteStart)
		.prepareKey(keyPasteEnd, .ti.PasteEnd)
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.enablePaste = "\x1b[?2004h"
		.disablePaste = "\x1b[?2004l"
		.prepareKey(keyPasteStart, "\x1b[200~")
		.prepareKey(keyPasteEnd, "\x1b[201~")
	}
}

func ( *tScreen) () {
	if .ti.DoubleUnderline != "" {
		.doubleUnder = .ti.DoubleUnderline
	} else if .ti.XTermLike {
		.doubleUnder = "\x1b[4:2m"
	}
	if .ti.CurlyUnderline != "" {
		.curlyUnder = .ti.CurlyUnderline
	} else if .ti.XTermLike {
		.curlyUnder = "\x1b[4:3m"
	}
	if .ti.DottedUnderline != "" {
		.dottedUnder = .ti.DottedUnderline
	} else if .ti.XTermLike {
		.dottedUnder = "\x1b[4:4m"
	}
	if .ti.DashedUnderline != "" {
		.dashedUnder = .ti.DashedUnderline
	} else if .ti.XTermLike {
		.dashedUnder = "\x1b[4:5m"
	}

	// Underline colors.  We're not going to rely upon terminfo for this
	// Essentially all terminals that support the curly underlines are
	// expected to also support coloring them too - which reflects actual
	// practice since these were introduced at about the same time.
	if .ti.UnderlineColor != "" {
		.underColor = .ti.UnderlineColor
	} else if .curlyUnder != "" {
		.underColor = "\x1b[58:5:%p1%dm"
	}
	if .ti.UnderlineColorRGB != "" {
		// An interesting wart here is that in order to facilitate
		// using just a single parameter, the Setulc parameter takes
		// the 24-bit color as an integer rather than separate bytes.
		// This matches the "new" style direct color approach that
		// ncurses took, even though everyone else went another way.
		.underRGB = .ti.UnderlineColorRGB
	} else if .underColor != "" {
		.underRGB = "\x1b[58:2::%p1%d:%p2%d:%p3%dm"
	}
	if .ti.UnderlineColorReset != "" {
		.underFg = .ti.UnderlineColorReset
	} else if .curlyUnder != "" {
		.underFg = "\x1b[59m"
	}
}

func ( *tScreen) () {
	// Linux is a special beast - because it has a mouse entry, but does
	// not swallow these OSC commands properly.
	if strings.Contains(.ti.Name, "linux") {
		return
	}
	// More stuff for limits in terminfo.  This time we are applying
	// the most common OSC (operating system commands).  Generally
	// terminals that don't understand these will ignore them.
	// Again, we condition this based on mouse capabilities.
	if .ti.EnterUrl != "" {
		.enterUrl = .ti.EnterUrl
		.exitUrl = .ti.ExitUrl
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.enterUrl = "\x1b]8;%p2%s;%p1%s\x1b\\"
		.exitUrl = "\x1b]8;;\x1b\\"
	}

	if .ti.SetWindowSize != "" {
		.setWinSize = .ti.SetWindowSize
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.setWinSize = "\x1b[8;%p1%p2%d;%dt"
	}

	if .ti.EnableFocusReporting != "" {
		.enableFocus = .ti.EnableFocusReporting
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.enableFocus = "\x1b[?1004h"
	}
	if .ti.DisableFocusReporting != "" {
		.disableFocus = .ti.DisableFocusReporting
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.disableFocus = "\x1b[?1004l"
	}

	if .ti.SetWindowTitle != "" {
		.setTitle = .ti.SetWindowTitle
	} else if .ti.XTermLike {
		.saveTitle = "\x1b[22;2t"
		.restoreTitle = "\x1b[23;2t"
		// this also tries to request that UTF-8 is allowed in the title
		.setTitle = "\x1b[>2t\x1b]2;%p1%s\x1b\\"
	}

	if .setClipboard == "" && .ti.XTermLike {
		// this string takes a base64 string and sends it to the clipboard.
		// it will also be able to retrieve the clipboard using "?" as the
		// sent string, when we support that.
		.setClipboard = "\x1b]52;c;%p1%s\x1b\\"
	}
}

func ( *tScreen) () {
	// Another workaround for lack of reporting in terminfo.
	// We assume if the terminal has a mouse entry, that it
	// offers bracketed paste.  But we allow specific overrides
	// via our terminal database.
	if .ti.CursorDefault != "" {
		.cursorStyles = map[CursorStyle]string{
			CursorStyleDefault:           .ti.CursorDefault,
			CursorStyleBlinkingBlock:     .ti.CursorBlinkingBlock,
			CursorStyleSteadyBlock:       .ti.CursorSteadyBlock,
			CursorStyleBlinkingUnderline: .ti.CursorBlinkingUnderline,
			CursorStyleSteadyUnderline:   .ti.CursorSteadyUnderline,
			CursorStyleBlinkingBar:       .ti.CursorBlinkingBar,
			CursorStyleSteadyBar:         .ti.CursorSteadyBar,
		}
	} else if .ti.Mouse != "" || .ti.XTermLike {
		.cursorStyles = map[CursorStyle]string{
			CursorStyleDefault:           "\x1b[0 q",
			CursorStyleBlinkingBlock:     "\x1b[1 q",
			CursorStyleSteadyBlock:       "\x1b[2 q",
			CursorStyleBlinkingUnderline: "\x1b[3 q",
			CursorStyleSteadyUnderline:   "\x1b[4 q",
			CursorStyleBlinkingBar:       "\x1b[5 q",
			CursorStyleSteadyBar:         "\x1b[6 q",
		}
	}
	if .ti.CursorColorRGB != "" {
		// if it was X11 style with just a single %p1%s, then convert
		.cursorRGB = .ti.CursorColorRGB
	}
	if .ti.CursorColorReset != "" {
		.cursorFg = .ti.CursorColorReset
	}
	if .cursorRGB == "" {
		.cursorRGB = "\x1b]12;%p1%s\007"
		.cursorFg = "\x1b]112\007"
	}

	// convert XTERM style color names to RGB color code.  We have no way to do palette colors
	.cursorRGB = strings.Replace(.cursorRGB, "%p1%s", "#%p1%02x%p2%02x%p3%02x", 1)
}

func ( *tScreen) ( Key,  string) {
	.prepareKeyMod(, ModNone, )
}

func ( *tScreen) () {
	 := .ti
	if strings.HasPrefix(.Name, "xterm") {
		// assume its some form of XTerm clone
		.ti.XTermLike = true
		.XTermLike = true
	}
	.prepareKey(KeyBackspace, .KeyBackspace)
	.prepareKey(KeyF1, .KeyF1)
	.prepareKey(KeyF2, .KeyF2)
	.prepareKey(KeyF3, .KeyF3)
	.prepareKey(KeyF4, .KeyF4)
	.prepareKey(KeyF5, .KeyF5)
	.prepareKey(KeyF6, .KeyF6)
	.prepareKey(KeyF7, .KeyF7)
	.prepareKey(KeyF8, .KeyF8)
	.prepareKey(KeyF9, .KeyF9)
	.prepareKey(KeyF10, .KeyF10)
	.prepareKey(KeyF11, .KeyF11)
	.prepareKey(KeyF12, .KeyF12)
	.prepareKey(KeyF13, .KeyF13)
	.prepareKey(KeyF14, .KeyF14)
	.prepareKey(KeyF15, .KeyF15)
	.prepareKey(KeyF16, .KeyF16)
	.prepareKey(KeyF17, .KeyF17)
	.prepareKey(KeyF18, .KeyF18)
	.prepareKey(KeyF19, .KeyF19)
	.prepareKey(KeyF20, .KeyF20)
	.prepareKey(KeyF21, .KeyF21)
	.prepareKey(KeyF22, .KeyF22)
	.prepareKey(KeyF23, .KeyF23)
	.prepareKey(KeyF24, .KeyF24)
	.prepareKey(KeyF25, .KeyF25)
	.prepareKey(KeyF26, .KeyF26)
	.prepareKey(KeyF27, .KeyF27)
	.prepareKey(KeyF28, .KeyF28)
	.prepareKey(KeyF29, .KeyF29)
	.prepareKey(KeyF30, .KeyF30)
	.prepareKey(KeyF31, .KeyF31)
	.prepareKey(KeyF32, .KeyF32)
	.prepareKey(KeyF33, .KeyF33)
	.prepareKey(KeyF34, .KeyF34)
	.prepareKey(KeyF35, .KeyF35)
	.prepareKey(KeyF36, .KeyF36)
	.prepareKey(KeyF37, .KeyF37)
	.prepareKey(KeyF38, .KeyF38)
	.prepareKey(KeyF39, .KeyF39)
	.prepareKey(KeyF40, .KeyF40)
	.prepareKey(KeyF41, .KeyF41)
	.prepareKey(KeyF42, .KeyF42)
	.prepareKey(KeyF43, .KeyF43)
	.prepareKey(KeyF44, .KeyF44)
	.prepareKey(KeyF45, .KeyF45)
	.prepareKey(KeyF46, .KeyF46)
	.prepareKey(KeyF47, .KeyF47)
	.prepareKey(KeyF48, .KeyF48)
	.prepareKey(KeyF49, .KeyF49)
	.prepareKey(KeyF50, .KeyF50)
	.prepareKey(KeyF51, .KeyF51)
	.prepareKey(KeyF52, .KeyF52)
	.prepareKey(KeyF53, .KeyF53)
	.prepareKey(KeyF54, .KeyF54)
	.prepareKey(KeyF55, .KeyF55)
	.prepareKey(KeyF56, .KeyF56)
	.prepareKey(KeyF57, .KeyF57)
	.prepareKey(KeyF58, .KeyF58)
	.prepareKey(KeyF59, .KeyF59)
	.prepareKey(KeyF60, .KeyF60)
	.prepareKey(KeyF61, .KeyF61)
	.prepareKey(KeyF62, .KeyF62)
	.prepareKey(KeyF63, .KeyF63)
	.prepareKey(KeyF64, .KeyF64)
	.prepareKey(KeyInsert, .KeyInsert)
	.prepareKey(KeyDelete, .KeyDelete)
	.prepareKey(KeyHome, .KeyHome)
	.prepareKey(KeyEnd, .KeyEnd)
	.prepareKey(KeyUp, .KeyUp)
	.prepareKey(KeyDown, .KeyDown)
	.prepareKey(KeyLeft, .KeyLeft)
	.prepareKey(KeyRight, .KeyRight)
	.prepareKey(KeyPgUp, .KeyPgUp)
	.prepareKey(KeyPgDn, .KeyPgDn)
	.prepareKey(KeyHelp, .KeyHelp)
	.prepareKey(KeyPrint, .KeyPrint)
	.prepareKey(KeyCancel, .KeyCancel)
	.prepareKey(KeyExit, .KeyExit)
	.prepareKey(KeyBacktab, .KeyBacktab)

	.prepareKeyMod(KeyRight, ModShift, .KeyShfRight)
	.prepareKeyMod(KeyLeft, ModShift, .KeyShfLeft)
	.prepareKeyMod(KeyUp, ModShift, .KeyShfUp)
	.prepareKeyMod(KeyDown, ModShift, .KeyShfDown)
	.prepareKeyMod(KeyHome, ModShift, .KeyShfHome)
	.prepareKeyMod(KeyEnd, ModShift, .KeyShfEnd)
	.prepareKeyMod(KeyPgUp, ModShift, .KeyShfPgUp)
	.prepareKeyMod(KeyPgDn, ModShift, .KeyShfPgDn)

	.prepareKeyMod(KeyRight, ModCtrl, .KeyCtrlRight)
	.prepareKeyMod(KeyLeft, ModCtrl, .KeyCtrlLeft)
	.prepareKeyMod(KeyUp, ModCtrl, .KeyCtrlUp)
	.prepareKeyMod(KeyDown, ModCtrl, .KeyCtrlDown)
	.prepareKeyMod(KeyHome, ModCtrl, .KeyCtrlHome)
	.prepareKeyMod(KeyEnd, ModCtrl, .KeyCtrlEnd)

	// Sadly, xterm handling of keycodes is somewhat erratic.  In
	// particular, different codes are sent depending on application
	// mode is in use or not, and the entries for many of these are
	// simply absent from terminfo on many systems.  So we insert
	// a number of escape sequences if they are not already used, in
	// order to have the widest correct usage.  Note that prepareKey
	// will not inject codes if the escape sequence is already known.
	// We also only do this for terminals that have the application
	// mode present.

	// Cursor mode
	if .EnterKeypad != "" {
		.prepareKey(KeyUp, "\x1b[A")
		.prepareKey(KeyDown, "\x1b[B")
		.prepareKey(KeyRight, "\x1b[C")
		.prepareKey(KeyLeft, "\x1b[D")
		.prepareKey(KeyEnd, "\x1b[F")
		.prepareKey(KeyHome, "\x1b[H")
		.prepareKey(KeyDelete, "\x1b[3~")
		.prepareKey(KeyHome, "\x1b[1~")
		.prepareKey(KeyEnd, "\x1b[4~")
		.prepareKey(KeyPgUp, "\x1b[5~")
		.prepareKey(KeyPgDn, "\x1b[6~")

		// Application mode
		.prepareKey(KeyUp, "\x1bOA")
		.prepareKey(KeyDown, "\x1bOB")
		.prepareKey(KeyRight, "\x1bOC")
		.prepareKey(KeyLeft, "\x1bOD")
		.prepareKey(KeyHome, "\x1bOH")
	}

	.prepareKey(keyPasteStart, .PasteStart)
	.prepareKey(keyPasteEnd, .PasteEnd)
	.prepareXtermModifiers()
	.prepareBracketedPaste()
	.prepareCursorStyles()
	.prepareUnderlines()
	.prepareExtendedOSC()

:
	// Add key mappings for control keys.
	for  := 0;  < ' '; ++ {
		// Do not insert direct key codes for ambiguous keys.
		// For example, ESC is used for lots of other keys, so
		// when parsing this we don't want to fast path handling
		// of it, but instead wait a bit before parsing it as in
		// isolation.
		for  := range .keycodes {
			if []byte()[0] == byte() {
				continue 
			}
		}

		.keyexist[Key()] = true

		 := ModCtrl
		switch Key() {
		case KeyBS, KeyTAB, KeyESC, KeyCR:
			// directly type-able- no control sequence
			 = ModNone
		}
		.keycodes[string(rune())] = &tKeyCode{key: Key(), mod: }
	}
}

func ( *tScreen) () {
	.finiOnce.Do(.finish)
}

func ( *tScreen) () {
	close(.quit)
	.finalize()
}

func ( *tScreen) ( Style) {
	.Lock()
	if !.fini {
		.style = 
	}
	.Unlock()
}

func ( *tScreen) ( rune,  []byte) []byte {

	 := make([]byte, 6)
	 := make([]byte, 6)
	 := utf8.EncodeRune(, )
	 = [:]
	 := 0
	var  error
	if  := .encoder;  != nil {
		.Reset()
		, _,  = .Transform(, , true)
	}
	if  != nil ||  == 0 || [0] == '\x1a' {
		// Combining characters are elided
		if len() == 0 {
			if ,  := .acs[];  {
				 = append(, []byte()...)
			} else if ,  := .fallback[];  {
				 = append(, []byte()...)
			} else {
				 = append(, '?')
			}
		}
	} else {
		 = append(, [:]...)
	}

	return 
}

func ( *tScreen) ( Color,  Color,  AttrMask) AttrMask {
	 := .ti
	if .Colors == 0 {
		// foreground vs background, we calculate luminance
		// and possibly do a reverse video
		if !.Valid() {
			return 
		}
		,  := .colors[]
		if ! {
			 = FindColor(, []Color{ColorBlack, ColorWhite})
			.colors[] = 
		}
		switch  {
		case ColorWhite:
			return 
		case ColorBlack:
			return  ^ AttrReverse
		}
	}

	if  == ColorReset ||  == ColorReset {
		.TPuts(.ResetFgBg)
	}
	if .truecolor {
		if .SetFgBgRGB != "" && .IsRGB() && .IsRGB() {
			, ,  := .RGB()
			, ,  := .RGB()
			.TPuts(.TParm(.SetFgBgRGB,
				int(), int(), int(),
				int(), int(), int()))
			return 
		}

		if .IsRGB() && .SetFgRGB != "" {
			, ,  := .RGB()
			.TPuts(.TParm(.SetFgRGB, int(), int(), int()))
			 = ColorDefault
		}

		if .IsRGB() && .SetBgRGB != "" {
			, ,  := .RGB()
			.TPuts(.TParm(.SetBgRGB,
				int(), int(), int()))
			 = ColorDefault
		}
	}

	if .Valid() {
		if ,  := .colors[];  {
			 = 
		} else {
			 = FindColor(, .palette)
			.colors[] = 
			 = 
		}
	}

	if .Valid() {
		if ,  := .colors[];  {
			 = 
		} else {
			 = FindColor(, .palette)
			.colors[] = 
			 = 
		}
	}

	if .Valid() && .Valid() && .SetFgBg != "" {
		.TPuts(.TParm(.SetFgBg, int(&0xff), int(&0xff)))
	} else {
		if .Valid() && .SetFg != "" {
			.TPuts(.TParm(.SetFg, int(&0xff)))
		}
		if .Valid() && .SetBg != "" {
			.TPuts(.TParm(.SetBg, int(&0xff)))
		}
	}
	return 
}

func ( *tScreen) (,  int) int {

	 := .ti

	, , ,  := .cells.GetContent(, )
	if !.cells.Dirty(, ) {
		return 
	}

	if  == .h-1 &&  == .w-1 && .ti.AutoMargin && .DisableAutoMargin == "" && .InsertChar != "" {
		// our solution is somewhat goofy.
		// we write to the second to the last cell what we want in the last cell, then we
		// insert a character at that 2nd to last position to shift the last column into
		// place, then we rewrite that 2nd to last cell.  Old terminals suck.
		.TPuts(.TGoto(-1, ))
		defer func() {
			.TPuts(.TGoto(-1, ))
			.TPuts(.InsertChar)
			.cy = 
			.cx =  - 1
			.cells.SetDirty(-1, , true)
			_ = .(-1, )
			.TPuts(.ti.TGoto(0, 0))
			.cy = 0
			.cx = 0
		}()
	} else if .cy !=  || .cx !=  {
		.TPuts(.TGoto(, ))
		.cx = 
		.cy = 
	}

	if  == StyleDefault {
		 = .style
	}
	if  != .curstyle {
		, ,  := .fg, .bg, .attrs

		.TPuts(.AttrOff)

		 = .sendFgBg(, , )
		if &AttrBold != 0 {
			.TPuts(.Bold)
		}
		if ,  := .ulStyle, .ulColor;  != UnderlineStyleNone {
			if .underColor != "" || .underRGB != "" {
				if  == ColorReset {
					.TPuts(.underFg)
				} else if .IsRGB() {
					if .underRGB != "" {
						, ,  := .RGB()
						.TPuts(.TParm(.underRGB, int(), int(), int()))
					} else {
						if ,  := .colors[];  {
							 = 
						} else {
							 = FindColor(, .palette)
							.colors[] = 
							 = 
						}
						.TPuts(.TParm(.underColor, int(&0xff)))
					}
				} else if .Valid() {
					.TPuts(.TParm(.underColor, int(&0xff)))
				}
			}
			.TPuts(.Underline) // to ensure everyone gets at least a basic underline
			switch  {
			case UnderlineStyleDouble:
				.TPuts(.doubleUnder)
			case UnderlineStyleCurly:
				.TPuts(.curlyUnder)
			case UnderlineStyleDotted:
				.TPuts(.dottedUnder)
			case UnderlineStyleDashed:
				.TPuts(.dashedUnder)
			}
		}
		if &AttrReverse != 0 {
			.TPuts(.Reverse)
		}
		if &AttrBlink != 0 {
			.TPuts(.Blink)
		}
		if &AttrDim != 0 {
			.TPuts(.Dim)
		}
		if &AttrItalic != 0 {
			.TPuts(.Italic)
		}
		if &AttrStrikeThrough != 0 {
			.TPuts(.StrikeThrough)
		}

		// URL string can be long, so don't send it unless we really need to
		if .enterUrl != "" && .curstyle.url != .url {
			if .url != "" {
				.TPuts(.TParm(.enterUrl, .url, .urlId))
			} else {
				.TPuts(.exitUrl)
			}
		}

		.curstyle = 
	}

	// now emit runes - taking care to not overrun width with a
	// wide character, and to ensure that we emit exactly one regular
	// character followed up by any residual combing characters

	if  < 1 {
		 = 1
	}

	var  string

	 := make([]byte, 0, 6)

	 = .encodeRune(, )
	for ,  := range  {
		 = .encodeRune(, )
	}

	 = string()
	if  > 1 &&  == "?" {
		// No FullWidth character support
		 = "? "
		.cx = -1
	}

	if  > .w- {
		// too wide to fit; emit a single space instead
		 = 1
		 = " "
	}
	.writeString()
	.cx += 
	.cells.SetDirty(, , false)
	if  > 1 {
		.cx = -1
	}

	return 
}

func ( *tScreen) (,  int) {
	.Lock()
	.cursorx = 
	.cursory = 
	.Unlock()
}

func ( *tScreen) ( CursorStyle,  Color) {
	.Lock()
	.cursorStyle = 
	.cursorColor = 
	.Unlock()
}

func ( *tScreen) () {
	.ShowCursor(-1, -1)
}

func ( *tScreen) () {

	,  := .cursorx, .cursory
	,  := .cells.Size()
	if  < 0 ||  < 0 ||  >=  ||  >=  {
		.hideCursor()
		return
	}
	.TPuts(.ti.TGoto(, ))
	.TPuts(.ti.ShowCursor)
	if .cursorStyles != nil {
		if ,  := .cursorStyles[.cursorStyle];  {
			.TPuts()
		}
	}
	if .cursorRGB != "" {
		if .cursorColor == ColorReset {
			.TPuts(.cursorFg)
		} else if .cursorColor.Valid() {
			, ,  := .cursorColor.RGB()
			.TPuts(.ti.TParm(.cursorRGB, int(), int(), int()))
		}
	}
	.cx = 
	.cy = 
}

// writeString sends a string to the terminal. The string is sent as-is and
// this function does not expand inline padding indications (of the form
// $<[delay]> where [delay] is msec). In order to have these expanded, use
// TPuts. If the screen is "buffering", the string is collected in a buffer,
// with the intention that the entire buffer be sent to the terminal in one
// write operation at some point later.
func ( *tScreen) ( string) {
	if .buffering {
		_, _ = io.WriteString(&.buf, )
	} else {
		_, _ = io.WriteString(.tty, )
	}
}

func ( *tScreen) ( string) {
	if .buffering {
		.ti.TPuts(&.buf, )
	} else {
		.ti.TPuts(.tty, )
	}
}

func ( *tScreen) () {
	.Lock()
	if !.fini {
		.resize()
		.draw()
	}
	.Unlock()
}

func ( *tScreen) () {
	.TPuts(.ti.AttrOff)
	.TPuts(.exitUrl)
	_ = .sendFgBg(.style.fg, .style.bg, AttrNone)
	.TPuts(.ti.Clear)
	.clear = false
}

func ( *tScreen) () {
	// does not update cursor position
	if .ti.HideCursor != "" {
		.TPuts(.ti.HideCursor)
	} else {
		// No way to hide cursor, stick it
		// at bottom right of screen
		.cx, .cy = .cells.Size()
		.TPuts(.ti.TGoto(.cx, .cy))
	}
}

func ( *tScreen) () {
	// clobber cursor position, because we're going to change it all
	.cx = -1
	.cy = -1
	// make no style assumptions
	.curstyle = styleInvalid

	.buf.Reset()
	.buffering = true
	defer func() {
		.buffering = false
	}()

	// hide the cursor while we move stuff around
	.hideCursor()

	if .clear {
		.clearScreen()
	}

	for  := 0;  < .h; ++ {
		for  := 0;  < .w; ++ {
			 := .drawCell(, )
			if  > 1 {
				if +1 < .w {
					// this is necessary so that if we ever
					// go back to drawing that cell, we
					// actually will *draw* it.
					.cells.SetDirty(+1, , true)
				}
			}
			 +=  - 1
		}
	}

	// restore the cursor
	.showCursor()

	_, _ = .buf.WriteTo(.tty)
}

func ( *tScreen) ( ...MouseFlags) {
	var  MouseFlags
	 := false
	for ,  := range  {
		 |= 
		 = true
	}
	if ! {
		 = MouseMotionEvents | MouseDragEvents | MouseButtonEvents
	}

	.Lock()
	.mouseFlags = 
	.enableMouse()
	.Unlock()
}

func ( *tScreen) ( MouseFlags) {
	// Rather than using terminfo to find mouse escape sequences, we rely on the fact that
	// pretty much *every* terminal that supports mouse tracking follows the
	// XTerm standards (the modern ones).
	if len(.mouse) != 0 {
		// start by disabling all tracking.
		.TPuts("\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l")
		if &MouseButtonEvents != 0 {
			.TPuts("\x1b[?1000h")
		}
		if &MouseDragEvents != 0 {
			.TPuts("\x1b[?1002h")
		}
		if &MouseMotionEvents != 0 {
			.TPuts("\x1b[?1003h")
		}
		if &(MouseButtonEvents|MouseDragEvents|MouseMotionEvents) != 0 {
			.TPuts("\x1b[?1006h")
		}
	}

}

func ( *tScreen) () {
	.Lock()
	.mouseFlags = 0
	.enableMouse(0)
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.pasteEnabled = true
	.enablePasting(true)
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.pasteEnabled = false
	.enablePasting(false)
	.Unlock()
}

func ( *tScreen) ( bool) {
	var  string
	if  {
		 = .enablePaste
	} else {
		 = .disablePaste
	}
	if  != "" {
		.TPuts()
	}
}

func ( *tScreen) () {
	.Lock()
	.focusEnabled = true
	.enableFocusReporting()
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	.focusEnabled = false
	.disableFocusReporting()
	.Unlock()
}

func ( *tScreen) () {
	if .enableFocus != "" {
		.TPuts(.enableFocus)
	}
}

func ( *tScreen) () {
	if .disableFocus != "" {
		.TPuts(.disableFocus)
	}
}

func ( *tScreen) () (int, int) {
	.Lock()
	,  := .w, .h
	.Unlock()
	return , 
}

func ( *tScreen) () {
	,  := .tty.WindowSize()
	if  != nil {
		return
	}
	if .Width == .w && .Height == .h {
		return
	}
	.cx = -1
	.cy = -1

	.cells.Resize(.Width, .Height)
	.cells.Invalidate()
	.h = .Height
	.w = .Width
	 := &EventResize{t: time.Now(), ws: }
	select {
	case .eventQ <- :
	default:
	}
}

func ( *tScreen) () int {
	// this doesn't change, no need for lock
	if .truecolor {
		return 1 << 24
	}
	return .ti.Colors
}

// nColors returns the size of the built-in palette.
// This is distinct from Colors(), as it will generally
// always be a small number. (<= 256)
func ( *tScreen) () int {
	return .ti.Colors
}

// vtACSNames is a map of bytes defined by terminfo that are used in
// the terminals Alternate Character Set to represent other glyphs.
// For example, the upper left corner of the box drawing set can be
// displayed by printing "l" while in the alternate character set.
// It's not quite that simple, since the "l" is the terminfo name,
// and it may be necessary to use a different character based on
// the terminal implementation (or the terminal may lack support for
// this altogether).  See buildAcsMap below for detail.
var vtACSNames = map[byte]rune{
	'+': RuneRArrow,
	',': RuneLArrow,
	'-': RuneUArrow,
	'.': RuneDArrow,
	'0': RuneBlock,
	'`': RuneDiamond,
	'a': RuneCkBoard,
	'b': '␉', // VT100, Not defined by terminfo
	'c': '␌', // VT100, Not defined by terminfo
	'd': '␋', // VT100, Not defined by terminfo
	'e': '␊', // VT100, Not defined by terminfo
	'f': RuneDegree,
	'g': RunePlMinus,
	'h': RuneBoard,
	'i': RuneLantern,
	'j': RuneLRCorner,
	'k': RuneURCorner,
	'l': RuneULCorner,
	'm': RuneLLCorner,
	'n': RunePlus,
	'o': RuneS1,
	'p': RuneS3,
	'q': RuneHLine,
	'r': RuneS7,
	's': RuneS9,
	't': RuneLTee,
	'u': RuneRTee,
	'v': RuneBTee,
	'w': RuneTTee,
	'x': RuneVLine,
	'y': RuneLEqual,
	'z': RuneGEqual,
	'{': RunePi,
	'|': RuneNEqual,
	'}': RuneSterling,
	'~': RuneBullet,
}

// buildAcsMap builds a map of characters that we translate from Unicode to
// alternate character encodings.  To do this, we use the standard VT100 ACS
// maps.  This is only done if the terminal lacks support for Unicode; we
// always prefer to emit Unicode glyphs when we are able.
func ( *tScreen) () {
	 := .ti.AltChars
	.acs = make(map[rune]string)
	for len() > 2 {
		 := [0]
		 := string([1])
		if ,  := vtACSNames[];  {
			.acs[] = .ti.EnterAcs +  + .ti.ExitAcs
		}
		 = [2:]
	}
}

func ( *tScreen) (,  int) (int, int) {
	,  := .cells.Size()
	if  < 0 {
		 = 0
	}
	if  < 0 {
		 = 0
	}
	if  > -1 {
		 =  - 1
	}
	if  > -1 {
		 =  - 1
	}
	return , 
}

// buildMouseEvent returns an event based on the supplied coordinates and button
// state. Note that the screen's mouse button state is updated based on the
// input to this function (i.e. it mutates the receiver).
func ( *tScreen) (, ,  int) *EventMouse {

	// XTerm mouse events only report at most one button at a time,
	// which may include a wheel button.  Wheel motion events are
	// reported as single impulses, while other button events are reported
	// as separate press & release events.

	 := ButtonNone
	 := ModNone

	// Mouse wheel has bit 6 set, no release events.  It should be noted
	// that wheel events are sometimes misdelivered as mouse button events
	// during a click-drag, so we debounce these, considering them to be
	// button press events unless we see an intervening release event.
	switch  & 0x43 {
	case 0:
		 = Button1
	case 1:
		 = Button3 // Note we prefer to treat right as button 2
	case 2:
		 = Button2 // And the middle button as button 3
	case 3:
		 = ButtonNone
	case 0x40:
		 = WheelUp
	case 0x41:
		 = WheelDown
	case 0x42:
		 = WheelLeft
	case 0x43:
		 = WheelRight
	}

	if &0x4 != 0 {
		 |= ModShift
	}
	if &0x8 != 0 {
		 |= ModAlt
	}
	if &0x10 != 0 {
		 |= ModCtrl
	}

	// Some terminals will report mouse coordinates outside the
	// screen, especially with click-drag events.  Clip the coordinates
	// to the screen in that case.
	,  = .clip(, )

	return NewEventMouse(, , , )
}

// parseSgrMouse attempts to locate an SGR mouse record at the start of the
// buffer.  It returns true, true if it found one, and the associated bytes
// be removed from the buffer.  It returns true, false if the buffer might
// contain such an event, but more bytes are necessary (partial match), and
// false, false if the content is definitely *not* an SGR mouse record.
func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {

	 := .Bytes()

	var , , ,  int
	 := false
	 := false
	 := false
	 := false
	 := 0
	 := 0

	for  = range  {
		switch [] {
		case '\x1b':
			if  != 0 {
				return false, false
			}
			 = 1

		case '\x9b':
			if  != 0 {
				return false, false
			}
			 = 2

		case '[':
			if  != 1 {
				return false, false
			}
			 = 2

		case '<':
			if  != 2 {
				return false, false
			}
			 = 0
			 = false
			 = false
			 = 3

		case '-':
			if  != 3 &&  != 4 &&  != 5 {
				return false, false
			}
			if  ||  {
				return false, false
			}
			 = true // stay in state

		case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
			if  != 3 &&  != 4 &&  != 5 {
				return false, false
			}
			 *= 10
			 += int([] - '0')
			 = true // stay in state

		case ';':
			if  {
				 = -
			}
			switch  {
			case 3:
				,  = , 0
				, ,  = false, false, 4
			case 4:
				,  = -1, 0
				, ,  = false, false, 5
			default:
				return false, false
			}

		case 'm', 'M':
			if  != 5 {
				return false, false
			}
			if  {
				 = -
			}
			 =  - 1

			 = ( & 32) != 0
			 = ( & 0x42) == 0x40
			 &^= 32
			if [] == 'm' {
				// mouse release, clear all buttons
				 |= 3
				 &^= 0x40
				.buttondn = false
			} else if  {
				/*
				 * Some broken terminals appear to send
				 * mouse button one motion events, instead of
				 * encoding 35 (no buttons) into these events.
				 * We resolve these by looking for a non-motion
				 * event first.
				 */
				if !.buttondn {
					 |= 3
					 &^= 0x40
				}
			} else if ! {
				.buttondn = true
			}
			// consume the event bytes
			for  >= 0 {
				_, _ = .ReadByte()
				--
			}
			* = append(*, .buildMouseEvent(, , ))
			return true, true
		}
	}

	// incomplete & inconclusive at this point
	return true, false
}

func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {
	 := 0
	 := .Bytes()
	for  := range  {
		switch  {
		case 0:
			if [] != '\x1b' {
				return false, false
			}
			 = 1
		case 1:
			if [] != '[' {
				return false, false
			}
			 = 2
		case 2:
			if [] != 'I' && [] != 'O' {
				return false, false
			}
			* = append(*, NewEventFocus([] == 'I'))
			_, _ = .ReadByte()
			_, _ = .ReadByte()
			_, _ = .ReadByte()
			return true, true
		}
	}
	return true, false
}

func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {
	 := .Bytes()
	 := 0
	 := []byte("\x1b]52;c;")

	if len() >= len() {
		if bytes.HasPrefix(, ) {
			// inconclusive so far
			return true, false
		}
		// definitely not a match
		return false, false
	}
	 = [len():]

	for ,  := range  {
		// valid base64 digits
		if  == 0 {
			if ( >= 'A' &&  <= 'Z') || ( >= 'a' &&  <= 'z') || ( >= '0' &&  <= '9') || ( == '+') || ( == '/') || ( == '=') {
				continue
			}
			if  == '\x1b' {
				 = 1
				continue
			}
			if  == '\a' {
				// matched with BEL instead of ST
				 = [:len()-1] // drop the trailing BEL
				 := make([]byte, base64.StdEncoding.DecodedLen(len()))
				if ,  := base64.StdEncoding.Decode(, );  == nil {
					* = append(*, NewEventClipboard([:]))
				}
				_, _ = .ReadBytes('\a')
				return true, true
			}
			return false, false
		}
		if  == 1 {
			if  == '\\' {
				 = [:len()-2] // drop the trailing ST (\x1b\\)
				// now decode the data
				 := make([]byte, base64.StdEncoding.DecodedLen(len()))
				if ,  := base64.StdEncoding.Decode(, );  == nil {
					* = append(*, NewEventClipboard([:]))
				}
				_, _ = .ReadBytes('\\')
				return true, true
			}
			return false, false
		}
	}
	// not enough data yet (not terminated)
	return true, false
}

// parseXtermMouse is like parseSgrMouse, but it parses a legacy
// X11 mouse record.
func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {

	 := .Bytes()

	 := 0
	 := 0
	 := 0
	 := 0

	for  := range  {
		switch  {
		case 0:
			switch [] {
			case '\x1b':
				 = 1
			case '\x9b':
				 = 2
			default:
				return false, false
			}
		case 1:
			if [] != '[' {
				return false, false
			}
			 = 2
		case 2:
			if [] != 'M' {
				return false, false
			}
			++
		case 3:
			 = int([])
			++
		case 4:
			 = int([]) - 32 - 1
			++
		case 5:
			 = int([]) - 32 - 1
			for  >= 0 {
				_, _ = .ReadByte()
				--
			}
			* = append(*, .buildMouseEvent(, , ))
			return true, true
		}
	}
	return true, false
}

func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {
	 := .Bytes()
	 := false
	for ,  := range .keycodes {
		 := []byte()
		if (len() == 1) && ([0] == '\x1b') {
			continue
		}
		if bytes.HasPrefix(, ) {
			// matched
			var  rune
			if len() == 1 {
				 = rune([0])
			}
			 := .mod
			if .escaped {
				 |= ModAlt
				.escaped = false
			}
			switch .key {
			case keyPasteStart:
				* = append(*, NewEventPaste(true))
			case keyPasteEnd:
				* = append(*, NewEventPaste(false))
			default:
				* = append(*, NewEventKey(.key, , ))
			}
			for  := 0;  < len(); ++ {
				_, _ = .ReadByte()
			}
			return true, true
		}
		if bytes.HasPrefix(, ) {
			 = true
		}
	}
	return , false
}

func ( *tScreen) ( *bytes.Buffer,  *[]Event) (bool, bool) {
	 := .Bytes()
	if [0] >= ' ' && [0] <= 0x7F {
		// printable ASCII easy to deal with -- no encodings
		 := ModNone
		if .escaped {
			 = ModAlt
			.escaped = false
		}
		* = append(*, NewEventKey(KeyRune, rune([0]), ))
		_, _ = .ReadByte()
		return true, true
	}

	if [0] < 0x80 {
		// Low numbered values are control keys, not runes.
		return false, false
	}

	 := make([]byte, 12)
	for  := 1;  <= len(); ++ {
		.decoder.Reset()
		, ,  := .decoder.Transform(, [:], true)
		if  == transform.ErrShortSrc {
			continue
		}
		if  != 0 {
			,  := utf8.DecodeRune([:])
			if  != utf8.RuneError {
				 := ModNone
				if .escaped {
					 = ModAlt
					.escaped = false
				}
				* = append(*, NewEventKey(KeyRune, , ))
			}
			for  > 0 {
				_, _ = .ReadByte()
				--
			}
			return true, true
		}
	}
	// Looks like potential escape
	return true, false
}

func ( *tScreen) ( *bytes.Buffer,  bool) {
	 := .collectEventsFromInput(, )

	for ,  := range  {
		select {
		case .eventQ <- :
		case <-.quit:
			return
		}
	}
}

// Return an array of Events extracted from the supplied buffer. This is done
// while holding the screen's lock - the events can then be queued for
// application processing with the lock released.
func ( *tScreen) ( *bytes.Buffer,  bool) []Event {

	 := make([]Event, 0, 20)

	.Lock()
	defer .Unlock()

	for {
		 := .Bytes()
		if len() == 0 {
			.Reset()
			return 
		}

		 := 0

		if ,  := .parseRune(, &);  {
			continue
		} else if  {
			++
		}

		if ,  := .parseFunctionKey(, &);  {
			continue
		} else if  {
			++
		}

		if ,  := .parseFocus(, &);  {
			continue
		} else if  {
			++
		}

		// Only parse mouse records if this term claims to have
		// mouse support

		if .ti.Mouse != "" {
			if ,  := .parseXtermMouse(, &);  {
				continue
			} else if  {
				++
			}

			if ,  := .parseSgrMouse(, &);  {
				continue
			} else if  {
				++
			}
		}

		if .setClipboard != "" {
			if ,  := .parseClipboard(, &);  {
				continue
			} else if  {
				++
			}
		}

		if  == 0 ||  {
			if [0] == '\x1b' {
				if len() == 1 {
					 = append(, NewEventKey(KeyEsc, 0, ModNone))
					.escaped = false
				} else {
					.escaped = true
				}
				_, _ = .ReadByte()
				continue
			}
			// Nothing was going to match, or we timed-out
			// waiting for more data -- just deliver the characters
			// to the app & let them sort it out.  Possibly we
			// should only do this for control characters like ESC.
			,  := .ReadByte()
			 := ModNone
			if .escaped {
				.escaped = false
				 = ModAlt
			}
			 = append(, NewEventKey(KeyRune, rune(), ))
			continue
		}

		// well we have some partial data, wait until we get
		// some more
		break
	}

	return 
}

func ( *tScreen) ( chan struct{}) {
	defer .wg.Done()
	 := &bytes.Buffer{}
	for {
		select {
		case <-:
			return
		case <-.quit:
			return
		case <-.resizeQ:
			.Lock()
			.cx = -1
			.cy = -1
			.resize()
			.cells.Invalidate()
			.draw()
			.Unlock()
			continue
		case <-.keytimer.C:
			// If the timer fired, and the current time
			// is after the expiration of the escape sequence,
			// then we assume the escape sequence reached its
			// conclusion, and process the chunk independently.
			// This lets us detect conflicts such as a lone ESC.
			if .Len() > 0 {
				if time.Now().After(.keyexpire) {
					.scanInput(, true)
				}
			}
			if .Len() > 0 {
				if !.keytimer.Stop() {
					select {
					case <-.keytimer.C:
					default:
					}
				}
				.keytimer.Reset(time.Millisecond * 50)
			}
		case  := <-.keychan:
			.Write()
			.keyexpire = time.Now().Add(time.Millisecond * 50)
			.scanInput(, false)
			if !.keytimer.Stop() {
				select {
				case <-.keytimer.C:
				default:
				}
			}
			if .Len() > 0 {
				.keytimer.Reset(time.Millisecond * 50)
			}
		}
	}
}

func ( *tScreen) ( chan struct{}) {

	defer .wg.Done()
	for {
		select {
		case <-:
			return
		default:
		}
		 := make([]byte, 128)
		,  := .tty.Read()
		switch  {
		case nil:
		default:
			.Lock()
			 := .running
			.Unlock()
			if  {
				select {
				case .eventQ <- NewEventError():
				case <-.quit:
				}
			}
			return
		}
		if  > 0 {
			.keychan <- [:]
		}
	}
}

func ( *tScreen) () {
	.Lock()
	.cx = -1
	.cy = -1
	if !.fini {
		.resize()
		.clear = true
		.cells.Invalidate()
		.draw()
	}
	.Unlock()
}

func ( *tScreen) () string {
	return .charset
}

func ( *tScreen) ( rune,  string) {
	.Lock()
	.fallback[] = 
	.Unlock()
}

func ( *tScreen) ( rune) {
	.Lock()
	delete(.fallback, )
	.Unlock()
}

func ( *tScreen) ( rune,  bool) bool {

	if  := .encoder;  != nil {
		 := make([]byte, 6)
		 := make([]byte, 6)
		 := utf8.EncodeRune(, )

		.Reset()
		, ,  := .Transform(, [:], true)
		if  != 0 &&  == nil && [0] != '\x1A' {
			return true
		}
	}
	// Terminal fallbacks always permitted, since we assume they are
	// basically nearly perfect renditions.
	if ,  := .acs[];  {
		return true
	}
	if ! {
		return false
	}
	if ,  := .fallback[];  {
		return true
	}
	return false
}

func ( *tScreen) () bool {
	return len(.mouse) != 0
}

func ( *tScreen) ( Key) bool {
	if  == KeyRune {
		return true
	}
	return .keyexist[]
}

func ( *tScreen) (,  int) {
	if .setWinSize != "" {
		.TPuts(.ti.TParm(.setWinSize, , ))
	}
	.cells.Invalidate()
	.resize()
}

func ( *tScreen) (int, int, int, int) {}

func ( *tScreen) () error {
	.disengage()
	return nil
}

func ( *tScreen) () error {
	return .engage()
}

func ( *tScreen) () (Tty, bool) {
	return .tty, true
}

// engage is used to place the terminal in raw mode and establish screen size, etc.
// Think of this is as tcell "engaging" the clutch, as it's going to be driving the
// terminal interface.
func ( *tScreen) () error {
	.Lock()
	defer .Unlock()
	if .tty == nil {
		return ErrNoScreen
	}
	.tty.NotifyResize(func() {
		select {
		case .resizeQ <- true:
		default:
		}
	})
	if .running {
		return errors.New("already engaged")
	}
	if  := .tty.Start();  != nil {
		return 
	}
	.running = true
	if ,  := .tty.WindowSize();  == nil && .Width != 0 && .Height != 0 {
		.cells.Resize(.Width, .Height)
	}
	 := make(chan struct{})
	.stopQ = 
	.enableMouse(.mouseFlags)
	.enablePasting(.pasteEnabled)
	if .focusEnabled {
		.enableFocusReporting()
	}

	 := .ti
	if os.Getenv("TCELL_ALTSCREEN") != "disable" {
		// Technically this may not be right, but every terminal we know about
		// (even Wyse 60) uses this to enter the alternate screen buffer, and
		// possibly save and restore the window title and/or icon.
		// (In theory there could be terminals that don't support X,Y cursor
		// positions without a setup command, but we don't support them.)
		.TPuts(.EnterCA)
		if .saveTitle != "" {
			.TPuts(.saveTitle)
		}
	}
	.TPuts(.EnterKeypad)
	.TPuts(.HideCursor)
	.TPuts(.EnableAcs)
	.TPuts(.DisableAutoMargin)
	.TPuts(.Clear)
	if .title != "" && .setTitle != "" {
		.TPuts(.ti.TParm(.setTitle, .title))
	}

	.wg.Add(2)
	go .inputLoop()
	go .mainLoop()
	return nil
}

// disengage is used to release the terminal back to support from the caller.
// Think of this as tcell disengaging the clutch, so that another application
// can take over the terminal interface.  This restores the TTY mode that was
// present when the application was first started.
func ( *tScreen) () {

	.Lock()
	if !.running {
		.Unlock()
		return
	}
	.running = false
	 := .stopQ
	close()
	_ = .tty.Drain()
	.Unlock()

	.tty.NotifyResize(nil)
	// wait for everything to shut down
	.wg.Wait()

	// shutdown the screen and disable special modes (e.g. mouse and bracketed paste)
	 := .ti
	.cells.Resize(0, 0)
	.TPuts(.ShowCursor)
	if .cursorStyles != nil && .cursorStyle != CursorStyleDefault {
		.TPuts(.cursorStyles[CursorStyleDefault])
	}
	if .cursorFg != "" && .cursorColor.Valid() {
		.TPuts(.cursorFg)
	}
	.TPuts(.ResetFgBg)
	.TPuts(.AttrOff)
	.TPuts(.ExitKeypad)
	.TPuts(.EnableAutoMargin)
	if os.Getenv("TCELL_ALTSCREEN") != "disable" {
		if .restoreTitle != "" {
			.TPuts(.restoreTitle)
		}
		.TPuts(.Clear) // only needed if ExitCA is empty
		.TPuts(.ExitCA)
	}
	.enableMouse(0)
	.enablePasting(false)
	.disableFocusReporting()

	_ = .tty.Stop()
}

// Beep emits a beep to the terminal.
func ( *tScreen) () error {
	.writeString(string(byte(7)))
	return nil
}

// finalize is used to at application shutdown, and restores the terminal
// to it's initial state.  It should not be called more than once.
func ( *tScreen) () {
	.disengage()
	_ = .tty.Close()
}

func ( *tScreen) () <-chan struct{} {
	return .quit
}

func ( *tScreen) () chan Event {
	return .eventQ
}

func ( *tScreen) () *CellBuffer {
	return &.cells
}

func ( *tScreen) ( string) {
	.Lock()
	.title = 
	if .setTitle != "" && .running {
		.TPuts(.ti.TParm(.setTitle, ))
	}
	.Unlock()
}

func ( *tScreen) ( []byte) {
	// Post binary data to the system clipboard.  It might be UTF-8, it might not be.
	.Lock()
	if .setClipboard != "" {
		 := base64.StdEncoding.EncodeToString()
		.TPuts(.ti.TParm(.setClipboard, ))
	}
	.Unlock()
}

func ( *tScreen) () {
	.Lock()
	if .setClipboard != "" {
		.TPuts(.ti.TParm(.setClipboard, "?"))
	}
	.Unlock()
}