1 /*
2  * Copyright (C) 2001-2004 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #ifndef vte_vte_private_h_included
20 #define vte_vte_private_h_included
21 
22 #include <sys/ioctl.h>
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/stat.h>
26 #ifdef HAVE_SYS_TERMIOS_H
27 #include <sys/termios.h>
28 #endif
29 #include <sys/time.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <math.h>
33 #include <pwd.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_TERMIOS_H
39 #include <termios.h>
40 #endif
41 #include <unistd.h>
42 #include <glib/gi18n-lib.h>
43 
44 #include "vte.h"
45 #include "buffer.h"
46 #include "debug.h"
47 #include "vteconv.h"
48 #include "vtedraw.h"
49 #include "ring.h"
50 #include "caps.h"
51 
52 G_BEGIN_DECLS
53 
54 #define VTE_TAB_WIDTH			8
55 #define VTE_LINE_WIDTH			1
56 #define VTE_ROWS			24
57 #define VTE_COLUMNS			80
58 
59 /*
60  * Colors are encoded in 25 bits as follows:
61  *
62  * 0 .. 255:
63  *   Colors set by SGR 256-color extension (38/48;5;index).
64  *   These are direct indices into the color palette.
65  *
66  * 256 .. VTE_PALETTE_SIZE - 1 (261):
67  *   Special values, such as default colors.
68  *   These are direct indices into the color palette.
69  *
70  * VTE_LEGACY_COLORS_OFFSET (512) .. VTE_LEGACY_COLORS_OFFSET + VTE_LEGACY_FULL_COLOR_SET_SIZE - 1 (527):
71  *   Colors set by legacy escapes (30..37/40..47, 90..97/100..107).
72  *   These are translated to 0 .. 15 before looking up in the palette, taking bold into account.
73  *
74  * VTE_DIM_COLORS (2^10) .. :
75  *   Dimmed version of the above, for foreground colors.
76  *   Cell attributes can't have these colors.
77  *
78  * VTE_RGB_COLOR (2^24) .. VTE_RGB_COLOR + 16Mi - 1 (2^25 - 1):
79  *   Colors set by SGR truecolor extension (38/48;2;red;green;blue)
80  *   These are direct RGB values.
81  */
82 #define VTE_LEGACY_COLORS_OFFSET	512
83 #define VTE_LEGACY_COLOR_SET_SIZE	8
84 #define VTE_LEGACY_FULL_COLOR_SET_SIZE	16
85 #define VTE_COLOR_PLAIN_OFFSET		0
86 #define VTE_COLOR_BRIGHT_OFFSET		8
87 #define VTE_DIM_COLOR			(1 << 10)
88 #define VTE_RGB_COLOR			(1 << 24)
89 /* More color defines in vterowdata.h */
90 
91 #define VTE_SCROLLBACK_INIT		512
92 #define VTE_DEFAULT_CURSOR		GDK_XTERM
93 #define VTE_MOUSING_CURSOR		GDK_LEFT_PTR
94 #define VTE_TAB_MAX			999
95 #define VTE_ADJUSTMENT_PRIORITY		G_PRIORITY_DEFAULT_IDLE
96 #define VTE_INPUT_RETRY_PRIORITY	G_PRIORITY_HIGH
97 #define VTE_INPUT_PRIORITY		G_PRIORITY_DEFAULT_IDLE
98 #define VTE_CHILD_INPUT_PRIORITY	G_PRIORITY_DEFAULT_IDLE
99 #define VTE_CHILD_OUTPUT_PRIORITY	G_PRIORITY_HIGH
100 #define VTE_FX_PRIORITY			G_PRIORITY_DEFAULT_IDLE
101 #define VTE_REGCOMP_FLAGS		REG_EXTENDED
102 #define VTE_REGEXEC_FLAGS		0
103 #define VTE_INPUT_CHUNK_SIZE		0x2000
104 #define VTE_MAX_INPUT_READ		0x1000
105 #define VTE_INVALID_BYTE		'?'
106 #define VTE_DISPLAY_TIMEOUT		10
107 #define VTE_UPDATE_TIMEOUT		15
108 #define VTE_UPDATE_REPEAT_TIMEOUT	30
109 #define VTE_MAX_PROCESS_TIME		100
110 #define VTE_CELL_BBOX_SLACK		1
111 #define VTE_DEFAULT_UTF8_AMBIGUOUS_WIDTH 1
112 
113 #define VTE_UTF8_BPC                    (6) /* Maximum number of bytes used per UTF-8 character */
114 
115 /* Keep in decreasing order of precedence. */
116 #define VTE_COLOR_SOURCE_ESCAPE 0
117 #define VTE_COLOR_SOURCE_API 1
118 
119 #define VTE_FONT_SCALE_MIN (.25)
120 #define VTE_FONT_SCALE_MAX (4.)
121 
122 #define I_(string) (g_intern_static_string(string))
123 
124 typedef enum {
125   VTE_REGEX_CURSOR_GDKCURSOR,
126   VTE_REGEX_CURSOR_GDKCURSORTYPE,
127   VTE_REGEX_CURSOR_NAME
128 } VteRegexCursorMode;
129 
130 /* The order is important */
131 typedef enum {
132 	MOUSE_TRACKING_NONE,
133 	MOUSE_TRACKING_SEND_XY_ON_CLICK,
134 	MOUSE_TRACKING_SEND_XY_ON_BUTTON,
135 	MOUSE_TRACKING_HILITE_TRACKING,
136 	MOUSE_TRACKING_CELL_MOTION_TRACKING,
137 	MOUSE_TRACKING_ALL_MOTION_TRACKING
138 } MouseTrackingMode;
139 
140 /* A match regex, with a tag. */
141 struct vte_match_regex {
142 	gint tag;
143         GRegex *regex;
144         GRegexMatchFlags match_flags;
145         VteRegexCursorMode cursor_mode;
146         union {
147 	       GdkCursor *cursor;
148                char *cursor_name;
149                GdkCursorType cursor_type;
150         } cursor;
151 };
152 
153 typedef enum _VteCharacterReplacement {
154         VTE_CHARACTER_REPLACEMENT_NONE,
155         VTE_CHARACTER_REPLACEMENT_LINE_DRAWING,
156         VTE_CHARACTER_REPLACEMENT_BRITISH
157 } VteCharacterReplacement;
158 
159 /* The terminal's keypad/cursor state.  A terminal can either be using the
160  * normal keypad, or the "application" keypad. */
161 typedef enum _VteKeymode {
162 	VTE_KEYMODE_NORMAL,
163 	VTE_KEYMODE_APPLICATION
164 } VteKeymode;
165 
166 typedef struct _VteScreen VteScreen;
167 
168 typedef struct _VtePaletteColor {
169 	struct {
170 		PangoColor color;
171 		gboolean is_set;
172 	} sources[2];
173 } VtePaletteColor;
174 
175 /* These correspond to the parameters for DECSCUSR (Set cursor style). */
176 typedef enum _VteCursorStyle {
177         /* We treat 0 and 1 differently, assuming that the VT510 does so too.
178          *
179          * See, according to the "VT510 Video Terminal Programmer Information",
180          * from vt100.net, paragraph "2.5.7 Cursor Display", there was a menu
181          * item in the "Terminal Set-Up" to set the cursor's style. It looks
182          * like that defaulted to blinking block. So it makes sense for 0 to
183          * mean "set cursor style to default (set by Set-Up)" and 1 to mean
184          * "set cursor style to blinking block", since that default need not be
185          * blinking block. Access to a VT510 is needed to test this theory,
186          * but it seems plausible. And, anyhow, we can even decide we know
187          * better than the VT510 designers! */
188         VTE_CURSOR_STYLE_TERMINAL_DEFAULT = 0,
189         VTE_CURSOR_STYLE_BLINK_BLOCK      = 1,
190         VTE_CURSOR_STYLE_STEADY_BLOCK     = 2,
191         VTE_CURSOR_STYLE_BLINK_UNDERLINE  = 3,
192         VTE_CURSOR_STYLE_STEADY_UNDERLINE = 4,
193         /* *_IBEAM are xterm extensions */
194         VTE_CURSOR_STYLE_BLINK_IBEAM      = 5,
195         VTE_CURSOR_STYLE_STEADY_IBEAM     = 6
196 } VteCursorStyle;
197 
198 /* Terminal private data. */
199 struct _VteTerminalPrivate {
200         /* Metric and sizing data: dimensions of the window */
201         glong row_count;
202         glong column_count;
203 
204 	/* Emulation setup data. */
205 	struct _vte_matcher *matcher;	/* control sequence matcher */
206 	const char *emulation;		/* terminal type to emulate */
207         gboolean autowrap;              /* auto wraparound at right margin */
208 	int keypad_mode, cursor_mode;	/* these would be VteKeymodes, but we
209 					   need to guarantee its type */
210 	GHashTable *dec_saved;
211 
212 	/* PTY handling data. */
213 	VtePty *pty;
214 	GIOChannel *pty_channel;	/* master channel */
215 	guint pty_input_source;
216 	guint pty_output_source;
217 	gboolean pty_input_active;
218 	GPid pty_pid;			/* pid of child using pty slave */
219 	guint child_watch_source;
220 
221 	/* Input data queues. */
222 	const char *encoding;		/* the pty's encoding */
223         int utf8_ambiguous_width;
224 	struct _vte_iso2022_state *iso2022;
225 	struct _vte_incoming_chunk{
226 		struct _vte_incoming_chunk *next;
227 		guint len;
228 		guchar data[VTE_INPUT_CHUNK_SIZE
229 			- 2 * sizeof(void *)];
230 	} *incoming;			/* pending bytestream */
231 	GArray *pending;		/* pending characters */
232 	GSList *update_regions;
233 	gboolean invalidated_all;	/* pending refresh of entire terminal */
234 	GList *active;                  /* is the terminal processing data */
235 	glong input_bytes;
236 	glong max_input_bytes;
237 
238 	/* Output data queue. */
239 	VteByteArray *outgoing;	/* pending input characters */
240 	VteConv outgoing_conv;
241 
242 	/* IConv buffer. */
243 	VteByteArray *conv_buffer;
244 
245 	/* Screen data.  We support the normal screen, and an alternate
246 	 * screen, which seems to be a DEC-specific feature. */
247 	struct _VteScreen {
248 		VteRing row_data[1];	/* buffer contents */
249 		long scroll_delta;	/* scroll offset */
250 		long insert_delta;	/* insertion offset */
251 
252                 /* Stuff saved along with the cursor */
253                 struct {
254                         VteVisualPosition cursor;
255                         gboolean reverse_mode;
256                         gboolean origin_mode;
257                         gboolean sendrecv_mode;
258                         gboolean insert_mode;
259                         gboolean linefeed_mode;
260                         VteCell defaults;
261                         VteCell color_defaults;
262                         VteCell fill_defaults;
263                         VteCharacterReplacement character_replacements[2];
264                         VteCharacterReplacement *character_replacement;
265                 } saved;
266 	} normal_screen, alternate_screen, *screen;
267 
268         /* Values we save along with the cursor */
269         VteVisualPosition cursor;	/* relative to the insertion delta */
270         gboolean reverse_mode;	/* reverse mode */
271         gboolean origin_mode;	/* origin mode */
272         gboolean sendrecv_mode;	/* sendrecv mode */
273         gboolean insert_mode;	/* insert mode */
274         gboolean linefeed_mode;	/* linefeed mode */
275         VteCell defaults;	/* default characteristics
276                                    for insertion of any new
277                                    characters */
278         VteCell color_defaults;	/* original defaults
279                                    plus the current
280                                    fore/back */
281         VteCell fill_defaults;	/* original defaults
282                                    plus the current
283                                    fore/back with no
284                                    character data */
285         VteCharacterReplacement character_replacements[2];  /* charsets in the G0 and G1 slots */
286         VteCharacterReplacement *character_replacement;     /* pointer to the active one */
287 
288 
289         /* Word chars */
290         char *word_char_exceptions_string;
291         gunichar *word_char_exceptions;
292         gsize word_char_exceptions_len;
293 
294 	/* Selection information. */
295 	gboolean has_selection;
296 	gboolean selecting;
297 	gboolean selecting_after_threshold;
298 	gboolean selecting_restart;
299 	gboolean selecting_had_delta;
300 	gboolean selection_block_mode;
301 	char *selection;
302 	enum vte_selection_type {
303 		selection_type_char,
304 		selection_type_word,
305 		selection_type_line
306 	} selection_type;
307 	struct selection_event_coords {
308 		long x, y;
309 	} selection_origin, selection_last;
310 	VteVisualPosition selection_start, selection_end;
311 
312 	/* Miscellaneous options. */
313 	VteEraseBinding backspace_binding, delete_binding;
314 	gboolean meta_sends_escape;
315 	gboolean audible_bell;
316 	gboolean margin_bell;
317 	guint bell_margin;
318 	gboolean allow_bold;
319         gboolean deccolm_mode; /* DECCOLM allowed */
320 	GHashTable *tabstops;
321 	gboolean text_modified_flag;
322 	gboolean text_inserted_flag;
323 	gboolean text_deleted_flag;
324 	gboolean rewrap_on_resize;
325 	gboolean bracketed_paste_mode;
326 
327 	/* Scrolling options. */
328 	gboolean scroll_background;
329 	gboolean scroll_on_output;
330 	gboolean scroll_on_keystroke;
331 	gboolean alternate_screen_scroll;
332 	long scrollback_lines;
333 
334         /* Restricted scrolling */
335         struct vte_scrolling_region {
336                 int start, end;
337         } scrolling_region;     /* the region we scroll in */
338         gboolean scrolling_restricted;
339 
340 	/* Cursor shape, as set via API */
341 	VteCursorShape cursor_shape;
342         float cursor_aspect_ratio;
343 
344 	/* Cursor blinking, as set in dconf. */
345         VteCursorBlinkMode cursor_blink_mode;
346 	gboolean cursor_blink_state;
347 	guint cursor_blink_tag;           /* cursor blinking timeout ID */
348         gint cursor_blink_cycle;          /* gtk-cursor-blink-time / 2 */
349 	gint cursor_blink_timeout;        /* gtk-cursor-blink-timeout */
350         gboolean cursor_blinks;           /* whether the cursor is actually blinking */
351 	gint64 cursor_blink_time;         /* how long the cursor has been blinking yet */
352 	gboolean cursor_visible;
353 	gboolean has_focus;               /* is the terminal window focused */
354 
355         /* DECSCUSR cursor style (shape and blinking possibly overridden
356          * via escape sequence) */
357         VteCursorStyle cursor_style;
358 
359 	/* Input device options. */
360         gboolean input_enabled;
361 	time_t last_keypress_time;
362 
363 	int mouse_tracking_mode; /* this is of type MouseTrackingMode,
364 				    but we need to guarantee its type. */
365         guint mouse_pressed_buttons;      /* bits 0, 1, 2 resp. for buttons 1, 2, 3 */
366         guint mouse_handled_buttons;      /* similar bitmap for buttons we handled ourselves */
367 	long mouse_last_x, mouse_last_y;
368 	gboolean mouse_autohide;
369 	guint mouse_autoscroll_tag;
370 	gboolean mouse_xterm_extension;
371 	gboolean mouse_urxvt_extension;
372 	double mouse_smooth_scroll_delta;
373 
374 	/* State variables for handling match checks. */
375 	char *match_contents;
376 	GArray *match_attributes;
377 	GArray *match_regexes;
378 	char *match;
379 	int match_tag;
380 	VteVisualPosition match_start, match_end;
381 	gboolean show_match;
382 
383 	/* Search data. */
384 	GRegex *search_regex;
385         GRegexMatchFlags search_match_flags;
386 	gboolean search_wrap_around;
387 	GArray *search_attrs; /* Cache attrs */
388 
389 	/* Data used when rendering the text which does not require server
390 	 * resources and which can be kept after unrealizing. */
391         PangoFontDescription *unscaled_font_desc;
392 	PangoFontDescription *fontdesc;
393         gdouble font_scale;
394 	gboolean fontdirty;
395         glong char_ascent;
396         glong char_descent;
397         /* dimensions of character cells */
398         glong char_width;
399         glong char_height;
400 
401 	/* Data used when rendering the text which reflects server resources
402 	 * and data, which should be dropped when unrealizing and (re)created
403 	 * when realizing. */
404 	struct _vte_draw *draw;
405 
406 	VtePaletteColor palette[VTE_PALETTE_SIZE];
407 
408 	/* Mouse cursors. */
409 	gboolean mouse_cursor_visible;
410 	GdkCursor *mouse_default_cursor,
411 		  *mouse_mousing_cursor,
412 		  *mouse_inviso_cursor;
413 
414 	/* Input method support. */
415 	GtkIMContext *im_context;
416 	gboolean im_preedit_active;
417 	char *im_preedit;
418 	PangoAttrList *im_preedit_attrs;
419 	int im_preedit_cursor;
420 
421 	gboolean accessible_emit;
422 
423 	/* Adjustment updates pending. */
424 	gboolean adjustment_changed_pending;
425 	gboolean adjustment_value_changed_pending;
426 
427 	gboolean cursor_moved_pending;
428 	gboolean contents_changed_pending;
429 
430 	/* window name changes */
431         gchar *window_title;
432 	gchar *window_title_changed;
433         gchar *icon_title;
434 	gchar *icon_title_changed;
435         gchar *current_directory_uri;
436         gchar *current_directory_uri_changed;
437         gchar *current_file_uri;
438         gchar *current_file_uri_changed;
439 
440 	/* Background */
441         gdouble background_alpha;
442 
443 	/* Key modifiers. */
444 	GdkModifierType modifiers;
445 
446 	/* Obscured? state. */
447 	GdkVisibilityState visibility_state;
448 
449 	/* Font stuff. */
450 	gboolean has_fonts;
451 	glong line_thickness;
452 	glong underline_position;
453 	glong strikethrough_position;
454 
455         /* Style stuff */
456         GtkBorder padding;
457 
458         /* GtkScrollable impl */
459         GtkAdjustment *hadjustment; /* unused */
460         GtkAdjustment *vadjustment;
461         guint hscroll_policy : 1; /* unused */
462 
463         guint vscroll_policy : 1;
464 };
465 
466 struct _VteTerminalClassPrivate {
467         GtkStyleProvider *style_provider;
468 };
469 
470 VteRowData *_vte_terminal_ensure_row(VteTerminal *terminal);
471 void _vte_terminal_set_pointer_visible(VteTerminal *terminal, gboolean visible);
472 void _vte_invalidate_all(VteTerminal *terminal);
473 void _vte_invalidate_cells(VteTerminal *terminal,
474 			   glong column_start, gint column_count,
475 			   glong row_start, gint row_count);
476 void _vte_invalidate_cell(VteTerminal *terminal, glong col, glong row);
477 void _vte_invalidate_cursor_once(VteTerminal *terminal, gboolean periodic);
478 VteRowData * _vte_new_row_data(VteTerminal *terminal);
479 void _vte_terminal_adjust_adjustments(VteTerminal *terminal);
480 void _vte_terminal_queue_contents_changed(VteTerminal *terminal);
481 void _vte_terminal_emit_text_deleted(VteTerminal *terminal);
482 void _vte_terminal_emit_text_inserted(VteTerminal *terminal);
483 void _vte_terminal_cursor_down (VteTerminal *terminal);
484 void _vte_terminal_drop_scrollback (VteTerminal *terminal);
485 void _vte_terminal_restore_cursor (VteTerminal *terminal, VteScreen *screen);
486 void _vte_terminal_save_cursor (VteTerminal *terminal, VteScreen *screen);
487 gboolean _vte_terminal_insert_char(VteTerminal *terminal, gunichar c,
488 			       gboolean force_insert_mode,
489 			       gboolean invalidate_cells);
490 void _vte_terminal_scroll_region(VteTerminal *terminal,
491 				 long row, glong count, glong delta);
492 void _vte_terminal_set_default_attributes(VteTerminal *terminal);
493 void _vte_terminal_clear_tabstop(VteTerminal *terminal, int column);
494 gboolean _vte_terminal_get_tabstop(VteTerminal *terminal, int column);
495 void _vte_terminal_set_tabstop(VteTerminal *terminal, int column);
496 void _vte_terminal_update_insert_delta(VteTerminal *terminal);
497 void _vte_terminal_cleanup_fragments(VteTerminal *terminal, long start, long end);
498 void _vte_terminal_audible_beep(VteTerminal *terminal);
499 void _vte_terminal_beep(VteTerminal *terminal);
500 PangoColor *_vte_terminal_get_color(const VteTerminal *terminal, int idx);
501 void _vte_terminal_set_color_internal(VteTerminal *terminal,
502                                       int idx,
503                                       int source,
504                                       const PangoColor *color);
505 
506 void _vte_terminal_inline_error_message(VteTerminal *terminal, const char *format, ...) G_GNUC_PRINTF(2,3);
507 
508 VteRowData *_vte_terminal_ring_insert (VteTerminal *terminal, glong position, gboolean fill);
509 VteRowData *_vte_terminal_ring_append (VteTerminal *terminal, gboolean fill);
510 void _vte_terminal_ring_remove (VteTerminal *terminal, glong position);
511 
512 void _vte_terminal_set_cursor_style(VteTerminal *terminal, VteCursorStyle style);
513 
514 /* vteseq.c: */
515 void _vte_terminal_handle_sequence(VteTerminal *terminal,
516 				   const char *match,
517 				   GValueArray *params);
518 
519 gboolean _vte_terminal_xy_to_grid(VteTerminal *terminal,
520                                   long x,
521                                   long y,
522                                   long *col,
523                                   long *row);
524 gboolean _vte_terminal_size_to_grid_size(VteTerminal *terminal,
525                                          long w,
526                                          long h,
527                                          long *cols,
528                                          long *rows);
529 
530 gboolean _vte_terminal_is_word_char(VteTerminal *terminal, gunichar c);
531 
532 G_END_DECLS
533 
534 #endif
535