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