Coverage Report

Created: 2025-08-24 07:01

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