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