Line | Count | Source |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | #include <sys/ioctl.h> |
21 | | |
22 | | #include <netinet/in.h> |
23 | | |
24 | | #include <curses.h> |
25 | | #include <errno.h> |
26 | | #include <fcntl.h> |
27 | | #include <resolv.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | #include <termios.h> |
31 | | #include <time.h> |
32 | | #include <unistd.h> |
33 | | |
34 | | #include "tmux.h" |
35 | | |
36 | | static int tty_log_fd = -1; |
37 | | |
38 | | static void tty_set_italics(struct tty *); |
39 | | static int tty_try_colour(struct tty *, int, const char *); |
40 | | static void tty_force_cursor_colour(struct tty *, int); |
41 | | static void tty_cursor_pane(struct tty *, const struct tty_ctx *, u_int, |
42 | | u_int); |
43 | | static void tty_cursor_pane_unless_wrap(struct tty *, |
44 | | const struct tty_ctx *, u_int, u_int); |
45 | | static void tty_colours(struct tty *, const struct grid_cell *); |
46 | | static void tty_check_fg(struct tty *, struct colour_palette *, |
47 | | struct grid_cell *); |
48 | | static void tty_check_bg(struct tty *, struct colour_palette *, |
49 | | struct grid_cell *); |
50 | | static void tty_check_us(struct tty *, struct colour_palette *, |
51 | | struct grid_cell *); |
52 | | static void tty_colours_fg(struct tty *, const struct grid_cell *); |
53 | | static void tty_colours_bg(struct tty *, const struct grid_cell *); |
54 | | static void tty_colours_us(struct tty *, const struct grid_cell *); |
55 | | |
56 | | static void tty_region_pane(struct tty *, const struct tty_ctx *, u_int, |
57 | | u_int); |
58 | | static void tty_region(struct tty *, u_int, u_int); |
59 | | static void tty_margin_pane(struct tty *, const struct tty_ctx *); |
60 | | static void tty_margin(struct tty *, u_int, u_int); |
61 | | static int tty_large_region(struct tty *, const struct tty_ctx *); |
62 | | static int tty_fake_bce(const struct tty *, const struct grid_cell *, |
63 | | u_int); |
64 | | static void tty_redraw_region(struct tty *, const struct tty_ctx *); |
65 | | static void tty_emulate_repeat(struct tty *, enum tty_code_code, |
66 | | enum tty_code_code, u_int); |
67 | | static void tty_repeat_space(struct tty *, u_int); |
68 | | static void tty_draw_pane(struct tty *, const struct tty_ctx *, u_int); |
69 | | static void tty_default_attributes(struct tty *, const struct grid_cell *, |
70 | | struct colour_palette *, u_int, struct hyperlinks *); |
71 | | static int tty_check_overlay(struct tty *, u_int, u_int); |
72 | | static void tty_check_overlay_range(struct tty *, u_int, u_int, u_int, |
73 | | struct overlay_ranges *); |
74 | | |
75 | | #ifdef ENABLE_SIXEL |
76 | | static void tty_write_one(void (*)(struct tty *, const struct tty_ctx *), |
77 | | struct client *, struct tty_ctx *); |
78 | | #endif |
79 | | |
80 | | #define tty_use_margin(tty) \ |
81 | 0 | (tty->term->flags & TERM_DECSLRM) |
82 | | #define tty_full_width(tty, ctx) \ |
83 | 0 | ((ctx)->xoff == 0 && (ctx)->sx >= (tty)->sx) |
84 | | |
85 | 0 | #define TTY_BLOCK_INTERVAL (100000 /* 100 milliseconds */) |
86 | 0 | #define TTY_BLOCK_START(tty) (1 + ((tty)->sx * (tty)->sy) * 8) |
87 | 0 | #define TTY_BLOCK_STOP(tty) (1 + ((tty)->sx * (tty)->sy) / 8) |
88 | | |
89 | 0 | #define TTY_QUERY_TIMEOUT 5 |
90 | 0 | #define TTY_REQUEST_LIMIT 30 |
91 | | |
92 | | void |
93 | | tty_create_log(void) |
94 | 0 | { |
95 | 0 | char name[64]; |
96 | |
|
97 | 0 | xsnprintf(name, sizeof name, "tmux-out-%ld.log", (long)getpid()); |
98 | |
|
99 | 0 | tty_log_fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
100 | 0 | if (tty_log_fd != -1 && fcntl(tty_log_fd, F_SETFD, FD_CLOEXEC) == -1) |
101 | 0 | fatal("fcntl failed"); |
102 | 0 | } |
103 | | |
104 | | int |
105 | | tty_init(struct tty *tty, struct client *c) |
106 | 0 | { |
107 | 0 | if (!isatty(c->fd)) |
108 | 0 | return (-1); |
109 | | |
110 | 0 | memset(tty, 0, sizeof *tty); |
111 | 0 | tty->client = c; |
112 | |
|
113 | 0 | tty->cstyle = SCREEN_CURSOR_DEFAULT; |
114 | 0 | tty->ccolour = -1; |
115 | 0 | tty->fg = tty->bg = -1; |
116 | |
|
117 | 0 | if (tcgetattr(c->fd, &tty->tio) != 0) |
118 | 0 | return (-1); |
119 | 0 | return (0); |
120 | 0 | } |
121 | | |
122 | | void |
123 | | tty_resize(struct tty *tty) |
124 | 0 | { |
125 | 0 | struct client *c = tty->client; |
126 | 0 | struct winsize ws; |
127 | 0 | u_int sx, sy, xpixel, ypixel; |
128 | |
|
129 | 0 | if (ioctl(c->fd, TIOCGWINSZ, &ws) != -1) { |
130 | 0 | sx = ws.ws_col; |
131 | 0 | if (sx == 0) { |
132 | 0 | sx = 80; |
133 | 0 | xpixel = 0; |
134 | 0 | } else |
135 | 0 | xpixel = ws.ws_xpixel / sx; |
136 | 0 | sy = ws.ws_row; |
137 | 0 | if (sy == 0) { |
138 | 0 | sy = 24; |
139 | 0 | ypixel = 0; |
140 | 0 | } else |
141 | 0 | ypixel = ws.ws_ypixel / sy; |
142 | |
|
143 | 0 | if ((xpixel == 0 || ypixel == 0) && |
144 | 0 | tty->out != NULL && |
145 | 0 | !(tty->flags & TTY_WINSIZEQUERY) && |
146 | 0 | (tty->term->flags & TERM_VT100LIKE)) { |
147 | 0 | tty_puts(tty, "\033[18t\033[14t"); |
148 | 0 | tty->flags |= TTY_WINSIZEQUERY; |
149 | 0 | } |
150 | 0 | } else { |
151 | 0 | sx = 80; |
152 | 0 | sy = 24; |
153 | 0 | xpixel = 0; |
154 | 0 | ypixel = 0; |
155 | 0 | } |
156 | 0 | log_debug("%s: %s now %ux%u (%ux%u)", __func__, c->name, sx, sy, |
157 | 0 | xpixel, ypixel); |
158 | 0 | tty_set_size(tty, sx, sy, xpixel, ypixel); |
159 | 0 | tty_invalidate(tty); |
160 | 0 | } |
161 | | |
162 | | void |
163 | | tty_set_size(struct tty *tty, u_int sx, u_int sy, u_int xpixel, u_int ypixel) |
164 | 0 | { |
165 | 0 | tty->sx = sx; |
166 | 0 | tty->sy = sy; |
167 | 0 | tty->xpixel = xpixel; |
168 | 0 | tty->ypixel = ypixel; |
169 | 0 | } |
170 | | |
171 | | static void |
172 | | tty_read_callback(__unused int fd, __unused short events, void *data) |
173 | 0 | { |
174 | 0 | struct tty *tty = data; |
175 | 0 | struct client *c = tty->client; |
176 | 0 | const char *name = c->name; |
177 | 0 | size_t size = EVBUFFER_LENGTH(tty->in); |
178 | 0 | int nread; |
179 | |
|
180 | 0 | nread = evbuffer_read(tty->in, c->fd, -1); |
181 | 0 | if (nread == 0 || nread == -1) { |
182 | 0 | if (nread == 0) |
183 | 0 | log_debug("%s: read closed", name); |
184 | 0 | else |
185 | 0 | log_debug("%s: read error: %s", name, strerror(errno)); |
186 | 0 | event_del(&tty->event_in); |
187 | 0 | server_client_lost(tty->client); |
188 | 0 | return; |
189 | 0 | } |
190 | 0 | log_debug("%s: read %d bytes (already %zu)", name, nread, size); |
191 | |
|
192 | 0 | while (tty_keys_next(tty)) |
193 | 0 | ; |
194 | 0 | } |
195 | | |
196 | | static void |
197 | | tty_timer_callback(__unused int fd, __unused short events, void *data) |
198 | 0 | { |
199 | 0 | struct tty *tty = data; |
200 | 0 | struct client *c = tty->client; |
201 | 0 | struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL }; |
202 | |
|
203 | 0 | log_debug("%s: %zu discarded", c->name, tty->discarded); |
204 | |
|
205 | 0 | c->flags |= CLIENT_ALLREDRAWFLAGS; |
206 | 0 | c->discarded += tty->discarded; |
207 | |
|
208 | 0 | if (tty->discarded < TTY_BLOCK_STOP(tty)) { |
209 | 0 | tty->flags &= ~TTY_BLOCK; |
210 | 0 | tty_invalidate(tty); |
211 | 0 | return; |
212 | 0 | } |
213 | 0 | tty->discarded = 0; |
214 | 0 | evtimer_add(&tty->timer, &tv); |
215 | 0 | } |
216 | | |
217 | | static int |
218 | | tty_block_maybe(struct tty *tty) |
219 | 0 | { |
220 | 0 | struct client *c = tty->client; |
221 | 0 | size_t size = EVBUFFER_LENGTH(tty->out); |
222 | 0 | struct timeval tv = { .tv_usec = TTY_BLOCK_INTERVAL }; |
223 | |
|
224 | 0 | if (size == 0) |
225 | 0 | tty->flags &= ~TTY_NOBLOCK; |
226 | 0 | else if (tty->flags & TTY_NOBLOCK) |
227 | 0 | return (0); |
228 | | |
229 | 0 | if (size < TTY_BLOCK_START(tty)) |
230 | 0 | return (0); |
231 | | |
232 | 0 | if (tty->flags & TTY_BLOCK) |
233 | 0 | return (1); |
234 | 0 | tty->flags |= TTY_BLOCK; |
235 | |
|
236 | 0 | log_debug("%s: can't keep up, %zu discarded", c->name, size); |
237 | |
|
238 | 0 | evbuffer_drain(tty->out, size); |
239 | 0 | c->discarded += size; |
240 | |
|
241 | 0 | tty->discarded = 0; |
242 | 0 | evtimer_add(&tty->timer, &tv); |
243 | 0 | return (1); |
244 | 0 | } |
245 | | |
246 | | static void |
247 | | tty_write_callback(__unused int fd, __unused short events, void *data) |
248 | 0 | { |
249 | 0 | struct tty *tty = data; |
250 | 0 | struct client *c = tty->client; |
251 | 0 | size_t size = EVBUFFER_LENGTH(tty->out); |
252 | 0 | int nwrite; |
253 | |
|
254 | 0 | nwrite = evbuffer_write(tty->out, c->fd); |
255 | 0 | if (nwrite == -1) |
256 | 0 | return; |
257 | 0 | log_debug("%s: wrote %d bytes (of %zu)", c->name, nwrite, size); |
258 | |
|
259 | 0 | if (c->redraw > 0) { |
260 | 0 | if ((size_t)nwrite >= c->redraw) |
261 | 0 | c->redraw = 0; |
262 | 0 | else |
263 | 0 | c->redraw -= nwrite; |
264 | 0 | log_debug("%s: waiting for redraw, %zu bytes left", c->name, |
265 | 0 | c->redraw); |
266 | 0 | } else if (tty_block_maybe(tty)) |
267 | 0 | return; |
268 | | |
269 | 0 | if (EVBUFFER_LENGTH(tty->out) != 0) |
270 | 0 | event_add(&tty->event_out, NULL); |
271 | 0 | } |
272 | | |
273 | | int |
274 | | tty_open(struct tty *tty, char **cause) |
275 | 0 | { |
276 | 0 | struct client *c = tty->client; |
277 | |
|
278 | 0 | tty->term = tty_term_create(tty, c->term_name, c->term_caps, |
279 | 0 | c->term_ncaps, &c->term_features, cause); |
280 | 0 | if (tty->term == NULL) { |
281 | 0 | tty_close(tty); |
282 | 0 | return (-1); |
283 | 0 | } |
284 | 0 | tty->flags |= TTY_OPENED; |
285 | |
|
286 | 0 | tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_BLOCK|TTY_TIMER); |
287 | |
|
288 | 0 | event_set(&tty->event_in, c->fd, EV_PERSIST|EV_READ, |
289 | 0 | tty_read_callback, tty); |
290 | 0 | tty->in = evbuffer_new(); |
291 | 0 | if (tty->in == NULL) |
292 | 0 | fatal("out of memory"); |
293 | | |
294 | 0 | event_set(&tty->event_out, c->fd, EV_WRITE, tty_write_callback, tty); |
295 | 0 | tty->out = evbuffer_new(); |
296 | 0 | if (tty->out == NULL) |
297 | 0 | fatal("out of memory"); |
298 | | |
299 | 0 | evtimer_set(&tty->timer, tty_timer_callback, tty); |
300 | |
|
301 | 0 | tty_start_tty(tty); |
302 | 0 | tty_keys_build(tty); |
303 | |
|
304 | 0 | return (0); |
305 | 0 | } |
306 | | |
307 | | static void |
308 | | tty_start_timer_callback(__unused int fd, __unused short events, void *data) |
309 | 0 | { |
310 | 0 | struct tty *tty = data; |
311 | 0 | struct client *c = tty->client; |
312 | |
|
313 | 0 | log_debug("%s: start timer fired", c->name); |
314 | |
|
315 | 0 | if ((tty->flags & (TTY_HAVEDA|TTY_HAVEDA2|TTY_HAVEXDA)) == 0) |
316 | 0 | tty_update_features(tty); |
317 | 0 | tty->flags |= TTY_ALL_REQUEST_FLAGS; |
318 | |
|
319 | 0 | tty->flags &= ~(TTY_WAITBG|TTY_WAITFG); |
320 | 0 | } |
321 | | |
322 | | static void |
323 | | tty_start_start_timer(struct tty *tty) |
324 | 0 | { |
325 | 0 | struct client *c = tty->client; |
326 | 0 | struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT }; |
327 | |
|
328 | 0 | log_debug("%s: start timer started", c->name); |
329 | 0 | evtimer_del(&tty->start_timer); |
330 | 0 | evtimer_set(&tty->start_timer, tty_start_timer_callback, tty); |
331 | 0 | evtimer_add(&tty->start_timer, &tv); |
332 | 0 | } |
333 | | |
334 | | void |
335 | | tty_start_tty(struct tty *tty) |
336 | 0 | { |
337 | 0 | struct client *c = tty->client; |
338 | 0 | struct termios tio; |
339 | |
|
340 | 0 | setblocking(c->fd, 0); |
341 | 0 | event_add(&tty->event_in, NULL); |
342 | |
|
343 | 0 | memcpy(&tio, &tty->tio, sizeof tio); |
344 | 0 | tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP); |
345 | 0 | tio.c_iflag |= IGNBRK; |
346 | 0 | tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET); |
347 | 0 | tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|ECHOPRT| |
348 | 0 | ECHOKE|ISIG); |
349 | 0 | tio.c_cc[VMIN] = 1; |
350 | 0 | tio.c_cc[VTIME] = 0; |
351 | 0 | if (tcsetattr(c->fd, TCSANOW, &tio) == 0) |
352 | 0 | tcflush(c->fd, TCOFLUSH); |
353 | |
|
354 | 0 | tty_putcode(tty, TTYC_SMCUP); |
355 | |
|
356 | 0 | tty_putcode(tty, TTYC_SMKX); |
357 | 0 | tty_putcode(tty, TTYC_CLEAR); |
358 | |
|
359 | 0 | if (tty_acs_needed(tty)) { |
360 | 0 | log_debug("%s: using capabilities for ACS", c->name); |
361 | 0 | tty_putcode(tty, TTYC_ENACS); |
362 | 0 | } else |
363 | 0 | log_debug("%s: using UTF-8 for ACS", c->name); |
364 | |
|
365 | 0 | tty_putcode(tty, TTYC_CNORM); |
366 | 0 | if (tty_term_has(tty->term, TTYC_KMOUS)) { |
367 | 0 | tty_puts(tty, "\033[?1000l\033[?1002l\033[?1003l"); |
368 | 0 | tty_puts(tty, "\033[?1006l\033[?1005l"); |
369 | 0 | } |
370 | 0 | if (tty_term_has(tty->term, TTYC_ENBP)) |
371 | 0 | tty_putcode(tty, TTYC_ENBP); |
372 | |
|
373 | 0 | if (tty->term->flags & TERM_VT100LIKE) { |
374 | | /* Subscribe to theme changes and request theme now. */ |
375 | 0 | tty_puts(tty, "\033[?2031h\033[?996n"); |
376 | 0 | } |
377 | |
|
378 | 0 | tty_start_start_timer(tty); |
379 | |
|
380 | 0 | tty->flags |= TTY_STARTED; |
381 | 0 | tty_invalidate(tty); |
382 | |
|
383 | 0 | if (tty->ccolour != -1) |
384 | 0 | tty_force_cursor_colour(tty, -1); |
385 | |
|
386 | 0 | tty->mouse_drag_flag = 0; |
387 | 0 | tty->mouse_drag_update = NULL; |
388 | 0 | tty->mouse_drag_release = NULL; |
389 | 0 | } |
390 | | |
391 | | void |
392 | | tty_send_requests(struct tty *tty) |
393 | 0 | { |
394 | 0 | if (~tty->flags & TTY_STARTED) |
395 | 0 | return; |
396 | | |
397 | 0 | if (tty->term->flags & TERM_VT100LIKE) { |
398 | 0 | if (~tty->flags & TTY_HAVEDA) |
399 | 0 | tty_puts(tty, "\033[c"); |
400 | 0 | if (~tty->flags & TTY_HAVEDA2) |
401 | 0 | tty_puts(tty, "\033[>c"); |
402 | 0 | if (~tty->flags & TTY_HAVEXDA) |
403 | 0 | tty_puts(tty, "\033[>q"); |
404 | 0 | tty_puts(tty, "\033]10;?\033\\\033]11;?\033\\"); |
405 | 0 | tty->flags |= (TTY_WAITBG|TTY_WAITFG); |
406 | 0 | } else |
407 | 0 | tty->flags |= TTY_ALL_REQUEST_FLAGS; |
408 | 0 | tty->last_requests = time(NULL); |
409 | 0 | } |
410 | | |
411 | | void |
412 | | tty_repeat_requests(struct tty *tty, int force) |
413 | 0 | { |
414 | 0 | struct client *c = tty->client; |
415 | 0 | time_t t = time(NULL); |
416 | 0 | u_int n = t - tty->last_requests; |
417 | |
|
418 | 0 | if (~tty->flags & TTY_STARTED) |
419 | 0 | return; |
420 | | |
421 | 0 | if (!force && n <= TTY_REQUEST_LIMIT) { |
422 | 0 | log_debug("%s: not repeating requests (%u seconds)", c->name, |
423 | 0 | n); |
424 | 0 | return; |
425 | 0 | } |
426 | 0 | log_debug("%s: %srepeating requests (%u seconds)", c->name, |
427 | 0 | force ? "(force) " : "" , n); |
428 | 0 | tty->last_requests = t; |
429 | |
|
430 | 0 | if (tty->term->flags & TERM_VT100LIKE) { |
431 | 0 | tty_puts(tty, "\033]10;?\033\\\033]11;?\033\\"); |
432 | 0 | tty->flags |= (TTY_WAITBG|TTY_WAITFG); |
433 | 0 | } |
434 | 0 | tty_start_start_timer(tty); |
435 | 0 | } |
436 | | |
437 | | void |
438 | | tty_stop_tty(struct tty *tty) |
439 | 0 | { |
440 | 0 | struct client *c = tty->client; |
441 | 0 | struct winsize ws; |
442 | |
|
443 | 0 | if (!(tty->flags & TTY_STARTED)) |
444 | 0 | return; |
445 | 0 | tty->flags &= ~TTY_STARTED; |
446 | |
|
447 | 0 | evtimer_del(&tty->start_timer); |
448 | |
|
449 | 0 | event_del(&tty->timer); |
450 | 0 | tty->flags &= ~TTY_BLOCK; |
451 | |
|
452 | 0 | event_del(&tty->event_in); |
453 | 0 | event_del(&tty->event_out); |
454 | | |
455 | | /* |
456 | | * Be flexible about error handling and try not kill the server just |
457 | | * because the fd is invalid. Things like ssh -t can easily leave us |
458 | | * with a dead tty. |
459 | | */ |
460 | 0 | if (ioctl(c->fd, TIOCGWINSZ, &ws) == -1) |
461 | 0 | return; |
462 | 0 | if (tcsetattr(c->fd, TCSANOW, &tty->tio) == -1) |
463 | 0 | return; |
464 | | |
465 | 0 | tty_raw(tty, tty_term_string_ii(tty->term, TTYC_CSR, 0, ws.ws_row - 1)); |
466 | 0 | if (tty_acs_needed(tty)) |
467 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS)); |
468 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0)); |
469 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX)); |
470 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR)); |
471 | 0 | if (tty->cstyle != SCREEN_CURSOR_DEFAULT) { |
472 | 0 | if (tty_term_has(tty->term, TTYC_SE)) |
473 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_SE)); |
474 | 0 | else if (tty_term_has(tty->term, TTYC_SS)) |
475 | 0 | tty_raw(tty, tty_term_string_i(tty->term, TTYC_SS, 0)); |
476 | 0 | } |
477 | 0 | if (tty->ccolour != -1) |
478 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_CR)); |
479 | |
|
480 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM)); |
481 | 0 | if (tty_term_has(tty->term, TTYC_KMOUS)) { |
482 | 0 | tty_raw(tty, "\033[?1000l\033[?1002l\033[?1003l"); |
483 | 0 | tty_raw(tty, "\033[?1006l\033[?1005l"); |
484 | 0 | } |
485 | 0 | if (tty_term_has(tty->term, TTYC_DSBP)) |
486 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_DSBP)); |
487 | |
|
488 | 0 | if (tty->term->flags & TERM_VT100LIKE) |
489 | 0 | tty_raw(tty, "\033[?7727l"); |
490 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_DSFCS)); |
491 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_DSEKS)); |
492 | |
|
493 | 0 | if (tty_use_margin(tty)) |
494 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_DSMG)); |
495 | 0 | tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP)); |
496 | |
|
497 | 0 | if (tty->term->flags & TERM_VT100LIKE) |
498 | 0 | tty_raw(tty, "\033[?2031l"); |
499 | |
|
500 | 0 | setblocking(c->fd, 1); |
501 | 0 | } |
502 | | |
503 | | void |
504 | | tty_close(struct tty *tty) |
505 | 0 | { |
506 | 0 | if (event_initialized(&tty->key_timer)) |
507 | 0 | evtimer_del(&tty->key_timer); |
508 | 0 | tty_stop_tty(tty); |
509 | |
|
510 | 0 | if (tty->flags & TTY_OPENED) { |
511 | 0 | evbuffer_free(tty->in); |
512 | 0 | event_del(&tty->event_in); |
513 | 0 | evbuffer_free(tty->out); |
514 | 0 | event_del(&tty->event_out); |
515 | |
|
516 | 0 | tty_term_free(tty->term); |
517 | 0 | tty_keys_free(tty); |
518 | |
|
519 | 0 | tty->flags &= ~TTY_OPENED; |
520 | 0 | } |
521 | 0 | } |
522 | | |
523 | | void |
524 | | tty_free(struct tty *tty) |
525 | 0 | { |
526 | 0 | tty_close(tty); |
527 | 0 | } |
528 | | |
529 | | void |
530 | | tty_update_features(struct tty *tty) |
531 | 0 | { |
532 | 0 | struct client *c = tty->client; |
533 | |
|
534 | 0 | if (tty_apply_features(tty->term, c->term_features)) |
535 | 0 | tty_term_apply_overrides(tty->term); |
536 | |
|
537 | 0 | if (tty_use_margin(tty)) |
538 | 0 | tty_putcode(tty, TTYC_ENMG); |
539 | 0 | if (options_get_number(global_options, "extended-keys")) |
540 | 0 | tty_puts(tty, tty_term_string(tty->term, TTYC_ENEKS)); |
541 | 0 | if (options_get_number(global_options, "focus-events")) |
542 | 0 | tty_puts(tty, tty_term_string(tty->term, TTYC_ENFCS)); |
543 | 0 | if (tty->term->flags & TERM_VT100LIKE) |
544 | 0 | tty_puts(tty, "\033[?7727h"); |
545 | | |
546 | | /* |
547 | | * Features might have changed since the first draw during attach. For |
548 | | * example, this happens when DA responses are received. |
549 | | */ |
550 | 0 | server_redraw_client(c); |
551 | |
|
552 | 0 | tty_invalidate(tty); |
553 | 0 | } |
554 | | |
555 | | void |
556 | | tty_raw(struct tty *tty, const char *s) |
557 | 0 | { |
558 | 0 | struct client *c = tty->client; |
559 | 0 | ssize_t n, slen; |
560 | 0 | u_int i; |
561 | |
|
562 | 0 | slen = strlen(s); |
563 | 0 | for (i = 0; i < 5; i++) { |
564 | 0 | n = write(c->fd, s, slen); |
565 | 0 | if (n >= 0) { |
566 | 0 | s += n; |
567 | 0 | slen -= n; |
568 | 0 | if (slen == 0) |
569 | 0 | break; |
570 | 0 | } else if (n == -1 && errno != EAGAIN) |
571 | 0 | break; |
572 | 0 | usleep(100); |
573 | 0 | } |
574 | 0 | } |
575 | | |
576 | | void |
577 | | tty_putcode(struct tty *tty, enum tty_code_code code) |
578 | 0 | { |
579 | 0 | tty_puts(tty, tty_term_string(tty->term, code)); |
580 | 0 | } |
581 | | |
582 | | void |
583 | | tty_putcode_i(struct tty *tty, enum tty_code_code code, int a) |
584 | 0 | { |
585 | 0 | if (a < 0) |
586 | 0 | return; |
587 | 0 | tty_puts(tty, tty_term_string_i(tty->term, code, a)); |
588 | 0 | } |
589 | | |
590 | | void |
591 | | tty_putcode_ii(struct tty *tty, enum tty_code_code code, int a, int b) |
592 | 0 | { |
593 | 0 | if (a < 0 || b < 0) |
594 | 0 | return; |
595 | 0 | tty_puts(tty, tty_term_string_ii(tty->term, code, a, b)); |
596 | 0 | } |
597 | | |
598 | | void |
599 | | tty_putcode_iii(struct tty *tty, enum tty_code_code code, int a, int b, int c) |
600 | 0 | { |
601 | 0 | if (a < 0 || b < 0 || c < 0) |
602 | 0 | return; |
603 | 0 | tty_puts(tty, tty_term_string_iii(tty->term, code, a, b, c)); |
604 | 0 | } |
605 | | |
606 | | void |
607 | | tty_putcode_s(struct tty *tty, enum tty_code_code code, const char *a) |
608 | 0 | { |
609 | 0 | if (a != NULL) |
610 | 0 | tty_puts(tty, tty_term_string_s(tty->term, code, a)); |
611 | 0 | } |
612 | | |
613 | | void |
614 | | tty_putcode_ss(struct tty *tty, enum tty_code_code code, const char *a, |
615 | | const char *b) |
616 | 0 | { |
617 | 0 | if (a != NULL && b != NULL) |
618 | 0 | tty_puts(tty, tty_term_string_ss(tty->term, code, a, b)); |
619 | 0 | } |
620 | | |
621 | | static void |
622 | | tty_add(struct tty *tty, const char *buf, size_t len) |
623 | 0 | { |
624 | 0 | struct client *c = tty->client; |
625 | |
|
626 | 0 | if (tty->flags & TTY_BLOCK) { |
627 | 0 | tty->discarded += len; |
628 | 0 | return; |
629 | 0 | } |
630 | | |
631 | 0 | evbuffer_add(tty->out, buf, len); |
632 | 0 | log_debug("%s: %.*s", c->name, (int)len, buf); |
633 | 0 | c->written += len; |
634 | |
|
635 | 0 | if (tty_log_fd != -1) |
636 | 0 | write(tty_log_fd, buf, len); |
637 | 0 | if (tty->flags & TTY_STARTED) |
638 | 0 | event_add(&tty->event_out, NULL); |
639 | 0 | } |
640 | | |
641 | | void |
642 | | tty_puts(struct tty *tty, const char *s) |
643 | 0 | { |
644 | 0 | if (*s != '\0') |
645 | 0 | tty_add(tty, s, strlen(s)); |
646 | 0 | } |
647 | | |
648 | | void |
649 | | tty_putc(struct tty *tty, u_char ch) |
650 | 0 | { |
651 | 0 | const char *acs; |
652 | |
|
653 | 0 | if ((tty->term->flags & TERM_NOAM) && |
654 | 0 | ch >= 0x20 && ch != 0x7f && |
655 | 0 | tty->cy == tty->sy - 1 && |
656 | 0 | tty->cx + 1 >= tty->sx) |
657 | 0 | return; |
658 | | |
659 | 0 | if (tty->cell.attr & GRID_ATTR_CHARSET) { |
660 | 0 | acs = tty_acs_get(tty, ch); |
661 | 0 | if (acs != NULL) |
662 | 0 | tty_add(tty, acs, strlen(acs)); |
663 | 0 | else |
664 | 0 | tty_add(tty, &ch, 1); |
665 | 0 | } else |
666 | 0 | tty_add(tty, &ch, 1); |
667 | |
|
668 | 0 | if (ch >= 0x20 && ch != 0x7f) { |
669 | 0 | if (tty->cx >= tty->sx) { |
670 | 0 | tty->cx = 1; |
671 | 0 | if (tty->cy != tty->rlower) |
672 | 0 | tty->cy++; |
673 | | |
674 | | /* |
675 | | * On !am terminals, force the cursor position to where |
676 | | * we think it should be after a line wrap - this means |
677 | | * it works on sensible terminals as well. |
678 | | */ |
679 | 0 | if (tty->term->flags & TERM_NOAM) |
680 | 0 | tty_putcode_ii(tty, TTYC_CUP, tty->cy, tty->cx); |
681 | 0 | } else |
682 | 0 | tty->cx++; |
683 | 0 | } |
684 | 0 | } |
685 | | |
686 | | void |
687 | | tty_putn(struct tty *tty, const void *buf, size_t len, u_int width) |
688 | 0 | { |
689 | 0 | if ((tty->term->flags & TERM_NOAM) && |
690 | 0 | tty->cy == tty->sy - 1 && |
691 | 0 | tty->cx + len >= tty->sx) |
692 | 0 | len = tty->sx - tty->cx - 1; |
693 | |
|
694 | 0 | tty_add(tty, buf, len); |
695 | 0 | if (tty->cx + width > tty->sx) { |
696 | 0 | tty->cx = (tty->cx + width) - tty->sx; |
697 | 0 | if (tty->cx <= tty->sx) |
698 | 0 | tty->cy++; |
699 | 0 | else |
700 | 0 | tty->cx = tty->cy = UINT_MAX; |
701 | 0 | } else |
702 | 0 | tty->cx += width; |
703 | 0 | } |
704 | | |
705 | | static void |
706 | | tty_set_italics(struct tty *tty) |
707 | 0 | { |
708 | 0 | const char *s; |
709 | |
|
710 | 0 | if (tty_term_has(tty->term, TTYC_SITM)) { |
711 | 0 | s = options_get_string(global_options, "default-terminal"); |
712 | 0 | if (strcmp(s, "screen") != 0 && strncmp(s, "screen-", 7) != 0) { |
713 | 0 | tty_putcode(tty, TTYC_SITM); |
714 | 0 | return; |
715 | 0 | } |
716 | 0 | } |
717 | 0 | tty_putcode(tty, TTYC_SMSO); |
718 | 0 | } |
719 | | |
720 | | void |
721 | | tty_set_title(struct tty *tty, const char *title) |
722 | 0 | { |
723 | 0 | if (!tty_term_has(tty->term, TTYC_TSL) || |
724 | 0 | !tty_term_has(tty->term, TTYC_FSL)) |
725 | 0 | return; |
726 | | |
727 | 0 | tty_putcode(tty, TTYC_TSL); |
728 | 0 | tty_puts(tty, title); |
729 | 0 | tty_putcode(tty, TTYC_FSL); |
730 | 0 | } |
731 | | |
732 | | void |
733 | | tty_set_path(struct tty *tty, const char *title) |
734 | 0 | { |
735 | 0 | if (!tty_term_has(tty->term, TTYC_SWD) || |
736 | 0 | !tty_term_has(tty->term, TTYC_FSL)) |
737 | 0 | return; |
738 | | |
739 | 0 | tty_putcode(tty, TTYC_SWD); |
740 | 0 | tty_puts(tty, title); |
741 | 0 | tty_putcode(tty, TTYC_FSL); |
742 | 0 | } |
743 | | |
744 | | static void |
745 | | tty_force_cursor_colour(struct tty *tty, int c) |
746 | 0 | { |
747 | 0 | u_char r, g, b; |
748 | 0 | char s[13]; |
749 | |
|
750 | 0 | if (c != -1) |
751 | 0 | c = colour_force_rgb(c); |
752 | 0 | if (c == tty->ccolour) |
753 | 0 | return; |
754 | 0 | if (c == -1) |
755 | 0 | tty_putcode(tty, TTYC_CR); |
756 | 0 | else { |
757 | 0 | colour_split_rgb(c, &r, &g, &b); |
758 | 0 | xsnprintf(s, sizeof s, "rgb:%02hhx/%02hhx/%02hhx", r, g, b); |
759 | 0 | tty_putcode_s(tty, TTYC_CS, s); |
760 | 0 | } |
761 | 0 | tty->ccolour = c; |
762 | 0 | } |
763 | | |
764 | | static int |
765 | | tty_update_cursor(struct tty *tty, int mode, struct screen *s) |
766 | 0 | { |
767 | 0 | enum screen_cursor_style cstyle; |
768 | 0 | int ccolour, changed, cmode = mode; |
769 | | |
770 | | /* Set cursor colour if changed. */ |
771 | 0 | if (s != NULL) { |
772 | 0 | ccolour = s->ccolour; |
773 | 0 | if (s->ccolour == -1) |
774 | 0 | ccolour = s->default_ccolour; |
775 | 0 | tty_force_cursor_colour(tty, ccolour); |
776 | 0 | } |
777 | | |
778 | | /* If cursor is off, set as invisible. */ |
779 | 0 | if (~cmode & MODE_CURSOR) { |
780 | 0 | if (tty->mode & MODE_CURSOR) |
781 | 0 | tty_putcode(tty, TTYC_CIVIS); |
782 | 0 | return (cmode); |
783 | 0 | } |
784 | | |
785 | | /* Check if blinking or very visible flag changed or style changed. */ |
786 | 0 | if (s == NULL) |
787 | 0 | cstyle = tty->cstyle; |
788 | 0 | else { |
789 | 0 | cstyle = s->cstyle; |
790 | 0 | if (cstyle == SCREEN_CURSOR_DEFAULT) { |
791 | 0 | if (~cmode & MODE_CURSOR_BLINKING_SET) { |
792 | 0 | if (s->default_mode & MODE_CURSOR_BLINKING) |
793 | 0 | cmode |= MODE_CURSOR_BLINKING; |
794 | 0 | else |
795 | 0 | cmode &= ~MODE_CURSOR_BLINKING; |
796 | 0 | } |
797 | 0 | cstyle = s->default_cstyle; |
798 | 0 | } |
799 | 0 | } |
800 | | |
801 | | /* If nothing changed, do nothing. */ |
802 | 0 | changed = cmode ^ tty->mode; |
803 | 0 | if ((changed & CURSOR_MODES) == 0 && cstyle == tty->cstyle) |
804 | 0 | return (cmode); |
805 | | |
806 | | /* |
807 | | * Set cursor style. If an explicit style has been set with DECSCUSR, |
808 | | * set it if supported, otherwise send cvvis for blinking styles. |
809 | | * |
810 | | * If no style, has been set (SCREEN_CURSOR_DEFAULT), then send cvvis |
811 | | * if either the blinking or very visible flags are set. |
812 | | */ |
813 | 0 | tty_putcode(tty, TTYC_CNORM); |
814 | 0 | switch (cstyle) { |
815 | 0 | case SCREEN_CURSOR_DEFAULT: |
816 | 0 | if (tty->cstyle != SCREEN_CURSOR_DEFAULT) { |
817 | 0 | if (tty_term_has(tty->term, TTYC_SE)) |
818 | 0 | tty_putcode(tty, TTYC_SE); |
819 | 0 | else |
820 | 0 | tty_putcode_i(tty, TTYC_SS, 0); |
821 | 0 | } |
822 | 0 | if (cmode & (MODE_CURSOR_BLINKING|MODE_CURSOR_VERY_VISIBLE)) |
823 | 0 | tty_putcode(tty, TTYC_CVVIS); |
824 | 0 | break; |
825 | 0 | case SCREEN_CURSOR_BLOCK: |
826 | 0 | if (tty_term_has(tty->term, TTYC_SS)) { |
827 | 0 | if (cmode & MODE_CURSOR_BLINKING) |
828 | 0 | tty_putcode_i(tty, TTYC_SS, 1); |
829 | 0 | else |
830 | 0 | tty_putcode_i(tty, TTYC_SS, 2); |
831 | 0 | } else if (cmode & MODE_CURSOR_BLINKING) |
832 | 0 | tty_putcode(tty, TTYC_CVVIS); |
833 | 0 | break; |
834 | 0 | case SCREEN_CURSOR_UNDERLINE: |
835 | 0 | if (tty_term_has(tty->term, TTYC_SS)) { |
836 | 0 | if (cmode & MODE_CURSOR_BLINKING) |
837 | 0 | tty_putcode_i(tty, TTYC_SS, 3); |
838 | 0 | else |
839 | 0 | tty_putcode_i(tty, TTYC_SS, 4); |
840 | 0 | } else if (cmode & MODE_CURSOR_BLINKING) |
841 | 0 | tty_putcode(tty, TTYC_CVVIS); |
842 | 0 | break; |
843 | 0 | case SCREEN_CURSOR_BAR: |
844 | 0 | if (tty_term_has(tty->term, TTYC_SS)) { |
845 | 0 | if (cmode & MODE_CURSOR_BLINKING) |
846 | 0 | tty_putcode_i(tty, TTYC_SS, 5); |
847 | 0 | else |
848 | 0 | tty_putcode_i(tty, TTYC_SS, 6); |
849 | 0 | } else if (cmode & MODE_CURSOR_BLINKING) |
850 | 0 | tty_putcode(tty, TTYC_CVVIS); |
851 | 0 | break; |
852 | 0 | } |
853 | 0 | tty->cstyle = cstyle; |
854 | 0 | return (cmode); |
855 | 0 | } |
856 | | |
857 | | void |
858 | | tty_update_mode(struct tty *tty, int mode, struct screen *s) |
859 | 0 | { |
860 | 0 | struct tty_term *term = tty->term; |
861 | 0 | struct client *c = tty->client; |
862 | 0 | int changed; |
863 | |
|
864 | 0 | if (tty->flags & TTY_NOCURSOR) |
865 | 0 | mode &= ~MODE_CURSOR; |
866 | |
|
867 | 0 | if (tty_update_cursor(tty, mode, s) & MODE_CURSOR_BLINKING) |
868 | 0 | mode |= MODE_CURSOR_BLINKING; |
869 | 0 | else |
870 | 0 | mode &= ~MODE_CURSOR_BLINKING; |
871 | |
|
872 | 0 | changed = mode ^ tty->mode; |
873 | 0 | if (log_get_level() != 0 && changed != 0) { |
874 | 0 | log_debug("%s: current mode %s", c->name, |
875 | 0 | screen_mode_to_string(tty->mode)); |
876 | 0 | log_debug("%s: setting mode %s", c->name, |
877 | 0 | screen_mode_to_string(mode)); |
878 | 0 | } |
879 | |
|
880 | 0 | if ((changed & ALL_MOUSE_MODES) && tty_term_has(term, TTYC_KMOUS)) { |
881 | | /* |
882 | | * If the mouse modes have changed, clear then all and apply |
883 | | * again. There are differences in how terminals track the |
884 | | * various bits. |
885 | | */ |
886 | 0 | tty_puts(tty, "\033[?1006l\033[?1000l\033[?1002l\033[?1003l"); |
887 | 0 | if (mode & ALL_MOUSE_MODES) |
888 | 0 | tty_puts(tty, "\033[?1006h"); |
889 | 0 | if (mode & MODE_MOUSE_ALL) |
890 | 0 | tty_puts(tty, "\033[?1000h\033[?1002h\033[?1003h"); |
891 | 0 | else if (mode & MODE_MOUSE_BUTTON) |
892 | 0 | tty_puts(tty, "\033[?1000h\033[?1002h"); |
893 | 0 | else if (mode & MODE_MOUSE_STANDARD) |
894 | 0 | tty_puts(tty, "\033[?1000h"); |
895 | 0 | } |
896 | 0 | tty->mode = mode; |
897 | 0 | } |
898 | | |
899 | | static void |
900 | | tty_emulate_repeat(struct tty *tty, enum tty_code_code code, |
901 | | enum tty_code_code code1, u_int n) |
902 | 0 | { |
903 | 0 | if (tty_term_has(tty->term, code)) |
904 | 0 | tty_putcode_i(tty, code, n); |
905 | 0 | else { |
906 | 0 | while (n-- > 0) |
907 | 0 | tty_putcode(tty, code1); |
908 | 0 | } |
909 | 0 | } |
910 | | |
911 | | static void |
912 | | tty_repeat_space(struct tty *tty, u_int n) |
913 | 0 | { |
914 | 0 | static char s[500]; |
915 | |
|
916 | 0 | if (*s != ' ') |
917 | 0 | memset(s, ' ', sizeof s); |
918 | |
|
919 | 0 | while (n > sizeof s) { |
920 | 0 | tty_putn(tty, s, sizeof s, sizeof s); |
921 | 0 | n -= sizeof s; |
922 | 0 | } |
923 | 0 | if (n != 0) |
924 | 0 | tty_putn(tty, s, n, n); |
925 | 0 | } |
926 | | |
927 | | /* Is this window bigger than the terminal? */ |
928 | | int |
929 | | tty_window_bigger(struct tty *tty) |
930 | 0 | { |
931 | 0 | struct client *c = tty->client; |
932 | 0 | struct window *w = c->session->curw->window; |
933 | |
|
934 | 0 | return (tty->sx < w->sx || tty->sy - status_line_size(c) < w->sy); |
935 | 0 | } |
936 | | |
937 | | /* What offset should this window be drawn at? */ |
938 | | int |
939 | | tty_window_offset(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy) |
940 | 0 | { |
941 | 0 | *ox = tty->oox; |
942 | 0 | *oy = tty->ooy; |
943 | 0 | *sx = tty->osx; |
944 | 0 | *sy = tty->osy; |
945 | |
|
946 | 0 | return (tty->oflag); |
947 | 0 | } |
948 | | |
949 | | /* What offset should this window be drawn at? */ |
950 | | static int |
951 | | tty_window_offset1(struct tty *tty, u_int *ox, u_int *oy, u_int *sx, u_int *sy) |
952 | 0 | { |
953 | 0 | struct client *c = tty->client; |
954 | 0 | struct window *w = c->session->curw->window; |
955 | 0 | struct window_pane *wp = server_client_get_pane(c); |
956 | 0 | u_int cx, cy, lines; |
957 | |
|
958 | 0 | lines = status_line_size(c); |
959 | |
|
960 | 0 | if (tty->sx >= w->sx && tty->sy - lines >= w->sy) { |
961 | 0 | *ox = 0; |
962 | 0 | *oy = 0; |
963 | 0 | *sx = w->sx; |
964 | 0 | *sy = w->sy; |
965 | |
|
966 | 0 | c->pan_window = NULL; |
967 | 0 | return (0); |
968 | 0 | } |
969 | | |
970 | 0 | *sx = tty->sx; |
971 | 0 | *sy = tty->sy - lines; |
972 | |
|
973 | 0 | if (c->pan_window == w) { |
974 | 0 | if (*sx >= w->sx) |
975 | 0 | c->pan_ox = 0; |
976 | 0 | else if (c->pan_ox + *sx > w->sx) |
977 | 0 | c->pan_ox = w->sx - *sx; |
978 | 0 | *ox = c->pan_ox; |
979 | 0 | if (*sy >= w->sy) |
980 | 0 | c->pan_oy = 0; |
981 | 0 | else if (c->pan_oy + *sy > w->sy) |
982 | 0 | c->pan_oy = w->sy - *sy; |
983 | 0 | *oy = c->pan_oy; |
984 | 0 | return (1); |
985 | 0 | } |
986 | | |
987 | 0 | if (~wp->screen->mode & MODE_CURSOR) { |
988 | 0 | *ox = 0; |
989 | 0 | *oy = 0; |
990 | 0 | } else { |
991 | 0 | cx = wp->xoff + wp->screen->cx; |
992 | 0 | cy = wp->yoff + wp->screen->cy; |
993 | |
|
994 | 0 | if (cx < *sx) |
995 | 0 | *ox = 0; |
996 | 0 | else if (cx > w->sx - *sx) |
997 | 0 | *ox = w->sx - *sx; |
998 | 0 | else |
999 | 0 | *ox = cx - *sx / 2; |
1000 | |
|
1001 | 0 | if (cy < *sy) |
1002 | 0 | *oy = 0; |
1003 | 0 | else if (cy > w->sy - *sy) |
1004 | 0 | *oy = w->sy - *sy; |
1005 | 0 | else |
1006 | 0 | *oy = cy - *sy / 2; |
1007 | 0 | } |
1008 | |
|
1009 | 0 | c->pan_window = NULL; |
1010 | 0 | return (1); |
1011 | 0 | } |
1012 | | |
1013 | | /* Update stored offsets for a window and redraw if necessary. */ |
1014 | | void |
1015 | | tty_update_window_offset(struct window *w) |
1016 | 0 | { |
1017 | 0 | struct client *c; |
1018 | |
|
1019 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1020 | 0 | if (c->session != NULL && |
1021 | 0 | c->session->curw != NULL && |
1022 | 0 | c->session->curw->window == w) |
1023 | 0 | tty_update_client_offset(c); |
1024 | 0 | } |
1025 | 0 | } |
1026 | | |
1027 | | /* Update stored offsets for a client and redraw if necessary. */ |
1028 | | void |
1029 | | tty_update_client_offset(struct client *c) |
1030 | 0 | { |
1031 | 0 | u_int ox, oy, sx, sy; |
1032 | |
|
1033 | 0 | if (~c->flags & CLIENT_TERMINAL) |
1034 | 0 | return; |
1035 | | |
1036 | 0 | c->tty.oflag = tty_window_offset1(&c->tty, &ox, &oy, &sx, &sy); |
1037 | 0 | if (ox == c->tty.oox && |
1038 | 0 | oy == c->tty.ooy && |
1039 | 0 | sx == c->tty.osx && |
1040 | 0 | sy == c->tty.osy) |
1041 | 0 | return; |
1042 | | |
1043 | 0 | log_debug ("%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)", |
1044 | 0 | __func__, c->name, c->tty.oox, c->tty.ooy, c->tty.osx, c->tty.osy, |
1045 | 0 | ox, oy, sx, sy); |
1046 | |
|
1047 | 0 | c->tty.oox = ox; |
1048 | 0 | c->tty.ooy = oy; |
1049 | 0 | c->tty.osx = sx; |
1050 | 0 | c->tty.osy = sy; |
1051 | |
|
1052 | 0 | c->flags |= (CLIENT_REDRAWWINDOW|CLIENT_REDRAWSTATUS); |
1053 | 0 | } |
1054 | | |
1055 | | /* |
1056 | | * Is the region large enough to be worth redrawing once later rather than |
1057 | | * probably several times now? Currently yes if it is more than 50% of the |
1058 | | * pane. |
1059 | | */ |
1060 | | static int |
1061 | | tty_large_region(__unused struct tty *tty, const struct tty_ctx *ctx) |
1062 | 0 | { |
1063 | 0 | return (ctx->orlower - ctx->orupper >= ctx->sy / 2); |
1064 | 0 | } |
1065 | | |
1066 | | /* |
1067 | | * Return if BCE is needed but the terminal doesn't have it - it'll need to be |
1068 | | * emulated. |
1069 | | */ |
1070 | | static int |
1071 | | tty_fake_bce(const struct tty *tty, const struct grid_cell *gc, u_int bg) |
1072 | 0 | { |
1073 | 0 | if (tty_term_flag(tty->term, TTYC_BCE)) |
1074 | 0 | return (0); |
1075 | 0 | if (!COLOUR_DEFAULT(bg) || !COLOUR_DEFAULT(gc->bg)) |
1076 | 0 | return (1); |
1077 | 0 | return (0); |
1078 | 0 | } |
1079 | | |
1080 | | /* |
1081 | | * Redraw scroll region using data from screen (already updated). Used when |
1082 | | * CSR not supported, or window is a pane that doesn't take up the full |
1083 | | * width of the terminal. |
1084 | | */ |
1085 | | static void |
1086 | | tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx) |
1087 | 0 | { |
1088 | 0 | struct client *c = tty->client; |
1089 | 0 | u_int i; |
1090 | | |
1091 | | /* |
1092 | | * If region is large, schedule a redraw. In most cases this is likely |
1093 | | * to be followed by some more scrolling. |
1094 | | */ |
1095 | 0 | if (tty_large_region(tty, ctx)) { |
1096 | 0 | log_debug("%s: %s large redraw", __func__, c->name); |
1097 | 0 | ctx->redraw_cb(ctx); |
1098 | 0 | return; |
1099 | 0 | } |
1100 | | |
1101 | 0 | for (i = ctx->orupper; i <= ctx->orlower; i++) |
1102 | 0 | tty_draw_pane(tty, ctx, i); |
1103 | 0 | } |
1104 | | |
1105 | | /* Is this position visible in the pane? */ |
1106 | | static int |
1107 | | tty_is_visible(__unused struct tty *tty, const struct tty_ctx *ctx, u_int px, |
1108 | | u_int py, u_int nx, u_int ny) |
1109 | 0 | { |
1110 | 0 | u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py; |
1111 | |
|
1112 | 0 | if (!ctx->bigger) |
1113 | 0 | return (1); |
1114 | | |
1115 | 0 | if (xoff + nx <= ctx->wox || xoff >= ctx->wox + ctx->wsx || |
1116 | 0 | yoff + ny <= ctx->woy || yoff >= ctx->woy + ctx->wsy) |
1117 | 0 | return (0); |
1118 | 0 | return (1); |
1119 | 0 | } |
1120 | | |
1121 | | /* Clamp line position to visible part of pane. */ |
1122 | | static int |
1123 | | tty_clamp_line(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py, |
1124 | | u_int nx, u_int *i, u_int *x, u_int *rx, u_int *ry) |
1125 | 0 | { |
1126 | 0 | u_int xoff = ctx->rxoff + px; |
1127 | |
|
1128 | 0 | if (!tty_is_visible(tty, ctx, px, py, nx, 1)) |
1129 | 0 | return (0); |
1130 | 0 | *ry = ctx->yoff + py - ctx->woy; |
1131 | |
|
1132 | 0 | if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) { |
1133 | | /* All visible. */ |
1134 | 0 | *i = 0; |
1135 | 0 | *x = ctx->xoff + px - ctx->wox; |
1136 | 0 | *rx = nx; |
1137 | 0 | } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) { |
1138 | | /* Both left and right not visible. */ |
1139 | 0 | *i = ctx->wox; |
1140 | 0 | *x = 0; |
1141 | 0 | *rx = ctx->wsx; |
1142 | 0 | } else if (xoff < ctx->wox) { |
1143 | | /* Left not visible. */ |
1144 | 0 | *i = ctx->wox - (ctx->xoff + px); |
1145 | 0 | *x = 0; |
1146 | 0 | *rx = nx - *i; |
1147 | 0 | } else { |
1148 | | /* Right not visible. */ |
1149 | 0 | *i = 0; |
1150 | 0 | *x = (ctx->xoff + px) - ctx->wox; |
1151 | 0 | *rx = ctx->wsx - *x; |
1152 | 0 | } |
1153 | 0 | if (*rx > nx) |
1154 | 0 | fatalx("%s: x too big, %u > %u", __func__, *rx, nx); |
1155 | | |
1156 | 0 | return (1); |
1157 | 0 | } |
1158 | | |
1159 | | /* Clear a line. */ |
1160 | | static void |
1161 | | tty_clear_line(struct tty *tty, const struct grid_cell *defaults, u_int py, |
1162 | | u_int px, u_int nx, u_int bg) |
1163 | 0 | { |
1164 | 0 | struct client *c = tty->client; |
1165 | 0 | struct overlay_ranges r; |
1166 | 0 | u_int i; |
1167 | |
|
1168 | 0 | log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py); |
1169 | | |
1170 | | /* Nothing to clear. */ |
1171 | 0 | if (nx == 0) |
1172 | 0 | return; |
1173 | | |
1174 | | /* If genuine BCE is available, can try escape sequences. */ |
1175 | 0 | if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) { |
1176 | | /* Off the end of the line, use EL if available. */ |
1177 | 0 | if (px + nx >= tty->sx && tty_term_has(tty->term, TTYC_EL)) { |
1178 | 0 | tty_cursor(tty, px, py); |
1179 | 0 | tty_putcode(tty, TTYC_EL); |
1180 | 0 | return; |
1181 | 0 | } |
1182 | | |
1183 | | /* At the start of the line. Use EL1. */ |
1184 | 0 | if (px == 0 && tty_term_has(tty->term, TTYC_EL1)) { |
1185 | 0 | tty_cursor(tty, px + nx - 1, py); |
1186 | 0 | tty_putcode(tty, TTYC_EL1); |
1187 | 0 | return; |
1188 | 0 | } |
1189 | | |
1190 | | /* Section of line. Use ECH if possible. */ |
1191 | 0 | if (tty_term_has(tty->term, TTYC_ECH)) { |
1192 | 0 | tty_cursor(tty, px, py); |
1193 | 0 | tty_putcode_i(tty, TTYC_ECH, nx); |
1194 | 0 | return; |
1195 | 0 | } |
1196 | 0 | } |
1197 | | |
1198 | | /* |
1199 | | * Couldn't use an escape sequence, use spaces. Clear only the visible |
1200 | | * bit if there is an overlay. |
1201 | | */ |
1202 | 0 | tty_check_overlay_range(tty, px, py, nx, &r); |
1203 | 0 | for (i = 0; i < OVERLAY_MAX_RANGES; i++) { |
1204 | 0 | if (r.nx[i] == 0) |
1205 | 0 | continue; |
1206 | 0 | tty_cursor(tty, r.px[i], py); |
1207 | 0 | tty_repeat_space(tty, r.nx[i]); |
1208 | 0 | } |
1209 | 0 | } |
1210 | | |
1211 | | /* Clear a line, adjusting to visible part of pane. */ |
1212 | | static void |
1213 | | tty_clear_pane_line(struct tty *tty, const struct tty_ctx *ctx, u_int py, |
1214 | | u_int px, u_int nx, u_int bg) |
1215 | 0 | { |
1216 | 0 | struct client *c = tty->client; |
1217 | 0 | u_int i, x, rx, ry; |
1218 | |
|
1219 | 0 | log_debug("%s: %s, %u at %u,%u", __func__, c->name, nx, px, py); |
1220 | |
|
1221 | 0 | if (tty_clamp_line(tty, ctx, px, py, nx, &i, &x, &rx, &ry)) |
1222 | 0 | tty_clear_line(tty, &ctx->defaults, ry, x, rx, bg); |
1223 | 0 | } |
1224 | | |
1225 | | /* Clamp area position to visible part of pane. */ |
1226 | | static int |
1227 | | tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py, |
1228 | | u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx, |
1229 | | u_int *ry) |
1230 | 0 | { |
1231 | 0 | u_int xoff = ctx->rxoff + px, yoff = ctx->ryoff + py; |
1232 | |
|
1233 | 0 | if (!tty_is_visible(tty, ctx, px, py, nx, ny)) |
1234 | 0 | return (0); |
1235 | | |
1236 | 0 | if (xoff >= ctx->wox && xoff + nx <= ctx->wox + ctx->wsx) { |
1237 | | /* All visible. */ |
1238 | 0 | *i = 0; |
1239 | 0 | *x = ctx->xoff + px - ctx->wox; |
1240 | 0 | *rx = nx; |
1241 | 0 | } else if (xoff < ctx->wox && xoff + nx > ctx->wox + ctx->wsx) { |
1242 | | /* Both left and right not visible. */ |
1243 | 0 | *i = ctx->wox; |
1244 | 0 | *x = 0; |
1245 | 0 | *rx = ctx->wsx; |
1246 | 0 | } else if (xoff < ctx->wox) { |
1247 | | /* Left not visible. */ |
1248 | 0 | *i = ctx->wox - (ctx->xoff + px); |
1249 | 0 | *x = 0; |
1250 | 0 | *rx = nx - *i; |
1251 | 0 | } else { |
1252 | | /* Right not visible. */ |
1253 | 0 | *i = 0; |
1254 | 0 | *x = (ctx->xoff + px) - ctx->wox; |
1255 | 0 | *rx = ctx->wsx - *x; |
1256 | 0 | } |
1257 | 0 | if (*rx > nx) |
1258 | 0 | fatalx("%s: x too big, %u > %u", __func__, *rx, nx); |
1259 | | |
1260 | 0 | if (yoff >= ctx->woy && yoff + ny <= ctx->woy + ctx->wsy) { |
1261 | | /* All visible. */ |
1262 | 0 | *j = 0; |
1263 | 0 | *y = ctx->yoff + py - ctx->woy; |
1264 | 0 | *ry = ny; |
1265 | 0 | } else if (yoff < ctx->woy && yoff + ny > ctx->woy + ctx->wsy) { |
1266 | | /* Both top and bottom not visible. */ |
1267 | 0 | *j = ctx->woy; |
1268 | 0 | *y = 0; |
1269 | 0 | *ry = ctx->wsy; |
1270 | 0 | } else if (yoff < ctx->woy) { |
1271 | | /* Top not visible. */ |
1272 | 0 | *j = ctx->woy - (ctx->yoff + py); |
1273 | 0 | *y = 0; |
1274 | 0 | *ry = ny - *j; |
1275 | 0 | } else { |
1276 | | /* Bottom not visible. */ |
1277 | 0 | *j = 0; |
1278 | 0 | *y = (ctx->yoff + py) - ctx->woy; |
1279 | 0 | *ry = ctx->wsy - *y; |
1280 | 0 | } |
1281 | 0 | if (*ry > ny) |
1282 | 0 | fatalx("%s: y too big, %u > %u", __func__, *ry, ny); |
1283 | | |
1284 | 0 | return (1); |
1285 | 0 | } |
1286 | | |
1287 | | /* Clear an area, adjusting to visible part of pane. */ |
1288 | | static void |
1289 | | tty_clear_area(struct tty *tty, const struct grid_cell *defaults, u_int py, |
1290 | | u_int ny, u_int px, u_int nx, u_int bg) |
1291 | 0 | { |
1292 | 0 | struct client *c = tty->client; |
1293 | 0 | u_int yy; |
1294 | 0 | char tmp[64]; |
1295 | |
|
1296 | 0 | log_debug("%s: %s, %u,%u at %u,%u", __func__, c->name, nx, ny, px, py); |
1297 | | |
1298 | | /* Nothing to clear. */ |
1299 | 0 | if (nx == 0 || ny == 0) |
1300 | 0 | return; |
1301 | | |
1302 | | /* If genuine BCE is available, can try escape sequences. */ |
1303 | 0 | if (c->overlay_check == NULL && !tty_fake_bce(tty, defaults, bg)) { |
1304 | | /* Use ED if clearing off the bottom of the terminal. */ |
1305 | 0 | if (px == 0 && |
1306 | 0 | px + nx >= tty->sx && |
1307 | 0 | py + ny >= tty->sy && |
1308 | 0 | tty_term_has(tty->term, TTYC_ED)) { |
1309 | 0 | tty_cursor(tty, 0, py); |
1310 | 0 | tty_putcode(tty, TTYC_ED); |
1311 | 0 | return; |
1312 | 0 | } |
1313 | | |
1314 | | /* |
1315 | | * On VT420 compatible terminals we can use DECFRA if the |
1316 | | * background colour isn't default (because it doesn't work |
1317 | | * after SGR 0). |
1318 | | */ |
1319 | 0 | if ((tty->term->flags & TERM_DECFRA) && !COLOUR_DEFAULT(bg)) { |
1320 | 0 | xsnprintf(tmp, sizeof tmp, "\033[32;%u;%u;%u;%u$x", |
1321 | 0 | py + 1, px + 1, py + ny, px + nx); |
1322 | 0 | tty_puts(tty, tmp); |
1323 | 0 | return; |
1324 | 0 | } |
1325 | | |
1326 | | /* Full lines can be scrolled away to clear them. */ |
1327 | 0 | if (px == 0 && |
1328 | 0 | px + nx >= tty->sx && |
1329 | 0 | ny > 2 && |
1330 | 0 | tty_term_has(tty->term, TTYC_CSR) && |
1331 | 0 | tty_term_has(tty->term, TTYC_INDN)) { |
1332 | 0 | tty_region(tty, py, py + ny - 1); |
1333 | 0 | tty_margin_off(tty); |
1334 | 0 | tty_putcode_i(tty, TTYC_INDN, ny); |
1335 | 0 | return; |
1336 | 0 | } |
1337 | | |
1338 | | /* |
1339 | | * If margins are supported, can just scroll the area off to |
1340 | | * clear it. |
1341 | | */ |
1342 | 0 | if (nx > 2 && |
1343 | 0 | ny > 2 && |
1344 | 0 | tty_term_has(tty->term, TTYC_CSR) && |
1345 | 0 | tty_use_margin(tty) && |
1346 | 0 | tty_term_has(tty->term, TTYC_INDN)) { |
1347 | 0 | tty_region(tty, py, py + ny - 1); |
1348 | 0 | tty_margin(tty, px, px + nx - 1); |
1349 | 0 | tty_putcode_i(tty, TTYC_INDN, ny); |
1350 | 0 | return; |
1351 | 0 | } |
1352 | 0 | } |
1353 | | |
1354 | | /* Couldn't use an escape sequence, loop over the lines. */ |
1355 | 0 | for (yy = py; yy < py + ny; yy++) |
1356 | 0 | tty_clear_line(tty, defaults, yy, px, nx, bg); |
1357 | 0 | } |
1358 | | |
1359 | | /* Clear an area in a pane. */ |
1360 | | static void |
1361 | | tty_clear_pane_area(struct tty *tty, const struct tty_ctx *ctx, u_int py, |
1362 | | u_int ny, u_int px, u_int nx, u_int bg) |
1363 | 0 | { |
1364 | 0 | u_int i, j, x, y, rx, ry; |
1365 | |
|
1366 | 0 | if (tty_clamp_area(tty, ctx, px, py, nx, ny, &i, &j, &x, &y, &rx, &ry)) |
1367 | 0 | tty_clear_area(tty, &ctx->defaults, y, ry, x, rx, bg); |
1368 | 0 | } |
1369 | | |
1370 | | static void |
1371 | | tty_draw_pane(struct tty *tty, const struct tty_ctx *ctx, u_int py) |
1372 | 0 | { |
1373 | 0 | struct screen *s = ctx->s; |
1374 | 0 | u_int nx = ctx->sx, i, x, rx, ry; |
1375 | |
|
1376 | 0 | log_debug("%s: %s %u %d", __func__, tty->client->name, py, ctx->bigger); |
1377 | |
|
1378 | 0 | if (!ctx->bigger) { |
1379 | 0 | tty_draw_line(tty, s, 0, py, nx, ctx->xoff, ctx->yoff + py, |
1380 | 0 | &ctx->defaults, ctx->palette); |
1381 | 0 | return; |
1382 | 0 | } |
1383 | 0 | if (tty_clamp_line(tty, ctx, 0, py, nx, &i, &x, &rx, &ry)) { |
1384 | 0 | tty_draw_line(tty, s, i, py, rx, x, ry, &ctx->defaults, |
1385 | 0 | ctx->palette); |
1386 | 0 | } |
1387 | 0 | } |
1388 | | |
1389 | | static const struct grid_cell * |
1390 | | tty_check_codeset(struct tty *tty, const struct grid_cell *gc) |
1391 | 0 | { |
1392 | 0 | static struct grid_cell new; |
1393 | 0 | int c; |
1394 | | |
1395 | | /* Characters less than 0x7f are always fine, no matter what. */ |
1396 | 0 | if (gc->data.size == 1 && *gc->data.data < 0x7f) |
1397 | 0 | return (gc); |
1398 | 0 | if (gc->flags & GRID_FLAG_TAB) |
1399 | 0 | return (gc); |
1400 | | |
1401 | | /* UTF-8 terminal and a UTF-8 character - fine. */ |
1402 | 0 | if (tty->client->flags & CLIENT_UTF8) |
1403 | 0 | return (gc); |
1404 | 0 | memcpy(&new, gc, sizeof new); |
1405 | | |
1406 | | /* See if this can be mapped to an ACS character. */ |
1407 | 0 | c = tty_acs_reverse_get(tty, gc->data.data, gc->data.size); |
1408 | 0 | if (c != -1) { |
1409 | 0 | utf8_set(&new.data, c); |
1410 | 0 | new.attr |= GRID_ATTR_CHARSET; |
1411 | 0 | return (&new); |
1412 | 0 | } |
1413 | | |
1414 | | /* Replace by the right number of underscores. */ |
1415 | 0 | new.data.size = gc->data.width; |
1416 | 0 | if (new.data.size > UTF8_SIZE) |
1417 | 0 | new.data.size = UTF8_SIZE; |
1418 | 0 | memset(new.data.data, '_', new.data.size); |
1419 | 0 | return (&new); |
1420 | 0 | } |
1421 | | |
1422 | | /* |
1423 | | * Check if a single character is obstructed by the overlay and return a |
1424 | | * boolean. |
1425 | | */ |
1426 | | static int |
1427 | | tty_check_overlay(struct tty *tty, u_int px, u_int py) |
1428 | 0 | { |
1429 | 0 | struct overlay_ranges r; |
1430 | | |
1431 | | /* |
1432 | | * A unit width range will always return nx[2] == 0 from a check, even |
1433 | | * with multiple overlays, so it's sufficient to check just the first |
1434 | | * two entries. |
1435 | | */ |
1436 | 0 | tty_check_overlay_range(tty, px, py, 1, &r); |
1437 | 0 | if (r.nx[0] + r.nx[1] == 0) |
1438 | 0 | return (0); |
1439 | 0 | return (1); |
1440 | 0 | } |
1441 | | |
1442 | | /* Return parts of the input range which are visible. */ |
1443 | | static void |
1444 | | tty_check_overlay_range(struct tty *tty, u_int px, u_int py, u_int nx, |
1445 | | struct overlay_ranges *r) |
1446 | 0 | { |
1447 | 0 | struct client *c = tty->client; |
1448 | |
|
1449 | 0 | if (c->overlay_check == NULL) { |
1450 | 0 | r->px[0] = px; |
1451 | 0 | r->nx[0] = nx; |
1452 | 0 | r->px[1] = 0; |
1453 | 0 | r->nx[1] = 0; |
1454 | 0 | r->px[2] = 0; |
1455 | 0 | r->nx[2] = 0; |
1456 | 0 | return; |
1457 | 0 | } |
1458 | | |
1459 | 0 | c->overlay_check(c, c->overlay_data, px, py, nx, r); |
1460 | 0 | } |
1461 | | |
1462 | | void |
1463 | | tty_draw_line(struct tty *tty, struct screen *s, u_int px, u_int py, u_int nx, |
1464 | | u_int atx, u_int aty, const struct grid_cell *defaults, |
1465 | | struct colour_palette *palette) |
1466 | 0 | { |
1467 | 0 | struct grid *gd = s->grid; |
1468 | 0 | struct grid_cell gc, last; |
1469 | 0 | const struct grid_cell *gcp; |
1470 | 0 | struct grid_line *gl; |
1471 | 0 | struct client *c = tty->client; |
1472 | 0 | struct overlay_ranges r; |
1473 | 0 | u_int i, j, ux, sx, width, hidden, eux, nxx; |
1474 | 0 | u_int cellsize; |
1475 | 0 | int flags, cleared = 0, wrapped = 0; |
1476 | 0 | char buf[512]; |
1477 | 0 | size_t len; |
1478 | |
|
1479 | 0 | log_debug("%s: px=%u py=%u nx=%u atx=%u aty=%u", __func__, |
1480 | 0 | px, py, nx, atx, aty); |
1481 | 0 | log_debug("%s: defaults: fg=%d, bg=%d", __func__, defaults->fg, |
1482 | 0 | defaults->bg); |
1483 | | |
1484 | | /* |
1485 | | * py is the line in the screen to draw. |
1486 | | * px is the start x and nx is the width to draw. |
1487 | | * atx,aty is the line on the terminal to draw it. |
1488 | | */ |
1489 | |
|
1490 | 0 | flags = (tty->flags & TTY_NOCURSOR); |
1491 | 0 | tty->flags |= TTY_NOCURSOR; |
1492 | 0 | tty_update_mode(tty, tty->mode, s); |
1493 | |
|
1494 | 0 | tty_region_off(tty); |
1495 | 0 | tty_margin_off(tty); |
1496 | | |
1497 | | /* |
1498 | | * Clamp the width to cellsize - note this is not cellused, because |
1499 | | * there may be empty background cells after it (from BCE). |
1500 | | */ |
1501 | 0 | sx = screen_size_x(s); |
1502 | 0 | if (nx > sx) |
1503 | 0 | nx = sx; |
1504 | 0 | cellsize = grid_get_line(gd, gd->hsize + py)->cellsize; |
1505 | 0 | if (sx > cellsize) |
1506 | 0 | sx = cellsize; |
1507 | 0 | if (sx > tty->sx) |
1508 | 0 | sx = tty->sx; |
1509 | 0 | if (sx > nx) |
1510 | 0 | sx = nx; |
1511 | 0 | ux = 0; |
1512 | |
|
1513 | 0 | if (py == 0) |
1514 | 0 | gl = NULL; |
1515 | 0 | else |
1516 | 0 | gl = grid_get_line(gd, gd->hsize + py - 1); |
1517 | 0 | if (gl == NULL || |
1518 | 0 | (~gl->flags & GRID_LINE_WRAPPED) || |
1519 | 0 | atx != 0 || |
1520 | 0 | tty->cx < tty->sx || |
1521 | 0 | nx < tty->sx) { |
1522 | 0 | if (nx < tty->sx && |
1523 | 0 | atx == 0 && |
1524 | 0 | px + sx != nx && |
1525 | 0 | tty_term_has(tty->term, TTYC_EL1) && |
1526 | 0 | !tty_fake_bce(tty, defaults, 8) && |
1527 | 0 | c->overlay_check == NULL) { |
1528 | 0 | tty_default_attributes(tty, defaults, palette, 8, |
1529 | 0 | s->hyperlinks); |
1530 | 0 | tty_cursor(tty, nx - 1, aty); |
1531 | 0 | tty_putcode(tty, TTYC_EL1); |
1532 | 0 | cleared = 1; |
1533 | 0 | } |
1534 | 0 | } else { |
1535 | 0 | log_debug("%s: wrapped line %u", __func__, aty); |
1536 | 0 | wrapped = 1; |
1537 | 0 | } |
1538 | |
|
1539 | 0 | memcpy(&last, &grid_default_cell, sizeof last); |
1540 | 0 | len = 0; |
1541 | 0 | width = 0; |
1542 | |
|
1543 | 0 | for (i = 0; i < sx; i++) { |
1544 | 0 | grid_view_get_cell(gd, px + i, py, &gc); |
1545 | 0 | gcp = tty_check_codeset(tty, &gc); |
1546 | 0 | if (len != 0 && |
1547 | 0 | (!tty_check_overlay(tty, atx + ux + width, aty) || |
1548 | 0 | (gcp->attr & GRID_ATTR_CHARSET) || |
1549 | 0 | gcp->flags != last.flags || |
1550 | 0 | gcp->attr != last.attr || |
1551 | 0 | gcp->fg != last.fg || |
1552 | 0 | gcp->bg != last.bg || |
1553 | 0 | gcp->us != last.us || |
1554 | 0 | gcp->link != last.link || |
1555 | 0 | ux + width + gcp->data.width > nx || |
1556 | 0 | (sizeof buf) - len < gcp->data.size)) { |
1557 | 0 | tty_attributes(tty, &last, defaults, palette, |
1558 | 0 | s->hyperlinks); |
1559 | 0 | if (last.flags & GRID_FLAG_CLEARED) { |
1560 | 0 | log_debug("%s: %zu cleared", __func__, len); |
1561 | 0 | tty_clear_line(tty, defaults, aty, atx + ux, |
1562 | 0 | width, last.bg); |
1563 | 0 | } else { |
1564 | 0 | if (!wrapped || atx != 0 || ux != 0) |
1565 | 0 | tty_cursor(tty, atx + ux, aty); |
1566 | 0 | tty_putn(tty, buf, len, width); |
1567 | 0 | } |
1568 | 0 | ux += width; |
1569 | |
|
1570 | 0 | len = 0; |
1571 | 0 | width = 0; |
1572 | 0 | wrapped = 0; |
1573 | 0 | } |
1574 | |
|
1575 | 0 | if (gcp->flags & GRID_FLAG_SELECTED) |
1576 | 0 | screen_select_cell(s, &last, gcp); |
1577 | 0 | else |
1578 | 0 | memcpy(&last, gcp, sizeof last); |
1579 | |
|
1580 | 0 | tty_check_overlay_range(tty, atx + ux, aty, gcp->data.width, |
1581 | 0 | &r); |
1582 | 0 | hidden = 0; |
1583 | 0 | for (j = 0; j < OVERLAY_MAX_RANGES; j++) |
1584 | 0 | hidden += r.nx[j]; |
1585 | 0 | hidden = gcp->data.width - hidden; |
1586 | 0 | if (hidden != 0 && hidden == gcp->data.width) { |
1587 | 0 | if (~gcp->flags & GRID_FLAG_PADDING) |
1588 | 0 | ux += gcp->data.width; |
1589 | 0 | } else if (hidden != 0 || ux + gcp->data.width > nx) { |
1590 | 0 | if (~gcp->flags & GRID_FLAG_PADDING) { |
1591 | 0 | tty_attributes(tty, &last, defaults, palette, |
1592 | 0 | s->hyperlinks); |
1593 | 0 | for (j = 0; j < OVERLAY_MAX_RANGES; j++) { |
1594 | 0 | if (r.nx[j] == 0) |
1595 | 0 | continue; |
1596 | | /* Effective width drawn so far. */ |
1597 | 0 | eux = r.px[j] - atx; |
1598 | 0 | if (eux < nx) { |
1599 | 0 | tty_cursor(tty, r.px[j], aty); |
1600 | 0 | nxx = nx - eux; |
1601 | 0 | if (r.nx[j] > nxx) |
1602 | 0 | r.nx[j] = nxx; |
1603 | 0 | tty_repeat_space(tty, r.nx[j]); |
1604 | 0 | ux = eux + r.nx[j]; |
1605 | 0 | } |
1606 | 0 | } |
1607 | 0 | } |
1608 | 0 | } else if (gcp->attr & GRID_ATTR_CHARSET) { |
1609 | 0 | tty_attributes(tty, &last, defaults, palette, |
1610 | 0 | s->hyperlinks); |
1611 | 0 | tty_cursor(tty, atx + ux, aty); |
1612 | 0 | for (j = 0; j < gcp->data.size; j++) |
1613 | 0 | tty_putc(tty, gcp->data.data[j]); |
1614 | 0 | ux += gcp->data.width; |
1615 | 0 | } else if (~gcp->flags & GRID_FLAG_PADDING) { |
1616 | 0 | memcpy(buf + len, gcp->data.data, gcp->data.size); |
1617 | 0 | len += gcp->data.size; |
1618 | 0 | width += gcp->data.width; |
1619 | 0 | } |
1620 | 0 | } |
1621 | 0 | if (len != 0 && ((~last.flags & GRID_FLAG_CLEARED) || last.bg != 8)) { |
1622 | 0 | tty_attributes(tty, &last, defaults, palette, s->hyperlinks); |
1623 | 0 | if (last.flags & GRID_FLAG_CLEARED) { |
1624 | 0 | log_debug("%s: %zu cleared (end)", __func__, len); |
1625 | 0 | tty_clear_line(tty, defaults, aty, atx + ux, width, |
1626 | 0 | last.bg); |
1627 | 0 | } else { |
1628 | 0 | if (!wrapped || atx != 0 || ux != 0) |
1629 | 0 | tty_cursor(tty, atx + ux, aty); |
1630 | 0 | tty_putn(tty, buf, len, width); |
1631 | 0 | } |
1632 | 0 | ux += width; |
1633 | 0 | } |
1634 | |
|
1635 | 0 | if (!cleared && ux < nx) { |
1636 | 0 | log_debug("%s: %u to end of line (%zu cleared)", __func__, |
1637 | 0 | nx - ux, len); |
1638 | 0 | tty_default_attributes(tty, defaults, palette, 8, |
1639 | 0 | s->hyperlinks); |
1640 | 0 | tty_clear_line(tty, defaults, aty, atx + ux, nx - ux, 8); |
1641 | 0 | } |
1642 | |
|
1643 | 0 | tty->flags = (tty->flags & ~TTY_NOCURSOR) | flags; |
1644 | 0 | tty_update_mode(tty, tty->mode, s); |
1645 | 0 | } |
1646 | | |
1647 | | #ifdef ENABLE_SIXEL |
1648 | | /* Update context for client. */ |
1649 | | static int |
1650 | | tty_set_client_cb(struct tty_ctx *ttyctx, struct client *c) |
1651 | | { |
1652 | | struct window_pane *wp = ttyctx->arg; |
1653 | | |
1654 | | if (c->session->curw->window != wp->window) |
1655 | | return (0); |
1656 | | if (wp->layout_cell == NULL) |
1657 | | return (0); |
1658 | | |
1659 | | /* Set the properties relevant to the current client. */ |
1660 | | ttyctx->bigger = tty_window_offset(&c->tty, &ttyctx->wox, &ttyctx->woy, |
1661 | | &ttyctx->wsx, &ttyctx->wsy); |
1662 | | |
1663 | | ttyctx->yoff = ttyctx->ryoff = wp->yoff; |
1664 | | if (status_at_line(c) == 0) |
1665 | | ttyctx->yoff += status_line_size(c); |
1666 | | |
1667 | | return (1); |
1668 | | } |
1669 | | |
1670 | | void |
1671 | | tty_draw_images(struct client *c, struct window_pane *wp, struct screen *s) |
1672 | | { |
1673 | | struct image *im; |
1674 | | struct tty_ctx ttyctx; |
1675 | | |
1676 | | TAILQ_FOREACH(im, &s->images, entry) { |
1677 | | memset(&ttyctx, 0, sizeof ttyctx); |
1678 | | |
1679 | | /* Set the client independent properties. */ |
1680 | | ttyctx.ocx = im->px; |
1681 | | ttyctx.ocy = im->py; |
1682 | | |
1683 | | ttyctx.orlower = s->rlower; |
1684 | | ttyctx.orupper = s->rupper; |
1685 | | |
1686 | | ttyctx.xoff = ttyctx.rxoff = wp->xoff; |
1687 | | ttyctx.sx = wp->sx; |
1688 | | ttyctx.sy = wp->sy; |
1689 | | |
1690 | | ttyctx.ptr = im; |
1691 | | ttyctx.arg = wp; |
1692 | | ttyctx.set_client_cb = tty_set_client_cb; |
1693 | | ttyctx.allow_invisible_panes = 1; |
1694 | | tty_write_one(tty_cmd_sixelimage, c, &ttyctx); |
1695 | | } |
1696 | | } |
1697 | | #endif |
1698 | | |
1699 | | void |
1700 | | tty_sync_start(struct tty *tty) |
1701 | 0 | { |
1702 | 0 | if (tty->flags & TTY_BLOCK) |
1703 | 0 | return; |
1704 | 0 | if (tty->flags & TTY_SYNCING) |
1705 | 0 | return; |
1706 | 0 | tty->flags |= TTY_SYNCING; |
1707 | |
|
1708 | 0 | if (tty_term_has(tty->term, TTYC_SYNC)) { |
1709 | 0 | log_debug("%s sync start", tty->client->name); |
1710 | 0 | tty_putcode_i(tty, TTYC_SYNC, 1); |
1711 | 0 | } |
1712 | 0 | } |
1713 | | |
1714 | | void |
1715 | | tty_sync_end(struct tty *tty) |
1716 | 0 | { |
1717 | 0 | if (tty->flags & TTY_BLOCK) |
1718 | 0 | return; |
1719 | 0 | if (~tty->flags & TTY_SYNCING) |
1720 | 0 | return; |
1721 | 0 | tty->flags &= ~TTY_SYNCING; |
1722 | |
|
1723 | 0 | if (tty_term_has(tty->term, TTYC_SYNC)) { |
1724 | 0 | log_debug("%s sync end", tty->client->name); |
1725 | 0 | tty_putcode_i(tty, TTYC_SYNC, 2); |
1726 | 0 | } |
1727 | 0 | } |
1728 | | |
1729 | | static int |
1730 | | tty_client_ready(const struct tty_ctx *ctx, struct client *c) |
1731 | 0 | { |
1732 | 0 | if (c->session == NULL || c->tty.term == NULL) |
1733 | 0 | return (0); |
1734 | 0 | if (c->flags & CLIENT_SUSPENDED) |
1735 | 0 | return (0); |
1736 | | |
1737 | | /* |
1738 | | * If invisible panes are allowed (used for passthrough), don't care if |
1739 | | * redrawing or frozen. |
1740 | | */ |
1741 | 0 | if (ctx->allow_invisible_panes) |
1742 | 0 | return (1); |
1743 | | |
1744 | 0 | if (c->flags & CLIENT_REDRAWWINDOW) |
1745 | 0 | return (0); |
1746 | 0 | if (c->tty.flags & TTY_FREEZE) |
1747 | 0 | return (0); |
1748 | 0 | return (1); |
1749 | 0 | } |
1750 | | |
1751 | | void |
1752 | | tty_write(void (*cmdfn)(struct tty *, const struct tty_ctx *), |
1753 | | struct tty_ctx *ctx) |
1754 | 51.8k | { |
1755 | 51.8k | struct client *c; |
1756 | 51.8k | int state; |
1757 | | |
1758 | 51.8k | if (ctx->set_client_cb == NULL) |
1759 | 0 | return; |
1760 | 51.8k | TAILQ_FOREACH(c, &clients, entry) { |
1761 | 0 | if (tty_client_ready(ctx, c)) { |
1762 | 0 | state = ctx->set_client_cb(ctx, c); |
1763 | 0 | if (state == -1) |
1764 | 0 | break; |
1765 | 0 | if (state == 0) |
1766 | 0 | continue; |
1767 | 0 | cmdfn(&c->tty, ctx); |
1768 | 0 | } |
1769 | 0 | } |
1770 | 51.8k | } |
1771 | | |
1772 | | #ifdef ENABLE_SIXEL |
1773 | | /* Only write to the incoming tty instead of every client. */ |
1774 | | static void |
1775 | | tty_write_one(void (*cmdfn)(struct tty *, const struct tty_ctx *), |
1776 | | struct client *c, struct tty_ctx *ctx) |
1777 | | { |
1778 | | if (ctx->set_client_cb == NULL) |
1779 | | return; |
1780 | | if ((ctx->set_client_cb(ctx, c)) == 1) |
1781 | | cmdfn(&c->tty, ctx); |
1782 | | } |
1783 | | #endif |
1784 | | |
1785 | | void |
1786 | | tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx) |
1787 | 0 | { |
1788 | 0 | struct client *c = tty->client; |
1789 | |
|
1790 | 0 | if (ctx->bigger || |
1791 | 0 | !tty_full_width(tty, ctx) || |
1792 | 0 | tty_fake_bce(tty, &ctx->defaults, ctx->bg) || |
1793 | 0 | (!tty_term_has(tty->term, TTYC_ICH) && |
1794 | 0 | !tty_term_has(tty->term, TTYC_ICH1)) || |
1795 | 0 | c->overlay_check != NULL) { |
1796 | 0 | tty_draw_pane(tty, ctx, ctx->ocy); |
1797 | 0 | return; |
1798 | 0 | } |
1799 | | |
1800 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1801 | 0 | ctx->s->hyperlinks); |
1802 | |
|
1803 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); |
1804 | |
|
1805 | 0 | tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num); |
1806 | 0 | } |
1807 | | |
1808 | | void |
1809 | | tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx) |
1810 | 0 | { |
1811 | 0 | struct client *c = tty->client; |
1812 | |
|
1813 | 0 | if (ctx->bigger || |
1814 | 0 | !tty_full_width(tty, ctx) || |
1815 | 0 | tty_fake_bce(tty, &ctx->defaults, ctx->bg) || |
1816 | 0 | (!tty_term_has(tty->term, TTYC_DCH) && |
1817 | 0 | !tty_term_has(tty->term, TTYC_DCH1)) || |
1818 | 0 | c->overlay_check != NULL) { |
1819 | 0 | tty_draw_pane(tty, ctx, ctx->ocy); |
1820 | 0 | return; |
1821 | 0 | } |
1822 | | |
1823 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1824 | 0 | ctx->s->hyperlinks); |
1825 | |
|
1826 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); |
1827 | |
|
1828 | 0 | tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num); |
1829 | 0 | } |
1830 | | |
1831 | | void |
1832 | | tty_cmd_clearcharacter(struct tty *tty, const struct tty_ctx *ctx) |
1833 | 0 | { |
1834 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1835 | 0 | ctx->s->hyperlinks); |
1836 | |
|
1837 | 0 | tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, ctx->num, ctx->bg); |
1838 | 0 | } |
1839 | | |
1840 | | void |
1841 | | tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx) |
1842 | 0 | { |
1843 | 0 | struct client *c = tty->client; |
1844 | |
|
1845 | 0 | if (ctx->bigger || |
1846 | 0 | !tty_full_width(tty, ctx) || |
1847 | 0 | tty_fake_bce(tty, &ctx->defaults, ctx->bg) || |
1848 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
1849 | 0 | !tty_term_has(tty->term, TTYC_IL1) || |
1850 | 0 | ctx->sx == 1 || |
1851 | 0 | ctx->sy == 1 || |
1852 | 0 | c->overlay_check != NULL) { |
1853 | 0 | tty_redraw_region(tty, ctx); |
1854 | 0 | return; |
1855 | 0 | } |
1856 | | |
1857 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1858 | 0 | ctx->s->hyperlinks); |
1859 | |
|
1860 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
1861 | 0 | tty_margin_off(tty); |
1862 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); |
1863 | |
|
1864 | 0 | tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num); |
1865 | 0 | tty->cx = tty->cy = UINT_MAX; |
1866 | 0 | } |
1867 | | |
1868 | | void |
1869 | | tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx) |
1870 | 0 | { |
1871 | 0 | struct client *c = tty->client; |
1872 | |
|
1873 | 0 | if (ctx->bigger || |
1874 | 0 | !tty_full_width(tty, ctx) || |
1875 | 0 | tty_fake_bce(tty, &ctx->defaults, ctx->bg) || |
1876 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
1877 | 0 | !tty_term_has(tty->term, TTYC_DL1) || |
1878 | 0 | ctx->sx == 1 || |
1879 | 0 | ctx->sy == 1 || |
1880 | 0 | c->overlay_check != NULL) { |
1881 | 0 | tty_redraw_region(tty, ctx); |
1882 | 0 | return; |
1883 | 0 | } |
1884 | | |
1885 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1886 | 0 | ctx->s->hyperlinks); |
1887 | |
|
1888 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
1889 | 0 | tty_margin_off(tty); |
1890 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); |
1891 | |
|
1892 | 0 | tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num); |
1893 | 0 | tty->cx = tty->cy = UINT_MAX; |
1894 | 0 | } |
1895 | | |
1896 | | void |
1897 | | tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx) |
1898 | 0 | { |
1899 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1900 | 0 | ctx->s->hyperlinks); |
1901 | |
|
1902 | 0 | tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->sx, ctx->bg); |
1903 | 0 | } |
1904 | | |
1905 | | void |
1906 | | tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx) |
1907 | 0 | { |
1908 | 0 | u_int nx = ctx->sx - ctx->ocx; |
1909 | |
|
1910 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1911 | 0 | ctx->s->hyperlinks); |
1912 | |
|
1913 | 0 | tty_clear_pane_line(tty, ctx, ctx->ocy, ctx->ocx, nx, ctx->bg); |
1914 | 0 | } |
1915 | | |
1916 | | void |
1917 | | tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx) |
1918 | 0 | { |
1919 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1920 | 0 | ctx->s->hyperlinks); |
1921 | |
|
1922 | 0 | tty_clear_pane_line(tty, ctx, ctx->ocy, 0, ctx->ocx + 1, ctx->bg); |
1923 | 0 | } |
1924 | | |
1925 | | void |
1926 | | tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx) |
1927 | 0 | { |
1928 | 0 | struct client *c = tty->client; |
1929 | |
|
1930 | 0 | if (ctx->ocy != ctx->orupper) |
1931 | 0 | return; |
1932 | | |
1933 | 0 | if (ctx->bigger || |
1934 | 0 | (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) || |
1935 | 0 | tty_fake_bce(tty, &ctx->defaults, 8) || |
1936 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
1937 | 0 | (!tty_term_has(tty->term, TTYC_RI) && |
1938 | 0 | !tty_term_has(tty->term, TTYC_RIN)) || |
1939 | 0 | ctx->sx == 1 || |
1940 | 0 | ctx->sy == 1 || |
1941 | 0 | c->overlay_check != NULL) { |
1942 | 0 | tty_redraw_region(tty, ctx); |
1943 | 0 | return; |
1944 | 0 | } |
1945 | | |
1946 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1947 | 0 | ctx->s->hyperlinks); |
1948 | |
|
1949 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
1950 | 0 | tty_margin_pane(tty, ctx); |
1951 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper); |
1952 | |
|
1953 | 0 | if (tty_term_has(tty->term, TTYC_RI)) |
1954 | 0 | tty_putcode(tty, TTYC_RI); |
1955 | 0 | else |
1956 | 0 | tty_putcode_i(tty, TTYC_RIN, 1); |
1957 | 0 | } |
1958 | | |
1959 | | void |
1960 | | tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx) |
1961 | 0 | { |
1962 | 0 | struct client *c = tty->client; |
1963 | |
|
1964 | 0 | if (ctx->ocy != ctx->orlower) |
1965 | 0 | return; |
1966 | | |
1967 | 0 | if (ctx->bigger || |
1968 | 0 | (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) || |
1969 | 0 | tty_fake_bce(tty, &ctx->defaults, 8) || |
1970 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
1971 | 0 | ctx->sx == 1 || |
1972 | 0 | ctx->sy == 1 || |
1973 | 0 | c->overlay_check != NULL) { |
1974 | 0 | tty_redraw_region(tty, ctx); |
1975 | 0 | return; |
1976 | 0 | } |
1977 | | |
1978 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
1979 | 0 | ctx->s->hyperlinks); |
1980 | |
|
1981 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
1982 | 0 | tty_margin_pane(tty, ctx); |
1983 | | |
1984 | | /* |
1985 | | * If we want to wrap a pane while using margins, the cursor needs to |
1986 | | * be exactly on the right of the region. If the cursor is entirely off |
1987 | | * the edge - move it back to the right. Some terminals are funny about |
1988 | | * this and insert extra spaces, so only use the right if margins are |
1989 | | * enabled. |
1990 | | */ |
1991 | 0 | if (ctx->xoff + ctx->ocx > tty->rright) { |
1992 | 0 | if (!tty_use_margin(tty)) |
1993 | 0 | tty_cursor(tty, 0, ctx->yoff + ctx->ocy); |
1994 | 0 | else |
1995 | 0 | tty_cursor(tty, tty->rright, ctx->yoff + ctx->ocy); |
1996 | 0 | } else |
1997 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy); |
1998 | |
|
1999 | 0 | tty_putc(tty, '\n'); |
2000 | 0 | } |
2001 | | |
2002 | | void |
2003 | | tty_cmd_scrollup(struct tty *tty, const struct tty_ctx *ctx) |
2004 | 0 | { |
2005 | 0 | struct client *c = tty->client; |
2006 | 0 | u_int i; |
2007 | |
|
2008 | 0 | if (ctx->bigger || |
2009 | 0 | (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) || |
2010 | 0 | tty_fake_bce(tty, &ctx->defaults, 8) || |
2011 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
2012 | 0 | ctx->sx == 1 || |
2013 | 0 | ctx->sy == 1 || |
2014 | 0 | c->overlay_check != NULL) { |
2015 | 0 | tty_redraw_region(tty, ctx); |
2016 | 0 | return; |
2017 | 0 | } |
2018 | | |
2019 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
2020 | 0 | ctx->s->hyperlinks); |
2021 | |
|
2022 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
2023 | 0 | tty_margin_pane(tty, ctx); |
2024 | |
|
2025 | 0 | if (ctx->num == 1 || !tty_term_has(tty->term, TTYC_INDN)) { |
2026 | 0 | if (!tty_use_margin(tty)) |
2027 | 0 | tty_cursor(tty, 0, tty->rlower); |
2028 | 0 | else |
2029 | 0 | tty_cursor(tty, tty->rright, tty->rlower); |
2030 | 0 | for (i = 0; i < ctx->num; i++) |
2031 | 0 | tty_putc(tty, '\n'); |
2032 | 0 | } else { |
2033 | 0 | if (tty->cy == UINT_MAX) |
2034 | 0 | tty_cursor(tty, 0, 0); |
2035 | 0 | else |
2036 | 0 | tty_cursor(tty, 0, tty->cy); |
2037 | 0 | tty_putcode_i(tty, TTYC_INDN, ctx->num); |
2038 | 0 | } |
2039 | 0 | } |
2040 | | |
2041 | | void |
2042 | | tty_cmd_scrolldown(struct tty *tty, const struct tty_ctx *ctx) |
2043 | 0 | { |
2044 | 0 | u_int i; |
2045 | 0 | struct client *c = tty->client; |
2046 | |
|
2047 | 0 | if (ctx->bigger || |
2048 | 0 | (!tty_full_width(tty, ctx) && !tty_use_margin(tty)) || |
2049 | 0 | tty_fake_bce(tty, &ctx->defaults, 8) || |
2050 | 0 | !tty_term_has(tty->term, TTYC_CSR) || |
2051 | 0 | (!tty_term_has(tty->term, TTYC_RI) && |
2052 | 0 | !tty_term_has(tty->term, TTYC_RIN)) || |
2053 | 0 | ctx->sx == 1 || |
2054 | 0 | ctx->sy == 1 || |
2055 | 0 | c->overlay_check != NULL) { |
2056 | 0 | tty_redraw_region(tty, ctx); |
2057 | 0 | return; |
2058 | 0 | } |
2059 | | |
2060 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
2061 | 0 | ctx->s->hyperlinks); |
2062 | |
|
2063 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
2064 | 0 | tty_margin_pane(tty, ctx); |
2065 | 0 | tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper); |
2066 | |
|
2067 | 0 | if (tty_term_has(tty->term, TTYC_RIN)) |
2068 | 0 | tty_putcode_i(tty, TTYC_RIN, ctx->num); |
2069 | 0 | else { |
2070 | 0 | for (i = 0; i < ctx->num; i++) |
2071 | 0 | tty_putcode(tty, TTYC_RI); |
2072 | 0 | } |
2073 | 0 | } |
2074 | | |
2075 | | void |
2076 | | tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx) |
2077 | 0 | { |
2078 | 0 | u_int px, py, nx, ny; |
2079 | |
|
2080 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
2081 | 0 | ctx->s->hyperlinks); |
2082 | |
|
2083 | 0 | tty_region_pane(tty, ctx, 0, ctx->sy - 1); |
2084 | 0 | tty_margin_off(tty); |
2085 | |
|
2086 | 0 | px = 0; |
2087 | 0 | nx = ctx->sx; |
2088 | 0 | py = ctx->ocy + 1; |
2089 | 0 | ny = ctx->sy - ctx->ocy - 1; |
2090 | |
|
2091 | 0 | tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg); |
2092 | |
|
2093 | 0 | px = ctx->ocx; |
2094 | 0 | nx = ctx->sx - ctx->ocx; |
2095 | 0 | py = ctx->ocy; |
2096 | |
|
2097 | 0 | tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg); |
2098 | 0 | } |
2099 | | |
2100 | | void |
2101 | | tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx) |
2102 | 0 | { |
2103 | 0 | u_int px, py, nx, ny; |
2104 | |
|
2105 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
2106 | 0 | ctx->s->hyperlinks); |
2107 | |
|
2108 | 0 | tty_region_pane(tty, ctx, 0, ctx->sy - 1); |
2109 | 0 | tty_margin_off(tty); |
2110 | |
|
2111 | 0 | px = 0; |
2112 | 0 | nx = ctx->sx; |
2113 | 0 | py = 0; |
2114 | 0 | ny = ctx->ocy; |
2115 | |
|
2116 | 0 | tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg); |
2117 | |
|
2118 | 0 | px = 0; |
2119 | 0 | nx = ctx->ocx + 1; |
2120 | 0 | py = ctx->ocy; |
2121 | |
|
2122 | 0 | tty_clear_pane_line(tty, ctx, py, px, nx, ctx->bg); |
2123 | 0 | } |
2124 | | |
2125 | | void |
2126 | | tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx) |
2127 | 0 | { |
2128 | 0 | u_int px, py, nx, ny; |
2129 | |
|
2130 | 0 | tty_default_attributes(tty, &ctx->defaults, ctx->palette, ctx->bg, |
2131 | 0 | ctx->s->hyperlinks); |
2132 | |
|
2133 | 0 | tty_region_pane(tty, ctx, 0, ctx->sy - 1); |
2134 | 0 | tty_margin_off(tty); |
2135 | |
|
2136 | 0 | px = 0; |
2137 | 0 | nx = ctx->sx; |
2138 | 0 | py = 0; |
2139 | 0 | ny = ctx->sy; |
2140 | |
|
2141 | 0 | tty_clear_pane_area(tty, ctx, py, ny, px, nx, ctx->bg); |
2142 | 0 | } |
2143 | | |
2144 | | void |
2145 | | tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx) |
2146 | 0 | { |
2147 | 0 | u_int i, j; |
2148 | |
|
2149 | 0 | if (ctx->bigger) { |
2150 | 0 | ctx->redraw_cb(ctx); |
2151 | 0 | return; |
2152 | 0 | } |
2153 | | |
2154 | 0 | tty_attributes(tty, &grid_default_cell, &ctx->defaults, ctx->palette, |
2155 | 0 | ctx->s->hyperlinks); |
2156 | |
|
2157 | 0 | tty_region_pane(tty, ctx, 0, ctx->sy - 1); |
2158 | 0 | tty_margin_off(tty); |
2159 | |
|
2160 | 0 | for (j = 0; j < ctx->sy; j++) { |
2161 | 0 | tty_cursor_pane(tty, ctx, 0, j); |
2162 | 0 | for (i = 0; i < ctx->sx; i++) |
2163 | 0 | tty_putc(tty, 'E'); |
2164 | 0 | } |
2165 | 0 | } |
2166 | | |
2167 | | void |
2168 | | tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx) |
2169 | 0 | { |
2170 | 0 | const struct grid_cell *gcp = ctx->cell; |
2171 | 0 | struct screen *s = ctx->s; |
2172 | 0 | struct overlay_ranges r; |
2173 | 0 | u_int px, py, i, vis = 0; |
2174 | |
|
2175 | 0 | px = ctx->xoff + ctx->ocx - ctx->wox; |
2176 | 0 | py = ctx->yoff + ctx->ocy - ctx->woy; |
2177 | 0 | if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, 1, 1) || |
2178 | 0 | (gcp->data.width == 1 && !tty_check_overlay(tty, px, py))) |
2179 | 0 | return; |
2180 | | |
2181 | | /* Handle partially obstructed wide characters. */ |
2182 | 0 | if (gcp->data.width > 1) { |
2183 | 0 | tty_check_overlay_range(tty, px, py, gcp->data.width, &r); |
2184 | 0 | for (i = 0; i < OVERLAY_MAX_RANGES; i++) |
2185 | 0 | vis += r.nx[i]; |
2186 | 0 | if (vis < gcp->data.width) { |
2187 | 0 | tty_draw_line(tty, s, s->cx, s->cy, gcp->data.width, |
2188 | 0 | px, py, &ctx->defaults, ctx->palette); |
2189 | 0 | return; |
2190 | 0 | } |
2191 | 0 | } |
2192 | | |
2193 | 0 | if (ctx->xoff + ctx->ocx - ctx->wox > tty->sx - 1 && |
2194 | 0 | ctx->ocy == ctx->orlower && |
2195 | 0 | tty_full_width(tty, ctx)) |
2196 | 0 | tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower); |
2197 | |
|
2198 | 0 | tty_margin_off(tty); |
2199 | 0 | tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy); |
2200 | |
|
2201 | 0 | tty_cell(tty, ctx->cell, &ctx->defaults, ctx->palette, |
2202 | 0 | ctx->s->hyperlinks); |
2203 | |
|
2204 | 0 | if (ctx->num == 1) |
2205 | 0 | tty_invalidate(tty); |
2206 | 0 | } |
2207 | | |
2208 | | void |
2209 | | tty_cmd_cells(struct tty *tty, const struct tty_ctx *ctx) |
2210 | 0 | { |
2211 | 0 | struct overlay_ranges r; |
2212 | 0 | u_int i, px, py, cx; |
2213 | 0 | char *cp = ctx->ptr; |
2214 | |
|
2215 | 0 | if (!tty_is_visible(tty, ctx, ctx->ocx, ctx->ocy, ctx->num, 1)) |
2216 | 0 | return; |
2217 | | |
2218 | 0 | if (ctx->bigger && |
2219 | 0 | (ctx->xoff + ctx->ocx < ctx->wox || |
2220 | 0 | ctx->xoff + ctx->ocx + ctx->num > ctx->wox + ctx->wsx)) { |
2221 | 0 | if (!ctx->wrapped || |
2222 | 0 | !tty_full_width(tty, ctx) || |
2223 | 0 | (tty->term->flags & TERM_NOAM) || |
2224 | 0 | ctx->xoff + ctx->ocx != 0 || |
2225 | 0 | ctx->yoff + ctx->ocy != tty->cy + 1 || |
2226 | 0 | tty->cx < tty->sx || |
2227 | 0 | tty->cy == tty->rlower) |
2228 | 0 | tty_draw_pane(tty, ctx, ctx->ocy); |
2229 | 0 | else |
2230 | 0 | ctx->redraw_cb(ctx); |
2231 | 0 | return; |
2232 | 0 | } |
2233 | | |
2234 | 0 | tty_margin_off(tty); |
2235 | 0 | tty_cursor_pane_unless_wrap(tty, ctx, ctx->ocx, ctx->ocy); |
2236 | 0 | tty_attributes(tty, ctx->cell, &ctx->defaults, ctx->palette, ctx->s->hyperlinks); |
2237 | | |
2238 | | /* Get tty position from pane position for overlay check. */ |
2239 | 0 | px = ctx->xoff + ctx->ocx - ctx->wox; |
2240 | 0 | py = ctx->yoff + ctx->ocy - ctx->woy; |
2241 | |
|
2242 | 0 | tty_check_overlay_range(tty, px, py, ctx->num, &r); |
2243 | 0 | for (i = 0; i < OVERLAY_MAX_RANGES; i++) { |
2244 | 0 | if (r.nx[i] == 0) |
2245 | 0 | continue; |
2246 | | /* Convert back to pane position for printing. */ |
2247 | 0 | cx = r.px[i] - ctx->xoff + ctx->wox; |
2248 | 0 | tty_cursor_pane_unless_wrap(tty, ctx, cx, ctx->ocy); |
2249 | 0 | tty_putn(tty, cp + r.px[i] - px, r.nx[i], r.nx[i]); |
2250 | 0 | } |
2251 | 0 | } |
2252 | | |
2253 | | void |
2254 | | tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx) |
2255 | 0 | { |
2256 | 0 | tty_set_selection(tty, ctx->ptr2, ctx->ptr, ctx->num); |
2257 | 0 | } |
2258 | | |
2259 | | void |
2260 | | tty_set_selection(struct tty *tty, const char *flags, const char *buf, |
2261 | | size_t len) |
2262 | 0 | { |
2263 | 0 | char *encoded; |
2264 | 0 | size_t size; |
2265 | |
|
2266 | 0 | if (~tty->flags & TTY_STARTED) |
2267 | 0 | return; |
2268 | 0 | if (!tty_term_has(tty->term, TTYC_MS)) |
2269 | 0 | return; |
2270 | | |
2271 | 0 | size = 4 * ((len + 2) / 3) + 1; /* storage for base64 */ |
2272 | 0 | encoded = xmalloc(size); |
2273 | |
|
2274 | 0 | b64_ntop(buf, len, encoded, size); |
2275 | 0 | tty->flags |= TTY_NOBLOCK; |
2276 | 0 | tty_putcode_ss(tty, TTYC_MS, flags, encoded); |
2277 | |
|
2278 | 0 | free(encoded); |
2279 | 0 | } |
2280 | | |
2281 | | void |
2282 | | tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx) |
2283 | 0 | { |
2284 | 0 | tty->flags |= TTY_NOBLOCK; |
2285 | 0 | tty_add(tty, ctx->ptr, ctx->num); |
2286 | 0 | tty_invalidate(tty); |
2287 | 0 | } |
2288 | | |
2289 | | #ifdef ENABLE_SIXEL |
2290 | | void |
2291 | | tty_cmd_sixelimage(struct tty *tty, const struct tty_ctx *ctx) |
2292 | | { |
2293 | | struct image *im = ctx->ptr; |
2294 | | struct sixel_image *si = im->data; |
2295 | | struct sixel_image *new; |
2296 | | char *data; |
2297 | | size_t size; |
2298 | | u_int cx = ctx->ocx, cy = ctx->ocy, sx, sy; |
2299 | | u_int i, j, x, y, rx, ry; |
2300 | | int fallback = 0; |
2301 | | |
2302 | | if ((~tty->term->flags & TERM_SIXEL) && |
2303 | | !tty_term_has(tty->term, TTYC_SXL)) |
2304 | | fallback = 1; |
2305 | | if (tty->xpixel == 0 || tty->ypixel == 0) |
2306 | | fallback = 1; |
2307 | | |
2308 | | sixel_size_in_cells(si, &sx, &sy); |
2309 | | log_debug("%s: image is %ux%u", __func__, sx, sy); |
2310 | | if (!tty_clamp_area(tty, ctx, cx, cy, sx, sy, &i, &j, &x, &y, &rx, &ry)) |
2311 | | return; |
2312 | | log_debug("%s: clamping to %u,%u-%u,%u", __func__, i, j, rx, ry); |
2313 | | |
2314 | | if (fallback == 1) { |
2315 | | data = xstrdup(im->fallback); |
2316 | | size = strlen(data); |
2317 | | } else { |
2318 | | new = sixel_scale(si, tty->xpixel, tty->ypixel, i, j, rx, ry, 0); |
2319 | | if (new == NULL) |
2320 | | return; |
2321 | | |
2322 | | data = sixel_print(new, si, &size); |
2323 | | } |
2324 | | if (data != NULL) { |
2325 | | log_debug("%s: %zu bytes: %s", __func__, size, data); |
2326 | | tty_region_off(tty); |
2327 | | tty_margin_off(tty); |
2328 | | tty_cursor(tty, x, y); |
2329 | | |
2330 | | tty->flags |= TTY_NOBLOCK; |
2331 | | tty_add(tty, data, size); |
2332 | | tty_invalidate(tty); |
2333 | | free(data); |
2334 | | } |
2335 | | |
2336 | | if (fallback == 0) |
2337 | | sixel_free(new); |
2338 | | } |
2339 | | #endif |
2340 | | |
2341 | | void |
2342 | | tty_cmd_syncstart(struct tty *tty, const struct tty_ctx *ctx) |
2343 | 0 | { |
2344 | 0 | if (ctx->num == 0x11) { |
2345 | | /* |
2346 | | * This is an overlay and a command that moves the cursor so |
2347 | | * start synchronized updates. |
2348 | | */ |
2349 | 0 | tty_sync_start(tty); |
2350 | 0 | } else if (~ctx->num & 0x10) { |
2351 | | /* |
2352 | | * This is a pane. If there is an overlay, always start; |
2353 | | * otherwise, only if requested. |
2354 | | */ |
2355 | 0 | if (ctx->num || tty->client->overlay_draw != NULL) |
2356 | 0 | tty_sync_start(tty); |
2357 | 0 | } |
2358 | 0 | } |
2359 | | |
2360 | | void |
2361 | | tty_cell(struct tty *tty, const struct grid_cell *gc, |
2362 | | const struct grid_cell *defaults, struct colour_palette *palette, |
2363 | | struct hyperlinks *hl) |
2364 | 0 | { |
2365 | 0 | const struct grid_cell *gcp; |
2366 | | |
2367 | | /* Skip last character if terminal is stupid. */ |
2368 | 0 | if ((tty->term->flags & TERM_NOAM) && |
2369 | 0 | tty->cy == tty->sy - 1 && |
2370 | 0 | tty->cx == tty->sx - 1) |
2371 | 0 | return; |
2372 | | |
2373 | | /* If this is a padding character, do nothing. */ |
2374 | 0 | if (gc->flags & GRID_FLAG_PADDING) |
2375 | 0 | return; |
2376 | | |
2377 | | /* Check the output codeset and apply attributes. */ |
2378 | 0 | gcp = tty_check_codeset(tty, gc); |
2379 | 0 | tty_attributes(tty, gcp, defaults, palette, hl); |
2380 | | |
2381 | | /* If it is a single character, write with putc to handle ACS. */ |
2382 | 0 | if (gcp->data.size == 1) { |
2383 | 0 | tty_attributes(tty, gcp, defaults, palette, hl); |
2384 | 0 | if (*gcp->data.data < 0x20 || *gcp->data.data == 0x7f) |
2385 | 0 | return; |
2386 | 0 | tty_putc(tty, *gcp->data.data); |
2387 | 0 | return; |
2388 | 0 | } |
2389 | | |
2390 | | /* Write the data. */ |
2391 | 0 | tty_putn(tty, gcp->data.data, gcp->data.size, gcp->data.width); |
2392 | 0 | } |
2393 | | |
2394 | | void |
2395 | | tty_reset(struct tty *tty) |
2396 | 0 | { |
2397 | 0 | struct grid_cell *gc = &tty->cell; |
2398 | |
|
2399 | 0 | if (!grid_cells_equal(gc, &grid_default_cell)) { |
2400 | 0 | if (gc->link != 0) |
2401 | 0 | tty_putcode_ss(tty, TTYC_HLS, "", ""); |
2402 | 0 | if ((gc->attr & GRID_ATTR_CHARSET) && tty_acs_needed(tty)) |
2403 | 0 | tty_putcode(tty, TTYC_RMACS); |
2404 | 0 | tty_putcode(tty, TTYC_SGR0); |
2405 | 0 | memcpy(gc, &grid_default_cell, sizeof *gc); |
2406 | 0 | } |
2407 | 0 | memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell); |
2408 | 0 | } |
2409 | | |
2410 | | void |
2411 | | tty_invalidate(struct tty *tty) |
2412 | 0 | { |
2413 | 0 | memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell); |
2414 | 0 | memcpy(&tty->last_cell, &grid_default_cell, sizeof tty->last_cell); |
2415 | |
|
2416 | 0 | tty->cx = tty->cy = UINT_MAX; |
2417 | 0 | tty->rupper = tty->rleft = UINT_MAX; |
2418 | 0 | tty->rlower = tty->rright = UINT_MAX; |
2419 | |
|
2420 | 0 | if (tty->flags & TTY_STARTED) { |
2421 | 0 | if (tty_use_margin(tty)) |
2422 | 0 | tty_putcode(tty, TTYC_ENMG); |
2423 | 0 | tty_putcode(tty, TTYC_SGR0); |
2424 | |
|
2425 | 0 | tty->mode = ALL_MODES; |
2426 | 0 | tty_update_mode(tty, MODE_CURSOR, NULL); |
2427 | |
|
2428 | 0 | tty_cursor(tty, 0, 0); |
2429 | 0 | tty_region_off(tty); |
2430 | 0 | tty_margin_off(tty); |
2431 | 0 | } else |
2432 | 0 | tty->mode = MODE_CURSOR; |
2433 | 0 | } |
2434 | | |
2435 | | /* Turn off margin. */ |
2436 | | void |
2437 | | tty_region_off(struct tty *tty) |
2438 | 0 | { |
2439 | 0 | tty_region(tty, 0, tty->sy - 1); |
2440 | 0 | } |
2441 | | |
2442 | | /* Set region inside pane. */ |
2443 | | static void |
2444 | | tty_region_pane(struct tty *tty, const struct tty_ctx *ctx, u_int rupper, |
2445 | | u_int rlower) |
2446 | 0 | { |
2447 | 0 | tty_region(tty, ctx->yoff + rupper - ctx->woy, |
2448 | 0 | ctx->yoff + rlower - ctx->woy); |
2449 | 0 | } |
2450 | | |
2451 | | /* Set region at absolute position. */ |
2452 | | static void |
2453 | | tty_region(struct tty *tty, u_int rupper, u_int rlower) |
2454 | 0 | { |
2455 | 0 | if (tty->rlower == rlower && tty->rupper == rupper) |
2456 | 0 | return; |
2457 | 0 | if (!tty_term_has(tty->term, TTYC_CSR)) |
2458 | 0 | return; |
2459 | | |
2460 | 0 | tty->rupper = rupper; |
2461 | 0 | tty->rlower = rlower; |
2462 | | |
2463 | | /* |
2464 | | * Some terminals (such as PuTTY) do not correctly reset the cursor to |
2465 | | * 0,0 if it is beyond the last column (they do not reset their wrap |
2466 | | * flag so further output causes a line feed). As a workaround, do an |
2467 | | * explicit move to 0 first. |
2468 | | */ |
2469 | 0 | if (tty->cx >= tty->sx) { |
2470 | 0 | if (tty->cy == UINT_MAX) |
2471 | 0 | tty_cursor(tty, 0, 0); |
2472 | 0 | else |
2473 | 0 | tty_cursor(tty, 0, tty->cy); |
2474 | 0 | } |
2475 | |
|
2476 | 0 | tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower); |
2477 | 0 | tty->cx = tty->cy = UINT_MAX; |
2478 | 0 | } |
2479 | | |
2480 | | /* Turn off margin. */ |
2481 | | void |
2482 | | tty_margin_off(struct tty *tty) |
2483 | 0 | { |
2484 | 0 | tty_margin(tty, 0, tty->sx - 1); |
2485 | 0 | } |
2486 | | |
2487 | | /* Set margin inside pane. */ |
2488 | | static void |
2489 | | tty_margin_pane(struct tty *tty, const struct tty_ctx *ctx) |
2490 | 0 | { |
2491 | 0 | tty_margin(tty, ctx->xoff - ctx->wox, |
2492 | 0 | ctx->xoff + ctx->sx - 1 - ctx->wox); |
2493 | 0 | } |
2494 | | |
2495 | | /* Set margin at absolute position. */ |
2496 | | static void |
2497 | | tty_margin(struct tty *tty, u_int rleft, u_int rright) |
2498 | 0 | { |
2499 | 0 | if (!tty_use_margin(tty)) |
2500 | 0 | return; |
2501 | 0 | if (tty->rleft == rleft && tty->rright == rright) |
2502 | 0 | return; |
2503 | | |
2504 | 0 | tty_putcode_ii(tty, TTYC_CSR, tty->rupper, tty->rlower); |
2505 | |
|
2506 | 0 | tty->rleft = rleft; |
2507 | 0 | tty->rright = rright; |
2508 | |
|
2509 | 0 | if (rleft == 0 && rright == tty->sx - 1) |
2510 | 0 | tty_putcode(tty, TTYC_CLMG); |
2511 | 0 | else |
2512 | 0 | tty_putcode_ii(tty, TTYC_CMG, rleft, rright); |
2513 | 0 | tty->cx = tty->cy = UINT_MAX; |
2514 | 0 | } |
2515 | | |
2516 | | /* |
2517 | | * Move the cursor, unless it would wrap itself when the next character is |
2518 | | * printed. |
2519 | | */ |
2520 | | static void |
2521 | | tty_cursor_pane_unless_wrap(struct tty *tty, const struct tty_ctx *ctx, |
2522 | | u_int cx, u_int cy) |
2523 | 0 | { |
2524 | 0 | if (!ctx->wrapped || |
2525 | 0 | !tty_full_width(tty, ctx) || |
2526 | 0 | (tty->term->flags & TERM_NOAM) || |
2527 | 0 | ctx->xoff + cx != 0 || |
2528 | 0 | ctx->yoff + cy != tty->cy + 1 || |
2529 | 0 | tty->cx < tty->sx || |
2530 | 0 | tty->cy == tty->rlower) |
2531 | 0 | tty_cursor_pane(tty, ctx, cx, cy); |
2532 | 0 | else |
2533 | 0 | log_debug("%s: will wrap at %u,%u", __func__, tty->cx, tty->cy); |
2534 | 0 | } |
2535 | | |
2536 | | /* Move cursor inside pane. */ |
2537 | | static void |
2538 | | tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy) |
2539 | 0 | { |
2540 | 0 | tty_cursor(tty, ctx->xoff + cx - ctx->wox, ctx->yoff + cy - ctx->woy); |
2541 | 0 | } |
2542 | | |
2543 | | /* Move cursor to absolute position. */ |
2544 | | void |
2545 | | tty_cursor(struct tty *tty, u_int cx, u_int cy) |
2546 | 0 | { |
2547 | 0 | struct tty_term *term = tty->term; |
2548 | 0 | u_int thisx, thisy; |
2549 | 0 | int change; |
2550 | |
|
2551 | 0 | if (tty->flags & TTY_BLOCK) |
2552 | 0 | return; |
2553 | | |
2554 | 0 | thisx = tty->cx; |
2555 | 0 | thisy = tty->cy; |
2556 | | |
2557 | | /* |
2558 | | * If in the automargin space, and want to be there, do not move. |
2559 | | * Otherwise, force the cursor to be in range (and complain). |
2560 | | */ |
2561 | 0 | if (cx == thisx && cy == thisy && cx == tty->sx) |
2562 | 0 | return; |
2563 | 0 | if (cx > tty->sx - 1) { |
2564 | 0 | log_debug("%s: x too big %u > %u", __func__, cx, tty->sx - 1); |
2565 | 0 | cx = tty->sx - 1; |
2566 | 0 | } |
2567 | | |
2568 | | /* No change. */ |
2569 | 0 | if (cx == thisx && cy == thisy) |
2570 | 0 | return; |
2571 | | |
2572 | | /* Currently at the very end of the line - use absolute movement. */ |
2573 | 0 | if (thisx > tty->sx - 1) |
2574 | 0 | goto absolute; |
2575 | | |
2576 | | /* Move to home position (0, 0). */ |
2577 | 0 | if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) { |
2578 | 0 | tty_putcode(tty, TTYC_HOME); |
2579 | 0 | goto out; |
2580 | 0 | } |
2581 | | |
2582 | | /* Zero on the next line. */ |
2583 | 0 | if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower && |
2584 | 0 | (!tty_use_margin(tty) || tty->rleft == 0)) { |
2585 | 0 | tty_putc(tty, '\r'); |
2586 | 0 | tty_putc(tty, '\n'); |
2587 | 0 | goto out; |
2588 | 0 | } |
2589 | | |
2590 | | /* Moving column or row. */ |
2591 | 0 | if (cy == thisy) { |
2592 | | /* |
2593 | | * Moving column only, row staying the same. |
2594 | | */ |
2595 | | |
2596 | | /* To left edge. */ |
2597 | 0 | if (cx == 0 && (!tty_use_margin(tty) || tty->rleft == 0)) { |
2598 | 0 | tty_putc(tty, '\r'); |
2599 | 0 | goto out; |
2600 | 0 | } |
2601 | | |
2602 | | /* One to the left. */ |
2603 | 0 | if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) { |
2604 | 0 | tty_putcode(tty, TTYC_CUB1); |
2605 | 0 | goto out; |
2606 | 0 | } |
2607 | | |
2608 | | /* One to the right. */ |
2609 | 0 | if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) { |
2610 | 0 | tty_putcode(tty, TTYC_CUF1); |
2611 | 0 | goto out; |
2612 | 0 | } |
2613 | | |
2614 | | /* Calculate difference. */ |
2615 | 0 | change = thisx - cx; /* +ve left, -ve right */ |
2616 | | |
2617 | | /* |
2618 | | * Use HPA if change is larger than absolute, otherwise move |
2619 | | * the cursor with CUB/CUF. |
2620 | | */ |
2621 | 0 | if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) { |
2622 | 0 | tty_putcode_i(tty, TTYC_HPA, cx); |
2623 | 0 | goto out; |
2624 | 0 | } else if (change > 0 && |
2625 | 0 | tty_term_has(term, TTYC_CUB) && |
2626 | 0 | !tty_use_margin(tty)) { |
2627 | 0 | if (change == 2 && tty_term_has(term, TTYC_CUB1)) { |
2628 | 0 | tty_putcode(tty, TTYC_CUB1); |
2629 | 0 | tty_putcode(tty, TTYC_CUB1); |
2630 | 0 | goto out; |
2631 | 0 | } |
2632 | 0 | tty_putcode_i(tty, TTYC_CUB, change); |
2633 | 0 | goto out; |
2634 | 0 | } else if (change < 0 && |
2635 | 0 | tty_term_has(term, TTYC_CUF) && |
2636 | 0 | !tty_use_margin(tty)) { |
2637 | 0 | tty_putcode_i(tty, TTYC_CUF, -change); |
2638 | 0 | goto out; |
2639 | 0 | } |
2640 | 0 | } else if (cx == thisx) { |
2641 | | /* |
2642 | | * Moving row only, column staying the same. |
2643 | | */ |
2644 | | |
2645 | | /* One above. */ |
2646 | 0 | if (thisy != tty->rupper && |
2647 | 0 | cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) { |
2648 | 0 | tty_putcode(tty, TTYC_CUU1); |
2649 | 0 | goto out; |
2650 | 0 | } |
2651 | | |
2652 | | /* One below. */ |
2653 | 0 | if (thisy != tty->rlower && |
2654 | 0 | cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) { |
2655 | 0 | tty_putcode(tty, TTYC_CUD1); |
2656 | 0 | goto out; |
2657 | 0 | } |
2658 | | |
2659 | | /* Calculate difference. */ |
2660 | 0 | change = thisy - cy; /* +ve up, -ve down */ |
2661 | | |
2662 | | /* |
2663 | | * Try to use VPA if change is larger than absolute or if this |
2664 | | * change would cross the scroll region, otherwise use CUU/CUD. |
2665 | | */ |
2666 | 0 | if ((u_int) abs(change) > cy || |
2667 | 0 | (change < 0 && cy - change > tty->rlower) || |
2668 | 0 | (change > 0 && cy - change < tty->rupper)) { |
2669 | 0 | if (tty_term_has(term, TTYC_VPA)) { |
2670 | 0 | tty_putcode_i(tty, TTYC_VPA, cy); |
2671 | 0 | goto out; |
2672 | 0 | } |
2673 | 0 | } else if (change > 0 && tty_term_has(term, TTYC_CUU)) { |
2674 | 0 | tty_putcode_i(tty, TTYC_CUU, change); |
2675 | 0 | goto out; |
2676 | 0 | } else if (change < 0 && tty_term_has(term, TTYC_CUD)) { |
2677 | 0 | tty_putcode_i(tty, TTYC_CUD, -change); |
2678 | 0 | goto out; |
2679 | 0 | } |
2680 | 0 | } |
2681 | | |
2682 | 0 | absolute: |
2683 | | /* Absolute movement. */ |
2684 | 0 | tty_putcode_ii(tty, TTYC_CUP, cy, cx); |
2685 | |
|
2686 | 0 | out: |
2687 | 0 | tty->cx = cx; |
2688 | 0 | tty->cy = cy; |
2689 | 0 | } |
2690 | | |
2691 | | static void |
2692 | | tty_hyperlink(struct tty *tty, const struct grid_cell *gc, |
2693 | | struct hyperlinks *hl) |
2694 | 0 | { |
2695 | 0 | const char *uri, *id; |
2696 | |
|
2697 | 0 | if (gc->link == tty->cell.link) |
2698 | 0 | return; |
2699 | 0 | tty->cell.link = gc->link; |
2700 | |
|
2701 | 0 | if (hl == NULL) |
2702 | 0 | return; |
2703 | | |
2704 | 0 | if (gc->link == 0 || !hyperlinks_get(hl, gc->link, &uri, NULL, &id)) |
2705 | 0 | tty_putcode_ss(tty, TTYC_HLS, "", ""); |
2706 | 0 | else |
2707 | 0 | tty_putcode_ss(tty, TTYC_HLS, id, uri); |
2708 | 0 | } |
2709 | | |
2710 | | void |
2711 | | tty_attributes(struct tty *tty, const struct grid_cell *gc, |
2712 | | const struct grid_cell *defaults, struct colour_palette *palette, |
2713 | | struct hyperlinks *hl) |
2714 | 0 | { |
2715 | 0 | struct grid_cell *tc = &tty->cell, gc2; |
2716 | 0 | int changed; |
2717 | | |
2718 | | /* Copy cell and update default colours. */ |
2719 | 0 | memcpy(&gc2, gc, sizeof gc2); |
2720 | 0 | if (~gc->flags & GRID_FLAG_NOPALETTE) { |
2721 | 0 | if (gc2.fg == 8) |
2722 | 0 | gc2.fg = defaults->fg; |
2723 | 0 | if (gc2.bg == 8) |
2724 | 0 | gc2.bg = defaults->bg; |
2725 | 0 | } |
2726 | | |
2727 | | /* Ignore cell if it is the same as the last one. */ |
2728 | 0 | if (gc2.attr == tty->last_cell.attr && |
2729 | 0 | gc2.fg == tty->last_cell.fg && |
2730 | 0 | gc2.bg == tty->last_cell.bg && |
2731 | 0 | gc2.us == tty->last_cell.us && |
2732 | 0 | gc2.link == tty->last_cell.link) |
2733 | 0 | return; |
2734 | | |
2735 | | /* |
2736 | | * If no setab, try to use the reverse attribute as a best-effort for a |
2737 | | * non-default background. This is a bit of a hack but it doesn't do |
2738 | | * any serious harm and makes a couple of applications happier. |
2739 | | */ |
2740 | 0 | if (!tty_term_has(tty->term, TTYC_SETAB)) { |
2741 | 0 | if (gc2.attr & GRID_ATTR_REVERSE) { |
2742 | 0 | if (gc2.fg != 7 && !COLOUR_DEFAULT(gc2.fg)) |
2743 | 0 | gc2.attr &= ~GRID_ATTR_REVERSE; |
2744 | 0 | } else { |
2745 | 0 | if (gc2.bg != 0 && !COLOUR_DEFAULT(gc2.bg)) |
2746 | 0 | gc2.attr |= GRID_ATTR_REVERSE; |
2747 | 0 | } |
2748 | 0 | } |
2749 | | |
2750 | | /* Fix up the colours if necessary. */ |
2751 | 0 | tty_check_fg(tty, palette, &gc2); |
2752 | 0 | tty_check_bg(tty, palette, &gc2); |
2753 | 0 | tty_check_us(tty, palette, &gc2); |
2754 | | |
2755 | | /* |
2756 | | * If any bits are being cleared or the underline colour is now default, |
2757 | | * reset everything. |
2758 | | */ |
2759 | 0 | if ((tc->attr & ~gc2.attr) || (tc->us != gc2.us && gc2.us == 0)) |
2760 | 0 | tty_reset(tty); |
2761 | | |
2762 | | /* |
2763 | | * Set the colours. This may call tty_reset() (so it comes next) and |
2764 | | * may add to (NOT remove) the desired attributes. |
2765 | | */ |
2766 | 0 | tty_colours(tty, &gc2); |
2767 | | |
2768 | | /* Filter out attribute bits already set. */ |
2769 | 0 | changed = gc2.attr & ~tc->attr; |
2770 | 0 | tc->attr = gc2.attr; |
2771 | | |
2772 | | /* Set the attributes. */ |
2773 | 0 | if (changed & GRID_ATTR_BRIGHT) |
2774 | 0 | tty_putcode(tty, TTYC_BOLD); |
2775 | 0 | if (changed & GRID_ATTR_DIM) |
2776 | 0 | tty_putcode(tty, TTYC_DIM); |
2777 | 0 | if (changed & GRID_ATTR_ITALICS) |
2778 | 0 | tty_set_italics(tty); |
2779 | 0 | if (changed & GRID_ATTR_ALL_UNDERSCORE) { |
2780 | 0 | if (changed & GRID_ATTR_UNDERSCORE) |
2781 | 0 | tty_putcode(tty, TTYC_SMUL); |
2782 | 0 | else if (changed & GRID_ATTR_UNDERSCORE_2) |
2783 | 0 | tty_putcode_i(tty, TTYC_SMULX, 2); |
2784 | 0 | else if (changed & GRID_ATTR_UNDERSCORE_3) |
2785 | 0 | tty_putcode_i(tty, TTYC_SMULX, 3); |
2786 | 0 | else if (changed & GRID_ATTR_UNDERSCORE_4) |
2787 | 0 | tty_putcode_i(tty, TTYC_SMULX, 4); |
2788 | 0 | else if (changed & GRID_ATTR_UNDERSCORE_5) |
2789 | 0 | tty_putcode_i(tty, TTYC_SMULX, 5); |
2790 | 0 | } |
2791 | 0 | if (changed & GRID_ATTR_BLINK) |
2792 | 0 | tty_putcode(tty, TTYC_BLINK); |
2793 | 0 | if (changed & GRID_ATTR_REVERSE) { |
2794 | 0 | if (tty_term_has(tty->term, TTYC_REV)) |
2795 | 0 | tty_putcode(tty, TTYC_REV); |
2796 | 0 | else if (tty_term_has(tty->term, TTYC_SMSO)) |
2797 | 0 | tty_putcode(tty, TTYC_SMSO); |
2798 | 0 | } |
2799 | 0 | if (changed & GRID_ATTR_HIDDEN) |
2800 | 0 | tty_putcode(tty, TTYC_INVIS); |
2801 | 0 | if (changed & GRID_ATTR_STRIKETHROUGH) |
2802 | 0 | tty_putcode(tty, TTYC_SMXX); |
2803 | 0 | if (changed & GRID_ATTR_OVERLINE) |
2804 | 0 | tty_putcode(tty, TTYC_SMOL); |
2805 | 0 | if ((changed & GRID_ATTR_CHARSET) && tty_acs_needed(tty)) |
2806 | 0 | tty_putcode(tty, TTYC_SMACS); |
2807 | | |
2808 | | /* Set hyperlink if any. */ |
2809 | 0 | tty_hyperlink(tty, gc, hl); |
2810 | |
|
2811 | 0 | memcpy(&tty->last_cell, &gc2, sizeof tty->last_cell); |
2812 | 0 | } |
2813 | | |
2814 | | static void |
2815 | | tty_colours(struct tty *tty, const struct grid_cell *gc) |
2816 | 0 | { |
2817 | 0 | struct grid_cell *tc = &tty->cell; |
2818 | | |
2819 | | /* No changes? Nothing is necessary. */ |
2820 | 0 | if (gc->fg == tc->fg && gc->bg == tc->bg && gc->us == tc->us) |
2821 | 0 | return; |
2822 | | |
2823 | | /* |
2824 | | * Is either the default colour? This is handled specially because the |
2825 | | * best solution might be to reset both colours to default, in which |
2826 | | * case if only one is default need to fall onward to set the other |
2827 | | * colour. |
2828 | | */ |
2829 | 0 | if (COLOUR_DEFAULT(gc->fg) || COLOUR_DEFAULT(gc->bg)) { |
2830 | | /* |
2831 | | * If don't have AX, send sgr0. This resets both colours to default. |
2832 | | * Otherwise, try to set the default colour only as needed. |
2833 | | */ |
2834 | 0 | if (!tty_term_flag(tty->term, TTYC_AX)) |
2835 | 0 | tty_reset(tty); |
2836 | 0 | else { |
2837 | 0 | if (COLOUR_DEFAULT(gc->fg) && !COLOUR_DEFAULT(tc->fg)) { |
2838 | 0 | tty_puts(tty, "\033[39m"); |
2839 | 0 | tc->fg = gc->fg; |
2840 | 0 | } |
2841 | 0 | if (COLOUR_DEFAULT(gc->bg) && !COLOUR_DEFAULT(tc->bg)) { |
2842 | 0 | tty_puts(tty, "\033[49m"); |
2843 | 0 | tc->bg = gc->bg; |
2844 | 0 | } |
2845 | 0 | } |
2846 | 0 | } |
2847 | | |
2848 | | /* Set the foreground colour. */ |
2849 | 0 | if (!COLOUR_DEFAULT(gc->fg) && gc->fg != tc->fg) |
2850 | 0 | tty_colours_fg(tty, gc); |
2851 | | |
2852 | | /* |
2853 | | * Set the background colour. This must come after the foreground as |
2854 | | * tty_colours_fg() can call tty_reset(). |
2855 | | */ |
2856 | 0 | if (!COLOUR_DEFAULT(gc->bg) && gc->bg != tc->bg) |
2857 | 0 | tty_colours_bg(tty, gc); |
2858 | | |
2859 | | /* Set the underscore colour. */ |
2860 | 0 | if (gc->us != tc->us) |
2861 | 0 | tty_colours_us(tty, gc); |
2862 | 0 | } |
2863 | | |
2864 | | static void |
2865 | | tty_check_fg(struct tty *tty, struct colour_palette *palette, |
2866 | | struct grid_cell *gc) |
2867 | 0 | { |
2868 | 0 | u_char r, g, b; |
2869 | 0 | u_int colours; |
2870 | 0 | int c; |
2871 | | |
2872 | | /* |
2873 | | * Perform substitution if this pane has a palette. If the bright |
2874 | | * attribute is set and Nobr is not present, use the bright entry in |
2875 | | * the palette by changing to the aixterm colour |
2876 | | */ |
2877 | 0 | if (~gc->flags & GRID_FLAG_NOPALETTE) { |
2878 | 0 | c = gc->fg; |
2879 | 0 | if (c < 8 && |
2880 | 0 | gc->attr & GRID_ATTR_BRIGHT && |
2881 | 0 | !tty_term_has(tty->term, TTYC_NOBR)) |
2882 | 0 | c += 90; |
2883 | 0 | if ((c = colour_palette_get(palette, c)) != -1) |
2884 | 0 | gc->fg = c; |
2885 | 0 | } |
2886 | | |
2887 | | /* Is this a 24-bit colour? */ |
2888 | 0 | if (gc->fg & COLOUR_FLAG_RGB) { |
2889 | | /* Not a 24-bit terminal? Translate to 256-colour palette. */ |
2890 | 0 | if (tty->term->flags & TERM_RGBCOLOURS) |
2891 | 0 | return; |
2892 | 0 | colour_split_rgb(gc->fg, &r, &g, &b); |
2893 | 0 | gc->fg = colour_find_rgb(r, g, b); |
2894 | 0 | } |
2895 | | |
2896 | | /* How many colours does this terminal have? */ |
2897 | 0 | if (tty->term->flags & TERM_256COLOURS) |
2898 | 0 | colours = 256; |
2899 | 0 | else |
2900 | 0 | colours = tty_term_number(tty->term, TTYC_COLORS); |
2901 | | |
2902 | | /* Is this a 256-colour colour? */ |
2903 | 0 | if (gc->fg & COLOUR_FLAG_256) { |
2904 | | /* And not a 256 colour mode? */ |
2905 | 0 | if (colours >= 256) |
2906 | 0 | return; |
2907 | 0 | gc->fg = colour_256to16(gc->fg); |
2908 | 0 | if (~gc->fg & 8) |
2909 | 0 | return; |
2910 | 0 | gc->fg &= 7; |
2911 | 0 | if (colours >= 16) |
2912 | 0 | gc->fg += 90; |
2913 | 0 | else { |
2914 | | /* |
2915 | | * Mapping to black-on-black or white-on-white is not |
2916 | | * much use, so change the foreground. |
2917 | | */ |
2918 | 0 | if (gc->fg == 0 && gc->bg == 0) |
2919 | 0 | gc->fg = 7; |
2920 | 0 | else if (gc->fg == 7 && gc->bg == 7) |
2921 | 0 | gc->fg = 0; |
2922 | 0 | } |
2923 | 0 | return; |
2924 | 0 | } |
2925 | | |
2926 | | /* Is this an aixterm colour? */ |
2927 | 0 | if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) { |
2928 | 0 | gc->fg -= 90; |
2929 | 0 | gc->attr |= GRID_ATTR_BRIGHT; |
2930 | 0 | } |
2931 | 0 | } |
2932 | | |
2933 | | static void |
2934 | | tty_check_bg(struct tty *tty, struct colour_palette *palette, |
2935 | | struct grid_cell *gc) |
2936 | 0 | { |
2937 | 0 | u_char r, g, b; |
2938 | 0 | u_int colours; |
2939 | 0 | int c; |
2940 | | |
2941 | | /* Perform substitution if this pane has a palette. */ |
2942 | 0 | if (~gc->flags & GRID_FLAG_NOPALETTE) { |
2943 | 0 | if ((c = colour_palette_get(palette, gc->bg)) != -1) |
2944 | 0 | gc->bg = c; |
2945 | 0 | } |
2946 | | |
2947 | | /* Is this a 24-bit colour? */ |
2948 | 0 | if (gc->bg & COLOUR_FLAG_RGB) { |
2949 | | /* Not a 24-bit terminal? Translate to 256-colour palette. */ |
2950 | 0 | if (tty->term->flags & TERM_RGBCOLOURS) |
2951 | 0 | return; |
2952 | 0 | colour_split_rgb(gc->bg, &r, &g, &b); |
2953 | 0 | gc->bg = colour_find_rgb(r, g, b); |
2954 | 0 | } |
2955 | | |
2956 | | /* How many colours does this terminal have? */ |
2957 | 0 | if (tty->term->flags & TERM_256COLOURS) |
2958 | 0 | colours = 256; |
2959 | 0 | else |
2960 | 0 | colours = tty_term_number(tty->term, TTYC_COLORS); |
2961 | | |
2962 | | /* Is this a 256-colour colour? */ |
2963 | 0 | if (gc->bg & COLOUR_FLAG_256) { |
2964 | | /* |
2965 | | * And not a 256 colour mode? Translate to 16-colour |
2966 | | * palette. Bold background doesn't exist portably, so just |
2967 | | * discard the bold bit if set. |
2968 | | */ |
2969 | 0 | if (colours >= 256) |
2970 | 0 | return; |
2971 | 0 | gc->bg = colour_256to16(gc->bg); |
2972 | 0 | if (~gc->bg & 8) |
2973 | 0 | return; |
2974 | 0 | gc->bg &= 7; |
2975 | 0 | if (colours >= 16) |
2976 | 0 | gc->bg += 90; |
2977 | 0 | return; |
2978 | 0 | } |
2979 | | |
2980 | | /* Is this an aixterm colour? */ |
2981 | 0 | if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) |
2982 | 0 | gc->bg -= 90; |
2983 | 0 | } |
2984 | | |
2985 | | static void |
2986 | | tty_check_us(__unused struct tty *tty, struct colour_palette *palette, |
2987 | | struct grid_cell *gc) |
2988 | 0 | { |
2989 | 0 | int c; |
2990 | | |
2991 | | /* Perform substitution if this pane has a palette. */ |
2992 | 0 | if (~gc->flags & GRID_FLAG_NOPALETTE) { |
2993 | 0 | if ((c = colour_palette_get(palette, gc->us)) != -1) |
2994 | 0 | gc->us = c; |
2995 | 0 | } |
2996 | | |
2997 | | /* Convert underscore colour if only RGB can be supported. */ |
2998 | 0 | if (!tty_term_has(tty->term, TTYC_SETULC1)) { |
2999 | 0 | if ((c = colour_force_rgb (gc->us)) == -1) |
3000 | 0 | gc->us = 8; |
3001 | 0 | else |
3002 | 0 | gc->us = c; |
3003 | 0 | } |
3004 | 0 | } |
3005 | | |
3006 | | static void |
3007 | | tty_colours_fg(struct tty *tty, const struct grid_cell *gc) |
3008 | 0 | { |
3009 | 0 | struct grid_cell *tc = &tty->cell; |
3010 | 0 | char s[32]; |
3011 | | |
3012 | | /* |
3013 | | * If the current colour is an aixterm bright colour and the new is not, |
3014 | | * reset because some terminals do not clear bright correctly. |
3015 | | */ |
3016 | 0 | if (tty->cell.fg >= 90 && |
3017 | 0 | tty->cell.bg <= 97 && |
3018 | 0 | (gc->fg < 90 || gc->fg > 97)) |
3019 | 0 | tty_reset(tty); |
3020 | | |
3021 | | /* Is this a 24-bit or 256-colour colour? */ |
3022 | 0 | if (gc->fg & COLOUR_FLAG_RGB || gc->fg & COLOUR_FLAG_256) { |
3023 | 0 | if (tty_try_colour(tty, gc->fg, "38") == 0) |
3024 | 0 | goto save; |
3025 | | /* Should not get here, already converted in tty_check_fg. */ |
3026 | 0 | return; |
3027 | 0 | } |
3028 | | |
3029 | | /* Is this an aixterm bright colour? */ |
3030 | 0 | if (gc->fg >= 90 && gc->fg <= 97) { |
3031 | 0 | if (tty->term->flags & TERM_256COLOURS) { |
3032 | 0 | xsnprintf(s, sizeof s, "\033[%dm", gc->fg); |
3033 | 0 | tty_puts(tty, s); |
3034 | 0 | } else |
3035 | 0 | tty_putcode_i(tty, TTYC_SETAF, gc->fg - 90 + 8); |
3036 | 0 | goto save; |
3037 | 0 | } |
3038 | | |
3039 | | /* Otherwise set the foreground colour. */ |
3040 | 0 | tty_putcode_i(tty, TTYC_SETAF, gc->fg); |
3041 | |
|
3042 | 0 | save: |
3043 | | /* Save the new values in the terminal current cell. */ |
3044 | 0 | tc->fg = gc->fg; |
3045 | 0 | } |
3046 | | |
3047 | | static void |
3048 | | tty_colours_bg(struct tty *tty, const struct grid_cell *gc) |
3049 | 0 | { |
3050 | 0 | struct grid_cell *tc = &tty->cell; |
3051 | 0 | char s[32]; |
3052 | | |
3053 | | /* Is this a 24-bit or 256-colour colour? */ |
3054 | 0 | if (gc->bg & COLOUR_FLAG_RGB || gc->bg & COLOUR_FLAG_256) { |
3055 | 0 | if (tty_try_colour(tty, gc->bg, "48") == 0) |
3056 | 0 | goto save; |
3057 | | /* Should not get here, already converted in tty_check_bg. */ |
3058 | 0 | return; |
3059 | 0 | } |
3060 | | |
3061 | | /* Is this an aixterm bright colour? */ |
3062 | 0 | if (gc->bg >= 90 && gc->bg <= 97) { |
3063 | 0 | if (tty->term->flags & TERM_256COLOURS) { |
3064 | 0 | xsnprintf(s, sizeof s, "\033[%dm", gc->bg + 10); |
3065 | 0 | tty_puts(tty, s); |
3066 | 0 | } else |
3067 | 0 | tty_putcode_i(tty, TTYC_SETAB, gc->bg - 90 + 8); |
3068 | 0 | goto save; |
3069 | 0 | } |
3070 | | |
3071 | | /* Otherwise set the background colour. */ |
3072 | 0 | tty_putcode_i(tty, TTYC_SETAB, gc->bg); |
3073 | |
|
3074 | 0 | save: |
3075 | | /* Save the new values in the terminal current cell. */ |
3076 | 0 | tc->bg = gc->bg; |
3077 | 0 | } |
3078 | | |
3079 | | static void |
3080 | | tty_colours_us(struct tty *tty, const struct grid_cell *gc) |
3081 | 0 | { |
3082 | 0 | struct grid_cell *tc = &tty->cell; |
3083 | 0 | u_int c; |
3084 | 0 | u_char r, g, b; |
3085 | | |
3086 | | /* Clear underline colour. */ |
3087 | 0 | if (COLOUR_DEFAULT(gc->us)) { |
3088 | 0 | tty_putcode(tty, TTYC_OL); |
3089 | 0 | goto save; |
3090 | 0 | } |
3091 | | |
3092 | | /* |
3093 | | * If this is not an RGB colour, use Setulc1 if it exists, otherwise |
3094 | | * convert. |
3095 | | */ |
3096 | 0 | if (~gc->us & COLOUR_FLAG_RGB) { |
3097 | 0 | c = gc->us; |
3098 | 0 | if ((~c & COLOUR_FLAG_256) && (c >= 90 && c <= 97)) |
3099 | 0 | c -= 82; |
3100 | 0 | tty_putcode_i(tty, TTYC_SETULC1, c & ~COLOUR_FLAG_256); |
3101 | 0 | return; |
3102 | 0 | } |
3103 | | |
3104 | | /* |
3105 | | * Setulc and setal follows the ncurses(3) one argument "direct colour" |
3106 | | * capability format. Calculate the colour value. |
3107 | | */ |
3108 | 0 | colour_split_rgb(gc->us, &r, &g, &b); |
3109 | 0 | c = (65536 * r) + (256 * g) + b; |
3110 | | |
3111 | | /* |
3112 | | * Write the colour. Only use setal if the RGB flag is set because the |
3113 | | * non-RGB version may be wrong. |
3114 | | */ |
3115 | 0 | if (tty_term_has(tty->term, TTYC_SETULC)) |
3116 | 0 | tty_putcode_i(tty, TTYC_SETULC, c); |
3117 | 0 | else if (tty_term_has(tty->term, TTYC_SETAL) && |
3118 | 0 | tty_term_has(tty->term, TTYC_RGB)) |
3119 | 0 | tty_putcode_i(tty, TTYC_SETAL, c); |
3120 | |
|
3121 | 0 | save: |
3122 | | /* Save the new values in the terminal current cell. */ |
3123 | 0 | tc->us = gc->us; |
3124 | 0 | } |
3125 | | |
3126 | | static int |
3127 | | tty_try_colour(struct tty *tty, int colour, const char *type) |
3128 | 0 | { |
3129 | 0 | u_char r, g, b; |
3130 | |
|
3131 | 0 | if (colour & COLOUR_FLAG_256) { |
3132 | 0 | if (*type == '3' && tty_term_has(tty->term, TTYC_SETAF)) |
3133 | 0 | tty_putcode_i(tty, TTYC_SETAF, colour & 0xff); |
3134 | 0 | else if (tty_term_has(tty->term, TTYC_SETAB)) |
3135 | 0 | tty_putcode_i(tty, TTYC_SETAB, colour & 0xff); |
3136 | 0 | return (0); |
3137 | 0 | } |
3138 | | |
3139 | 0 | if (colour & COLOUR_FLAG_RGB) { |
3140 | 0 | colour_split_rgb(colour & 0xffffff, &r, &g, &b); |
3141 | 0 | if (*type == '3' && tty_term_has(tty->term, TTYC_SETRGBF)) |
3142 | 0 | tty_putcode_iii(tty, TTYC_SETRGBF, r, g, b); |
3143 | 0 | else if (tty_term_has(tty->term, TTYC_SETRGBB)) |
3144 | 0 | tty_putcode_iii(tty, TTYC_SETRGBB, r, g, b); |
3145 | 0 | return (0); |
3146 | 0 | } |
3147 | | |
3148 | 0 | return (-1); |
3149 | 0 | } |
3150 | | |
3151 | | static void |
3152 | | tty_window_default_style(struct grid_cell *gc, struct window_pane *wp) |
3153 | 7.91k | { |
3154 | 7.91k | memcpy(gc, &grid_default_cell, sizeof *gc); |
3155 | 7.91k | gc->fg = wp->palette.fg; |
3156 | 7.91k | gc->bg = wp->palette.bg; |
3157 | 7.91k | } |
3158 | | |
3159 | | void |
3160 | | tty_default_colours(struct grid_cell *gc, struct window_pane *wp) |
3161 | 49.9k | { |
3162 | 49.9k | struct options *oo = wp->options; |
3163 | 49.9k | struct format_tree *ft; |
3164 | | |
3165 | 49.9k | memcpy(gc, &grid_default_cell, sizeof *gc); |
3166 | | |
3167 | 49.9k | if (wp->flags & PANE_STYLECHANGED) { |
3168 | 3.95k | log_debug("%%%u: style changed", wp->id); |
3169 | 3.95k | wp->flags &= ~PANE_STYLECHANGED; |
3170 | | |
3171 | 3.95k | ft = format_create(NULL, NULL, FORMAT_PANE|wp->id, |
3172 | 3.95k | FORMAT_NOJOBS); |
3173 | 3.95k | format_defaults(ft, NULL, NULL, NULL, wp); |
3174 | 3.95k | tty_window_default_style(&wp->cached_active_gc, wp); |
3175 | 3.95k | style_add(&wp->cached_active_gc, oo, "window-active-style", ft); |
3176 | 3.95k | tty_window_default_style(&wp->cached_gc, wp); |
3177 | 3.95k | style_add(&wp->cached_gc, oo, "window-style", ft); |
3178 | 3.95k | format_free(ft); |
3179 | 3.95k | } |
3180 | | |
3181 | 49.9k | if (gc->fg == 8) { |
3182 | 49.9k | if (wp == wp->window->active && wp->cached_active_gc.fg != 8) |
3183 | 0 | gc->fg = wp->cached_active_gc.fg; |
3184 | 49.9k | else |
3185 | 49.9k | gc->fg = wp->cached_gc.fg; |
3186 | 49.9k | } |
3187 | | |
3188 | 49.9k | if (gc->bg == 8) { |
3189 | 49.9k | if (wp == wp->window->active && wp->cached_active_gc.bg != 8) |
3190 | 0 | gc->bg = wp->cached_active_gc.bg; |
3191 | 49.9k | else |
3192 | 49.9k | gc->bg = wp->cached_gc.bg; |
3193 | 49.9k | } |
3194 | 49.9k | } |
3195 | | |
3196 | | static void |
3197 | | tty_default_attributes(struct tty *tty, const struct grid_cell *defaults, |
3198 | | struct colour_palette *palette, u_int bg, struct hyperlinks *hl) |
3199 | 0 | { |
3200 | 0 | struct grid_cell gc; |
3201 | |
|
3202 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
3203 | 0 | gc.bg = bg; |
3204 | 0 | tty_attributes(tty, &gc, defaults, palette, hl); |
3205 | 0 | } |
3206 | | |
3207 | | static void |
3208 | | tty_clipboard_query_callback(__unused int fd, __unused short events, void *data) |
3209 | 0 | { |
3210 | 0 | struct tty *tty = data; |
3211 | 0 | struct client *c = tty->client; |
3212 | |
|
3213 | 0 | c->flags &= ~CLIENT_CLIPBOARDBUFFER; |
3214 | 0 | free(c->clipboard_panes); |
3215 | 0 | c->clipboard_panes = NULL; |
3216 | 0 | c->clipboard_npanes = 0; |
3217 | |
|
3218 | 0 | tty->flags &= ~TTY_OSC52QUERY; |
3219 | 0 | } |
3220 | | |
3221 | | void |
3222 | | tty_clipboard_query(struct tty *tty) |
3223 | 0 | { |
3224 | 0 | struct timeval tv = { .tv_sec = TTY_QUERY_TIMEOUT }; |
3225 | |
|
3226 | 0 | if ((~tty->flags & TTY_STARTED) || (tty->flags & TTY_OSC52QUERY)) |
3227 | 0 | return; |
3228 | 0 | tty_putcode_ss(tty, TTYC_MS, "", "?"); |
3229 | |
|
3230 | 0 | tty->flags |= TTY_OSC52QUERY; |
3231 | 0 | evtimer_set(&tty->clipboard_timer, tty_clipboard_query_callback, tty); |
3232 | | evtimer_add(&tty->clipboard_timer, &tv); |
3233 | 0 | } |