Coverage Report

Created: 2025-10-13 07:06

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