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