Coverage Report

Created: 2026-09-03 06:29

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.510 2026/09/01 19:50:58 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 on a dead pane waiting for a key. */
1395
static int
1396
server_client_handle_dead_key(struct window_pane *wp, key_code key)
1397
0
{
1398
0
  if (wp == NULL ||
1399
0
      (~wp->flags & PANE_EXITED) ||
1400
0
      KEYC_IS_MOUSE(key) ||
1401
0
      KEYC_IS_PASTE(key) ||
1402
0
      options_get_number(wp->options, "remain-on-exit") != 3)
1403
0
    return (0);
1404
0
  options_set_number(wp->options, "remain-on-exit", 0);
1405
0
  server_destroy_pane(wp, 0);
1406
0
  return (1);
1407
0
}
1408
1409
/*
1410
 * Handle data key input from client. This owns and can modify the key event it
1411
 * is given and is responsible for freeing it.
1412
 */
1413
static enum cmd_retval
1414
server_client_key_callback(struct cmdq_item *item, void *data)
1415
0
{
1416
0
  struct key_event    *event = data;
1417
0
  struct client     *c, *ec = event->client;
1418
0
  key_code       key = event->key;
1419
0
  struct mouse_event    *m = &event->m;
1420
0
  struct session      *s;
1421
0
  struct winlink      *wl;
1422
0
  struct window_pane    *wp;
1423
0
  struct window_mode_entry  *wme;
1424
0
  struct timeval       tv;
1425
0
  struct key_table    *table, *first;
1426
0
  struct key_binding    *bd;
1427
0
  u_int        repeat;
1428
0
  uint64_t       flags, prefix_delay;
1429
0
  struct cmd_find_state    fs;
1430
0
  key_code       key0, prefix, prefix2;
1431
1432
0
  if (ec != NULL)
1433
0
    c = ec;
1434
0
  else
1435
0
    c = cmdq_get_client(item);
1436
0
  s = c->session;
1437
1438
  /* Check the client is good to accept input. */
1439
0
  if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1440
0
    goto out;
1441
0
  wl = s->curw;
1442
1443
  /* Update the activity timer. */
1444
0
  memcpy(&c->last_activity_time, &c->activity_time,
1445
0
      sizeof c->last_activity_time);
1446
0
  if (gettimeofday(&c->activity_time, NULL) != 0)
1447
0
    fatal("gettimeofday failed");
1448
0
  session_update_activity(s, &c->activity_time);
1449
1450
  /* Check for mouse keys. */
1451
0
  m->valid = 0;
1452
0
  if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1453
0
    if (c->flags & CLIENT_READONLY)
1454
0
      goto out;
1455
0
    key = server_client_check_mouse(c, event);
1456
0
    if (key == KEYC_UNKNOWN)
1457
0
      goto out;
1458
1459
0
    m->valid = 1;
1460
0
    m->key = key;
1461
1462
    /*
1463
     * Mouse drag is in progress, so fire the callback (now that
1464
     * the mouse event is valid).
1465
     */
1466
0
    if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1467
0
      c->tty.mouse_drag_update(c, m);
1468
0
      goto out;
1469
0
    }
1470
0
    event->key = key;
1471
0
  }
1472
1473
  /* Find affected pane. */
1474
0
  if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1475
0
    cmd_find_from_client(&fs, c, 0);
1476
0
  wp = fs.wp;
1477
1478
  /* Forward mouse keys if disabled. */
1479
0
  if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1480
0
    goto forward_key;
1481
1482
  /* Forward if bracket pasting. */
1483
0
  if (server_client_is_bracket_paste (c, key))
1484
0
    goto paste_key;
1485
1486
  /* Treat everything as a regular key when pasting is detected. */
1487
0
  if (!KEYC_IS_MOUSE(key) &&
1488
0
      key != KEYC_FOCUS_IN &&
1489
0
      key != KEYC_FOCUS_OUT &&
1490
0
      (~key & KEYC_SENT) &&
1491
0
      server_client_is_assume_paste(c))
1492
0
    goto paste_key;
1493
1494
  /* Forward keys directly if this pane is capturing all keys. */
1495
0
  if (wp != NULL &&
1496
0
      (wp->flags & PANE_CAPTUREALLKEYS) &&
1497
0
      (~wp->flags & PANE_EXITED) &&
1498
0
      !KEYC_IS_MOUSE(key) &&
1499
0
      TAILQ_EMPTY(&wp->modes))
1500
0
    goto forward_key;
1501
1502
  /* Focus events are not keys and cannot be bound. */
1503
0
  if (key == KEYC_FOCUS_IN || key == KEYC_FOCUS_OUT)
1504
0
    goto forward_key;
1505
1506
  /*
1507
   * Work out the current key table. If the pane is in a mode, use
1508
   * the mode table instead of the default key table.
1509
   */
1510
0
  if (server_client_is_default_key_table(c, c->keytable) &&
1511
0
      wp != NULL &&
1512
0
      (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1513
0
      wme->mode->key_table != NULL)
1514
0
    table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1515
0
  else
1516
0
    table = c->keytable;
1517
0
  first = table;
1518
1519
0
table_changed:
1520
  /*
1521
   * The prefix always takes precedence and forces a switch to the prefix
1522
   * table, unless we are already there.
1523
   */
1524
0
  prefix = (key_code)options_get_number(s->options, "prefix");
1525
0
  prefix2 = (key_code)options_get_number(s->options, "prefix2");
1526
0
  key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1527
0
  if ((key0 == (prefix & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) ||
1528
0
      key0 == (prefix2 & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS))) &&
1529
0
      strcmp(table->name, "prefix") != 0) {
1530
0
    server_client_set_key_table(c, "prefix");
1531
0
    server_status_client(c);
1532
0
    goto out;
1533
0
  }
1534
0
  flags = c->flags;
1535
1536
0
try_again:
1537
  /* Log key table. */
1538
0
  if (wp == NULL)
1539
0
    log_debug("key table %s (no pane)", table->name);
1540
0
  else
1541
0
    log_debug("key table %s (pane %%%u)", table->name, wp->id);
1542
0
  if (c->flags & CLIENT_REPEAT)
1543
0
    log_debug("currently repeating");
1544
1545
0
  bd = key_bindings_get(table, key0);
1546
1547
  /*
1548
   * If prefix-timeout is enabled and we're in the prefix table, see if
1549
   * the timeout has been exceeded. Revert to the root table if so.
1550
   */
1551
0
  prefix_delay = options_get_number(global_options, "prefix-timeout");
1552
0
  if (prefix_delay > 0 &&
1553
0
      strcmp(table->name, "prefix") == 0 &&
1554
0
      server_client_key_table_activity_diff(c) > prefix_delay) {
1555
    /*
1556
     * If repeating is active and this is a repeating binding,
1557
     * ignore the timeout.
1558
     */
1559
0
    if (bd != NULL &&
1560
0
        (c->flags & CLIENT_REPEAT) &&
1561
0
        (bd->flags & KEY_BINDING_REPEAT)) {
1562
0
      log_debug("prefix timeout ignored, repeat is active");
1563
0
    } else {
1564
0
      log_debug("prefix timeout exceeded");
1565
0
      server_client_set_key_table(c, NULL);
1566
0
      first = table = c->keytable;
1567
0
      server_status_client(c);
1568
0
      goto table_changed;
1569
0
    }
1570
0
  }
1571
1572
  /* Try to see if there is a key binding in the current table. */
1573
0
  if (bd != NULL) {
1574
    /*
1575
     * Key was matched in this table. If currently repeating but a
1576
     * non-repeating binding was found, stop repeating and try
1577
     * again in the root table.
1578
     */
1579
0
    if ((c->flags & CLIENT_REPEAT) &&
1580
0
        (~bd->flags & KEY_BINDING_REPEAT)) {
1581
0
      log_debug("found in key table %s (not repeating)",
1582
0
          table->name);
1583
0
      server_client_set_key_table(c, NULL);
1584
0
      first = table = c->keytable;
1585
0
      c->flags &= ~CLIENT_REPEAT;
1586
0
      server_status_client(c);
1587
0
      goto table_changed;
1588
0
    }
1589
0
    log_debug("found in key table %s", table->name);
1590
1591
    /*
1592
     * Take a reference to this table to make sure the key binding
1593
     * doesn't disappear.
1594
     */
1595
0
    table->references++;
1596
1597
    /*
1598
     * If this is a repeating key, start the timer. Otherwise reset
1599
     * the client back to the root table.
1600
     */
1601
0
    repeat = server_client_repeat_time(c, bd);
1602
0
    if (repeat != 0) {
1603
0
      c->flags |= CLIENT_REPEAT;
1604
0
      c->last_key = bd->key;
1605
1606
0
      tv.tv_sec = repeat / 1000;
1607
0
      tv.tv_usec = (repeat % 1000) * 1000L;
1608
0
      evtimer_del(&c->repeat_timer);
1609
0
      evtimer_add(&c->repeat_timer, &tv);
1610
0
    } else {
1611
0
      c->flags &= ~CLIENT_REPEAT;
1612
0
      server_client_set_key_table(c, NULL);
1613
0
    }
1614
0
    server_status_client(c);
1615
1616
    /* Execute the key binding. */
1617
0
    key_bindings_dispatch(bd, item, c, event, &fs);
1618
0
    key_bindings_unref_table(table);
1619
0
    goto out;
1620
0
  }
1621
1622
  /*
1623
   * No match, try the ANY key.
1624
   */
1625
0
  if (key0 != KEYC_ANY) {
1626
0
    key0 = KEYC_ANY;
1627
0
    goto try_again;
1628
0
  }
