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