Coverage Report

Created: 2026-05-30 06:39

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