Coverage Report

Created: 2026-06-10 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/server-client.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2009 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
#include <sys/uio.h>
22
23
#include <errno.h>
24
#include <fcntl.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <time.h>
28
#include <unistd.h>
29
30
#include "tmux.h"
31
32
static void server_client_free(int, short, void *);
33
static void server_client_check_pane_resize(struct window_pane *);
34
static void server_client_check_pane_buffer(struct window_pane *);
35
static void server_client_check_window_resize(struct window *);
36
static key_code server_client_check_mouse(struct client *, struct key_event *);
37
static void server_client_repeat_timer(int, short, void *);
38
static void server_client_click_timer(int, short, void *);
39
static void server_client_check_exit(struct client *);
40
static void server_client_check_redraw(struct client *);
41
static void server_client_check_modes(struct client *);
42
static void server_client_set_title(struct client *);
43
static void server_client_set_path(struct client *);
44
static void server_client_set_progress_bar(struct client *);
45
static void server_client_reset_state(struct client *);
46
static void server_client_update_latest(struct client *);
47
static void server_client_dispatch(struct imsg *, void *);
48
static int  server_client_dispatch_command(struct client *, struct imsg *);
49
static int  server_client_dispatch_identify(struct client *, struct imsg *);
50
static int  server_client_dispatch_shell(struct client *);
51
static void server_client_report_theme(struct client *, enum client_theme);
52
53
/* Compare client windows. */
54
static int
55
server_client_window_cmp(struct client_window *cw1,
56
    struct client_window *cw2)
57
0
{
58
0
  if (cw1->window < cw2->window)
59
0
    return (-1);
60
0
  if (cw1->window > cw2->window)
61
0
    return (1);
62
0
  return (0);
63
0
}
64
0
RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp);
Unexecuted instantiation: client_windows_RB_REMOVE_COLOR
Unexecuted instantiation: client_windows_RB_REMOVE
Unexecuted instantiation: client_windows_RB_INSERT
Unexecuted instantiation: client_windows_RB_FIND
Unexecuted instantiation: client_windows_RB_NFIND
Unexecuted instantiation: client_windows_RB_MINMAX
65
0
66
0
/* Number of attached clients. */
67
0
u_int
68
0
server_client_how_many(void)
69
0
{
70
0
  struct client *c;
71
0
  u_int    n;
72
73
0
  n = 0;
74
0
  TAILQ_FOREACH(c, &clients, entry) {
75
0
    if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS))
76
0
      n++;
77
0
  }
78
0
  return (n);
79
0
}
80
81
/* Overlay timer callback. */
82
static void
83
server_client_overlay_timer(__unused int fd, __unused short events, void *data)
84
0
{
85
0
  server_client_clear_overlay(data);
86
0
}
87
88
/* Set an overlay on client. */
89
void
90
server_client_set_overlay(struct client *c, u_int delay,
91
    overlay_check_cb checkcb, overlay_mode_cb modecb,
92
    overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb,
93
    overlay_resize_cb resizecb, void *data)
94
0
{
95
0
  struct timeval  tv;
96
97
0
  if (c->overlay_draw != NULL)
98
0
    server_client_clear_overlay(c);
99
100
0
  tv.tv_sec = delay / 1000;
101
0
  tv.tv_usec = (delay % 1000) * 1000L;
102
103
0
  if (event_initialized(&c->overlay_timer))
104
0
    evtimer_del(&c->overlay_timer);
105
0
  evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
106
0
  if (delay != 0)
107
0
    evtimer_add(&c->overlay_timer, &tv);
108
109
0
  c->overlay_check = checkcb;
110
0
  c->overlay_mode = modecb;
111
0
  c->overlay_draw = drawcb;
112
0
  c->overlay_key = keycb;
113
0
  c->overlay_free = freecb;
114
0
  c->overlay_resize = resizecb;
115
0
  c->overlay_data = data;
116
117
0
  if (c->overlay_check == NULL)
118
0
    c->tty.flags |= TTY_FREEZE;
119
0
  if (c->overlay_mode == NULL)
120
0
    c->tty.flags |= TTY_NOCURSOR;
121
0
  window_update_focus(c->session->curw->window);
122
0
  server_redraw_client(c);
123
0
}
124
125
/* Clear overlay mode on client. */
126
void
127
server_client_clear_overlay(struct client *c)
128
0
{
129
0
  if (c->overlay_draw == NULL)
130
0
    return;
131
132
0
  if (event_initialized(&c->overlay_timer))
133
0
    evtimer_del(&c->overlay_timer);
134
135
0
  if (c->overlay_free != NULL)
136
0
    c->overlay_free(c, c->overlay_data);
137
138
0
  c->overlay_check = NULL;
139
0
  c->overlay_mode = NULL;
140
0
  c->overlay_draw = NULL;
141
0
  c->overlay_key = NULL;
142
0
  c->overlay_free = NULL;
143
0
  c->overlay_resize = NULL;
144
0
  c->overlay_data = NULL;
145
146
0
  c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
147
0
  if (c->session != NULL)
148
0
    window_update_focus(c->session->curw->window);
149
0
  server_redraw_client(c);
150
0
}
151
152
/* Are these ranges empty? That is, nothing is visible. */
153
int
154
server_client_ranges_is_empty(struct visible_ranges *r)
155
0
{
156
0
  u_int i;
157
158
0
  for (i = 0; i < r->used; i++) {
159
0
    if (r->ranges[i].nx != 0)
160
0
      return (0);
161
0
  }
162
0
  return (1);
163
0
}
164
165
/* Ensure we have space for at least n ranges. */
166
void
167
server_client_ensure_ranges(struct visible_ranges *r, u_int n)
168
0
{
169
0
  if (r->size >= n)
170
0
    return;
171
0
  r->ranges = xrecallocarray(r->ranges, r->size, n, sizeof *r->ranges);
172
0
  r->size = n;
173
0
}
174
175
/*
176
 * Given overlay position and dimensions, return parts of the input range which
177
 * are visible.
178
 */
179
void
180
server_client_overlay_range(u_int x, u_int y, u_int sx, u_int sy, u_int px,
181
    u_int py, u_int nx, struct visible_ranges *r)
182
0
{
183
0
  u_int ox, onx;
184
185
  /* Trivial case of no overlap in the y direction. */
186
0
  if (py < y || py > y + sy - 1) {
187
0
    server_client_ensure_ranges(r, 1);
188
0
    r->ranges[0].px = px;
189
0
    r->ranges[0].nx = nx;
190
0
    r->used = 1;
191
0
    return;
192
0
  }
193
0
  server_client_ensure_ranges(r, 2);
194
195
  /* Visible bit to the left of the popup. */
196
0
  if (px < x) {
197
0
    r->ranges[0].px = px;
198
0
    r->ranges[0].nx = x - px;
199
0
    if (r->ranges[0].nx > nx)
200
0
      r->ranges[0].nx = nx;
201
0
  } else {
202
0
    r->ranges[0].px = 0;
203
0
    r->ranges[0].nx = 0;
204
0
  }
205
206
  /* Visible bit to the right of the popup. */
207
0
  ox = x + sx;
208
0
  if (px > ox)
209
0
    ox = px;
210
0
  onx = px + nx;
211
0
  if (onx > ox) {
212
0
    r->ranges[1].px = ox;
213
0
    r->ranges[1].nx = onx - ox;
214
0
  } else {
215
0
    r->ranges[1].px = 0;
216
0
    r->ranges[1].nx = 0;
217
0
  }
218
0
  r->used = 2;
219
0
}
220
221
/* Check if this client is inside this server. */
222
int
223
server_client_check_nested(struct client *c)
224
0
{
225
0
  struct environ_entry  *envent;
226
0
  struct window_pane  *wp;
227
228
0
  envent = environ_find(c->environ, "TMUX");
229
0
  if (envent == NULL || *envent->value == '\0')
230
0
    return (0);
231
232
0
  RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
233
0
    if (strcmp(wp->tty, c->ttyname) == 0)
234
0
      return (1);
235
0
  }
236
0
  return (0);
237
0
}
238
239
/* Set client key table. */
240
void
241
server_client_set_key_table(struct client *c, const char *name)
242
0
{
243
0
  if (name == NULL)
244
0
    name = server_client_get_key_table(c);
245
246
0
  key_bindings_unref_table(c->keytable);
247
0
  c->keytable = key_bindings_get_table(name, 1);
248
0
  c->keytable->references++;
249
0
  if (gettimeofday(&c->keytable->activity_time, NULL) != 0)
250
0
    fatal("gettimeofday failed");
251
0
}
252
253
static uint64_t
254
server_client_key_table_activity_diff(struct client *c)
255
0
{
256
0
  struct timeval  diff;
257
258
0
  timersub(&c->activity_time, &c->keytable->activity_time, &diff);
259
0
  return ((diff.tv_sec * 1000ULL) + (diff.tv_usec / 1000ULL));
260
0
}
261
262
/* Get default key table. */
263
const char *
264
server_client_get_key_table(struct client *c)
265
0
{
266
0
  struct session  *s = c->session;
267
0
  const char  *name;
268
269
0
  if (s == NULL)
270
0
    return ("root");
271
272
0
  name = options_get_string(s->options, "key-table");
273
0
  if (*name == '\0')
274
0
    return ("root");
275
0
  return (name);
276
0
}
277
278
/* Is this table the default key table? */
279
static int
280
server_client_is_default_key_table(struct client *c, struct key_table *table)
281
0
{
282
0
  return (strcmp(table->name, server_client_get_key_table(c)) == 0);
283
0
}
284
285
/* Create a new client. */
286
struct client *
287
server_client_create(int fd)
288
0
{
289
0
  struct client *c;
290
291
0
  setblocking(fd, 0);
292
293
0
  c = xcalloc(1, sizeof *c);
294
0
  c->references = 1;
295
0
  c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
296
297
0
  if (gettimeofday(&c->creation_time, NULL) != 0)
298
0
    fatal("gettimeofday failed");
299
0
  memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
300
301
0
  c->environ = environ_create();
302
303
0
  c->fd = -1;
304
0
  c->out_fd = -1;
305
306
0
  c->queue = cmdq_new();
307
0
  RB_INIT(&c->windows);
308
0
  RB_INIT(&c->files);
309
310
0
  c->tty.sx = 80;
311
0
  c->tty.sy = 24;
312
0
  c->theme = THEME_UNKNOWN;
313
314
0
  status_init(c);
315
0
  c->flags |= CLIENT_FOCUSED;
316
317
0
  c->keytable = key_bindings_get_table("root", 1);
318
0
  c->keytable->references++;
319
320
0
  evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
321
0
  evtimer_set(&c->click_timer, server_client_click_timer, c);
322
323
0
  c->click_wp = -1;
324
325
0
  TAILQ_INIT(&c->input_requests);
326
327
0
  TAILQ_INSERT_TAIL(&clients, c, entry);
328
0
  log_debug("new client %p", c);
329
0
  return (c);
330
0
}
331
332
/* Open client terminal if needed. */
333
int
334
server_client_open(struct client *c, char **cause)
335
0
{
336
0
  const char  *ttynam = _PATH_TTY;
337
338
0
  if (c->flags & CLIENT_CONTROL)
339
0
    return (0);
340
341
0
  if (strcmp(c->ttyname, ttynam) == 0||
342
0
      ((isatty(STDIN_FILENO) &&
343
0
      (ttynam = ttyname(STDIN_FILENO)) != NULL &&
344
0
      strcmp(c->ttyname, ttynam) == 0) ||
345
0
      (isatty(STDOUT_FILENO) &&
346
0
      (ttynam = ttyname(STDOUT_FILENO)) != NULL &&
347
0
      strcmp(c->ttyname, ttynam) == 0) ||
348
0
      (isatty(STDERR_FILENO) &&
349
0
      (ttynam = ttyname(STDERR_FILENO)) != NULL &&
350
0
      strcmp(c->ttyname, ttynam) == 0))) {
351
0
    xasprintf(cause, "can't use %s", c->ttyname);
352
0
    return (-1);
353
0
  }
354
355
0
  if (!(c->flags & CLIENT_TERMINAL)) {
356
0
    *cause = xstrdup("not a terminal");
357
0
    return (-1);
358
0
  }
359
360
0
  if (tty_open(&c->tty, cause) != 0)
361
0
    return (-1);
362
363
0
  return (0);
364
0
}
365
366
/* Lost an attached client. */
367
static void
368
server_client_attached_lost(struct client *c)
369
0
{
370
0
  struct session  *s;
371
0
  struct window *w;
372
0
  struct client *loop;
373
0
  struct client *found;
374
375
0
  log_debug("lost attached client %p", c);
376
377
  /*
378
   * By this point the session in the client has been cleared so walk all
379
   * windows to find any with this client as the latest.
380
   */
381
0
  RB_FOREACH(w, windows, &windows) {
382
0
    if (w->latest != c)
383
0
      continue;
384
385
0
    found = NULL;
386
0
    TAILQ_FOREACH(loop, &clients, entry) {
387
0
      s = loop->session;
388
0
      if (loop == c || s == NULL || s->curw->window != w)
389
0
        continue;
390
0
      if (found == NULL || timercmp(&loop->activity_time,
391
0
          &found->activity_time, >))
392
0
        found = loop;
393
0
    }
394
0
    if (found != NULL)
395
0
      server_client_update_latest(found);
396
0
  }
397
0
}
398
399
/* Set client session. */
400
void
401
server_client_set_session(struct client *c, struct session *s)
402
0
{
403
0
  struct session  *old = c->session;
404
405
0
  if (s != NULL && c->session != NULL && c->session != s)
406
0
    c->last_session = c->session;
407
0
  else if (s == NULL)
408
0
    c->last_session = NULL;
409
0
  c->session = s;
410
0
  c->flags |= CLIENT_FOCUSED;
411
412
0
  if (old != NULL && old->curw != NULL)
413
0
    window_update_focus(old->curw->window);
414
0
  if (s != NULL) {
415
0
    s->curw->window->latest = c;
416
0
    recalculate_sizes();
417
0
    window_update_focus(s->curw->window);
418
0
    session_update_activity(s, NULL);
419
0
    session_theme_changed(s);
420
0
    gettimeofday(&s->last_attached_time, NULL);
421
0
    s->curw->flags &= ~WINLINK_ALERTFLAGS;
422
0
    alerts_check_session(s);
423
0
    tty_update_client_offset(c);
424
0
    status_timer_start(c);
425
0
    notify_client("client-session-changed", c);
426
0
    server_redraw_client(c);
427
0
  }
428
429
0
  server_check_unattached();
430
0
  server_update_socket();
431
0
}
432
433
/* Lost a client. */
434
void
435
server_client_lost(struct client *c)
436
0
{
437
0
  struct client_file  *cf, *cf1;
438
0
  struct client_window  *cw, *cw1;
439
440
0
  c->flags |= CLIENT_DEAD;
441
442
0
  server_client_clear_overlay(c);
443
0
  status_prompt_clear(c);
444
0
  status_message_clear(c);
445
446
0
  RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) {
447
0
    cf->error = EINTR;
448
0
    file_fire_done(cf);
449
0
  }
