Coverage Report

Created: 2024-02-25 06:30

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