1629
1630
  /*
1631
   * Binding movement keys is useless since we only turn them on when the
1632
   * application requests, so don't let them exit the prefix table.
1633
   */
1634
0
  if (key == KEYC_MOUSEMOVE_PANE ||
1635
0
      key == KEYC_MOUSEMOVE_STATUS ||
1636
0
      key == KEYC_MOUSEMOVE_STATUS_LEFT ||
1637
0
      key == KEYC_MOUSEMOVE_STATUS_RIGHT ||
1638
0
      key == KEYC_MOUSEMOVE_STATUS_DEFAULT ||
1639
0
      key == KEYC_MOUSEMOVE_BORDER)
1640
0
    goto forward_key;
1641
1642
  /*
1643
   * No match in this table. If not in the root table or if repeating
1644
   * switch the client back to the root table and try again.
1645
   */
1646
0
  log_debug("not found in key table %s", table->name);
1647
0
  if (!server_client_is_default_key_table(c, table) ||
1648
0
      (c->flags & CLIENT_REPEAT)) {
1649
0
    log_debug("trying in root table");
1650
0
    server_client_set_key_table(c, NULL);
1651
0
    table = c->keytable;
1652
0
    if (c->flags & CLIENT_REPEAT)
1653
0
      first = table;
1654
0
    c->flags &= ~CLIENT_REPEAT;
1655
0
    server_status_client(c);
1656
0
    goto table_changed;
1657
0
  }
1658
1659
  /*
1660
   * No match in the root table either. If this wasn't the first table
1661
   * tried, don't pass the key to the pane.
1662
   */
1663
0
  if (first != table && (~flags & CLIENT_REPEAT)) {
1664
0
    server_client_set_key_table(c, NULL);
1665
0
    server_status_client(c);
1666
0
    goto out;
1667
0
  }
1668
1669
0
forward_key:
1670
0
  if (server_client_handle_dead_key(wp, key))
1671
0
    goto out;
1672
0
  if (c->flags & CLIENT_READONLY)
1673
0
    goto out;
1674
0
  if (wp != NULL)
1675
0
    window_pane_key(wp, c, s, wl, key, m);
1676
0
  goto out;
1677
1678
0
paste_key:
1679
0
  if (c->flags & CLIENT_READONLY)
1680
0
    goto out;
1681
0
  if (event->buf != NULL)
1682
0
    window_pane_paste(wp, key, event->buf, event->len);
1683
0
  key = KEYC_NONE;
1684
0
  goto out;
1685
1686
0
out:
1687
0
  if (s != NULL && key != KEYC_FOCUS_OUT)
1688
0
    server_client_update_latest(c);
1689
0
  if (ec != NULL)
1690
0
    server_client_unref(ec);
1691
0
  free(event->buf);
1692
0
  free(event);
1693
0
  return (CMD_RETURN_NORMAL);
1694
0
}
1695
1696
/* Handle a key event for the active window menu, if any. */
1697
static int
1698
server_client_handle_menu_key(struct client *c, struct key_event *event)
1699
0
{
1700
0
  struct window   *w = c->session->curw->window;
1701
0
  struct key_event   new_event;
1702
0
  struct mouse_event  *m;
1703
0
  u_int      ox, oy, sx, sy;
1704
1705
0
  if (w->menu == NULL)
1706
0
    return (0);
1707
1708
0
  memcpy(&new_event, event, sizeof new_event);
1709
0
  if (KEYC_IS_MOUSE(event->key)) {
1710
0
    m = &new_event.m;
1711
0
    m->statusat = status_at_line(c);
1712
0
    m->statuslines = status_line_size(c);
1713
1714
0
    tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1715
0
    m->x += ox;
1716
0
    if (m->statusat == 0) {
1717
0
      if (m->y < m->statuslines)
1718
0
        m->x = m->y = UINT_MAX;
1719
0
      else
1720
0
        m->y = m->y - m->statuslines + oy;
1721
0
    } else if (m->statusat > 0 && m->y >= (u_int)m->statusat)
1722
0
      m->x = m->y = UINT_MAX;
1723
0
    else
1724
0
      m->y += oy;
1725
0
  }
1726
1727
0
  if (menu_key(c, w->menu, &new_event) == 1)
1728
0
    menu_close(w);
1729
0
  return (1);
1730
0
}
1731
1732
/* Handle a key event. */
1733
static int
1734
server_client_handle_key0(struct client *c, struct key_event *event,
1735
    struct cmdq_item *after, struct cmdq_item **next)
1736
0
{
1737
0
  struct session    *s = c->session;
1738
0
  struct cmdq_item  *item;
1739
0
  struct window_pane  *wp;
1740
1741
  /* Check the client is good to accept input. */
1742
0
  if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1743
0
    return (0);
1744
1745
  /*
1746
   * Handle theme reporting keys before overlays so they work even when a
1747
   * popup is open.
1748
   */
1749
0
  if (event->key == KEYC_REPORT_LIGHT_THEME) {
1750
0
    server_client_report_theme(c, THEME_LIGHT);
1751
0
    return (0);
1752
0
  }
1753
0
  if (event->key == KEYC_REPORT_DARK_THEME) {
1754
0
    server_client_report_theme(c, THEME_DARK);
1755
0
    return (0);
1756
0
  }
1757
1758
  /*
1759
   * Key presses in overlay mode, for panes capturing all keys and in the
1760
   * command prompt are a special case. The queue might be blocked so they
1761
   * need to be processed immediately rather than queued.
1762
   */
1763
0
  if (~c->flags & CLIENT_READONLY) {
1764
0
    if (c->message_string != NULL) {
1765
0
      if (c->message_ignore_keys)
1766
0
        return (0);
1767
0
      status_message_clear(c);
1768
0
    }
1769
1770
0
    if (c->overlay_key != NULL) {
1771
0
      switch (c->overlay_key(c, c->overlay_data, event)) {
1772
0
      case 0:
1773
0
        return (0);
1774
0
      case 1:
1775
0
        server_client_clear_overlay(c);
1776
0
        return (0);
1777
0
      }
1778
0
    }
1779
1780
0
    server_client_clear_overlay(c);
1781
1782
0
    wp = s->curw->window->active;
1783
0
    if (wp != NULL &&
1784
0
        (wp->flags & PANE_CAPTUREALLKEYS) &&
1785
0
        TAILQ_EMPTY(&wp->modes) &&
1786
0
        !KEYC_IS_MOUSE(event->key)) {
1787
0
      if (server_client_handle_dead_key(wp, event->key))
1788
0
        return (0);
1789
0
      if (~wp->flags & PANE_EXITED) {
1790
0
        window_pane_key(wp, c, s, s->curw, event->key,
1791
0
            &event->m);
1792
0
        return (0);
1793
0
      }
1794
0
    }
1795
1796
0
    if (server_client_handle_menu_key(c, event))
1797
0
      return (0);
1798
0
    if (c->prompt != NULL) {
1799
0
      switch (status_prompt_key(c, event->key, &event->m)) {
1800
0
      case PROMPT_KEY_HANDLED:
1801
0
      case PROMPT_KEY_CLOSE:
1802
0
        return (0);
1803
0
      case PROMPT_KEY_NOT_HANDLED:
1804
0
      case PROMPT_KEY_MOVE:
1805
0
        break;
1806
0
      }
1807
0
    }
1808
1809
0
    wp = s->curw->window->active;
1810
0
    if (wp == NULL || !window_pane_has_prompt(wp)) {
1811
0
      TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1812
0
        if (window_pane_has_prompt(wp) &&
1813
0
            window_pane_is_visible(wp))
1814
0
          break;
1815
0
      }
1816
0
    }
1817
0
    if (wp != NULL &&
1818
0
        window_pane_has_prompt(wp) &&
1819
0
        window_pane_is_visible(wp)) {
1820
0
      switch (window_pane_prompt_key(wp, c, event->key,
1821
0
          &event->m)) {
1822
0
      case PROMPT_KEY_HANDLED:
1823
0
      case PROMPT_KEY_CLOSE:
1824
0
      case PROMPT_KEY_MOVE:
1825
0
        return (0);
1826
0
      case PROMPT_KEY_NOT_HANDLED:
1827
0
        if (KEYC_IS_MOUSE(event->key))
1828
0
          return (0);
1829
0
        break;
1830
0
      }
1831
0
    }
1832
0
  }
1833
1834
  /*
1835
   * Add the key to the queue so it happens after any commands queued by
1836
   * previous keys.
1837
   */
1838
0
  item = cmdq_get_callback(server_client_key_callback, event);
1839
0
  if (after != NULL) {
1840
0
    event->client = c;
1841
0
    c->references++;
1842
0
    item = cmdq_insert_after(after, item);
1843
0
    if (next != NULL)
1844
0
      *next = item;
1845
0
    return (1);
1846
0
  }
1847
0
  cmdq_append(c, item);
1848
0
  return (1);
1849
0
}
1850
1851
/* Handle key and insert at end of queue. */
1852
int
1853
server_client_handle_key(struct client *c, struct key_event *event)
1854
0
{
1855
0
  return (server_client_handle_key0(c, event, NULL, NULL));
1856
0
}
1857
1858
/* Handle key and insert after another item. */
1859
int
1860
server_client_handle_key_after(struct client *c, struct key_event *event,
1861
    struct cmdq_item *after, struct cmdq_item **next)
