Coverage Report

Created: 2026-09-13 06:05

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