450
0
  RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) {
451
0
    RB_REMOVE(client_windows, &c->windows, cw);
452
0
    free(cw);
453
0
  }
454
455
0
  TAILQ_REMOVE(&clients, c, entry);
456
0
  log_debug("lost client %p", c);
457
458
0
  if (c->flags & CLIENT_ATTACHED) {
459
0
    server_client_attached_lost(c);
460
0
    notify_client("client-detached", c);
461
0
  }
462
463
0
  if (c->flags & CLIENT_CONTROL)
464
0
    control_stop(c);
465
0
  if (c->flags & CLIENT_TERMINAL)
466
0
    tty_free(&c->tty);
467
0
  free(c->ttyname);
468
0
  free(c->clipboard_panes);
469
470
0
  free(c->term_name);
471
0
  free(c->term_type);
472
0
  tty_term_free_list(c->term_caps, c->term_ncaps);
473
474
0
  status_free(c);
475
0
  input_cancel_requests(c);
476
477
0
  free(c->title);
478
0
  free((void *)c->cwd);
479
480
0
  evtimer_del(&c->repeat_timer);
481
0
  evtimer_del(&c->click_timer);
482
483
0
  key_bindings_unref_table(c->keytable);
484
485
0
  free(c->message_string);
486
0
  if (event_initialized(&c->message_timer))
487
0
    evtimer_del(&c->message_timer);
488
489
0
  free(c->prompt_saved);
490
0
  free(c->prompt_string);
491
0
  free(c->prompt_buffer);
492
493
0
  format_lost_client(c);
494
0
  environ_free(c->environ);
495
496
0
  proc_remove_peer(c->peer);
497
0
  c->peer = NULL;
498
499
0
  if (c->out_fd != -1)
500
0
    close(c->out_fd);
501
0
  if (c->fd != -1) {
502
0
    close(c->fd);
503
0
    c->fd = -1;
504
0
  }
505
0
  server_client_unref(c);
506
507
0
  server_add_accept(0); /* may be more file descriptors now */
508
509
0
  recalculate_sizes();
510
0
  server_check_unattached();
511
0
  server_update_socket();
512
0
}
513
514
/* Remove reference from a client. */
515
void
516
server_client_unref(struct client *c)
517
0
{
518
0
  log_debug("unref client %p (%d references)", c, c->references);
519
520
0
  c->references--;
521
0
  if (c->references == 0)
522
0
    event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
523
0
}
524
525
/* Free dead client. */
526
static void
527
server_client_free(__unused int fd, __unused short events, void *arg)
528
0
{
529
0
  struct client *c = arg;
530
531
0
  log_debug("free client %p (%d references)", c, c->references);
532
533
0
  cmdq_free(c->queue);
534
535
0
  if (c->references == 0) {
536
0
    free((void *)c->name);
537
0
    free((void *)c->user);
538
0
    free(c);
539
0
  }
540
0
}
541
542
/* Suspend a client. */
543
void
544
server_client_suspend(struct client *c)
545
0
{
546
0
  struct session  *s = c->session;
547
548
0
  if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
549
0
    return;
550
551
0
  tty_stop_tty(&c->tty);
552
0
  c->flags |= CLIENT_SUSPENDED;
553
0
  proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
554
0
}
555
556
/* Detach a client. */
557
void
558
server_client_detach(struct client *c, enum msgtype msgtype)
559
0
{
560
0
  struct session  *s = c->session;
561
562
0
  if (s == NULL || (c->flags & CLIENT_NODETACHFLAGS))
563
0
    return;
564
565
0
  c->flags |= CLIENT_EXIT;
566
567
0
  c->exit_type = CLIENT_EXIT_DETACH;
568
0
  c->exit_msgtype = msgtype;
569
0
  c->exit_session = xstrdup(s->name);
570
0
}
571
572
/* Execute command to replace a client. */
573
void
574
server_client_exec(struct client *c, const char *cmd)
575
0
{
576
0
  struct session  *s = c->session;
577
0
  char    *msg;
578
0
  const char  *shell;
579
0
  size_t     cmdsize, shellsize;
580
581
0
  if (*cmd == '\0')
582
0
    return;
583
0
  cmdsize = strlen(cmd) + 1;
584
585
0
  if (s != NULL)
586
0
    shell = options_get_string(s->options, "default-shell");
587
0
  else
588
0
    shell = options_get_string(global_s_options, "default-shell");
589
0
  if (!checkshell(shell))
590
0
    shell = _PATH_BSHELL;
591
0
  shellsize = strlen(shell) + 1;
592
593
0
  msg = xmalloc(cmdsize + shellsize);
594
0
  memcpy(msg, cmd, cmdsize);
595
0
  memcpy(msg + cmdsize, shell, shellsize);
596
597
0
  proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
598
0
  free(msg);
599
0
}
600
601
static enum key_code_mouse_location
602
server_client_check_mouse_in_pane(struct window_pane *wp, int px, int py,
603
    u_int *sl_mpos)
