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