374330e2 |
1 | #include <windows.h> |
2 | |
3 | #include <stdio.h> |
4 | #include <stdlib.h> |
5 | |
6 | #include "putty.h" |
7 | |
c9def1b8 |
8 | #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */ |
e14a5a13 |
9 | #define CL_VT100 0x0002 /* VT100 */ |
10 | #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */ |
11 | #define CL_VT102 0x0008 /* VT102 */ |
12 | #define CL_VT220 0x0010 /* VT220 */ |
13 | #define CL_VT320 0x0020 /* VT320 */ |
14 | #define CL_VT420 0x0040 /* VT420 */ |
15 | #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */ |
16 | #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */ |
c9def1b8 |
17 | #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */ |
18 | #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */ |
19 | #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */ |
e14a5a13 |
20 | |
21 | #define TM_ANSIMIN (CL_ANSIMIN) |
c9def1b8 |
22 | #define TM_VT100 (CL_ANSIMIN|CL_VT100) |
23 | #define TM_VT100AVO (TM_VT100|CL_VT100AVO) |
24 | #define TM_VT102 (TM_VT100AVO|CL_VT102) |
25 | #define TM_VT220 (TM_VT102|CL_VT220) |
26 | #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320) |
27 | #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI) |
28 | |
29 | #define TM_PUTTY (0xFFFF) |
e14a5a13 |
30 | |
31 | #define compatibility(x) \ |
32 | if ( ((CL_##x)&compatibility_level) == 0 ) { \ |
33 | termstate=TOPLEVEL; \ |
34 | break; \ |
35 | } |
c9def1b8 |
36 | #define compatibility2(x,y) \ |
37 | if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \ |
38 | termstate=TOPLEVEL; \ |
39 | break; \ |
40 | } |
e14a5a13 |
41 | |
42 | #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 ) |
43 | |
44 | static int compatibility_level = TM_PUTTY; |
45 | |
46 | |
374330e2 |
47 | static unsigned long *text; /* buffer of text on terminal screen */ |
48 | static unsigned long *scrtop; /* top of working screen */ |
49 | static unsigned long *disptop; /* top of displayed screen */ |
50 | static unsigned long *sbtop; /* top of scrollback */ |
c9def1b8 |
51 | static unsigned long *sbbot; /* furthest extent of scrollback */ |
374330e2 |
52 | static unsigned long *cpos; /* cursor position (convenience) */ |
53 | static unsigned long *disptext; /* buffer of text on real screen */ |
54 | static unsigned long *wanttext; /* buffer of text we want on screen */ |
55 | static unsigned long *alttext; /* buffer of text on alt. screen */ |
56 | |
57 | static unsigned char *selspace; /* buffer for building selections in */ |
58 | |
59 | #define TSIZE (sizeof(*text)) |
60 | #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0) |
61 | |
62 | static unsigned long curr_attr, save_attr; |
e14a5a13 |
63 | static unsigned long erase_char = ERASE_CHAR; |
374330e2 |
64 | |
65 | static int curs_x, curs_y; /* cursor */ |
66 | static int save_x, save_y; /* saved cursor position */ |
67 | static int marg_t, marg_b; /* scroll margins */ |
68 | static int dec_om; /* DEC origin mode flag */ |
69 | static int wrap, wrapnext; /* wrap flags */ |
70 | static int insert; /* insert-mode flag */ |
71 | static int cset; /* 0 or 1: which char set */ |
72 | static int save_cset, save_csattr; /* saved with cursor position */ |
73 | static int rvideo; /* global reverse video flag */ |
e14a5a13 |
74 | static int cursor_on; /* cursor enabled flag */ |
75 | static int reset_132; /* Flag ESC c resets to 80 cols */ |
76 | static int use_bce; /* Use Background coloured erase */ |
77 | static int blinker; /* When blinking is the cursor on ? */ |
c9def1b8 |
78 | static int tblinker; /* When the blinking text is on */ |
79 | static int blink_is_real; /* Actually blink blinking text */ |
374330e2 |
80 | |
81 | static unsigned long cset_attr[2]; |
82 | |
83 | /* |
84 | * Saved settings on the alternate screen. |
85 | */ |
86 | static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset; |
87 | static int alt_t, alt_b; |
88 | static int alt_which; |
89 | |
90 | #define ARGS_MAX 32 /* max # of esc sequence arguments */ |
064916ea |
91 | #define ARG_DEFAULT 0 /* if an arg isn't specified */ |
374330e2 |
92 | #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) ) |
93 | static int esc_args[ARGS_MAX]; |
94 | static int esc_nargs; |
95 | static int esc_query; |
8b811b12 |
96 | #define ANSI(x,y) ((x)+((y)<<8)) |
97 | #define ANSI_QUE(x) ANSI(x,TRUE) |
374330e2 |
98 | |
99 | #define OSC_STR_MAX 2048 |
100 | static int osc_strlen; |
101 | static char osc_string[OSC_STR_MAX+1]; |
102 | static int osc_w; |
103 | |
c9def1b8 |
104 | static char id_string[1024] = "\033[?6c"; |
105 | |
374330e2 |
106 | static unsigned char *tabs; |
107 | |
374330e2 |
108 | static enum { |
cabfd08c |
109 | TOPLEVEL, |
110 | SEEN_ESC, |
111 | SEEN_CSI, |
112 | SEEN_OSC, |
113 | SEEN_OSC_W, |
114 | |
115 | DO_CTRLS, |
116 | |
117 | IGNORE_NEXT, |
118 | SET_GL, SET_GR, |
119 | SEEN_OSC_P, |
120 | OSC_STRING, OSC_MAYBE_ST, |
e14a5a13 |
121 | SEEN_ESCHASH, |
122 | VT52_ESC, |
123 | VT52_Y1, |
124 | VT52_Y2 |
374330e2 |
125 | } termstate; |
126 | |
127 | static enum { |
128 | NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED |
129 | } selstate; |
130 | static enum { |
131 | SM_CHAR, SM_WORD, SM_LINE |
132 | } selmode; |
133 | static unsigned long *selstart, *selend, *selanchor; |
134 | |
135 | static short wordness[256] = { |
136 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */ |
137 | 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */ |
138 | 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */ |
139 | 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */ |
140 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */ |
141 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */ |
142 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */ |
143 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */ |
144 | }; |
145 | |
146 | static unsigned char sel_nl[] = SEL_NL; |
c9def1b8 |
147 | static char * paste_buffer = 0; |
148 | static int paste_len, paste_pos, paste_hold; |
374330e2 |
149 | |
150 | /* |
151 | * Internal prototypes. |
152 | */ |
153 | static void do_paint (Context, int); |
154 | static void erase_lots (int, int, int); |
155 | static void swap_screen (int); |
156 | static void update_sbar (void); |
157 | static void deselect (void); |
158 | |
159 | /* |
160 | * Set up power-on settings for the terminal. |
161 | */ |
162 | static void power_on(void) { |
163 | curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0; |
164 | alt_t = marg_t = 0; |
165 | if (rows != -1) |
166 | alt_b = marg_b = rows - 1; |
167 | else |
168 | alt_b = marg_b = 0; |
169 | if (cols != -1) { |
170 | int i; |
171 | for (i = 0; i < cols; i++) |
172 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
173 | } |
174 | alt_om = dec_om = cfg.dec_om; |
175 | alt_wnext = wrapnext = alt_ins = insert = FALSE; |
176 | alt_wrap = wrap = cfg.wrap_mode; |
177 | alt_cset = cset = 0; |
178 | cset_attr[0] = cset_attr[1] = ATTR_ASCII; |
179 | rvideo = 0; |
e14a5a13 |
180 | cursor_on = 1; |
374330e2 |
181 | save_attr = curr_attr = ATTR_DEFAULT; |
182 | app_cursor_keys = cfg.app_cursor; |
183 | app_keypad_keys = cfg.app_keypad; |
c9def1b8 |
184 | use_bce = cfg.bce; |
185 | blink_is_real = cfg.blinktext; |
e14a5a13 |
186 | erase_char = ERASE_CHAR; |
374330e2 |
187 | alt_which = 0; |
188 | { |
189 | int i; |
190 | for (i = 0; i < 256; i++) |
191 | wordness[i] = cfg.wordness[i]; |
192 | } |
193 | if (text) { |
194 | swap_screen (1); |
195 | erase_lots (FALSE, TRUE, TRUE); |
196 | swap_screen (0); |
197 | erase_lots (FALSE, TRUE, TRUE); |
198 | } |
199 | } |
200 | |
201 | /* |
202 | * Force a screen update. |
203 | */ |
204 | void term_update(void) { |
205 | Context ctx; |
206 | ctx = get_ctx(); |
207 | if (ctx) { |
c9def1b8 |
208 | if ( (seen_key_event && (cfg.scroll_on_key)) || |
209 | (seen_disp_event && (!cfg.scroll_on_key)) ) { |
cabfd08c |
210 | disptop = scrtop; |
211 | seen_disp_event = seen_key_event = 0; |
212 | } |
374330e2 |
213 | do_paint (ctx, TRUE); |
ec8679e9 |
214 | sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1)); |
374330e2 |
215 | free_ctx (ctx); |
374330e2 |
216 | } |
217 | } |
218 | |
219 | /* |
220 | * Same as power_on(), but an external function. |
221 | */ |
222 | void term_pwron(void) { |
223 | power_on(); |
224 | fix_cpos; |
225 | disptop = scrtop; |
226 | deselect(); |
227 | term_update(); |
228 | } |
229 | |
230 | /* |
231 | * Clear the scrollback. |
232 | */ |
233 | void term_clrsb(void) { |
234 | disptop = sbtop = scrtop; |
235 | update_sbar(); |
236 | } |
237 | |
238 | /* |
239 | * Initialise the terminal. |
240 | */ |
241 | void term_init(void) { |
c9def1b8 |
242 | text = sbtop = sbbot = scrtop = disptop = cpos = NULL; |
374330e2 |
243 | disptext = wanttext = NULL; |
244 | tabs = NULL; |
245 | selspace = NULL; |
246 | deselect(); |
247 | rows = cols = -1; |
374330e2 |
248 | power_on(); |
249 | } |
250 | |
251 | /* |
252 | * Set up the terminal for a given size. |
253 | */ |
254 | void term_size(int newrows, int newcols, int newsavelines) { |
255 | unsigned long *newtext, *newdisp, *newwant, *newalt; |
256 | int i, j, crows, ccols; |
257 | |
d6832394 |
258 | int save_alt_which = alt_which; |
259 | |
374330e2 |
260 | if (newrows == rows && newcols == cols && newsavelines == savelines) |
261 | return; /* nothing to do */ |
262 | |
d6832394 |
263 | deselect(); |
264 | swap_screen(0); |
265 | |
374330e2 |
266 | alt_t = marg_t = 0; |
267 | alt_b = marg_b = newrows - 1; |
268 | |
269 | newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE); |
c9def1b8 |
270 | sbbot = newtext + newsavelines*(newcols+1); |
374330e2 |
271 | for (i=0; i<(newrows+newsavelines)*(newcols+1); i++) |
c9def1b8 |
272 | newtext[i] = erase_char; |
374330e2 |
273 | if (rows != -1) { |
274 | crows = rows + (scrtop - sbtop) / (cols+1); |
275 | if (crows > newrows+newsavelines) |
276 | crows = newrows+newsavelines; |
c9def1b8 |
277 | if (newrows>crows) |
278 | disptop = newtext; |
279 | else |
280 | disptop = newtext + (crows-newrows)*(newcols+1); |
374330e2 |
281 | ccols = (cols < newcols ? cols : newcols); |
282 | for (i=0; i<crows; i++) { |
c9def1b8 |
283 | int oldidx = (rows - crows + i) * (cols+1); |
284 | int newidx = (newrows - crows + i) * (newcols+1); |
285 | |
374330e2 |
286 | for (j=0; j<ccols; j++) |
c9def1b8 |
287 | disptop[newidx+j] = scrtop[oldidx+j]; |
288 | disptop[newidx+newcols] = |
289 | (cols == newcols ? scrtop[oldidx+cols] |
290 | : (scrtop[oldidx+cols]&LATTR_MODE)); |
374330e2 |
291 | } |
c9def1b8 |
292 | sbtop = newtext; |
293 | } else { |
294 | sbtop = disptop = newtext; |
295 | } |
374330e2 |
296 | scrtop = disptop; |
297 | sfree (text); |
298 | text = newtext; |
299 | |
300 | newdisp = smalloc (newrows*(newcols+1)*TSIZE); |
301 | for (i=0; i<newrows*(newcols+1); i++) |
302 | newdisp[i] = ATTR_INVALID; |
303 | sfree (disptext); |
304 | disptext = newdisp; |
305 | |
306 | newwant = smalloc (newrows*(newcols+1)*TSIZE); |
307 | for (i=0; i<newrows*(newcols+1); i++) |
308 | newwant[i] = ATTR_INVALID; |
309 | sfree (wanttext); |
310 | wanttext = newwant; |
311 | |
312 | newalt = smalloc (newrows*(newcols+1)*TSIZE); |
313 | for (i=0; i<newrows*(newcols+1); i++) |
e14a5a13 |
314 | newalt[i] = erase_char; |
374330e2 |
315 | sfree (alttext); |
316 | alttext = newalt; |
317 | |
318 | sfree (selspace); |
319 | selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) ); |
320 | |
321 | tabs = srealloc (tabs, newcols*sizeof(*tabs)); |
322 | { |
323 | int i; |
324 | for (i = (cols > 0 ? cols : 0); i < newcols; i++) |
325 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
326 | } |
327 | |
328 | if (rows > 0) |
329 | curs_y += newrows - rows; |
330 | if (curs_y < 0) |
331 | curs_y = 0; |
332 | if (curs_y >= newrows) |
333 | curs_y = newrows-1; |
334 | if (curs_x >= newcols) |
335 | curs_x = newcols-1; |
336 | alt_x = alt_y = 0; |
337 | wrapnext = alt_wnext = FALSE; |
338 | |
339 | rows = newrows; |
340 | cols = newcols; |
341 | savelines = newsavelines; |
342 | fix_cpos; |
343 | |
d6832394 |
344 | swap_screen(save_alt_which); |
345 | |
374330e2 |
346 | update_sbar(); |
347 | term_update(); |
348 | } |
349 | |
350 | /* |
351 | * Swap screens. |
352 | */ |
353 | static void swap_screen (int which) { |
354 | int t; |
355 | unsigned long tt; |
356 | |
357 | if (which == alt_which) |
358 | return; |
359 | |
360 | alt_which = which; |
361 | |
362 | for (t=0; t<rows*(cols+1); t++) { |
363 | tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt; |
364 | } |
365 | |
366 | t = curs_x; curs_x = alt_x; alt_x = t; |
367 | t = curs_y; curs_y = alt_y; alt_y = t; |
368 | t = marg_t; marg_t = alt_t; alt_t = t; |
369 | t = marg_b; marg_b = alt_b; alt_b = t; |
370 | t = dec_om; dec_om = alt_om; alt_om = t; |
371 | t = wrap; wrap = alt_wrap; alt_wrap = t; |
372 | t = wrapnext; wrapnext = alt_wnext; alt_wnext = t; |
373 | t = insert; insert = alt_ins; alt_ins = t; |
374 | t = cset; cset = alt_cset; alt_cset = t; |
375 | |
376 | fix_cpos; |
377 | } |
378 | |
379 | /* |
374330e2 |
380 | * Update the scroll bar. |
381 | */ |
382 | static void update_sbar(void) { |
383 | int min; |
384 | |
385 | min = (sbtop - text) / (cols+1); |
386 | set_sbar ((scrtop - text) / (cols+1) + rows - min, |
387 | (disptop - text) / (cols+1) - min, |
388 | rows); |
389 | } |
390 | |
391 | /* |
392 | * Check whether the region bounded by the two pointers intersects |
393 | * the scroll region, and de-select the on-screen selection if so. |
394 | */ |
395 | static void check_selection (unsigned long *from, unsigned long *to) { |
396 | if (from < selend && selstart < to) |
397 | deselect(); |
398 | } |
399 | |
400 | /* |
401 | * Scroll the screen. (`lines' is +ve for scrolling forward, -ve |
402 | * for backward.) `sb' is TRUE if the scrolling is permitted to |
403 | * affect the scrollback buffer. |
404 | */ |
405 | static void scroll (int topline, int botline, int lines, int sb) { |
406 | unsigned long *scroll_top; |
c9def1b8 |
407 | unsigned long *newscr; |
374330e2 |
408 | int scroll_size, size, i; |
c9def1b8 |
409 | int scrtop_is_disptop = (scrtop==disptop); |
410 | static int recursive = 0; |
411 | |
412 | /* Only scroll more than the window if we're doing a 10% scroll */ |
413 | if (!recursive && lines > botline - topline + 1) |
414 | lines = botline - topline + 1; |
374330e2 |
415 | |
416 | scroll_top = scrtop + topline*(cols+1); |
417 | size = (lines < 0 ? -lines : lines) * (cols+1); |
418 | scroll_size = (botline - topline + 1) * (cols+1) - size; |
419 | |
cabfd08c |
420 | if (lines > 0 && topline == 0 && alt_which == 0 && sb) { |
374330e2 |
421 | /* |
cabfd08c |
422 | * Since we're going to scroll the top line and we're on the |
c9def1b8 |
423 | * scrolling screen let's also effect the scrollback buffer. |
424 | * |
425 | * This is normally done by moving the position the screen |
426 | * painter reads from to reduce the amount of memory copying |
427 | * required. |
374330e2 |
428 | */ |
c9def1b8 |
429 | if (scroll_size >= 0 && !recursive) { |
430 | newscr = scrtop + lines * (cols+1); |
431 | |
432 | if (newscr > sbbot && botline == rows-1) { |
433 | /* We've hit the bottom of memory, so we have to do a |
434 | * physical scroll. But instead of just 1 line do it |
435 | * by 10% of the available memory. |
436 | * |
437 | * If the scroll region isn't the whole screen then we can't |
438 | * do this as it stands. We would need to recover the bottom |
439 | * of the screen from the scroll buffer after being sure that |
440 | * it doesn't get deleted. |
441 | */ |
442 | |
443 | i = (rows+savelines)/10; |
444 | |
445 | /* Make it simple and ensure safe recursion */ |
446 | if ( i<savelines-1) { |
447 | recursive ++; |
448 | scroll(topline, botline, i, sb); |
449 | recursive --; |
450 | |
451 | newscr = scrtop - i * (cols+1); |
452 | if (scrtop_is_disptop) disptop = newscr; |
453 | scrtop = newscr; |
454 | } |
455 | |
456 | newscr = scrtop + lines * (cols+1); |
457 | } |
458 | |
459 | if (newscr <= sbbot) { |
460 | if (scrtop_is_disptop) disptop = newscr; |
461 | scrtop = newscr; |
462 | |
463 | if (botline == rows-1 ) |
464 | for (i = 0; i < size; i++) |
465 | scrtop[i+scroll_size] = erase_char; |
466 | |
467 | update_sbar(); |
468 | fix_cpos; |
469 | |
470 | if (botline != rows-1) { |
471 | /* This fastscroll only works for full window scrolls. |
472 | * If the caller wanted a partial one we have to reverse |
473 | * scroll the bottom of the screen. |
474 | */ |
475 | scroll(botline-lines+1, rows-1, -lines, 0); |
476 | } |
477 | return ; |
478 | } |
479 | |
480 | /* If we can't scroll by memory remapping do it physically. |
481 | * But rather than expensivly doing the scroll buffer just |
482 | * scroll the screen. All it means is that sometimes we choose |
483 | * to not add lines from a scroll region to the scroll buffer. |
484 | */ |
485 | |
486 | if (savelines <= 400) { |
487 | sbtop -= lines * (cols+1); |
488 | if (sbtop < text) |
489 | sbtop = text; |
490 | scroll_size += scroll_top - sbtop; |
491 | scroll_top = sbtop; |
492 | |
493 | update_sbar(); |
494 | } |
495 | } else { |
496 | /* Ho hum, expensive scroll required. */ |
497 | |
498 | sbtop -= lines * (cols+1); |
499 | if (sbtop < text) |
500 | sbtop = text; |
501 | scroll_size += scroll_top - sbtop; |
502 | scroll_top = sbtop; |
503 | |
504 | update_sbar(); |
505 | } |
374330e2 |
506 | } |
507 | |
508 | if (scroll_size < 0) { |
509 | size += scroll_size; |
510 | scroll_size = 0; |
511 | } |
512 | |
513 | if (lines > 0) { |
514 | if (scroll_size) |
515 | memmove (scroll_top, scroll_top + size, scroll_size*TSIZE); |
516 | for (i = 0; i < size; i++) |
e14a5a13 |
517 | scroll_top[i+scroll_size] = erase_char; |
374330e2 |
518 | if (selstart > scroll_top && |
519 | selstart < scroll_top + size + scroll_size) { |
520 | selstart -= size; |
521 | if (selstart < scroll_top) |
522 | selstart = scroll_top; |
523 | } |
524 | if (selend > scroll_top && |
525 | selend < scroll_top + size + scroll_size) { |
526 | selend -= size; |
527 | if (selend < scroll_top) |
528 | selend = scroll_top; |
529 | } |
c9def1b8 |
530 | if (scrtop_is_disptop) |
531 | disptop = scrtop; |
532 | else |
533 | if (disptop > scroll_top && |
534 | disptop < scroll_top + size + scroll_size) { |
535 | disptop -= size; |
536 | if (disptop < scroll_top) |
537 | disptop = scroll_top; |
538 | } |
374330e2 |
539 | } else { |
540 | if (scroll_size) |
541 | memmove (scroll_top + size, scroll_top, scroll_size*TSIZE); |
542 | for (i = 0; i < size; i++) |
e14a5a13 |
543 | scroll_top[i] = erase_char; |
374330e2 |
544 | if (selstart > scroll_top && |
545 | selstart < scroll_top + size + scroll_size) { |
546 | selstart += size; |
547 | if (selstart > scroll_top + size + scroll_size) |
548 | selstart = scroll_top + size + scroll_size; |
549 | } |
550 | if (selend > scroll_top && |
551 | selend < scroll_top + size + scroll_size) { |
552 | selend += size; |
553 | if (selend > scroll_top + size + scroll_size) |
554 | selend = scroll_top + size + scroll_size; |
555 | } |
c9def1b8 |
556 | if (scrtop_is_disptop) |
557 | disptop = scrtop; |
558 | else if (disptop > scroll_top && |
559 | disptop < scroll_top + size + scroll_size) { |
560 | disptop += size; |
561 | if (disptop > scroll_top + size + scroll_size) |
562 | disptop = scroll_top + size + scroll_size; |
563 | } |
374330e2 |
564 | } |
374330e2 |
565 | } |
566 | |
567 | /* |
568 | * Move the cursor to a given position, clipping at boundaries. We |
569 | * may or may not want to clip at the scroll margin: marg_clip is 0 |
570 | * not to, 1 to disallow _passing_ the margins, and 2 to disallow |
571 | * even _being_ outside the margins. |
572 | */ |
573 | static void move (int x, int y, int marg_clip) { |
574 | if (x < 0) |
575 | x = 0; |
576 | if (x >= cols) |
577 | x = cols-1; |
578 | if (marg_clip) { |
579 | if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t) |
580 | y = marg_t; |
581 | if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b) |
582 | y = marg_b; |
583 | } |
584 | if (y < 0) |
585 | y = 0; |
586 | if (y >= rows) |
587 | y = rows-1; |
588 | curs_x = x; |
589 | curs_y = y; |
590 | fix_cpos; |
591 | wrapnext = FALSE; |
592 | } |
593 | |
594 | /* |
595 | * Save or restore the cursor and SGR mode. |
596 | */ |
597 | static void save_cursor(int save) { |
598 | if (save) { |
599 | save_x = curs_x; |
600 | save_y = curs_y; |
601 | save_attr = curr_attr; |
602 | save_cset = cset; |
603 | save_csattr = cset_attr[cset]; |
604 | } else { |
605 | curs_x = save_x; |
606 | curs_y = save_y; |
c9def1b8 |
607 | /* Make sure the window hasn't shrunk since the save */ |
608 | if (curs_x >= cols) curs_x = cols-1; |
609 | if (curs_y >= rows) curs_y = rows-1; |
610 | |
374330e2 |
611 | curr_attr = save_attr; |
612 | cset = save_cset; |
613 | cset_attr[cset] = save_csattr; |
614 | fix_cpos; |
e14a5a13 |
615 | if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
374330e2 |
616 | } |
617 | } |
618 | |
619 | /* |
620 | * Erase a large portion of the screen: the whole screen, or the |
621 | * whole line, or parts thereof. |
622 | */ |
623 | static void erase_lots (int line_only, int from_begin, int to_end) { |
624 | unsigned long *startpos, *endpos; |
625 | |
626 | if (line_only) { |
627 | startpos = cpos - curs_x; |
c9def1b8 |
628 | endpos = startpos + cols; |
629 | /* I've removed the +1 so that the Wide screen stuff is not |
630 | * removed when it shouldn't be. |
631 | */ |
374330e2 |
632 | } else { |
633 | startpos = scrtop; |
634 | endpos = startpos + rows * (cols+1); |
635 | } |
636 | if (!from_begin) |
637 | startpos = cpos; |
638 | if (!to_end) |
e14a5a13 |
639 | endpos = cpos+1; |
374330e2 |
640 | check_selection (startpos, endpos); |
e14a5a13 |
641 | |
642 | /* Clear screen also forces a full window redraw, just in case. */ |
643 | if (startpos == scrtop && endpos == scrtop + rows * (cols+1)) |
644 | term_invalidate(); |
645 | |
374330e2 |
646 | while (startpos < endpos) |
e14a5a13 |
647 | *startpos++ = erase_char; |
374330e2 |
648 | } |
649 | |
650 | /* |
651 | * Insert or delete characters within the current line. n is +ve if |
652 | * insertion is desired, and -ve for deletion. |
653 | */ |
654 | static void insch (int n) { |
655 | int dir = (n < 0 ? -1 : +1); |
656 | int m; |
657 | |
658 | n = (n < 0 ? -n : n); |
659 | if (n > cols - curs_x) |
660 | n = cols - curs_x; |
661 | m = cols - curs_x - n; |
662 | check_selection (cpos, cpos+n); |
663 | if (dir < 0) { |
664 | memmove (cpos, cpos+n, m*TSIZE); |
665 | while (n--) |
e14a5a13 |
666 | cpos[m++] = erase_char; |
374330e2 |
667 | } else { |
668 | memmove (cpos+n, cpos, m*TSIZE); |
669 | while (n--) |
e14a5a13 |
670 | cpos[n] = erase_char; |
374330e2 |
671 | } |
672 | } |
673 | |
674 | /* |
675 | * Toggle terminal mode `mode' to state `state'. (`query' indicates |
676 | * whether the mode is a DEC private one or a normal one.) |
677 | */ |
678 | static void toggle_mode (int mode, int query, int state) { |
679 | if (query) switch (mode) { |
680 | case 1: /* application cursor keys */ |
681 | app_cursor_keys = state; |
682 | break; |
e14a5a13 |
683 | case 2: /* VT52 mode */ |
684 | vt52_mode = !state; |
685 | break; |
374330e2 |
686 | case 3: /* 80/132 columns */ |
687 | deselect(); |
e14a5a13 |
688 | request_resize (state ? 132 : 80, rows, 1); |
689 | reset_132 = state; |
374330e2 |
690 | break; |
691 | case 5: /* reverse video */ |
692 | rvideo = state; |
cabfd08c |
693 | seen_disp_event = TRUE; |
e14a5a13 |
694 | if (state) term_update(); |
374330e2 |
695 | break; |
696 | case 6: /* DEC origin mode */ |
697 | dec_om = state; |
698 | break; |
699 | case 7: /* auto wrap */ |
700 | wrap = state; |
701 | break; |
c9def1b8 |
702 | case 8: /* auto key repeat */ |
703 | repeat_off = !state; |
704 | break; |
e14a5a13 |
705 | case 25: /* enable/disable cursor */ |
706 | compatibility(VT220); |
707 | cursor_on = state; |
708 | seen_disp_event = TRUE; |
709 | break; |
374330e2 |
710 | case 47: /* alternate screen */ |
e14a5a13 |
711 | compatibility(OTHER); |
374330e2 |
712 | deselect(); |
713 | swap_screen (state); |
714 | disptop = scrtop; |
715 | break; |
716 | } else switch (mode) { |
717 | case 4: /* set insert mode */ |
e14a5a13 |
718 | compatibility(VT102); |
374330e2 |
719 | insert = state; |
720 | break; |
e14a5a13 |
721 | case 12: /* set echo mode */ |
722 | /* |
723 | * This may be very good in smcup and rmcup (or smkx & rmkx) if you |
724 | * have a long RTT and the telnet client/daemon doesn't understand |
725 | * linemode. |
726 | * |
727 | * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the |
728 | * tty and _really_ confuses some programs. |
729 | */ |
730 | compatibility(VT220); |
731 | ldisc = (state? &ldisc_simple : &ldisc_term); |
732 | break; |
c9def1b8 |
733 | case 20: /* Return sends ... */ |
734 | cr_lf_return = state; |
735 | break; |
374330e2 |
736 | } |
737 | } |
738 | |
739 | /* |
740 | * Process an OSC sequence: set window title or icon name. |
741 | */ |
742 | static void do_osc(void) { |
743 | if (osc_w) { |
744 | while (osc_strlen--) |
745 | wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0]; |
746 | } else { |
747 | osc_string[osc_strlen] = '\0'; |
748 | switch (esc_args[0]) { |
749 | case 0: |
750 | case 1: |
751 | set_icon (osc_string); |
752 | if (esc_args[0] == 1) |
753 | break; |
754 | /* fall through: parameter 0 means set both */ |
755 | case 2: |
756 | case 21: |
757 | set_title (osc_string); |
758 | break; |
759 | } |
760 | } |
761 | } |
762 | |
763 | /* |
764 | * Remove everything currently in `inbuf' and stick it up on the |
765 | * in-memory display. There's a big state machine in here to |
766 | * process escape sequences... |
767 | */ |
768 | void term_out(void) { |
c9def1b8 |
769 | int c, inbuf_reap; |
770 | |
771 | static int beep_overload = 0; |
772 | int beep_count = 0; |
773 | |
774 | for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++) |
775 | { |
776 | c = inbuf[inbuf_reap]; |
374330e2 |
777 | |
c9def1b8 |
778 | /* |
5fd04f07 |
779 | * Optionally log the session traffic to a file. Useful for |
780 | * debugging and possibly also useful for actual logging. |
781 | */ |
782 | if (logfile) { |
374330e2 |
783 | static FILE *fp = NULL; |
5fd04f07 |
784 | if (!fp) fp = fopen(logfile, "wb"); |
374330e2 |
785 | if (fp) fputc (c, fp); |
786 | } |
e14a5a13 |
787 | /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even |
788 | * be able to display 8-bit characters, but I'll let that go 'cause |
789 | * of i18n. |
790 | */ |
c9def1b8 |
791 | if( ( (c&0x60) == 0 || c == '\177') && |
792 | termstate < DO_CTRLS && |
e14a5a13 |
793 | ( (c&0x80) == 0 || has_compat(VT220))) { |
374330e2 |
794 | switch (c) { |
795 | case '\005': /* terminal type query */ |
e14a5a13 |
796 | /* Strictly speaking this is VT100 but a VT100 defaults to |
797 | * no response. Other terminals respond at their option. |
798 | * |
799 | * Don't put a CR in the default string as this tends to |
800 | * upset some weird software. |
801 | * |
802 | * An xterm returns "xterm" (5 characters) |
803 | */ |
804 | compatibility(OTHER); |
805 | ldisc->send ("PuTTY", 5); |
374330e2 |
806 | break; |
807 | case '\007': |
c9def1b8 |
808 | beep_count++; |
809 | if(beep_count>6) beep_overload=1; |
374330e2 |
810 | disptop = scrtop; |
374330e2 |
811 | break; |
812 | case '\b': |
c9def1b8 |
813 | if (curs_x == 0 && curs_y == 0) |
814 | ; |
815 | else if (curs_x == 0 && curs_y > 0) |
374330e2 |
816 | curs_x = cols-1, curs_y--; |
817 | else if (wrapnext) |
818 | wrapnext = FALSE; |
819 | else |
820 | curs_x--; |
821 | fix_cpos; |
cabfd08c |
822 | seen_disp_event = TRUE; |
374330e2 |
823 | break; |
824 | case '\016': |
e14a5a13 |
825 | compatibility(VT100); |
374330e2 |
826 | cset = 1; |
827 | break; |
828 | case '\017': |
e14a5a13 |
829 | compatibility(VT100); |
374330e2 |
830 | cset = 0; |
831 | break; |
832 | case '\033': |
e14a5a13 |
833 | if (vt52_mode) |
834 | termstate = VT52_ESC; |
835 | else { |
836 | compatibility(ANSIMIN); |
837 | termstate = SEEN_ESC; |
838 | } |
374330e2 |
839 | break; |
840 | case 0233: |
e14a5a13 |
841 | compatibility(VT220); |
374330e2 |
842 | termstate = SEEN_CSI; |
843 | esc_nargs = 1; |
844 | esc_args[0] = ARG_DEFAULT; |
845 | esc_query = FALSE; |
846 | break; |
847 | case 0235: |
e14a5a13 |
848 | compatibility(VT220); |
374330e2 |
849 | termstate = SEEN_OSC; |
850 | esc_args[0] = 0; |
851 | break; |
852 | case '\r': |
853 | curs_x = 0; |
854 | wrapnext = FALSE; |
855 | fix_cpos; |
cabfd08c |
856 | seen_disp_event = TRUE; |
c9def1b8 |
857 | paste_hold = 0; |
374330e2 |
858 | break; |
859 | case '\013': |
860 | case '\014': |
e14a5a13 |
861 | compatibility(VT100); |
374330e2 |
862 | case '\n': |
863 | if (curs_y == marg_b) |
864 | scroll (marg_t, marg_b, 1, TRUE); |
865 | else if (curs_y < rows-1) |
866 | curs_y++; |
cabfd08c |
867 | if (cfg.lfhascr) |
868 | curs_x = 0; |
374330e2 |
869 | fix_cpos; |
870 | wrapnext = FALSE; |
cabfd08c |
871 | seen_disp_event = 1; |
c9def1b8 |
872 | paste_hold = 0; |
374330e2 |
873 | break; |
874 | case '\t': |
374330e2 |
875 | { |
876 | unsigned long *old_cpos = cpos; |
c9def1b8 |
877 | unsigned long *p = scrtop + curs_y * (cols+1) + cols; |
878 | |
879 | do { |
880 | curs_x++; |
881 | } while (curs_x < cols-1 && !tabs[curs_x]); |
882 | |
883 | if ((*p & LATTR_MODE) != LATTR_NORM) |
884 | { |
885 | if (curs_x >= cols/2) |
886 | curs_x = cols/2-1; |
887 | } |
888 | else |
889 | { |
890 | if (curs_x >= cols) |
891 | curs_x = cols-1; |
892 | } |
893 | |
374330e2 |
894 | fix_cpos; |
895 | check_selection (old_cpos, cpos); |
896 | } |
cabfd08c |
897 | seen_disp_event = TRUE; |
374330e2 |
898 | break; |
cabfd08c |
899 | } |
900 | } |
901 | else switch (termstate) { |
902 | case TOPLEVEL: |
8b811b12 |
903 | /* Only graphic characters get this far, ctrls are stripped above */ |
904 | if (wrapnext) { |
c9def1b8 |
905 | cpos[1] |= ATTR_WRAPPED; |
8b811b12 |
906 | if (curs_y == marg_b) |
907 | scroll (marg_t, marg_b, 1, TRUE); |
908 | else if (curs_y < rows-1) |
909 | curs_y++; |
910 | curs_x = 0; |
911 | fix_cpos; |
912 | wrapnext = FALSE; |
8b811b12 |
913 | } |
914 | if (insert) |
915 | insch (1); |
e14a5a13 |
916 | if (selstate != NO_SELECTION) |
917 | check_selection (cpos, cpos+1); |
918 | switch (cset_attr[cset]) { |
919 | /* Linedraw characters are different from 'ESC ( B' only |
920 | * for a small range, for ones outside that range make sure |
921 | * we use the same font as well as the same encoding. |
922 | */ |
923 | case ATTR_LINEDRW: |
924 | if (c<0x60 || c>0x7F) |
925 | *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr | |
926 | ATTR_ASCII; |
927 | else |
928 | *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW; |
929 | break; |
930 | default: |
931 | *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr | |
932 | (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII); |
933 | break; |
934 | } |
8b811b12 |
935 | curs_x++; |
936 | if (curs_x == cols) { |
937 | cpos--; |
938 | curs_x--; |
939 | wrapnext = wrap; |
374330e2 |
940 | } |
8b811b12 |
941 | seen_disp_event = 1; |
374330e2 |
942 | break; |
cabfd08c |
943 | |
374330e2 |
944 | case IGNORE_NEXT: |
945 | termstate = TOPLEVEL; |
946 | break; |
947 | case OSC_MAYBE_ST: |
948 | /* |
949 | * This state is virtually identical to SEEN_ESC, with the |
950 | * exception that we have an OSC sequence in the pipeline, |
951 | * and _if_ we see a backslash, we process it. |
952 | */ |
953 | if (c == '\\') { |
954 | do_osc(); |
955 | termstate = TOPLEVEL; |
956 | break; |
957 | } |
958 | /* else fall through */ |
959 | case SEEN_ESC: |
960 | termstate = TOPLEVEL; |
961 | switch (c) { |
374330e2 |
962 | case ' ': /* some weird sequence? */ |
e14a5a13 |
963 | compatibility(VT220); |
374330e2 |
964 | termstate = IGNORE_NEXT; |
965 | break; |
966 | case '[': /* enter CSI mode */ |
967 | termstate = SEEN_CSI; |
968 | esc_nargs = 1; |
969 | esc_args[0] = ARG_DEFAULT; |
970 | esc_query = FALSE; |
971 | break; |
972 | case ']': /* xterm escape sequences */ |
e14a5a13 |
973 | /* Compatibility is nasty here, xterm, linux, decterm yuk! */ |
974 | compatibility(OTHER); |
374330e2 |
975 | termstate = SEEN_OSC; |
976 | esc_args[0] = 0; |
977 | break; |
978 | case '(': /* should set GL */ |
e14a5a13 |
979 | compatibility(VT100); |
374330e2 |
980 | termstate = SET_GL; |
981 | break; |
982 | case ')': /* should set GR */ |
e14a5a13 |
983 | compatibility(VT100); |
374330e2 |
984 | termstate = SET_GR; |
985 | break; |
986 | case '7': /* save cursor */ |
e14a5a13 |
987 | compatibility(VT100); |
374330e2 |
988 | save_cursor (TRUE); |
989 | break; |
990 | case '8': /* restore cursor */ |
e14a5a13 |
991 | compatibility(VT100); |
374330e2 |
992 | save_cursor (FALSE); |
cabfd08c |
993 | seen_disp_event = TRUE; |
374330e2 |
994 | break; |
995 | case '=': |
e14a5a13 |
996 | compatibility(VT100); |
374330e2 |
997 | app_keypad_keys = TRUE; |
998 | break; |
999 | case '>': |
e14a5a13 |
1000 | compatibility(VT100); |
374330e2 |
1001 | app_keypad_keys = FALSE; |
1002 | break; |
1003 | case 'D': /* exactly equivalent to LF */ |
e14a5a13 |
1004 | compatibility(VT100); |
374330e2 |
1005 | if (curs_y == marg_b) |
1006 | scroll (marg_t, marg_b, 1, TRUE); |
1007 | else if (curs_y < rows-1) |
1008 | curs_y++; |
1009 | fix_cpos; |
1010 | wrapnext = FALSE; |
cabfd08c |
1011 | seen_disp_event = TRUE; |
374330e2 |
1012 | break; |
1013 | case 'E': /* exactly equivalent to CR-LF */ |
e14a5a13 |
1014 | compatibility(VT100); |
374330e2 |
1015 | curs_x = 0; |
374330e2 |
1016 | if (curs_y == marg_b) |
1017 | scroll (marg_t, marg_b, 1, TRUE); |
1018 | else if (curs_y < rows-1) |
1019 | curs_y++; |
1020 | fix_cpos; |
1021 | wrapnext = FALSE; |
cabfd08c |
1022 | seen_disp_event = TRUE; |
374330e2 |
1023 | break; |
1024 | case 'M': /* reverse index - backwards LF */ |
e14a5a13 |
1025 | compatibility(VT100); |
374330e2 |
1026 | if (curs_y == marg_t) |
1027 | scroll (marg_t, marg_b, -1, TRUE); |
1028 | else if (curs_y > 0) |
1029 | curs_y--; |
1030 | fix_cpos; |
1031 | wrapnext = FALSE; |
cabfd08c |
1032 | seen_disp_event = TRUE; |
374330e2 |
1033 | break; |
1034 | case 'Z': /* terminal type query */ |
e14a5a13 |
1035 | compatibility(VT100); |
c9def1b8 |
1036 | ldisc->send (id_string, strlen(id_string)); |
374330e2 |
1037 | break; |
1038 | case 'c': /* restore power-on settings */ |
e14a5a13 |
1039 | compatibility(VT100); |
374330e2 |
1040 | power_on(); |
e14a5a13 |
1041 | if (reset_132) { |
1042 | request_resize (80, rows, 1); |
1043 | reset_132 = 0; |
1044 | } |
374330e2 |
1045 | fix_cpos; |
1046 | disptop = scrtop; |
cabfd08c |
1047 | seen_disp_event = TRUE; |
374330e2 |
1048 | break; |
1049 | case '#': /* ESC # 8 fills screen with Es :-) */ |
e14a5a13 |
1050 | compatibility(VT100); |
374330e2 |
1051 | termstate = SEEN_ESCHASH; |
1052 | break; |
1053 | case 'H': /* set a tab */ |
e14a5a13 |
1054 | compatibility(VT100); |
374330e2 |
1055 | tabs[curs_x] = TRUE; |
1056 | break; |
1057 | } |
1058 | break; |
1059 | case SEEN_CSI: |
1060 | termstate = TOPLEVEL; /* default */ |
8b811b12 |
1061 | if( isdigit(c) ) |
1062 | { |
374330e2 |
1063 | if (esc_nargs <= ARGS_MAX) { |
1064 | if (esc_args[esc_nargs-1] == ARG_DEFAULT) |
1065 | esc_args[esc_nargs-1] = 0; |
1066 | esc_args[esc_nargs-1] = |
1067 | 10 * esc_args[esc_nargs-1] + c - '0'; |
1068 | } |
1069 | termstate = SEEN_CSI; |
8b811b12 |
1070 | } |
1071 | else if( c == ';' ) |
1072 | { |
374330e2 |
1073 | if (++esc_nargs <= ARGS_MAX) |
1074 | esc_args[esc_nargs-1] = ARG_DEFAULT; |
1075 | termstate = SEEN_CSI; |
8b811b12 |
1076 | } |
1077 | else if( c < '@' ) |
1078 | { |
1079 | if( esc_query ) esc_query = -1; |
1080 | else if( c == '?' ) esc_query = TRUE; |
1081 | else esc_query = c; |
374330e2 |
1082 | termstate = SEEN_CSI; |
8b811b12 |
1083 | } |
1084 | else switch (ANSI(c,esc_query)) { |
374330e2 |
1085 | case 'A': /* move up N lines */ |
1086 | move (curs_x, curs_y - def(esc_args[0], 1), 1); |
cabfd08c |
1087 | seen_disp_event = TRUE; |
374330e2 |
1088 | break; |
e14a5a13 |
1089 | case 'e': /* move down N lines */ |
1090 | compatibility(ANSI); |
1091 | case 'B': |
374330e2 |
1092 | move (curs_x, curs_y + def(esc_args[0], 1), 1); |
cabfd08c |
1093 | seen_disp_event = TRUE; |
374330e2 |
1094 | break; |
e14a5a13 |
1095 | case 'a': /* move right N cols */ |
1096 | compatibility(ANSI); |
1097 | case 'C': |
374330e2 |
1098 | move (curs_x + def(esc_args[0], 1), curs_y, 1); |
cabfd08c |
1099 | seen_disp_event = TRUE; |
374330e2 |
1100 | break; |
1101 | case 'D': /* move left N cols */ |
1102 | move (curs_x - def(esc_args[0], 1), curs_y, 1); |
cabfd08c |
1103 | seen_disp_event = TRUE; |
374330e2 |
1104 | break; |
1105 | case 'E': /* move down N lines and CR */ |
e14a5a13 |
1106 | compatibility(ANSI); |
374330e2 |
1107 | move (0, curs_y + def(esc_args[0], 1), 1); |
cabfd08c |
1108 | seen_disp_event = TRUE; |
374330e2 |
1109 | break; |
1110 | case 'F': /* move up N lines and CR */ |
e14a5a13 |
1111 | compatibility(ANSI); |
374330e2 |
1112 | move (0, curs_y - def(esc_args[0], 1), 1); |
cabfd08c |
1113 | seen_disp_event = TRUE; |
374330e2 |
1114 | break; |
1115 | case 'G': case '`': /* set horizontal posn */ |
e14a5a13 |
1116 | compatibility(ANSI); |
374330e2 |
1117 | move (def(esc_args[0], 1) - 1, curs_y, 0); |
cabfd08c |
1118 | seen_disp_event = TRUE; |
374330e2 |
1119 | break; |
1120 | case 'd': /* set vertical posn */ |
e14a5a13 |
1121 | compatibility(ANSI); |
374330e2 |
1122 | move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
1123 | (dec_om ? 2 : 0)); |
cabfd08c |
1124 | seen_disp_event = TRUE; |
374330e2 |
1125 | break; |
1126 | case 'H': case 'f': /* set horz and vert posns at once */ |
1127 | if (esc_nargs < 2) |
1128 | esc_args[1] = ARG_DEFAULT; |
1129 | move (def(esc_args[1], 1) - 1, |
1130 | (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
1131 | (dec_om ? 2 : 0)); |
cabfd08c |
1132 | seen_disp_event = TRUE; |
374330e2 |
1133 | break; |
1134 | case 'J': /* erase screen or parts of it */ |
1135 | { |
1136 | unsigned int i = def(esc_args[0], 0) + 1; |
1137 | if (i > 3) |
1138 | i = 0; |
1139 | erase_lots(FALSE, !!(i & 2), !!(i & 1)); |
1140 | } |
1141 | disptop = scrtop; |
cabfd08c |
1142 | seen_disp_event = TRUE; |
374330e2 |
1143 | break; |
1144 | case 'K': /* erase line or parts of it */ |
1145 | { |
1146 | unsigned int i = def(esc_args[0], 0) + 1; |
1147 | if (i > 3) |
1148 | i = 0; |
1149 | erase_lots(TRUE, !!(i & 2), !!(i & 1)); |
1150 | } |
cabfd08c |
1151 | seen_disp_event = TRUE; |
374330e2 |
1152 | break; |
1153 | case 'L': /* insert lines */ |
e14a5a13 |
1154 | compatibility(VT102); |
374330e2 |
1155 | if (curs_y <= marg_b) |
1156 | scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE); |
cabfd08c |
1157 | seen_disp_event = TRUE; |
374330e2 |
1158 | break; |
1159 | case 'M': /* delete lines */ |
e14a5a13 |
1160 | compatibility(VT102); |
374330e2 |
1161 | if (curs_y <= marg_b) |
cabfd08c |
1162 | scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE); |
1163 | seen_disp_event = TRUE; |
374330e2 |
1164 | break; |
1165 | case '@': /* insert chars */ |
e14a5a13 |
1166 | /* XXX VTTEST says this is vt220, vt510 manual says vt102 */ |
1167 | compatibility(VT102); |
374330e2 |
1168 | insch (def(esc_args[0], 1)); |
cabfd08c |
1169 | seen_disp_event = TRUE; |
374330e2 |
1170 | break; |
1171 | case 'P': /* delete chars */ |
e14a5a13 |
1172 | compatibility(VT102); |
374330e2 |
1173 | insch (-def(esc_args[0], 1)); |
cabfd08c |
1174 | seen_disp_event = TRUE; |
374330e2 |
1175 | break; |
1176 | case 'c': /* terminal type query */ |
e14a5a13 |
1177 | compatibility(VT100); |
1178 | /* This is the response for a VT102 */ |
c9def1b8 |
1179 | ldisc->send (id_string, strlen(id_string)); |
374330e2 |
1180 | break; |
1181 | case 'n': /* cursor position query */ |
1182 | if (esc_args[0] == 6) { |
1183 | char buf[32]; |
1184 | sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1); |
5bc238bb |
1185 | ldisc->send (buf, strlen(buf)); |
374330e2 |
1186 | } |
e14a5a13 |
1187 | else if (esc_args[0] == 5) { |
e14a5a13 |
1188 | ldisc->send ("\033[0n", 4); |
1189 | } |
374330e2 |
1190 | break; |
e14a5a13 |
1191 | case 'h': /* toggle modes to high */ |
8b811b12 |
1192 | case ANSI_QUE('h'): |
e14a5a13 |
1193 | compatibility(VT100); |
1194 | { |
1195 | int i; |
1196 | for (i=0; i<esc_nargs; i++) |
1197 | toggle_mode (esc_args[i], esc_query, TRUE); |
1198 | } |
374330e2 |
1199 | break; |
e14a5a13 |
1200 | case 'l': /* toggle modes to low */ |
8b811b12 |
1201 | case ANSI_QUE('l'): |
e14a5a13 |
1202 | compatibility(VT100); |
1203 | { |
1204 | int i; |
1205 | for (i=0; i<esc_nargs; i++) |
1206 | toggle_mode (esc_args[i], esc_query, FALSE); |
1207 | } |
374330e2 |
1208 | break; |
1209 | case 'g': /* clear tabs */ |
e14a5a13 |
1210 | compatibility(VT100); |
374330e2 |
1211 | if (esc_nargs == 1) { |
1212 | if (esc_args[0] == 0) { |
1213 | tabs[curs_x] = FALSE; |
1214 | } else if (esc_args[0] == 3) { |
1215 | int i; |
1216 | for (i = 0; i < cols; i++) |
1217 | tabs[i] = FALSE; |
1218 | } |
1219 | } |
1220 | break; |
1221 | case 'r': /* set scroll margins */ |
e14a5a13 |
1222 | compatibility(VT100); |
8b811b12 |
1223 | if (esc_nargs <= 2) { |
374330e2 |
1224 | int top, bot; |
7e394092 |
1225 | top = def(esc_args[0], 1) - 1; |
1226 | bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows : |
1227 | def(esc_args[1], rows)) - 1; |
374330e2 |
1228 | if (bot >= rows) |
1229 | bot = rows-1; |
7e394092 |
1230 | /* VTTEST Bug 9 - if region is less than 2 lines |
1231 | * don't change region. |
1232 | */ |
1233 | if (bot-top > 1) { |
374330e2 |
1234 | marg_t = top; |
1235 | marg_b = bot; |
1236 | curs_x = 0; |
1237 | /* |
1238 | * I used to think the cursor should be |
1239 | * placed at the top of the newly marginned |
1240 | * area. Apparently not: VMS TPU falls over |
1241 | * if so. |
e14a5a13 |
1242 | * |
1243 | * Well actually it should for Origin mode - RDB |
374330e2 |
1244 | */ |
e14a5a13 |
1245 | curs_y = (dec_om ? marg_t : 0); |
374330e2 |
1246 | fix_cpos; |
cabfd08c |
1247 | seen_disp_event = TRUE; |
374330e2 |
1248 | } |
1249 | } |
1250 | break; |
1251 | case 'm': /* set graphics rendition */ |
1252 | { |
e14a5a13 |
1253 | /* |
1254 | * A VT100 without the AVO only had one attribute, either |
1255 | * underline or reverse video depending on the cursor type, |
1256 | * this was selected by CSI 7m. |
1257 | * |
1258 | * case 2: |
1259 | * This is DIM on the VT100-AVO and VT102 |
1260 | * case 5: |
1261 | * This is BLINK on the VT100-AVO and VT102+ |
1262 | * case 8: |
1263 | * This is INVIS on the VT100-AVO and VT102 |
1264 | * case 21: |
1265 | * This like 22 disables BOLD, DIM and INVIS |
1266 | * |
1267 | * The ANSI colours appear on any terminal that has colour |
1268 | * (obviously) but the interaction between sgr0 and the |
1269 | * colours varies but is usually related to the background |
1270 | * colour erase item. |
1271 | * The interaction between colour attributes and the mono |
1272 | * ones is also very implementation dependent. |
1273 | * |
1274 | * The 39 and 49 attributes are likely to be unimplemented. |
1275 | */ |
374330e2 |
1276 | int i; |
1277 | for (i=0; i<esc_nargs; i++) { |
1278 | switch (def(esc_args[i], 0)) { |
1279 | case 0: /* restore defaults */ |
1280 | curr_attr = ATTR_DEFAULT; break; |
1281 | case 1: /* enable bold */ |
e14a5a13 |
1282 | compatibility(VT100AVO); |
374330e2 |
1283 | curr_attr |= ATTR_BOLD; break; |
374330e2 |
1284 | case 21: /* (enable double underline) */ |
e14a5a13 |
1285 | compatibility(OTHER); |
1286 | case 4: /* enable underline */ |
1287 | compatibility(VT100AVO); |
374330e2 |
1288 | curr_attr |= ATTR_UNDER; break; |
e14a5a13 |
1289 | case 5: /* enable blink */ |
1290 | compatibility(VT100AVO); |
1291 | curr_attr |= ATTR_BLINK; break; |
374330e2 |
1292 | case 7: /* enable reverse video */ |
1293 | curr_attr |= ATTR_REVERSE; break; |
1294 | case 22: /* disable bold */ |
e14a5a13 |
1295 | compatibility(VT220); |
374330e2 |
1296 | curr_attr &= ~ATTR_BOLD; break; |
1297 | case 24: /* disable underline */ |
e14a5a13 |
1298 | compatibility(VT220); |
374330e2 |
1299 | curr_attr &= ~ATTR_UNDER; break; |
e14a5a13 |
1300 | case 25: /* disable blink */ |
1301 | compatibility(VT220); |
1302 | curr_attr &= ~ATTR_BLINK; break; |
374330e2 |
1303 | case 27: /* disable reverse video */ |
e14a5a13 |
1304 | compatibility(VT220); |
374330e2 |
1305 | curr_attr &= ~ATTR_REVERSE; break; |
1306 | case 30: case 31: case 32: case 33: |
1307 | case 34: case 35: case 36: case 37: |
1308 | /* foreground */ |
1309 | curr_attr &= ~ATTR_FGMASK; |
1310 | curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT; |
1311 | break; |
1312 | case 39: /* default-foreground */ |
1313 | curr_attr &= ~ATTR_FGMASK; |
1314 | curr_attr |= ATTR_DEFFG; |
1315 | break; |
1316 | case 40: case 41: case 42: case 43: |
1317 | case 44: case 45: case 46: case 47: |
1318 | /* background */ |
1319 | curr_attr &= ~ATTR_BGMASK; |
1320 | curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT; |
1321 | break; |
1322 | case 49: /* default-background */ |
1323 | curr_attr &= ~ATTR_BGMASK; |
1324 | curr_attr |= ATTR_DEFBG; |
1325 | break; |
1326 | } |
1327 | } |
e14a5a13 |
1328 | if (use_bce) |
c9def1b8 |
1329 | erase_char = |
1330 | (' '| |
1331 | (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK)) |
1332 | ); |
374330e2 |
1333 | } |
1334 | break; |
1335 | case 's': /* save cursor */ |
1336 | save_cursor (TRUE); |
1337 | break; |
1338 | case 'u': /* restore cursor */ |
1339 | save_cursor (FALSE); |
cabfd08c |
1340 | seen_disp_event = TRUE; |
374330e2 |
1341 | break; |
1342 | case 't': /* set page size - ie window height */ |
e14a5a13 |
1343 | /* |
1344 | * VT340/VT420 sequence DECSLPP, DEC only allows values |
1345 | * 24/25/36/48/72/144 other emulators (eg dtterm) use |
1346 | * illegal values (eg first arg 1..9) for window changing |
1347 | * and reports. |
1348 | */ |
1349 | compatibility(VT340TEXT); |
1350 | if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) { |
c9def1b8 |
1351 | request_resize (cols, def(esc_args[0], 24), 0); |
e14a5a13 |
1352 | deselect(); |
1353 | } |
1354 | break; |
1355 | case ANSI('|', '*'): |
1356 | /* VT420 sequence DECSNLS |
1357 | * Set number of lines on screen |
1358 | * VT420 uses VGA like hardware and can support any size in |
1359 | * reasonable range (24..49 AIUI) with no default specified. |
1360 | */ |
1361 | compatibility(VT420); |
c9def1b8 |
1362 | if (esc_nargs==1 && esc_args[0]>0) { |
1363 | request_resize (cols, def(esc_args[0], cfg.height), 0); |
e14a5a13 |
1364 | deselect(); |
1365 | } |
1366 | break; |
1367 | case ANSI('|', '$'): |
1368 | /* VT340/VT420 sequence DECSCPP |
1369 | * Set number of columns per page |
1370 | * Docs imply range is only 80 or 132, but I'll allow any. |
1371 | */ |
1372 | compatibility(VT340TEXT); |
1373 | if (esc_nargs<=1) { |
c9def1b8 |
1374 | request_resize (def(esc_args[0], cfg.width), rows, 0); |
e14a5a13 |
1375 | deselect(); |
1376 | } |
374330e2 |
1377 | break; |
1378 | case 'X': /* write N spaces w/o moving cursor */ |
e14a5a13 |
1379 | /* XXX VTTEST says this is vt220, vt510 manual says vt100 */ |
c9def1b8 |
1380 | compatibility(ANSIMIN); |
374330e2 |
1381 | { |
1382 | int n = def(esc_args[0], 1); |
1383 | unsigned long *p = cpos; |
1384 | if (n > cols - curs_x) |
1385 | n = cols - curs_x; |
1386 | check_selection (cpos, cpos+n); |
1387 | while (n--) |
e14a5a13 |
1388 | *p++ = erase_char; |
cabfd08c |
1389 | seen_disp_event = TRUE; |
374330e2 |
1390 | } |
1391 | break; |
1392 | case 'x': /* report terminal characteristics */ |
e14a5a13 |
1393 | compatibility(VT100); |
374330e2 |
1394 | { |
1395 | char buf[32]; |
1396 | int i = def(esc_args[0], 0); |
1397 | if (i == 0 || i == 1) { |
1398 | strcpy (buf, "\033[2;1;1;112;112;1;0x"); |
1399 | buf[2] += i; |
5bc238bb |
1400 | ldisc->send (buf, 20); |
374330e2 |
1401 | } |
1402 | } |
1403 | break; |
e14a5a13 |
1404 | case ANSI('L','='): |
1405 | compatibility(OTHER); |
1406 | use_bce = (esc_args[0]<=0); |
1407 | erase_char = ERASE_CHAR; |
1408 | if (use_bce) |
1409 | erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
1410 | break; |
c9def1b8 |
1411 | case ANSI('E','='): |
1412 | compatibility(OTHER); |
1413 | blink_is_real = (esc_args[0]>=1); |
1414 | break; |
e14a5a13 |
1415 | case ANSI('p','"'): |
1416 | /* Allow the host to make this emulator a 'perfect' VT102. |
1417 | * This first appeared in the VT220, but we do need to get |
1418 | * back to PuTTY mode so I won't check it. |
1419 | * |
1420 | * The arg == 60 is a PuTTY extension. |
1421 | * The 2nd arg, 8bit vs 7bit is not obeyed. |
1422 | * |
1423 | * Setting VT102 mode should also change the Fkeys to |
1424 | * generate PF* codes as a real VT102 has no Fkeys. |
1425 | * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys |
1426 | * send nothing. |
1427 | * |
1428 | * Note ESC c will NOT change this! |
1429 | */ |
1430 | |
1431 | if (esc_args[0] == 61) compatibility_level = TM_VT102; |
1432 | else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN; |
1433 | else compatibility_level = TM_PUTTY; |
1434 | break; |
374330e2 |
1435 | } |
1436 | break; |
1437 | case SET_GL: |
1438 | case SET_GR: |
e14a5a13 |
1439 | /* VT100 only here, checked above */ |
374330e2 |
1440 | switch (c) { |
1441 | case 'A': |
1442 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR; |
1443 | break; |
1444 | case '0': |
1445 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW; |
1446 | break; |
e14a5a13 |
1447 | case 'B': |
374330e2 |
1448 | default: /* specifically, 'B' */ |
1449 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII; |
1450 | break; |
1451 | } |
e14a5a13 |
1452 | if( !has_compat(VT220) || c != '%' ) |
8b811b12 |
1453 | termstate = TOPLEVEL; |
374330e2 |
1454 | break; |
1455 | case SEEN_OSC: |
1456 | osc_w = FALSE; |
1457 | switch (c) { |
374330e2 |
1458 | case 'P': /* Linux palette sequence */ |
1459 | termstate = SEEN_OSC_P; |
1460 | osc_strlen = 0; |
1461 | break; |
1462 | case 'R': /* Linux palette reset */ |
1463 | palette_reset(); |
1464 | term_invalidate(); |
1465 | termstate = TOPLEVEL; |
1466 | break; |
1467 | case 'W': /* word-set */ |
1468 | termstate = SEEN_OSC_W; |
1469 | osc_w = TRUE; |
1470 | break; |
1471 | case '0': case '1': case '2': case '3': case '4': |
1472 | case '5': case '6': case '7': case '8': case '9': |
1473 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
1474 | break; |
1475 | case 'L': |
1476 | /* |
1477 | * Grotty hack to support xterm and DECterm title |
1478 | * sequences concurrently. |
1479 | */ |
1480 | if (esc_args[0] == 2) { |
1481 | esc_args[0] = 1; |
1482 | break; |
1483 | } |
1484 | /* else fall through */ |
1485 | default: |
1486 | termstate = OSC_STRING; |
1487 | osc_strlen = 0; |
1488 | } |
1489 | break; |
1490 | case OSC_STRING: |
8b811b12 |
1491 | /* |
1492 | * This OSC stuff is EVIL. It takes just one character to get into |
1493 | * sysline mode and it's not initially obvious how to get out. |
1494 | * So I've added CR and LF as string aborts. |
1495 | * This shouldn't effect compatibility as I believe embedded |
1496 | * control characters are supposed to be interpreted (maybe?) |
1497 | * and they don't display anything useful anyway. |
1498 | * |
1499 | * -- RDB |
1500 | */ |
1501 | if (c == '\n' || c == '\r') { |
1502 | termstate = TOPLEVEL; |
1503 | } else if (c == 0234 || c == '\007' ) { |
374330e2 |
1504 | /* |
1505 | * These characters terminate the string; ST and BEL |
1506 | * terminate the sequence and trigger instant |
1507 | * processing of it, whereas ESC goes back to SEEN_ESC |
1508 | * mode unless it is followed by \, in which case it is |
1509 | * synonymous with ST in the first place. |
1510 | */ |
1511 | do_osc(); |
1512 | termstate = TOPLEVEL; |
1513 | } else if (c == '\033') |
1514 | termstate = OSC_MAYBE_ST; |
1515 | else if (osc_strlen < OSC_STR_MAX) |
1516 | osc_string[osc_strlen++] = c; |
1517 | break; |
1518 | case SEEN_OSC_P: |
1519 | { |
1520 | int max = (osc_strlen == 0 ? 21 : 16); |
1521 | int val; |
1522 | if (c >= '0' && c <= '9') |
1523 | val = c - '0'; |
1524 | else if (c >= 'A' && c <= 'A'+max-10) |
1525 | val = c - 'A' + 10; |
1526 | else if (c >= 'a' && c <= 'a'+max-10) |
1527 | val = c - 'a' + 10; |
1528 | else |
1529 | termstate = TOPLEVEL; |
1530 | osc_string[osc_strlen++] = val; |
1531 | if (osc_strlen >= 7) { |
1532 | palette_set (osc_string[0], |
1533 | osc_string[1] * 16 + osc_string[2], |
1534 | osc_string[3] * 16 + osc_string[4], |
1535 | osc_string[5] * 16 + osc_string[6]); |
1536 | term_invalidate(); |
1537 | termstate = TOPLEVEL; |
1538 | } |
1539 | } |
1540 | break; |
1541 | case SEEN_OSC_W: |
1542 | switch (c) { |
374330e2 |
1543 | case '0': case '1': case '2': case '3': case '4': |
1544 | case '5': case '6': case '7': case '8': case '9': |
1545 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
1546 | break; |
1547 | default: |
1548 | termstate = OSC_STRING; |
1549 | osc_strlen = 0; |
1550 | } |
1551 | break; |
1552 | case SEEN_ESCHASH: |
c9def1b8 |
1553 | { |
1554 | unsigned long *p; |
1555 | unsigned long nlattr; |
1556 | int n; |
1557 | |
1558 | switch (c) { |
1559 | case '8': |
1560 | p = scrtop; |
1561 | n = rows * (cols+1); |
1562 | while (n--) |
1563 | *p++ = ATTR_DEFAULT | 'E'; |
1564 | disptop = scrtop; |
1565 | seen_disp_event = TRUE; |
1566 | check_selection (scrtop, scrtop + rows * (cols+1)); |
1567 | break; |
1568 | |
1569 | case '3': nlattr = LATTR_TOP; if(0) { |
1570 | case '4': nlattr = LATTR_BOT; } if(0) { |
1571 | case '5': nlattr = LATTR_NORM; } if(0) { |
1572 | case '6': nlattr = LATTR_WIDE; } |
1573 | |
1574 | p = scrtop + curs_y * (cols+1) + cols; |
1575 | *p &= ~LATTR_MODE; |
1576 | *p |= nlattr; |
1577 | } |
374330e2 |
1578 | } |
1579 | termstate = TOPLEVEL; |
1580 | break; |
e14a5a13 |
1581 | case VT52_ESC: |
1582 | termstate = TOPLEVEL; |
1583 | seen_disp_event = TRUE; |
1584 | switch (c) { |
1585 | case 'A': |
1586 | move (curs_x, curs_y - 1, 1); |
1587 | break; |
1588 | case 'B': |
1589 | move (curs_x, curs_y + 1, 1); |
1590 | break; |
1591 | case 'C': |
1592 | move (curs_x + 1, curs_y, 1); |
1593 | break; |
1594 | case 'D': |
1595 | move (curs_x - 1, curs_y, 1); |
1596 | break; |
1597 | case 'F': |
1598 | cset_attr[cset=0] = ATTR_LINEDRW; |
1599 | break; |
1600 | case 'G': |
1601 | cset_attr[cset=0] = ATTR_ASCII; |
1602 | break; |
1603 | case 'H': |
1604 | move (0, 0, 0); |
1605 | break; |
1606 | case 'I': |
1607 | if (curs_y == 0) |
1608 | scroll (0, rows-1, -1, TRUE); |
1609 | else if (curs_y > 0) |
1610 | curs_y--; |
1611 | fix_cpos; |
1612 | wrapnext = FALSE; |
1613 | break; |
1614 | case 'J': |
1615 | erase_lots(FALSE, FALSE, TRUE); |
1616 | disptop = scrtop; |
1617 | break; |
1618 | case 'K': |
1619 | erase_lots(TRUE, FALSE, TRUE); |
1620 | break; |
1621 | case 'V': |
1622 | /* XXX Print cursor line */ |
1623 | break; |
1624 | case 'W': |
1625 | /* XXX Start controller mode */ |
1626 | break; |
1627 | case 'X': |
1628 | /* XXX Stop controller mode */ |
1629 | break; |
1630 | case 'Y': |
1631 | termstate = VT52_Y1; |
1632 | break; |
1633 | case 'Z': |
1634 | ldisc->send ("\033/Z", 3); |
1635 | break; |
1636 | case '=': |
1637 | app_cursor_keys = TRUE; |
1638 | break; |
1639 | case '>': |
1640 | app_cursor_keys = FALSE; |
1641 | break; |
1642 | case '<': |
1643 | /* XXX This should switch to VT100 mode not current or default |
1644 | * VT mode. But this will only have effect in a VT220+ |
1645 | * emulation. |
1646 | */ |
1647 | vt52_mode = FALSE; |
1648 | break; |
1649 | case '^': |
1650 | /* XXX Enter auto print mode */ |
1651 | break; |
1652 | case '_': |
1653 | /* XXX Exit auto print mode */ |
1654 | break; |
1655 | case ']': |
1656 | /* XXX Print screen */ |
1657 | break; |
1658 | } |
1659 | break; |
1660 | case VT52_Y1: |
1661 | termstate = VT52_Y2; |
1662 | move(curs_x, c-' ', 0); |
1663 | break; |
1664 | case VT52_Y2: |
1665 | termstate = TOPLEVEL; |
1666 | move(c-' ', curs_y, 0); |
1667 | break; |
374330e2 |
1668 | } |
e14a5a13 |
1669 | if (selstate != NO_SELECTION) |
1670 | check_selection (cpos, cpos+1); |
374330e2 |
1671 | } |
c9def1b8 |
1672 | inbuf_head = 0; |
1673 | |
1674 | if (beep_overload) |
1675 | { |
1676 | if(!beep_count) beep_overload=0; |
1677 | } |
1678 | else if(beep_count && beep_count<5 && cfg.beep) |
1679 | beep(beep_count/3); |
374330e2 |
1680 | } |
1681 | |
1682 | /* |
1683 | * Compare two lines to determine whether they are sufficiently |
1684 | * alike to scroll-optimise one to the other. Return the degree of |
1685 | * similarity. |
1686 | */ |
1687 | static int linecmp (unsigned long *a, unsigned long *b) { |
1688 | int i, n; |
1689 | |
1690 | for (i=n=0; i < cols; i++) |
1691 | n += (*a++ == *b++); |
1692 | return n; |
1693 | } |
1694 | |
1695 | /* |
1696 | * Given a context, update the window. Out of paranoia, we don't |
1697 | * allow WM_PAINT responses to do scrolling optimisations. |
1698 | */ |
ec8679e9 |
1699 | static void do_paint (Context ctx, int may_optimise){ |
374330e2 |
1700 | int i, j, start, our_curs_y; |
1701 | unsigned long attr, rv, cursor; |
1702 | char ch[1024]; |
1703 | |
e14a5a13 |
1704 | if (cursor_on) { |
1705 | if (has_focus) { |
217dceef |
1706 | if (blinker || !cfg.blink_cur) |
1707 | cursor = ATTR_ACTCURS; |
1708 | else |
1709 | cursor = 0; |
e14a5a13 |
1710 | } |
1711 | else |
1712 | cursor = ATTR_PASCURS; |
1713 | } |
1714 | else cursor = 0; |
374330e2 |
1715 | rv = (rvideo ? ATTR_REVERSE : 0); |
1716 | our_curs_y = curs_y + (scrtop - disptop) / (cols+1); |
1717 | |
1718 | for (i=0; i<rows; i++) { |
1719 | int idx = i*(cols+1); |
c9def1b8 |
1720 | int lattr = (disptop[idx+cols] & LATTR_MODE); |
374330e2 |
1721 | for (j=0; j<=cols; j++,idx++) { |
1722 | unsigned long *d = disptop+idx; |
c9def1b8 |
1723 | wanttext[idx] = lattr | ((*d ^ rv |
374330e2 |
1724 | ^ (selstart <= d && d < selend ? |
1725 | ATTR_REVERSE : 0)) | |
1726 | (i==our_curs_y && j==curs_x ? cursor : 0)); |
c9def1b8 |
1727 | |
1728 | if (blink_is_real) { |
1729 | if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) ) |
1730 | { |
1731 | wanttext[idx] &= ATTR_MASK; |
1732 | wanttext[idx] += ' '; |
1733 | } |
1734 | wanttext[idx] &= ~ATTR_BLINK; |
1735 | } |
374330e2 |
1736 | } |
1737 | } |
1738 | |
1739 | /* |
1740 | * We would perform scrolling optimisations in here, if they |
1741 | * didn't have a nasty tendency to cause the whole sodding |
1742 | * program to hang for a second at speed-critical moments. |
1743 | * We'll leave it well alone... |
1744 | */ |
1745 | |
1746 | for (i=0; i<rows; i++) { |
1747 | int idx = i*(cols+1); |
c9def1b8 |
1748 | int lattr = (wanttext[idx+cols] & LATTR_MODE); |
374330e2 |
1749 | start = -1; |
1750 | for (j=0; j<=cols; j++,idx++) { |
1751 | unsigned long t = wanttext[idx]; |
1752 | int needs_update = (j < cols && t != disptext[idx]); |
1753 | int keep_going = (start != -1 && needs_update && |
1754 | (t & ATTR_MASK) == attr && |
1755 | j-start < sizeof(ch)); |
1756 | if (start != -1 && !keep_going) { |
c9def1b8 |
1757 | do_text (ctx, start, i, ch, j-start, attr, lattr); |
374330e2 |
1758 | start = -1; |
1759 | } |
1760 | if (needs_update) { |
1761 | if (start == -1) { |
1762 | start = j; |
1763 | attr = t & ATTR_MASK; |
1764 | } |
1765 | ch[j-start] = (char) (t & CHAR_MASK); |
1766 | } |
1767 | disptext[idx] = t; |
1768 | } |
1769 | } |
1770 | } |
1771 | |
1772 | /* |
e14a5a13 |
1773 | * Flick the switch that says if blinking things should be shown or hidden. |
1774 | */ |
1775 | |
1776 | void term_blink(int flg) { |
1777 | static long last_blink = 0; |
c9def1b8 |
1778 | static long last_tblink = 0; |
e14a5a13 |
1779 | long now, blink_diff; |
1780 | |
c9def1b8 |
1781 | now = GetTickCount(); |
1782 | blink_diff = now-last_tblink; |
1783 | |
1784 | /* Make sure the text blinks no more than 2Hz */ |
1785 | if (blink_diff<0 || blink_diff>450) |
1786 | { |
1787 | last_tblink = now; |
1788 | tblinker = !tblinker; |
1789 | } |
1790 | |
e14a5a13 |
1791 | if (flg) { |
1792 | blinker = 1; |
c9def1b8 |
1793 | last_blink = now; |
e14a5a13 |
1794 | return; |
1795 | } |
e14a5a13 |
1796 | |
1797 | blink_diff = now-last_blink; |
1798 | |
1799 | /* Make sure the cursor blinks no more than 2Hz */ |
1800 | if (blink_diff>=0 && blink_diff<450) |
1801 | return; |
1802 | |
1803 | last_blink = now; |
1804 | blinker = !blinker; |
1805 | } |
1806 | |
1807 | /* |
374330e2 |
1808 | * Invalidate the whole screen so it will be repainted in full. |
1809 | */ |
1810 | void term_invalidate(void) { |
1811 | int i; |
1812 | |
1813 | for (i=0; i<rows*(cols+1); i++) |
1814 | disptext[i] = ATTR_INVALID; |
1815 | } |
1816 | |
1817 | /* |
1818 | * Paint the window in response to a WM_PAINT message. |
1819 | */ |
1820 | void term_paint (Context ctx, int l, int t, int r, int b) { |
1821 | int i, j, left, top, right, bottom; |
1822 | |
1823 | left = l / font_width; |
1824 | right = (r - 1) / font_width; |
1825 | top = t / font_height; |
1826 | bottom = (b - 1) / font_height; |
e42ad027 |
1827 | for (i = top; i <= bottom && i < rows ; i++) |
c9def1b8 |
1828 | { |
1829 | if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM) |
1830 | for (j = left; j <= right && j < cols ; j++) |
1831 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
1832 | else |
1833 | for (j = left/2; j <= right/2+1 && j < cols ; j++) |
1834 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
1835 | } |
374330e2 |
1836 | |
e14a5a13 |
1837 | /* This should happen soon enough, also for some reason it sometimes |
1838 | * fails to actually do anything when re-sizing ... painting the wrong |
1839 | * window perhaps ? |
374330e2 |
1840 | do_paint (ctx, FALSE); |
e14a5a13 |
1841 | */ |
374330e2 |
1842 | } |
1843 | |
1844 | /* |
1845 | * Attempt to scroll the scrollback. The second parameter gives the |
1846 | * position we want to scroll to; the first is +1 to denote that |
1847 | * this position is relative to the beginning of the scrollback, -1 |
1848 | * to denote it is relative to the end, and 0 to denote that it is |
1849 | * relative to the current position. |
1850 | */ |
1851 | void term_scroll (int rel, int where) { |
1852 | int n = where * (cols+1); |
1853 | |
1854 | disptop = (rel < 0 ? scrtop : |
1855 | rel > 0 ? sbtop : disptop) + n; |
1856 | if (disptop < sbtop) |
1857 | disptop = sbtop; |
1858 | if (disptop > scrtop) |
1859 | disptop = scrtop; |
1860 | update_sbar(); |
1861 | term_update(); |
1862 | } |
1863 | |
1864 | /* |
1865 | * Spread the selection outwards according to the selection mode. |
1866 | */ |
1867 | static unsigned long *sel_spread_half (unsigned long *p, int dir) { |
1868 | unsigned long *linestart, *lineend; |
1869 | int x; |
1870 | short wvalue; |
1871 | |
1872 | x = (p - text) % (cols+1); |
1873 | linestart = p - x; |
1874 | lineend = linestart + cols; |
1875 | |
1876 | switch (selmode) { |
1877 | case SM_CHAR: |
1878 | /* |
1879 | * In this mode, every character is a separate unit, except |
1880 | * for runs of spaces at the end of a non-wrapping line. |
1881 | */ |
1882 | if (!(linestart[cols] & ATTR_WRAPPED)) { |
1883 | unsigned long *q = lineend; |
1884 | while (q > linestart && (q[-1] & CHAR_MASK) == 0x20) |
1885 | q--; |
1886 | if (q == lineend) |
1887 | q--; |
1888 | if (p >= q) |
1889 | p = (dir == -1 ? q : lineend - 1); |
1890 | } |
1891 | break; |
1892 | case SM_WORD: |
1893 | /* |
1894 | * In this mode, the units are maximal runs of characters |
1895 | * whose `wordness' has the same value. |
1896 | */ |
1897 | wvalue = wordness[*p & CHAR_MASK]; |
1898 | if (dir == +1) { |
1899 | while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue) |
1900 | p++; |
1901 | } else { |
1902 | while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue) |
1903 | p--; |
1904 | } |
1905 | break; |
1906 | case SM_LINE: |
1907 | /* |
1908 | * In this mode, every line is a unit. |
1909 | */ |
1910 | p = (dir == -1 ? linestart : lineend - 1); |
1911 | break; |
1912 | } |
1913 | return p; |
1914 | } |
1915 | |
1916 | static void sel_spread (void) { |
1917 | selstart = sel_spread_half (selstart, -1); |
1918 | selend = sel_spread_half (selend - 1, +1) + 1; |
1919 | } |
1920 | |
1921 | void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) { |
37508af4 |
1922 | unsigned long *selpoint; |
1923 | |
1924 | if (y<0) y = 0; |
1925 | if (y>=rows) y = rows-1; |
094ed2a6 |
1926 | if (x<0) { |
1927 | if (y > 0) { |
1928 | x = cols-1; |
1929 | y--; |
1930 | } else |
1931 | x = 0; |
1932 | } |
37508af4 |
1933 | if (x>=cols) x = cols-1; |
1934 | |
c9def1b8 |
1935 | selpoint = disptop + y * (cols+1); |
1936 | if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM) |
1937 | selpoint += x/2; |
1938 | else |
1939 | selpoint += x; |
374330e2 |
1940 | |
1941 | if (b == MB_SELECT && a == MA_CLICK) { |
1942 | deselect(); |
1943 | selstate = ABOUT_TO; |
1944 | selanchor = selpoint; |
1945 | selmode = SM_CHAR; |
1946 | } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) { |
1947 | deselect(); |
1948 | selmode = (a == MA_2CLK ? SM_WORD : SM_LINE); |
1949 | selstate = DRAGGING; |
1950 | selstart = selanchor = selpoint; |
1951 | selend = selstart + 1; |
1952 | sel_spread(); |
1953 | } else if ((b == MB_SELECT && a == MA_DRAG) || |
1954 | (b == MB_EXTEND && a != MA_RELEASE)) { |
1955 | if (selstate == ABOUT_TO && selanchor == selpoint) |
1956 | return; |
1957 | if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) { |
1958 | if (selpoint-selstart < (selend-selstart)/2) |
1959 | selanchor = selend - 1; |
1960 | else |
1961 | selanchor = selstart; |
1962 | selstate = DRAGGING; |
1963 | } |
1964 | if (selstate != ABOUT_TO && selstate != DRAGGING) |
1965 | selanchor = selpoint; |
1966 | selstate = DRAGGING; |
1967 | if (selpoint < selanchor) { |
1968 | selstart = selpoint; |
1969 | selend = selanchor + 1; |
1970 | } else { |
1971 | selstart = selanchor; |
1972 | selend = selpoint + 1; |
1973 | } |
1974 | sel_spread(); |
1975 | } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) { |
1976 | if (selstate == DRAGGING) { |
1977 | /* |
1978 | * We've completed a selection. We now transfer the |
1979 | * data to the clipboard. |
1980 | */ |
1981 | unsigned char *p = selspace; |
1982 | unsigned long *q = selstart; |
1983 | |
1984 | while (q < selend) { |
1985 | int nl = FALSE; |
1986 | unsigned long *lineend = q - (q-text) % (cols+1) + cols; |
1987 | unsigned long *nlpos = lineend; |
1988 | |
1989 | if (!(*nlpos & ATTR_WRAPPED)) { |
1990 | while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q) |
1991 | nlpos--; |
1992 | if (nlpos < selend) |
1993 | nl = TRUE; |
1994 | } |
1995 | while (q < nlpos && q < selend) |
1996 | *p++ = (unsigned char) (*q++ & CHAR_MASK); |
1997 | if (nl) { |
1998 | int i; |
1999 | for (i=0; i<sizeof(sel_nl); i++) |
2000 | *p++ = sel_nl[i]; |
2001 | } |
2002 | q = lineend + 1; /* start of next line */ |
2003 | } |
2004 | write_clip (selspace, p - selspace); |
2005 | selstate = SELECTED; |
2006 | } else |
2007 | selstate = NO_SELECTION; |
2008 | } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) { |
2009 | char *data; |
2010 | int len; |
2011 | |
2012 | get_clip((void **) &data, &len); |
2013 | if (data) { |
2014 | char *p, *q; |
c9def1b8 |
2015 | |
2016 | if (paste_buffer) sfree(paste_buffer); |
2017 | paste_pos = paste_hold = paste_len = 0; |
2018 | paste_buffer = smalloc(len); |
2019 | |
374330e2 |
2020 | p = q = data; |
2021 | while (p < data+len) { |
2022 | while (p < data+len && |
2023 | !(p <= data+len-sizeof(sel_nl) && |
2024 | !memcmp(p, sel_nl, sizeof(sel_nl)))) |
2025 | p++; |
14963b8f |
2026 | |
2027 | { |
2028 | int i; |
2029 | unsigned char c; |
2030 | for(i=0;i<p-q;i++) |
2031 | { |
2032 | c=xlat_kbd2tty(q[i]); |
c9def1b8 |
2033 | paste_buffer[paste_len++] = c; |
14963b8f |
2034 | } |
2035 | } |
2036 | |
374330e2 |
2037 | if (p <= data+len-sizeof(sel_nl) && |
2038 | !memcmp(p, sel_nl, sizeof(sel_nl))) { |
c9def1b8 |
2039 | paste_buffer[paste_len++] = '\r'; |
374330e2 |
2040 | p += sizeof(sel_nl); |
2041 | } |
2042 | q = p; |
2043 | } |
c9def1b8 |
2044 | |
2045 | /* Assume a small paste will be OK in one go. */ |
2046 | if (paste_len<256) { |
2047 | ldisc->send (paste_buffer, paste_len); |
2048 | if (paste_buffer) sfree(paste_buffer); |
2049 | paste_buffer = 0; |
2050 | paste_pos = paste_hold = paste_len = 0; |
2051 | } |
374330e2 |
2052 | } |
2053 | get_clip(NULL, NULL); |
2054 | } |
2055 | |
2056 | term_update(); |
2057 | } |
2058 | |
c9def1b8 |
2059 | void term_nopaste() { |
2060 | if(paste_len == 0) return; |
2061 | sfree(paste_buffer); |
2062 | paste_buffer = 0; |
2063 | paste_len = 0; |
2064 | } |
2065 | |
2066 | void term_paste() { |
2067 | static long last_paste = 0; |
2068 | long now, paste_diff; |
2069 | |
2070 | if(paste_len == 0) return; |
2071 | |
2072 | /* Don't wait forever to paste */ |
2073 | if(paste_hold) { |
2074 | now = GetTickCount(); |
2075 | paste_diff = now-last_paste; |
2076 | if (paste_diff>=0 && paste_diff<450) |
2077 | return; |
2078 | } |
2079 | paste_hold = 0; |
2080 | |
2081 | while(paste_pos<paste_len) |
2082 | { |
2083 | char c = paste_buffer[paste_pos++]; |
2084 | ldisc->send (&c, 1); |
2085 | |
2086 | if (c =='\r') { |
2087 | paste_hold = 1; |
2088 | return; |
2089 | } |
2090 | } |
2091 | sfree(paste_buffer); |
2092 | paste_buffer = 0; |
2093 | paste_len = 0; |
2094 | } |
2095 | |
374330e2 |
2096 | static void deselect (void) { |
2097 | selstate = NO_SELECTION; |
2098 | selstart = selend = scrtop; |
2099 | } |
2100 | |
2101 | void term_deselect (void) { |
2102 | deselect(); |
2103 | term_update(); |
2104 | } |