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