Annotation of src/usr.bin/tmux/server-client.c, Revision 1.129
1.129 ! nicm 1: /* $OpenBSD: server-client.c,v 1.128 2015/02/06 17:17:12 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.1 nicm 21:
1.114 benno 22: #include <errno.h>
1.12 nicm 23: #include <event.h>
1.1 nicm 24: #include <fcntl.h>
1.98 nicm 25: #include <paths.h>
26: #include <stdlib.h>
1.1 nicm 27: #include <string.h>
1.4 nicm 28: #include <time.h>
1.1 nicm 29: #include <unistd.h>
30:
31: #include "tmux.h"
32:
1.91 nicm 33: void server_client_check_focus(struct window_pane *);
1.92 nicm 34: void server_client_check_resize(struct window_pane *);
1.81 nicm 35: void server_client_check_mouse(struct client *, struct window_pane *);
1.17 nicm 36: void server_client_repeat_timer(int, short, void *);
1.36 nicm 37: void server_client_check_exit(struct client *);
1.1 nicm 38: void server_client_check_redraw(struct client *);
39: void server_client_set_title(struct client *);
1.18 nicm 40: void server_client_reset_state(struct client *);
1.82 nicm 41: int server_client_assume_paste(struct session *);
1.1 nicm 42:
43: int server_client_msg_dispatch(struct client *);
1.108 nicm 44: void server_client_msg_command(struct client *, struct imsg *);
1.109 nicm 45: void server_client_msg_identify(struct client *, struct imsg *);
1.1 nicm 46: void server_client_msg_shell(struct client *);
47:
48: /* Create a new client. */
49: void
50: server_client_create(int fd)
51: {
52: struct client *c;
53: u_int i;
54:
1.49 nicm 55: setblocking(fd, 0);
1.1 nicm 56:
57: c = xcalloc(1, sizeof *c);
58: c->references = 0;
59: imsg_init(&c->ibuf, fd);
1.14 nicm 60: server_update_event(c);
1.25 nicm 61:
1.10 nicm 62: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 63: fatal("gettimeofday failed");
1.11 nicm 64: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 65:
66: environ_init(&c->environ);
1.1 nicm 67:
1.94 nicm 68: c->cmdq = cmdq_new(c);
69: c->cmdq->client_exit = 1;
70:
1.116 nicm 71: c->stdin_data = evbuffer_new();
72: c->stdout_data = evbuffer_new();
73: c->stderr_data = evbuffer_new();
1.33 nicm 74:
1.1 nicm 75: c->tty.fd = -1;
76: c->title = NULL;
77:
78: c->session = NULL;
1.45 nicm 79: c->last_session = NULL;
1.1 nicm 80: c->tty.sx = 80;
81: c->tty.sy = 24;
82:
83: screen_init(&c->status, c->tty.sx, 1, 0);
1.51 nicm 84: RB_INIT(&c->status_new);
85: RB_INIT(&c->status_old);
1.1 nicm 86:
87: c->message_string = NULL;
1.21 nicm 88: ARRAY_INIT(&c->message_log);
1.1 nicm 89:
90: c->prompt_string = NULL;
91: c->prompt_buffer = NULL;
92: c->prompt_index = 0;
93:
1.81 nicm 94: c->tty.mouse.xb = c->tty.mouse.button = 3;
95: c->tty.mouse.x = c->tty.mouse.y = -1;
96: c->tty.mouse.lx = c->tty.mouse.ly = -1;
97: c->tty.mouse.sx = c->tty.mouse.sy = -1;
98: c->tty.mouse.event = MOUSE_EVENT_UP;
99: c->tty.mouse.flags = 0;
1.57 nicm 100:
1.93 nicm 101: c->flags |= CLIENT_FOCUSED;
102:
1.17 nicm 103: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
104:
1.1 nicm 105: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
106: if (ARRAY_ITEM(&clients, i) == NULL) {
107: ARRAY_SET(&clients, i, c);
108: return;
109: }
110: }
111: ARRAY_ADD(&clients, c);
112: log_debug("new client %d", fd);
1.72 nicm 113: }
114:
115: /* Open client terminal if needed. */
116: int
1.119 nicm 117: server_client_open(struct client *c, char **cause)
1.72 nicm 118: {
1.75 nicm 119: if (c->flags & CLIENT_CONTROL)
120: return (0);
1.120 nicm 121:
122: if (strcmp(c->ttyname, "/dev/tty") == 0) {
123: *cause = xstrdup("can't use /dev/tty");
124: return (-1);
125: }
1.75 nicm 126:
1.72 nicm 127: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 128: *cause = xstrdup("not a terminal");
1.72 nicm 129: return (-1);
130: }
131:
1.119 nicm 132: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 133: return (-1);
134:
135: return (0);
1.1 nicm 136: }
137:
138: /* Lost a client. */
139: void
140: server_client_lost(struct client *c)
141: {
1.21 nicm 142: struct message_entry *msg;
143: u_int i;
1.1 nicm 144:
145: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
146: if (ARRAY_ITEM(&clients, i) == c)
147: ARRAY_SET(&clients, i, NULL);
148: }
149: log_debug("lost client %d", c->ibuf.fd);
150:
151: /*
152: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
153: * and tty_free might close an unrelated fd.
154: */
155: if (c->flags & CLIENT_TERMINAL)
156: tty_free(&c->tty);
1.109 nicm 157: free(c->ttyname);
158: free(c->term);
1.1 nicm 159:
1.113 nicm 160: evbuffer_free(c->stdin_data);
161: evbuffer_free(c->stdout_data);
1.97 nicm 162: if (c->stderr_data != c->stdout_data)
1.126 nicm 163: evbuffer_free(c->stderr_data);
1.33 nicm 164:
1.51 nicm 165: status_free_jobs(&c->status_new);
166: status_free_jobs(&c->status_old);
1.1 nicm 167: screen_free(&c->status);
168:
1.77 nicm 169: free(c->title);
1.109 nicm 170: close(c->cwd);
1.1 nicm 171:
1.17 nicm 172: evtimer_del(&c->repeat_timer);
173:
1.69 nicm 174: if (event_initialized(&c->identify_timer))
175: evtimer_del(&c->identify_timer);
1.15 nicm 176:
1.77 nicm 177: free(c->message_string);
1.116 nicm 178: if (event_initialized(&c->message_timer))
1.69 nicm 179: evtimer_del(&c->message_timer);
1.21 nicm 180: for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
181: msg = &ARRAY_ITEM(&c->message_log, i);
1.77 nicm 182: free(msg->msg);
1.21 nicm 183: }
184: ARRAY_FREE(&c->message_log);
1.1 nicm 185:
1.77 nicm 186: free(c->prompt_string);
187: free(c->prompt_buffer);
1.61 nicm 188:
1.94 nicm 189: c->cmdq->dead = 1;
190: cmdq_free(c->cmdq);
191: c->cmdq = NULL;
192:
1.61 nicm 193: environ_free(&c->environ);
1.1 nicm 194:
195: close(c->ibuf.fd);
196: imsg_clear(&c->ibuf);
1.69 nicm 197: if (event_initialized(&c->event))
198: event_del(&c->event);
1.13 nicm 199:
1.1 nicm 200: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
201: if (ARRAY_ITEM(&dead_clients, i) == NULL) {
202: ARRAY_SET(&dead_clients, i, c);
203: break;
204: }
205: }
206: if (i == ARRAY_LENGTH(&dead_clients))
207: ARRAY_ADD(&dead_clients, c);
208: c->flags |= CLIENT_DEAD;
1.71 nicm 209:
210: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 211:
212: recalculate_sizes();
1.41 nicm 213: server_check_unattached();
1.19 nicm 214: server_update_socket();
1.9 nicm 215: }
216:
1.1 nicm 217: /* Process a single client event. */
218: void
1.12 nicm 219: server_client_callback(int fd, short events, void *data)
1.1 nicm 220: {
221: struct client *c = data;
1.7 nicm 222:
223: if (c->flags & CLIENT_DEAD)
224: return;
1.1 nicm 225:
226: if (fd == c->ibuf.fd) {
1.122 krw 227: if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) <= 0 &&
1.114 benno 228: errno != EAGAIN)
1.1 nicm 229: goto client_lost;
230:
231: if (c->flags & CLIENT_BAD) {
232: if (c->ibuf.w.queued == 0)
233: goto client_lost;
234: return;
235: }
236:
1.12 nicm 237: if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1 nicm 238: goto client_lost;
239: }
1.14 nicm 240:
1.73 nicm 241: server_push_stdout(c);
242: server_push_stderr(c);
243:
1.25 nicm 244: server_update_event(c);
1.1 nicm 245: return;
246:
247: client_lost:
248: server_client_lost(c);
249: }
250:
1.16 nicm 251: /* Handle client status timer. */
252: void
253: server_client_status_timer(void)
254: {
255: struct client *c;
256: struct session *s;
257: struct timeval tv;
1.20 nicm 258: u_int i;
259: int interval;
260: time_t difference;
1.16 nicm 261:
262: if (gettimeofday(&tv, NULL) != 0)
263: fatal("gettimeofday failed");
264:
265: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
266: c = ARRAY_ITEM(&clients, i);
267: if (c == NULL || c->session == NULL)
268: continue;
269: if (c->message_string != NULL || c->prompt_string != NULL) {
270: /*
271: * Don't need timed redraw for messages/prompts so bail
272: * now. The status timer isn't reset when they are
273: * redrawn anyway.
274: */
275: continue;
276: }
277: s = c->session;
278:
279: if (!options_get_number(&s->options, "status"))
280: continue;
281: interval = options_get_number(&s->options, "status-interval");
282:
1.20 nicm 283: difference = tv.tv_sec - c->status_timer.tv_sec;
1.117 nicm 284: if (interval != 0 && difference >= interval) {
1.51 nicm 285: status_update_jobs(c);
1.16 nicm 286: c->flags |= CLIENT_STATUS;
287: }
288: }
289: }
290:
1.66 nicm 291: /* Check for mouse keys. */
292: void
1.81 nicm 293: server_client_check_mouse(struct client *c, struct window_pane *wp)
1.66 nicm 294: {
1.81 nicm 295: struct session *s = c->session;
296: struct options *oo = &s->options;
297: struct mouse_event *m = &c->tty.mouse;
298: int statusat;
1.66 nicm 299:
300: statusat = status_at_line(c);
301:
302: /* Is this a window selection click on the status line? */
1.81 nicm 303: if (statusat != -1 && m->y == (u_int)statusat &&
1.66 nicm 304: options_get_number(oo, "mouse-select-window")) {
1.81 nicm 305: if (m->event & MOUSE_EVENT_CLICK) {
306: status_set_window_at(c, m->x);
307: } else if (m->event == MOUSE_EVENT_WHEEL) {
308: if (m->wheel == MOUSE_WHEEL_UP)
1.66 nicm 309: session_previous(c->session, 0);
1.81 nicm 310: else if (m->wheel == MOUSE_WHEEL_DOWN)
1.66 nicm 311: session_next(c->session, 0);
1.81 nicm 312: server_redraw_session(s);
1.66 nicm 313: }
1.81 nicm 314: recalculate_sizes();
1.67 nicm 315: return;
1.66 nicm 316: }
317:
318: /*
319: * Not on status line - adjust mouse position if status line is at the
320: * top and limit if at the bottom. From here on a struct mouse
321: * represents the offset onto the window itself.
322: */
1.81 nicm 323: if (statusat == 0 && m->y > 0)
324: m->y--;
325: else if (statusat > 0 && m->y >= (u_int)statusat)
326: m->y = statusat - 1;
1.66 nicm 327:
1.118 nicm 328: /* Is this a pane selection? */
1.66 nicm 329: if (options_get_number(oo, "mouse-select-pane") &&
1.118 nicm 330: (m->event == MOUSE_EVENT_DOWN || m->event == MOUSE_EVENT_WHEEL)) {
1.81 nicm 331: window_set_active_at(wp->window, m->x, m->y);
1.125 nicm 332: server_status_window(wp->window);
1.66 nicm 333: server_redraw_window_borders(wp->window);
334: wp = wp->window->active; /* may have changed */
335: }
336:
337: /* Check if trying to resize pane. */
338: if (options_get_number(oo, "mouse-resize-pane"))
1.81 nicm 339: layout_resize_pane_mouse(c);
1.66 nicm 340:
341: /* Update last and pass through to client. */
1.81 nicm 342: window_pane_mouse(wp, c->session, m);
1.66 nicm 343: }
344:
1.82 nicm 345: /* Is this fast enough to probably be a paste? */
346: int
347: server_client_assume_paste(struct session *s)
348: {
349: struct timeval tv;
1.84 nicm 350: int t;
1.82 nicm 351:
352: if ((t = options_get_number(&s->options, "assume-paste-time")) == 0)
1.83 nicm 353: return (0);
1.82 nicm 354:
355: timersub(&s->activity_time, &s->last_activity_time, &tv);
356: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000)
1.83 nicm 357: return (1);
358: return (0);
1.82 nicm 359: }
360:
1.18 nicm 361: /* Handle data key input from client. */
362: void
1.74 nicm 363: server_client_handle_key(struct client *c, int key)
1.18 nicm 364: {
365: struct session *s;
366: struct window *w;
367: struct window_pane *wp;
368: struct timeval tv;
369: struct key_binding *bd;
1.82 nicm 370: int xtimeout, isprefix, ispaste;
1.18 nicm 371:
372: /* Check the client is good to accept input. */
373: if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
374: return;
1.86 nicm 375:
1.18 nicm 376: if (c->session == NULL)
377: return;
378: s = c->session;
379:
380: /* Update the activity timer. */
381: if (gettimeofday(&c->activity_time, NULL) != 0)
382: fatal("gettimeofday failed");
1.82 nicm 383:
384: memcpy(&s->last_activity_time, &s->activity_time,
385: sizeof s->last_activity_time);
1.18 nicm 386: memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
387:
388: w = c->session->curw->window;
389: wp = w->active;
390:
391: /* Special case: number keys jump to pane in identify mode. */
1.25 nicm 392: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 393: if (c->flags & CLIENT_READONLY)
394: return;
1.95 nicm 395: window_unzoom(w);
1.18 nicm 396: wp = window_pane_at_index(w, key - '0');
397: if (wp != NULL && window_pane_visible(wp))
398: window_set_active_pane(w, wp);
399: server_clear_identify(c);
400: return;
401: }
402:
403: /* Handle status line. */
1.30 nicm 404: if (!(c->flags & CLIENT_READONLY)) {
405: status_message_clear(c);
406: server_clear_identify(c);
407: }
1.18 nicm 408: if (c->prompt_string != NULL) {
1.30 nicm 409: if (!(c->flags & CLIENT_READONLY))
410: status_prompt_key(c, key);
1.18 nicm 411: return;
412: }
413:
414: /* Check for mouse keys. */
415: if (key == KEYC_MOUSE) {
1.30 nicm 416: if (c->flags & CLIENT_READONLY)
417: return;
1.81 nicm 418: server_client_check_mouse(c, wp);
1.18 nicm 419: return;
420: }
421:
422: /* Is this a prefix key? */
1.82 nicm 423: if (key == options_get_number(&s->options, "prefix"))
1.63 nicm 424: isprefix = 1;
1.82 nicm 425: else if (key == options_get_number(&s->options, "prefix2"))
1.63 nicm 426: isprefix = 1;
427: else
428: isprefix = 0;
1.18 nicm 429:
1.82 nicm 430: /* Treat prefix as a regular key when pasting is detected. */
431: ispaste = server_client_assume_paste(s);
432: if (ispaste)
433: isprefix = 0;
434:
1.18 nicm 435: /* No previous prefix key. */
436: if (!(c->flags & CLIENT_PREFIX)) {
1.82 nicm 437: if (isprefix) {
1.18 nicm 438: c->flags |= CLIENT_PREFIX;
1.85 nicm 439: server_status_client(c);
1.82 nicm 440: return;
1.18 nicm 441: }
1.82 nicm 442:
443: /* Try as a non-prefix key binding. */
444: if (ispaste || (bd = key_bindings_lookup(key)) == NULL) {
445: if (!(c->flags & CLIENT_READONLY))
446: window_pane_key(wp, s, key);
447: } else
448: key_bindings_dispatch(bd, c);
1.18 nicm 449: return;
450: }
451:
452: /* Prefix key already pressed. Reset prefix and lookup key. */
453: c->flags &= ~CLIENT_PREFIX;
1.85 nicm 454: server_status_client(c);
1.18 nicm 455: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
456: /* If repeating, treat this as a key, else ignore. */
457: if (c->flags & CLIENT_REPEAT) {
458: c->flags &= ~CLIENT_REPEAT;
459: if (isprefix)
460: c->flags |= CLIENT_PREFIX;
1.30 nicm 461: else if (!(c->flags & CLIENT_READONLY))
1.82 nicm 462: window_pane_key(wp, s, key);
1.18 nicm 463: }
464: return;
465: }
466:
467: /* If already repeating, but this key can't repeat, skip it. */
468: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
469: c->flags &= ~CLIENT_REPEAT;
470: if (isprefix)
471: c->flags |= CLIENT_PREFIX;
1.30 nicm 472: else if (!(c->flags & CLIENT_READONLY))
1.82 nicm 473: window_pane_key(wp, s, key);
1.18 nicm 474: return;
475: }
476:
477: /* If this key can repeat, reset the repeat flags and timer. */
1.82 nicm 478: xtimeout = options_get_number(&s->options, "repeat-time");
1.18 nicm 479: if (xtimeout != 0 && bd->can_repeat) {
480: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25 nicm 481:
1.18 nicm 482: tv.tv_sec = xtimeout / 1000;
483: tv.tv_usec = (xtimeout % 1000) * 1000L;
484: evtimer_del(&c->repeat_timer);
485: evtimer_add(&c->repeat_timer, &tv);
486: }
487:
488: /* Dispatch the command. */
489: key_bindings_dispatch(bd, c);
490: }
491:
1.2 nicm 492: /* Client functions that need to happen every loop. */
493: void
494: server_client_loop(void)
495: {
496: struct client *c;
497: struct window *w;
498: struct window_pane *wp;
499: u_int i;
500:
501: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
502: c = ARRAY_ITEM(&clients, i);
1.36 nicm 503: if (c == NULL)
1.2 nicm 504: continue;
505:
1.36 nicm 506: server_client_check_exit(c);
507: if (c->session != NULL) {
508: server_client_check_redraw(c);
509: server_client_reset_state(c);
510: }
1.2 nicm 511: }
512:
513: /*
514: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 515: * their flags now. Also check pane focus and resize.
1.2 nicm 516: */
517: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
518: w = ARRAY_ITEM(&windows, i);
519: if (w == NULL)
520: continue;
521:
522: w->flags &= ~WINDOW_REDRAW;
1.91 nicm 523: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 524: if (wp->fd != -1) {
525: server_client_check_focus(wp);
526: server_client_check_resize(wp);
527: }
1.2 nicm 528: wp->flags &= ~PANE_REDRAW;
1.91 nicm 529: }
1.2 nicm 530: }
1.92 nicm 531: }
532:
533: /* Check if pane should be resized. */
534: void
535: server_client_check_resize(struct window_pane *wp)
536: {
537: struct winsize ws;
538:
1.101 nicm 539: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 540: return;
541:
542: memset(&ws, 0, sizeof ws);
543: ws.ws_col = wp->sx;
544: ws.ws_row = wp->sy;
545:
1.100 nicm 546: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 547: fatal("ioctl failed");
548:
549: wp->flags &= ~PANE_RESIZE;
1.91 nicm 550: }
551:
552: /* Check whether pane should be focused. */
553: void
554: server_client_check_focus(struct window_pane *wp)
555: {
1.93 nicm 556: u_int i;
557: struct client *c;
1.102 nicm 558: int push;
1.103 nicm 559:
560: /* Are focus events off? */
561: if (!options_get_number(&global_options, "focus-events"))
562: return;
1.102 nicm 563:
564: /* Do we need to push the focus state? */
565: push = wp->flags & PANE_FOCUSPUSH;
566: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 567:
568: /* If we don't care about focus, forget it. */
569: if (!(wp->base.mode & MODE_FOCUSON))
570: return;
571:
572: /* If we're not the active pane in our window, we're not focused. */
573: if (wp->window->active != wp)
574: goto not_focused;
575:
576: /* If we're in a mode, we're not focused. */
577: if (wp->screen != &wp->base)
578: goto not_focused;
579:
580: /*
1.93 nicm 581: * If our window is the current window in any focused clients with an
582: * attached session, we're focused.
1.91 nicm 583: */
1.93 nicm 584: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
585: c = ARRAY_ITEM(&clients, i);
586: if (c == NULL || c->session == NULL)
587: continue;
588:
589: if (!(c->flags & CLIENT_FOCUSED))
1.91 nicm 590: continue;
1.93 nicm 591: if (c->session->flags & SESSION_UNATTACHED)
592: continue;
593:
594: if (c->session->curw->window == wp->window)
1.91 nicm 595: goto focused;
596: }
597:
598: not_focused:
1.102 nicm 599: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 600: bufferevent_write(wp->event, "\033[O", 3);
601: wp->flags &= ~PANE_FOCUSED;
602: return;
603:
604: focused:
1.102 nicm 605: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 606: bufferevent_write(wp->event, "\033[I", 3);
607: wp->flags |= PANE_FOCUSED;
1.2 nicm 608: }
609:
1.18 nicm 610: /*
611: * Update cursor position and mode settings. The scroll region and attributes
612: * are cleared when idle (waiting for an event) as this is the most likely time
613: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
614: * compromise between excessive resets and likelihood of an interrupt.
615: *
616: * tty_region/tty_reset/tty_update_mode already take care of not resetting
617: * things that are already in their default state.
618: */
1.1 nicm 619: void
1.18 nicm 620: server_client_reset_state(struct client *c)
1.1 nicm 621: {
1.18 nicm 622: struct window *w = c->session->curw->window;
623: struct window_pane *wp = w->active;
624: struct screen *s = wp->screen;
625: struct options *oo = &c->session->options;
1.54 nicm 626: struct options *wo = &w->options;
1.66 nicm 627: int status, mode, o;
1.60 nicm 628:
629: if (c->flags & CLIENT_SUSPENDED)
630: return;
1.1 nicm 631:
1.86 nicm 632: if (c->flags & CLIENT_CONTROL)
633: return;
634:
1.1 nicm 635: tty_region(&c->tty, 0, c->tty.sy - 1);
636:
637: status = options_get_number(oo, "status");
638: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
639: tty_cursor(&c->tty, 0, 0);
1.66 nicm 640: else {
1.126 nicm 641: o = status && options_get_number(oo, "status-position") == 0;
1.66 nicm 642: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
643: }
1.1 nicm 644:
1.50 nicm 645: /*
1.57 nicm 646: * Resizing panes with the mouse requires at least button mode to give
647: * a smooth appearance.
648: */
649: mode = s->mode;
1.81 nicm 650: if ((c->tty.mouse.flags & MOUSE_RESIZE_PANE) &&
1.123 nicm 651: !(mode & MODE_MOUSE_BUTTON))
1.57 nicm 652: mode |= MODE_MOUSE_BUTTON;
653:
654: /*
1.50 nicm 655: * Any mode will do for mouse-select-pane, but set standard mode if
656: * none.
657: */
1.54 nicm 658: if ((mode & ALL_MOUSE_MODES) == 0) {
659: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55 nicm 660: options_get_number(oo, "mouse-select-pane"))
1.57 nicm 661: mode |= MODE_MOUSE_STANDARD;
662: else if (options_get_number(oo, "mouse-resize-pane"))
1.54 nicm 663: mode |= MODE_MOUSE_STANDARD;
664: else if (options_get_number(oo, "mouse-select-window"))
665: mode |= MODE_MOUSE_STANDARD;
666: else if (options_get_number(wo, "mode-mouse"))
667: mode |= MODE_MOUSE_STANDARD;
668: }
1.48 nicm 669:
670: /*
671: * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
672: * user has set mouse-utf8 and any mouse mode is in effect, turn on
673: * UTF-8 mouse input. If the receiving terminal hasn't requested it
674: * (that is, it isn't in s->mode), then it'll be converted in
675: * input_mouse.
676: */
677: if ((c->tty.flags & TTY_UTF8) &&
678: (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
679: mode |= MODE_MOUSE_UTF8;
680: else
681: mode &= ~MODE_MOUSE_UTF8;
682:
683: /* Set the terminal mode and reset attributes. */
1.59 nicm 684: tty_update_mode(&c->tty, mode, s);
1.1 nicm 685: tty_reset(&c->tty);
1.17 nicm 686: }
687:
688: /* Repeat time callback. */
689: void
690: server_client_repeat_timer(unused int fd, unused short events, void *data)
691: {
692: struct client *c = data;
693:
1.85 nicm 694: if (c->flags & CLIENT_REPEAT) {
695: if (c->flags & CLIENT_PREFIX)
696: server_status_client(c);
1.17 nicm 697: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.85 nicm 698: }
1.1 nicm 699: }
700:
1.36 nicm 701: /* Check if client should be exited. */
702: void
703: server_client_check_exit(struct client *c)
704: {
705: if (!(c->flags & CLIENT_EXIT))
706: return;
707:
1.73 nicm 708: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
709: return;
710: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 711: return;
1.73 nicm 712: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 713: return;
714:
1.107 nicm 715: server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval);
1.36 nicm 716: c->flags &= ~CLIENT_EXIT;
1.38 nicm 717: }
718:
1.1 nicm 719: /* Check for client redraws. */
720: void
721: server_client_check_redraw(struct client *c)
722: {
723: struct session *s = c->session;
724: struct window_pane *wp;
725: int flags, redraw;
726:
1.86 nicm 727: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 728: return;
729:
1.1 nicm 730: flags = c->tty.flags & TTY_FREEZE;
731: c->tty.flags &= ~TTY_FREEZE;
732:
733: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
734: if (options_get_number(&s->options, "set-titles"))
735: server_client_set_title(c);
1.12 nicm 736:
1.1 nicm 737: if (c->message_string != NULL)
738: redraw = status_message_redraw(c);
739: else if (c->prompt_string != NULL)
740: redraw = status_prompt_redraw(c);
741: else
742: redraw = status_redraw(c);
743: if (!redraw)
744: c->flags &= ~CLIENT_STATUS;
745: }
746:
747: if (c->flags & CLIENT_REDRAW) {
1.115 nicm 748: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 749: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 750: } else if (c->flags & CLIENT_REDRAWWINDOW) {
751: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
752: screen_redraw_pane(c, wp);
753: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 754: } else {
755: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
756: if (wp->flags & PANE_REDRAW)
757: screen_redraw_pane(c, wp);
758: }
759: }
760:
1.27 nicm 761: if (c->flags & CLIENT_BORDERS)
1.115 nicm 762: screen_redraw_screen(c, 0, 0, 1);
1.27 nicm 763:
1.1 nicm 764: if (c->flags & CLIENT_STATUS)
1.115 nicm 765: screen_redraw_screen(c, 0, 1, 0);
1.1 nicm 766:
767: c->tty.flags |= flags;
768:
1.27 nicm 769: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 770: }
771:
772: /* Set client title. */
773: void
774: server_client_set_title(struct client *c)
775: {
1.128 nicm 776: struct session *s = c->session;
777: const char *template;
778: char *title;
779: struct format_tree *ft;
1.1 nicm 780:
781: template = options_get_string(&s->options, "set-titles-string");
1.25 nicm 782:
1.128 nicm 783: ft = format_create();
784: format_defaults(ft, c, NULL, NULL, NULL);
785:
786: title = format_expand_time(ft, template, time(NULL));
1.1 nicm 787: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 788: free(c->title);
1.1 nicm 789: c->title = xstrdup(title);
790: tty_set_title(&c->tty, c->title);
791: }
1.77 nicm 792: free(title);
1.128 nicm 793:
794: format_free(ft);
1.1 nicm 795: }
796:
797: /* Dispatch message from client. */
798: int
799: server_client_msg_dispatch(struct client *c)
800: {
801: struct imsg imsg;
1.73 nicm 802: struct msg_stdin_data stdindata;
1.108 nicm 803: const char *data;
1.1 nicm 804: ssize_t n, datalen;
805:
1.8 deraadt 806: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
807: return (-1);
1.1 nicm 808:
809: for (;;) {
810: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
811: return (-1);
812: if (n == 0)
813: return (0);
1.108 nicm 814:
815: data = imsg.data;
1.1 nicm 816: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
817:
818: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
819: server_write_client(c, MSG_VERSION, NULL, 0);
820: c->flags |= CLIENT_BAD;
1.112 nicm 821: if (imsg.fd != -1)
822: close(imsg.fd);
1.1 nicm 823: imsg_free(&imsg);
824: continue;
825: }
826:
1.129 ! nicm 827: log_debug("got %u from client %d", imsg.hdr.type, c->ibuf.fd);
1.1 nicm 828: switch (imsg.hdr.type) {
1.109 nicm 829: case MSG_IDENTIFY_FLAGS:
830: case MSG_IDENTIFY_TERM:
831: case MSG_IDENTIFY_TTYNAME:
832: case MSG_IDENTIFY_CWD:
833: case MSG_IDENTIFY_STDIN:
834: case MSG_IDENTIFY_ENVIRON:
835: case MSG_IDENTIFY_DONE:
836: server_client_msg_identify(c, &imsg);
1.1 nicm 837: break;
1.108 nicm 838: case MSG_COMMAND:
839: server_client_msg_command(c, &imsg);
840: break;
1.73 nicm 841: case MSG_STDIN:
842: if (datalen != sizeof stdindata)
843: fatalx("bad MSG_STDIN size");
1.108 nicm 844: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 845:
1.73 nicm 846: if (c->stdin_callback == NULL)
847: break;
848: if (stdindata.size <= 0)
849: c->stdin_closed = 1;
850: else {
851: evbuffer_add(c->stdin_data, stdindata.data,
852: stdindata.size);
853: }
854: c->stdin_callback(c, c->stdin_closed,
855: c->stdin_callback_data);
1.33 nicm 856: break;
1.1 nicm 857: case MSG_RESIZE:
858: if (datalen != 0)
859: fatalx("bad MSG_RESIZE size");
860:
1.86 nicm 861: if (c->flags & CLIENT_CONTROL)
862: break;
1.32 nicm 863: if (tty_resize(&c->tty)) {
864: recalculate_sizes();
865: server_redraw_client(c);
866: }
1.1 nicm 867: break;
868: case MSG_EXITING:
869: if (datalen != 0)
870: fatalx("bad MSG_EXITING size");
871:
872: c->session = NULL;
873: tty_close(&c->tty);
874: server_write_client(c, MSG_EXITED, NULL, 0);
875: break;
876: case MSG_WAKEUP:
877: case MSG_UNLOCK:
878: if (datalen != 0)
879: fatalx("bad MSG_WAKEUP size");
880:
881: if (!(c->flags & CLIENT_SUSPENDED))
882: break;
883: c->flags &= ~CLIENT_SUSPENDED;
1.121 nicm 884:
885: if (c->tty.fd == -1) /* exited in the meantime */
886: break;
1.10 nicm 887:
1.11 nicm 888: if (gettimeofday(&c->activity_time, NULL) != 0)
889: fatal("gettimeofday");
1.47 nicm 890: if (c->session != NULL)
891: session_update_activity(c->session);
1.10 nicm 892:
1.1 nicm 893: tty_start_tty(&c->tty);
894: server_redraw_client(c);
895: recalculate_sizes();
896: break;
897: case MSG_SHELL:
898: if (datalen != 0)
899: fatalx("bad MSG_SHELL size");
900:
901: server_client_msg_shell(c);
902: break;
903: }
904:
905: imsg_free(&imsg);
906: }
907: }
908:
909: /* Handle command message. */
910: void
1.108 nicm 911: server_client_msg_command(struct client *c, struct imsg *imsg)
1.1 nicm 912: {
1.108 nicm 913: struct msg_command_data data;
914: char *buf;
915: size_t len;
916: struct cmd_list *cmdlist = NULL;
917: int argc;
918: char **argv, *cause;
919:
920: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
921: fatalx("bad MSG_COMMAND size");
922: memcpy(&data, imsg->data, sizeof data);
923:
1.124 nicm 924: buf = (char *)imsg->data + sizeof data;
1.108 nicm 925: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
926: if (len > 0 && buf[len - 1] != '\0')
927: fatalx("bad MSG_COMMAND string");
928:
929: argc = data.argc;
930: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94 nicm 931: cmdq_error(c->cmdq, "command too long");
1.1 nicm 932: goto error;
933: }
934:
935: if (argc == 0) {
936: argc = 1;
937: argv = xcalloc(1, sizeof *argv);
938: *argv = xstrdup("new-session");
939: }
940:
1.94 nicm 941: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
942: cmdq_error(c->cmdq, "%s", cause);
1.1 nicm 943: cmd_free_argv(argc, argv);
944: goto error;
945: }
946: cmd_free_argv(argc, argv);
947:
1.113 nicm 948: if (c != cfg_client || cfg_finished)
949: cmdq_run(c->cmdq, cmdlist);
950: else
951: cmdq_append(c->cmdq, cmdlist);
1.1 nicm 952: cmd_list_free(cmdlist);
953: return;
954:
955: error:
956: if (cmdlist != NULL)
957: cmd_list_free(cmdlist);
1.88 nicm 958:
1.36 nicm 959: c->flags |= CLIENT_EXIT;
1.1 nicm 960: }
961:
962: /* Handle identify message. */
963: void
1.109 nicm 964: server_client_msg_identify(struct client *c, struct imsg *imsg)
1.1 nicm 965: {
1.109 nicm 966: const char *data;
967: size_t datalen;
968: int flags;
969:
970: if (c->flags & CLIENT_IDENTIFIED)
971: fatalx("out-of-order identify message");
972:
973: data = imsg->data;
974: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
975:
976: switch (imsg->hdr.type) {
977: case MSG_IDENTIFY_FLAGS:
978: if (datalen != sizeof flags)
979: fatalx("bad MSG_IDENTIFY_FLAGS size");
980: memcpy(&flags, data, sizeof flags);
981: c->flags |= flags;
982: break;
983: case MSG_IDENTIFY_TERM:
1.110 nicm 984: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 985: fatalx("bad MSG_IDENTIFY_TERM string");
986: c->term = xstrdup(data);
987: break;
988: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 989: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 990: fatalx("bad MSG_IDENTIFY_TTYNAME string");
991: c->ttyname = xstrdup(data);
992: break;
993: case MSG_IDENTIFY_CWD:
994: if (datalen != 0)
995: fatalx("bad MSG_IDENTIFY_CWD size");
996: c->cwd = imsg->fd;
997: break;
998: case MSG_IDENTIFY_STDIN:
999: if (datalen != 0)
1000: fatalx("bad MSG_IDENTIFY_STDIN size");
1001: c->fd = imsg->fd;
1002: break;
1003: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 1004: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 1005: fatalx("bad MSG_IDENTIFY_ENVIRON string");
1006: if (strchr(data, '=') != NULL)
1007: environ_put(&c->environ, data);
1008: break;
1009: default:
1010: break;
1011: }
1012:
1013: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1014: return;
1015: c->flags |= CLIENT_IDENTIFIED;
1016:
1017: #ifdef __CYGWIN__
1018: c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
1019: c->cwd = open(".", O_RDONLY);
1020: #endif
1.75 nicm 1021:
1.109 nicm 1022: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1023: c->stdin_callback = control_callback;
1.109 nicm 1024:
1.97 nicm 1025: evbuffer_free(c->stderr_data);
1026: c->stderr_data = c->stdout_data;
1.109 nicm 1027:
1028: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1029: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.79 nicm 1030: server_write_client(c, MSG_STDIN, NULL, 0);
1.75 nicm 1031:
1032: c->tty.fd = -1;
1033: c->tty.log_fd = -1;
1034:
1.109 nicm 1035: close(c->fd);
1036: c->fd = -1;
1037:
1.75 nicm 1038: return;
1039: }
1.1 nicm 1040:
1.109 nicm 1041: if (c->fd == -1)
1.104 nicm 1042: return;
1.109 nicm 1043: if (!isatty(c->fd)) {
1044: close(c->fd);
1045: c->fd = -1;
1.80 nicm 1046: return;
1047: }
1.109 nicm 1048: tty_init(&c->tty, c, c->fd, c->term);
1049: if (c->flags & CLIENT_UTF8)
1.1 nicm 1050: c->tty.flags |= TTY_UTF8;
1.109 nicm 1051: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1052: c->tty.term_flags |= TERM_256COLOURS;
1053:
1054: tty_resize(&c->tty);
1055:
1.109 nicm 1056: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1057: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1058: }
1059:
1060: /* Handle shell message. */
1061: void
1062: server_client_msg_shell(struct client *c)
1063: {
1.107 nicm 1064: const char *shell;
1.25 nicm 1065:
1.1 nicm 1066: shell = options_get_string(&global_s_options, "default-shell");
1067: if (*shell == '\0' || areshell(shell))
1068: shell = _PATH_BSHELL;
1.107 nicm 1069: server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1);
1.25 nicm 1070:
1.1 nicm 1071: c->flags |= CLIENT_BAD; /* it will die after exec */
1072: }