Terminal.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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::DSR(Parameters params)
  881. {
  882. if (params.size() == 1 && params[0] == 5) {
  883. // Device status
  884. emit_string("\033[0n"); // Terminal status OK!
  885. } else if (params.size() == 1 && params[0] == 6) {
  886. // Cursor position query
  887. emit_string(String::formatted("\e[{};{}R", cursor_row() + 1, cursor_column() + 1));
  888. } else {
  889. dbgln("Unknown DSR");
  890. }
  891. }
  892. void Terminal::ICH(Parameters params)
  893. {
  894. unsigned num = 1;
  895. if (params.size() >= 1 && params[0] != 0)
  896. num = params[0];
  897. num = min<unsigned>(num, columns() - cursor_column());
  898. scroll_right(cursor_row(), cursor_column(), num);
  899. }
  900. void Terminal::on_input(u8 byte)
  901. {
  902. m_parser.on_input(byte);
  903. }
  904. void Terminal::emit_code_point(u32 code_point)
  905. {
  906. auto new_column = cursor_column() + 1;
  907. if (new_column < columns()) {
  908. put_character_at(cursor_row(), cursor_column(), code_point);
  909. set_cursor(cursor_row(), new_column, true);
  910. return;
  911. }
  912. if (m_stomp) {
  913. m_stomp = false;
  914. TemporaryChange change { m_controls_are_logically_generated, true };
  915. carriage_return();
  916. linefeed();
  917. put_character_at(cursor_row(), cursor_column(), code_point);
  918. set_cursor(cursor_row(), 1);
  919. } else {
  920. // Curious: We wait once on the right-hand side
  921. m_stomp = true;
  922. put_character_at(cursor_row(), cursor_column(), code_point);
  923. }
  924. }
  925. void Terminal::execute_control_code(u8 code)
  926. {
  927. ArmedScopeGuard clear_position_before_cr {
  928. [&] {
  929. m_column_before_carriage_return.clear();
  930. }
  931. };
  932. switch (code) {
  933. case '\a':
  934. m_client.beep();
  935. return;
  936. case '\b':
  937. if (cursor_column()) {
  938. set_cursor(cursor_row(), cursor_column() - 1);
  939. return;
  940. }
  941. return;
  942. case '\t': {
  943. for (unsigned i = cursor_column() + 1; i < columns(); ++i) {
  944. if (m_horizontal_tabs[i]) {
  945. set_cursor(cursor_row(), i);
  946. return;
  947. }
  948. }
  949. return;
  950. }
  951. case '\n':
  952. case '\v':
  953. case '\f':
  954. if (m_column_before_carriage_return == m_columns - 1)
  955. m_column_before_carriage_return = m_columns;
  956. linefeed();
  957. return;
  958. case '\r':
  959. carriage_return();
  960. clear_position_before_cr.disarm();
  961. return;
  962. default:
  963. unimplemented_control_code(code);
  964. }
  965. }
  966. void Terminal::execute_escape_sequence(Intermediates intermediates, bool ignore, u8 last_byte)
  967. {
  968. // FIXME: Handle it somehow?
  969. if (ignore)
  970. dbgln("Escape sequence has its ignore flag set.");
  971. if (intermediates.size() == 0) {
  972. switch (last_byte) {
  973. case 'D':
  974. IND();
  975. return;
  976. case 'E':
  977. NEL();
  978. return;
  979. case 'M':
  980. RI();
  981. return;
  982. case '\\':
  983. // ST (string terminator) -- do nothing
  984. return;
  985. case '6':
  986. DECBI();
  987. return;
  988. case '7':
  989. DECSC();
  990. return;
  991. case '8':
  992. DECRC();
  993. return;
  994. case '9':
  995. DECFI();
  996. return;
  997. }
  998. } else if (intermediates[0] == '#') {
  999. switch (last_byte) {
  1000. case '8':
  1001. // Confidence Test - Fill screen with E's
  1002. for (size_t row = 0; row < m_rows; ++row) {
  1003. for (size_t column = 0; column < m_columns; ++column) {
  1004. put_character_at(row, column, 'E');
  1005. }
  1006. }
  1007. return;
  1008. }
  1009. }
  1010. unimplemented_escape_sequence(intermediates, last_byte);
  1011. }
  1012. void Terminal::execute_csi_sequence(Parameters parameters, Intermediates intermediates, bool ignore, u8 last_byte)
  1013. {
  1014. // FIXME: Handle it somehow?
  1015. if (ignore)
  1016. dbgln("CSI sequence has its ignore flag set.");
  1017. if (intermediates.is_empty()) {
  1018. switch (last_byte) {
  1019. case '@':
  1020. return ICH(parameters);
  1021. case 'A':
  1022. return CUU(parameters);
  1023. case 'B':
  1024. return CUD(parameters);
  1025. case 'C':
  1026. return CUF(parameters);
  1027. case 'D':
  1028. return CUB(parameters);
  1029. case 'E':
  1030. return CNL(parameters);
  1031. case 'F':
  1032. return CPL(parameters);
  1033. case 'G':
  1034. return CHA(parameters);
  1035. case 'H':
  1036. return CUP(parameters);
  1037. case 'J':
  1038. return ED(parameters);
  1039. case 'K':
  1040. return EL(parameters);
  1041. case 'L':
  1042. return IL(parameters);
  1043. case 'M':
  1044. return DL(parameters);
  1045. case 'P':
  1046. return DCH(parameters);
  1047. case 'S':
  1048. return SU(parameters);
  1049. case 'T':
  1050. return SD(parameters);
  1051. case 'X':
  1052. return ECH(parameters);
  1053. case '`':
  1054. return HPA(parameters);
  1055. case 'a':
  1056. return HPR(parameters);
  1057. case 'b':
  1058. return REP(parameters);
  1059. case 'c':
  1060. return DA(parameters);
  1061. case 'd':
  1062. return VPA(parameters);
  1063. case 'e':
  1064. return VPR(parameters);
  1065. case 'f':
  1066. return HVP(parameters);
  1067. case 'h':
  1068. return SM(parameters);
  1069. case 'l':
  1070. return RM(parameters);
  1071. case 'm':
  1072. return SGR(parameters);
  1073. case 'n':
  1074. return DSR(parameters);
  1075. case 'r':
  1076. return DECSTBM(parameters);
  1077. case 's':
  1078. return SCOSC();
  1079. case 't':
  1080. return XTERM_WM(parameters);
  1081. case 'u':
  1082. return SCORC();
  1083. }
  1084. } else if (intermediates.size() == 1 && intermediates[0] == '?') {
  1085. switch (last_byte) {
  1086. case 'h':
  1087. return DECSET(parameters);
  1088. case 'l':
  1089. return DECRST(parameters);
  1090. }
  1091. } else if (intermediates.size() == 1 && intermediates[0] == '\'') {
  1092. switch (last_byte) {
  1093. case '}':
  1094. return DECIC(parameters);
  1095. case '~':
  1096. return DECDC(parameters);
  1097. }
  1098. } else if (intermediates.size() == 1 && intermediates[0] == ' ') {
  1099. switch (last_byte) {
  1100. case 'q':
  1101. return DECSCUSR(parameters);
  1102. }
  1103. }
  1104. unimplemented_csi_sequence(parameters, intermediates, last_byte);
  1105. }
  1106. void Terminal::execute_osc_sequence(OscParameters parameters, u8 last_byte)
  1107. {
  1108. auto stringview_ify = [&](size_t param_idx) {
  1109. return StringView(parameters[param_idx]);
  1110. };
  1111. if (parameters.size() == 0 || parameters[0].is_empty()) {
  1112. unimplemented_osc_sequence(parameters, last_byte);
  1113. return;
  1114. }
  1115. auto command_number = stringview_ify(0).to_uint();
  1116. if (!command_number.has_value()) {
  1117. unimplemented_osc_sequence(parameters, last_byte);
  1118. return;
  1119. }
  1120. switch (command_number.value()) {
  1121. case 0:
  1122. case 1:
  1123. case 2:
  1124. if (parameters.size() < 2) {
  1125. dbgln("Attempted to set window title without any parameters");
  1126. } else {
  1127. // FIXME: the split breaks titles containing semicolons.
  1128. // Should we expose the raw OSC string from the parser? Or join by semicolon?
  1129. m_current_window_title = stringview_ify(1).to_string();
  1130. m_client.set_window_title(m_current_window_title);
  1131. }
  1132. break;
  1133. case 8:
  1134. #ifndef KERNEL
  1135. if (parameters.size() < 3) {
  1136. dbgln("Attempted to set href but gave too few parameters");
  1137. } else if (parameters[1].is_empty() && parameters[2].is_empty()) {
  1138. // Clear hyperlink
  1139. m_current_state.attribute.href = String();
  1140. m_current_state.attribute.href_id = String();
  1141. } else {
  1142. m_current_state.attribute.href = stringview_ify(2);
  1143. // FIXME: Respect the provided ID
  1144. m_current_state.attribute.href_id = String::number(m_next_href_id++);
  1145. }
  1146. #endif
  1147. break;
  1148. case 9:
  1149. if (parameters.size() < 2)
  1150. dbgln("Atttempted to set window progress but gave too few parameters");
  1151. else if (parameters.size() == 2)
  1152. m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), 0);
  1153. else
  1154. m_client.set_window_progress(stringview_ify(1).to_int().value_or(-1), stringview_ify(2).to_int().value_or(0));
  1155. break;
  1156. default:
  1157. unimplemented_osc_sequence(parameters, last_byte);
  1158. }
  1159. }
  1160. void Terminal::dcs_hook(Parameters, Intermediates, bool, u8)
  1161. {
  1162. dbgln("Received DCS parameters, but we don't support it yet");
  1163. }
  1164. void Terminal::receive_dcs_char(u8 byte)
  1165. {
  1166. dbgln_if(TERMINAL_DEBUG, "DCS string character {:c}", byte);
  1167. }
  1168. void Terminal::execute_dcs_sequence()
  1169. {
  1170. }
  1171. void Terminal::inject_string(const StringView& str)
  1172. {
  1173. for (size_t i = 0; i < str.length(); ++i)
  1174. on_input(str[i]);
  1175. }
  1176. void Terminal::emit_string(const StringView& string)
  1177. {
  1178. m_client.emit((const u8*)string.characters_without_null_termination(), string.length());
  1179. }
  1180. void Terminal::handle_key_press(KeyCode key, u32 code_point, u8 flags)
  1181. {
  1182. bool ctrl = flags & Mod_Ctrl;
  1183. bool alt = flags & Mod_Alt;
  1184. bool shift = flags & Mod_Shift;
  1185. unsigned modifier_mask = int(shift) + (int(alt) << 1) + (int(ctrl) << 2);
  1186. auto emit_final_with_modifier = [this, modifier_mask](char final) {
  1187. if (modifier_mask)
  1188. emit_string(String::formatted("\e[1;{}{:c}", modifier_mask + 1, final));
  1189. else
  1190. emit_string(String::formatted("\e[{:c}", final));
  1191. };
  1192. auto emit_tilde_with_modifier = [this, modifier_mask](unsigned num) {
  1193. if (modifier_mask)
  1194. emit_string(String::formatted("\e[{};{}~", num, modifier_mask + 1));
  1195. else
  1196. emit_string(String::formatted("\e[{}~", num));
  1197. };
  1198. switch (key) {
  1199. case KeyCode::Key_Up:
  1200. emit_final_with_modifier('A');
  1201. return;
  1202. case KeyCode::Key_Down:
  1203. emit_final_with_modifier('B');
  1204. return;
  1205. case KeyCode::Key_Right:
  1206. emit_final_with_modifier('C');
  1207. return;
  1208. case KeyCode::Key_Left:
  1209. emit_final_with_modifier('D');
  1210. return;
  1211. case KeyCode::Key_Insert:
  1212. emit_tilde_with_modifier(2);
  1213. return;
  1214. case KeyCode::Key_Delete:
  1215. emit_tilde_with_modifier(3);
  1216. return;
  1217. case KeyCode::Key_Home:
  1218. emit_final_with_modifier('H');
  1219. return;
  1220. case KeyCode::Key_End:
  1221. emit_final_with_modifier('F');
  1222. return;
  1223. case KeyCode::Key_PageUp:
  1224. emit_tilde_with_modifier(5);
  1225. return;
  1226. case KeyCode::Key_PageDown:
  1227. emit_tilde_with_modifier(6);
  1228. return;
  1229. case KeyCode::Key_Return:
  1230. // The standard says that CR should be generated by the return key.
  1231. // The TTY will take care of translating it to CR LF for the terminal.
  1232. emit_string("\r");
  1233. return;
  1234. default:
  1235. break;
  1236. }
  1237. if (!code_point) {
  1238. // Probably a modifier being pressed.
  1239. return;
  1240. }
  1241. if (shift && key == KeyCode::Key_Tab) {
  1242. emit_string("\033[Z");
  1243. return;
  1244. }
  1245. // Key event was not one of the above special cases,
  1246. // attempt to treat it as a character...
  1247. if (ctrl) {
  1248. if (code_point >= 'a' && code_point <= 'z') {
  1249. code_point = code_point - 'a' + 1;
  1250. } else if (code_point == '\\') {
  1251. code_point = 0x1c;
  1252. }
  1253. }
  1254. // Alt modifier sends escape prefix.
  1255. if (alt)
  1256. emit_string("\033");
  1257. StringBuilder sb;
  1258. sb.append_code_point(code_point);
  1259. emit_string(sb.to_string());
  1260. }
  1261. void Terminal::unimplemented_control_code(u8 code)
  1262. {
  1263. dbgln_if(TERMINAL_DEBUG, "Unimplemented control code {:02x}", code);
  1264. }
  1265. void Terminal::unimplemented_escape_sequence(Intermediates intermediates, u8 last_byte)
  1266. {
  1267. StringBuilder builder;
  1268. builder.appendff("Unimplemented escape sequence {:c}", last_byte);
  1269. if (!intermediates.is_empty()) {
  1270. builder.append(", intermediates: ");
  1271. for (size_t i = 0; i < intermediates.size(); ++i)
  1272. builder.append((char)intermediates[i]);
  1273. }
  1274. dbgln("{}", builder.string_view());
  1275. }
  1276. void Terminal::unimplemented_csi_sequence(Parameters parameters, Intermediates intermediates, u8 last_byte)
  1277. {
  1278. StringBuilder builder;
  1279. builder.appendff("Unimplemented CSI sequence: {:c}", last_byte);
  1280. if (!parameters.is_empty()) {
  1281. builder.append(", parameters: [");
  1282. for (size_t i = 0; i < parameters.size(); ++i)
  1283. builder.appendff("{}{}", (i == 0) ? "" : ", ", parameters[i]);
  1284. builder.append("]");
  1285. }
  1286. if (!intermediates.is_empty()) {
  1287. builder.append(", intermediates:");
  1288. for (size_t i = 0; i < intermediates.size(); ++i)
  1289. builder.append((char)intermediates[i]);
  1290. }
  1291. dbgln("{}", builder.string_view());
  1292. }
  1293. void Terminal::unimplemented_osc_sequence(OscParameters parameters, u8 last_byte)
  1294. {
  1295. StringBuilder builder;
  1296. builder.appendff("Unimplemented OSC sequence parameters: (bel_terminated={}) [ ", last_byte == '\a');
  1297. bool first = true;
  1298. for (auto parameter : parameters) {
  1299. if (!first)
  1300. builder.append(", ");
  1301. builder.append("[");
  1302. for (auto character : parameter)
  1303. builder.append((char)character);
  1304. builder.append("]");
  1305. first = false;
  1306. }
  1307. builder.append(" ]");
  1308. dbgln("{}", builder.string_view());
  1309. }
  1310. #ifndef KERNEL
  1311. void Terminal::set_size(u16 columns, u16 rows)
  1312. {
  1313. if (!columns)
  1314. columns = 1;
  1315. if (!rows)
  1316. rows = 1;
  1317. if (columns == m_columns && rows == m_rows)
  1318. return;
  1319. // If we're making the terminal larger (column-wise), start at the end and go up, taking cells from the line below.
  1320. // otherwise start at the beginning and go down, pushing cells into the line below.
  1321. auto resize_and_rewrap = [&](auto& buffer, auto& old_cursor) {
  1322. auto cursor_on_line = [&](auto index) {
  1323. return index == old_cursor.row ? &old_cursor : nullptr;
  1324. };
  1325. // Two passes, one from top to bottom, another from bottom to top
  1326. for (size_t pass = 0; pass < 2; ++pass) {
  1327. auto forwards = (pass == 0) ^ (columns < m_columns);
  1328. if (forwards) {
  1329. for (size_t i = 1; i <= buffer.size(); ++i) {
  1330. auto is_at_seam = i == 1;
  1331. auto next_line = is_at_seam ? nullptr : &buffer[buffer.size() - i + 1];
  1332. auto& line = buffer[buffer.size() - i];
  1333. auto next_cursor = cursor_on_line(buffer.size() - i + 1);
  1334. line.rewrap(columns, next_line, next_cursor ?: cursor_on_line(buffer.size() - i), !!next_cursor);
  1335. }
  1336. } else {
  1337. for (size_t i = 0; i < buffer.size(); ++i) {
  1338. auto is_at_seam = i + 1 == buffer.size();
  1339. auto next_line = is_at_seam ? nullptr : &buffer[i + 1];
  1340. auto next_cursor = cursor_on_line(i + 1);
  1341. buffer[i].rewrap(columns, next_line, next_cursor ?: cursor_on_line(i), !!next_cursor);
  1342. }
  1343. }
  1344. Queue<size_t> lines_to_reevaluate;
  1345. for (size_t i = 0; i < buffer.size(); ++i) {
  1346. if (buffer[i].length() != columns)
  1347. lines_to_reevaluate.enqueue(i);
  1348. }
  1349. size_t rows_inserted = 0;
  1350. while (!lines_to_reevaluate.is_empty()) {
  1351. auto index = lines_to_reevaluate.dequeue();
  1352. auto is_at_seam = index + 1 == buffer.size();
  1353. auto next_line = is_at_seam ? nullptr : &buffer[index + 1];
  1354. auto& line = buffer[index];
  1355. auto next_cursor = cursor_on_line(index + 1);
  1356. line.rewrap(columns, next_line, next_cursor ?: cursor_on_line(index), !!next_cursor);
  1357. if (line.length() > columns) {
  1358. auto current_cursor = cursor_on_line(index);
  1359. // Split the line into two (or more)
  1360. ++index;
  1361. ++rows_inserted;
  1362. buffer.insert(index, make<Line>(0));
  1363. VERIFY(buffer[index].length() == 0);
  1364. line.rewrap(columns, &buffer[index], current_cursor, false);
  1365. // If we inserted a line and the old cursor was after that line, increment its row
  1366. if (!current_cursor && old_cursor.row >= index)
  1367. ++old_cursor.row;
  1368. if (buffer[index].length() != columns)
  1369. lines_to_reevaluate.enqueue(index);
  1370. }
  1371. if (next_line && next_line->length() != columns)
  1372. lines_to_reevaluate.enqueue(index + 1);
  1373. }
  1374. }
  1375. for (auto& line : buffer)
  1376. line.set_length(columns);
  1377. return old_cursor;
  1378. };
  1379. auto old_history_size = m_history.size();
  1380. m_history.extend(move(m_normal_screen_buffer));
  1381. CursorPosition cursor_tracker { cursor_row() + old_history_size, cursor_column() };
  1382. resize_and_rewrap(m_history, cursor_tracker);
  1383. if (auto extra_lines = m_history.size() - rows) {
  1384. while (extra_lines > 0) {
  1385. if (m_history.size() <= cursor_tracker.row)
  1386. break;
  1387. if (m_history.last().is_empty()) {
  1388. if (m_history.size() >= 2 && m_history[m_history.size() - 2].termination_column().has_value())
  1389. break;
  1390. --extra_lines;
  1391. m_history.take_last();
  1392. continue;
  1393. }
  1394. break;
  1395. }
  1396. }
  1397. // FIXME: This can use a more performant way to move the last N entries
  1398. // from the history into the normal buffer
  1399. m_normal_screen_buffer.ensure_capacity(rows);
  1400. while (m_normal_screen_buffer.size() < rows) {
  1401. if (!m_history.is_empty())
  1402. m_normal_screen_buffer.prepend(m_history.take_last());
  1403. else
  1404. m_normal_screen_buffer.unchecked_append(make<Line>(columns));
  1405. }
  1406. cursor_tracker.row -= m_history.size();
  1407. if (m_history.size() != old_history_size) {
  1408. m_client.terminal_history_changed(-old_history_size);
  1409. m_client.terminal_history_changed(m_history.size());
  1410. }
  1411. CursorPosition dummy_cursor_tracker {};
  1412. resize_and_rewrap(m_alternate_screen_buffer, dummy_cursor_tracker);
  1413. if (m_alternate_screen_buffer.size() > rows)
  1414. m_alternate_screen_buffer.remove(0, m_alternate_screen_buffer.size() - rows);
  1415. if (rows > m_rows) {
  1416. while (m_normal_screen_buffer.size() < rows)
  1417. m_normal_screen_buffer.append(make<Line>(columns));
  1418. while (m_alternate_screen_buffer.size() < rows)
  1419. m_alternate_screen_buffer.append(make<Line>(columns));
  1420. } else {
  1421. m_normal_screen_buffer.shrink(rows);
  1422. m_alternate_screen_buffer.shrink(rows);
  1423. }
  1424. m_columns = columns;
  1425. m_rows = rows;
  1426. m_scroll_region_top = 0;
  1427. m_scroll_region_bottom = rows - 1;
  1428. m_current_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1429. m_normal_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1430. m_alternate_saved_state.cursor.clamp(m_rows - 1, m_columns - 1);
  1431. m_saved_cursor_position.clamp(m_rows - 1, m_columns - 1);
  1432. m_horizontal_tabs.resize(columns);
  1433. for (unsigned i = 0; i < columns; ++i)
  1434. m_horizontal_tabs[i] = (i % 8) == 0;
  1435. // Rightmost column is always last tab on line.
  1436. m_horizontal_tabs[columns - 1] = 1;
  1437. set_cursor(cursor_tracker.row, cursor_tracker.column);
  1438. m_client.terminal_did_resize(m_columns, m_rows);
  1439. dbgln_if(TERMINAL_DEBUG, "Set terminal size: {}x{}", m_rows, m_columns);
  1440. }
  1441. #endif
  1442. #ifndef KERNEL
  1443. void Terminal::invalidate_cursor()
  1444. {
  1445. if (cursor_row() < active_buffer().size())
  1446. active_buffer()[cursor_row()].set_dirty(true);
  1447. }
  1448. Attribute Terminal::attribute_at(const Position& position) const
  1449. {
  1450. if (!position.is_valid())
  1451. return {};
  1452. if (position.row() >= static_cast<int>(line_count()))
  1453. return {};
  1454. auto& line = this->line(position.row());
  1455. if (static_cast<size_t>(position.column()) >= line.length())
  1456. return {};
  1457. return line.attribute_at(position.column());
  1458. }
  1459. #endif
  1460. }