terminal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Internals of the Terminal structure, for those other modules
  3. * which need to look inside it. It would be nice if this could be
  4. * folded back into terminal.c in future, with an abstraction layer
  5. * to handle everything that other modules need to know about it;
  6. * but for the moment, this will do.
  7. */
  8. #ifndef PUTTY_TERMINAL_H
  9. #define PUTTY_TERMINAL_H
  10. #include "tree234.h"
  11. struct beeptime {
  12. struct beeptime *next;
  13. unsigned long ticks;
  14. };
  15. #define TRUST_SIGIL_WIDTH 3
  16. #define TRUST_SIGIL_CHAR 0xDFFE
  17. typedef struct {
  18. int y, x;
  19. } pos;
  20. typedef struct termchar termchar;
  21. typedef struct termline termline;
  22. struct termchar {
  23. /*
  24. * Any code in terminal.c which definitely needs to be changed
  25. * when extra fields are added here is labelled with a comment
  26. * saying FULL-TERMCHAR.
  27. */
  28. unsigned long chr;
  29. unsigned long attr;
  30. truecolour truecolour;
  31. /*
  32. * The cc_next field is used to link multiple termchars
  33. * together into a list, so as to fit more than one character
  34. * into a character cell (Unicode combining characters).
  35. *
  36. * cc_next is a relative offset into the current array of
  37. * termchars. I.e. to advance to the next character in a list,
  38. * one does `tc += tc->next'.
  39. *
  40. * Zero means end of list.
  41. */
  42. int cc_next;
  43. };
  44. struct termline {
  45. unsigned short lattr;
  46. int cols; /* number of real columns on the line */
  47. int size; /* number of allocated termchars
  48. * (cc-lists may make this > cols) */
  49. bool temporary; /* true if decompressed from scrollback */
  50. int cc_free; /* offset to first cc in free list */
  51. struct termchar *chars;
  52. bool trusted;
  53. };
  54. struct bidi_cache_entry {
  55. int width;
  56. bool trusted;
  57. struct termchar *chars;
  58. int *forward, *backward; /* the permutations of line positions */
  59. };
  60. struct term_utf8_decode {
  61. int state; /* Is there a pending UTF-8 character */
  62. int chr; /* and what is it so far? */
  63. int size; /* The size of the UTF character. */
  64. };
  65. struct terminal_tag {
  66. int compatibility_level;
  67. tree234 *scrollback; /* lines scrolled off top of screen */
  68. tree234 *screen; /* lines on primary screen */
  69. tree234 *alt_screen; /* lines on alternate screen */
  70. int disptop; /* distance scrolled back (0 or -ve) */
  71. int tempsblines; /* number of lines of .scrollback that
  72. can be retrieved onto the terminal
  73. ("temporary scrollback") */
  74. termline **disptext; /* buffer of text on real screen */
  75. int dispcursx, dispcursy; /* location of cursor on real screen */
  76. int curstype; /* type of cursor on real screen */
  77. #define VBELL_TIMEOUT (TICKSPERSEC/10) /* visual bell lasts 1/10 sec */
  78. struct beeptime *beephead, *beeptail;
  79. int nbeeps;
  80. bool beep_overloaded;
  81. long lastbeep;
  82. #define TTYPE termchar
  83. #define TSIZE (sizeof(TTYPE))
  84. int default_attr, curr_attr, save_attr;
  85. truecolour curr_truecolour, save_truecolour;
  86. termchar basic_erase_char, erase_char;
  87. bufchain inbuf; /* terminal input buffer */
  88. pos curs; /* cursor */
  89. pos savecurs; /* saved cursor position */
  90. int marg_t, marg_b; /* scroll margins */
  91. bool dec_om; /* DEC origin mode flag */
  92. bool wrap, wrapnext; /* wrap flags */
  93. bool insert; /* insert-mode flag */
  94. int cset; /* 0 or 1: which char set */
  95. int save_cset, save_csattr; /* saved with cursor position */
  96. bool save_utf, save_wnext; /* saved with cursor position */
  97. bool rvideo; /* global reverse video flag */
  98. unsigned long rvbell_startpoint; /* for ESC[?5hESC[?5l vbell */
  99. bool cursor_on; /* cursor enabled flag */
  100. bool reset_132; /* Flag ESC c resets to 80 cols */
  101. bool use_bce; /* Use Background coloured erase */
  102. bool cblinker; /* When blinking is the cursor on ? */
  103. bool tblinker; /* When the blinking text is on */
  104. bool blink_is_real; /* Actually blink blinking text */
  105. int sco_acs, save_sco_acs; /* CSI 10,11,12m -> OEM charset */
  106. bool vt52_bold; /* Force bold on non-bold colours */
  107. bool utf; /* Are we in toggleable UTF-8 mode? */
  108. term_utf8_decode utf8; /* If so, here's our decoding state */
  109. bool printing, only_printing; /* Are we doing ANSI printing? */
  110. int print_state; /* state of print-end-sequence scan */
  111. bufchain printer_buf; /* buffered data for printer */
  112. printer_job *print_job;
  113. /* ESC 7 saved state for the alternate screen */
  114. pos alt_savecurs;
  115. int alt_save_attr;
  116. truecolour alt_save_truecolour;
  117. int alt_save_cset, alt_save_csattr;
  118. bool alt_save_utf;
  119. bool alt_save_wnext;
  120. int alt_save_sco_acs;
  121. int rows, cols, savelines;
  122. bool has_focus;
  123. bool in_vbell;
  124. long vbell_end;
  125. bool app_cursor_keys, app_keypad_keys, vt52_mode;
  126. bool repeat_off, srm_echo, cr_lf_return;
  127. bool seen_disp_event;
  128. bool big_cursor;
  129. bool xterm_mouse_forbidden;
  130. int xterm_mouse; /* send mouse messages to host */
  131. bool xterm_extended_mouse;
  132. bool urxvt_extended_mouse;
  133. int mouse_is_down; /* used while tracking mouse buttons */
  134. bool bracketed_paste, bracketed_paste_active;
  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. int osc_strlen;
  157. char osc_string[OSC_STR_MAX + 1];
  158. bool osc_w;
  159. char id_string[1024];
  160. unsigned char *tabs;
  161. enum {
  162. TOPLEVEL,
  163. SEEN_ESC,
  164. SEEN_CSI,
  165. SEEN_OSC,
  166. SEEN_OSC_W,
  167. DO_CTRLS,
  168. SEEN_OSC_P,
  169. OSC_STRING, OSC_MAYBE_ST,
  170. VT52_ESC,
  171. VT52_Y1,
  172. VT52_Y2,
  173. VT52_FG,
  174. VT52_BG
  175. } termstate;
  176. enum {
  177. NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
  178. } selstate;
  179. enum {
  180. LEXICOGRAPHIC, RECTANGULAR
  181. } seltype;
  182. enum {
  183. SM_CHAR, SM_WORD, SM_LINE
  184. } selmode;
  185. pos selstart, selend, selanchor;
  186. short wordness[256];
  187. /* Mask of attributes to pay attention to when painting. */
  188. int attr_mask;
  189. wchar_t *paste_buffer;
  190. int paste_len, paste_pos;
  191. Backend *backend;
  192. Ldisc *ldisc;
  193. TermWin *win;
  194. LogContext *logctx;
  195. struct unicode_data *ucsdata;
  196. unsigned long last_graphic_char;
  197. /*
  198. * We maintain a full copy of a Conf here, not merely a pointer
  199. * to it. That way, when we're passed a new one for
  200. * reconfiguration, we can check the differences and adjust the
  201. * _current_ setting of (e.g.) auto wrap mode rather than only
  202. * the default.
  203. */
  204. Conf *conf;
  205. /*
  206. * GUI implementations of seat_output call term_out, but it can
  207. * also be called from the ldisc if the ldisc is called _within_
  208. * term_out. So we have to guard against re-entrancy - if
  209. * seat_output is called recursively like this, it will simply add
  210. * data to the end of the buffer term_out is in the process of
  211. * working through.
  212. */
  213. bool in_term_out;
  214. /*
  215. * We don't permit window updates too close together, to avoid CPU
  216. * churn pointlessly redrawing the window faster than the user can
  217. * read. So after an update, we set window_update_cooldown = true
  218. * and schedule a timer to reset it to false. In between those
  219. * times, window updates are not performed, and instead we set
  220. * window_update_pending = true, which will remind us to perform
  221. * the deferred redraw when the cooldown period ends and
  222. * window_update_cooldown is reset to false.
  223. */
  224. bool window_update_pending, window_update_cooldown;
  225. long window_update_cooldown_end;
  226. /*
  227. * Track pending blinks and tblinks.
  228. */
  229. bool tblink_pending, cblink_pending;
  230. long next_tblink, next_cblink;
  231. /*
  232. * These are buffers used by the bidi and Arabic shaping code.
  233. */
  234. termchar *ltemp;
  235. int ltemp_size;
  236. bidi_char *wcFrom, *wcTo;
  237. int wcFromTo_size;
  238. struct bidi_cache_entry *pre_bidi_cache, *post_bidi_cache;
  239. size_t bidi_cache_size;
  240. /*
  241. * Current trust state, used to annotate every line of the
  242. * terminal that a graphic character is output to.
  243. */
  244. bool trusted;
  245. /*
  246. * We copy a bunch of stuff out of the Conf structure into local
  247. * fields in the Terminal structure, to avoid the repeated
  248. * tree234 lookups which would be involved in fetching them from
  249. * the former every time.
  250. */
  251. bool ansi_colour;
  252. char *answerback;
  253. int answerbacklen;
  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;
  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. bool minimised;
  302. /* Multi-layered colour palette. The colours from Conf (plus the
  303. * default xterm-256 ones that don't have Conf ids at all) have
  304. * lowest priority, followed by platform overrides if any,
  305. * followed by escape-sequence overrides during the session. */
  306. struct term_subpalette {
  307. rgb values[OSC4_NCOLOURS];
  308. bool present[OSC4_NCOLOURS];
  309. } subpalettes[3];
  310. #define SUBPAL_CONF 0
  311. #define SUBPAL_PLATFORM 1
  312. #define SUBPAL_SESSION 2
  313. /* The composite palette that we make out of the above */
  314. rgb palette[OSC4_NCOLOURS];
  315. unsigned winpos_x, winpos_y, winpixsize_x, winpixsize_y;
  316. /*
  317. * Assorted 'pending' flags for ancillary window changes performed
  318. * in term_update. Generally, to trigger one of these operations,
  319. * you set the pending flag and/or the parameters here, then call
  320. * term_schedule_update.
  321. */
  322. bool win_move_pending;
  323. int win_move_pending_x, win_move_pending_y;
  324. bool win_resize_pending;
  325. int win_resize_pending_w, win_resize_pending_h;
  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. static inline bool in_utf(Terminal *term)
  341. {
  342. return term->utf || term->ucsdata->line_codepage == CP_UTF8;
  343. }
  344. unsigned long term_translate(
  345. Terminal *term, term_utf8_decode *utf8, unsigned char c);
  346. static inline int term_char_width(Terminal *term, unsigned int c)
  347. {
  348. return term->cjk_ambig_wide ? mk_wcwidth_cjk(c) : mk_wcwidth(c);
  349. }
  350. /*
  351. * UCSINCOMPLETE is returned from term_translate if it's successfully
  352. * absorbed a byte but not emitted a complete character yet.
  353. * UCSTRUNCATED indicates a truncated multibyte sequence (so the
  354. * caller emits an error character and then calls term_translate again
  355. * with the same input byte). UCSINVALID indicates some other invalid
  356. * multibyte sequence, such as an overlong synonym, or a standalone
  357. * continuation byte, or a completely illegal thing like 0xFE. These
  358. * values are not stored in the terminal data structures at all.
  359. */
  360. #define UCSINCOMPLETE 0x8000003FU /* '?' */
  361. #define UCSTRUNCATED 0x80000021U /* '!' */
  362. #define UCSINVALID 0x8000002AU /* '*' */
  363. /*
  364. * Maximum number of combining characters we're willing to store in a
  365. * character cell. Our linked-list data representation permits an
  366. * unlimited number of these in principle, but if we allowed that in
  367. * practice then it would be an easy DoS to just squirt a squillion
  368. * identical combining characters to someone's terminal and cause
  369. * their PuTTY or pterm to consume lots of memory and CPU pointlessly.
  370. *
  371. * The precise figure of 32 is more or less arbitrary, but one point
  372. * supporting it is UAX #15's comment that 30 combining characters is
  373. * "significantly beyond what is required for any linguistic or
  374. * technical usage".
  375. */
  376. #define CC_LIMIT 32
  377. /* ----------------------------------------------------------------------
  378. * Helper functions for dealing with the small 'pos' structure.
  379. */
  380. static inline bool poslt(pos p1, pos p2)
  381. {
  382. if (p1.y != p2.y)
  383. return p1.y < p2.y;
  384. return p1.x < p2.x;
  385. }
  386. static inline bool posle(pos p1, pos p2)
  387. {
  388. if (p1.y != p2.y)
  389. return p1.y < p2.y;
  390. return p1.x <= p2.x;
  391. }
  392. static inline bool poseq(pos p1, pos p2)
  393. {
  394. return p1.y == p2.y && p1.x == p2.x;
  395. }
  396. static inline int posdiff_fn(pos p1, pos p2, int cols)
  397. {
  398. return (p1.y - p2.y) * (cols+1) + (p1.x - p2.x);
  399. }
  400. /* Convenience wrapper on posdiff_fn which uses the 'Terminal *term'
  401. * that more or less every function in terminal.c will have in scope.
  402. * For safety's sake I include a TYPECHECK that ensures it really is a
  403. * structure pointer of the right type. */
  404. #define GET_TERM_COLS TYPECHECK(term == (Terminal *)0, term->cols)
  405. #define posdiff(p1,p2) posdiff_fn(p1, p2, GET_TERM_COLS)
  406. /* Product-order comparisons for rectangular block selection. */
  407. static inline bool posPle(pos p1, pos p2)
  408. {
  409. return p1.y <= p2.y && p1.x <= p2.x;
  410. }
  411. static inline bool posPle_left(pos p1, pos p2)
  412. {
  413. /*
  414. * This function is used for checking whether a given character
  415. * cell of the terminal ought to be highlighted as part of the
  416. * selection, by comparing with term->selend. term->selend stores
  417. * the location one space to the right of the last highlighted
  418. * character. So we want to highlight the characters that are
  419. * less-or-equal (in the product order) to the character just left
  420. * of p2.
  421. *
  422. * (Setting up term->selend that way was the easiest way to get
  423. * rectangular selection working at all, in a code base that had
  424. * done lexicographic selection the way I happened to have done
  425. * it.)
  426. */
  427. return p1.y <= p2.y && p1.x < p2.x;
  428. }
  429. static inline bool incpos_fn(pos *p, int cols)
  430. {
  431. if (p->x == cols) {
  432. p->x = 0;
  433. p->y++;
  434. return true;
  435. }
  436. p->x++;
  437. return false;
  438. }
  439. static inline bool decpos_fn(pos *p, int cols)
  440. {
  441. if (p->x == 0) {
  442. p->x = cols;
  443. p->y--;
  444. return true;
  445. }
  446. p->x--;
  447. return false;
  448. }
  449. /* Convenience wrappers on incpos and decpos which use term->cols
  450. * (similarly to posdiff above), and also (for mild convenience and
  451. * mostly historical inertia) let you leave off the & at every call
  452. * site. */
  453. #define incpos(p) incpos_fn(&(p), GET_TERM_COLS)
  454. #define decpos(p) decpos_fn(&(p), GET_TERM_COLS)
  455. #endif