604
0
{
605
0
  struct window   *w = wp->window;
606
0
  struct options    *wo = w->options;
607
0
  struct window_pane  *fwp;
608
0
  int      pane_status, sb, sb_pos, sb_w, sb_pad;
609
0
  int      pane_status_line, sl_top, sl_bottom;
610
0
  int      bdr_bottom, bdr_top, bdr_left, bdr_right;
611
612
0
  sb = options_get_number(wo, "pane-scrollbars");
613
0
  sb_pos = options_get_number(wo, "pane-scrollbars-position");
614
0
  pane_status = options_get_number(wo, "pane-border-status");
615
616
0
  if (window_pane_show_scrollbar(wp, sb)) {
617
0
    sb_w = wp->scrollbar_style.width;
618
0
    sb_pad = wp->scrollbar_style.pad;
619
0
  } else {
620
0
    sb_w = 0;
621
0
    sb_pad = 0;
622
0
  }
623
624
0
  if (pane_status == PANE_STATUS_TOP)
625
0
    pane_status_line = wp->yoff - 1;
626
0
  else if (pane_status == PANE_STATUS_BOTTOM)
627
0
    pane_status_line = wp->yoff + wp->sy;
628
0
  else
629
0
    pane_status_line = -1; /* not used */
630
631
  /* Check if point is within the pane or scrollbar. */
632
0
  if (((pane_status != PANE_STATUS_OFF &&
633
0
      py != pane_status_line && py != wp->yoff + (int)wp->sy) ||
634
0
      (wp->yoff == 0 && py < (int)wp->sy) ||
635
0
      (py >= wp->yoff && py < wp->yoff + (int)wp->sy)) &&
636
0
      ((sb_pos == PANE_SCROLLBARS_RIGHT &&
637
0
      px < wp->xoff + (int)wp->sx + sb_pad + sb_w) ||
638
0
      (sb_pos == PANE_SCROLLBARS_LEFT &&
639
0
      px < wp->xoff + (int)wp->sx - sb_pad - sb_w))) {
640
    /* Check if in the scrollbar. */
641
0
    if ((sb_pos == PANE_SCROLLBARS_RIGHT &&
642
0
        (px >= wp->xoff + (int)wp->sx + sb_pad &&
643
0
        px < wp->xoff + (int)wp->sx + sb_pad + sb_w)) ||
644
0
        (sb_pos == PANE_SCROLLBARS_LEFT &&
645
0
        (px >= wp->xoff - sb_pad - sb_w &&
646
0
        px < wp->xoff - sb_pad))) {
647
      /* Check where inside the scrollbar. */
648
0
      sl_top = wp->yoff + wp->sb_slider_y;
649
0
      sl_bottom = (wp->yoff + wp->sb_slider_y +
650
0
          wp->sb_slider_h - 1);
651
0
      if (py < sl_top)
652
0
        return (KEYC_MOUSE_LOCATION_SCROLLBAR_UP);
653
0
      else if (py >= sl_top && py <= sl_bottom) {
654
0
        *sl_mpos = (py - wp->sb_slider_y - wp->yoff);
655
0
        return (KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER);
656
0
      } else /* py > sl_bottom */
657
0
        return (KEYC_MOUSE_LOCATION_SCROLLBAR_DOWN);
658
0
    } else if (window_pane_is_floating(wp) &&
659
0
        (px == wp->xoff - 1 ||
660
0
        py == wp->yoff - 1 ||
661
0
        py == wp->yoff + (int)wp->sy)) {
662
      /* Floating pane left, bottom or top border. */
663
0
      return (KEYC_MOUSE_LOCATION_BORDER);
664
0
    } else {
665
      /* Must be inside the pane. */
666
0
      return (KEYC_MOUSE_LOCATION_PANE);
667
0
    }
668
0
  } else {
669
    /* Try the pane borders. */
670
0
    TAILQ_FOREACH(fwp, &w->panes, entry) {
671
0
      if ((w->flags & WINDOW_ZOOMED) &&
672
0
          (~fwp->flags & PANE_ZOOMED))
673
0
        continue;
674
0
      bdr_top = fwp->yoff - 1;
675
0
      bdr_bottom = fwp->yoff + fwp->sy;
676
0
      if (sb_pos == PANE_SCROLLBARS_LEFT)
677
0
        bdr_right = fwp->xoff + fwp->sx;
678
0
      else {
679
        /* PANE_SCROLLBARS_RIGHT or none. */
680
0
        bdr_right = fwp->xoff + fwp->sx + sb_pad + sb_w;
681
0
      }
682
0
      if (py >= fwp->yoff - 1 &&
683
0
          py <= fwp->yoff + (int)fwp->sy) {
684
0
        if (px == bdr_right)
685
0
          break;
686
0
        if (window_pane_is_floating(wp)) {
687
          /* Floating pane, check left border. */
688
0
          bdr_left = fwp->xoff - 1;
689
0
          if (px == bdr_left)
690
0
            break;
691
0
        }
692
0
      }
693
0
      if (px >= fwp->xoff - 1 &&
694
0
          px <= fwp->xoff + (int)fwp->sx) {
695
0
        bdr_bottom = fwp->yoff + fwp->sy;
696
0
        if (py == bdr_bottom)
697
0
          break;
698
0
        if (py == bdr_top)
699
0
          break;
700
0
      }
701
0
    }
702
0
    if (fwp != NULL)
703
0
      return (KEYC_MOUSE_LOCATION_BORDER);
704
0
  }
705
0
  return (KEYC_MOUSE_LOCATION_NOWHERE);
706
0
}
707
708
/* Check for mouse keys. */
709
static key_code
710
server_client_check_mouse(struct client *c, struct key_event *event)
711
0
{
712
0
  struct mouse_event    *m = &event->m;
713
0
  struct session      *s = c->session, *fs;
714
0
  struct window     *w = s->curw->window;
715
0
  struct winlink      *fwl;
716
0
  struct window_pane    *wp, *fwp, *lwp = NULL;
717
0
  u_int        x, y, sx, sy, px, py, n, sl_mpos = 0;
718
0
  u_int        b, bn;
719
0
  int        ignore = 0;
720
0
  key_code       key;
721
0
  struct timeval       tv;
722
0
  struct style_range    *sr;
723
0
  enum key_code_type     type = KEYC_TYPE_NOTYPE;
724
0
  enum key_code_mouse_location   loc = KEYC_MOUSE_LOCATION_NOWHERE;
725
726
0
  log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
727
0
      m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
728
729
  /* Find last pane, if any. */
730
0
  if (c->tty.mouse_last_pane != -1) {
731
0
    lwp = window_pane_find_by_id(c->tty.mouse_last_pane);
732
0
    if (lwp != NULL)
733
0
      log_debug("%s mouse last pane %%%u", c->name, lwp->id);
734
0
  }
735
736
  /* What type of event is this? */
737
0
  if (event->key == KEYC_DOUBLECLICK) {
738
0
    type = KEYC_TYPE_DOUBLECLICK;
739
0
    x = m->x, y = m->y, b = m->b;
740
0
    ignore = 1;
741
0
    log_debug("double-click at %u,%u", x, y);
742
0
  } else if ((m->sgr_type != ' ' &&
743
0
      MOUSE_DRAG(m->sgr_b) &&
744
0
      MOUSE_RELEASE(m->sgr_b)) ||
745
0
      (m->sgr_type == ' ' &&
746
0
      MOUSE_DRAG(m->b) &&
747
0
      MOUSE_RELEASE(m->b) &&
748
0
      MOUSE_RELEASE(m->lb))) {
749
0
    type = KEYC_TYPE_MOUSEMOVE;
750
0
    x = m->x, y = m->y, b = 0;
751
0
    log_debug("move at %u,%u", x, y);
752
0
  } else if (MOUSE_DRAG(m->b)) {
753
0
    type = KEYC_TYPE_MOUSEDRAG;
754
0
    if (c->tty.mouse_drag_flag) {
755
0
      x = m->x, y = m->y, b = m->b;
756
0
      if (x == m->lx && y == m->ly)
757
0
        return (KEYC_UNKNOWN);
758
0
      log_debug("drag update at %u,%u", x, y);
759
0
    } else {
760
0
      x = m->lx, y = m->ly, b = m->lb;
761
0
      log_debug("drag start at %u,%u", x, y);
762
0
    }
763
0
  } else if (MOUSE_WHEEL(m->b)) {
764
0
    if ((m->b & MOUSE_MASK_BUTTONS) == MOUSE_WHEEL_UP)
765
0
      type = KEYC_TYPE_WHEELUP;
766
0
    else
767
0
      type = KEYC_TYPE_WHEELDOWN;
768
0
    x = m->x, y = m->y, b = m->b;
769
0
    log_debug("wheel at %u,%u", x, y);
770
0
  } else if (MOUSE_RELEASE(m->b)) {
771
0
    type = KEYC_TYPE_MOUSEUP;
772
0
    x = m->x, y = m->y, b = m->lb;
773
0
    if (m->sgr_type == 'm')
774
0
      b = m->sgr_b;
775
0
    log_debug("up at %u,%u", x, y);
776
0
  } else {
777
0
    if (c->flags & CLIENT_DOUBLECLICK) {
778
0
      evtimer_del(&c->click_timer);
779
0
      c->flags &= ~CLIENT_DOUBLECLICK;
780
0
      type = KEYC_TYPE_SECONDCLICK;
781
0
      x = m->x, y = m->y, b = m->b;
782
0
      log_debug("second-click at %u,%u", x, y);
783
0
      c->flags |= CLIENT_TRIPLECLICK;
784
0
    } else if (c->flags & CLIENT_TRIPLECLICK) {
785
0
      evtimer_del(&c->click_timer);
786
0
      c->flags &= ~CLIENT_TRIPLECLICK;
787
0
      type = KEYC_TYPE_TRIPLECLICK;
788
0
      x = m->x, y = m->y, b = m->b;
789
0
      log_debug("triple-click at %u,%u", x, y);
790
0
      goto have_event;
791
0
    }
792
793
    /* DOWN is the only remaining event type. */
794
0
    if (type == KEYC_TYPE_NOTYPE) {
795
0
      type = KEYC_TYPE_MOUSEDOWN;
796
0
      x = m->x, y = m->y, b = m->b;
797
0
      log_debug("down at %u,%u", x, y);
798
0
      c->flags |= CLIENT_DOUBLECLICK;
799
0
    }
800
0
  }
801
802
0
have_event:
803
0
  if (type == KEYC_TYPE_NOTYPE)
804
0
    return (KEYC_UNKNOWN);
805
806
  /* Save the session. */
807
0
  m->s = s->id;
808
0
  m->w = -1;
809
0
  m->wp = -1;
810
0
  m->ignore = ignore;
811
812
  /* Is this on the status line? */
813
0
  m->statusat = status_at_line(c);
814
0
  m->statuslines = status_line_size(c);
815
0
  if (m->statusat != -1 &&
816
0
      y >= (u_int)m->statusat &&
817
0
      y < m->statusat + m->statuslines) {
818
0
    sr = status_get_range(c, x, y - m->statusat);
819
0
    if (sr == NULL) {
820
0
      loc = KEYC_MOUSE_LOCATION_STATUS_DEFAULT;
821
0
    } else {
822
0
      switch (sr->type) {
823
0
      case STYLE_RANGE_NONE:
824
0
        return (KEYC_UNKNOWN);
825
0
      case STYLE_RANGE_LEFT:
826
0
        log_debug("mouse range: left");
827
0
        loc = KEYC_MOUSE_LOCATION_STATUS_LEFT;
828
0
        break;
829
0
      case STYLE_RANGE_RIGHT:
830
0
        log_debug("mouse range: right");
831
0
        loc = KEYC_MOUSE_LOCATION_STATUS_RIGHT;
832
0
        break;
833
0
      case STYLE_RANGE_PANE:
834
0
        fwp = window_pane_find_by_id(sr->argument);
835
0
        if (fwp == NULL)
836
0
          return (KEYC_UNKNOWN);
837
0
        m->wp = sr->argument;
838
839
0
        log_debug("mouse range: pane %%%u", m->wp);
840
0
        loc = KEYC_MOUSE_LOCATION_STATUS;
841
0
        break;
842
0
      case STYLE_RANGE_WINDOW:
843
0
        fwl = winlink_find_by_index(&s->windows,
844
0
            sr->argument);
845
0
        if (fwl == NULL)
846
0
          return (KEYC_UNKNOWN);
847
0
        m->w = fwl->window->id;
848
849
0
        log_debug("mouse range: window @%u", m->w);
850
0
        loc = KEYC_MOUSE_LOCATION_STATUS;
851
0
        break;
852
0
      case STYLE_RANGE_SESSION:
853
0
        fs = session_find_by_id(sr->argument);
854
0
        if (fs == NULL)
855
0
          return (KEYC_UNKNOWN);
856
0
        m->s = sr->argument;
857
858
0
        log_debug("mouse range: session $%u", m->s);
859
0
        loc = KEYC_MOUSE_LOCATION_STATUS;
860
0
        break;
861
0
      case STYLE_RANGE_USER:
862
0
        log_debug("mouse range: user");
863
0
        loc = KEYC_MOUSE_LOCATION_STATUS;
864
0
        break;
865
0
      case STYLE_RANGE_CONTROL:
866
0
        n = sr->argument; /* parsing keeps this < 10 */
867
0
        log_debug("mouse range: control %u", n);
868
0
        loc = KEYC_MOUSE_LOCATION_CONTROL0 + n;
869
0
        break;
870
0
      }
871
0
    }
872
0
  }
873
874
  /*
875
   * Not on status line. Adjust position and check for border, pane, or
876
   * scrollbar.
877
   */
878
0
  if (loc == KEYC_MOUSE_LOCATION_NOWHERE) {
879
0
    if (c->tty.mouse_scrolling_flag) {
880
0
      if (lwp != NULL) {
881
0
        loc = KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER;
882
0
        m->wp = lwp->id;
883
0
        m->w = lwp->window->id;
884
0
      }
885
0
    } else {
886
0
      px = x;
887
0
      if (m->statusat == 0 && y >= m->statuslines)
888
0
        py = y - m->statuslines;
889
0
      else if (m->statusat > 0 && y >= (u_int)m->statusat)
890
0
        py = m->statusat - 1;
891
0
      else
892
0
        py = y;
893
894
0
      tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
895
0
      log_debug("mouse window @%u at %u,%u (%ux%u)",
896
0
          w->id, m->ox, m->oy, sx, sy);
897
0
      if (px > sx || py > sy)
898
0
        return (KEYC_UNKNOWN);
899
0
      px = px + m->ox;
900
0
      py = py + m->oy;
901
902
0
      if (type == KEYC_TYPE_MOUSEDRAG && lwp != NULL) {
903
        /* Use pane from last mouse event. */
904
0
        wp = lwp;
905
0
      } else {
906
        /* Try inside the pane. */
907
0
        wp = window_get_active_at(w, px, py);
908
0
      }
909
0
      if (wp == NULL)
910
0
        return (KEYC_UNKNOWN);
911
0
      loc = server_client_check_mouse_in_pane(wp, px, py,
912
0
          &sl_mpos);
913
914
0
      if (loc == KEYC_MOUSE_LOCATION_PANE) {
915
0
        log_debug("mouse %u,%u on pane %%%u", x, y,
916
0
            wp->id);
917
0
      } else if (loc == KEYC_MOUSE_LOCATION_BORDER) {
918
0
        sr = window_pane_border_status_get_range(wp, px,
919
0
          py);
920
0
        if (sr != NULL) {
921
0
          n = sr->argument;
922
0
          loc = KEYC_MOUSE_LOCATION_CONTROL0 + n;
923
0
        }
924
0
        log_debug("mouse on pane %%%u border", wp->id);
925
0
      } else if (loc == KEYC_MOUSE_LOCATION_SCROLLBAR_UP ||
926
0
          loc == KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER ||
927
0
          loc == KEYC_MOUSE_LOCATION_SCROLLBAR_DOWN) {
928
0
        log_debug("mouse on pane %%%u scrollbar",
929
0
            wp->id);
930
0
      }
931
0
      m->wp = wp->id;
932
0
      m->w = wp->window->id;
933
0
    }
934
0
  }
935
936
  /* Reset click type or add a click timer if needed. */
937
0
  if (type == KEYC_TYPE_MOUSEDOWN ||
938
0
      type == KEYC_TYPE_SECONDCLICK ||
939
0
      type == KEYC_TYPE_TRIPLECLICK) {
940
0
    if (type != KEYC_TYPE_MOUSEDOWN &&
941
0
        (m->b != c->click_button ||
942
0
        loc != (enum key_code_mouse_location)c->click_loc ||
943
0
        m->wp != c->click_wp)) {
944
0
      type = KEYC_TYPE_MOUSEDOWN;
945
0
      log_debug("click sequence reset at %u,%u", x, y);
946
0
      c->flags &= ~CLIENT_TRIPLECLICK;
947
0
      c->flags |= CLIENT_DOUBLECLICK;
948
0
    }
949
950
0
    if (type != KEYC_TYPE_TRIPLECLICK && KEYC_CLICK_TIMEOUT != 0) {
951
0
      memcpy(&c->click_event, m, sizeof c->click_event);
952
0
      c->click_button = m->b;
953
0
      c->click_loc = loc;
954
0
      c->click_wp = m->wp;
955
956
0
      log_debug("click timer started");
957
0
      tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
958
0
      tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
959
0
      evtimer_del(&c->click_timer);
960
0
      evtimer_add(&c->click_timer, &tv);
961
0
    }
962
0
  }
963
964
0
  key = KEYC_UNKNOWN;
965
966
  /* Stop dragging if needed. */
967
0
  if (type != KEYC_TYPE_MOUSEDRAG &&
968
0
      type != KEYC_TYPE_WHEELUP &&
969
0
      type != KEYC_TYPE_WHEELDOWN &&
970
0
      type != KEYC_TYPE_DOUBLECLICK &&
971
0
      type != KEYC_TYPE_TRIPLECLICK &&
972
0
      c->tty.mouse_drag_flag != 0) {
973
0
    if (c->tty.mouse_drag_release != NULL)
974
0
      c->tty.mouse_drag_release(c, m);
975
976
0
    c->tty.mouse_drag_update = NULL;
977
0
    c->tty.mouse_drag_release = NULL;
978
0
    c->tty.mouse_scrolling_flag = 0;
979
980
    /*
981
     * End a mouse drag by passing a MouseDragEnd key corresponding
982
     * to the button that started the drag.
983
     */
984
0
    type = KEYC_TYPE_MOUSEDRAGEND;
985
0
    c->tty.mouse_drag_flag = 0;
986
0
    c->tty.mouse_slider_mpos = -1;
987
0
    c->tty.mouse_last_pane = -1;
988
0
  }
989
990
  /* Convert to a key binding. */
991
0
  if (type == KEYC_TYPE_MOUSEMOVE && loc == KEYC_MOUSE_LOCATION_PANE) {
992
0
    key = KEYC_MOUSEMOVE_PANE;
993
0
    if (wp != NULL &&
994
0
        wp != w->active &&
995
0
        options_get_number(s->options, "focus-follows-mouse")) {
996
0
      window_redraw_active_switch(w, wp);
997
0
      window_set_active_pane(w, wp, 1);
998
0
      server_redraw_window_borders(w);
999
0
      server_status_window(w);
1000
0
    }
1001
0
  }
1002
0
  if (type == KEYC_TYPE_MOUSEDRAG) {
1003
0
    if (c->tty.mouse_drag_update != NULL)
1004
0
      key = KEYC_DRAGGING;
1005
1006
    /*
1007
     * Begin a drag by setting the flag to a non-zero value that
1008
     * corresponds to the mouse button in use. If starting to drag
1009
     * the scrollbar, store the relative position in the slider
1010
     * where the user grabbed.
1011
     */
1012
0
    c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1013
1014
    /* Only change pane if not already dragging a pane border. */
1015
0
    if (lwp == NULL) {
1016
0
      lwp = wp = window_get_active_at(w, px, py);
1017
0
      if (wp != NULL)
1018
0
        c->tty.mouse_last_pane = wp->id;
1019
0
    }
1020
0
    if (c->tty.mouse_scrolling_flag == 0 &&
1021
0
        loc == KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER) {
1022
0
      c->tty.mouse_scrolling_flag = 1;
1023
0
      if (m->statusat == 0) {
1024
0
        c->tty.mouse_slider_mpos = sl_mpos +
1025
0
            m->statuslines;
1026
0
      } else
1027
0
        c->tty.mouse_slider_mpos = sl_mpos;
1028
0
    }
1029
0
  }
1030
1031
0
  if (key == KEYC_UNKNOWN) {
1032
    /* Adjust the button number. */
1033
0
    if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_1)
1034
0
      bn = 1;
1035
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_2)
1036
0
      bn = 2;
1037
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_3)
1038
0
      bn = 3;
1039
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_6)
1040
0
      bn = 6;
1041
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_7)
1042
0
      bn = 7;
1043
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_8)
1044
0
      bn = 8;
1045
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_9)
1046
0
      bn = 9;
1047
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_10)
1048
0
      bn = 10;
1049
0
    else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_11)
1050
0
      bn = 11;
1051
0
    else
1052
0
      bn = 0;
1053
0
    key = KEYC_MAKE_MOUSE_KEY(type, bn, loc);
1054
0
  }
1055
1056
  /* Apply modifiers if any. */
1057
0
  if (b & MOUSE_MASK_META)
1058
0
    key |= KEYC_META;
1059
0
  if (b & MOUSE_MASK_CTRL)
1060
0
    key |= KEYC_CTRL;
1061
0
  if (b & MOUSE_MASK_SHIFT)
1062
0
    key |= KEYC_SHIFT;
1063
1064
0
  if (log_get_level() != 0)
1065
0
    log_debug("mouse key is %s", key_string_lookup_key (key, 1));
1066
0
  return (key);
