Annotation of src/usr.bin/tmux/tty.c, Revision 1.115
1.115 ! nicm 1: /* $OpenBSD: tty.c,v 1.114 2012/02/15 17:25:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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:
1.106 nicm 22: #include <netinet/in.h>
23:
1.1 nicm 24: #include <errno.h>
25: #include <fcntl.h>
1.106 nicm 26: #include <resolv.h>
1.42 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
29: #include <termios.h>
30: #include <unistd.h>
31:
32: #include "tmux.h"
33:
1.67 nicm 34: void tty_read_callback(struct bufferevent *, void *);
1.66 nicm 35: void tty_error_callback(struct bufferevent *, short, void *);
36:
1.1 nicm 37: int tty_try_256(struct tty *, u_char, const char *);
38: int tty_try_88(struct tty *, u_char, const char *);
39:
1.85 nicm 40: void tty_colours(struct tty *, const struct grid_cell *);
41: void tty_check_fg(struct tty *, struct grid_cell *);
42: void tty_check_bg(struct tty *, struct grid_cell *);
43: void tty_colours_fg(struct tty *, const struct grid_cell *);
1.73 nicm 44: void tty_colours_bg(struct tty *, const struct grid_cell *);
1.1 nicm 45:
1.24 nicm 46: void tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13 nicm 47: void tty_emulate_repeat(
48: struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.14 nicm 49: void tty_cell(struct tty *,
1.77 nicm 50: const struct grid_cell *, const struct grid_utf8 *);
1.1 nicm 51:
1.90 nicm 52: #define tty_use_acs(tty) \
1.91 nicm 53: (tty_term_has(tty->term, TTYC_ACSC) && !((tty)->flags & TTY_UTF8))
1.90 nicm 54:
1.1 nicm 55: void
1.30 nicm 56: tty_init(struct tty *tty, int fd, char *term)
1.1 nicm 57: {
1.30 nicm 58: char *path;
59:
60: memset(tty, 0, sizeof *tty);
1.23 nicm 61: tty->log_fd = -1;
1.22 nicm 62:
1.9 nicm 63: if (term == NULL || *term == '\0')
1.1 nicm 64: tty->termname = xstrdup("unknown");
65: else
66: tty->termname = xstrdup(term);
1.30 nicm 67: tty->fd = fd;
68:
69: if ((path = ttyname(fd)) == NULL)
70: fatalx("ttyname failed");
71: tty->path = xstrdup(path);
1.108 nicm 72: tty->cstyle = 0;
1.107 nicm 73: tty->ccolour = xstrdup("");
1.30 nicm 74:
1.1 nicm 75: tty->flags = 0;
76: tty->term_flags = 0;
1.31 nicm 77: }
78:
1.88 nicm 79: int
1.31 nicm 80: tty_resize(struct tty *tty)
81: {
82: struct winsize ws;
1.88 nicm 83: u_int sx, sy;
1.31 nicm 84:
85: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
1.88 nicm 86: sx = ws.ws_col;
87: if (sx == 0)
88: sx = 80;
89: sy = ws.ws_row;
90: if (sy == 0)
91: sy = 24;
92: } else {
93: sx = 80;
94: sy = 24;
1.31 nicm 95: }
1.114 nicm 96: if (!tty_set_size(tty, sx, sy))
1.88 nicm 97: return (0);
1.31 nicm 98:
99: tty->cx = UINT_MAX;
100: tty->cy = UINT_MAX;
101:
102: tty->rupper = UINT_MAX;
103: tty->rlower = UINT_MAX;
1.88 nicm 104:
105: /*
106: * If the terminal has been started, reset the actual scroll region and
107: * cursor position, as this may not have happened.
108: */
109: if (tty->flags & TTY_STARTED) {
110: tty_cursor(tty, 0, 0);
111: tty_region(tty, 0, tty->sy - 1);
112: }
113:
1.114 nicm 114: return (1);
115: }
116:
117: int
118: tty_set_size(struct tty *tty, u_int sx, u_int sy) {
119: if (sx == tty->sx && sy == tty->sy)
120: return (0);
121: tty->sx = sx;
122: tty->sy = sy;
1.88 nicm 123: return (1);
1.1 nicm 124: }
125:
126: int
1.17 nicm 127: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1 nicm 128: {
1.88 nicm 129: char out[64];
1.30 nicm 130: int fd;
1.1 nicm 131:
1.30 nicm 132: if (debug_level > 3) {
1.88 nicm 133: xsnprintf(out, sizeof out, "tmux-out-%ld.log", (long) getpid());
134: fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1.30 nicm 135: if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
136: fatal("fcntl failed");
137: tty->log_fd = fd;
1.1 nicm 138: }
139:
1.17 nicm 140: tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21 nicm 141: if (tty->term == NULL) {
142: tty_close(tty);
143: return (-1);
144: }
145: tty->flags |= TTY_OPENED;
1.1 nicm 146:
1.66 nicm 147: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
1.1 nicm 148:
1.66 nicm 149: tty->event = bufferevent_new(
1.67 nicm 150: tty->fd, tty_read_callback, NULL, tty_error_callback, tty);
1.1 nicm 151:
152: tty_start_tty(tty);
153:
154: tty_keys_init(tty);
155:
156: return (0);
157: }
158:
1.73 nicm 159: /* ARGSUSED */
1.1 nicm 160: void
1.67 nicm 161: tty_read_callback(unused struct bufferevent *bufev, void *data)
162: {
163: struct tty *tty = data;
164:
165: while (tty_keys_next(tty))
166: ;
167: }
168:
1.73 nicm 169: /* ARGSUSED */
1.67 nicm 170: void
1.66 nicm 171: tty_error_callback(
172: unused struct bufferevent *bufev, unused short what, unused void *data)
173: {
174: }
175:
176: void
1.1 nicm 177: tty_start_tty(struct tty *tty)
178: {
179: struct termios tio;
1.33 nicm 180:
1.99 nicm 181: if (tty->fd == -1 || tcgetattr(tty->fd, &tty->tio) != 0)
1.33 nicm 182: return;
1.1 nicm 183:
1.96 nicm 184: setblocking(tty->fd, 0);
1.34 nicm 185:
1.66 nicm 186: bufferevent_enable(tty->event, EV_READ|EV_WRITE);
187:
1.1 nicm 188: memcpy(&tio, &tty->tio, sizeof tio);
189: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
190: tio.c_iflag |= IGNBRK;
191: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
192: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
193: ECHOPRT|ECHOKE|ECHOCTL|ISIG);
194: tio.c_cc[VMIN] = 1;
1.60 deraadt 195: tio.c_cc[VTIME] = 0;
1.99 nicm 196: if (tcsetattr(tty->fd, TCSANOW, &tio) == 0)
197: tcflush(tty->fd, TCIOFLUSH);
1.1 nicm 198:
1.10 nicm 199: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 200:
1.25 nicm 201: tty_putcode(tty, TTYC_SGR0);
202: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
203:
1.76 nicm 204: tty_putcode(tty, TTYC_RMKX);
1.90 nicm 205: if (tty_use_acs(tty))
206: tty_putcode(tty, TTYC_ENACS);
1.1 nicm 207: tty_putcode(tty, TTYC_CLEAR);
208:
209: tty_putcode(tty, TTYC_CNORM);
210: if (tty_term_has(tty->term, TTYC_KMOUS))
211: tty_puts(tty, "\033[?1000l");
212:
213: tty->cx = UINT_MAX;
214: tty->cy = UINT_MAX;
215:
216: tty->rlower = UINT_MAX;
217: tty->rupper = UINT_MAX;
218:
219: tty->mode = MODE_CURSOR;
1.20 nicm 220:
221: tty->flags |= TTY_STARTED;
1.107 nicm 222:
223: tty_force_cursor_colour(tty, "");
1.1 nicm 224: }
225:
226: void
227: tty_stop_tty(struct tty *tty)
228: {
229: struct winsize ws;
230:
1.20 nicm 231: if (!(tty->flags & TTY_STARTED))
232: return;
233: tty->flags &= ~TTY_STARTED;
234:
1.66 nicm 235: bufferevent_disable(tty->event, EV_READ|EV_WRITE);
236:
1.1 nicm 237: /*
238: * Be flexible about error handling and try not kill the server just
239: * because the fd is invalid. Things like ssh -t can easily leave us
240: * with a dead tty.
241: */
242: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
243: return;
244: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
245: return;
246:
1.103 nicm 247: setblocking(tty->fd, 1);
248:
1.1 nicm 249: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
1.90 nicm 250: if (tty_use_acs(tty))
251: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
1.1 nicm 252: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
253: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
254: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
1.108 nicm 255: if (tty_term_has(tty->term, TTYC_CS1) && tty->cstyle != 0) {
256: if (tty_term_has(tty->term, TTYC_CSR1))
257: tty_raw(tty, tty_term_string(tty->term, TTYC_CSR1));
1.109 nicm 258: else
1.108 nicm 259: tty_raw(tty, tty_term_string1(tty->term, TTYC_CS1, 0));
260: }
1.107 nicm 261: tty_raw(tty, tty_term_string(tty->term, TTYC_CR));
1.1 nicm 262:
263: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
264: if (tty_term_has(tty->term, TTYC_KMOUS))
265: tty_raw(tty, "\033[?1000l");
1.10 nicm 266:
267: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.1 nicm 268: }
269:
270: void
1.20 nicm 271: tty_close(struct tty *tty)
1.1 nicm 272: {
273: if (tty->log_fd != -1) {
274: close(tty->log_fd);
275: tty->log_fd = -1;
276: }
277:
1.67 nicm 278: evtimer_del(&tty->key_timer);
1.20 nicm 279: tty_stop_tty(tty);
1.1 nicm 280:
1.21 nicm 281: if (tty->flags & TTY_OPENED) {
1.66 nicm 282: bufferevent_free(tty->event);
283:
1.21 nicm 284: tty_term_free(tty->term);
285: tty_keys_free(tty);
286:
287: tty->flags &= ~TTY_OPENED;
288: }
1.1 nicm 289:
1.21 nicm 290: if (tty->fd != -1) {
291: close(tty->fd);
292: tty->fd = -1;
293: }
1.1 nicm 294: }
295:
296: void
1.20 nicm 297: tty_free(struct tty *tty)
1.1 nicm 298: {
1.20 nicm 299: tty_close(tty);
1.1 nicm 300:
1.107 nicm 301: xfree(tty->ccolour);
1.1 nicm 302: if (tty->path != NULL)
303: xfree(tty->path);
304: if (tty->termname != NULL)
305: xfree(tty->termname);
306: }
307:
308: void
309: tty_raw(struct tty *tty, const char *s)
310: {
311: write(tty->fd, s, strlen(s));
312: }
313:
314: void
315: tty_putcode(struct tty *tty, enum tty_code_code code)
316: {
317: tty_puts(tty, tty_term_string(tty->term, code));
318: }
319:
320: void
321: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
322: {
323: if (a < 0)
324: return;
325: tty_puts(tty, tty_term_string1(tty->term, code, a));
326: }
327:
328: void
329: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
330: {
331: if (a < 0 || b < 0)
332: return;
333: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
334: }
335:
336: void
1.107 nicm 337: tty_putcode_ptr1(struct tty *tty, enum tty_code_code code, const void *a)
338: {
339: if (a != NULL)
340: tty_puts(tty, tty_term_ptr1(tty->term, code, a));
341: }
342:
343: void
1.106 nicm 344: tty_putcode_ptr2(struct tty *tty, enum tty_code_code code, const void *a, const void *b)
345: {
346: if (a != NULL && b != NULL)
347: tty_puts(tty, tty_term_ptr2(tty->term, code, a, b));
348: }
349:
350: void
1.1 nicm 351: tty_puts(struct tty *tty, const char *s)
352: {
353: if (*s == '\0')
354: return;
1.66 nicm 355: bufferevent_write(tty->event, s, strlen(s));
1.1 nicm 356:
357: if (tty->log_fd != -1)
358: write(tty->log_fd, s, strlen(s));
359: }
360:
361: void
362: tty_putc(struct tty *tty, u_char ch)
363: {
1.90 nicm 364: const char *acs;
365: u_int sx;
1.1 nicm 366:
1.90 nicm 367: if (tty->cell.attr & GRID_ATTR_CHARSET) {
368: acs = tty_acs_get(tty, ch);
369: if (acs != NULL)
370: bufferevent_write(tty->event, acs, strlen(acs));
371: else
372: bufferevent_write(tty->event, &ch, 1);
373: } else
374: bufferevent_write(tty->event, &ch, 1);
1.1 nicm 375:
376: if (ch >= 0x20 && ch != 0x7f) {
377: sx = tty->sx;
378: if (tty->term->flags & TERM_EARLYWRAP)
379: sx--;
380:
1.46 nicm 381: if (tty->cx >= sx) {
382: tty->cx = 1;
383: if (tty->cy != tty->rlower)
384: tty->cy++;
1.1 nicm 385: } else
386: tty->cx++;
387: }
388:
389: if (tty->log_fd != -1)
390: write(tty->log_fd, &ch, 1);
391: }
392:
393: void
1.5 nicm 394: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
395: {
1.71 nicm 396: size_t size;
1.5 nicm 397:
1.71 nicm 398: size = grid_utf8_size(gu);
399: bufferevent_write(tty->event, gu->data, size);
400: if (tty->log_fd != -1)
401: write(tty->log_fd, gu->data, size);
1.54 nicm 402: tty->cx += gu->width;
1.5 nicm 403: }
404:
405: void
1.1 nicm 406: tty_set_title(struct tty *tty, const char *title)
407: {
1.105 nicm 408: if (!tty_term_has(tty->term, TTYC_TSL) ||
409: !tty_term_has(tty->term, TTYC_FSL))
1.1 nicm 410: return;
411:
1.105 nicm 412: tty_putcode(tty, TTYC_TSL);
1.1 nicm 413: tty_puts(tty, title);
1.105 nicm 414: tty_putcode(tty, TTYC_FSL);
1.1 nicm 415: }
416:
417: void
1.107 nicm 418: tty_force_cursor_colour(struct tty *tty, const char *ccolour)
419: {
420: if (*ccolour == '\0')
421: tty_putcode(tty, TTYC_CR);
422: else
423: tty_putcode_ptr1(tty, TTYC_CC, ccolour);
424: xfree(tty->ccolour);
425: tty->ccolour = xstrdup(ccolour);
426: }
427:
428: void
429: tty_update_mode(struct tty *tty, int mode, struct screen *s)
1.1 nicm 430: {
431: int changed;
432:
1.107 nicm 433: if (strcmp(s->ccolour, tty->ccolour))
434: tty_force_cursor_colour(tty, s->ccolour);
435:
1.1 nicm 436: if (tty->flags & TTY_NOCURSOR)
437: mode &= ~MODE_CURSOR;
438:
439: changed = mode ^ tty->mode;
440: if (changed & MODE_CURSOR) {
441: if (mode & MODE_CURSOR)
442: tty_putcode(tty, TTYC_CNORM);
443: else
444: tty_putcode(tty, TTYC_CIVIS);
1.108 nicm 445: }
446: if (tty->cstyle != s->cstyle) {
447: if (tty_term_has(tty->term, TTYC_CS1)) {
448: if (s->cstyle == 0 &&
449: tty_term_has(tty->term, TTYC_CSR1))
450: tty_putcode(tty, TTYC_CSR1);
451: else
452: tty_putcode1(tty, TTYC_CS1, s->cstyle);
453: }
454: tty->cstyle = s->cstyle;
1.1 nicm 455: }
1.94 nicm 456: if (changed & ALL_MOUSE_MODES) {
457: if (mode & ALL_MOUSE_MODES) {
1.95 nicm 458: if (mode & MODE_MOUSE_UTF8)
459: tty_puts(tty, "\033[?1005h");
1.98 nicm 460: if (mode & MODE_MOUSE_ANY)
461: tty_puts(tty, "\033[?1003h");
1.94 nicm 462: else if (mode & MODE_MOUSE_BUTTON)
463: tty_puts(tty, "\033[?1002h");
1.98 nicm 464: else if (mode & MODE_MOUSE_STANDARD)
465: tty_puts(tty, "\033[?1000h");
1.86 nicm 466: } else {
1.98 nicm 467: if (tty->mode & MODE_MOUSE_ANY)
468: tty_puts(tty, "\033[?1003l");
1.94 nicm 469: else if (tty->mode & MODE_MOUSE_BUTTON)
470: tty_puts(tty, "\033[?1002l");
1.98 nicm 471: else if (tty->mode & MODE_MOUSE_STANDARD)
472: tty_puts(tty, "\033[?1000l");
1.95 nicm 473: if (tty->mode & MODE_MOUSE_UTF8)
474: tty_puts(tty, "\033[?1005l");
1.86 nicm 475: }
1.76 nicm 476: }
477: if (changed & MODE_KKEYPAD) {
478: if (mode & MODE_KKEYPAD)
479: tty_putcode(tty, TTYC_SMKX);
480: else
481: tty_putcode(tty, TTYC_RMKX);
1.115 ! nicm 482: }
! 483: if (changed & MODE_BRACKETPASTE) {
! 484: if (mode & MODE_BRACKETPASTE)
! 485: tty_puts(tty, "\033[?2004h");
! 486: else
! 487: tty_puts(tty, "\033[?2004l");
1.1 nicm 488: }
489: tty->mode = mode;
490: }
491:
492: void
493: tty_emulate_repeat(
494: struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
495: {
496: if (tty_term_has(tty->term, code))
497: tty_putcode1(tty, code, n);
498: else {
499: while (n-- > 0)
500: tty_putcode(tty, code1);
501: }
502: }
503:
504: /*
505: * Redraw scroll region using data from screen (already updated). Used when
506: * CSR not supported, or window is a pane that doesn't take up the full
507: * width of the terminal.
508: */
509: void
1.24 nicm 510: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 511: {
1.14 nicm 512: struct window_pane *wp = ctx->wp;
513: struct screen *s = wp->screen;
514: u_int i;
1.1 nicm 515:
516: /*
517: * If region is >= 50% of the screen, just schedule a window redraw. In
518: * most cases, this is likely to be followed by some more scrolling -
519: * without this, the entire pane ends up being redrawn many times which
520: * can be much more data.
521: */
1.101 nicm 522: if (ctx->orlower - ctx->orupper >= screen_size_y(s) / 2) {
1.1 nicm 523: wp->flags |= PANE_REDRAW;
524: return;
525: }
526:
1.14 nicm 527: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
528: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.113 nicm 529: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 530: } else {
1.14 nicm 531: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.113 nicm 532: tty_draw_line(tty, s, i, ctx->xoff, ctx->yoff);
1.1 nicm 533: }
534: }
535:
536: void
537: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
538: {
539: const struct grid_cell *gc;
1.46 nicm 540: struct grid_line *gl;
1.16 nicm 541: struct grid_cell tmpgc;
1.1 nicm 542: const struct grid_utf8 *gu;
543: u_int i, sx;
544:
1.107 nicm 545: tty_update_mode(tty, tty->mode & ~MODE_CURSOR, s);
1.35 nicm 546:
1.1 nicm 547: sx = screen_size_x(s);
1.19 nicm 548: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
549: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 550: if (sx > tty->sx)
551: sx = tty->sx;
552:
1.46 nicm 553: /*
554: * Don't move the cursor to the start permission if it will wrap there
1.50 nicm 555: * itself.
1.46 nicm 556: */
557: gl = NULL;
558: if (py != 0)
559: gl = &s->grid->linedata[s->grid->hsize + py - 1];
1.83 nicm 560: if (oy + py == 0 || gl == NULL || !(gl->flags & GRID_LINE_WRAPPED) ||
1.47 nicm 561: tty->cx < tty->sx || ox != 0 ||
562: (oy + py != tty->cy + 1 && tty->cy != s->rlower + oy))
1.46 nicm 563: tty_cursor(tty, ox, oy + py);
564:
1.1 nicm 565: for (i = 0; i < sx; i++) {
566: gc = grid_view_peek_cell(s->grid, i, py);
567:
568: gu = NULL;
569: if (gc->flags & GRID_FLAG_UTF8)
570: gu = grid_view_peek_utf8(s->grid, i, py);
571:
572: if (screen_check_selection(s, i, py)) {
1.16 nicm 573: memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
574: tmpgc.data = gc->data;
1.77 nicm 575: tmpgc.flags = gc->flags &
1.38 nicm 576: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
577: tmpgc.flags |= s->sel.cell.flags &
578: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16 nicm 579: tty_cell(tty, &tmpgc, gu);
1.1 nicm 580: } else
581: tty_cell(tty, gc, gu);
582: }
583:
1.35 nicm 584: if (sx >= tty->sx) {
1.107 nicm 585: tty_update_mode(tty, tty->mode, s);
1.1 nicm 586: return;
1.35 nicm 587: }
1.1 nicm 588: tty_reset(tty);
589:
1.41 nicm 590: tty_cursor(tty, ox + sx, oy + py);
1.1 nicm 591: if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
592: tty_putcode(tty, TTYC_EL);
593: else {
594: for (i = sx; i < screen_size_x(s); i++)
595: tty_putc(tty, ' ');
1.15 nicm 596: }
1.107 nicm 597: tty_update_mode(tty, tty->mode, s);
1.15 nicm 598: }
599:
600: void
1.113 nicm 601: tty_write(
602: void (*cmdfn)(struct tty *, const struct tty_ctx *), struct tty_ctx *ctx)
1.15 nicm 603: {
604: struct window_pane *wp = ctx->wp;
605: struct client *c;
1.113 nicm 606: struct session *s;
607: struct options *oo;
1.15 nicm 608: u_int i;
609:
1.75 nicm 610: /* wp can be NULL if updating the screen but not the terminal. */
1.15 nicm 611: if (wp == NULL)
612: return;
613:
614: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
615: return;
1.93 nicm 616: if (!window_pane_visible(wp))
1.15 nicm 617: return;
618:
619: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
620: c = ARRAY_ITEM(&clients, i);
621: if (c == NULL || c->session == NULL)
622: continue;
623: if (c->flags & CLIENT_SUSPENDED)
624: continue;
1.113 nicm 625: s = c->session;
1.15 nicm 626:
1.113 nicm 627: if (s->curw->window == wp->window) {
1.89 nicm 628: if (c->tty.term == NULL)
629: continue;
630: if (c->tty.flags & (TTY_FREEZE|TTY_BACKOFF))
1.15 nicm 631: continue;
1.113 nicm 632: oo = &s->options;
633:
634: ctx->xoff = wp->xoff;
635: ctx->yoff = wp->yoff;
636: if (status_at_line(c) == 0)
637: ctx->yoff++;
638:
1.15 nicm 639: cmdfn(&c->tty, ctx);
640: }
1.1 nicm 641: }
642: }
643:
644: void
1.24 nicm 645: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 646: {
1.77 nicm 647: struct window_pane *wp = ctx->wp;
1.12 nicm 648: struct screen *s = wp->screen;
1.24 nicm 649: u_int i;
1.1 nicm 650:
1.113 nicm 651: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx) {
652: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 653: return;
654: }
655:
656: tty_reset(tty);
657:
1.77 nicm 658: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 659:
1.1 nicm 660: if (tty_term_has(tty->term, TTYC_ICH) ||
661: tty_term_has(tty->term, TTYC_ICH1))
1.12 nicm 662: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.77 nicm 663: else if (tty_term_has(tty->term, TTYC_SMIR) &&
1.72 nicm 664: tty_term_has(tty->term, TTYC_RMIR)) {
1.1 nicm 665: tty_putcode(tty, TTYC_SMIR);
1.24 nicm 666: for (i = 0; i < ctx->num; i++)
1.1 nicm 667: tty_putc(tty, ' ');
668: tty_putcode(tty, TTYC_RMIR);
1.72 nicm 669: } else
1.113 nicm 670: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 671: }
672:
673: void
1.24 nicm 674: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 675: {
1.77 nicm 676: struct window_pane *wp = ctx->wp;
1.12 nicm 677: struct screen *s = wp->screen;
1.1 nicm 678:
1.113 nicm 679: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx ||
1.26 nicm 680: (!tty_term_has(tty->term, TTYC_DCH) &&
681: !tty_term_has(tty->term, TTYC_DCH1))) {
1.113 nicm 682: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.1 nicm 683: return;
684: }
685:
686: tty_reset(tty);
687:
1.77 nicm 688: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.41 nicm 689:
1.26 nicm 690: if (tty_term_has(tty->term, TTYC_DCH) ||
691: tty_term_has(tty->term, TTYC_DCH1))
692: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1 nicm 693: }
694:
695: void
1.24 nicm 696: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 697: {
1.77 nicm 698: struct window_pane *wp = ctx->wp;
1.12 nicm 699: struct screen *s = wp->screen;
1.1 nicm 700:
1.113 nicm 701: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx ||
1.77 nicm 702: !tty_term_has(tty->term, TTYC_CSR) ||
1.72 nicm 703: !tty_term_has(tty->term, TTYC_IL1)) {
1.14 nicm 704: tty_redraw_region(tty, ctx);
1.1 nicm 705: return;
706: }
707:
708: tty_reset(tty);
709:
1.77 nicm 710: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
711: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 712:
1.12 nicm 713: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 714: }
715:
716: void
1.24 nicm 717: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 718: {
1.77 nicm 719: struct window_pane *wp = ctx->wp;
1.12 nicm 720: struct screen *s = wp->screen;
1.1 nicm 721:
1.113 nicm 722: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx ||
1.72 nicm 723: !tty_term_has(tty->term, TTYC_CSR) ||
724: !tty_term_has(tty->term, TTYC_DL1)) {
1.14 nicm 725: tty_redraw_region(tty, ctx);
1.1 nicm 726: return;
727: }
728:
729: tty_reset(tty);
730:
1.77 nicm 731: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
732: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 733:
1.12 nicm 734: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 735: }
736:
737: void
1.24 nicm 738: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 739: {
1.77 nicm 740: struct window_pane *wp = ctx->wp;
1.12 nicm 741: struct screen *s = wp->screen;
742: u_int i;
1.1 nicm 743:
744: tty_reset(tty);
745:
1.77 nicm 746: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.41 nicm 747:
1.113 nicm 748: if (ctx->xoff == 0 && screen_size_x(s) >= tty->sx &&
1.1 nicm 749: tty_term_has(tty->term, TTYC_EL)) {
750: tty_putcode(tty, TTYC_EL);
751: } else {
752: for (i = 0; i < screen_size_x(s); i++)
753: tty_putc(tty, ' ');
754: }
755: }
756:
757: void
1.24 nicm 758: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 759: {
1.77 nicm 760: struct window_pane *wp = ctx->wp;
1.12 nicm 761: struct screen *s = wp->screen;
762: u_int i;
1.1 nicm 763:
764: tty_reset(tty);
765:
1.41 nicm 766: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
767:
1.113 nicm 768: if (ctx->xoff == 0 && screen_size_x(s) >= tty->sx &&
1.1 nicm 769: tty_term_has(tty->term, TTYC_EL))
770: tty_putcode(tty, TTYC_EL);
771: else {
1.14 nicm 772: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 773: tty_putc(tty, ' ');
774: }
775: }
776:
777: void
1.24 nicm 778: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 779: {
1.113 nicm 780: u_int i;
1.1 nicm 781:
782: tty_reset(tty);
783:
1.113 nicm 784: if (ctx->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41 nicm 785: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 786: tty_putcode(tty, TTYC_EL1);
787: } else {
1.41 nicm 788: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.14 nicm 789: for (i = 0; i < ctx->ocx + 1; i++)
1.1 nicm 790: tty_putc(tty, ' ');
791: }
792: }
793:
794: void
1.24 nicm 795: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 796: {
1.77 nicm 797: struct window_pane *wp = ctx->wp;
1.12 nicm 798: struct screen *s = wp->screen;
1.1 nicm 799:
1.56 nicm 800: if (ctx->ocy != ctx->orupper)
801: return;
802:
1.113 nicm 803: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx ||
1.70 nicm 804: !tty_term_has(tty->term, TTYC_CSR) ||
805: !tty_term_has(tty->term, TTYC_RI)) {
1.14 nicm 806: tty_redraw_region(tty, ctx);
1.1 nicm 807: return;
808: }
809:
1.56 nicm 810: tty_reset(tty);
1.77 nicm 811:
1.56 nicm 812: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
813: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.77 nicm 814:
1.56 nicm 815: tty_putcode(tty, TTYC_RI);
1.1 nicm 816: }
817:
818: void
1.24 nicm 819: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 820: {
1.77 nicm 821: struct window_pane *wp = ctx->wp;
1.12 nicm 822: struct screen *s = wp->screen;
1.1 nicm 823:
1.56 nicm 824: if (ctx->ocy != ctx->orlower)
825: return;
826:
1.113 nicm 827: if (ctx->xoff != 0 || screen_size_x(s) < tty->sx ||
1.1 nicm 828: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 829: tty_redraw_region(tty, ctx);
1.1 nicm 830: return;
831: }
1.52 nicm 832:
833: /*
834: * If this line wrapped naturally (ctx->num is nonzero), don't do
835: * anything - the cursor can just be moved to the last cell and wrap
836: * naturally.
837: */
838: if (ctx->num && !(tty->term->flags & TERM_EARLYWRAP))
839: return;
1.1 nicm 840:
1.56 nicm 841: tty_reset(tty);
1.77 nicm 842:
1.56 nicm 843: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
844: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.77 nicm 845:
1.56 nicm 846: tty_putc(tty, '\n');
1.1 nicm 847: }
848:
849: void
1.24 nicm 850: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 851: {
1.77 nicm 852: struct window_pane *wp = ctx->wp;
1.12 nicm 853: struct screen *s = wp->screen;
854: u_int i, j;
1.1 nicm 855:
856: tty_reset(tty);
857:
1.39 nicm 858: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 859: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 860:
1.113 nicm 861: if (ctx->xoff == 0 && screen_size_x(s) >= tty->sx &&
1.1 nicm 862: tty_term_has(tty->term, TTYC_EL)) {
863: tty_putcode(tty, TTYC_EL);
1.14 nicm 864: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 865: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 866: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 867: tty_putcode(tty, TTYC_EL);
868: if (i == screen_size_y(s) - 1)
869: continue;
870: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
871: tty->cy++;
872: }
873: }
874: } else {
1.14 nicm 875: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 876: tty_putc(tty, ' ');
1.68 nicm 877: for (j = ctx->ocy + 1; j < screen_size_y(s); j++) {
1.41 nicm 878: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 879: for (i = 0; i < screen_size_x(s); i++)
880: tty_putc(tty, ' ');
881: }
882: }
883: }
884:
885: void
1.24 nicm 886: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 887: {
1.77 nicm 888: struct window_pane *wp = ctx->wp;
1.12 nicm 889: struct screen *s = wp->screen;
890: u_int i, j;
1.1 nicm 891:
892: tty_reset(tty);
893:
1.39 nicm 894: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 895: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 896:
1.113 nicm 897: if (ctx->xoff == 0 && screen_size_x(s) >= tty->sx &&
1.1 nicm 898: tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 899: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 900: tty_putcode(tty, TTYC_EL);
901: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
902: tty->cy++;
903: }
904: } else {
1.14 nicm 905: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 906: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 907: for (i = 0; i < screen_size_x(s); i++)
908: tty_putc(tty, ' ');
909: }
910: }
1.14 nicm 911: for (i = 0; i <= ctx->ocx; i++)
1.1 nicm 912: tty_putc(tty, ' ');
913: }
914:
915: void
1.24 nicm 916: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 917: {
1.77 nicm 918: struct window_pane *wp = ctx->wp;
1.12 nicm 919: struct screen *s = wp->screen;
920: u_int i, j;
1.1 nicm 921:
922: tty_reset(tty);
923:
1.39 nicm 924: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 925: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 926:
1.113 nicm 927: if (ctx->xoff == 0 && screen_size_x(s) >= tty->sx &&
1.1 nicm 928: tty_term_has(tty->term, TTYC_EL)) {
929: for (i = 0; i < screen_size_y(s); i++) {
930: tty_putcode(tty, TTYC_EL);
931: if (i != screen_size_y(s) - 1) {
932: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
933: tty->cy++;
934: }
935: }
936: } else {
937: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 938: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 939: for (i = 0; i < screen_size_x(s); i++)
940: tty_putc(tty, ' ');
941: }
1.4 nicm 942: }
943: }
944:
945: void
1.24 nicm 946: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 947: {
1.12 nicm 948: struct window_pane *wp = ctx->wp;
949: struct screen *s = wp->screen;
950: u_int i, j;
1.4 nicm 951:
952: tty_reset(tty);
953:
1.39 nicm 954: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 955:
956: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 957: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 958: for (i = 0; i < screen_size_x(s); i++)
959: tty_putc(tty, 'E');
1.1 nicm 960: }
961: }
962:
963: void
1.24 nicm 964: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 965: {
1.46 nicm 966: struct window_pane *wp = ctx->wp;
967: struct screen *s = wp->screen;
1.58 nicm 968: u_int cx;
1.102 nicm 969: u_int width;
970: const struct grid_cell *gc = ctx->cell;
971: const struct grid_utf8 *gu = ctx->utf8;
972:
973: if (gc->flags & GRID_FLAG_UTF8)
974: width = gu->width;
975: else
976: width = 1;
1.46 nicm 977:
978: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
979:
1.57 nicm 980: /* Is the cursor in the very last position? */
1.102 nicm 981: if (ctx->ocx > wp->sx - width) {
1.113 nicm 982: if (ctx->xoff != 0 || wp->sx != tty->sx) {
1.57 nicm 983: /*
984: * The pane doesn't fill the entire line, the linefeed
985: * will already have happened, so just move the cursor.
986: */
987: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
988: } else if (tty->cx < tty->sx) {
989: /*
990: * The cursor isn't in the last position already, so
1.102 nicm 991: * move as far left as possible and redraw the last
1.57 nicm 992: * cell to move into the last position.
993: */
1.111 nicm 994: if (ctx->last_cell.flags & GRID_FLAG_UTF8)
995: cx = screen_size_x(s) - ctx->last_utf8.width;
996: else
997: cx = screen_size_x(s) - 1;
1.50 nicm 998: tty_cursor_pane(tty, ctx, cx, ctx->ocy);
999: tty_cell(tty, &ctx->last_cell, &ctx->last_utf8);
1000: }
1001: } else
1.46 nicm 1002: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 1003:
1.12 nicm 1004: tty_cell(tty, ctx->cell, ctx->utf8);
1.1 nicm 1005: }
1006:
1007: void
1.24 nicm 1008: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 1009: {
1.53 nicm 1010: struct window_pane *wp = ctx->wp;
1.1 nicm 1011:
1.53 nicm 1012: /*
1013: * Cannot rely on not being a partial character, so just redraw the
1014: * whole line.
1015: */
1.113 nicm 1016: tty_draw_line(tty, wp->screen, ctx->ocy, ctx->xoff, ctx->yoff);
1.106 nicm 1017: }
1018:
1019: void
1020: tty_cmd_setselection(struct tty *tty, const struct tty_ctx *ctx)
1021: {
1022: char *buf;
1023: size_t off;
1024:
1025: if (!tty_term_has(tty->term, TTYC_MS))
1026: return;
1027:
1028: off = 4 * ((ctx->num + 2) / 3) + 1; /* storage for base64 */
1029: buf = xmalloc(off);
1030:
1031: b64_ntop(ctx->ptr, ctx->num, buf, off);
1032: tty_putcode_ptr2(tty, TTYC_MS, "", buf);
1033:
1034: xfree(buf);
1.100 nicm 1035: }
1036:
1037: void
1038: tty_cmd_rawstring(struct tty *tty, const struct tty_ctx *ctx)
1039: {
1040: u_int i;
1041: u_char *str = ctx->ptr;
1042:
1043: for (i = 0; i < ctx->num; i++)
1044: tty_putc(tty, str[i]);
1.1 nicm 1045: }
1046:
1047: void
1048: tty_cell(
1049: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
1050: {
1051: u_int i;
1052:
1053: /* Skip last character if terminal is stupid. */
1054: if (tty->term->flags & TERM_EARLYWRAP &&
1055: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
1056: return;
1057:
1058: /* If this is a padding character, do nothing. */
1059: if (gc->flags & GRID_FLAG_PADDING)
1060: return;
1061:
1062: /* Set the attributes. */
1063: tty_attributes(tty, gc);
1064:
1065: /* If not UTF-8, write directly. */
1066: if (!(gc->flags & GRID_FLAG_UTF8)) {
1067: if (gc->data < 0x20 || gc->data == 0x7f)
1068: return;
1069: tty_putc(tty, gc->data);
1070: return;
1071: }
1072:
1073: /* If the terminal doesn't support UTF-8, write underscores. */
1074: if (!(tty->flags & TTY_UTF8)) {
1075: for (i = 0; i < gu->width; i++)
1076: tty_putc(tty, '_');
1077: return;
1078: }
1079:
1080: /* Otherwise, write UTF-8. */
1.5 nicm 1081: tty_pututf8(tty, gu);
1.1 nicm 1082: }
1083:
1084: void
1085: tty_reset(struct tty *tty)
1086: {
1087: struct grid_cell *gc = &tty->cell;
1088:
1089: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
1090: return;
1091:
1.90 nicm 1092: if ((gc->attr & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1093: tty_putcode(tty, TTYC_RMACS);
1094: tty_putcode(tty, TTYC_SGR0);
1095: memcpy(gc, &grid_default_cell, sizeof *gc);
1096: }
1097:
1.40 nicm 1098: /* Set region inside pane. */
1.1 nicm 1099: void
1.39 nicm 1100: tty_region_pane(
1101: struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1 nicm 1102: {
1.113 nicm 1103: tty_region(tty, ctx->yoff + rupper, ctx->yoff + rlower);
1.39 nicm 1104: }
1105:
1.40 nicm 1106: /* Set region at absolute position. */
1.39 nicm 1107: void
1.40 nicm 1108: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 1109: {
1110: if (tty->rlower == rlower && tty->rupper == rupper)
1111: return;
1.1 nicm 1112: if (!tty_term_has(tty->term, TTYC_CSR))
1113: return;
1.39 nicm 1114:
1115: tty->rupper = rupper;
1116: tty->rlower = rlower;
1.55 nicm 1117:
1118: /*
1119: * Some terminals (such as PuTTY) do not correctly reset the cursor to
1120: * 0,0 if it is beyond the last column (they do not reset their wrap
1121: * flag so further output causes a line feed). As a workaround, do an
1122: * explicit move to 0 first.
1123: */
1124: if (tty->cx >= tty->sx)
1125: tty_cursor(tty, 0, tty->cy);
1.42 nicm 1126:
1.39 nicm 1127: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.78 nicm 1128: tty_cursor(tty, 0, 0);
1.1 nicm 1129: }
1130:
1.42 nicm 1131: /* Move cursor inside pane. */
1.1 nicm 1132: void
1.41 nicm 1133: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
1134: {
1.113 nicm 1135: tty_cursor(tty, ctx->xoff + cx, ctx->yoff + cy);
1.41 nicm 1136: }
1137:
1.42 nicm 1138: /* Move cursor to absolute position. */
1.41 nicm 1139: void
1140: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 1141: {
1.42 nicm 1142: struct tty_term *term = tty->term;
1143: u_int thisx, thisy;
1144: int change;
1.77 nicm 1145:
1.42 nicm 1146: if (cx > tty->sx - 1)
1147: cx = tty->sx - 1;
1148:
1149: thisx = tty->cx;
1150: thisy = tty->cy;
1151:
1152: /* No change. */
1153: if (cx == thisx && cy == thisy)
1154: return;
1155:
1.43 nicm 1156: /* Very end of the line, just use absolute movement. */
1157: if (thisx > tty->sx - 1)
1158: goto absolute;
1159:
1.42 nicm 1160: /* Move to home position (0, 0). */
1161: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
1162: tty_putcode(tty, TTYC_HOME);
1163: goto out;
1164: }
1165:
1166: /* Zero on the next line. */
1.48 nicm 1167: if (cx == 0 && cy == thisy + 1 && thisy != tty->rlower) {
1.1 nicm 1168: tty_putc(tty, '\r');
1.42 nicm 1169: tty_putc(tty, '\n');
1170: goto out;
1171: }
1172:
1.45 nicm 1173: /* Moving column or row. */
1.42 nicm 1174: if (cy == thisy) {
1.45 nicm 1175: /*
1176: * Moving column only, row staying the same.
1177: */
1178:
1.42 nicm 1179: /* To left edge. */
1180: if (cx == 0) {
1181: tty_putc(tty, '\r');
1182: goto out;
1183: }
1184:
1185: /* One to the left. */
1186: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
1187: tty_putcode(tty, TTYC_CUB1);
1188: goto out;
1189: }
1190:
1191: /* One to the right. */
1192: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
1193: tty_putcode(tty, TTYC_CUF1);
1194: goto out;
1195: }
1196:
1197: /* Calculate difference. */
1198: change = thisx - cx; /* +ve left, -ve right */
1199:
1200: /*
1201: * Use HPA if change is larger than absolute, otherwise move
1202: * the cursor with CUB/CUF.
1203: */
1.87 nicm 1204: if ((u_int) abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
1.42 nicm 1205: tty_putcode1(tty, TTYC_HPA, cx);
1206: goto out;
1207: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1208: tty_putcode1(tty, TTYC_CUB, change);
1209: goto out;
1210: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1211: tty_putcode1(tty, TTYC_CUF, -change);
1212: goto out;
1213: }
1.45 nicm 1214: } else if (cx == thisx) {
1215: /*
1216: * Moving row only, column staying the same.
1217: */
1.42 nicm 1218:
1219: /* One above. */
1.77 nicm 1220: if (thisy != tty->rupper &&
1.42 nicm 1221: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1222: tty_putcode(tty, TTYC_CUU1);
1223: goto out;
1224: }
1225:
1226: /* One below. */
1.49 nicm 1227: if (thisy != tty->rlower &&
1.42 nicm 1228: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1229: tty_putcode(tty, TTYC_CUD1);
1230: goto out;
1231: }
1232:
1233: /* Calculate difference. */
1234: change = thisy - cy; /* +ve up, -ve down */
1235:
1236: /*
1.77 nicm 1237: * Try to use VPA if change is larger than absolute or if this
1238: * change would cross the scroll region, otherwise use CUU/CUD.
1.42 nicm 1239: */
1.87 nicm 1240: if ((u_int) abs(change) > cy ||
1.42 nicm 1241: (change < 0 && cy - change > tty->rlower) ||
1.44 nicm 1242: (change > 0 && cy - change < tty->rupper)) {
1243: if (tty_term_has(term, TTYC_VPA)) {
1244: tty_putcode1(tty, TTYC_VPA, cy);
1245: goto out;
1246: }
1.42 nicm 1247: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1248: tty_putcode1(tty, TTYC_CUU, change);
1249: goto out;
1250: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1251: tty_putcode1(tty, TTYC_CUD, -change);
1252: goto out;
1253: }
1254: }
1255:
1.43 nicm 1256: absolute:
1.42 nicm 1257: /* Absolute movement. */
1258: tty_putcode2(tty, TTYC_CUP, cy, cx);
1259:
1260: out:
1261: tty->cx = cx;
1262: tty->cy = cy;
1.1 nicm 1263: }
1264:
1265: void
1266: tty_attributes(struct tty *tty, const struct grid_cell *gc)
1267: {
1.63 nicm 1268: struct grid_cell *tc = &tty->cell, gc2;
1.85 nicm 1269: u_char changed;
1.61 nicm 1270:
1.85 nicm 1271: memcpy(&gc2, gc, sizeof gc2);
1.63 nicm 1272:
1.18 nicm 1273: /*
1274: * If no setab, try to use the reverse attribute as a best-effort for a
1275: * non-default background. This is a bit of a hack but it doesn't do
1276: * any serious harm and makes a couple of applications happier.
1277: */
1278: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1.85 nicm 1279: if (gc2.attr & GRID_ATTR_REVERSE) {
1280: if (gc2.fg != 7 && gc2.fg != 8)
1.64 nicm 1281: gc2.attr &= ~GRID_ATTR_REVERSE;
1.18 nicm 1282: } else {
1.85 nicm 1283: if (gc2.bg != 0 && gc2.bg != 8)
1.64 nicm 1284: gc2.attr |= GRID_ATTR_REVERSE;
1.18 nicm 1285: }
1286: }
1.1 nicm 1287:
1.85 nicm 1288: /* Fix up the colours if necessary. */
1289: tty_check_fg(tty, &gc2);
1290: tty_check_bg(tty, &gc2);
1291:
1.64 nicm 1292: /* If any bits are being cleared, reset everything. */
1.85 nicm 1293: if (tc->attr & ~gc2.attr)
1.64 nicm 1294: tty_reset(tty);
1295:
1296: /*
1297: * Set the colours. This may call tty_reset() (so it comes next) and
1298: * may add to (NOT remove) the desired attributes by changing new_attr.
1299: */
1.85 nicm 1300: tty_colours(tty, &gc2);
1.64 nicm 1301:
1.1 nicm 1302: /* Filter out attribute bits already set. */
1.85 nicm 1303: changed = gc2.attr & ~tc->attr;
1304: tc->attr = gc2.attr;
1.1 nicm 1305:
1306: /* Set the attributes. */
1307: if (changed & GRID_ATTR_BRIGHT)
1308: tty_putcode(tty, TTYC_BOLD);
1309: if (changed & GRID_ATTR_DIM)
1310: tty_putcode(tty, TTYC_DIM);
1311: if (changed & GRID_ATTR_ITALICS)
1.104 nicm 1312: {
1313: if (tty_term_has(tty->term, TTYC_SITM))
1314: tty_putcode(tty, TTYC_SITM);
1315: else
1316: tty_putcode(tty, TTYC_SMSO);
1317: }
1.1 nicm 1318: if (changed & GRID_ATTR_UNDERSCORE)
1319: tty_putcode(tty, TTYC_SMUL);
1320: if (changed & GRID_ATTR_BLINK)
1321: tty_putcode(tty, TTYC_BLINK);
1322: if (changed & GRID_ATTR_REVERSE) {
1323: if (tty_term_has(tty->term, TTYC_REV))
1324: tty_putcode(tty, TTYC_REV);
1325: else if (tty_term_has(tty->term, TTYC_SMSO))
1326: tty_putcode(tty, TTYC_SMSO);
1327: }
1328: if (changed & GRID_ATTR_HIDDEN)
1329: tty_putcode(tty, TTYC_INVIS);
1.90 nicm 1330: if ((changed & GRID_ATTR_CHARSET) && tty_use_acs(tty))
1.1 nicm 1331: tty_putcode(tty, TTYC_SMACS);
1332: }
1333:
1.61 nicm 1334: void
1.85 nicm 1335: tty_colours(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1336: {
1.61 nicm 1337: struct grid_cell *tc = &tty->cell;
1.62 nicm 1338: u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
1339: int have_ax, fg_default, bg_default;
1.61 nicm 1340:
1341: /* No changes? Nothing is necessary. */
1.62 nicm 1342: if (fg == tc->fg && bg == tc->bg &&
1343: ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
1.63 nicm 1344: return;
1.1 nicm 1345:
1.61 nicm 1346: /*
1347: * Is either the default colour? This is handled specially because the
1348: * best solution might be to reset both colours to default, in which
1349: * case if only one is default need to fall onward to set the other
1350: * colour.
1351: */
1.62 nicm 1352: fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
1353: bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
1.61 nicm 1354: if (fg_default || bg_default) {
1355: /*
1356: * If don't have AX but do have op, send sgr0 (op can't
1357: * actually be used because it is sometimes the same as sgr0
1358: * and sometimes isn't). This resets both colours to default.
1359: *
1360: * Otherwise, try to set the default colour only as needed.
1361: */
1362: have_ax = tty_term_has(tty->term, TTYC_AX);
1363: if (!have_ax && tty_term_has(tty->term, TTYC_OP))
1364: tty_reset(tty);
1365: else {
1366: if (fg_default &&
1.81 nicm 1367: (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
1.61 nicm 1368: if (have_ax)
1369: tty_puts(tty, "\033[39m");
1.81 nicm 1370: else if (tc->fg != 7 ||
1371: tc->flags & GRID_FLAG_FG256)
1.61 nicm 1372: tty_putcode1(tty, TTYC_SETAF, 7);
1373: tc->fg = 8;
1374: tc->flags &= ~GRID_FLAG_FG256;
1375: }
1376: if (bg_default &&
1.81 nicm 1377: (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
1.77 nicm 1378: if (have_ax)
1.61 nicm 1379: tty_puts(tty, "\033[49m");
1.81 nicm 1380: else if (tc->bg != 0 ||
1381: tc->flags & GRID_FLAG_BG256)
1.61 nicm 1382: tty_putcode1(tty, TTYC_SETAB, 0);
1383: tc->bg = 8;
1384: tc->flags &= ~GRID_FLAG_BG256;
1385: }
1386: }
1387: }
1.1 nicm 1388:
1.61 nicm 1389: /* Set the foreground colour. */
1390: if (!fg_default && (fg != tc->fg ||
1.62 nicm 1391: ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
1.85 nicm 1392: tty_colours_fg(tty, gc);
1.1 nicm 1393:
1.61 nicm 1394: /*
1395: * Set the background colour. This must come after the foreground as
1396: * tty_colour_fg() can call tty_reset().
1397: */
1398: if (!bg_default && (bg != tc->bg ||
1.62 nicm 1399: ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
1.73 nicm 1400: tty_colours_bg(tty, gc);
1.1 nicm 1401: }
1402:
1403: void
1.85 nicm 1404: tty_check_fg(struct tty *tty, struct grid_cell *gc)
1405: {
1406: u_int colours;
1407:
1408: /* Is this a 256-colour colour? */
1409: if (gc->flags & GRID_FLAG_FG256) {
1410: /* And not a 256 colour mode? */
1411: if (!(tty->term->flags & TERM_88COLOURS) &&
1412: !(tty->term_flags & TERM_88COLOURS) &&
1413: !(tty->term->flags & TERM_256COLOURS) &&
1414: !(tty->term_flags & TERM_256COLOURS)) {
1415: gc->fg = colour_256to16(gc->fg);
1416: if (gc->fg & 8) {
1417: gc->fg &= 7;
1418: gc->attr |= GRID_ATTR_BRIGHT;
1419: } else
1420: gc->attr &= ~GRID_ATTR_BRIGHT;
1421: gc->flags &= ~GRID_FLAG_FG256;
1422: }
1423: return;
1424: }
1425:
1426: /* Is this an aixterm colour? */
1427: colours = tty_term_number(tty->term, TTYC_COLORS);
1428: if (gc->fg >= 90 && gc->fg <= 97 && colours < 16) {
1429: gc->fg -= 90;
1430: gc->attr |= GRID_ATTR_BRIGHT;
1431: }
1432: }
1433:
1434: void
1435: tty_check_bg(struct tty *tty, struct grid_cell *gc)
1436: {
1437: u_int colours;
1438:
1439: /* Is this a 256-colour colour? */
1440: if (gc->flags & GRID_FLAG_BG256) {
1441: /*
1442: * And not a 256 colour mode? Translate to 16-colour
1443: * palette. Bold background doesn't exist portably, so just
1444: * discard the bold bit if set.
1445: */
1446: if (!(tty->term->flags & TERM_88COLOURS) &&
1447: !(tty->term_flags & TERM_88COLOURS) &&
1448: !(tty->term->flags & TERM_256COLOURS) &&
1449: !(tty->term_flags & TERM_256COLOURS)) {
1450: gc->bg = colour_256to16(gc->bg);
1451: if (gc->bg & 8)
1452: gc->bg &= 7;
1453: gc->attr &= ~GRID_ATTR_BRIGHT;
1454: gc->flags &= ~GRID_FLAG_BG256;
1455: }
1456: return;
1457: }
1458:
1459: /* Is this an aixterm colour? */
1460: colours = tty_term_number(tty->term, TTYC_COLORS);
1.112 nicm 1461: if (gc->bg >= 90 && gc->bg <= 97 && colours < 16) {
1.85 nicm 1462: gc->bg -= 90;
1463: gc->attr |= GRID_ATTR_BRIGHT;
1464: }
1465: }
1466:
1467: void
1468: tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1469: {
1.61 nicm 1470: struct grid_cell *tc = &tty->cell;
1471: u_char fg = gc->fg;
1.79 nicm 1472: char s[32];
1.1 nicm 1473:
1.61 nicm 1474: /* Is this a 256-colour colour? */
1.1 nicm 1475: if (gc->flags & GRID_FLAG_FG256) {
1.61 nicm 1476: /* Try as 256 colours or translating to 88. */
1.1 nicm 1477: if (tty_try_256(tty, fg, "38") == 0)
1.61 nicm 1478: goto save_fg;
1.1 nicm 1479: if (tty_try_88(tty, fg, "38") == 0)
1.61 nicm 1480: goto save_fg;
1.85 nicm 1481: /* Else already handled by tty_check_fg. */
1482: return;
1.1 nicm 1483: }
1484:
1.79 nicm 1485: /* Is this an aixterm bright colour? */
1486: if (fg >= 90 && fg <= 97) {
1.85 nicm 1487: xsnprintf(s, sizeof s, "\033[%dm", fg);
1488: tty_puts(tty, s);
1489: goto save_fg;
1.79 nicm 1490: }
1491:
1.61 nicm 1492: /* Otherwise set the foreground colour. */
1493: tty_putcode1(tty, TTYC_SETAF, fg);
1494:
1.77 nicm 1495: save_fg:
1.61 nicm 1496: /* Save the new values in the terminal current cell. */
1497: tc->fg = fg;
1498: tc->flags &= ~GRID_FLAG_FG256;
1499: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1500: }
1501:
1502: void
1.73 nicm 1503: tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
1.1 nicm 1504: {
1.61 nicm 1505: struct grid_cell *tc = &tty->cell;
1506: u_char bg = gc->bg;
1.79 nicm 1507: char s[32];
1.1 nicm 1508:
1.61 nicm 1509: /* Is this a 256-colour colour? */
1.1 nicm 1510: if (gc->flags & GRID_FLAG_BG256) {
1.61 nicm 1511: /* Try as 256 colours or translating to 88. */
1.1 nicm 1512: if (tty_try_256(tty, bg, "48") == 0)
1.61 nicm 1513: goto save_bg;
1.1 nicm 1514: if (tty_try_88(tty, bg, "48") == 0)
1.61 nicm 1515: goto save_bg;
1.85 nicm 1516: /* Else already handled by tty_check_bg. */
1517: return;
1.79 nicm 1518: }
1519:
1520: /* Is this an aixterm bright colour? */
1.112 nicm 1521: if (bg >= 90 && bg <= 97) {
1.79 nicm 1522: /* 16 colour terminals or above only. */
1523: if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
1.112 nicm 1524: xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
1.79 nicm 1525: tty_puts(tty, s);
1526: goto save_bg;
1527: }
1.112 nicm 1528: bg -= 90;
1.79 nicm 1529: /* no such thing as a bold background */
1.1 nicm 1530: }
1531:
1.61 nicm 1532: /* Otherwise set the background colour. */
1533: tty_putcode1(tty, TTYC_SETAB, bg);
1534:
1535: save_bg:
1536: /* Save the new values in the terminal current cell. */
1537: tc->bg = bg;
1538: tc->flags &= ~GRID_FLAG_BG256;
1539: tc->flags |= gc->flags & GRID_FLAG_BG256;
1540: }
1541:
1542: int
1543: tty_try_256(struct tty *tty, u_char colour, const char *type)
1544: {
1545: char s[32];
1546:
1547: if (!(tty->term->flags & TERM_256COLOURS) &&
1548: !(tty->term_flags & TERM_256COLOURS))
1549: return (-1);
1550:
1551: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1552: tty_puts(tty, s);
1553: return (0);
1554: }
1555:
1556: int
1557: tty_try_88(struct tty *tty, u_char colour, const char *type)
1558: {
1559: char s[32];
1560:
1561: if (!(tty->term->flags & TERM_88COLOURS) &&
1562: !(tty->term_flags & TERM_88COLOURS))
1563: return (-1);
1564: colour = colour_256to88(colour);
1565:
1566: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1567: tty_puts(tty, s);
1568: return (0);
1.110 nicm 1569: }
1570:
1571: void
1572: tty_bell(struct tty *tty)
1573: {
1574: tty_putcode(tty, TTYC_BEL);
1.1 nicm 1575: }