csi.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import 'dart:collection';
  2. import 'package:xterm/terminal/modes.dart';
  3. import 'package:xterm/terminal/sgr.dart';
  4. import 'package:xterm/terminal/terminal.dart';
  5. import 'package:xterm/util/lookup_table.dart';
  6. typedef CsiSequenceHandler = void Function(CSI, Terminal);
  7. final _csiHandlers = FastLookupTable({
  8. 'c'.codeUnitAt(0): csiSendDeviceAttributesHandler,
  9. 'd'.codeUnitAt(0): csiLinePositionAbsolute,
  10. 'f'.codeUnitAt(0): csiCursorPositionHandler,
  11. 'g'.codeUnitAt(0): csiTabClearHandler,
  12. 'h'.codeUnitAt(0): csiModeHandler, // SM - Set Mode
  13. 'l'.codeUnitAt(0): csiModeHandler, // RM - Reset Mode
  14. 'm'.codeUnitAt(0): sgrHandler,
  15. 'n'.codeUnitAt(0): csiDeviceStatusReportHandler,
  16. 'r'.codeUnitAt(0): csiSetMarginsHandler, // DECSTBM
  17. 't'.codeUnitAt(0): csiWindowManipulation,
  18. 'A'.codeUnitAt(0): csiCursorUpHandler,
  19. 'B'.codeUnitAt(0): csiCursorDownHandler,
  20. 'C'.codeUnitAt(0): csiCursorForwardHandler,
  21. 'D'.codeUnitAt(0): csiCursorBackwardHandler,
  22. 'E'.codeUnitAt(0): csiCursorNextLineHandler,
  23. 'F'.codeUnitAt(0): csiCursorPrecedingLineHandler,
  24. 'G'.codeUnitAt(0): csiCursorHorizontalAbsoluteHandler,
  25. 'H'.codeUnitAt(0): csiCursorPositionHandler, // CUP - Cursor Position
  26. 'J'.codeUnitAt(0): csiEraseInDisplayHandler, // DECSED - Selective Erase
  27. 'K'.codeUnitAt(0): csiEraseInLineHandler,
  28. 'L'.codeUnitAt(0): csiInsertLinesHandler,
  29. 'M'.codeUnitAt(0): csiDeleteLinesHandler,
  30. 'P'.codeUnitAt(0): csiDeleteHandler,
  31. 'S'.codeUnitAt(0): csiScrollUpHandler,
  32. 'T'.codeUnitAt(0): csiScrollDownHandler,
  33. 'X'.codeUnitAt(0): csiEraseCharactersHandler,
  34. '@'.codeUnitAt(0): csiInsertBlankCharactersHandler,
  35. });
  36. class CSI {
  37. CSI({
  38. required this.params,
  39. required this.finalByte,
  40. // required this.intermediates,
  41. });
  42. int? prefix;
  43. List<int> params;
  44. int finalByte;
  45. // final List<int> intermediates;
  46. @override
  47. String toString() {
  48. return params.join(';') + String.fromCharCode(finalByte);
  49. }
  50. }
  51. /// Keep a singleton of [CSI] to reduce object allocation. This should only be
  52. /// modified by [_parseCsi].
  53. final _csi = CSI(
  54. finalByte: 0,
  55. params: [],
  56. );
  57. final _semicolon = ';'.codeUnitAt(0);
  58. /// Parse a CSI from the head of the queue. Return null if the CSI isn't
  59. /// complete.
  60. CSI? _parseCsi(Queue<int> queue) {
  61. _csi.params.clear();
  62. // Keep track of how many characters should be taken from the queue.
  63. var readOffset = 0;
  64. if (queue.isEmpty) {
  65. return null;
  66. }
  67. // ascii char
  68. // 48 '0'
  69. // 49 '1'
  70. // 50 '2'
  71. // 51 '3'
  72. // 52 '4'
  73. // 53 '5'
  74. // 54 '6'
  75. // 55 '7'
  76. // 56 '8'
  77. // 57 '9'
  78. // 58 ':'
  79. // 59 ';'
  80. // 60 '<'
  81. // 61 '='
  82. // 62 '>'
  83. // 63 '?'
  84. // test whether the csi is a `CSI ? Ps ...` or `CSI Ps ...`
  85. final firstChar = queue.first;
  86. if (firstChar >= 58 && firstChar <= 63) {
  87. _csi.prefix = firstChar;
  88. readOffset++;
  89. } else {
  90. _csi.prefix = null;
  91. }
  92. var param = 0;
  93. var hasParam = false;
  94. while (true) {
  95. // The sequence isn't completed, just ignore it.
  96. if (queue.length <= readOffset) {
  97. return null;
  98. }
  99. // final char = queue.removeFirst();
  100. final char = queue.elementAt(readOffset++);
  101. if (char == _semicolon) {
  102. if (hasParam) {
  103. _csi.params.add(param);
  104. }
  105. param = 0;
  106. continue;
  107. }
  108. // '0' <= char <= '9'
  109. if (char >= 48 && char <= 57) {
  110. hasParam = true;
  111. param *= 10;
  112. param += char - 48;
  113. continue;
  114. }
  115. if (char > 0 && char <= 0x2F) {
  116. // intermediates.add(char);
  117. continue;
  118. }
  119. const csiMin = 0x40;
  120. const csiMax = 0x7e;
  121. if (char >= csiMin && char <= csiMax) {
  122. // The sequence is complete. So we consume it from the queue.
  123. for (var i = 0; i < readOffset; i++) {
  124. queue.removeFirst();
  125. }
  126. if (hasParam) {
  127. _csi.params.add(param);
  128. }
  129. _csi.finalByte = char;
  130. return _csi;
  131. }
  132. }
  133. }
  134. /// CSI - Control Sequence Introducer: sequence starting with ESC [ (7bit) or
  135. /// CSI (\x9B, 8bit)
  136. bool csiHandler(Queue<int> queue, Terminal terminal) {
  137. final csi = _parseCsi(queue);
  138. if (csi == null) {
  139. return false;
  140. }
  141. // terminal.debug.onCsi(csi);
  142. final handler = _csiHandlers[csi.finalByte];
  143. if (handler != null) {
  144. handler(csi, terminal);
  145. } else {
  146. terminal.debug.onError('unknown: $csi');
  147. }
  148. return true;
  149. }
  150. /// DECSED - Selective Erase In Display
  151. ///
  152. /// ```text
  153. /// CSI ? P s J
  154. ///
  155. /// Erase in Display (DECSED)
  156. ///
  157. /// P s = 0 → Selective Erase Below (default)
  158. /// P s = 1 → Selective Erase Above
  159. /// P s = 2 → Selective Erase All
  160. /// ```
  161. void csiEraseInDisplayHandler(CSI csi, Terminal terminal) {
  162. var ps = 0;
  163. if (csi.params.isNotEmpty) {
  164. ps = csi.params.first;
  165. }
  166. switch (ps) {
  167. case 0:
  168. terminal.buffer.eraseDisplayFromCursor();
  169. break;
  170. case 1:
  171. terminal.buffer.eraseDisplayToCursor();
  172. break;
  173. case 2:
  174. case 3:
  175. terminal.buffer.eraseDisplay();
  176. break;
  177. default:
  178. terminal.debug.onError("Unsupported ED: CSI $ps J");
  179. }
  180. }
  181. void csiEraseInLineHandler(CSI csi, Terminal terminal) {
  182. var ps = 0;
  183. if (csi.params.isNotEmpty) {
  184. ps = csi.params.first;
  185. }
  186. switch (ps) {
  187. case 0:
  188. terminal.buffer.eraseLineFromCursor();
  189. break;
  190. case 1:
  191. terminal.buffer.eraseLineToCursor();
  192. break;
  193. case 2:
  194. terminal.buffer.eraseLine();
  195. break;
  196. default:
  197. terminal.debug.onError("Unsupported EL: CSI $ps K");
  198. }
  199. }
  200. /// CUP - Cursor Position
  201. void csiCursorPositionHandler(CSI csi, Terminal terminal) {
  202. var x = 1;
  203. var y = 1;
  204. if (csi.params.length == 2) {
  205. y = csi.params[0];
  206. x = csi.params[1];
  207. }
  208. terminal.buffer.setPosition(x - 1, y - 1);
  209. }
  210. void csiLinePositionAbsolute(CSI csi, Terminal terminal) {
  211. var row = 1;
  212. if (csi.params.isNotEmpty) {
  213. row = csi.params.first;
  214. }
  215. terminal.buffer.setCursorY(row - 1);
  216. }
  217. void csiCursorHorizontalAbsoluteHandler(CSI csi, Terminal terminal) {
  218. var x = 1;
  219. if (csi.params.isNotEmpty) {
  220. x = csi.params.first;
  221. }
  222. terminal.buffer.setCursorX(x - 1);
  223. }
  224. void csiCursorForwardHandler(CSI csi, Terminal terminal) {
  225. var offset = 1;
  226. if (csi.params.isNotEmpty) {
  227. offset = csi.params.first;
  228. }
  229. terminal.buffer.movePosition(offset, 0);
  230. }
  231. void csiCursorBackwardHandler(CSI csi, Terminal terminal) {
  232. var offset = 1;
  233. if (csi.params.isNotEmpty) {
  234. offset = csi.params.first;
  235. }
  236. terminal.buffer.movePosition(-offset, 0);
  237. }
  238. void csiEraseCharactersHandler(CSI csi, Terminal terminal) {
  239. var count = 1;
  240. if (csi.params.isNotEmpty) {
  241. count = csi.params.first;
  242. }
  243. terminal.buffer.eraseCharacters(count);
  244. }
  245. void csiModeHandler(CSI csi, Terminal terminal) {
  246. // terminal.ActiveBuffer().ClearSelection()
  247. return csiSetModes(csi, terminal);
  248. }
  249. void csiDeviceStatusReportHandler(CSI csi, Terminal terminal) {
  250. if (csi.params.isEmpty) return;
  251. switch (csi.params[0]) {
  252. case 5:
  253. terminal.backend?.write("\x1b[0n");
  254. break;
  255. case 6: // report cursor position
  256. terminal.backend
  257. ?.write("\x1b[${terminal.cursorX + 1};${terminal.cursorY + 1}R");
  258. break;
  259. default:
  260. terminal.debug
  261. .onError('Unknown Device Status Report identifier: ${csi.params[0]}');
  262. return;
  263. }
  264. }
  265. void csiSendDeviceAttributesHandler(CSI csi, Terminal terminal) {
  266. var response = '?1;2';
  267. if (csi.prefix == 62 /* '>' */) {
  268. response = '>0;0;0';
  269. }
  270. terminal.backend?.write('\x1b[${response}c');
  271. }
  272. void csiCursorUpHandler(CSI csi, Terminal terminal) {
  273. var distance = 1;
  274. if (csi.params.isNotEmpty) {
  275. distance = csi.params.first;
  276. }
  277. terminal.buffer.movePosition(0, -distance);
  278. }
  279. void csiCursorDownHandler(CSI csi, Terminal terminal) {
  280. var distance = 1;
  281. if (csi.params.isNotEmpty) {
  282. distance = csi.params.first;
  283. }
  284. terminal.buffer.movePosition(0, distance);
  285. }
  286. /// DECSTBM – Set Top and Bottom Margins (DEC Private)
  287. ///
  288. /// ESC [ Pn; Pn r
  289. ///
  290. /// This sequence sets the top and bottom margins to define the scrolling
  291. /// region. The first parameter is the line number of the first line in the
  292. /// scrolling region; the second parameter is the line number of the bottom line
  293. /// in the scrolling region. Default is the en tire screen (no margins). The
  294. /// minimum size of the scrolling region allowed is two lines, i.e., the top
  295. /// margin must be less than the bottom margin. The cursor is placed in the home
  296. /// position (see Origin Mode DECOM).
  297. void csiSetMarginsHandler(CSI csi, Terminal terminal) {
  298. var top = 1;
  299. var bottom = terminal.viewHeight;
  300. if (csi.params.length > 2) {
  301. return;
  302. }
  303. if (csi.params.isNotEmpty) {
  304. top = csi.params[0];
  305. if (csi.params.length > 1) {
  306. bottom = csi.params[1];
  307. }
  308. }
  309. terminal.buffer.setVerticalMargins(top - 1, bottom - 1);
  310. terminal.buffer.setPosition(0, 0);
  311. }
  312. void csiDeleteHandler(CSI csi, Terminal terminal) {
  313. var count = 1;
  314. if (csi.params.isNotEmpty) {
  315. count = csi.params.first;
  316. }
  317. if (count < 1) {
  318. count = 1;
  319. }
  320. terminal.buffer.deleteChars(count);
  321. }
  322. void csiTabClearHandler(CSI csi, Terminal terminal) {
  323. // TODO
  324. }
  325. void csiWindowManipulation(CSI csi, Terminal terminal) {
  326. // not supported
  327. }
  328. void csiCursorNextLineHandler(CSI csi, Terminal terminal) {
  329. var count = 1;
  330. if (csi.params.isNotEmpty) {
  331. count = csi.params.first;
  332. }
  333. if (count < 1) {
  334. count = 1;
  335. }
  336. terminal.buffer.moveCursorY(count);
  337. terminal.buffer.setCursorX(0);
  338. }
  339. void csiCursorPrecedingLineHandler(CSI csi, Terminal terminal) {
  340. var count = 1;
  341. if (csi.params.isNotEmpty) {
  342. count = csi.params.first;
  343. }
  344. if (count < 1) {
  345. count = 1;
  346. }
  347. terminal.buffer.moveCursorY(-count);
  348. terminal.buffer.setCursorX(0);
  349. }
  350. void csiInsertLinesHandler(CSI csi, Terminal terminal) {
  351. var count = 1;
  352. if (csi.params.isNotEmpty) {
  353. count = csi.params.first;
  354. }
  355. if (count < 1) {
  356. count = 1;
  357. }
  358. terminal.buffer.insertLines(count);
  359. }
  360. void csiDeleteLinesHandler(CSI csi, Terminal terminal) {
  361. var count = 1;
  362. if (csi.params.isNotEmpty) {
  363. count = csi.params.first;
  364. }
  365. if (count < 1) {
  366. count = 1;
  367. }
  368. terminal.buffer.deleteLines(count);
  369. }
  370. void csiScrollUpHandler(CSI csi, Terminal terminal) {
  371. var count = 1;
  372. if (csi.params.isNotEmpty) {
  373. count = csi.params.first;
  374. }
  375. if (count < 1) {
  376. count = 1;
  377. }
  378. terminal.buffer.areaScrollUp(count);
  379. }
  380. void csiScrollDownHandler(CSI csi, Terminal terminal) {
  381. var count = 1;
  382. if (csi.params.isNotEmpty) {
  383. count = csi.params.first;
  384. }
  385. if (count < 1) {
  386. count = 1;
  387. }
  388. terminal.buffer.areaScrollDown(count);
  389. }
  390. void csiInsertBlankCharactersHandler(CSI csi, Terminal terminal) {
  391. var count = 1;
  392. if (csi.params.isNotEmpty) {
  393. count = csi.params.first;
  394. }
  395. if (count < 1) {
  396. count = 1;
  397. }
  398. terminal.buffer.insertBlankCharacters(count);
  399. }