Terminal.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Terminal.h"
  8. #include <AK/Debug.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/StringView.h>
  11. #include <LibVT/Color.h>
  12. #include <LibVT/Terminal.h>
  13. #ifdef KERNEL
  14. # include <Kernel/TTY/VirtualConsole.h>
  15. #endif
  16. namespace VT {
  17. #ifndef KERNEL
  18. Terminal::Terminal(TerminalClient& client)
  19. #else
  20. Terminal::Terminal(Kernel::VirtualConsole& client)
  21. #endif
  22. : m_client(client)
  23. , m_parser(*this)
  24. {
  25. }
  26. #ifndef KERNEL
  27. void Terminal::clear()
  28. {
  29. dbgln_if(TERMINAL_DEBUG, "Clear the entire screen");
  30. for (size_t i = 0; i < rows(); ++i)
  31. active_buffer()[i].clear();
  32. set_cursor(0, 0);
  33. }
  34. void Terminal::clear_history()
  35. {
  36. dbgln_if(TERMINAL_DEBUG, "Clear history");
  37. auto previous_history_size = m_history.size();
  38. m_history.clear();
  39. m_history_start = 0;
  40. m_client.terminal_history_changed(-previous_history_size);
  41. }
  42. #endif
  43. void Terminal::alter_mode(bool should_set, Parameters params, Intermediates intermediates)
  44. {
  45. auto steady_cursor_to_blinking = [](CursorStyle style) {
  46. switch (style) {
  47. case SteadyBar:
  48. return BlinkingBar;
  49. case SteadyBlock:
  50. return BlinkingBlock;
  51. case SteadyUnderline:
  52. return BlinkingUnderline;
  53. default:
  54. return style;
  55. }
  56. };
  57. auto blinking_cursor_to_steady = [](CursorStyle style) {
  58. switch (style) {
  59. case BlinkingBar:
  60. return SteadyBar;
  61. case BlinkingBlock:
  62. return SteadyBlock;
  63. case BlinkingUnderline:
  64. return SteadyUnderline;
  65. default:
  66. return style;
  67. }
  68. };
  69. if (intermediates.size() > 0 && intermediates[0] == '?') {
  70. for (auto mode : params) {
  71. switch (mode) {
  72. case 3: {
  73. // 80/132-column mode (DECCOLM)
  74. unsigned new_columns = should_set ? 132 : 80;
  75. dbgln_if(TERMINAL_DEBUG, "Setting {}-column mode", new_columns);
  76. set_size(new_columns, rows());
  77. clear();
  78. break;
  79. }
  80. case 12:
  81. if (should_set) {
  82. // Start blinking cursor
  83. m_cursor_style = steady_cursor_to_blinking(m_cursor_style);
  84. } else {
  85. // Stop blinking cursor
  86. m_cursor_style = blinking_cursor_to_steady(m_cursor_style);
  87. }
  88. m_client.set_cursor_style(m_cursor_style);
  89. break;
  90. case 25:
  91. if (should_set) {
  92. // Show cursor
  93. m_cursor_style = m_saved_cursor_style;
  94. m_client.set_cursor_style(m_cursor_style);
  95. } else {
  96. // Hide cursor
  97. m_saved_cursor_style = m_cursor_style;
  98. m_cursor_style = None;
  99. m_client.set_cursor_style(None);
  100. }
  101. break;
  102. case 1047:
  103. #ifndef KERNEL
  104. if (should_set) {
  105. dbgln_if(TERMINAL_DEBUG, "Switching to Alternate Screen Buffer");
  106. m_use_alternate_screen_buffer = true;
  107. clear();
  108. } else {
  109. dbgln_if(TERMINAL_DEBUG, "Switching to Normal Screen Buffer");
  110. m_use_alternate_screen_buffer = false;
  111. }
  112. m_need_full_flush = true;
  113. #else
  114. dbgln("Alternate Screen Buffer is not supported");
  115. #endif
  116. break;
  117. case 1048:
  118. if (should_set)
  119. SCOSC();
  120. else
  121. SCORC();
  122. break;
  123. case 1049:
  124. #ifndef KERNEL
  125. if (should_set) {
  126. dbgln_if(TERMINAL_DEBUG, "Switching to Alternate Screen Buffer and saving state");
  127. m_normal_saved_state = m_current_state;
  128. m_use_alternate_screen_buffer = true;
  129. clear();
  130. } else {
  131. dbgln_if(TERMINAL_DEBUG, "Switching to Normal Screen Buffer and restoring state");
  132. m_current_state = m_normal_saved_state;
  133. m_use_alternate_screen_buffer = false;
  134. set_cursor(cursor_row(), cursor_column());
  135. }
  136. m_need_full_flush = true;
  137. #else
  138. dbgln("Alternate Screen Buffer is not supported");
  139. #endif
  140. break;
  141. case 2004:
  142. dbgln_if(TERMINAL_DEBUG, "Setting bracketed mode enabled={}", should_set);
  143. m_needs_bracketed_paste = should_set;
  144. break;
  145. default:
  146. dbgln("Terminal::alter_mode: Unimplemented private mode {} (should_set={})", mode, should_set);
  147. break;
  148. }
  149. }
  150. } else {
  151. for (auto mode : params) {
  152. switch (mode) {
  153. // FIXME: implement *something* for this
  154. default:
  155. dbgln("Terminal::alter_mode: Unimplemented mode {} (should_set={})", mode, should_set);
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. void Terminal::RM(Parameters params, Intermediates intermediates)
  162. {
  163. alter_mode(false, params, intermediates);
  164. }
  165. void Terminal::SM(Parameters params, Intermediates intermediates)
  166. {
  167. alter_mode(true, params, intermediates);
  168. }
  169. void Terminal::SGR(Parameters params)
  170. {
  171. if (params.is_empty()) {
  172. m_current_state.attribute.reset();
  173. return;
  174. }
  175. auto parse_color = [&]() -> Optional<Color> {
  176. if (params.size() < 2) {
  177. dbgln("Color code has no type");
  178. return {};
  179. }
  180. u32 rgb = 0;
  181. switch (params[1]) {
  182. case 5: // 8-bit
  183. if (params.size() < 3) {
  184. dbgln("8-bit color code has too few parameters");
  185. return {};
  186. }
  187. if (params[2] > 255) {
  188. dbgln("8-bit color code has out-of-bounds value");
  189. return {};
  190. }
  191. return Color::indexed(params[2]);
  192. case 2: // 24-bit
  193. if (params.size() < 5) {
  194. dbgln("24-bit color code has too few parameters");
  195. return {};
  196. }
  197. for (size_t i = 0; i < 3; ++i) {
  198. rgb <<= 8;
  199. rgb |= params[i + 2];
  200. }
  201. return Color::rgb(rgb);
  202. default:
  203. dbgln("Unknown color type {}", params[1]);
  204. return {};
  205. }
  206. };
  207. if (params[0] == 38) {
  208. m_current_state.attribute.foreground_color = parse_color().value_or(m_current_state.attribute.foreground_color);
  209. } else if (params[0] == 48) {
  210. m_current_state.attribute.background_color = parse_color().value_or(m_current_state.attribute.background_color);
  211. } else {
  212. // A single escape sequence may set multiple parameters.
  213. for (auto param : params) {
  214. switch (param) {
  215. case 0:
  216. // Reset
  217. m_current_state.attribute.reset();
  218. break;
  219. case 1:
  220. m_current_state.attribute.flags |= Attribute::Bold;
  221. break;
  222. case 3:
  223. m_current_state.attribute.flags |= Attribute::Italic;
  224. break;
  225. case 4:
  226. m_current_state.attribute.flags |= Attribute::Underline;
  227. break;
  228. case 5:
  229. m_current_state.attribute.flags |= Attribute::Blink;
  230. break;
  231. case 7:
  232. m_current_state.attribute.flags |= Attribute::Negative;
  233. break;
  234. case 22:
  235. m_current_state.attribute.flags &= ~Attribute::Bold;
  236. break;
  237. case 23:
  238. m_current_state.attribute.flags &= ~Attribute::Italic;
  239. break;
  240. case 24:
  241. m_current_state.attribute.flags &= ~Attribute::Underline;
  242. break;
  243. case 25:
  244. m_current_state.attribute.flags &= ~Attribute::Blink;
  245. break;
  246. case 27:
  247. m_current_state.attribute.flags &= ~Attribute::Negative;
  248. break;
  249. case 30:
  250. case 31:
  251. case 32:
  252. case 33:
  253. case 34:
  254. case 35:
  255. case 36:
  256. case 37:
  257. // Foreground color
  258. m_current_state.attribute.foreground_color = Color::named(static_cast<Color::ANSIColor>(param - 30));
  259. break;
  260. case 39:
  261. // reset foreground
  262. m_current_state.attribute.foreground_color = Attribute::default_foreground_color;
  263. break;
  264. case 40:
  265. case 41:
  266. case 42:
  267. case 43:
  268. case 44:
  269. case 45:
  270. case 46:
  271. case 47:
  272. // Background color
  273. m_current_state.attribute.background_color = Color::named(static_cast<Color::ANSIColor>(param - 40));
  274. break;
  275. case 49:
  276. // reset background
  277. m_current_state.attribute.background_color = Attribute::default_background_color;
  278. break;
  279. case 90:
  280. case 91:
  281. case 92:
  282. case 93:
  283. case 94:
  284. case 95:
  285. case 96:
  286. case 97:
  287. // Bright foreground color
  288. m_current_state.attribute.foreground_color = Color::named(static_cast<Color::ANSIColor>(8 + param - 90));
  289. break;
  290. case 100:
  291. case 101:
  292. case 102:
  293. case 103:
  294. case 104:
  295. case 105:
  296. case 106:
  297. case 107:
  298. // Bright background color
  299. m_current_state.attribute.background_color = Color::named(static_cast<Color::ANSIColor>(8 + param - 100));
  300. break;
  301. default:
  302. dbgln("FIXME: SGR: p: {}", param);
  303. }
  304. }
  305. }
  306. }
  307. void Terminal::SCOSC()
  308. {
  309. dbgln_if(TERMINAL_DEBUG, "Save cursor position");
  310. m_saved_cursor_position = m_current_state.cursor;
  311. }
  312. void Terminal::SCORC()
  313. {
  314. dbgln_if(TERMINAL_DEBUG, "Restore cursor position");
  315. m_current_state.cursor = m_saved_cursor_position;
  316. set_cursor(cursor_row(), cursor_column());
  317. }
  318. void Terminal::DECSC()
  319. {
  320. dbgln_if(TERMINAL_DEBUG, "Save cursor (and other state)");
  321. if (m_use_alternate_screen_buffer) {
  322. m_alternate_saved_state = m_current_state;
  323. } else {
  324. m_normal_saved_state = m_current_state;
  325. }
  326. }
  327. void Terminal::DECRC()
  328. {
  329. dbgln_if(TERMINAL_DEBUG, "Restore cursor (and other state)");
  330. if (m_use_alternate_screen_buffer) {
  331. m_current_state = m_alternate_saved_state;
  332. } else {
  333. m_current_state = m_normal_saved_state;
  334. }
  335. set_cursor(cursor_row(), cursor_column());
  336. }
  337. void Terminal::XTERM_WM(Parameters params)
  338. {
  339. if (params.size() < 1)
  340. return;
  341. switch (params[0]) {
  342. case 22: {
  343. if (params.size() > 1 && params[1] == 1) {
  344. dbgln("FIXME: we don't support icon titles");
  345. return;
  346. }
  347. dbgln_if(TERMINAL_DEBUG, "Title stack push: {}", m_current_window_title);
  348. [[maybe_unused]] auto rc = m_title_stack.try_append(move(m_current_window_title));
  349. break;
  350. }
  351. case 23: {
  352. if (params.size() > 1 && params[1] == 1)
  353. return;
  354. if (m_title_stack.is_empty()) {
  355. dbgln("Shenanigans: Tried to pop from empty title stack");
  356. return;
  357. }
  358. m_current_window_title = m_title_stack.take_last();
  359. dbgln_if(TERMINAL_DEBUG, "Title stack pop: {}", m_current_window_title);
  360. m_client.set_window_title(m_current_window_title);
  361. break;
  362. }
  363. default:
  364. dbgln("FIXME: XTERM_WM: Ps: {} (param count: {})", params[0], params.size());
  365. }
  366. }
  367. void Terminal::DECSTBM(Parameters params)
  368. {
  369. unsigned top = 1;
  370. unsigned bottom = m_rows;
  371. if (params.size() >= 1 && params[0] != 0)
  372. top = params[0];
  373. if (params.size() >= 2 && params[1] != 0)
  374. bottom = params[1];
  375. if ((bottom - top) < 2 || bottom > m_rows) {
  376. dbgln("Error: DECSTBM: scrolling region invalid: {}-{}", top, bottom);
  377. return;
  378. }
  379. m_scroll_region_top = top - 1;
  380. m_scroll_region_bottom = bottom - 1;
  381. set_cursor(0, 0);
  382. dbgln_if(TERMINAL_DEBUG, "Set scrolling region: {}-{}", m_scroll_region_top, m_scroll_region_bottom);
  383. }
  384. void Terminal::CUP(Parameters params)
  385. {
  386. // CUP – Cursor Position
  387. unsigned row = 1;
  388. unsigned col = 1;
  389. if (params.size() >= 1 && params[0] != 0)
  390. row = params[0];
  391. if (params.size() >= 2 && params[1] != 0)
  392. col = params[1];
  393. set_cursor(row - 1, col - 1);
  394. }
  395. void Terminal::HVP(Parameters params)
  396. {
  397. unsigned row = 1;
  398. unsigned col = 1;
  399. if (params.size() >= 1 && params[0] != 0)
  400. row = params[0];
  401. if (params.size() >= 2 && params[1] != 0)
  402. col = params[1];
  403. set_cursor(row - 1, col - 1);
  404. }
  405. void Terminal::CUU(Parameters params)
  406. {
  407. unsigned num = 1;
  408. if (params.size() >= 1 && params[0] != 0)
  409. num = params[0];
  410. int new_row = cursor_row() - num;
  411. if (new_row < 0)
  412. new_row = 0;
  413. set_cursor(new_row, cursor_column());
  414. }
  415. void Terminal::CUD(Parameters params)
  416. {
  417. unsigned num = 1;
  418. if (params.size() >= 1 && params[0] != 0)
  419. num = params[0];
  420. unsigned new_row = cursor_row() + num;
  421. if (new_row >= m_rows)
  422. new_row = m_rows - 1;
  423. set_cursor(new_row, cursor_column());
  424. }
  425. void Terminal::CUF(Parameters params)
  426. {
  427. unsigned num = 1;
  428. if (params.size() >= 1 && params[0] != 0)
  429. num = params[0];
  430. unsigned new_column = cursor_column() + num;
  431. if (new_column >= m_columns)
  432. new_column = m_columns - 1;
  433. set_cursor(cursor_row(), new_column);
  434. }
  435. void Terminal::CUB(Parameters params)
  436. {
  437. unsigned num = 1;
  438. if (params.size() >= 1 && params[0] != 0)
  439. num = params[0];
  440. int new_column = (int)cursor_column() - num;
  441. if (new_column < 0)
  442. new_column = 0;
  443. set_cursor(cursor_row(), new_column);
  444. }
  445. void Terminal::CNL(Parameters params)
  446. {
  447. unsigned num = 1;
  448. if (params.size() >= 1 && params[0] != 0)
  449. num = params[0];
  450. unsigned new_row = cursor_row() + num;
  451. if (new_row >= m_columns)
  452. new_row = m_columns - 1;
  453. set_cursor(new_row, 0);
  454. }
  455. void Terminal::CPL(Parameters params)
  456. {
  457. unsigned num = 1;
  458. if (params.size() >= 1 && params[0] != 0)
  459. num = params[0];
  460. int new_row = (int)cursor_row() - num;
  461. if (new_row < 0)
  462. new_row = 0;
  463. set_cursor(new_row, 0);
  464. }
  465. void Terminal::CHA(Parameters params)
  466. {
  467. unsigned new_column = 1;
  468. if (params.size() >= 1 && params[0] != 0)
  469. new_column = params[0];
  470. if (new_column > m_columns)
  471. new_column = m_columns;
  472. set_cursor(cursor_row(), new_column - 1);
  473. }
  474. void Terminal::REP(Parameters params)
  475. {
  476. unsigned count = 1;
  477. if (params.size() >= 1 && params[0] != 0)
  478. count = params[0];
  479. for (unsigned i = 0; i < count; ++i)
  480. put_character_at(m_current_state.cursor.row, m_current_state.cursor.column++, m_last_code_point);
  481. }
  482. void Terminal::VPA(Parameters params)
  483. {
  484. unsigned new_row = 1;
  485. if (params.size() >= 1 && params[0] != 0)
  486. new_row = params[0];
  487. if (new_row > m_rows)
  488. new_row = m_rows;
  489. set_cursor(new_row - 1, cursor_column());
  490. }
  491. void Terminal::VPR(Parameters params)
  492. {
  493. unsigned num = 1;
  494. if (params.size() >= 1 && params[0] != 0)
  495. num = params[0];
  496. int new_row = cursor_row() + num;
  497. if (new_row >= m_rows)
  498. new_row = m_rows - 1;
  499. set_cursor(new_row, cursor_column());
  500. }
  501. void Terminal::HPA(Parameters params)
  502. {
  503. unsigned new_column = 1;
  504. if (params.size() >= 1 && params[0] != 0)
  505. new_column = params[0];
  506. if (new_column > m_columns)
  507. new_column = m_columns;
  508. set_cursor(cursor_row(), new_column - 1);
  509. }
  510. void Terminal::HPR(Parameters params)
  511. {
  512. unsigned num = 1;
  513. if (params.size() >= 1 && params[0] != 0)
  514. num = params[0];
  515. unsigned new_column = cursor_column() + num;
  516. if (new_column >= m_columns)
  517. new_column = m_columns - 1;
  518. set_cursor(cursor_row(), new_column);
  519. }
  520. void Terminal::ECH(Parameters params)
  521. {
  522. // Erase characters (without moving cursor)
  523. unsigned num = 1;
  524. if (params.size() >= 1 && params[0] != 0)
  525. num = params[0];
  526. // Clear num characters from the right of the cursor.
  527. auto clear_end = min<unsigned>(m_columns, cursor_column() + num - 1);
  528. dbgln_if(TERMINAL_DEBUG, "Erase characters {}-{} on line {}", cursor_column(), clear_end, cursor_row());
  529. clear_in_line(cursor_row(), cursor_column(), clear_end);
  530. }
  531. void Terminal::EL(Parameters params)
  532. {
  533. unsigned mode = 0;
  534. if (params.size() >= 1)
  535. mode = params[0];
  536. switch (mode) {
  537. case 0:
  538. dbgln_if(TERMINAL_DEBUG, "Clear line {} from cursor column ({}) to the end", cursor_row(), cursor_column());
  539. clear_in_line(cursor_row(), cursor_column(), m_columns - 1);
  540. break;
  541. case 1:
  542. dbgln_if(TERMINAL_DEBUG, "Clear line {} from the start to cursor column ({})", cursor_row(), cursor_column());
  543. clear_in_line(cursor_row(), 0, cursor_column());
  544. break;
  545. case 2:
  546. dbgln_if(TERMINAL_DEBUG, "Clear line {} completely", cursor_row());
  547. clear_in_line(cursor_row(), 0, m_columns - 1);
  548. break;
  549. default:
  550. unimplemented_csi_sequence(params, {}, 'K');
  551. break;
  552. }
  553. }
  554. void Terminal::ED(Parameters params)
  555. {
  556. unsigned mode = 0;
  557. if (params.size() >= 1)
  558. mode = params[0];
  559. switch (mode) {
  560. case 0:
  561. dbgln_if(TERMINAL_DEBUG, "Clear from cursor ({},{}) to end of screen", cursor_row(), cursor_column());
  562. clear_in_line(cursor_row(), cursor_column(), m_columns - 1);
  563. for (int row = cursor_row() + 1; row < m_rows; ++row)
  564. clear_in_line(row, 0, m_columns - 1);
  565. break;
  566. case 1:
  567. dbgln_if(TERMINAL_DEBUG, "Clear from beginning of screen to cursor ({},{})", cursor_row(), cursor_column());
  568. clear_in_line(cursor_row(), 0, cursor_column());
  569. for (int row = cursor_row() - 1; row >= 0; --row)
  570. clear_in_line(row, 0, m_columns - 1);
  571. break;
  572. case 2:
  573. clear();
  574. break;
  575. case 3:
  576. clear_history();
  577. break;
  578. default:
  579. unimplemented_csi_sequence(params, {}, 'J');
  580. break;
  581. }
  582. }
  583. void Terminal::SU(Parameters params)
  584. {
  585. unsigned count = 1;
  586. if (params.size() >= 1 && params[0] != 0)
  587. count = params[0];
  588. scroll_up(count);
  589. }
  590. void Terminal::SD(Parameters params)
  591. {
  592. unsigned count = 1;
  593. if (params.size() >= 1 && params[0] != 0)
  594. count = params[0];
  595. scroll_down(count);
  596. }
  597. void Terminal::DECSCUSR(Parameters params)
  598. {
  599. unsigned style = 1;
  600. if (params.size() >= 1 && params[0] != 0)
  601. style = params[0];
  602. switch (style) {
  603. case 1:
  604. m_client.set_cursor_style(BlinkingBlock);
  605. break;
  606. case 2:
  607. m_client.set_cursor_style(SteadyBlock);
  608. break;
  609. case 3:
  610. m_client.set_cursor_style(BlinkingUnderline);
  611. break;
  612. case 4:
  613. m_client.set_cursor_style(SteadyUnderline);
  614. break;
  615. case 5:
  616. m_client.set_cursor_style(BlinkingBar);
  617. break;
  618. case 6:
  619. m_client.set_cursor_style(SteadyBar);
  620. break;
  621. default:
  622. dbgln("Unknown cursor style {}", style);
  623. }
  624. }
  625. void Terminal::IL(Parameters params)
  626. {
  627. size_t count = 1;
  628. if (params.size() >= 1 && params[0] != 0)
  629. count = params[0];
  630. if (!is_within_scroll_region(cursor_row())) {
  631. dbgln("Shenanigans! Tried to insert line outside the scroll region");
  632. return;
  633. }
  634. scroll_down(cursor_row(), m_scroll_region_bottom, count);
  635. }
  636. void Terminal::DA(Parameters)
  637. {
  638. emit_string("\033[?1;0c");
  639. }
  640. void Terminal::DL(Parameters params)
  641. {
  642. size_t count = 1;
  643. if (params.size() >= 1 && params[0] != 0)
  644. count = params[0];
  645. if (!is_within_scroll_region(cursor_row())) {
  646. dbgln("Shenanigans! Tried to delete line outside the scroll region");
  647. return;
  648. }
  649. scroll_up(cursor_row(), m_scroll_region_bottom, count);
  650. }
  651. void Terminal::DCH(Parameters params)
  652. {
  653. int num = 1;
  654. if (params.size() >= 1 && params[0] != 0)
  655. num = params[0];
  656. num = min<int>(num, columns() - cursor_column());
  657. scroll_left(cursor_row(), cursor_column(), num);
  658. }
  659. void Terminal::linefeed()
  660. {
  661. u16 new_row = cursor_row();
  662. if (cursor_row() == m_scroll_region_bottom) {
  663. scroll_up();
  664. } else {
  665. ++new_row;
  666. };
  667. // We shouldn't jump to the first column after receiving a line feed.
  668. // The TTY will take care of generating the carriage return.
  669. set_cursor(new_row, cursor_column());
  670. }
  671. void Terminal::carriage_return()
  672. {
  673. dbgln_if(TERMINAL_DEBUG, "Carriage return");
  674. set_cursor(cursor_row(), 0);
  675. }
  676. void Terminal::scroll_up(size_t count)
  677. {
  678. scroll_up(m_scroll_region_top, m_scroll_region_bottom, count);
  679. }
  680. void Terminal::scroll_down(size_t count)
  681. {
  682. scroll_down(m_scroll_region_top, m_scroll_region_bottom, count);
  683. }
  684. #ifndef KERNEL
  685. // Insert `count` blank lines at the bottom of the region. Text moves up, top lines get added to the scrollback.
  686. void Terminal::scroll_up(u16 region_top, u16 region_bottom, size_t count)
  687. {
  688. VERIFY(region_top <= region_bottom);
  689. VERIFY(region_bottom < rows());
  690. // Only the specified region should be affected.
  691. size_t region_size = region_bottom - region_top + 1;
  692. count = min(count, region_size);
  693. dbgln_if(TERMINAL_DEBUG, "Scroll up {} lines in region {}-{}", count, region_top, region_bottom);
  694. // NOTE: We have to invalidate the cursor first.
  695. invalidate_cursor();
  696. int history_delta = -count;
  697. bool should_move_to_scrollback = !m_use_alternate_screen_buffer && max_history_size() != 0;
  698. if (should_move_to_scrollback) {
  699. auto remaining_lines = max_history_size() - history_size();
  700. history_delta = (count > remaining_lines) ? remaining_lines - count : 0;
  701. for (size_t i = 0; i < count; ++i)
  702. add_line_to_history(move(active_buffer().ptr_at(region_top + i)));
  703. }
  704. // Move lines into their new place.
  705. for (u16 row = region_top; row + count <= region_bottom; ++row)
  706. swap(active_buffer().ptr_at(row), active_buffer().ptr_at(row + count));
  707. // Clear 'new' lines at the bottom.
  708. if (should_move_to_scrollback) {
  709. // Since we moved the previous lines into history, we can't just clear them.
  710. for (u16 row = region_bottom + 1 - count; row <= region_bottom; ++row)
  711. active_buffer().ptr_at(row) = make<Line>(columns());
  712. } else {
  713. // The new lines haven't been moved and we don't want to leak memory.
  714. for (u16 row = region_bottom + 1 - count; row <= region_bottom; ++row)
  715. active_buffer()[row].clear();
  716. }
  717. // Set dirty flag on swapped lines.
  718. // The other lines have implicitly been set dirty by being cleared.
  719. for (u16 row = region_top; row <= region_bottom - count; ++row)
  720. active_buffer()[row].set_dirty(true);
  721. m_client.terminal_history_changed(history_delta);
  722. }
  723. // Insert `count` blank lines at the top of the region. Text moves down. Does not affect the scrollback buffer.
  724. void Terminal::scroll_down(u16 region_top, u16 region_bottom, size_t count)
  725. {
  726. VERIFY(region_top <= region_bottom);
  727. VERIFY(region_bottom < rows());
  728. // Only the specified region should be affected.
  729. size_t region_size = region_bottom - region_top + 1;
  730. count = min(count, region_size);
  731. dbgln_if(TERMINAL_DEBUG, "Scroll down {} lines in region {}-{}", count, region_top, region_bottom);
  732. // NOTE: We have to invalidate the cursor first.
  733. invalidate_cursor();
  734. // Move lines into their new place.
  735. for (int row = region_bottom; row >= static_cast<int>(region_top + count); --row)
  736. swap(active_buffer().ptr_at(row), active_buffer().ptr_at(row - count));
  737. // Clear the 'new' lines at the top.
  738. for (u16 row = region_top; row < region_top + count; ++row)
  739. active_buffer()[row].clear();
  740. // Set dirty flag on swapped lines.
  741. // The other lines have implicitly been set dirty by being cleared.
  742. for (u16 row = region_top + count; row <= region_bottom; ++row)
  743. active_buffer()[row].set_dirty(true);
  744. }
  745. // Insert `count` blank cells at the end of the line. Text moves left.
  746. void Terminal::scroll_left(u16 row, u16 column, size_t count)
  747. {
  748. VERIFY(row < rows());
  749. VERIFY(column < columns());
  750. count = min<size_t>(count, columns() - column);
  751. dbgln_if(TERMINAL_DEBUG, "Scroll left {} columns from line {} column {}", count, row, column);
  752. auto& line = active_buffer()[row];
  753. for (size_t i = column; i < columns() - count; ++i)
  754. swap(line.cell_at(i), line.cell_at(i + count));
  755. clear_in_line(row, columns() - count, columns() - 1);
  756. line.set_dirty(true);
  757. }
  758. // Insert `count` blank cells after `row`. Text moves right.
  759. void Terminal::scroll_right(u16 row, u16 column, size_t count)
  760. {
  761. VERIFY(row < rows());
  762. VERIFY(column < columns());
  763. count = min<size_t>(count, columns() - column);
  764. dbgln_if(TERMINAL_DEBUG, "Scroll right {} columns from line {} column {}", count, row, column);
  765. auto& line = active_buffer()[row];
  766. for (int i = columns() - 1; i >= static_cast<int>(column + count); --i)
  767. swap(line.cell_at(i), line.cell_at(i - count));
  768. clear_in_line(row, column, column + count - 1);
  769. line.set_dirty(true);
  770. }
  771. void Terminal::put_character_at(unsigned row, unsigned column, u32 code_point)
  772. {
  773. VERIFY(row < rows());
  774. VERIFY(column < columns());
  775. auto& line = active_buffer()[row];
  776. line.set_code_point(column, code_point);
  777. line.attribute_at(column) = m_current_state.attribute;
  778. line.attribute_at(column).flags |= Attribute::Touched;
  779. line.set_dirty(true);
  780. m_last_code_point = code_point;
  781. }
  782. void Terminal::clear_in_line(u16 row, u16 first_column, u16 last_column)
  783. {
  784. VERIFY(row < rows());
  785. active_buffer()[row].clear_range(first_column, last_column);
  786. }
  787. #endif
  788. void Terminal::set_cursor(unsigned a_row, unsigned a_column, bool skip_debug)
  789. {
  790. unsigned row = min(a_row, m_rows - 1u);
  791. unsigned column = min(a_column, m_columns - 1u);
  792. if (row == cursor_row() && column == cursor_column())
  793. return;
  794. VERIFY(row < rows());
  795. VERIFY(column < columns());
  796. invalidate_cursor();
  797. m_current_state.cursor.row = row;
  798. m_current_state.cursor.column = column;
  799. m_stomp = false;
  800. invalidate_cursor();
  801. if (!skip_debug)
  802. dbgln_if(TERMINAL_DEBUG, "Set cursor position: {},{}", cursor_row(), cursor_column());
  803. }
  804. void Terminal::NEL()
  805. {
  806. if (cursor_row() == m_scroll_region_bottom)
  807. scroll_up();
  808. else
  809. set_cursor(cursor_row() + 1, 0);
  810. }
  811. void Terminal::IND()
  812. {
  813. // Not equivalent to CUD: if we are at the bottom margin, we have to scroll up.
  814. if (cursor_row() == m_scroll_region_bottom)
  815. scroll_up();
  816. else
  817. set_cursor(cursor_row() + 1, cursor_column());
  818. }
  819. void Terminal::RI()
  820. {
  821. // Not equivalent to CUU : if we at the top margin , we have to scroll down.
  822. if (cursor_row() == m_scroll_region_top)
  823. scroll_down();
  824. else
  825. set_cursor(cursor_row() - 1, cursor_column());
  826. }
  827. void Terminal::DECFI()
  828. {
  829. if (cursor_column() == columns() - 1)
  830. scroll_left(cursor_row(), 0, 1);
  831. else
  832. set_cursor(cursor_row(), cursor_column() + 1);
  833. }
  834. void Terminal::DECBI()
  835. {
  836. if (cursor_column() == 0)
  837. scroll_right(cursor_row(), 0, 1);
  838. else
  839. set_cursor(cursor_row(), cursor_column() - 1);
  840. }
  841. void Terminal::DECIC(Parameters params)
  842. {
  843. unsigned num = 1;
  844. if (params.size() >= 1 && params[0] != 0)
  845. num = params[0];
  846. num = min<unsigned>(num, columns() - cursor_column());
  847. for (unsigned row = cursor_row(); row <= m_scroll_region_bottom; ++row)
  848. scroll_right(row, cursor_column(), num);
  849. }
  850. void Terminal::DECDC(Parameters params)
  851. {
  852. unsigned num = 1;
  853. if (params.size() >= 1 && params[0] != 0)
  854. num = params[0];
  855. num = min<unsigned>(num, columns() - cursor_column());
  856. for (unsigned row = cursor_row(); row <= m_scroll_region_bottom; ++row)
  857. scroll_left(row, cursor_column(), num);
  858. }
  859. void Terminal::DSR(Parameters params)
  860. {
  861. if (params.size() == 1 && params[0] == 5) {
  862. // Device status
  863. emit_string("\033[0n"); // Terminal status OK!
  864. } else if (params.size() == 1 && params[0] == 6) {
  865. // Cursor position query
  866. emit_string(String::formatted("\e[{};{}R", cursor_row() + 1, cursor_column() + 1));
  867. } else {
  868. dbgln("Unknown DSR");
  869. }
  870. }
  871. void Terminal::ICH(Parameters params)
  872. {
  873. unsigned num = 1;
  874. if (params.size() >= 1 && params[0] != 0)
  875. num = params[0];
  876. num = min<unsigned>(num, columns() - cursor_column());
  877. scroll_right(cursor_row(), cursor_column(), num);
  878. }
  879. void Terminal::on_input(u8 byte)
  880. {
  881. m_parser.on_input(byte);
  882. }
  883. void Terminal::emit_code_point(u32 code_point)
  884. {
  885. auto new_column = cursor_column() + 1;
  886. if (new_column < columns()) {
  887. put_character_at(cursor_row(), cursor_column(), code_point);
  888. set_cursor(cursor_row(), new_column, true);
  889. return;
  890. }
  891. if (m_stomp) {
  892. m_stomp = false;
  893. carriage_return();
  894. linefeed();
  895. put_character_at(cursor_row(), cursor_column(), code_point);
  896. set_cursor(cursor_row(), 1);
  897. } else {
  898. // Curious: We wait once on the right-hand side
  899. m_stomp = true;
  900. put_character_at(cursor_row(), cursor_column(), code_point);
  901. }
  902. }
  903. void Terminal::execute_control_code(u8 code)
  904. {
  905. switch (code) {
  906. case '\a':
  907. m_client.beep();
  908. return;
  909. case '\b':
  910. if (cursor_column()) {
  911. set_cursor(cursor_row(), cursor_column() - 1);
  912. return;
  913. }
  914. return;
  915. case '\t': {
  916. for (unsigned i = cursor_column() + 1; i < columns(); ++i) {
  917. if (m_horizontal_tabs[i]) {
  918. set_cursor(cursor_row(), i);
  919. return;
  920. }
  921. }
  922. return;
  923. }
  924. case '\n':
  925. case '\v':
  926. case '\f':
  927. linefeed();
  928. return;
  929. case '\r':
  930. carriage_return();
  931. return;
  932. default:
  933. unimplemented_control_code(code);
  934. }
  935. }
  936. void Terminal::execute_escape_sequence(Intermediates intermediates, bool ignore, u8 last_byte)
  937. {
  938. // FIXME: Handle it somehow?
  939. if (ignore)
  940. dbgln("Escape sequence has its ignore flag set.");
  941. if (intermediates.size() == 0) {
  942. switch (last_byte) {
  943. case 'D':
  944. IND();
  945. return;
  946. case 'E':
  947. NEL();
  948. return;
  949. case 'M':
  950. RI();
  951. return;
  952. case '\\':
  953. // ST (string terminator) -- do nothing
  954. return;
  955. case '6':
  956. DECBI();
  957. return;
  958. case '7':
  959. DECSC();
  960. return;
  961. case '8':
  962. DECRC();
  963. return;
  964. case '9':
  965. DECFI();
  966. return;
  967. }
  968. } else if (intermediates[0] == '#') {
  969. switch (last_byte) {
  970. case '8':
  971. // Confidence Test - Fill screen with E's
  972. for (size_t row = 0; row < m_rows; ++row) {
  973. for (size_t column = 0; column < m_columns; ++column) {
  974. put_character_at(row, column, 'E');
  975. }
  976. }
  977. return;
  978. }
  979. }
  980. unimplemented_escape_sequence(intermediates, last_byte);
  981. }
  982. void Terminal::execute_csi_sequence(Parameters parameters, Intermediates intermediates, bool ignore, u8 last_byte)
  983. {
  984. // FIXME: Handle it somehow?
  985. if (ignore)
  986. dbgln("CSI sequence has its ignore flag set.");
  987. switch (last_byte) {
  988. case '@':
  989. ICH(parameters);
  990. break;
  991. case 'A':
  992. CUU(parameters);
  993. break;
  994. case 'B':
  995. CUD(parameters);
  996. break;
  997. case 'C':
  998. CUF(parameters);
  999. break;
  1000. case 'D':
  1001. CUB(parameters);
  1002. break;
  1003. case 'E':
  1004. CNL(parameters);
  1005. break;
  1006. case 'F':
  1007. CPL(parameters);
  1008. break;
  1009. case 'G':
  1010. CHA(parameters);
  1011. break;
  1012. case 'H':
  1013. CUP(parameters);
  1014. break;
  1015. case 'J':
  1016. ED(parameters);
  1017. break;
  1018. case 'K':
  1019. EL(parameters);
  1020. break;
  1021. case 'L':
  1022. IL(parameters);
  1023. break;
  1024. case 'M':
  1025. DL(parameters);
  1026. break;
  1027. case 'P':
  1028. DCH(parameters);
  1029. break;
  1030. case 'S':
  1031. SU(parameters);
  1032. break;
  1033. case 'T':
  1034. SD(parameters);
  1035. break;
  1036. case 'X':
  1037. ECH(parameters);
  1038. break;
  1039. case '`':
  1040. HPA(parameters);
  1041. break;
  1042. case 'a':
  1043. HPR(parameters);
  1044. break;
  1045. case 'b':
  1046. REP(parameters);
  1047. break;
  1048. case 'c':
  1049. DA(parameters);
  1050. break;
  1051. case 'd':
  1052. VPA(parameters);
  1053. break;
  1054. case 'e':
  1055. VPR(parameters);
  1056. break;
  1057. case 'f':
  1058. HVP(parameters);
  1059. break;
  1060. case 'h':
  1061. SM(parameters, intermediates);
  1062. break;
  1063. case 'l':
  1064. RM(parameters, intermediates);
  1065. break;
  1066. case 'm':
  1067. SGR(parameters);
  1068. break;
  1069. case 'n':
  1070. DSR(parameters);
  1071. break;
  1072. case 'q':
  1073. if (intermediates.size() >= 1 && intermediates[0] == ' ')
  1074. DECSCUSR(parameters);
  1075. else
  1076. unimplemented_csi_sequence(parameters, intermediates, last_byte);
  1077. break;
  1078. case 'r':
  1079. DECSTBM(parameters);
  1080. break;
  1081. case 's':
  1082. SCOSC();
  1083. break;
  1084. case 't':
  1085. XTERM_WM(parameters);
  1086. break;
  1087. case 'u':
  1088. SCORC();
  1089. break;
  1090. case '}':
  1091. if (intermediates.size() >= 1 && intermediates[0] == '\'')
  1092. DECIC(parameters);
  1093. else
  1094. unimplemented_csi_sequence(parameters, intermediates, last_byte);
  1095. break;
  1096. case '~':
  1097. if (intermediates.size() >= 1 && intermediates[0] == '\'')
  1098. DECDC(parameters);
  1099. else
  1100. unimplemented_csi_sequence(parameters, intermediates, last_byte);
  1101. break;
  1102. default:
  1103. unimplemented_csi_sequence(parameters, intermediates, last_byte);
  1104. }
  1105. }
  1106. void Terminal::execute_osc_sequence(OscParameters parameters, u8 last_byte)
  1107. {
  1108. auto stringview_ify = [&](size_t param_idx) {
  1109. return StringView(parameters[param_idx]);
  1110. };
  1111. if (parameters.size() == 0 || parameters[0].is_empty()) {
  1112. unimplemented_osc_sequence(parameters, last_byte);
  1113. return;
  1114. }
  1115. auto command_number = stringview_ify(0).to_uint();
  1116. if (!command_number.has_value()) {
  1117. unimplemented_osc_sequence(parameters, last_byte);
  1118. return;
  1119. }
  1120. switch (command_number.value()) {
  1121. case 0:
  1122. case 1:
  1123. case 2:
  1124. if (parameters.size() < 2) {
  1125. dbgln("Attempted to set window title without any parameters");
  1126. } else {
  1127. // FIXME: the split breaks titles containing semicolons.
  1128. // Should we expose the raw OSC string from the parser? Or join by semicolon?
  1129. m_current_window_title = stringview_ify(1).to_string();
  1130. m_client.set_window_title(m_current_window_title);
  1131. }
  1132. break;
  1133. case 8:
  1134. #ifndef KERNEL
  1135. if (parameters.size() < 3) {
  1136. dbgln("Attempted to set href but gave too few parameters");
  1137. } else if (parameters[1].is_empty() && parameters[2].is_empty()) {
  1138. // Clear hyperlink
  1139. m_current_state.attribute.href = String();
  1140. m_current_state.attribute.href_id = String();
  1141. } else {
  1142. m_current_state.attribute.href = stringview_ify(2);
  1143. // FIXME: Respect the provided ID
  1144. m_current_state.attribute.href_id = String::number(m_next_href_id++);
  1145. }
  1146. #endif
  1147. break;
  1148. case 9:
  1149. if (parameters.size() < 2)
  1150. dbgln("Atttempted to set window progress but gave too few parameters");
  1151. else if (parameters.size() == 2)
  1152. m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), 0);
  1153. else
  1154. m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), stringview_ify(2).to_int().value_or(0));
  1155. break;
  1156. default:
  1157. unimplemented_osc_sequence(parameters, last_byte);
  1158. }
  1159. }
  1160. void Terminal::dcs_hook(Parameters, Intermediates, bool, u8)
  1161. {
  1162. dbgln("Received DCS parameters, but we don't support it yet");
  1163. }
  1164. void Terminal::receive_dcs_char(u8 byte)
  1165. {
  1166. dbgln_if(TERMINAL_DEBUG, "DCS string character {:c}", byte);
  1167. }
  1168. void Terminal::execute_dcs_sequence()
  1169. {
  1170. }
  1171. void Terminal::inject_string(const StringView& str)
  1172. {
  1173. for (size_t i = 0; i < str.length(); ++i)
  1174. on_input(str[i]);
  1175. }
  1176. void Terminal::emit_string(const StringView& string)
  1177. {
  1178. m_client.emit((const u8*)string.characters_without_null_termination(), string.length());
  1179. }
  1180. void Terminal::handle_key_press(KeyCode key, u32 code_point, u8 flags)
  1181. {
  1182. bool ctrl = flags & Mod_Ctrl;
  1183. bool alt = flags & Mod_Alt;
  1184. bool shift = flags & Mod_Shift;
  1185. unsigned modifier_mask = int(shift) + (int(alt) << 1) + (int(ctrl) << 2);
  1186. auto emit_final_with_modifier = [this, modifier_mask](char final) {
  1187. if (modifier_mask)
  1188. emit_string(String::formatted("\e[1;{}{:c}", modifier_mask + 1, final));
  1189. else
  1190. emit_string(String::formatted("\e[{:c}", final));
  1191. };
  1192. auto emit_tilde_with_modifier = [this, modifier_mask](unsigned num) {
  1193. if (modifier_mask)
  1194. emit_string(String::formatted("\e[{};{}~", num, modifier_mask + 1));
  1195. else
  1196. emit_string(String::formatted("\e[{}~", num));
  1197. };
  1198. switch (key) {
  1199. case KeyCode::Key_Up:
  1200. emit_final_with_modifier('A');
  1201. return;
  1202. case KeyCode::Key_Down:
  1203. emit_final_with_modifier('B');
  1204. return;
  1205. case KeyCode::Key_Right:
  1206. emit_final_with_modifier('C');
  1207. return;
  1208. case KeyCode::Key_Left:
  1209. emit_final_with_modifier('D');
  1210. return;
  1211. case KeyCode::Key_Insert:
  1212. emit_tilde_with_modifier(2);
  1213. return;
  1214. case KeyCode::Key_Delete:
  1215. emit_tilde_with_modifier(3);
  1216. return;
  1217. case KeyCode::Key_Home:
  1218. emit_final_with_modifier('H');
  1219. return;
  1220. case KeyCode::Key_End:
  1221. emit_final_with_modifier('F');
  1222. return;
  1223. case KeyCode::Key_PageUp:
  1224. emit_tilde_with_modifier(5);
  1225. return;
  1226. case KeyCode::Key_PageDown:
  1227. emit_tilde_with_modifier(6);
  1228. return;
  1229. case KeyCode::Key_Return:
  1230. // The standard says that CR should be generated by the return key.
  1231. // The TTY will take care of translating it to CR LF for the terminal.
  1232. emit_string("\r");
  1233. return;
  1234. default:
  1235. break;
  1236. }
  1237. if (!code_point) {
  1238. // Probably a modifier being pressed.
  1239. return;
  1240. }
  1241. if (shift && key == KeyCode::Key_Tab) {
  1242. emit_string("\033[Z");
  1243. return;
  1244. }
  1245. // Key event was not one of the above special cases,
  1246. // attempt to treat it as a character...
  1247. if (ctrl) {
  1248. if (code_point >= 'a' && code_point <= 'z') {
  1249. code_point = code_point - 'a' + 1;
  1250. } else if (code_point == '\\') {
  1251. code_point = 0x1c;
  1252. }
  1253. }
  1254. // Alt modifier sends escape prefix.
  1255. if (alt)
  1256. emit_string("\033");
  1257. StringBuilder sb;
  1258. sb.append_code_point(code_point);
  1259. emit_string(sb.to_string());
  1260. }
  1261. void Terminal::unimplemented_control_code(u8 code)
  1262. {
  1263. dbgln("Unimplemented control code {:02x}", code);
  1264. }
  1265. void Terminal::unimplemented_escape_sequence(Intermediates intermediates, u8 last_byte)
  1266. {
  1267. StringBuilder builder;
  1268. builder.appendff("Unimplemented escape sequence {:c}", last_byte);
  1269. if (!intermediates.is_empty()) {
  1270. builder.append(", intermediates: ");
  1271. for (size_t i = 0; i < intermediates.size(); ++i)
  1272. builder.append((char)intermediates[i]);
  1273. }
  1274. dbgln("{}", builder.string_view());
  1275. }
  1276. void Terminal::unimplemented_csi_sequence(Parameters parameters, Intermediates intermediates, u8 last_byte)
  1277. {
  1278. StringBuilder builder;
  1279. builder.appendff("Unimplemented CSI sequence: {:c}", last_byte);
  1280. if (!parameters.is_empty()) {
  1281. builder.append(", parameters: [");
  1282. for (size_t i = 0; i < parameters.size(); ++i)
  1283. builder.appendff("{}{}", (i == 0) ? "" : ", ", parameters[i]);
  1284. builder.append("]");
  1285. }
  1286. if (!intermediates.is_empty()) {
  1287. builder.append(", intermediates:");
  1288. for (size_t i = 0; i < intermediates.size(); ++i)
  1289. builder.append((char)intermediates[i]);
  1290. }
  1291. dbgln("{}", builder.string_view());
  1292. }
  1293. void Terminal::unimplemented_osc_sequence(OscParameters parameters, u8 last_byte)
  1294. {
  1295. StringBuilder builder;
  1296. builder.appendff("Unimplemented OSC sequence parameters: (bel_terminated={}) [ ", last_byte == '\a');
  1297. bool first = true;
  1298. for (auto parameter : parameters) {
  1299. if (!first)
  1300. builder.append(", ");
  1301. builder.append("[");
  1302. for (auto character : parameter)
  1303. builder.append((char)character);
  1304. builder.append("]");
  1305. first = false;
  1306. }
  1307. builder.append(" ]");
  1308. dbgln("{}", builder.string_view());
  1309. }
  1310. #ifndef KERNEL
  1311. void Terminal::set_size(u16 columns, u16 rows)
  1312. {
  1313. if (!columns)
  1314. columns = 1;
  1315. if (!rows)
  1316. rows = 1;
  1317. if (columns == m_columns && rows == m_rows)
  1318. return;
  1319. if (rows > m_rows) {
  1320. while (m_normal_screen_buffer.size() < rows)
  1321. m_normal_screen_buffer.append(make<Line>(columns));
  1322. while (m_alternate_screen_buffer.size() < rows)
  1323. m_alternate_screen_buffer.append(make<Line>(columns));
  1324. } else {
  1325. m_normal_screen_buffer.shrink(rows);
  1326. m_alternate_screen_buffer.shrink(rows);
  1327. }
  1328. for (int i = 0; i < rows; ++i) {
  1329. m_normal_screen_buffer[i].set_length(columns);
  1330. m_alternate_screen_buffer[i].set_length(columns);
  1331. }
  1332. m_columns = columns;
  1333. m_rows = rows;
  1334. m_scroll_region_top = 0;
  1335. m_scroll_region_bottom = rows - 1;
  1336. m_current_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1337. m_normal_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1338. m_alternate_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1339. m_saved_cursor_position.clamp(m_rows - 1, m_columns - 1);
  1340. m_horizontal_tabs.resize(columns);
  1341. for (unsigned i = 0; i < columns; ++i)
  1342. m_horizontal_tabs[i] = (i % 8) == 0;
  1343. // Rightmost column is always last tab on line.
  1344. m_horizontal_tabs[columns - 1] = 1;
  1345. m_client.terminal_did_resize(m_columns, m_rows);
  1346. dbgln_if(TERMINAL_DEBUG, "Set terminal size: {}x{}", m_rows, m_columns);
  1347. }
  1348. #endif
  1349. #ifndef KERNEL
  1350. void Terminal::invalidate_cursor()
  1351. {
  1352. active_buffer()[cursor_row()].set_dirty(true);
  1353. }
  1354. Attribute Terminal::attribute_at(const Position& position) const
  1355. {
  1356. if (!position.is_valid())
  1357. return {};
  1358. if (position.row() >= static_cast<int>(line_count()))
  1359. return {};
  1360. auto& line = this->line(position.row());
  1361. if (static_cast<size_t>(position.column()) >= line.length())
  1362. return {};
  1363. return line.attribute_at(position.column());
  1364. }
  1365. #endif
  1366. }