1067
0
}
1068
1069
/* Is this a bracket paste key? */
1070
static int
1071
server_client_is_bracket_paste(struct client *c, key_code key)
1072
0
{
1073
0
  if ((key & KEYC_MASK_KEY) == KEYC_PASTE_START) {
1074
0
    c->flags |= CLIENT_BRACKETPASTING;
1075
0
    c->paste_time = current_time;
1076
0
    log_debug("%s: bracket paste on", c->name);
1077
0
    return (0);
1078
0
  }
1079
1080
0
  if ((key & KEYC_MASK_KEY) == KEYC_PASTE_END) {
1081
0
    c->flags &= ~CLIENT_BRACKETPASTING;
1082
0
    log_debug("%s: bracket paste off", c->name);
1083
0
    return (0);
1084
0
  }
1085
1086
0
  return !!(c->flags & CLIENT_BRACKETPASTING);
1087
0
}
1088
1089
/* Is this fast enough to probably be a paste? */
1090
static int
1091
server_client_is_assume_paste(struct client *c)
1092
0
{
1093
0
  struct session  *s = c->session;
1094
0
  struct timeval   tv;
1095
0
  int    t;
1096
1097
0
  if (c->flags & CLIENT_BRACKETPASTING)
1098
0
    return (0);
1099
0
  if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1100
0
    return (0);
1101
0
  if (tty_term_has(c->tty.term, TTYC_ENBP))
1102
0
    return (0);
1103
1104
0
  timersub(&c->activity_time, &c->last_activity_time, &tv);
1105
0
  if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
1106
0
    if (c->flags & CLIENT_ASSUMEPASTING)
1107
0
      return (1);
1108
0
    c->flags |= CLIENT_ASSUMEPASTING;
1109
0
    c->paste_time = current_time;
1110
0
    log_debug("%s: assume paste on", c->name);
1111
0
    return (0);
1112
0
  }
1113
0
  if (c->flags & CLIENT_ASSUMEPASTING) {
1114
0
    c->flags &= ~CLIENT_ASSUMEPASTING;
1115
0
    log_debug("%s: assume paste off", c->name);
1116
0
  }
1117
0
  return (0);
1118
0
}
1119
1120
/* Has the latest client changed? */
1121
static void
1122
server_client_update_latest(struct client *c)
1123
0
{
1124
0
  struct window *w;
1125
1126
0
  if (c->session == NULL)
1127
0
    return;
1128
0
  w = c->session->curw->window;
1129
1130
0
  if (w->latest == c)
1131
0
    return;
1132
0
  w->latest = c;
1133
1134
0
  if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
1135
0
    recalculate_size(w, 0);
1136
1137
0
  notify_client("client-active", c);
1138
0
}
1139
1140
/* Get repeat time. */
1141
static u_int
1142
server_client_repeat_time(struct client *c, struct key_binding *bd)
1143
0
{
1144
0
  struct session  *s = c->session;
1145
0
  u_int    repeat, initial;
1146
1147
0
  if (~bd->flags & KEY_BINDING_REPEAT)
1148
0
    return (0);
1149
0
  repeat = options_get_number(s->options, "repeat-time");
1150
0
  if (repeat == 0)
1151
0
    return (0);
1152
0
  if ((~c->flags & CLIENT_REPEAT) || bd->key != c->last_key) {
1153
0
    initial = options_get_number(s->options, "initial-repeat-time");
1154
0
    if (initial != 0)
1155
0
      repeat = initial;
1156
0
  }
1157
0
  return (repeat);
1158
0
}
1159
1160
/*
1161
 * Handle data key input from client. This owns and can modify the key event it
1162
 * is given and is responsible for freeing it.
1163
 */
1164
static enum cmd_retval
1165
server_client_key_callback(struct cmdq_item *item, void *data)
1166
0
{
1167
0
  struct client     *c = cmdq_get_client(item);
1168
0
  struct key_event    *event = data;
1169
0
  key_code       key = event->key;
1170
0
  struct mouse_event    *m = &event->m;
1171
0
  struct session      *s = c->session;
1172
0
  struct winlink      *wl;
1173
0
  struct window_pane    *wp;
1174
0
  struct window_mode_entry  *wme;
1175
0
  struct timeval       tv;
1176
0
  struct key_table    *table, *first;
1177
0
  struct key_binding    *bd;
1178
0
  u_int        repeat;
1179
0
  uint64_t       flags, prefix_delay;
1180
0
  struct cmd_find_state    fs;
1181
0
  key_code       key0, prefix, prefix2;
1182
1183
  /* Check the client is good to accept input. */
1184
0
  if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1185
0
    goto out;
1186
0
  wl = s->curw;
1187
1188
  /* Update the activity timer. */
1189
0
  memcpy(&c->last_activity_time, &c->activity_time,
1190
0
      sizeof c->last_activity_time);
1191
0
  if (gettimeofday(&c->activity_time, NULL) != 0)
1192
0
    fatal("gettimeofday failed");
1193
0
  session_update_activity(s, &c->activity_time);
1194
1195
  /* Check for mouse keys. */
1196
0
  m->valid = 0;
1197
0
  if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1198
0
    if (c->flags & CLIENT_READONLY)
1199
0
      goto out;
1200
0
    key = server_client_check_mouse(c, event);
1201
0
    if (key == KEYC_UNKNOWN)
1202
0
      goto out;
1203
1204
0
    m->valid = 1;
1205
0
    m->key = key;
1206
1207
    /*
1208
     * Mouse drag is in progress, so fire the callback (now that
1209
     * the mouse event is valid).
1210
     */
1211
0
    if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1212
0
      c->tty.mouse_drag_update(c, m);
1213
0
      goto out;
1214
0
    }
1215
0
    event->key = key;
1216
0
  }
1217
1218
  /* Find affected pane. */
1219
0
  if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1220
0
    cmd_find_from_client(&fs, c, 0);
1221
0
  wp = fs.wp;
1222
1223
  /* Forward mouse keys if disabled. */
1224
0
  if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1225
0
    goto forward_key;
1226
1227
  /* Forward if bracket pasting. */
1228
0
  if (server_client_is_bracket_paste (c, key))
1229
0
    goto paste_key;
1230
1231
  /* Treat everything as a regular key when pasting is detected. */
1232
0
  if (!KEYC_IS_MOUSE(key) &&
1233
0
      key != KEYC_FOCUS_IN &&
1234
0
      key != KEYC_FOCUS_OUT &&
1235
0
      (~key & KEYC_SENT) &&
1236
0
      server_client_is_assume_paste(c))
1237
0
    goto paste_key;
1238
1239
  /*
1240
   * Work out the current key table. If the pane is in a mode, use
1241
   * the mode table instead of the default key table.
1242
   */
1243
0
  if (server_client_is_default_key_table(c, c->keytable) &&
1244
0
      wp != NULL &&
1245
0
      (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1246
0
      wme->mode->key_table != NULL)
1247
0
    table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1248
0
  else
1249
0
    table = c->keytable;
1250
0
  first = table;
1251
1252
0
table_changed:
1253
  /*
1254
   * The prefix always takes precedence and forces a switch to the prefix
1255
   * table, unless we are already there.
1256
   */
1257
0
  prefix = (key_code)options_get_number(s->options, "prefix");
1258
0
  prefix2 = (key_code)options_get_number(s->options, "prefix2");
1259
0
  key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1260
0
  if ((key0 == (prefix & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) ||
1261
0
      key0 == (prefix2 & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS))) &&
1262
0
      strcmp(table->name, "prefix") != 0) {
1263
0
    server_client_set_key_table(c, "prefix");
1264
0
    server_status_client(c);
1265
0
    goto out;
1266
0
  }
1267
0
  flags = c->flags;
1268
1269
0
try_again:
1270
  /* Log key table. */
1271
0
  if (wp == NULL)
1272
0
    log_debug("key table %s (no pane)", table->name);
1273
0
  else
1274
0
    log_debug("key table %s (pane %%%u)", table->name, wp->id);
1275
0
  if (c->flags & CLIENT_REPEAT)
1276
0
    log_debug("currently repeating");
1277
1278
0
  bd = key_bindings_get(table, key0);
1279
1280
  /*
1281
   * If prefix-timeout is enabled and we're in the prefix table, see if
1282
   * the timeout has been exceeded. Revert to the root table if so.
1283
   */
1284
0
  prefix_delay = options_get_number(global_options, "prefix-timeout");
1285
0
  if (prefix_delay > 0 &&
1286
0
      strcmp(table->name, "prefix") == 0 &&
1287
0
      server_client_key_table_activity_diff(c) > prefix_delay) {
1288
    /*
1289
     * If repeating is active and this is a repeating binding,
1290
     * ignore the timeout.
1291
     */
1292
0
    if (bd != NULL &&
1293
0
        (c->flags & CLIENT_REPEAT) &&
1294
0
        (bd->flags & KEY_BINDING_REPEAT)) {
1295
0
      log_debug("prefix timeout ignored, repeat is active");
1296
0
    } else {
1297
0
      log_debug("prefix timeout exceeded");
1298
0
      server_client_set_key_table(c, NULL);
1299
0
      first = table = c->keytable;
1300
0
      server_status_client(c);
1301
0
      goto table_changed;
1302
0
    }
1303
0
  }
1304
1305
  /* Try to see if there is a key binding in the current table. */
1306
0
  if (bd != NULL) {
1307
    /*
1308
     * Key was matched in this table. If currently repeating but a
1309
     * non-repeating binding was found, stop repeating and try
1310
     * again in the root table.
1311
     */
1312
0
    if ((c->flags & CLIENT_REPEAT) &&
1313
0
        (~bd->flags & KEY_BINDING_REPEAT)) {
1314
0
      log_debug("found in key table %s (not repeating)",
1315
0
          table->name);
1316
0
      server_client_set_key_table(c, NULL);
1317
0
      first = table = c->keytable;
1318
0
      c->flags &= ~CLIENT_REPEAT;
1319
0
      server_status_client(c);
1320
0
      goto table_changed;
1321
0
    }
1322
0
    log_debug("found in key table %s", table->name);
1323
1324
    /*
1325
     * Take a reference to this table to make sure the key binding
1326
     * doesn't disappear.
1327
     */
1328
0
    table->references++;
1329
1330
    /*
1331
     * If this is a repeating key, start the timer. Otherwise reset
1332
     * the client back to the root table.
1333
     */
1334
0
    repeat = server_client_repeat_time(c, bd);
1335
0
    if (repeat != 0) {
1336
0
      c->flags |= CLIENT_REPEAT;
1337
0
      c->last_key = bd->key;
1338
1339
0
      tv.tv_sec = repeat / 1000;
1340
0
      tv.tv_usec = (repeat % 1000) * 1000L;
1341
0
      evtimer_del(&c->repeat_timer);
1342
0
      evtimer_add(&c->repeat_timer, &tv);
1343
0
    } else {
1344
0
      c->flags &= ~CLIENT_REPEAT;
1345
0
      server_client_set_key_table(c, NULL);
1346
0
    }
1347
0
    server_status_client(c);
1348
1349
    /* Execute the key binding. */
1350
0
    key_bindings_dispatch(bd, item, c, event, &fs);
1351
0
    key_bindings_unref_table(table);
1352
0
    goto out;
1353
0
  }
1354
1355
  /*
1356
   * No match, try the ANY key.
1357
   */
1358
0
  if (key0 != KEYC_ANY) {
1359
0
    key0 = KEYC_ANY;
1360
0
    goto try_again;
1361
0
  }
1362
1363
  /*
1364
   * Binding movement keys is useless since we only turn them on when the
1365
   * application requests, so don't let them exit the prefix table.
1366
   */
1367
0
  if (key == KEYC_MOUSEMOVE_PANE ||
1368
0
      key == KEYC_MOUSEMOVE_STATUS ||
1369
0
      key == KEYC_MOUSEMOVE_STATUS_LEFT ||
1370
0
      key == KEYC_MOUSEMOVE_STATUS_RIGHT ||
1371
0
      key == KEYC_MOUSEMOVE_STATUS_DEFAULT ||
1372
0
      key == KEYC_MOUSEMOVE_BORDER)
1373
0
    goto forward_key;
1374
1375
  /*
1376
   * No match in this table. If not in the root table or if repeating
1377
   * switch the client back to the root table and try again.
1378
   */
1379
0
  log_debug("not found in key table %s", table->name);
1380
0
  if (!server_client_is_default_key_table(c, table) ||
1381
0
      (c->flags & CLIENT_REPEAT)) {
1382
0
    log_debug("trying in root table");
1383
0
    server_client_set_key_table(c, NULL);
1384
0
    table = c->keytable;
1385
0
    if (c->flags & CLIENT_REPEAT)
1386
0
      first = table;
1387
0
    c->flags &= ~CLIENT_REPEAT;
1388
0
    server_status_client(c);
1389
0
    goto table_changed;
1390
0
  }
1391
1392
  /*
1393
   * No match in the root table either. If this wasn't the first table
1394
   * tried, don't pass the key to the pane.
1395
   */
1396
0
  if (first != table && (~flags & CLIENT_REPEAT)) {
1397
0
    server_client_set_key_table(c, NULL);
1398
0
    server_status_client(c);
1399
0
    goto out;
1400
0
  }
1401
1402
0
forward_key:
1403
0
  if (wp != NULL &&
1404
0
      (wp->flags & PANE_EXITED) &&
1405
0
      !KEYC_IS_MOUSE(key) &&
1406
0
      !KEYC_IS_PASTE(key) &&
1407
0
      options_get_number(wp->options, "remain-on-exit") == 3) {
1408
0
    options_set_number(wp->options, "remain-on-exit", 0);
1409
0
    server_destroy_pane(wp, 0);
1410
0
    goto out;
1411
0
  }
1412
0
  if (c->flags & CLIENT_READONLY)
1413
0
    goto out;
1414
0
  if (wp != NULL)
1415
0
    window_pane_key(wp, c, s, wl, key, m);
1416
0
  goto out;
1417
1418
0
paste_key:
1419
0
  if (c->flags & CLIENT_READONLY)
1420
0
    goto out;
1421
0
  if (event->buf != NULL)
1422
0
    window_pane_paste(wp, key, event->buf, event->len);
1423
0
  key = KEYC_NONE;
1424
0
  goto out;
1425
1426
0
out:
1427
0
  if (s != NULL && key != KEYC_FOCUS_OUT)
1428
0
    server_client_update_latest(c);
1429
0
  free(event->buf);
1430
0
  free(event);
1431
0
  return (CMD_RETURN_NORMAL);
