Annotation of src/usr.bin/tmux/server-client.c, Revision 1.201
1.201 ! nicm 1: /* $OpenBSD: server-client.c,v 1.200 2016/11/16 11:37:16 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 *);
46:
47: static void server_client_dispatch(struct imsg *, void *);
48: static void server_client_dispatch_command(struct client *, struct imsg *);
49: static void server_client_dispatch_identify(struct client *, struct imsg *);
50: static void server_client_dispatch_shell(struct client *);
1.140 nicm 51:
52: /* Check if this client is inside this server. */
53: int
54: server_client_check_nested(struct client *c)
55: {
56: struct environ_entry *envent;
57: struct window_pane *wp;
58:
59: if (c->tty.path == NULL)
60: return (0);
61:
1.164 nicm 62: envent = environ_find(c->environ, "TMUX");
1.140 nicm 63: if (envent == NULL || *envent->value == '\0')
64: return (0);
65:
66: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
67: if (strcmp(wp->tty, c->tty.path) == 0)
68: return (1);
69: }
70: return (0);
71: }
1.1 nicm 72:
1.132 nicm 73: /* Set client key table. */
74: void
1.178 nicm 75: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 76: {
1.178 nicm 77: if (name == NULL)
78: name = server_client_get_key_table(c);
79:
1.132 nicm 80: key_bindings_unref_table(c->keytable);
81: c->keytable = key_bindings_get_table(name, 1);
82: c->keytable->references++;
83: }
84:
1.178 nicm 85: /* Get default key table. */
86: const char *
87: server_client_get_key_table(struct client *c)
88: {
89: struct session *s = c->session;
90: const char *name;
91:
92: if (s == NULL)
93: return ("root");
94:
95: name = options_get_string(s->options, "key-table");
96: if (*name == '\0')
97: return ("root");
98: return (name);
99: }
100:
1.191 nicm 101: /* Is this client using the default key table? */
102: int
103: server_client_is_default_key_table(struct client *c)
104: {
105: return (strcmp(c->keytable->name, server_client_get_key_table(c)) == 0);
106: }
107:
1.1 nicm 108: /* Create a new client. */
109: void
110: server_client_create(int fd)
111: {
112: struct client *c;
113:
1.49 nicm 114: setblocking(fd, 0);
1.1 nicm 115:
116: c = xcalloc(1, sizeof *c);
1.141 nicm 117: c->references = 1;
1.162 nicm 118: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 119:
1.10 nicm 120: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 121: fatal("gettimeofday failed");
1.11 nicm 122: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 123:
1.164 nicm 124: c->environ = environ_create();
1.1 nicm 125:
1.146 nicm 126: c->fd = -1;
1.165 nicm 127: c->cwd = NULL;
1.145 nicm 128:
1.194 nicm 129: TAILQ_INIT(&c->queue);
1.94 nicm 130:
1.116 nicm 131: c->stdin_data = evbuffer_new();
132: c->stdout_data = evbuffer_new();
133: c->stderr_data = evbuffer_new();
1.33 nicm 134:
1.1 nicm 135: c->tty.fd = -1;
136: c->title = NULL;
137:
138: c->session = NULL;
1.45 nicm 139: c->last_session = NULL;
1.1 nicm 140: c->tty.sx = 80;
141: c->tty.sy = 24;
142:
143: screen_init(&c->status, c->tty.sx, 1, 0);
144:
145: c->message_string = NULL;
1.136 nicm 146: TAILQ_INIT(&c->message_log);
1.1 nicm 147:
148: c->prompt_string = NULL;
149: c->prompt_buffer = NULL;
150: c->prompt_index = 0;
151:
1.93 nicm 152: c->flags |= CLIENT_FOCUSED;
153:
1.132 nicm 154: c->keytable = key_bindings_get_table("root", 1);
155: c->keytable->references++;
156:
1.17 nicm 157: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 158: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 159:
1.135 nicm 160: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 161: log_debug("new client %p", c);
1.72 nicm 162: }
163:
164: /* Open client terminal if needed. */
165: int
1.119 nicm 166: server_client_open(struct client *c, char **cause)
1.72 nicm 167: {
1.75 nicm 168: if (c->flags & CLIENT_CONTROL)
169: return (0);
1.120 nicm 170:
171: if (strcmp(c->ttyname, "/dev/tty") == 0) {
172: *cause = xstrdup("can't use /dev/tty");
173: return (-1);
174: }
1.75 nicm 175:
1.72 nicm 176: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 177: *cause = xstrdup("not a terminal");
1.72 nicm 178: return (-1);
179: }
180:
1.119 nicm 181: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 182: return (-1);
183:
184: return (0);
1.1 nicm 185: }
186:
187: /* Lost a client. */
188: void
189: server_client_lost(struct client *c)
190: {
1.136 nicm 191: struct message_entry *msg, *msg1;
1.1 nicm 192:
1.141 nicm 193: c->flags |= CLIENT_DEAD;
194:
1.187 nicm 195: server_clear_identify(c, NULL);
1.141 nicm 196: status_prompt_clear(c);
197: status_message_clear(c);
198:
199: if (c->stdin_callback != NULL)
200: c->stdin_callback(c, 1, c->stdin_callback_data);
201:
1.135 nicm 202: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 203: log_debug("lost client %p", c);
1.1 nicm 204:
205: /*
206: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
207: * and tty_free might close an unrelated fd.
208: */
209: if (c->flags & CLIENT_TERMINAL)
210: tty_free(&c->tty);
1.109 nicm 211: free(c->ttyname);
212: free(c->term);
1.1 nicm 213:
1.113 nicm 214: evbuffer_free(c->stdin_data);
215: evbuffer_free(c->stdout_data);
1.97 nicm 216: if (c->stderr_data != c->stdout_data)
1.126 nicm 217: evbuffer_free(c->stderr_data);
1.33 nicm 218:
1.148 nicm 219: if (event_initialized(&c->status_timer))
220: evtimer_del(&c->status_timer);
1.1 nicm 221: screen_free(&c->status);
222:
1.77 nicm 223: free(c->title);
1.165 nicm 224: free((void *)c->cwd);
1.1 nicm 225:
1.17 nicm 226: evtimer_del(&c->repeat_timer);
1.192 nicm 227: evtimer_del(&c->click_timer);
1.17 nicm 228:
1.132 nicm 229: key_bindings_unref_table(c->keytable);
230:
1.69 nicm 231: if (event_initialized(&c->identify_timer))
232: evtimer_del(&c->identify_timer);
1.15 nicm 233:
1.77 nicm 234: free(c->message_string);
1.116 nicm 235: if (event_initialized(&c->message_timer))
1.69 nicm 236: evtimer_del(&c->message_timer);
1.136 nicm 237: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 238: free(msg->msg);
1.136 nicm 239: TAILQ_REMOVE(&c->message_log, msg, entry);
240: free(msg);
1.21 nicm 241: }
1.1 nicm 242:
1.77 nicm 243: free(c->prompt_string);
244: free(c->prompt_buffer);
1.61 nicm 245:
1.164 nicm 246: environ_free(c->environ);
1.1 nicm 247:
1.162 nicm 248: proc_remove_peer(c->peer);
249: c->peer = NULL;
1.13 nicm 250:
1.142 nicm 251: server_client_unref(c);
1.71 nicm 252:
253: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 254:
255: recalculate_sizes();
1.41 nicm 256: server_check_unattached();
1.19 nicm 257: server_update_socket();
1.141 nicm 258: }
259:
260: /* Remove reference from a client. */
261: void
1.142 nicm 262: server_client_unref(struct client *c)
1.141 nicm 263: {
1.157 nicm 264: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 265:
266: c->references--;
267: if (c->references == 0)
268: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
269: }
270:
271: /* Free dead client. */
1.190 nicm 272: static void
1.170 nicm 273: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 274: {
275: struct client *c = arg;
276:
1.157 nicm 277: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 278:
1.194 nicm 279: if (!TAILQ_EMPTY(&c->queue))
280: fatalx("queue not empty");
281:
1.141 nicm 282: if (c->references == 0)
283: free(c);
1.9 nicm 284: }
285:
1.174 nicm 286: /* Detach a client. */
287: void
288: server_client_detach(struct client *c, enum msgtype msgtype)
289: {
290: struct session *s = c->session;
291:
292: if (s == NULL)
293: return;
294:
1.196 nicm 295: notify_client("client-detached", c);
1.174 nicm 296: proc_send_s(c->peer, msgtype, s->name);
297: }
298:
1.66 nicm 299: /* Check for mouse keys. */
1.190 nicm 300: static key_code
1.131 nicm 301: server_client_check_mouse(struct client *c)
1.66 nicm 302: {
1.192 nicm 303: struct session *s = c->session;
304: struct mouse_event *m = &c->tty.mouse;
305: struct window *w;
306: struct window_pane *wp;
307: u_int x, y, b;
308: int flag;
309: key_code key;
310: struct timeval tv;
311: enum { NOTYPE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type = NOTYPE;
312: enum { NOWHERE, PANE, STATUS, BORDER } where = NOWHERE;
1.131 nicm 313:
314: log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
315: m->lx, m->ly, c->tty.mouse_drag_flag);
316:
317: /* What type of event is this? */
318: if (MOUSE_DRAG(m->b)) {
319: type = DRAG;
320: if (c->tty.mouse_drag_flag) {
321: x = m->x, y = m->y, b = m->b;
322: log_debug("drag update at %u,%u", x, y);
323: } else {
324: x = m->lx, y = m->ly, b = m->lb;
325: log_debug("drag start at %u,%u", x, y);
326: }
327: } else if (MOUSE_WHEEL(m->b)) {
328: type = WHEEL;
329: x = m->x, y = m->y, b = m->b;
330: log_debug("wheel at %u,%u", x, y);
1.200 nicm 331: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 332: type = UP;
333: x = m->x, y = m->y, b = m->lb;
334: log_debug("up at %u,%u", x, y);
335: } else {
1.192 nicm 336: if (c->flags & CLIENT_DOUBLECLICK) {
337: evtimer_del(&c->click_timer);
338: c->flags &= ~CLIENT_DOUBLECLICK;
339: if (m->b == c->click_button) {
340: type = DOUBLE;
341: x = m->x, y = m->y, b = m->b;
342: log_debug("double-click at %u,%u", x, y);
343: flag = CLIENT_TRIPLECLICK;
344: goto add_timer;
345: }
346: } else if (c->flags & CLIENT_TRIPLECLICK) {
347: evtimer_del(&c->click_timer);
348: c->flags &= ~CLIENT_TRIPLECLICK;
349: if (m->b == c->click_button) {
350: type = TRIPLE;
351: x = m->x, y = m->y, b = m->b;
352: log_debug("triple-click at %u,%u", x, y);
353: goto have_event;
354: }
355: }
356:
1.131 nicm 357: type = DOWN;
358: x = m->x, y = m->y, b = m->b;
359: log_debug("down at %u,%u", x, y);
1.192 nicm 360: flag = CLIENT_DOUBLECLICK;
361:
362: add_timer:
363: if (KEYC_CLICK_TIMEOUT != 0) {
364: c->flags |= flag;
365: c->click_button = m->b;
366:
367: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
368: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
369: evtimer_del(&c->click_timer);
370: evtimer_add(&c->click_timer, &tv);
371: }
1.131 nicm 372: }
1.192 nicm 373:
374: have_event:
1.131 nicm 375: if (type == NOTYPE)
1.177 nicm 376: return (KEYC_UNKNOWN);
1.131 nicm 377:
378: /* Always save the session. */
379: m->s = s->id;
380:
381: /* Is this on the status line? */
382: m->statusat = status_at_line(c);
383: if (m->statusat != -1 && y == (u_int)m->statusat) {
384: w = status_get_window_at(c, x);
385: if (w == NULL)
1.177 nicm 386: return (KEYC_UNKNOWN);
1.131 nicm 387: m->w = w->id;
388: where = STATUS;
389: } else
390: m->w = -1;
391:
392: /* Not on status line. Adjust position and check for border or pane. */
393: if (where == NOWHERE) {
394: if (m->statusat == 0 && y > 0)
395: y--;
396: else if (m->statusat > 0 && y >= (u_int)m->statusat)
397: y = m->statusat - 1;
398:
399: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
400: if ((wp->xoff + wp->sx == x &&
401: wp->yoff <= 1 + y &&
402: wp->yoff + wp->sy >= y) ||
403: (wp->yoff + wp->sy == y &&
404: wp->xoff <= 1 + x &&
405: wp->xoff + wp->sx >= x))
406: break;
407: }
408: if (wp != NULL)
409: where = BORDER;
410: else {
411: wp = window_get_active_at(s->curw->window, x, y);
1.173 nicm 412: if (wp != NULL) {
1.131 nicm 413: where = PANE;
1.173 nicm 414: log_debug("mouse at %u,%u is on pane %%%u",
415: x, y, wp->id);
416: }
1.131 nicm 417: }
418: if (where == NOWHERE)
1.177 nicm 419: return (KEYC_UNKNOWN);
1.131 nicm 420: m->wp = wp->id;
421: m->w = wp->window->id;
422: } else
423: m->wp = -1;
424:
425: /* Stop dragging if needed. */
1.200 nicm 426: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 427: if (c->tty.mouse_drag_release != NULL)
428: c->tty.mouse_drag_release(c, m);
429:
430: c->tty.mouse_drag_update = NULL;
431: c->tty.mouse_drag_release = NULL;
432:
1.182 nicm 433: /*
1.183 nicm 434: * End a mouse drag by passing a MouseDragEnd key corresponding
435: * to the button that started the drag.
1.182 nicm 436: */
437: switch (c->tty.mouse_drag_flag) {
438: case 1:
439: if (where == PANE)
1.183 nicm 440: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 441: if (where == STATUS)
1.183 nicm 442: key = KEYC_MOUSEDRAGEND1_STATUS;
1.182 nicm 443: if (where == BORDER)
1.183 nicm 444: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 445: break;
446: case 2:
447: if (where == PANE)
1.183 nicm 448: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 449: if (where == STATUS)
1.183 nicm 450: key = KEYC_MOUSEDRAGEND2_STATUS;
1.182 nicm 451: if (where == BORDER)
1.183 nicm 452: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 453: break;
454: case 3:
455: if (where == PANE)
1.183 nicm 456: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 457: if (where == STATUS)
1.183 nicm 458: key = KEYC_MOUSEDRAGEND3_STATUS;
1.182 nicm 459: if (where == BORDER)
1.183 nicm 460: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 461: break;
462: default:
463: key = KEYC_MOUSE;
464: break;
465: }
1.131 nicm 466: c->tty.mouse_drag_flag = 0;
1.182 nicm 467:
468: return (key);
1.131 nicm 469: }
470:
471: /* Convert to a key binding. */
1.177 nicm 472: key = KEYC_UNKNOWN;
1.131 nicm 473: switch (type) {
474: case NOTYPE:
475: break;
476: case DRAG:
477: if (c->tty.mouse_drag_update != NULL)
478: c->tty.mouse_drag_update(c, m);
479: else {
480: switch (MOUSE_BUTTONS(b)) {
481: case 0:
482: if (where == PANE)
483: key = KEYC_MOUSEDRAG1_PANE;
484: if (where == STATUS)
485: key = KEYC_MOUSEDRAG1_STATUS;
486: if (where == BORDER)
487: key = KEYC_MOUSEDRAG1_BORDER;
488: break;
489: case 1:
490: if (where == PANE)
491: key = KEYC_MOUSEDRAG2_PANE;
492: if (where == STATUS)
493: key = KEYC_MOUSEDRAG2_STATUS;
494: if (where == BORDER)
495: key = KEYC_MOUSEDRAG2_BORDER;
496: break;
497: case 2:
498: if (where == PANE)
499: key = KEYC_MOUSEDRAG3_PANE;
500: if (where == STATUS)
501: key = KEYC_MOUSEDRAG3_STATUS;
502: if (where == BORDER)
503: key = KEYC_MOUSEDRAG3_BORDER;
504: break;
505: }
506: }
1.66 nicm 507:
1.182 nicm 508: /*
509: * Begin a drag by setting the flag to a non-zero value that
510: * corresponds to the mouse button in use.
511: */
512: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 513: break;
514: case WHEEL:
515: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
516: if (where == PANE)
517: key = KEYC_WHEELUP_PANE;
518: if (where == STATUS)
519: key = KEYC_WHEELUP_STATUS;
520: if (where == BORDER)
521: key = KEYC_WHEELUP_BORDER;
522: } else {
523: if (where == PANE)
524: key = KEYC_WHEELDOWN_PANE;
525: if (where == STATUS)
526: key = KEYC_WHEELDOWN_STATUS;
527: if (where == BORDER)
528: key = KEYC_WHEELDOWN_BORDER;
529: }
530: break;
531: case UP:
532: switch (MOUSE_BUTTONS(b)) {
533: case 0:
534: if (where == PANE)
535: key = KEYC_MOUSEUP1_PANE;
536: if (where == STATUS)
537: key = KEYC_MOUSEUP1_STATUS;
538: if (where == BORDER)
539: key = KEYC_MOUSEUP1_BORDER;
540: break;
541: case 1:
542: if (where == PANE)
543: key = KEYC_MOUSEUP2_PANE;
544: if (where == STATUS)
545: key = KEYC_MOUSEUP2_STATUS;
546: if (where == BORDER)
547: key = KEYC_MOUSEUP2_BORDER;
548: break;
549: case 2:
550: if (where == PANE)
551: key = KEYC_MOUSEUP3_PANE;
552: if (where == STATUS)
553: key = KEYC_MOUSEUP3_STATUS;
554: if (where == BORDER)
555: key = KEYC_MOUSEUP3_BORDER;
556: break;
557: }
558: break;
559: case DOWN:
560: switch (MOUSE_BUTTONS(b)) {
561: case 0:
562: if (where == PANE)
563: key = KEYC_MOUSEDOWN1_PANE;
564: if (where == STATUS)
565: key = KEYC_MOUSEDOWN1_STATUS;
566: if (where == BORDER)
567: key = KEYC_MOUSEDOWN1_BORDER;
568: break;
569: case 1:
570: if (where == PANE)
571: key = KEYC_MOUSEDOWN2_PANE;
572: if (where == STATUS)
573: key = KEYC_MOUSEDOWN2_STATUS;
574: if (where == BORDER)
575: key = KEYC_MOUSEDOWN2_BORDER;
576: break;
577: case 2:
578: if (where == PANE)
579: key = KEYC_MOUSEDOWN3_PANE;
580: if (where == STATUS)
581: key = KEYC_MOUSEDOWN3_STATUS;
582: if (where == BORDER)
583: key = KEYC_MOUSEDOWN3_BORDER;
584: break;
1.66 nicm 585: }
1.131 nicm 586: break;
1.192 nicm 587: case DOUBLE:
588: switch (MOUSE_BUTTONS(b)) {
589: case 0:
590: if (where == PANE)
591: key = KEYC_DOUBLECLICK1_PANE;
592: if (where == STATUS)
593: key = KEYC_DOUBLECLICK1_STATUS;
594: if (where == BORDER)
595: key = KEYC_DOUBLECLICK1_BORDER;
596: break;
597: case 1:
598: if (where == PANE)
599: key = KEYC_DOUBLECLICK2_PANE;
600: if (where == STATUS)
601: key = KEYC_DOUBLECLICK2_STATUS;
602: if (where == BORDER)
603: key = KEYC_DOUBLECLICK2_BORDER;
604: break;
605: case 2:
606: if (where == PANE)
607: key = KEYC_DOUBLECLICK3_PANE;
608: if (where == STATUS)
609: key = KEYC_DOUBLECLICK3_STATUS;
610: if (where == BORDER)
611: key = KEYC_DOUBLECLICK3_BORDER;
612: break;
613: }
614: break;
615: case TRIPLE:
616: switch (MOUSE_BUTTONS(b)) {
617: case 0:
618: if (where == PANE)
619: key = KEYC_TRIPLECLICK1_PANE;
620: if (where == STATUS)
621: key = KEYC_TRIPLECLICK1_STATUS;
622: if (where == BORDER)
623: key = KEYC_TRIPLECLICK1_BORDER;
624: break;
625: case 1:
626: if (where == PANE)
627: key = KEYC_TRIPLECLICK2_PANE;
628: if (where == STATUS)
629: key = KEYC_TRIPLECLICK2_STATUS;
630: if (where == BORDER)
631: key = KEYC_TRIPLECLICK2_BORDER;
632: break;
633: case 2:
634: if (where == PANE)
635: key = KEYC_TRIPLECLICK3_PANE;
636: if (where == STATUS)
637: key = KEYC_TRIPLECLICK3_STATUS;
638: if (where == BORDER)
639: key = KEYC_TRIPLECLICK3_BORDER;
640: break;
641: }
642: break;
1.66 nicm 643: }
1.177 nicm 644: if (key == KEYC_UNKNOWN)
645: return (KEYC_UNKNOWN);
1.66 nicm 646:
1.131 nicm 647: /* Apply modifiers if any. */
648: if (b & MOUSE_MASK_META)
649: key |= KEYC_ESCAPE;
650: if (b & MOUSE_MASK_CTRL)
651: key |= KEYC_CTRL;
652: if (b & MOUSE_MASK_SHIFT)
653: key |= KEYC_SHIFT;
1.66 nicm 654:
1.131 nicm 655: return (key);
1.66 nicm 656: }
657:
1.82 nicm 658: /* Is this fast enough to probably be a paste? */
1.190 nicm 659: static int
1.82 nicm 660: server_client_assume_paste(struct session *s)
661: {
662: struct timeval tv;
1.84 nicm 663: int t;
1.82 nicm 664:
1.163 nicm 665: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 666: return (0);
1.82 nicm 667:
668: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 669: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
670: log_debug("session %s pasting (flag %d)", s->name,
671: !!(s->flags & SESSION_PASTING));
672: if (s->flags & SESSION_PASTING)
673: return (1);
674: s->flags |= SESSION_PASTING;
675: return (0);
676: }
677: log_debug("session %s not pasting", s->name);
678: s->flags &= ~SESSION_PASTING;
1.83 nicm 679: return (0);
1.82 nicm 680: }
681:
1.18 nicm 682: /* Handle data key input from client. */
683: void
1.168 nicm 684: server_client_handle_key(struct client *c, key_code key)
1.18 nicm 685: {
1.131 nicm 686: struct mouse_event *m = &c->tty.mouse;
1.132 nicm 687: struct session *s = c->session;
1.18 nicm 688: struct window *w;
689: struct window_pane *wp;
690: struct timeval tv;
1.191 nicm 691: const char *name;
1.156 nicm 692: struct key_table *table;
1.132 nicm 693: struct key_binding bd_find, *bd;
694: int xtimeout;
1.201 ! nicm 695: struct cmd_find_state fs;
1.18 nicm 696:
697: /* Check the client is good to accept input. */
1.132 nicm 698: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18 nicm 699: return;
1.132 nicm 700: w = s->curw->window;
1.191 nicm 701: if (KEYC_IS_MOUSE(key))
702: wp = cmd_mouse_pane(m, NULL, NULL);
703: else
704: wp = w->active;
1.18 nicm 705:
706: /* Update the activity timer. */
707: if (gettimeofday(&c->activity_time, NULL) != 0)
708: fatal("gettimeofday failed");
1.149 nicm 709: session_update_activity(s, &c->activity_time);
1.18 nicm 710:
1.132 nicm 711: /* Number keys jump to pane in identify mode. */
1.25 nicm 712: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 713: if (c->flags & CLIENT_READONLY)
714: return;
1.95 nicm 715: window_unzoom(w);
1.18 nicm 716: wp = window_pane_at_index(w, key - '0');
1.187 nicm 717: if (wp != NULL && !window_pane_visible(wp))
718: wp = NULL;
719: server_clear_identify(c, wp);
1.18 nicm 720: return;
721: }
722:
723: /* Handle status line. */
1.30 nicm 724: if (!(c->flags & CLIENT_READONLY)) {
725: status_message_clear(c);
1.187 nicm 726: server_clear_identify(c, NULL);
1.30 nicm 727: }
1.18 nicm 728: if (c->prompt_string != NULL) {
1.193 nicm 729: if (c->flags & CLIENT_READONLY)
730: return;
731: if (status_prompt_key(c, key) == 0)
732: return;
1.18 nicm 733: }
734:
735: /* Check for mouse keys. */
736: if (key == KEYC_MOUSE) {
1.30 nicm 737: if (c->flags & CLIENT_READONLY)
738: return;
1.131 nicm 739: key = server_client_check_mouse(c);
1.177 nicm 740: if (key == KEYC_UNKNOWN)
1.131 nicm 741: return;
742:
743: m->valid = 1;
744: m->key = key;
745:
1.163 nicm 746: if (!options_get_number(s->options, "mouse"))
1.161 nicm 747: goto forward;
1.131 nicm 748: } else
749: m->valid = 0;
1.18 nicm 750:
1.132 nicm 751: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 752: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
753: goto forward;
1.18 nicm 754:
1.132 nicm 755: retry:
1.191 nicm 756: /*
757: * Work out the current key table. If the pane is in a mode, use
758: * the mode table instead of the default key table.
759: */
760: name = NULL;
761: if (wp != NULL && wp->mode != NULL && wp->mode->key_table != NULL)
762: name = wp->mode->key_table(wp);
763: if (name == NULL || !server_client_is_default_key_table(c))
764: table = c->keytable;
765: else
766: table = key_bindings_get_table(name, 1);
767:
1.132 nicm 768: /* Try to see if there is a key binding in the current table. */
769: bd_find.key = key;
1.191 nicm 770: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132 nicm 771: if (bd != NULL) {
772: /*
773: * Key was matched in this table. If currently repeating but a
774: * non-repeating binding was found, stop repeating and try
775: * again in the root table.
776: */
777: if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
1.178 nicm 778: server_client_set_key_table(c, NULL);
1.132 nicm 779: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 780: server_status_client(c);
1.132 nicm 781: goto retry;
1.18 nicm 782: }
1.82 nicm 783:
1.132 nicm 784: /*
785: * Take a reference to this table to make sure the key binding
786: * doesn't disappear.
787: */
788: table->references++;
789:
790: /*
791: * If this is a repeating key, start the timer. Otherwise reset
792: * the client back to the root table.
793: */
1.163 nicm 794: xtimeout = options_get_number(s->options, "repeat-time");
1.132 nicm 795: if (xtimeout != 0 && bd->can_repeat) {
796: c->flags |= CLIENT_REPEAT;
797:
798: tv.tv_sec = xtimeout / 1000;
799: tv.tv_usec = (xtimeout % 1000) * 1000L;
800: evtimer_del(&c->repeat_timer);
801: evtimer_add(&c->repeat_timer, &tv);
802: } else {
1.18 nicm 803: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 804: server_client_set_key_table(c, NULL);
1.18 nicm 805: }
1.132 nicm 806: server_status_client(c);
807:
1.201 ! nicm 808: /* Find default state if the pane is known. */
! 809: cmd_find_clear_state(&fs, NULL, 0);
! 810: if (wp != NULL) {
! 811: fs.s = s;
! 812: fs.wl = fs.s->curw;
! 813: fs.w = fs.wl->window;
! 814: fs.wp = wp;
! 815: cmd_find_log_state(__func__, &fs);
! 816:
! 817: if (!cmd_find_valid_state(&fs))
! 818: fatalx("invalid key state");
! 819: }
! 820:
1.132 nicm 821: /* Dispatch the key binding. */
1.201 ! nicm 822: key_bindings_dispatch(bd, c, m, &fs);
1.132 nicm 823: key_bindings_unref_table(table);
1.18 nicm 824: return;
825: }
826:
1.132 nicm 827: /*
828: * No match in this table. If repeating, switch the client back to the
829: * root table and try again.
830: */
831: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 832: server_client_set_key_table(c, NULL);
1.18 nicm 833: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 834: server_status_client(c);
835: goto retry;
1.18 nicm 836: }
837:
1.132 nicm 838: /* If no match and we're not in the root table, that's it. */
1.191 nicm 839: if (name == NULL && !server_client_is_default_key_table(c)) {
1.178 nicm 840: server_client_set_key_table(c, NULL);
1.132 nicm 841: server_status_client(c);
842: return;
1.18 nicm 843: }
844:
1.132 nicm 845: /*
846: * No match, but in the root table. Prefix switches to the prefix table
847: * and everything else is passed through.
848: */
1.168 nicm 849: if (key == (key_code)options_get_number(s->options, "prefix") ||
850: key == (key_code)options_get_number(s->options, "prefix2")) {
1.178 nicm 851: server_client_set_key_table(c, "prefix");
1.132 nicm 852: server_status_client(c);
1.161 nicm 853: return;
854: }
855:
856: forward:
857: if (c->flags & CLIENT_READONLY)
858: return;
859: if (wp != NULL)
1.132 nicm 860: window_pane_key(wp, c, s, key, m);
1.18 nicm 861: }
862:
1.2 nicm 863: /* Client functions that need to happen every loop. */
864: void
865: server_client_loop(void)
866: {
867: struct client *c;
868: struct window *w;
869: struct window_pane *wp;
1.197 nicm 870: int focus;
1.2 nicm 871:
1.135 nicm 872: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 873: server_client_check_exit(c);
874: if (c->session != NULL) {
875: server_client_check_redraw(c);
876: server_client_reset_state(c);
877: }
1.2 nicm 878: }
879:
880: /*
881: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 882: * their flags now. Also check pane focus and resize.
1.2 nicm 883: */
1.197 nicm 884: focus = options_get_number(global_options, "focus-events");
1.134 nicm 885: RB_FOREACH(w, windows, &windows) {
1.2 nicm 886: w->flags &= ~WINDOW_REDRAW;
1.91 nicm 887: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 888: if (wp->fd != -1) {
1.197 nicm 889: if (focus)
890: server_client_check_focus(wp);
1.101 nicm 891: server_client_check_resize(wp);
892: }
1.2 nicm 893: wp->flags &= ~PANE_REDRAW;
1.91 nicm 894: }
1.150 nicm 895: check_window_name(w);
1.2 nicm 896: }
1.92 nicm 897: }
898:
1.188 nicm 899: static void
900: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 901: {
1.188 nicm 902: struct window_pane *wp = data;
903: struct winsize ws;
904:
905: evtimer_del(&wp->resize_timer);
1.92 nicm 906:
1.101 nicm 907: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 908: return;
909:
910: memset(&ws, 0, sizeof ws);
911: ws.ws_col = wp->sx;
912: ws.ws_row = wp->sy;
913:
1.100 nicm 914: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 915: fatal("ioctl failed");
916:
917: wp->flags &= ~PANE_RESIZE;
1.188 nicm 918: }
919:
920: /* Check if pane should be resized. */
1.190 nicm 921: static void
1.188 nicm 922: server_client_check_resize(struct window_pane *wp)
923: {
924: struct timeval tv = { .tv_usec = 250000 };
925:
926: if (!(wp->flags & PANE_RESIZE))
927: return;
928:
929: if (!event_initialized(&wp->resize_timer))
930: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
931:
932: /*
933: * The first resize should happen immediately, so if the timer is not
934: * running, do it now.
935: */
936: if (!evtimer_pending(&wp->resize_timer, NULL))
937: server_client_resize_event(-1, 0, wp);
938:
939: /*
940: * If the pane is in the alternate screen, let the timer expire and
941: * resize to give the application a chance to redraw. If not, keep
942: * pushing the timer back.
943: */
944: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
945: return;
946: evtimer_del(&wp->resize_timer);
947: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 948: }
949:
950: /* Check whether pane should be focused. */
1.190 nicm 951: static void
1.91 nicm 952: server_client_check_focus(struct window_pane *wp)
953: {
1.93 nicm 954: struct client *c;
1.102 nicm 955: int push;
956:
957: /* Do we need to push the focus state? */
958: push = wp->flags & PANE_FOCUSPUSH;
959: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 960:
961: /* If we don't care about focus, forget it. */
962: if (!(wp->base.mode & MODE_FOCUSON))
963: return;
964:
965: /* If we're not the active pane in our window, we're not focused. */
966: if (wp->window->active != wp)
967: goto not_focused;
968:
969: /* If we're in a mode, we're not focused. */
970: if (wp->screen != &wp->base)
971: goto not_focused;
972:
973: /*
1.93 nicm 974: * If our window is the current window in any focused clients with an
975: * attached session, we're focused.
1.91 nicm 976: */
1.135 nicm 977: TAILQ_FOREACH(c, &clients, entry) {
978: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 979: continue;
1.93 nicm 980: if (c->session->flags & SESSION_UNATTACHED)
981: continue;
982:
983: if (c->session->curw->window == wp->window)
1.91 nicm 984: goto focused;
985: }
986:
987: not_focused:
1.102 nicm 988: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 989: bufferevent_write(wp->event, "\033[O", 3);
990: wp->flags &= ~PANE_FOCUSED;
991: return;
992:
993: focused:
1.102 nicm 994: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 995: bufferevent_write(wp->event, "\033[I", 3);
996: wp->flags |= PANE_FOCUSED;
1.2 nicm 997: }
998:
1.18 nicm 999: /*
1000: * Update cursor position and mode settings. The scroll region and attributes
1001: * are cleared when idle (waiting for an event) as this is the most likely time
1002: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1003: * compromise between excessive resets and likelihood of an interrupt.
1004: *
1005: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1006: * things that are already in their default state.
1007: */
1.190 nicm 1008: static void
1.18 nicm 1009: server_client_reset_state(struct client *c)
1.1 nicm 1010: {
1.18 nicm 1011: struct window *w = c->session->curw->window;
1012: struct window_pane *wp = w->active;
1013: struct screen *s = wp->screen;
1.163 nicm 1014: struct options *oo = c->session->options;
1.66 nicm 1015: int status, mode, o;
1.60 nicm 1016:
1.186 nicm 1017: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1018: return;
1019:
1.199 nicm 1020: tty_region_off(&c->tty);
1021: tty_margin_off(&c->tty);
1.1 nicm 1022:
1023: status = options_get_number(oo, "status");
1024: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
1025: tty_cursor(&c->tty, 0, 0);
1.66 nicm 1026: else {
1.126 nicm 1027: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 1028: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
1029: }
1.1 nicm 1030:
1.50 nicm 1031: /*
1.131 nicm 1032: * Set mouse mode if requested. To support dragging, always use button
1033: * mode.
1.57 nicm 1034: */
1035: mode = s->mode;
1.131 nicm 1036: if (options_get_number(oo, "mouse"))
1037: mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48 nicm 1038:
1039: /* Set the terminal mode and reset attributes. */
1.59 nicm 1040: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1041: tty_reset(&c->tty);
1.17 nicm 1042: }
1043:
1044: /* Repeat time callback. */
1.190 nicm 1045: static void
1.170 nicm 1046: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1047: {
1048: struct client *c = data;
1049:
1.85 nicm 1050: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1051: server_client_set_key_table(c, NULL);
1.132 nicm 1052: c->flags &= ~CLIENT_REPEAT;
1053: server_status_client(c);
1.85 nicm 1054: }
1.192 nicm 1055: }
1056:
1057: /* Double-click callback. */
1058: static void
1059: server_client_click_timer(__unused int fd, __unused short events, void *data)
1060: {
1061: struct client *c = data;
1062:
1063: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1064: }
1065:
1.36 nicm 1066: /* Check if client should be exited. */
1.190 nicm 1067: static void
1.36 nicm 1068: server_client_check_exit(struct client *c)
1069: {
1070: if (!(c->flags & CLIENT_EXIT))
1071: return;
1072:
1.73 nicm 1073: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1074: return;
1075: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1076: return;
1.73 nicm 1077: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1078: return;
1079:
1.162 nicm 1080: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1081: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1082: }
1083:
1.1 nicm 1084: /* Check for client redraws. */
1.190 nicm 1085: static void
1.1 nicm 1086: server_client_check_redraw(struct client *c)
1087: {
1088: struct session *s = c->session;
1.137 nicm 1089: struct tty *tty = &c->tty;
1.1 nicm 1090: struct window_pane *wp;
1.189 nicm 1091: int flags, masked;
1.1 nicm 1092:
1.86 nicm 1093: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1094: return;
1095:
1.1 nicm 1096: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 1097: if (options_get_number(s->options, "set-titles"))
1.1 nicm 1098: server_client_set_title(c);
1.189 nicm 1099: screen_redraw_update(c); /* will adjust flags */
1.1 nicm 1100: }
1101:
1.137 nicm 1102: flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
1103: tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
1104:
1.1 nicm 1105: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 1106: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 1107: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 1108: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 1109: } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137 nicm 1110: tty_update_mode(tty, tty->mode, NULL);
1.38 nicm 1111: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
1112: screen_redraw_pane(c, wp);
1113: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 1114: } else {
1115: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1116: if (wp->flags & PANE_REDRAW) {
1117: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1118: screen_redraw_pane(c, wp);
1.137 nicm 1119: }
1.1 nicm 1120: }
1121: }
1122:
1.185 nicm 1123: masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
1124: if (masked != 0)
1.137 nicm 1125: tty_update_mode(tty, tty->mode, NULL);
1.185 nicm 1126: if (masked == CLIENT_BORDERS)
1.115 nicm 1127: screen_redraw_screen(c, 0, 0, 1);
1.185 nicm 1128: else if (masked == CLIENT_STATUS)
1.115 nicm 1129: screen_redraw_screen(c, 0, 1, 0);
1.185 nicm 1130: else if (masked != 0)
1131: screen_redraw_screen(c, 0, 1, 1);
1.1 nicm 1132:
1.137 nicm 1133: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1134: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1135:
1.153 nicm 1136: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
1137: CLIENT_STATUSFORCE);
1.1 nicm 1138: }
1139:
1140: /* Set client title. */
1.190 nicm 1141: static void
1.1 nicm 1142: server_client_set_title(struct client *c)
1143: {
1.128 nicm 1144: struct session *s = c->session;
1145: const char *template;
1146: char *title;
1147: struct format_tree *ft;
1.1 nicm 1148:
1.163 nicm 1149: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1150:
1.176 nicm 1151: ft = format_create(NULL, 0);
1.128 nicm 1152: format_defaults(ft, c, NULL, NULL, NULL);
1153:
1154: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 1155: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1156: free(c->title);
1.1 nicm 1157: c->title = xstrdup(title);
1158: tty_set_title(&c->tty, c->title);
1159: }
1.77 nicm 1160: free(title);
1.128 nicm 1161:
1162: format_free(ft);
1.1 nicm 1163: }
1164:
1165: /* Dispatch message from client. */
1.190 nicm 1166: static void
1.162 nicm 1167: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1168: {
1.162 nicm 1169: struct client *c = arg;
1.73 nicm 1170: struct msg_stdin_data stdindata;
1.108 nicm 1171: const char *data;
1.162 nicm 1172: ssize_t datalen;
1.149 nicm 1173: struct session *s;
1.1 nicm 1174:
1.162 nicm 1175: if (c->flags & CLIENT_DEAD)
1176: return;
1177:
1178: if (imsg == NULL) {
1179: server_client_lost(c);
1180: return;
1181: }
1.1 nicm 1182:
1.162 nicm 1183: data = imsg->data;
1184: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1185:
1.162 nicm 1186: switch (imsg->hdr.type) {
1187: case MSG_IDENTIFY_FLAGS:
1188: case MSG_IDENTIFY_TERM:
1189: case MSG_IDENTIFY_TTYNAME:
1190: case MSG_IDENTIFY_CWD:
1191: case MSG_IDENTIFY_STDIN:
1192: case MSG_IDENTIFY_ENVIRON:
1193: case MSG_IDENTIFY_CLIENTPID:
1194: case MSG_IDENTIFY_DONE:
1195: server_client_dispatch_identify(c, imsg);
1196: break;
1197: case MSG_COMMAND:
1198: server_client_dispatch_command(c, imsg);
1199: break;
1200: case MSG_STDIN:
1201: if (datalen != sizeof stdindata)
1202: fatalx("bad MSG_STDIN size");
1203: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1204:
1.162 nicm 1205: if (c->stdin_callback == NULL)
1.33 nicm 1206: break;
1.162 nicm 1207: if (stdindata.size <= 0)
1208: c->stdin_closed = 1;
1209: else {
1210: evbuffer_add(c->stdin_data, stdindata.data,
1211: stdindata.size);
1212: }
1213: c->stdin_callback(c, c->stdin_closed,
1214: c->stdin_callback_data);
1215: break;
1216: case MSG_RESIZE:
1217: if (datalen != 0)
1218: fatalx("bad MSG_RESIZE size");
1.1 nicm 1219:
1.162 nicm 1220: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1221: break;
1.162 nicm 1222: if (tty_resize(&c->tty)) {
1223: recalculate_sizes();
1224: server_redraw_client(c);
1225: }
1.174 nicm 1226: if (c->session != NULL)
1.196 nicm 1227: notify_client("client-resized", c);
1.162 nicm 1228: break;
1229: case MSG_EXITING:
1230: if (datalen != 0)
1231: fatalx("bad MSG_EXITING size");
1.1 nicm 1232:
1.162 nicm 1233: c->session = NULL;
1234: tty_close(&c->tty);
1235: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1236: break;
1237: case MSG_WAKEUP:
1238: case MSG_UNLOCK:
1239: if (datalen != 0)
1240: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1241:
1.162 nicm 1242: if (!(c->flags & CLIENT_SUSPENDED))
1243: break;
1244: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1245:
1.162 nicm 1246: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1247: break;
1.162 nicm 1248: s = c->session;
1.1 nicm 1249:
1.162 nicm 1250: if (gettimeofday(&c->activity_time, NULL) != 0)
1251: fatal("gettimeofday failed");
1252:
1253: tty_start_tty(&c->tty);
1254: server_redraw_client(c);
1255: recalculate_sizes();
1.184 nicm 1256:
1257: if (s != NULL)
1258: session_update_activity(s, &c->activity_time);
1.162 nicm 1259: break;
1260: case MSG_SHELL:
1261: if (datalen != 0)
1262: fatalx("bad MSG_SHELL size");
1.1 nicm 1263:
1.162 nicm 1264: server_client_dispatch_shell(c);
1265: break;
1.1 nicm 1266: }
1267: }
1268:
1.194 nicm 1269: /* Callback when command is done. */
1270: static enum cmd_retval
1.195 nicm 1271: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1272: {
1.195 nicm 1273: struct client *c = item->client;
1.194 nicm 1274:
1275: if (~c->flags & CLIENT_ATTACHED)
1276: c->flags |= CLIENT_EXIT;
1277: return (CMD_RETURN_NORMAL);
1278: }
1279:
1280: /* Show an error message. */
1281: static enum cmd_retval
1.195 nicm 1282: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1283: {
1284: char *error = data;
1285:
1.195 nicm 1286: cmdq_error(item, "%s", error);
1.194 nicm 1287: free(error);
1288:
1289: return (CMD_RETURN_NORMAL);
1290: }
1291:
1.1 nicm 1292: /* Handle command message. */
1.190 nicm 1293: static void
1.162 nicm 1294: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1295: {
1.108 nicm 1296: struct msg_command_data data;
1297: char *buf;
1298: size_t len;
1299: struct cmd_list *cmdlist = NULL;
1300: int argc;
1301: char **argv, *cause;
1302:
1303: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1304: fatalx("bad MSG_COMMAND size");
1305: memcpy(&data, imsg->data, sizeof data);
1306:
1.124 nicm 1307: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1308: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1309: if (len > 0 && buf[len - 1] != '\0')
1310: fatalx("bad MSG_COMMAND string");
1311:
1312: argc = data.argc;
1313: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1314: cause = xstrdup("command too long");
1.1 nicm 1315: goto error;
1316: }
1317:
1318: if (argc == 0) {
1319: argc = 1;
1320: argv = xcalloc(1, sizeof *argv);
1321: *argv = xstrdup("new-session");
1322: }
1323:
1.94 nicm 1324: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1325: cmd_free_argv(argc, argv);
1326: goto error;
1327: }
1328: cmd_free_argv(argc, argv);
1329:
1.194 nicm 1330: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1331: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1332: cmd_list_free(cmdlist);
1333: return;
1334:
1335: error:
1.194 nicm 1336: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1337:
1.1 nicm 1338: if (cmdlist != NULL)
1339: cmd_list_free(cmdlist);
1.88 nicm 1340:
1.36 nicm 1341: c->flags |= CLIENT_EXIT;
1.1 nicm 1342: }
1343:
1344: /* Handle identify message. */
1.190 nicm 1345: static void
1.162 nicm 1346: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1347: {
1.165 nicm 1348: const char *data, *home;
1.109 nicm 1349: size_t datalen;
1350: int flags;
1351:
1352: if (c->flags & CLIENT_IDENTIFIED)
1353: fatalx("out-of-order identify message");
1354:
1355: data = imsg->data;
1356: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1357:
1358: switch (imsg->hdr.type) {
1359: case MSG_IDENTIFY_FLAGS:
1360: if (datalen != sizeof flags)
1361: fatalx("bad MSG_IDENTIFY_FLAGS size");
1362: memcpy(&flags, data, sizeof flags);
1363: c->flags |= flags;
1.158 nicm 1364: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1365: break;
1366: case MSG_IDENTIFY_TERM:
1.110 nicm 1367: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1368: fatalx("bad MSG_IDENTIFY_TERM string");
1369: c->term = xstrdup(data);
1.158 nicm 1370: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1371: break;
1372: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1373: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1374: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1375: c->ttyname = xstrdup(data);
1.158 nicm 1376: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1377: break;
1378: case MSG_IDENTIFY_CWD:
1.155 nicm 1379: if (datalen == 0 || data[datalen - 1] != '\0')
1380: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1381: if (access(data, X_OK) == 0)
1382: c->cwd = xstrdup(data);
1383: else if ((home = find_home()) != NULL)
1384: c->cwd = xstrdup(home);
1385: else
1386: c->cwd = xstrdup("/");
1.158 nicm 1387: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1388: break;
1389: case MSG_IDENTIFY_STDIN:
1390: if (datalen != 0)
1391: fatalx("bad MSG_IDENTIFY_STDIN size");
1392: c->fd = imsg->fd;
1.158 nicm 1393: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1394: break;
1395: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1396: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1397: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1398: if (strchr(data, '=') != NULL)
1.164 nicm 1399: environ_put(c->environ, data);
1.158 nicm 1400: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1401: break;
1402: case MSG_IDENTIFY_CLIENTPID:
1403: if (datalen != sizeof c->pid)
1404: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1405: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1406: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1407: break;
1408: default:
1409: break;
1410: }
1411:
1412: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1413: return;
1414: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1415:
1.109 nicm 1416: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1417: c->stdin_callback = control_callback;
1.109 nicm 1418:
1.97 nicm 1419: evbuffer_free(c->stderr_data);
1420: c->stderr_data = c->stdout_data;
1.109 nicm 1421:
1422: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1423: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1424: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1425:
1426: c->tty.fd = -1;
1427:
1.109 nicm 1428: close(c->fd);
1429: c->fd = -1;
1430:
1.75 nicm 1431: return;
1432: }
1.1 nicm 1433:
1.109 nicm 1434: if (c->fd == -1)
1.104 nicm 1435: return;
1.145 nicm 1436: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1437: close(c->fd);
1438: c->fd = -1;
1.80 nicm 1439: return;
1440: }
1.109 nicm 1441: if (c->flags & CLIENT_UTF8)
1.1 nicm 1442: c->tty.flags |= TTY_UTF8;
1.109 nicm 1443: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1444: c->tty.term_flags |= TERM_256COLOURS;
1445:
1446: tty_resize(&c->tty);
1447:
1.109 nicm 1448: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1449: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1450: }
1451:
1452: /* Handle shell message. */
1.190 nicm 1453: static void
1.162 nicm 1454: server_client_dispatch_shell(struct client *c)
1.1 nicm 1455: {
1.107 nicm 1456: const char *shell;
1.25 nicm 1457:
1.163 nicm 1458: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1459: if (*shell == '\0' || areshell(shell))
1460: shell = _PATH_BSHELL;
1.162 nicm 1461: proc_send_s(c->peer, MSG_SHELL, shell);
1.25 nicm 1462:
1.162 nicm 1463: proc_kill_peer(c->peer);
1.169 nicm 1464: }
1465:
1466: /* Event callback to push more stdout data if any left. */
1467: static void
1.170 nicm 1468: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1469: {
1470: struct client *c = arg;
1471:
1472: if (~c->flags & CLIENT_DEAD)
1473: server_client_push_stdout(c);
1474: server_client_unref(c);
1475: }
1476:
1477: /* Push stdout to client if possible. */
1478: void
1479: server_client_push_stdout(struct client *c)
1480: {
1481: struct msg_stdout_data data;
1482: size_t sent, left;
1483:
1484: left = EVBUFFER_LENGTH(c->stdout_data);
1485: while (left != 0) {
1486: sent = left;
1487: if (sent > sizeof data.data)
1488: sent = sizeof data.data;
1489: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1490: data.size = sent;
1491:
1492: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1493: break;
1494: evbuffer_drain(c->stdout_data, sent);
1495:
1496: left = EVBUFFER_LENGTH(c->stdout_data);
1497: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1498: sent, left);
1499: }
1500: if (left != 0) {
1501: c->references++;
1502: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1503: log_debug("%s: client %p, queued", __func__, c);
1504: }
1505: }
1506:
1507: /* Event callback to push more stderr data if any left. */
1508: static void
1.170 nicm 1509: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1510: {
1511: struct client *c = arg;
1512:
1513: if (~c->flags & CLIENT_DEAD)
1514: server_client_push_stderr(c);
1515: server_client_unref(c);
1516: }
1517:
1518: /* Push stderr to client if possible. */
1519: void
1520: server_client_push_stderr(struct client *c)
1521: {
1522: struct msg_stderr_data data;
1523: size_t sent, left;
1524:
1525: if (c->stderr_data == c->stdout_data) {
1526: server_client_push_stdout(c);
1527: return;
1528: }
1529:
1530: left = EVBUFFER_LENGTH(c->stderr_data);
1531: while (left != 0) {
1532: sent = left;
1533: if (sent > sizeof data.data)
1534: sent = sizeof data.data;
1535: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1536: data.size = sent;
1537:
1538: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1539: break;
1540: evbuffer_drain(c->stderr_data, sent);
1541:
1542: left = EVBUFFER_LENGTH(c->stderr_data);
1543: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1544: sent, left);
1545: }
1546: if (left != 0) {
1547: c->references++;
1548: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1549: log_debug("%s: client %p, queued", __func__, c);
1550: }
1.1 nicm 1551: }