csi.dart 11 KB

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