Coverage Report

Created: 2024-02-25 06:30

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