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