Annotation of src/usr.bin/tmux/server-client.c, Revision 1.282
1.282 ! nicm 1: /* $OpenBSD: server-client.c,v 1.281 2019/05/07 20:01:41 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 *);
36: static void server_client_check_focus(struct window_pane *);
37: static void server_client_check_resize(struct window_pane *);
1.276 nicm 38: static key_code server_client_check_mouse(struct client *, struct key_event *);
1.190 nicm 39: static void server_client_repeat_timer(int, short, void *);
1.192 nicm 40: static void server_client_click_timer(int, short, void *);
1.190 nicm 41: static void server_client_check_exit(struct client *);
42: static void server_client_check_redraw(struct client *);
43: static void server_client_set_title(struct client *);
44: static void server_client_reset_state(struct client *);
45: static int server_client_assume_paste(struct session *);
1.281 nicm 46: static void server_client_clear_overlay(struct client *);
1.190 nicm 47:
48: static void server_client_dispatch(struct imsg *, void *);
49: static void server_client_dispatch_command(struct client *, struct imsg *);
50: static void server_client_dispatch_identify(struct client *, struct imsg *);
51: static void server_client_dispatch_shell(struct client *);
1.233 nicm 52:
53: /* Number of attached clients. */
54: u_int
55: server_client_how_many(void)
56: {
57: struct client *c;
58: u_int n;
59:
60: n = 0;
61: TAILQ_FOREACH(c, &clients, entry) {
62: if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
63: n++;
64: }
65: return (n);
66: }
1.140 nicm 67:
1.281 nicm 68: /* Overlay timer callback. */
1.214 nicm 69: static void
1.281 nicm 70: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214 nicm 71: {
1.281 nicm 72: server_client_clear_overlay(data);
1.214 nicm 73: }
74:
1.281 nicm 75: /* Set an overlay on client. */
1.214 nicm 76: void
1.281 nicm 77: server_client_set_overlay(struct client *c, u_int delay, overlay_draw_cb drawcb,
78: overlay_key_cb keycb, overlay_free_cb freecb, void *data)
1.214 nicm 79: {
80: struct timeval tv;
81:
1.282 ! nicm 82: if (c->overlay_draw != NULL)
! 83: server_client_clear_overlay(c);
! 84:
1.214 nicm 85: tv.tv_sec = delay / 1000;
86: tv.tv_usec = (delay % 1000) * 1000L;
87:
1.281 nicm 88: if (event_initialized(&c->overlay_timer))
89: evtimer_del(&c->overlay_timer);
90: evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242 nicm 91: if (delay != 0)
1.281 nicm 92: evtimer_add(&c->overlay_timer, &tv);
93:
94: c->overlay_draw = drawcb;
95: c->overlay_key = keycb;
96: c->overlay_free = freecb;
97: c->overlay_data = data;
1.214 nicm 98:
99: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
100: server_redraw_client(c);
101: }
102:
1.281 nicm 103: /* Clear overlay mode on client. */
1.254 nicm 104: static void
1.281 nicm 105: server_client_clear_overlay(struct client *c)
1.214 nicm 106: {
1.281 nicm 107: if (c->overlay_draw == NULL)
1.214 nicm 108: return;
109:
1.281 nicm 110: if (event_initialized(&c->overlay_timer))
111: evtimer_del(&c->overlay_timer);
112:
113: if (c->overlay_free != NULL)
114: c->overlay_free(c);
115:
116: c->overlay_draw = NULL;
117: c->overlay_key = NULL;
1.214 nicm 118:
119: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
120: server_redraw_client(c);
121: }
122:
1.140 nicm 123: /* Check if this client is inside this server. */
124: int
125: server_client_check_nested(struct client *c)
126: {
127: struct environ_entry *envent;
128: struct window_pane *wp;
129:
1.164 nicm 130: envent = environ_find(c->environ, "TMUX");
1.140 nicm 131: if (envent == NULL || *envent->value == '\0')
132: return (0);
133:
134: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 135: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 136: return (1);
137: }
138: return (0);
139: }
1.1 nicm 140:
1.132 nicm 141: /* Set client key table. */
142: void
1.178 nicm 143: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 144: {
1.178 nicm 145: if (name == NULL)
146: name = server_client_get_key_table(c);
147:
1.132 nicm 148: key_bindings_unref_table(c->keytable);
149: c->keytable = key_bindings_get_table(name, 1);
150: c->keytable->references++;
151: }
152:
1.178 nicm 153: /* Get default key table. */
154: const char *
155: server_client_get_key_table(struct client *c)
156: {
157: struct session *s = c->session;
158: const char *name;
159:
160: if (s == NULL)
161: return ("root");
162:
163: name = options_get_string(s->options, "key-table");
164: if (*name == '\0')
165: return ("root");
166: return (name);
167: }
168:
1.223 nicm 169: /* Is this table the default key table? */
170: static int
171: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191 nicm 172: {
1.223 nicm 173: return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191 nicm 174: }
175:
1.1 nicm 176: /* Create a new client. */
1.246 nicm 177: struct client *
1.1 nicm 178: server_client_create(int fd)
179: {
180: struct client *c;
181:
1.49 nicm 182: setblocking(fd, 0);
1.1 nicm 183:
184: c = xcalloc(1, sizeof *c);
1.141 nicm 185: c->references = 1;
1.162 nicm 186: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 187:
1.10 nicm 188: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 189: fatal("gettimeofday failed");
1.11 nicm 190: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 191:
1.164 nicm 192: c->environ = environ_create();
1.1 nicm 193:
1.146 nicm 194: c->fd = -1;
1.165 nicm 195: c->cwd = NULL;
1.145 nicm 196:
1.194 nicm 197: TAILQ_INIT(&c->queue);
1.94 nicm 198:
1.116 nicm 199: c->stdin_data = evbuffer_new();
1.263 nicm 200: if (c->stdin_data == NULL)
201: fatalx("out of memory");
1.116 nicm 202: c->stdout_data = evbuffer_new();
1.263 nicm 203: if (c->stdout_data == NULL)
204: fatalx("out of memory");
1.116 nicm 205: c->stderr_data = evbuffer_new();
1.263 nicm 206: if (c->stderr_data == NULL)
207: fatalx("out of memory");
1.33 nicm 208:
1.1 nicm 209: c->tty.fd = -1;
210: c->title = NULL;
211:
212: c->session = NULL;
1.45 nicm 213: c->last_session = NULL;
1.261 nicm 214:
1.1 nicm 215: c->tty.sx = 80;
216: c->tty.sy = 24;
217:
1.271 nicm 218: status_init(c);
1.1 nicm 219:
220: c->message_string = NULL;
1.136 nicm 221: TAILQ_INIT(&c->message_log);
1.1 nicm 222:
223: c->prompt_string = NULL;
224: c->prompt_buffer = NULL;
225: c->prompt_index = 0;
226:
1.93 nicm 227: c->flags |= CLIENT_FOCUSED;
228:
1.132 nicm 229: c->keytable = key_bindings_get_table("root", 1);
230: c->keytable->references++;
231:
1.17 nicm 232: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 233: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 234:
1.135 nicm 235: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 236: log_debug("new client %p", c);
1.246 nicm 237: return (c);
1.72 nicm 238: }
239:
240: /* Open client terminal if needed. */
241: int
1.119 nicm 242: server_client_open(struct client *c, char **cause)
1.72 nicm 243: {
1.75 nicm 244: if (c->flags & CLIENT_CONTROL)
245: return (0);
1.120 nicm 246:
247: if (strcmp(c->ttyname, "/dev/tty") == 0) {
248: *cause = xstrdup("can't use /dev/tty");
249: return (-1);
250: }
1.75 nicm 251:
1.72 nicm 252: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 253: *cause = xstrdup("not a terminal");
1.72 nicm 254: return (-1);
255: }
256:
1.119 nicm 257: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 258: return (-1);
259:
260: return (0);
1.1 nicm 261: }
262:
263: /* Lost a client. */
264: void
265: server_client_lost(struct client *c)
266: {
1.136 nicm 267: struct message_entry *msg, *msg1;
1.1 nicm 268:
1.141 nicm 269: c->flags |= CLIENT_DEAD;
270:
1.281 nicm 271: server_client_clear_overlay(c);
1.141 nicm 272: status_prompt_clear(c);
273: status_message_clear(c);
274:
275: if (c->stdin_callback != NULL)
276: c->stdin_callback(c, 1, c->stdin_callback_data);
277:
1.135 nicm 278: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 279: log_debug("lost client %p", c);
1.1 nicm 280:
281: /*
282: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
283: * and tty_free might close an unrelated fd.
284: */
285: if (c->flags & CLIENT_TERMINAL)
286: tty_free(&c->tty);
1.109 nicm 287: free(c->ttyname);
288: free(c->term);
1.1 nicm 289:
1.113 nicm 290: evbuffer_free(c->stdin_data);
291: evbuffer_free(c->stdout_data);
1.97 nicm 292: if (c->stderr_data != c->stdout_data)
1.126 nicm 293: evbuffer_free(c->stderr_data);
1.33 nicm 294:
1.270 nicm 295: status_free(c);
1.1 nicm 296:
1.77 nicm 297: free(c->title);
1.165 nicm 298: free((void *)c->cwd);
1.1 nicm 299:
1.17 nicm 300: evtimer_del(&c->repeat_timer);
1.192 nicm 301: evtimer_del(&c->click_timer);
1.17 nicm 302:
1.132 nicm 303: key_bindings_unref_table(c->keytable);
304:
1.77 nicm 305: free(c->message_string);
1.116 nicm 306: if (event_initialized(&c->message_timer))
1.69 nicm 307: evtimer_del(&c->message_timer);
1.136 nicm 308: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 309: free(msg->msg);
1.136 nicm 310: TAILQ_REMOVE(&c->message_log, msg, entry);
311: free(msg);
1.21 nicm 312: }
1.1 nicm 313:
1.259 nicm 314: free(c->prompt_saved);
1.77 nicm 315: free(c->prompt_string);
316: free(c->prompt_buffer);
1.61 nicm 317:
1.228 nicm 318: format_lost_client(c);
1.164 nicm 319: environ_free(c->environ);
1.1 nicm 320:
1.162 nicm 321: proc_remove_peer(c->peer);
322: c->peer = NULL;
1.13 nicm 323:
1.142 nicm 324: server_client_unref(c);
1.71 nicm 325:
326: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 327:
328: recalculate_sizes();
1.41 nicm 329: server_check_unattached();
1.19 nicm 330: server_update_socket();
1.141 nicm 331: }
332:
333: /* Remove reference from a client. */
334: void
1.142 nicm 335: server_client_unref(struct client *c)
1.141 nicm 336: {
1.157 nicm 337: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 338:
339: c->references--;
340: if (c->references == 0)
341: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
342: }
343:
344: /* Free dead client. */
1.190 nicm 345: static void
1.170 nicm 346: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 347: {
348: struct client *c = arg;
349:
1.157 nicm 350: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 351:
1.194 nicm 352: if (!TAILQ_EMPTY(&c->queue))
353: fatalx("queue not empty");
354:
1.216 nicm 355: if (c->references == 0) {
356: free((void *)c->name);
1.141 nicm 357: free(c);
1.216 nicm 358: }
1.9 nicm 359: }
360:
1.220 nicm 361: /* Suspend a client. */
362: void
363: server_client_suspend(struct client *c)
364: {
1.232 nicm 365: struct session *s = c->session;
1.220 nicm 366:
367: if (s == NULL || (c->flags & CLIENT_DETACHING))
368: return;
369:
370: tty_stop_tty(&c->tty);
371: c->flags |= CLIENT_SUSPENDED;
372: proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
373: }
374:
1.174 nicm 375: /* Detach a client. */
376: void
377: server_client_detach(struct client *c, enum msgtype msgtype)
378: {
1.232 nicm 379: struct session *s = c->session;
1.174 nicm 380:
1.220 nicm 381: if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174 nicm 382: return;
383:
1.220 nicm 384: c->flags |= CLIENT_DETACHING;
1.196 nicm 385: notify_client("client-detached", c);
1.240 nicm 386: proc_send(c->peer, msgtype, -1, s->name, strlen(s->name) + 1);
1.207 nicm 387: }
388:
1.208 nicm 389: /* Execute command to replace a client. */
1.207 nicm 390: void
391: server_client_exec(struct client *c, const char *cmd)
392: {
393: struct session *s = c->session;
1.208 nicm 394: char *msg;
395: const char *shell;
1.207 nicm 396: size_t cmdsize, shellsize;
397:
398: if (*cmd == '\0')
399: return;
400: cmdsize = strlen(cmd) + 1;
401:
402: if (s != NULL)
403: shell = options_get_string(s->options, "default-shell");
404: else
405: shell = options_get_string(global_s_options, "default-shell");
406: shellsize = strlen(shell) + 1;
407:
408: msg = xmalloc(cmdsize + shellsize);
409: memcpy(msg, cmd, cmdsize);
410: memcpy(msg + cmdsize, shell, shellsize);
411:
412: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
413: free(msg);
1.174 nicm 414: }
415:
1.66 nicm 416: /* Check for mouse keys. */
1.190 nicm 417: static key_code
1.276 nicm 418: server_client_check_mouse(struct client *c, struct key_event *event)
1.66 nicm 419: {
1.276 nicm 420: struct mouse_event *m = &event->m;
1.192 nicm 421: struct session *s = c->session;
1.272 nicm 422: struct winlink *wl;
1.192 nicm 423: struct window_pane *wp;
1.261 nicm 424: u_int x, y, b, sx, sy, px, py;
1.192 nicm 425: int flag;
426: key_code key;
427: struct timeval tv;
1.272 nicm 428: struct style_range *sr;
1.209 nicm 429: enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
1.276 nicm 430: enum { NOWHERE,
431: PANE,
432: STATUS,
433: STATUS_LEFT,
434: STATUS_RIGHT,
435: STATUS_DEFAULT,
436: BORDER } where;
1.209 nicm 437:
438: type = NOTYPE;
439: where = NOWHERE;
1.131 nicm 440:
1.261 nicm 441: log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
442: m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131 nicm 443:
444: /* What type of event is this? */
1.209 nicm 445: if ((m->sgr_type != ' ' &&
446: MOUSE_DRAG(m->sgr_b) &&
447: MOUSE_BUTTONS(m->sgr_b) == 3) ||
448: (m->sgr_type == ' ' &&
449: MOUSE_DRAG(m->b) &&
450: MOUSE_BUTTONS(m->b) == 3 &&
451: MOUSE_BUTTONS(m->lb) == 3)) {
452: type = MOVE;
453: x = m->x, y = m->y, b = 0;
454: log_debug("move at %u,%u", x, y);
455: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 456: type = DRAG;
457: if (c->tty.mouse_drag_flag) {
458: x = m->x, y = m->y, b = m->b;
1.278 nicm 459: if (x == m->lx && y == m->ly)
460: return (KEYC_UNKNOWN);
1.131 nicm 461: log_debug("drag update at %u,%u", x, y);
462: } else {
1.277 nicm 463: x = m->lx, y = m->ly, b = m->lb;
1.131 nicm 464: log_debug("drag start at %u,%u", x, y);
465: }
466: } else if (MOUSE_WHEEL(m->b)) {
467: type = WHEEL;
468: x = m->x, y = m->y, b = m->b;
469: log_debug("wheel at %u,%u", x, y);
1.200 nicm 470: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 471: type = UP;
472: x = m->x, y = m->y, b = m->lb;
473: log_debug("up at %u,%u", x, y);
474: } else {
1.192 nicm 475: if (c->flags & CLIENT_DOUBLECLICK) {
476: evtimer_del(&c->click_timer);
477: c->flags &= ~CLIENT_DOUBLECLICK;
478: if (m->b == c->click_button) {
479: type = DOUBLE;
480: x = m->x, y = m->y, b = m->b;
481: log_debug("double-click at %u,%u", x, y);
482: flag = CLIENT_TRIPLECLICK;
483: goto add_timer;
484: }
485: } else if (c->flags & CLIENT_TRIPLECLICK) {
486: evtimer_del(&c->click_timer);
487: c->flags &= ~CLIENT_TRIPLECLICK;
488: if (m->b == c->click_button) {
489: type = TRIPLE;
490: x = m->x, y = m->y, b = m->b;
491: log_debug("triple-click at %u,%u", x, y);
492: goto have_event;
493: }
494: }
495:
1.131 nicm 496: type = DOWN;
497: x = m->x, y = m->y, b = m->b;
498: log_debug("down at %u,%u", x, y);
1.192 nicm 499: flag = CLIENT_DOUBLECLICK;
500:
501: add_timer:
502: if (KEYC_CLICK_TIMEOUT != 0) {
503: c->flags |= flag;
504: c->click_button = m->b;
505:
506: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
507: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
508: evtimer_del(&c->click_timer);
509: evtimer_add(&c->click_timer, &tv);
510: }
1.131 nicm 511: }
1.192 nicm 512:
513: have_event:
1.131 nicm 514: if (type == NOTYPE)
1.177 nicm 515: return (KEYC_UNKNOWN);
1.131 nicm 516:
1.258 nicm 517: /* Save the session. */
1.131 nicm 518: m->s = s->id;
1.258 nicm 519: m->w = -1;
1.131 nicm 520:
521: /* Is this on the status line? */
522: m->statusat = status_at_line(c);
1.272 nicm 523: if (m->statusat != -1 &&
524: y >= (u_int)m->statusat &&
1.273 nicm 525: y < m->statusat + status_line_size(c)) {
1.272 nicm 526: sr = status_get_range(c, x, y - m->statusat);
1.274 nicm 527: if (sr == NULL) {
528: where = STATUS_DEFAULT;
529: } else {
530: switch (sr->type) {
531: case STYLE_RANGE_NONE:
1.273 nicm 532: return (KEYC_UNKNOWN);
1.274 nicm 533: case STYLE_RANGE_LEFT:
534: where = STATUS_LEFT;
535: break;
536: case STYLE_RANGE_RIGHT:
537: where = STATUS_RIGHT;
538: break;
539: case STYLE_RANGE_WINDOW:
540: wl = winlink_find_by_index(&s->windows, sr->argument);
541: if (wl == NULL)
542: return (KEYC_UNKNOWN);
543: m->w = wl->window->id;
1.273 nicm 544:
1.274 nicm 545: where = STATUS;
546: break;
547: }
1.258 nicm 548: }
549: }
1.131 nicm 550:
551: /* Not on status line. Adjust position and check for border or pane. */
552: if (where == NOWHERE) {
1.261 nicm 553: px = x;
1.131 nicm 554: if (m->statusat == 0 && y > 0)
1.261 nicm 555: py = y - 1;
1.131 nicm 556: else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261 nicm 557: py = m->statusat - 1;
558: else
559: py = y;
560:
561: tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
562: log_debug("mouse window @%u at %u,%u (%ux%u)",
563: s->curw->window->id, m->ox, m->oy, sx, sy);
564: if (px > sx || py > sy)
565: return (KEYC_UNKNOWN);
566: px = px + m->ox;
567: py = py + m->oy;
1.131 nicm 568:
1.260 nicm 569: /* Try the pane borders if not zoomed. */
570: if (~s->curw->window->flags & WINDOW_ZOOMED) {
571: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261 nicm 572: if ((wp->xoff + wp->sx == px &&
573: wp->yoff <= 1 + py &&
574: wp->yoff + wp->sy >= py) ||
575: (wp->yoff + wp->sy == py &&
576: wp->xoff <= 1 + px &&
577: wp->xoff + wp->sx >= px))
1.260 nicm 578: break;
579: }
580: if (wp != NULL)
581: where = BORDER;
1.131 nicm 582: }
1.260 nicm 583:
584: /* Otherwise try inside the pane. */
585: if (where == NOWHERE) {
1.261 nicm 586: wp = window_get_active_at(s->curw->window, px, py);
1.260 nicm 587: if (wp != NULL)
1.131 nicm 588: where = PANE;
589: }
1.260 nicm 590:
1.131 nicm 591: if (where == NOWHERE)
1.177 nicm 592: return (KEYC_UNKNOWN);
1.260 nicm 593: if (where == PANE)
594: log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
595: else if (where == BORDER)
596: log_debug("mouse on pane %%%u border", wp->id);
1.131 nicm 597: m->wp = wp->id;
598: m->w = wp->window->id;
599: } else
600: m->wp = -1;
601:
602: /* Stop dragging if needed. */
1.200 nicm 603: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 604: if (c->tty.mouse_drag_release != NULL)
605: c->tty.mouse_drag_release(c, m);
606:
607: c->tty.mouse_drag_update = NULL;
608: c->tty.mouse_drag_release = NULL;
609:
1.182 nicm 610: /*
1.183 nicm 611: * End a mouse drag by passing a MouseDragEnd key corresponding
612: * to the button that started the drag.
1.182 nicm 613: */
614: switch (c->tty.mouse_drag_flag) {
615: case 1:
616: if (where == PANE)
1.183 nicm 617: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 618: if (where == STATUS)
1.183 nicm 619: key = KEYC_MOUSEDRAGEND1_STATUS;
1.258 nicm 620: if (where == STATUS_LEFT)
621: key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
622: if (where == STATUS_RIGHT)
623: key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274 nicm 624: if (where == STATUS_DEFAULT)
625: key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182 nicm 626: if (where == BORDER)
1.183 nicm 627: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 628: break;
629: case 2:
630: if (where == PANE)
1.183 nicm 631: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 632: if (where == STATUS)
1.183 nicm 633: key = KEYC_MOUSEDRAGEND2_STATUS;
1.258 nicm 634: if (where == STATUS_LEFT)
635: key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
636: if (where == STATUS_RIGHT)
637: key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274 nicm 638: if (where == STATUS_DEFAULT)
639: key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182 nicm 640: if (where == BORDER)
1.183 nicm 641: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 642: break;
643: case 3:
644: if (where == PANE)
1.183 nicm 645: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 646: if (where == STATUS)
1.183 nicm 647: key = KEYC_MOUSEDRAGEND3_STATUS;
1.258 nicm 648: if (where == STATUS_LEFT)
649: key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
650: if (where == STATUS_RIGHT)
651: key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274 nicm 652: if (where == STATUS_DEFAULT)
653: key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182 nicm 654: if (where == BORDER)
1.183 nicm 655: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 656: break;
657: default:
658: key = KEYC_MOUSE;
659: break;
660: }
1.131 nicm 661: c->tty.mouse_drag_flag = 0;
1.182 nicm 662:
663: return (key);
1.131 nicm 664: }
665:
666: /* Convert to a key binding. */
1.177 nicm 667: key = KEYC_UNKNOWN;
1.131 nicm 668: switch (type) {
669: case NOTYPE:
670: break;
1.209 nicm 671: case MOVE:
672: if (where == PANE)
673: key = KEYC_MOUSEMOVE_PANE;
674: if (where == STATUS)
675: key = KEYC_MOUSEMOVE_STATUS;
1.274 nicm 676: if (where == STATUS_LEFT)
677: key = KEYC_MOUSEMOVE_STATUS_LEFT;
678: if (where == STATUS_RIGHT)
679: key = KEYC_MOUSEMOVE_STATUS_RIGHT;
680: if (where == STATUS_DEFAULT)
681: key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209 nicm 682: if (where == BORDER)
683: key = KEYC_MOUSEMOVE_BORDER;
684: break;
1.131 nicm 685: case DRAG:
1.204 nicm 686: if (c->tty.mouse_drag_update != NULL)
687: key = KEYC_DRAGGING;
688: else {
1.131 nicm 689: switch (MOUSE_BUTTONS(b)) {
690: case 0:
691: if (where == PANE)
692: key = KEYC_MOUSEDRAG1_PANE;
693: if (where == STATUS)
694: key = KEYC_MOUSEDRAG1_STATUS;
1.258 nicm 695: if (where == STATUS_LEFT)
696: key = KEYC_MOUSEDRAG1_STATUS_LEFT;
697: if (where == STATUS_RIGHT)
698: key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274 nicm 699: if (where == STATUS_DEFAULT)
700: key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131 nicm 701: if (where == BORDER)
702: key = KEYC_MOUSEDRAG1_BORDER;
703: break;
704: case 1:
705: if (where == PANE)
706: key = KEYC_MOUSEDRAG2_PANE;
707: if (where == STATUS)
708: key = KEYC_MOUSEDRAG2_STATUS;
1.258 nicm 709: if (where == STATUS_LEFT)
710: key = KEYC_MOUSEDRAG2_STATUS_LEFT;
711: if (where == STATUS_RIGHT)
712: key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274 nicm 713: if (where == STATUS_DEFAULT)
714: key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131 nicm 715: if (where == BORDER)
716: key = KEYC_MOUSEDRAG2_BORDER;
717: break;
718: case 2:
719: if (where == PANE)
720: key = KEYC_MOUSEDRAG3_PANE;
721: if (where == STATUS)
722: key = KEYC_MOUSEDRAG3_STATUS;
1.258 nicm 723: if (where == STATUS_LEFT)
724: key = KEYC_MOUSEDRAG3_STATUS_LEFT;
725: if (where == STATUS_RIGHT)
726: key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274 nicm 727: if (where == STATUS_DEFAULT)
728: key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131 nicm 729: if (where == BORDER)
730: key = KEYC_MOUSEDRAG3_BORDER;
731: break;
732: }
733: }
1.66 nicm 734:
1.182 nicm 735: /*
736: * Begin a drag by setting the flag to a non-zero value that
737: * corresponds to the mouse button in use.
738: */
739: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 740: break;
741: case WHEEL:
742: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
743: if (where == PANE)
744: key = KEYC_WHEELUP_PANE;
745: if (where == STATUS)
746: key = KEYC_WHEELUP_STATUS;
1.258 nicm 747: if (where == STATUS_LEFT)
748: key = KEYC_WHEELUP_STATUS_LEFT;
749: if (where == STATUS_RIGHT)
750: key = KEYC_WHEELUP_STATUS_RIGHT;
1.274 nicm 751: if (where == STATUS_DEFAULT)
752: key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131 nicm 753: if (where == BORDER)
754: key = KEYC_WHEELUP_BORDER;
755: } else {
756: if (where == PANE)
757: key = KEYC_WHEELDOWN_PANE;
758: if (where == STATUS)
759: key = KEYC_WHEELDOWN_STATUS;
1.274 nicm 760: if (where == STATUS_LEFT)
761: key = KEYC_WHEELDOWN_STATUS_LEFT;
762: if (where == STATUS_RIGHT)
763: key = KEYC_WHEELDOWN_STATUS_RIGHT;
764: if (where == STATUS_DEFAULT)
765: key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131 nicm 766: if (where == BORDER)
767: key = KEYC_WHEELDOWN_BORDER;
768: }
769: break;
770: case UP:
771: switch (MOUSE_BUTTONS(b)) {
772: case 0:
773: if (where == PANE)
774: key = KEYC_MOUSEUP1_PANE;
775: if (where == STATUS)
776: key = KEYC_MOUSEUP1_STATUS;
1.258 nicm 777: if (where == STATUS_LEFT)
778: key = KEYC_MOUSEUP1_STATUS_LEFT;
779: if (where == STATUS_RIGHT)
780: key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274 nicm 781: if (where == STATUS_DEFAULT)
782: key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131 nicm 783: if (where == BORDER)
784: key = KEYC_MOUSEUP1_BORDER;
785: break;
786: case 1:
787: if (where == PANE)
788: key = KEYC_MOUSEUP2_PANE;
789: if (where == STATUS)
790: key = KEYC_MOUSEUP2_STATUS;
1.258 nicm 791: if (where == STATUS_LEFT)
792: key = KEYC_MOUSEUP2_STATUS_LEFT;
793: if (where == STATUS_RIGHT)
794: key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274 nicm 795: if (where == STATUS_DEFAULT)
796: key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131 nicm 797: if (where == BORDER)
798: key = KEYC_MOUSEUP2_BORDER;
799: break;
800: case 2:
801: if (where == PANE)
802: key = KEYC_MOUSEUP3_PANE;
803: if (where == STATUS)
804: key = KEYC_MOUSEUP3_STATUS;
1.258 nicm 805: if (where == STATUS_LEFT)
806: key = KEYC_MOUSEUP3_STATUS_LEFT;
807: if (where == STATUS_RIGHT)
808: key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274 nicm 809: if (where == STATUS_DEFAULT)
810: key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131 nicm 811: if (where == BORDER)
812: key = KEYC_MOUSEUP3_BORDER;
813: break;
814: }
815: break;
816: case DOWN:
817: switch (MOUSE_BUTTONS(b)) {
818: case 0:
819: if (where == PANE)
820: key = KEYC_MOUSEDOWN1_PANE;
821: if (where == STATUS)
822: key = KEYC_MOUSEDOWN1_STATUS;
1.258 nicm 823: if (where == STATUS_LEFT)
824: key = KEYC_MOUSEDOWN1_STATUS_LEFT;
825: if (where == STATUS_RIGHT)
826: key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274 nicm 827: if (where == STATUS_DEFAULT)
828: key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131 nicm 829: if (where == BORDER)
830: key = KEYC_MOUSEDOWN1_BORDER;
831: break;
832: case 1:
833: if (where == PANE)
834: key = KEYC_MOUSEDOWN2_PANE;
835: if (where == STATUS)
836: key = KEYC_MOUSEDOWN2_STATUS;
1.258 nicm 837: if (where == STATUS_LEFT)
838: key = KEYC_MOUSEDOWN2_STATUS_LEFT;
839: if (where == STATUS_RIGHT)
840: key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274 nicm 841: if (where == STATUS_DEFAULT)
842: key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131 nicm 843: if (where == BORDER)
844: key = KEYC_MOUSEDOWN2_BORDER;
845: break;
846: case 2:
847: if (where == PANE)
848: key = KEYC_MOUSEDOWN3_PANE;
849: if (where == STATUS)
850: key = KEYC_MOUSEDOWN3_STATUS;
1.258 nicm 851: if (where == STATUS_LEFT)
852: key = KEYC_MOUSEDOWN3_STATUS_LEFT;
853: if (where == STATUS_RIGHT)
854: key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274 nicm 855: if (where == STATUS_DEFAULT)
856: key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131 nicm 857: if (where == BORDER)
858: key = KEYC_MOUSEDOWN3_BORDER;
859: break;
1.66 nicm 860: }
1.131 nicm 861: break;
1.192 nicm 862: case DOUBLE:
863: switch (MOUSE_BUTTONS(b)) {
864: case 0:
865: if (where == PANE)
866: key = KEYC_DOUBLECLICK1_PANE;
867: if (where == STATUS)
868: key = KEYC_DOUBLECLICK1_STATUS;
1.258 nicm 869: if (where == STATUS_LEFT)
870: key = KEYC_DOUBLECLICK1_STATUS_LEFT;
871: if (where == STATUS_RIGHT)
872: key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274 nicm 873: if (where == STATUS_DEFAULT)
874: key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192 nicm 875: if (where == BORDER)
876: key = KEYC_DOUBLECLICK1_BORDER;
877: break;
878: case 1:
879: if (where == PANE)
880: key = KEYC_DOUBLECLICK2_PANE;
881: if (where == STATUS)
882: key = KEYC_DOUBLECLICK2_STATUS;
1.258 nicm 883: if (where == STATUS_LEFT)
884: key = KEYC_DOUBLECLICK2_STATUS_LEFT;
885: if (where == STATUS_RIGHT)
886: key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274 nicm 887: if (where == STATUS_DEFAULT)
888: key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192 nicm 889: if (where == BORDER)
890: key = KEYC_DOUBLECLICK2_BORDER;
891: break;
892: case 2:
893: if (where == PANE)
894: key = KEYC_DOUBLECLICK3_PANE;
895: if (where == STATUS)
896: key = KEYC_DOUBLECLICK3_STATUS;
1.258 nicm 897: if (where == STATUS_LEFT)
898: key = KEYC_DOUBLECLICK3_STATUS_LEFT;
899: if (where == STATUS_RIGHT)
900: key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274 nicm 901: if (where == STATUS_DEFAULT)
902: key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192 nicm 903: if (where == BORDER)
904: key = KEYC_DOUBLECLICK3_BORDER;
905: break;
906: }
907: break;
908: case TRIPLE:
909: switch (MOUSE_BUTTONS(b)) {
910: case 0:
911: if (where == PANE)
912: key = KEYC_TRIPLECLICK1_PANE;
913: if (where == STATUS)
914: key = KEYC_TRIPLECLICK1_STATUS;
1.258 nicm 915: if (where == STATUS_LEFT)
916: key = KEYC_TRIPLECLICK1_STATUS_LEFT;
917: if (where == STATUS_RIGHT)
918: key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274 nicm 919: if (where == STATUS_DEFAULT)
920: key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192 nicm 921: if (where == BORDER)
922: key = KEYC_TRIPLECLICK1_BORDER;
923: break;
924: case 1:
925: if (where == PANE)
926: key = KEYC_TRIPLECLICK2_PANE;
927: if (where == STATUS)
928: key = KEYC_TRIPLECLICK2_STATUS;
1.258 nicm 929: if (where == STATUS_LEFT)
930: key = KEYC_TRIPLECLICK2_STATUS_LEFT;
931: if (where == STATUS_RIGHT)
932: key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274 nicm 933: if (where == STATUS_DEFAULT)
934: key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192 nicm 935: if (where == BORDER)
936: key = KEYC_TRIPLECLICK2_BORDER;
937: break;
938: case 2:
939: if (where == PANE)
940: key = KEYC_TRIPLECLICK3_PANE;
941: if (where == STATUS)
942: key = KEYC_TRIPLECLICK3_STATUS;
1.258 nicm 943: if (where == STATUS_LEFT)
944: key = KEYC_TRIPLECLICK3_STATUS_LEFT;
945: if (where == STATUS_RIGHT)
946: key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274 nicm 947: if (where == STATUS_DEFAULT)
948: key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192 nicm 949: if (where == BORDER)
950: key = KEYC_TRIPLECLICK3_BORDER;
951: break;
952: }
953: break;
1.66 nicm 954: }
1.177 nicm 955: if (key == KEYC_UNKNOWN)
956: return (KEYC_UNKNOWN);
1.66 nicm 957:
1.131 nicm 958: /* Apply modifiers if any. */
959: if (b & MOUSE_MASK_META)
960: key |= KEYC_ESCAPE;
961: if (b & MOUSE_MASK_CTRL)
962: key |= KEYC_CTRL;
963: if (b & MOUSE_MASK_SHIFT)
964: key |= KEYC_SHIFT;
1.66 nicm 965:
1.131 nicm 966: return (key);
1.66 nicm 967: }
968:
1.82 nicm 969: /* Is this fast enough to probably be a paste? */
1.190 nicm 970: static int
1.82 nicm 971: server_client_assume_paste(struct session *s)
972: {
973: struct timeval tv;
1.84 nicm 974: int t;
1.82 nicm 975:
1.163 nicm 976: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 977: return (0);
1.82 nicm 978:
979: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 980: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
981: log_debug("session %s pasting (flag %d)", s->name,
982: !!(s->flags & SESSION_PASTING));
983: if (s->flags & SESSION_PASTING)
984: return (1);
985: s->flags |= SESSION_PASTING;
986: return (0);
987: }
988: log_debug("session %s not pasting", s->name);
989: s->flags &= ~SESSION_PASTING;
1.83 nicm 990: return (0);
1.82 nicm 991: }
992:
1.276 nicm 993: /*
994: * Handle data key input from client. This owns and can modify the key event it
995: * is given and is responsible for freeing it.
996: */
1.280 nicm 997: static enum cmd_retval
1.276 nicm 998: server_client_key_callback(struct cmdq_item *item, void *data)
1.18 nicm 999: {
1.276 nicm 1000: struct client *c = item->client;
1001: struct key_event *event = data;
1002: key_code key = event->key;
1003: struct mouse_event *m = &event->m;
1.267 nicm 1004: struct session *s = c->session;
1005: struct winlink *wl;
1006: struct window *w;
1007: struct window_pane *wp;
1008: struct window_mode_entry *wme;
1009: struct timeval tv;
1010: struct key_table *table, *first;
1011: struct key_binding *bd;
1012: int xtimeout, flags;
1013: struct cmd_find_state fs;
1014: key_code key0;
1.18 nicm 1015:
1016: /* Check the client is good to accept input. */
1.132 nicm 1017: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.276 nicm 1018: goto out;
1.264 nicm 1019: wl = s->curw;
1020: w = wl->window;
1.18 nicm 1021:
1022: /* Update the activity timer. */
1023: if (gettimeofday(&c->activity_time, NULL) != 0)
1024: fatal("gettimeofday failed");
1.149 nicm 1025: session_update_activity(s, &c->activity_time);
1.18 nicm 1026:
1027: /* Handle status line. */
1.281 nicm 1028: if (~c->flags & CLIENT_READONLY)
1.30 nicm 1029: status_message_clear(c);
1.18 nicm 1030: if (c->prompt_string != NULL) {
1.193 nicm 1031: if (c->flags & CLIENT_READONLY)
1.276 nicm 1032: goto out;
1.193 nicm 1033: if (status_prompt_key(c, key) == 0)
1.276 nicm 1034: goto out;
1.18 nicm 1035: }
1036:
1037: /* Check for mouse keys. */
1.204 nicm 1038: m->valid = 0;
1.18 nicm 1039: if (key == KEYC_MOUSE) {
1.30 nicm 1040: if (c->flags & CLIENT_READONLY)
1.276 nicm 1041: goto out;
1042: key = server_client_check_mouse(c, event);
1.177 nicm 1043: if (key == KEYC_UNKNOWN)
1.276 nicm 1044: goto out;
1.131 nicm 1045:
1046: m->valid = 1;
1047: m->key = key;
1.203 nicm 1048:
1049: /*
1.204 nicm 1050: * Mouse drag is in progress, so fire the callback (now that
1051: * the mouse event is valid).
1.203 nicm 1052: */
1.204 nicm 1053: if (key == KEYC_DRAGGING) {
1054: c->tty.mouse_drag_update(c, m);
1.276 nicm 1055: goto out;
1.204 nicm 1056: }
1.276 nicm 1057: }
1.18 nicm 1058:
1.202 nicm 1059: /* Find affected pane. */
1.243 nicm 1060: if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1061: cmd_find_from_session(&fs, s, 0);
1.227 nicm 1062: wp = fs.wp;
1.202 nicm 1063:
1064: /* Forward mouse keys if disabled. */
1.206 nicm 1065: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253 nicm 1066: goto forward_key;
1.202 nicm 1067:
1.132 nicm 1068: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 1069: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253 nicm 1070: goto forward_key;
1.18 nicm 1071:
1.191 nicm 1072: /*
1073: * Work out the current key table. If the pane is in a mode, use
1074: * the mode table instead of the default key table.
1075: */
1.223 nicm 1076: if (server_client_is_default_key_table(c, c->keytable) &&
1077: wp != NULL &&
1.267 nicm 1078: (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1079: wme->mode->key_table != NULL)
1080: table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223 nicm 1081: else
1.191 nicm 1082: table = c->keytable;
1.223 nicm 1083: first = table;
1.191 nicm 1084:
1.253 nicm 1085: table_changed:
1.205 nicm 1086: /*
1087: * The prefix always takes precedence and forces a switch to the prefix
1088: * table, unless we are already there.
1089: */
1.252 nicm 1090: key0 = (key & ~KEYC_XTERM);
1.239 nicm 1091: if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
1092: key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205 nicm 1093: strcmp(table->name, "prefix") != 0) {
1094: server_client_set_key_table(c, "prefix");
1095: server_status_client(c);
1.276 nicm 1096: goto out;
1.205 nicm 1097: }
1.238 nicm 1098: flags = c->flags;
1.205 nicm 1099:
1.262 nicm 1100: try_again:
1.223 nicm 1101: /* Log key table. */
1102: if (wp == NULL)
1103: log_debug("key table %s (no pane)", table->name);
1104: else
1105: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225 nicm 1106: if (c->flags & CLIENT_REPEAT)
1107: log_debug("currently repeating");
1.223 nicm 1108:
1.132 nicm 1109: /* Try to see if there is a key binding in the current table. */
1.254 nicm 1110: bd = key_bindings_get(table, key0);
1.132 nicm 1111: if (bd != NULL) {
1112: /*
1113: * Key was matched in this table. If currently repeating but a
1114: * non-repeating binding was found, stop repeating and try
1115: * again in the root table.
1116: */
1.222 nicm 1117: if ((c->flags & CLIENT_REPEAT) &&
1118: (~bd->flags & KEY_BINDING_REPEAT)) {
1.262 nicm 1119: log_debug("found in key table %s (not repeating)",
1120: table->name);
1.178 nicm 1121: server_client_set_key_table(c, NULL);
1.262 nicm 1122: first = table = c->keytable;
1.132 nicm 1123: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 1124: server_status_client(c);
1.253 nicm 1125: goto table_changed;
1.18 nicm 1126: }
1.223 nicm 1127: log_debug("found in key table %s", table->name);
1.82 nicm 1128:
1.132 nicm 1129: /*
1130: * Take a reference to this table to make sure the key binding
1131: * doesn't disappear.
1132: */
1133: table->references++;
1134:
1135: /*
1136: * If this is a repeating key, start the timer. Otherwise reset
1137: * the client back to the root table.
1138: */
1.163 nicm 1139: xtimeout = options_get_number(s->options, "repeat-time");
1.222 nicm 1140: if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132 nicm 1141: c->flags |= CLIENT_REPEAT;
1142:
1143: tv.tv_sec = xtimeout / 1000;
1144: tv.tv_usec = (xtimeout % 1000) * 1000L;
1145: evtimer_del(&c->repeat_timer);
1146: evtimer_add(&c->repeat_timer, &tv);
1147: } else {
1.18 nicm 1148: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 1149: server_client_set_key_table(c, NULL);
1.18 nicm 1150: }
1.132 nicm 1151: server_status_client(c);
1152:
1.227 nicm 1153: /* Execute the key binding. */
1.276 nicm 1154: key_bindings_dispatch(bd, item, c, m, &fs);
1.132 nicm 1155: key_bindings_unref_table(table);
1.276 nicm 1156: goto out;
1.18 nicm 1157: }
1158:
1.132 nicm 1159: /*
1.253 nicm 1160: * No match, try the ANY key.
1161: */
1162: if (key0 != KEYC_ANY) {
1163: key0 = KEYC_ANY;
1164: goto try_again;
1165: }
1166:
1167: /*
1.223 nicm 1168: * No match in this table. If not in the root table or if repeating,
1169: * switch the client back to the root table and try again.
1.132 nicm 1170: */
1.223 nicm 1171: log_debug("not found in key table %s", table->name);
1172: if (!server_client_is_default_key_table(c, table) ||
1173: (c->flags & CLIENT_REPEAT)) {
1.262 nicm 1174: log_debug("trying in root table");
1.178 nicm 1175: server_client_set_key_table(c, NULL);
1.262 nicm 1176: table = c->keytable;
1177: if (c->flags & CLIENT_REPEAT)
1178: first = table;
1.18 nicm 1179: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 1180: server_status_client(c);
1.253 nicm 1181: goto table_changed;
1.18 nicm 1182: }
1183:
1.223 nicm 1184: /*
1185: * No match in the root table either. If this wasn't the first table
1186: * tried, don't pass the key to the pane.
1187: */
1.238 nicm 1188: if (first != table && (~flags & CLIENT_REPEAT)) {
1.178 nicm 1189: server_client_set_key_table(c, NULL);
1.132 nicm 1190: server_status_client(c);
1.276 nicm 1191: goto out;
1.161 nicm 1192: }
1193:
1.253 nicm 1194: forward_key:
1.161 nicm 1195: if (c->flags & CLIENT_READONLY)
1.276 nicm 1196: goto out;
1.161 nicm 1197: if (wp != NULL)
1.264 nicm 1198: window_pane_key(wp, c, s, wl, key, m);
1.276 nicm 1199:
1200: out:
1201: free(event);
1202: return (CMD_RETURN_NORMAL);
1.280 nicm 1203: }
1204:
1205: /* Handle a key event. */
1206: int
1207: server_client_handle_key(struct client *c, struct key_event *event)
1208: {
1209: struct session *s = c->session;
1210: struct cmdq_item *item;
1211:
1212: /* Check the client is good to accept input. */
1213: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1214: return (0);
1215:
1216: /*
1.281 nicm 1217: * Key presses in overlay mode are a special case. The queue might be
1.280 nicm 1218: * blocked so they need to be processed immediately rather than queued.
1219: */
1.281 nicm 1220: if ((~c->flags & CLIENT_READONLY) && c->overlay_key != NULL) {
1221: if (c->overlay_key(c, event) != 0)
1222: server_client_clear_overlay(c);
1.280 nicm 1223: return (0);
1224: }
1225:
1226: /*
1227: * Add the key to the queue so it happens after any commands queued by
1228: * previous keys.
1229: */
1230: item = cmdq_get_callback(server_client_key_callback, event);
1231: cmdq_append(c, item);
1232: return (1);
1.18 nicm 1233: }
1234:
1.2 nicm 1235: /* Client functions that need to happen every loop. */
1236: void
1237: server_client_loop(void)
1238: {
1239: struct client *c;
1240: struct window *w;
1241: struct window_pane *wp;
1.197 nicm 1242: int focus;
1.2 nicm 1243:
1.135 nicm 1244: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1245: server_client_check_exit(c);
1246: if (c->session != NULL) {
1247: server_client_check_redraw(c);
1248: server_client_reset_state(c);
1249: }
1.2 nicm 1250: }
1251:
1252: /*
1253: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 1254: * their flags now. Also check pane focus and resize.
1.2 nicm 1255: */
1.197 nicm 1256: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1257: RB_FOREACH(w, windows, &windows) {
1.91 nicm 1258: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 1259: if (wp->fd != -1) {
1.197 nicm 1260: if (focus)
1261: server_client_check_focus(wp);
1.101 nicm 1262: server_client_check_resize(wp);
1263: }
1.2 nicm 1264: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1265: }
1.150 nicm 1266: check_window_name(w);
1.2 nicm 1267: }
1.92 nicm 1268: }
1269:
1.237 nicm 1270: /* Check if we need to force a resize. */
1271: static int
1272: server_client_resize_force(struct window_pane *wp)
1273: {
1274: struct timeval tv = { .tv_usec = 100000 };
1275: struct winsize ws;
1276:
1277: /*
1278: * If we are resizing to the same size as when we entered the loop
1279: * (that is, to the same size the application currently thinks it is),
1280: * tmux may have gone through several resizes internally and thrown
1281: * away parts of the screen. So we need the application to actually
1282: * redraw even though its final size has not changed.
1283: */
1284:
1285: if (wp->flags & PANE_RESIZEFORCE) {
1286: wp->flags &= ~PANE_RESIZEFORCE;
1287: return (0);
1288: }
1289:
1290: if (wp->sx != wp->osx ||
1291: wp->sy != wp->osy ||
1292: wp->sx <= 1 ||
1293: wp->sy <= 1)
1294: return (0);
1295:
1296: memset(&ws, 0, sizeof ws);
1297: ws.ws_col = wp->sx;
1298: ws.ws_row = wp->sy - 1;
1.244 nicm 1299: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237 nicm 1300: fatal("ioctl failed");
1301: log_debug("%s: %%%u forcing resize", __func__, wp->id);
1302:
1303: evtimer_add(&wp->resize_timer, &tv);
1304: wp->flags |= PANE_RESIZEFORCE;
1305: return (1);
1306: }
1307:
1.211 nicm 1308: /* Resize timer event. */
1.188 nicm 1309: static void
1310: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 1311: {
1.188 nicm 1312: struct window_pane *wp = data;
1313: struct winsize ws;
1314:
1315: evtimer_del(&wp->resize_timer);
1.92 nicm 1316:
1.101 nicm 1317: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 1318: return;
1.237 nicm 1319: if (server_client_resize_force(wp))
1320: return;
1.235 nicm 1321:
1.92 nicm 1322: memset(&ws, 0, sizeof ws);
1323: ws.ws_col = wp->sx;
1324: ws.ws_row = wp->sy;
1.244 nicm 1325: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 1326: fatal("ioctl failed");
1.235 nicm 1327: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92 nicm 1328:
1329: wp->flags &= ~PANE_RESIZE;
1.235 nicm 1330:
1331: wp->osx = wp->sx;
1332: wp->osy = wp->sy;
1.188 nicm 1333: }
1334:
1335: /* Check if pane should be resized. */
1.190 nicm 1336: static void
1.188 nicm 1337: server_client_check_resize(struct window_pane *wp)
1338: {
1339: struct timeval tv = { .tv_usec = 250000 };
1340:
1341: if (!(wp->flags & PANE_RESIZE))
1342: return;
1.235 nicm 1343: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.188 nicm 1344:
1345: if (!event_initialized(&wp->resize_timer))
1346: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1347:
1348: /*
1349: * The first resize should happen immediately, so if the timer is not
1350: * running, do it now.
1351: */
1352: if (!evtimer_pending(&wp->resize_timer, NULL))
1353: server_client_resize_event(-1, 0, wp);
1354:
1355: /*
1356: * If the pane is in the alternate screen, let the timer expire and
1357: * resize to give the application a chance to redraw. If not, keep
1358: * pushing the timer back.
1359: */
1360: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
1361: return;
1362: evtimer_del(&wp->resize_timer);
1363: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 1364: }
1365:
1366: /* Check whether pane should be focused. */
1.190 nicm 1367: static void
1.91 nicm 1368: server_client_check_focus(struct window_pane *wp)
1369: {
1.93 nicm 1370: struct client *c;
1.102 nicm 1371: int push;
1372:
1373: /* Do we need to push the focus state? */
1374: push = wp->flags & PANE_FOCUSPUSH;
1375: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1376:
1377: /* If we're not the active pane in our window, we're not focused. */
1378: if (wp->window->active != wp)
1379: goto not_focused;
1380:
1381: /* If we're in a mode, we're not focused. */
1382: if (wp->screen != &wp->base)
1383: goto not_focused;
1384:
1385: /*
1.93 nicm 1386: * If our window is the current window in any focused clients with an
1387: * attached session, we're focused.
1.91 nicm 1388: */
1.135 nicm 1389: TAILQ_FOREACH(c, &clients, entry) {
1390: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1391: continue;
1.255 nicm 1392: if (c->session->attached == 0)
1.93 nicm 1393: continue;
1394:
1395: if (c->session->curw->window == wp->window)
1.91 nicm 1396: goto focused;
1397: }
1398:
1399: not_focused:
1.251 nicm 1400: if (push || (wp->flags & PANE_FOCUSED)) {
1401: if (wp->base.mode & MODE_FOCUSON)
1402: bufferevent_write(wp->event, "\033[O", 3);
1403: notify_pane("pane-focus-out", wp);
1404: }
1.91 nicm 1405: wp->flags &= ~PANE_FOCUSED;
1406: return;
1407:
1408: focused:
1.251 nicm 1409: if (push || !(wp->flags & PANE_FOCUSED)) {
1410: if (wp->base.mode & MODE_FOCUSON)
1411: bufferevent_write(wp->event, "\033[I", 3);
1412: notify_pane("pane-focus-in", wp);
1.275 nicm 1413: session_update_activity(c->session, NULL);
1.251 nicm 1414: }
1.91 nicm 1415: wp->flags |= PANE_FOCUSED;
1.2 nicm 1416: }
1417:
1.18 nicm 1418: /*
1419: * Update cursor position and mode settings. The scroll region and attributes
1420: * are cleared when idle (waiting for an event) as this is the most likely time
1421: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1422: * compromise between excessive resets and likelihood of an interrupt.
1423: *
1424: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1425: * things that are already in their default state.
1426: */
1.190 nicm 1427: static void
1.18 nicm 1428: server_client_reset_state(struct client *c)
1.1 nicm 1429: {
1.18 nicm 1430: struct window *w = c->session->curw->window;
1.209 nicm 1431: struct window_pane *wp = w->active, *loop;
1.18 nicm 1432: struct screen *s = wp->screen;
1.163 nicm 1433: struct options *oo = c->session->options;
1.261 nicm 1434: int mode, cursor = 0;
1435: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1436:
1.186 nicm 1437: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1438: return;
1.282 ! nicm 1439: if (c->overlay_draw != NULL)
! 1440: return;
1.261 nicm 1441: mode = s->mode;
1.86 nicm 1442:
1.199 nicm 1443: tty_region_off(&c->tty);
1444: tty_margin_off(&c->tty);
1.1 nicm 1445:
1.261 nicm 1446: /* Move cursor to pane cursor and offset. */
1447: cursor = 0;
1448: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1449: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1450: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1451: cursor = 1;
1452:
1453: cx = wp->xoff + s->cx - ox;
1454: cy = wp->yoff + s->cy - oy;
1455:
1456: if (status_at_line(c) == 0)
1457: cy += status_line_size(c);
1458: }
1459: if (!cursor)
1460: mode &= ~MODE_CURSOR;
1461: tty_cursor(&c->tty, cx, cy);
1.1 nicm 1462:
1.50 nicm 1463: /*
1.131 nicm 1464: * Set mouse mode if requested. To support dragging, always use button
1465: * mode.
1.57 nicm 1466: */
1.209 nicm 1467: if (options_get_number(oo, "mouse")) {
1468: mode &= ~ALL_MOUSE_MODES;
1469: TAILQ_FOREACH(loop, &w->panes, entry) {
1470: if (loop->screen->mode & MODE_MOUSE_ALL)
1471: mode |= MODE_MOUSE_ALL;
1472: }
1473: if (~mode & MODE_MOUSE_ALL)
1474: mode |= MODE_MOUSE_BUTTON;
1475: }
1.215 nicm 1476:
1477: /* Clear bracketed paste mode if at the prompt. */
1478: if (c->prompt_string != NULL)
1479: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1480:
1481: /* Set the terminal mode and reset attributes. */
1.59 nicm 1482: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1483: tty_reset(&c->tty);
1.17 nicm 1484: }
1485:
1486: /* Repeat time callback. */
1.190 nicm 1487: static void
1.170 nicm 1488: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1489: {
1490: struct client *c = data;
1491:
1.85 nicm 1492: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1493: server_client_set_key_table(c, NULL);
1.132 nicm 1494: c->flags &= ~CLIENT_REPEAT;
1495: server_status_client(c);
1.85 nicm 1496: }
1.192 nicm 1497: }
1498:
1499: /* Double-click callback. */
1500: static void
1501: server_client_click_timer(__unused int fd, __unused short events, void *data)
1502: {
1503: struct client *c = data;
1504:
1505: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1506: }
1507:
1.36 nicm 1508: /* Check if client should be exited. */
1.190 nicm 1509: static void
1.36 nicm 1510: server_client_check_exit(struct client *c)
1511: {
1512: if (!(c->flags & CLIENT_EXIT))
1513: return;
1514:
1.73 nicm 1515: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1516: return;
1517: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1518: return;
1.73 nicm 1519: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1520: return;
1521:
1.249 nicm 1522: if (c->flags & CLIENT_ATTACHED)
1523: notify_client("client-detached", c);
1.162 nicm 1524: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1525: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1526: }
1527:
1.218 nicm 1528: /* Redraw timer callback. */
1529: static void
1530: server_client_redraw_timer(__unused int fd, __unused short events,
1531: __unused void* data)
1532: {
1533: log_debug("redraw timer fired");
1534: }
1535:
1.1 nicm 1536: /* Check for client redraws. */
1.190 nicm 1537: static void
1.1 nicm 1538: server_client_check_redraw(struct client *c)
1539: {
1540: struct session *s = c->session;
1.137 nicm 1541: struct tty *tty = &c->tty;
1.1 nicm 1542: struct window_pane *wp;
1.256 nicm 1543: int needed, flags;
1.218 nicm 1544: struct timeval tv = { .tv_usec = 1000 };
1545: static struct event ev;
1546: size_t left;
1.1 nicm 1547:
1.86 nicm 1548: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1549: return;
1.257 nicm 1550: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.282 ! nicm 1551: log_debug("%s: redraw%s%s%s%s", c->name,
1.257 nicm 1552: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1553: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282 ! nicm 1554: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
! 1555: (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "");
1.257 nicm 1556: }
1.218 nicm 1557:
1558: /*
1559: * If there is outstanding data, defer the redraw until it has been
1560: * consumed. We can just add a timer to get out of the event loop and
1561: * end up back here.
1562: */
1563: needed = 0;
1.256 nicm 1564: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1565: needed = 1;
1566: else {
1567: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1568: if (wp->flags & PANE_REDRAW) {
1569: needed = 1;
1570: break;
1571: }
1572: }
1573: }
1.241 nicm 1574: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1575: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1576: if (!evtimer_initialized(&ev))
1577: evtimer_set(&ev, server_client_redraw_timer, NULL);
1578: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1579: log_debug("redraw timer started");
1580: evtimer_add(&ev, &tv);
1.241 nicm 1581: }
1.218 nicm 1582:
1.241 nicm 1583: /*
1584: * We may have got here for a single pane redraw, but force a
1585: * full redraw next time in case other panes have been updated.
1586: */
1.256 nicm 1587: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241 nicm 1588: return;
1589: } else if (needed)
1.218 nicm 1590: log_debug("%s: redraw needed", c->name);
1.79 nicm 1591:
1.219 nicm 1592: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1593: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137 nicm 1594:
1.256 nicm 1595: if (~c->flags & CLIENT_REDRAWWINDOW) {
1596: /*
1597: * If not redrawing the entire window, check whether each pane
1598: * needs to be redrawn.
1599: */
1.1 nicm 1600: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1601: if (wp->flags & PANE_REDRAW) {
1602: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1603: screen_redraw_pane(c, wp);
1.137 nicm 1604: }
1.1 nicm 1605: }
1606: }
1607:
1.256 nicm 1608: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1609: if (options_get_number(s->options, "set-titles"))
1610: server_client_set_title(c);
1611: screen_redraw_screen(c);
1612: }
1.1 nicm 1613:
1.137 nicm 1614: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1615: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1616:
1.256 nicm 1617: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1618:
1619: if (needed) {
1620: /*
1621: * We would have deferred the redraw unless the output buffer
1622: * was empty, so we can record how many bytes the redraw
1623: * generated.
1624: */
1625: c->redraw = EVBUFFER_LENGTH(tty->out);
1626: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1627: }
1.1 nicm 1628: }
1629:
1630: /* Set client title. */
1.190 nicm 1631: static void
1.1 nicm 1632: server_client_set_title(struct client *c)
1633: {
1.128 nicm 1634: struct session *s = c->session;
1635: const char *template;
1636: char *title;
1637: struct format_tree *ft;
1.1 nicm 1638:
1.163 nicm 1639: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1640:
1.228 nicm 1641: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1642: format_defaults(ft, c, NULL, NULL, NULL);
1643:
1.269 nicm 1644: title = format_expand_time(ft, template);
1.1 nicm 1645: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1646: free(c->title);
1.1 nicm 1647: c->title = xstrdup(title);
1648: tty_set_title(&c->tty, c->title);
1649: }
1.77 nicm 1650: free(title);
1.128 nicm 1651:
1652: format_free(ft);
1.1 nicm 1653: }
1654:
1655: /* Dispatch message from client. */
1.190 nicm 1656: static void
1.162 nicm 1657: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1658: {
1.162 nicm 1659: struct client *c = arg;
1.73 nicm 1660: struct msg_stdin_data stdindata;
1.108 nicm 1661: const char *data;
1.162 nicm 1662: ssize_t datalen;
1.149 nicm 1663: struct session *s;
1.1 nicm 1664:
1.162 nicm 1665: if (c->flags & CLIENT_DEAD)
1666: return;
1667:
1668: if (imsg == NULL) {
1669: server_client_lost(c);
1670: return;
1671: }
1.1 nicm 1672:
1.162 nicm 1673: data = imsg->data;
1674: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1675:
1.162 nicm 1676: switch (imsg->hdr.type) {
1677: case MSG_IDENTIFY_FLAGS:
1678: case MSG_IDENTIFY_TERM:
1679: case MSG_IDENTIFY_TTYNAME:
1680: case MSG_IDENTIFY_CWD:
1681: case MSG_IDENTIFY_STDIN:
1682: case MSG_IDENTIFY_ENVIRON:
1683: case MSG_IDENTIFY_CLIENTPID:
1684: case MSG_IDENTIFY_DONE:
1685: server_client_dispatch_identify(c, imsg);
1686: break;
1687: case MSG_COMMAND:
1688: server_client_dispatch_command(c, imsg);
1689: break;
1690: case MSG_STDIN:
1691: if (datalen != sizeof stdindata)
1692: fatalx("bad MSG_STDIN size");
1693: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1694:
1.162 nicm 1695: if (c->stdin_callback == NULL)
1.33 nicm 1696: break;
1.162 nicm 1697: if (stdindata.size <= 0)
1698: c->stdin_closed = 1;
1699: else {
1700: evbuffer_add(c->stdin_data, stdindata.data,
1701: stdindata.size);
1702: }
1.279 nicm 1703: c->stdin_callback(c, c->stdin_closed, c->stdin_callback_data);
1.162 nicm 1704: break;
1705: case MSG_RESIZE:
1706: if (datalen != 0)
1707: fatalx("bad MSG_RESIZE size");
1.1 nicm 1708:
1.162 nicm 1709: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1710: break;
1.282 ! nicm 1711: server_client_clear_overlay(c);
1.236 nicm 1712: tty_resize(&c->tty);
1713: recalculate_sizes();
1714: server_redraw_client(c);
1.174 nicm 1715: if (c->session != NULL)
1.196 nicm 1716: notify_client("client-resized", c);
1.162 nicm 1717: break;
1718: case MSG_EXITING:
1719: if (datalen != 0)
1720: fatalx("bad MSG_EXITING size");
1.1 nicm 1721:
1.162 nicm 1722: c->session = NULL;
1723: tty_close(&c->tty);
1724: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1725: break;
1726: case MSG_WAKEUP:
1727: case MSG_UNLOCK:
1728: if (datalen != 0)
1729: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1730:
1.162 nicm 1731: if (!(c->flags & CLIENT_SUSPENDED))
1732: break;
1733: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1734:
1.162 nicm 1735: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1736: break;
1.162 nicm 1737: s = c->session;
1.1 nicm 1738:
1.162 nicm 1739: if (gettimeofday(&c->activity_time, NULL) != 0)
1740: fatal("gettimeofday failed");
1741:
1742: tty_start_tty(&c->tty);
1743: server_redraw_client(c);
1744: recalculate_sizes();
1.184 nicm 1745:
1746: if (s != NULL)
1747: session_update_activity(s, &c->activity_time);
1.162 nicm 1748: break;
1749: case MSG_SHELL:
1750: if (datalen != 0)
1751: fatalx("bad MSG_SHELL size");
1.1 nicm 1752:
1.162 nicm 1753: server_client_dispatch_shell(c);
1754: break;
1.1 nicm 1755: }
1756: }
1757:
1.194 nicm 1758: /* Callback when command is done. */
1759: static enum cmd_retval
1.195 nicm 1760: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1761: {
1.195 nicm 1762: struct client *c = item->client;
1.194 nicm 1763:
1764: if (~c->flags & CLIENT_ATTACHED)
1765: c->flags |= CLIENT_EXIT;
1766: return (CMD_RETURN_NORMAL);
1767: }
1768:
1769: /* Show an error message. */
1770: static enum cmd_retval
1.195 nicm 1771: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1772: {
1773: char *error = data;
1774:
1.195 nicm 1775: cmdq_error(item, "%s", error);
1.194 nicm 1776: free(error);
1777:
1778: return (CMD_RETURN_NORMAL);
1779: }
1780:
1.1 nicm 1781: /* Handle command message. */
1.190 nicm 1782: static void
1.162 nicm 1783: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1784: {
1.108 nicm 1785: struct msg_command_data data;
1786: char *buf;
1787: size_t len;
1788: struct cmd_list *cmdlist = NULL;
1789: int argc;
1790: char **argv, *cause;
1.246 nicm 1791:
1792: if (c->flags & CLIENT_EXIT)
1793: return;
1.108 nicm 1794:
1795: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1796: fatalx("bad MSG_COMMAND size");
1797: memcpy(&data, imsg->data, sizeof data);
1798:
1.124 nicm 1799: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1800: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1801: if (len > 0 && buf[len - 1] != '\0')
1802: fatalx("bad MSG_COMMAND string");
1803:
1804: argc = data.argc;
1805: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1806: cause = xstrdup("command too long");
1.1 nicm 1807: goto error;
1808: }
1809:
1810: if (argc == 0) {
1811: argc = 1;
1812: argv = xcalloc(1, sizeof *argv);
1813: *argv = xstrdup("new-session");
1814: }
1815:
1.94 nicm 1816: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1817: cmd_free_argv(argc, argv);
1818: goto error;
1819: }
1820: cmd_free_argv(argc, argv);
1821:
1.194 nicm 1822: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1823: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1824: cmd_list_free(cmdlist);
1825: return;
1826:
1827: error:
1.194 nicm 1828: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1829:
1.1 nicm 1830: if (cmdlist != NULL)
1831: cmd_list_free(cmdlist);
1.88 nicm 1832:
1.36 nicm 1833: c->flags |= CLIENT_EXIT;
1.1 nicm 1834: }
1835:
1836: /* Handle identify message. */
1.190 nicm 1837: static void
1.162 nicm 1838: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1839: {
1.165 nicm 1840: const char *data, *home;
1.232 nicm 1841: size_t datalen;
1.109 nicm 1842: int flags;
1.216 nicm 1843: char *name;
1.109 nicm 1844:
1845: if (c->flags & CLIENT_IDENTIFIED)
1846: fatalx("out-of-order identify message");
1847:
1848: data = imsg->data;
1849: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1850:
1851: switch (imsg->hdr.type) {
1852: case MSG_IDENTIFY_FLAGS:
1853: if (datalen != sizeof flags)
1854: fatalx("bad MSG_IDENTIFY_FLAGS size");
1855: memcpy(&flags, data, sizeof flags);
1856: c->flags |= flags;
1.158 nicm 1857: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1858: break;
1859: case MSG_IDENTIFY_TERM:
1.110 nicm 1860: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1861: fatalx("bad MSG_IDENTIFY_TERM string");
1862: c->term = xstrdup(data);
1.158 nicm 1863: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1864: break;
1865: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1866: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1867: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1868: c->ttyname = xstrdup(data);
1.158 nicm 1869: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1870: break;
1871: case MSG_IDENTIFY_CWD:
1.155 nicm 1872: if (datalen == 0 || data[datalen - 1] != '\0')
1873: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1874: if (access(data, X_OK) == 0)
1875: c->cwd = xstrdup(data);
1876: else if ((home = find_home()) != NULL)
1877: c->cwd = xstrdup(home);
1878: else
1879: c->cwd = xstrdup("/");
1.158 nicm 1880: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1881: break;
1882: case MSG_IDENTIFY_STDIN:
1883: if (datalen != 0)
1884: fatalx("bad MSG_IDENTIFY_STDIN size");
1885: c->fd = imsg->fd;
1.158 nicm 1886: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1887: break;
1888: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1889: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1890: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1891: if (strchr(data, '=') != NULL)
1.164 nicm 1892: environ_put(c->environ, data);
1.158 nicm 1893: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1894: break;
1895: case MSG_IDENTIFY_CLIENTPID:
1896: if (datalen != sizeof c->pid)
1897: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1898: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1899: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1900: break;
1901: default:
1902: break;
1903: }
1904:
1905: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1906: return;
1907: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1908:
1.216 nicm 1909: if (*c->ttyname != '\0')
1910: name = xstrdup(c->ttyname);
1911: else
1912: xasprintf(&name, "client-%ld", (long)c->pid);
1913: c->name = name;
1914: log_debug("client %p name is %s", c, c->name);
1915:
1.109 nicm 1916: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1917: c->stdin_callback = control_callback;
1.109 nicm 1918:
1.97 nicm 1919: evbuffer_free(c->stderr_data);
1920: c->stderr_data = c->stdout_data;
1.109 nicm 1921:
1922: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1923: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1924: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1925:
1926: c->tty.fd = -1;
1927:
1.109 nicm 1928: close(c->fd);
1929: c->fd = -1;
1930:
1.75 nicm 1931: return;
1932: }
1.1 nicm 1933:
1.109 nicm 1934: if (c->fd == -1)
1.104 nicm 1935: return;
1.145 nicm 1936: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1937: close(c->fd);
1938: c->fd = -1;
1.80 nicm 1939: return;
1940: }
1.109 nicm 1941: if (c->flags & CLIENT_UTF8)
1.1 nicm 1942: c->tty.flags |= TTY_UTF8;
1.109 nicm 1943: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1944: c->tty.term_flags |= TERM_256COLOURS;
1945:
1946: tty_resize(&c->tty);
1947:
1.109 nicm 1948: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1949: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1950: }
1951:
1952: /* Handle shell message. */
1.190 nicm 1953: static void
1.162 nicm 1954: server_client_dispatch_shell(struct client *c)
1.1 nicm 1955: {
1.107 nicm 1956: const char *shell;
1.25 nicm 1957:
1.163 nicm 1958: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1959: if (*shell == '\0' || areshell(shell))
1960: shell = _PATH_BSHELL;
1.240 nicm 1961: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 1962:
1.162 nicm 1963: proc_kill_peer(c->peer);
1.169 nicm 1964: }
1965:
1966: /* Event callback to push more stdout data if any left. */
1967: static void
1.170 nicm 1968: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1969: {
1970: struct client *c = arg;
1971:
1972: if (~c->flags & CLIENT_DEAD)
1973: server_client_push_stdout(c);
1974: server_client_unref(c);
1975: }
1976:
1977: /* Push stdout to client if possible. */
1978: void
1979: server_client_push_stdout(struct client *c)
1980: {
1981: struct msg_stdout_data data;
1.232 nicm 1982: size_t sent, left;
1.169 nicm 1983:
1984: left = EVBUFFER_LENGTH(c->stdout_data);
1985: while (left != 0) {
1986: sent = left;
1987: if (sent > sizeof data.data)
1988: sent = sizeof data.data;
1989: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1990: data.size = sent;
1991:
1992: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1993: break;
1994: evbuffer_drain(c->stdout_data, sent);
1995:
1996: left = EVBUFFER_LENGTH(c->stdout_data);
1997: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1998: sent, left);
1999: }
2000: if (left != 0) {
2001: c->references++;
2002: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
2003: log_debug("%s: client %p, queued", __func__, c);
2004: }
2005: }
2006:
2007: /* Event callback to push more stderr data if any left. */
2008: static void
1.170 nicm 2009: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 2010: {
2011: struct client *c = arg;
2012:
2013: if (~c->flags & CLIENT_DEAD)
2014: server_client_push_stderr(c);
2015: server_client_unref(c);
2016: }
2017:
2018: /* Push stderr to client if possible. */
2019: void
2020: server_client_push_stderr(struct client *c)
2021: {
2022: struct msg_stderr_data data;
1.232 nicm 2023: size_t sent, left;
1.169 nicm 2024:
2025: if (c->stderr_data == c->stdout_data) {
2026: server_client_push_stdout(c);
2027: return;
2028: }
2029:
2030: left = EVBUFFER_LENGTH(c->stderr_data);
2031: while (left != 0) {
2032: sent = left;
2033: if (sent > sizeof data.data)
2034: sent = sizeof data.data;
2035: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
2036: data.size = sent;
2037:
2038: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
2039: break;
2040: evbuffer_drain(c->stderr_data, sent);
2041:
2042: left = EVBUFFER_LENGTH(c->stderr_data);
2043: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
2044: sent, left);
2045: }
2046: if (left != 0) {
2047: c->references++;
2048: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
2049: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 2050: }
2051: }
2052:
2053: /* Add to client message log. */
2054: void
2055: server_client_add_message(struct client *c, const char *fmt, ...)
2056: {
2057: struct message_entry *msg, *msg1;
2058: char *s;
2059: va_list ap;
2060: u_int limit;
2061:
2062: va_start(ap, fmt);
2063: xvasprintf(&s, fmt, ap);
2064: va_end(ap);
2065:
1.216 nicm 2066: log_debug("message %s (client %p)", s, c);
1.212 nicm 2067:
2068: msg = xcalloc(1, sizeof *msg);
2069: msg->msg_time = time(NULL);
2070: msg->msg_num = c->message_next++;
2071: msg->msg = s;
2072: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2073:
2074: limit = options_get_number(global_options, "message-limit");
2075: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2076: if (msg->msg_num + limit >= c->message_next)
2077: break;
2078: free(msg->msg);
2079: TAILQ_REMOVE(&c->message_log, msg, entry);
2080: free(msg);
1.169 nicm 2081: }
1.213 nicm 2082: }
2083:
2084: /* Get client working directory. */
2085: const char *
1.250 nicm 2086: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2087: {
1.250 nicm 2088: const char *home;
1.213 nicm 2089:
1.265 nicm 2090: if (!cfg_finished && cfg_client != NULL)
2091: return (cfg_client->cwd);
1.213 nicm 2092: if (c != NULL && c->session == NULL && c->cwd != NULL)
2093: return (c->cwd);
1.250 nicm 2094: if (s != NULL && s->cwd != NULL)
2095: return (s->cwd);
1.213 nicm 2096: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2097: return (s->cwd);
1.250 nicm 2098: if ((home = find_home()) != NULL)
2099: return (home);
2100: return ("/");
1.213 nicm 2101: }
2102:
2103: /* Resolve an absolute path or relative to client working directory. */
2104: char *
2105: server_client_get_path(struct client *c, const char *file)
2106: {
2107: char *path, resolved[PATH_MAX];
2108:
2109: if (*file == '/')
2110: path = xstrdup(file);
2111: else
1.250 nicm 2112: xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213 nicm 2113: if (realpath(path, resolved) == NULL)
2114: return (path);
2115: free(path);
2116: return (xstrdup(resolved));
1.1 nicm 2117: }