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