Terminal.cpp 48 KB

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