| 1 |
// Copyright 2019 The TCell Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use file except in compliance with the License. |
| 5 |
// You may obtain a copy of the license at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package tcell |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/base64" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"log" |
| 24 |
"os" |
| 25 |
"strconv" |
| 26 |
"strings" |
| 27 |
"sync" |
| 28 |
"time" |
| 29 |
"unicode/utf8" |
| 30 |
|
| 31 |
"golang.org/x/term" |
| 32 |
"golang.org/x/text/transform" |
| 33 |
|
| 34 |
"github.com/micro-editor/tcell/v2/terminfo" |
| 35 |
|
| 36 |
// import the stock terminals |
| 37 |
_ "github.com/micro-editor/tcell/v2/terminfo/base" |
| 38 |
) |
| 39 |
|
| 40 |
// Magic strings for bracketed paste |
| 41 |
// See http://cirw.in/blog/bracketed-paste |
| 42 |
const ( |
| 43 |
pasteEnable = "\x1b[?2004h" |
| 44 |
pasteDisable = "\x1b[?2004l" |
| 45 |
pasteBegin = "\x1b[200~" |
| 46 |
pasteEnd = "\x1b[201~" |
| 47 |
|
| 48 |
pasteSet = "\x1b]52;%c;%s\x1b\\" |
| 49 |
pasteGet = "\x1b]52;%c;?\x1b\\" |
| 50 |
pasteClear = "\x1b]52;%c;!\x1b\\" |
| 51 |
|
| 52 |
pasteOSC52Begin = "\x1b]52;" |
| 53 |
pasteOSC52End = "\x1b\\" |
| 54 |
|
| 55 |
setTitle = "\x1b]2;title\a" |
| 56 |
) |
| 57 |
|
| 58 |
// NewTerminfoScreen returns a Screen that uses the stock TTY interface |
| 59 |
// and POSIX termios, combined with a terminfo description taken from |
| 60 |
// the $TERM environment variable. It returns an error if the terminal |
| 61 |
// is not supported for any reason. |
| 62 |
// |
| 63 |
// For terminals that do not support dynamic resize events, the $LINES |
| 64 |
// $COLUMNS environment variables can be set to the actual window size, |
| 65 |
// otherwise defaults taken from the terminal database are used. |
| 66 |
func NewTerminfoScreen() (Screen, error) { |
| 67 |
ti, e := terminfo.LookupTerminfo(os.Getenv("TERM")) |
| 68 |
if e != nil { |
| 69 |
ti, e = loadDynamicTerminfo(os.Getenv("TERM")) |
| 70 |
if e != nil { |
| 71 |
return nil, e |
| 72 |
} |
| 73 |
} else { |
| 74 |
terminfo.AddTerminfo(ti) |
| 75 |
} |
| 76 |
t := &tScreen{ti: ti} |
| 77 |
|
| 78 |
t.keyexist = make(map[Key]bool) |
| 79 |
t.keycodes = make(map[string]*tKeyCode) |
| 80 |
if len(ti.Mouse) > 0 { |
| 81 |
t.mouse = []byte(ti.Mouse) |
| 82 |
} |
| 83 |
t.prepareKeys() |
| 84 |
t.buildAcsMap() |
| 85 |
t.sigwinch = make(chan os.Signal, 10) |
| 86 |
t.fallback = make(map[rune]string) |
| 87 |
for k, v := range RuneFallbacks { |
| 88 |
t.fallback[k] = v |
| 89 |
} |
| 90 |
|
| 91 |
return t, nil |
| 92 |
} |
| 93 |
|
| 94 |
// tKeyCode represents a combination of a key code and modifiers. |
| 95 |
type tKeyCode struct { |
| 96 |
key Key |
| 97 |
mod ModMask |
| 98 |
} |
| 99 |
|
| 100 |
// tScreen represents a screen backed by a terminfo implementation. |
| 101 |
type tScreen struct { |
| 102 |
ti *terminfo.Terminfo |
| 103 |
h int |
| 104 |
w int |
| 105 |
fini bool |
| 106 |
cells CellBuffer |
| 107 |
in io.Reader |
| 108 |
out io.Writer |
| 109 |
buffering bool // true if we are collecting writes to buf instead of sending directly to out |
| 110 |
buf bytes.Buffer |
| 111 |
escbuf *bytes.Buffer |
| 112 |
paste bool |
| 113 |
curstyle Style |
| 114 |
style Style |
| 115 |
evch chan Event |
| 116 |
sigwinch chan os.Signal |
| 117 |
quit chan struct{} |
| 118 |
indoneq chan struct{} |
| 119 |
keyexist map[Key]bool |
| 120 |
keycodes map[string]*tKeyCode |
| 121 |
keychan chan []byte |
| 122 |
keytimer *time.Timer |
| 123 |
keyexpire time.Time |
| 124 |
cx int |
| 125 |
cy int |
| 126 |
mouse []byte |
| 127 |
clear bool |
| 128 |
cursorx int |
| 129 |
cursory int |
| 130 |
wasbtn bool |
| 131 |
acs map[rune]string |
| 132 |
charset string |
| 133 |
encoder transform.Transformer |
| 134 |
decoder transform.Transformer |
| 135 |
fallback map[rune]string |
| 136 |
colors map[Color]Color |
| 137 |
palette []Color |
| 138 |
truecolor bool |
| 139 |
escaped bool |
| 140 |
buttondn bool |
| 141 |
rawseq []string |
| 142 |
finiOnce sync.Once |
| 143 |
saved *term.State |
| 144 |
|
| 145 |
sync.Mutex |
| 146 |
} |
| 147 |
|
| 148 |
func (t *tScreen) Init() error { |
| 149 |
t.evch = make(chan Event, 10) |
| 150 |
t.indoneq = make(chan struct{}) |
| 151 |
t.keychan = make(chan []byte, 10) |
| 152 |
t.rawseq = make([]string, 0, 4) |
| 153 |
t.keytimer = time.NewTimer(time.Millisecond * 50) |
| 154 |
t.charset = "UTF-8" |
| 155 |
|
| 156 |
t.charset = getCharset() |
| 157 |
if enc := GetEncoding(t.charset); enc != nil { |
| 158 |
t.encoder = enc.NewEncoder() |
| 159 |
t.decoder = enc.NewDecoder() |
| 160 |
} else { |
| 161 |
return ErrNoCharset |
| 162 |
} |
| 163 |
ti := t.ti |
| 164 |
|
| 165 |
// environment overrides |
| 166 |
w := ti.Columns |
| 167 |
h := ti.Lines |
| 168 |
if i, _ := strconv.Atoi(os.Getenv("LINES")); i != 0 { |
| 169 |
h = i |
| 170 |
} |
| 171 |
if i, _ := strconv.Atoi(os.Getenv("COLUMNS")); i != 0 { |
| 172 |
w = i |
| 173 |
} |
| 174 |
if e := t.termioInit(); e != nil { |
| 175 |
return e |
| 176 |
} |
| 177 |
|
| 178 |
if t.ti.SetFgBgRGB != "" || t.ti.SetFgRGB != "" || t.ti.SetBgRGB != "" { |
| 179 |
t.truecolor = true |
| 180 |
} |
| 181 |
// A user who wants to have his themes honored can |
| 182 |
// set this environment variable. |
| 183 |
if os.Getenv("TCELL_TRUECOLOR") == "disable" { |
| 184 |
t.truecolor = false |
| 185 |
} |
| 186 |
nColors := t.nColors() |
| 187 |
if nColors > 256 { |
| 188 |
nColors = 256 // clip to reasonable limits |
| 189 |
} |
| 190 |
t.colors = make(map[Color]Color, nColors) |
| 191 |
t.palette = make([]Color, nColors) |
| 192 |
for i := 0; i < nColors; i++ { |
| 193 |
t.palette[i] = Color(i) | ColorValid |
| 194 |
// identity map for our builtin colors |
| 195 |
t.colors[Color(i)|ColorValid] = Color(i) | ColorValid |
| 196 |
} |
| 197 |
|
| 198 |
t.TPuts(ti.EnterCA) |
| 199 |
t.TPuts(ti.HideCursor) |
| 200 |
t.TPuts(ti.EnableAcs) |
| 201 |
t.TPuts(ti.Clear) |
| 202 |
t.TPuts(pasteEnable) |
| 203 |
|
| 204 |
t.quit = make(chan struct{}) |
| 205 |
|
| 206 |
t.Lock() |
| 207 |
t.cx = -1 |
| 208 |
t.cy = -1 |
| 209 |
t.style = StyleDefault |
| 210 |
t.cells.Resize(w, h) |
| 211 |
t.cursorx = -1 |
| 212 |
t.cursory = -1 |
| 213 |
t.resize() |
| 214 |
t.Unlock() |
| 215 |
|
| 216 |
go t.mainLoop() |
| 217 |
go t.inputLoop() |
| 218 |
|
| 219 |
return nil |
| 220 |
} |
| 221 |
|
| 222 |
func (t *tScreen) SetPaste(p bool) { |
| 223 |
t.paste = p |
| 224 |
} |
| 225 |
|
| 226 |
func (t *tScreen) RegisterRawSeq(r string) { |
| 227 |
for _, seq := range t.rawseq { |
| 228 |
if seq == r { |
| 229 |
return |
| 230 |
} |
| 231 |
} |
| 232 |
t.rawseq = append(t.rawseq, r) |
| 233 |
} |
| 234 |
|
| 235 |
func (t *tScreen) UnregisterRawSeq(r string) { |
| 236 |
for i, seq := range t.rawseq { |
| 237 |
if seq == r { |
| 238 |
t.rawseq[i] = t.rawseq[len(t.rawseq)-1] |
| 239 |
t.rawseq = t.rawseq[:len(t.rawseq)-1] |
| 240 |
break |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func (t *tScreen) prepareKeyMod(key Key, mod ModMask, val string) { |
| 246 |
if val != "" { |
| 247 |
// Do not override codes that already exist |
| 248 |
if _, exist := t.keycodes[val]; !exist { |
| 249 |
t.keyexist[key] = true |
| 250 |
t.keycodes[val] = &tKeyCode{key: key, mod: mod} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
func (t *tScreen) prepareKeyModReplace(key Key, replace Key, mod ModMask, val string) { |
| 256 |
if val != "" { |
| 257 |
// Do not override codes that already exist |
| 258 |
if old, exist := t.keycodes[val]; !exist || old.key == replace { |
| 259 |
t.keyexist[key] = true |
| 260 |
t.keycodes[val] = &tKeyCode{key: key, mod: mod} |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func (t *tScreen) prepareKeyModXTerm(key Key, val string) { |
| 266 |
|
| 267 |
if strings.HasPrefix(val, "\x1b[") && strings.HasSuffix(val, "~") { |
| 268 |
|
| 269 |
// Drop the trailing ~ |
| 270 |
val = val[:len(val)-1] |
| 271 |
|
| 272 |
// These suffixes are calculated assuming Xterm style modifier suffixes. |
| 273 |
// Please see https://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf for |
| 274 |
// more information (specifically "PC-Style Function Keys"). |
| 275 |
t.prepareKeyModReplace(key, key+12, ModShift, val+";2~") |
| 276 |
t.prepareKeyModReplace(key, key+48, ModAlt, val+";3~") |
| 277 |
t.prepareKeyModReplace(key, key+60, ModAlt|ModShift, val+";4~") |
| 278 |
t.prepareKeyModReplace(key, key+24, ModCtrl, val+";5~") |
| 279 |
t.prepareKeyModReplace(key, key+36, ModCtrl|ModShift, val+";6~") |
| 280 |
t.prepareKeyMod(key, ModAlt|ModCtrl, val+";7~") |
| 281 |
t.prepareKeyMod(key, ModShift|ModAlt|ModCtrl, val+";8~") |
| 282 |
t.prepareKeyMod(key, ModMeta, val+";9~") |
| 283 |
t.prepareKeyMod(key, ModMeta|ModShift, val+";10~") |
| 284 |
t.prepareKeyMod(key, ModMeta|ModAlt, val+";11~") |
| 285 |
t.prepareKeyMod(key, ModMeta|ModAlt|ModShift, val+";12~") |
| 286 |
t.prepareKeyMod(key, ModMeta|ModCtrl, val+";13~") |
| 287 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModShift, val+";14~") |
| 288 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt, val+";15~") |
| 289 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt|ModShift, val+";16~") |
| 290 |
} else if strings.HasPrefix(val, "\x1bO") && len(val) == 3 { |
| 291 |
val = val[2:] |
| 292 |
t.prepareKeyModReplace(key, key+12, ModShift, "\x1b[1;2"+val) |
| 293 |
t.prepareKeyModReplace(key, key+48, ModAlt, "\x1b[1;3"+val) |
| 294 |
t.prepareKeyModReplace(key, key+24, ModCtrl, "\x1b[1;5"+val) |
| 295 |
t.prepareKeyModReplace(key, key+36, ModCtrl|ModShift, "\x1b[1;6"+val) |
| 296 |
t.prepareKeyModReplace(key, key+60, ModAlt|ModShift, "\x1b[1;4"+val) |
| 297 |
t.prepareKeyMod(key, ModAlt|ModCtrl, "\x1b[1;7"+val) |
| 298 |
t.prepareKeyMod(key, ModShift|ModAlt|ModCtrl, "\x1b[1;8"+val) |
| 299 |
t.prepareKeyMod(key, ModMeta, "\x1b[1;9"+val) |
| 300 |
t.prepareKeyMod(key, ModMeta|ModShift, "\x1b[1;10"+val) |
| 301 |
t.prepareKeyMod(key, ModMeta|ModAlt, "\x1b[1;11"+val) |
| 302 |
t.prepareKeyMod(key, ModMeta|ModAlt|ModShift, "\x1b[1;12"+val) |
| 303 |
t.prepareKeyMod(key, ModMeta|ModCtrl, "\x1b[1;13"+val) |
| 304 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModShift, "\x1b[1;14"+val) |
| 305 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt, "\x1b[1;15"+val) |
| 306 |
t.prepareKeyMod(key, ModMeta|ModCtrl|ModAlt|ModShift, "\x1b[1;16"+val) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func (t *tScreen) prepareXtermModifiers() { |
| 311 |
if t.ti.Modifiers != terminfo.ModifiersXTerm { |
| 312 |
return |
| 313 |
} |
| 314 |
t.prepareKeyModXTerm(KeyRight, t.ti.KeyRight) |
| 315 |
t.prepareKeyModXTerm(KeyLeft, t.ti.KeyLeft) |
| 316 |
t.prepareKeyModXTerm(KeyUp, t.ti.KeyUp) |
| 317 |
t.prepareKeyModXTerm(KeyDown, t.ti.KeyDown) |
| 318 |
t.prepareKeyModXTerm(KeyInsert, t.ti.KeyInsert) |
| 319 |
t.prepareKeyModXTerm(KeyDelete, t.ti.KeyDelete) |
| 320 |
t.prepareKeyModXTerm(KeyPgUp, t.ti.KeyPgUp) |
| 321 |
t.prepareKeyModXTerm(KeyPgDn, t.ti.KeyPgDn) |
| 322 |
t.prepareKeyModXTerm(KeyHome, t.ti.KeyHome) |
| 323 |
t.prepareKeyModXTerm(KeyEnd, t.ti.KeyEnd) |
| 324 |
t.prepareKeyModXTerm(KeyF1, t.ti.KeyF1) |
| 325 |
t.prepareKeyModXTerm(KeyF2, t.ti.KeyF2) |
| 326 |
t.prepareKeyModXTerm(KeyF3, t.ti.KeyF3) |
| 327 |
t.prepareKeyModXTerm(KeyF4, t.ti.KeyF4) |
| 328 |
t.prepareKeyModXTerm(KeyF5, t.ti.KeyF5) |
| 329 |
t.prepareKeyModXTerm(KeyF6, t.ti.KeyF6) |
| 330 |
t.prepareKeyModXTerm(KeyF7, t.ti.KeyF7) |
| 331 |
t.prepareKeyModXTerm(KeyF8, t.ti.KeyF8) |
| 332 |
t.prepareKeyModXTerm(KeyF9, t.ti.KeyF9) |
| 333 |
t.prepareKeyModXTerm(KeyF10, t.ti.KeyF10) |
| 334 |
t.prepareKeyModXTerm(KeyF11, t.ti.KeyF11) |
| 335 |
t.prepareKeyModXTerm(KeyF12, t.ti.KeyF12) |
| 336 |
} |
| 337 |
|
| 338 |
func (t *tScreen) prepareKey(key Key, val string) { |
| 339 |
t.prepareKeyMod(key, ModNone, val) |
| 340 |
} |
| 341 |
|
| 342 |
func (t *tScreen) prepareKeys() { |
| 343 |
ti := t.ti |
| 344 |
t.prepareKey(KeyBackspace, ti.KeyBackspace) |
| 345 |
t.prepareKey(KeyF1, ti.KeyF1) |
| 346 |
t.prepareKey(KeyF2, ti.KeyF2) |
| 347 |
t.prepareKey(KeyF3, ti.KeyF3) |
| 348 |
t.prepareKey(KeyF4, ti.KeyF4) |
| 349 |
t.prepareKey(KeyF5, ti.KeyF5) |
| 350 |
t.prepareKey(KeyF6, ti.KeyF6) |
| 351 |
t.prepareKey(KeyF7, ti.KeyF7) |
| 352 |
t.prepareKey(KeyF8, ti.KeyF8) |
| 353 |
t.prepareKey(KeyF9, ti.KeyF9) |
| 354 |
t.prepareKey(KeyF10, ti.KeyF10) |
| 355 |
t.prepareKey(KeyF11, ti.KeyF11) |
| 356 |
t.prepareKey(KeyF12, ti.KeyF12) |
| 357 |
t.prepareKey(KeyF13, ti.KeyF13) |
| 358 |
t.prepareKey(KeyF14, ti.KeyF14) |
| 359 |
t.prepareKey(KeyF15, ti.KeyF15) |
| 360 |
t.prepareKey(KeyF16, ti.KeyF16) |
| 361 |
t.prepareKey(KeyF17, ti.KeyF17) |
| 362 |
t.prepareKey(KeyF18, ti.KeyF18) |
| 363 |
t.prepareKey(KeyF19, ti.KeyF19) |
| 364 |
t.prepareKey(KeyF20, ti.KeyF20) |
| 365 |
t.prepareKey(KeyF21, ti.KeyF21) |
| 366 |
t.prepareKey(KeyF22, ti.KeyF22) |
| 367 |
t.prepareKey(KeyF23, ti.KeyF23) |
| 368 |
t.prepareKey(KeyF24, ti.KeyF24) |
| 369 |
t.prepareKey(KeyF25, ti.KeyF25) |
| 370 |
t.prepareKey(KeyF26, ti.KeyF26) |
| 371 |
t.prepareKey(KeyF27, ti.KeyF27) |
| 372 |
t.prepareKey(KeyF28, ti.KeyF28) |
| 373 |
t.prepareKey(KeyF29, ti.KeyF29) |
| 374 |
t.prepareKey(KeyF30, ti.KeyF30) |
| 375 |
t.prepareKey(KeyF31, ti.KeyF31) |
| 376 |
t.prepareKey(KeyF32, ti.KeyF32) |
| 377 |
t.prepareKey(KeyF33, ti.KeyF33) |
| 378 |
t.prepareKey(KeyF34, ti.KeyF34) |
| 379 |
t.prepareKey(KeyF35, ti.KeyF35) |
| 380 |
t.prepareKey(KeyF36, ti.KeyF36) |
| 381 |
t.prepareKey(KeyF37, ti.KeyF37) |
| 382 |
t.prepareKey(KeyF38, ti.KeyF38) |
| 383 |
t.prepareKey(KeyF39, ti.KeyF39) |
| 384 |
t.prepareKey(KeyF40, ti.KeyF40) |
| 385 |
t.prepareKey(KeyF41, ti.KeyF41) |
| 386 |
t.prepareKey(KeyF42, ti.KeyF42) |
| 387 |
t.prepareKey(KeyF43, ti.KeyF43) |
| 388 |
t.prepareKey(KeyF44, ti.KeyF44) |
| 389 |
t.prepareKey(KeyF45, ti.KeyF45) |
| 390 |
t.prepareKey(KeyF46, ti.KeyF46) |
| 391 |
t.prepareKey(KeyF47, ti.KeyF47) |
| 392 |
t.prepareKey(KeyF48, ti.KeyF48) |
| 393 |
t.prepareKey(KeyF49, ti.KeyF49) |
| 394 |
t.prepareKey(KeyF50, ti.KeyF50) |
| 395 |
t.prepareKey(KeyF51, ti.KeyF51) |
| 396 |
t.prepareKey(KeyF52, ti.KeyF52) |
| 397 |
t.prepareKey(KeyF53, ti.KeyF53) |
| 398 |
t.prepareKey(KeyF54, ti.KeyF54) |
| 399 |
t.prepareKey(KeyF55, ti.KeyF55) |
| 400 |
t.prepareKey(KeyF56, ti.KeyF56) |
| 401 |
t.prepareKey(KeyF57, ti.KeyF57) |
| 402 |
t.prepareKey(KeyF58, ti.KeyF58) |
| 403 |
t.prepareKey(KeyF59, ti.KeyF59) |
| 404 |
t.prepareKey(KeyF60, ti.KeyF60) |
| 405 |
t.prepareKey(KeyF61, ti.KeyF61) |
| 406 |
t.prepareKey(KeyF62, ti.KeyF62) |
| 407 |
t.prepareKey(KeyF63, ti.KeyF63) |
| 408 |
t.prepareKey(KeyF64, ti.KeyF64) |
| 409 |
t.prepareKey(KeyInsert, ti.KeyInsert) |
| 410 |
t.prepareKey(KeyDelete, ti.KeyDelete) |
| 411 |
t.prepareKey(KeyHome, ti.KeyHome) |
| 412 |
t.prepareKey(KeyEnd, ti.KeyEnd) |
| 413 |
t.prepareKey(KeyUp, ti.KeyUp) |
| 414 |
t.prepareKey(KeyDown, ti.KeyDown) |
| 415 |
t.prepareKey(KeyLeft, ti.KeyLeft) |
| 416 |
t.prepareKey(KeyRight, ti.KeyRight) |
| 417 |
t.prepareKey(KeyPgUp, ti.KeyPgUp) |
| 418 |
t.prepareKey(KeyPgDn, ti.KeyPgDn) |
| 419 |
t.prepareKey(KeyHelp, ti.KeyHelp) |
| 420 |
t.prepareKey(KeyPrint, ti.KeyPrint) |
| 421 |
t.prepareKey(KeyCancel, ti.KeyCancel) |
| 422 |
t.prepareKey(KeyExit, ti.KeyExit) |
| 423 |
t.prepareKey(KeyBacktab, ti.KeyBacktab) |
| 424 |
|
| 425 |
t.prepareKeyMod(KeyRight, ModShift, ti.KeyShfRight) |
| 426 |
t.prepareKeyMod(KeyLeft, ModShift, ti.KeyShfLeft) |
| 427 |
t.prepareKeyMod(KeyUp, ModShift, ti.KeyShfUp) |
| 428 |
t.prepareKeyMod(KeyDown, ModShift, ti.KeyShfDown) |
| 429 |
t.prepareKeyMod(KeyHome, ModShift, ti.KeyShfHome) |
| 430 |
t.prepareKeyMod(KeyEnd, ModShift, ti.KeyShfEnd) |
| 431 |
t.prepareKeyMod(KeyPgUp, ModShift, ti.KeyShfPgUp) |
| 432 |
t.prepareKeyMod(KeyPgDn, ModShift, ti.KeyShfPgDn) |
| 433 |
|
| 434 |
t.prepareKeyMod(KeyRight, ModCtrl, ti.KeyCtrlRight) |
| 435 |
t.prepareKeyMod(KeyLeft, ModCtrl, ti.KeyCtrlLeft) |
| 436 |
t.prepareKeyMod(KeyUp, ModCtrl, ti.KeyCtrlUp) |
| 437 |
t.prepareKeyMod(KeyDown, ModCtrl, ti.KeyCtrlDown) |
| 438 |
t.prepareKeyMod(KeyHome, ModCtrl, ti.KeyCtrlHome) |
| 439 |
t.prepareKeyMod(KeyEnd, ModCtrl, ti.KeyCtrlEnd) |
| 440 |
|
| 441 |
if t.ti.Modifiers == terminfo.ModifiersDynamic { |
| 442 |
t.prepareKeyMod(KeyUp, ModMeta, ti.KeyMetaUp) |
| 443 |
t.prepareKeyMod(KeyDown, ModMeta, ti.KeyMetaDown) |
| 444 |
t.prepareKeyMod(KeyRight, ModMeta, ti.KeyMetaRight) |
| 445 |
t.prepareKeyMod(KeyLeft, ModMeta, ti.KeyMetaLeft) |
| 446 |
t.prepareKeyMod(KeyUp, ModAlt, ti.KeyAltUp) |
| 447 |
t.prepareKeyMod(KeyDown, ModAlt, ti.KeyAltDown) |
| 448 |
t.prepareKeyMod(KeyRight, ModAlt, ti.KeyAltRight) |
| 449 |
t.prepareKeyMod(KeyLeft, ModAlt, ti.KeyAltLeft) |
| 450 |
t.prepareKeyMod(KeyUp, ModAlt|ModShift, ti.KeyAltShfUp) |
| 451 |
t.prepareKeyMod(KeyDown, ModAlt|ModShift, ti.KeyAltShfDown) |
| 452 |
t.prepareKeyMod(KeyRight, ModAlt|ModShift, ti.KeyAltShfRight) |
| 453 |
t.prepareKeyMod(KeyLeft, ModAlt|ModShift, ti.KeyAltShfLeft) |
| 454 |
|
| 455 |
t.prepareKeyMod(KeyUp, ModMeta|ModShift, ti.KeyMetaShfUp) |
| 456 |
t.prepareKeyMod(KeyDown, ModMeta|ModShift, ti.KeyMetaShfDown) |
| 457 |
t.prepareKeyMod(KeyRight, ModMeta|ModShift, ti.KeyMetaShfRight) |
| 458 |
t.prepareKeyMod(KeyLeft, ModMeta|ModShift, ti.KeyMetaShfLeft) |
| 459 |
|
| 460 |
t.prepareKeyMod(KeyUp, ModCtrl|ModShift, ti.KeyCtrlShfUp) |
| 461 |
t.prepareKeyMod(KeyDown, ModCtrl|ModShift, ti.KeyCtrlShfDown) |
| 462 |
t.prepareKeyMod(KeyRight, ModCtrl|ModShift, ti.KeyCtrlShfRight) |
| 463 |
t.prepareKeyMod(KeyLeft, ModCtrl|ModShift, ti.KeyCtrlShfLeft) |
| 464 |
|
| 465 |
t.prepareKeyMod(KeyHome, ModAlt, ti.KeyAltHome) |
| 466 |
t.prepareKeyMod(KeyEnd, ModAlt, ti.KeyAltEnd) |
| 467 |
t.prepareKeyMod(KeyHome, ModCtrl|ModShift, ti.KeyCtrlShfHome) |
| 468 |
t.prepareKeyMod(KeyEnd, ModCtrl|ModShift, ti.KeyCtrlShfEnd) |
| 469 |
t.prepareKeyMod(KeyHome, ModAlt|ModShift, ti.KeyAltShfHome) |
| 470 |
t.prepareKeyMod(KeyEnd, ModAlt|ModShift, ti.KeyAltShfEnd) |
| 471 |
t.prepareKeyMod(KeyHome, ModMeta|ModShift, ti.KeyMetaShfHome) |
| 472 |
t.prepareKeyMod(KeyEnd, ModMeta|ModShift, ti.KeyMetaShfEnd) |
| 473 |
} |
| 474 |
|
| 475 |
// Sadly, xterm handling of keycodes is somewhat erratic. In |
| 476 |
// particular, different codes are sent depending on application |
| 477 |
// mode is in use or not, and the entries for many of these are |
| 478 |
// simply absent from terminfo on many systems. So we insert |
| 479 |
// a number of escape sequences if they are not already used, in |
| 480 |
// order to have the widest correct usage. Note that prepareKey |
| 481 |
// will not inject codes if the escape sequence is already known. |
| 482 |
// We also only do this for terminals that have the application |
| 483 |
// mode present. |
| 484 |
|
| 485 |
// Cursor mode |
| 486 |
if ti.EnterKeypad != "" { |
| 487 |
t.prepareKey(KeyUp, "\x1b[A") |
| 488 |
t.prepareKey(KeyDown, "\x1b[B") |
| 489 |
t.prepareKey(KeyRight, "\x1b[C") |
| 490 |
t.prepareKey(KeyLeft, "\x1b[D") |
| 491 |
t.prepareKey(KeyEnd, "\x1b[F") |
| 492 |
t.prepareKey(KeyHome, "\x1b[H") |
| 493 |
t.prepareKey(KeyDelete, "\x1b[3~") |
| 494 |
t.prepareKey(KeyHome, "\x1b[1~") |
| 495 |
t.prepareKey(KeyEnd, "\x1b[4~") |
| 496 |
t.prepareKey(KeyPgUp, "\x1b[5~") |
| 497 |
t.prepareKey(KeyPgDn, "\x1b[6~") |
| 498 |
|
| 499 |
// Application mode |
| 500 |
t.prepareKey(KeyUp, "\x1bOA") |
| 501 |
t.prepareKey(KeyDown, "\x1bOB") |
| 502 |
t.prepareKey(KeyRight, "\x1bOC") |
| 503 |
t.prepareKey(KeyLeft, "\x1bOD") |
| 504 |
t.prepareKey(KeyHome, "\x1bOH") |
| 505 |
} |
| 506 |
|
| 507 |
t.prepareXtermModifiers() |
| 508 |
|
| 509 |
outer: |
| 510 |
// Add key mappings for control keys. |
| 511 |
for i := 0; i < ' '; i++ { |
| 512 |
// Do not insert direct key codes for ambiguous keys. |
| 513 |
// For example, ESC is used for lots of other keys, so |
| 514 |
// when parsing this we don't want to fast path handling |
| 515 |
// of it, but instead wait a bit before parsing it as in |
| 516 |
// isolation. |
| 517 |
for esc := range t.keycodes { |
| 518 |
if []byte(esc)[0] == byte(i) { |
| 519 |
continue outer |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
t.keyexist[Key(i)] = true |
| 524 |
|
| 525 |
mod := ModCtrl |
| 526 |
switch Key(i) { |
| 527 |
case KeyBS, KeyTAB, KeyESC, KeyCR: |
| 528 |
// directly typeable- no control sequence |
| 529 |
mod = ModNone |
| 530 |
} |
| 531 |
t.keycodes[string(rune(i))] = &tKeyCode{key: Key(i), mod: mod} |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
func (t *tScreen) Fini() { |
| 536 |
t.finiOnce.Do(t.finish) |
| 537 |
} |
| 538 |
|
| 539 |
func (t *tScreen) finish() { |
| 540 |
t.Lock() |
| 541 |
defer t.Unlock() |
| 542 |
|
| 543 |
ti := t.ti |
| 544 |
t.cells.Resize(0, 0) |
| 545 |
t.TPuts(ti.ShowCursor) |
| 546 |
t.TPuts(ti.AttrOff) |
| 547 |
t.TPuts(ti.Clear) |
| 548 |
t.TPuts(ti.ExitCA) |
| 549 |
t.TPuts(ti.ExitKeypad) |
| 550 |
t.TPuts(ti.TParm(ti.MouseMode, 0)) |
| 551 |
t.TPuts(pasteDisable) |
| 552 |
t.curstyle = styleInvalid |
| 553 |
t.clear = false |
| 554 |
t.fini = true |
| 555 |
|
| 556 |
select { |
| 557 |
case <-t.quit: |
| 558 |
// do nothing, already closed |
| 559 |
|
| 560 |
default: |
| 561 |
close(t.quit) |
| 562 |
} |
| 563 |
|
| 564 |
t.termioFini() |
| 565 |
} |
| 566 |
|
| 567 |
func (t *tScreen) SetStyle(style Style) { |
| 568 |
t.Lock() |
| 569 |
if !t.fini { |
| 570 |
t.style = style |
| 571 |
} |
| 572 |
t.Unlock() |
| 573 |
} |
| 574 |
|
| 575 |
func (t *tScreen) Clear() { |
| 576 |
t.Fill(' ', t.style) |
| 577 |
} |
| 578 |
|
| 579 |
func (t *tScreen) Fill(r rune, style Style) { |
| 580 |
t.Lock() |
| 581 |
if !t.fini { |
| 582 |
t.cells.Fill(r, style) |
| 583 |
} |
| 584 |
t.Unlock() |
| 585 |
} |
| 586 |
|
| 587 |
func (t *tScreen) SetContent(x, y int, mainc rune, combc []rune, style Style) { |
| 588 |
t.Lock() |
| 589 |
if !t.fini { |
| 590 |
t.cells.SetContent(x, y, mainc, combc, style) |
| 591 |
} |
| 592 |
t.Unlock() |
| 593 |
} |
| 594 |
|
| 595 |
func (t *tScreen) GetContent(x, y int) (rune, []rune, Style, int) { |
| 596 |
t.Lock() |
| 597 |
mainc, combc, style, width := t.cells.GetContent(x, y) |
| 598 |
t.Unlock() |
| 599 |
return mainc, combc, style, width |
| 600 |
} |
| 601 |
|
| 602 |
func (t *tScreen) SetCell(x, y int, style Style, ch ...rune) { |
| 603 |
if len(ch) > 0 { |
| 604 |
t.SetContent(x, y, ch[0], ch[1:], style) |
| 605 |
} else { |
| 606 |
t.SetContent(x, y, ' ', nil, style) |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
func (t *tScreen) encodeRune(r rune, buf []byte) []byte { |
| 611 |
|
| 612 |
nb := make([]byte, 6) |
| 613 |
ob := make([]byte, 6) |
| 614 |
num := utf8.EncodeRune(ob, r) |
| 615 |
ob = ob[:num] |
| 616 |
dst := 0 |
| 617 |
var err error |
| 618 |
if enc := t.encoder; enc != nil { |
| 619 |
enc.Reset() |
| 620 |
dst, _, err = enc.Transform(nb, ob, true) |
| 621 |
} |
| 622 |
if err != nil || dst == 0 || nb[0] == '\x1a' { |
| 623 |
// Combining characters are elided |
| 624 |
if len(buf) == 0 { |
| 625 |
if acs, ok := t.acs[r]; ok { |
| 626 |
buf = append(buf, []byte(acs)...) |
| 627 |
} else if fb, ok := t.fallback[r]; ok { |
| 628 |
buf = append(buf, []byte(fb)...) |
| 629 |
} else { |
| 630 |
buf = append(buf, '?') |
| 631 |
} |
| 632 |
} |
| 633 |
} else { |
| 634 |
buf = append(buf, nb[:dst]...) |
| 635 |
} |
| 636 |
|
| 637 |
return buf |
| 638 |
} |
| 639 |
|
| 640 |
func (t *tScreen) sendFgBg(fg Color, bg Color) { |
| 641 |
ti := t.ti |
| 642 |
if ti.Colors == 0 { |
| 643 |
return |
| 644 |
} |
| 645 |
if fg == ColorReset || bg == ColorReset { |
| 646 |
t.TPuts(ti.ResetFgBg) |
| 647 |
} |
| 648 |
if t.truecolor { |
| 649 |
if ti.SetFgBgRGB != "" && fg.IsRGB() && bg.IsRGB() { |
| 650 |
r1, g1, b1 := fg.RGB() |
| 651 |
r2, g2, b2 := bg.RGB() |
| 652 |
t.TPuts(ti.TParm(ti.SetFgBgRGB, |
| 653 |
int(r1), int(g1), int(b1), |
| 654 |
int(r2), int(g2), int(b2))) |
| 655 |
return |
| 656 |
} |
| 657 |
|
| 658 |
if fg.IsRGB() && ti.SetFgRGB != "" { |
| 659 |
r, g, b := fg.RGB() |
| 660 |
t.TPuts(ti.TParm(ti.SetFgRGB, int(r), int(g), int(b))) |
| 661 |
fg = ColorDefault |
| 662 |
} |
| 663 |
|
| 664 |
if bg.IsRGB() && ti.SetBgRGB != "" { |
| 665 |
r, g, b := bg.RGB() |
| 666 |
t.TPuts(ti.TParm(ti.SetBgRGB, |
| 667 |
int(r), int(g), int(b))) |
| 668 |
bg = ColorDefault |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
if fg.Valid() { |
| 673 |
if v, ok := t.colors[fg]; ok { |
| 674 |
fg = v |
| 675 |
} else { |
| 676 |
v = FindColor(fg, t.palette) |
| 677 |
t.colors[fg] = v |
| 678 |
fg = v |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
if bg.Valid() { |
| 683 |
if v, ok := t.colors[bg]; ok { |
| 684 |
bg = v |
| 685 |
} else { |
| 686 |
v = FindColor(bg, t.palette) |
| 687 |
t.colors[bg] = v |
| 688 |
bg = v |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if fg.Valid() && bg.Valid() && ti.SetFgBg != "" { |
| 693 |
t.TPuts(ti.TParm(ti.SetFgBg, int(fg&0xff), int(bg&0xff))) |
| 694 |
} else { |
| 695 |
if fg.Valid() && ti.SetFg != "" { |
| 696 |
t.TPuts(ti.TParm(ti.SetFg, int(fg&0xff))) |
| 697 |
} |
| 698 |
if bg.Valid() && ti.SetBg != "" { |
| 699 |
t.TPuts(ti.TParm(ti.SetBg, int(bg&0xff))) |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
func (t *tScreen) drawCell(x, y int) int { |
| 705 |
|
| 706 |
ti := t.ti |
| 707 |
|
| 708 |
mainc, combc, style, width := t.cells.GetContent(x, y) |
| 709 |
if !t.cells.Dirty(x, y) { |
| 710 |
return width |
| 711 |
} |
| 712 |
|
| 713 |
if t.cy != y || t.cx != x { |
| 714 |
t.TPuts(ti.TGoto(x, y)) |
| 715 |
t.cx = x |
| 716 |
t.cy = y |
| 717 |
} |
| 718 |
|
| 719 |
if style == StyleDefault { |
| 720 |
style = t.style |
| 721 |
} |
| 722 |
if style != t.curstyle { |
| 723 |
fg, bg, attrs := style.Decompose() |
| 724 |
|
| 725 |
t.TPuts(ti.AttrOff) |
| 726 |
|
| 727 |
t.sendFgBg(fg, bg) |
| 728 |
if attrs&AttrBold != 0 { |
| 729 |
t.TPuts(ti.Bold) |
| 730 |
} |
| 731 |
if attrs&AttrUnderline != 0 { |
| 732 |
t.TPuts(ti.Underline) |
| 733 |
} |
| 734 |
if attrs&AttrReverse != 0 { |
| 735 |
t.TPuts(ti.Reverse) |
| 736 |
} |
| 737 |
if attrs&AttrBlink != 0 { |
| 738 |
t.TPuts(ti.Blink) |
| 739 |
} |
| 740 |
if attrs&AttrDim != 0 { |
| 741 |
t.TPuts(ti.Dim) |
| 742 |
} |
| 743 |
if attrs&AttrItalic != 0 { |
| 744 |
t.TPuts(ti.Italic) |
| 745 |
} |
| 746 |
if attrs&AttrStrikeThrough != 0 { |
| 747 |
t.TPuts(ti.StrikeThrough) |
| 748 |
} |
| 749 |
t.curstyle = style |
| 750 |
} |
| 751 |
// now emit runes - taking care to not overrun width with a |
| 752 |
// wide character, and to ensure that we emit exactly one regular |
| 753 |
// character followed up by any residual combing characters |
| 754 |
|
| 755 |
if width < 1 { |
| 756 |
width = 1 |
| 757 |
} |
| 758 |
|
| 759 |
var str string |
| 760 |
|
| 761 |
buf := make([]byte, 0, 6) |
| 762 |
|
| 763 |
buf = t.encodeRune(mainc, buf) |
| 764 |
for _, r := range combc { |
| 765 |
buf = t.encodeRune(r, buf) |
| 766 |
} |
| 767 |
|
| 768 |
str = string(buf) |
| 769 |
if width > 1 && str == "?" { |
| 770 |
// No FullWidth character support |
| 771 |
str = "? " |
| 772 |
t.cx = -1 |
| 773 |
} |
| 774 |
|
| 775 |
// XXX: check for hazeltine not being able to display ~ |
| 776 |
|
| 777 |
if x > t.w-width { |
| 778 |
// too wide to fit; emit a single space instead |
| 779 |
width = 1 |
| 780 |
str = " " |
| 781 |
} |
| 782 |
t.writeString(str) |
| 783 |
t.cx += width |
| 784 |
t.cells.SetDirty(x, y, false) |
| 785 |
if width > 1 { |
| 786 |
t.cx = -1 |
| 787 |
} |
| 788 |
|
| 789 |
return width |
| 790 |
} |
| 791 |
|
| 792 |
func (t *tScreen) ShowCursor(x, y int) { |
| 793 |
t.Lock() |
| 794 |
t.cursorx = x |
| 795 |
t.cursory = y |
| 796 |
t.Unlock() |
| 797 |
} |
| 798 |
|
| 799 |
func (t *tScreen) HideCursor() { |
| 800 |
t.ShowCursor(-1, -1) |
| 801 |
} |
| 802 |
|
| 803 |
func (t *tScreen) showCursor() { |
| 804 |
|
| 805 |
x, y := t.cursorx, t.cursory |
| 806 |
w, h := t.cells.Size() |
| 807 |
if x < 0 || y < 0 || x >= w || y >= h { |
| 808 |
t.hideCursor() |
| 809 |
return |
| 810 |
} |
| 811 |
t.TPuts(t.ti.TGoto(x, y)) |
| 812 |
t.TPuts(t.ti.ShowCursor) |
| 813 |
t.cx = x |
| 814 |
t.cy = y |
| 815 |
} |
| 816 |
|
| 817 |
// writeString sends a string to the terminal. The string is sent as-is and |
| 818 |
// this function does not expand inline padding indications (of the form |
| 819 |
// $<[delay]> where [delay] is msec). In order to have these expanded, use |
| 820 |
// TPuts. If the screen is "buffering", the string is collected in a buffer, |
| 821 |
// with the intention that the entire buffer be sent to the terminal in one |
| 822 |
// write operation at some point later. |
| 823 |
func (t *tScreen) writeString(s string) { |
| 824 |
if t.buffering { |
| 825 |
io.WriteString(&t.buf, s) |
| 826 |
} else { |
| 827 |
io.WriteString(t.out, s) |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
func (t *tScreen) TPuts(s string) { |
| 832 |
if t.buffering { |
| 833 |
t.ti.TPuts(&t.buf, s) |
| 834 |
} else { |
| 835 |
t.ti.TPuts(t.out, s) |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
func (t *tScreen) Show() { |
| 840 |
t.Lock() |
| 841 |
if !t.fini { |
| 842 |
t.resize() |
| 843 |
t.draw() |
| 844 |
} |
| 845 |
t.Unlock() |
| 846 |
} |
| 847 |
|
| 848 |
func (t *tScreen) clearScreen() { |
| 849 |
fg, bg, _ := t.style.Decompose() |
| 850 |
t.sendFgBg(fg, bg) |
| 851 |
t.TPuts(t.ti.Clear) |
| 852 |
t.clear = false |
| 853 |
} |
| 854 |
|
| 855 |
func (t *tScreen) hideCursor() { |
| 856 |
// does not update cursor position |
| 857 |
if t.ti.HideCursor != "" { |
| 858 |
t.TPuts(t.ti.HideCursor) |
| 859 |
} else { |
| 860 |
// No way to hide cursor, stick it |
| 861 |
// at bottom right of screen |
| 862 |
t.cx, t.cy = t.cells.Size() |
| 863 |
t.TPuts(t.ti.TGoto(t.cx, t.cy)) |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
func (t *tScreen) draw() { |
| 868 |
// clobber cursor position, because we're gonna change it all |
| 869 |
t.cx = -1 |
| 870 |
t.cy = -1 |
| 871 |
|
| 872 |
t.buf.Reset() |
| 873 |
t.buffering = true |
| 874 |
defer func() { |
| 875 |
t.buffering = false |
| 876 |
}() |
| 877 |
|
| 878 |
// hide the cursor while we move stuff around |
| 879 |
t.hideCursor() |
| 880 |
|
| 881 |
if t.clear { |
| 882 |
t.clearScreen() |
| 883 |
} |
| 884 |
|
| 885 |
for y := 0; y < t.h; y++ { |
| 886 |
for x := 0; x < t.w; x++ { |
| 887 |
width := t.drawCell(x, y) |
| 888 |
if width > 1 { |
| 889 |
if x+1 < t.w { |
| 890 |
// this is necessary so that if we ever |
| 891 |
// go back to drawing that cell, we |
| 892 |
// actually will *draw* it. |
| 893 |
t.cells.SetDirty(x+1, y, true) |
| 894 |
} |
| 895 |
} |
| 896 |
x += width - 1 |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
// restore the cursor |
| 901 |
t.showCursor() |
| 902 |
|
| 903 |
t.buf.WriteTo(t.out) |
| 904 |
} |
| 905 |
|
| 906 |
func (t *tScreen) EnableMouse() { |
| 907 |
if len(t.mouse) != 0 { |
| 908 |
t.TPuts(t.ti.TParm(t.ti.MouseMode, 1)) |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
func (t *tScreen) DisableMouse() { |
| 913 |
if len(t.mouse) != 0 { |
| 914 |
t.TPuts(t.ti.TParm(t.ti.MouseMode, 0)) |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
func (t *tScreen) Size() (int, int) { |
| 919 |
t.Lock() |
| 920 |
w, h := t.w, t.h |
| 921 |
t.Unlock() |
| 922 |
return w, h |
| 923 |
} |
| 924 |
|
| 925 |
func (t *tScreen) resize() { |
| 926 |
if w, h, e := t.getWinSize(); e == nil { |
| 927 |
if w != t.w || h != t.h { |
| 928 |
t.cx = -1 |
| 929 |
t.cy = -1 |
| 930 |
|
| 931 |
t.cells.Resize(w, h) |
| 932 |
t.cells.Invalidate() |
| 933 |
t.h = h |
| 934 |
t.w = w |
| 935 |
ev := NewEventResize(w, h) |
| 936 |
t.PostEvent(ev) |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
func (t *tScreen) Colors() int { |
| 942 |
// this doesn't change, no need for lock |
| 943 |
if t.truecolor { |
| 944 |
return 1 << 24 |
| 945 |
} |
| 946 |
return t.ti.Colors |
| 947 |
} |
| 948 |
|
| 949 |
// nColors returns the size of the built-in palette. |
| 950 |
// This is distinct from Colors(), as it will generally |
| 951 |
// always be a small number. (<= 256) |
| 952 |
func (t *tScreen) nColors() int { |
| 953 |
return t.ti.Colors |
| 954 |
} |
| 955 |
|
| 956 |
func (t *tScreen) PollEvent() Event { |
| 957 |
select { |
| 958 |
case <-t.quit: |
| 959 |
return nil |
| 960 |
case ev := <-t.evch: |
| 961 |
return ev |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
// vtACSNames is a map of bytes defined by terminfo that are used in |
| 966 |
// the terminals Alternate Character Set to represent other glyphs. |
| 967 |
// For example, the upper left corner of the box drawing set can be |
| 968 |
// displayed by printing "l" while in the alternate character set. |
| 969 |
// Its not quite that simple, since the "l" is the terminfo name, |
| 970 |
// and it may be necessary to use a different character based on |
| 971 |
// the terminal implementation (or the terminal may lack support for |
| 972 |
// this altogether). See buildAcsMap below for detail. |
| 973 |
var vtACSNames = map[byte]rune{ |
| 974 |
'+': RuneRArrow, |
| 975 |
',': RuneLArrow, |
| 976 |
'-': RuneUArrow, |
| 977 |
'.': RuneDArrow, |
| 978 |
'0': RuneBlock, |
| 979 |
'`': RuneDiamond, |
| 980 |
'a': RuneCkBoard, |
| 981 |
'b': '␉', // VT100, Not defined by terminfo |
| 982 |
'c': '␌', // VT100, Not defined by terminfo |
| 983 |
'd': '␋', // VT100, Not defined by terminfo |
| 984 |
'e': '␊', // VT100, Not defined by terminfo |
| 985 |
'f': RuneDegree, |
| 986 |
'g': RunePlMinus, |
| 987 |
'h': RuneBoard, |
| 988 |
'i': RuneLantern, |
| 989 |
'j': RuneLRCorner, |
| 990 |
'k': RuneURCorner, |
| 991 |
'l': RuneULCorner, |
| 992 |
'm': RuneLLCorner, |
| 993 |
'n': RunePlus, |
| 994 |
'o': RuneS1, |
| 995 |
'p': RuneS3, |
| 996 |
'q': RuneHLine, |
| 997 |
'r': RuneS7, |
| 998 |
's': RuneS9, |
| 999 |
't': RuneLTee, |
| 1000 |
'u': RuneRTee, |
| 1001 |
'v': RuneBTee, |
| 1002 |
'w': RuneTTee, |
| 1003 |
'x': RuneVLine, |
| 1004 |
'y': RuneLEqual, |
| 1005 |
'z': RuneGEqual, |
| 1006 |
'{': RunePi, |
| 1007 |
'|': RuneNEqual, |
| 1008 |
'}': RuneSterling, |
| 1009 |
'~': RuneBullet, |
| 1010 |
} |
| 1011 |
|
| 1012 |
// buildAcsMap builds a map of characters that we translate from Unicode to |
| 1013 |
// alternate character encodings. To do this, we use the standard VT100 ACS |
| 1014 |
// maps. This is only done if the terminal lacks support for Unicode; we |
| 1015 |
// always prefer to emit Unicode glyphs when we are able. |
| 1016 |
func (t *tScreen) buildAcsMap() { |
| 1017 |
acsstr := t.ti.AltChars |
| 1018 |
t.acs = make(map[rune]string) |
| 1019 |
for len(acsstr) > 2 { |
| 1020 |
srcv := acsstr[0] |
| 1021 |
dstv := string(acsstr[1]) |
| 1022 |
if r, ok := vtACSNames[srcv]; ok { |
| 1023 |
t.acs[r] = t.ti.EnterAcs + dstv + t.ti.ExitAcs |
| 1024 |
} |
| 1025 |
acsstr = acsstr[2:] |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
func (t *tScreen) PostEventWait(ev Event) { |
| 1030 |
t.evch <- ev |
| 1031 |
} |
| 1032 |
|
| 1033 |
func (t *tScreen) PostEvent(ev Event) error { |
| 1034 |
select { |
| 1035 |
case t.evch <- ev: |
| 1036 |
return nil |
| 1037 |
default: |
| 1038 |
return ErrEventQFull |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
func (t *tScreen) clip(x, y int) (int, int) { |
| 1043 |
w, h := t.cells.Size() |
| 1044 |
if x < 0 { |
| 1045 |
x = 0 |
| 1046 |
} |
| 1047 |
if y < 0 { |
| 1048 |
y = 0 |
| 1049 |
} |
| 1050 |
if x > w-1 { |
| 1051 |
x = w - 1 |
| 1052 |
} |
| 1053 |
if y > h-1 { |
| 1054 |
y = h - 1 |
| 1055 |
} |
| 1056 |
return x, y |
| 1057 |
} |
| 1058 |
|
| 1059 |
// buildMouseEvent returns an event based on the supplied coordinates and button |
| 1060 |
// state. Note that the screen's mouse button state is updated based on the |
| 1061 |
// input to this function (i.e. it mutates the receiver). |
| 1062 |
func (t *tScreen) buildMouseEvent(x, y, btn int) *EventMouse { |
| 1063 |
// XTerm mouse events only report at most one button at a time, |
| 1064 |
// which may include a wheel button. Wheel motion events are |
| 1065 |
// reported as single impulses, while other button events are reported |
| 1066 |
// as separate press & release events. |
| 1067 |
|
| 1068 |
button := ButtonNone |
| 1069 |
mod := ModNone |
| 1070 |
|
| 1071 |
// Mouse wheel has bit 6 set, no release events. It should be noted |
| 1072 |
// that wheel events are sometimes misdelivered as mouse button events |
| 1073 |
// during a click-drag, so we debounce these, considering them to be |
| 1074 |
// button press events unless we see an intervening release event. |
| 1075 |
switch btn & 0x43 { |
| 1076 |
case 0: |
| 1077 |
button = Button1 |
| 1078 |
t.wasbtn = true |
| 1079 |
case 1: |
| 1080 |
button = Button3 // Note we prefer to treat right as button 2 |
| 1081 |
t.wasbtn = true |
| 1082 |
case 2: |
| 1083 |
button = Button2 // And the middle button as button 3 |
| 1084 |
t.wasbtn = true |
| 1085 |
case 3: |
| 1086 |
button = ButtonNone |
| 1087 |
t.wasbtn = false |
| 1088 |
case 0x40: |
| 1089 |
if !t.wasbtn { |
| 1090 |
button = WheelUp |
| 1091 |
} else { |
| 1092 |
button = Button1 |
| 1093 |
} |
| 1094 |
case 0x41: |
| 1095 |
if !t.wasbtn { |
| 1096 |
button = WheelDown |
| 1097 |
} else { |
| 1098 |
button = Button1 |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
if btn&0x4 != 0 { |
| 1103 |
mod |= ModShift |
| 1104 |
} |
| 1105 |
if btn&0x8 != 0 { |
| 1106 |
mod |= ModAlt |
| 1107 |
} |
| 1108 |
if btn&0x10 != 0 { |
| 1109 |
mod |= ModCtrl |
| 1110 |
} |
| 1111 |
|
| 1112 |
// Some terminals will report mouse coordinates outside the |
| 1113 |
// screen, especially with click-drag events. Clip the coordinates |
| 1114 |
// to the screen in that case. |
| 1115 |
x, y = t.clip(x, y) |
| 1116 |
|
| 1117 |
escseq := t.escbuf.String() |
| 1118 |
t.escbuf.Reset() |
| 1119 |
return NewEventMouse(x, y, button, mod, escseq) |
| 1120 |
} |
| 1121 |
|
| 1122 |
// parseSgrMouse attempts to locate an SGR mouse record at the start of the |
| 1123 |
// buffer. It returns true, true if it found one, and the associated bytes |
| 1124 |
// be removed from the buffer. It returns true, false if the buffer might |
| 1125 |
// contain such an event, but more bytes are necessary (partial match), and |
| 1126 |
// false, false if the content is definitely *not* an SGR mouse record. |
| 1127 |
func (t *tScreen) parseSgrMouse(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1128 |
b := buf.Bytes() |
| 1129 |
|
| 1130 |
var x, y, btn, state int |
| 1131 |
dig := false |
| 1132 |
neg := false |
| 1133 |
motion := false |
| 1134 |
i := 0 |
| 1135 |
val := 0 |
| 1136 |
|
| 1137 |
if t.escaped { |
| 1138 |
state = 1 |
| 1139 |
} |
| 1140 |
|
| 1141 |
for i = range b { |
| 1142 |
switch b[i] { |
| 1143 |
case '\x1b': |
| 1144 |
if state != 0 { |
| 1145 |
return false, false |
| 1146 |
} |
| 1147 |
state = 1 |
| 1148 |
|
| 1149 |
case '\x9b': |
| 1150 |
if state != 0 { |
| 1151 |
return false, false |
| 1152 |
} |
| 1153 |
state = 2 |
| 1154 |
|
| 1155 |
case '[': |
| 1156 |
if state != 1 { |
| 1157 |
return false, false |
| 1158 |
} |
| 1159 |
state = 2 |
| 1160 |
|
| 1161 |
case '<': |
| 1162 |
if state != 2 { |
| 1163 |
return false, false |
| 1164 |
} |
| 1165 |
val = 0 |
| 1166 |
dig = false |
| 1167 |
neg = false |
| 1168 |
state = 3 |
| 1169 |
|
| 1170 |
case '-': |
| 1171 |
if state != 3 && state != 4 && state != 5 { |
| 1172 |
return false, false |
| 1173 |
} |
| 1174 |
if dig || neg { |
| 1175 |
|
| 1176 |
return false, false |
| 1177 |
} |
| 1178 |
neg = true // stay in state |
| 1179 |
|
| 1180 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 1181 |
if state != 3 && state != 4 && state != 5 { |
| 1182 |
return false, false |
| 1183 |
} |
| 1184 |
val *= 10 |
| 1185 |
val += int(b[i] - '0') |
| 1186 |
dig = true // stay in state |
| 1187 |
|
| 1188 |
case ';': |
| 1189 |
if neg { |
| 1190 |
val = -val |
| 1191 |
} |
| 1192 |
switch state { |
| 1193 |
case 3: |
| 1194 |
btn, val = val, 0 |
| 1195 |
neg, dig, state = false, false, 4 |
| 1196 |
case 4: |
| 1197 |
x, val = val-1, 0 |
| 1198 |
neg, dig, state = false, false, 5 |
| 1199 |
default: |
| 1200 |
return false, false |
| 1201 |
} |
| 1202 |
|
| 1203 |
case 'm', 'M': |
| 1204 |
if state != 5 { |
| 1205 |
return false, false |
| 1206 |
} |
| 1207 |
if neg { |
| 1208 |
val = -val |
| 1209 |
} |
| 1210 |
y = val - 1 |
| 1211 |
|
| 1212 |
motion = (btn & 32) != 0 |
| 1213 |
btn &^= 32 |
| 1214 |
if b[i] == 'm' { |
| 1215 |
// mouse release, clear all buttons |
| 1216 |
btn |= 3 |
| 1217 |
btn &^= 0x40 |
| 1218 |
t.buttondn = false |
| 1219 |
} else if motion { |
| 1220 |
/* |
| 1221 |
* Some broken terminals appear to send |
| 1222 |
* mouse button one motion events, instead of |
| 1223 |
* encoding 35 (no buttons) into these events. |
| 1224 |
* We resolve these by looking for a non-motion |
| 1225 |
* event first. |
| 1226 |
*/ |
| 1227 |
if !t.buttondn { |
| 1228 |
btn |= 3 |
| 1229 |
btn &^= 0x40 |
| 1230 |
} |
| 1231 |
} else { |
| 1232 |
t.buttondn = true |
| 1233 |
} |
| 1234 |
// consume the event bytes |
| 1235 |
for i >= 0 { |
| 1236 |
by, _ := buf.ReadByte() |
| 1237 |
t.escbuf.WriteByte(by) |
| 1238 |
i-- |
| 1239 |
} |
| 1240 |
*evs = append(*evs, t.buildMouseEvent(x, y, btn)) |
| 1241 |
return true, true |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
// incomplete & inconclusve at this point |
| 1246 |
return true, false |
| 1247 |
} |
| 1248 |
|
| 1249 |
// parseXtermMouse is like parseSgrMouse, but it parses a legacy |
| 1250 |
// X11 mouse record. |
| 1251 |
func (t *tScreen) parseXtermMouse(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1252 |
|
| 1253 |
b := buf.Bytes() |
| 1254 |
|
| 1255 |
state := 0 |
| 1256 |
btn := 0 |
| 1257 |
x := 0 |
| 1258 |
y := 0 |
| 1259 |
|
| 1260 |
if t.escaped { |
| 1261 |
state = 1 |
| 1262 |
} |
| 1263 |
|
| 1264 |
for i := range b { |
| 1265 |
switch state { |
| 1266 |
case 0: |
| 1267 |
switch b[i] { |
| 1268 |
case '\x1b': |
| 1269 |
state = 1 |
| 1270 |
case '\x9b': |
| 1271 |
state = 2 |
| 1272 |
default: |
| 1273 |
return false, false |
| 1274 |
} |
| 1275 |
case 1: |
| 1276 |
if b[i] != '[' { |
| 1277 |
return false, false |
| 1278 |
} |
| 1279 |
state = 2 |
| 1280 |
case 2: |
| 1281 |
if b[i] != 'M' { |
| 1282 |
return false, false |
| 1283 |
} |
| 1284 |
state++ |
| 1285 |
case 3: |
| 1286 |
btn = int(b[i]) |
| 1287 |
state++ |
| 1288 |
case 4: |
| 1289 |
x = int(b[i]) - 32 - 1 |
| 1290 |
state++ |
| 1291 |
case 5: |
| 1292 |
y = int(b[i]) - 32 - 1 |
| 1293 |
for i >= 0 { |
| 1294 |
by, _ := buf.ReadByte() |
| 1295 |
t.escbuf.WriteByte(by) |
| 1296 |
i-- |
| 1297 |
} |
| 1298 |
*evs = append(*evs, t.buildMouseEvent(x, y, btn)) |
| 1299 |
return true, true |
| 1300 |
} |
| 1301 |
} |
| 1302 |
return true, false |
| 1303 |
} |
| 1304 |
|
| 1305 |
func (t *tScreen) parseFunctionKey(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1306 |
b := buf.Bytes() |
| 1307 |
partial := false |
| 1308 |
for e, k := range t.keycodes { |
| 1309 |
esc := []byte(e) |
| 1310 |
if (len(esc) == 1) && (esc[0] == '\x1b') { |
| 1311 |
continue |
| 1312 |
} |
| 1313 |
if bytes.HasPrefix(b, esc) { |
| 1314 |
// matched |
| 1315 |
var r rune |
| 1316 |
if len(esc) == 1 { |
| 1317 |
r = rune(b[0]) |
| 1318 |
} |
| 1319 |
mod := k.mod |
| 1320 |
if t.escaped { |
| 1321 |
mod |= ModAlt |
| 1322 |
t.escaped = false |
| 1323 |
} |
| 1324 |
for i := 0; i < len(esc); i++ { |
| 1325 |
by, _ := buf.ReadByte() |
| 1326 |
t.escbuf.WriteByte(by) |
| 1327 |
} |
| 1328 |
*evs = append(*evs, NewEventKey(k.key, r, mod, t.escbuf.String())) |
| 1329 |
t.escbuf.Reset() |
| 1330 |
return true, true |
| 1331 |
} |
| 1332 |
if bytes.HasPrefix(esc, b) { |
| 1333 |
partial = true |
| 1334 |
} |
| 1335 |
} |
| 1336 |
return partial, false |
| 1337 |
} |
| 1338 |
|
| 1339 |
func (t *tScreen) parseRune(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1340 |
b := buf.Bytes() |
| 1341 |
if b[0] >= ' ' && b[0] <= 0x7F { |
| 1342 |
// printable ASCII easy to deal with -- no encodings |
| 1343 |
mod := ModNone |
| 1344 |
if t.escaped { |
| 1345 |
mod = ModAlt |
| 1346 |
t.escaped = false |
| 1347 |
} |
| 1348 |
by, _ := buf.ReadByte() |
| 1349 |
t.escbuf.WriteByte(by) |
| 1350 |
*evs = append(*evs, NewEventKey(KeyRune, rune(b[0]), mod, t.escbuf.String())) |
| 1351 |
t.escbuf.Reset() |
| 1352 |
return true, true |
| 1353 |
} |
| 1354 |
|
| 1355 |
if b[0] < 0x80 { |
| 1356 |
// Low numbered values are control keys, not runes. |
| 1357 |
return false, false |
| 1358 |
} |
| 1359 |
|
| 1360 |
utfb := make([]byte, 12) |
| 1361 |
for l := 1; l <= len(b); l++ { |
| 1362 |
t.decoder.Reset() |
| 1363 |
nout, nin, e := t.decoder.Transform(utfb, b[:l], true) |
| 1364 |
if e == transform.ErrShortSrc { |
| 1365 |
continue |
| 1366 |
} |
| 1367 |
if nout != 0 { |
| 1368 |
r, _ := utf8.DecodeRune(utfb[:nout]) |
| 1369 |
if r != utf8.RuneError { |
| 1370 |
mod := ModNone |
| 1371 |
if t.escaped { |
| 1372 |
mod = ModAlt |
| 1373 |
t.escaped = false |
| 1374 |
} |
| 1375 |
*evs = append(*evs, NewEventKey(KeyRune, r, mod, t.escbuf.String())) |
| 1376 |
t.escbuf.Reset() |
| 1377 |
} |
| 1378 |
for nin > 0 { |
| 1379 |
by, _ := buf.ReadByte() |
| 1380 |
t.escbuf.WriteByte(by) |
| 1381 |
nin-- |
| 1382 |
} |
| 1383 |
return true, true |
| 1384 |
} |
| 1385 |
} |
| 1386 |
// Looks like potential escape |
| 1387 |
return true, false |
| 1388 |
} |
| 1389 |
|
| 1390 |
// This function interprets a block of characters without escapes as a paste |
| 1391 |
// Generally the terminal will only send large blocks of text if a paste is |
| 1392 |
// occurring, though it may send small blocks of characters together if the user |
| 1393 |
// is typing quickly |
| 1394 |
// We set a threshold of 8 bytes before making the block into a paste |
| 1395 |
func (t *tScreen) parsePaste(buf *bytes.Buffer, evs *[]Event) bool { |
| 1396 |
b := buf.Bytes() |
| 1397 |
|
| 1398 |
if b[0] != '\x1b' { |
| 1399 |
esci := bytes.IndexByte(b, '\x1b') |
| 1400 |
if esci != -1 { |
| 1401 |
b = b[:esci] |
| 1402 |
} |
| 1403 |
if len(b) > 1 { |
| 1404 |
for i := 0; i < len(b); i++ { |
| 1405 |
by, _ := buf.ReadByte() |
| 1406 |
t.escbuf.WriteByte(by) |
| 1407 |
} |
| 1408 |
str := string(bytes.Replace(b, []byte{'\r'}, []byte{'\n'}, -1)) |
| 1409 |
*evs = append(*evs, NewEventPaste(str, t.escbuf.String())) |
| 1410 |
t.escbuf.Reset() |
| 1411 |
return true |
| 1412 |
} |
| 1413 |
} |
| 1414 |
return false |
| 1415 |
} |
| 1416 |
|
| 1417 |
func (t *tScreen) parseRegisteredRawSeq(buf *bytes.Buffer, evs *[]Event) bool { |
| 1418 |
b := buf.Bytes() |
| 1419 |
|
| 1420 |
if b[0] == '\x1b' && len(t.rawseq) > 0 { |
| 1421 |
strb := string(b) |
| 1422 |
for _, r := range t.rawseq { |
| 1423 |
if strings.HasPrefix(strb, r) { |
| 1424 |
// a registered raw sequence matched the prefix |
| 1425 |
*evs = append(*evs, NewEventRaw(r)) |
| 1426 |
t.escbuf.Reset() |
| 1427 |
for i := 0; i < len(r); i++ { |
| 1428 |
buf.ReadByte() |
| 1429 |
} |
| 1430 |
return true |
| 1431 |
} |
| 1432 |
} |
| 1433 |
} |
| 1434 |
return false |
| 1435 |
} |
| 1436 |
|
| 1437 |
func (t *tScreen) parseOSC52Paste(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1438 |
str := buf.String() |
| 1439 |
b := buf.Bytes() |
| 1440 |
|
| 1441 |
prefixLen := len(pasteOSC52Begin) + 2 |
| 1442 |
if strings.HasPrefix(str, pasteOSC52Begin) || strings.HasPrefix(pasteOSC52Begin, str) { |
| 1443 |
idx := strings.Index(str, pasteOSC52End) |
| 1444 |
log.Println(b, idx, len(str)) |
| 1445 |
if idx >= prefixLen { |
| 1446 |
// OSC52 paste has ended |
| 1447 |
payload := buf.Next(idx)[prefixLen:] |
| 1448 |
buf.Next(len(pasteOSC52End)) |
| 1449 |
data := make([]byte, len(payload)) |
| 1450 |
n, err := base64.StdEncoding.Decode(data, payload) |
| 1451 |
data = data[:n] |
| 1452 |
|
| 1453 |
t.escbuf.Write(b) |
| 1454 |
|
| 1455 |
if err != nil { |
| 1456 |
// discard the paste since it is invalid |
| 1457 |
return true, true |
| 1458 |
} |
| 1459 |
|
| 1460 |
*evs = append(*evs, NewEventPaste(string(data), t.escbuf.String())) |
| 1461 |
t.escbuf.Reset() |
| 1462 |
return true, true |
| 1463 |
} |
| 1464 |
// More still coming |
| 1465 |
return true, false |
| 1466 |
} |
| 1467 |
|
| 1468 |
return false, false |
| 1469 |
} |
| 1470 |
|
| 1471 |
func (t *tScreen) parseBracketedPaste(buf *bytes.Buffer, evs *[]Event) (bool, bool) { |
| 1472 |
// Replace all carriage returns with newlines |
| 1473 |
str := strings.Replace(buf.String(), "\r", "\n", -1) |
| 1474 |
if strings.HasPrefix(str, pasteBegin) || strings.HasPrefix(pasteBegin, str) { |
| 1475 |
idx := strings.Index(str, pasteEnd) |
| 1476 |
// The bracketed paste has started |
| 1477 |
if idx != -1 && idx >= len(pasteBegin) { |
| 1478 |
// The bracketed paste has ended |
| 1479 |
// Strip out the start and end sequences |
| 1480 |
t.escbuf.Write(buf.Next(idx + len(pasteEnd))) |
| 1481 |
text := str[len(pasteBegin):idx] |
| 1482 |
*evs = append(*evs, NewEventPaste(text, t.escbuf.String())) |
| 1483 |
t.escbuf.Reset() |
| 1484 |
return true, true |
| 1485 |
} |
| 1486 |
// There is still more coming |
| 1487 |
return true, false |
| 1488 |
} |
| 1489 |
return false, false |
| 1490 |
} |
| 1491 |
|
| 1492 |
func (t *tScreen) scanInput(buf *bytes.Buffer, expire bool) { |
| 1493 |
evs := t.collectEventsFromInput(buf, expire) |
| 1494 |
|
| 1495 |
for _, ev := range evs { |
| 1496 |
switch ev.(type) { |
| 1497 |
case *EventMouse: |
| 1498 |
t.PostEvent(ev) |
| 1499 |
default: |
| 1500 |
t.PostEventWait(ev) |
| 1501 |
} |
| 1502 |
} |
| 1503 |
} |
| 1504 |
|
| 1505 |
// Return an array of Events extracted from the supplied buffer. This is done |
| 1506 |
// while holding the screen's lock - the events can then be queued for |
| 1507 |
// application processing with the lock released. |
| 1508 |
func (t *tScreen) collectEventsFromInput(buf *bytes.Buffer, expire bool) []Event { |
| 1509 |
res := make([]Event, 0, 20) |
| 1510 |
|
| 1511 |
t.Lock() |
| 1512 |
defer t.Unlock() |
| 1513 |
|
| 1514 |
for { |
| 1515 |
b := buf.Bytes() |
| 1516 |
if len(b) == 0 { |
| 1517 |
buf.Reset() |
| 1518 |
return res |
| 1519 |
} |
| 1520 |
|
| 1521 |
partials := 0 |
| 1522 |
|
| 1523 |
if t.parseRegisteredRawSeq(buf, &res) { |
| 1524 |
continue |
| 1525 |
} |
| 1526 |
|
| 1527 |
if t.paste && t.parsePaste(buf, &res) { |
| 1528 |
continue |
| 1529 |
} |
| 1530 |
|
| 1531 |
if part, comp := t.parseOSC52Paste(buf, &res); comp { |
| 1532 |
continue |
| 1533 |
} else if part { |
| 1534 |
partials++ |
| 1535 |
} |
| 1536 |
|
| 1537 |
if part, comp := t.parseBracketedPaste(buf, &res); comp { |
| 1538 |
continue |
| 1539 |
} else if part { |
| 1540 |
partials++ |
| 1541 |
} |
| 1542 |
|
| 1543 |
if part, comp := t.parseRune(buf, &res); comp { |
| 1544 |
continue |
| 1545 |
} else if part { |
| 1546 |
partials++ |
| 1547 |
} |
| 1548 |
|
| 1549 |
if part, comp := t.parseFunctionKey(buf, &res); comp { |
| 1550 |
continue |
| 1551 |
} else if part { |
| 1552 |
partials++ |
| 1553 |
} |
| 1554 |
|
| 1555 |
// Only parse mouse records if this term claims to have |
| 1556 |
// mouse support |
| 1557 |
|
| 1558 |
if t.ti.Mouse != "" { |
| 1559 |
if part, comp := t.parseXtermMouse(buf, &res); comp { |
| 1560 |
continue |
| 1561 |
} else if part { |
| 1562 |
partials++ |
| 1563 |
} |
| 1564 |
|
| 1565 |
if part, comp := t.parseSgrMouse(buf, &res); comp { |
| 1566 |
continue |
| 1567 |
} else if part { |
| 1568 |
partials++ |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
if partials == 0 || expire { |
| 1573 |
if b[0] == '\x1b' { |
| 1574 |
if len(b) == 1 { |
| 1575 |
res = append(res, NewEventKey(KeyEsc, 0, ModNone, "\x1b")) |
| 1576 |
t.escbuf.Reset() |
| 1577 |
t.escaped = false |
| 1578 |
} else { |
| 1579 |
t.escaped = true |
| 1580 |
} |
| 1581 |
by, _ := buf.ReadByte() |
| 1582 |
t.escbuf.WriteByte(by) |
| 1583 |
continue |
| 1584 |
} |
| 1585 |
// Nothing was going to match, or we timed out |
| 1586 |
// waiting for more data -- just deliver the characters |
| 1587 |
// to the app & let them sort it out. Possibly we |
| 1588 |
// should only do this for control characters like ESC. |
| 1589 |
by, _ := buf.ReadByte() |
| 1590 |
t.escbuf.WriteByte(by) |
| 1591 |
res = append(res, NewEventRaw(t.escbuf.String())) |
| 1592 |
t.escbuf.Reset() |
| 1593 |
continue |
| 1594 |
} |
| 1595 |
|
| 1596 |
// well we have some partial data, wait until we get |
| 1597 |
// some more |
| 1598 |
break |
| 1599 |
} |
| 1600 |
|
| 1601 |
return res |
| 1602 |
} |
| 1603 |
|
| 1604 |
func (t *tScreen) mainLoop() { |
| 1605 |
buf := &bytes.Buffer{} |
| 1606 |
t.escbuf = &bytes.Buffer{} |
| 1607 |
for { |
| 1608 |
select { |
| 1609 |
case <-t.quit: |
| 1610 |
close(t.indoneq) |
| 1611 |
return |
| 1612 |
case <-t.sigwinch: |
| 1613 |
t.Lock() |
| 1614 |
t.cx = -1 |
| 1615 |
t.cy = -1 |
| 1616 |
t.resize() |
| 1617 |
t.cells.Invalidate() |
| 1618 |
t.draw() |
| 1619 |
t.Unlock() |
| 1620 |
continue |
| 1621 |
case <-t.keytimer.C: |
| 1622 |
// If the timer fired, and the current time |
| 1623 |
// is after the expiration of the escape sequence, |
| 1624 |
// then we assume the escape sequence reached it's |
| 1625 |
// conclusion, and process the chunk independently. |
| 1626 |
// This lets us detect conflicts such as a lone ESC. |
| 1627 |
if buf.Len() > 0 { |
| 1628 |
if time.Now().After(t.keyexpire) { |
| 1629 |
t.scanInput(buf, true) |
| 1630 |
} |
| 1631 |
} |
| 1632 |
if buf.Len() > 0 { |
| 1633 |
if !t.keytimer.Stop() { |
| 1634 |
select { |
| 1635 |
case <-t.keytimer.C: |
| 1636 |
default: |
| 1637 |
} |
| 1638 |
} |
| 1639 |
t.keytimer.Reset(time.Millisecond * 50) |
| 1640 |
} |
| 1641 |
case chunk := <-t.keychan: |
| 1642 |
buf.Write(chunk) |
| 1643 |
t.keyexpire = time.Now().Add(time.Millisecond * 50) |
| 1644 |
t.scanInput(buf, false) |
| 1645 |
if !t.keytimer.Stop() { |
| 1646 |
select { |
| 1647 |
case <-t.keytimer.C: |
| 1648 |
default: |
| 1649 |
} |
| 1650 |
} |
| 1651 |
if buf.Len() > 0 { |
| 1652 |
t.keytimer.Reset(time.Millisecond * 50) |
| 1653 |
} |
| 1654 |
} |
| 1655 |
} |
| 1656 |
} |
| 1657 |
|
| 1658 |
func (t *tScreen) inputLoop() { |
| 1659 |
for { |
| 1660 |
chunk := make([]byte, 4096) |
| 1661 |
n, e := t.in.Read(chunk) |
| 1662 |
switch e { |
| 1663 |
case nil: |
| 1664 |
default: |
| 1665 |
t.PostEvent(NewEventError(e)) |
| 1666 |
return |
| 1667 |
} |
| 1668 |
t.keychan <- chunk[:n] |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
func (t *tScreen) Sync() { |
| 1673 |
t.Lock() |
| 1674 |
t.cx = -1 |
| 1675 |
t.cy = -1 |
| 1676 |
if !t.fini { |
| 1677 |
t.resize() |
| 1678 |
t.clear = true |
| 1679 |
t.cells.Invalidate() |
| 1680 |
t.draw() |
| 1681 |
} |
| 1682 |
t.Unlock() |
| 1683 |
} |
| 1684 |
|
| 1685 |
func (t *tScreen) CharacterSet() string { |
| 1686 |
return t.charset |
| 1687 |
} |
| 1688 |
|
| 1689 |
func (t *tScreen) RegisterRuneFallback(orig rune, fallback string) { |
| 1690 |
t.Lock() |
| 1691 |
t.fallback[orig] = fallback |
| 1692 |
t.Unlock() |
| 1693 |
} |
| 1694 |
|
| 1695 |
func (t *tScreen) UnregisterRuneFallback(orig rune) { |
| 1696 |
t.Lock() |
| 1697 |
delete(t.fallback, orig) |
| 1698 |
t.Unlock() |
| 1699 |
} |
| 1700 |
|
| 1701 |
func (t *tScreen) CanDisplay(r rune, checkFallbacks bool) bool { |
| 1702 |
|
| 1703 |
if enc := t.encoder; enc != nil { |
| 1704 |
nb := make([]byte, 6) |
| 1705 |
ob := make([]byte, 6) |
| 1706 |
num := utf8.EncodeRune(ob, r) |
| 1707 |
|
| 1708 |
enc.Reset() |
| 1709 |
dst, _, err := enc.Transform(nb, ob[:num], true) |
| 1710 |
if dst != 0 && err == nil && nb[0] != '\x1A' { |
| 1711 |
return true |
| 1712 |
} |
| 1713 |
} |
| 1714 |
// Terminal fallbacks always permitted, since we assume they are |
| 1715 |
// basically nearly perfect renditions. |
| 1716 |
if _, ok := t.acs[r]; ok { |
| 1717 |
return true |
| 1718 |
} |
| 1719 |
if !checkFallbacks { |
| 1720 |
return false |
| 1721 |
} |
| 1722 |
if _, ok := t.fallback[r]; ok { |
| 1723 |
return true |
| 1724 |
} |
| 1725 |
return false |
| 1726 |
} |
| 1727 |
|
| 1728 |
func (t *tScreen) HasMouse() bool { |
| 1729 |
return len(t.mouse) != 0 |
| 1730 |
} |
| 1731 |
|
| 1732 |
func (t *tScreen) HasKey(k Key) bool { |
| 1733 |
if k == KeyRune { |
| 1734 |
return true |
| 1735 |
} |
| 1736 |
return t.keyexist[k] |
| 1737 |
} |
| 1738 |
|
| 1739 |
func (t *tScreen) Resize(int, int, int, int) {} |
| 1740 |
|
| 1741 |
func (t *tScreen) GetClipboard(register string) error { |
| 1742 |
if len(register) <= 0 { |
| 1743 |
return errors.New("No register provided") |
| 1744 |
} |
| 1745 |
|
| 1746 |
r := register[0] |
| 1747 |
|
| 1748 |
if r != 'c' && r != 'p' { |
| 1749 |
return errors.New("Invalid register") |
| 1750 |
} |
| 1751 |
|
| 1752 |
t.TPuts(fmt.Sprintf(pasteGet, r)) |
| 1753 |
|
| 1754 |
return nil |
| 1755 |
} |
| 1756 |
|
| 1757 |
func (t *tScreen) SetClipboard(text, register string) error { |
| 1758 |
if len(register) <= 0 { |
| 1759 |
return errors.New("No register provided") |
| 1760 |
} |
| 1761 |
|
| 1762 |
r := register[0] |
| 1763 |
|
| 1764 |
if r != 'c' && r != 'p' { |
| 1765 |
return errors.New("Invalid register") |
| 1766 |
} |
| 1767 |
|
| 1768 |
t.TPuts(fmt.Sprintf(pasteClear, r)) |
| 1769 |
|
| 1770 |
var err error = nil |
| 1771 |
// Maximum paste length for OSC 52 |
| 1772 |
if len(text) >= 74994 { |
| 1773 |
err = errors.New("Text truncated: exceeds 74994 bytes") |
| 1774 |
} |
| 1775 |
|
| 1776 |
str := base64.StdEncoding.EncodeToString([]byte(text)) |
| 1777 |
|
| 1778 |
t.TPuts(fmt.Sprintf(pasteSet, r, str)) |
| 1779 |
|
| 1780 |
return err |
| 1781 |
} |