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