Annotation of src/usr.bin/tmux/server-client.c, Revision 1.295
1.295 ! nicm 1: /* $OpenBSD: server-client.c,v 1.294 2019/08/28 07:34:32 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:
546: wl = winlink_find_by_index(&s->windows, sr->argument);
547: if (wl == NULL)
548: return (KEYC_UNKNOWN);
549: m->w = wl->window->id;
1.273 nicm 550:
1.274 nicm 551: where = STATUS;
552: break;
553: }
1.258 nicm 554: }
555: }
1.131 nicm 556:
557: /* Not on status line. Adjust position and check for border or pane. */
558: if (where == NOWHERE) {
1.261 nicm 559: px = x;
1.292 nicm 560: if (m->statusat == 0 && y >= m->statuslines)
561: py = y - m->statuslines;
1.131 nicm 562: else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261 nicm 563: py = m->statusat - 1;
564: else
565: py = y;
566:
567: tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
568: log_debug("mouse window @%u at %u,%u (%ux%u)",
569: s->curw->window->id, m->ox, m->oy, sx, sy);
570: if (px > sx || py > sy)
571: return (KEYC_UNKNOWN);
572: px = px + m->ox;
573: py = py + m->oy;
1.131 nicm 574:
1.260 nicm 575: /* Try the pane borders if not zoomed. */
576: if (~s->curw->window->flags & WINDOW_ZOOMED) {
577: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261 nicm 578: if ((wp->xoff + wp->sx == px &&
579: wp->yoff <= 1 + py &&
580: wp->yoff + wp->sy >= py) ||
581: (wp->yoff + wp->sy == py &&
582: wp->xoff <= 1 + px &&
583: wp->xoff + wp->sx >= px))
1.260 nicm 584: break;
585: }
586: if (wp != NULL)
587: where = BORDER;
1.131 nicm 588: }
1.260 nicm 589:
590: /* Otherwise try inside the pane. */
591: if (where == NOWHERE) {
1.261 nicm 592: wp = window_get_active_at(s->curw->window, px, py);
1.260 nicm 593: if (wp != NULL)
1.131 nicm 594: where = PANE;
595: }
1.260 nicm 596:
1.131 nicm 597: if (where == NOWHERE)
1.177 nicm 598: return (KEYC_UNKNOWN);
1.260 nicm 599: if (where == PANE)
600: log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
601: else if (where == BORDER)
602: log_debug("mouse on pane %%%u border", wp->id);
1.131 nicm 603: m->wp = wp->id;
604: m->w = wp->window->id;
605: } else
606: m->wp = -1;
607:
608: /* Stop dragging if needed. */
1.200 nicm 609: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 610: if (c->tty.mouse_drag_release != NULL)
611: c->tty.mouse_drag_release(c, m);
612:
613: c->tty.mouse_drag_update = NULL;
614: c->tty.mouse_drag_release = NULL;
615:
1.182 nicm 616: /*
1.183 nicm 617: * End a mouse drag by passing a MouseDragEnd key corresponding
618: * to the button that started the drag.
1.182 nicm 619: */
620: switch (c->tty.mouse_drag_flag) {
621: case 1:
622: if (where == PANE)
1.183 nicm 623: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 624: if (where == STATUS)
1.183 nicm 625: key = KEYC_MOUSEDRAGEND1_STATUS;
1.258 nicm 626: if (where == STATUS_LEFT)
627: key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
628: if (where == STATUS_RIGHT)
629: key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274 nicm 630: if (where == STATUS_DEFAULT)
631: key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182 nicm 632: if (where == BORDER)
1.183 nicm 633: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 634: break;
635: case 2:
636: if (where == PANE)
1.183 nicm 637: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 638: if (where == STATUS)
1.183 nicm 639: key = KEYC_MOUSEDRAGEND2_STATUS;
1.258 nicm 640: if (where == STATUS_LEFT)
641: key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
642: if (where == STATUS_RIGHT)
643: key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274 nicm 644: if (where == STATUS_DEFAULT)
645: key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182 nicm 646: if (where == BORDER)
1.183 nicm 647: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 648: break;
649: case 3:
650: if (where == PANE)
1.183 nicm 651: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 652: if (where == STATUS)
1.183 nicm 653: key = KEYC_MOUSEDRAGEND3_STATUS;
1.258 nicm 654: if (where == STATUS_LEFT)
655: key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
656: if (where == STATUS_RIGHT)
657: key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274 nicm 658: if (where == STATUS_DEFAULT)
659: key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182 nicm 660: if (where == BORDER)
1.183 nicm 661: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 662: break;
663: default:
664: key = KEYC_MOUSE;
665: break;
666: }
1.131 nicm 667: c->tty.mouse_drag_flag = 0;
1.182 nicm 668:
669: return (key);
1.131 nicm 670: }
671:
672: /* Convert to a key binding. */
1.177 nicm 673: key = KEYC_UNKNOWN;
1.131 nicm 674: switch (type) {
675: case NOTYPE:
676: break;
1.209 nicm 677: case MOVE:
678: if (where == PANE)
679: key = KEYC_MOUSEMOVE_PANE;
680: if (where == STATUS)
681: key = KEYC_MOUSEMOVE_STATUS;
1.274 nicm 682: if (where == STATUS_LEFT)
683: key = KEYC_MOUSEMOVE_STATUS_LEFT;
684: if (where == STATUS_RIGHT)
685: key = KEYC_MOUSEMOVE_STATUS_RIGHT;
686: if (where == STATUS_DEFAULT)
687: key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209 nicm 688: if (where == BORDER)
689: key = KEYC_MOUSEMOVE_BORDER;
690: break;
1.131 nicm 691: case DRAG:
1.204 nicm 692: if (c->tty.mouse_drag_update != NULL)
693: key = KEYC_DRAGGING;
694: else {
1.131 nicm 695: switch (MOUSE_BUTTONS(b)) {
696: case 0:
697: if (where == PANE)
698: key = KEYC_MOUSEDRAG1_PANE;
699: if (where == STATUS)
700: key = KEYC_MOUSEDRAG1_STATUS;
1.258 nicm 701: if (where == STATUS_LEFT)
702: key = KEYC_MOUSEDRAG1_STATUS_LEFT;
703: if (where == STATUS_RIGHT)
704: key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274 nicm 705: if (where == STATUS_DEFAULT)
706: key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131 nicm 707: if (where == BORDER)
708: key = KEYC_MOUSEDRAG1_BORDER;
709: break;
710: case 1:
711: if (where == PANE)
712: key = KEYC_MOUSEDRAG2_PANE;
713: if (where == STATUS)
714: key = KEYC_MOUSEDRAG2_STATUS;
1.258 nicm 715: if (where == STATUS_LEFT)
716: key = KEYC_MOUSEDRAG2_STATUS_LEFT;
717: if (where == STATUS_RIGHT)
718: key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274 nicm 719: if (where == STATUS_DEFAULT)
720: key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131 nicm 721: if (where == BORDER)
722: key = KEYC_MOUSEDRAG2_BORDER;
723: break;
724: case 2:
725: if (where == PANE)
726: key = KEYC_MOUSEDRAG3_PANE;
727: if (where == STATUS)
728: key = KEYC_MOUSEDRAG3_STATUS;
1.258 nicm 729: if (where == STATUS_LEFT)
730: key = KEYC_MOUSEDRAG3_STATUS_LEFT;
731: if (where == STATUS_RIGHT)
732: key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274 nicm 733: if (where == STATUS_DEFAULT)
734: key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131 nicm 735: if (where == BORDER)
736: key = KEYC_MOUSEDRAG3_BORDER;
737: break;
738: }
739: }
1.66 nicm 740:
1.182 nicm 741: /*
742: * Begin a drag by setting the flag to a non-zero value that
743: * corresponds to the mouse button in use.
744: */
745: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 746: break;
747: case WHEEL:
748: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
749: if (where == PANE)
750: key = KEYC_WHEELUP_PANE;
751: if (where == STATUS)
752: key = KEYC_WHEELUP_STATUS;
1.258 nicm 753: if (where == STATUS_LEFT)
754: key = KEYC_WHEELUP_STATUS_LEFT;
755: if (where == STATUS_RIGHT)
756: key = KEYC_WHEELUP_STATUS_RIGHT;
1.274 nicm 757: if (where == STATUS_DEFAULT)
758: key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131 nicm 759: if (where == BORDER)
760: key = KEYC_WHEELUP_BORDER;
761: } else {
762: if (where == PANE)
763: key = KEYC_WHEELDOWN_PANE;
764: if (where == STATUS)
765: key = KEYC_WHEELDOWN_STATUS;
1.274 nicm 766: if (where == STATUS_LEFT)
767: key = KEYC_WHEELDOWN_STATUS_LEFT;
768: if (where == STATUS_RIGHT)
769: key = KEYC_WHEELDOWN_STATUS_RIGHT;
770: if (where == STATUS_DEFAULT)
771: key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131 nicm 772: if (where == BORDER)
773: key = KEYC_WHEELDOWN_BORDER;
774: }
775: break;
776: case UP:
777: switch (MOUSE_BUTTONS(b)) {
778: case 0:
779: if (where == PANE)
780: key = KEYC_MOUSEUP1_PANE;
781: if (where == STATUS)
782: key = KEYC_MOUSEUP1_STATUS;
1.258 nicm 783: if (where == STATUS_LEFT)
784: key = KEYC_MOUSEUP1_STATUS_LEFT;
785: if (where == STATUS_RIGHT)
786: key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274 nicm 787: if (where == STATUS_DEFAULT)
788: key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131 nicm 789: if (where == BORDER)
790: key = KEYC_MOUSEUP1_BORDER;
791: break;
792: case 1:
793: if (where == PANE)
794: key = KEYC_MOUSEUP2_PANE;
795: if (where == STATUS)
796: key = KEYC_MOUSEUP2_STATUS;
1.258 nicm 797: if (where == STATUS_LEFT)
798: key = KEYC_MOUSEUP2_STATUS_LEFT;
799: if (where == STATUS_RIGHT)
800: key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274 nicm 801: if (where == STATUS_DEFAULT)
802: key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131 nicm 803: if (where == BORDER)
804: key = KEYC_MOUSEUP2_BORDER;
805: break;
806: case 2:
807: if (where == PANE)
808: key = KEYC_MOUSEUP3_PANE;
809: if (where == STATUS)
810: key = KEYC_MOUSEUP3_STATUS;
1.258 nicm 811: if (where == STATUS_LEFT)
812: key = KEYC_MOUSEUP3_STATUS_LEFT;
813: if (where == STATUS_RIGHT)
814: key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274 nicm 815: if (where == STATUS_DEFAULT)
816: key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131 nicm 817: if (where == BORDER)
818: key = KEYC_MOUSEUP3_BORDER;
819: break;
820: }
821: break;
822: case DOWN:
823: switch (MOUSE_BUTTONS(b)) {
824: case 0:
825: if (where == PANE)
826: key = KEYC_MOUSEDOWN1_PANE;
827: if (where == STATUS)
828: key = KEYC_MOUSEDOWN1_STATUS;
1.258 nicm 829: if (where == STATUS_LEFT)
830: key = KEYC_MOUSEDOWN1_STATUS_LEFT;
831: if (where == STATUS_RIGHT)
832: key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274 nicm 833: if (where == STATUS_DEFAULT)
834: key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131 nicm 835: if (where == BORDER)
836: key = KEYC_MOUSEDOWN1_BORDER;
837: break;
838: case 1:
839: if (where == PANE)
840: key = KEYC_MOUSEDOWN2_PANE;
841: if (where == STATUS)
842: key = KEYC_MOUSEDOWN2_STATUS;
1.258 nicm 843: if (where == STATUS_LEFT)
844: key = KEYC_MOUSEDOWN2_STATUS_LEFT;
845: if (where == STATUS_RIGHT)
846: key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274 nicm 847: if (where == STATUS_DEFAULT)
848: key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131 nicm 849: if (where == BORDER)
850: key = KEYC_MOUSEDOWN2_BORDER;
851: break;
852: case 2:
853: if (where == PANE)
854: key = KEYC_MOUSEDOWN3_PANE;
855: if (where == STATUS)
856: key = KEYC_MOUSEDOWN3_STATUS;
1.258 nicm 857: if (where == STATUS_LEFT)
858: key = KEYC_MOUSEDOWN3_STATUS_LEFT;
859: if (where == STATUS_RIGHT)
860: key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274 nicm 861: if (where == STATUS_DEFAULT)
862: key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131 nicm 863: if (where == BORDER)
864: key = KEYC_MOUSEDOWN3_BORDER;
865: break;
1.66 nicm 866: }
1.131 nicm 867: break;
1.192 nicm 868: case DOUBLE:
869: switch (MOUSE_BUTTONS(b)) {
870: case 0:
871: if (where == PANE)
872: key = KEYC_DOUBLECLICK1_PANE;
873: if (where == STATUS)
874: key = KEYC_DOUBLECLICK1_STATUS;
1.258 nicm 875: if (where == STATUS_LEFT)
876: key = KEYC_DOUBLECLICK1_STATUS_LEFT;
877: if (where == STATUS_RIGHT)
878: key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274 nicm 879: if (where == STATUS_DEFAULT)
880: key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192 nicm 881: if (where == BORDER)
882: key = KEYC_DOUBLECLICK1_BORDER;
883: break;
884: case 1:
885: if (where == PANE)
886: key = KEYC_DOUBLECLICK2_PANE;
887: if (where == STATUS)
888: key = KEYC_DOUBLECLICK2_STATUS;
1.258 nicm 889: if (where == STATUS_LEFT)
890: key = KEYC_DOUBLECLICK2_STATUS_LEFT;
891: if (where == STATUS_RIGHT)
892: key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274 nicm 893: if (where == STATUS_DEFAULT)
894: key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192 nicm 895: if (where == BORDER)
896: key = KEYC_DOUBLECLICK2_BORDER;
897: break;
898: case 2:
899: if (where == PANE)
900: key = KEYC_DOUBLECLICK3_PANE;
901: if (where == STATUS)
902: key = KEYC_DOUBLECLICK3_STATUS;
1.258 nicm 903: if (where == STATUS_LEFT)
904: key = KEYC_DOUBLECLICK3_STATUS_LEFT;
905: if (where == STATUS_RIGHT)
906: key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274 nicm 907: if (where == STATUS_DEFAULT)
908: key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192 nicm 909: if (where == BORDER)
910: key = KEYC_DOUBLECLICK3_BORDER;
911: break;
912: }
913: break;
914: case TRIPLE:
915: switch (MOUSE_BUTTONS(b)) {
916: case 0:
917: if (where == PANE)
918: key = KEYC_TRIPLECLICK1_PANE;
919: if (where == STATUS)
920: key = KEYC_TRIPLECLICK1_STATUS;
1.258 nicm 921: if (where == STATUS_LEFT)
922: key = KEYC_TRIPLECLICK1_STATUS_LEFT;
923: if (where == STATUS_RIGHT)
924: key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274 nicm 925: if (where == STATUS_DEFAULT)
926: key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192 nicm 927: if (where == BORDER)
928: key = KEYC_TRIPLECLICK1_BORDER;
929: break;
930: case 1:
931: if (where == PANE)
932: key = KEYC_TRIPLECLICK2_PANE;
933: if (where == STATUS)
934: key = KEYC_TRIPLECLICK2_STATUS;
1.258 nicm 935: if (where == STATUS_LEFT)
936: key = KEYC_TRIPLECLICK2_STATUS_LEFT;
937: if (where == STATUS_RIGHT)
938: key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274 nicm 939: if (where == STATUS_DEFAULT)
940: key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192 nicm 941: if (where == BORDER)
942: key = KEYC_TRIPLECLICK2_BORDER;
943: break;
944: case 2:
945: if (where == PANE)
946: key = KEYC_TRIPLECLICK3_PANE;
947: if (where == STATUS)
948: key = KEYC_TRIPLECLICK3_STATUS;
1.258 nicm 949: if (where == STATUS_LEFT)
950: key = KEYC_TRIPLECLICK3_STATUS_LEFT;
951: if (where == STATUS_RIGHT)
952: key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274 nicm 953: if (where == STATUS_DEFAULT)
954: key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192 nicm 955: if (where == BORDER)
956: key = KEYC_TRIPLECLICK3_BORDER;
957: break;
958: }
959: break;
1.66 nicm 960: }
1.177 nicm 961: if (key == KEYC_UNKNOWN)
962: return (KEYC_UNKNOWN);
1.66 nicm 963:
1.131 nicm 964: /* Apply modifiers if any. */
965: if (b & MOUSE_MASK_META)
966: key |= KEYC_ESCAPE;
967: if (b & MOUSE_MASK_CTRL)
968: key |= KEYC_CTRL;
969: if (b & MOUSE_MASK_SHIFT)
970: key |= KEYC_SHIFT;
1.66 nicm 971:
1.131 nicm 972: return (key);
1.66 nicm 973: }
974:
1.82 nicm 975: /* Is this fast enough to probably be a paste? */
1.190 nicm 976: static int
1.82 nicm 977: server_client_assume_paste(struct session *s)
978: {
979: struct timeval tv;
1.84 nicm 980: int t;
1.82 nicm 981:
1.163 nicm 982: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 983: return (0);
1.82 nicm 984:
985: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 986: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
987: log_debug("session %s pasting (flag %d)", s->name,
988: !!(s->flags & SESSION_PASTING));
989: if (s->flags & SESSION_PASTING)
990: return (1);
991: s->flags |= SESSION_PASTING;
992: return (0);
993: }
994: log_debug("session %s not pasting", s->name);
995: s->flags &= ~SESSION_PASTING;
1.83 nicm 996: return (0);
1.82 nicm 997: }
998:
1.295 ! nicm 999: /* Has the latest client changed? */
! 1000: static void
! 1001: server_client_update_latest(struct client *c)
! 1002: {
! 1003: struct window *w;
! 1004:
! 1005: if (c->session == NULL)
! 1006: return;
! 1007: w = c->session->curw->window;
! 1008:
! 1009: if (w->latest == c)
! 1010: return;
! 1011: w->latest = c;
! 1012:
! 1013: if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
! 1014: recalculate_size(w);
! 1015: }
! 1016:
1.276 nicm 1017: /*
1018: * Handle data key input from client. This owns and can modify the key event it
1019: * is given and is responsible for freeing it.
1020: */
1.280 nicm 1021: static enum cmd_retval
1.276 nicm 1022: server_client_key_callback(struct cmdq_item *item, void *data)
1.18 nicm 1023: {
1.276 nicm 1024: struct client *c = item->client;
1025: struct key_event *event = data;
1026: key_code key = event->key;
1027: struct mouse_event *m = &event->m;
1.267 nicm 1028: struct session *s = c->session;
1029: struct winlink *wl;
1030: struct window_pane *wp;
1031: struct window_mode_entry *wme;
1032: struct timeval tv;
1033: struct key_table *table, *first;
1034: struct key_binding *bd;
1035: int xtimeout, flags;
1036: struct cmd_find_state fs;
1037: key_code key0;
1.18 nicm 1038:
1039: /* Check the client is good to accept input. */
1.132 nicm 1040: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.276 nicm 1041: goto out;
1.264 nicm 1042: wl = s->curw;
1.18 nicm 1043:
1044: /* Update the activity timer. */
1045: if (gettimeofday(&c->activity_time, NULL) != 0)
1046: fatal("gettimeofday failed");
1.149 nicm 1047: session_update_activity(s, &c->activity_time);
1.18 nicm 1048:
1049: /* Check for mouse keys. */
1.204 nicm 1050: m->valid = 0;
1.18 nicm 1051: if (key == KEYC_MOUSE) {
1.30 nicm 1052: if (c->flags & CLIENT_READONLY)
1.276 nicm 1053: goto out;
1054: key = server_client_check_mouse(c, event);
1.177 nicm 1055: if (key == KEYC_UNKNOWN)
1.276 nicm 1056: goto out;
1.131 nicm 1057:
1058: m->valid = 1;
1059: m->key = key;
1.203 nicm 1060:
1061: /*
1.204 nicm 1062: * Mouse drag is in progress, so fire the callback (now that
1063: * the mouse event is valid).
1.203 nicm 1064: */
1.204 nicm 1065: if (key == KEYC_DRAGGING) {
1066: c->tty.mouse_drag_update(c, m);
1.276 nicm 1067: goto out;
1.204 nicm 1068: }
1.276 nicm 1069: }
1.18 nicm 1070:
1.202 nicm 1071: /* Find affected pane. */
1.243 nicm 1072: if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1073: cmd_find_from_session(&fs, s, 0);
1.227 nicm 1074: wp = fs.wp;
1.202 nicm 1075:
1076: /* Forward mouse keys if disabled. */
1.206 nicm 1077: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253 nicm 1078: goto forward_key;
1.202 nicm 1079:
1.132 nicm 1080: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 1081: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253 nicm 1082: goto forward_key;
1.18 nicm 1083:
1.191 nicm 1084: /*
1085: * Work out the current key table. If the pane is in a mode, use
1086: * the mode table instead of the default key table.
1087: */
1.223 nicm 1088: if (server_client_is_default_key_table(c, c->keytable) &&
1089: wp != NULL &&
1.267 nicm 1090: (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1091: wme->mode->key_table != NULL)
1092: table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223 nicm 1093: else
1.191 nicm 1094: table = c->keytable;
1.223 nicm 1095: first = table;
1.191 nicm 1096:
1.253 nicm 1097: table_changed:
1.205 nicm 1098: /*
1099: * The prefix always takes precedence and forces a switch to the prefix
1100: * table, unless we are already there.
1101: */
1.252 nicm 1102: key0 = (key & ~KEYC_XTERM);
1.239 nicm 1103: if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
1104: key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205 nicm 1105: strcmp(table->name, "prefix") != 0) {
1106: server_client_set_key_table(c, "prefix");
1107: server_status_client(c);
1.276 nicm 1108: goto out;
1.205 nicm 1109: }
1.238 nicm 1110: flags = c->flags;
1.205 nicm 1111:
1.262 nicm 1112: try_again:
1.223 nicm 1113: /* Log key table. */
1114: if (wp == NULL)
1115: log_debug("key table %s (no pane)", table->name);
1116: else
1117: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225 nicm 1118: if (c->flags & CLIENT_REPEAT)
1119: log_debug("currently repeating");
1.223 nicm 1120:
1.132 nicm 1121: /* Try to see if there is a key binding in the current table. */
1.254 nicm 1122: bd = key_bindings_get(table, key0);
1.132 nicm 1123: if (bd != NULL) {
1124: /*
1125: * Key was matched in this table. If currently repeating but a
1126: * non-repeating binding was found, stop repeating and try
1127: * again in the root table.
1128: */
1.222 nicm 1129: if ((c->flags & CLIENT_REPEAT) &&
1130: (~bd->flags & KEY_BINDING_REPEAT)) {
1.262 nicm 1131: log_debug("found in key table %s (not repeating)",
1132: table->name);
1.178 nicm 1133: server_client_set_key_table(c, NULL);
1.262 nicm 1134: first = table = c->keytable;
1.132 nicm 1135: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 1136: server_status_client(c);
1.253 nicm 1137: goto table_changed;
1.18 nicm 1138: }
1.223 nicm 1139: log_debug("found in key table %s", table->name);
1.82 nicm 1140:
1.132 nicm 1141: /*
1142: * Take a reference to this table to make sure the key binding
1143: * doesn't disappear.
1144: */
1145: table->references++;
1146:
1147: /*
1148: * If this is a repeating key, start the timer. Otherwise reset
1149: * the client back to the root table.
1150: */
1.163 nicm 1151: xtimeout = options_get_number(s->options, "repeat-time");
1.222 nicm 1152: if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132 nicm 1153: c->flags |= CLIENT_REPEAT;
1154:
1155: tv.tv_sec = xtimeout / 1000;
1156: tv.tv_usec = (xtimeout % 1000) * 1000L;
1157: evtimer_del(&c->repeat_timer);
1158: evtimer_add(&c->repeat_timer, &tv);
1159: } else {
1.18 nicm 1160: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 1161: server_client_set_key_table(c, NULL);
1.18 nicm 1162: }
1.132 nicm 1163: server_status_client(c);
1164:
1.227 nicm 1165: /* Execute the key binding. */
1.276 nicm 1166: key_bindings_dispatch(bd, item, c, m, &fs);
1.132 nicm 1167: key_bindings_unref_table(table);
1.276 nicm 1168: goto out;
1.18 nicm 1169: }
1170:
1.132 nicm 1171: /*
1.253 nicm 1172: * No match, try the ANY key.
1173: */
1174: if (key0 != KEYC_ANY) {
1175: key0 = KEYC_ANY;
1176: goto try_again;
1177: }
1178:
1179: /*
1.223 nicm 1180: * No match in this table. If not in the root table or if repeating,
1181: * switch the client back to the root table and try again.
1.132 nicm 1182: */
1.223 nicm 1183: log_debug("not found in key table %s", table->name);
1184: if (!server_client_is_default_key_table(c, table) ||
1185: (c->flags & CLIENT_REPEAT)) {
1.262 nicm 1186: log_debug("trying in root table");
1.178 nicm 1187: server_client_set_key_table(c, NULL);
1.262 nicm 1188: table = c->keytable;
1189: if (c->flags & CLIENT_REPEAT)
1190: first = table;
1.18 nicm 1191: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 1192: server_status_client(c);
1.253 nicm 1193: goto table_changed;
1.18 nicm 1194: }
1195:
1.223 nicm 1196: /*
1197: * No match in the root table either. If this wasn't the first table
1198: * tried, don't pass the key to the pane.
1199: */
1.238 nicm 1200: if (first != table && (~flags & CLIENT_REPEAT)) {
1.178 nicm 1201: server_client_set_key_table(c, NULL);
1.132 nicm 1202: server_status_client(c);
1.276 nicm 1203: goto out;
1.161 nicm 1204: }
1205:
1.253 nicm 1206: forward_key:
1.161 nicm 1207: if (c->flags & CLIENT_READONLY)
1.276 nicm 1208: goto out;
1.161 nicm 1209: if (wp != NULL)
1.264 nicm 1210: window_pane_key(wp, c, s, wl, key, m);
1.276 nicm 1211:
1212: out:
1.295 ! nicm 1213: if (s != NULL)
! 1214: server_client_update_latest(c);
1.276 nicm 1215: free(event);
1216: return (CMD_RETURN_NORMAL);
1.280 nicm 1217: }
1218:
1219: /* Handle a key event. */
1220: int
1221: server_client_handle_key(struct client *c, struct key_event *event)
1222: {
1223: struct session *s = c->session;
1224: struct cmdq_item *item;
1225:
1226: /* Check the client is good to accept input. */
1227: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1228: return (0);
1229:
1230: /*
1.291 nicm 1231: * Key presses in overlay mode and the command prompt are a special
1232: * case. The queue might be blocked so they need to be processed
1233: * immediately rather than queued.
1.280 nicm 1234: */
1.291 nicm 1235: if (~c->flags & CLIENT_READONLY) {
1236: status_message_clear(c);
1237: if (c->prompt_string != NULL) {
1238: if (status_prompt_key(c, event->key) == 0)
1239: return (0);
1240: }
1241: if (c->overlay_key != NULL) {
1242: switch (c->overlay_key(c, event)) {
1243: case 0:
1244: return (0);
1245: case 1:
1246: server_client_clear_overlay(c);
1247: return (0);
1248: }
1.290 nicm 1249: }
1.293 nicm 1250: server_client_clear_overlay(c);
1.280 nicm 1251: }
1252:
1253: /*
1254: * Add the key to the queue so it happens after any commands queued by
1255: * previous keys.
1256: */
1257: item = cmdq_get_callback(server_client_key_callback, event);
1258: cmdq_append(c, item);
1259: return (1);
1.18 nicm 1260: }
1261:
1.2 nicm 1262: /* Client functions that need to happen every loop. */
1263: void
1264: server_client_loop(void)
1265: {
1266: struct client *c;
1267: struct window *w;
1268: struct window_pane *wp;
1.287 nicm 1269: struct winlink *wl;
1270: struct session *s;
1.197 nicm 1271: int focus;
1.2 nicm 1272:
1.135 nicm 1273: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1274: server_client_check_exit(c);
1275: if (c->session != NULL) {
1276: server_client_check_redraw(c);
1277: server_client_reset_state(c);
1278: }
1.2 nicm 1279: }
1280:
1281: /*
1282: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 1283: * their flags now. Also check pane focus and resize.
1.2 nicm 1284: */
1.197 nicm 1285: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1286: RB_FOREACH(w, windows, &windows) {
1.287 nicm 1287: TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1288: s = wl->session;
1289: if (s->attached != 0 && s->curw == wl)
1290: break;
1291: }
1.91 nicm 1292: TAILQ_FOREACH(wp, &w->panes, entry) {
1.289 nicm 1293: if (wp->fd != -1) {
1.197 nicm 1294: if (focus)
1295: server_client_check_focus(wp);
1.289 nicm 1296: if (wl != NULL)
1297: server_client_check_resize(wp);
1.101 nicm 1298: }
1.2 nicm 1299: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1300: }
1.150 nicm 1301: check_window_name(w);
1.2 nicm 1302: }
1.92 nicm 1303: }
1304:
1.237 nicm 1305: /* Check if we need to force a resize. */
1306: static int
1307: server_client_resize_force(struct window_pane *wp)
1308: {
1309: struct timeval tv = { .tv_usec = 100000 };
1310: struct winsize ws;
1311:
1312: /*
1313: * If we are resizing to the same size as when we entered the loop
1314: * (that is, to the same size the application currently thinks it is),
1315: * tmux may have gone through several resizes internally and thrown
1316: * away parts of the screen. So we need the application to actually
1317: * redraw even though its final size has not changed.
1318: */
1319:
1320: if (wp->flags & PANE_RESIZEFORCE) {
1321: wp->flags &= ~PANE_RESIZEFORCE;
1322: return (0);
1323: }
1324:
1325: if (wp->sx != wp->osx ||
1326: wp->sy != wp->osy ||
1327: wp->sx <= 1 ||
1328: wp->sy <= 1)
1329: return (0);
1330:
1331: memset(&ws, 0, sizeof ws);
1332: ws.ws_col = wp->sx;
1333: ws.ws_row = wp->sy - 1;
1.244 nicm 1334: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.237 nicm 1335: fatal("ioctl failed");
1336: log_debug("%s: %%%u forcing resize", __func__, wp->id);
1337:
1338: evtimer_add(&wp->resize_timer, &tv);
1339: wp->flags |= PANE_RESIZEFORCE;
1340: return (1);
1341: }
1342:
1.294 nicm 1343: /* Resize a pane. */
1.188 nicm 1344: static void
1.294 nicm 1345: server_client_resize_pane(struct window_pane *wp)
1.92 nicm 1346: {
1.294 nicm 1347: struct winsize ws;
1.235 nicm 1348:
1.92 nicm 1349: memset(&ws, 0, sizeof ws);
1350: ws.ws_col = wp->sx;
1351: ws.ws_row = wp->sy;
1.244 nicm 1352: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 1353: fatal("ioctl failed");
1.235 nicm 1354: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.92 nicm 1355:
1356: wp->flags &= ~PANE_RESIZE;
1.235 nicm 1357:
1358: wp->osx = wp->sx;
1359: wp->osy = wp->sy;
1.188 nicm 1360: }
1361:
1.294 nicm 1362: /* Start the resize timer. */
1363: static void
1364: server_client_start_resize_timer(struct window_pane *wp)
1365: {
1366: struct timeval tv = { .tv_usec = 250000 };
1367:
1368: if (!evtimer_pending(&wp->resize_timer, NULL))
1369: evtimer_add(&wp->resize_timer, &tv);
1370: }
1371:
1372: /* Resize timer event. */
1373: static void
1374: server_client_resize_event(__unused int fd, __unused short events, void *data)
1375: {
1376: struct window_pane *wp = data;
1377:
1378: evtimer_del(&wp->resize_timer);
1379:
1380: if (~wp->flags & PANE_RESIZE)
1381: return;
1382: log_debug("%s: %%%u timer fired (was%s resized)", __func__, wp->id,
1383: (wp->flags & PANE_RESIZED) ? "" : " not");
1384:
1385: if (wp->saved_grid == NULL && (wp->flags & PANE_RESIZED)) {
1386: log_debug("%s: %%%u deferring timer", __func__, wp->id);
1387: server_client_start_resize_timer(wp);
1388: } else if (!server_client_resize_force(wp)) {
1389: log_debug("%s: %%%u resizing pane", __func__, wp->id);
1390: server_client_resize_pane(wp);
1391: }
1392: wp->flags &= ~PANE_RESIZED;
1393: }
1394:
1.188 nicm 1395: /* Check if pane should be resized. */
1.190 nicm 1396: static void
1.188 nicm 1397: server_client_check_resize(struct window_pane *wp)
1398: {
1.294 nicm 1399: if (~wp->flags & PANE_RESIZE)
1.188 nicm 1400: return;
1401:
1402: if (!event_initialized(&wp->resize_timer))
1403: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1404:
1.294 nicm 1405: if (!evtimer_pending(&wp->resize_timer, NULL)) {
1406: log_debug("%s: %%%u starting timer", __func__, wp->id);
1407: server_client_resize_pane(wp);
1408: server_client_start_resize_timer(wp);
1409: } else
1410: log_debug("%s: %%%u timer running", __func__, wp->id);
1.91 nicm 1411: }
1412:
1413: /* Check whether pane should be focused. */
1.190 nicm 1414: static void
1.91 nicm 1415: server_client_check_focus(struct window_pane *wp)
1416: {
1.93 nicm 1417: struct client *c;
1.102 nicm 1418: int push;
1419:
1420: /* Do we need to push the focus state? */
1421: push = wp->flags & PANE_FOCUSPUSH;
1422: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1423:
1424: /* If we're not the active pane in our window, we're not focused. */
1425: if (wp->window->active != wp)
1426: goto not_focused;
1427:
1428: /* If we're in a mode, we're not focused. */
1429: if (wp->screen != &wp->base)
1430: goto not_focused;
1431:
1432: /*
1.93 nicm 1433: * If our window is the current window in any focused clients with an
1434: * attached session, we're focused.
1.91 nicm 1435: */
1.135 nicm 1436: TAILQ_FOREACH(c, &clients, entry) {
1437: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1438: continue;
1.255 nicm 1439: if (c->session->attached == 0)
1.93 nicm 1440: continue;
1441:
1442: if (c->session->curw->window == wp->window)
1.91 nicm 1443: goto focused;
1444: }
1445:
1446: not_focused:
1.251 nicm 1447: if (push || (wp->flags & PANE_FOCUSED)) {
1448: if (wp->base.mode & MODE_FOCUSON)
1449: bufferevent_write(wp->event, "\033[O", 3);
1450: notify_pane("pane-focus-out", wp);
1451: }
1.91 nicm 1452: wp->flags &= ~PANE_FOCUSED;
1453: return;
1454:
1455: focused:
1.251 nicm 1456: if (push || !(wp->flags & PANE_FOCUSED)) {
1457: if (wp->base.mode & MODE_FOCUSON)
1458: bufferevent_write(wp->event, "\033[I", 3);
1459: notify_pane("pane-focus-in", wp);
1.275 nicm 1460: session_update_activity(c->session, NULL);
1.251 nicm 1461: }
1.91 nicm 1462: wp->flags |= PANE_FOCUSED;
1.2 nicm 1463: }
1464:
1.18 nicm 1465: /*
1466: * Update cursor position and mode settings. The scroll region and attributes
1467: * are cleared when idle (waiting for an event) as this is the most likely time
1468: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1469: * compromise between excessive resets and likelihood of an interrupt.
1470: *
1471: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1472: * things that are already in their default state.
1473: */
1.190 nicm 1474: static void
1.18 nicm 1475: server_client_reset_state(struct client *c)
1.1 nicm 1476: {
1.18 nicm 1477: struct window *w = c->session->curw->window;
1.209 nicm 1478: struct window_pane *wp = w->active, *loop;
1.18 nicm 1479: struct screen *s = wp->screen;
1.163 nicm 1480: struct options *oo = c->session->options;
1.261 nicm 1481: int mode, cursor = 0;
1482: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1483:
1.186 nicm 1484: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1485: return;
1.282 nicm 1486: if (c->overlay_draw != NULL)
1487: return;
1.261 nicm 1488: mode = s->mode;
1.86 nicm 1489:
1.199 nicm 1490: tty_region_off(&c->tty);
1491: tty_margin_off(&c->tty);
1.1 nicm 1492:
1.261 nicm 1493: /* Move cursor to pane cursor and offset. */
1494: cursor = 0;
1495: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1496: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1497: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1498: cursor = 1;
1499:
1500: cx = wp->xoff + s->cx - ox;
1501: cy = wp->yoff + s->cy - oy;
1502:
1503: if (status_at_line(c) == 0)
1504: cy += status_line_size(c);
1505: }
1506: if (!cursor)
1507: mode &= ~MODE_CURSOR;
1508: tty_cursor(&c->tty, cx, cy);
1.1 nicm 1509:
1.50 nicm 1510: /*
1.131 nicm 1511: * Set mouse mode if requested. To support dragging, always use button
1512: * mode.
1.57 nicm 1513: */
1.209 nicm 1514: if (options_get_number(oo, "mouse")) {
1515: mode &= ~ALL_MOUSE_MODES;
1516: TAILQ_FOREACH(loop, &w->panes, entry) {
1517: if (loop->screen->mode & MODE_MOUSE_ALL)
1518: mode |= MODE_MOUSE_ALL;
1519: }
1520: if (~mode & MODE_MOUSE_ALL)
1521: mode |= MODE_MOUSE_BUTTON;
1522: }
1.215 nicm 1523:
1524: /* Clear bracketed paste mode if at the prompt. */
1525: if (c->prompt_string != NULL)
1526: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1527:
1528: /* Set the terminal mode and reset attributes. */
1.59 nicm 1529: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1530: tty_reset(&c->tty);
1.17 nicm 1531: }
1532:
1533: /* Repeat time callback. */
1.190 nicm 1534: static void
1.170 nicm 1535: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1536: {
1537: struct client *c = data;
1538:
1.85 nicm 1539: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1540: server_client_set_key_table(c, NULL);
1.132 nicm 1541: c->flags &= ~CLIENT_REPEAT;
1542: server_status_client(c);
1.85 nicm 1543: }
1.192 nicm 1544: }
1545:
1546: /* Double-click callback. */
1547: static void
1548: server_client_click_timer(__unused int fd, __unused short events, void *data)
1549: {
1550: struct client *c = data;
1551:
1552: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1553: }
1554:
1.36 nicm 1555: /* Check if client should be exited. */
1.190 nicm 1556: static void
1.36 nicm 1557: server_client_check_exit(struct client *c)
1558: {
1.286 nicm 1559: if (~c->flags & CLIENT_EXIT)
1560: return;
1561: if (c->flags & CLIENT_EXITED)
1.36 nicm 1562: return;
1563:
1.73 nicm 1564: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1565: return;
1566: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1567: return;
1.73 nicm 1568: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1569: return;
1570:
1.249 nicm 1571: if (c->flags & CLIENT_ATTACHED)
1572: notify_client("client-detached", c);
1.162 nicm 1573: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.286 nicm 1574: c->flags |= CLIENT_EXITED;
1.38 nicm 1575: }
1576:
1.218 nicm 1577: /* Redraw timer callback. */
1578: static void
1579: server_client_redraw_timer(__unused int fd, __unused short events,
1580: __unused void* data)
1581: {
1582: log_debug("redraw timer fired");
1583: }
1584:
1.1 nicm 1585: /* Check for client redraws. */
1.190 nicm 1586: static void
1.1 nicm 1587: server_client_check_redraw(struct client *c)
1588: {
1589: struct session *s = c->session;
1.137 nicm 1590: struct tty *tty = &c->tty;
1.1 nicm 1591: struct window_pane *wp;
1.256 nicm 1592: int needed, flags;
1.218 nicm 1593: struct timeval tv = { .tv_usec = 1000 };
1594: static struct event ev;
1595: size_t left;
1.1 nicm 1596:
1.86 nicm 1597: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1598: return;
1.257 nicm 1599: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.282 nicm 1600: log_debug("%s: redraw%s%s%s%s", c->name,
1.257 nicm 1601: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1602: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282 nicm 1603: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
1604: (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "");
1.257 nicm 1605: }
1.218 nicm 1606:
1607: /*
1608: * If there is outstanding data, defer the redraw until it has been
1609: * consumed. We can just add a timer to get out of the event loop and
1610: * end up back here.
1611: */
1612: needed = 0;
1.256 nicm 1613: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1614: needed = 1;
1615: else {
1616: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1617: if (wp->flags & PANE_REDRAW) {
1618: needed = 1;
1619: break;
1620: }
1621: }
1622: }
1.241 nicm 1623: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1624: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1625: if (!evtimer_initialized(&ev))
1626: evtimer_set(&ev, server_client_redraw_timer, NULL);
1627: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1628: log_debug("redraw timer started");
1629: evtimer_add(&ev, &tv);
1.241 nicm 1630: }
1.218 nicm 1631:
1.241 nicm 1632: /*
1633: * We may have got here for a single pane redraw, but force a
1634: * full redraw next time in case other panes have been updated.
1635: */
1.256 nicm 1636: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241 nicm 1637: return;
1638: } else if (needed)
1.218 nicm 1639: log_debug("%s: redraw needed", c->name);
1.79 nicm 1640:
1.219 nicm 1641: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1642: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137 nicm 1643:
1.256 nicm 1644: if (~c->flags & CLIENT_REDRAWWINDOW) {
1645: /*
1646: * If not redrawing the entire window, check whether each pane
1647: * needs to be redrawn.
1648: */
1.1 nicm 1649: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1650: if (wp->flags & PANE_REDRAW) {
1651: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1652: screen_redraw_pane(c, wp);
1.137 nicm 1653: }
1.1 nicm 1654: }
1655: }
1656:
1.256 nicm 1657: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1658: if (options_get_number(s->options, "set-titles"))
1659: server_client_set_title(c);
1660: screen_redraw_screen(c);
1661: }
1.1 nicm 1662:
1.137 nicm 1663: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1664: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1665:
1.256 nicm 1666: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1667:
1668: if (needed) {
1669: /*
1670: * We would have deferred the redraw unless the output buffer
1671: * was empty, so we can record how many bytes the redraw
1672: * generated.
1673: */
1674: c->redraw = EVBUFFER_LENGTH(tty->out);
1675: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1676: }
1.1 nicm 1677: }
1678:
1679: /* Set client title. */
1.190 nicm 1680: static void
1.1 nicm 1681: server_client_set_title(struct client *c)
1682: {
1.128 nicm 1683: struct session *s = c->session;
1684: const char *template;
1685: char *title;
1686: struct format_tree *ft;
1.1 nicm 1687:
1.163 nicm 1688: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1689:
1.228 nicm 1690: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1691: format_defaults(ft, c, NULL, NULL, NULL);
1692:
1.269 nicm 1693: title = format_expand_time(ft, template);
1.1 nicm 1694: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1695: free(c->title);
1.1 nicm 1696: c->title = xstrdup(title);
1697: tty_set_title(&c->tty, c->title);
1698: }
1.77 nicm 1699: free(title);
1.128 nicm 1700:
1701: format_free(ft);
1.1 nicm 1702: }
1703:
1704: /* Dispatch message from client. */
1.190 nicm 1705: static void
1.162 nicm 1706: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1707: {
1.162 nicm 1708: struct client *c = arg;
1.73 nicm 1709: struct msg_stdin_data stdindata;
1.108 nicm 1710: const char *data;
1.162 nicm 1711: ssize_t datalen;
1.149 nicm 1712: struct session *s;
1.1 nicm 1713:
1.162 nicm 1714: if (c->flags & CLIENT_DEAD)
1715: return;
1716:
1717: if (imsg == NULL) {
1718: server_client_lost(c);
1719: return;
1720: }
1.1 nicm 1721:
1.162 nicm 1722: data = imsg->data;
1723: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1724:
1.162 nicm 1725: switch (imsg->hdr.type) {
1726: case MSG_IDENTIFY_FLAGS:
1727: case MSG_IDENTIFY_TERM:
1728: case MSG_IDENTIFY_TTYNAME:
1729: case MSG_IDENTIFY_CWD:
1730: case MSG_IDENTIFY_STDIN:
1731: case MSG_IDENTIFY_ENVIRON:
1732: case MSG_IDENTIFY_CLIENTPID:
1733: case MSG_IDENTIFY_DONE:
1734: server_client_dispatch_identify(c, imsg);
1735: break;
1736: case MSG_COMMAND:
1737: server_client_dispatch_command(c, imsg);
1738: break;
1739: case MSG_STDIN:
1740: if (datalen != sizeof stdindata)
1741: fatalx("bad MSG_STDIN size");
1742: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1743:
1.162 nicm 1744: if (c->stdin_callback == NULL)
1.33 nicm 1745: break;
1.162 nicm 1746: if (stdindata.size <= 0)
1747: c->stdin_closed = 1;
1748: else {
1749: evbuffer_add(c->stdin_data, stdindata.data,
1750: stdindata.size);
1751: }
1.279 nicm 1752: c->stdin_callback(c, c->stdin_closed, c->stdin_callback_data);
1.162 nicm 1753: break;
1754: case MSG_RESIZE:
1755: if (datalen != 0)
1756: fatalx("bad MSG_RESIZE size");
1.1 nicm 1757:
1.162 nicm 1758: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1759: break;
1.295 ! nicm 1760: server_client_update_latest(c);
1.282 nicm 1761: server_client_clear_overlay(c);
1.236 nicm 1762: tty_resize(&c->tty);
1763: recalculate_sizes();
1764: server_redraw_client(c);
1.174 nicm 1765: if (c->session != NULL)
1.196 nicm 1766: notify_client("client-resized", c);
1.162 nicm 1767: break;
1768: case MSG_EXITING:
1769: if (datalen != 0)
1770: fatalx("bad MSG_EXITING size");
1.1 nicm 1771:
1.162 nicm 1772: c->session = NULL;
1773: tty_close(&c->tty);
1774: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1775: break;
1776: case MSG_WAKEUP:
1777: case MSG_UNLOCK:
1778: if (datalen != 0)
1779: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1780:
1.162 nicm 1781: if (!(c->flags & CLIENT_SUSPENDED))
1782: break;
1783: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1784:
1.162 nicm 1785: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1786: break;
1.162 nicm 1787: s = c->session;
1.1 nicm 1788:
1.162 nicm 1789: if (gettimeofday(&c->activity_time, NULL) != 0)
1790: fatal("gettimeofday failed");
1791:
1792: tty_start_tty(&c->tty);
1793: server_redraw_client(c);
1794: recalculate_sizes();
1.184 nicm 1795:
1796: if (s != NULL)
1797: session_update_activity(s, &c->activity_time);
1.162 nicm 1798: break;
1799: case MSG_SHELL:
1800: if (datalen != 0)
1801: fatalx("bad MSG_SHELL size");
1.1 nicm 1802:
1.162 nicm 1803: server_client_dispatch_shell(c);
1804: break;
1.1 nicm 1805: }
1806: }
1807:
1.194 nicm 1808: /* Callback when command is done. */
1809: static enum cmd_retval
1.195 nicm 1810: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1811: {
1.195 nicm 1812: struct client *c = item->client;
1.194 nicm 1813:
1814: if (~c->flags & CLIENT_ATTACHED)
1815: c->flags |= CLIENT_EXIT;
1816: return (CMD_RETURN_NORMAL);
1817: }
1818:
1.1 nicm 1819: /* Handle command message. */
1.190 nicm 1820: static void
1.162 nicm 1821: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1822: {
1.108 nicm 1823: struct msg_command_data data;
1824: char *buf;
1825: size_t len;
1826: int argc;
1827: char **argv, *cause;
1.285 nicm 1828: struct cmd_parse_result *pr;
1.246 nicm 1829:
1830: if (c->flags & CLIENT_EXIT)
1831: return;
1.108 nicm 1832:
1833: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1834: fatalx("bad MSG_COMMAND size");
1835: memcpy(&data, imsg->data, sizeof data);
1836:
1.124 nicm 1837: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1838: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1839: if (len > 0 && buf[len - 1] != '\0')
1840: fatalx("bad MSG_COMMAND string");
1841:
1842: argc = data.argc;
1843: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1844: cause = xstrdup("command too long");
1.1 nicm 1845: goto error;
1846: }
1847:
1848: if (argc == 0) {
1849: argc = 1;
1850: argv = xcalloc(1, sizeof *argv);
1851: *argv = xstrdup("new-session");
1852: }
1853:
1.285 nicm 1854: pr = cmd_parse_from_arguments(argc, argv, NULL);
1855: switch (pr->status) {
1856: case CMD_PARSE_EMPTY:
1857: cause = xstrdup("empty command");
1.1 nicm 1858: goto error;
1.285 nicm 1859: case CMD_PARSE_ERROR:
1860: cause = pr->error;
1861: goto error;
1862: case CMD_PARSE_SUCCESS:
1863: break;
1.1 nicm 1864: }
1865: cmd_free_argv(argc, argv);
1866:
1.285 nicm 1867: cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL, NULL, 0));
1.194 nicm 1868: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.285 nicm 1869:
1870: cmd_list_free(pr->cmdlist);
1.1 nicm 1871: return;
1872:
1873: error:
1.285 nicm 1874: cmd_free_argv(argc, argv);
1875:
1.284 nicm 1876: cmdq_append(c, cmdq_get_error(cause));
1877: free(cause);
1.88 nicm 1878:
1.36 nicm 1879: c->flags |= CLIENT_EXIT;
1.1 nicm 1880: }
1881:
1882: /* Handle identify message. */
1.190 nicm 1883: static void
1.162 nicm 1884: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1885: {
1.165 nicm 1886: const char *data, *home;
1.232 nicm 1887: size_t datalen;
1.109 nicm 1888: int flags;
1.216 nicm 1889: char *name;
1.109 nicm 1890:
1891: if (c->flags & CLIENT_IDENTIFIED)
1892: fatalx("out-of-order identify message");
1893:
1894: data = imsg->data;
1895: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1896:
1897: switch (imsg->hdr.type) {
1898: case MSG_IDENTIFY_FLAGS:
1899: if (datalen != sizeof flags)
1900: fatalx("bad MSG_IDENTIFY_FLAGS size");
1901: memcpy(&flags, data, sizeof flags);
1902: c->flags |= flags;
1.158 nicm 1903: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1904: break;
1905: case MSG_IDENTIFY_TERM:
1.110 nicm 1906: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1907: fatalx("bad MSG_IDENTIFY_TERM string");
1908: c->term = xstrdup(data);
1.158 nicm 1909: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1910: break;
1911: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1912: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1913: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1914: c->ttyname = xstrdup(data);
1.158 nicm 1915: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1916: break;
1917: case MSG_IDENTIFY_CWD:
1.155 nicm 1918: if (datalen == 0 || data[datalen - 1] != '\0')
1919: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1920: if (access(data, X_OK) == 0)
1921: c->cwd = xstrdup(data);
1922: else if ((home = find_home()) != NULL)
1923: c->cwd = xstrdup(home);
1924: else
1925: c->cwd = xstrdup("/");
1.158 nicm 1926: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1927: break;
1928: case MSG_IDENTIFY_STDIN:
1929: if (datalen != 0)
1930: fatalx("bad MSG_IDENTIFY_STDIN size");
1931: c->fd = imsg->fd;
1.158 nicm 1932: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1933: break;
1934: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1935: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1936: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1937: if (strchr(data, '=') != NULL)
1.164 nicm 1938: environ_put(c->environ, data);
1.158 nicm 1939: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1940: break;
1941: case MSG_IDENTIFY_CLIENTPID:
1942: if (datalen != sizeof c->pid)
1943: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1944: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1945: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1946: break;
1947: default:
1948: break;
1949: }
1950:
1951: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1952: return;
1953: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1954:
1.216 nicm 1955: if (*c->ttyname != '\0')
1956: name = xstrdup(c->ttyname);
1957: else
1958: xasprintf(&name, "client-%ld", (long)c->pid);
1959: c->name = name;
1960: log_debug("client %p name is %s", c, c->name);
1961:
1.109 nicm 1962: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1963: c->stdin_callback = control_callback;
1.109 nicm 1964:
1.97 nicm 1965: evbuffer_free(c->stderr_data);
1966: c->stderr_data = c->stdout_data;
1.109 nicm 1967:
1968: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1969: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1970: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1971:
1972: c->tty.fd = -1;
1973:
1.109 nicm 1974: close(c->fd);
1975: c->fd = -1;
1.288 nicm 1976: } else if (c->fd != -1) {
1977: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1978: close(c->fd);
1979: c->fd = -1;
1980: } else {
1981: if (c->flags & CLIENT_UTF8)
1982: c->tty.flags |= TTY_UTF8;
1983: if (c->flags & CLIENT_256COLOURS)
1984: c->tty.term_flags |= TERM_256COLOURS;
1985: tty_resize(&c->tty);
1986: c->flags |= CLIENT_TERMINAL;
1987: }
1.75 nicm 1988: }
1.1 nicm 1989:
1.288 nicm 1990: /*
1991: * If this is the first client that has finished identifying, load
1992: * configuration files.
1993: */
1994: if ((~c->flags & CLIENT_EXIT) &&
1995: !cfg_finished &&
1996: c == TAILQ_FIRST(&clients) &&
1997: TAILQ_NEXT(c, entry) == NULL)
1998: start_cfg();
1.1 nicm 1999: }
2000:
2001: /* Handle shell message. */
1.190 nicm 2002: static void
1.162 nicm 2003: server_client_dispatch_shell(struct client *c)
1.1 nicm 2004: {
1.107 nicm 2005: const char *shell;
1.25 nicm 2006:
1.163 nicm 2007: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 2008: if (*shell == '\0' || areshell(shell))
2009: shell = _PATH_BSHELL;
1.240 nicm 2010: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 2011:
1.162 nicm 2012: proc_kill_peer(c->peer);
1.169 nicm 2013: }
2014:
2015: /* Event callback to push more stdout data if any left. */
2016: static void
1.170 nicm 2017: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 2018: {
2019: struct client *c = arg;
2020:
2021: if (~c->flags & CLIENT_DEAD)
2022: server_client_push_stdout(c);
2023: server_client_unref(c);
2024: }
2025:
2026: /* Push stdout to client if possible. */
2027: void
2028: server_client_push_stdout(struct client *c)
2029: {
2030: struct msg_stdout_data data;
1.232 nicm 2031: size_t sent, left;
1.169 nicm 2032:
2033: left = EVBUFFER_LENGTH(c->stdout_data);
2034: while (left != 0) {
2035: sent = left;
2036: if (sent > sizeof data.data)
2037: sent = sizeof data.data;
2038: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
2039: data.size = sent;
2040:
2041: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
2042: break;
2043: evbuffer_drain(c->stdout_data, sent);
2044:
2045: left = EVBUFFER_LENGTH(c->stdout_data);
2046: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
2047: sent, left);
2048: }
2049: if (left != 0) {
2050: c->references++;
2051: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
2052: log_debug("%s: client %p, queued", __func__, c);
2053: }
2054: }
2055:
2056: /* Event callback to push more stderr data if any left. */
2057: static void
1.170 nicm 2058: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 2059: {
2060: struct client *c = arg;
2061:
2062: if (~c->flags & CLIENT_DEAD)
2063: server_client_push_stderr(c);
2064: server_client_unref(c);
2065: }
2066:
2067: /* Push stderr to client if possible. */
2068: void
2069: server_client_push_stderr(struct client *c)
2070: {
2071: struct msg_stderr_data data;
1.232 nicm 2072: size_t sent, left;
1.169 nicm 2073:
2074: if (c->stderr_data == c->stdout_data) {
2075: server_client_push_stdout(c);
2076: return;
2077: }
2078:
2079: left = EVBUFFER_LENGTH(c->stderr_data);
2080: while (left != 0) {
2081: sent = left;
2082: if (sent > sizeof data.data)
2083: sent = sizeof data.data;
2084: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
2085: data.size = sent;
2086:
2087: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
2088: break;
2089: evbuffer_drain(c->stderr_data, sent);
2090:
2091: left = EVBUFFER_LENGTH(c->stderr_data);
2092: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
2093: sent, left);
2094: }
2095: if (left != 0) {
2096: c->references++;
2097: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
2098: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 2099: }
2100: }
2101:
2102: /* Add to client message log. */
2103: void
2104: server_client_add_message(struct client *c, const char *fmt, ...)
2105: {
2106: struct message_entry *msg, *msg1;
2107: char *s;
2108: va_list ap;
2109: u_int limit;
2110:
2111: va_start(ap, fmt);
2112: xvasprintf(&s, fmt, ap);
2113: va_end(ap);
2114:
1.216 nicm 2115: log_debug("message %s (client %p)", s, c);
1.212 nicm 2116:
2117: msg = xcalloc(1, sizeof *msg);
2118: msg->msg_time = time(NULL);
2119: msg->msg_num = c->message_next++;
2120: msg->msg = s;
2121: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2122:
2123: limit = options_get_number(global_options, "message-limit");
2124: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2125: if (msg->msg_num + limit >= c->message_next)
2126: break;
2127: free(msg->msg);
2128: TAILQ_REMOVE(&c->message_log, msg, entry);
2129: free(msg);
1.169 nicm 2130: }
1.213 nicm 2131: }
2132:
2133: /* Get client working directory. */
2134: const char *
1.250 nicm 2135: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2136: {
1.250 nicm 2137: const char *home;
1.213 nicm 2138:
1.265 nicm 2139: if (!cfg_finished && cfg_client != NULL)
2140: return (cfg_client->cwd);
1.213 nicm 2141: if (c != NULL && c->session == NULL && c->cwd != NULL)
2142: return (c->cwd);
1.250 nicm 2143: if (s != NULL && s->cwd != NULL)
2144: return (s->cwd);
1.213 nicm 2145: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2146: return (s->cwd);
1.250 nicm 2147: if ((home = find_home()) != NULL)
2148: return (home);
2149: return ("/");
1.213 nicm 2150: }
2151:
2152: /* Resolve an absolute path or relative to client working directory. */
2153: char *
2154: server_client_get_path(struct client *c, const char *file)
2155: {
2156: char *path, resolved[PATH_MAX];
2157:
2158: if (*file == '/')
2159: path = xstrdup(file);
2160: else
1.250 nicm 2161: xasprintf(&path, "%s/%s", server_client_get_cwd(c, NULL), file);
1.213 nicm 2162: if (realpath(path, resolved) == NULL)
2163: return (path);
2164: free(path);
2165: return (xstrdup(resolved));
1.1 nicm 2166: }