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