1862
0
{
1863
0
  return (server_client_handle_key0(c, event, after, next));
1864
0
}
1865
1866
/* Client functions that need to happen every loop. */
1867
void
1868
server_client_loop(void)
1869
0
{
1870
0
  struct client     *c;
1871
0
  struct window     *w;
1872
0
  struct window_pane    *wp;
1873
0
  struct window_mode_entry  *wme;
1874
1875
  /* Check for window resize. This is done before redrawing. */
1876
0
  RB_FOREACH(w, windows, &windows)
1877
0
    server_client_check_window_resize(w);
1878
1879
  /* Notify modes that pane styles may have changed. */
1880
0
  RB_FOREACH(w, windows, &windows) {
1881
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
1882
0
      if (wp->flags & PANE_STYLECHANGED) {
1883
0
        wme = TAILQ_FIRST(&wp->modes);
1884
0
        if (wme != NULL &&
1885
0
            wme->mode->style_changed != NULL)
1886
0
          wme->mode->style_changed(wme);
1887
0
      }
1888
0
    }
1889
0
  }
1890
1891
  /* Check clients. */
1892
0
  TAILQ_FOREACH(c, &clients, entry) {
1893
0
    server_client_check_exit(c, 0);
1894
0
    if (c->session != NULL && c->session->curw != NULL) {
1895
0
      server_client_check_modes(c);
1896
0
      server_client_check_redraw(c);
1897
0
      server_client_reset_state(c);
1898
0
    }
1899
0
  }
1900
1901
  /*
1902
   * Any windows will have been redrawn as part of clients, so clear
1903
   * their flags now.
1904
   */
1905
0
  RB_FOREACH(w, windows, &windows) {
1906
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
1907
0
      if (wp->fd != -1) {
1908
0
        server_client_check_pane_resize(wp);
1909
0
        server_client_check_pane_buffer(wp);
1910
0
      }
1911
0
      wp->flags &= ~(PANE_REDRAW|PANE_REDRAWSCROLLBAR|
1912
0
          PANE_ACTIVITY);
1913
0
    }
1914
0
    check_window_name(w);
1915
0
  }
1916
1917
  /* Send theme updates. */
1918
0
  RB_FOREACH(w, windows, &windows) {
1919
0
    TAILQ_FOREACH(wp, &w->panes, entry)
1920
0
      window_pane_send_theme_update(wp);
1921
0
  }
1922
0
}
1923
1924
/* Check if window needs to be resized. */
1925
static void
1926
server_client_check_window_resize(struct window *w)
1927
0
{
1928
0
  struct winlink  *wl;
1929
1930
0
  if (~w->flags & WINDOW_RESIZE)
1931
0
    return;
1932
1933
0
  TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1934
0
    if (wl->session->attached != 0 && wl->session->curw == wl)
1935
0
      break;
1936
0
  }
1937
0
  if (wl == NULL)
1938
0
    return;
1939
1940
0
  log_debug("%s: resizing window @%u", __func__, w->id);
1941
0
  resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
1942
0
}
1943
1944
/* Resize timer event. */
1945
static void
1946
server_client_resize_timer(__unused int fd, __unused short events, void *data)
1947
0
{
1948
0
  struct window_pane  *wp = data;
1949
1950
0
  log_debug("%s: %%%u resize timer expired", __func__, wp->id);
1951
0
  evtimer_del(&wp->resize_timer);
1952
0
}
1953
1954
/* Check if pane should be resized. */
1955
static void
1956
server_client_check_pane_resize(struct window_pane *wp)
1957
0
{
1958
0
  struct window_pane_resize *r, *first, *last;
1959
0
  struct timeval       tv = { .tv_usec = 250000 };
1960
1961
0
  if (TAILQ_EMPTY(&wp->resize_queue))
1962
0
    return;
1963
1964
0
  if (!event_initialized(&wp->resize_timer))
1965
0
    evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
1966
0
  if (evtimer_pending(&wp->resize_timer, NULL))
1967
0
    return;
1968
1969
0
  log_debug("%s: %%%u needs to be resized", __func__, wp->id);
1970
0
  TAILQ_FOREACH(r, &wp->resize_queue, entry) {
1971
0
    log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy,
1972
0
        r->sx, r->sy);
1973
0
  }
1974
1975
  /*
1976
   * There are three cases that matter:
1977
   *
1978
   * - Only one resize. It can just be applied.
1979
   *
1980
   * - Multiple resizes and the ending size is different from the
1981
   *   starting size. We can discard all resizes except the most recent.
1982
   *
1983
   * - Multiple resizes and the ending size is the same as the starting
1984
   *   size. We must resize at least twice to force the application to
1985
   *   redraw. So apply the first and leave the last on the queue for
1986
   *   next time.
1987
   */
1988
0
  first = TAILQ_FIRST(&wp->resize_queue);
1989
0
  last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes);
1990
0
  if (first == last) {
1991
    /* Only one resize. */
1992
0
    window_pane_send_resize(wp, first->sx, first->sy);
1993
0
    TAILQ_REMOVE(&wp->resize_queue, first, entry);
1994
0
    free(first);
1995
0
  } else if (last->sx != first->osx || last->sy != first->osy) {
1996
    /* Multiple resizes ending up with a different size. */
1997
0
    window_pane_send_resize(wp, last->sx, last->sy);
1998
0
    window_pane_clear_resizes(wp, NULL);
1999
0
  } else {
2000
    /*
2001
     * Multiple resizes ending up with the same size. There will
2002
     * not be more than one to the same size in succession so we
2003
     * can just use the last-but-one on the list and leave the last
2004
     * for later. We reduce the time until the next check to avoid
2005
     * a long delay between the resizes.
2006
     */
2007
0
    r = TAILQ_PREV(last, window_pane_resizes, entry);
2008
0
    window_pane_send_resize(wp, r->sx, r->sy);
2009
0
    window_pane_clear_resizes(wp, last);
2010
0
    tv.tv_usec = 10000;
2011
0
  }
2012
0
  evtimer_add(&wp->resize_timer, &tv);
2013
0
}
2014
2015
/* Check pane buffer size. */
2016
static void
2017
server_client_check_pane_buffer(struct window_pane *wp)
2018
0
{
2019
0
  struct evbuffer     *evb = wp->event->input;
2020
0
  size_t         minimum;
2021
0
  struct client     *c;
2022
0
  struct window_pane_offset *wpo;
2023
0
  int        off = 1, flag;
2024
0
  u_int        attached_clients = 0;
2025
0
  size_t         new_size;
2026
2027
  /*
2028
   * Work out the minimum used size. This is the most that can be removed
2029
   * from the buffer.
2030
   */
2031
0
  minimum = wp->offset.used;
2032
0
  if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
2033
0
    minimum = wp->pipe_offset.used;
2034
0
  TAILQ_FOREACH(c, &clients, entry) {
2035
0
    if (c->session == NULL)
2036
0
      continue;
2037
0
    attached_clients++;
2038
2039
0
    if (~c->flags & CLIENT_CONTROL) {
2040
0
      off = 0;
2041
0
      continue;
2042
0
    }
2043
0
    wpo = control_pane_offset(c, wp, &flag);
2044
0
    if (wpo == NULL) {
2045
0
      if (!flag)
2046
0
        off = 0;
2047
0
      continue;
2048
0
    }
2049
0
    if (!flag)
2050
0
      off = 0;
2051
2052
0
    window_pane_get_new_data(wp, wpo, &new_size);
2053
0
    log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
2054
0
        __func__, c->name, wpo->used - wp->base_offset, new_size,
2055
0
        wp->id);
2056
0
    if (wpo->used < minimum)
2057
0
      minimum = wpo->used;
2058
0
  }
2059
0
  if (attached_clients == 0)
2060
0
    off = 0;
2061
0
  minimum -= wp->base_offset;
2062
0
  if (minimum == 0)
2063
0
    goto out;
2064
2065
  /* Drain the buffer. */
2066
0
  log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
2067
0
      wp->id, minimum, EVBUFFER_LENGTH(evb));
2068
0
  evbuffer_drain(evb, minimum);
2069
2070
  /*
2071
   * Adjust the base offset. If it would roll over, all the offsets into
2072
   * the buffer need to be adjusted.
2073
   */
2074
0
  if (wp->base_offset > SIZE_MAX - minimum) {
2075
0
    log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
2076
0
    wp->offset.used -= wp->base_offset;
2077
0
    if (wp->pipe_fd != -1)
2078
0
      wp->pipe_offset.used -= wp->base_offset;
2079
0
    TAILQ_FOREACH(c, &clients, entry) {
2080
0
      if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
2081
0
        continue;
2082
0
      wpo = control_pane_offset(c, wp, &flag);
2083
0
      if (wpo != NULL && !flag)
2084
0
        wpo->used -= wp->base_offset;
2085
0
    }
2086
0
    wp->base_offset = minimum;
2087
0
  } else
2088
0
    wp->base_offset += minimum;
2089
2090
0
out:
2091
  /*
2092
   * If there is data remaining, and there are no clients able to consume
2093
   * it, do not read any more. This is true when there are attached
2094
   * clients, all of which are control clients which are not able to
2095
   * accept any more data.
2096
   */
2097
0
  log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
2098
0
  if (off)
2099
0
    bufferevent_disable(wp->event, EV_READ);
2100
0
  else
2101
0
    bufferevent_enable(wp->event, EV_READ);
2102
0
}
2103
2104
/* Move cursor for pane prompt. */
2105
static int
2106
server_client_prompt_cursor(struct client *c, struct window_pane *wp, int *mode,
2107
    u_int *cx, u_int *cy)
