Terminal.cpp 50 KB

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