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