terminal.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Internals of the Terminal structure, used by other modules in the
  3. * terminal subdirectory and by test suites.
  4. */
  5. #ifndef PUTTY_TERMINAL_H
  6. #define PUTTY_TERMINAL_H
  7. #include "tree234.h"
  8. struct beeptime {
  9. struct beeptime *next;
  10. unsigned long ticks;
  11. };
  12. #define TRUST_SIGIL_WIDTH 3
  13. #define TRUST_SIGIL_CHAR 0xDFFE
  14. typedef struct {
  15. int y, x;
  16. } pos;
  17. typedef struct termchar termchar;
  18. typedef struct termline termline;
  19. struct termchar {
  20. /*
  21. * Any code in terminal.c which definitely needs to be changed
  22. * when extra fields are added here is labelled with a comment
  23. * saying FULL-TERMCHAR.
  24. */
  25. unsigned long chr;
  26. unsigned long attr;
  27. truecolour truecolour;
  28. /*
  29. * The cc_next field is used to link multiple termchars
  30. * together into a list, so as to fit more than one character
  31. * into a character cell (Unicode combining characters).
  32. *
  33. * cc_next is a relative offset into the current array of
  34. * termchars. I.e. to advance to the next character in a list,
  35. * one does `tc += tc->next'.
  36. *
  37. * Zero means end of list.
  38. */
  39. int cc_next;
  40. };
  41. struct termline {
  42. unsigned short lattr;
  43. int cols; /* number of real columns on the line */
  44. int size; /* number of allocated termchars
  45. * (cc-lists may make this > cols) */
  46. bool temporary; /* true if decompressed from scrollback */
  47. int cc_free; /* offset to first cc in free list */
  48. struct termchar *chars;
  49. bool trusted;
  50. };
  51. struct bidi_cache_entry {
  52. int width;
  53. bool trusted;
  54. struct termchar *chars;
  55. int *forward, *backward; /* the permutations of line positions */
  56. };
  57. struct term_utf8_decode {
  58. int state; /* Is there a pending UTF-8 character */
  59. int chr; /* and what is it so far? */
  60. int size; /* The size of the UTF character. */
  61. };
  62. struct term_userpass_state;
  63. struct terminal_tag {
  64. int compatibility_level;
  65. tree234 *scrollback; /* lines scrolled off top of screen */
  66. tree234 *screen; /* lines on primary screen */
  67. tree234 *alt_screen; /* lines on alternate screen */
  68. int disptop; /* distance scrolled back (0 or -ve) */
  69. int tempsblines; /* number of lines of .scrollback that
  70. can be retrieved onto the terminal
  71. ("temporary scrollback") */
  72. termline **disptext; /* buffer of text on real screen */
  73. int dispcursx, dispcursy; /* location of cursor on real screen */
  74. int curstype; /* type of cursor on real screen */
  75. #define VBELL_TIMEOUT (TICKSPERSEC/10) /* visual bell lasts 1/10 sec */
  76. struct beeptime *beephead, *beeptail;
  77. int nbeeps;
  78. bool beep_overloaded;
  79. long lastbeep;
  80. #define TTYPE termchar
  81. #define TSIZE (sizeof(TTYPE))
  82. int default_attr, curr_attr, save_attr;
  83. truecolour curr_truecolour, save_truecolour;
  84. termchar basic_erase_char, erase_char;
  85. bufchain inbuf; /* terminal input buffer */
  86. pos curs; /* cursor */
  87. pos savecurs; /* saved cursor position */
  88. int marg_t, marg_b; /* scroll margins */
  89. bool dec_om; /* DEC origin mode flag */
  90. bool wrap, wrapnext; /* wrap flags */
  91. bool insert; /* insert-mode flag */
  92. int cset; /* 0 or 1: which char set */
  93. int save_cset, save_csattr; /* saved with cursor position */
  94. bool save_utf, save_wnext; /* saved with cursor position */
  95. bool rvideo; /* global reverse video flag */
  96. unsigned long rvbell_startpoint; /* for ESC[?5hESC[?5l vbell */
  97. bool cursor_on; /* cursor enabled flag */
  98. bool reset_132; /* Flag ESC c resets to 80 cols */
  99. bool use_bce; /* Use Background coloured erase */
  100. bool cblinker; /* When blinking is the cursor on ? */
  101. bool tblinker; /* When the blinking text is on */
  102. bool blink_is_real; /* Actually blink blinking text */
  103. int sco_acs, save_sco_acs; /* CSI 10,11,12m -> OEM charset */
  104. bool vt52_bold; /* Force bold on non-bold colours */
  105. bool utf; /* Are we in toggleable UTF-8 mode? */
  106. term_utf8_decode utf8; /* If so, here's our decoding state */
  107. bool printing, only_printing; /* Are we doing ANSI printing? */
  108. int print_state; /* state of print-end-sequence scan */
  109. bufchain printer_buf; /* buffered data for printer */
  110. printer_job *print_job;
  111. /* ESC 7 saved state for the alternate screen */
  112. pos alt_savecurs;
  113. int alt_save_attr;
  114. truecolour alt_save_truecolour;
  115. int alt_save_cset, alt_save_csattr;
  116. bool alt_save_utf;
  117. bool alt_save_wnext;
  118. int alt_save_sco_acs;
  119. int rows, cols, savelines;
  120. bool has_focus;
  121. bool in_vbell;
  122. long vbell_end;
  123. bool app_cursor_keys, app_keypad_keys, vt52_mode;
  124. bool repeat_off, srm_echo, cr_lf_return;
  125. bool big_cursor;
  126. bool xterm_mouse_forbidden;
  127. int xterm_mouse; /* send mouse messages to host */
  128. bool xterm_extended_mouse;
  129. bool urxvt_extended_mouse;
  130. int mouse_is_down; /* used while tracking mouse buttons */
  131. int raw_mouse_reported_x;
  132. int raw_mouse_reported_y;
  133. bool bracketed_paste, bracketed_paste_active;
  134. bool no_bracketed_paste; /* disabled in configuration */
  135. int cset_attr[2];
  136. /*
  137. * Saved settings on the alternate screen.
  138. */
  139. int alt_x, alt_y;
  140. bool alt_wnext, alt_ins;
  141. bool alt_om, alt_wrap;
  142. int alt_cset, alt_sco_acs;
  143. bool alt_utf;
  144. int alt_t, alt_b;
  145. int alt_which;
  146. int alt_sblines; /* # of lines on alternate screen that should be used for scrollback. */
  147. #define ARGS_MAX 32 /* max # of esc sequence arguments */
  148. #define ARG_DEFAULT 0 /* if an arg isn't specified */
  149. #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
  150. unsigned esc_args[ARGS_MAX];
  151. int esc_nargs;
  152. int esc_query;
  153. #define ANSI(x,y) ((x)+((y)*256))
  154. #define ANSI_QUE(x) ANSI(x,1)
  155. #define OSC_STR_MAX 2048
  156. bool osc_is_apc;
  157. int osc_strlen;
  158. char osc_string[OSC_STR_MAX + 1];
  159. bool osc_w;
  160. char id_string[1024];
  161. unsigned char *tabs;
  162. enum {
  163. TOPLEVEL,
  164. SEEN_ESC,
  165. SEEN_CSI,
  166. SEEN_OSC,
  167. SEEN_OSC_W,
  168. DO_CTRLS,
  169. SEEN_OSC_P,
  170. OSC_STRING, OSC_MAYBE_ST, OSC_MAYBE_ST_UTF8,
  171. VT52_ESC,
  172. VT52_Y1,
  173. VT52_Y2,
  174. VT52_FG,
  175. VT52_BG
  176. } termstate;
  177. enum {
  178. NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
  179. } selstate;
  180. enum {
  181. LEXICOGRAPHIC, RECTANGULAR
  182. } seltype;
  183. enum {
  184. SM_CHAR, SM_WORD, SM_LINE
  185. } selmode;
  186. pos selstart, selend, selanchor;
  187. short wordness[256];
  188. /* Mask of attributes to pay attention to when painting. */
  189. int attr_mask;
  190. wchar_t *paste_buffer;
  191. size_t paste_len, paste_pos;
  192. Backend *backend;
  193. Ldisc *ldisc;
  194. TermWin *win;
  195. LogContext *logctx;
  196. struct unicode_data *ucsdata;
  197. unsigned long last_graphic_char;
  198. /*
  199. * We maintain a full copy of a Conf here, not merely a pointer
  200. * to it. That way, when we're passed a new one for
  201. * reconfiguration, we can check the differences and adjust the
  202. * _current_ setting of (e.g.) auto wrap mode rather than only
  203. * the default.
  204. */
  205. Conf *conf;
  206. /*
  207. * GUI implementations of seat_output call term_out, but it can
  208. * also be called from the ldisc if the ldisc is called _within_
  209. * term_out. So we have to guard against re-entrancy - if
  210. * seat_output is called recursively like this, it will simply add
  211. * data to the end of the buffer term_out is in the process of
  212. * working through.
  213. */
  214. bool in_term_out;
  215. /*
  216. * We don't permit window updates too close together, to avoid CPU
  217. * churn pointlessly redrawing the window faster than the user can
  218. * read. So after an update, we set window_update_cooldown = true
  219. * and schedule a timer to reset it to false. In between those
  220. * times, window updates are not performed, and instead we set
  221. * window_update_pending = true, which will remind us to perform
  222. * the deferred redraw when the cooldown period ends and
  223. * window_update_cooldown is reset to false.
  224. */
  225. bool window_update_pending, window_update_cooldown;
  226. long window_update_cooldown_end;
  227. /*
  228. * Track pending blinks and tblinks.
  229. */
  230. bool tblink_pending, cblink_pending;
  231. long next_tblink, next_cblink;
  232. /*
  233. * These are buffers used by the bidi and Arabic shaping code.
  234. */
  235. termchar *ltemp;
  236. int ltemp_size;
  237. bidi_char *wcFrom, *wcTo;
  238. int wcFromTo_size;
  239. struct bidi_cache_entry *pre_bidi_cache, *post_bidi_cache;
  240. size_t bidi_cache_size;
  241. /*
  242. * Current trust state, used to annotate every line of the
  243. * terminal that a graphic character is output to.
  244. */
  245. bool trusted;
  246. /*
  247. * We copy a bunch of stuff out of the Conf structure into local
  248. * fields in the Terminal structure, to avoid the repeated
  249. * tree234 lookups which would be involved in fetching them from
  250. * the former every time.
  251. */
  252. bool ansi_colour;
  253. strbuf *answerback;
  254. bool no_arabicshaping;
  255. int beep;
  256. bool bellovl;
  257. int bellovl_n;
  258. int bellovl_s;
  259. int bellovl_t;
  260. bool no_bidi;
  261. bool bksp_is_delete;
  262. bool blink_cur;
  263. bool blinktext;
  264. bool cjk_ambig_wide;
  265. int conf_height;
  266. int conf_width;
  267. bool crhaslf;
  268. bool erase_to_scrollback;
  269. int funky_type, sharrow_type;
  270. bool lfhascr;
  271. bool logflush;
  272. int logtype;
  273. bool mouse_override;
  274. bool nethack_keypad;
  275. bool no_alt_screen;
  276. bool no_applic_c;
  277. bool no_applic_k;
  278. bool no_dbackspace;
  279. bool no_mouse_rep;
  280. bool no_remote_charset;
  281. bool no_remote_resize;
  282. bool no_remote_wintitle;
  283. bool no_remote_clearscroll;
  284. bool rawcnp;
  285. bool utf8linedraw;
  286. bool rect_select;
  287. int remote_qtitle_action;
  288. bool rxvt_homeend;
  289. bool scroll_on_disp;
  290. bool scroll_on_key;
  291. bool xterm_256_colour;
  292. bool true_colour;
  293. wchar_t *last_selected_text;
  294. int *last_selected_attr;
  295. truecolour *last_selected_tc;
  296. size_t last_selected_len;
  297. int mouse_select_clipboards[N_CLIPBOARDS];
  298. int n_mouse_select_clipboards;
  299. int mouse_paste_clipboard;
  300. char *window_title, *icon_title;
  301. int wintitle_codepage, icontitle_codepage;
  302. bool minimised;
  303. BidiContext *bidi_ctx;
  304. /* Multi-layered colour palette. The colours from Conf (plus the
  305. * default xterm-256 ones that don't have Conf ids at all) have
  306. * lowest priority, followed by platform overrides if any,
  307. * followed by escape-sequence overrides during the session. */
  308. struct term_subpalette {
  309. rgb values[OSC4_NCOLOURS];
  310. bool present[OSC4_NCOLOURS];
  311. } subpalettes[3];
  312. #define SUBPAL_CONF 0
  313. #define SUBPAL_PLATFORM 1
  314. #define SUBPAL_SESSION 2
  315. /* The composite palette that we make out of the above */
  316. rgb palette[OSC4_NCOLOURS];
  317. unsigned winpos_x, winpos_y, winpixsize_x, winpixsize_y;
  318. /*
  319. * Assorted 'pending' flags for ancillary window changes performed
  320. * in term_update. Generally, to trigger one of these operations,
  321. * you set the pending flag and/or the parameters here, then call
  322. * term_schedule_update.
  323. */
  324. bool win_move_pending;
  325. int win_move_pending_x, win_move_pending_y;
  326. bool win_zorder_pending;
  327. bool win_zorder_top;
  328. bool win_minimise_pending;
  329. bool win_minimise_enable;
  330. bool win_maximise_pending;
  331. bool win_maximise_enable;
  332. bool win_title_pending, win_icon_title_pending;
  333. bool win_pointer_shape_pending;
  334. bool win_pointer_shape_raw;
  335. bool win_refresh_pending;
  336. bool win_scrollbar_update_pending;
  337. bool win_palette_pending;
  338. unsigned win_palette_pending_min, win_palette_pending_limit;
  339. /*
  340. * Unlike the rest of the above 'pending' flags, the one for
  341. * window resizing has to be more complicated, because it's very
  342. * likely that a server sending a window-resize escape sequence is
  343. * going to follow it up immediately with further terminal output
  344. * that draws a full-screen application expecting the terminal to
  345. * be the new size.
  346. *
  347. * So, once we've requested a window resize from the TermWin, we
  348. * have to stop processing terminal data until we get back the
  349. * notification that our window really has changed size (or until
  350. * we find out that it's not going to).
  351. *
  352. * Hence, window resizes go through a small state machine with two
  353. * different kinds of 'pending'. NEED_SEND is the state where
  354. * we've received an escape sequence asking for a new size but not
  355. * yet sent it to the TermWin via win_request_resize; AWAIT_REPLY
  356. * is the state where we've sent it to the TermWin and are
  357. * expecting a call back to term_size().
  358. *
  359. * So _both_ of those 'pending' states inhibit terminal output
  360. * processing.
  361. *
  362. * (Hence, once we're in either state, we should never handle
  363. * another resize sequence, so the only possible path through this
  364. * state machine is to get all the way back to the ground state
  365. * before doing anything else interesting.)
  366. */
  367. enum {
  368. WIN_RESIZE_NO, WIN_RESIZE_NEED_SEND, WIN_RESIZE_AWAIT_REPLY
  369. } win_resize_pending;
  370. int win_resize_pending_w, win_resize_pending_h;
  371. /*
  372. * Indicates whether term_get_userpass_input is currently using
  373. * the terminal to present a password prompt or similar, and if
  374. * so, whether it's overridden the terminal into UTF-8 mode.
  375. */
  376. struct term_userpass_state *userpass_state;
  377. bool userpass_utf8_override;
  378. };
  379. static inline bool in_utf(Terminal *term)
  380. {
  381. return (term->utf ||
  382. term->ucsdata->line_codepage == CP_UTF8 ||
  383. (term->userpass_state && term->userpass_utf8_override));
  384. }
  385. unsigned long term_translate(
  386. Terminal *term, term_utf8_decode *utf8, unsigned char c);
  387. static inline int term_char_width(Terminal *term, unsigned int c)
  388. {
  389. return term->cjk_ambig_wide ? mk_wcwidth_cjk(c) : mk_wcwidth(c);
  390. }
  391. /*
  392. * UCSINCOMPLETE is returned from term_translate if it's successfully
  393. * absorbed a byte but not emitted a complete character yet.
  394. * UCSTRUNCATED indicates a truncated multibyte sequence (so the
  395. * caller emits an error character and then calls term_translate again
  396. * with the same input byte). UCSINVALID indicates some other invalid
  397. * multibyte sequence, such as an overlong synonym, or a standalone
  398. * continuation byte, or a completely illegal thing like 0xFE. These
  399. * values are not stored in the terminal data structures at all.
  400. */
  401. #define UCSINCOMPLETE 0x8000003FU /* '?' */
  402. #define UCSTRUNCATED 0x80000021U /* '!' */
  403. #define UCSINVALID 0x8000002AU /* '*' */
  404. /*
  405. * Maximum number of combining characters we're willing to store in a
  406. * character cell. Our linked-list data representation permits an
  407. * unlimited number of these in principle, but if we allowed that in
  408. * practice then it would be an easy DoS to just squirt a squillion
  409. * identical combining characters to someone's terminal and cause
  410. * their PuTTY or pterm to consume lots of memory and CPU pointlessly.
  411. *
  412. * The precise figure of 32 is more or less arbitrary, but one point
  413. * supporting it is UAX #15's comment that 30 combining characters is
  414. * "significantly beyond what is required for any linguistic or
  415. * technical usage".
  416. */
  417. #define CC_LIMIT 32
  418. /* ----------------------------------------------------------------------
  419. * Helper functions for dealing with the small 'pos' structure.
  420. */
  421. static inline bool poslt(pos p1, pos p2)
  422. {
  423. if (p1.y != p2.y)
  424. return p1.y < p2.y;
  425. return p1.x < p2.x;
  426. }
  427. static inline bool posle(pos p1, pos p2)
  428. {
  429. if (p1.y != p2.y)
  430. return p1.y < p2.y;
  431. return p1.x <= p2.x;
  432. }
  433. static inline bool poseq(pos p1, pos p2)
  434. {
  435. return p1.y == p2.y && p1.x == p2.x;
  436. }
  437. static inline int posdiff_fn(pos p1, pos p2, int cols)
  438. {
  439. return (p1.y - p2.y) * (cols+1) + (p1.x - p2.x);
  440. }
  441. /* Convenience wrapper on posdiff_fn which uses the 'Terminal *term'
  442. * that more or less every function in terminal.c will have in scope.
  443. * For safety's sake I include a TYPECHECK that ensures it really is a
  444. * structure pointer of the right type. */
  445. #define GET_TERM_COLS TYPECHECK(term == (Terminal *)0, term->cols)
  446. #define posdiff(p1,p2) posdiff_fn(p1, p2, GET_TERM_COLS)
  447. /* Product-order comparisons for rectangular block selection. */
  448. static inline bool posPle(pos p1, pos p2)
  449. {
  450. return p1.y <= p2.y && p1.x <= p2.x;
  451. }
  452. static inline bool posPle_left(pos p1, pos p2)
  453. {
  454. /*
  455. * This function is used for checking whether a given character
  456. * cell of the terminal ought to be highlighted as part of the
  457. * selection, by comparing with term->selend. term->selend stores
  458. * the location one space to the right of the last highlighted
  459. * character. So we want to highlight the characters that are
  460. * less-or-equal (in the product order) to the character just left
  461. * of p2.
  462. *
  463. * (Setting up term->selend that way was the easiest way to get
  464. * rectangular selection working at all, in a code base that had
  465. * done lexicographic selection the way I happened to have done
  466. * it.)
  467. */
  468. return p1.y <= p2.y && p1.x < p2.x;
  469. }
  470. static inline bool incpos_fn(pos *p, int cols)
  471. {
  472. if (p->x == cols) {
  473. p->x = 0;
  474. p->y++;
  475. return true;
  476. }
  477. p->x++;
  478. return false;
  479. }
  480. static inline bool decpos_fn(pos *p, int cols)
  481. {
  482. if (p->x == 0) {
  483. p->x = cols;
  484. p->y--;
  485. return true;
  486. }
  487. p->x--;
  488. return false;
  489. }
  490. /* Convenience wrappers on incpos and decpos which use term->cols
  491. * (similarly to posdiff above), and also (for mild convenience and
  492. * mostly historical inertia) let you leave off the & at every call
  493. * site. */
  494. #define incpos(p) incpos_fn(&(p), GET_TERM_COLS)
  495. #define decpos(p) decpos_fn(&(p), GET_TERM_COLS)
  496. struct TermLineEditorCallbackReceiverVtable {
  497. void (*to_terminal)(TermLineEditorCallbackReceiver *rcv, ptrlen data);
  498. void (*to_backend)(TermLineEditorCallbackReceiver *rcv, ptrlen data);
  499. void (*special)(TermLineEditorCallbackReceiver *rcv,
  500. SessionSpecialCode code, int arg);
  501. void (*newline)(TermLineEditorCallbackReceiver *rcv);
  502. };
  503. struct TermLineEditorCallbackReceiver {
  504. const TermLineEditorCallbackReceiverVtable *vt;
  505. };
  506. TermLineEditor *lineedit_new(Terminal *term, unsigned flags,
  507. TermLineEditorCallbackReceiver *receiver);
  508. void lineedit_free(TermLineEditor *le);
  509. void lineedit_input(TermLineEditor *le, char ch, bool dedicated);
  510. void lineedit_modify_flags(TermLineEditor *le, unsigned clr, unsigned flip);
  511. void lineedit_send_line(TermLineEditor *le);
  512. /*
  513. * Flags controlling the behaviour of TermLineEditor.
  514. */
  515. #define LINEEDIT_FLAGS(X) \
  516. X(LE_INTERRUPT) /* pass SS_IP back to client on ^C */ \
  517. X(LE_SUSPEND) /* pass SS_SUSP back to client on ^Z */ \
  518. X(LE_ABORT) /* pass SS_ABORT back to client on ^\ */ \
  519. X(LE_EOF_ALWAYS) /* pass SS_EOF to client on *any* ^Z
  520. * (X(not)just if the line buffer is empty) */ \
  521. X(LE_ESC_ERASES) /* make ESC erase the line, as well as ^U */ \
  522. X(LE_CRLF_NEWLINE) /* interpret manual ^M^J the same as Return */ \
  523. /* end of list */
  524. enum {
  525. #define ALLOCATE_BIT_POSITION(flag) flag ## _bitpos,
  526. LINEEDIT_FLAGS(ALLOCATE_BIT_POSITION)
  527. #undef ALLOCATE_BIT_POSITION
  528. };
  529. enum {
  530. #define DEFINE_FLAG_BIT(flag) flag = 1 << flag ## _bitpos,
  531. LINEEDIT_FLAGS(DEFINE_FLAG_BIT)
  532. #undef DEFINE_FLAG_BIT
  533. };
  534. termline *term_get_line(Terminal *term, int y);
  535. void term_release_line(termline *line);
  536. #endif