Coverage Report

Created: 2026-07-16 06:53

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