Annotation of src/usr.bin/tmux/server-client.c, Revision 1.204
1.204 ! nicm 1: /* $OpenBSD: server-client.c,v 1.203 2016/11/24 14:38:55 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:
1.204 ! nicm 477: if (c->tty.mouse_drag_update != NULL)
! 478: key = KEYC_DRAGGING;
! 479: else {
1.131 nicm 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.18 nicm 701:
702: /* Update the activity timer. */
703: if (gettimeofday(&c->activity_time, NULL) != 0)
704: fatal("gettimeofday failed");
1.149 nicm 705: session_update_activity(s, &c->activity_time);
1.18 nicm 706:
1.132 nicm 707: /* Number keys jump to pane in identify mode. */
1.25 nicm 708: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 709: if (c->flags & CLIENT_READONLY)
710: return;
1.95 nicm 711: window_unzoom(w);
1.18 nicm 712: wp = window_pane_at_index(w, key - '0');
1.187 nicm 713: if (wp != NULL && !window_pane_visible(wp))
714: wp = NULL;
715: server_clear_identify(c, wp);
1.18 nicm 716: return;
717: }
718:
719: /* Handle status line. */
1.30 nicm 720: if (!(c->flags & CLIENT_READONLY)) {
721: status_message_clear(c);
1.187 nicm 722: server_clear_identify(c, NULL);
1.30 nicm 723: }
1.18 nicm 724: if (c->prompt_string != NULL) {
1.193 nicm 725: if (c->flags & CLIENT_READONLY)
726: return;
727: if (status_prompt_key(c, key) == 0)
728: return;
1.18 nicm 729: }
730:
731: /* Check for mouse keys. */
1.204 ! nicm 732: m->valid = 0;
1.18 nicm 733: if (key == KEYC_MOUSE) {
1.30 nicm 734: if (c->flags & CLIENT_READONLY)
735: return;
1.131 nicm 736: key = server_client_check_mouse(c);
1.177 nicm 737: if (key == KEYC_UNKNOWN)
1.131 nicm 738: return;
739:
740: m->valid = 1;
741: m->key = key;
1.203 nicm 742:
743: /*
1.204 ! nicm 744: * Mouse drag is in progress, so fire the callback (now that
! 745: * the mouse event is valid).
1.203 nicm 746: */
1.204 ! nicm 747: if (key == KEYC_DRAGGING) {
! 748: c->tty.mouse_drag_update(c, m);
1.203 nicm 749: return;
1.204 ! nicm 750: }
1.131 nicm 751: } else
752: m->valid = 0;
1.18 nicm 753:
1.202 nicm 754: /* Find affected pane. */
755: if (KEYC_IS_MOUSE(key) && m->valid)
756: wp = cmd_mouse_pane(m, NULL, NULL);
757: else
758: wp = w->active;
759:
760: /* Forward mouse keys if disabled. */
761: if (key == KEYC_MOUSE && !options_get_number(s->options, "mouse"))
762: goto forward;
763:
1.132 nicm 764: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 765: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
766: goto forward;
1.18 nicm 767:
1.132 nicm 768: retry:
1.191 nicm 769: /*
770: * Work out the current key table. If the pane is in a mode, use
771: * the mode table instead of the default key table.
772: */
773: name = NULL;
774: if (wp != NULL && wp->mode != NULL && wp->mode->key_table != NULL)
775: name = wp->mode->key_table(wp);
776: if (name == NULL || !server_client_is_default_key_table(c))
777: table = c->keytable;
778: else
779: table = key_bindings_get_table(name, 1);
1.202 nicm 780: if (wp == NULL)
781: log_debug("key table %s (no pane)", table->name);
782: else
783: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.191 nicm 784:
1.132 nicm 785: /* Try to see if there is a key binding in the current table. */
786: bd_find.key = key;
1.191 nicm 787: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132 nicm 788: if (bd != NULL) {
789: /*
790: * Key was matched in this table. If currently repeating but a
791: * non-repeating binding was found, stop repeating and try
792: * again in the root table.
793: */
794: if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
1.178 nicm 795: server_client_set_key_table(c, NULL);
1.132 nicm 796: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 797: server_status_client(c);
1.132 nicm 798: goto retry;
1.18 nicm 799: }
1.82 nicm 800:
1.132 nicm 801: /*
802: * Take a reference to this table to make sure the key binding
803: * doesn't disappear.
804: */
805: table->references++;
806:
807: /*
808: * If this is a repeating key, start the timer. Otherwise reset
809: * the client back to the root table.
810: */
1.163 nicm 811: xtimeout = options_get_number(s->options, "repeat-time");
1.132 nicm 812: if (xtimeout != 0 && bd->can_repeat) {
813: c->flags |= CLIENT_REPEAT;
814:
815: tv.tv_sec = xtimeout / 1000;
816: tv.tv_usec = (xtimeout % 1000) * 1000L;
817: evtimer_del(&c->repeat_timer);
818: evtimer_add(&c->repeat_timer, &tv);
819: } else {
1.18 nicm 820: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 821: server_client_set_key_table(c, NULL);
1.18 nicm 822: }
1.132 nicm 823: server_status_client(c);
824:
1.201 nicm 825: /* Find default state if the pane is known. */
826: cmd_find_clear_state(&fs, NULL, 0);
827: if (wp != NULL) {
828: fs.s = s;
829: fs.wl = fs.s->curw;
830: fs.w = fs.wl->window;
831: fs.wp = wp;
832: cmd_find_log_state(__func__, &fs);
833:
834: if (!cmd_find_valid_state(&fs))
835: fatalx("invalid key state");
836: }
837:
1.132 nicm 838: /* Dispatch the key binding. */
1.201 nicm 839: key_bindings_dispatch(bd, c, m, &fs);
1.132 nicm 840: key_bindings_unref_table(table);
1.18 nicm 841: return;
842: }
843:
1.132 nicm 844: /*
845: * No match in this table. If repeating, switch the client back to the
846: * root table and try again.
847: */
848: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 849: server_client_set_key_table(c, NULL);
1.18 nicm 850: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 851: server_status_client(c);
852: goto retry;
1.18 nicm 853: }
854:
1.132 nicm 855: /* If no match and we're not in the root table, that's it. */
1.191 nicm 856: if (name == NULL && !server_client_is_default_key_table(c)) {
1.178 nicm 857: server_client_set_key_table(c, NULL);
1.132 nicm 858: server_status_client(c);
859: return;
1.18 nicm 860: }
861:
1.132 nicm 862: /*
863: * No match, but in the root table. Prefix switches to the prefix table
864: * and everything else is passed through.
865: */
1.168 nicm 866: if (key == (key_code)options_get_number(s->options, "prefix") ||
867: key == (key_code)options_get_number(s->options, "prefix2")) {
1.178 nicm 868: server_client_set_key_table(c, "prefix");
1.132 nicm 869: server_status_client(c);
1.161 nicm 870: return;
871: }
872:
873: forward:
874: if (c->flags & CLIENT_READONLY)
875: return;
876: if (wp != NULL)
1.132 nicm 877: window_pane_key(wp, c, s, key, m);
1.18 nicm 878: }
879:
1.2 nicm 880: /* Client functions that need to happen every loop. */
881: void
882: server_client_loop(void)
883: {
884: struct client *c;
885: struct window *w;
886: struct window_pane *wp;
1.197 nicm 887: int focus;
1.2 nicm 888:
1.135 nicm 889: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 890: server_client_check_exit(c);
891: if (c->session != NULL) {
892: server_client_check_redraw(c);
893: server_client_reset_state(c);
894: }
1.2 nicm 895: }
896:
897: /*
898: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 899: * their flags now. Also check pane focus and resize.
1.2 nicm 900: */
1.197 nicm 901: focus = options_get_number(global_options, "focus-events");
1.134 nicm 902: RB_FOREACH(w, windows, &windows) {
1.2 nicm 903: w->flags &= ~WINDOW_REDRAW;
1.91 nicm 904: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 905: if (wp->fd != -1) {
1.197 nicm 906: if (focus)
907: server_client_check_focus(wp);
1.101 nicm 908: server_client_check_resize(wp);
909: }
1.2 nicm 910: wp->flags &= ~PANE_REDRAW;
1.91 nicm 911: }
1.150 nicm 912: check_window_name(w);
1.2 nicm 913: }
1.92 nicm 914: }
915:
1.188 nicm 916: static void
917: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 918: {
1.188 nicm 919: struct window_pane *wp = data;
920: struct winsize ws;
921:
922: evtimer_del(&wp->resize_timer);
1.92 nicm 923:
1.101 nicm 924: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 925: return;
926:
927: memset(&ws, 0, sizeof ws);
928: ws.ws_col = wp->sx;
929: ws.ws_row = wp->sy;
930:
1.100 nicm 931: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 932: fatal("ioctl failed");
933:
934: wp->flags &= ~PANE_RESIZE;
1.188 nicm 935: }
936:
937: /* Check if pane should be resized. */
1.190 nicm 938: static void
1.188 nicm 939: server_client_check_resize(struct window_pane *wp)
940: {
941: struct timeval tv = { .tv_usec = 250000 };
942:
943: if (!(wp->flags & PANE_RESIZE))
944: return;
945:
946: if (!event_initialized(&wp->resize_timer))
947: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
948:
949: /*
950: * The first resize should happen immediately, so if the timer is not
951: * running, do it now.
952: */
953: if (!evtimer_pending(&wp->resize_timer, NULL))
954: server_client_resize_event(-1, 0, wp);
955:
956: /*
957: * If the pane is in the alternate screen, let the timer expire and
958: * resize to give the application a chance to redraw. If not, keep
959: * pushing the timer back.
960: */
961: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
962: return;
963: evtimer_del(&wp->resize_timer);
964: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 965: }
966:
967: /* Check whether pane should be focused. */
1.190 nicm 968: static void
1.91 nicm 969: server_client_check_focus(struct window_pane *wp)
970: {
1.93 nicm 971: struct client *c;
1.102 nicm 972: int push;
973:
974: /* Do we need to push the focus state? */
975: push = wp->flags & PANE_FOCUSPUSH;
976: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 977:
978: /* If we don't care about focus, forget it. */
979: if (!(wp->base.mode & MODE_FOCUSON))
980: return;
981:
982: /* If we're not the active pane in our window, we're not focused. */
983: if (wp->window->active != wp)
984: goto not_focused;
985:
986: /* If we're in a mode, we're not focused. */
987: if (wp->screen != &wp->base)
988: goto not_focused;
989:
990: /*
1.93 nicm 991: * If our window is the current window in any focused clients with an
992: * attached session, we're focused.
1.91 nicm 993: */
1.135 nicm 994: TAILQ_FOREACH(c, &clients, entry) {
995: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 996: continue;
1.93 nicm 997: if (c->session->flags & SESSION_UNATTACHED)
998: continue;
999:
1000: if (c->session->curw->window == wp->window)
1.91 nicm 1001: goto focused;
1002: }
1003:
1004: not_focused:
1.102 nicm 1005: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 1006: bufferevent_write(wp->event, "\033[O", 3);
1007: wp->flags &= ~PANE_FOCUSED;
1008: return;
1009:
1010: focused:
1.102 nicm 1011: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 1012: bufferevent_write(wp->event, "\033[I", 3);
1013: wp->flags |= PANE_FOCUSED;
1.2 nicm 1014: }
1015:
1.18 nicm 1016: /*
1017: * Update cursor position and mode settings. The scroll region and attributes
1018: * are cleared when idle (waiting for an event) as this is the most likely time
1019: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1020: * compromise between excessive resets and likelihood of an interrupt.
1021: *
1022: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1023: * things that are already in their default state.
1024: */
1.190 nicm 1025: static void
1.18 nicm 1026: server_client_reset_state(struct client *c)
1.1 nicm 1027: {
1.18 nicm 1028: struct window *w = c->session->curw->window;
1029: struct window_pane *wp = w->active;
1030: struct screen *s = wp->screen;
1.163 nicm 1031: struct options *oo = c->session->options;
1.66 nicm 1032: int status, mode, o;
1.60 nicm 1033:
1.186 nicm 1034: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1035: return;
1036:
1.199 nicm 1037: tty_region_off(&c->tty);
1038: tty_margin_off(&c->tty);
1.1 nicm 1039:
1040: status = options_get_number(oo, "status");
1041: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
1042: tty_cursor(&c->tty, 0, 0);
1.66 nicm 1043: else {
1.126 nicm 1044: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 1045: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
1046: }
1.1 nicm 1047:
1.50 nicm 1048: /*
1.131 nicm 1049: * Set mouse mode if requested. To support dragging, always use button
1050: * mode.
1.57 nicm 1051: */
1052: mode = s->mode;
1.131 nicm 1053: if (options_get_number(oo, "mouse"))
1054: mode = (mode & ~ALL_MOUSE_MODES) | MODE_MOUSE_BUTTON;
1.48 nicm 1055:
1056: /* Set the terminal mode and reset attributes. */
1.59 nicm 1057: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1058: tty_reset(&c->tty);
1.17 nicm 1059: }
1060:
1061: /* Repeat time callback. */
1.190 nicm 1062: static void
1.170 nicm 1063: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1064: {
1065: struct client *c = data;
1066:
1.85 nicm 1067: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1068: server_client_set_key_table(c, NULL);
1.132 nicm 1069: c->flags &= ~CLIENT_REPEAT;
1070: server_status_client(c);
1.85 nicm 1071: }
1.192 nicm 1072: }
1073:
1074: /* Double-click callback. */
1075: static void
1076: server_client_click_timer(__unused int fd, __unused short events, void *data)
1077: {
1078: struct client *c = data;
1079:
1080: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1081: }
1082:
1.36 nicm 1083: /* Check if client should be exited. */
1.190 nicm 1084: static void
1.36 nicm 1085: server_client_check_exit(struct client *c)
1086: {
1087: if (!(c->flags & CLIENT_EXIT))
1088: return;
1089:
1.73 nicm 1090: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1091: return;
1092: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1093: return;
1.73 nicm 1094: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1095: return;
1096:
1.162 nicm 1097: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1098: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1099: }
1100:
1.1 nicm 1101: /* Check for client redraws. */
1.190 nicm 1102: static void
1.1 nicm 1103: server_client_check_redraw(struct client *c)
1104: {
1105: struct session *s = c->session;
1.137 nicm 1106: struct tty *tty = &c->tty;
1.1 nicm 1107: struct window_pane *wp;
1.189 nicm 1108: int flags, masked;
1.1 nicm 1109:
1.86 nicm 1110: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1111: return;
1112:
1.1 nicm 1113: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 1114: if (options_get_number(s->options, "set-titles"))
1.1 nicm 1115: server_client_set_title(c);
1.189 nicm 1116: screen_redraw_update(c); /* will adjust flags */
1.1 nicm 1117: }
1118:
1.137 nicm 1119: flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
1120: tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
1121:
1.1 nicm 1122: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 1123: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 1124: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 1125: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 1126: } else if (c->flags & CLIENT_REDRAWWINDOW) {
1.137 nicm 1127: tty_update_mode(tty, tty->mode, NULL);
1.38 nicm 1128: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
1129: screen_redraw_pane(c, wp);
1130: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 1131: } else {
1132: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1133: if (wp->flags & PANE_REDRAW) {
1134: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1135: screen_redraw_pane(c, wp);
1.137 nicm 1136: }
1.1 nicm 1137: }
1138: }
1139:
1.185 nicm 1140: masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
1141: if (masked != 0)
1.137 nicm 1142: tty_update_mode(tty, tty->mode, NULL);
1.185 nicm 1143: if (masked == CLIENT_BORDERS)
1.115 nicm 1144: screen_redraw_screen(c, 0, 0, 1);
1.185 nicm 1145: else if (masked == CLIENT_STATUS)
1.115 nicm 1146: screen_redraw_screen(c, 0, 1, 0);
1.185 nicm 1147: else if (masked != 0)
1148: screen_redraw_screen(c, 0, 1, 1);
1.1 nicm 1149:
1.137 nicm 1150: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1151: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1152:
1.153 nicm 1153: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
1154: CLIENT_STATUSFORCE);
1.1 nicm 1155: }
1156:
1157: /* Set client title. */
1.190 nicm 1158: static void
1.1 nicm 1159: server_client_set_title(struct client *c)
1160: {
1.128 nicm 1161: struct session *s = c->session;
1162: const char *template;
1163: char *title;
1164: struct format_tree *ft;
1.1 nicm 1165:
1.163 nicm 1166: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1167:
1.176 nicm 1168: ft = format_create(NULL, 0);
1.128 nicm 1169: format_defaults(ft, c, NULL, NULL, NULL);
1170:
1171: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 1172: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1173: free(c->title);
1.1 nicm 1174: c->title = xstrdup(title);
1175: tty_set_title(&c->tty, c->title);
1176: }
1.77 nicm 1177: free(title);
1.128 nicm 1178:
1179: format_free(ft);
1.1 nicm 1180: }
1181:
1182: /* Dispatch message from client. */
1.190 nicm 1183: static void
1.162 nicm 1184: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1185: {
1.162 nicm 1186: struct client *c = arg;
1.73 nicm 1187: struct msg_stdin_data stdindata;
1.108 nicm 1188: const char *data;
1.162 nicm 1189: ssize_t datalen;
1.149 nicm 1190: struct session *s;
1.1 nicm 1191:
1.162 nicm 1192: if (c->flags & CLIENT_DEAD)
1193: return;
1194:
1195: if (imsg == NULL) {
1196: server_client_lost(c);
1197: return;
1198: }
1.1 nicm 1199:
1.162 nicm 1200: data = imsg->data;
1201: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1202:
1.162 nicm 1203: switch (imsg->hdr.type) {
1204: case MSG_IDENTIFY_FLAGS:
1205: case MSG_IDENTIFY_TERM:
1206: case MSG_IDENTIFY_TTYNAME:
1207: case MSG_IDENTIFY_CWD:
1208: case MSG_IDENTIFY_STDIN:
1209: case MSG_IDENTIFY_ENVIRON:
1210: case MSG_IDENTIFY_CLIENTPID:
1211: case MSG_IDENTIFY_DONE:
1212: server_client_dispatch_identify(c, imsg);
1213: break;
1214: case MSG_COMMAND:
1215: server_client_dispatch_command(c, imsg);
1216: break;
1217: case MSG_STDIN:
1218: if (datalen != sizeof stdindata)
1219: fatalx("bad MSG_STDIN size");
1220: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1221:
1.162 nicm 1222: if (c->stdin_callback == NULL)
1.33 nicm 1223: break;
1.162 nicm 1224: if (stdindata.size <= 0)
1225: c->stdin_closed = 1;
1226: else {
1227: evbuffer_add(c->stdin_data, stdindata.data,
1228: stdindata.size);
1229: }
1230: c->stdin_callback(c, c->stdin_closed,
1231: c->stdin_callback_data);
1232: break;
1233: case MSG_RESIZE:
1234: if (datalen != 0)
1235: fatalx("bad MSG_RESIZE size");
1.1 nicm 1236:
1.162 nicm 1237: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1238: break;
1.162 nicm 1239: if (tty_resize(&c->tty)) {
1240: recalculate_sizes();
1241: server_redraw_client(c);
1242: }
1.174 nicm 1243: if (c->session != NULL)
1.196 nicm 1244: notify_client("client-resized", c);
1.162 nicm 1245: break;
1246: case MSG_EXITING:
1247: if (datalen != 0)
1248: fatalx("bad MSG_EXITING size");
1.1 nicm 1249:
1.162 nicm 1250: c->session = NULL;
1251: tty_close(&c->tty);
1252: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1253: break;
1254: case MSG_WAKEUP:
1255: case MSG_UNLOCK:
1256: if (datalen != 0)
1257: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1258:
1.162 nicm 1259: if (!(c->flags & CLIENT_SUSPENDED))
1260: break;
1261: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1262:
1.162 nicm 1263: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1264: break;
1.162 nicm 1265: s = c->session;
1.1 nicm 1266:
1.162 nicm 1267: if (gettimeofday(&c->activity_time, NULL) != 0)
1268: fatal("gettimeofday failed");
1269:
1270: tty_start_tty(&c->tty);
1271: server_redraw_client(c);
1272: recalculate_sizes();
1.184 nicm 1273:
1274: if (s != NULL)
1275: session_update_activity(s, &c->activity_time);
1.162 nicm 1276: break;
1277: case MSG_SHELL:
1278: if (datalen != 0)
1279: fatalx("bad MSG_SHELL size");
1.1 nicm 1280:
1.162 nicm 1281: server_client_dispatch_shell(c);
1282: break;
1.1 nicm 1283: }
1284: }
1285:
1.194 nicm 1286: /* Callback when command is done. */
1287: static enum cmd_retval
1.195 nicm 1288: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1289: {
1.195 nicm 1290: struct client *c = item->client;
1.194 nicm 1291:
1292: if (~c->flags & CLIENT_ATTACHED)
1293: c->flags |= CLIENT_EXIT;
1294: return (CMD_RETURN_NORMAL);
1295: }
1296:
1297: /* Show an error message. */
1298: static enum cmd_retval
1.195 nicm 1299: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1300: {
1301: char *error = data;
1302:
1.195 nicm 1303: cmdq_error(item, "%s", error);
1.194 nicm 1304: free(error);
1305:
1306: return (CMD_RETURN_NORMAL);
1307: }
1308:
1.1 nicm 1309: /* Handle command message. */
1.190 nicm 1310: static void
1.162 nicm 1311: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1312: {
1.108 nicm 1313: struct msg_command_data data;
1314: char *buf;
1315: size_t len;
1316: struct cmd_list *cmdlist = NULL;
1317: int argc;
1318: char **argv, *cause;
1319:
1320: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1321: fatalx("bad MSG_COMMAND size");
1322: memcpy(&data, imsg->data, sizeof data);
1323:
1.124 nicm 1324: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1325: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1326: if (len > 0 && buf[len - 1] != '\0')
1327: fatalx("bad MSG_COMMAND string");
1328:
1329: argc = data.argc;
1330: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1331: cause = xstrdup("command too long");
1.1 nicm 1332: goto error;
1333: }
1334:
1335: if (argc == 0) {
1336: argc = 1;
1337: argv = xcalloc(1, sizeof *argv);
1338: *argv = xstrdup("new-session");
1339: }
1340:
1.94 nicm 1341: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1342: cmd_free_argv(argc, argv);
1343: goto error;
1344: }
1345: cmd_free_argv(argc, argv);
1346:
1.194 nicm 1347: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1348: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1349: cmd_list_free(cmdlist);
1350: return;
1351:
1352: error:
1.194 nicm 1353: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1354:
1.1 nicm 1355: if (cmdlist != NULL)
1356: cmd_list_free(cmdlist);
1.88 nicm 1357:
1.36 nicm 1358: c->flags |= CLIENT_EXIT;
1.1 nicm 1359: }
1360:
1361: /* Handle identify message. */
1.190 nicm 1362: static void
1.162 nicm 1363: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1364: {
1.165 nicm 1365: const char *data, *home;
1.109 nicm 1366: size_t datalen;
1367: int flags;
1368:
1369: if (c->flags & CLIENT_IDENTIFIED)
1370: fatalx("out-of-order identify message");
1371:
1372: data = imsg->data;
1373: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1374:
1375: switch (imsg->hdr.type) {
1376: case MSG_IDENTIFY_FLAGS:
1377: if (datalen != sizeof flags)
1378: fatalx("bad MSG_IDENTIFY_FLAGS size");
1379: memcpy(&flags, data, sizeof flags);
1380: c->flags |= flags;
1.158 nicm 1381: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1382: break;
1383: case MSG_IDENTIFY_TERM:
1.110 nicm 1384: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1385: fatalx("bad MSG_IDENTIFY_TERM string");
1386: c->term = xstrdup(data);
1.158 nicm 1387: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1388: break;
1389: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1390: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1391: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1392: c->ttyname = xstrdup(data);
1.158 nicm 1393: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1394: break;
1395: case MSG_IDENTIFY_CWD:
1.155 nicm 1396: if (datalen == 0 || data[datalen - 1] != '\0')
1397: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1398: if (access(data, X_OK) == 0)
1399: c->cwd = xstrdup(data);
1400: else if ((home = find_home()) != NULL)
1401: c->cwd = xstrdup(home);
1402: else
1403: c->cwd = xstrdup("/");
1.158 nicm 1404: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1405: break;
1406: case MSG_IDENTIFY_STDIN:
1407: if (datalen != 0)
1408: fatalx("bad MSG_IDENTIFY_STDIN size");
1409: c->fd = imsg->fd;
1.158 nicm 1410: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1411: break;
1412: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1413: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1414: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1415: if (strchr(data, '=') != NULL)
1.164 nicm 1416: environ_put(c->environ, data);
1.158 nicm 1417: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1418: break;
1419: case MSG_IDENTIFY_CLIENTPID:
1420: if (datalen != sizeof c->pid)
1421: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1422: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1423: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1424: break;
1425: default:
1426: break;
1427: }
1428:
1429: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1430: return;
1431: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1432:
1.109 nicm 1433: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1434: c->stdin_callback = control_callback;
1.109 nicm 1435:
1.97 nicm 1436: evbuffer_free(c->stderr_data);
1437: c->stderr_data = c->stdout_data;
1.109 nicm 1438:
1439: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1440: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1441: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1442:
1443: c->tty.fd = -1;
1444:
1.109 nicm 1445: close(c->fd);
1446: c->fd = -1;
1447:
1.75 nicm 1448: return;
1449: }
1.1 nicm 1450:
1.109 nicm 1451: if (c->fd == -1)
1.104 nicm 1452: return;
1.145 nicm 1453: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1454: close(c->fd);
1455: c->fd = -1;
1.80 nicm 1456: return;
1457: }
1.109 nicm 1458: if (c->flags & CLIENT_UTF8)
1.1 nicm 1459: c->tty.flags |= TTY_UTF8;
1.109 nicm 1460: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1461: c->tty.term_flags |= TERM_256COLOURS;
1462:
1463: tty_resize(&c->tty);
1464:
1.109 nicm 1465: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1466: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1467: }
1468:
1469: /* Handle shell message. */
1.190 nicm 1470: static void
1.162 nicm 1471: server_client_dispatch_shell(struct client *c)
1.1 nicm 1472: {
1.107 nicm 1473: const char *shell;
1.25 nicm 1474:
1.163 nicm 1475: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1476: if (*shell == '\0' || areshell(shell))
1477: shell = _PATH_BSHELL;
1.162 nicm 1478: proc_send_s(c->peer, MSG_SHELL, shell);
1.25 nicm 1479:
1.162 nicm 1480: proc_kill_peer(c->peer);
1.169 nicm 1481: }
1482:
1483: /* Event callback to push more stdout data if any left. */
1484: static void
1.170 nicm 1485: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1486: {
1487: struct client *c = arg;
1488:
1489: if (~c->flags & CLIENT_DEAD)
1490: server_client_push_stdout(c);
1491: server_client_unref(c);
1492: }
1493:
1494: /* Push stdout to client if possible. */
1495: void
1496: server_client_push_stdout(struct client *c)
1497: {
1498: struct msg_stdout_data data;
1499: size_t sent, left;
1500:
1501: left = EVBUFFER_LENGTH(c->stdout_data);
1502: while (left != 0) {
1503: sent = left;
1504: if (sent > sizeof data.data)
1505: sent = sizeof data.data;
1506: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1507: data.size = sent;
1508:
1509: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1510: break;
1511: evbuffer_drain(c->stdout_data, sent);
1512:
1513: left = EVBUFFER_LENGTH(c->stdout_data);
1514: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1515: sent, left);
1516: }
1517: if (left != 0) {
1518: c->references++;
1519: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1520: log_debug("%s: client %p, queued", __func__, c);
1521: }
1522: }
1523:
1524: /* Event callback to push more stderr data if any left. */
1525: static void
1.170 nicm 1526: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1527: {
1528: struct client *c = arg;
1529:
1530: if (~c->flags & CLIENT_DEAD)
1531: server_client_push_stderr(c);
1532: server_client_unref(c);
1533: }
1534:
1535: /* Push stderr to client if possible. */
1536: void
1537: server_client_push_stderr(struct client *c)
1538: {
1539: struct msg_stderr_data data;
1540: size_t sent, left;
1541:
1542: if (c->stderr_data == c->stdout_data) {
1543: server_client_push_stdout(c);
1544: return;
1545: }
1546:
1547: left = EVBUFFER_LENGTH(c->stderr_data);
1548: while (left != 0) {
1549: sent = left;
1550: if (sent > sizeof data.data)
1551: sent = sizeof data.data;
1552: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1553: data.size = sent;
1554:
1555: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1556: break;
1557: evbuffer_drain(c->stderr_data, sent);
1558:
1559: left = EVBUFFER_LENGTH(c->stderr_data);
1560: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1561: sent, left);
1562: }
1563: if (left != 0) {
1564: c->references++;
1565: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1566: log_debug("%s: client %p, queued", __func__, c);
1567: }
1.1 nicm 1568: }