Terminal.cpp 48 KB

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