Terminal.cpp 51 KB

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