1432
0
}
1433
1434
/* Handle a key event. */
1435
int
1436
server_client_handle_key(struct client *c, struct key_event *event)
1437
0
{
1438
0
  struct session    *s = c->session;
1439
0
  struct cmdq_item  *item;
1440
1441
  /* Check the client is good to accept input. */
1442
0
  if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1443
0
    return (0);
1444
1445
  /*
1446
   * Handle theme reporting keys before overlays so they work even when a
1447
   * popup is open.
1448
   */
1449
0
  if (event->key == KEYC_REPORT_LIGHT_THEME) {
1450
0
    server_client_report_theme(c, THEME_LIGHT);
1451
0
    return (0);
1452
0
  }
1453
0
  if (event->key == KEYC_REPORT_DARK_THEME) {
1454
0
    server_client_report_theme(c, THEME_DARK);
1455
0
    return (0);
1456
0
  }
1457
1458
  /*
1459
   * Key presses in overlay mode and the command prompt are a special
1460
   * case. The queue might be blocked so they need to be processed
1461
   * immediately rather than queued.
1462
   */
1463
0
  if (~c->flags & CLIENT_READONLY) {
1464
0
    if (c->message_string != NULL) {
1465
0
      if (c->message_ignore_keys)
1466
0
        return (0);
1467
0
      status_message_clear(c);
1468
0
    }
1469
0
    if (c->overlay_key != NULL) {
1470
0
      switch (c->overlay_key(c, c->overlay_data, event)) {
1471
0
      case 0:
1472
0
        return (0);
1473
0
      case 1:
1474
0
        server_client_clear_overlay(c);
1475
0
        return (0);
1476
0
      }
1477
0
    }
1478
0
    server_client_clear_overlay(c);
1479
0
    if (c->prompt_string != NULL) {
1480
0
      if (status_prompt_key(c, event->key) == 0)
1481
0
        return (0);
1482
0
    }
1483
0
  }
1484
1485
  /*
1486
   * Add the key to the queue so it happens after any commands queued by
1487
   * previous keys.
1488
   */
1489
0
  item = cmdq_get_callback(server_client_key_callback, event);
1490
0
  cmdq_append(c, item);
1491
0
  return (1);
1492
0
}
1493
1494
/* Client functions that need to happen every loop. */
1495
void
1496
server_client_loop(void)
1497
0
{
1498
0
  struct client     *c;
1499
0
  struct window     *w;
1500
0
  struct window_pane    *wp;
1501
0
  struct window_mode_entry  *wme;
1502
1503
  /* Check for window resize. This is done before redrawing. */
1504
0
  RB_FOREACH(w, windows, &windows)
1505
0
    server_client_check_window_resize(w);
1506
1507
  /* Notify modes that pane styles may have changed. */
1508
0
  RB_FOREACH(w, windows, &windows) {
1509
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
1510
0
      if (wp->flags & PANE_STYLECHANGED) {
1511
0
        wme = TAILQ_FIRST(&wp->modes);
1512
0
        if (wme != NULL &&
1513
0
            wme->mode->style_changed != NULL)
1514
0
          wme->mode->style_changed(wme);
1515
0
      }
1516
0
    }
1517
0
  }
1518
1519
  /* Check clients. */
1520
0
  TAILQ_FOREACH(c, &clients, entry) {
1521
0
    server_client_check_exit(c);
1522
0
    if (c->session != NULL && c->session->curw != NULL) {
1523
0
      server_client_check_modes(c);
1524
0
      server_client_check_redraw(c);
1525
0
      server_client_reset_state(c);
1526
0
    }
1527
0
  }
1528
1529
  /*
1530
   * Any windows will have been redrawn as part of clients, so clear
1531
   * their flags now.
1532
   */
1533
0
  RB_FOREACH(w, windows, &windows) {
1534
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
1535
0
      if (wp->fd != -1) {
1536
0
        server_client_check_pane_resize(wp);
1537
0
        server_client_check_pane_buffer(wp);
1538
0
      }
1539
0
      wp->flags &= ~(PANE_REDRAW|PANE_REDRAWSCROLLBAR);
1540
0
    }
1541
0
    check_window_name(w);
1542
0
  }
1543
1544
  /* Send theme updates. */
1545
0
  RB_FOREACH(w, windows, &windows) {
1546
0
    TAILQ_FOREACH(wp, &w->panes, entry)
1547
0
      window_pane_send_theme_update(wp);
1548
0
  }
1549
0
}
1550
1551
/* Check if window needs to be resized. */
1552
static void
1553
server_client_check_window_resize(struct window *w)
1554
0
{
1555
0
  struct winlink  *wl;
1556
1557
0
  if (~w->flags & WINDOW_RESIZE)
1558
0
    return;
1559
1560
0
  TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1561
0
    if (wl->session->attached != 0 && wl->session->curw == wl)
1562
0
      break;
1563
0
  }
1564
0
  if (wl == NULL)
1565
0
    return;
1566
1567
0
  log_debug("%s: resizing window @%u", __func__, w->id);
1568
0
  resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
1569
0
}
1570
1571
/* Resize timer event. */
1572
static void
1573
server_client_resize_timer(__unused int fd, __unused short events, void *data)
1574
0
{
1575
0
  struct window_pane  *wp = data;
1576
1577
0
  log_debug("%s: %%%u resize timer expired", __func__, wp->id);
1578
0
  evtimer_del(&wp->resize_timer);
1579
0
}
1580
1581
/* Check if pane should be resized. */
1582
static void
1583
server_client_check_pane_resize(struct window_pane *wp)
1584
0
{
1585
0
  struct window_pane_resize *r, *r1, *first, *last;
1586
0
  struct timeval       tv = { .tv_usec = 250000 };
1587
1588
0
  if (TAILQ_EMPTY(&wp->resize_queue))
1589
0
    return;
1590
1591
0
  if (!event_initialized(&wp->resize_timer))
1592
0
    evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
1593
0
  if (evtimer_pending(&wp->resize_timer, NULL))
1594
0
    return;
1595
1596
0
  log_debug("%s: %%%u needs to be resized", __func__, wp->id);
1597
0
  TAILQ_FOREACH(r, &wp->resize_queue, entry) {
1598
0
    log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy,
1599
0
        r->sx, r->sy);
1600
0
  }
1601
1602
  /*
1603
   * There are three cases that matter:
1604
   *
1605
   * - Only one resize. It can just be applied.
1606
   *
1607
   * - Multiple resizes and the ending size is different from the
1608
   *   starting size. We can discard all resizes except the most recent.
1609
   *
1610
   * - Multiple resizes and the ending size is the same as the starting
1611
   *   size. We must resize at least twice to force the application to
1612
   *   redraw. So apply the first and leave the last on the queue for
1613
   *   next time.
1614
   */
1615
0
  first = TAILQ_FIRST(&wp->resize_queue);
1616
0
  last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes);
1617
0
  if (first == last) {
1618
    /* Only one resize. */
1619
0
    window_pane_send_resize(wp, first->sx, first->sy);
1620
0
    TAILQ_REMOVE(&wp->resize_queue, first, entry);
1621
0
    free(first);
1622
0
  } else if (last->sx != first->osx || last->sy != first->osy) {
1623
    /* Multiple resizes ending up with a different size. */
1624
0
    window_pane_send_resize(wp, last->sx, last->sy);
1625
0
    TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1626
0
      TAILQ_REMOVE(&wp->resize_queue, r, entry);
1627
0
      free(r);
1628
0
    }
1629
0
  } else {
1630
    /*
1631
     * Multiple resizes ending up with the same size. There will
1632
     * not be more than one to the same size in succession so we
1633
     * can just use the last-but-one on the list and leave the last
1634
     * for later. We reduce the time until the next check to avoid
1635
     * a long delay between the resizes.
1636
     */
1637
0
    r = TAILQ_PREV(last, window_pane_resizes, entry);
1638
0
    window_pane_send_resize(wp, r->sx, r->sy);
1639
0
    TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1640
0
      if (r == last)
1641
0
        break;
1642
0
      TAILQ_REMOVE(&wp->resize_queue, r, entry);
1643
0
      free(r);
1644
0
    }
1645
0
    tv.tv_usec = 10000;
1646
0
  }
1647
0
  evtimer_add(&wp->resize_timer, &tv);
1648
0
}
1649
1650
/* Check pane buffer size. */
1651
static void
1652
server_client_check_pane_buffer(struct window_pane *wp)
1653
0
{
1654
0
  struct evbuffer     *evb = wp->event->input;
1655
0
  size_t         minimum;
1656
0
  struct client     *c;
1657
0
  struct window_pane_offset *wpo;
1658
0
  int        off = 1, flag;
1659
0
  u_int        attached_clients = 0;
1660
0
  size_t         new_size;
1661
1662
  /*
1663
   * Work out the minimum used size. This is the most that can be removed
1664
   * from the buffer.
1665
   */
1666
0
  minimum = wp->offset.used;
1667
0
  if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
1668
0
    minimum = wp->pipe_offset.used;
1669
0
  TAILQ_FOREACH(c, &clients, entry) {
1670
0
    if (c->session == NULL)
1671
0
      continue;
1672
0
    attached_clients++;
1673
1674
0
    if (~c->flags & CLIENT_CONTROL) {
1675
0
      off = 0;
1676
0
      continue;
1677
0
    }
1678
0
    wpo = control_pane_offset(c, wp, &flag);
1679
0
    if (wpo == NULL) {
1680
0
      if (!flag)
1681
0
        off = 0;
1682
0
      continue;
1683
0
    }
1684
0
    if (!flag)
1685
0
      off = 0;
1686
1687
0
    window_pane_get_new_data(wp, wpo, &new_size);
1688
0
    log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
1689
0
        __func__, c->name, wpo->used - wp->base_offset, new_size,
1690
0
        wp->id);
1691
0
    if (wpo->used < minimum)
1692
0
      minimum = wpo->used;
1693
0
  }
1694
0
  if (attached_clients == 0)
1695
0
    off = 0;
1696
0
  minimum -= wp->base_offset;
1697
0
  if (minimum == 0)
1698
0
    goto out;
1699
1700
  /* Drain the buffer. */
1701
0
  log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
1702
0
      wp->id, minimum, EVBUFFER_LENGTH(evb));
1703
0
  evbuffer_drain(evb, minimum);
1704
1705
  /*
1706
   * Adjust the base offset. If it would roll over, all the offsets into
1707
   * the buffer need to be adjusted.
1708
   */
1709
0
  if (wp->base_offset > SIZE_MAX - minimum) {
1710
0
    log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
1711
0
    wp->offset.used -= wp->base_offset;
1712
0
    if (wp->pipe_fd != -1)
1713
0
      wp->pipe_offset.used -= wp->base_offset;
1714
0
    TAILQ_FOREACH(c, &clients, entry) {
1715
0
      if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
1716
0
        continue;
1717
0
      wpo = control_pane_offset(c, wp, &flag);
1718
0
      if (wpo != NULL && !flag)
1719
0
        wpo->used -= wp->base_offset;
1720
0
    }
1721
0
    wp->base_offset = minimum;
1722
0
  } else
1723
0
    wp->base_offset += minimum;
1724
1725
0
out:
1726
  /*
1727
   * If there is data remaining, and there are no clients able to consume
1728
   * it, do not read any more. This is true when there are attached
1729
   * clients, all of which are control clients which are not able to
1730
   * accept any more data.
1731
   */
1732
0
  log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
1733
0
  if (off)
1734
0
    bufferevent_disable(wp->event, EV_READ);
1735
0
  else
1736
0
    bufferevent_enable(wp->event, EV_READ);
1737
0
}
1738
1739
/*
1740
 * Update cursor position and mode settings. The scroll region and attributes
1741
 * are cleared when idle (waiting for an event) as this is the most likely time
1742
 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1743
 * compromise between excessive resets and likelihood of an interrupt.
1744
 *
1745
 * tty_region/tty_reset/tty_update_mode already take care of not resetting
1746
 * things that are already in their default state.
1747
 */
