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