| 1 |
// Copyright 2016 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 |
"sync" |
| 19 |
"unicode/utf8" |
| 20 |
|
| 21 |
"golang.org/x/text/transform" |
| 22 |
) |
| 23 |
|
| 24 |
// NewSimulationScreen returns a SimulationScreen. Note that |
| 25 |
// SimulationScreen is also a Screen. |
| 26 |
func NewSimulationScreen(charset string) SimulationScreen { |
| 27 |
if charset == "" { |
| 28 |
charset = "UTF-8" |
| 29 |
} |
| 30 |
s := &simscreen{charset: charset} |
| 31 |
return s |
| 32 |
} |
| 33 |
|
| 34 |
// SimulationScreen represents a screen simulation. This is intended to |
| 35 |
// be a superset of normal Screens, but also adds some important interfaces |
| 36 |
// for testing. |
| 37 |
type SimulationScreen interface { |
| 38 |
// InjectKeyBytes injects a stream of bytes corresponding to |
| 39 |
// the native encoding (see charset). It turns true if the entire |
| 40 |
// set of bytes were processed and delivered as KeyEvents, false |
| 41 |
// if any bytes were not fully understood. Any bytes that are not |
| 42 |
// fully converted are discarded. |
| 43 |
InjectKeyBytes(buf []byte) bool |
| 44 |
|
| 45 |
// InjectKey injects a key event. The rune is a UTF-8 rune, post |
| 46 |
// any translation. |
| 47 |
InjectKey(key Key, r rune, mod ModMask) |
| 48 |
|
| 49 |
// InjectMouse injects a mouse event. |
| 50 |
InjectMouse(x, y int, buttons ButtonMask, mod ModMask) |
| 51 |
|
| 52 |
// InjectResize injects a resize event |
| 53 |
InjectResize() |
| 54 |
|
| 55 |
// SetSize resizes the underlying physical screen. It also causes |
| 56 |
// a resize event to be injected during the next Show() or Sync(). |
| 57 |
// A new physical contents array will be allocated (with data from |
| 58 |
// the old copied), so any prior value obtained with GetContents |
| 59 |
// won't be used anymore |
| 60 |
SetSize(width, height int) |
| 61 |
|
| 62 |
// GetContents returns screen contents as an array of |
| 63 |
// cells, along with the physical width & height. Note that the |
| 64 |
// physical contents will be used until the next time SetSize() |
| 65 |
// is called. |
| 66 |
GetContents() (cells []SimCell, width int, height int) |
| 67 |
|
| 68 |
// GetCursor returns the cursor details. |
| 69 |
GetCursor() (x int, y int, visible bool) |
| 70 |
|
| 71 |
Screen |
| 72 |
} |
| 73 |
|
| 74 |
// SimCell represents a simulated screen cell. The purpose of this |
| 75 |
// is to track on screen content. |
| 76 |
type SimCell struct { |
| 77 |
// Bytes is the actual character bytes. Normally this is |
| 78 |
// rune data, but it could be be data in another encoding system. |
| 79 |
Bytes []byte |
| 80 |
|
| 81 |
// Style is the style used to display the data. |
| 82 |
Style Style |
| 83 |
|
| 84 |
// Runes is the list of runes, unadulterated, in UTF-8. |
| 85 |
Runes []rune |
| 86 |
} |
| 87 |
|
| 88 |
type simscreen struct { |
| 89 |
physw int |
| 90 |
physh int |
| 91 |
fini bool |
| 92 |
style Style |
| 93 |
evch chan Event |
| 94 |
quit chan struct{} |
| 95 |
|
| 96 |
front []SimCell |
| 97 |
back CellBuffer |
| 98 |
clear bool |
| 99 |
cursorx int |
| 100 |
cursory int |
| 101 |
cursorvis bool |
| 102 |
mouse bool |
| 103 |
charset string |
| 104 |
encoder transform.Transformer |
| 105 |
decoder transform.Transformer |
| 106 |
fillchar rune |
| 107 |
fillstyle Style |
| 108 |
fallback map[rune]string |
| 109 |
|
| 110 |
sync.Mutex |
| 111 |
} |
| 112 |
|
| 113 |
func (s *simscreen) Init() error { |
| 114 |
s.evch = make(chan Event, 10) |
| 115 |
s.quit = make(chan struct{}) |
| 116 |
s.fillchar = 'X' |
| 117 |
s.fillstyle = StyleDefault |
| 118 |
s.mouse = false |
| 119 |
s.physw = 80 |
| 120 |
s.physh = 25 |
| 121 |
s.cursorx = -1 |
| 122 |
s.cursory = -1 |
| 123 |
s.style = StyleDefault |
| 124 |
|
| 125 |
if enc := GetEncoding(s.charset); enc != nil { |
| 126 |
s.encoder = enc.NewEncoder() |
| 127 |
s.decoder = enc.NewDecoder() |
| 128 |
} else { |
| 129 |
return ErrNoCharset |
| 130 |
} |
| 131 |
|
| 132 |
s.front = make([]SimCell, s.physw*s.physh) |
| 133 |
s.back.Resize(80, 25) |
| 134 |
|
| 135 |
// default fallbacks |
| 136 |
s.fallback = make(map[rune]string) |
| 137 |
for k, v := range RuneFallbacks { |
| 138 |
s.fallback[k] = v |
| 139 |
} |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
func (s *simscreen) Fini() { |
| 144 |
s.Lock() |
| 145 |
s.fini = true |
| 146 |
s.back.Resize(0, 0) |
| 147 |
s.Unlock() |
| 148 |
if s.quit != nil { |
| 149 |
close(s.quit) |
| 150 |
} |
| 151 |
s.physw = 0 |
| 152 |
s.physh = 0 |
| 153 |
s.front = nil |
| 154 |
} |
| 155 |
|
| 156 |
func (s *simscreen) SetStyle(style Style) { |
| 157 |
s.Lock() |
| 158 |
s.style = style |
| 159 |
s.Unlock() |
| 160 |
} |
| 161 |
|
| 162 |
func (s *simscreen) Clear() { |
| 163 |
s.Fill(' ', s.style) |
| 164 |
} |
| 165 |
|
| 166 |
func (s *simscreen) Fill(r rune, style Style) { |
| 167 |
s.Lock() |
| 168 |
s.back.Fill(r, style) |
| 169 |
s.Unlock() |
| 170 |
} |
| 171 |
|
| 172 |
func (s *simscreen) SetCell(x, y int, style Style, ch ...rune) { |
| 173 |
|
| 174 |
if len(ch) > 0 { |
| 175 |
s.SetContent(x, y, ch[0], ch[1:], style) |
| 176 |
} else { |
| 177 |
s.SetContent(x, y, ' ', nil, style) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func (s *simscreen) SetContent(x, y int, mainc rune, combc []rune, st Style) { |
| 182 |
|
| 183 |
s.Lock() |
| 184 |
s.back.SetContent(x, y, mainc, combc, st) |
| 185 |
s.Unlock() |
| 186 |
} |
| 187 |
|
| 188 |
func (s *simscreen) GetContent(x, y int) (rune, []rune, Style, int) { |
| 189 |
var mainc rune |
| 190 |
var combc []rune |
| 191 |
var style Style |
| 192 |
var width int |
| 193 |
s.Lock() |
| 194 |
mainc, combc, style, width = s.back.GetContent(x, y) |
| 195 |
s.Unlock() |
| 196 |
return mainc, combc, style, width |
| 197 |
} |
| 198 |
|
| 199 |
func (s *simscreen) drawCell(x, y int) int { |
| 200 |
|
| 201 |
mainc, combc, style, width := s.back.GetContent(x, y) |
| 202 |
if !s.back.Dirty(x, y) { |
| 203 |
return width |
| 204 |
} |
| 205 |
if x >= s.physw || y >= s.physh || x < 0 || y < 0 { |
| 206 |
return width |
| 207 |
} |
| 208 |
simc := &s.front[(y*s.physw)+x] |
| 209 |
|
| 210 |
if style == StyleDefault { |
| 211 |
style = s.style |
| 212 |
} |
| 213 |
simc.Style = style |
| 214 |
simc.Runes = append([]rune{mainc}, combc...) |
| 215 |
|
| 216 |
// now emit runes - taking care to not overrun width with a |
| 217 |
// wide character, and to ensure that we emit exactly one regular |
| 218 |
// character followed up by any residual combing characters |
| 219 |
|
| 220 |
simc.Bytes = nil |
| 221 |
|
| 222 |
if x > s.physw-width { |
| 223 |
simc.Runes = []rune{' '} |
| 224 |
simc.Bytes = []byte{' '} |
| 225 |
return width |
| 226 |
} |
| 227 |
|
| 228 |
lbuf := make([]byte, 12) |
| 229 |
ubuf := make([]byte, 12) |
| 230 |
nout := 0 |
| 231 |
|
| 232 |
for _, r := range simc.Runes { |
| 233 |
|
| 234 |
l := utf8.EncodeRune(ubuf, r) |
| 235 |
|
| 236 |
nout, _, _ = s.encoder.Transform(lbuf, ubuf[:l], true) |
| 237 |
|
| 238 |
if nout == 0 || lbuf[0] == '\x1a' { |
| 239 |
|
| 240 |
// skip combining |
| 241 |
|
| 242 |
if subst, ok := s.fallback[r]; ok { |
| 243 |
simc.Bytes = append(simc.Bytes, |
| 244 |
[]byte(subst)...) |
| 245 |
|
| 246 |
} else if r >= ' ' && r <= '~' { |
| 247 |
simc.Bytes = append(simc.Bytes, byte(r)) |
| 248 |
|
| 249 |
} else if simc.Bytes == nil { |
| 250 |
simc.Bytes = append(simc.Bytes, '?') |
| 251 |
} |
| 252 |
} else { |
| 253 |
simc.Bytes = append(simc.Bytes, lbuf[:nout]...) |
| 254 |
} |
| 255 |
} |
| 256 |
s.back.SetDirty(x, y, false) |
| 257 |
return width |
| 258 |
} |
| 259 |
|
| 260 |
func (s *simscreen) ShowCursor(x, y int) { |
| 261 |
s.Lock() |
| 262 |
s.cursorx, s.cursory = x, y |
| 263 |
s.showCursor() |
| 264 |
s.Unlock() |
| 265 |
} |
| 266 |
|
| 267 |
func (s *simscreen) HideCursor() { |
| 268 |
s.ShowCursor(-1, -1) |
| 269 |
} |
| 270 |
|
| 271 |
func (s *simscreen) showCursor() { |
| 272 |
|
| 273 |
x, y := s.cursorx, s.cursory |
| 274 |
if x < 0 || y < 0 || x >= s.physw || y >= s.physh { |
| 275 |
s.cursorvis = false |
| 276 |
} else { |
| 277 |
s.cursorvis = true |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func (s *simscreen) hideCursor() { |
| 282 |
// does not update cursor position |
| 283 |
s.cursorvis = false |
| 284 |
} |
| 285 |
|
| 286 |
func (s *simscreen) Show() { |
| 287 |
s.Lock() |
| 288 |
s.resize() |
| 289 |
s.draw() |
| 290 |
s.Unlock() |
| 291 |
} |
| 292 |
|
| 293 |
func (s *simscreen) clearScreen() { |
| 294 |
// We emulate a hardware clear by filling with a specific pattern |
| 295 |
for i := range s.front { |
| 296 |
s.front[i].Style = s.fillstyle |
| 297 |
s.front[i].Runes = []rune{s.fillchar} |
| 298 |
s.front[i].Bytes = []byte{byte(s.fillchar)} |
| 299 |
} |
| 300 |
s.clear = false |
| 301 |
} |
| 302 |
|
| 303 |
func (s *simscreen) draw() { |
| 304 |
s.hideCursor() |
| 305 |
if s.clear { |
| 306 |
s.clearScreen() |
| 307 |
} |
| 308 |
|
| 309 |
w, h := s.back.Size() |
| 310 |
for y := 0; y < h; y++ { |
| 311 |
for x := 0; x < w; x++ { |
| 312 |
width := s.drawCell(x, y) |
| 313 |
x += width - 1 |
| 314 |
} |
| 315 |
} |
| 316 |
s.showCursor() |
| 317 |
} |
| 318 |
|
| 319 |
func (s *simscreen) EnableMouse() { |
| 320 |
s.mouse = true |
| 321 |
} |
| 322 |
|
| 323 |
func (s *simscreen) DisableMouse() { |
| 324 |
s.mouse = false |
| 325 |
} |
| 326 |
|
| 327 |
func (s *simscreen) Size() (int, int) { |
| 328 |
s.Lock() |
| 329 |
w, h := s.back.Size() |
| 330 |
s.Unlock() |
| 331 |
return w, h |
| 332 |
} |
| 333 |
|
| 334 |
func (s *simscreen) resize() { |
| 335 |
w, h := s.physw, s.physh |
| 336 |
ow, oh := s.back.Size() |
| 337 |
if w != ow || h != oh { |
| 338 |
s.back.Resize(w, h) |
| 339 |
ev := NewEventResize(w, h) |
| 340 |
s.PostEvent(ev) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
func (s *simscreen) Colors() int { |
| 345 |
return 256 |
| 346 |
} |
| 347 |
|
| 348 |
func (s *simscreen) PollEvent() Event { |
| 349 |
select { |
| 350 |
case <-s.quit: |
| 351 |
return nil |
| 352 |
case ev := <-s.evch: |
| 353 |
return ev |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
func (s *simscreen) PostEventWait(ev Event) { |
| 358 |
s.evch <- ev |
| 359 |
} |
| 360 |
|
| 361 |
func (s *simscreen) PostEvent(ev Event) error { |
| 362 |
select { |
| 363 |
case s.evch <- ev: |
| 364 |
return nil |
| 365 |
default: |
| 366 |
return ErrEventQFull |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func (s *simscreen) InjectMouse(x, y int, buttons ButtonMask, mod ModMask) { |
| 371 |
ev := NewEventMouse(x, y, buttons, mod, "") |
| 372 |
s.PostEvent(ev) |
| 373 |
} |
| 374 |
|
| 375 |
func (s *simscreen) InjectKey(key Key, r rune, mod ModMask) { |
| 376 |
ev := NewEventKey(key, r, mod, "") |
| 377 |
s.PostEvent(ev) |
| 378 |
} |
| 379 |
|
| 380 |
func (s *simscreen) InjectKeyBytes(b []byte) bool { |
| 381 |
failed := false |
| 382 |
|
| 383 |
outer: |
| 384 |
for len(b) > 0 { |
| 385 |
if b[0] >= ' ' && b[0] <= 0x7F { |
| 386 |
// printable ASCII easy to deal with -- no encodings |
| 387 |
ev := NewEventKey(KeyRune, rune(b[0]), ModNone, "") |
| 388 |
s.PostEvent(ev) |
| 389 |
b = b[1:] |
| 390 |
continue |
| 391 |
} |
| 392 |
|
| 393 |
if b[0] < 0x80 { |
| 394 |
mod := ModNone |
| 395 |
// No encodings start with low numbered values |
| 396 |
if Key(b[0]) >= KeyCtrlA && Key(b[0]) <= KeyCtrlZ { |
| 397 |
mod = ModCtrl |
| 398 |
} |
| 399 |
ev := NewEventKey(Key(b[0]), 0, mod, "") |
| 400 |
s.PostEvent(ev) |
| 401 |
continue |
| 402 |
} |
| 403 |
|
| 404 |
utfb := make([]byte, len(b)*4) // worst case |
| 405 |
for l := 1; l < len(b); l++ { |
| 406 |
s.decoder.Reset() |
| 407 |
nout, nin, _ := s.decoder.Transform(utfb, b[:l], true) |
| 408 |
|
| 409 |
if nout != 0 { |
| 410 |
r, _ := utf8.DecodeRune(utfb[:nout]) |
| 411 |
if r != utf8.RuneError { |
| 412 |
ev := NewEventKey(KeyRune, r, ModNone, "") |
| 413 |
s.PostEvent(ev) |
| 414 |
} |
| 415 |
b = b[nin:] |
| 416 |
continue outer |
| 417 |
} |
| 418 |
} |
| 419 |
failed = true |
| 420 |
b = b[1:] |
| 421 |
continue |
| 422 |
} |
| 423 |
|
| 424 |
return !failed |
| 425 |
} |
| 426 |
|
| 427 |
func (s *simscreen) InjectResize() { |
| 428 |
w, h := s.physw, s.physh |
| 429 |
ev := NewEventResize(w, h) |
| 430 |
s.PostEvent(ev) |
| 431 |
} |
| 432 |
|
| 433 |
func (s *simscreen) Sync() { |
| 434 |
s.Lock() |
| 435 |
s.clear = true |
| 436 |
s.resize() |
| 437 |
s.back.Invalidate() |
| 438 |
s.draw() |
| 439 |
s.Unlock() |
| 440 |
} |
| 441 |
|
| 442 |
func (s *simscreen) CharacterSet() string { |
| 443 |
return s.charset |
| 444 |
} |
| 445 |
|
| 446 |
func (s *simscreen) SetSize(w, h int) { |
| 447 |
s.Lock() |
| 448 |
newc := make([]SimCell, w*h) |
| 449 |
for row := 0; row < h && row < s.physh; row++ { |
| 450 |
for col := 0; col < w && col < s.physw; col++ { |
| 451 |
newc[(row*w)+col] = s.front[(row*s.physw)+col] |
| 452 |
} |
| 453 |
} |
| 454 |
s.cursorx, s.cursory = -1, -1 |
| 455 |
s.physw, s.physh = w, h |
| 456 |
s.front = newc |
| 457 |
s.back.Resize(w, h) |
| 458 |
s.Unlock() |
| 459 |
} |
| 460 |
|
| 461 |
func (s *simscreen) GetContents() ([]SimCell, int, int) { |
| 462 |
s.Lock() |
| 463 |
cells, w, h := s.front, s.physw, s.physh |
| 464 |
s.Unlock() |
| 465 |
return cells, w, h |
| 466 |
} |
| 467 |
|
| 468 |
func (s *simscreen) GetCursor() (int, int, bool) { |
| 469 |
s.Lock() |
| 470 |
x, y, vis := s.cursorx, s.cursory, s.cursorvis |
| 471 |
s.Unlock() |
| 472 |
return x, y, vis |
| 473 |
} |
| 474 |
|
| 475 |
func (s *simscreen) RegisterRuneFallback(r rune, subst string) { |
| 476 |
s.Lock() |
| 477 |
s.fallback[r] = subst |
| 478 |
s.Unlock() |
| 479 |
} |
| 480 |
|
| 481 |
func (s *simscreen) UnregisterRuneFallback(r rune) { |
| 482 |
s.Lock() |
| 483 |
delete(s.fallback, r) |
| 484 |
s.Unlock() |
| 485 |
} |
| 486 |
|
| 487 |
func (s *simscreen) CanDisplay(r rune, checkFallbacks bool) bool { |
| 488 |
|
| 489 |
if enc := s.encoder; enc != nil { |
| 490 |
nb := make([]byte, 6) |
| 491 |
ob := make([]byte, 6) |
| 492 |
num := utf8.EncodeRune(ob, r) |
| 493 |
|
| 494 |
enc.Reset() |
| 495 |
dst, _, err := enc.Transform(nb, ob[:num], true) |
| 496 |
if dst != 0 && err == nil && nb[0] != '\x1A' { |
| 497 |
return true |
| 498 |
} |
| 499 |
} |
| 500 |
if !checkFallbacks { |
| 501 |
return false |
| 502 |
} |
| 503 |
if _, ok := s.fallback[r]; ok { |
| 504 |
return true |
| 505 |
} |
| 506 |
return false |
| 507 |
} |
| 508 |
|
| 509 |
func (s *simscreen) HasMouse() bool { |
| 510 |
return false |
| 511 |
} |
| 512 |
|
| 513 |
func (s *simscreen) Resize(int, int, int, int) {} |
| 514 |
|
| 515 |
func (s *simscreen) HasKey(Key) bool { |
| 516 |
return true |
| 517 |
} |
| 518 |
|
| 519 |
func (s *simscreen) RegisterRawSeq(string) {} |
| 520 |
func (s *simscreen) UnregisterRawSeq(string) {} |
| 521 |
func (s *simscreen) SetPaste(bool) {} |
| 522 |
|
| 523 |
func (s *simscreen) GetClipboard(string) error { return nil } |
| 524 |
func (s *simscreen) SetClipboard(string, string) error { return nil } |
| 525 |
func (s *simscreen) Beep() error { return nil } |