Terminal.cpp 48 KB

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