1748
static void
1749
server_client_reset_state(struct client *c)
1750
0
{
1751
0
  struct tty    *tty = &c->tty;
1752
0
  struct window   *w = c->session->curw->window;
1753
0
  struct window_pane  *wp = server_client_get_pane(c), *loop;
1754
0
  struct screen   *s = NULL;
1755
0
  struct options    *oo = c->session->options;
1756
0
  int      mode = 0, cursor, flags;
1757
0
  u_int      cx = 0, cy = 0, ox, oy, sx, sy, n;
1758
0
  struct visible_ranges *r;
1759
1760
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1761
0
    return;
1762
1763
  /* Disable the block flag. */
1764
0
  flags = (tty->flags & TTY_BLOCK);
1765
0
  tty->flags &= ~TTY_BLOCK;
1766
1767
  /* Get mode from overlay if any, else from screen. */
1768
0
  if (c->overlay_draw != NULL) {
1769
0
    if (c->overlay_mode != NULL)
1770
0
      s = c->overlay_mode(c, c->overlay_data, &cx, &cy);
1771
0
  } else if (wp != NULL && c->prompt_string == NULL)
1772
0
    s = wp->screen;
1773
0
  else
1774
0
    s = c->status.active;
1775
0
  if (s != NULL)
1776
0
    mode = s->mode;
1777
0
  if (log_get_level() != 0) {
1778
0
    log_debug("%s: client %s mode %s", __func__, c->name,
1779
0
        screen_mode_to_string(mode));
1780
0
  }
1781
1782
  /* Reset region and margin. */
1783
0
  tty_region_off(tty);
1784
0
  tty_margin_off(tty);
1785
1786
  /* Move cursor to pane cursor and offset. */
1787
0
  if (c->prompt_string != NULL) {
1788
0
    n = options_get_number(oo, "status-position");
1789
0
    if (n == 0)
1790
0
      cy = status_prompt_line_at(c);
1791
0
    else {
1792
0
      n = status_line_size(c) - status_prompt_line_at(c);
1793
0
      if (n <= tty->sy)
1794
0
        cy = tty->sy - n;
1795
0
      else
1796
0
        cy = tty->sy - 1;
1797
0
    }
1798
0
    cx = c->prompt_cursor;
1799
0
  } else if (wp != NULL && c->overlay_draw == NULL) {
1800
0
    cursor = 0;
1801
0
    tty_window_offset(tty, &ox, &oy, &sx, &sy);
1802
0
    if (wp->xoff + (int)s->cx >= (int)ox &&
1803
0
        wp->xoff + (int)s->cx <= (int)ox + (int)sx &&
1804
0
        wp->yoff + (int)s->cy >= (int)oy &&
1805
0
        wp->yoff + (int)s->cy <= (int)oy + (int)sy) {
1806
0
      cursor = 1;
1807
1808
0
      cx = wp->xoff + (int)s->cx - (int)ox;
1809
0
      cy = wp->yoff + (int)s->cy - (int)oy;
1810
1811
0
      r = screen_redraw_get_visible_ranges(wp, cx, cy, 1, NULL);
1812
0
      if (!screen_redraw_is_visible(r, cx))
1813
0
        cursor = 0;
1814
1815
0
      if (status_at_line(c) == 0)
1816
0
        cy += status_line_size(c);
1817
0
    }
1818
1819
0
    if (!cursor)
1820
0
      mode &= ~MODE_CURSOR;
1821
0
  } else if (c->overlay_mode == NULL || s == NULL)
1822
0
    mode &= ~MODE_CURSOR;
1823
1824
0
  log_debug("%s: cursor to %u,%u", __func__, cx, cy);
1825
0
  tty_cursor(tty, cx, cy);
1826
1827
  /*
1828
   * Set mouse mode if requested. To support dragging, always use button
1829
   * mode. For focus-follows-mouse, we need all-motion mode to receive
1830
   * movement events.
1831
   */
1832
0
  if (options_get_number(oo, "mouse")) {
1833
0
    if (c->overlay_draw == NULL) {
1834
0
      mode &= ~ALL_MOUSE_MODES;
1835
0
      TAILQ_FOREACH(loop, &w->panes, entry) {
1836
0
        if (loop->screen->mode & MODE_MOUSE_ALL)
1837
0
          mode |= MODE_MOUSE_ALL;
1838
0
      }
1839
0
    }
1840
0
    if (options_get_number(oo, "focus-follows-mouse"))
1841
0
      mode |= MODE_MOUSE_ALL;
1842
0
    else if (~mode & MODE_MOUSE_ALL)
1843
0
      mode |= MODE_MOUSE_BUTTON;
1844
0
  }
1845
1846
  /* Clear bracketed paste mode if at the prompt. */
1847
0
  if (c->overlay_draw == NULL && c->prompt_string != NULL)
1848
0
    mode &= ~MODE_BRACKETPASTE;
1849
1850
  /* Set the terminal mode and reset attributes. */
1851
0
  tty_update_mode(tty, mode, s);
1852
0
  tty_reset(tty);
1853
1854
  /* All writing must be done, send a sync end (if it was started). */
1855
0
  tty_sync_end(tty);
1856
0
  tty->flags |= flags;
1857
0
}
1858
1859
/* Repeat time callback. */
1860
static void
1861
server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1862
0
{
1863
0
  struct client *c = data;
1864
1865
0
  if (c->flags & CLIENT_REPEAT) {
1866
0
    server_client_set_key_table(c, NULL);
1867
0
    c->flags &= ~CLIENT_REPEAT;
1868
0
    server_status_client(c);
1869
0
  }
1870
0
}
1871
1872
/* Double-click callback. */
1873
static void
1874
server_client_click_timer(__unused int fd, __unused short events, void *data)
1875
0
{
1876
0
  struct client   *c = data;
1877
0
  struct key_event  *event;
1878
1879
0
  log_debug("click timer expired");
1880
1881
0
  if (c->flags & CLIENT_TRIPLECLICK) {
1882
    /*
1883
     * Waiting for a third click that hasn't happened, so this must
1884
     * have been a double click.
1885
     */
1886
0
    event = xcalloc(1, sizeof *event);
1887
0
    event->key = KEYC_DOUBLECLICK;
1888
0
    memcpy(&event->m, &c->click_event, sizeof event->m);
1889
0
    if (!server_client_handle_key(c, event)) {
1890
0
      free(event->buf);
1891
0
      free(event);
1892
0
    }
1893
0
  }
1894
0
  c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1895
0
}
1896
1897
/* Check if client should be exited. */
1898
static void
1899
server_client_check_exit(struct client *c)
1900
0
{
1901
0
  struct client_file  *cf;
1902
0
  const char    *name = c->exit_session;
1903
0
  char      *data;
1904
0
  size_t       size, msize;
1905
1906
0
  if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1907
0
    return;
1908
0
  if (~c->flags & CLIENT_EXIT)
1909
0
    return;
1910
1911
0
  if (c->flags & CLIENT_CONTROL) {
1912
0
    control_discard(c);
1913
0
    if (!control_all_done(c))
1914
0
      return;
1915
0
  }
1916
0
  RB_FOREACH(cf, client_files, &c->files) {
1917
0
    if (EVBUFFER_LENGTH(cf->buffer) != 0)
1918
0
      return;
1919
0
  }
1920
0
  c->flags |= CLIENT_EXITED;
1921
1922
0
  switch (c->exit_type) {
1923
0
  case CLIENT_EXIT_RETURN:
1924
0
    if (c->exit_message != NULL)
1925
0
      msize = strlen(c->exit_message) + 1;
1926
0
    else
1927
0
      msize = 0;
1928
0
    size = (sizeof c->retval) + msize;
1929
0
    data = xmalloc(size);
1930
0
    memcpy(data, &c->retval, sizeof c->retval);
1931
0
    if (c->exit_message != NULL)
1932
0
      memcpy(data + sizeof c->retval, c->exit_message, msize);
1933
0
    proc_send(c->peer, MSG_EXIT, -1, data, size);
1934
0
    free(data);
1935
0
    break;
1936
0
  case CLIENT_EXIT_SHUTDOWN:
1937
0
    proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1938
0
    break;
1939
0
  case CLIENT_EXIT_DETACH:
1940
0
    proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
1941
0
    break;
1942
0
  }
1943
0
  free(c->exit_session);
1944
0
  free(c->exit_message);
1945
0
}
1946
1947
/* Redraw timer callback. */
1948
static void
1949
server_client_redraw_timer(__unused int fd, __unused short events,
1950
    __unused void *data)
1951
0
{
1952
0
  log_debug("redraw timer fired");
1953
0
}
1954
1955
/*
1956
 * Check if modes need to be updated. Only modes in the current window are
1957
 * updated and it is done when the status line is redrawn.
1958
 */
