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