2108
0
{
2109
0
  struct tty    *tty = &c->tty;
2110
0
  struct visible_ranges *r;
2111
0
  u_int      ox, oy, sx, sy;
2112
0
  int      px, py;
2113
2114
0
  if (!window_pane_has_prompt(wp))
2115
0
    return (0);
2116
0
  *mode &= ~MODE_CURSOR;
2117
2118
0
  tty_window_offset(tty, &ox, &oy, &sx, &sy);
2119
0
  if (status_at_line(c) == 0)
2120
0
    py = wp->yoff;
2121
0
  else
2122
0
    py = wp->yoff + wp->sy - 1;
2123
0
  px = wp->xoff + wp->prompt_cx;
2124
0
  if (px < (int)ox || px > (int)(ox + sx) ||
2125
0
      py < (int)oy || py > (int)(oy + sy))
2126
0
    return (1);
2127
2128
0
  *cx = px - ox;
2129
0
  *cy = py - oy;
2130
2131
0
  r = window_visible_ranges(wp, *cx, *cy, 1, NULL);
2132
0
  if (window_position_is_visible(r, *cx)) {
2133
0
    if (status_at_line(c) == 0)
2134
0
      *cy += status_line_size(c);
2135
0
    *mode |= MODE_CURSOR;
2136
0
  }
2137
0
  return (1);
2138
0
}
2139
2140
/*
2141
 * Update cursor position and mode settings. The scroll region and attributes
2142
 * are cleared when idle (waiting for an event) as this is the most likely time
2143
 * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
2144
 * compromise between excessive resets and likelihood of an interrupt.
2145
 *
2146
 * tty_region/tty_reset/tty_update_mode already take care of not resetting
2147
 * things that are already in their default state.
2148
 */
2149
static void
2150
server_client_reset_state(struct client *c)
2151
0
{
2152
0
  struct tty    *tty = &c->tty;
2153
0
  struct window   *w = c->session->curw->window;
2154
0
  struct window_pane  *wp = w->active, *loop;
2155
0
  struct screen   *s = NULL;
2156
0
  struct options    *oo = c->session->options;
2157
0
  int      mode = 0, cursor, flags, pane_mode = 0;
2158
0
  u_int      cx = 0, cy = 0, ox, oy, sx, sy, prompt = 0;
2159
0
  u_int      sb_w;
2160
0
  struct visible_ranges *r;
2161
2162
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2163
0
    return;
2164
2165
  /* Disable the block flag. */
2166
0
  flags = (tty->flags & TTY_BLOCK);
2167
0
  tty->flags &= ~TTY_BLOCK;
2168
2169
  /* Get mode from overlay if any, else from screen. */
2170
0
  if (c->overlay_draw != NULL) {
2171
0
    if (c->overlay_mode != NULL)
2172
0
      s = c->overlay_mode(c, c->overlay_data, &cx, &cy);
2173
0
  } else if (w->menu != NULL) {
2174
0
    menu_get_cursor(w->menu, &cx, &cy);
2175
0
    s = menu_screen(w->menu);
2176
0
  } else if (wp != NULL && c->prompt == NULL)
2177
0
    s = wp->screen;
2178
0
  else
2179
0
    s = c->status.active;
2180
0
  if (s != NULL)
2181
0
    mode = s->mode;
2182
0
  if (log_get_level() != 0) {
2183
0
    log_debug("%s: client %s mode %s", __func__, c->name,
2184
0
        screen_mode_to_string(mode));
2185
0
  }
2186
2187
  /* Reset region and margin. */
2188
0
  tty_region_off(tty);
2189
0
  tty_margin_off(tty);
2190
2191
  /* Move cursor to pane cursor and offset. */
2192
0
  if (c->prompt != NULL) {
2193
0
    prompt = 1;
2194
0
    status_prompt_cursor(c, &cx, &cy);
2195
0
  } else if (wp != NULL && c->overlay_draw == NULL) {
2196
0
    if (w->menu != NULL) {
2197
0
      tty_window_offset(tty, &ox, &oy, &sx, &sy);
2198
0
      if (cx < ox || cx >= ox + sx ||
2199
0
          cy < oy || cy >= oy + sy)
2200
0
        mode &= ~MODE_CURSOR;
2201
0
      else {
2202
0
        cx -= ox;
2203
0
        cy -= oy;
2204
0
        if (status_at_line(c) == 0)
2205
0
          cy += status_line_size(c);
2206
0
      }
2207
0
      prompt = 1;
2208
0
    } else {
2209
0
      prompt = server_client_prompt_cursor(c, wp, &mode, &cx,
2210
0
          &cy);
2211
0
    }
2212
0
    if (!prompt) {
2213
0
      cursor = 0;
2214
0
      pane_mode = wp->base.mode;
2215
2216
0
      tty_window_offset(tty, &ox, &oy, &sx, &sy);
2217
0
      if (wp->xoff + (int)s->cx >= (int)ox &&
2218
0
          wp->xoff + (int)s->cx <= (int)ox + (int)sx &&
2219
0
          wp->yoff + (int)s->cy >= (int)oy &&
2220
0
          wp->yoff + (int)s->cy <= (int)oy + (int)sy) {
2221
0
        cursor = 1;
2222
2223
0
        cx = wp->xoff + (int)s->cx - (int)ox;
2224
0
        cy = wp->yoff + (int)s->cy - (int)oy;
2225
2226
0
        r = window_visible_ranges(wp, cx, cy, 1, NULL);
2227
0
        if (!window_position_is_visible(r, cx))
2228
0
          cursor = 0;
2229
2230
0
        if (window_pane_scrollbar_overlay_visible(wp)) {
2231
0
          sb_w = wp->scrollbar_style.width;
2232
0
          if (sb_w > wp->sx)
2233
0
            sb_w = wp->sx;
2234
0
          if (sb_w != 0 &&
2235
0
              w->sb_pos == PANE_SCROLLBARS_LEFT) {
2236
0
            if (s->cx < sb_w)
2237
0
              cursor = 0;
2238
0
          } else if (sb_w != 0 &&
2239
0
              s->cx >= wp->sx - sb_w)
2240
0
            cursor = 0;
2241
0
        }
2242
2243
0
        if (status_at_line(c) == 0)
2244
0
          cy += status_line_size(c);
2245
0
      }
2246
2247
0
      if (!cursor)
2248
0
        mode &= ~MODE_CURSOR;
2249
0
    }
2250
0
  } else if (c->overlay_mode == NULL || s == NULL)
2251
0
    mode &= ~MODE_CURSOR;
2252
0
  if (~pane_mode & MODE_SYNC) {
2253
0
    log_debug("%s: cursor to %u,%u", __func__, cx, cy);
2254
0
    tty_cursor(tty, cx, cy);
2255
0
  } else {
2256
0
    mode &= ~CURSOR_MODES;
2257
0
    mode |= tty->mode & CURSOR_MODES;
2258
0
    s = NULL;
2259
0
  }
2260
2261
  /*
2262
   * Set mouse mode if requested. To support dragging, always use button
2263
   * mode. For focus-follows-mouse, we need all-motion mode to receive
2264
   * movement events.
2265
   */
2266
0
  if (options_get_number(oo, "mouse")) {
2267
0
    if (c->overlay_draw == NULL && w->menu == NULL) {
2268
0
      mode &= ~ALL_MOUSE_MODES;
2269
0
      TAILQ_FOREACH(loop, &w->panes, entry) {
2270
0
        if (loop->screen->mode & MODE_MOUSE_ALL)
2271
0
          mode |= MODE_MOUSE_ALL;
2272
0
      }
2273
0
    }
2274
0
    if (options_get_number(oo, "focus-follows-mouse") ||
2275
0
        w->sb == PANE_SCROLLBARS_MODAL ||
2276
0
        w->sb == PANE_SCROLLBARS_AUTOHIDE)
2277
0
      mode |= MODE_MOUSE_ALL;
2278
0
    else if (~mode & MODE_MOUSE_ALL)
2279
0
      mode |= MODE_MOUSE_BUTTON;
2280
0
  }
2281
2282
  /* Clear bracketed paste mode if at the prompt. */
2283
0
  if (c->overlay_draw == NULL && prompt)
2284
0
    mode &= ~MODE_BRACKETPASTE;
2285
2286
  /* Set the terminal mode and reset attributes. */
2287
0
  tty_update_mode(tty, mode, s);
2288
0
  tty_reset(tty);
2289
2290
  /* All writing must be done, send a sync end (if it was started). */
2291
0
  tty_sync_end(tty);
2292
0
  tty->flags |= flags;
2293
0
}
2294
2295
/* Repeat time callback. */
2296
static void
2297
server_client_repeat_timer(__unused int fd, __unused short events, void *data)
2298
0
{
2299
0
  struct client *c = data;
2300
2301
0
  if (c->flags & CLIENT_REPEAT) {
2302
0
    server_client_set_key_table(c, NULL);
2303
0
    c->flags &= ~CLIENT_REPEAT;
2304
0
    server_status_client(c);
2305
0
  }
2306
0
}
2307
2308
/* Double-click callback. */
2309
static void
2310
server_client_click_timer(__unused int fd, __unused short events, void *data)
2311
0
{
2312
0
  struct client   *c = data;
2313
0
  struct key_event  *event;
2314
2315
0
  log_debug("click timer expired");
2316
2317
0
  if (c->flags & CLIENT_TRIPLECLICK) {
2318
    /*
2319
     * Waiting for a third click that hasn't happened, so this must
2320
     * have been a double click.
2321
     */
2322
0
    event = xcalloc(1, sizeof *event);
2323
0
    event->key = KEYC_DOUBLECLICK;
2324
0
    memcpy(&event->m, &c->click_event, sizeof event->m);
2325
0
    if (!server_client_handle_key(c, event)) {
2326
0
      free(event->buf);
2327
0
      free(event);
2328
0
    }
2329
0
  }
2330
0
  c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
2331
0
}
2332
2333
/* Start client exit timer. */
2334
static void
2335
server_client_start_exit_timer(struct client *c)
2336
0
{
2337
0
  struct timeval  tv = { .tv_sec = 10 };
2338
2339
0
  if (!evtimer_pending(&c->exit_timer, NULL))
2340
0
    evtimer_add(&c->exit_timer, &tv);
2341
0
}
2342
2343
/* Exit timer has expired: stop waiting for the client. */
2344
static void
2345
server_client_exit_timer(__unused int fd, __unused short events, void *data)
2346
0
{
2347
0
  struct client *c = data;
2348
2349
0
  if (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED))
