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