1959
static void
1960
server_client_check_modes(struct client *c)
1961
0
{
1962
0
  struct window     *w = c->session->curw->window;
1963
0
  struct window_pane    *wp;
1964
0
  struct window_mode_entry  *wme;
1965
1966
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1967
0
    return;
1968
0
  if (~c->flags & CLIENT_REDRAWSTATUS)
1969
0
    return;
1970
0
  TAILQ_FOREACH(wp, &w->panes, entry) {
1971
0
    wme = TAILQ_FIRST(&wp->modes);
1972
0
    if (wme != NULL && wme->mode->update != NULL)
1973
0
      wme->mode->update(wme);
1974
0
  }
1975
0
}
1976
1977
/* Check for client redraws. */
1978
static void
1979
server_client_check_redraw(struct client *c)
1980
0
{
1981
0
  struct session    *s = c->session;
1982
0
  struct tty    *tty = &c->tty;
1983
0
  struct window   *w = c->session->curw->window;
1984
0
  struct window_pane  *wp;
1985
0
  int      needed, tty_flags, mode = tty->mode;
1986
0
  uint64_t     client_flags = 0;
1987
0
  int      redraw_pane, redraw_scrollbar_only;
1988
0
  u_int      bit = 0;
1989
0
  struct timeval     tv = { .tv_usec = 1000 };
1990
0
  static struct event  ev;
1991
0
  size_t       left;
1992
1993
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1994
0
    return;
1995
0
  if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1996
0
    log_debug("%s: redraw%s%s%s%s%s%s", c->name,
1997
0
        (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1998
0
        (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1999
0
        (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
2000
0
        (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
2001
0
        (c->flags & CLIENT_REDRAWPANES) ? " panes" : "",
2002
0
        (c->flags & CLIENT_REDRAWSCROLLBARS) ? " scrollbars" : "");
2003
0
  }
2004
2005
  /*
2006
   * If there is outstanding data, defer the redraw until it has been
2007
   * consumed. We can just add a timer to get out of the event loop and
2008
   * end up back here.
2009
   */
2010
0
  needed = 0;
2011
0
  if (c->flags & CLIENT_ALLREDRAWFLAGS)
2012
0
    needed = 1;
2013
0
  else {
2014
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
2015
0
      if (wp->flags & PANE_REDRAW) {
2016
0
        needed = 1;
2017
0
        client_flags |= CLIENT_REDRAWPANES;
2018
0
        break;
2019
0
      }
2020
0
      if (wp->flags & PANE_REDRAWSCROLLBAR) {
2021
0
        needed = 1;
2022
0
        client_flags |= CLIENT_REDRAWSCROLLBARS;
2023
        /* no break - later panes may need redraw */
2024
0
      }
2025
0
    }
2026
0
  }
2027
0
  if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
2028
0
    log_debug("%s: redraw deferred (%zu left)", c->name, left);
2029
0
    if (!evtimer_initialized(&ev))
2030
0
      evtimer_set(&ev, server_client_redraw_timer, NULL);
2031
0
    if (!evtimer_pending(&ev, NULL)) {
2032
0
      log_debug("redraw timer started");
2033
0
      evtimer_add(&ev, &tv);
2034
0
    }
2035
2036
0
    if (~c->flags & CLIENT_REDRAWWINDOW) {
2037
0
      TAILQ_FOREACH(wp, &w->panes, entry) {
2038
0
        if (wp->flags & (PANE_REDRAW)) {
2039
0
          log_debug("%s: pane %%%u needs redraw",
2040
0
              c->name, wp->id);
2041
0
          c->redraw_panes |= (1 << bit);
2042
0
        } else if (wp->flags & PANE_REDRAWSCROLLBAR) {
2043
0
          log_debug("%s: pane %%%u scrollbar "
2044
0
              "needs redraw", c->name, wp->id);
2045
0
          c->redraw_scrollbars |= (1 << bit);
2046
0
        }
2047
0
        if (++bit == 64) {
2048
          /*
2049
           * If more that 64 panes, give up and
2050
           * just redraw the window.
2051
           */
2052
0
          client_flags &= ~(CLIENT_REDRAWPANES|
2053
0
              CLIENT_REDRAWSCROLLBARS);
2054
0
          client_flags |= CLIENT_REDRAWWINDOW;
2055
0
          break;
2056
0
        }
2057
0
      }
2058
0
      if (c->redraw_panes != 0)
2059
0
        c->flags |= CLIENT_REDRAWPANES;
2060
0
      if (c->redraw_scrollbars != 0)
2061
0
        c->flags |= CLIENT_REDRAWSCROLLBARS;
2062
0
    }
2063
0
    c->flags |= client_flags;
2064
0
    return;
2065
0
  } else if (needed)
2066
0
    log_debug("%s: redraw needed", c->name);
2067
2068
0
  tty_flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
2069
0
  tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
2070
2071
0
  if (~c->flags & CLIENT_REDRAWWINDOW) {
2072
    /*
2073
     * If not redrawing the entire window, check whether each pane
2074
     * needs to be redrawn.
2075
     */
2076
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
2077
0
      redraw_pane = 0;
2078
0
      redraw_scrollbar_only = 0;
2079
0
      if (wp->flags & PANE_REDRAW)
2080
0
        redraw_pane = 1;
2081
0
      else if (c->flags & CLIENT_REDRAWPANES) {
2082
0
        if (c->redraw_panes & (1 << bit))
2083
0
          redraw_pane = 1;
2084
0
      } else if (c->flags & CLIENT_REDRAWSCROLLBARS) {
2085
0
        if (c->redraw_scrollbars & (1 << bit))
2086
0
          redraw_scrollbar_only = 1;
2087
0
      }
2088
0
      bit++;
2089
0
      if (!redraw_pane && !redraw_scrollbar_only)
2090
0
        continue;
2091
0
      if (redraw_scrollbar_only) {
2092
0
        log_debug("%s: redrawing (scrollbar only) pane "
2093
0
            "%%%u", __func__, wp->id);
2094
0
      } else {
2095
0
        log_debug("%s: redrawing pane %%%u", __func__,
2096
0
            wp->id);
2097
0
      }
2098
0
      screen_redraw_pane(c, wp, redraw_scrollbar_only);
2099
0
    }
2100
0
    c->redraw_panes = 0;
2101
0
    c->redraw_scrollbars = 0;
2102
0
    c->flags &= ~(CLIENT_REDRAWPANES|CLIENT_REDRAWSCROLLBARS);
2103
0
  }
2104
2105
0
  if (c->flags & CLIENT_ALLREDRAWFLAGS) {
2106
0
    if (options_get_number(s->options, "set-titles")) {
2107
0
      server_client_set_title(c);
2108
0
      server_client_set_path(c);
2109
0
    }
2110
0
    server_client_set_progress_bar(c);
2111
0
    screen_redraw_screen(c);
2112
0
  }
2113
2114
0
  tty->flags = (tty->flags & ~TTY_NOCURSOR)|(tty_flags & TTY_NOCURSOR);
2115
0
  tty_update_mode(tty, mode, NULL);
2116
0
  tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|
2117
0
      tty_flags;
2118
2119
0
  c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
2120
2121
0
  if (needed) {
2122
    /*
2123
     * We would have deferred the redraw unless the output buffer
2124
     * was empty, so we can record how many bytes the redraw
2125
     * generated.
2126
     */
2127
0
    c->redraw = EVBUFFER_LENGTH(tty->out);
2128
0
    log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
2129
0
  }
2130
0
}
2131
2132
/* Set client title. */
2133
static void
2134
server_client_set_title(struct client *c)
2135
0
{
2136
0
  struct session    *s = c->session;
2137
0
  const char    *template;
2138
0
  char      *title;
2139
0
  struct format_tree  *ft;
2140
2141
0
  template = options_get_string(s->options, "set-titles-string");
2142
2143
0
  ft = format_create(c, NULL, FORMAT_NONE, 0);
2144
0
  format_defaults(ft, c, NULL, NULL, NULL);
2145
2146
0
  title = format_expand_time(ft, template);
2147
0
  if (c->title == NULL || strcmp(title, c->title) != 0) {
2148
0
    free(c->title);
2149
0
    c->title = xstrdup(title);
2150
0
    tty_set_title(&c->tty, c->title);
2151
0
  }
2152
0
  free(title);
2153
2154
0
  format_free(ft);
2155
0
}
2156
2157
/* Set client path. */
2158
static void
2159
server_client_set_path(struct client *c)
2160
0
{
2161
0
  struct session  *s = c->session;
2162
0
  const char  *path;
2163
2164
0
  if (s->curw == NULL || s->curw->window->active == NULL)
2165
0
    return;
2166
0
  if (s->curw->window->active->base.path == NULL)
2167
0
    path = "";
2168
0
  else
2169
0
    path = s->curw->window->active->base.path;
2170
0
  if (c->path == NULL || strcmp(path, c->path) != 0) {
2171
0
    free(c->path);
2172
0
    c->path = xstrdup(path);
2173
0
    tty_set_path(&c->tty, c->path);
2174
0
  }
2175
0
}
2176
2177
/* Set client progress bar. */
2178
static void
2179
server_client_set_progress_bar(struct client *c)
2180
0
{
2181
0
  struct session    *s = c->session;
2182
0
  struct progress_bar *pane_pb;
2183
2184
0
  if (s->curw == NULL || s->curw->window->active == NULL)
2185
0
    return;
2186
0
  pane_pb = &s->curw->window->active->base.progress_bar;
2187
0
  if (pane_pb->state == c->progress_bar.state &&
2188
0
      pane_pb->progress == c->progress_bar.progress)
2189
0
    return;
2190
0
  memcpy(&c->progress_bar, pane_pb, sizeof c->progress_bar);
2191
0
  tty_set_progress_bar(&c->tty, &c->progress_bar);
2192
0
}
2193
2194
/* Dispatch message from client. */
2195
static void
2196
server_client_dispatch(struct imsg *imsg, void *arg)
2197
0
{
2198
0
  struct client *c = arg;
2199
0
  ssize_t    datalen;
2200
0
  struct session  *s;
2201
2202
0
  if (c->flags & CLIENT_DEAD)
2203
0
    return;
2204
2205
0
  if (imsg == NULL) {
2206
0
    server_client_lost(c);
2207
0
    return;
2208
0
  }
2209
2210
0
  datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2211
2212
0
  switch (imsg->hdr.type) {
2213
0
  case MSG_IDENTIFY_CLIENTPID:
2214
0
  case MSG_IDENTIFY_CWD:
2215
0
  case MSG_IDENTIFY_ENVIRON:
2216
0
  case MSG_IDENTIFY_FEATURES:
2217
0
  case MSG_IDENTIFY_FLAGS:
2218
0
  case MSG_IDENTIFY_LONGFLAGS:
2219
0
  case MSG_IDENTIFY_STDIN:
2220
0
  case MSG_IDENTIFY_STDOUT:
2221
0
  case MSG_IDENTIFY_TERM:
2222
0
  case MSG_IDENTIFY_TERMINFO:
2223
0
  case MSG_IDENTIFY_TTYNAME:
2224
0
  case MSG_IDENTIFY_DONE:
2225
0
    if (server_client_dispatch_identify(c, imsg) != 0)
2226
0
      goto bad;
2227
0
    break;
2228
0
  case MSG_COMMAND:
2229
0
    if (server_client_dispatch_command(c, imsg) != 0)
2230
0
      goto bad;
2231
0
    break;
2232
0
  case MSG_RESIZE:
2233
0
    if (datalen != 0)
2234
0
      goto bad;
2235
2236
0
    if (c->flags & CLIENT_CONTROL)
2237
0
      break;
2238
0
    server_client_update_latest(c);
2239
0
    tty_resize(&c->tty);
2240
0
    tty_repeat_requests(&c->tty, 0);
2241
0
    recalculate_sizes();
2242
0
    if (c->overlay_resize == NULL)
2243
0
      server_client_clear_overlay(c);
2244
0
    else
2245
0
      c->overlay_resize(c, c->overlay_data);
2246
0
    server_redraw_client(c);
2247
0
    if (c->session != NULL)
2248
0
      notify_client("client-resized", c);
2249
0
    break;
2250
0
  case MSG_EXITING:
2251
0
    if (datalen != 0)
2252
0
      goto bad;
2253
0
    server_client_set_session(c, NULL);
2254
0
    recalculate_sizes();
2255
0
    tty_close(&c->tty);
2256
0
    proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
2257
0
    break;
2258
0
  case MSG_WAKEUP:
2259
0
  case MSG_UNLOCK:
2260
0
    if (datalen != 0)
2261
0
      goto bad;
2262
2263
0
    if (!(c->flags & CLIENT_SUSPENDED))
2264
0
      break;
2265
0
    c->flags &= ~CLIENT_SUSPENDED;
2266
2267
0
    if (c->fd == -1 || c->session == NULL) /* exited already */
2268
0
      break;
2269
0
    s = c->session;
2270
2271
0
    if (gettimeofday(&c->activity_time, NULL) != 0)
2272
0
      fatal("gettimeofday failed");
2273
2274
0
    tty_start_tty(&c->tty);
2275
0
    server_redraw_client(c);
2276
0
    recalculate_sizes();
2277
2278
0
    if (s != NULL)
2279
0
      session_update_activity(s, &c->activity_time);
2280
0
    break;
2281
0
  case MSG_SHELL:
2282
0
    if (datalen != 0)
2283
0
      goto bad;
2284
0
    if (server_client_dispatch_shell(c) != 0)
2285
0
      goto bad;
2286
0
    break;
2287
0
  case MSG_WRITE_READY:
2288
0
    if (file_write_ready(&c->files, imsg) != 0)
2289
0
      goto bad;
2290
0
    break;
2291
0
  case MSG_READ:
2292
0
    if (file_read_data(&c->files, imsg) != 0)
2293
0
      goto bad;
2294
0
    break;
2295
0
  case MSG_READ_DONE:
2296
0
    if (file_read_done(&c->files, imsg) != 0)
2297
0
      goto bad;
2298
0
    break;
2299
0
  }
2300
2301
0
  return;
2302
2303
0
bad:
2304
0
  log_debug("client %p invalid message type %d", c, imsg->hdr.type);
2305
0
  proc_kill_peer(c->peer);
2306
0
}
2307
2308
/* Callback when command is not allowed. */
2309
static enum cmd_retval
2310
server_client_read_only(struct cmdq_item *item, __unused void *data)
2311
0
{
2312
0
  cmdq_error(item, "client is read-only");
2313
0
  return (CMD_RETURN_ERROR);
2314
0
}
2315
2316
/* Callback for default command. */
2317
static enum cmd_retval
2318
server_client_default_command(struct cmdq_item *item, __unused void *data)
2319
0
{
2320
0
  struct client   *c = cmdq_get_client(item);
2321
0
  struct cmd_list   *cmdlist;
2322
0
  struct cmdq_item  *new_item;
2323
2324
0
  cmdlist = options_get_command(global_options, "default-client-command");
2325
0
  if ((c->flags & CLIENT_READONLY) &&
2326
0
      !cmd_list_all_have(cmdlist, CMD_READONLY))
2327
0
    new_item = cmdq_get_callback(server_client_read_only, NULL);
2328
0
  else
2329
0
    new_item = cmdq_get_command(cmdlist, NULL);
2330
0
  cmdq_insert_after(item, new_item);
2331
0
  return (CMD_RETURN_NORMAL);
2332
0
}
2333
2334
/* Callback when command is done. */
2335
static enum cmd_retval
2336
server_client_command_done(struct cmdq_item *item, __unused void *data)
2337
0
{
2338
0
  struct client *c = cmdq_get_client(item);
2339
2340
0
  if (~c->flags & CLIENT_ATTACHED)
2341
0
    c->flags |= CLIENT_EXIT;
2342
0
  else if (~c->flags & CLIENT_EXIT) {
2343
0
    if (c->flags & CLIENT_CONTROL)
2344
0
      control_ready(c);
2345
0
    tty_send_requests(&c->tty);
2346
0
  }
2347
0
  return (CMD_RETURN_NORMAL);
2348
0
}
2349
2350
/* Handle command message. */
2351
static int
2352
server_client_dispatch_command(struct client *c, struct imsg *imsg)
2353
0
{
2354
0
  struct msg_command    data;
2355
0
  char       *buf;
2356
0
  size_t        len;
2357
0
  int       argc = 0;
2358
0
  char      **argv, *cause;
2359
0
  struct cmd_parse_result  *pr;
2360
0
  struct args_value  *values;
2361
0
  struct cmdq_item   *new_item;
2362
2363
0
  if (c->flags & CLIENT_EXIT)
2364
0
    return (0);
2365
2366
0
  if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
2367
0
    return (-1);
2368
0
  memcpy(&data, imsg->data, sizeof data);
2369
2370
0
  buf = (char *)imsg->data + sizeof data;
2371
0
  len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
2372
0
  if (len > 0 && buf[len - 1] != '\0')
2373
0
    return (-1);
2374
2375
0
  if (cmd_unpack_argv(buf, len, data.argc, &argv) != 0) {
2376
0
    cause = xstrdup("command too long");
2377
0
    goto error;
2378
0
  }
2379
2380
0
  argc = data.argc;
2381
0
  if (argc == 0) {
2382
0
    new_item = cmdq_get_callback(server_client_default_command,
2383
0
        NULL);
2384
0
  } else {
2385
0
    values = args_from_vector(argc, argv);
2386
0
    pr = cmd_parse_from_arguments(values, argc, NULL);
2387
0
    switch (pr->status) {
2388
0
    case CMD_PARSE_ERROR:
2389
0
      cause = pr->error;
2390
0
      goto error;
2391
0
    case CMD_PARSE_SUCCESS:
2392
0
      break;
2393
0
    }
2394
0
    args_free_values(values, argc);
2395
0
    free(values);
2396
0
    cmd_free_argv(argc, argv);
2397
0
    if ((c->flags & CLIENT_READONLY) &&
2398
0
        !cmd_list_all_have(pr->cmdlist, CMD_READONLY)) {
2399
0
      new_item = cmdq_get_callback(server_client_read_only,
2400
0
          NULL);
2401
0
    } else
2402
0
      new_item = cmdq_get_command(pr->cmdlist, NULL);
2403
0
    cmd_list_free(pr->cmdlist);
2404
0
  }
2405
0
  cmdq_append(c, new_item);
2406
0
  cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
2407
2408
0
  return (0);
2409
2410
0
error:
2411
0
  cmd_free_argv(argc, argv);
2412
2413
0
  cmdq_append(c, cmdq_get_error(cause));
2414
0
  free(cause);
2415
2416
0
  c->flags |= CLIENT_EXIT;
2417
0
  return (0);
2418
0
}
2419
2420
/* Handle identify message. */
2421
static int
2422
server_client_dispatch_identify(struct client *c, struct imsg *imsg)
2423
0
{
2424
0
  const char  *data, *home;
2425
0
  size_t     datalen;
2426
0
  int    flags, feat;
2427
0
  uint64_t   longflags;
2428
0
  char    *name;
2429
2430
0
  if (c->flags & CLIENT_IDENTIFIED)
2431
0
    return (-1);
2432
2433
0
  data = imsg->data;
2434
0
  datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2435
2436
0
  switch (imsg->hdr.type) {
2437
0
  case MSG_IDENTIFY_FEATURES:
2438
0
    if (datalen != sizeof feat)
2439
0
      return (-1);
2440
0
    memcpy(&feat, data, sizeof feat);
2441
0
    c->term_features |= feat;
2442
0
    log_debug("client %p IDENTIFY_FEATURES %s", c,
2443
0
        tty_get_features(feat));
2444
0
    break;
2445
0
  case MSG_IDENTIFY_FLAGS:
2446
0
    if (datalen != sizeof flags)
2447
0
      return (-1);
2448
0
    memcpy(&flags, data, sizeof flags);
2449
0
    c->flags |= flags;
2450
0
    log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
2451
0
    break;
2452
0
  case MSG_IDENTIFY_LONGFLAGS:
2453
0
    if (datalen != sizeof longflags)
2454
0
      return (-1);
2455
0
    memcpy(&longflags, data, sizeof longflags);
2456
0
    c->flags |= longflags;
2457
0
    log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
2458
0
        (unsigned long long)longflags);
2459
0
    break;
2460
0
  case MSG_IDENTIFY_TERM:
2461
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2462
0
      return (-1);
2463
0
    c->term_name = xstrdup(data);
2464
0
    log_debug("client %p IDENTIFY_TERM %s", c, data);
2465
0
    break;
2466
0
  case MSG_IDENTIFY_TERMINFO:
2467
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2468
0
      return (-1);
2469
0
    c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
2470
0
        sizeof *c->term_caps);
2471
0
    c->term_caps[c->term_ncaps++] = xstrdup(data);
2472
0
    log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
2473
0
    break;
2474
0
  case MSG_IDENTIFY_TTYNAME:
2475
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2476
0
      return (-1);
2477
0
    c->ttyname = xstrdup(data);
2478
0
    log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
2479
0
    break;
2480
0
  case MSG_IDENTIFY_CWD:
2481
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2482
0
      return (-1);
2483
0
    if (access(data, X_OK) == 0)
2484
0
      c->cwd = xstrdup(data);
2485
0
    else if ((home = find_home()) != NULL)
2486
0
      c->cwd = xstrdup(home);
2487
0
    else
2488
0
      c->cwd = xstrdup("/");
2489
0
    log_debug("client %p IDENTIFY_CWD %s", c, data);
2490
0
    break;
2491
0
  case MSG_IDENTIFY_STDIN:
2492
0
    if (datalen != 0)
2493
0
      return (-1);
2494
0
    c->fd = imsg_get_fd(imsg);
2495
0
    log_debug("client %p IDENTIFY_STDIN %d", c, c->fd);
2496
0
    break;
2497
0
  case MSG_IDENTIFY_STDOUT:
2498
0
    if (datalen != 0)
2499
0
      return (-1);
2500
0
    c->out_fd = imsg_get_fd(imsg);
2501
0
    log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd);
2502
0
    break;
2503
0
  case MSG_IDENTIFY_ENVIRON:
2504
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2505
0
      return (-1);
2506
0
    if (strchr(data, '=') != NULL)
2507
0
      environ_put(c->environ, data, 0);
2508
0
    log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
2509
0
    break;
2510
0
  case MSG_IDENTIFY_CLIENTPID:
2511
0
    if (datalen != sizeof c->pid)
2512
0
      return (-1);
2513
0
    memcpy(&c->pid, data, sizeof c->pid);
2514
0
    log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
2515
0
    break;
2516
0
  default:
2517
0
    break;
2518
0
  }
2519
2520
0
  if (imsg->hdr.type != MSG_IDENTIFY_DONE)
2521
0
    return (0);