2350
0
    return;
2351
2352
0
  if (c->flags & CLIENT_EXITED) {
2353
0
    log_debug("%s: %s took too long to exit", __func__, c->name);
2354
0
    server_client_lost(c);
2355
0
  } else if (c->flags & CLIENT_EXIT) {
2356
0
    log_debug("%s: %s took too long to flush", __func__, c->name);
2357
0
    server_client_check_exit(c, 1);
2358
0
  }
2359
0
}
2360
2361
/* Check if client should be exited, abandoning buffered output if forced. */
2362
static void
2363
server_client_check_exit(struct client *c, int force)
2364
0
{
2365
0
  struct client_file  *cf;
2366
0
  const char    *name = c->exit_session;
2367
0
  char      *data;
2368
0
  size_t       size, msize;
2369
2370
0
  if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
2371
0
    return;
2372
0
  if (~c->flags & CLIENT_EXIT)
2373
0
    return;
2374
2375
0
  if (c->flags & CLIENT_CONTROL) {
2376
0
    if (force)
2377
0
      control_discard_all(c);
2378
0
    else {
2379
0
      control_discard(c);
2380
0
      if (!control_all_done(c)) {
2381
0
        server_client_start_exit_timer(c);
2382
0
        return;
2383
0
      }
2384
0
    }
2385
0
  }
2386
0
  if (!force) {
2387
0
    RB_FOREACH(cf, client_files, &c->files) {
2388
0
      if (EVBUFFER_LENGTH(cf->buffer) != 0) {
2389
0
        server_client_start_exit_timer(c);
2390
0
        return;
2391
0
      }
2392
0
    }
2393
0
  }
2394
0
  c->flags |= CLIENT_EXITED;
2395
2396
0
  evtimer_del(&c->exit_timer);
2397
0
  server_client_start_exit_timer(c);
2398
2399
0
  switch (c->exit_type) {
2400
0
  case CLIENT_EXIT_RETURN:
2401
0
    if (c->exit_message != NULL)
2402
0
      msize = strlen(c->exit_message) + 1;
2403
0
    else
2404
0
      msize = 0;
2405
0
    size = (sizeof c->retval) + msize;
2406
0
    data = xmalloc(size);
2407
0
    memcpy(data, &c->retval, sizeof c->retval);
2408
0
    if (c->exit_message != NULL)
2409
0
      memcpy(data + sizeof c->retval, c->exit_message, msize);
2410
0
    proc_send(c->peer, MSG_EXIT, -1, data, size);
2411
0
    free(data);
2412
0
    break;
2413
0
  case CLIENT_EXIT_SHUTDOWN:
2414
0
    proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
2415
0
    break;
2416
0
  case CLIENT_EXIT_DETACH:
2417
0
    proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
2418
0
    break;
2419
0
  }
2420
0
}
2421
2422
/* Redraw timer callback. */
2423
static void
2424
server_client_redraw_timer(__unused int fd, __unused short events,
2425
    __unused void *data)
2426
0
{
2427
0
  log_debug("redraw timer fired");
2428
0
}
2429
2430
/*
2431
 * Check if modes need to be updated. Only modes in the current window are
2432
 * updated and it is done when the status line is redrawn.
2433
 */
