Annotation of src/usr.bin/tmux/server-client.c, Revision 1.120
1.120 ! nicm 1: /* $OpenBSD: server-client.c,v 1.119 2014/02/23 00:53:06 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)
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.114 benno 227: if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0 &&
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.66 nicm 332: server_redraw_window_borders(wp->window);
333: wp = wp->window->active; /* may have changed */
334: }
335:
336: /* Check if trying to resize pane. */
337: if (options_get_number(oo, "mouse-resize-pane"))
1.81 nicm 338: layout_resize_pane_mouse(c);
1.66 nicm 339:
340: /* Update last and pass through to client. */
1.81 nicm 341: window_pane_mouse(wp, c->session, m);
1.66 nicm 342: }
343:
1.82 nicm 344: /* Is this fast enough to probably be a paste? */
345: int
346: server_client_assume_paste(struct session *s)
347: {
348: struct timeval tv;
1.84 nicm 349: int t;
1.82 nicm 350:
351: if ((t = options_get_number(&s->options, "assume-paste-time")) == 0)
1.83 nicm 352: return (0);
1.82 nicm 353:
354: timersub(&s->activity_time, &s->last_activity_time, &tv);
355: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000)
1.83 nicm 356: return (1);
357: return (0);
1.82 nicm 358: }
359:
1.18 nicm 360: /* Handle data key input from client. */
361: void
1.74 nicm 362: server_client_handle_key(struct client *c, int key)
1.18 nicm 363: {
364: struct session *s;
365: struct window *w;
366: struct window_pane *wp;
367: struct timeval tv;
368: struct key_binding *bd;
1.82 nicm 369: int xtimeout, isprefix, ispaste;
1.18 nicm 370:
371: /* Check the client is good to accept input. */
372: if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
373: return;
1.86 nicm 374:
1.18 nicm 375: if (c->session == NULL)
376: return;
377: s = c->session;
378:
379: /* Update the activity timer. */
380: if (gettimeofday(&c->activity_time, NULL) != 0)
381: fatal("gettimeofday failed");
1.82 nicm 382:
383: memcpy(&s->last_activity_time, &s->activity_time,
384: sizeof s->last_activity_time);
1.18 nicm 385: memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
386:
387: w = c->session->curw->window;
388: wp = w->active;
389:
390: /* Special case: number keys jump to pane in identify mode. */
1.25 nicm 391: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 392: if (c->flags & CLIENT_READONLY)
393: return;
1.95 nicm 394: window_unzoom(w);
1.18 nicm 395: wp = window_pane_at_index(w, key - '0');
396: if (wp != NULL && window_pane_visible(wp))
397: window_set_active_pane(w, wp);
398: server_clear_identify(c);
399: return;
400: }
401:
402: /* Handle status line. */
1.30 nicm 403: if (!(c->flags & CLIENT_READONLY)) {
404: status_message_clear(c);
405: server_clear_identify(c);
406: }
1.18 nicm 407: if (c->prompt_string != NULL) {
1.30 nicm 408: if (!(c->flags & CLIENT_READONLY))
409: status_prompt_key(c, key);
1.18 nicm 410: return;
411: }
412:
413: /* Check for mouse keys. */
414: if (key == KEYC_MOUSE) {
1.30 nicm 415: if (c->flags & CLIENT_READONLY)
416: return;
1.81 nicm 417: server_client_check_mouse(c, wp);
1.18 nicm 418: return;
419: }
420:
421: /* Is this a prefix key? */
1.82 nicm 422: if (key == options_get_number(&s->options, "prefix"))
1.63 nicm 423: isprefix = 1;
1.82 nicm 424: else if (key == options_get_number(&s->options, "prefix2"))
1.63 nicm 425: isprefix = 1;
426: else
427: isprefix = 0;
1.18 nicm 428:
1.82 nicm 429: /* Treat prefix as a regular key when pasting is detected. */
430: ispaste = server_client_assume_paste(s);
431: if (ispaste)
432: isprefix = 0;
433:
1.18 nicm 434: /* No previous prefix key. */
435: if (!(c->flags & CLIENT_PREFIX)) {
1.82 nicm 436: if (isprefix) {
1.18 nicm 437: c->flags |= CLIENT_PREFIX;
1.85 nicm 438: server_status_client(c);
1.82 nicm 439: return;
1.18 nicm 440: }
1.82 nicm 441:
442: /* Try as a non-prefix key binding. */
443: if (ispaste || (bd = key_bindings_lookup(key)) == NULL) {
444: if (!(c->flags & CLIENT_READONLY))
445: window_pane_key(wp, s, key);
446: } else
447: key_bindings_dispatch(bd, c);
1.18 nicm 448: return;
449: }
450:
451: /* Prefix key already pressed. Reset prefix and lookup key. */
452: c->flags &= ~CLIENT_PREFIX;
1.85 nicm 453: server_status_client(c);
1.18 nicm 454: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
455: /* If repeating, treat this as a key, else ignore. */
456: if (c->flags & CLIENT_REPEAT) {
457: c->flags &= ~CLIENT_REPEAT;
458: if (isprefix)
459: c->flags |= CLIENT_PREFIX;
1.30 nicm 460: else if (!(c->flags & CLIENT_READONLY))
1.82 nicm 461: window_pane_key(wp, s, key);
1.18 nicm 462: }
463: return;
464: }
465:
466: /* If already repeating, but this key can't repeat, skip it. */
467: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
468: c->flags &= ~CLIENT_REPEAT;
469: if (isprefix)
470: c->flags |= CLIENT_PREFIX;
1.30 nicm 471: else if (!(c->flags & CLIENT_READONLY))
1.82 nicm 472: window_pane_key(wp, s, key);
1.18 nicm 473: return;
474: }
475:
476: /* If this key can repeat, reset the repeat flags and timer. */
1.82 nicm 477: xtimeout = options_get_number(&s->options, "repeat-time");
1.18 nicm 478: if (xtimeout != 0 && bd->can_repeat) {
479: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25 nicm 480:
1.18 nicm 481: tv.tv_sec = xtimeout / 1000;
482: tv.tv_usec = (xtimeout % 1000) * 1000L;
483: evtimer_del(&c->repeat_timer);
484: evtimer_add(&c->repeat_timer, &tv);
485: }
486:
487: /* Dispatch the command. */
488: key_bindings_dispatch(bd, c);
489: }
490:
1.2 nicm 491: /* Client functions that need to happen every loop. */
492: void
493: server_client_loop(void)
494: {
495: struct client *c;
496: struct window *w;
497: struct window_pane *wp;
498: u_int i;
499:
500: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
501: c = ARRAY_ITEM(&clients, i);
1.36 nicm 502: if (c == NULL)
1.2 nicm 503: continue;
504:
1.36 nicm 505: server_client_check_exit(c);
506: if (c->session != NULL) {
507: server_client_check_redraw(c);
508: server_client_reset_state(c);
509: }
1.2 nicm 510: }
511:
512: /*
513: * Any windows will have been redrawn as part of clients, so clear
1.92 nicm 514: * their flags now. Also check pane focus and resize.
1.2 nicm 515: */
516: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
517: w = ARRAY_ITEM(&windows, i);
518: if (w == NULL)
519: continue;
520:
521: w->flags &= ~WINDOW_REDRAW;
1.91 nicm 522: TAILQ_FOREACH(wp, &w->panes, entry) {
1.101 nicm 523: if (wp->fd != -1) {
524: server_client_check_focus(wp);
525: server_client_check_resize(wp);
526: }
1.2 nicm 527: wp->flags &= ~PANE_REDRAW;
1.91 nicm 528: }
1.2 nicm 529: }
1.92 nicm 530: }
531:
532: /* Check if pane should be resized. */
533: void
534: server_client_check_resize(struct window_pane *wp)
535: {
536: struct winsize ws;
537:
1.101 nicm 538: if (!(wp->flags & PANE_RESIZE))
1.92 nicm 539: return;
540:
541: memset(&ws, 0, sizeof ws);
542: ws.ws_col = wp->sx;
543: ws.ws_row = wp->sy;
544:
1.100 nicm 545: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
1.92 nicm 546: fatal("ioctl failed");
547:
548: wp->flags &= ~PANE_RESIZE;
1.91 nicm 549: }
550:
551: /* Check whether pane should be focused. */
552: void
553: server_client_check_focus(struct window_pane *wp)
554: {
1.93 nicm 555: u_int i;
556: struct client *c;
1.102 nicm 557: int push;
1.103 nicm 558:
559: /* Are focus events off? */
560: if (!options_get_number(&global_options, "focus-events"))
561: return;
1.102 nicm 562:
563: /* Do we need to push the focus state? */
564: push = wp->flags & PANE_FOCUSPUSH;
565: wp->flags &= ~PANE_FOCUSPUSH;
1.91 nicm 566:
567: /* If we don't care about focus, forget it. */
568: if (!(wp->base.mode & MODE_FOCUSON))
569: return;
570:
571: /* If we're not the active pane in our window, we're not focused. */
572: if (wp->window->active != wp)
573: goto not_focused;
574:
575: /* If we're in a mode, we're not focused. */
576: if (wp->screen != &wp->base)
577: goto not_focused;
578:
579: /*
1.93 nicm 580: * If our window is the current window in any focused clients with an
581: * attached session, we're focused.
1.91 nicm 582: */
1.93 nicm 583: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
584: c = ARRAY_ITEM(&clients, i);
585: if (c == NULL || c->session == NULL)
586: continue;
587:
588: if (!(c->flags & CLIENT_FOCUSED))
1.91 nicm 589: continue;
1.93 nicm 590: if (c->session->flags & SESSION_UNATTACHED)
591: continue;
592:
593: if (c->session->curw->window == wp->window)
1.91 nicm 594: goto focused;
595: }
596:
597: not_focused:
1.102 nicm 598: if (push || (wp->flags & PANE_FOCUSED))
1.91 nicm 599: bufferevent_write(wp->event, "\033[O", 3);
600: wp->flags &= ~PANE_FOCUSED;
601: return;
602:
603: focused:
1.102 nicm 604: if (push || !(wp->flags & PANE_FOCUSED))
1.91 nicm 605: bufferevent_write(wp->event, "\033[I", 3);
606: wp->flags |= PANE_FOCUSED;
1.2 nicm 607: }
608:
1.18 nicm 609: /*
610: * Update cursor position and mode settings. The scroll region and attributes
611: * are cleared when idle (waiting for an event) as this is the most likely time
612: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
613: * compromise between excessive resets and likelihood of an interrupt.
614: *
615: * tty_region/tty_reset/tty_update_mode already take care of not resetting
616: * things that are already in their default state.
617: */
1.1 nicm 618: void
1.18 nicm 619: server_client_reset_state(struct client *c)
1.1 nicm 620: {
1.18 nicm 621: struct window *w = c->session->curw->window;
622: struct window_pane *wp = w->active;
623: struct screen *s = wp->screen;
624: struct options *oo = &c->session->options;
1.54 nicm 625: struct options *wo = &w->options;
1.66 nicm 626: int status, mode, o;
1.60 nicm 627:
628: if (c->flags & CLIENT_SUSPENDED)
629: return;
1.1 nicm 630:
1.86 nicm 631: if (c->flags & CLIENT_CONTROL)
632: return;
633:
1.1 nicm 634: tty_region(&c->tty, 0, c->tty.sy - 1);
635:
636: status = options_get_number(oo, "status");
637: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
638: tty_cursor(&c->tty, 0, 0);
1.66 nicm 639: else {
640: o = status && options_get_number (oo, "status-position") == 0;
641: tty_cursor(&c->tty, wp->xoff + s->cx, o + wp->yoff + s->cy);
642: }
1.1 nicm 643:
1.50 nicm 644: /*
1.57 nicm 645: * Resizing panes with the mouse requires at least button mode to give
646: * a smooth appearance.
647: */
648: mode = s->mode;
1.81 nicm 649: if ((c->tty.mouse.flags & MOUSE_RESIZE_PANE) &&
1.57 nicm 650: !(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ANY)))
651: mode |= MODE_MOUSE_BUTTON;
652:
653: /*
1.50 nicm 654: * Any mode will do for mouse-select-pane, but set standard mode if
655: * none.
656: */
1.54 nicm 657: if ((mode & ALL_MOUSE_MODES) == 0) {
658: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
1.55 nicm 659: options_get_number(oo, "mouse-select-pane"))
1.57 nicm 660: mode |= MODE_MOUSE_STANDARD;
661: else if (options_get_number(oo, "mouse-resize-pane"))
1.54 nicm 662: mode |= MODE_MOUSE_STANDARD;
663: else if (options_get_number(oo, "mouse-select-window"))
664: mode |= MODE_MOUSE_STANDARD;
665: else if (options_get_number(wo, "mode-mouse"))
666: mode |= MODE_MOUSE_STANDARD;
667: }
1.48 nicm 668:
669: /*
670: * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
671: * user has set mouse-utf8 and any mouse mode is in effect, turn on
672: * UTF-8 mouse input. If the receiving terminal hasn't requested it
673: * (that is, it isn't in s->mode), then it'll be converted in
674: * input_mouse.
675: */
676: if ((c->tty.flags & TTY_UTF8) &&
677: (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
678: mode |= MODE_MOUSE_UTF8;
679: else
680: mode &= ~MODE_MOUSE_UTF8;
681:
682: /* Set the terminal mode and reset attributes. */
1.59 nicm 683: tty_update_mode(&c->tty, mode, s);
1.1 nicm 684: tty_reset(&c->tty);
1.17 nicm 685: }
686:
687: /* Repeat time callback. */
688: void
689: server_client_repeat_timer(unused int fd, unused short events, void *data)
690: {
691: struct client *c = data;
692:
1.85 nicm 693: if (c->flags & CLIENT_REPEAT) {
694: if (c->flags & CLIENT_PREFIX)
695: server_status_client(c);
1.17 nicm 696: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.85 nicm 697: }
1.1 nicm 698: }
699:
1.36 nicm 700: /* Check if client should be exited. */
701: void
702: server_client_check_exit(struct client *c)
703: {
704: if (!(c->flags & CLIENT_EXIT))
705: return;
706:
1.73 nicm 707: if (EVBUFFER_LENGTH(c->stdin_data) != 0)
708: return;
709: if (EVBUFFER_LENGTH(c->stdout_data) != 0)
1.36 nicm 710: return;
1.73 nicm 711: if (EVBUFFER_LENGTH(c->stderr_data) != 0)
1.36 nicm 712: return;
713:
1.107 nicm 714: server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval);
1.36 nicm 715: c->flags &= ~CLIENT_EXIT;
1.38 nicm 716: }
717:
1.1 nicm 718: /* Check for client redraws. */
719: void
720: server_client_check_redraw(struct client *c)
721: {
722: struct session *s = c->session;
723: struct window_pane *wp;
724: int flags, redraw;
725:
1.86 nicm 726: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 727: return;
728:
1.1 nicm 729: flags = c->tty.flags & TTY_FREEZE;
730: c->tty.flags &= ~TTY_FREEZE;
731:
732: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
733: if (options_get_number(&s->options, "set-titles"))
734: server_client_set_title(c);
1.12 nicm 735:
1.1 nicm 736: if (c->message_string != NULL)
737: redraw = status_message_redraw(c);
738: else if (c->prompt_string != NULL)
739: redraw = status_prompt_redraw(c);
740: else
741: redraw = status_redraw(c);
742: if (!redraw)
743: c->flags &= ~CLIENT_STATUS;
744: }
745:
746: if (c->flags & CLIENT_REDRAW) {
1.115 nicm 747: screen_redraw_screen(c, 1, 1, 1);
1.27 nicm 748: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 749: } else if (c->flags & CLIENT_REDRAWWINDOW) {
750: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
751: screen_redraw_pane(c, wp);
752: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 753: } else {
754: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
755: if (wp->flags & PANE_REDRAW)
756: screen_redraw_pane(c, wp);
757: }
758: }
759:
1.27 nicm 760: if (c->flags & CLIENT_BORDERS)
1.115 nicm 761: screen_redraw_screen(c, 0, 0, 1);
1.27 nicm 762:
1.1 nicm 763: if (c->flags & CLIENT_STATUS)
1.115 nicm 764: screen_redraw_screen(c, 0, 1, 0);
1.1 nicm 765:
766: c->tty.flags |= flags;
767:
1.27 nicm 768: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 769: }
770:
771: /* Set client title. */
772: void
773: server_client_set_title(struct client *c)
774: {
775: struct session *s = c->session;
776: const char *template;
777: char *title;
778:
779: template = options_get_string(&s->options, "set-titles-string");
1.25 nicm 780:
1.52 nicm 781: title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1 nicm 782: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 783: free(c->title);
1.1 nicm 784: c->title = xstrdup(title);
785: tty_set_title(&c->tty, c->title);
786: }
1.77 nicm 787: free(title);
1.1 nicm 788: }
789:
790: /* Dispatch message from client. */
791: int
792: server_client_msg_dispatch(struct client *c)
793: {
794: struct imsg imsg;
1.73 nicm 795: struct msg_stdin_data stdindata;
1.108 nicm 796: const char *data;
1.1 nicm 797: ssize_t n, datalen;
798:
1.8 deraadt 799: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
800: return (-1);
1.1 nicm 801:
802: for (;;) {
803: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
804: return (-1);
805: if (n == 0)
806: return (0);
1.108 nicm 807:
808: data = imsg.data;
1.1 nicm 809: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
810:
811: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
812: server_write_client(c, MSG_VERSION, NULL, 0);
813: c->flags |= CLIENT_BAD;
1.112 nicm 814: if (imsg.fd != -1)
815: close(imsg.fd);
1.1 nicm 816: imsg_free(&imsg);
817: continue;
818: }
819:
820: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
821: switch (imsg.hdr.type) {
1.109 nicm 822: case MSG_IDENTIFY_FLAGS:
823: case MSG_IDENTIFY_TERM:
824: case MSG_IDENTIFY_TTYNAME:
825: case MSG_IDENTIFY_CWD:
826: case MSG_IDENTIFY_STDIN:
827: case MSG_IDENTIFY_ENVIRON:
828: case MSG_IDENTIFY_DONE:
829: server_client_msg_identify(c, &imsg);
1.1 nicm 830: break;
1.108 nicm 831: case MSG_COMMAND:
832: server_client_msg_command(c, &imsg);
833: break;
1.73 nicm 834: case MSG_STDIN:
835: if (datalen != sizeof stdindata)
836: fatalx("bad MSG_STDIN size");
1.108 nicm 837: memcpy(&stdindata, data, sizeof stdindata);
1.36 nicm 838:
1.73 nicm 839: if (c->stdin_callback == NULL)
840: break;
841: if (stdindata.size <= 0)
842: c->stdin_closed = 1;
843: else {
844: evbuffer_add(c->stdin_data, stdindata.data,
845: stdindata.size);
846: }
847: c->stdin_callback(c, c->stdin_closed,
848: c->stdin_callback_data);
1.33 nicm 849: break;
1.1 nicm 850: case MSG_RESIZE:
851: if (datalen != 0)
852: fatalx("bad MSG_RESIZE size");
853:
1.86 nicm 854: if (c->flags & CLIENT_CONTROL)
855: break;
1.32 nicm 856: if (tty_resize(&c->tty)) {
857: recalculate_sizes();
858: server_redraw_client(c);
859: }
1.1 nicm 860: break;
861: case MSG_EXITING:
862: if (datalen != 0)
863: fatalx("bad MSG_EXITING size");
864:
865: c->session = NULL;
866: tty_close(&c->tty);
867: server_write_client(c, MSG_EXITED, NULL, 0);
868: break;
869: case MSG_WAKEUP:
870: case MSG_UNLOCK:
871: if (datalen != 0)
872: fatalx("bad MSG_WAKEUP size");
873:
874: if (!(c->flags & CLIENT_SUSPENDED))
875: break;
876: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 877:
1.11 nicm 878: if (gettimeofday(&c->activity_time, NULL) != 0)
879: fatal("gettimeofday");
1.47 nicm 880: if (c->session != NULL)
881: session_update_activity(c->session);
1.10 nicm 882:
1.1 nicm 883: tty_start_tty(&c->tty);
884: server_redraw_client(c);
885: recalculate_sizes();
886: break;
887: case MSG_SHELL:
888: if (datalen != 0)
889: fatalx("bad MSG_SHELL size");
890:
891: server_client_msg_shell(c);
892: break;
893: }
894:
895: imsg_free(&imsg);
896: }
897: }
898:
899: /* Handle command message. */
900: void
1.108 nicm 901: server_client_msg_command(struct client *c, struct imsg *imsg)
1.1 nicm 902: {
1.108 nicm 903: struct msg_command_data data;
904: char *buf;
905: size_t len;
906: struct cmd_list *cmdlist = NULL;
907: int argc;
908: char **argv, *cause;
909:
910: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
911: fatalx("bad MSG_COMMAND size");
912: memcpy(&data, imsg->data, sizeof data);
913:
914: buf = (char*)imsg->data + sizeof data;
915: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
916: if (len > 0 && buf[len - 1] != '\0')
917: fatalx("bad MSG_COMMAND string");
918:
919: argc = data.argc;
920: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.94 nicm 921: cmdq_error(c->cmdq, "command too long");
1.1 nicm 922: goto error;
923: }
924:
925: if (argc == 0) {
926: argc = 1;
927: argv = xcalloc(1, sizeof *argv);
928: *argv = xstrdup("new-session");
929: }
930:
1.94 nicm 931: if ((cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause)) == NULL) {
932: cmdq_error(c->cmdq, "%s", cause);
1.1 nicm 933: cmd_free_argv(argc, argv);
934: goto error;
935: }
936: cmd_free_argv(argc, argv);
937:
1.113 nicm 938: if (c != cfg_client || cfg_finished)
939: cmdq_run(c->cmdq, cmdlist);
940: else
941: cmdq_append(c->cmdq, cmdlist);
1.1 nicm 942: cmd_list_free(cmdlist);
943: return;
944:
945: error:
946: if (cmdlist != NULL)
947: cmd_list_free(cmdlist);
1.88 nicm 948:
1.36 nicm 949: c->flags |= CLIENT_EXIT;
1.1 nicm 950: }
951:
952: /* Handle identify message. */
953: void
1.109 nicm 954: server_client_msg_identify(struct client *c, struct imsg *imsg)
1.1 nicm 955: {
1.109 nicm 956: const char *data;
957: size_t datalen;
958: int flags;
959:
960: if (c->flags & CLIENT_IDENTIFIED)
961: fatalx("out-of-order identify message");
962:
963: data = imsg->data;
964: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
965:
966: switch (imsg->hdr.type) {
967: case MSG_IDENTIFY_FLAGS:
968: if (datalen != sizeof flags)
969: fatalx("bad MSG_IDENTIFY_FLAGS size");
970: memcpy(&flags, data, sizeof flags);
971: c->flags |= flags;
972: break;
973: case MSG_IDENTIFY_TERM:
1.110 nicm 974: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 975: fatalx("bad MSG_IDENTIFY_TERM string");
976: c->term = xstrdup(data);
977: break;
978: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 979: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 980: fatalx("bad MSG_IDENTIFY_TTYNAME string");
981: c->ttyname = xstrdup(data);
982: break;
983: case MSG_IDENTIFY_CWD:
984: if (datalen != 0)
985: fatalx("bad MSG_IDENTIFY_CWD size");
986: c->cwd = imsg->fd;
987: break;
988: case MSG_IDENTIFY_STDIN:
989: if (datalen != 0)
990: fatalx("bad MSG_IDENTIFY_STDIN size");
991: c->fd = imsg->fd;
992: break;
993: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 994: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 995: fatalx("bad MSG_IDENTIFY_ENVIRON string");
996: if (strchr(data, '=') != NULL)
997: environ_put(&c->environ, data);
998: break;
999: default:
1000: break;
1001: }
1002:
1003: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
1004: return;
1005: c->flags |= CLIENT_IDENTIFIED;
1006:
1007: #ifdef __CYGWIN__
1008: c->fd = open(c->ttyname, O_RDWR|O_NOCTTY);
1009: c->cwd = open(".", O_RDONLY);
1010: #endif
1.75 nicm 1011:
1.109 nicm 1012: if (c->flags & CLIENT_CONTROL) {
1.75 nicm 1013: c->stdin_callback = control_callback;
1.109 nicm 1014:
1.97 nicm 1015: evbuffer_free(c->stderr_data);
1016: c->stderr_data = c->stdout_data;
1.109 nicm 1017:
1018: if (c->flags & CLIENT_CONTROLCONTROL)
1.96 nicm 1019: evbuffer_add_printf(c->stdout_data, "\033P1000p");
1.79 nicm 1020: server_write_client(c, MSG_STDIN, NULL, 0);
1.75 nicm 1021:
1022: c->tty.fd = -1;
1023: c->tty.log_fd = -1;
1024:
1.109 nicm 1025: close(c->fd);
1026: c->fd = -1;
1027:
1.75 nicm 1028: return;
1029: }
1.1 nicm 1030:
1.109 nicm 1031: if (c->fd == -1)
1.104 nicm 1032: return;
1.109 nicm 1033: if (!isatty(c->fd)) {
1034: close(c->fd);
1035: c->fd = -1;
1.80 nicm 1036: return;
1037: }
1.109 nicm 1038: tty_init(&c->tty, c, c->fd, c->term);
1039: if (c->flags & CLIENT_UTF8)
1.1 nicm 1040: c->tty.flags |= TTY_UTF8;
1.109 nicm 1041: if (c->flags & CLIENT_256COLOURS)
1.1 nicm 1042: c->tty.term_flags |= TERM_256COLOURS;
1043:
1044: tty_resize(&c->tty);
1045:
1.109 nicm 1046: if (!(c->flags & CLIENT_CONTROL))
1.86 nicm 1047: c->flags |= CLIENT_TERMINAL;
1.1 nicm 1048: }
1049:
1050: /* Handle shell message. */
1051: void
1052: server_client_msg_shell(struct client *c)
1053: {
1.107 nicm 1054: const char *shell;
1.25 nicm 1055:
1.1 nicm 1056: shell = options_get_string(&global_s_options, "default-shell");
1057: if (*shell == '\0' || areshell(shell))
1058: shell = _PATH_BSHELL;
1.107 nicm 1059: server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1);
1.25 nicm 1060:
1.1 nicm 1061: c->flags |= CLIENT_BAD; /* it will die after exec */
1062: }