Annotation of src/usr.bin/tmux/server-client.c, Revision 1.373
1.373 ! nicm 1: /* $OpenBSD: server-client.c,v 1.372 2021/04/12 09:36:12 nicm Exp $ */
1.1 nicm 2:
3: /*
1.181 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.92 nicm 20: #include <sys/ioctl.h>
1.162 nicm 21: #include <sys/uio.h>
1.1 nicm 22:
1.114 benno 23: #include <errno.h>
1.12 nicm 24: #include <event.h>
1.1 nicm 25: #include <fcntl.h>
1.162 nicm 26: #include <imsg.h>
1.98 nicm 27: #include <paths.h>
28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
1.4 nicm 30: #include <time.h>
1.1 nicm 31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
1.190 nicm 35: static void server_client_free(int, short, void *);
1.344 nicm 36: static void server_client_check_pane_focus(struct window_pane *);
37: static void server_client_check_pane_resize(struct window_pane *);
1.345 nicm 38: static void server_client_check_pane_buffer(struct window_pane *);
1.344 nicm 39: static void server_client_check_window_resize(struct window *);
1.276 nicm 40: static key_code server_client_check_mouse(struct client *, struct key_event *);
1.190 nicm 41: static void server_client_repeat_timer(int, short, void *);
1.192 nicm 42: static void server_client_click_timer(int, short, void *);
1.190 nicm 43: static void server_client_check_exit(struct client *);
44: static void server_client_check_redraw(struct client *);
1.366 nicm 45: static void server_client_check_modes(struct client *);
1.190 nicm 46: static void server_client_set_title(struct client *);
47: static void server_client_reset_state(struct client *);
48: static int server_client_assume_paste(struct session *);
1.373 ! nicm 49: static void server_client_update_latest(struct client *);
1.190 nicm 50:
51: static void server_client_dispatch(struct imsg *, void *);
52: static void server_client_dispatch_command(struct client *, struct imsg *);
53: static void server_client_dispatch_identify(struct client *, struct imsg *);
54: static void server_client_dispatch_shell(struct client *);
1.233 nicm 55:
1.341 nicm 56: /* Compare client windows. */
57: static int
58: server_client_window_cmp(struct client_window *cw1,
59: struct client_window *cw2)
60: {
61: if (cw1->window < cw2->window)
62: return (-1);
63: if (cw1->window > cw2->window)
64: return (1);
65: return (0);
66: }
67: RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp);
68:
1.233 nicm 69: /* Number of attached clients. */
70: u_int
71: server_client_how_many(void)
72: {
73: struct client *c;
74: u_int n;
75:
76: n = 0;
77: TAILQ_FOREACH(c, &clients, entry) {
1.352 nicm 78: if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS))
1.233 nicm 79: n++;
80: }
81: return (n);
82: }
1.140 nicm 83:
1.281 nicm 84: /* Overlay timer callback. */
1.214 nicm 85: static void
1.281 nicm 86: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214 nicm 87: {
1.281 nicm 88: server_client_clear_overlay(data);
1.214 nicm 89: }
90:
1.281 nicm 91: /* Set an overlay on client. */
1.214 nicm 92: void
1.309 nicm 93: server_client_set_overlay(struct client *c, u_int delay,
94: overlay_check_cb checkcb, overlay_mode_cb modecb,
95: overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb,
96: void *data)
1.214 nicm 97: {
98: struct timeval tv;
99:
1.282 nicm 100: if (c->overlay_draw != NULL)
101: server_client_clear_overlay(c);
102:
1.214 nicm 103: tv.tv_sec = delay / 1000;
104: tv.tv_usec = (delay % 1000) * 1000L;
105:
1.281 nicm 106: if (event_initialized(&c->overlay_timer))
107: evtimer_del(&c->overlay_timer);
108: evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242 nicm 109: if (delay != 0)
1.281 nicm 110: evtimer_add(&c->overlay_timer, &tv);
111:
1.309 nicm 112: c->overlay_check = checkcb;
113: c->overlay_mode = modecb;
1.281 nicm 114: c->overlay_draw = drawcb;
115: c->overlay_key = keycb;
116: c->overlay_free = freecb;
117: c->overlay_data = data;
1.214 nicm 118:
1.309 nicm 119: c->tty.flags |= TTY_FREEZE;
120: if (c->overlay_mode == NULL)
121: c->tty.flags |= TTY_NOCURSOR;
1.214 nicm 122: server_redraw_client(c);
123: }
124:
1.281 nicm 125: /* Clear overlay mode on client. */
1.309 nicm 126: void
1.281 nicm 127: server_client_clear_overlay(struct client *c)
1.214 nicm 128: {
1.281 nicm 129: if (c->overlay_draw == NULL)
1.214 nicm 130: return;
131:
1.281 nicm 132: if (event_initialized(&c->overlay_timer))
133: evtimer_del(&c->overlay_timer);
134:
135: if (c->overlay_free != NULL)
136: c->overlay_free(c);
137:
1.309 nicm 138: c->overlay_check = NULL;
139: c->overlay_mode = NULL;
1.281 nicm 140: c->overlay_draw = NULL;
141: c->overlay_key = NULL;
1.309 nicm 142: c->overlay_free = NULL;
143: c->overlay_data = NULL;
1.214 nicm 144:
145: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
146: server_redraw_client(c);
147: }
148:
1.140 nicm 149: /* Check if this client is inside this server. */
150: int
151: server_client_check_nested(struct client *c)
152: {
153: struct environ_entry *envent;
154: struct window_pane *wp;
155:
1.164 nicm 156: envent = environ_find(c->environ, "TMUX");
1.140 nicm 157: if (envent == NULL || *envent->value == '\0')
158: return (0);
159:
160: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 161: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 162: return (1);
163: }
164: return (0);
165: }
1.1 nicm 166:
1.132 nicm 167: /* Set client key table. */
168: void
1.178 nicm 169: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 170: {
1.178 nicm 171: if (name == NULL)
172: name = server_client_get_key_table(c);
173:
1.132 nicm 174: key_bindings_unref_table(c->keytable);
175: c->keytable = key_bindings_get_table(name, 1);
176: c->keytable->references++;
177: }
178:
1.178 nicm 179: /* Get default key table. */
180: const char *
181: server_client_get_key_table(struct client *c)
182: {
183: struct session *s = c->session;
184: const char *name;
185:
186: if (s == NULL)
187: return ("root");
188:
189: name = options_get_string(s->options, "key-table");
190: if (*name == '\0')
191: return ("root");
192: return (name);
193: }
194:
1.223 nicm 195: /* Is this table the default key table? */
196: static int
197: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191 nicm 198: {
1.223 nicm 199: return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191 nicm 200: }
201:
1.1 nicm 202: /* Create a new client. */
1.246 nicm 203: struct client *
1.1 nicm 204: server_client_create(int fd)
205: {
206: struct client *c;
207:
1.49 nicm 208: setblocking(fd, 0);
1.1 nicm 209:
210: c = xcalloc(1, sizeof *c);
1.141 nicm 211: c->references = 1;
1.162 nicm 212: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 213:
1.10 nicm 214: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 215: fatal("gettimeofday failed");
1.11 nicm 216: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 217:
1.164 nicm 218: c->environ = environ_create();
1.1 nicm 219:
1.146 nicm 220: c->fd = -1;
1.351 nicm 221: c->out_fd = -1;
1.145 nicm 222:
1.316 nicm 223: c->queue = cmdq_new();
1.341 nicm 224: RB_INIT(&c->windows);
1.345 nicm 225: RB_INIT(&c->files);
1.94 nicm 226:
1.1 nicm 227: c->tty.sx = 80;
228: c->tty.sy = 24;
229:
1.271 nicm 230: status_init(c);
1.93 nicm 231: c->flags |= CLIENT_FOCUSED;
232:
1.132 nicm 233: c->keytable = key_bindings_get_table("root", 1);
234: c->keytable->references++;
235:
1.17 nicm 236: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 237: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 238:
1.135 nicm 239: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 240: log_debug("new client %p", c);
1.246 nicm 241: return (c);
1.72 nicm 242: }
243:
244: /* Open client terminal if needed. */
245: int
1.119 nicm 246: server_client_open(struct client *c, char **cause)
1.72 nicm 247: {
1.339 nicm 248: const char *ttynam = _PATH_TTY;
249:
1.75 nicm 250: if (c->flags & CLIENT_CONTROL)
251: return (0);
1.120 nicm 252:
1.339 nicm 253: if (strcmp(c->ttyname, ttynam) == 0||
254: ((isatty(STDIN_FILENO) &&
255: (ttynam = ttyname(STDIN_FILENO)) != NULL &&
256: strcmp(c->ttyname, ttynam) == 0) ||
257: (isatty(STDOUT_FILENO) &&
258: (ttynam = ttyname(STDOUT_FILENO)) != NULL &&
259: strcmp(c->ttyname, ttynam) == 0) ||
260: (isatty(STDERR_FILENO) &&
261: (ttynam = ttyname(STDERR_FILENO)) != NULL &&
262: strcmp(c->ttyname, ttynam) == 0))) {
263: xasprintf(cause, "can't use %s", c->ttyname);
1.120 nicm 264: return (-1);
265: }
1.75 nicm 266:
1.72 nicm 267: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 268: *cause = xstrdup("not a terminal");
1.72 nicm 269: return (-1);
270: }
271:
1.119 nicm 272: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 273: return (-1);
274:
275: return (0);
1.1 nicm 276: }
277:
1.373 ! nicm 278: /* Lost an attached client. */
! 279: static void
! 280: server_client_attached_lost(struct client *c)
! 281: {
! 282: struct session *s = c->session;
! 283: struct window *w;
! 284: struct client *loop;
! 285: struct client *found;
! 286:
! 287: log_debug("lost attached client %p", c);
! 288:
! 289: /*
! 290: * By this point the session in the client has been cleared so walk all
! 291: * windows to find any with this client as the latest.
! 292: */
! 293: RB_FOREACH(w, windows, &windows) {
! 294: if (w->latest != c)
! 295: continue;
! 296:
! 297: found = NULL;
! 298: TAILQ_FOREACH(loop, &clients, entry) {
! 299: s = loop->session;
! 300: if (loop == c || s == NULL || s->curw->window != w)
! 301: continue;
! 302: if (found == NULL ||
! 303: timercmp(&loop->activity_time, &found->activity_time,
! 304: >))
! 305: found = loop;
! 306: }
! 307: if (found != NULL)
! 308: server_client_update_latest(found);
! 309: }
! 310: }
! 311:
1.1 nicm 312: /* Lost a client. */
313: void
314: server_client_lost(struct client *c)
315: {
1.340 nicm 316: struct client_file *cf, *cf1;
1.341 nicm 317: struct client_window *cw, *cw1;
1.1 nicm 318:
1.141 nicm 319: c->flags |= CLIENT_DEAD;
320:
1.281 nicm 321: server_client_clear_overlay(c);
1.141 nicm 322: status_prompt_clear(c);
323: status_message_clear(c);
324:
1.340 nicm 325: RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) {
1.300 nicm 326: cf->error = EINTR;
327: file_fire_done(cf);
328: }
1.341 nicm 329: RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) {
330: RB_REMOVE(client_windows, &c->windows, cw);
331: free(cw);
332: }
1.141 nicm 333:
1.135 nicm 334: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 335: log_debug("lost client %p", c);
1.1 nicm 336:
1.373 ! nicm 337: if (c->flags & CLIENT_ATTACHED) {
! 338: server_client_attached_lost(c);
1.371 nicm 339: notify_client("client-detached", c);
1.373 ! nicm 340: }
1.371 nicm 341:
1.349 nicm 342: if (c->flags & CLIENT_CONTROL)
343: control_stop(c);
1.1 nicm 344: if (c->flags & CLIENT_TERMINAL)
345: tty_free(&c->tty);
1.109 nicm 346: free(c->ttyname);
1.333 nicm 347:
1.329 nicm 348: free(c->term_name);
1.333 nicm 349: free(c->term_type);
1.370 nicm 350: tty_term_free_list(c->term_caps, c->term_ncaps);
1.1 nicm 351:
1.270 nicm 352: status_free(c);
1.1 nicm 353:
1.77 nicm 354: free(c->title);
1.165 nicm 355: free((void *)c->cwd);
1.1 nicm 356:
1.17 nicm 357: evtimer_del(&c->repeat_timer);
1.192 nicm 358: evtimer_del(&c->click_timer);
1.17 nicm 359:
1.132 nicm 360: key_bindings_unref_table(c->keytable);
361:
1.77 nicm 362: free(c->message_string);
1.116 nicm 363: if (event_initialized(&c->message_timer))
1.69 nicm 364: evtimer_del(&c->message_timer);
1.1 nicm 365:
1.259 nicm 366: free(c->prompt_saved);
1.77 nicm 367: free(c->prompt_string);
368: free(c->prompt_buffer);
1.61 nicm 369:
1.228 nicm 370: format_lost_client(c);
1.164 nicm 371: environ_free(c->environ);
1.1 nicm 372:
1.162 nicm 373: proc_remove_peer(c->peer);
374: c->peer = NULL;
1.13 nicm 375:
1.351 nicm 376: if (c->out_fd != -1)
377: close(c->out_fd);
1.348 nicm 378: if (c->fd != -1) {
379: close(c->fd);
380: c->fd = -1;
381: }
1.142 nicm 382: server_client_unref(c);
1.71 nicm 383:
384: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 385:
386: recalculate_sizes();
1.41 nicm 387: server_check_unattached();
1.19 nicm 388: server_update_socket();
1.141 nicm 389: }
390:
391: /* Remove reference from a client. */
392: void
1.142 nicm 393: server_client_unref(struct client *c)
1.141 nicm 394: {
1.157 nicm 395: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 396:
397: c->references--;
398: if (c->references == 0)
399: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
400: }
401:
402: /* Free dead client. */
1.190 nicm 403: static void
1.170 nicm 404: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 405: {
406: struct client *c = arg;
407:
1.157 nicm 408: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 409:
1.316 nicm 410: cmdq_free(c->queue);
1.194 nicm 411:
1.216 nicm 412: if (c->references == 0) {
413: free((void *)c->name);
1.141 nicm 414: free(c);
1.216 nicm 415: }
1.9 nicm 416: }
417:
1.220 nicm 418: /* Suspend a client. */
419: void
420: server_client_suspend(struct client *c)
421: {
1.232 nicm 422: struct session *s = c->session;
1.220 nicm 423:
1.352 nicm 424: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.220 nicm 425: return;
426:
427: tty_stop_tty(&c->tty);
428: c->flags |= CLIENT_SUSPENDED;
429: proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
430: }
431:
1.174 nicm 432: /* Detach a client. */
433: void
434: server_client_detach(struct client *c, enum msgtype msgtype)
435: {
1.232 nicm 436: struct session *s = c->session;
1.174 nicm 437:
1.352 nicm 438: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.174 nicm 439: return;
440:
1.352 nicm 441: c->flags |= CLIENT_EXIT;
442:
443: c->exit_type = CLIENT_EXIT_DETACH;
444: c->exit_msgtype = msgtype;
445: c->exit_session = xstrdup(s->name);
1.207 nicm 446: }
447:
1.208 nicm 448: /* Execute command to replace a client. */
1.207 nicm 449: void
450: server_client_exec(struct client *c, const char *cmd)
451: {
452: struct session *s = c->session;
1.208 nicm 453: char *msg;
454: const char *shell;
1.207 nicm 455: size_t cmdsize, shellsize;
456:
457: if (*cmd == '\0')
458: return;
459: cmdsize = strlen(cmd) + 1;
460:
461: if (s != NULL)
462: shell = options_get_string(s->options, "default-shell");
463: else
464: shell = options_get_string(global_s_options, "default-shell");
1.308 nicm 465: if (!checkshell(shell))
466: shell = _PATH_BSHELL;
1.207 nicm 467: shellsize = strlen(shell) + 1;
468:
469: msg = xmalloc(cmdsize + shellsize);
470: memcpy(msg, cmd, cmdsize);
471: memcpy(msg + cmdsize, shell, shellsize);
472:
473: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
474: free(msg);
1.174 nicm 475: }
476:
1.66 nicm 477: /* Check for mouse keys. */
1.190 nicm 478: static key_code
1.276 nicm 479: server_client_check_mouse(struct client *c, struct key_event *event)
1.66 nicm 480: {
1.276 nicm 481: struct mouse_event *m = &event->m;
1.192 nicm 482: struct session *s = c->session;
1.272 nicm 483: struct winlink *wl;
1.192 nicm 484: struct window_pane *wp;
1.261 nicm 485: u_int x, y, b, sx, sy, px, py;
1.307 nicm 486: int ignore = 0;
1.192 nicm 487: key_code key;
488: struct timeval tv;
1.272 nicm 489: struct style_range *sr;
1.283 nicm 490: enum { NOTYPE,
491: MOVE,
492: DOWN,
493: UP,
494: DRAG,
495: WHEEL,
1.311 nicm 496: SECOND,
1.283 nicm 497: DOUBLE,
498: TRIPLE } type = NOTYPE;
1.276 nicm 499: enum { NOWHERE,
500: PANE,
501: STATUS,
502: STATUS_LEFT,
503: STATUS_RIGHT,
504: STATUS_DEFAULT,
1.283 nicm 505: BORDER } where = NOWHERE;
1.131 nicm 506:
1.261 nicm 507: log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
508: m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131 nicm 509:
510: /* What type of event is this? */
1.306 nicm 511: if (event->key == KEYC_DOUBLECLICK) {
512: type = DOUBLE;
513: x = m->x, y = m->y, b = m->b;
1.307 nicm 514: ignore = 1;
1.306 nicm 515: log_debug("double-click at %u,%u", x, y);
516: } else if ((m->sgr_type != ' ' &&
1.209 nicm 517: MOUSE_DRAG(m->sgr_b) &&
518: MOUSE_BUTTONS(m->sgr_b) == 3) ||
519: (m->sgr_type == ' ' &&
520: MOUSE_DRAG(m->b) &&
521: MOUSE_BUTTONS(m->b) == 3 &&
522: MOUSE_BUTTONS(m->lb) == 3)) {
523: type = MOVE;
524: x = m->x, y = m->y, b = 0;
525: log_debug("move at %u,%u", x, y);
526: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 527: type = DRAG;
528: if (c->tty.mouse_drag_flag) {
529: x = m->x, y = m->y, b = m->b;
1.278 nicm 530: if (x == m->lx && y == m->ly)
531: return (KEYC_UNKNOWN);
1.131 nicm 532: log_debug("drag update at %u,%u", x, y);
533: } else {
1.277 nicm 534: x = m->lx, y = m->ly, b = m->lb;
1.131 nicm 535: log_debug("drag start at %u,%u", x, y);
536: }
537: } else if (MOUSE_WHEEL(m->b)) {
538: type = WHEEL;
539: x = m->x, y = m->y, b = m->b;
540: log_debug("wheel at %u,%u", x, y);
1.200 nicm 541: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 542: type = UP;
543: x = m->x, y = m->y, b = m->lb;
544: log_debug("up at %u,%u", x, y);
545: } else {
1.192 nicm 546: if (c->flags & CLIENT_DOUBLECLICK) {
547: evtimer_del(&c->click_timer);
548: c->flags &= ~CLIENT_DOUBLECLICK;
549: if (m->b == c->click_button) {
1.311 nicm 550: type = SECOND;
551: x = m->x, y = m->y, b = m->b;
552: log_debug("second-click at %u,%u", x, y);
1.306 nicm 553: c->flags |= CLIENT_TRIPLECLICK;
1.192 nicm 554: }
555: } else if (c->flags & CLIENT_TRIPLECLICK) {
556: evtimer_del(&c->click_timer);
557: c->flags &= ~CLIENT_TRIPLECLICK;
558: if (m->b == c->click_button) {
559: type = TRIPLE;
560: x = m->x, y = m->y, b = m->b;
561: log_debug("triple-click at %u,%u", x, y);
562: goto have_event;
563: }
1.311 nicm 564: } else {
565: type = DOWN;
566: x = m->x, y = m->y, b = m->b;
567: log_debug("down at %u,%u", x, y);
1.307 nicm 568: c->flags |= CLIENT_DOUBLECLICK;
1.311 nicm 569: }
1.192 nicm 570:
571: if (KEYC_CLICK_TIMEOUT != 0) {
1.306 nicm 572: memcpy(&c->click_event, m, sizeof c->click_event);
1.192 nicm 573: c->click_button = m->b;
574:
1.312 nicm 575: log_debug("click timer started");
1.192 nicm 576: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
577: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
578: evtimer_del(&c->click_timer);
579: evtimer_add(&c->click_timer, &tv);
580: }
1.131 nicm 581: }
1.192 nicm 582:
583: have_event:
1.131 nicm 584: if (type == NOTYPE)
1.177 nicm 585: return (KEYC_UNKNOWN);
1.131 nicm 586:
1.258 nicm 587: /* Save the session. */
1.131 nicm 588: m->s = s->id;
1.258 nicm 589: m->w = -1;
1.307 nicm 590: m->ignore = ignore;
1.131 nicm 591:
592: /* Is this on the status line? */
593: m->statusat = status_at_line(c);
1.292 nicm 594: m->statuslines = status_line_size(c);
1.272 nicm 595: if (m->statusat != -1 &&
596: y >= (u_int)m->statusat &&
1.292 nicm 597: y < m->statusat + m->statuslines) {
1.272 nicm 598: sr = status_get_range(c, x, y - m->statusat);
1.274 nicm 599: if (sr == NULL) {
600: where = STATUS_DEFAULT;
601: } else {
602: switch (sr->type) {
603: case STYLE_RANGE_NONE:
1.273 nicm 604: return (KEYC_UNKNOWN);
1.274 nicm 605: case STYLE_RANGE_LEFT:
606: where = STATUS_LEFT;
607: break;
608: case STYLE_RANGE_RIGHT:
609: where = STATUS_RIGHT;
610: break;
611: case STYLE_RANGE_WINDOW:
1.298 nicm 612: wl = winlink_find_by_index(&s->windows,
613: sr->argument);
1.274 nicm 614: if (wl == NULL)
615: return (KEYC_UNKNOWN);
616: m->w = wl->window->id;
1.273 nicm 617:
1.274 nicm 618: where = STATUS;
619: break;
620: }
1.258 nicm 621: }
622: }
1.131 nicm 623:
624: /* Not on status line. Adjust position and check for border or pane. */
625: if (where == NOWHERE) {
1.261 nicm 626: px = x;
1.292 nicm 627: if (m->statusat == 0 && y >= m->statuslines)
628: py = y - m->statuslines;
1.131 nicm 629: else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261 nicm 630: py = m->statusat - 1;
631: else
632: py = y;
633:
634: tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
635: log_debug("mouse window @%u at %u,%u (%ux%u)",
636: s->curw->window->id, m->ox, m->oy, sx, sy);
637: if (px > sx || py > sy)
638: return (KEYC_UNKNOWN);
639: px = px + m->ox;
640: py = py + m->oy;
1.131 nicm 641:
1.260 nicm 642: /* Try the pane borders if not zoomed. */
643: if (~s->curw->window->flags & WINDOW_ZOOMED) {
644: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261 nicm 645: if ((wp->xoff + wp->sx == px &&
646: wp->yoff <= 1 + py &&
647: wp->yoff + wp->sy >= py) ||
648: (wp->yoff + wp->sy == py &&
649: wp->xoff <= 1 + px &&
650: wp->xoff + wp->sx >= px))
1.260 nicm 651: break;
652: }
653: if (wp != NULL)
654: where = BORDER;
1.131 nicm 655: }
1.260 nicm 656:
657: /* Otherwise try inside the pane. */
658: if (where == NOWHERE) {
1.261 nicm 659: wp = window_get_active_at(s->curw->window, px, py);
1.260 nicm 660: if (wp != NULL)
1.131 nicm 661: where = PANE;
1.315 nicm 662: else
663: return (KEYC_UNKNOWN);
1.131 nicm 664: }
1.260 nicm 665: if (where == PANE)
666: log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
667: else if (where == BORDER)
668: log_debug("mouse on pane %%%u border", wp->id);
1.131 nicm 669: m->wp = wp->id;
670: m->w = wp->window->id;
671: } else
672: m->wp = -1;
673:
674: /* Stop dragging if needed. */
1.200 nicm 675: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 676: if (c->tty.mouse_drag_release != NULL)
677: c->tty.mouse_drag_release(c, m);
678:
679: c->tty.mouse_drag_update = NULL;
680: c->tty.mouse_drag_release = NULL;
681:
1.182 nicm 682: /*
1.183 nicm 683: * End a mouse drag by passing a MouseDragEnd key corresponding
684: * to the button that started the drag.
1.182 nicm 685: */
686: switch (c->tty.mouse_drag_flag) {
687: case 1:
688: if (where == PANE)
1.183 nicm 689: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 690: if (where == STATUS)
1.183 nicm 691: key = KEYC_MOUSEDRAGEND1_STATUS;
1.258 nicm 692: if (where == STATUS_LEFT)
693: key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
694: if (where == STATUS_RIGHT)
695: key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274 nicm 696: if (where == STATUS_DEFAULT)
697: key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182 nicm 698: if (where == BORDER)
1.183 nicm 699: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 700: break;
701: case 2:
702: if (where == PANE)
1.183 nicm 703: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 704: if (where == STATUS)
1.183 nicm 705: key = KEYC_MOUSEDRAGEND2_STATUS;
1.258 nicm 706: if (where == STATUS_LEFT)
707: key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
708: if (where == STATUS_RIGHT)
709: key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274 nicm 710: if (where == STATUS_DEFAULT)
711: key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182 nicm 712: if (where == BORDER)
1.183 nicm 713: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 714: break;
715: case 3:
716: if (where == PANE)
1.183 nicm 717: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 718: if (where == STATUS)
1.183 nicm 719: key = KEYC_MOUSEDRAGEND3_STATUS;
1.258 nicm 720: if (where == STATUS_LEFT)
721: key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
722: if (where == STATUS_RIGHT)
723: key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274 nicm 724: if (where == STATUS_DEFAULT)
725: key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182 nicm 726: if (where == BORDER)
1.183 nicm 727: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 728: break;
729: default:
730: key = KEYC_MOUSE;
731: break;
732: }
1.131 nicm 733: c->tty.mouse_drag_flag = 0;
1.305 nicm 734: goto out;
1.131 nicm 735: }
736:
737: /* Convert to a key binding. */
1.177 nicm 738: key = KEYC_UNKNOWN;
1.131 nicm 739: switch (type) {
740: case NOTYPE:
741: break;
1.209 nicm 742: case MOVE:
743: if (where == PANE)
744: key = KEYC_MOUSEMOVE_PANE;
745: if (where == STATUS)
746: key = KEYC_MOUSEMOVE_STATUS;
1.274 nicm 747: if (where == STATUS_LEFT)
748: key = KEYC_MOUSEMOVE_STATUS_LEFT;
749: if (where == STATUS_RIGHT)
750: key = KEYC_MOUSEMOVE_STATUS_RIGHT;
751: if (where == STATUS_DEFAULT)
752: key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209 nicm 753: if (where == BORDER)
754: key = KEYC_MOUSEMOVE_BORDER;
755: break;
1.131 nicm 756: case DRAG:
1.204 nicm 757: if (c->tty.mouse_drag_update != NULL)
758: key = KEYC_DRAGGING;
759: else {
1.131 nicm 760: switch (MOUSE_BUTTONS(b)) {
761: case 0:
762: if (where == PANE)
763: key = KEYC_MOUSEDRAG1_PANE;
764: if (where == STATUS)
765: key = KEYC_MOUSEDRAG1_STATUS;
1.258 nicm 766: if (where == STATUS_LEFT)
767: key = KEYC_MOUSEDRAG1_STATUS_LEFT;
768: if (where == STATUS_RIGHT)
769: key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274 nicm 770: if (where == STATUS_DEFAULT)
771: key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131 nicm 772: if (where == BORDER)
773: key = KEYC_MOUSEDRAG1_BORDER;
774: break;
775: case 1:
776: if (where == PANE)
777: key = KEYC_MOUSEDRAG2_PANE;
778: if (where == STATUS)
779: key = KEYC_MOUSEDRAG2_STATUS;
1.258 nicm 780: if (where == STATUS_LEFT)
781: key = KEYC_MOUSEDRAG2_STATUS_LEFT;
782: if (where == STATUS_RIGHT)
783: key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274 nicm 784: if (where == STATUS_DEFAULT)
785: key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131 nicm 786: if (where == BORDER)
787: key = KEYC_MOUSEDRAG2_BORDER;
788: break;
789: case 2:
790: if (where == PANE)
791: key = KEYC_MOUSEDRAG3_PANE;
792: if (where == STATUS)
793: key = KEYC_MOUSEDRAG3_STATUS;
1.258 nicm 794: if (where == STATUS_LEFT)
795: key = KEYC_MOUSEDRAG3_STATUS_LEFT;
796: if (where == STATUS_RIGHT)
797: key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274 nicm 798: if (where == STATUS_DEFAULT)
799: key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131 nicm 800: if (where == BORDER)
801: key = KEYC_MOUSEDRAG3_BORDER;
802: break;
803: }
804: }
1.66 nicm 805:
1.182 nicm 806: /*
807: * Begin a drag by setting the flag to a non-zero value that
808: * corresponds to the mouse button in use.
809: */
810: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 811: break;
812: case WHEEL:
813: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
814: if (where == PANE)
815: key = KEYC_WHEELUP_PANE;
816: if (where == STATUS)
817: key = KEYC_WHEELUP_STATUS;
1.258 nicm 818: if (where == STATUS_LEFT)
819: key = KEYC_WHEELUP_STATUS_LEFT;
820: if (where == STATUS_RIGHT)
821: key = KEYC_WHEELUP_STATUS_RIGHT;
1.274 nicm 822: if (where == STATUS_DEFAULT)
823: key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131 nicm 824: if (where == BORDER)
825: key = KEYC_WHEELUP_BORDER;
826: } else {
827: if (where == PANE)
828: key = KEYC_WHEELDOWN_PANE;
829: if (where == STATUS)
830: key = KEYC_WHEELDOWN_STATUS;
1.274 nicm 831: if (where == STATUS_LEFT)
832: key = KEYC_WHEELDOWN_STATUS_LEFT;
833: if (where == STATUS_RIGHT)
834: key = KEYC_WHEELDOWN_STATUS_RIGHT;
835: if (where == STATUS_DEFAULT)
836: key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131 nicm 837: if (where == BORDER)
838: key = KEYC_WHEELDOWN_BORDER;
839: }
840: break;
841: case UP:
842: switch (MOUSE_BUTTONS(b)) {
843: case 0:
844: if (where == PANE)
845: key = KEYC_MOUSEUP1_PANE;
846: if (where == STATUS)
847: key = KEYC_MOUSEUP1_STATUS;
1.258 nicm 848: if (where == STATUS_LEFT)
849: key = KEYC_MOUSEUP1_STATUS_LEFT;
850: if (where == STATUS_RIGHT)
851: key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274 nicm 852: if (where == STATUS_DEFAULT)
853: key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131 nicm 854: if (where == BORDER)
855: key = KEYC_MOUSEUP1_BORDER;
856: break;
857: case 1:
858: if (where == PANE)
859: key = KEYC_MOUSEUP2_PANE;
860: if (where == STATUS)
861: key = KEYC_MOUSEUP2_STATUS;
1.258 nicm 862: if (where == STATUS_LEFT)
863: key = KEYC_MOUSEUP2_STATUS_LEFT;
864: if (where == STATUS_RIGHT)
865: key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274 nicm 866: if (where == STATUS_DEFAULT)
867: key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131 nicm 868: if (where == BORDER)
869: key = KEYC_MOUSEUP2_BORDER;
870: break;
871: case 2:
872: if (where == PANE)
873: key = KEYC_MOUSEUP3_PANE;
874: if (where == STATUS)
875: key = KEYC_MOUSEUP3_STATUS;
1.258 nicm 876: if (where == STATUS_LEFT)
877: key = KEYC_MOUSEUP3_STATUS_LEFT;
878: if (where == STATUS_RIGHT)
879: key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274 nicm 880: if (where == STATUS_DEFAULT)
881: key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131 nicm 882: if (where == BORDER)
883: key = KEYC_MOUSEUP3_BORDER;
884: break;
885: }
886: break;
887: case DOWN:
888: switch (MOUSE_BUTTONS(b)) {
889: case 0:
890: if (where == PANE)
891: key = KEYC_MOUSEDOWN1_PANE;
892: if (where == STATUS)
893: key = KEYC_MOUSEDOWN1_STATUS;
1.258 nicm 894: if (where == STATUS_LEFT)
895: key = KEYC_MOUSEDOWN1_STATUS_LEFT;
896: if (where == STATUS_RIGHT)
897: key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274 nicm 898: if (where == STATUS_DEFAULT)
899: key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131 nicm 900: if (where == BORDER)
901: key = KEYC_MOUSEDOWN1_BORDER;
902: break;
903: case 1:
904: if (where == PANE)
905: key = KEYC_MOUSEDOWN2_PANE;
906: if (where == STATUS)
907: key = KEYC_MOUSEDOWN2_STATUS;
1.258 nicm 908: if (where == STATUS_LEFT)
909: key = KEYC_MOUSEDOWN2_STATUS_LEFT;
910: if (where == STATUS_RIGHT)
911: key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274 nicm 912: if (where == STATUS_DEFAULT)
913: key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131 nicm 914: if (where == BORDER)
915: key = KEYC_MOUSEDOWN2_BORDER;
916: break;
917: case 2:
918: if (where == PANE)
919: key = KEYC_MOUSEDOWN3_PANE;
920: if (where == STATUS)
921: key = KEYC_MOUSEDOWN3_STATUS;
1.258 nicm 922: if (where == STATUS_LEFT)
923: key = KEYC_MOUSEDOWN3_STATUS_LEFT;
924: if (where == STATUS_RIGHT)
925: key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274 nicm 926: if (where == STATUS_DEFAULT)
927: key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131 nicm 928: if (where == BORDER)
929: key = KEYC_MOUSEDOWN3_BORDER;
1.311 nicm 930: break;
931: }
932: break;
933: case SECOND:
934: switch (MOUSE_BUTTONS(b)) {
935: case 0:
936: if (where == PANE)
937: key = KEYC_SECONDCLICK1_PANE;
938: if (where == STATUS)
939: key = KEYC_SECONDCLICK1_STATUS;
940: if (where == STATUS_LEFT)
941: key = KEYC_SECONDCLICK1_STATUS_LEFT;
942: if (where == STATUS_RIGHT)
943: key = KEYC_SECONDCLICK1_STATUS_RIGHT;
944: if (where == STATUS_DEFAULT)
945: key = KEYC_SECONDCLICK1_STATUS_DEFAULT;
946: if (where == BORDER)
947: key = KEYC_SECONDCLICK1_BORDER;
948: break;
949: case 1:
950: if (where == PANE)
951: key = KEYC_SECONDCLICK2_PANE;
952: if (where == STATUS)
953: key = KEYC_SECONDCLICK2_STATUS;
954: if (where == STATUS_LEFT)
955: key = KEYC_SECONDCLICK2_STATUS_LEFT;
956: if (where == STATUS_RIGHT)
957: key = KEYC_SECONDCLICK2_STATUS_RIGHT;
958: if (where == STATUS_DEFAULT)
959: key = KEYC_SECONDCLICK2_STATUS_DEFAULT;
960: if (where == BORDER)
961: key = KEYC_SECONDCLICK2_BORDER;
962: break;
963: case 2:
964: if (where == PANE)
965: key = KEYC_SECONDCLICK3_PANE;
966: if (where == STATUS)
967: key = KEYC_SECONDCLICK3_STATUS;
968: if (where == STATUS_LEFT)
969: key = KEYC_SECONDCLICK3_STATUS_LEFT;
970: if (where == STATUS_RIGHT)
971: key = KEYC_SECONDCLICK3_STATUS_RIGHT;
972: if (where == STATUS_DEFAULT)
973: key = KEYC_SECONDCLICK3_STATUS_DEFAULT;
974: if (where == BORDER)
975: key = KEYC_SECONDCLICK3_BORDER;
1.131 nicm 976: break;
1.66 nicm 977: }
1.131 nicm 978: break;
1.192 nicm 979: case DOUBLE:
980: switch (MOUSE_BUTTONS(b)) {
981: case 0:
982: if (where == PANE)
983: key = KEYC_DOUBLECLICK1_PANE;
984: if (where == STATUS)
985: key = KEYC_DOUBLECLICK1_STATUS;
1.258 nicm 986: if (where == STATUS_LEFT)
987: key = KEYC_DOUBLECLICK1_STATUS_LEFT;
988: if (where == STATUS_RIGHT)
989: key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274 nicm 990: if (where == STATUS_DEFAULT)
991: key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192 nicm 992: if (where == BORDER)
993: key = KEYC_DOUBLECLICK1_BORDER;
994: break;
995: case 1:
996: if (where == PANE)
997: key = KEYC_DOUBLECLICK2_PANE;
998: if (where == STATUS)
999: key = KEYC_DOUBLECLICK2_STATUS;
1.258 nicm 1000: if (where == STATUS_LEFT)
1001: key = KEYC_DOUBLECLICK2_STATUS_LEFT;
1002: if (where == STATUS_RIGHT)
1003: key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274 nicm 1004: if (where == STATUS_DEFAULT)
1005: key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192 nicm 1006: if (where == BORDER)
1007: key = KEYC_DOUBLECLICK2_BORDER;
1008: break;
1009: case 2:
1010: if (where == PANE)
1011: key = KEYC_DOUBLECLICK3_PANE;
1012: if (where == STATUS)
1013: key = KEYC_DOUBLECLICK3_STATUS;
1.258 nicm 1014: if (where == STATUS_LEFT)
1015: key = KEYC_DOUBLECLICK3_STATUS_LEFT;
1016: if (where == STATUS_RIGHT)
1017: key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274 nicm 1018: if (where == STATUS_DEFAULT)
1019: key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192 nicm 1020: if (where == BORDER)
1021: key = KEYC_DOUBLECLICK3_BORDER;
1022: break;
1023: }
1024: break;
1025: case TRIPLE:
1026: switch (MOUSE_BUTTONS(b)) {
1027: case 0:
1028: if (where == PANE)
1029: key = KEYC_TRIPLECLICK1_PANE;
1030: if (where == STATUS)
1031: key = KEYC_TRIPLECLICK1_STATUS;
1.258 nicm 1032: if (where == STATUS_LEFT)
1033: key = KEYC_TRIPLECLICK1_STATUS_LEFT;
1034: if (where == STATUS_RIGHT)
1035: key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274 nicm 1036: if (where == STATUS_DEFAULT)
1037: key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192 nicm 1038: if (where == BORDER)
1039: key = KEYC_TRIPLECLICK1_BORDER;
1040: break;
1041: case 1:
1042: if (where == PANE)
1043: key = KEYC_TRIPLECLICK2_PANE;
1044: if (where == STATUS)
1045: key = KEYC_TRIPLECLICK2_STATUS;
1.258 nicm 1046: if (where == STATUS_LEFT)
1047: key = KEYC_TRIPLECLICK2_STATUS_LEFT;
1048: if (where == STATUS_RIGHT)
1049: key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274 nicm 1050: if (where == STATUS_DEFAULT)
1051: key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192 nicm 1052: if (where == BORDER)
1053: key = KEYC_TRIPLECLICK2_BORDER;
1054: break;
1055: case 2:
1056: if (where == PANE)
1057: key = KEYC_TRIPLECLICK3_PANE;
1058: if (where == STATUS)
1059: key = KEYC_TRIPLECLICK3_STATUS;
1.258 nicm 1060: if (where == STATUS_LEFT)
1061: key = KEYC_TRIPLECLICK3_STATUS_LEFT;
1062: if (where == STATUS_RIGHT)
1063: key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274 nicm 1064: if (where == STATUS_DEFAULT)
1065: key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192 nicm 1066: if (where == BORDER)
1067: key = KEYC_TRIPLECLICK3_BORDER;
1068: break;
1069: }
1070: break;
1.66 nicm 1071: }
1.177 nicm 1072: if (key == KEYC_UNKNOWN)
1073: return (KEYC_UNKNOWN);
1.66 nicm 1074:
1.305 nicm 1075: out:
1.131 nicm 1076: /* Apply modifiers if any. */
1077: if (b & MOUSE_MASK_META)
1.342 nicm 1078: key |= KEYC_META;
1.131 nicm 1079: if (b & MOUSE_MASK_CTRL)
1080: key |= KEYC_CTRL;
1081: if (b & MOUSE_MASK_SHIFT)
1082: key |= KEYC_SHIFT;
1.66 nicm 1083:
1.305 nicm 1084: if (log_get_level() != 0)
1.343 nicm 1085: log_debug("mouse key is %s", key_string_lookup_key (key, 1));
1.131 nicm 1086: return (key);
1.66 nicm 1087: }
1088:
1.82 nicm 1089: /* Is this fast enough to probably be a paste? */
1.190 nicm 1090: static int
1.82 nicm 1091: server_client_assume_paste(struct session *s)
1092: {
1093: struct timeval tv;
1.84 nicm 1094: int t;
1.82 nicm 1095:
1.163 nicm 1096: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 1097: return (0);
1.82 nicm 1098:
1099: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 1100: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
1101: log_debug("session %s pasting (flag %d)", s->name,
1102: !!(s->flags & SESSION_PASTING));
1103: if (s->flags & SESSION_PASTING)
1104: return (1);
1105: s->flags |= SESSION_PASTING;
1106: return (0);
1107: }
1108: log_debug("session %s not pasting", s->name);
1109: s->flags &= ~SESSION_PASTING;
1.83 nicm 1110: return (0);
1.82 nicm 1111: }
1112:
1.295 nicm 1113: /* Has the latest client changed? */
1114: static void
1115: server_client_update_latest(struct client *c)
1116: {
1117: struct window *w;
1118:
1119: if (c->session == NULL)
1120: return;
1121: w = c->session->curw->window;
1122:
1123: if (w->latest == c)
1124: return;
1125: w->latest = c;
1126:
1127: if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
1.355 nicm 1128: recalculate_size(w, 0);
1.295 nicm 1129: }
1130:
1.276 nicm 1131: /*
1132: * Handle data key input from client. This owns and can modify the key event it
1133: * is given and is responsible for freeing it.
1134: */
1.280 nicm 1135: static enum cmd_retval
1.276 nicm 1136: server_client_key_callback(struct cmdq_item *item, void *data)
1.18 nicm 1137: {
1.316 nicm 1138: struct client *c = cmdq_get_client(item);
1.276 nicm 1139: struct key_event *event = data;
1140: key_code key = event->key;
1141: struct mouse_event *m = &event->m;
1.267 nicm 1142: struct session *s = c->session;
1143: struct winlink *wl;
1144: struct window_pane *wp;
1145: struct window_mode_entry *wme;
1146: struct timeval tv;
1147: struct key_table *table, *first;
1148: struct key_binding *bd;
1149: int xtimeout, flags;
1150: struct cmd_find_state fs;
1151: key_code key0;
1.18 nicm 1152:
1153: /* Check the client is good to accept input. */
1.303 nicm 1154: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.276 nicm 1155: goto out;
1.264 nicm 1156: wl = s->curw;
1.18 nicm 1157:
1158: /* Update the activity timer. */
1159: if (gettimeofday(&c->activity_time, NULL) != 0)
1160: fatal("gettimeofday failed");
1.149 nicm 1161: session_update_activity(s, &c->activity_time);
1.18 nicm 1162:
1163: /* Check for mouse keys. */
1.204 nicm 1164: m->valid = 0;
1.306 nicm 1165: if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1.30 nicm 1166: if (c->flags & CLIENT_READONLY)
1.276 nicm 1167: goto out;
1168: key = server_client_check_mouse(c, event);
1.177 nicm 1169: if (key == KEYC_UNKNOWN)
1.276 nicm 1170: goto out;
1.131 nicm 1171:
1172: m->valid = 1;
1173: m->key = key;
1.203 nicm 1174:
1175: /*
1.204 nicm 1176: * Mouse drag is in progress, so fire the callback (now that
1177: * the mouse event is valid).
1.203 nicm 1178: */
1.305 nicm 1179: if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1.204 nicm 1180: c->tty.mouse_drag_update(c, m);
1.276 nicm 1181: goto out;
1.204 nicm 1182: }
1.338 nicm 1183: event->key = key;
1.276 nicm 1184: }
1.18 nicm 1185:
1.202 nicm 1186: /* Find affected pane. */
1.243 nicm 1187: if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1.341 nicm 1188: cmd_find_from_client(&fs, c, 0);
1.227 nicm 1189: wp = fs.wp;
1.202 nicm 1190:
1191: /* Forward mouse keys if disabled. */
1.206 nicm 1192: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253 nicm 1193: goto forward_key;
1.202 nicm 1194:
1.132 nicm 1195: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 1196: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253 nicm 1197: goto forward_key;
1.18 nicm 1198:
1.191 nicm 1199: /*
1200: * Work out the current key table. If the pane is in a mode, use
1201: * the mode table instead of the default key table.
1202: */
1.223 nicm 1203: if (server_client_is_default_key_table(c, c->keytable) &&
1204: wp != NULL &&
1.267 nicm 1205: (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1206: wme->mode->key_table != NULL)
1207: table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223 nicm 1208: else
1.191 nicm 1209: table = c->keytable;
1.223 nicm 1210: first = table;
1.191 nicm 1211:
1.253 nicm 1212: table_changed:
1.205 nicm 1213: /*
1214: * The prefix always takes precedence and forces a switch to the prefix
1215: * table, unless we are already there.
1216: */
1.343 nicm 1217: key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1.239 nicm 1218: if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
1219: key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205 nicm 1220: strcmp(table->name, "prefix") != 0) {
1221: server_client_set_key_table(c, "prefix");
1222: server_status_client(c);
1.276 nicm 1223: goto out;
1.205 nicm 1224: }
1.238 nicm 1225: flags = c->flags;
1.205 nicm 1226:
1.262 nicm 1227: try_again:
1.223 nicm 1228: /* Log key table. */
1229: if (wp == NULL)
1230: log_debug("key table %s (no pane)", table->name);
1231: else
1232: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225 nicm 1233: if (c->flags & CLIENT_REPEAT)
1234: log_debug("currently repeating");
1.223 nicm 1235:
1.132 nicm 1236: /* Try to see if there is a key binding in the current table. */
1.254 nicm 1237: bd = key_bindings_get(table, key0);
1.132 nicm 1238: if (bd != NULL) {
1239: /*
1240: * Key was matched in this table. If currently repeating but a
1241: * non-repeating binding was found, stop repeating and try
1242: * again in the root table.
1243: */
1.222 nicm 1244: if ((c->flags & CLIENT_REPEAT) &&
1245: (~bd->flags & KEY_BINDING_REPEAT)) {
1.262 nicm 1246: log_debug("found in key table %s (not repeating)",
1247: table->name);
1.178 nicm 1248: server_client_set_key_table(c, NULL);
1.262 nicm 1249: first = table = c->keytable;
1.132 nicm 1250: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 1251: server_status_client(c);
1.253 nicm 1252: goto table_changed;
1.18 nicm 1253: }
1.223 nicm 1254: log_debug("found in key table %s", table->name);
1.82 nicm 1255:
1.132 nicm 1256: /*
1257: * Take a reference to this table to make sure the key binding
1258: * doesn't disappear.
1259: */
1260: table->references++;
1261:
1262: /*
1263: * If this is a repeating key, start the timer. Otherwise reset
1264: * the client back to the root table.
1265: */
1.163 nicm 1266: xtimeout = options_get_number(s->options, "repeat-time");
1.222 nicm 1267: if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132 nicm 1268: c->flags |= CLIENT_REPEAT;
1269:
1270: tv.tv_sec = xtimeout / 1000;
1271: tv.tv_usec = (xtimeout % 1000) * 1000L;
1272: evtimer_del(&c->repeat_timer);
1273: evtimer_add(&c->repeat_timer, &tv);
1274: } else {
1.18 nicm 1275: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 1276: server_client_set_key_table(c, NULL);
1.18 nicm 1277: }
1.132 nicm 1278: server_status_client(c);
1279:
1.227 nicm 1280: /* Execute the key binding. */
1.317 nicm 1281: key_bindings_dispatch(bd, item, c, event, &fs);
1.132 nicm 1282: key_bindings_unref_table(table);
1.276 nicm 1283: goto out;
1.18 nicm 1284: }
1285:
1.132 nicm 1286: /*
1.253 nicm 1287: * No match, try the ANY key.
1288: */
1289: if (key0 != KEYC_ANY) {
1290: key0 = KEYC_ANY;
1291: goto try_again;
1292: }
1293:
1294: /*
1.223 nicm 1295: * No match in this table. If not in the root table or if repeating,
1296: * switch the client back to the root table and try again.
1.132 nicm 1297: */
1.223 nicm 1298: log_debug("not found in key table %s", table->name);
1299: if (!server_client_is_default_key_table(c, table) ||
1300: (c->flags & CLIENT_REPEAT)) {
1.262 nicm 1301: log_debug("trying in root table");
1.178 nicm 1302: server_client_set_key_table(c, NULL);
1.262 nicm 1303: table = c->keytable;
1304: if (c->flags & CLIENT_REPEAT)
1305: first = table;
1.18 nicm 1306: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 1307: server_status_client(c);
1.253 nicm 1308: goto table_changed;
1.18 nicm 1309: }
1310:
1.223 nicm 1311: /*
1312: * No match in the root table either. If this wasn't the first table
1313: * tried, don't pass the key to the pane.
1314: */
1.238 nicm 1315: if (first != table && (~flags & CLIENT_REPEAT)) {
1.178 nicm 1316: server_client_set_key_table(c, NULL);
1.132 nicm 1317: server_status_client(c);
1.276 nicm 1318: goto out;
1.161 nicm 1319: }
1320:
1.253 nicm 1321: forward_key:
1.161 nicm 1322: if (c->flags & CLIENT_READONLY)
1.276 nicm 1323: goto out;
1.161 nicm 1324: if (wp != NULL)
1.264 nicm 1325: window_pane_key(wp, c, s, wl, key, m);
1.276 nicm 1326:
1327: out:
1.347 nicm 1328: if (s != NULL && key != KEYC_FOCUS_OUT)
1.295 nicm 1329: server_client_update_latest(c);
1.276 nicm 1330: free(event);
1331: return (CMD_RETURN_NORMAL);
1.280 nicm 1332: }
1333:
1334: /* Handle a key event. */
1335: int
1336: server_client_handle_key(struct client *c, struct key_event *event)
1337: {
1338: struct session *s = c->session;
1339: struct cmdq_item *item;
1340:
1341: /* Check the client is good to accept input. */
1.303 nicm 1342: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.280 nicm 1343: return (0);
1344:
1345: /*
1.291 nicm 1346: * Key presses in overlay mode and the command prompt are a special
1347: * case. The queue might be blocked so they need to be processed
1348: * immediately rather than queued.
1.280 nicm 1349: */
1.291 nicm 1350: if (~c->flags & CLIENT_READONLY) {
1.372 nicm 1351: if (c->message_string != NULL) {
1352: if (c->message_ignore_keys)
1353: return (0);
1354: status_message_clear(c);
1355: }
1.291 nicm 1356: if (c->overlay_key != NULL) {
1357: switch (c->overlay_key(c, event)) {
1358: case 0:
1359: return (0);
1360: case 1:
1361: server_client_clear_overlay(c);
1362: return (0);
1363: }
1.290 nicm 1364: }
1.293 nicm 1365: server_client_clear_overlay(c);
1.335 nicm 1366: if (c->prompt_string != NULL) {
1367: if (status_prompt_key(c, event->key) == 0)
1368: return (0);
1369: }
1.280 nicm 1370: }
1371:
1372: /*
1373: * Add the key to the queue so it happens after any commands queued by
1374: * previous keys.
1375: */
1376: item = cmdq_get_callback(server_client_key_callback, event);
1377: cmdq_append(c, item);
1378: return (1);
1.18 nicm 1379: }
1380:
1.2 nicm 1381: /* Client functions that need to happen every loop. */
1382: void
1383: server_client_loop(void)
1384: {
1385: struct client *c;
1386: struct window *w;
1387: struct window_pane *wp;
1.344 nicm 1388: int focus;
1389:
1390: /* Check for window resize. This is done before redrawing. */
1391: RB_FOREACH(w, windows, &windows)
1392: server_client_check_window_resize(w);
1.2 nicm 1393:
1.344 nicm 1394: /* Check clients. */
1.135 nicm 1395: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1396: server_client_check_exit(c);
1397: if (c->session != NULL) {
1.366 nicm 1398: server_client_check_modes(c);
1.36 nicm 1399: server_client_check_redraw(c);
1400: server_client_reset_state(c);
1401: }
1.2 nicm 1402: }
1403:
1404: /*
1405: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 1406: * their flags now. Also check pane focus and resize.
1.2 nicm 1407: */
1.197 nicm 1408: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1409: RB_FOREACH(w, windows, &windows) {
1.91 nicm 1410: TAILQ_FOREACH(wp, &w->panes, entry) {
1.289 nicm 1411: if (wp->fd != -1) {
1.197 nicm 1412: if (focus)
1.344 nicm 1413: server_client_check_pane_focus(wp);
1414: server_client_check_pane_resize(wp);
1.345 nicm 1415: server_client_check_pane_buffer(wp);
1.101 nicm 1416: }
1.326 nicm 1417: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1418: }
1.150 nicm 1419: check_window_name(w);
1.2 nicm 1420: }
1.92 nicm 1421: }
1422:
1.344 nicm 1423: /* Check if window needs to be resized. */
1424: static void
1425: server_client_check_window_resize(struct window *w)
1426: {
1427: struct winlink *wl;
1428:
1429: if (~w->flags & WINDOW_RESIZE)
1430: return;
1431:
1432: TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1433: if (wl->session->attached != 0 && wl->session->curw == wl)
1434: break;
1435: }
1436: if (wl == NULL)
1437: return;
1438:
1439: log_debug("%s: resizing window @%u", __func__, w->id);
1440: resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
1441: }
1442:
1.356 nicm 1443: /* Resize timer event. */
1.188 nicm 1444: static void
1.356 nicm 1445: server_client_resize_timer(__unused int fd, __unused short events, void *data)
1.92 nicm 1446: {
1.356 nicm 1447: struct window_pane *wp = data;
1.92 nicm 1448:
1.356 nicm 1449: log_debug("%s: %%%u resize timer expired", __func__, wp->id);
1450: evtimer_del(&wp->resize_timer);
1.188 nicm 1451: }
1452:
1.294 nicm 1453: /* Start the resize timer. */
1454: static void
1455: server_client_start_resize_timer(struct window_pane *wp)
1456: {
1457: struct timeval tv = { .tv_usec = 250000 };
1458:
1.356 nicm 1459: log_debug("%s: %%%u resize timer started", __func__, wp->id);
1460: evtimer_add(&wp->resize_timer, &tv);
1.294 nicm 1461: }
1462:
1.356 nicm 1463: /* Force timer event. */
1.294 nicm 1464: static void
1.356 nicm 1465: server_client_force_timer(__unused int fd, __unused short events, void *data)
1.294 nicm 1466: {
1467: struct window_pane *wp = data;
1468:
1.356 nicm 1469: log_debug("%s: %%%u force timer expired", __func__, wp->id);
1470: evtimer_del(&wp->force_timer);
1471: wp->flags |= PANE_RESIZENOW;
1472: }
1.294 nicm 1473:
1.356 nicm 1474: /* Start the force timer. */
1475: static void
1476: server_client_start_force_timer(struct window_pane *wp)
1477: {
1478: struct timeval tv = { .tv_usec = 10000 };
1.294 nicm 1479:
1.356 nicm 1480: log_debug("%s: %%%u force timer started", __func__, wp->id);
1481: evtimer_add(&wp->force_timer, &tv);
1.294 nicm 1482: }
1483:
1.188 nicm 1484: /* Check if pane should be resized. */
1.190 nicm 1485: static void
1.344 nicm 1486: server_client_check_pane_resize(struct window_pane *wp)
1.188 nicm 1487: {
1.356 nicm 1488: if (!event_initialized(&wp->resize_timer))
1489: evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
1490: if (!event_initialized(&wp->force_timer))
1491: evtimer_set(&wp->force_timer, server_client_force_timer, wp);
1492:
1.294 nicm 1493: if (~wp->flags & PANE_RESIZE)
1.188 nicm 1494: return;
1.356 nicm 1495: log_debug("%s: %%%u needs to be resized", __func__, wp->id);
1.188 nicm 1496:
1.356 nicm 1497: if (evtimer_pending(&wp->resize_timer, NULL)) {
1498: log_debug("%s: %%%u resize timer is running", __func__, wp->id);
1499: return;
1500: }
1501: server_client_start_resize_timer(wp);
1.188 nicm 1502:
1.356 nicm 1503: if (~wp->flags & PANE_RESIZEFORCE) {
1504: /*
1505: * The timer is not running and we don't need to force a
1506: * resize, so just resize immediately.
1507: */
1508: log_debug("%s: resizing %%%u now", __func__, wp->id);
1509: window_pane_send_resize(wp, 0);
1510: wp->flags &= ~PANE_RESIZE;
1511: } else {
1512: /*
1513: * The timer is not running, but we need to force a resize. If
1514: * the force timer has expired, resize to the real size now.
1515: * Otherwise resize to the force size and start the timer.
1516: */
1517: if (wp->flags & PANE_RESIZENOW) {
1.359 nicm 1518: log_debug("%s: resizing %%%u after forced resize",
1519: __func__, wp->id);
1.356 nicm 1520: window_pane_send_resize(wp, 0);
1521: wp->flags &= ~(PANE_RESIZE|PANE_RESIZEFORCE|PANE_RESIZENOW);
1522: } else if (!evtimer_pending(&wp->force_timer, NULL)) {
1.359 nicm 1523: log_debug("%s: forcing resize of %%%u", __func__,
1524: wp->id);
1.356 nicm 1525: window_pane_send_resize(wp, 1);
1526: server_client_start_force_timer(wp);
1527: }
1528: }
1.345 nicm 1529: }
1530:
1531: /* Check pane buffer size. */
1532: static void
1533: server_client_check_pane_buffer(struct window_pane *wp)
1534: {
1535: struct evbuffer *evb = wp->event->input;
1536: size_t minimum;
1537: struct client *c;
1.346 nicm 1538: struct window_pane_offset *wpo;
1539: int off = 1, flag;
1540: u_int attached_clients = 0;
1.353 nicm 1541: size_t new_size;
1.345 nicm 1542:
1543: /*
1.352 nicm 1544: * Work out the minimum used size. This is the most that can be removed
1545: * from the buffer.
1.345 nicm 1546: */
1.352 nicm 1547: minimum = wp->offset.used;
1548: if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
1549: minimum = wp->pipe_offset.used;
1.345 nicm 1550: TAILQ_FOREACH(c, &clients, entry) {
1551: if (c->session == NULL)
1552: continue;
1.346 nicm 1553: attached_clients++;
1554:
1555: if (~c->flags & CLIENT_CONTROL) {
1.345 nicm 1556: off = 0;
1557: continue;
1558: }
1.346 nicm 1559: wpo = control_pane_offset(c, wp, &flag);
1560: if (wpo == NULL) {
1.345 nicm 1561: off = 0;
1.346 nicm 1562: continue;
1563: }
1564: if (!flag)
1565: off = 0;
1566:
1.353 nicm 1567: window_pane_get_new_data(wp, wpo, &new_size);
1568: log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
1569: __func__, c->name, wpo->used - wp->base_offset, new_size,
1570: wp->id);
1571: if (wpo->used < minimum)
1.352 nicm 1572: minimum = wpo->used;
1.345 nicm 1573: }
1.346 nicm 1574: if (attached_clients == 0)
1575: off = 0;
1.345 nicm 1576: minimum -= wp->base_offset;
1577: if (minimum == 0)
1578: goto out;
1579:
1580: /* Drain the buffer. */
1.352 nicm 1581: log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
1582: wp->id, minimum, EVBUFFER_LENGTH(evb));
1.345 nicm 1583: evbuffer_drain(evb, minimum);
1584:
1585: /*
1586: * Adjust the base offset. If it would roll over, all the offsets into
1587: * the buffer need to be adjusted.
1588: */
1589: if (wp->base_offset > SIZE_MAX - minimum) {
1590: log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
1591: wp->offset.used -= wp->base_offset;
1.352 nicm 1592: if (wp->pipe_fd != -1)
1.345 nicm 1593: wp->pipe_offset.used -= wp->base_offset;
1594: TAILQ_FOREACH(c, &clients, entry) {
1595: if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
1596: continue;
1.346 nicm 1597: wpo = control_pane_offset(c, wp, &flag);
1.352 nicm 1598: if (wpo != NULL && !flag)
1.346 nicm 1599: wpo->used -= wp->base_offset;
1.345 nicm 1600: }
1601: wp->base_offset = minimum;
1602: } else
1603: wp->base_offset += minimum;
1604:
1605: out:
1606: /*
1607: * If there is data remaining, and there are no clients able to consume
1.351 nicm 1608: * it, do not read any more. This is true when there are attached
1609: * clients, all of which are control clients which are not able to
1610: * accept any more data.
1.345 nicm 1611: */
1.352 nicm 1612: log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
1.345 nicm 1613: if (off)
1614: bufferevent_disable(wp->event, EV_READ);
1615: else
1616: bufferevent_enable(wp->event, EV_READ);
1.91 nicm 1617: }
1618:
1619: /* Check whether pane should be focused. */
1.190 nicm 1620: static void
1.344 nicm 1621: server_client_check_pane_focus(struct window_pane *wp)
1.91 nicm 1622: {
1.93 nicm 1623: struct client *c;
1.102 nicm 1624: int push;
1625:
1626: /* Do we need to push the focus state? */
1627: push = wp->flags & PANE_FOCUSPUSH;
1628: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1629:
1630: /* If we're not the active pane in our window, we're not focused. */
1631: if (wp->window->active != wp)
1632: goto not_focused;
1633:
1634: /*
1.93 nicm 1635: * If our window is the current window in any focused clients with an
1636: * attached session, we're focused.
1.91 nicm 1637: */
1.135 nicm 1638: TAILQ_FOREACH(c, &clients, entry) {
1639: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1640: continue;
1.255 nicm 1641: if (c->session->attached == 0)
1.93 nicm 1642: continue;
1643:
1644: if (c->session->curw->window == wp->window)
1.91 nicm 1645: goto focused;
1646: }
1647:
1648: not_focused:
1.251 nicm 1649: if (push || (wp->flags & PANE_FOCUSED)) {
1650: if (wp->base.mode & MODE_FOCUSON)
1651: bufferevent_write(wp->event, "\033[O", 3);
1652: notify_pane("pane-focus-out", wp);
1653: }
1.91 nicm 1654: wp->flags &= ~PANE_FOCUSED;
1655: return;
1656:
1657: focused:
1.251 nicm 1658: if (push || !(wp->flags & PANE_FOCUSED)) {
1659: if (wp->base.mode & MODE_FOCUSON)
1660: bufferevent_write(wp->event, "\033[I", 3);
1661: notify_pane("pane-focus-in", wp);
1.275 nicm 1662: session_update_activity(c->session, NULL);
1.251 nicm 1663: }
1.91 nicm 1664: wp->flags |= PANE_FOCUSED;
1.2 nicm 1665: }
1666:
1.18 nicm 1667: /*
1668: * Update cursor position and mode settings. The scroll region and attributes
1669: * are cleared when idle (waiting for an event) as this is the most likely time
1670: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1671: * compromise between excessive resets and likelihood of an interrupt.
1672: *
1673: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1674: * things that are already in their default state.
1675: */
1.190 nicm 1676: static void
1.18 nicm 1677: server_client_reset_state(struct client *c)
1.1 nicm 1678: {
1.332 nicm 1679: struct tty *tty = &c->tty;
1.18 nicm 1680: struct window *w = c->session->curw->window;
1.341 nicm 1681: struct window_pane *wp = server_client_get_pane(c), *loop;
1.336 nicm 1682: struct screen *s = NULL;
1.163 nicm 1683: struct options *oo = c->session->options;
1.336 nicm 1684: int mode = 0, cursor, flags;
1.261 nicm 1685: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1686:
1.186 nicm 1687: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1688: return;
1689:
1.332 nicm 1690: /* Disable the block flag. */
1691: flags = (tty->flags & TTY_BLOCK);
1692: tty->flags &= ~TTY_BLOCK;
1693:
1.309 nicm 1694: /* Get mode from overlay if any, else from screen. */
1695: if (c->overlay_draw != NULL) {
1.336 nicm 1696: if (c->overlay_mode != NULL)
1697: s = c->overlay_mode(c, &cx, &cy);
1698: } else
1.309 nicm 1699: s = wp->screen;
1.336 nicm 1700: if (s != NULL)
1.309 nicm 1701: mode = s->mode;
1702: log_debug("%s: client %s mode %x", __func__, c->name, mode);
1703:
1704: /* Reset region and margin. */
1.332 nicm 1705: tty_region_off(tty);
1706: tty_margin_off(tty);
1.1 nicm 1707:
1.261 nicm 1708: /* Move cursor to pane cursor and offset. */
1.309 nicm 1709: if (c->overlay_draw == NULL) {
1710: cursor = 0;
1.332 nicm 1711: tty_window_offset(tty, &ox, &oy, &sx, &sy);
1.309 nicm 1712: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1713: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1714: cursor = 1;
1.261 nicm 1715:
1.309 nicm 1716: cx = wp->xoff + s->cx - ox;
1717: cy = wp->yoff + s->cy - oy;
1.261 nicm 1718:
1.309 nicm 1719: if (status_at_line(c) == 0)
1720: cy += status_line_size(c);
1721: }
1722: if (!cursor)
1723: mode &= ~MODE_CURSOR;
1.261 nicm 1724: }
1.332 nicm 1725: log_debug("%s: cursor to %u,%u", __func__, cx, cy);
1726: tty_cursor(tty, cx, cy);
1.1 nicm 1727:
1.50 nicm 1728: /*
1.131 nicm 1729: * Set mouse mode if requested. To support dragging, always use button
1730: * mode.
1.57 nicm 1731: */
1.209 nicm 1732: if (options_get_number(oo, "mouse")) {
1.309 nicm 1733: if (c->overlay_draw == NULL) {
1.363 nicm 1734: mode &= ~ALL_MOUSE_MODES;
1.309 nicm 1735: TAILQ_FOREACH(loop, &w->panes, entry) {
1736: if (loop->screen->mode & MODE_MOUSE_ALL)
1737: mode |= MODE_MOUSE_ALL;
1738: }
1.209 nicm 1739: }
1740: if (~mode & MODE_MOUSE_ALL)
1741: mode |= MODE_MOUSE_BUTTON;
1742: }
1.215 nicm 1743:
1744: /* Clear bracketed paste mode if at the prompt. */
1.309 nicm 1745: if (c->overlay_draw == NULL && c->prompt_string != NULL)
1.215 nicm 1746: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1747:
1748: /* Set the terminal mode and reset attributes. */
1.332 nicm 1749: tty_update_mode(tty, mode, s);
1750: tty_reset(tty);
1.330 nicm 1751:
1.332 nicm 1752: /* All writing must be done, send a sync end (if it was started). */
1753: tty_sync_end(tty);
1754: tty->flags |= flags;
1.17 nicm 1755: }
1756:
1757: /* Repeat time callback. */
1.190 nicm 1758: static void
1.170 nicm 1759: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1760: {
1761: struct client *c = data;
1762:
1.85 nicm 1763: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1764: server_client_set_key_table(c, NULL);
1.132 nicm 1765: c->flags &= ~CLIENT_REPEAT;
1766: server_status_client(c);
1.85 nicm 1767: }
1.192 nicm 1768: }
1769:
1770: /* Double-click callback. */
1771: static void
1772: server_client_click_timer(__unused int fd, __unused short events, void *data)
1773: {
1.306 nicm 1774: struct client *c = data;
1775: struct key_event *event;
1776:
1777: log_debug("click timer expired");
1.192 nicm 1778:
1.306 nicm 1779: if (c->flags & CLIENT_TRIPLECLICK) {
1780: /*
1781: * Waiting for a third click that hasn't happened, so this must
1782: * have been a double click.
1783: */
1784: event = xmalloc(sizeof *event);
1785: event->key = KEYC_DOUBLECLICK;
1786: memcpy(&event->m, &c->click_event, sizeof event->m);
1787: if (!server_client_handle_key(c, event))
1788: free(event);
1789: }
1.192 nicm 1790: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1791: }
1792:
1.36 nicm 1793: /* Check if client should be exited. */
1.190 nicm 1794: static void
1.36 nicm 1795: server_client_check_exit(struct client *c)
1796: {
1.300 nicm 1797: struct client_file *cf;
1.352 nicm 1798: const char *name = c->exit_session;
1.357 nicm 1799: char *data;
1800: size_t size, msize;
1.300 nicm 1801:
1.354 nicm 1802: if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1803: return;
1804: if (~c->flags & CLIENT_EXIT)
1.36 nicm 1805: return;
1806:
1.352 nicm 1807: if (c->flags & CLIENT_CONTROL) {
1.355 nicm 1808: control_discard(c);
1.352 nicm 1809: if (!control_all_done(c))
1810: return;
1811: }
1.300 nicm 1812: RB_FOREACH(cf, client_files, &c->files) {
1813: if (EVBUFFER_LENGTH(cf->buffer) != 0)
1814: return;
1815: }
1.286 nicm 1816: c->flags |= CLIENT_EXITED;
1.352 nicm 1817:
1818: switch (c->exit_type) {
1819: case CLIENT_EXIT_RETURN:
1.367 nicm 1820: if (c->exit_message != NULL)
1.357 nicm 1821: msize = strlen(c->exit_message) + 1;
1.367 nicm 1822: else
1823: msize = 0;
1824: size = (sizeof c->retval) + msize;
1.357 nicm 1825: data = xmalloc(size);
1826: memcpy(data, &c->retval, sizeof c->retval);
1827: if (c->exit_message != NULL)
1828: memcpy(data + sizeof c->retval, c->exit_message, msize);
1829: proc_send(c->peer, MSG_EXIT, -1, data, size);
1830: free(data);
1.352 nicm 1831: break;
1832: case CLIENT_EXIT_SHUTDOWN:
1833: proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1834: break;
1835: case CLIENT_EXIT_DETACH:
1836: proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
1837: break;
1838: }
1839: free(c->exit_session);
1.357 nicm 1840: free(c->exit_message);
1.38 nicm 1841: }
1842:
1.218 nicm 1843: /* Redraw timer callback. */
1844: static void
1845: server_client_redraw_timer(__unused int fd, __unused short events,
1.299 nicm 1846: __unused void *data)
1.218 nicm 1847: {
1848: log_debug("redraw timer fired");
1.366 nicm 1849: }
1850:
1851: /*
1852: * Check if modes need to be updated. Only modes in the current window are
1853: * updated and it is done when the status line is redrawn.
1854: */
1855: static void
1856: server_client_check_modes(struct client *c)
1857: {
1858: struct window *w = c->session->curw->window;
1859: struct window_pane *wp;
1860: struct window_mode_entry *wme;
1861:
1862: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1863: return;
1864: if (~c->flags & CLIENT_REDRAWSTATUS)
1865: return;
1866: TAILQ_FOREACH(wp, &w->panes, entry) {
1867: wme = TAILQ_FIRST(&wp->modes);
1868: if (wme != NULL && wme->mode->update != NULL)
1869: wme->mode->update(wme);
1870: }
1.218 nicm 1871: }
1872:
1.1 nicm 1873: /* Check for client redraws. */
1.190 nicm 1874: static void
1.1 nicm 1875: server_client_check_redraw(struct client *c)
1876: {
1877: struct session *s = c->session;
1.137 nicm 1878: struct tty *tty = &c->tty;
1.326 nicm 1879: struct window *w = c->session->curw->window;
1.1 nicm 1880: struct window_pane *wp;
1.325 nicm 1881: int needed, flags, mode = tty->mode, new_flags = 0;
1.326 nicm 1882: int redraw;
1883: u_int bit = 0;
1.218 nicm 1884: struct timeval tv = { .tv_usec = 1000 };
1885: static struct event ev;
1886: size_t left;
1.1 nicm 1887:
1.86 nicm 1888: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1889: return;
1.257 nicm 1890: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.325 nicm 1891: log_debug("%s: redraw%s%s%s%s%s", c->name,
1.257 nicm 1892: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1893: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282 nicm 1894: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
1.325 nicm 1895: (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
1896: (c->flags & CLIENT_REDRAWPANES) ? " panes" : "");
1.257 nicm 1897: }
1.218 nicm 1898:
1899: /*
1900: * If there is outstanding data, defer the redraw until it has been
1901: * consumed. We can just add a timer to get out of the event loop and
1902: * end up back here.
1903: */
1904: needed = 0;
1.256 nicm 1905: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1906: needed = 1;
1907: else {
1.326 nicm 1908: TAILQ_FOREACH(wp, &w->panes, entry) {
1.218 nicm 1909: if (wp->flags & PANE_REDRAW) {
1910: needed = 1;
1911: break;
1912: }
1913: }
1.325 nicm 1914: if (needed)
1915: new_flags |= CLIENT_REDRAWPANES;
1.218 nicm 1916: }
1.241 nicm 1917: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1918: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1919: if (!evtimer_initialized(&ev))
1920: evtimer_set(&ev, server_client_redraw_timer, NULL);
1921: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1922: log_debug("redraw timer started");
1923: evtimer_add(&ev, &tv);
1.241 nicm 1924: }
1.327 nicm 1925:
1926: if (~c->flags & CLIENT_REDRAWWINDOW) {
1.326 nicm 1927: TAILQ_FOREACH(wp, &w->panes, entry) {
1.327 nicm 1928: if (wp->flags & PANE_REDRAW) {
1929: log_debug("%s: pane %%%u needs redraw",
1930: c->name, wp->id);
1.326 nicm 1931: c->redraw_panes |= (1 << bit);
1.327 nicm 1932: }
1.326 nicm 1933: if (++bit == 64) {
1934: /*
1935: * If more that 64 panes, give up and
1936: * just redraw the window.
1937: */
1938: new_flags &= CLIENT_REDRAWPANES;
1939: new_flags |= CLIENT_REDRAWWINDOW;
1940: break;
1941: }
1942: }
1.327 nicm 1943: if (c->redraw_panes != 0)
1944: c->flags |= CLIENT_REDRAWPANES;
1.326 nicm 1945: }
1.325 nicm 1946: c->flags |= new_flags;
1.241 nicm 1947: return;
1948: } else if (needed)
1.218 nicm 1949: log_debug("%s: redraw needed", c->name);
1.79 nicm 1950:
1.219 nicm 1951: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1.326 nicm 1952: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
1.137 nicm 1953:
1.256 nicm 1954: if (~c->flags & CLIENT_REDRAWWINDOW) {
1955: /*
1956: * If not redrawing the entire window, check whether each pane
1957: * needs to be redrawn.
1958: */
1.326 nicm 1959: TAILQ_FOREACH(wp, &w->panes, entry) {
1960: redraw = 0;
1961: if (wp->flags & PANE_REDRAW)
1962: redraw = 1;
1963: else if (c->flags & CLIENT_REDRAWPANES)
1964: redraw = !!(c->redraw_panes & (1 << bit));
1.328 nicm 1965: bit++;
1.326 nicm 1966: if (!redraw)
1967: continue;
1968: log_debug("%s: redrawing pane %%%u", __func__, wp->id);
1969: screen_redraw_pane(c, wp);
1.1 nicm 1970: }
1.331 nicm 1971: c->redraw_panes = 0;
1.325 nicm 1972: c->flags &= ~CLIENT_REDRAWPANES;
1.1 nicm 1973: }
1974:
1.256 nicm 1975: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1976: if (options_get_number(s->options, "set-titles"))
1977: server_client_set_title(c);
1978: screen_redraw_screen(c);
1979: }
1.1 nicm 1980:
1.326 nicm 1981: tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR);
1.320 nicm 1982: tty_update_mode(tty, mode, NULL);
1.326 nicm 1983: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags;
1.1 nicm 1984:
1.256 nicm 1985: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1986:
1987: if (needed) {
1988: /*
1989: * We would have deferred the redraw unless the output buffer
1990: * was empty, so we can record how many bytes the redraw
1991: * generated.
1992: */
1993: c->redraw = EVBUFFER_LENGTH(tty->out);
1994: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1995: }
1.1 nicm 1996: }
1997:
1998: /* Set client title. */
1.190 nicm 1999: static void
1.1 nicm 2000: server_client_set_title(struct client *c)
2001: {
1.128 nicm 2002: struct session *s = c->session;
2003: const char *template;
2004: char *title;
2005: struct format_tree *ft;
1.1 nicm 2006:
1.163 nicm 2007: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 2008:
1.228 nicm 2009: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 2010: format_defaults(ft, c, NULL, NULL, NULL);
2011:
1.269 nicm 2012: title = format_expand_time(ft, template);
1.1 nicm 2013: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 2014: free(c->title);
1.1 nicm 2015: c->title = xstrdup(title);
2016: tty_set_title(&c->tty, c->title);
2017: }
1.77 nicm 2018: free(title);
1.128 nicm 2019:
2020: format_free(ft);
1.1 nicm 2021: }
2022:
2023: /* Dispatch message from client. */
1.190 nicm 2024: static void
1.162 nicm 2025: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 2026: {
1.300 nicm 2027: struct client *c = arg;
2028: ssize_t datalen;
2029: struct session *s;
1.1 nicm 2030:
1.162 nicm 2031: if (c->flags & CLIENT_DEAD)
2032: return;
2033:
2034: if (imsg == NULL) {
2035: server_client_lost(c);
2036: return;
2037: }
1.1 nicm 2038:
1.162 nicm 2039: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 2040:
1.162 nicm 2041: switch (imsg->hdr.type) {
1.370 nicm 2042: case MSG_IDENTIFY_CLIENTPID:
2043: case MSG_IDENTIFY_CWD:
2044: case MSG_IDENTIFY_ENVIRON:
1.329 nicm 2045: case MSG_IDENTIFY_FEATURES:
1.162 nicm 2046: case MSG_IDENTIFY_FLAGS:
1.361 nicm 2047: case MSG_IDENTIFY_LONGFLAGS:
1.370 nicm 2048: case MSG_IDENTIFY_STDIN:
2049: case MSG_IDENTIFY_STDOUT:
1.162 nicm 2050: case MSG_IDENTIFY_TERM:
1.370 nicm 2051: case MSG_IDENTIFY_TERMINFO:
1.162 nicm 2052: case MSG_IDENTIFY_TTYNAME:
2053: case MSG_IDENTIFY_DONE:
2054: server_client_dispatch_identify(c, imsg);
2055: break;
2056: case MSG_COMMAND:
2057: server_client_dispatch_command(c, imsg);
2058: break;
2059: case MSG_RESIZE:
2060: if (datalen != 0)
2061: fatalx("bad MSG_RESIZE size");
1.1 nicm 2062:
1.162 nicm 2063: if (c->flags & CLIENT_CONTROL)
1.1 nicm 2064: break;
1.295 nicm 2065: server_client_update_latest(c);
1.282 nicm 2066: server_client_clear_overlay(c);
1.236 nicm 2067: tty_resize(&c->tty);
2068: recalculate_sizes();
2069: server_redraw_client(c);
1.174 nicm 2070: if (c->session != NULL)
1.196 nicm 2071: notify_client("client-resized", c);
1.162 nicm 2072: break;
2073: case MSG_EXITING:
2074: if (datalen != 0)
2075: fatalx("bad MSG_EXITING size");
2076: c->session = NULL;
2077: tty_close(&c->tty);
2078: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
2079: break;
2080: case MSG_WAKEUP:
2081: case MSG_UNLOCK:
2082: if (datalen != 0)
2083: fatalx("bad MSG_WAKEUP size");
1.121 nicm 2084:
1.162 nicm 2085: if (!(c->flags & CLIENT_SUSPENDED))
2086: break;
2087: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 2088:
1.365 nicm 2089: if (c->fd == -1 || c->session == NULL) /* exited already */
1.1 nicm 2090: break;
1.162 nicm 2091: s = c->session;
1.1 nicm 2092:
1.162 nicm 2093: if (gettimeofday(&c->activity_time, NULL) != 0)
2094: fatal("gettimeofday failed");
2095:
2096: tty_start_tty(&c->tty);
2097: server_redraw_client(c);
2098: recalculate_sizes();
1.184 nicm 2099:
2100: if (s != NULL)
2101: session_update_activity(s, &c->activity_time);
1.162 nicm 2102: break;
2103: case MSG_SHELL:
2104: if (datalen != 0)
2105: fatalx("bad MSG_SHELL size");
1.1 nicm 2106:
1.162 nicm 2107: server_client_dispatch_shell(c);
2108: break;
1.300 nicm 2109: case MSG_WRITE_READY:
1.369 nicm 2110: file_write_ready(&c->files, imsg);
1.300 nicm 2111: break;
2112: case MSG_READ:
1.369 nicm 2113: file_read_data(&c->files, imsg);
1.300 nicm 2114: break;
2115: case MSG_READ_DONE:
1.369 nicm 2116: file_read_done(&c->files, imsg);
1.300 nicm 2117: break;
1.1 nicm 2118: }
2119: }
2120:
1.194 nicm 2121: /* Callback when command is done. */
2122: static enum cmd_retval
1.195 nicm 2123: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 2124: {
1.316 nicm 2125: struct client *c = cmdq_get_client(item);
1.194 nicm 2126:
2127: if (~c->flags & CLIENT_ATTACHED)
2128: c->flags |= CLIENT_EXIT;
1.352 nicm 2129: else if (~c->flags & CLIENT_EXIT)
1.314 nicm 2130: tty_send_requests(&c->tty);
1.194 nicm 2131: return (CMD_RETURN_NORMAL);
2132: }
2133:
1.1 nicm 2134: /* Handle command message. */
1.190 nicm 2135: static void
1.162 nicm 2136: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 2137: {
1.300 nicm 2138: struct msg_command data;
1.108 nicm 2139: char *buf;
2140: size_t len;
2141: int argc;
2142: char **argv, *cause;
1.285 nicm 2143: struct cmd_parse_result *pr;
1.246 nicm 2144:
2145: if (c->flags & CLIENT_EXIT)
2146: return;
1.108 nicm 2147:
2148: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
2149: fatalx("bad MSG_COMMAND size");
2150: memcpy(&data, imsg->data, sizeof data);
2151:
1.124 nicm 2152: buf = (char *)imsg->data + sizeof data;
1.108 nicm 2153: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
2154: if (len > 0 && buf[len - 1] != '\0')
2155: fatalx("bad MSG_COMMAND string");
2156:
2157: argc = data.argc;
2158: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 2159: cause = xstrdup("command too long");
1.1 nicm 2160: goto error;
2161: }
2162:
2163: if (argc == 0) {
2164: argc = 1;
2165: argv = xcalloc(1, sizeof *argv);
2166: *argv = xstrdup("new-session");
2167: }
2168:
1.285 nicm 2169: pr = cmd_parse_from_arguments(argc, argv, NULL);
2170: switch (pr->status) {
2171: case CMD_PARSE_EMPTY:
2172: cause = xstrdup("empty command");
1.1 nicm 2173: goto error;
1.285 nicm 2174: case CMD_PARSE_ERROR:
2175: cause = pr->error;
2176: goto error;
2177: case CMD_PARSE_SUCCESS:
2178: break;
1.1 nicm 2179: }
2180: cmd_free_argv(argc, argv);
2181:
1.318 nicm 2182: cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL));
1.194 nicm 2183: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.285 nicm 2184:
2185: cmd_list_free(pr->cmdlist);
1.1 nicm 2186: return;
2187:
2188: error:
1.285 nicm 2189: cmd_free_argv(argc, argv);
2190:
1.284 nicm 2191: cmdq_append(c, cmdq_get_error(cause));
2192: free(cause);
1.88 nicm 2193:
1.36 nicm 2194: c->flags |= CLIENT_EXIT;
1.1 nicm 2195: }
2196:
2197: /* Handle identify message. */
1.190 nicm 2198: static void
1.162 nicm 2199: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 2200: {
1.165 nicm 2201: const char *data, *home;
1.232 nicm 2202: size_t datalen;
1.329 nicm 2203: int flags, feat;
1.361 nicm 2204: uint64_t longflags;
1.216 nicm 2205: char *name;
1.109 nicm 2206:
2207: if (c->flags & CLIENT_IDENTIFIED)
2208: fatalx("out-of-order identify message");
2209:
2210: data = imsg->data;
2211: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2212:
2213: switch (imsg->hdr.type) {
1.329 nicm 2214: case MSG_IDENTIFY_FEATURES:
2215: if (datalen != sizeof feat)
2216: fatalx("bad MSG_IDENTIFY_FEATURES size");
2217: memcpy(&feat, data, sizeof feat);
2218: c->term_features |= feat;
2219: log_debug("client %p IDENTIFY_FEATURES %s", c,
2220: tty_get_features(feat));
2221: break;
1.109 nicm 2222: case MSG_IDENTIFY_FLAGS:
2223: if (datalen != sizeof flags)
2224: fatalx("bad MSG_IDENTIFY_FLAGS size");
2225: memcpy(&flags, data, sizeof flags);
2226: c->flags |= flags;
1.158 nicm 2227: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.361 nicm 2228: break;
2229: case MSG_IDENTIFY_LONGFLAGS:
2230: if (datalen != sizeof longflags)
2231: fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2232: memcpy(&longflags, data, sizeof longflags);
2233: c->flags |= longflags;
2234: log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
2235: (unsigned long long)longflags);
1.109 nicm 2236: break;
2237: case MSG_IDENTIFY_TERM:
1.110 nicm 2238: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2239: fatalx("bad MSG_IDENTIFY_TERM string");
1.329 nicm 2240: if (*data == '\0')
2241: c->term_name = xstrdup("unknown");
2242: else
2243: c->term_name = xstrdup(data);
1.158 nicm 2244: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.370 nicm 2245: break;
2246: case MSG_IDENTIFY_TERMINFO:
2247: if (datalen == 0 || data[datalen - 1] != '\0')
2248: fatalx("bad MSG_IDENTIFY_TERMINFO string");
2249: c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
2250: sizeof *c->term_caps);
2251: c->term_caps[c->term_ncaps++] = xstrdup(data);
2252: log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
1.109 nicm 2253: break;
2254: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 2255: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2256: fatalx("bad MSG_IDENTIFY_TTYNAME string");
2257: c->ttyname = xstrdup(data);
1.158 nicm 2258: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 2259: break;
2260: case MSG_IDENTIFY_CWD:
1.155 nicm 2261: if (datalen == 0 || data[datalen - 1] != '\0')
2262: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 2263: if (access(data, X_OK) == 0)
2264: c->cwd = xstrdup(data);
2265: else if ((home = find_home()) != NULL)
2266: c->cwd = xstrdup(home);
2267: else
2268: c->cwd = xstrdup("/");
1.158 nicm 2269: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 2270: break;
2271: case MSG_IDENTIFY_STDIN:
2272: if (datalen != 0)
2273: fatalx("bad MSG_IDENTIFY_STDIN size");
2274: c->fd = imsg->fd;
1.158 nicm 2275: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 2276: break;
1.351 nicm 2277: case MSG_IDENTIFY_STDOUT:
2278: if (datalen != 0)
2279: fatalx("bad MSG_IDENTIFY_STDOUT size");
2280: c->out_fd = imsg->fd;
2281: log_debug("client %p IDENTIFY_STDOUT %d", c, imsg->fd);
2282: break;
1.109 nicm 2283: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 2284: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2285: fatalx("bad MSG_IDENTIFY_ENVIRON string");
2286: if (strchr(data, '=') != NULL)
1.312 nicm 2287: environ_put(c->environ, data, 0);
1.158 nicm 2288: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 2289: break;
2290: case MSG_IDENTIFY_CLIENTPID:
2291: if (datalen != sizeof c->pid)
2292: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
2293: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 2294: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 2295: break;
2296: default:
2297: break;
2298: }
2299:
2300: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
2301: return;
2302: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 2303:
1.216 nicm 2304: if (*c->ttyname != '\0')
2305: name = xstrdup(c->ttyname);
2306: else
2307: xasprintf(&name, "client-%ld", (long)c->pid);
2308: c->name = name;
2309: log_debug("client %p name is %s", c, c->name);
2310:
1.362 nicm 2311: if (c->flags & CLIENT_CONTROL)
1.300 nicm 2312: control_start(c);
1.351 nicm 2313: else if (c->fd != -1) {
1.348 nicm 2314: if (tty_init(&c->tty, c) != 0) {
1.288 nicm 2315: close(c->fd);
2316: c->fd = -1;
2317: } else {
2318: tty_resize(&c->tty);
2319: c->flags |= CLIENT_TERMINAL;
2320: }
1.351 nicm 2321: close(c->out_fd);
2322: c->out_fd = -1;
1.75 nicm 2323: }
1.1 nicm 2324:
1.288 nicm 2325: /*
1.362 nicm 2326: * If this is the first client, load configuration files. Any later
2327: * clients are allowed to continue with their command even if the
2328: * config has not been loaded - they might have been run from inside it
1.288 nicm 2329: */
2330: if ((~c->flags & CLIENT_EXIT) &&
1.362 nicm 2331: !cfg_finished &&
2332: c == TAILQ_FIRST(&clients))
1.288 nicm 2333: start_cfg();
1.1 nicm 2334: }
2335:
2336: /* Handle shell message. */
1.190 nicm 2337: static void
1.162 nicm 2338: server_client_dispatch_shell(struct client *c)
1.1 nicm 2339: {
1.107 nicm 2340: const char *shell;
1.25 nicm 2341:
1.163 nicm 2342: shell = options_get_string(global_s_options, "default-shell");
1.308 nicm 2343: if (!checkshell(shell))
1.1 nicm 2344: shell = _PATH_BSHELL;
1.240 nicm 2345: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 2346:
1.162 nicm 2347: proc_kill_peer(c->peer);
1.213 nicm 2348: }
2349:
2350: /* Get client working directory. */
2351: const char *
1.250 nicm 2352: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2353: {
1.250 nicm 2354: const char *home;
1.213 nicm 2355:
1.265 nicm 2356: if (!cfg_finished && cfg_client != NULL)
2357: return (cfg_client->cwd);
1.213 nicm 2358: if (c != NULL && c->session == NULL && c->cwd != NULL)
2359: return (c->cwd);
1.250 nicm 2360: if (s != NULL && s->cwd != NULL)
2361: return (s->cwd);
1.213 nicm 2362: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2363: return (s->cwd);
1.250 nicm 2364: if ((home = find_home()) != NULL)
2365: return (home);
2366: return ("/");
1.337 nicm 2367: }
2368:
1.355 nicm 2369: /* Get control client flags. */
2370: static uint64_t
2371: server_client_control_flags(struct client *c, const char *next)
2372: {
2373: if (strcmp(next, "pause-after") == 0) {
2374: c->pause_age = 0;
2375: return (CLIENT_CONTROL_PAUSEAFTER);
2376: }
2377: if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
2378: c->pause_age *= 1000;
2379: return (CLIENT_CONTROL_PAUSEAFTER);
2380: }
2381: if (strcmp(next, "no-output") == 0)
2382: return (CLIENT_CONTROL_NOOUTPUT);
1.358 nicm 2383: if (strcmp(next, "wait-exit") == 0)
2384: return (CLIENT_CONTROL_WAITEXIT);
1.355 nicm 2385: return (0);
2386: }
2387:
1.337 nicm 2388: /* Set client flags. */
2389: void
2390: server_client_set_flags(struct client *c, const char *flags)
2391: {
2392: char *s, *copy, *next;
1.351 nicm 2393: uint64_t flag;
2394: int not;
1.337 nicm 2395:
2396: s = copy = xstrdup (flags);
2397: while ((next = strsep(&s, ",")) != NULL) {
2398: not = (*next == '!');
2399: if (not)
2400: next++;
2401:
1.355 nicm 2402: if (c->flags & CLIENT_CONTROL)
2403: flag = server_client_control_flags(c, next);
2404: else
2405: flag = 0;
1.350 nicm 2406: if (strcmp(next, "read-only") == 0)
1.337 nicm 2407: flag = CLIENT_READONLY;
2408: else if (strcmp(next, "ignore-size") == 0)
2409: flag = CLIENT_IGNORESIZE;
1.341 nicm 2410: else if (strcmp(next, "active-pane") == 0)
2411: flag = CLIENT_ACTIVEPANE;
1.350 nicm 2412: if (flag == 0)
1.337 nicm 2413: continue;
2414:
2415: log_debug("client %s set flag %s", c->name, next);
2416: if (not)
2417: c->flags &= ~flag;
2418: else
2419: c->flags |= flag;
1.346 nicm 2420: if (flag == CLIENT_CONTROL_NOOUTPUT)
1.352 nicm 2421: control_reset_offsets(c);
1.337 nicm 2422: }
2423: free(copy);
1.358 nicm 2424: proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
1.337 nicm 2425: }
2426:
1.340 nicm 2427: /* Get client flags. This is only flags useful to show to users. */
1.337 nicm 2428: const char *
2429: server_client_get_flags(struct client *c)
2430: {
1.355 nicm 2431: static char s[256];
2432: char tmp[32];
1.337 nicm 2433:
2434: *s = '\0';
2435: if (c->flags & CLIENT_ATTACHED)
2436: strlcat(s, "attached,", sizeof s);
1.368 nicm 2437: if (c->flags & CLIENT_FOCUSED)
2438: strlcat(s, "focused,", sizeof s);
1.337 nicm 2439: if (c->flags & CLIENT_CONTROL)
2440: strlcat(s, "control-mode,", sizeof s);
2441: if (c->flags & CLIENT_IGNORESIZE)
2442: strlcat(s, "ignore-size,", sizeof s);
2443: if (c->flags & CLIENT_CONTROL_NOOUTPUT)
2444: strlcat(s, "no-output,", sizeof s);
1.358 nicm 2445: if (c->flags & CLIENT_CONTROL_WAITEXIT)
2446: strlcat(s, "wait-exit,", sizeof s);
1.355 nicm 2447: if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
2448: xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
2449: c->pause_age / 1000);
2450: strlcat(s, tmp, sizeof s);
2451: }
1.337 nicm 2452: if (c->flags & CLIENT_READONLY)
2453: strlcat(s, "read-only,", sizeof s);
1.341 nicm 2454: if (c->flags & CLIENT_ACTIVEPANE)
2455: strlcat(s, "active-pane,", sizeof s);
1.337 nicm 2456: if (c->flags & CLIENT_SUSPENDED)
2457: strlcat(s, "suspended,", sizeof s);
2458: if (c->flags & CLIENT_UTF8)
2459: strlcat(s, "UTF-8,", sizeof s);
2460: if (*s != '\0')
2461: s[strlen(s) - 1] = '\0';
2462: return (s);
1.341 nicm 2463: }
2464:
2465: /* Get client window. */
2466: static struct client_window *
2467: server_client_get_client_window(struct client *c, u_int id)
2468: {
2469: struct client_window cw = { .window = id };
2470:
2471: return (RB_FIND(client_windows, &c->windows, &cw));
2472: }
2473:
2474: /* Get client active pane. */
2475: struct window_pane *
2476: server_client_get_pane(struct client *c)
2477: {
2478: struct session *s = c->session;
2479: struct client_window *cw;
2480:
2481: if (s == NULL)
2482: return (NULL);
2483:
2484: if (~c->flags & CLIENT_ACTIVEPANE)
2485: return (s->curw->window->active);
2486: cw = server_client_get_client_window(c, s->curw->window->id);
2487: if (cw == NULL)
2488: return (s->curw->window->active);
2489: return (cw->pane);
2490: }
2491:
2492: /* Set client active pane. */
2493: void
2494: server_client_set_pane(struct client *c, struct window_pane *wp)
2495: {
2496: struct session *s = c->session;
2497: struct client_window *cw;
2498:
2499: if (s == NULL)
2500: return;
2501:
2502: cw = server_client_get_client_window(c, s->curw->window->id);
2503: if (cw == NULL) {
2504: cw = xcalloc(1, sizeof *cw);
2505: cw->window = s->curw->window->id;
2506: RB_INSERT(client_windows, &c->windows, cw);
2507: }
2508: cw->pane = wp;
2509: log_debug("%s pane now %%%u", c->name, wp->id);
2510: }
2511:
2512: /* Remove pane from client lists. */
2513: void
2514: server_client_remove_pane(struct window_pane *wp)
2515: {
2516: struct client *c;
2517: struct window *w = wp->window;
2518: struct client_window *cw;
2519:
2520: TAILQ_FOREACH(c, &clients, entry) {
2521: cw = server_client_get_client_window(c, w->id);
2522: if (cw != NULL && cw->pane == wp) {
2523: RB_REMOVE(client_windows, &c->windows, cw);
2524: free(cw);
2525: }
2526: }
1.1 nicm 2527: }