2434
static void
2435
server_client_check_modes(struct client *c)
2436
0
{
2437
0
  struct window     *w = c->session->curw->window;
2438
0
  struct window_pane    *wp;
2439
0
  struct window_mode_entry  *wme;
2440
2441
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2442
0
    return;
2443
0
  if (~c->flags & CLIENT_REDRAWSTATUS)
2444
0
    return;
2445
0
  TAILQ_FOREACH(wp, &w->panes, entry) {
2446
0
    wme = TAILQ_FIRST(&wp->modes);
2447
0
    if (wme != NULL && wme->mode->update != NULL)
2448
0
      wme->mode->update(wme);
2449
0
  }
2450
0
}
2451
2452
/* Check if any panes need to be redrawn. */
2453
static int
2454
server_client_any_pane_redraw(struct client *c)
2455
0
{
2456
0
  struct session    *s = c->session;
2457
0
  struct window   *w = s->curw->window;
2458
0
  struct window_pane  *wp;
2459
2460
0
  if (c->flags & CLIENT_REDRAWWINDOW)
2461
0
    return (1);
2462
0
  TAILQ_FOREACH(wp, &w->panes, entry) {
2463
0
    if (wp->flags & (PANE_REDRAW|PANE_REDRAWSCROLLBAR))
2464
0
      return (1);
2465
0
  }
2466
0
  return (0);
2467
0
}
2468
2469
/* Check for client redraws. */
2470
static void
2471
server_client_check_redraw(struct client *c)
2472
0
{
2473
0
  struct session    *s = c->session;
2474
0
  struct tty    *tty = &c->tty;
2475
0
  struct window   *w = s->curw->window;
2476
0
  struct window_pane  *wp;
2477
0
  int      needed, tflags, mode = tty->mode;
2478
0
  struct timeval     tv = { .tv_usec = 1000 };
2479
0
  static struct event  ev;
2480
0
  size_t       n;
2481
2482
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
2483
0
    return;
2484
0
  if (c->flags & CLIENT_ALLREDRAWFLAGS) {
2485
0
    log_debug("%s: redraw%s%s%s%s%s", c->name,
2486
0
        (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
2487
0
        (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
2488
0
        (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
2489
0
        (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
2490
0
        (c->flags & CLIENT_REDRAWMENU) ? " menu" : "");
2491
0
  }
2492
2493
  /* Work out if a redraw is actually needed. */
2494
0
  needed = 0;
2495
0
  if (c->flags & (CLIENT_ALLREDRAWFLAGS|CLIENT_REDRAWSCROLLBARS))
2496
0
    needed = 1;
2497
0
  else if (server_client_any_pane_redraw(c))
2498
0
    needed = 1;
2499
0
  if (!needed) {
2500
0
    c->flags &= ~CLIENT_STATUSFORCE;
2501
0
    return;
2502
0
  }
2503
2504
  /*
2505
   * If there is outstanding data, defer the redraw until it has been
2506
   * consumed. We can just add a timer to get out of the event loop and
2507
   * end up back here.
2508
   */
2509
0
  n = EVBUFFER_LENGTH(tty->out);
2510
0
  if (n != 0 || (tty->flags & TTY_BLOCK)) {
2511
0
    if (n != 0)
2512
0
      log_debug("%s: redraw deferred (%zu left)", c->name, n);
2513
0
    else
2514
0
      log_debug("%s: redraw deferred (blocked)", c->name);
2515
0
    if (!evtimer_initialized(&ev))
2516
0
      evtimer_set(&ev, server_client_redraw_timer, NULL);
2517
0
    if (!evtimer_pending(&ev, NULL)) {
2518
0
      log_debug("redraw timer started");
2519
0
      evtimer_add(&ev, &tv);
2520
0
    }
2521
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
2522
0
      if (wp->flags & PANE_REDRAW) {
2523
0
        c->flags |= CLIENT_REDRAWWINDOW;
2524
0
        break;
2525
0
      }
2526
0
      if (wp->flags & PANE_REDRAWSCROLLBAR)
2527
0
        c->flags |= CLIENT_REDRAWSCROLLBARS;
2528
0
    }
2529
0
    return;
2530
0
  }
2531
2532
  /* Unfreeze the tty and turn off the cursor. */
2533
0
  log_debug("%s: redraw needed", c->name);
2534
0
  tflags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
2535
0
  tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
2536
2537
  /*
2538
   * If not redrawing the entire window, check whether each pane needs to
2539
   * be redrawn.
2540
   */
2541
0
  if (~c->flags & CLIENT_REDRAWWINDOW) {
2542
0
    TAILQ_FOREACH(wp, &w->panes, entry) {
2543
0
      if (wp->flags & PANE_REDRAW) {
2544
0
        log_debug("%s: redraw pane %%%u", __func__,
2545
0
            wp->id);
2546
0
        redraw_pane(c, wp);
2547
0
      } else if ((wp->flags & PANE_REDRAWSCROLLBAR) ||
2548
0
          (c->flags & CLIENT_REDRAWSCROLLBARS)) {
2549
0
        log_debug("%s: redraw scrollbar %%%u", __func__,
2550
0
            wp->id);
2551
0
        redraw_pane_scrollbar(c, wp);
2552
0
      }
2553
0
    }
2554
0
  }
2555
2556
  /*
2557
   * Set titles etc and do the redraw if there are redraw flags (and we
2558
   * aren't here just to redraw panes).
2559
   */
2560
0
  if (c->flags & CLIENT_ALLREDRAWFLAGS) {
2561
0
    if (options_get_number(s->options, "set-titles")) {
2562
0
      server_client_set_title(c);
2563
0
      server_client_set_path(c);
2564
0
    }
2565
0
    server_client_set_progress_bar(c);
2566
0
    redraw_screen(c);
2567
0
  }
2568
2569
  /* Put the tty back how it was. */
2570
0
  tty->flags = (tty->flags & ~TTY_NOCURSOR)|(tflags & TTY_NOCURSOR);
2571
0
  tty_update_mode(tty, mode, NULL);
2572
0
  tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|tflags;
2573
2574
  /*
2575
   * All the redraw flags can now be cleared. Also record how many bytes
2576
   * were written.
2577
   */
2578
0
  c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_REDRAWSCROLLBARS|
2579
0
      CLIENT_STATUSFORCE);
2580
0
  c->redraw = EVBUFFER_LENGTH(tty->out);
2581
0
  log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
2582
0
}
2583
2584
/* Set client title. */
2585
static void
2586
server_client_set_title(struct client *c)
2587
0
{
2588
0
  struct session    *s = c->session;
2589
0
  const char    *template;
2590
0
  char      *title;
2591
0
  struct format_tree  *ft;
2592
2593
0
  template = options_get_string(s->options, "set-titles-string");
2594
2595
0
  ft = format_create(c, NULL, FORMAT_NONE, 0);
2596
0
  format_defaults(ft, c, NULL, NULL, NULL);
2597
2598
0
  title = format_expand_time(ft, template);
2599
0
  if (c->title == NULL || strcmp(title, c->title) != 0) {
2600
0
    free(c->title);
2601
0
    c->title = xstrdup(title);
2602
0
    tty_set_title(&c->tty, c->title);
2603
0
  }
2604
0
  free(title);
2605
2606
0
  format_free(ft);
2607
0
}
2608
2609
/* Set client path. */
2610
static void
2611
server_client_set_path(struct client *c)
2612
0
{
2613
0
  struct session  *s = c->session;
2614
0
  const char  *path;
2615
2616
0
  if (s->curw == NULL || s->curw->window->active == NULL)
2617
0
    return;
2618
0
  if (s->curw->window->active->base.path == NULL)
2619
0
    path = "";
2620
0
  else
2621
0
    path = s->curw->window->active->base.path;
2622
0
  if (c->path == NULL || strcmp(path, c->path) != 0) {
2623
0
    free(c->path);
2624
0
    c->path = xstrdup(path);
2625
0
    tty_set_path(&c->tty, c->path);
2626
0
  }
2627
0
}
2628
2629
/* Set client progress bar. */
2630
static void
2631
server_client_set_progress_bar(struct client *c)
2632
0
{
2633
0
  struct session    *s = c->session;
2634
0
  struct progress_bar *pane_pb;
2635
2636
0
  if (s->curw == NULL || s->curw->window->active == NULL)
2637
0
    return;
2638
0
  pane_pb = &s->curw->window->active->base.progress_bar;
2639
0
  if (pane_pb->state == c->progress_bar.state &&
2640
0
      pane_pb->progress == c->progress_bar.progress)
2641
0
    return;
2642
0
  memcpy(&c->progress_bar, pane_pb, sizeof c->progress_bar);
2643
0
  tty_set_progress_bar(&c->tty, &c->progress_bar);
2644
0
}
2645
2646
/* Dispatch message from client. */
2647
static void
2648
server_client_dispatch(struct imsg *imsg, void *arg)
2649
0
{
2650
0
  struct client *c = arg;
2651
0
  ssize_t    datalen;
2652
0
  struct session  *s;
2653
0
  u_int    old_sx, old_sy;
2654
2655
0
  if (c->flags & CLIENT_DEAD)
2656
0
    return;
2657
2658
0
  if (imsg == NULL) {
2659
0
    server_client_lost(c);
2660
0
    return;
2661
0
  }
2662
2663
0
  datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2664
2665
0
  switch (imsg->hdr.type) {
2666
0
  case MSG_IDENTIFY_CLIENTPID:
2667
0
  case MSG_IDENTIFY_CWD:
2668
0
  case MSG_IDENTIFY_ENVIRON:
2669
0
  case MSG_IDENTIFY_FEATURES:
2670
0
  case MSG_IDENTIFY_FLAGS:
2671
0
  case MSG_IDENTIFY_LONGFLAGS:
2672
0
  case MSG_IDENTIFY_STDIN:
2673
0
  case MSG_IDENTIFY_STDOUT:
2674
0
  case MSG_IDENTIFY_TERM:
2675
0
  case MSG_IDENTIFY_TERMINFO:
2676
0
  case MSG_IDENTIFY_TTYNAME:
2677
0
  case MSG_IDENTIFY_DONE:
2678
0
    if (server_client_dispatch_identify(c, imsg) != 0)
2679
0
      goto bad;
2680
0
    break;
2681
0
  case MSG_COMMAND:
2682
0
    if (server_client_dispatch_command(c, imsg) != 0)
2683
0
      goto bad;
2684
0
    break;
2685
0
  case MSG_RESIZE:
2686
0
    if (datalen != 0)
2687
0
      goto bad;
2688
2689
0
    if (c->flags & CLIENT_CONTROL)
2690
0
      break;
2691
0
    server_client_update_latest(c);
2692
0
    old_sx = c->tty.sx;
2693
0
    old_sy = c->tty.sy;
2694
0
    tty_resize(&c->tty);
2695
0
    tty_repeat_requests(&c->tty, 0);
2696
0
    recalculate_sizes();
2697
0
    if (c->overlay_resize == NULL)
2698
0
      server_client_clear_overlay(c);
2699
0
    else
2700
0
      c->overlay_resize(c, c->overlay_data);
2701
0
    server_redraw_client(c);
2702
0
    if (c->session != NULL)
2703
0
      server_client_fire_resized(c, old_sx, old_sy);
2704
0
    break;
2705
0
  case MSG_EXITING:
2706
0
    if (datalen != 0)
2707
0
      goto bad;
2708
0
    server_client_set_session(c, NULL);
2709
0
    recalculate_sizes();
2710
0
    tty_close(&c->tty);
2711
0
    proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
2712
0
    break;
2713
0
  case MSG_WAKEUP:
2714
0
  case MSG_UNLOCK:
2715
0
    if (datalen != 0)
2716
0
      goto bad;
2717
2718
0
    if (!(c->flags & CLIENT_SUSPENDED))
2719
0
      break;
2720
0
    c->flags &= ~CLIENT_SUSPENDED;
2721
2722
0
    if (c->fd == -1 || c->session == NULL) /* exited already */
2723
0
      break;
2724
0
    s = c->session;
2725
2726
0
    if (gettimeofday(&c->activity_time, NULL) != 0)
2727
0
      fatal("gettimeofday failed");
2728
2729
0
    tty_start_tty(&c->tty);
2730
0
    server_redraw_client(c);
2731
0
    recalculate_sizes();
2732
2733
0
    if (s != NULL)
2734
0
      session_update_activity(s, &c->activity_time);
2735
0
    break;
2736
0
  case MSG_SHELL:
2737
0
    if (datalen != 0)
2738
0
      goto bad;
2739
0
    if (server_client_dispatch_shell(c) != 0)
2740
0
      goto bad;
2741
0
    break;
2742
0
  case MSG_WRITE_READY:
2743
0
    if (file_write_ready(&c->files, imsg) != 0)
2744
0
      goto bad;
2745
0
    break;
2746
0
  case MSG_WRITE_DONE:
2747
0
    if (file_write_done(&c->files, imsg) != 0)
2748
0
      goto bad;
2749
0
    break;
2750
0
  case MSG_READ:
2751
0
    if (file_read_data(&c->files, imsg) != 0)
2752
0
      goto bad;
2753
0
    break;
2754
0
  case MSG_READ_DONE:
2755
0
    if (file_read_done(&c->files, imsg) != 0)
2756
0
      goto bad;
2757
0
    break;
2758
0
  }
2759
2760
0
  return;
2761
2762
0
bad:
2763
0
  log_debug("client %p invalid message type %d", c, imsg->hdr.type);
2764
0
  proc_kill_peer(c->peer);
2765
0
}
2766
2767
/* Callback when command is not allowed. */
2768
static enum cmd_retval
2769
server_client_read_only(struct cmdq_item *item, __unused void *data)
2770
0
{
2771
0
  cmdq_error(item, "client is read-only");
2772
0
  return (CMD_RETURN_ERROR);
2773
0
}
2774
2775
/* Callback for default command. */
2776
static enum cmd_retval
2777
server_client_default_command(struct cmdq_item *item, __unused void *data)
2778
0
{
2779
0
  struct client   *c = cmdq_get_client(item);
2780
0
  struct cmd_list   *cmdlist;
2781
0
  struct cmdq_item  *new_item;
2782
2783
0
  cmdlist = options_get_command(global_options, "default-client-command");
2784
0
  if ((c->flags & CLIENT_READONLY) &&
2785
0
      !cmd_list_all_have(cmdlist, CMD_READONLY))
2786
0
    new_item = cmdq_get_callback(server_client_read_only, NULL);
2787
0
  else
2788
0
    new_item = cmdq_get_command(cmdlist, NULL);
2789
0
  cmdq_insert_after(item, new_item);
2790
0
  return (CMD_RETURN_NORMAL);
2791
0
}
2792
2793
/* Callback when command is done. */
2794
static enum cmd_retval
2795
server_client_command_done(struct cmdq_item *item, __unused void *data)
2796
0
{
2797
0
  struct client *c = cmdq_get_client(item);
2798
2799
0
  if (~c->flags & CLIENT_ATTACHED)
2800
0
    c->flags |= CLIENT_EXIT;
2801
0
  else if (~c->flags & CLIENT_EXIT) {
2802
0
    if (c->flags & CLIENT_CONTROL)
2803
0
      control_ready(c);
2804
0
    tty_send_requests(&c->tty);
2805
0
  }
2806
0
  return (CMD_RETURN_NORMAL);
2807
0
}
2808
2809
/* Handle command message. */
2810
static int
2811
server_client_dispatch_command(struct client *c, struct imsg *imsg)
2812
0
{
2813
0
  struct msg_command    data;
2814
0
  char       *buf;
2815
0
  size_t        len;
2816
0
  int       argc = 0;
2817
0
  char      **argv, *cause;
2818
0
  struct cmd_parse_result  *pr;
2819
0
  struct args_value  *values;
2820
0
  struct cmdq_item   *new_item;
2821
2822
0
  if (c->flags & CLIENT_EXIT)
2823
0
    return (0);
2824
2825
0
  if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
2826
0
    return (-1);
2827
0
  memcpy(&data, imsg->data, sizeof data);
2828
2829
0
  buf = (char *)imsg->data + sizeof data;
2830
0
  len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
2831
0
  if (len > 0 && buf[len - 1] != '\0')
2832
0
    return (-1);
2833
2834
0
  if (cmd_unpack_argv(buf, len, data.argc, &argv) != 0) {
2835
0
    cause = xstrdup("command too long");
2836
0
    goto error;
2837
0
  }
2838
2839
0
  argc = data.argc;
2840
0
  if (argc == 0) {
2841
0
    new_item = cmdq_get_callback(server_client_default_command,
2842
0
        NULL);
2843
0
  } else {
2844
0
    values = args_from_vector(argc, argv);
2845
0
    pr = cmd_parse_from_arguments(values, argc, NULL);
2846
0
    switch (pr->status) {
2847
0
    case CMD_PARSE_ERROR:
2848
0
      cause = pr->error;
2849
0
      goto error;
2850
0
    case CMD_PARSE_SUCCESS:
2851
0
      break;
2852
0
    }
2853
0
    args_free_values(values, argc);
2854
0
    free(values);
2855
0
    cmd_free_argv(argc, argv);
2856
0
    if ((c->flags & CLIENT_READONLY) &&
2857
0
        !cmd_list_all_have(pr->cmdlist, CMD_READONLY)) {
2858
0
      new_item = cmdq_get_callback(server_client_read_only,
2859
0
          NULL);
2860
0
    } else
2861
0
      new_item = cmdq_get_command(pr->cmdlist, NULL);
2862
0
    cmd_list_free(pr->cmdlist);
2863
0
  }
2864
0
  cmdq_append(c, new_item);
2865
0
  cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
2866
2867
0
  return (0);
2868
2869
0
error:
2870
0
  cmd_free_argv(argc, argv);
2871
2872
0
  cmdq_append(c, cmdq_get_error(cause));
2873
0
  free(cause);
2874
2875
0
  c->flags |= CLIENT_EXIT;
2876
0
  return (0);
2877
0
}
2878
2879
/* Handle identify message. */
2880
static int
2881
server_client_dispatch_identify(struct client *c, struct imsg *imsg)
2882
0
{
2883
0
  const char  *data, *home;
2884
0
  size_t     datalen;
2885
0
  int    flags, feat;
2886
0
  uint64_t   longflags;
2887
0
  char    *name;
2888
2889
0
  if (c->flags & CLIENT_IDENTIFIED)
2890
0
    return (-1);
2891
2892
0
  data = imsg->data;
2893
0
  datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2894
2895
0
  switch (imsg->hdr.type) {
2896
0
  case MSG_IDENTIFY_FEATURES:
2897
0
    if (datalen != sizeof feat)
2898
0
      return (-1);
2899
0
    memcpy(&feat, data, sizeof feat);
2900
0
    c->term_features |= feat;
2901
0
    log_debug("client %p IDENTIFY_FEATURES %s", c,
2902
0
        tty_get_features(feat));
2903
0
    break;
2904
0
  case MSG_IDENTIFY_FLAGS:
2905
0
    if (datalen != sizeof flags)
2906
0
      return (-1);
2907
0
    memcpy(&flags, data, sizeof flags);
2908
0
    c->flags |= flags;
2909
0
    log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
2910
0
    break;
2911
0
  case MSG_IDENTIFY_LONGFLAGS:
2912
0
    if (datalen != sizeof longflags)
2913
0
      return (-1);
2914
0
    memcpy(&longflags, data, sizeof longflags);
2915
0
    c->flags |= longflags;
2916
0
    log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
2917
0
        (unsigned long long)longflags);
2918
0
    break;
2919
0
  case MSG_IDENTIFY_TERM:
2920
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2921
0
      return (-1);
2922
0
    c->term_name = xstrdup(data);
2923
0
    log_debug("client %p IDENTIFY_TERM %s", c, data);
2924
0
    break;
2925
0
  case MSG_IDENTIFY_TERMINFO:
2926
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2927
0
      return (-1);
2928
0
    c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
2929
0
        sizeof *c->term_caps);
2930
0
    c->term_caps[c->term_ncaps++] = xstrdup(data);
2931
0
    log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
2932
0
    break;
2933
0
  case MSG_IDENTIFY_TTYNAME:
2934
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2935
0
      return (-1);
2936
0
    c->ttyname = xstrdup(data);
2937
0
    log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
2938
0
    break;
2939
0
  case MSG_IDENTIFY_CWD:
2940
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2941
0
      return (-1);
2942
0
    if (access(data, X_OK) == 0)
2943
0
      c->cwd = xstrdup(data);
2944
0
    else if ((home = find_home()) != NULL)
2945
0
      c->cwd = xstrdup(home);
2946
0
    else
2947
0
      c->cwd = xstrdup("/");
2948
0
    log_debug("client %p IDENTIFY_CWD %s", c, data);
2949
0
    break;
2950
0
  case MSG_IDENTIFY_STDIN:
2951
0
    if (datalen != 0)
2952
0
      return (-1);
2953
0
    c->fd = imsg_get_fd(imsg);
2954
0
    log_debug("client %p IDENTIFY_STDIN %d", c, c->fd);
2955
0
    break;
2956
0
  case MSG_IDENTIFY_STDOUT:
2957
0
    if (datalen != 0)
2958
0
      return (-1);
2959
0
    c->out_fd = imsg_get_fd(imsg);
2960
0
    log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd);
2961
0
    break;
2962
0
  case MSG_IDENTIFY_ENVIRON:
2963
0
    if (datalen == 0 || data[datalen - 1] != '\0')
2964
0
      return (-1);
2965
0
    if (strchr(data, '=') != NULL)
2966
0
      environ_put(c->environ, data, 0);
2967
0
    log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
2968
0
    break;
2969
0
  case MSG_IDENTIFY_CLIENTPID:
2970
0
    if (datalen != sizeof c->pid)
2971
0
      return (-1);
2972
0
    memcpy(&c->pid, data, sizeof c->pid);
2973
0
    log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
2974
0
    break;
2975
0
  default:
2976
0
    break;
2977
0
  }
