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