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