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