Coverage Report

Created: 2024-09-08 06:17

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