Coverage Report

Created: 2023-06-07 06:04

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