2522
0
  c->flags |= CLIENT_IDENTIFIED;
2523
2524
0
  if (c->term_name == NULL || *c->term_name == '\0') {
2525
0
    free(c->term_name);
2526
0
    c->term_name = xstrdup("unknown");
2527
0
  }
2528
2529
0
  if (c->ttyname != NULL && *c->ttyname != '\0')
2530
0
    name = xstrdup(c->ttyname);
2531
0
  else
2532
0
    xasprintf(&name, "client-%ld", (long)c->pid);
2533
0
  c->name = name;
2534
0
  log_debug("client %p name is %s", c, c->name);
2535
2536
#ifdef __CYGWIN__
2537
  c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
2538
  c->out_fd = dup(c->fd);
2539
#endif
2540
2541
0
  if (c->flags & CLIENT_CONTROL)
2542
0
    control_start(c);
2543
0
  else if (c->fd != -1) {
2544
0
    if (tty_init(&c->tty, c) != 0) {
2545
0
      close(c->fd);
2546
0
      c->fd = -1;
2547
0
    } else {
2548
0
      tty_resize(&c->tty);
2549
0
      c->flags |= CLIENT_TERMINAL;
2550
0
    }
2551
0
    if (c->out_fd != -1)
2552
0
      close(c->out_fd);
2553
0
    c->out_fd = -1;
2554
0
  }
2555
2556
  /* If pasting has taken too long, turn it off. */
2557
0
  if (c->flags & (CLIENT_BRACKETPASTING|CLIENT_ASSUMEPASTING) &&
2558
0
      current_time - c->paste_time > CLIENT_PASTE_TIME_LIMIT) {
2559
0
    log_debug("%s: paste time limit exceeded", c->name);
2560
0
    c->flags &= ~(CLIENT_BRACKETPASTING|CLIENT_ASSUMEPASTING);
2561
0
  }
2562
2563
  /*
2564
   * If this is the first client, load configuration files. Any later
2565
   * clients are allowed to continue with their command even if the
2566
   * config has not been loaded - they might have been run from inside it
2567
   */
2568
0
  if ((~c->flags & CLIENT_EXIT) &&
2569
0
       !cfg_finished &&
2570
0
       c == TAILQ_FIRST(&clients))
2571
0
    start_cfg();
2572
2573
0
  return (0);
2574
0
}
2575
2576
/* Handle shell message. */
2577
static int
2578
server_client_dispatch_shell(struct client *c)
2579
0
{
2580
0
  const char  *shell;
2581
2582
0
  shell = options_get_string(global_s_options, "default-shell");
2583
0
  if (!checkshell(shell))
2584
0
    shell = _PATH_BSHELL;
2585
0
  proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
2586
2587
0
  proc_kill_peer(c->peer);
2588
0
  return (0);
2589
0
}
2590
2591
/* Get client working directory. */
2592
const char *
2593
server_client_get_cwd(struct client *c, struct session *s)
2594
0
{
2595
0
  const char  *home;
2596
2597
0
  if (!cfg_finished && cfg_client != NULL)
2598
0
    return (cfg_client->cwd);
2599
0
  if (c != NULL && c->session == NULL && c->cwd != NULL)
2600
0
    return (c->cwd);
2601
0
  if (s != NULL && s->cwd != NULL)
2602
0
    return (s->cwd);
2603
0
  if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2604
0
    return (s->cwd);
2605
0
  if ((home = find_home()) != NULL)
2606
0
    return (home);
2607
0
  return ("/");
2608
0
}
2609
2610
/* Get control client flags. */
2611
static uint64_t
2612
server_client_control_flags(struct client *c, const char *next)
2613
0
{
2614
0
  if (strcmp(next, "pause-after") == 0) {
2615
0
    c->pause_age = 0;
2616
0
    return (CLIENT_CONTROL_PAUSEAFTER);
2617
0
  }
2618
0
  if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
2619
0
    c->pause_age *= 1000;
2620
0
    return (CLIENT_CONTROL_PAUSEAFTER);
2621
0
  }
2622
0
  if (strcmp(next, "no-output") == 0)
2623
0
    return (CLIENT_CONTROL_NOOUTPUT);
2624
0
  if (strcmp(next, "wait-exit") == 0)
2625
0
    return (CLIENT_CONTROL_WAITEXIT);
2626
0
  return (0);
2627
0
}
2628
2629
/* Set client flags. */
2630
void
2631
server_client_set_flags(struct client *c, const char *flags)
2632
0
{
2633
0
  char  *s, *copy, *next;
2634
0
  uint64_t flag;
2635
0
  int  not;
2636
2637
0
  s = copy = xstrdup(flags);
2638
0
  while ((next = strsep(&s, ",")) != NULL) {
2639
0
    not = (*next == '!');
2640
0
    if (not)
2641
0
      next++;
2642
2643
0
    if (c->flags & CLIENT_CONTROL)
2644
0
      flag = server_client_control_flags(c, next);
2645
0
    else
2646
0
      flag = 0;
2647
0
    if (strcmp(next, "read-only") == 0)
2648
0
      flag = CLIENT_READONLY;
2649
0
    else if (strcmp(next, "ignore-size") == 0)
2650
0
      flag = CLIENT_IGNORESIZE;
2651
0
    else if (strcmp(next, "active-pane") == 0)
2652
0
      flag = CLIENT_ACTIVEPANE;
2653
0
    else if (strcmp(next, "no-detach-on-destroy") == 0)
2654
0
      flag = CLIENT_NO_DETACH_ON_DESTROY;
2655
0
    if (flag == 0)
2656
0
      continue;
2657
2658
0
    log_debug("client %s set flag %s", c->name, next);
2659
0
    if (not) {
2660
0
      if (c->flags & CLIENT_READONLY)
2661
0
        flag &= ~CLIENT_READONLY;
2662
0
      c->flags &= ~flag;
2663
0
    } else
2664
0
      c->flags |= flag;
2665
0
    if (flag == CLIENT_CONTROL_NOOUTPUT)
2666
0
      control_reset_offsets(c);
2667
0
  }
2668
0
  free(copy);
2669
0
  proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
2670
0
}
2671
2672
/* Get client flags. This is only flags useful to show to users. */
2673
const char *
2674
server_client_get_flags(struct client *c)
2675
0
{
2676
0
  static char s[256];
2677
0
  char    tmp[32];
2678
2679
0
  *s = '\0';
2680
0
  if (c->flags & CLIENT_ATTACHED)
2681
0
    strlcat(s, "attached,", sizeof s);
2682
0
  if (c->flags & CLIENT_FOCUSED)
2683
0
    strlcat(s, "focused,", sizeof s);
2684
0
  if (c->flags & CLIENT_CONTROL)
2685
0
    strlcat(s, "control-mode,", sizeof s);
2686
0
  if (c->flags & CLIENT_IGNORESIZE)
2687
0
    strlcat(s, "ignore-size,", sizeof s);
2688
0
  if (c->flags & CLIENT_NO_DETACH_ON_DESTROY)
2689
0
    strlcat(s, "no-detach-on-destroy,", sizeof s);
2690
0
  if (c->flags & CLIENT_CONTROL_NOOUTPUT)
2691
0
    strlcat(s, "no-output,", sizeof s);
2692
0
  if (c->flags & CLIENT_CONTROL_WAITEXIT)
2693
0
    strlcat(s, "wait-exit,", sizeof s);
2694
0
  if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
2695
0
    xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
2696
0
        c->pause_age / 1000);
2697
0
    strlcat(s, tmp, sizeof s);
2698
0
  }
2699
0
  if (c->flags & CLIENT_READONLY)
2700
0
    strlcat(s, "read-only,", sizeof s);
2701
0
  if (c->flags & CLIENT_ACTIVEPANE)
2702
0
    strlcat(s, "active-pane,", sizeof s);
2703
0
  if (c->flags & CLIENT_SUSPENDED)
2704
0
    strlcat(s, "suspended,", sizeof s);
2705
0
  if (c->flags & CLIENT_UTF8)
2706
0
    strlcat(s, "UTF-8,", sizeof s);
2707
0
  if (*s != '\0')
2708
0
    s[strlen(s) - 1] = '\0';
2709
0
  return (s);
2710
0
}
2711
2712
/* Get client window. */
2713
struct client_window *
2714
server_client_get_client_window(struct client *c, u_int id)
2715
0
{
2716
0
  struct client_window  cw = { .window = id };
2717
2718
0
  return (RB_FIND(client_windows, &c->windows, &cw));
2719
0
}
2720
2721
/* Add client window. */
2722
struct client_window *
2723
server_client_add_client_window(struct client *c, u_int id)
2724
0
{
2725
0
  struct client_window  *cw;
2726
2727
0
  cw = server_client_get_client_window(c, id);
2728
0
  if (cw == NULL) {
2729
0
    cw = xcalloc(1, sizeof *cw);
2730
0
    cw->window = id;
2731
0
    RB_INSERT(client_windows, &c->windows, cw);
2732
0
  }
2733
0
  return (cw);
2734
0
}
2735
2736
/* Get client active pane. */
2737
struct window_pane *
2738
server_client_get_pane(struct client *c)
2739
0
{
2740
0
  struct session    *s = c->session;
2741
0
  struct client_window  *cw;
2742
2743
0
  if (s == NULL)
2744
0
    return (NULL);
2745
2746
0
  if (~c->flags & CLIENT_ACTIVEPANE)
2747
0
    return (s->curw->window->active);
2748
0
  cw = server_client_get_client_window(c, s->curw->window->id);
2749
0
  if (cw == NULL)
2750
0
    return (s->curw->window->active);
2751
0
  return (cw->pane);
2752
0
}
2753
2754
/* Set client active pane. */
2755
void
2756
server_client_set_pane(struct client *c, struct window_pane *wp)
2757
0
{
2758
0
  struct session    *s = c->session;
2759
0
  struct client_window  *cw;
2760
2761
0
  if (s == NULL)
2762
0
    return;
2763
2764
0
  cw = server_client_add_client_window(c, s->curw->window->id);
2765
0
  cw->pane = wp;
2766
0
  log_debug("%s pane now %%%u", c->name, wp->id);
2767
0
}
2768
2769
/* Remove pane from client lists. */
2770
void
2771
server_client_remove_pane(struct window_pane *wp)
2772
0
{
2773
0
  struct client   *c;
2774
0
  struct window   *w = wp->window;
2775
0
  struct client_window  *cw;
2776
2777
0
  TAILQ_FOREACH(c, &clients, entry) {
2778
0
    cw = server_client_get_client_window(c, w->id);
2779
0
    if (cw != NULL && cw->pane == wp) {
2780
0
      RB_REMOVE(client_windows, &c->windows, cw);
2781
0
      free(cw);
2782
0
    }
2783
0
    if (c->tty.mouse_last_pane == (int)wp->id) {
2784
0
      c->tty.mouse_last_pane = -1;
2785
0
      c->tty.mouse_drag_update = NULL;
2786
0
      c->tty.mouse_scrolling_flag = 0;
2787
0
    }
2788
0
  }
2789
0
}
2790
2791
/* Print to a client. */
2792
void
2793
server_client_print(struct client *c, int parse, struct evbuffer *evb)
2794
0
{
2795
0
  void        *data = EVBUFFER_DATA(evb);
2796
0
  size_t         size = EVBUFFER_LENGTH(evb);
2797
0
  struct window_pane    *wp;
2798
0
  struct window_mode_entry  *wme;
2799
0
  char        *sanitized, *msg, *line, empty = '\0';
2800
2801
0
  if (!parse) {
2802
0
    utf8_stravisx(&msg, data, size,
2803
0
        VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH);
2804
0
  } else {
2805
0
    if (size == 0)
2806
0
      msg = &empty;
2807
0
    else {
2808
0
      msg = EVBUFFER_DATA(evb);
2809
0
      if (msg[size - 1] != '\0')
2810
0
        evbuffer_add(evb, "", 1);
2811
0
    }
2812
0
  }
2813
0
  log_debug("%s: %s", __func__, msg);
2814
2815
0
  if (c == NULL)
2816
0
    goto out;
2817
2818
0
  if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
2819
0
    if (~c->flags & CLIENT_UTF8) {
2820
0
      sanitized = utf8_sanitize(msg);
2821
0
      if (c->flags & CLIENT_CONTROL)
2822
0
        control_write(c, "%s", sanitized);
2823
0
      else
2824
0
        file_print(c, "%s\n", sanitized);
2825
0
      free(sanitized);
2826
0
    } else {
2827
0
      if (c->flags & CLIENT_CONTROL)
2828
0
        control_write(c, "%s", msg);
2829
0
      else
2830
0
        file_print(c, "%s\n", msg);
2831
0
    }
2832
0
    goto out;
2833
0
  }
2834
2835
0
  wp = server_client_get_pane(c);
2836
0
  wme = TAILQ_FIRST(&wp->modes);
2837
0
  if (wme == NULL || wme->mode != &window_view_mode)
2838
0
    window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
2839
0
  if (parse) {
2840
0
    do {
2841
0
      line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF);
2842
0
      if (line != NULL) {
2843
0
        window_copy_add(wp, 1, "%s", line);
2844
0
        free(line);
2845
0
      }
2846
0
    } while (line != NULL);
2847
2848
0
    size = EVBUFFER_LENGTH(evb);
2849
0
    if (size != 0) {
2850
0
      line = EVBUFFER_DATA(evb);
2851
0
      window_copy_add(wp, 1, "%.*s", (int)size, line);
2852
0
    }
2853
0
  } else
2854
0
    window_copy_add(wp, 0, "%s", msg);
2855
2856
0
out:
2857
0
  if (!parse)
2858
0
    free(msg);
2859
0
}
2860
2861
static void
2862
server_client_report_theme(struct client *c, enum client_theme theme)
2863
0
{
2864
0
  if (theme == THEME_LIGHT) {
2865
0
    c->theme = THEME_LIGHT;
2866
0
    notify_client("client-light-theme", c);
2867
0
  } else {
2868
0
    c->theme = THEME_DARK;
2869
0
    notify_client("client-dark-theme", c);
2870
0
  }
2871
2872
  /*
2873
   * Request foreground and background colour again. Don't forward 2031 to
2874
   * panes until a response is received.
2875
   */
2876
0
  tty_repeat_requests(&c->tty, 1);
2877
0
}