Coverage Report

Created: 2026-03-12 06:53

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