Terminal.cpp 51 KB

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