2978
2979
0
  if (imsg->hdr.type != MSG_IDENTIFY_DONE)
2980
0
    return (0);
2981
0
  c->flags |= CLIENT_IDENTIFIED;
2982
2983
0
  if (c->term_name == NULL || *c->term_name == '\0') {
2984
0
    free(c->term_name);
2985
0
    c->term_name = xstrdup("unknown");
2986
0
  }
2987
2988
0
  if (c->ttyname != NULL && *c->ttyname != '\0')
2989
0
    name = xstrdup(c->ttyname);
2990
0
  else
2991
0
    xasprintf(&name, "client-%ld", (long)c->pid);
2992
0
  c->name = name;
2993
0
  log_debug("client %p name is %s", c, c->name);
2994
2995
#ifdef __CYGWIN__
2996
  c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
2997
  c->out_fd = dup(c->fd);
2998
#endif
2999
3000
0
  if (c->flags & CLIENT_CONTROL)
3001
0
    control_start(c);
3002
0
  else if (c->fd != -1) {
3003
0
    if (tty_init(&c->tty, c) != 0) {
3004
0
      close(c->fd);
3005
0
      c->fd = -1;
3006
0
    } else {
3007
0
      tty_resize(&c->tty);
3008
0
      c->flags |= CLIENT_TERMINAL;
3009
0
    }
3010
0
    if (c->out_fd != -1)
3011
0
      close(c->out_fd);
3012
0
    c->out_fd = -1;
3013
0
  }
3014
0
  if (c->flags & (CLIENT_CONTROL|CLIENT_TERMINAL))
3015
0
    events_fire_client("client-created", c);
3016
3017
  /* If pasting has taken too long, turn it off. */
3018
0
  if (c->flags & (CLIENT_BRACKETPASTING|CLIENT_ASSUMEPASTING) &&
3019
0
      current_time - c->paste_time > CLIENT_PASTE_TIME_LIMIT) {
3020
0
    log_debug("%s: paste time limit exceeded", c->name);
3021
0
    c->flags &= ~(CLIENT_BRACKETPASTING|CLIENT_ASSUMEPASTING);
3022
0
  }
3023
3024
  /*
3025
   * If this is the first client, load configuration files. Any later
3026
   * clients are allowed to continue with their command even if the
3027
   * config has not been loaded - they might have been run from inside it
3028
   */
3029
0
  if ((~c->flags & CLIENT_EXIT) &&
3030
0
       !cfg_finished &&
3031
0
       c == TAILQ_FIRST(&clients))
3032
0
    start_cfg();
3033
3034
0
  return (0);
