Annotation of src/usr.bin/tmux/server-client.c, Revision 1.218
1.218 ! nicm 1: /* $OpenBSD: server-client.c,v 1.217 2017/04/17 06:40:32 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 *);
38: static key_code server_client_check_mouse(struct client *);
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 *);
46:
47: static void server_client_dispatch(struct imsg *, void *);
48: static void server_client_dispatch_command(struct client *, struct imsg *);
49: static void server_client_dispatch_identify(struct client *, struct imsg *);
50: static void server_client_dispatch_shell(struct client *);
1.140 nicm 51:
1.214 nicm 52: /* Idenfity mode callback. */
53: static void
54: server_client_callback_identify(__unused int fd, __unused short events, void *data)
55: {
56: server_client_clear_identify(data, NULL);
57: }
58:
59: /* Set identify mode on client. */
60: void
61: server_client_set_identify(struct client *c)
62: {
63: struct timeval tv;
64: int delay;
65:
66: delay = options_get_number(c->session->options, "display-panes-time");
67: tv.tv_sec = delay / 1000;
68: tv.tv_usec = (delay % 1000) * 1000L;
69:
70: if (event_initialized(&c->identify_timer))
71: evtimer_del(&c->identify_timer);
72: evtimer_set(&c->identify_timer, server_client_callback_identify, c);
73: evtimer_add(&c->identify_timer, &tv);
74:
75: c->flags |= CLIENT_IDENTIFY;
76: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
77: server_redraw_client(c);
78: }
79:
80: /* Clear identify mode on client. */
81: void
82: server_client_clear_identify(struct client *c, struct window_pane *wp)
83: {
84: if (~c->flags & CLIENT_IDENTIFY)
85: return;
86: c->flags &= ~CLIENT_IDENTIFY;
87:
88: if (c->identify_callback != NULL)
89: c->identify_callback(c, wp);
90:
91: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
92: server_redraw_client(c);
93: }
94:
1.140 nicm 95: /* Check if this client is inside this server. */
96: int
97: server_client_check_nested(struct client *c)
98: {
99: struct environ_entry *envent;
100: struct window_pane *wp;
101:
1.164 nicm 102: envent = environ_find(c->environ, "TMUX");
1.140 nicm 103: if (envent == NULL || *envent->value == '\0')
104: return (0);
105:
106: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 107: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 108: return (1);
109: }
110: return (0);
111: }
1.1 nicm 112:
1.132 nicm 113: /* Set client key table. */
114: void
1.178 nicm 115: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 116: {
1.178 nicm 117: if (name == NULL)
118: name = server_client_get_key_table(c);
119:
1.132 nicm 120: key_bindings_unref_table(c->keytable);
121: c->keytable = key_bindings_get_table(name, 1);
122: c->keytable->references++;
123: }
124:
1.178 nicm 125: /* Get default key table. */
126: const char *
127: server_client_get_key_table(struct client *c)
128: {
129: struct session *s = c->session;
130: const char *name;
131:
132: if (s == NULL)
133: return ("root");
134:
135: name = options_get_string(s->options, "key-table");
136: if (*name == '\0')
137: return ("root");
138: return (name);
139: }
140:
1.191 nicm 141: /* Is this client using the default key table? */
142: int
143: server_client_is_default_key_table(struct client *c)
144: {
145: return (strcmp(c->keytable->name, server_client_get_key_table(c)) == 0);
146: }
147:
1.1 nicm 148: /* Create a new client. */
149: void
150: server_client_create(int fd)
151: {
152: struct client *c;
153:
1.49 nicm 154: setblocking(fd, 0);
1.1 nicm 155:
156: c = xcalloc(1, sizeof *c);
1.141 nicm 157: c->references = 1;
1.162 nicm 158: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 159:
1.10 nicm 160: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 161: fatal("gettimeofday failed");
1.11 nicm 162: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 163:
1.164 nicm 164: c->environ = environ_create();
1.1 nicm 165:
1.146 nicm 166: c->fd = -1;
1.165 nicm 167: c->cwd = NULL;
1.145 nicm 168:
1.194 nicm 169: TAILQ_INIT(&c->queue);
1.94 nicm 170:
1.116 nicm 171: c->stdin_data = evbuffer_new();
172: c->stdout_data = evbuffer_new();
173: c->stderr_data = evbuffer_new();
1.33 nicm 174:
1.1 nicm 175: c->tty.fd = -1;
176: c->title = NULL;
177:
178: c->session = NULL;
1.45 nicm 179: c->last_session = NULL;
1.1 nicm 180: c->tty.sx = 80;
181: c->tty.sy = 24;
182:
183: screen_init(&c->status, c->tty.sx, 1, 0);
184:
185: c->message_string = NULL;
1.136 nicm 186: TAILQ_INIT(&c->message_log);
1.1 nicm 187:
188: c->prompt_string = NULL;
189: c->prompt_buffer = NULL;
190: c->prompt_index = 0;
191:
1.93 nicm 192: c->flags |= CLIENT_FOCUSED;
193:
1.132 nicm 194: c->keytable = key_bindings_get_table("root", 1);
195: c->keytable->references++;
196:
1.17 nicm 197: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 198: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 199:
1.135 nicm 200: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 201: log_debug("new client %p", c);
1.72 nicm 202: }
203:
204: /* Open client terminal if needed. */
205: int
1.119 nicm 206: server_client_open(struct client *c, char **cause)
1.72 nicm 207: {
1.75 nicm 208: if (c->flags & CLIENT_CONTROL)
209: return (0);
1.120 nicm 210:
211: if (strcmp(c->ttyname, "/dev/tty") == 0) {
212: *cause = xstrdup("can't use /dev/tty");
213: return (-1);
214: }
1.75 nicm 215:
1.72 nicm 216: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 217: *cause = xstrdup("not a terminal");
1.72 nicm 218: return (-1);
219: }
220:
1.119 nicm 221: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 222: return (-1);
223:
224: return (0);
1.1 nicm 225: }
226:
227: /* Lost a client. */
228: void
229: server_client_lost(struct client *c)
230: {
1.136 nicm 231: struct message_entry *msg, *msg1;
1.1 nicm 232:
1.141 nicm 233: c->flags |= CLIENT_DEAD;
234:
1.214 nicm 235: server_client_clear_identify(c, NULL);
1.141 nicm 236: status_prompt_clear(c);
237: status_message_clear(c);
238:
239: if (c->stdin_callback != NULL)
240: c->stdin_callback(c, 1, c->stdin_callback_data);
241:
1.135 nicm 242: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 243: log_debug("lost client %p", c);
1.1 nicm 244:
245: /*
246: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
247: * and tty_free might close an unrelated fd.
248: */
249: if (c->flags & CLIENT_TERMINAL)
250: tty_free(&c->tty);
1.109 nicm 251: free(c->ttyname);
252: free(c->term);
1.1 nicm 253:
1.113 nicm 254: evbuffer_free(c->stdin_data);
255: evbuffer_free(c->stdout_data);
1.97 nicm 256: if (c->stderr_data != c->stdout_data)
1.126 nicm 257: evbuffer_free(c->stderr_data);
1.33 nicm 258:
1.148 nicm 259: if (event_initialized(&c->status_timer))
260: evtimer_del(&c->status_timer);
1.1 nicm 261: screen_free(&c->status);
262:
1.77 nicm 263: free(c->title);
1.165 nicm 264: free((void *)c->cwd);
1.1 nicm 265:
1.17 nicm 266: evtimer_del(&c->repeat_timer);
1.192 nicm 267: evtimer_del(&c->click_timer);
1.17 nicm 268:
1.132 nicm 269: key_bindings_unref_table(c->keytable);
270:
1.69 nicm 271: if (event_initialized(&c->identify_timer))
272: evtimer_del(&c->identify_timer);
1.15 nicm 273:
1.77 nicm 274: free(c->message_string);
1.116 nicm 275: if (event_initialized(&c->message_timer))
1.69 nicm 276: evtimer_del(&c->message_timer);
1.136 nicm 277: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1.77 nicm 278: free(msg->msg);
1.136 nicm 279: TAILQ_REMOVE(&c->message_log, msg, entry);
280: free(msg);
1.21 nicm 281: }
1.1 nicm 282:
1.77 nicm 283: free(c->prompt_string);
284: free(c->prompt_buffer);
1.61 nicm 285:
1.164 nicm 286: environ_free(c->environ);
1.1 nicm 287:
1.162 nicm 288: proc_remove_peer(c->peer);
289: c->peer = NULL;
1.13 nicm 290:
1.142 nicm 291: server_client_unref(c);
1.71 nicm 292:
293: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 294:
295: recalculate_sizes();
1.41 nicm 296: server_check_unattached();
1.19 nicm 297: server_update_socket();
1.141 nicm 298: }
299:
300: /* Remove reference from a client. */
301: void
1.142 nicm 302: server_client_unref(struct client *c)
1.141 nicm 303: {
1.157 nicm 304: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 305:
306: c->references--;
307: if (c->references == 0)
308: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
309: }
310:
311: /* Free dead client. */
1.190 nicm 312: static void
1.170 nicm 313: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 314: {
315: struct client *c = arg;
316:
1.157 nicm 317: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 318:
1.194 nicm 319: if (!TAILQ_EMPTY(&c->queue))
320: fatalx("queue not empty");
321:
1.216 nicm 322: if (c->references == 0) {
323: free((void *)c->name);
1.141 nicm 324: free(c);
1.216 nicm 325: }
1.9 nicm 326: }
327:
1.174 nicm 328: /* Detach a client. */
329: void
330: server_client_detach(struct client *c, enum msgtype msgtype)
331: {
332: struct session *s = c->session;
333:
334: if (s == NULL)
335: return;
336:
1.196 nicm 337: notify_client("client-detached", c);
1.174 nicm 338: proc_send_s(c->peer, msgtype, s->name);
1.207 nicm 339: }
340:
1.208 nicm 341: /* Execute command to replace a client. */
1.207 nicm 342: void
343: server_client_exec(struct client *c, const char *cmd)
344: {
345: struct session *s = c->session;
1.208 nicm 346: char *msg;
347: const char *shell;
1.207 nicm 348: size_t cmdsize, shellsize;
349:
350: if (*cmd == '\0')
351: return;
352: cmdsize = strlen(cmd) + 1;
353:
354: if (s != NULL)
355: shell = options_get_string(s->options, "default-shell");
356: else
357: shell = options_get_string(global_s_options, "default-shell");
358: shellsize = strlen(shell) + 1;
359:
360: msg = xmalloc(cmdsize + shellsize);
361: memcpy(msg, cmd, cmdsize);
362: memcpy(msg + cmdsize, shell, shellsize);
363:
364: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
365: free(msg);
1.174 nicm 366: }
367:
1.66 nicm 368: /* Check for mouse keys. */
1.190 nicm 369: static key_code
1.131 nicm 370: server_client_check_mouse(struct client *c)
1.66 nicm 371: {
1.192 nicm 372: struct session *s = c->session;
373: struct mouse_event *m = &c->tty.mouse;
374: struct window *w;
375: struct window_pane *wp;
376: u_int x, y, b;
377: int flag;
378: key_code key;
379: struct timeval tv;
1.209 nicm 380: enum { NOTYPE, MOVE, DOWN, UP, DRAG, WHEEL, DOUBLE, TRIPLE } type;
381: enum { NOWHERE, PANE, STATUS, BORDER } where;
382:
383: type = NOTYPE;
384: where = NOWHERE;
1.131 nicm 385:
386: log_debug("mouse %02x at %u,%u (last %u,%u) (%d)", m->b, m->x, m->y,
387: m->lx, m->ly, c->tty.mouse_drag_flag);
388:
389: /* What type of event is this? */
1.209 nicm 390: if ((m->sgr_type != ' ' &&
391: MOUSE_DRAG(m->sgr_b) &&
392: MOUSE_BUTTONS(m->sgr_b) == 3) ||
393: (m->sgr_type == ' ' &&
394: MOUSE_DRAG(m->b) &&
395: MOUSE_BUTTONS(m->b) == 3 &&
396: MOUSE_BUTTONS(m->lb) == 3)) {
397: type = MOVE;
398: x = m->x, y = m->y, b = 0;
399: log_debug("move at %u,%u", x, y);
400: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 401: type = DRAG;
402: if (c->tty.mouse_drag_flag) {
403: x = m->x, y = m->y, b = m->b;
404: log_debug("drag update at %u,%u", x, y);
405: } else {
406: x = m->lx, y = m->ly, b = m->lb;
407: log_debug("drag start at %u,%u", x, y);
408: }
409: } else if (MOUSE_WHEEL(m->b)) {
410: type = WHEEL;
411: x = m->x, y = m->y, b = m->b;
412: log_debug("wheel at %u,%u", x, y);
1.200 nicm 413: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 414: type = UP;
415: x = m->x, y = m->y, b = m->lb;
416: log_debug("up at %u,%u", x, y);
417: } else {
1.192 nicm 418: if (c->flags & CLIENT_DOUBLECLICK) {
419: evtimer_del(&c->click_timer);
420: c->flags &= ~CLIENT_DOUBLECLICK;
421: if (m->b == c->click_button) {
422: type = DOUBLE;
423: x = m->x, y = m->y, b = m->b;
424: log_debug("double-click at %u,%u", x, y);
425: flag = CLIENT_TRIPLECLICK;
426: goto add_timer;
427: }
428: } else if (c->flags & CLIENT_TRIPLECLICK) {
429: evtimer_del(&c->click_timer);
430: c->flags &= ~CLIENT_TRIPLECLICK;
431: if (m->b == c->click_button) {
432: type = TRIPLE;
433: x = m->x, y = m->y, b = m->b;
434: log_debug("triple-click at %u,%u", x, y);
435: goto have_event;
436: }
437: }
438:
1.131 nicm 439: type = DOWN;
440: x = m->x, y = m->y, b = m->b;
441: log_debug("down at %u,%u", x, y);
1.192 nicm 442: flag = CLIENT_DOUBLECLICK;
443:
444: add_timer:
445: if (KEYC_CLICK_TIMEOUT != 0) {
446: c->flags |= flag;
447: c->click_button = m->b;
448:
449: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
450: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
451: evtimer_del(&c->click_timer);
452: evtimer_add(&c->click_timer, &tv);
453: }
1.131 nicm 454: }
1.192 nicm 455:
456: have_event:
1.131 nicm 457: if (type == NOTYPE)
1.177 nicm 458: return (KEYC_UNKNOWN);
1.131 nicm 459:
460: /* Always save the session. */
461: m->s = s->id;
462:
463: /* Is this on the status line? */
464: m->statusat = status_at_line(c);
465: if (m->statusat != -1 && y == (u_int)m->statusat) {
466: w = status_get_window_at(c, x);
467: if (w == NULL)
1.177 nicm 468: return (KEYC_UNKNOWN);
1.131 nicm 469: m->w = w->id;
470: where = STATUS;
471: } else
472: m->w = -1;
473:
474: /* Not on status line. Adjust position and check for border or pane. */
475: if (where == NOWHERE) {
476: if (m->statusat == 0 && y > 0)
477: y--;
478: else if (m->statusat > 0 && y >= (u_int)m->statusat)
479: y = m->statusat - 1;
480:
481: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
482: if ((wp->xoff + wp->sx == x &&
483: wp->yoff <= 1 + y &&
484: wp->yoff + wp->sy >= y) ||
485: (wp->yoff + wp->sy == y &&
486: wp->xoff <= 1 + x &&
487: wp->xoff + wp->sx >= x))
488: break;
489: }
490: if (wp != NULL)
491: where = BORDER;
492: else {
493: wp = window_get_active_at(s->curw->window, x, y);
1.173 nicm 494: if (wp != NULL) {
1.131 nicm 495: where = PANE;
1.173 nicm 496: log_debug("mouse at %u,%u is on pane %%%u",
497: x, y, wp->id);
498: }
1.131 nicm 499: }
500: if (where == NOWHERE)
1.177 nicm 501: return (KEYC_UNKNOWN);
1.131 nicm 502: m->wp = wp->id;
503: m->w = wp->window->id;
504: } else
505: m->wp = -1;
506:
507: /* Stop dragging if needed. */
1.200 nicm 508: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 509: if (c->tty.mouse_drag_release != NULL)
510: c->tty.mouse_drag_release(c, m);
511:
512: c->tty.mouse_drag_update = NULL;
513: c->tty.mouse_drag_release = NULL;
514:
1.182 nicm 515: /*
1.183 nicm 516: * End a mouse drag by passing a MouseDragEnd key corresponding
517: * to the button that started the drag.
1.182 nicm 518: */
519: switch (c->tty.mouse_drag_flag) {
520: case 1:
521: if (where == PANE)
1.183 nicm 522: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 523: if (where == STATUS)
1.183 nicm 524: key = KEYC_MOUSEDRAGEND1_STATUS;
1.182 nicm 525: if (where == BORDER)
1.183 nicm 526: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 527: break;
528: case 2:
529: if (where == PANE)
1.183 nicm 530: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 531: if (where == STATUS)
1.183 nicm 532: key = KEYC_MOUSEDRAGEND2_STATUS;
1.182 nicm 533: if (where == BORDER)
1.183 nicm 534: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 535: break;
536: case 3:
537: if (where == PANE)
1.183 nicm 538: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 539: if (where == STATUS)
1.183 nicm 540: key = KEYC_MOUSEDRAGEND3_STATUS;
1.182 nicm 541: if (where == BORDER)
1.183 nicm 542: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 543: break;
544: default:
545: key = KEYC_MOUSE;
546: break;
547: }
1.131 nicm 548: c->tty.mouse_drag_flag = 0;
1.182 nicm 549:
550: return (key);
1.131 nicm 551: }
552:
553: /* Convert to a key binding. */
1.177 nicm 554: key = KEYC_UNKNOWN;
1.131 nicm 555: switch (type) {
556: case NOTYPE:
557: break;
1.209 nicm 558: case MOVE:
559: if (where == PANE)
560: key = KEYC_MOUSEMOVE_PANE;
561: if (where == STATUS)
562: key = KEYC_MOUSEMOVE_STATUS;
563: if (where == BORDER)
564: key = KEYC_MOUSEMOVE_BORDER;
565: break;
1.131 nicm 566: case DRAG:
1.204 nicm 567: if (c->tty.mouse_drag_update != NULL)
568: key = KEYC_DRAGGING;
569: else {
1.131 nicm 570: switch (MOUSE_BUTTONS(b)) {
571: case 0:
572: if (where == PANE)
573: key = KEYC_MOUSEDRAG1_PANE;
574: if (where == STATUS)
575: key = KEYC_MOUSEDRAG1_STATUS;
576: if (where == BORDER)
577: key = KEYC_MOUSEDRAG1_BORDER;
578: break;
579: case 1:
580: if (where == PANE)
581: key = KEYC_MOUSEDRAG2_PANE;
582: if (where == STATUS)
583: key = KEYC_MOUSEDRAG2_STATUS;
584: if (where == BORDER)
585: key = KEYC_MOUSEDRAG2_BORDER;
586: break;
587: case 2:
588: if (where == PANE)
589: key = KEYC_MOUSEDRAG3_PANE;
590: if (where == STATUS)
591: key = KEYC_MOUSEDRAG3_STATUS;
592: if (where == BORDER)
593: key = KEYC_MOUSEDRAG3_BORDER;
594: break;
595: }
596: }
1.66 nicm 597:
1.182 nicm 598: /*
599: * Begin a drag by setting the flag to a non-zero value that
600: * corresponds to the mouse button in use.
601: */
602: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 603: break;
604: case WHEEL:
605: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
606: if (where == PANE)
607: key = KEYC_WHEELUP_PANE;
608: if (where == STATUS)
609: key = KEYC_WHEELUP_STATUS;
610: if (where == BORDER)
611: key = KEYC_WHEELUP_BORDER;
612: } else {
613: if (where == PANE)
614: key = KEYC_WHEELDOWN_PANE;
615: if (where == STATUS)
616: key = KEYC_WHEELDOWN_STATUS;
617: if (where == BORDER)
618: key = KEYC_WHEELDOWN_BORDER;
619: }
620: break;
621: case UP:
622: switch (MOUSE_BUTTONS(b)) {
623: case 0:
624: if (where == PANE)
625: key = KEYC_MOUSEUP1_PANE;
626: if (where == STATUS)
627: key = KEYC_MOUSEUP1_STATUS;
628: if (where == BORDER)
629: key = KEYC_MOUSEUP1_BORDER;
630: break;
631: case 1:
632: if (where == PANE)
633: key = KEYC_MOUSEUP2_PANE;
634: if (where == STATUS)
635: key = KEYC_MOUSEUP2_STATUS;
636: if (where == BORDER)
637: key = KEYC_MOUSEUP2_BORDER;
638: break;
639: case 2:
640: if (where == PANE)
641: key = KEYC_MOUSEUP3_PANE;
642: if (where == STATUS)
643: key = KEYC_MOUSEUP3_STATUS;
644: if (where == BORDER)
645: key = KEYC_MOUSEUP3_BORDER;
646: break;
647: }
648: break;
649: case DOWN:
650: switch (MOUSE_BUTTONS(b)) {
651: case 0:
652: if (where == PANE)
653: key = KEYC_MOUSEDOWN1_PANE;
654: if (where == STATUS)
655: key = KEYC_MOUSEDOWN1_STATUS;
656: if (where == BORDER)
657: key = KEYC_MOUSEDOWN1_BORDER;
658: break;
659: case 1:
660: if (where == PANE)
661: key = KEYC_MOUSEDOWN2_PANE;
662: if (where == STATUS)
663: key = KEYC_MOUSEDOWN2_STATUS;
664: if (where == BORDER)
665: key = KEYC_MOUSEDOWN2_BORDER;
666: break;
667: case 2:
668: if (where == PANE)
669: key = KEYC_MOUSEDOWN3_PANE;
670: if (where == STATUS)
671: key = KEYC_MOUSEDOWN3_STATUS;
672: if (where == BORDER)
673: key = KEYC_MOUSEDOWN3_BORDER;
674: break;
1.66 nicm 675: }
1.131 nicm 676: break;
1.192 nicm 677: case DOUBLE:
678: switch (MOUSE_BUTTONS(b)) {
679: case 0:
680: if (where == PANE)
681: key = KEYC_DOUBLECLICK1_PANE;
682: if (where == STATUS)
683: key = KEYC_DOUBLECLICK1_STATUS;
684: if (where == BORDER)
685: key = KEYC_DOUBLECLICK1_BORDER;
686: break;
687: case 1:
688: if (where == PANE)
689: key = KEYC_DOUBLECLICK2_PANE;
690: if (where == STATUS)
691: key = KEYC_DOUBLECLICK2_STATUS;
692: if (where == BORDER)
693: key = KEYC_DOUBLECLICK2_BORDER;
694: break;
695: case 2:
696: if (where == PANE)
697: key = KEYC_DOUBLECLICK3_PANE;
698: if (where == STATUS)
699: key = KEYC_DOUBLECLICK3_STATUS;
700: if (where == BORDER)
701: key = KEYC_DOUBLECLICK3_BORDER;
702: break;
703: }
704: break;
705: case TRIPLE:
706: switch (MOUSE_BUTTONS(b)) {
707: case 0:
708: if (where == PANE)
709: key = KEYC_TRIPLECLICK1_PANE;
710: if (where == STATUS)
711: key = KEYC_TRIPLECLICK1_STATUS;
712: if (where == BORDER)
713: key = KEYC_TRIPLECLICK1_BORDER;
714: break;
715: case 1:
716: if (where == PANE)
717: key = KEYC_TRIPLECLICK2_PANE;
718: if (where == STATUS)
719: key = KEYC_TRIPLECLICK2_STATUS;
720: if (where == BORDER)
721: key = KEYC_TRIPLECLICK2_BORDER;
722: break;
723: case 2:
724: if (where == PANE)
725: key = KEYC_TRIPLECLICK3_PANE;
726: if (where == STATUS)
727: key = KEYC_TRIPLECLICK3_STATUS;
728: if (where == BORDER)
729: key = KEYC_TRIPLECLICK3_BORDER;
730: break;
731: }
732: break;
1.66 nicm 733: }
1.177 nicm 734: if (key == KEYC_UNKNOWN)
735: return (KEYC_UNKNOWN);
1.66 nicm 736:
1.131 nicm 737: /* Apply modifiers if any. */
738: if (b & MOUSE_MASK_META)
739: key |= KEYC_ESCAPE;
740: if (b & MOUSE_MASK_CTRL)
741: key |= KEYC_CTRL;
742: if (b & MOUSE_MASK_SHIFT)
743: key |= KEYC_SHIFT;
1.66 nicm 744:
1.131 nicm 745: return (key);
1.66 nicm 746: }
747:
1.82 nicm 748: /* Is this fast enough to probably be a paste? */
1.190 nicm 749: static int
1.82 nicm 750: server_client_assume_paste(struct session *s)
751: {
752: struct timeval tv;
1.84 nicm 753: int t;
1.82 nicm 754:
1.163 nicm 755: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 756: return (0);
1.82 nicm 757:
758: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 759: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
760: log_debug("session %s pasting (flag %d)", s->name,
761: !!(s->flags & SESSION_PASTING));
762: if (s->flags & SESSION_PASTING)
763: return (1);
764: s->flags |= SESSION_PASTING;
765: return (0);
766: }
767: log_debug("session %s not pasting", s->name);
768: s->flags &= ~SESSION_PASTING;
1.83 nicm 769: return (0);
1.82 nicm 770: }
771:
1.18 nicm 772: /* Handle data key input from client. */
773: void
1.168 nicm 774: server_client_handle_key(struct client *c, key_code key)
1.18 nicm 775: {
1.131 nicm 776: struct mouse_event *m = &c->tty.mouse;
1.132 nicm 777: struct session *s = c->session;
1.18 nicm 778: struct window *w;
779: struct window_pane *wp;
780: struct timeval tv;
1.191 nicm 781: const char *name;
1.156 nicm 782: struct key_table *table;
1.132 nicm 783: struct key_binding bd_find, *bd;
784: int xtimeout;
1.201 nicm 785: struct cmd_find_state fs;
1.18 nicm 786:
787: /* Check the client is good to accept input. */
1.132 nicm 788: if (s == NULL || (c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
1.18 nicm 789: return;
1.132 nicm 790: w = s->curw->window;
1.18 nicm 791:
792: /* Update the activity timer. */
793: if (gettimeofday(&c->activity_time, NULL) != 0)
794: fatal("gettimeofday failed");
1.149 nicm 795: session_update_activity(s, &c->activity_time);
1.18 nicm 796:
1.132 nicm 797: /* Number keys jump to pane in identify mode. */
1.25 nicm 798: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 799: if (c->flags & CLIENT_READONLY)
800: return;
1.95 nicm 801: window_unzoom(w);
1.18 nicm 802: wp = window_pane_at_index(w, key - '0');
1.187 nicm 803: if (wp != NULL && !window_pane_visible(wp))
804: wp = NULL;
1.214 nicm 805: server_client_clear_identify(c, wp);
1.18 nicm 806: return;
807: }
808:
809: /* Handle status line. */
1.30 nicm 810: if (!(c->flags & CLIENT_READONLY)) {
811: status_message_clear(c);
1.214 nicm 812: server_client_clear_identify(c, NULL);
1.30 nicm 813: }
1.18 nicm 814: if (c->prompt_string != NULL) {
1.193 nicm 815: if (c->flags & CLIENT_READONLY)
816: return;
817: if (status_prompt_key(c, key) == 0)
818: return;
1.18 nicm 819: }
820:
821: /* Check for mouse keys. */
1.204 nicm 822: m->valid = 0;
1.18 nicm 823: if (key == KEYC_MOUSE) {
1.30 nicm 824: if (c->flags & CLIENT_READONLY)
825: return;
1.131 nicm 826: key = server_client_check_mouse(c);
1.177 nicm 827: if (key == KEYC_UNKNOWN)
1.131 nicm 828: return;
829:
830: m->valid = 1;
831: m->key = key;
1.203 nicm 832:
833: /*
1.204 nicm 834: * Mouse drag is in progress, so fire the callback (now that
835: * the mouse event is valid).
1.203 nicm 836: */
1.204 nicm 837: if (key == KEYC_DRAGGING) {
838: c->tty.mouse_drag_update(c, m);
1.203 nicm 839: return;
1.204 nicm 840: }
1.131 nicm 841: } else
842: m->valid = 0;
1.18 nicm 843:
1.202 nicm 844: /* Find affected pane. */
845: if (KEYC_IS_MOUSE(key) && m->valid)
846: wp = cmd_mouse_pane(m, NULL, NULL);
847: else
848: wp = w->active;
849:
850: /* Forward mouse keys if disabled. */
1.206 nicm 851: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.202 nicm 852: goto forward;
853:
1.132 nicm 854: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 855: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
856: goto forward;
1.18 nicm 857:
1.132 nicm 858: retry:
1.191 nicm 859: /*
860: * Work out the current key table. If the pane is in a mode, use
861: * the mode table instead of the default key table.
862: */
863: name = NULL;
864: if (wp != NULL && wp->mode != NULL && wp->mode->key_table != NULL)
865: name = wp->mode->key_table(wp);
866: if (name == NULL || !server_client_is_default_key_table(c))
867: table = c->keytable;
868: else
869: table = key_bindings_get_table(name, 1);
1.202 nicm 870: if (wp == NULL)
871: log_debug("key table %s (no pane)", table->name);
872: else
873: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.191 nicm 874:
1.205 nicm 875: /*
876: * The prefix always takes precedence and forces a switch to the prefix
877: * table, unless we are already there.
878: */
879: if ((key == (key_code)options_get_number(s->options, "prefix") ||
880: key == (key_code)options_get_number(s->options, "prefix2")) &&
881: strcmp(table->name, "prefix") != 0) {
882: server_client_set_key_table(c, "prefix");
883: server_status_client(c);
884: return;
885: }
886:
1.132 nicm 887: /* Try to see if there is a key binding in the current table. */
888: bd_find.key = key;
1.191 nicm 889: bd = RB_FIND(key_bindings, &table->key_bindings, &bd_find);
1.132 nicm 890: if (bd != NULL) {
891: /*
892: * Key was matched in this table. If currently repeating but a
893: * non-repeating binding was found, stop repeating and try
894: * again in the root table.
895: */
896: if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) {
1.178 nicm 897: server_client_set_key_table(c, NULL);
1.132 nicm 898: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 899: server_status_client(c);
1.132 nicm 900: goto retry;
1.18 nicm 901: }
1.82 nicm 902:
1.132 nicm 903: /*
904: * Take a reference to this table to make sure the key binding
905: * doesn't disappear.
906: */
907: table->references++;
908:
909: /*
910: * If this is a repeating key, start the timer. Otherwise reset
911: * the client back to the root table.
912: */
1.163 nicm 913: xtimeout = options_get_number(s->options, "repeat-time");
1.132 nicm 914: if (xtimeout != 0 && bd->can_repeat) {
915: c->flags |= CLIENT_REPEAT;
916:
917: tv.tv_sec = xtimeout / 1000;
918: tv.tv_usec = (xtimeout % 1000) * 1000L;
919: evtimer_del(&c->repeat_timer);
920: evtimer_add(&c->repeat_timer, &tv);
921: } else {
1.18 nicm 922: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 923: server_client_set_key_table(c, NULL);
1.18 nicm 924: }
1.132 nicm 925: server_status_client(c);
926:
1.201 nicm 927: /* Find default state if the pane is known. */
928: cmd_find_clear_state(&fs, NULL, 0);
929: if (wp != NULL) {
930: fs.s = s;
931: fs.wl = fs.s->curw;
932: fs.w = fs.wl->window;
933: fs.wp = wp;
934: cmd_find_log_state(__func__, &fs);
935:
936: if (!cmd_find_valid_state(&fs))
937: fatalx("invalid key state");
938: }
939:
1.132 nicm 940: /* Dispatch the key binding. */
1.201 nicm 941: key_bindings_dispatch(bd, c, m, &fs);
1.132 nicm 942: key_bindings_unref_table(table);
1.18 nicm 943: return;
944: }
945:
1.132 nicm 946: /*
947: * No match in this table. If repeating, switch the client back to the
948: * root table and try again.
949: */
950: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 951: server_client_set_key_table(c, NULL);
1.18 nicm 952: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 953: server_status_client(c);
954: goto retry;
1.18 nicm 955: }
956:
1.132 nicm 957: /* If no match and we're not in the root table, that's it. */
1.191 nicm 958: if (name == NULL && !server_client_is_default_key_table(c)) {
1.205 nicm 959: log_debug("no key in key table %s", table->name);
1.178 nicm 960: server_client_set_key_table(c, NULL);
1.132 nicm 961: server_status_client(c);
1.161 nicm 962: return;
963: }
964:
965: forward:
966: if (c->flags & CLIENT_READONLY)
967: return;
968: if (wp != NULL)
1.132 nicm 969: window_pane_key(wp, c, s, key, m);
1.18 nicm 970: }
971:
1.2 nicm 972: /* Client functions that need to happen every loop. */
973: void
974: server_client_loop(void)
975: {
976: struct client *c;
977: struct window *w;
978: struct window_pane *wp;
1.197 nicm 979: int focus;
1.2 nicm 980:
1.135 nicm 981: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 982: server_client_check_exit(c);
983: if (c->session != NULL) {
984: server_client_check_redraw(c);
985: server_client_reset_state(c);
986: }
1.2 nicm 987: }
988:
989: /*
990: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 991: * their flags now. Also check pane focus and resize.
1.2 nicm 992: */
1.197 nicm 993: focus = options_get_number(global_options, "focus-events");
1.134 nicm 994: RB_FOREACH(w, windows, &windows) {
1.91 nicm 995: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 996: if (wp->fd != -1) {
1.197 nicm 997: if (focus)
998: server_client_check_focus(wp);
1.101 nicm 999: server_client_check_resize(wp);
1000: }
1.2 nicm 1001: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1002: }
1.150 nicm 1003: check_window_name(w);
1.2 nicm 1004: }
1.92 nicm 1005: }
1006:
1.211 nicm 1007: /* Resize timer event. */
1.188 nicm 1008: static void
1009: server_client_resize_event(__unused int fd, __unused short events, void *data)
1.92 nicm 1010: {
1.188 nicm 1011: struct window_pane *wp = data;
1012: struct winsize ws;
1013:
1014: evtimer_del(&wp->resize_timer);
1.92 nicm 1015:
1.101 nicm 1016: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 1017: return;
1018:
1019: memset(&ws, 0, sizeof ws);
1020: ws.ws_col = wp->sx;
1021: ws.ws_row = wp->sy;
1022:
1.100 nicm 1023: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 1024: fatal("ioctl failed");
1025:
1026: wp->flags &= ~PANE_RESIZE;
1.188 nicm 1027: }
1028:
1029: /* Check if pane should be resized. */
1.190 nicm 1030: static void
1.188 nicm 1031: server_client_check_resize(struct window_pane *wp)
1032: {
1033: struct timeval tv = { .tv_usec = 250000 };
1034:
1035: if (!(wp->flags & PANE_RESIZE))
1036: return;
1037:
1038: if (!event_initialized(&wp->resize_timer))
1039: evtimer_set(&wp->resize_timer, server_client_resize_event, wp);
1040:
1041: /*
1042: * The first resize should happen immediately, so if the timer is not
1043: * running, do it now.
1044: */
1045: if (!evtimer_pending(&wp->resize_timer, NULL))
1046: server_client_resize_event(-1, 0, wp);
1047:
1048: /*
1049: * If the pane is in the alternate screen, let the timer expire and
1050: * resize to give the application a chance to redraw. If not, keep
1051: * pushing the timer back.
1052: */
1053: if (wp->saved_grid != NULL && evtimer_pending(&wp->resize_timer, NULL))
1054: return;
1055: evtimer_del(&wp->resize_timer);
1056: evtimer_add(&wp->resize_timer, &tv);
1.91 nicm 1057: }
1058:
1059: /* Check whether pane should be focused. */
1.190 nicm 1060: static void
1.91 nicm 1061: server_client_check_focus(struct window_pane *wp)
1062: {
1.93 nicm 1063: struct client *c;
1.102 nicm 1064: int push;
1065:
1066: /* Do we need to push the focus state? */
1067: push = wp->flags & PANE_FOCUSPUSH;
1068: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 1069:
1070: /* If we don't care about focus, forget it. */
1071: if (!(wp->base.mode & MODE_FOCUSON))
1072: return;
1073:
1074: /* If we're not the active pane in our window, we're not focused. */
1075: if (wp->window->active != wp)
1076: goto not_focused;
1077:
1078: /* If we're in a mode, we're not focused. */
1079: if (wp->screen != &wp->base)
1080: goto not_focused;
1081:
1082: /*
1.93 nicm 1083: * If our window is the current window in any focused clients with an
1084: * attached session, we're focused.
1.91 nicm 1085: */
1.135 nicm 1086: TAILQ_FOREACH(c, &clients, entry) {
1087: if (c->session == NULL || !(c->flags & CLIENT_FOCUSED))
1.91 nicm 1088: continue;
1.93 nicm 1089: if (c->session->flags & SESSION_UNATTACHED)
1090: continue;
1091:
1092: if (c->session->curw->window == wp->window)
1.91 nicm 1093: goto focused;
1094: }
1095:
1096: not_focused:
1.102 nicm 1097: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 1098: bufferevent_write(wp->event, "\033[O", 3);
1099: wp->flags &= ~PANE_FOCUSED;
1100: return;
1101:
1102: focused:
1.102 nicm 1103: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 1104: bufferevent_write(wp->event, "\033[I", 3);
1105: wp->flags |= PANE_FOCUSED;
1.2 nicm 1106: }
1107:
1.18 nicm 1108: /*
1109: * Update cursor position and mode settings. The scroll region and attributes
1110: * are cleared when idle (waiting for an event) as this is the most likely time
1111: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1112: * compromise between excessive resets and likelihood of an interrupt.
1113: *
1114: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1115: * things that are already in their default state.
1116: */
1.190 nicm 1117: static void
1.18 nicm 1118: server_client_reset_state(struct client *c)
1.1 nicm 1119: {
1.18 nicm 1120: struct window *w = c->session->curw->window;
1.209 nicm 1121: struct window_pane *wp = w->active, *loop;
1.18 nicm 1122: struct screen *s = wp->screen;
1.163 nicm 1123: struct options *oo = c->session->options;
1.66 nicm 1124: int status, mode, o;
1.60 nicm 1125:
1.186 nicm 1126: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1127: return;
1128:
1.199 nicm 1129: tty_region_off(&c->tty);
1130: tty_margin_off(&c->tty);
1.1 nicm 1131:
1132: status = options_get_number(oo, "status");
1133: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
1134: tty_cursor(&c->tty, 0, 0);
1.66 nicm 1135: else {
1.126 nicm 1136: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 1137: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
1138: }
1.1 nicm 1139:
1.50 nicm 1140: /*
1.131 nicm 1141: * Set mouse mode if requested. To support dragging, always use button
1142: * mode.
1.57 nicm 1143: */
1144: mode = s->mode;
1.209 nicm 1145: if (options_get_number(oo, "mouse")) {
1146: mode &= ~ALL_MOUSE_MODES;
1147: TAILQ_FOREACH(loop, &w->panes, entry) {
1148: if (loop->screen->mode & MODE_MOUSE_ALL)
1149: mode |= MODE_MOUSE_ALL;
1150: }
1151: if (~mode & MODE_MOUSE_ALL)
1152: mode |= MODE_MOUSE_BUTTON;
1153: }
1.215 nicm 1154:
1155: /* Clear bracketed paste mode if at the prompt. */
1156: if (c->prompt_string != NULL)
1157: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1158:
1159: /* Set the terminal mode and reset attributes. */
1.59 nicm 1160: tty_update_mode(&c->tty, mode, s);
1.1 nicm 1161: tty_reset(&c->tty);
1.17 nicm 1162: }
1163:
1164: /* Repeat time callback. */
1.190 nicm 1165: static void
1.170 nicm 1166: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1167: {
1168: struct client *c = data;
1169:
1.85 nicm 1170: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1171: server_client_set_key_table(c, NULL);
1.132 nicm 1172: c->flags &= ~CLIENT_REPEAT;
1173: server_status_client(c);
1.85 nicm 1174: }
1.192 nicm 1175: }
1176:
1177: /* Double-click callback. */
1178: static void
1179: server_client_click_timer(__unused int fd, __unused short events, void *data)
1180: {
1181: struct client *c = data;
1182:
1183: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1184: }
1185:
1.36 nicm 1186: /* Check if client should be exited. */
1.190 nicm 1187: static void
1.36 nicm 1188: server_client_check_exit(struct client *c)
1189: {
1190: if (!(c->flags & CLIENT_EXIT))
1191: return;
1192:
1.73 nicm 1193: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
1194: return;
1195: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 1196: return;
1.73 nicm 1197: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 1198: return;
1199:
1.162 nicm 1200: proc_send(c->peer, MSG_EXIT, -1, &c->retval, sizeof c->retval);
1.36 nicm 1201: c->flags &= ~CLIENT_EXIT;
1.38 nicm 1202: }
1203:
1.218 ! nicm 1204: /* Redraw timer callback. */
! 1205: static void
! 1206: server_client_redraw_timer(__unused int fd, __unused short events,
! 1207: __unused void* data)
! 1208: {
! 1209: log_debug("redraw timer fired");
! 1210: }
! 1211:
1.1 nicm 1212: /* Check for client redraws. */
1.190 nicm 1213: static void
1.1 nicm 1214: server_client_check_redraw(struct client *c)
1215: {
1216: struct session *s = c->session;
1.137 nicm 1217: struct tty *tty = &c->tty;
1.1 nicm 1218: struct window_pane *wp;
1.218 ! nicm 1219: int needed, flags, masked;
! 1220: struct timeval tv = { .tv_usec = 1000 };
! 1221: static struct event ev;
! 1222: size_t left;
1.1 nicm 1223:
1.86 nicm 1224: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1225: return;
1.218 ! nicm 1226:
! 1227: /*
! 1228: * If there is outstanding data, defer the redraw until it has been
! 1229: * consumed. We can just add a timer to get out of the event loop and
! 1230: * end up back here.
! 1231: */
! 1232: needed = 0;
! 1233: if (c->flags & CLIENT_REDRAW)
! 1234: needed = 1;
! 1235: else {
! 1236: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
! 1237: if (wp->flags & PANE_REDRAW) {
! 1238: needed = 1;
! 1239: break;
! 1240: }
! 1241: }
! 1242: }
! 1243: if (needed) {
! 1244: left = EVBUFFER_LENGTH(tty->out);
! 1245: if (left != 0) {
! 1246: log_debug("%s: redraw deferred (%zu left)", c->name, left);
! 1247: if (evtimer_initialized(&ev) && evtimer_pending(&ev, NULL))
! 1248: return;
! 1249: log_debug("redraw timer started");
! 1250: evtimer_set(&ev, server_client_redraw_timer, NULL);
! 1251: evtimer_add(&ev, &tv);
! 1252:
! 1253: /*
! 1254: * We may have got here for a single pane redraw, but
! 1255: * force a full redraw next time in case other panes
! 1256: * have been updated.
! 1257: */
! 1258: c->flags |= CLIENT_REDRAW;
! 1259: return;
! 1260: }
! 1261: if (evtimer_initialized(&ev))
! 1262: evtimer_del(&ev);
! 1263: log_debug("%s: redraw needed", c->name);
! 1264: }
1.79 nicm 1265:
1.1 nicm 1266: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.163 nicm 1267: if (options_get_number(s->options, "set-titles"))
1.1 nicm 1268: server_client_set_title(c);
1.189 nicm 1269: screen_redraw_update(c); /* will adjust flags */
1.1 nicm 1270: }
1271:
1.137 nicm 1272: flags = tty->flags & (TTY_FREEZE|TTY_NOCURSOR);
1273: tty->flags = (tty->flags & ~TTY_FREEZE) | TTY_NOCURSOR;
1274:
1.1 nicm 1275: if (c->flags & CLIENT_REDRAW) {
1.137 nicm 1276: tty_update_mode(tty, tty->mode, NULL);
1.115 nicm 1277: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 1278: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 1279: } else {
1280: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
1.137 nicm 1281: if (wp->flags & PANE_REDRAW) {
1282: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1283: screen_redraw_pane(c, wp);
1.137 nicm 1284: }
1.1 nicm 1285: }
1286: }
1287:
1.185 nicm 1288: masked = c->flags & (CLIENT_BORDERS|CLIENT_STATUS);
1289: if (masked != 0)
1.137 nicm 1290: tty_update_mode(tty, tty->mode, NULL);
1.185 nicm 1291: if (masked == CLIENT_BORDERS)
1.115 nicm 1292: screen_redraw_screen(c, 0, 0, 1);
1.185 nicm 1293: else if (masked == CLIENT_STATUS)
1.115 nicm 1294: screen_redraw_screen(c, 0, 1, 0);
1.185 nicm 1295: else if (masked != 0)
1296: screen_redraw_screen(c, 0, 1, 1);
1.1 nicm 1297:
1.137 nicm 1298: tty->flags = (tty->flags & ~(TTY_FREEZE|TTY_NOCURSOR)) | flags;
1299: tty_update_mode(tty, tty->mode, NULL);
1.1 nicm 1300:
1.153 nicm 1301: c->flags &= ~(CLIENT_REDRAW|CLIENT_BORDERS|CLIENT_STATUS|
1302: CLIENT_STATUSFORCE);
1.1 nicm 1303: }
1304:
1305: /* Set client title. */
1.190 nicm 1306: static void
1.1 nicm 1307: server_client_set_title(struct client *c)
1308: {
1.128 nicm 1309: struct session *s = c->session;
1310: const char *template;
1311: char *title;
1312: struct format_tree *ft;
1.1 nicm 1313:
1.163 nicm 1314: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1315:
1.210 nicm 1316: ft = format_create(NULL, FORMAT_NONE, 0);
1.128 nicm 1317: format_defaults(ft, c, NULL, NULL, NULL);
1318:
1319: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 1320: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1321: free(c->title);
1.1 nicm 1322: c->title = xstrdup(title);
1323: tty_set_title(&c->tty, c->title);
1324: }
1.77 nicm 1325: free(title);
1.128 nicm 1326:
1327: format_free(ft);
1.1 nicm 1328: }
1329:
1330: /* Dispatch message from client. */
1.190 nicm 1331: static void
1.162 nicm 1332: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 1333: {
1.162 nicm 1334: struct client *c = arg;
1.73 nicm 1335: struct msg_stdin_data stdindata;
1.108 nicm 1336: const char *data;
1.162 nicm 1337: ssize_t datalen;
1.149 nicm 1338: struct session *s;
1.1 nicm 1339:
1.162 nicm 1340: if (c->flags & CLIENT_DEAD)
1341: return;
1342:
1343: if (imsg == NULL) {
1344: server_client_lost(c);
1345: return;
1346: }
1.1 nicm 1347:
1.162 nicm 1348: data = imsg->data;
1349: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 1350:
1.162 nicm 1351: switch (imsg->hdr.type) {
1352: case MSG_IDENTIFY_FLAGS:
1353: case MSG_IDENTIFY_TERM:
1354: case MSG_IDENTIFY_TTYNAME:
1355: case MSG_IDENTIFY_CWD:
1356: case MSG_IDENTIFY_STDIN:
1357: case MSG_IDENTIFY_ENVIRON:
1358: case MSG_IDENTIFY_CLIENTPID:
1359: case MSG_IDENTIFY_DONE:
1360: server_client_dispatch_identify(c, imsg);
1361: break;
1362: case MSG_COMMAND:
1363: server_client_dispatch_command(c, imsg);
1364: break;
1365: case MSG_STDIN:
1366: if (datalen != sizeof stdindata)
1367: fatalx("bad MSG_STDIN size");
1368: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 1369:
1.162 nicm 1370: if (c->stdin_callback == NULL)
1.33 nicm 1371: break;
1.162 nicm 1372: if (stdindata.size <= 0)
1373: c->stdin_closed = 1;
1374: else {
1375: evbuffer_add(c->stdin_data, stdindata.data,
1376: stdindata.size);
1377: }
1378: c->stdin_callback(c, c->stdin_closed,
1379: c->stdin_callback_data);
1380: break;
1381: case MSG_RESIZE:
1382: if (datalen != 0)
1383: fatalx("bad MSG_RESIZE size");
1.1 nicm 1384:
1.162 nicm 1385: if (c->flags & CLIENT_CONTROL)
1.1 nicm 1386: break;
1.162 nicm 1387: if (tty_resize(&c->tty)) {
1388: recalculate_sizes();
1389: server_redraw_client(c);
1390: }
1.174 nicm 1391: if (c->session != NULL)
1.196 nicm 1392: notify_client("client-resized", c);
1.162 nicm 1393: break;
1394: case MSG_EXITING:
1395: if (datalen != 0)
1396: fatalx("bad MSG_EXITING size");
1.1 nicm 1397:
1.162 nicm 1398: c->session = NULL;
1399: tty_close(&c->tty);
1400: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
1401: break;
1402: case MSG_WAKEUP:
1403: case MSG_UNLOCK:
1404: if (datalen != 0)
1405: fatalx("bad MSG_WAKEUP size");
1.121 nicm 1406:
1.162 nicm 1407: if (!(c->flags & CLIENT_SUSPENDED))
1408: break;
1409: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 1410:
1.162 nicm 1411: if (c->tty.fd == -1) /* exited in the meantime */
1.1 nicm 1412: break;
1.162 nicm 1413: s = c->session;
1.1 nicm 1414:
1.162 nicm 1415: if (gettimeofday(&c->activity_time, NULL) != 0)
1416: fatal("gettimeofday failed");
1417:
1418: tty_start_tty(&c->tty);
1419: server_redraw_client(c);
1420: recalculate_sizes();
1.184 nicm 1421:
1422: if (s != NULL)
1423: session_update_activity(s, &c->activity_time);
1.162 nicm 1424: break;
1425: case MSG_SHELL:
1426: if (datalen != 0)
1427: fatalx("bad MSG_SHELL size");
1.1 nicm 1428:
1.162 nicm 1429: server_client_dispatch_shell(c);
1430: break;
1.1 nicm 1431: }
1432: }
1433:
1.194 nicm 1434: /* Callback when command is done. */
1435: static enum cmd_retval
1.195 nicm 1436: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 1437: {
1.195 nicm 1438: struct client *c = item->client;
1.194 nicm 1439:
1440: if (~c->flags & CLIENT_ATTACHED)
1441: c->flags |= CLIENT_EXIT;
1442: return (CMD_RETURN_NORMAL);
1443: }
1444:
1445: /* Show an error message. */
1446: static enum cmd_retval
1.195 nicm 1447: server_client_command_error(struct cmdq_item *item, void *data)
1.194 nicm 1448: {
1449: char *error = data;
1450:
1.195 nicm 1451: cmdq_error(item, "%s", error);
1.194 nicm 1452: free(error);
1453:
1454: return (CMD_RETURN_NORMAL);
1455: }
1456:
1.1 nicm 1457: /* Handle command message. */
1.190 nicm 1458: static void
1.162 nicm 1459: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 1460: {
1.108 nicm 1461: struct msg_command_data data;
1462: char *buf;
1463: size_t len;
1464: struct cmd_list *cmdlist = NULL;
1465: int argc;
1466: char **argv, *cause;
1467:
1468: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
1469: fatalx("bad MSG_COMMAND size");
1470: memcpy(&data, imsg->data, sizeof data);
1471:
1.124 nicm 1472: buf = (char *)imsg->data + sizeof data;
1.108 nicm 1473: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
1474: if (len > 0 && buf[len - 1] != '\0')
1475: fatalx("bad MSG_COMMAND string");
1476:
1477: argc = data.argc;
1478: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 1479: cause = xstrdup("command too long");
1.1 nicm 1480: goto error;
1481: }
1482:
1483: if (argc == 0) {
1484: argc = 1;
1485: argv = xcalloc(1, sizeof *argv);
1486: *argv = xstrdup("new-session");
1487: }
1488:
1.94 nicm 1489: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
1.1 nicm 1490: cmd_free_argv(argc, argv);
1491: goto error;
1492: }
1493: cmd_free_argv(argc, argv);
1494:
1.194 nicm 1495: cmdq_append(c, cmdq_get_command(cmdlist, NULL, NULL, 0));
1496: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.1 nicm 1497: cmd_list_free(cmdlist);
1498: return;
1499:
1500: error:
1.194 nicm 1501: cmdq_append(c, cmdq_get_callback(server_client_command_error, cause));
1502:
1.1 nicm 1503: if (cmdlist != NULL)
1504: cmd_list_free(cmdlist);
1.88 nicm 1505:
1.36 nicm 1506: c->flags |= CLIENT_EXIT;
1.1 nicm 1507: }
1508:
1509: /* Handle identify message. */
1.190 nicm 1510: static void
1.162 nicm 1511: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 1512: {
1.165 nicm 1513: const char *data, *home;
1.109 nicm 1514: size_t datalen;
1515: int flags;
1.216 nicm 1516: char *name;
1.109 nicm 1517:
1518: if (c->flags & CLIENT_IDENTIFIED)
1519: fatalx("out-of-order identify message");
1520:
1521: data = imsg->data;
1522: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1523:
1524: switch (imsg->hdr.type) {
1525: case MSG_IDENTIFY_FLAGS:
1526: if (datalen != sizeof flags)
1527: fatalx("bad MSG_IDENTIFY_FLAGS size");
1528: memcpy(&flags, data, sizeof flags);
1529: c->flags |= flags;
1.158 nicm 1530: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.109 nicm 1531: break;
1532: case MSG_IDENTIFY_TERM:
1.110 nicm 1533: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1534: fatalx("bad MSG_IDENTIFY_TERM string");
1535: c->term = xstrdup(data);
1.158 nicm 1536: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.109 nicm 1537: break;
1538: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 1539: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1540: fatalx("bad MSG_IDENTIFY_TTYNAME string");
1541: c->ttyname = xstrdup(data);
1.158 nicm 1542: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 1543: break;
1544: case MSG_IDENTIFY_CWD:
1.155 nicm 1545: if (datalen == 0 || data[datalen - 1] != '\0')
1546: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 1547: if (access(data, X_OK) == 0)
1548: c->cwd = xstrdup(data);
1549: else if ((home = find_home()) != NULL)
1550: c->cwd = xstrdup(home);
1551: else
1552: c->cwd = xstrdup("/");
1.158 nicm 1553: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 1554: break;
1555: case MSG_IDENTIFY_STDIN:
1556: if (datalen != 0)
1557: fatalx("bad MSG_IDENTIFY_STDIN size");
1558: c->fd = imsg->fd;
1.158 nicm 1559: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 1560: break;
1561: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1562: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1563: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1564: if (strchr(data, '=') != NULL)
1.164 nicm 1565: environ_put(c->environ, data);
1.158 nicm 1566: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 1567: break;
1568: case MSG_IDENTIFY_CLIENTPID:
1569: if (datalen != sizeof c->pid)
1570: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
1571: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 1572: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 1573: break;
1574: default:
1575: break;
1576: }
1577:
1578: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1579: return;
1580: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 1581:
1.216 nicm 1582: if (*c->ttyname != '\0')
1583: name = xstrdup(c->ttyname);
1584: else
1585: xasprintf(&name, "client-%ld", (long)c->pid);
1586: c->name = name;
1587: log_debug("client %p name is %s", c, c->name);
1588:
1.109 nicm 1589: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1590: c->stdin_callback = control_callback;
1.109 nicm 1591:
1.97 nicm 1592: evbuffer_free(c->stderr_data);
1593: c->stderr_data = c->stdout_data;
1.109 nicm 1594:
1595: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1596: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.162 nicm 1597: proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
1.75 nicm 1598:
1599: c->tty.fd = -1;
1600:
1.109 nicm 1601: close(c->fd);
1602: c->fd = -1;
1603:
1.75 nicm 1604: return;
1605: }
1.1 nicm 1606:
1.109 nicm 1607: if (c->fd == -1)
1.104 nicm 1608: return;
1.145 nicm 1609: if (tty_init(&c->tty, c, c->fd, c->term) != 0) {
1.109 nicm 1610: close(c->fd);
1611: c->fd = -1;
1.80 nicm 1612: return;
1613: }
1.109 nicm 1614: if (c->flags & CLIENT_UTF8)
1.1 nicm 1615: c->tty.flags |= TTY_UTF8;
1.109 nicm 1616: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1617: c->tty.term_flags |= TERM_256COLOURS;
1618:
1619: tty_resize(&c->tty);
1620:
1.109 nicm 1621: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1622: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1623: }
1624:
1625: /* Handle shell message. */
1.190 nicm 1626: static void
1.162 nicm 1627: server_client_dispatch_shell(struct client *c)
1.1 nicm 1628: {
1.107 nicm 1629: const char *shell;
1.25 nicm 1630:
1.163 nicm 1631: shell = options_get_string(global_s_options, "default-shell");
1.1 nicm 1632: if (*shell == '\0' || areshell(shell))
1633: shell = _PATH_BSHELL;
1.162 nicm 1634: proc_send_s(c->peer, MSG_SHELL, shell);
1.25 nicm 1635:
1.162 nicm 1636: proc_kill_peer(c->peer);
1.169 nicm 1637: }
1638:
1639: /* Event callback to push more stdout data if any left. */
1640: static void
1.170 nicm 1641: server_client_stdout_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1642: {
1643: struct client *c = arg;
1644:
1645: if (~c->flags & CLIENT_DEAD)
1646: server_client_push_stdout(c);
1647: server_client_unref(c);
1648: }
1649:
1650: /* Push stdout to client if possible. */
1651: void
1652: server_client_push_stdout(struct client *c)
1653: {
1654: struct msg_stdout_data data;
1655: size_t sent, left;
1656:
1657: left = EVBUFFER_LENGTH(c->stdout_data);
1658: while (left != 0) {
1659: sent = left;
1660: if (sent > sizeof data.data)
1661: sent = sizeof data.data;
1662: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
1663: data.size = sent;
1664:
1665: if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
1666: break;
1667: evbuffer_drain(c->stdout_data, sent);
1668:
1669: left = EVBUFFER_LENGTH(c->stdout_data);
1670: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1671: sent, left);
1672: }
1673: if (left != 0) {
1674: c->references++;
1675: event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
1676: log_debug("%s: client %p, queued", __func__, c);
1677: }
1678: }
1679:
1680: /* Event callback to push more stderr data if any left. */
1681: static void
1.170 nicm 1682: server_client_stderr_cb(__unused int fd, __unused short events, void *arg)
1.169 nicm 1683: {
1684: struct client *c = arg;
1685:
1686: if (~c->flags & CLIENT_DEAD)
1687: server_client_push_stderr(c);
1688: server_client_unref(c);
1689: }
1690:
1691: /* Push stderr to client if possible. */
1692: void
1693: server_client_push_stderr(struct client *c)
1694: {
1695: struct msg_stderr_data data;
1696: size_t sent, left;
1697:
1698: if (c->stderr_data == c->stdout_data) {
1699: server_client_push_stdout(c);
1700: return;
1701: }
1702:
1703: left = EVBUFFER_LENGTH(c->stderr_data);
1704: while (left != 0) {
1705: sent = left;
1706: if (sent > sizeof data.data)
1707: sent = sizeof data.data;
1708: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
1709: data.size = sent;
1710:
1711: if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
1712: break;
1713: evbuffer_drain(c->stderr_data, sent);
1714:
1715: left = EVBUFFER_LENGTH(c->stderr_data);
1716: log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
1717: sent, left);
1718: }
1719: if (left != 0) {
1720: c->references++;
1721: event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
1722: log_debug("%s: client %p, queued", __func__, c);
1.212 nicm 1723: }
1724: }
1725:
1726: /* Add to client message log. */
1727: void
1728: server_client_add_message(struct client *c, const char *fmt, ...)
1729: {
1730: struct message_entry *msg, *msg1;
1731: char *s;
1732: va_list ap;
1733: u_int limit;
1734:
1735: va_start(ap, fmt);
1736: xvasprintf(&s, fmt, ap);
1737: va_end(ap);
1738:
1.216 nicm 1739: log_debug("message %s (client %p)", s, c);
1.212 nicm 1740:
1741: msg = xcalloc(1, sizeof *msg);
1742: msg->msg_time = time(NULL);
1743: msg->msg_num = c->message_next++;
1744: msg->msg = s;
1745: TAILQ_INSERT_TAIL(&c->message_log, msg, entry);
1746:
1747: limit = options_get_number(global_options, "message-limit");
1748: TAILQ_FOREACH_SAFE(msg, &c->message_log, entry, msg1) {
1749: if (msg->msg_num + limit >= c->message_next)
1750: break;
1751: free(msg->msg);
1752: TAILQ_REMOVE(&c->message_log, msg, entry);
1753: free(msg);
1.169 nicm 1754: }
1.213 nicm 1755: }
1756:
1757: /* Get client working directory. */
1758: const char *
1759: server_client_get_cwd(struct client *c)
1760: {
1761: struct session *s;
1762:
1763: if (c != NULL && c->session == NULL && c->cwd != NULL)
1764: return (c->cwd);
1765: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
1766: return (s->cwd);
1767: return (".");
1768: }
1769:
1770: /* Resolve an absolute path or relative to client working directory. */
1771: char *
1772: server_client_get_path(struct client *c, const char *file)
1773: {
1774: char *path, resolved[PATH_MAX];
1775:
1776: if (*file == '/')
1777: path = xstrdup(file);
1778: else
1779: xasprintf(&path, "%s/%s", server_client_get_cwd(c), file);
1780: if (realpath(path, resolved) == NULL)
1781: return (path);
1782: free(path);
1783: return (xstrdup(resolved));
1.1 nicm 1784: }