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