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