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