Annotation of src/usr.bin/tmux/server-client.c, Revision 1.280
1.280 ! nicm 1: /* $OpenBSD: server-client.c,v 1.279 2019/05/03 20:44:24 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: */
1.280 ! nicm 989: static enum cmd_retval
1.276 nicm 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.280 ! nicm 1207: }
! 1208:
! 1209: /* Handle a key event. */
! 1210: int
! 1211: server_client_handle_key(struct client *c, struct key_event *event)
! 1212: {
! 1213: struct session *s = c->session;
! 1214: struct window *w;
! 1215: struct window_pane *wp = NULL;
! 1216: struct cmdq_item *item;
! 1217:
! 1218: /* Check the client is good to accept input. */
! 1219: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
! 1220: return (0);
! 1221: w = s->curw->window;
! 1222:
! 1223: /*
! 1224: * Key presses in identify mode are a special case. The queue might be
! 1225: * blocked so they need to be processed immediately rather than queued.
! 1226: */
! 1227: if (c->flags & CLIENT_IDENTIFY) {
! 1228: if (c->flags & CLIENT_READONLY)
! 1229: return (0);
! 1230: if (event->key >= '0' && event->key <= '9') {
! 1231: window_unzoom(w);
! 1232: wp = window_pane_at_index(w, event->key - '0');
! 1233: }
! 1234: server_client_clear_identify(c, wp);
! 1235: return (0);
! 1236: }
! 1237:
! 1238: /*
! 1239: * Add the key to the queue so it happens after any commands queued by
! 1240: * previous keys.
! 1241: */
! 1242: item = cmdq_get_callback(server_client_key_callback, event);
! 1243: cmdq_append(c, item);
! 1244: return (1);
1.18 nicm 1245: }
1246:
1.2 nicm 1247: /* Client functions that need to happen every loop. */
1248: void
1249: server_client_loop(void)
1250: {
1251: struct client *c;
1252: struct window *w;
1253: struct window_pane *wp;
1.197 nicm 1254: int focus;
1.2 nicm 1255:
1.135 nicm 1256: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1257: server_client_check_exit(c);
1258: if (c->session != NULL) {
1259: server_client_check_redraw(c);
1260: server_client_reset_state(c);
1261: }
1.2 nicm 1262: }
1263:
1264: /*
1265: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 1266: * their flags now. Also check pane focus and resize.
1.2 nicm 1267: */
1.197 nicm 1268: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1269: RB_FOREACH(w, windows, &windows) {
1.91 nicm 1270: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 1271: if (wp->fd != -1) {
1.197 nicm 1272: if (focus)
1273: server_client_check_focus(wp);
1.101 nicm 1274: server_client_check_resize(wp);
1275: }
1.2 nicm 1276: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1277: }
1.150 nicm 1278: check_window_name(w);
1.2 nicm 1279: }
1.92 nicm 1280: }
1281:
1.237 nicm 1282: /* Check if we need to force a resize. */
1283: static int
1284: server_client_resize_force(struct window_pane *wp)
1285: {
1286: struct timeval tv = { .tv_usec = 100000 };
1287: struct winsize ws;
1288:
1289: /*
1290: * If we are resizing to the same size as when we entered the loop
1291: * (that is, to the same size the application currently thinks it is),
1292: * tmux may have gone through several resizes internally and thrown
1293: * away parts of the screen. So we need the application to actually
1294: * redraw even though its final size has not changed.
1295: */
1296:
1297: if (wp->flags & PANE_RESIZEFORCE) {
1298: wp->flags &= ~PANE_RESIZEFORCE;
1299: return (0);
1300: }
1301:
1302: if (wp->sx != wp->osx ||
1303: wp->sy != wp->osy ||
1304: wp->sx <= 1 ||
1305: wp->sy <= 1)
1306: return (0);
1307:
1308: memset(&ws, 0, sizeof ws);
1309: ws.ws_col = wp->sx;
1310: ws.ws_row = wp->sy - 1;
1.244 nicm 1311: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237 nicm 1312: fatal("ioctl failed");
1313: log_debug("%s: %%%u forcing resize", __func__, wp->id);
1314:
1315: evtimer_add(&wp->resize_timer, &tv);
1316: wp->flags |= PANE_RESIZEFORCE;
1317: return (1);
1318: }
1319:
1.211 nicm 1320: /* Resize timer event. */
1.188 nicm 1321: static void
1322: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 1323: {
1.188 nicm 1324: struct window_pane *wp = data;
1325: struct winsize ws;
1326:
1327: evtimer_del(&wp->resize_timer);
1.92 nicm 1328:
1.101 nicm 1329: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 1330: return;
1.237 nicm 1331: if (server_client_resize_force(wp))
1332: return;
1.235 nicm 1333:
1.92 nicm 1334: memset(&ws, 0, sizeof ws);
1335: ws.ws_col = wp->sx;
1336: ws.ws_row = wp->sy;
1.244 nicm 1337: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 1338: fatal("ioctl failed");
1.235 nicm 1339: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92 nicm 1340:
1341: wp->flags &= ~PANE_RESIZE;
1.235 nicm 1342:
1343: wp->osx = wp->sx;
1344: wp->osy = wp->sy;
1.188 nicm 1345: }
1346:
1347: /* Check if pane should be resized. */
1.190 nicm 1348: static void
1.188 nicm 1349: server_client_check_resize(struct window_pane *wp)
1350: {
1351: struct timeval tv = { .tv_usec = 250000 };
1352:
1353: if (!(wp->flags & PANE_RESIZE))
1354: return;
1.235 nicm 1355: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.188 nicm 1356:
1357: if (!event_initialized(&wp->resize_timer))
1358: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1359:
1360: /*
1361: * The first resize should happen immediately, so if the timer is not
1362: * running, do it now.
1363: */
1364: if (!evtimer_pending(&wp->resize_timer, NULL))
1365: server_client_resize_event(-1, 0, wp);
1366:
1367: /*
1368: * If the pane is in the alternate screen, let the timer expire and
1369: * resize to give the application a chance to redraw. If not, keep
1370: * pushing the timer back.
1371: */
1372: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
1373: return;
1374: evtimer_del(&wp->resize_timer);
1375: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 1376: }
1377:
1378: /* Check whether pane should be focused. */
1.190 nicm 1379: static void
1.91 nicm 1380: server_client_check_focus(struct window_pane *wp)
1381: {
1.93 nicm 1382: struct client *c;
1.102 nicm 1383: int push;
1384:
1385: /* Do we need to push the focus state? */
1386: push = wp->flags & PANE_FOCUSPUSH;
1387: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1388:
1389: /* If we're not the active pane in our window, we're not focused. */
1390: if (wp->window->active != wp)
1391: goto not_focused;
1392:
1393: /* If we're in a mode, we're not focused. */
1394: if (wp->screen != &wp->base)
1395: goto not_focused;
1396:
1397: /*
1.93 nicm 1398: * If our window is the current window in any focused clients with an
1399: * attached session, we're focused.
1.91 nicm 1400: */
1.135 nicm 1401: TAILQ_FOREACH(c, &clients, entry) {
1402: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1403: continue;
1.255 nicm 1404: if (c->session->attached == 0)
1.93 nicm 1405: continue;
1406:
1407: if (c->session->curw->window == wp->window)
1.91 nicm 1408: goto focused;
1409: }
1410:
1411: not_focused:
1.251 nicm 1412: if (push || (wp->flags & PANE_FOCUSED)) {
1413: if (wp->base.mode & MODE_FOCUSON)
1414: bufferevent_write(wp->event, "\033[O", 3);
1415: notify_pane("pane-focus-out", wp);
1416: }
1.91 nicm 1417: wp->flags &= ~PANE_FOCUSED;
1418: return;
1419:
1420: focused:
1.251 nicm 1421: if (push || !(wp->flags & PANE_FOCUSED)) {
1422: if (wp->base.mode & MODE_FOCUSON)
1423: bufferevent_write(wp->event, "\033[I", 3);
1424: notify_pane("pane-focus-in", wp);
1.275 nicm 1425: session_update_activity(c->session, NULL);
1.251 nicm 1426: }
1.91 nicm 1427: wp->flags |= PANE_FOCUSED;
1.2 nicm 1428: }
1429:
1.18 nicm 1430: /*
1431: * Update cursor position and mode settings. The scroll region and attributes
1432: * are cleared when idle (waiting for an event) as this is the most likely time
1433: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1434: * compromise between excessive resets and likelihood of an interrupt.
1435: *
1436: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1437: * things that are already in their default state.
1438: */
1.190 nicm 1439: static void
1.18 nicm 1440: server_client_reset_state(struct client *c)
1.1 nicm 1441: {
1.18 nicm 1442: struct window *w = c->session->curw->window;
1.209 nicm 1443: struct window_pane *wp = w->active, *loop;
1.18 nicm 1444: struct screen *s = wp->screen;
1.163 nicm 1445: struct options *oo = c->session->options;
1.261 nicm 1446: int mode, cursor = 0;
1447: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1448:
1.186 nicm 1449: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1450: return;
1.261 nicm 1451: mode = s->mode;
1.86 nicm 1452:
1.199 nicm 1453: tty_region_off(&c->tty);
1454: tty_margin_off(&c->tty);
1.1 nicm 1455:
1.261 nicm 1456: /* Move cursor to pane cursor and offset. */
1457: cursor = 0;
1458: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1459: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1460: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1461: cursor = 1;
1462:
1463: cx = wp->xoff + s->cx - ox;
1464: cy = wp->yoff + s->cy - oy;
1465:
1466: if (status_at_line(c) == 0)
1467: cy += status_line_size(c);
1468: }
1469: if (!cursor)
1470: mode &= ~MODE_CURSOR;
1471: tty_cursor(&c->tty, cx, cy);
1.1 nicm 1472:
1.50 nicm 1473: /*
1.131 nicm 1474: * Set mouse mode if requested. To support dragging, always use button
1475: * mode.
1.57 nicm 1476: */
1.209 nicm 1477: if (options_get_number(oo, "mouse")) {
1478: mode &= ~ALL_MOUSE_MODES;
1479: TAILQ_FOREACH(loop, &w->panes, entry) {
1480: if (loop->screen->mode & MODE_MOUSE_ALL)
1481: mode |= MODE_MOUSE_ALL;
1482: }
1483: if (~mode & MODE_MOUSE_ALL)
1484: mode |= MODE_MOUSE_BUTTON;
1485: }
1.215 nicm 1486:
1487: /* Clear bracketed paste mode if at the prompt. */
1488: if (c->prompt_string != NULL)
1489: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1490:
1491: /* Set the terminal mode and reset attributes. */
1.59 nicm 1492: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1493: tty_reset(&c->tty);
1.17 nicm 1494: }
1495:
1496: /* Repeat time callback. */
1.190 nicm 1497: static void
1.170 nicm 1498: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1499: {
1500: struct client *c = data;
1501:
1.85 nicm 1502: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1503: server_client_set_key_table(c, NULL);
1.132 nicm 1504: c->flags &= ~CLIENT_REPEAT;
1505: server_status_client(c);
1.85 nicm 1506: }
1.192 nicm 1507: }
1508:
1509: /* Double-click callback. */
1510: static void
1511: server_client_click_timer(__unused int fd, __unused short events, void *data)
1512: {
1513: struct client *c = data;
1514:
1515: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1516: }
1517:
1.36 nicm 1518: /* Check if client should be exited. */
1.190 nicm 1519: static void
1.36 nicm 1520: server_client_check_exit(struct client *c)
1521: {
1522: if (!(c->flags & CLIENT_EXIT))
1523: return;
1524:
1.73 nicm 1525: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1526: return;
1527: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1528: return;
1.73 nicm 1529: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1530: return;
1531:
1.249 nicm 1532: if (c->flags & CLIENT_ATTACHED)
1533: notify_client("client-detached", c);
1.162 nicm 1534: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1535: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1536: }
1537:
1.218 nicm 1538: /* Redraw timer callback. */
1539: static void
1540: server_client_redraw_timer(__unused int fd, __unused short events,
1541: __unused void* data)
1542: {
1543: log_debug("redraw timer fired");
1544: }
1545:
1.1 nicm 1546: /* Check for client redraws. */
1.190 nicm 1547: static void
1.1 nicm 1548: server_client_check_redraw(struct client *c)
1549: {
1550: struct session *s = c->session;
1.137 nicm 1551: struct tty *tty = &c->tty;
1.1 nicm 1552: struct window_pane *wp;
1.256 nicm 1553: int needed, flags;
1.218 nicm 1554: struct timeval tv = { .tv_usec = 1000 };
1555: static struct event ev;
1556: size_t left;
1.1 nicm 1557:
1.86 nicm 1558: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1559: return;
1.257 nicm 1560: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1561: log_debug("%s: redraw%s%s%s", c->name,
1562: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1563: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1564: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "");
1565: }
1.218 nicm 1566:
1567: /*
1568: * If there is outstanding data, defer the redraw until it has been
1569: * consumed. We can just add a timer to get out of the event loop and
1570: * end up back here.
1571: */
1572: needed = 0;
1.256 nicm 1573: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1574: needed = 1;
1575: else {
1576: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1577: if (wp->flags & PANE_REDRAW) {
1578: needed = 1;
1579: break;
1580: }
1581: }
1582: }
1.241 nicm 1583: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1584: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1585: if (!evtimer_initialized(&ev))
1586: evtimer_set(&ev, server_client_redraw_timer, NULL);
1587: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1588: log_debug("redraw timer started");
1589: evtimer_add(&ev, &tv);
1.241 nicm 1590: }
1.218 nicm 1591:
1.241 nicm 1592: /*
1593: * We may have got here for a single pane redraw, but force a
1594: * full redraw next time in case other panes have been updated.
1595: */
1.256 nicm 1596: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241 nicm 1597: return;
1598: } else if (needed)
1.218 nicm 1599: log_debug("%s: redraw needed", c->name);
1.79 nicm 1600:
1.219 nicm 1601: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1602: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137 nicm 1603:
1.256 nicm 1604: if (~c->flags & CLIENT_REDRAWWINDOW) {
1605: /*
1606: * If not redrawing the entire window, check whether each pane
1607: * needs to be redrawn.
1608: */
1.1 nicm 1609: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1610: if (wp->flags & PANE_REDRAW) {
1611: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1612: screen_redraw_pane(c, wp);
1.137 nicm 1613: }
1.1 nicm 1614: }
1615: }
1616:
1.256 nicm 1617: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1618: if (options_get_number(s->options, "set-titles"))
1619: server_client_set_title(c);
1620: screen_redraw_screen(c);
1621: }
1.1 nicm 1622:
1.137 nicm 1623: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1624: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1625:
1.256 nicm 1626: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1627:
1628: if (needed) {
1629: /*
1630: * We would have deferred the redraw unless the output buffer
1631: * was empty, so we can record how many bytes the redraw
1632: * generated.
1633: */
1634: c->redraw = EVBUFFER_LENGTH(tty->out);
1635: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1636: }
1.1 nicm 1637: }
1638:
1639: /* Set client title. */
1.190 nicm 1640: static void
1.1 nicm 1641: server_client_set_title(struct client *c)
1642: {
1.128 nicm 1643: struct session *s = c->session;
1644: const char *template;
1645: char *title;
1646: struct format_tree *ft;
1.1 nicm 1647:
1.163 nicm 1648: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1649:
1.228 nicm 1650: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1651: format_defaults(ft, c, NULL, NULL, NULL);
1652:
1.269 nicm 1653: title = format_expand_time(ft, template);
1.1 nicm 1654: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1655: free(c->title);
1.1 nicm 1656: c->title = xstrdup(title);
1657: tty_set_title(&c->tty, c->title);
1658: }
1.77 nicm 1659: free(title);
1.128 nicm 1660:
1661: format_free(ft);
1.1 nicm 1662: }
1663:
1664: /* Dispatch message from client. */
1.190 nicm 1665: static void
1.162 nicm 1666: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1667: {
1.162 nicm 1668: struct client *c = arg;
1.73 nicm 1669: struct msg_stdin_data stdindata;
1.108 nicm 1670: const char *data;
1.162 nicm 1671: ssize_t datalen;
1.149 nicm 1672: struct session *s;
1.1 nicm 1673:
1.162 nicm 1674: if (c->flags & CLIENT_DEAD)
1675: return;
1676:
1677: if (imsg == NULL) {
1678: server_client_lost(c);
1679: return;
1680: }
1.1 nicm 1681:
1.162 nicm 1682: data = imsg->data;
1683: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1684:
1.162 nicm 1685: switch (imsg->hdr.type) {
1686: case MSG_IDENTIFY_FLAGS:
1687: case MSG_IDENTIFY_TERM:
1688: case MSG_IDENTIFY_TTYNAME:
1689: case MSG_IDENTIFY_CWD:
1690: case MSG_IDENTIFY_STDIN:
1691: case MSG_IDENTIFY_ENVIRON:
1692: case MSG_IDENTIFY_CLIENTPID:
1693: case MSG_IDENTIFY_DONE:
1694: server_client_dispatch_identify(c, imsg);
1695: break;
1696: case MSG_COMMAND:
1697: server_client_dispatch_command(c, imsg);
1698: break;
1699: case MSG_STDIN:
1700: if (datalen != sizeof stdindata)
1701: fatalx("bad MSG_STDIN size");
1702: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1703:
1.162 nicm 1704: if (c->stdin_callback == NULL)
1.33 nicm 1705: break;
1.162 nicm 1706: if (stdindata.size <= 0)
1707: c->stdin_closed = 1;
1708: else {
1709: evbuffer_add(c->stdin_data, stdindata.data,
1710: stdindata.size);
1711: }
1.279 nicm 1712: c->stdin_callback(c, c->stdin_closed, c->stdin_callback_data);
1.162 nicm 1713: break;
1714: case MSG_RESIZE:
1715: if (datalen != 0)
1716: fatalx("bad MSG_RESIZE size");
1.1 nicm 1717:
1.162 nicm 1718: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1719: break;
1.236 nicm 1720: tty_resize(&c->tty);
1721: recalculate_sizes();
1722: server_redraw_client(c);
1.174 nicm 1723: if (c->session != NULL)
1.196 nicm 1724: notify_client("client-resized", c);
1.162 nicm 1725: break;
1726: case MSG_EXITING:
1727: if (datalen != 0)
1728: fatalx("bad MSG_EXITING size");
1.1 nicm 1729:
1.162 nicm 1730: c->session = NULL;
1731: tty_close(&c->tty);
1732: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1733: break;
1734: case MSG_WAKEUP:
1735: case MSG_UNLOCK:
1736: if (datalen != 0)
1737: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1738:
1.162 nicm 1739: if (!(c->flags & CLIENT_SUSPENDED))
1740: break;
1741: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1742:
1.162 nicm 1743: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1744: break;
1.162 nicm 1745: s = c->session;
1.1 nicm 1746:
1.162 nicm 1747: if (gettimeofday(&c->activity_time, NULL) != 0)
1748: fatal("gettimeofday failed");
1749:
1750: tty_start_tty(&c->tty);
1751: server_redraw_client(c);
1752: recalculate_sizes();
1.184 nicm 1753:
1754: if (s != NULL)
1755: session_update_activity(s, &c->activity_time);
1.162 nicm 1756: break;
1757: case MSG_SHELL:
1758: if (datalen != 0)
1759: fatalx("bad MSG_SHELL size");
1.1 nicm 1760:
1.162 nicm 1761: server_client_dispatch_shell(c);
1762: break;
1.1 nicm 1763: }
1764: }
1765:
1.194 nicm 1766: /* Callback when command is done. */
1767: static enum cmd_retval
1.195 nicm 1768: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1769: {
1.195 nicm 1770: struct client *c = item->client;
1.194 nicm 1771:
1772: if (~c->flags & CLIENT_ATTACHED)
1773: c->flags |= CLIENT_EXIT;
1774: return (CMD_RETURN_NORMAL);
1775: }
1776:
1777: /* Show an error message. */
1778: static enum cmd_retval
1.195 nicm 1779: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1780: {
1781: char *error = data;
1782:
1.195 nicm 1783: cmdq_error(item, "%s", error);
1.194 nicm 1784: free(error);
1785:
1786: return (CMD_RETURN_NORMAL);
1787: }
1788:
1.1 nicm 1789: /* Handle command message. */
1.190 nicm 1790: static void
1.162 nicm 1791: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1792: {
1.108 nicm 1793: struct msg_command_data data;
1794: char *buf;
1795: size_t len;
1796: struct cmd_list *cmdlist = NULL;
1797: int argc;
1798: char **argv, *cause;
1.246 nicm 1799:
1800: if (c->flags & CLIENT_EXIT)
1801: return;
1.108 nicm 1802:
1803: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1804: fatalx("bad MSG_COMMAND size");
1805: memcpy(&data, imsg->data, sizeof data);
1806:
1.124 nicm 1807: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1808: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1809: if (len > 0 && buf[len - 1] != '\0')
1810: fatalx("bad MSG_COMMAND string");
1811:
1812: argc = data.argc;
1813: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1814: cause = xstrdup("command too long");
1.1 nicm 1815: goto error;
1816: }
1817:
1818: if (argc == 0) {
1819: argc = 1;
1820: argv = xcalloc(1, sizeof *argv);
1821: *argv = xstrdup("new-session");
1822: }
1823:
1.94 nicm 1824: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1825: cmd_free_argv(argc, argv);
1826: goto error;
1827: }
1828: cmd_free_argv(argc, argv);
1829:
1.194 nicm 1830: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1831: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1832: cmd_list_free(cmdlist);
1833: return;
1834:
1835: error:
1.194 nicm 1836: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1837:
1.1 nicm 1838: if (cmdlist != NULL)
1839: cmd_list_free(cmdlist);
1.88 nicm 1840:
1.36 nicm 1841: c->flags |= CLIENT_EXIT;
1.1 nicm 1842: }
1843:
1844: /* Handle identify message. */
1.190 nicm 1845: static void
1.162 nicm 1846: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1847: {
1.165 nicm 1848: const char *data, *home;
1.232 nicm 1849: size_t datalen;
1.109 nicm 1850: int flags;
1.216 nicm 1851: char *name;
1.109 nicm 1852:
1853: if (c->flags & CLIENT_IDENTIFIED)
1854: fatalx("out-of-order identify message");
1855:
1856: data = imsg->data;
1857: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1858:
1859: switch (imsg->hdr.type) {
1860: case MSG_IDENTIFY_FLAGS:
1861: if (datalen != sizeof flags)
1862: fatalx("bad MSG_IDENTIFY_FLAGS size");
1863: memcpy(&flags, data, sizeof flags);
1864: c->flags |= flags;
1.158 nicm 1865: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1866: break;
1867: case MSG_IDENTIFY_TERM:
1.110 nicm 1868: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1869: fatalx("bad MSG_IDENTIFY_TERM string");
1870: c->term = xstrdup(data);
1.158 nicm 1871: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1872: break;
1873: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1874: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1875: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1876: c->ttyname = xstrdup(data);
1.158 nicm 1877: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1878: break;
1879: case MSG_IDENTIFY_CWD:
1.155 nicm 1880: if (datalen == 0 || data[datalen - 1] != '\0')
1881: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1882: if (access(data, X_OK) == 0)
1883: c->cwd = xstrdup(data);
1884: else if ((home = find_home()) != NULL)
1885: c->cwd = xstrdup(home);
1886: else
1887: c->cwd = xstrdup("/");
1.158 nicm 1888: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1889: break;
1890: case MSG_IDENTIFY_STDIN:
1891: if (datalen != 0)
1892: fatalx("bad MSG_IDENTIFY_STDIN size");
1893: c->fd = imsg->fd;
1.158 nicm 1894: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1895: break;
1896: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1897: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1898: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1899: if (strchr(data, '=') != NULL)
1.164 nicm 1900: environ_put(c->environ, data);
1.158 nicm 1901: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1902: break;
1903: case MSG_IDENTIFY_CLIENTPID:
1904: if (datalen != sizeof c->pid)
1905: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1906: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1907: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1908: break;
1909: default:
1910: break;
1911: }
1912:
1913: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1914: return;
1915: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1916:
1.216 nicm 1917: if (*c->ttyname != '\0')
1918: name = xstrdup(c->ttyname);
1919: else
1920: xasprintf(&name, "client-%ld", (long)c->pid);
1921: c->name = name;
1922: log_debug("client %p name is %s", c, c->name);
1923:
1.109 nicm 1924: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1925: c->stdin_callback = control_callback;
1.109 nicm 1926:
1.97 nicm 1927: evbuffer_free(c->stderr_data);
1928: c->stderr_data = c->stdout_data;
1.109 nicm 1929:
1930: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1931: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1932: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1933:
1934: c->tty.fd = -1;
1935:
1.109 nicm 1936: close(c->fd);
1937: c->fd = -1;
1938:
1.75 nicm 1939: return;
1940: }
1.1 nicm 1941:
1.109 nicm 1942: if (c->fd == -1)
1.104 nicm 1943: return;
1.145 nicm 1944: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1945: close(c->fd);
1946: c->fd = -1;
1.80 nicm 1947: return;
1948: }
1.109 nicm 1949: if (c->flags & CLIENT_UTF8)
1.1 nicm 1950: c->tty.flags |= TTY_UTF8;
1.109 nicm 1951: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1952: c->tty.term_flags |= TERM_256COLOURS;
1953:
1954: tty_resize(&c->tty);
1955:
1.109 nicm 1956: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1957: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1958: }
1959:
1960: /* Handle shell message. */
1.190 nicm 1961: static void
1.162 nicm 1962: server_client_dispatch_shell(struct client *c)
1.1 nicm 1963: {
1.107 nicm 1964: const char *shell;
1.25 nicm 1965:
1.163 nicm 1966: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1967: if (*shell == '\0' || areshell(shell))
1968: shell = _PATH_BSHELL;
1.240 nicm 1969: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 1970:
1.162 nicm 1971: proc_kill_peer(c->peer);
1.169 nicm 1972: }
1973:
1974: /* Event callback to push more stdout data if any left. */
1975: static void
1.170 nicm 1976: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1977: {
1978: struct client *c = arg;
1979:
1980: if (~c->flags & CLIENT_DEAD)
1981: server_client_push_stdout(c);
1982: server_client_unref(c);
1983: }
1984:
1985: /* Push stdout to client if possible. */
1986: void
1987: server_client_push_stdout(struct client *c)
1988: {
1989: struct msg_stdout_data data;
1.232 nicm 1990: size_t sent, left;
1.169 nicm 1991:
1992: left = EVBUFFER_LENGTH(c->stdout_data);
1993: while (left != 0) {
1994: sent = left;
1995: if (sent > sizeof data.data)
1996: sent = sizeof data.data;
1997: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1998: data.size = sent;
1999:
2000: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
2001: break;
2002: evbuffer_drain(c->stdout_data, sent);
2003:
2004: left = EVBUFFER_LENGTH(c->stdout_data);
2005: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
2006: sent, left);
2007: }
2008: if (left != 0) {
2009: c->references++;
2010: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
2011: log_debug("%s: client %p, queued", __func__, c);
2012: }
2013: }
2014:
2015: /* Event callback to push more stderr data if any left. */
2016: static void
1.170 nicm 2017: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 2018: {
2019: struct client *c = arg;
2020:
2021: if (~c->flags & CLIENT_DEAD)
2022: server_client_push_stderr(c);
2023: server_client_unref(c);
2024: }
2025:
2026: /* Push stderr to client if possible. */
2027: void
2028: server_client_push_stderr(struct client *c)
2029: {
2030: struct msg_stderr_data data;
1.232 nicm 2031: size_t sent, left;
1.169 nicm 2032:
2033: if (c->stderr_data == c->stdout_data) {
2034: server_client_push_stdout(c);
2035: return;
2036: }
2037:
2038: left = EVBUFFER_LENGTH(c->stderr_data);
2039: while (left != 0) {
2040: sent = left;
2041: if (sent > sizeof data.data)
2042: sent = sizeof data.data;
2043: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
2044: data.size = sent;
2045:
2046: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
2047: break;
2048: evbuffer_drain(c->stderr_data, sent);
2049:
2050: left = EVBUFFER_LENGTH(c->stderr_data);
2051: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
2052: sent, left);
2053: }
2054: if (left != 0) {
2055: c->references++;
2056: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
2057: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 2058: }
2059: }
2060:
2061: /* Add to client message log. */
2062: void
2063: server_client_add_message(struct client *c, const char *fmt, ...)
2064: {
2065: struct message_entry *msg, *msg1;
2066: char *s;
2067: va_list ap;
2068: u_int limit;
2069:
2070: va_start(ap, fmt);
2071: xvasprintf(&s, fmt, ap);
2072: va_end(ap);
2073:
1.216 nicm 2074: log_debug("message %s (client %p)", s, c);
1.212 nicm 2075:
2076: msg = xcalloc(1, sizeof *msg);
2077: msg->msg_time = time(NULL);
2078: msg->msg_num = c->message_next++;
2079: msg->msg = s;
2080: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2081:
2082: limit = options_get_number(global_options, "message-limit");
2083: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2084: if (msg->msg_num + limit >= c->message_next)
2085: break;
2086: free(msg->msg);
2087: TAILQ_REMOVE(&c->message_log, msg, entry);
2088: free(msg);
1.169 nicm 2089: }
1.213 nicm 2090: }
2091:
2092: /* Get client working directory. */
2093: const char *
1.250 nicm 2094: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2095: {
1.250 nicm 2096: const char *home;
1.213 nicm 2097:
1.265 nicm 2098: if (!cfg_finished && cfg_client != NULL)
2099: return (cfg_client->cwd);
1.213 nicm 2100: if (c != NULL && c->session == NULL && c->cwd != NULL)
2101: return (c->cwd);
1.250 nicm 2102: if (s != NULL && s->cwd != NULL)
2103: return (s->cwd);
1.213 nicm 2104: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2105: return (s->cwd);
1.250 nicm 2106: if ((home = find_home()) != NULL)
2107: return (home);
2108: return ("/");
1.213 nicm 2109: }
2110:
2111: /* Resolve an absolute path or relative to client working directory. */
2112: char *
2113: server_client_get_path(struct client *c, const char *file)
2114: {
2115: char *path, resolved[PATH_MAX];
2116:
2117: if (*file == '/')
2118: path = xstrdup(file);
2119: else
1.250 nicm 2120: xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213 nicm 2121: if (realpath(path, resolved) == NULL)
2122: return (path);
2123: free(path);
2124: return (xstrdup(resolved));
1.1 nicm 2125: }