Annotation of src/usr.bin/tmux/tty.c, Revision 1.43
1.43 ! nicm 1: /* $OpenBSD: tty.c,v 1.42 2009/10/12 13:01:18 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:
22: #include <errno.h>
23: #include <fcntl.h>
1.42 nicm 24: #include <stdlib.h>
1.1 nicm 25: #include <string.h>
26: #include <termios.h>
27: #include <unistd.h>
28:
29: #include "tmux.h"
30:
31: void tty_fill_acs(struct tty *);
32:
33: int tty_try_256(struct tty *, u_char, const char *);
34: int tty_try_88(struct tty *, u_char, const char *);
35:
36: void tty_attributes_fg(struct tty *, const struct grid_cell *);
37: void tty_attributes_bg(struct tty *, const struct grid_cell *);
38:
1.24 nicm 39: void tty_redraw_region(struct tty *, const struct tty_ctx *);
1.13 nicm 40: void tty_emulate_repeat(
41: struct tty *, enum tty_code_code, enum tty_code_code, u_int);
1.14 nicm 42: void tty_cell(struct tty *,
43: const struct grid_cell *, const struct grid_utf8 *);
1.1 nicm 44:
45: void
1.30 nicm 46: tty_init(struct tty *tty, int fd, char *term)
1.1 nicm 47: {
1.30 nicm 48: char *path;
49:
50: memset(tty, 0, sizeof *tty);
1.23 nicm 51: tty->log_fd = -1;
1.22 nicm 52:
1.9 nicm 53: if (term == NULL || *term == '\0')
1.1 nicm 54: tty->termname = xstrdup("unknown");
55: else
56: tty->termname = xstrdup(term);
1.30 nicm 57:
58: if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
59: fatal("fcntl failed");
60: tty->fd = fd;
61:
62: if ((path = ttyname(fd)) == NULL)
63: fatalx("ttyname failed");
64: tty->path = xstrdup(path);
65:
1.1 nicm 66: tty->flags = 0;
67: tty->term_flags = 0;
1.31 nicm 68: }
69:
70: void
71: tty_resize(struct tty *tty)
72: {
73: struct winsize ws;
74:
75: if (ioctl(tty->fd, TIOCGWINSZ, &ws) != -1) {
76: tty->sx = ws.ws_col;
77: tty->sy = ws.ws_row;
78: }
79: if (tty->sx == 0)
80: tty->sx = 80;
81: if (tty->sy == 0)
82: tty->sy = 24;
83:
84: tty->cx = UINT_MAX;
85: tty->cy = UINT_MAX;
86:
87: tty->rupper = UINT_MAX;
88: tty->rlower = UINT_MAX;
1.1 nicm 89: }
90:
91: int
1.17 nicm 92: tty_open(struct tty *tty, const char *overrides, char **cause)
1.1 nicm 93: {
1.30 nicm 94: int fd;
1.1 nicm 95:
1.30 nicm 96: if (debug_level > 3) {
97: fd = open("tmux.out", O_WRONLY|O_CREAT|O_TRUNC, 0644);
98: if (fd != -1 && fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
99: fatal("fcntl failed");
100: tty->log_fd = fd;
1.1 nicm 101: }
102:
1.17 nicm 103: tty->term = tty_term_find(tty->termname, tty->fd, overrides, cause);
1.21 nicm 104: if (tty->term == NULL) {
105: tty_close(tty);
106: return (-1);
107: }
108: tty->flags |= TTY_OPENED;
1.1 nicm 109:
110: tty->in = buffer_create(BUFSIZ);
111: tty->out = buffer_create(BUFSIZ);
112:
1.29 nicm 113: tty->flags &= ~(TTY_NOCURSOR|TTY_FREEZE|TTY_ESCAPE);
1.1 nicm 114:
115: tty_start_tty(tty);
116:
117: tty_keys_init(tty);
118:
119: tty_fill_acs(tty);
120:
121: return (0);
122: }
123:
124: void
125: tty_start_tty(struct tty *tty)
126: {
127: struct termios tio;
1.34 nicm 128: int what, mode;
1.33 nicm 129:
130: if (tty->fd == -1)
131: return;
1.1 nicm 132:
1.34 nicm 133: if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
134: fatal("fcntl failed");
135: if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
136: fatal("fcntl failed");
137:
1.1 nicm 138: if (tcgetattr(tty->fd, &tty->tio) != 0)
139: fatal("tcgetattr failed");
140: memcpy(&tio, &tty->tio, sizeof tio);
141: tio.c_iflag &= ~(IXON|IXOFF|ICRNL|INLCR|IGNCR|IMAXBEL|ISTRIP);
142: tio.c_iflag |= IGNBRK;
143: tio.c_oflag &= ~(OPOST|ONLCR|OCRNL|ONLRET);
144: tio.c_lflag &= ~(IEXTEN|ICANON|ECHO|ECHOE|ECHONL|ECHOCTL|
145: ECHOPRT|ECHOKE|ECHOCTL|ISIG);
146: tio.c_cc[VMIN] = 1;
147: tio.c_cc[VTIME] = 0;
148: if (tcsetattr(tty->fd, TCSANOW, &tio) != 0)
149: fatal("tcsetattr failed");
150:
151: what = 0;
152: if (ioctl(tty->fd, TIOCFLUSH, &what) != 0)
153: fatal("ioctl(TIOCFLUSH)");
154:
1.10 nicm 155: tty_putcode(tty, TTYC_SMCUP);
1.1 nicm 156:
1.25 nicm 157: tty_putcode(tty, TTYC_SGR0);
158: memcpy(&tty->cell, &grid_default_cell, sizeof tty->cell);
159:
1.1 nicm 160: tty_putcode(tty, TTYC_SMKX);
161: tty_putcode(tty, TTYC_ENACS);
162: tty_putcode(tty, TTYC_CLEAR);
163:
164: tty_putcode(tty, TTYC_CNORM);
165: if (tty_term_has(tty->term, TTYC_KMOUS))
166: tty_puts(tty, "\033[?1000l");
167:
168: tty->cx = UINT_MAX;
169: tty->cy = UINT_MAX;
170:
171: tty->rlower = UINT_MAX;
172: tty->rupper = UINT_MAX;
173:
174: tty->mode = MODE_CURSOR;
1.20 nicm 175:
176: tty->flags |= TTY_STARTED;
1.1 nicm 177: }
178:
179: void
180: tty_stop_tty(struct tty *tty)
181: {
182: struct winsize ws;
1.34 nicm 183: int mode;
1.1 nicm 184:
1.20 nicm 185: if (!(tty->flags & TTY_STARTED))
186: return;
187: tty->flags &= ~TTY_STARTED;
188:
1.1 nicm 189: /*
190: * Be flexible about error handling and try not kill the server just
191: * because the fd is invalid. Things like ssh -t can easily leave us
192: * with a dead tty.
193: */
1.34 nicm 194: if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
195: return;
196: if (fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK) == -1)
197: return;
1.1 nicm 198: if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1)
199: return;
200: if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1)
201: return;
202:
203: tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1));
204: tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS));
205: tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0));
206: tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX));
207: tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR));
208:
209: tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM));
210: if (tty_term_has(tty->term, TTYC_KMOUS))
211: tty_raw(tty, "\033[?1000l");
1.10 nicm 212:
213: tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP));
1.1 nicm 214: }
215:
216: void
217: tty_fill_acs(struct tty *tty)
218: {
219: const char *ptr;
220:
221: memset(tty->acs, 0, sizeof tty->acs);
222: if (!tty_term_has(tty->term, TTYC_ACSC))
223: return;
224:
225: ptr = tty_term_string(tty->term, TTYC_ACSC);
226: if (strlen(ptr) % 2 != 0)
227: return;
228: for (; *ptr != '\0'; ptr += 2)
229: tty->acs[(u_char) ptr[0]] = ptr[1];
230: }
231:
232: u_char
233: tty_get_acs(struct tty *tty, u_char ch)
234: {
235: if (tty->acs[ch] != '\0')
236: return (tty->acs[ch]);
237: return (ch);
238: }
239:
240: void
1.20 nicm 241: tty_close(struct tty *tty)
1.1 nicm 242: {
243: if (tty->log_fd != -1) {
244: close(tty->log_fd);
245: tty->log_fd = -1;
246: }
247:
1.20 nicm 248: tty_stop_tty(tty);
1.1 nicm 249:
1.21 nicm 250: if (tty->flags & TTY_OPENED) {
251: tty_term_free(tty->term);
252: tty_keys_free(tty);
253:
254: buffer_destroy(tty->in);
255: buffer_destroy(tty->out);
1.1 nicm 256:
1.21 nicm 257: tty->flags &= ~TTY_OPENED;
258: }
1.1 nicm 259:
1.21 nicm 260: if (tty->fd != -1) {
261: close(tty->fd);
262: tty->fd = -1;
263: }
1.1 nicm 264: }
265:
266: void
1.20 nicm 267: tty_free(struct tty *tty)
1.1 nicm 268: {
1.20 nicm 269: tty_close(tty);
1.1 nicm 270:
271: if (tty->path != NULL)
272: xfree(tty->path);
273: if (tty->termname != NULL)
274: xfree(tty->termname);
275: }
276:
277: void
278: tty_raw(struct tty *tty, const char *s)
279: {
280: write(tty->fd, s, strlen(s));
281: }
282:
283: void
284: tty_putcode(struct tty *tty, enum tty_code_code code)
285: {
286: tty_puts(tty, tty_term_string(tty->term, code));
287: }
288:
289: void
290: tty_putcode1(struct tty *tty, enum tty_code_code code, int a)
291: {
292: if (a < 0)
293: return;
294: tty_puts(tty, tty_term_string1(tty->term, code, a));
295: }
296:
297: void
298: tty_putcode2(struct tty *tty, enum tty_code_code code, int a, int b)
299: {
300: if (a < 0 || b < 0)
301: return;
302: tty_puts(tty, tty_term_string2(tty->term, code, a, b));
303: }
304:
305: void
306: tty_puts(struct tty *tty, const char *s)
307: {
308: if (*s == '\0')
309: return;
310: buffer_write(tty->out, s, strlen(s));
311:
312: if (tty->log_fd != -1)
313: write(tty->log_fd, s, strlen(s));
314: }
315:
316: void
317: tty_putc(struct tty *tty, u_char ch)
318: {
319: u_int sx;
320:
321: if (tty->cell.attr & GRID_ATTR_CHARSET)
322: ch = tty_get_acs(tty, ch);
323: buffer_write8(tty->out, ch);
324:
325: if (ch >= 0x20 && ch != 0x7f) {
326: sx = tty->sx;
327: if (tty->term->flags & TERM_EARLYWRAP)
328: sx--;
329:
330: if (tty->cx == sx) {
331: tty->cx = 0;
332: tty->cy++;
333: } else
334: tty->cx++;
335: }
336:
337: if (tty->log_fd != -1)
338: write(tty->log_fd, &ch, 1);
339: }
340:
341: void
1.5 nicm 342: tty_pututf8(struct tty *tty, const struct grid_utf8 *gu)
343: {
344: u_int i, width;
345:
346: for (i = 0; i < UTF8_SIZE; i++) {
347: if (gu->data[i] == 0xff)
348: break;
349: buffer_write8(tty->out, gu->data[i]);
350: if (tty->log_fd != -1)
351: write(tty->log_fd, &gu->data[i], 1);
352: }
353:
354: width = utf8_width(gu->data);
355: tty->cx += width;
356: }
357:
358: void
1.1 nicm 359: tty_set_title(struct tty *tty, const char *title)
360: {
361: if (strstr(tty->termname, "xterm") == NULL &&
362: strstr(tty->termname, "rxvt") == NULL &&
363: strcmp(tty->termname, "screen") != 0)
364: return;
365:
366: tty_puts(tty, "\033]0;");
367: tty_puts(tty, title);
368: tty_putc(tty, '\007');
369: }
370:
371: void
372: tty_update_mode(struct tty *tty, int mode)
373: {
374: int changed;
375:
376: if (tty->flags & TTY_NOCURSOR)
377: mode &= ~MODE_CURSOR;
378:
379: changed = mode ^ tty->mode;
380: if (changed & MODE_CURSOR) {
381: if (mode & MODE_CURSOR)
382: tty_putcode(tty, TTYC_CNORM);
383: else
384: tty_putcode(tty, TTYC_CIVIS);
385: }
386: if (changed & MODE_MOUSE) {
387: if (mode & MODE_MOUSE)
388: tty_puts(tty, "\033[?1000h");
389: else
390: tty_puts(tty, "\033[?1000l");
391: }
392: tty->mode = mode;
393: }
394:
395: void
396: tty_emulate_repeat(
397: struct tty *tty, enum tty_code_code code, enum tty_code_code code1, u_int n)
398: {
399: if (tty_term_has(tty->term, code))
400: tty_putcode1(tty, code, n);
401: else {
402: while (n-- > 0)
403: tty_putcode(tty, code1);
404: }
405: }
406:
407: /*
408: * Redraw scroll region using data from screen (already updated). Used when
409: * CSR not supported, or window is a pane that doesn't take up the full
410: * width of the terminal.
411: */
412: void
1.24 nicm 413: tty_redraw_region(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 414: {
1.14 nicm 415: struct window_pane *wp = ctx->wp;
416: struct screen *s = wp->screen;
417: u_int i;
1.1 nicm 418:
419: /*
420: * If region is >= 50% of the screen, just schedule a window redraw. In
421: * most cases, this is likely to be followed by some more scrolling -
422: * without this, the entire pane ends up being redrawn many times which
423: * can be much more data.
424: */
1.14 nicm 425: if (ctx->orupper - ctx->orlower >= screen_size_y(s) / 2) {
1.1 nicm 426: wp->flags |= PANE_REDRAW;
427: return;
428: }
429:
1.14 nicm 430: if (ctx->ocy < ctx->orupper || ctx->ocy > ctx->orlower) {
431: for (i = ctx->ocy; i < screen_size_y(s); i++)
1.1 nicm 432: tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
433: } else {
1.14 nicm 434: for (i = ctx->orupper; i <= ctx->orlower; i++)
1.1 nicm 435: tty_draw_line(tty, s, i, wp->xoff, wp->yoff);
436: }
437: }
438:
439: void
440: tty_draw_line(struct tty *tty, struct screen *s, u_int py, u_int ox, u_int oy)
441: {
442: const struct grid_cell *gc;
1.16 nicm 443: struct grid_cell tmpgc;
1.1 nicm 444: const struct grid_utf8 *gu;
445: u_int i, sx;
446:
1.35 nicm 447: tty_update_mode(tty, tty->mode & ~MODE_CURSOR);
448:
1.1 nicm 449: sx = screen_size_x(s);
1.19 nicm 450: if (sx > s->grid->linedata[s->grid->hsize + py].cellsize)
451: sx = s->grid->linedata[s->grid->hsize + py].cellsize;
1.1 nicm 452: if (sx > tty->sx)
453: sx = tty->sx;
454:
1.41 nicm 455: tty_cursor(tty, ox, oy + py);
1.1 nicm 456: for (i = 0; i < sx; i++) {
457: gc = grid_view_peek_cell(s->grid, i, py);
458:
459: gu = NULL;
460: if (gc->flags & GRID_FLAG_UTF8)
461: gu = grid_view_peek_utf8(s->grid, i, py);
462:
463: if (screen_check_selection(s, i, py)) {
1.16 nicm 464: memcpy(&tmpgc, &s->sel.cell, sizeof tmpgc);
465: tmpgc.data = gc->data;
1.38 nicm 466: tmpgc.flags = gc->flags &
467: ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
468: tmpgc.flags |= s->sel.cell.flags &
469: (GRID_FLAG_FG256|GRID_FLAG_BG256);
1.16 nicm 470: tty_cell(tty, &tmpgc, gu);
1.1 nicm 471: } else
472: tty_cell(tty, gc, gu);
473: }
474:
1.35 nicm 475: if (sx >= tty->sx) {
476: tty_update_mode(tty, tty->mode);
1.1 nicm 477: return;
1.35 nicm 478: }
1.1 nicm 479: tty_reset(tty);
480:
1.41 nicm 481: tty_cursor(tty, ox + sx, oy + py);
1.1 nicm 482: if (screen_size_x(s) >= tty->sx && tty_term_has(tty->term, TTYC_EL))
483: tty_putcode(tty, TTYC_EL);
484: else {
485: for (i = sx; i < screen_size_x(s); i++)
486: tty_putc(tty, ' ');
1.15 nicm 487: }
1.35 nicm 488: tty_update_mode(tty, tty->mode);
1.15 nicm 489: }
490:
491: void
1.24 nicm 492: tty_write(void (*cmdfn)(
493: struct tty *, const struct tty_ctx *), const struct tty_ctx *ctx)
1.15 nicm 494: {
495: struct window_pane *wp = ctx->wp;
496: struct client *c;
497: u_int i;
498:
499: if (wp == NULL)
500: return;
501:
502: if (wp->window->flags & WINDOW_REDRAW || wp->flags & PANE_REDRAW)
503: return;
504: if (wp->window->flags & WINDOW_HIDDEN || !window_pane_visible(wp))
505: return;
506:
507: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
508: c = ARRAY_ITEM(&clients, i);
509: if (c == NULL || c->session == NULL)
510: continue;
511: if (c->flags & CLIENT_SUSPENDED)
512: continue;
513:
514: if (c->session->curw->window == wp->window) {
515: if (c->tty.flags & TTY_FREEZE || c->tty.term == NULL)
516: continue;
517: cmdfn(&c->tty, ctx);
518: }
1.1 nicm 519: }
520: }
521:
522: void
1.24 nicm 523: tty_cmd_insertcharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 524: {
1.12 nicm 525: struct window_pane *wp = ctx->wp;
526: struct screen *s = wp->screen;
1.24 nicm 527: u_int i;
1.1 nicm 528:
529: if (wp->xoff != 0 || screen_size_x(s) < tty->sx) {
1.14 nicm 530: tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 531: return;
532: }
533:
534: tty_reset(tty);
535:
1.41 nicm 536: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
537:
1.1 nicm 538: if (tty_term_has(tty->term, TTYC_ICH) ||
539: tty_term_has(tty->term, TTYC_ICH1))
1.12 nicm 540: tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, ctx->num);
1.1 nicm 541: else {
542: tty_putcode(tty, TTYC_SMIR);
1.24 nicm 543: for (i = 0; i < ctx->num; i++)
1.1 nicm 544: tty_putc(tty, ' ');
545: tty_putcode(tty, TTYC_RMIR);
546: }
547: }
548:
549: void
1.24 nicm 550: tty_cmd_deletecharacter(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 551: {
1.12 nicm 552: struct window_pane *wp = ctx->wp;
553: struct screen *s = wp->screen;
1.1 nicm 554:
1.26 nicm 555: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
556: (!tty_term_has(tty->term, TTYC_DCH) &&
557: !tty_term_has(tty->term, TTYC_DCH1))) {
1.14 nicm 558: tty_draw_line(tty, wp->screen, ctx->ocy, wp->xoff, wp->yoff);
1.1 nicm 559: return;
560: }
561:
562: tty_reset(tty);
563:
1.41 nicm 564: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
565:
1.26 nicm 566: if (tty_term_has(tty->term, TTYC_DCH) ||
567: tty_term_has(tty->term, TTYC_DCH1))
568: tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, ctx->num);
1.1 nicm 569: }
570:
571: void
1.24 nicm 572: tty_cmd_insertline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 573: {
1.12 nicm 574: struct window_pane *wp = ctx->wp;
575: struct screen *s = wp->screen;
1.1 nicm 576:
577: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
578: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 579: tty_redraw_region(tty, ctx);
1.1 nicm 580: return;
581: }
582:
583: tty_reset(tty);
584:
1.39 nicm 585: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41 nicm 586: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 587:
1.12 nicm 588: tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, ctx->num);
1.1 nicm 589: }
590:
591: void
1.24 nicm 592: tty_cmd_deleteline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 593: {
1.12 nicm 594: struct window_pane *wp = ctx->wp;
595: struct screen *s = wp->screen;
1.1 nicm 596:
597: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
598: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 599: tty_redraw_region(tty, ctx);
1.1 nicm 600: return;
601: }
602:
603: tty_reset(tty);
604:
1.39 nicm 605: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41 nicm 606: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 607:
1.12 nicm 608: tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, ctx->num);
1.1 nicm 609: }
610:
611: void
1.24 nicm 612: tty_cmd_clearline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 613: {
1.12 nicm 614: struct window_pane *wp = ctx->wp;
615: struct screen *s = wp->screen;
616: u_int i;
1.1 nicm 617:
618: tty_reset(tty);
619:
1.41 nicm 620: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
621:
1.1 nicm 622: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
623: tty_term_has(tty->term, TTYC_EL)) {
624: tty_putcode(tty, TTYC_EL);
625: } else {
626: for (i = 0; i < screen_size_x(s); i++)
627: tty_putc(tty, ' ');
628: }
629: }
630:
631: void
1.24 nicm 632: tty_cmd_clearendofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 633: {
1.12 nicm 634: struct window_pane *wp = ctx->wp;
635: struct screen *s = wp->screen;
636: u_int i;
1.1 nicm 637:
638: tty_reset(tty);
639:
1.41 nicm 640: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
641:
1.1 nicm 642: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
643: tty_term_has(tty->term, TTYC_EL))
644: tty_putcode(tty, TTYC_EL);
645: else {
1.14 nicm 646: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 647: tty_putc(tty, ' ');
648: }
649: }
650:
651: void
1.24 nicm 652: tty_cmd_clearstartofline(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 653: {
1.12 nicm 654: struct window_pane *wp = ctx->wp;
655: u_int i;
1.1 nicm 656:
657: tty_reset(tty);
658:
659: if (wp->xoff == 0 && tty_term_has(tty->term, TTYC_EL1)) {
1.41 nicm 660: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 661: tty_putcode(tty, TTYC_EL1);
662: } else {
1.41 nicm 663: tty_cursor_pane(tty, ctx, 0, ctx->ocy);
1.14 nicm 664: for (i = 0; i < ctx->ocx + 1; i++)
1.1 nicm 665: tty_putc(tty, ' ');
666: }
667: }
668:
669: void
1.24 nicm 670: tty_cmd_reverseindex(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 671: {
1.12 nicm 672: struct window_pane *wp = ctx->wp;
673: struct screen *s = wp->screen;
1.1 nicm 674:
675: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
676: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 677: tty_redraw_region(tty, ctx);
1.1 nicm 678: return;
679: }
680:
1.14 nicm 681: if (ctx->ocy == ctx->orupper) {
1.38 nicm 682: tty_reset(tty);
1.39 nicm 683:
684: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41 nicm 685: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->orupper);
1.39 nicm 686:
1.1 nicm 687: tty_putcode(tty, TTYC_RI);
688: }
689: }
690:
691: void
1.24 nicm 692: tty_cmd_linefeed(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 693: {
1.12 nicm 694: struct window_pane *wp = ctx->wp;
695: struct screen *s = wp->screen;
1.1 nicm 696:
697: if (wp->xoff != 0 || screen_size_x(s) < tty->sx ||
698: !tty_term_has(tty->term, TTYC_CSR)) {
1.14 nicm 699: tty_redraw_region(tty, ctx);
1.1 nicm 700: return;
701: }
702:
1.14 nicm 703: if (ctx->ocy == ctx->orlower) {
1.37 nicm 704: tty_reset(tty);
1.39 nicm 705:
706: tty_region_pane(tty, ctx, ctx->orupper, ctx->orlower);
1.41 nicm 707: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 708:
1.1 nicm 709: tty_putc(tty, '\n');
710: }
711: }
712:
713: void
1.24 nicm 714: tty_cmd_clearendofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 715: {
1.12 nicm 716: struct window_pane *wp = ctx->wp;
717: struct screen *s = wp->screen;
718: u_int i, j;
1.1 nicm 719:
720: tty_reset(tty);
721:
1.39 nicm 722: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 723: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.39 nicm 724:
1.1 nicm 725: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
726: tty_term_has(tty->term, TTYC_EL)) {
727: tty_putcode(tty, TTYC_EL);
1.14 nicm 728: if (ctx->ocy != screen_size_y(s) - 1) {
1.41 nicm 729: tty_cursor_pane(tty, ctx, 0, ctx->ocy + 1);
1.14 nicm 730: for (i = ctx->ocy + 1; i < screen_size_y(s); i++) {
1.1 nicm 731: tty_putcode(tty, TTYC_EL);
732: if (i == screen_size_y(s) - 1)
733: continue;
734: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
735: tty->cy++;
736: }
737: }
738: } else {
1.14 nicm 739: for (i = ctx->ocx; i < screen_size_x(s); i++)
1.1 nicm 740: tty_putc(tty, ' ');
1.14 nicm 741: for (j = ctx->ocy; j < screen_size_y(s); j++) {
1.41 nicm 742: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 743: for (i = 0; i < screen_size_x(s); i++)
744: tty_putc(tty, ' ');
745: }
746: }
747: }
748:
749: void
1.24 nicm 750: tty_cmd_clearstartofscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 751: {
1.12 nicm 752: struct window_pane *wp = ctx->wp;
753: struct screen *s = wp->screen;
754: u_int i, j;
1.1 nicm 755:
756: tty_reset(tty);
757:
1.39 nicm 758: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 759: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 760:
1.1 nicm 761: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
762: tty_term_has(tty->term, TTYC_EL)) {
1.14 nicm 763: for (i = 0; i < ctx->ocy; i++) {
1.1 nicm 764: tty_putcode(tty, TTYC_EL);
765: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
766: tty->cy++;
767: }
768: } else {
1.14 nicm 769: for (j = 0; j < ctx->ocy; j++) {
1.41 nicm 770: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 771: for (i = 0; i < screen_size_x(s); i++)
772: tty_putc(tty, ' ');
773: }
774: }
1.14 nicm 775: for (i = 0; i <= ctx->ocx; i++)
1.1 nicm 776: tty_putc(tty, ' ');
777: }
778:
779: void
1.24 nicm 780: tty_cmd_clearscreen(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 781: {
1.12 nicm 782: struct window_pane *wp = ctx->wp;
783: struct screen *s = wp->screen;
784: u_int i, j;
1.1 nicm 785:
786: tty_reset(tty);
787:
1.39 nicm 788: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.41 nicm 789: tty_cursor_pane(tty, ctx, 0, 0);
1.39 nicm 790:
1.1 nicm 791: if (wp->xoff == 0 && screen_size_x(s) >= tty->sx &&
792: tty_term_has(tty->term, TTYC_EL)) {
793: for (i = 0; i < screen_size_y(s); i++) {
794: tty_putcode(tty, TTYC_EL);
795: if (i != screen_size_y(s) - 1) {
796: tty_emulate_repeat(tty, TTYC_CUD, TTYC_CUD1, 1);
797: tty->cy++;
798: }
799: }
800: } else {
801: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 802: tty_cursor_pane(tty, ctx, 0, j);
1.1 nicm 803: for (i = 0; i < screen_size_x(s); i++)
804: tty_putc(tty, ' ');
805: }
1.4 nicm 806: }
807: }
808:
809: void
1.24 nicm 810: tty_cmd_alignmenttest(struct tty *tty, const struct tty_ctx *ctx)
1.4 nicm 811: {
1.12 nicm 812: struct window_pane *wp = ctx->wp;
813: struct screen *s = wp->screen;
814: u_int i, j;
1.4 nicm 815:
816: tty_reset(tty);
817:
1.39 nicm 818: tty_region_pane(tty, ctx, 0, screen_size_y(s) - 1);
1.4 nicm 819:
820: for (j = 0; j < screen_size_y(s); j++) {
1.41 nicm 821: tty_cursor_pane(tty, ctx, 0, j);
1.4 nicm 822: for (i = 0; i < screen_size_x(s); i++)
823: tty_putc(tty, 'E');
1.1 nicm 824: }
825: }
826:
827: void
1.24 nicm 828: tty_cmd_cell(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 829: {
1.41 nicm 830: tty_cursor_pane(tty, ctx, ctx->ocx, ctx->ocy);
1.1 nicm 831:
1.12 nicm 832: tty_cell(tty, ctx->cell, ctx->utf8);
1.1 nicm 833: }
834:
835: void
1.24 nicm 836: tty_cmd_utf8character(struct tty *tty, const struct tty_ctx *ctx)
1.1 nicm 837: {
1.12 nicm 838: u_char *ptr = ctx->ptr;
1.11 nicm 839: size_t i;
1.1 nicm 840:
1.11 nicm 841: for (i = 0; i < UTF8_SIZE; i++) {
1.12 nicm 842: if (ptr[i] == 0xff)
1.11 nicm 843: break;
1.12 nicm 844: tty_putc(tty, ptr[i]);
1.11 nicm 845: }
1.1 nicm 846: }
847:
848: void
849: tty_cell(
850: struct tty *tty, const struct grid_cell *gc, const struct grid_utf8 *gu)
851: {
852: u_int i;
853:
854: /* Skip last character if terminal is stupid. */
855: if (tty->term->flags & TERM_EARLYWRAP &&
856: tty->cy == tty->sy - 1 && tty->cx == tty->sx - 1)
857: return;
858:
859: /* If this is a padding character, do nothing. */
860: if (gc->flags & GRID_FLAG_PADDING)
861: return;
862:
863: /* Set the attributes. */
864: tty_attributes(tty, gc);
865:
866: /* If not UTF-8, write directly. */
867: if (!(gc->flags & GRID_FLAG_UTF8)) {
868: if (gc->data < 0x20 || gc->data == 0x7f)
869: return;
870: tty_putc(tty, gc->data);
871: return;
872: }
873:
874: /* If the terminal doesn't support UTF-8, write underscores. */
875: if (!(tty->flags & TTY_UTF8)) {
876: for (i = 0; i < gu->width; i++)
877: tty_putc(tty, '_');
878: return;
879: }
880:
881: /* Otherwise, write UTF-8. */
1.5 nicm 882: tty_pututf8(tty, gu);
1.1 nicm 883: }
884:
885: void
886: tty_reset(struct tty *tty)
887: {
888: struct grid_cell *gc = &tty->cell;
889:
890: if (memcmp(gc, &grid_default_cell, sizeof *gc) == 0)
891: return;
892:
893: if (tty_term_has(tty->term, TTYC_RMACS) && gc->attr & GRID_ATTR_CHARSET)
894: tty_putcode(tty, TTYC_RMACS);
895: tty_putcode(tty, TTYC_SGR0);
896: memcpy(gc, &grid_default_cell, sizeof *gc);
897: }
898:
1.40 nicm 899: /* Set region inside pane. */
1.1 nicm 900: void
1.39 nicm 901: tty_region_pane(
902: struct tty *tty, const struct tty_ctx *ctx, u_int rupper, u_int rlower)
1.1 nicm 903: {
1.39 nicm 904: struct window_pane *wp = ctx->wp;
905:
1.40 nicm 906: tty_region(tty, wp->yoff + rupper, wp->yoff + rlower);
1.39 nicm 907: }
908:
1.40 nicm 909: /* Set region at absolute position. */
1.39 nicm 910: void
1.40 nicm 911: tty_region(struct tty *tty, u_int rupper, u_int rlower)
1.39 nicm 912: {
913: if (tty->rlower == rlower && tty->rupper == rupper)
914: return;
1.1 nicm 915: if (!tty_term_has(tty->term, TTYC_CSR))
916: return;
1.39 nicm 917:
918: tty->rupper = rupper;
919: tty->rlower = rlower;
1.42 nicm 920:
1.39 nicm 921: tty->cx = 0;
922: tty->cy = 0;
923:
924: tty_putcode2(tty, TTYC_CSR, tty->rupper, tty->rlower);
1.1 nicm 925: }
926:
1.42 nicm 927: /* Move cursor inside pane. */
1.1 nicm 928: void
1.41 nicm 929: tty_cursor_pane(struct tty *tty, const struct tty_ctx *ctx, u_int cx, u_int cy)
930: {
931: struct window_pane *wp = ctx->wp;
932:
933: tty_cursor(tty, wp->xoff + cx, wp->yoff + cy);
934: }
935:
1.42 nicm 936: /* Move cursor to absolute position. */
1.41 nicm 937: void
938: tty_cursor(struct tty *tty, u_int cx, u_int cy)
1.1 nicm 939: {
1.42 nicm 940: struct tty_term *term = tty->term;
941: u_int thisx, thisy;
942: int change;
943:
944: if (cx > tty->sx - 1)
945: cx = tty->sx - 1;
946:
947: thisx = tty->cx;
948: thisy = tty->cy;
949:
950: /* No change. */
951: if (cx == thisx && cy == thisy)
952: return;
953:
1.43 ! nicm 954: /* Very end of the line, just use absolute movement. */
! 955: if (thisx > tty->sx - 1)
! 956: goto absolute;
! 957:
1.42 nicm 958: /* Move to home position (0, 0). */
959: if (cx == 0 && cy == 0 && tty_term_has(term, TTYC_HOME)) {
960: tty_putcode(tty, TTYC_HOME);
961: goto out;
962: }
963:
964: /* Zero on the next line. */
965: if (cx == 0 && cy == thisy + 1) {
1.1 nicm 966: tty_putc(tty, '\r');
1.42 nicm 967: tty_putc(tty, '\n');
968: goto out;
969: }
970:
971: /* Row staying the same. */
972: if (cy == thisy) {
973: /* To left edge. */
974: if (cx == 0) {
975: tty_putc(tty, '\r');
976: goto out;
977: }
978:
979: /* One to the left. */
980: if (cx == thisx - 1 && tty_term_has(term, TTYC_CUB1)) {
981: tty_putcode(tty, TTYC_CUB1);
982: goto out;
983: }
984:
985: /* One to the right. */
986: if (cx == thisx + 1 && tty_term_has(term, TTYC_CUF1)) {
987: tty_putcode(tty, TTYC_CUF1);
988: goto out;
989: }
990:
991: /* Calculate difference. */
992: change = thisx - cx; /* +ve left, -ve right */
993:
994: /*
995: * Use HPA if change is larger than absolute, otherwise move
996: * the cursor with CUB/CUF.
997: */
998: if (abs(change) > cx && tty_term_has(term, TTYC_HPA)) {
999: tty_putcode1(tty, TTYC_HPA, cx);
1000: goto out;
1001: } else if (change > 0 && tty_term_has(term, TTYC_CUB)) {
1002: tty_putcode1(tty, TTYC_CUB, change);
1003: goto out;
1004: } else if (change < 0 && tty_term_has(term, TTYC_CUF)) {
1005: tty_putcode1(tty, TTYC_CUF, -change);
1006: goto out;
1007: }
1.1 nicm 1008: }
1.42 nicm 1009:
1010: /* Column staying the same. */
1011: if (cx == thisx ) {
1012: /* One above. */
1013: if (cy != tty->rupper &&
1014: cy == thisy - 1 && tty_term_has(term, TTYC_CUU1)) {
1015: tty_putcode(tty, TTYC_CUU1);
1016: goto out;
1017: }
1018:
1019: /* One below. */
1020: if (cy != tty->rlower &&
1021: cy == thisy + 1 && tty_term_has(term, TTYC_CUD1)) {
1022: tty_putcode(tty, TTYC_CUD1);
1023: goto out;
1024: }
1025:
1026: /* Calculate difference. */
1027: change = thisy - cy; /* +ve up, -ve down */
1028:
1029: /*
1030: * Use VPA if change is larger than absolute or if this change
1031: * would cross the scroll region, otherwise use CUU/CUD.
1032: */
1033: if ((abs(change) > cy ||
1034: (change < 0 && cy - change > tty->rlower) ||
1035: (change > 0 && cy - change < tty->rupper)) &&
1036: tty_term_has(term, TTYC_VPA)) {
1037: tty_putcode1(tty, TTYC_VPA, cy);
1038: goto out;
1039: } else if (change > 0 && tty_term_has(term, TTYC_CUU)) {
1040: tty_putcode1(tty, TTYC_CUU, change);
1041: goto out;
1042: } else if (change < 0 && tty_term_has(term, TTYC_CUD)) {
1043: tty_putcode1(tty, TTYC_CUD, -change);
1044: goto out;
1045: }
1046: }
1047:
1.43 ! nicm 1048: absolute:
1.42 nicm 1049: /* Absolute movement. */
1050: tty_putcode2(tty, TTYC_CUP, cy, cx);
1051:
1052: out:
1053: tty->cx = cx;
1054: tty->cy = cy;
1.1 nicm 1055: }
1056:
1057: void
1058: tty_attributes(struct tty *tty, const struct grid_cell *gc)
1059: {
1060: struct grid_cell *tc = &tty->cell;
1061: u_char changed;
1.18 nicm 1062: u_int fg, bg, attr;
1063:
1064: /*
1065: * If no setab, try to use the reverse attribute as a best-effort for a
1066: * non-default background. This is a bit of a hack but it doesn't do
1067: * any serious harm and makes a couple of applications happier.
1068: */
1069: fg = gc->fg; bg = gc->bg; attr = gc->attr;
1070: if (!tty_term_has(tty->term, TTYC_SETAB)) {
1071: if (attr & GRID_ATTR_REVERSE) {
1072: if (fg != 7 && fg != 8)
1073: attr &= ~GRID_ATTR_REVERSE;
1074: } else {
1075: if (bg != 0 && bg != 8)
1076: attr |= GRID_ATTR_REVERSE;
1077: }
1078: }
1.1 nicm 1079:
1080: /* If any bits are being cleared, reset everything. */
1.18 nicm 1081: if (tc->attr & ~attr)
1.1 nicm 1082: tty_reset(tty);
1083:
1084: /* Filter out attribute bits already set. */
1.18 nicm 1085: changed = attr & ~tc->attr;
1086: tc->attr = attr;
1.1 nicm 1087:
1088: /* Set the attributes. */
1089: if (changed & GRID_ATTR_BRIGHT)
1090: tty_putcode(tty, TTYC_BOLD);
1091: if (changed & GRID_ATTR_DIM)
1092: tty_putcode(tty, TTYC_DIM);
1093: if (changed & GRID_ATTR_ITALICS)
1094: tty_putcode(tty, TTYC_SMSO);
1095: if (changed & GRID_ATTR_UNDERSCORE)
1096: tty_putcode(tty, TTYC_SMUL);
1097: if (changed & GRID_ATTR_BLINK)
1098: tty_putcode(tty, TTYC_BLINK);
1099: if (changed & GRID_ATTR_REVERSE) {
1100: if (tty_term_has(tty->term, TTYC_REV))
1101: tty_putcode(tty, TTYC_REV);
1102: else if (tty_term_has(tty->term, TTYC_SMSO))
1103: tty_putcode(tty, TTYC_SMSO);
1104: }
1105: if (changed & GRID_ATTR_HIDDEN)
1106: tty_putcode(tty, TTYC_INVIS);
1107: if (changed & GRID_ATTR_CHARSET)
1108: tty_putcode(tty, TTYC_SMACS);
1109:
1110: /* Set foreground colour. */
1111: if (fg != tc->fg ||
1112: (gc->flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) {
1113: tty_attributes_fg(tty, gc);
1114: tc->fg = fg;
1.8 nicm 1115: tc->flags &= ~GRID_FLAG_FG256;
1116: tc->flags |= gc->flags & GRID_FLAG_FG256;
1.1 nicm 1117: }
1118:
1119: /* Set background colour. */
1120: if (bg != tc->bg ||
1121: (gc->flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) {
1122: tty_attributes_bg(tty, gc);
1123: tc->bg = bg;
1.8 nicm 1124: tc->flags &= ~GRID_FLAG_BG256;
1125: tc->flags |= gc->flags & GRID_FLAG_BG256;
1.1 nicm 1126: }
1127: }
1128:
1129: int
1130: tty_try_256(struct tty *tty, u_char colour, const char *type)
1131: {
1132: char s[32];
1133:
1134: if (!(tty->term->flags & TERM_256COLOURS) &&
1135: !(tty->term_flags & TERM_256COLOURS))
1136: return (-1);
1137:
1138: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1139: tty_puts(tty, s);
1140: return (0);
1141: }
1142:
1143: int
1144: tty_try_88(struct tty *tty, u_char colour, const char *type)
1145: {
1146: char s[32];
1147:
1148: if (!(tty->term->flags & TERM_88COLOURS) &&
1149: !(tty->term_flags & TERM_88COLOURS))
1150: return (-1);
1151: colour = colour_256to88(colour);
1152:
1153: xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
1154: tty_puts(tty, s);
1155: return (0);
1156: }
1157:
1158: void
1159: tty_attributes_fg(struct tty *tty, const struct grid_cell *gc)
1160: {
1161: u_char fg;
1162:
1163: fg = gc->fg;
1164: if (gc->flags & GRID_FLAG_FG256) {
1165: if (tty_try_256(tty, fg, "38") == 0)
1166: return;
1167: if (tty_try_88(tty, fg, "38") == 0)
1168: return;
1169: fg = colour_256to16(fg);
1170: if (fg & 8) {
1171: fg &= 7;
1172: tty_putcode(tty, TTYC_BOLD);
1173: tty->cell.attr |= GRID_ATTR_BRIGHT;
1174: } else if (tty->cell.attr & GRID_ATTR_BRIGHT)
1175: tty_reset(tty);
1176: }
1177:
1178: if (fg == 8 &&
1179: !(tty->term->flags & TERM_HASDEFAULTS) &&
1180: !(tty->term_flags & TERM_HASDEFAULTS))
1181: fg = 7;
1182: if (fg == 8)
1183: tty_puts(tty, "\033[39m");
1184: else
1185: tty_putcode1(tty, TTYC_SETAF, fg);
1186: }
1187:
1188: void
1189: tty_attributes_bg(struct tty *tty, const struct grid_cell *gc)
1190: {
1191: u_char bg;
1192:
1193: bg = gc->bg;
1194: if (gc->flags & GRID_FLAG_BG256) {
1195: if (tty_try_256(tty, bg, "48") == 0)
1196: return;
1197: if (tty_try_88(tty, bg, "48") == 0)
1198: return;
1199: bg = colour_256to16(bg);
1200: if (bg & 8)
1201: bg &= 7;
1202: }
1203:
1204: if (bg == 8 &&
1205: !(tty->term->flags & TERM_HASDEFAULTS) &&
1206: !(tty->term_flags & TERM_HASDEFAULTS))
1207: bg = 0;
1208: if (bg == 8)
1209: tty_puts(tty, "\033[49m");
1210: else
1211: tty_putcode1(tty, TTYC_SETAB, bg);
1212: }