Annotation of src/usr.bin/tmux/server-client.c, Revision 1.306
1.306 ! nicm 1: /* $OpenBSD: server-client.c,v 1.305 2020/02/19 14:25:00 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.300 nicm 53: static void server_client_dispatch_write_ready(struct client *,
54: struct imsg *);
55: static void server_client_dispatch_read_data(struct client *,
56: struct imsg *);
57: static void server_client_dispatch_read_done(struct client *,
58: struct imsg *);
1.233 nicm 59:
60: /* Number of attached clients. */
61: u_int
62: server_client_how_many(void)
63: {
64: struct client *c;
65: u_int n;
66:
67: n = 0;
68: TAILQ_FOREACH(c, &clients, entry) {
69: if (c->session != NULL && (~c->flags & CLIENT_DETACHING))
70: n++;
71: }
72: return (n);
73: }
1.140 nicm 74:
1.281 nicm 75: /* Overlay timer callback. */
1.214 nicm 76: static void
1.281 nicm 77: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214 nicm 78: {
1.281 nicm 79: server_client_clear_overlay(data);
1.214 nicm 80: }
81:
1.281 nicm 82: /* Set an overlay on client. */
1.214 nicm 83: void
1.281 nicm 84: server_client_set_overlay(struct client *c, u_int delay, overlay_draw_cb drawcb,
85: overlay_key_cb keycb, overlay_free_cb freecb, void *data)
1.214 nicm 86: {
87: struct timeval tv;
88:
1.282 nicm 89: if (c->overlay_draw != NULL)
90: server_client_clear_overlay(c);
91:
1.214 nicm 92: tv.tv_sec = delay / 1000;
93: tv.tv_usec = (delay % 1000) * 1000L;
94:
1.281 nicm 95: if (event_initialized(&c->overlay_timer))
96: evtimer_del(&c->overlay_timer);
97: evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242 nicm 98: if (delay != 0)
1.281 nicm 99: evtimer_add(&c->overlay_timer, &tv);
100:
101: c->overlay_draw = drawcb;
102: c->overlay_key = keycb;
103: c->overlay_free = freecb;
104: c->overlay_data = data;
1.214 nicm 105:
106: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
107: server_redraw_client(c);
108: }
109:
1.281 nicm 110: /* Clear overlay mode on client. */
1.254 nicm 111: static void
1.281 nicm 112: server_client_clear_overlay(struct client *c)
1.214 nicm 113: {
1.281 nicm 114: if (c->overlay_draw == NULL)
1.214 nicm 115: return;
116:
1.281 nicm 117: if (event_initialized(&c->overlay_timer))
118: evtimer_del(&c->overlay_timer);
119:
120: if (c->overlay_free != NULL)
121: c->overlay_free(c);
122:
123: c->overlay_draw = NULL;
124: c->overlay_key = NULL;
1.214 nicm 125:
126: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
127: server_redraw_client(c);
128: }
129:
1.140 nicm 130: /* Check if this client is inside this server. */
131: int
132: server_client_check_nested(struct client *c)
133: {
134: struct environ_entry *envent;
135: struct window_pane *wp;
136:
1.164 nicm 137: envent = environ_find(c->environ, "TMUX");
1.140 nicm 138: if (envent == NULL || *envent->value == '\0')
139: return (0);
140:
141: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 142: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 143: return (1);
144: }
145: return (0);
146: }
1.1 nicm 147:
1.132 nicm 148: /* Set client key table. */
149: void
1.178 nicm 150: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 151: {
1.178 nicm 152: if (name == NULL)
153: name = server_client_get_key_table(c);
154:
1.132 nicm 155: key_bindings_unref_table(c->keytable);
156: c->keytable = key_bindings_get_table(name, 1);
157: c->keytable->references++;
158: }
159:
1.178 nicm 160: /* Get default key table. */
161: const char *
162: server_client_get_key_table(struct client *c)
163: {
164: struct session *s = c->session;
165: const char *name;
166:
167: if (s == NULL)
168: return ("root");
169:
170: name = options_get_string(s->options, "key-table");
171: if (*name == '\0')
172: return ("root");
173: return (name);
174: }
175:
1.223 nicm 176: /* Is this table the default key table? */
177: static int
178: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191 nicm 179: {
1.223 nicm 180: return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191 nicm 181: }
182:
1.1 nicm 183: /* Create a new client. */
1.246 nicm 184: struct client *
1.1 nicm 185: server_client_create(int fd)
186: {
187: struct client *c;
188:
1.49 nicm 189: setblocking(fd, 0);
1.1 nicm 190:
191: c = xcalloc(1, sizeof *c);
1.141 nicm 192: c->references = 1;
1.162 nicm 193: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 194:
1.10 nicm 195: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 196: fatal("gettimeofday failed");
1.11 nicm 197: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 198:
1.164 nicm 199: c->environ = environ_create();
1.1 nicm 200:
1.146 nicm 201: c->fd = -1;
1.165 nicm 202: c->cwd = NULL;
1.145 nicm 203:
1.194 nicm 204: TAILQ_INIT(&c->queue);
1.94 nicm 205:
1.1 nicm 206: c->tty.fd = -1;
207: c->title = NULL;
208:
209: c->session = NULL;
1.45 nicm 210: c->last_session = NULL;
1.261 nicm 211:
1.1 nicm 212: c->tty.sx = 80;
213: c->tty.sy = 24;
214:
1.271 nicm 215: status_init(c);
1.1 nicm 216:
217: c->message_string = NULL;
1.136 nicm 218: TAILQ_INIT(&c->message_log);
1.1 nicm 219:
220: c->prompt_string = NULL;
221: c->prompt_buffer = NULL;
222: c->prompt_index = 0;
223:
1.300 nicm 224: RB_INIT(&c->files);
225:
1.93 nicm 226: c->flags |= CLIENT_FOCUSED;
227:
1.132 nicm 228: c->keytable = key_bindings_get_table("root", 1);
229: c->keytable->references++;
230:
1.17 nicm 231: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 232: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 233:
1.135 nicm 234: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 235: log_debug("new client %p", c);
1.246 nicm 236: return (c);
1.72 nicm 237: }
238:
239: /* Open client terminal if needed. */
240: int
1.119 nicm 241: server_client_open(struct client *c, char **cause)
1.72 nicm 242: {
1.75 nicm 243: if (c->flags & CLIENT_CONTROL)
244: return (0);
1.120 nicm 245:
246: if (strcmp(c->ttyname, "/dev/tty") == 0) {
247: *cause = xstrdup("can't use /dev/tty");
248: return (-1);
249: }
1.75 nicm 250:
1.72 nicm 251: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 252: *cause = xstrdup("not a terminal");
1.72 nicm 253: return (-1);
254: }
255:
1.119 nicm 256: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 257: return (-1);
258:
259: return (0);
1.1 nicm 260: }
261:
262: /* Lost a client. */
263: void
264: server_client_lost(struct client *c)
265: {
1.136 nicm 266: struct message_entry *msg, *msg1;
1.300 nicm 267: struct client_file *cf;
1.1 nicm 268:
1.141 nicm 269: c->flags |= CLIENT_DEAD;
270:
1.281 nicm 271: server_client_clear_overlay(c);
1.141 nicm 272: status_prompt_clear(c);
273: status_message_clear(c);
274:
1.300 nicm 275: RB_FOREACH(cf, client_files, &c->files) {
276: cf->error = EINTR;
277: file_fire_done(cf);
278: }
1.141 nicm 279:
1.135 nicm 280: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 281: log_debug("lost client %p", c);
1.1 nicm 282:
283: /*
284: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
285: * and tty_free might close an unrelated fd.
286: */
287: if (c->flags & CLIENT_TERMINAL)
288: tty_free(&c->tty);
1.109 nicm 289: free(c->ttyname);
290: free(c->term);
1.1 nicm 291:
1.270 nicm 292: status_free(c);
1.1 nicm 293:
1.77 nicm 294: free(c->title);
1.165 nicm 295: free((void *)c->cwd);
1.1 nicm 296:
1.17 nicm 297: evtimer_del(&c->repeat_timer);
1.192 nicm 298: evtimer_del(&c->click_timer);
1.17 nicm 299:
1.132 nicm 300: key_bindings_unref_table(c->keytable);
301:
1.77 nicm 302: free(c->message_string);
1.116 nicm 303: if (event_initialized(&c->message_timer))
1.69 nicm 304: evtimer_del(&c->message_timer);
1.136 nicm 305: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 306: free(msg->msg);
1.136 nicm 307: TAILQ_REMOVE(&c->message_log, msg, entry);
308: free(msg);
1.21 nicm 309: }
1.1 nicm 310:
1.259 nicm 311: free(c->prompt_saved);
1.77 nicm 312: free(c->prompt_string);
313: free(c->prompt_buffer);
1.61 nicm 314:
1.228 nicm 315: format_lost_client(c);
1.164 nicm 316: environ_free(c->environ);
1.1 nicm 317:
1.162 nicm 318: proc_remove_peer(c->peer);
319: c->peer = NULL;
1.13 nicm 320:
1.142 nicm 321: server_client_unref(c);
1.71 nicm 322:
323: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 324:
325: recalculate_sizes();
1.41 nicm 326: server_check_unattached();
1.19 nicm 327: server_update_socket();
1.141 nicm 328: }
329:
330: /* Remove reference from a client. */
331: void
1.142 nicm 332: server_client_unref(struct client *c)
1.141 nicm 333: {
1.157 nicm 334: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 335:
336: c->references--;
337: if (c->references == 0)
338: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
339: }
340:
341: /* Free dead client. */
1.190 nicm 342: static void
1.170 nicm 343: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 344: {
345: struct client *c = arg;
346:
1.157 nicm 347: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 348:
1.194 nicm 349: if (!TAILQ_EMPTY(&c->queue))
350: fatalx("queue not empty");
351:
1.216 nicm 352: if (c->references == 0) {
353: free((void *)c->name);
1.141 nicm 354: free(c);
1.216 nicm 355: }
1.9 nicm 356: }
357:
1.220 nicm 358: /* Suspend a client. */
359: void
360: server_client_suspend(struct client *c)
361: {
1.232 nicm 362: struct session *s = c->session;
1.220 nicm 363:
364: if (s == NULL || (c->flags & CLIENT_DETACHING))
365: return;
366:
367: tty_stop_tty(&c->tty);
368: c->flags |= CLIENT_SUSPENDED;
369: proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
370: }
371:
1.174 nicm 372: /* Detach a client. */
373: void
374: server_client_detach(struct client *c, enum msgtype msgtype)
375: {
1.232 nicm 376: struct session *s = c->session;
1.174 nicm 377:
1.220 nicm 378: if (s == NULL || (c->flags & CLIENT_DETACHING))
1.174 nicm 379: return;
380:
1.220 nicm 381: c->flags |= CLIENT_DETACHING;
1.196 nicm 382: notify_client("client-detached", c);
1.240 nicm 383: proc_send(c->peer, msgtype, -1, s->name, strlen(s->name) + 1);
1.207 nicm 384: }
385:
1.208 nicm 386: /* Execute command to replace a client. */
1.207 nicm 387: void
388: server_client_exec(struct client *c, const char *cmd)
389: {
390: struct session *s = c->session;
1.208 nicm 391: char *msg;
392: const char *shell;
1.207 nicm 393: size_t cmdsize, shellsize;
394:
395: if (*cmd == '\0')
396: return;
397: cmdsize = strlen(cmd) + 1;
398:
399: if (s != NULL)
400: shell = options_get_string(s->options, "default-shell");
401: else
402: shell = options_get_string(global_s_options, "default-shell");
403: shellsize = strlen(shell) + 1;
404:
405: msg = xmalloc(cmdsize + shellsize);
406: memcpy(msg, cmd, cmdsize);
407: memcpy(msg + cmdsize, shell, shellsize);
408:
409: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
410: free(msg);
1.174 nicm 411: }
412:
1.66 nicm 413: /* Check for mouse keys. */
1.190 nicm 414: static key_code
1.276 nicm 415: server_client_check_mouse(struct client *c, struct key_event *event)
1.66 nicm 416: {
1.276 nicm 417: struct mouse_event *m = &event->m;
1.192 nicm 418: struct session *s = c->session;
1.272 nicm 419: struct winlink *wl;
1.192 nicm 420: struct window_pane *wp;
1.261 nicm 421: u_int x, y, b, sx, sy, px, py;
1.192 nicm 422: key_code key;
423: struct timeval tv;
1.272 nicm 424: struct style_range *sr;
1.283 nicm 425: enum { NOTYPE,
426: MOVE,
427: DOWN,
428: UP,
429: DRAG,
430: WHEEL,
431: DOUBLE,
432: TRIPLE } type = NOTYPE;
1.276 nicm 433: enum { NOWHERE,
434: PANE,
435: STATUS,
436: STATUS_LEFT,
437: STATUS_RIGHT,
438: STATUS_DEFAULT,
1.283 nicm 439: BORDER } where = NOWHERE;
1.131 nicm 440:
1.261 nicm 441: log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
442: m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131 nicm 443:
444: /* What type of event is this? */
1.306 ! nicm 445: if (event->key == KEYC_DOUBLECLICK) {
! 446: type = DOUBLE;
! 447: x = m->x, y = m->y, b = m->b;
! 448: log_debug("double-click at %u,%u", x, y);
! 449: } else if ((m->sgr_type != ' ' &&
1.209 nicm 450: MOUSE_DRAG(m->sgr_b) &&
451: MOUSE_BUTTONS(m->sgr_b) == 3) ||
452: (m->sgr_type == ' ' &&
453: MOUSE_DRAG(m->b) &&
454: MOUSE_BUTTONS(m->b) == 3 &&
455: MOUSE_BUTTONS(m->lb) == 3)) {
456: type = MOVE;
457: x = m->x, y = m->y, b = 0;
458: log_debug("move at %u,%u", x, y);
459: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 460: type = DRAG;
461: if (c->tty.mouse_drag_flag) {
462: x = m->x, y = m->y, b = m->b;
1.278 nicm 463: if (x == m->lx && y == m->ly)
464: return (KEYC_UNKNOWN);
1.131 nicm 465: log_debug("drag update at %u,%u", x, y);
466: } else {
1.277 nicm 467: x = m->lx, y = m->ly, b = m->lb;
1.131 nicm 468: log_debug("drag start at %u,%u", x, y);
469: }
470: } else if (MOUSE_WHEEL(m->b)) {
471: type = WHEEL;
472: x = m->x, y = m->y, b = m->b;
473: log_debug("wheel at %u,%u", x, y);
1.200 nicm 474: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 475: type = UP;
476: x = m->x, y = m->y, b = m->lb;
477: log_debug("up at %u,%u", x, y);
478: } else {
1.192 nicm 479: if (c->flags & CLIENT_DOUBLECLICK) {
480: evtimer_del(&c->click_timer);
481: c->flags &= ~CLIENT_DOUBLECLICK;
482: if (m->b == c->click_button) {
1.306 ! nicm 483: type = NOTYPE;
! 484: c->flags |= CLIENT_TRIPLECLICK;
1.192 nicm 485: goto add_timer;
486: }
487: } else if (c->flags & CLIENT_TRIPLECLICK) {
488: evtimer_del(&c->click_timer);
489: c->flags &= ~CLIENT_TRIPLECLICK;
490: if (m->b == c->click_button) {
491: type = TRIPLE;
492: x = m->x, y = m->y, b = m->b;
493: log_debug("triple-click at %u,%u", x, y);
494: goto have_event;
495: }
496: }
497:
1.131 nicm 498: type = DOWN;
499: x = m->x, y = m->y, b = m->b;
500: log_debug("down at %u,%u", x, y);
1.306 ! nicm 501: c->flags |= CLIENT_DOUBLECLICK;
1.192 nicm 502:
503: add_timer:
504: if (KEYC_CLICK_TIMEOUT != 0) {
1.306 ! nicm 505: memcpy(&c->click_event, m, sizeof c->click_event);
1.192 nicm 506: c->click_button = m->b;
507:
508: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
509: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
510: evtimer_del(&c->click_timer);
511: evtimer_add(&c->click_timer, &tv);
512: }
1.131 nicm 513: }
1.192 nicm 514:
515: have_event:
1.131 nicm 516: if (type == NOTYPE)
1.177 nicm 517: return (KEYC_UNKNOWN);
1.131 nicm 518:
1.258 nicm 519: /* Save the session. */
1.131 nicm 520: m->s = s->id;
1.258 nicm 521: m->w = -1;
1.131 nicm 522:
523: /* Is this on the status line? */
524: m->statusat = status_at_line(c);
1.292 nicm 525: m->statuslines = status_line_size(c);
1.272 nicm 526: if (m->statusat != -1 &&
527: y >= (u_int)m->statusat &&
1.292 nicm 528: y < m->statusat + m->statuslines) {
1.272 nicm 529: sr = status_get_range(c, x, y - m->statusat);
1.274 nicm 530: if (sr == NULL) {
531: where = STATUS_DEFAULT;
532: } else {
533: switch (sr->type) {
534: case STYLE_RANGE_NONE:
1.273 nicm 535: return (KEYC_UNKNOWN);
1.274 nicm 536: case STYLE_RANGE_LEFT:
537: where = STATUS_LEFT;
538: break;
539: case STYLE_RANGE_RIGHT:
540: where = STATUS_RIGHT;
541: break;
542: case STYLE_RANGE_WINDOW:
1.298 nicm 543: wl = winlink_find_by_index(&s->windows,
544: sr->argument);
1.274 nicm 545: if (wl == NULL)
546: return (KEYC_UNKNOWN);
547: m->w = wl->window->id;
1.273 nicm 548:
1.274 nicm 549: where = STATUS;
550: break;
551: }
1.258 nicm 552: }
553: }
1.131 nicm 554:
555: /* Not on status line. Adjust position and check for border or pane. */
556: if (where == NOWHERE) {
1.261 nicm 557: px = x;
1.292 nicm 558: if (m->statusat == 0 && y >= m->statuslines)
559: py = y - m->statuslines;
1.131 nicm 560: else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261 nicm 561: py = m->statusat - 1;
562: else
563: py = y;
564:
565: tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
566: log_debug("mouse window @%u at %u,%u (%ux%u)",
567: s->curw->window->id, m->ox, m->oy, sx, sy);
568: if (px > sx || py > sy)
569: return (KEYC_UNKNOWN);
570: px = px + m->ox;
571: py = py + m->oy;
1.131 nicm 572:
1.260 nicm 573: /* Try the pane borders if not zoomed. */
574: if (~s->curw->window->flags & WINDOW_ZOOMED) {
575: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261 nicm 576: if ((wp->xoff + wp->sx == px &&
577: wp->yoff <= 1 + py &&
578: wp->yoff + wp->sy >= py) ||
579: (wp->yoff + wp->sy == py &&
580: wp->xoff <= 1 + px &&
581: wp->xoff + wp->sx >= px))
1.260 nicm 582: break;
583: }
584: if (wp != NULL)
585: where = BORDER;
1.131 nicm 586: }
1.260 nicm 587:
588: /* Otherwise try inside the pane. */
589: if (where == NOWHERE) {
1.261 nicm 590: wp = window_get_active_at(s->curw->window, px, py);
1.260 nicm 591: if (wp != NULL)
1.131 nicm 592: where = PANE;
593: }
1.260 nicm 594:
1.131 nicm 595: if (where == NOWHERE)
1.177 nicm 596: return (KEYC_UNKNOWN);
1.260 nicm 597: if (where == PANE)
598: log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
599: else if (where == BORDER)
600: log_debug("mouse on pane %%%u border", wp->id);
1.131 nicm 601: m->wp = wp->id;
602: m->w = wp->window->id;
603: } else
604: m->wp = -1;
605:
606: /* Stop dragging if needed. */
1.200 nicm 607: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 608: if (c->tty.mouse_drag_release != NULL)
609: c->tty.mouse_drag_release(c, m);
610:
611: c->tty.mouse_drag_update = NULL;
612: c->tty.mouse_drag_release = NULL;
613:
1.182 nicm 614: /*
1.183 nicm 615: * End a mouse drag by passing a MouseDragEnd key corresponding
616: * to the button that started the drag.
1.182 nicm 617: */
618: switch (c->tty.mouse_drag_flag) {
619: case 1:
620: if (where == PANE)
1.183 nicm 621: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 622: if (where == STATUS)
1.183 nicm 623: key = KEYC_MOUSEDRAGEND1_STATUS;
1.258 nicm 624: if (where == STATUS_LEFT)
625: key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
626: if (where == STATUS_RIGHT)
627: key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274 nicm 628: if (where == STATUS_DEFAULT)
629: key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182 nicm 630: if (where == BORDER)
1.183 nicm 631: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 632: break;
633: case 2:
634: if (where == PANE)
1.183 nicm 635: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 636: if (where == STATUS)
1.183 nicm 637: key = KEYC_MOUSEDRAGEND2_STATUS;
1.258 nicm 638: if (where == STATUS_LEFT)
639: key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
640: if (where == STATUS_RIGHT)
641: key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274 nicm 642: if (where == STATUS_DEFAULT)
643: key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182 nicm 644: if (where == BORDER)
1.183 nicm 645: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 646: break;
647: case 3:
648: if (where == PANE)
1.183 nicm 649: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 650: if (where == STATUS)
1.183 nicm 651: key = KEYC_MOUSEDRAGEND3_STATUS;
1.258 nicm 652: if (where == STATUS_LEFT)
653: key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
654: if (where == STATUS_RIGHT)
655: key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274 nicm 656: if (where == STATUS_DEFAULT)
657: key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182 nicm 658: if (where == BORDER)
1.183 nicm 659: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 660: break;
661: default:
662: key = KEYC_MOUSE;
663: break;
664: }
1.131 nicm 665: c->tty.mouse_drag_flag = 0;
1.305 nicm 666: goto out;
1.131 nicm 667: }
668:
669: /* Convert to a key binding. */
1.177 nicm 670: key = KEYC_UNKNOWN;
1.131 nicm 671: switch (type) {
672: case NOTYPE:
673: break;
1.209 nicm 674: case MOVE:
675: if (where == PANE)
676: key = KEYC_MOUSEMOVE_PANE;
677: if (where == STATUS)
678: key = KEYC_MOUSEMOVE_STATUS;
1.274 nicm 679: if (where == STATUS_LEFT)
680: key = KEYC_MOUSEMOVE_STATUS_LEFT;
681: if (where == STATUS_RIGHT)
682: key = KEYC_MOUSEMOVE_STATUS_RIGHT;
683: if (where == STATUS_DEFAULT)
684: key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209 nicm 685: if (where == BORDER)
686: key = KEYC_MOUSEMOVE_BORDER;
687: break;
1.131 nicm 688: case DRAG:
1.204 nicm 689: if (c->tty.mouse_drag_update != NULL)
690: key = KEYC_DRAGGING;
691: else {
1.131 nicm 692: switch (MOUSE_BUTTONS(b)) {
693: case 0:
694: if (where == PANE)
695: key = KEYC_MOUSEDRAG1_PANE;
696: if (where == STATUS)
697: key = KEYC_MOUSEDRAG1_STATUS;
1.258 nicm 698: if (where == STATUS_LEFT)
699: key = KEYC_MOUSEDRAG1_STATUS_LEFT;
700: if (where == STATUS_RIGHT)
701: key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274 nicm 702: if (where == STATUS_DEFAULT)
703: key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131 nicm 704: if (where == BORDER)
705: key = KEYC_MOUSEDRAG1_BORDER;
706: break;
707: case 1:
708: if (where == PANE)
709: key = KEYC_MOUSEDRAG2_PANE;
710: if (where == STATUS)
711: key = KEYC_MOUSEDRAG2_STATUS;
1.258 nicm 712: if (where == STATUS_LEFT)
713: key = KEYC_MOUSEDRAG2_STATUS_LEFT;
714: if (where == STATUS_RIGHT)
715: key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274 nicm 716: if (where == STATUS_DEFAULT)
717: key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131 nicm 718: if (where == BORDER)
719: key = KEYC_MOUSEDRAG2_BORDER;
720: break;
721: case 2:
722: if (where == PANE)
723: key = KEYC_MOUSEDRAG3_PANE;
724: if (where == STATUS)
725: key = KEYC_MOUSEDRAG3_STATUS;
1.258 nicm 726: if (where == STATUS_LEFT)
727: key = KEYC_MOUSEDRAG3_STATUS_LEFT;
728: if (where == STATUS_RIGHT)
729: key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274 nicm 730: if (where == STATUS_DEFAULT)
731: key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131 nicm 732: if (where == BORDER)
733: key = KEYC_MOUSEDRAG3_BORDER;
734: break;
735: }
736: }
1.66 nicm 737:
1.182 nicm 738: /*
739: * Begin a drag by setting the flag to a non-zero value that
740: * corresponds to the mouse button in use.
741: */
742: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 743: break;
744: case WHEEL:
745: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
746: if (where == PANE)
747: key = KEYC_WHEELUP_PANE;
748: if (where == STATUS)
749: key = KEYC_WHEELUP_STATUS;
1.258 nicm 750: if (where == STATUS_LEFT)
751: key = KEYC_WHEELUP_STATUS_LEFT;
752: if (where == STATUS_RIGHT)
753: key = KEYC_WHEELUP_STATUS_RIGHT;
1.274 nicm 754: if (where == STATUS_DEFAULT)
755: key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131 nicm 756: if (where == BORDER)
757: key = KEYC_WHEELUP_BORDER;
758: } else {
759: if (where == PANE)
760: key = KEYC_WHEELDOWN_PANE;
761: if (where == STATUS)
762: key = KEYC_WHEELDOWN_STATUS;
1.274 nicm 763: if (where == STATUS_LEFT)
764: key = KEYC_WHEELDOWN_STATUS_LEFT;
765: if (where == STATUS_RIGHT)
766: key = KEYC_WHEELDOWN_STATUS_RIGHT;
767: if (where == STATUS_DEFAULT)
768: key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131 nicm 769: if (where == BORDER)
770: key = KEYC_WHEELDOWN_BORDER;
771: }
772: break;
773: case UP:
774: switch (MOUSE_BUTTONS(b)) {
775: case 0:
776: if (where == PANE)
777: key = KEYC_MOUSEUP1_PANE;
778: if (where == STATUS)
779: key = KEYC_MOUSEUP1_STATUS;
1.258 nicm 780: if (where == STATUS_LEFT)
781: key = KEYC_MOUSEUP1_STATUS_LEFT;
782: if (where == STATUS_RIGHT)
783: key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274 nicm 784: if (where == STATUS_DEFAULT)
785: key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131 nicm 786: if (where == BORDER)
787: key = KEYC_MOUSEUP1_BORDER;
788: break;
789: case 1:
790: if (where == PANE)
791: key = KEYC_MOUSEUP2_PANE;
792: if (where == STATUS)
793: key = KEYC_MOUSEUP2_STATUS;
1.258 nicm 794: if (where == STATUS_LEFT)
795: key = KEYC_MOUSEUP2_STATUS_LEFT;
796: if (where == STATUS_RIGHT)
797: key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274 nicm 798: if (where == STATUS_DEFAULT)
799: key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131 nicm 800: if (where == BORDER)
801: key = KEYC_MOUSEUP2_BORDER;
802: break;
803: case 2:
804: if (where == PANE)
805: key = KEYC_MOUSEUP3_PANE;
806: if (where == STATUS)
807: key = KEYC_MOUSEUP3_STATUS;
1.258 nicm 808: if (where == STATUS_LEFT)
809: key = KEYC_MOUSEUP3_STATUS_LEFT;
810: if (where == STATUS_RIGHT)
811: key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274 nicm 812: if (where == STATUS_DEFAULT)
813: key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131 nicm 814: if (where == BORDER)
815: key = KEYC_MOUSEUP3_BORDER;
816: break;
817: }
818: break;
819: case DOWN:
820: switch (MOUSE_BUTTONS(b)) {
821: case 0:
822: if (where == PANE)
823: key = KEYC_MOUSEDOWN1_PANE;
824: if (where == STATUS)
825: key = KEYC_MOUSEDOWN1_STATUS;
1.258 nicm 826: if (where == STATUS_LEFT)
827: key = KEYC_MOUSEDOWN1_STATUS_LEFT;
828: if (where == STATUS_RIGHT)
829: key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274 nicm 830: if (where == STATUS_DEFAULT)
831: key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131 nicm 832: if (where == BORDER)
833: key = KEYC_MOUSEDOWN1_BORDER;
834: break;
835: case 1:
836: if (where == PANE)
837: key = KEYC_MOUSEDOWN2_PANE;
838: if (where == STATUS)
839: key = KEYC_MOUSEDOWN2_STATUS;
1.258 nicm 840: if (where == STATUS_LEFT)
841: key = KEYC_MOUSEDOWN2_STATUS_LEFT;
842: if (where == STATUS_RIGHT)
843: key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274 nicm 844: if (where == STATUS_DEFAULT)
845: key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131 nicm 846: if (where == BORDER)
847: key = KEYC_MOUSEDOWN2_BORDER;
848: break;
849: case 2:
850: if (where == PANE)
851: key = KEYC_MOUSEDOWN3_PANE;
852: if (where == STATUS)
853: key = KEYC_MOUSEDOWN3_STATUS;
1.258 nicm 854: if (where == STATUS_LEFT)
855: key = KEYC_MOUSEDOWN3_STATUS_LEFT;
856: if (where == STATUS_RIGHT)
857: key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274 nicm 858: if (where == STATUS_DEFAULT)
859: key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131 nicm 860: if (where == BORDER)
861: key = KEYC_MOUSEDOWN3_BORDER;
862: break;
1.66 nicm 863: }
1.131 nicm 864: break;
1.192 nicm 865: case DOUBLE:
866: switch (MOUSE_BUTTONS(b)) {
867: case 0:
868: if (where == PANE)
869: key = KEYC_DOUBLECLICK1_PANE;
870: if (where == STATUS)
871: key = KEYC_DOUBLECLICK1_STATUS;
1.258 nicm 872: if (where == STATUS_LEFT)
873: key = KEYC_DOUBLECLICK1_STATUS_LEFT;
874: if (where == STATUS_RIGHT)
875: key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274 nicm 876: if (where == STATUS_DEFAULT)
877: key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192 nicm 878: if (where == BORDER)
879: key = KEYC_DOUBLECLICK1_BORDER;
880: break;
881: case 1:
882: if (where == PANE)
883: key = KEYC_DOUBLECLICK2_PANE;
884: if (where == STATUS)
885: key = KEYC_DOUBLECLICK2_STATUS;
1.258 nicm 886: if (where == STATUS_LEFT)
887: key = KEYC_DOUBLECLICK2_STATUS_LEFT;
888: if (where == STATUS_RIGHT)
889: key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274 nicm 890: if (where == STATUS_DEFAULT)
891: key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192 nicm 892: if (where == BORDER)
893: key = KEYC_DOUBLECLICK2_BORDER;
894: break;
895: case 2:
896: if (where == PANE)
897: key = KEYC_DOUBLECLICK3_PANE;
898: if (where == STATUS)
899: key = KEYC_DOUBLECLICK3_STATUS;
1.258 nicm 900: if (where == STATUS_LEFT)
901: key = KEYC_DOUBLECLICK3_STATUS_LEFT;
902: if (where == STATUS_RIGHT)
903: key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274 nicm 904: if (where == STATUS_DEFAULT)
905: key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192 nicm 906: if (where == BORDER)
907: key = KEYC_DOUBLECLICK3_BORDER;
908: break;
909: }
910: break;
911: case TRIPLE:
912: switch (MOUSE_BUTTONS(b)) {
913: case 0:
914: if (where == PANE)
915: key = KEYC_TRIPLECLICK1_PANE;
916: if (where == STATUS)
917: key = KEYC_TRIPLECLICK1_STATUS;
1.258 nicm 918: if (where == STATUS_LEFT)
919: key = KEYC_TRIPLECLICK1_STATUS_LEFT;
920: if (where == STATUS_RIGHT)
921: key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274 nicm 922: if (where == STATUS_DEFAULT)
923: key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192 nicm 924: if (where == BORDER)
925: key = KEYC_TRIPLECLICK1_BORDER;
926: break;
927: case 1:
928: if (where == PANE)
929: key = KEYC_TRIPLECLICK2_PANE;
930: if (where == STATUS)
931: key = KEYC_TRIPLECLICK2_STATUS;
1.258 nicm 932: if (where == STATUS_LEFT)
933: key = KEYC_TRIPLECLICK2_STATUS_LEFT;
934: if (where == STATUS_RIGHT)
935: key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274 nicm 936: if (where == STATUS_DEFAULT)
937: key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192 nicm 938: if (where == BORDER)
939: key = KEYC_TRIPLECLICK2_BORDER;
940: break;
941: case 2:
942: if (where == PANE)
943: key = KEYC_TRIPLECLICK3_PANE;
944: if (where == STATUS)
945: key = KEYC_TRIPLECLICK3_STATUS;
1.258 nicm 946: if (where == STATUS_LEFT)
947: key = KEYC_TRIPLECLICK3_STATUS_LEFT;
948: if (where == STATUS_RIGHT)
949: key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274 nicm 950: if (where == STATUS_DEFAULT)
951: key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192 nicm 952: if (where == BORDER)
953: key = KEYC_TRIPLECLICK3_BORDER;
954: break;
955: }
956: break;
1.66 nicm 957: }
1.177 nicm 958: if (key == KEYC_UNKNOWN)
959: return (KEYC_UNKNOWN);
1.66 nicm 960:
1.305 nicm 961: out:
1.131 nicm 962: /* Apply modifiers if any. */
963: if (b & MOUSE_MASK_META)
964: key |= KEYC_ESCAPE;
965: if (b & MOUSE_MASK_CTRL)
966: key |= KEYC_CTRL;
967: if (b & MOUSE_MASK_SHIFT)
968: key |= KEYC_SHIFT;
1.66 nicm 969:
1.305 nicm 970: if (log_get_level() != 0)
971: log_debug("mouse key is %s", key_string_lookup_key (key));
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.303 nicm 1040: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
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.306 ! nicm 1051: if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
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.305 nicm 1065: if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1.204 nicm 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. */
1.303 nicm 1227: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.280 nicm 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.296 nicm 1271: int focus, attached, resize;
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.296 nicm 1284: *
1285: * As an optimization, panes in windows that are in an attached session
1286: * but not the current window are not resized (this reduces the amount
1287: * of work needed when, for example, resizing an X terminal a
1288: * lot). Windows in no attached session are resized immediately since
1289: * that is likely to have come from a command like split-window and be
1290: * what the user wanted.
1.2 nicm 1291: */
1.197 nicm 1292: focus = options_get_number(global_options, "focus-events");
1.134 nicm 1293: RB_FOREACH(w, windows, &windows) {
1.296 nicm 1294: attached = resize = 0;
1.287 nicm 1295: TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1296: s = wl->session;
1.296 nicm 1297: if (s->attached != 0)
1298: attached = 1;
1299: if (s->attached != 0 && s->curw == wl) {
1300: resize = 1;
1.287 nicm 1301: break;
1.296 nicm 1302: }
1.287 nicm 1303: }
1.296 nicm 1304: if (!attached)
1305: resize = 1;
1.91 nicm 1306: TAILQ_FOREACH(wp, &w->panes, entry) {
1.289 nicm 1307: if (wp->fd != -1) {
1.197 nicm 1308: if (focus)
1309: server_client_check_focus(wp);
1.296 nicm 1310: if (resize)
1.289 nicm 1311: server_client_check_resize(wp);
1.101 nicm 1312: }
1.2 nicm 1313: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1314: }
1.150 nicm 1315: check_window_name(w);
1.2 nicm 1316: }
1.92 nicm 1317: }
1318:
1.237 nicm 1319: /* Check if we need to force a resize. */
1320: static int
1321: server_client_resize_force(struct window_pane *wp)
1322: {
1323: struct timeval tv = { .tv_usec = 100000 };
1324:
1325: /*
1326: * If we are resizing to the same size as when we entered the loop
1327: * (that is, to the same size the application currently thinks it is),
1328: * tmux may have gone through several resizes internally and thrown
1329: * away parts of the screen. So we need the application to actually
1330: * redraw even though its final size has not changed.
1331: */
1332:
1333: if (wp->flags & PANE_RESIZEFORCE) {
1334: wp->flags &= ~PANE_RESIZEFORCE;
1335: return (0);
1336: }
1337:
1338: if (wp->sx != wp->osx ||
1339: wp->sy != wp->osy ||
1340: wp->sx <= 1 ||
1341: wp->sy <= 1)
1342: return (0);
1343:
1344: log_debug("%s: %%%u forcing resize", __func__, wp->id);
1.297 nicm 1345: window_pane_send_resize(wp, -1);
1.237 nicm 1346:
1347: evtimer_add(&wp->resize_timer, &tv);
1348: wp->flags |= PANE_RESIZEFORCE;
1349: return (1);
1350: }
1351:
1.294 nicm 1352: /* Resize a pane. */
1.188 nicm 1353: static void
1.294 nicm 1354: server_client_resize_pane(struct window_pane *wp)
1.92 nicm 1355: {
1.235 nicm 1356: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, wp->sy);
1.297 nicm 1357: window_pane_send_resize(wp, 0);
1.92 nicm 1358:
1359: wp->flags &= ~PANE_RESIZE;
1.235 nicm 1360:
1361: wp->osx = wp->sx;
1362: wp->osy = wp->sy;
1.188 nicm 1363: }
1364:
1.294 nicm 1365: /* Start the resize timer. */
1366: static void
1367: server_client_start_resize_timer(struct window_pane *wp)
1368: {
1369: struct timeval tv = { .tv_usec = 250000 };
1370:
1371: if (!evtimer_pending(&wp->resize_timer, NULL))
1372: evtimer_add(&wp->resize_timer, &tv);
1373: }
1374:
1375: /* Resize timer event. */
1376: static void
1377: server_client_resize_event(__unused int fd, __unused short events, void *data)
1378: {
1379: struct window_pane *wp = data;
1380:
1381: evtimer_del(&wp->resize_timer);
1382:
1383: if (~wp->flags & PANE_RESIZE)
1384: return;
1385: log_debug("%s: %%%u timer fired (was%s resized)", __func__, wp->id,
1386: (wp->flags & PANE_RESIZED) ? "" : " not");
1387:
1388: if (wp->saved_grid == NULL && (wp->flags & PANE_RESIZED)) {
1389: log_debug("%s: %%%u deferring timer", __func__, wp->id);
1390: server_client_start_resize_timer(wp);
1391: } else if (!server_client_resize_force(wp)) {
1392: log_debug("%s: %%%u resizing pane", __func__, wp->id);
1393: server_client_resize_pane(wp);
1394: }
1395: wp->flags &= ~PANE_RESIZED;
1396: }
1397:
1.188 nicm 1398: /* Check if pane should be resized. */
1.190 nicm 1399: static void
1.188 nicm 1400: server_client_check_resize(struct window_pane *wp)
1401: {
1.294 nicm 1402: if (~wp->flags & PANE_RESIZE)
1.188 nicm 1403: return;
1404:
1405: if (!event_initialized(&wp->resize_timer))
1406: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1407:
1.294 nicm 1408: if (!evtimer_pending(&wp->resize_timer, NULL)) {
1409: log_debug("%s: %%%u starting timer", __func__, wp->id);
1410: server_client_resize_pane(wp);
1411: server_client_start_resize_timer(wp);
1412: } else
1413: log_debug("%s: %%%u timer running", __func__, wp->id);
1.91 nicm 1414: }
1415:
1416: /* Check whether pane should be focused. */
1.190 nicm 1417: static void
1.91 nicm 1418: server_client_check_focus(struct window_pane *wp)
1419: {
1.93 nicm 1420: struct client *c;
1.102 nicm 1421: int push;
1422:
1423: /* Do we need to push the focus state? */
1424: push = wp->flags & PANE_FOCUSPUSH;
1425: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1426:
1427: /* If we're not the active pane in our window, we're not focused. */
1428: if (wp->window->active != wp)
1429: goto not_focused;
1430:
1431: /* If we're in a mode, we're not focused. */
1432: if (wp->screen != &wp->base)
1433: goto not_focused;
1434:
1435: /*
1.93 nicm 1436: * If our window is the current window in any focused clients with an
1437: * attached session, we're focused.
1.91 nicm 1438: */
1.135 nicm 1439: TAILQ_FOREACH(c, &clients, entry) {
1440: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1441: continue;
1.255 nicm 1442: if (c->session->attached == 0)
1.93 nicm 1443: continue;
1444:
1445: if (c->session->curw->window == wp->window)
1.91 nicm 1446: goto focused;
1447: }
1448:
1449: not_focused:
1.251 nicm 1450: if (push || (wp->flags & PANE_FOCUSED)) {
1451: if (wp->base.mode & MODE_FOCUSON)
1452: bufferevent_write(wp->event, "\033[O", 3);
1453: notify_pane("pane-focus-out", wp);
1454: }
1.91 nicm 1455: wp->flags &= ~PANE_FOCUSED;
1456: return;
1457:
1458: focused:
1.251 nicm 1459: if (push || !(wp->flags & PANE_FOCUSED)) {
1460: if (wp->base.mode & MODE_FOCUSON)
1461: bufferevent_write(wp->event, "\033[I", 3);
1462: notify_pane("pane-focus-in", wp);
1.275 nicm 1463: session_update_activity(c->session, NULL);
1.251 nicm 1464: }
1.91 nicm 1465: wp->flags |= PANE_FOCUSED;
1.2 nicm 1466: }
1467:
1.18 nicm 1468: /*
1469: * Update cursor position and mode settings. The scroll region and attributes
1470: * are cleared when idle (waiting for an event) as this is the most likely time
1471: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1472: * compromise between excessive resets and likelihood of an interrupt.
1473: *
1474: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1475: * things that are already in their default state.
1476: */
1.190 nicm 1477: static void
1.18 nicm 1478: server_client_reset_state(struct client *c)
1.1 nicm 1479: {
1.18 nicm 1480: struct window *w = c->session->curw->window;
1.209 nicm 1481: struct window_pane *wp = w->active, *loop;
1.18 nicm 1482: struct screen *s = wp->screen;
1.163 nicm 1483: struct options *oo = c->session->options;
1.261 nicm 1484: int mode, cursor = 0;
1485: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1486:
1.186 nicm 1487: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1488: return;
1.282 nicm 1489: if (c->overlay_draw != NULL)
1490: return;
1.261 nicm 1491: mode = s->mode;
1.86 nicm 1492:
1.199 nicm 1493: tty_region_off(&c->tty);
1494: tty_margin_off(&c->tty);
1.1 nicm 1495:
1.261 nicm 1496: /* Move cursor to pane cursor and offset. */
1497: cursor = 0;
1498: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
1499: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1500: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1501: cursor = 1;
1502:
1503: cx = wp->xoff + s->cx - ox;
1504: cy = wp->yoff + s->cy - oy;
1505:
1506: if (status_at_line(c) == 0)
1507: cy += status_line_size(c);
1508: }
1509: if (!cursor)
1510: mode &= ~MODE_CURSOR;
1511: tty_cursor(&c->tty, cx, cy);
1.1 nicm 1512:
1.50 nicm 1513: /*
1.131 nicm 1514: * Set mouse mode if requested. To support dragging, always use button
1515: * mode.
1.57 nicm 1516: */
1.209 nicm 1517: if (options_get_number(oo, "mouse")) {
1518: mode &= ~ALL_MOUSE_MODES;
1519: TAILQ_FOREACH(loop, &w->panes, entry) {
1520: if (loop->screen->mode & MODE_MOUSE_ALL)
1521: mode |= MODE_MOUSE_ALL;
1522: }
1523: if (~mode & MODE_MOUSE_ALL)
1524: mode |= MODE_MOUSE_BUTTON;
1525: }
1.215 nicm 1526:
1527: /* Clear bracketed paste mode if at the prompt. */
1528: if (c->prompt_string != NULL)
1529: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1530:
1531: /* Set the terminal mode and reset attributes. */
1.59 nicm 1532: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1533: tty_reset(&c->tty);
1.17 nicm 1534: }
1535:
1536: /* Repeat time callback. */
1.190 nicm 1537: static void
1.170 nicm 1538: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1539: {
1540: struct client *c = data;
1541:
1.85 nicm 1542: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1543: server_client_set_key_table(c, NULL);
1.132 nicm 1544: c->flags &= ~CLIENT_REPEAT;
1545: server_status_client(c);
1.85 nicm 1546: }
1.192 nicm 1547: }
1548:
1549: /* Double-click callback. */
1550: static void
1551: server_client_click_timer(__unused int fd, __unused short events, void *data)
1552: {
1.306 ! nicm 1553: struct client *c = data;
! 1554: struct key_event *event;
! 1555:
! 1556: log_debug("click timer expired");
1.192 nicm 1557:
1.306 ! nicm 1558: if (c->flags & CLIENT_TRIPLECLICK) {
! 1559: /*
! 1560: * Waiting for a third click that hasn't happened, so this must
! 1561: * have been a double click.
! 1562: */
! 1563: event = xmalloc(sizeof *event);
! 1564: event->key = KEYC_DOUBLECLICK;
! 1565: memcpy(&event->m, &c->click_event, sizeof event->m);
! 1566: if (!server_client_handle_key(c, event))
! 1567: free(event);
! 1568: }
1.192 nicm 1569: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1570: }
1571:
1.36 nicm 1572: /* Check if client should be exited. */
1.190 nicm 1573: static void
1.36 nicm 1574: server_client_check_exit(struct client *c)
1575: {
1.300 nicm 1576: struct client_file *cf;
1577:
1.286 nicm 1578: if (~c->flags & CLIENT_EXIT)
1579: return;
1580: if (c->flags & CLIENT_EXITED)
1.36 nicm 1581: return;
1582:
1.300 nicm 1583: RB_FOREACH(cf, client_files, &c->files) {
1584: if (EVBUFFER_LENGTH(cf->buffer) != 0)
1585: return;
1586: }
1.36 nicm 1587:
1.249 nicm 1588: if (c->flags & CLIENT_ATTACHED)
1589: notify_client("client-detached", c);
1.162 nicm 1590: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.286 nicm 1591: c->flags |= CLIENT_EXITED;
1.38 nicm 1592: }
1593:
1.218 nicm 1594: /* Redraw timer callback. */
1595: static void
1596: server_client_redraw_timer(__unused int fd, __unused short events,
1.299 nicm 1597: __unused void *data)
1.218 nicm 1598: {
1599: log_debug("redraw timer fired");
1600: }
1601:
1.1 nicm 1602: /* Check for client redraws. */
1.190 nicm 1603: static void
1.1 nicm 1604: server_client_check_redraw(struct client *c)
1605: {
1606: struct session *s = c->session;
1.137 nicm 1607: struct tty *tty = &c->tty;
1.1 nicm 1608: struct window_pane *wp;
1.256 nicm 1609: int needed, flags;
1.218 nicm 1610: struct timeval tv = { .tv_usec = 1000 };
1611: static struct event ev;
1612: size_t left;
1.1 nicm 1613:
1.86 nicm 1614: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1615: return;
1.257 nicm 1616: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.282 nicm 1617: log_debug("%s: redraw%s%s%s%s", c->name,
1.257 nicm 1618: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1619: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282 nicm 1620: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
1621: (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "");
1.257 nicm 1622: }
1.218 nicm 1623:
1624: /*
1625: * If there is outstanding data, defer the redraw until it has been
1626: * consumed. We can just add a timer to get out of the event loop and
1627: * end up back here.
1628: */
1629: needed = 0;
1.256 nicm 1630: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1631: needed = 1;
1632: else {
1633: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1634: if (wp->flags & PANE_REDRAW) {
1635: needed = 1;
1636: break;
1637: }
1638: }
1639: }
1.241 nicm 1640: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1641: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1642: if (!evtimer_initialized(&ev))
1643: evtimer_set(&ev, server_client_redraw_timer, NULL);
1644: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1645: log_debug("redraw timer started");
1646: evtimer_add(&ev, &tv);
1.241 nicm 1647: }
1.218 nicm 1648:
1.241 nicm 1649: /*
1650: * We may have got here for a single pane redraw, but force a
1651: * full redraw next time in case other panes have been updated.
1652: */
1.256 nicm 1653: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.241 nicm 1654: return;
1655: } else if (needed)
1.218 nicm 1656: log_debug("%s: redraw needed", c->name);
1.79 nicm 1657:
1.219 nicm 1658: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1659: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE)) | TTY_NOCURSOR;
1.137 nicm 1660:
1.256 nicm 1661: if (~c->flags & CLIENT_REDRAWWINDOW) {
1662: /*
1663: * If not redrawing the entire window, check whether each pane
1664: * needs to be redrawn.
1665: */
1.1 nicm 1666: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1667: if (wp->flags & PANE_REDRAW) {
1668: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1669: screen_redraw_pane(c, wp);
1.137 nicm 1670: }
1.1 nicm 1671: }
1672: }
1673:
1.256 nicm 1674: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1675: if (options_get_number(s->options, "set-titles"))
1676: server_client_set_title(c);
1677: screen_redraw_screen(c);
1678: }
1.1 nicm 1679:
1.137 nicm 1680: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1681: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1682:
1.256 nicm 1683: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1684:
1685: if (needed) {
1686: /*
1687: * We would have deferred the redraw unless the output buffer
1688: * was empty, so we can record how many bytes the redraw
1689: * generated.
1690: */
1691: c->redraw = EVBUFFER_LENGTH(tty->out);
1692: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1693: }
1.1 nicm 1694: }
1695:
1696: /* Set client title. */
1.190 nicm 1697: static void
1.1 nicm 1698: server_client_set_title(struct client *c)
1699: {
1.128 nicm 1700: struct session *s = c->session;
1701: const char *template;
1702: char *title;
1703: struct format_tree *ft;
1.1 nicm 1704:
1.163 nicm 1705: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1706:
1.228 nicm 1707: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1708: format_defaults(ft, c, NULL, NULL, NULL);
1709:
1.269 nicm 1710: title = format_expand_time(ft, template);
1.1 nicm 1711: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1712: free(c->title);
1.1 nicm 1713: c->title = xstrdup(title);
1714: tty_set_title(&c->tty, c->title);
1715: }
1.77 nicm 1716: free(title);
1.128 nicm 1717:
1718: format_free(ft);
1.1 nicm 1719: }
1720:
1721: /* Dispatch message from client. */
1.190 nicm 1722: static void
1.162 nicm 1723: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1724: {
1.300 nicm 1725: struct client *c = arg;
1726: ssize_t datalen;
1727: struct session *s;
1.1 nicm 1728:
1.162 nicm 1729: if (c->flags & CLIENT_DEAD)
1730: return;
1731:
1732: if (imsg == NULL) {
1733: server_client_lost(c);
1734: return;
1735: }
1.1 nicm 1736:
1.162 nicm 1737: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1738:
1.162 nicm 1739: switch (imsg->hdr.type) {
1740: case MSG_IDENTIFY_FLAGS:
1741: case MSG_IDENTIFY_TERM:
1742: case MSG_IDENTIFY_TTYNAME:
1743: case MSG_IDENTIFY_CWD:
1744: case MSG_IDENTIFY_STDIN:
1745: case MSG_IDENTIFY_ENVIRON:
1746: case MSG_IDENTIFY_CLIENTPID:
1747: case MSG_IDENTIFY_DONE:
1748: server_client_dispatch_identify(c, imsg);
1749: break;
1750: case MSG_COMMAND:
1751: server_client_dispatch_command(c, imsg);
1752: break;
1753: case MSG_RESIZE:
1754: if (datalen != 0)
1755: fatalx("bad MSG_RESIZE size");
1.1 nicm 1756:
1.162 nicm 1757: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1758: break;
1.295 nicm 1759: server_client_update_latest(c);
1.282 nicm 1760: server_client_clear_overlay(c);
1.236 nicm 1761: tty_resize(&c->tty);
1762: recalculate_sizes();
1763: server_redraw_client(c);
1.174 nicm 1764: if (c->session != NULL)
1.196 nicm 1765: notify_client("client-resized", c);
1.162 nicm 1766: break;
1767: case MSG_EXITING:
1768: if (datalen != 0)
1769: fatalx("bad MSG_EXITING size");
1.1 nicm 1770:
1.162 nicm 1771: c->session = NULL;
1772: tty_close(&c->tty);
1773: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1774: break;
1775: case MSG_WAKEUP:
1776: case MSG_UNLOCK:
1777: if (datalen != 0)
1778: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1779:
1.162 nicm 1780: if (!(c->flags & CLIENT_SUSPENDED))
1781: break;
1782: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1783:
1.162 nicm 1784: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1785: break;
1.162 nicm 1786: s = c->session;
1.1 nicm 1787:
1.162 nicm 1788: if (gettimeofday(&c->activity_time, NULL) != 0)
1789: fatal("gettimeofday failed");
1790:
1791: tty_start_tty(&c->tty);
1792: server_redraw_client(c);
1793: recalculate_sizes();
1.184 nicm 1794:
1795: if (s != NULL)
1796: session_update_activity(s, &c->activity_time);
1.162 nicm 1797: break;
1798: case MSG_SHELL:
1799: if (datalen != 0)
1800: fatalx("bad MSG_SHELL size");
1.1 nicm 1801:
1.162 nicm 1802: server_client_dispatch_shell(c);
1803: break;
1.300 nicm 1804: case MSG_WRITE_READY:
1805: server_client_dispatch_write_ready(c, imsg);
1806: break;
1807: case MSG_READ:
1808: server_client_dispatch_read_data(c, imsg);
1809: break;
1810: case MSG_READ_DONE:
1811: server_client_dispatch_read_done(c, imsg);
1812: break;
1.1 nicm 1813: }
1814: }
1815:
1.194 nicm 1816: /* Callback when command is done. */
1817: static enum cmd_retval
1.195 nicm 1818: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1819: {
1.195 nicm 1820: struct client *c = item->client;
1.194 nicm 1821:
1822: if (~c->flags & CLIENT_ATTACHED)
1823: c->flags |= CLIENT_EXIT;
1824: return (CMD_RETURN_NORMAL);
1825: }
1826:
1.1 nicm 1827: /* Handle command message. */
1.190 nicm 1828: static void
1.162 nicm 1829: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1830: {
1.300 nicm 1831: struct msg_command data;
1.108 nicm 1832: char *buf;
1833: size_t len;
1834: int argc;
1835: char **argv, *cause;
1.285 nicm 1836: struct cmd_parse_result *pr;
1.246 nicm 1837:
1838: if (c->flags & CLIENT_EXIT)
1839: return;
1.108 nicm 1840:
1841: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1842: fatalx("bad MSG_COMMAND size");
1843: memcpy(&data, imsg->data, sizeof data);
1844:
1.124 nicm 1845: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1846: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1847: if (len > 0 && buf[len - 1] != '\0')
1848: fatalx("bad MSG_COMMAND string");
1849:
1850: argc = data.argc;
1851: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1852: cause = xstrdup("command too long");
1.1 nicm 1853: goto error;
1854: }
1855:
1856: if (argc == 0) {
1857: argc = 1;
1858: argv = xcalloc(1, sizeof *argv);
1859: *argv = xstrdup("new-session");
1860: }
1861:
1.285 nicm 1862: pr = cmd_parse_from_arguments(argc, argv, NULL);
1863: switch (pr->status) {
1864: case CMD_PARSE_EMPTY:
1865: cause = xstrdup("empty command");
1.1 nicm 1866: goto error;
1.285 nicm 1867: case CMD_PARSE_ERROR:
1868: cause = pr->error;
1869: goto error;
1870: case CMD_PARSE_SUCCESS:
1871: break;
1.1 nicm 1872: }
1873: cmd_free_argv(argc, argv);
1874:
1.285 nicm 1875: cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL, NULL, 0));
1.194 nicm 1876: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.285 nicm 1877:
1878: cmd_list_free(pr->cmdlist);
1.1 nicm 1879: return;
1880:
1881: error:
1.285 nicm 1882: cmd_free_argv(argc, argv);
1883:
1.284 nicm 1884: cmdq_append(c, cmdq_get_error(cause));
1885: free(cause);
1.88 nicm 1886:
1.36 nicm 1887: c->flags |= CLIENT_EXIT;
1.1 nicm 1888: }
1889:
1890: /* Handle identify message. */
1.190 nicm 1891: static void
1.162 nicm 1892: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1893: {
1.165 nicm 1894: const char *data, *home;
1.232 nicm 1895: size_t datalen;
1.109 nicm 1896: int flags;
1.216 nicm 1897: char *name;
1.109 nicm 1898:
1899: if (c->flags & CLIENT_IDENTIFIED)
1900: fatalx("out-of-order identify message");
1901:
1902: data = imsg->data;
1903: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1904:
1905: switch (imsg->hdr.type) {
1906: case MSG_IDENTIFY_FLAGS:
1907: if (datalen != sizeof flags)
1908: fatalx("bad MSG_IDENTIFY_FLAGS size");
1909: memcpy(&flags, data, sizeof flags);
1910: c->flags |= flags;
1.158 nicm 1911: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1912: break;
1913: case MSG_IDENTIFY_TERM:
1.110 nicm 1914: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1915: fatalx("bad MSG_IDENTIFY_TERM string");
1916: c->term = xstrdup(data);
1.158 nicm 1917: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1918: break;
1919: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1920: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1921: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1922: c->ttyname = xstrdup(data);
1.158 nicm 1923: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1924: break;
1925: case MSG_IDENTIFY_CWD:
1.155 nicm 1926: if (datalen == 0 || data[datalen - 1] != '\0')
1927: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1928: if (access(data, X_OK) == 0)
1929: c->cwd = xstrdup(data);
1930: else if ((home = find_home()) != NULL)
1931: c->cwd = xstrdup(home);
1932: else
1933: c->cwd = xstrdup("/");
1.158 nicm 1934: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1935: break;
1936: case MSG_IDENTIFY_STDIN:
1937: if (datalen != 0)
1938: fatalx("bad MSG_IDENTIFY_STDIN size");
1939: c->fd = imsg->fd;
1.158 nicm 1940: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1941: break;
1942: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1943: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1944: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1945: if (strchr(data, '=') != NULL)
1.164 nicm 1946: environ_put(c->environ, data);
1.158 nicm 1947: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1948: break;
1949: case MSG_IDENTIFY_CLIENTPID:
1950: if (datalen != sizeof c->pid)
1951: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1952: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1953: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1954: break;
1955: default:
1956: break;
1957: }
1958:
1959: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1960: return;
1961: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1962:
1.216 nicm 1963: if (*c->ttyname != '\0')
1964: name = xstrdup(c->ttyname);
1965: else
1966: xasprintf(&name, "client-%ld", (long)c->pid);
1967: c->name = name;
1968: log_debug("client %p name is %s", c, c->name);
1969:
1.109 nicm 1970: if (c->flags & CLIENT_CONTROL) {
1.300 nicm 1971: close(c->fd);
1972: c->fd = -1;
1.75 nicm 1973:
1.300 nicm 1974: control_start(c);
1.75 nicm 1975: c->tty.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:
1.300 nicm 2015: /* Handle write ready message. */
1.169 nicm 2016: static void
1.300 nicm 2017: server_client_dispatch_write_ready(struct client *c, struct imsg *imsg)
1.169 nicm 2018: {
1.300 nicm 2019: struct msg_write_ready *msg = imsg->data;
2020: size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
2021: struct client_file find, *cf;
2022:
2023: if (msglen != sizeof *msg)
2024: fatalx("bad MSG_WRITE_READY size");
2025: find.stream = msg->stream;
2026: if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL)
2027: return;
2028: if (msg->error != 0) {
2029: cf->error = msg->error;
2030: file_fire_done(cf);
2031: } else
2032: file_push(cf);
1.169 nicm 2033: }
2034:
1.300 nicm 2035: /* Handle read data message. */
2036: static void
2037: server_client_dispatch_read_data(struct client *c, struct imsg *imsg)
1.169 nicm 2038: {
1.300 nicm 2039: struct msg_read_data *msg = imsg->data;
2040: size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
2041: struct client_file find, *cf;
1.301 nicm 2042: void *bdata = msg + 1;
2043: size_t bsize = msglen - sizeof *msg;
1.169 nicm 2044:
1.301 nicm 2045: if (msglen < sizeof *msg)
1.300 nicm 2046: fatalx("bad MSG_READ_DATA size");
2047: find.stream = msg->stream;
2048: if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL)
2049: return;
1.169 nicm 2050:
1.300 nicm 2051: log_debug("%s: file %d read %zu bytes", c->name, cf->stream, bsize);
2052: if (cf->error == 0) {
2053: if (evbuffer_add(cf->buffer, bdata, bsize) != 0) {
2054: cf->error = ENOMEM;
2055: file_fire_done(cf);
2056: } else
2057: file_fire_read(cf);
1.169 nicm 2058: }
2059: }
2060:
1.300 nicm 2061: /* Handle read done message. */
1.169 nicm 2062: static void
1.300 nicm 2063: server_client_dispatch_read_done(struct client *c, struct imsg *imsg)
1.169 nicm 2064: {
1.300 nicm 2065: struct msg_read_done *msg = imsg->data;
2066: size_t msglen = imsg->hdr.len - IMSG_HEADER_SIZE;
2067: struct client_file find, *cf;
1.169 nicm 2068:
1.300 nicm 2069: if (msglen != sizeof *msg)
2070: fatalx("bad MSG_READ_DONE size");
2071: find.stream = msg->stream;
2072: if ((cf = RB_FIND(client_files, &c->files, &find)) == NULL)
1.169 nicm 2073: return;
2074:
1.300 nicm 2075: log_debug("%s: file %d read done", c->name, cf->stream);
2076: cf->error = msg->error;
2077: file_fire_done(cf);
1.212 nicm 2078: }
2079:
2080: /* Add to client message log. */
2081: void
2082: server_client_add_message(struct client *c, const char *fmt, ...)
2083: {
2084: struct message_entry *msg, *msg1;
2085: char *s;
2086: va_list ap;
2087: u_int limit;
2088:
2089: va_start(ap, fmt);
2090: xvasprintf(&s, fmt, ap);
2091: va_end(ap);
2092:
1.216 nicm 2093: log_debug("message %s (client %p)", s, c);
1.212 nicm 2094:
2095: msg = xcalloc(1, sizeof *msg);
2096: msg->msg_time = time(NULL);
2097: msg->msg_num = c->message_next++;
2098: msg->msg = s;
2099: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
2100:
2101: limit = options_get_number(global_options, "message-limit");
2102: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
2103: if (msg->msg_num + limit >= c->message_next)
2104: break;
2105: free(msg->msg);
2106: TAILQ_REMOVE(&c->message_log, msg, entry);
2107: free(msg);
1.169 nicm 2108: }
1.213 nicm 2109: }
2110:
2111: /* Get client working directory. */
2112: const char *
1.250 nicm 2113: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2114: {
1.250 nicm 2115: const char *home;
1.213 nicm 2116:
1.265 nicm 2117: if (!cfg_finished && cfg_client != NULL)
2118: return (cfg_client->cwd);
1.213 nicm 2119: if (c != NULL && c->session == NULL && c->cwd != NULL)
2120: return (c->cwd);
1.250 nicm 2121: if (s != NULL && s->cwd != NULL)
2122: return (s->cwd);
1.213 nicm 2123: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2124: return (s->cwd);
1.250 nicm 2125: if ((home = find_home()) != NULL)
2126: return (home);
2127: return ("/");
1.1 nicm 2128: }