3035
0
}
3036
3037
/* Handle shell message. */
3038
static int
3039
server_client_dispatch_shell(struct client *c)
3040
0
{
3041
0
  const char  *shell;
3042
3043
0
  shell = options_get_string(global_s_options, "default-shell");
3044
0
  if (!checkshell(shell))
3045
0
    shell = _PATH_BSHELL;
3046
0
  proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
3047
3048
0
  proc_kill_peer(c->peer);
3049
0
  return (0);
3050
0
}
3051
3052
/* Get client working directory. */
3053
const char *
3054
server_client_get_cwd(struct client *c, struct session *s)
3055
0
{
3056
0
  const char  *home;
3057
3058
0
  if (!cfg_finished && cfg_client != NULL)
3059
0
    return (cfg_client->cwd);
3060
0
  if (c != NULL && c->session == NULL && c->cwd != NULL)
3061
0
    return (c->cwd);
3062
0
  if (s != NULL && s->cwd != NULL)
3063
0
    return (s->cwd);
3064
0
  if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
3065
0
    return (s->cwd);
3066
0
  if ((home = find_home()) != NULL)
3067
0
    return (home);
3068
0
  return ("/");
3069
0
}
3070
3071
/* Get control client flags. */
3072
static uint64_t
3073
server_client_control_flags(struct client *c, const char *next)
3074
0
{
3075
0
  if (strcmp(next, "pause-after") == 0) {
3076
0
    c->pause_age = 0;
3077
0
    return (CLIENT_CONTROL_PAUSEAFTER);
3078
0
  }
3079
0
  if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
3080
0
    c->pause_age *= 1000;
3081
0
    return (CLIENT_CONTROL_PAUSEAFTER);
3082
0
  }
3083
0
  if (strcmp(next, "no-output") == 0)
3084
0
    return (CLIENT_CONTROL_NOOUTPUT);
3085
0
  if (strcmp(next, "wait-exit") == 0)
3086
0
    return (CLIENT_CONTROL_WAITEXIT);
3087
0
  return (0);
3088
0
}
3089
3090
/* Set client flags. */
3091
void
3092
server_client_set_flags(struct client *c, const char *flags)
3093
0
{
3094
0
  char  *s, *copy, *next;
3095
0
  uint64_t flag;
3096
0
  int  not;
3097
3098
0
  s = copy = xstrdup(flags);
3099
0
  while ((next = strsep(&s, ",")) != NULL) {
3100
0
    not = (*next == '!');
3101
0
    if (not)
3102
0
      next++;
3103
3104
0
    if (c->flags & CLIENT_CONTROL)
3105
0
      flag = server_client_control_flags(c, next);
3106
0
    else
3107
0
      flag = 0;
3108
0
    if (strcmp(next, "read-only") == 0)
3109
0
      flag = CLIENT_READONLY;
3110
0
    else if (strcmp(next, "ignore-size") == 0)
3111
0
      flag = CLIENT_IGNORESIZE;
3112
0
    else if (strcmp(next, "no-detach-on-destroy") == 0)
3113
0
      flag = CLIENT_NO_DETACH_ON_DESTROY;
3114
0
    if (flag == 0)
3115
0
      continue;
3116
3117
0
    log_debug("client %s set flag %s", c->name, next);
3118
0
    if (not) {
3119
0
      if (c->flags & CLIENT_READONLY)
3120
0
        flag &= ~CLIENT_READONLY;
3121
0
      c->flags &= ~flag;
3122
0
    } else
3123
0
      c->flags |= flag;
3124
0
    if (flag == CLIENT_CONTROL_NOOUTPUT)
3125
0
      control_reset_offsets(c);
3126
0
  }
3127
0
  free(copy);
3128
0
  proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
3129
0
}
3130
3131
/* Get client flags. This is only flags useful to show to users. */
3132
const char *
3133
server_client_get_flags(struct client *c)
3134
0
{
3135
0
  static char s[256];
3136
0
  char    tmp[32];
3137
3138
0
  *s = '\0';
3139
0
  if (c->flags & CLIENT_ATTACHED)
3140
0
    strlcat(s, "attached,", sizeof s);
3141
0
  if (c->flags & CLIENT_FOCUSED)
3142
0
    strlcat(s, "focused,", sizeof s);
3143
0
  if (c->flags & CLIENT_CONTROL)
3144
0
    strlcat(s, "control-mode,", sizeof s);
3145
0
  if (c->flags & CLIENT_IGNORESIZE)
3146
0
    strlcat(s, "ignore-size,", sizeof s);
3147
0
  if (c->flags & CLIENT_NO_DETACH_ON_DESTROY)
3148
0
    strlcat(s, "no-detach-on-destroy,", sizeof s);
3149
0
  if (c->flags & CLIENT_CONTROL_NOOUTPUT)
3150
0
    strlcat(s, "no-output,", sizeof s);
3151
0
  if (c->flags & CLIENT_CONTROL_WAITEXIT)
3152
0
    strlcat(s, "wait-exit,", sizeof s);
3153
0
  if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
3154
0
    xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
3155
0
        c->pause_age / 1000);
3156
0
    strlcat(s, tmp, sizeof s);
3157
0
  }
3158
0
  if (c->flags & CLIENT_READONLY)
3159
0
    strlcat(s, "read-only,", sizeof s);
3160
0
  if (c->flags & CLIENT_SUSPENDED)
3161
0
    strlcat(s, "suspended,", sizeof s);
3162
0
  if (c->flags & CLIENT_UTF8)
3163
0
    strlcat(s, "UTF-8,", sizeof s);
3164
0
  if (*s != '\0')
3165
0
    s[strlen(s) - 1] = '\0';
3166
0
  return (s);
3167
0
}
3168
3169
/* Remove pane from client state. */
3170
void
3171
server_client_remove_pane(struct window_pane *wp)
3172
0
{
3173
0
  struct client     *c;
3174
3175
0
  TAILQ_FOREACH(c, &clients, entry) {
3176
0
    if (c->tty.mouse_last_pane == (int)wp->id) {
3177
0
      c->tty.mouse_last_pane = -1;
3178
0
      c->tty.mouse_drag_update = NULL;
3179
0
      c->tty.mouse_scrolling_flag = 0;
3180
0
    }
3181
0
  }
3182
0
}
3183
3184
/* Print to a client. */
3185
void
3186
server_client_print(struct client *c, int parse, struct evbuffer *evb)
3187
0
{
3188
0
  void        *data = EVBUFFER_DATA(evb);
3189
0
  size_t         size = EVBUFFER_LENGTH(evb);
3190
0
  struct window_pane    *wp;
3191
0
  struct window_mode_entry  *wme;
3192
0
  char        *sanitized, *msg, *line, empty = '\0';
3193
3194
0
  if (!parse) {
3195
0
    utf8_stravisx(&msg, data, size,
3196
0
        VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH);
3197
0
  } else {
3198
0
    if (size == 0)
3199
0
      msg = &empty;
3200
0
    else {
3201
0
      msg = EVBUFFER_DATA(evb);
3202
0
      if (msg[size - 1] != '\0')
3203
0
        evbuffer_add(evb, "", 1);
3204
0
    }
3205
0
  }
3206
0
  log_debug("%s: %s", __func__, msg);
3207
3208
0
  if (c == NULL)
3209
0
    goto out;
3210
3211
0
  if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
3212
0
    if (~c->flags & CLIENT_UTF8) {
3213
0
      sanitized = utf8_sanitize(msg);
3214
0
      if (c->flags & CLIENT_CONTROL)
3215
0
        control_write(c, "%s", sanitized);
3216
0
      else
3217
0
        file_print(c, "%s\n", sanitized);
3218
0
      free(sanitized);
3219
0
    } else {
3220
0
      if (c->flags & CLIENT_CONTROL)
3221
0
        control_write(c, "%s", msg);
3222
0
      else
3223
0
        file_print(c, "%s\n", msg);
3224
0
    }
3225
0
    goto out;
3226
0
  }
3227
3228
0
  wp = c->session->curw->window->active;
3229
0
  wme = TAILQ_FIRST(&wp->modes);
3230
0
  if (wme == NULL || wme->mode != &window_view_mode)
3231
0
    window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL,
3232
0
        NULL);
3233
0
  if (parse) {
3234
0
    do {
3235
0
      line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF);
3236
0
      if (line != NULL) {
3237
0
        window_copy_add(wp, 1, "%s", line);
3238
0
        free(line);
3239
0
      }
3240
0
    } while (line != NULL);
3241
3242
0
    size = EVBUFFER_LENGTH(evb);
3243
0
    if (size != 0) {
3244
0
      line = EVBUFFER_DATA(evb);
3245
0
      window_copy_add(wp, 1, "%.*s", (int)size, line);
3246
0
    }
3247
0
  } else
3248
0
    window_copy_add(wp, 0, "%s", msg);
3249
3250
0
out:
3251
0
  if (!parse)
3252
0
    free(msg);
3253
0
}
3254
3255
static void
3256
server_client_report_theme(struct client *c, enum client_theme theme)
3257
0
{
3258
0
  enum client_theme  old = c->theme;
3259
3260
0
  if (theme == THEME_LIGHT) {
3261
0
    c->theme = THEME_LIGHT;
3262
0
    events_fire_client("client-light-theme", c);
3263
0
  } else {
3264
0
    c->theme = THEME_DARK;
3265
0
    events_fire_client("client-dark-theme", c);
3266
0
  }
3267
3268
  /*
3269
   * If the theme has changed, update the theme colours and redraw the
3270
   * client.
3271
   */
3272
0
  if (c->theme != old) {
3273
0
    server_client_update_theme_colours(c);
3274
0
    if (c->tty.flags & TTY_OPENED)
3275
0
      tty_invalidate(&c->tty);
3276
0
    server_redraw_client(c);
3277
0
  }
3278
3279
  /*
3280
   * Request foreground and background colour again. Don't forward 2031 to
3281
   * panes until a response is received.
3282
   */
3283
0
  tty_repeat_requests(&c->tty, 1);
3284
0
}