Annotation of src/usr.bin/tmux/window.c, Revision 1.214
1.214 ! nicm 1: /* $OpenBSD: window.c,v 1.213 2018/10/18 08:38:01 nicm Exp $ */
1.1 nicm 2:
3: /*
1.156 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.162 nicm 20: #include <sys/ioctl.h>
1.1 nicm 21:
22: #include <errno.h>
23: #include <fcntl.h>
1.5 nicm 24: #include <fnmatch.h>
1.203 nicm 25: #include <signal.h>
1.1 nicm 26: #include <stdint.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <termios.h>
1.163 nicm 30: #include <time.h>
1.1 nicm 31: #include <unistd.h>
32: #include <util.h>
1.198 nicm 33: #include <vis.h>
1.1 nicm 34:
35: #include "tmux.h"
36:
37: /*
1.14 nicm 38: * Each window is attached to a number of panes, each of which is a pty. This
1.1 nicm 39: * file contains code to handle them.
40: *
41: * A pane has two buffers attached, these are filled and emptied by the main
42: * server poll loop. Output data is received from pty's in screen format,
43: * translated and returned as a series of escape sequences and strings via
44: * input_parse (in input.c). Input data is received as key codes and written
45: * directly via input_key.
46: *
47: * Each pane also has a "virtual" screen (screen.c) which contains the current
48: * state and is redisplayed when the window is reattached to a client.
49: *
50: * Windows are stored directly on a global array and wrapped in any number of
51: * winlink structs to be linked onto local session RB trees. A reference count
52: * is maintained and a window removed from the global list and destroyed when
53: * it reaches zero.
54: */
1.124 nicm 55:
1.1 nicm 56: /* Global window list. */
57: struct windows windows;
58:
1.64 nicm 59: /* Global panes tree. */
60: struct window_pane_tree all_window_panes;
1.166 nicm 61: static u_int next_window_pane_id;
62: static u_int next_window_id;
63: static u_int next_active_point;
1.37 nicm 64:
1.174 nicm 65: static void window_destroy(struct window *);
66:
1.169 nicm 67: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
68: u_int);
69: static void window_pane_destroy(struct window_pane *);
70:
1.166 nicm 71: static void window_pane_read_callback(struct bufferevent *, void *);
72: static void window_pane_error_callback(struct bufferevent *, short, void *);
73:
1.169 nicm 74: static int winlink_next_index(struct winlinks *, int);
75:
1.166 nicm 76: static struct window_pane *window_pane_choose_best(struct window_pane **,
77: u_int);
1.109 nicm 78:
1.122 nicm 79: RB_GENERATE(windows, window, entry, window_cmp);
1.169 nicm 80: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
81: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
1.122 nicm 82:
83: int
84: window_cmp(struct window *w1, struct window *w2)
85: {
86: return (w1->id - w2->id);
87: }
88:
1.1 nicm 89: int
90: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
91: {
92: return (wl1->idx - wl2->idx);
1.12 nicm 93: }
94:
1.64 nicm 95: int
96: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
97: {
98: return (wp1->id - wp2->id);
99: }
100:
1.12 nicm 101: struct winlink *
102: winlink_find_by_window(struct winlinks *wwl, struct window *w)
103: {
104: struct winlink *wl;
105:
106: RB_FOREACH(wl, winlinks, wwl) {
107: if (wl->window == w)
108: return (wl);
109: }
110:
111: return (NULL);
1.1 nicm 112: }
113:
114: struct winlink *
115: winlink_find_by_index(struct winlinks *wwl, int idx)
116: {
117: struct winlink wl;
118:
119: if (idx < 0)
120: fatalx("bad index");
121:
122: wl.idx = idx;
123: return (RB_FIND(winlinks, wwl, &wl));
124: }
125:
1.71 nicm 126: struct winlink *
127: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
128: {
129: struct winlink *wl;
130:
131: RB_FOREACH(wl, winlinks, wwl) {
132: if (wl->window->id == id)
133: return (wl);
134: }
1.78 nicm 135: return (NULL);
1.71 nicm 136: }
137:
1.169 nicm 138: static int
1.22 nicm 139: winlink_next_index(struct winlinks *wwl, int idx)
1.1 nicm 140: {
1.22 nicm 141: int i;
1.1 nicm 142:
1.22 nicm 143: i = idx;
144: do {
1.1 nicm 145: if (winlink_find_by_index(wwl, i) == NULL)
146: return (i);
1.22 nicm 147: if (i == INT_MAX)
148: i = 0;
149: else
150: i++;
151: } while (i != idx);
152: return (-1);
1.1 nicm 153: }
154:
155: u_int
156: winlink_count(struct winlinks *wwl)
157: {
158: struct winlink *wl;
159: u_int n;
160:
161: n = 0;
162: RB_FOREACH(wl, winlinks, wwl)
163: n++;
164:
165: return (n);
166: }
167:
168: struct winlink *
1.63 nicm 169: winlink_add(struct winlinks *wwl, int idx)
1.1 nicm 170: {
171: struct winlink *wl;
172:
1.22 nicm 173: if (idx < 0) {
174: if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
175: return (NULL);
176: } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1 nicm 177: return (NULL);
178:
179: wl = xcalloc(1, sizeof *wl);
180: wl->idx = idx;
181: RB_INSERT(winlinks, wwl, wl);
182:
1.63 nicm 183: return (wl);
184: }
185:
186: void
187: winlink_set_window(struct winlink *wl, struct window *w)
188: {
1.174 nicm 189: if (wl->window != NULL) {
190: TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
1.192 nicm 191: window_remove_ref(wl->window, __func__);
1.174 nicm 192: }
193: TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
1.63 nicm 194: wl->window = w;
1.192 nicm 195: window_add_ref(w, __func__);
1.1 nicm 196: }
197:
198: void
199: winlink_remove(struct winlinks *wwl, struct winlink *wl)
200: {
201: struct window *w = wl->window;
202:
1.174 nicm 203: if (w != NULL) {
204: TAILQ_REMOVE(&w->winlinks, wl, wentry);
1.192 nicm 205: window_remove_ref(w, __func__);
1.174 nicm 206: }
207:
1.1 nicm 208: RB_REMOVE(winlinks, wwl, wl);
1.82 nicm 209: free(wl->status_text);
210: free(wl);
1.1 nicm 211: }
212:
213: struct winlink *
1.41 nicm 214: winlink_next(struct winlink *wl)
1.1 nicm 215: {
216: return (RB_NEXT(winlinks, wwl, wl));
217: }
218:
219: struct winlink *
1.41 nicm 220: winlink_previous(struct winlink *wl)
1.1 nicm 221: {
222: return (RB_PREV(winlinks, wwl, wl));
1.52 nicm 223: }
224:
225: struct winlink *
1.53 nicm 226: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 227: {
228: for (; n > 0; n--) {
229: if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53 nicm 230: wl = RB_MIN(winlinks, &s->windows);
1.52 nicm 231: }
232:
233: return (wl);
234: }
235:
236: struct winlink *
1.53 nicm 237: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 238: {
239: for (; n > 0; n--) {
240: if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53 nicm 241: wl = RB_MAX(winlinks, &s->windows);
1.52 nicm 242: }
243:
244: return (wl);
1.1 nicm 245: }
246:
247: void
248: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
249: {
250: if (wl == NULL)
251: return;
252:
253: winlink_stack_remove(stack, wl);
1.28 nicm 254: TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1 nicm 255: }
256:
257: void
258: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
259: {
260: struct winlink *wl2;
261:
262: if (wl == NULL)
263: return;
1.42 nicm 264:
1.28 nicm 265: TAILQ_FOREACH(wl2, stack, sentry) {
1.1 nicm 266: if (wl2 == wl) {
1.28 nicm 267: TAILQ_REMOVE(stack, wl, sentry);
1.1 nicm 268: return;
269: }
270: }
271: }
272:
273: struct window *
1.125 nicm 274: window_find_by_id_str(const char *s)
1.123 nicm 275: {
276: const char *errstr;
277: u_int id;
278:
279: if (*s != '@')
280: return (NULL);
281:
282: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
283: if (errstr != NULL)
284: return (NULL);
285: return (window_find_by_id(id));
286: }
287:
288: struct window *
1.71 nicm 289: window_find_by_id(u_int id)
290: {
1.122 nicm 291: struct window w;
1.71 nicm 292:
1.122 nicm 293: w.id = id;
294: return (RB_FIND(windows, &windows, &w));
1.71 nicm 295: }
296:
1.143 nicm 297: void
298: window_update_activity(struct window *w)
299: {
300: gettimeofday(&w->activity_time, NULL);
301: alerts_queue(w, WINDOW_ACTIVITY);
302: }
303:
1.71 nicm 304: struct window *
1.171 nicm 305: window_create(u_int sx, u_int sy)
1.1 nicm 306: {
307: struct window *w;
308:
1.38 nicm 309: w = xcalloc(1, sizeof *w);
1.1 nicm 310: w->name = NULL;
1.160 nicm 311: w->flags = WINDOW_STYLECHANGED;
1.1 nicm 312:
313: TAILQ_INIT(&w->panes);
314: w->active = NULL;
1.14 nicm 315:
1.17 nicm 316: w->lastlayout = -1;
1.14 nicm 317: w->layout_root = NULL;
1.42 nicm 318:
1.1 nicm 319: w->sx = sx;
320: w->sy = sy;
1.133 nicm 321:
1.146 nicm 322: w->options = options_create(global_w_options);
1.1 nicm 323:
324: w->references = 0;
1.174 nicm 325: TAILQ_INIT(&w->winlinks);
1.1 nicm 326:
1.122 nicm 327: w->id = next_window_id++;
1.129 nicm 328: RB_INSERT(windows, &windows, w);
1.122 nicm 329:
1.143 nicm 330: window_update_activity(w);
331:
1.1 nicm 332: return (w);
333: }
334:
335: struct window *
1.171 nicm 336: window_create_spawn(const char *name, int argc, char **argv, const char *path,
1.147 nicm 337: const char *shell, const char *cwd, struct environ *env,
338: struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause)
1.1 nicm 339: {
1.14 nicm 340: struct window *w;
341: struct window_pane *wp;
1.1 nicm 342:
1.171 nicm 343: w = window_create(sx, sy);
1.208 nicm 344: wp = window_add_pane(w, NULL, 0, 0, hlimit);
1.93 nicm 345: layout_init(w, wp);
1.91 nicm 346:
1.171 nicm 347: if (window_pane_spawn(wp, argc, argv, path, shell, cwd,
348: env, tio, cause) != 0) {
1.1 nicm 349: window_destroy(w);
350: return (NULL);
351: }
1.91 nicm 352:
1.1 nicm 353: w->active = TAILQ_FIRST(&w->panes);
354: if (name != NULL) {
355: w->name = xstrdup(name);
1.146 nicm 356: options_set_number(w->options, "automatic-rename", 0);
1.1 nicm 357: } else
358: w->name = default_window_name(w);
1.91 nicm 359:
1.193 nicm 360: notify_window("window-pane-changed", w);
361:
1.1 nicm 362: return (w);
363: }
364:
1.174 nicm 365: static void
1.1 nicm 366: window_destroy(struct window *w)
367: {
1.192 nicm 368: log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174 nicm 369:
1.122 nicm 370: RB_REMOVE(windows, &windows, w);
1.1 nicm 371:
1.14 nicm 372: if (w->layout_root != NULL)
1.135 nicm 373: layout_free_cell(w->layout_root);
374: if (w->saved_layout_root != NULL)
375: layout_free_cell(w->saved_layout_root);
1.127 nicm 376: free(w->old_layout);
1.139 nicm 377:
1.141 nicm 378: if (event_initialized(&w->name_event))
379: evtimer_del(&w->name_event);
1.38 nicm 380:
1.143 nicm 381: if (event_initialized(&w->alerts_timer))
382: evtimer_del(&w->alerts_timer);
1.213 nicm 383: if (event_initialized(&w->offset_timer))
384: event_del(&w->offset_timer);
1.143 nicm 385:
1.146 nicm 386: options_free(w->options);
1.1 nicm 387:
388: window_destroy_panes(w);
389:
1.82 nicm 390: free(w->name);
391: free(w);
1.84 nicm 392: }
393:
1.200 nicm 394: int
395: window_pane_destroy_ready(struct window_pane *wp)
396: {
1.201 nicm 397: int n;
398:
399: if (wp->pipe_fd != -1) {
400: if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
401: return (0);
402: if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
403: return (0);
404: }
1.200 nicm 405:
406: if (~wp->flags & PANE_EXITED)
407: return (0);
408: return (1);
409: }
410:
1.84 nicm 411: void
1.192 nicm 412: window_add_ref(struct window *w, const char *from)
413: {
414: w->references++;
415: log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
416: }
417:
418: void
419: window_remove_ref(struct window *w, const char *from)
1.84 nicm 420: {
421: w->references--;
1.192 nicm 422: log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
423:
1.84 nicm 424: if (w->references == 0)
425: window_destroy(w);
1.72 nicm 426: }
427:
428: void
429: window_set_name(struct window *w, const char *new_name)
430: {
1.82 nicm 431: free(w->name);
1.198 nicm 432: utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.172 nicm 433: notify_window("window-renamed", w);
1.1 nicm 434: }
435:
1.15 nicm 436: void
1.1 nicm 437: window_resize(struct window *w, u_int sx, u_int sy)
438: {
439: w->sx = sx;
440: w->sy = sy;
441: }
442:
1.114 nicm 443: int
1.118 nicm 444: window_has_pane(struct window *w, struct window_pane *wp)
445: {
446: struct window_pane *wp1;
447:
448: TAILQ_FOREACH(wp1, &w->panes, entry) {
449: if (wp1 == wp)
450: return (1);
451: }
452: return (0);
453: }
454:
455: int
1.1 nicm 456: window_set_active_pane(struct window *w, struct window_pane *wp)
457: {
1.185 nicm 458: log_debug("%s: pane %%%u (was %%%u)", __func__, wp->id, w->active->id);
1.59 nicm 459: if (wp == w->active)
1.114 nicm 460: return (0);
1.58 nicm 461: w->last = w->active;
1.1 nicm 462: w->active = wp;
1.109 nicm 463: w->active->active_point = next_active_point++;
1.136 nicm 464: w->active->flags |= PANE_CHANGED;
1.213 nicm 465: tty_update_window_offset(w);
1.193 nicm 466: notify_window("window-pane-changed", w);
1.114 nicm 467: return (1);
1.145 nicm 468: }
469:
470: void
471: window_redraw_active_switch(struct window *w, struct window_pane *wp)
472: {
1.177 nicm 473: const struct grid_cell *gc;
1.145 nicm 474:
475: if (wp == w->active)
476: return;
477:
478: /*
479: * If window-style and window-active-style are the same, we don't need
1.177 nicm 480: * to redraw panes when switching active panes.
1.145 nicm 481: */
1.177 nicm 482: gc = options_get_style(w->options, "window-active-style");
483: if (style_equal(gc, options_get_style(w->options, "window-style")))
1.145 nicm 484: return;
1.177 nicm 485:
486: /*
487: * If the now active or inactive pane do not have a custom style or if
488: * the palette is different, they need to be redrawn.
489: */
1.178 nicm 490: if (window_pane_get_palette(w->active, w->active->colgc.fg) != -1 ||
491: window_pane_get_palette(w->active, w->active->colgc.bg) != -1 ||
1.177 nicm 492: style_equal(&grid_default_cell, &w->active->colgc))
1.145 nicm 493: w->active->flags |= PANE_REDRAW;
1.178 nicm 494: if (window_pane_get_palette(wp, wp->colgc.fg) != -1 ||
495: window_pane_get_palette(wp, wp->colgc.bg) != -1 ||
1.177 nicm 496: style_equal(&grid_default_cell, &wp->colgc))
1.145 nicm 497: wp->flags |= PANE_REDRAW;
1.29 nicm 498: }
499:
1.66 nicm 500: struct window_pane *
501: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 502: {
503: struct window_pane *wp;
504:
505: TAILQ_FOREACH(wp, &w->panes, entry) {
1.213 nicm 506: if (!window_pane_visible(wp))
507: continue;
1.66 nicm 508: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 509: continue;
1.66 nicm 510: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 511: continue;
1.66 nicm 512: return (wp);
513: }
514: return (NULL);
515: }
516:
517: struct window_pane *
518: window_find_string(struct window *w, const char *s)
519: {
520: u_int x, y;
521:
522: x = w->sx / 2;
523: y = w->sy / 2;
524:
525: if (strcasecmp(s, "top") == 0)
526: y = 0;
527: else if (strcasecmp(s, "bottom") == 0)
528: y = w->sy - 1;
529: else if (strcasecmp(s, "left") == 0)
530: x = 0;
531: else if (strcasecmp(s, "right") == 0)
532: x = w->sx - 1;
533: else if (strcasecmp(s, "top-left") == 0) {
534: x = 0;
535: y = 0;
536: } else if (strcasecmp(s, "top-right") == 0) {
537: x = w->sx - 1;
538: y = 0;
539: } else if (strcasecmp(s, "bottom-left") == 0) {
540: x = 0;
541: y = w->sy - 1;
542: } else if (strcasecmp(s, "bottom-right") == 0) {
543: x = w->sx - 1;
544: y = w->sy - 1;
545: } else
546: return (NULL);
547:
548: return (window_get_active_at(w, x, y));
1.1 nicm 549: }
550:
1.93 nicm 551: int
552: window_zoom(struct window_pane *wp)
553: {
554: struct window *w = wp->window;
555: struct window_pane *wp1;
556:
557: if (w->flags & WINDOW_ZOOMED)
558: return (-1);
559:
1.94 nicm 560: if (window_count_panes(w) == 1)
561: return (-1);
562:
1.93 nicm 563: if (w->active != wp)
564: window_set_active_pane(w, wp);
565:
566: TAILQ_FOREACH(wp1, &w->panes, entry) {
567: wp1->saved_layout_cell = wp1->layout_cell;
568: wp1->layout_cell = NULL;
569: }
570:
571: w->saved_layout_root = w->layout_root;
572: layout_init(w, wp);
573: w->flags |= WINDOW_ZOOMED;
1.172 nicm 574: notify_window("window-layout-changed", w);
1.93 nicm 575:
576: return (0);
577: }
578:
579: int
580: window_unzoom(struct window *w)
581: {
1.97 nicm 582: struct window_pane *wp;
1.93 nicm 583:
584: if (!(w->flags & WINDOW_ZOOMED))
585: return (-1);
586:
587: w->flags &= ~WINDOW_ZOOMED;
588: layout_free(w);
589: w->layout_root = w->saved_layout_root;
1.120 nicm 590: w->saved_layout_root = NULL;
1.93 nicm 591:
1.97 nicm 592: TAILQ_FOREACH(wp, &w->panes, entry) {
593: wp->layout_cell = wp->saved_layout_cell;
594: wp->saved_layout_cell = NULL;
1.93 nicm 595: }
1.213 nicm 596: layout_fix_panes(w);
1.172 nicm 597: notify_window("window-layout-changed", w);
1.93 nicm 598:
599: return (0);
600: }
601:
1.1 nicm 602: struct window_pane *
1.185 nicm 603: window_add_pane(struct window *w, struct window_pane *other, int before,
1.208 nicm 604: int full_size, u_int hlimit)
1.1 nicm 605: {
606: struct window_pane *wp;
607:
1.185 nicm 608: if (other == NULL)
609: other = w->active;
610:
1.14 nicm 611: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186 nicm 612: if (TAILQ_EMPTY(&w->panes)) {
613: log_debug("%s: @%u at start", __func__, w->id);
1.1 nicm 614: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.186 nicm 615: } else if (before) {
616: log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.208 nicm 617: if (full_size)
618: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
619: else
620: TAILQ_INSERT_BEFORE(other, wp, entry);
1.186 nicm 621: } else {
622: log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.208 nicm 623: if (full_size)
624: TAILQ_INSERT_TAIL(&w->panes, wp, entry);
625: else
626: TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186 nicm 627: }
1.1 nicm 628: return (wp);
629: }
630:
631: void
1.105 nicm 632: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 633: {
1.205 nicm 634: log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
635:
1.152 nicm 636: if (wp == marked_pane.wp)
1.132 nicm 637: server_clear_marked();
638:
1.57 nicm 639: if (wp == w->active) {
1.58 nicm 640: w->active = w->last;
641: w->last = NULL;
642: if (w->active == NULL) {
643: w->active = TAILQ_PREV(wp, window_panes, entry);
644: if (w->active == NULL)
645: w->active = TAILQ_NEXT(wp, entry);
646: }
1.193 nicm 647: if (w->active != NULL) {
1.151 nicm 648: w->active->flags |= PANE_CHANGED;
1.193 nicm 649: notify_window("window-pane-changed", w);
650: }
1.58 nicm 651: } else if (wp == w->last)
652: w->last = NULL;
1.105 nicm 653: }
654:
655: void
656: window_remove_pane(struct window *w, struct window_pane *wp)
657: {
658: window_lost_pane(w, wp);
1.1 nicm 659:
660: TAILQ_REMOVE(&w->panes, wp, entry);
661: window_pane_destroy(wp);
662: }
663:
664: struct window_pane *
665: window_pane_at_index(struct window *w, u_int idx)
666: {
667: struct window_pane *wp;
668: u_int n;
669:
1.146 nicm 670: n = options_get_number(w->options, "pane-base-index");
1.1 nicm 671: TAILQ_FOREACH(wp, &w->panes, entry) {
672: if (n == idx)
673: return (wp);
674: n++;
675: }
676: return (NULL);
1.53 nicm 677: }
678:
679: struct window_pane *
680: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
681: {
682: for (; n > 0; n--) {
683: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
684: wp = TAILQ_FIRST(&w->panes);
685: }
686:
687: return (wp);
688: }
689:
690: struct window_pane *
691: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
692: u_int n)
693: {
694: for (; n > 0; n--) {
695: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
696: wp = TAILQ_LAST(&w->panes, window_panes);
697: }
698:
699: return (wp);
1.13 nicm 700: }
701:
1.69 nicm 702: int
703: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 704: {
705: struct window_pane *wq;
1.69 nicm 706: struct window *w = wp->window;
1.13 nicm 707:
1.146 nicm 708: *i = options_get_number(w->options, "pane-base-index");
1.13 nicm 709: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 710: if (wp == wq) {
711: return (0);
712: }
713: (*i)++;
1.13 nicm 714: }
1.69 nicm 715:
716: return (-1);
1.1 nicm 717: }
718:
719: u_int
720: window_count_panes(struct window *w)
721: {
722: struct window_pane *wp;
723: u_int n;
724:
725: n = 0;
726: TAILQ_FOREACH(wp, &w->panes, entry)
727: n++;
728: return (n);
729: }
730:
731: void
732: window_destroy_panes(struct window *w)
733: {
734: struct window_pane *wp;
735:
736: while (!TAILQ_EMPTY(&w->panes)) {
737: wp = TAILQ_FIRST(&w->panes);
738: TAILQ_REMOVE(&w->panes, wp, entry);
739: window_pane_destroy(wp);
740: }
1.61 nicm 741: }
742:
1.188 nicm 743: const char *
1.189 nicm 744: window_printable_flags(struct winlink *wl)
1.61 nicm 745: {
1.189 nicm 746: struct session *s = wl->session;
747: static char flags[32];
748: int pos;
1.61 nicm 749:
750: pos = 0;
751: if (wl->flags & WINLINK_ACTIVITY)
752: flags[pos++] = '#';
753: if (wl->flags & WINLINK_BELL)
754: flags[pos++] = '!';
755: if (wl->flags & WINLINK_SILENCE)
756: flags[pos++] = '~';
757: if (wl == s->curw)
758: flags[pos++] = '*';
759: if (wl == TAILQ_FIRST(&s->lastw))
760: flags[pos++] = '-';
1.152 nicm 761: if (server_check_marked() && wl == marked_pane.wl)
1.132 nicm 762: flags[pos++] = 'M';
1.93 nicm 763: if (wl->window->flags & WINDOW_ZOOMED)
764: flags[pos++] = 'Z';
1.61 nicm 765: flags[pos] = '\0';
1.188 nicm 766: return (flags);
1.1 nicm 767: }
768:
1.123 nicm 769: struct window_pane *
770: window_pane_find_by_id_str(const char *s)
771: {
772: const char *errstr;
773: u_int id;
774:
775: if (*s != '%')
776: return (NULL);
777:
778: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
779: if (errstr != NULL)
780: return (NULL);
781: return (window_pane_find_by_id(id));
782: }
783:
1.64 nicm 784: struct window_pane *
785: window_pane_find_by_id(u_int id)
786: {
787: struct window_pane wp;
788:
789: wp.id = id;
790: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
791: }
792:
1.169 nicm 793: static struct window_pane *
1.1 nicm 794: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
795: {
796: struct window_pane *wp;
1.140 nicm 797: char host[HOST_NAME_MAX + 1];
1.1 nicm 798:
799: wp = xcalloc(1, sizeof *wp);
800: wp->window = w;
801:
1.71 nicm 802: wp->id = next_window_pane_id++;
1.64 nicm 803: RB_INSERT(window_pane_tree, &all_window_panes, wp);
804:
1.110 nicm 805: wp->argc = 0;
806: wp->argv = NULL;
1.23 nicm 807: wp->shell = NULL;
1.147 nicm 808: wp->cwd = NULL;
1.1 nicm 809:
810: wp->fd = -1;
1.37 nicm 811: wp->event = NULL;
1.1 nicm 812:
813: wp->mode = NULL;
1.176 nicm 814: wp->modeprefix = 1;
1.14 nicm 815:
816: wp->layout_cell = NULL;
1.1 nicm 817:
818: wp->xoff = 0;
1.42 nicm 819: wp->yoff = 0;
1.1 nicm 820:
1.197 nicm 821: wp->sx = wp->osx = sx;
822: wp->sy = wp->osx = sy;
1.1 nicm 823:
1.32 nicm 824: wp->pipe_fd = -1;
825: wp->pipe_off = 0;
1.36 nicm 826: wp->pipe_event = NULL;
1.32 nicm 827:
1.9 nicm 828: wp->saved_grid = NULL;
1.117 nicm 829:
830: memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9 nicm 831:
1.1 nicm 832: screen_init(&wp->base, sx, sy, hlimit);
833: wp->screen = &wp->base;
1.159 nicm 834:
835: screen_init(&wp->status_screen, 1, 1, 0);
1.140 nicm 836:
837: if (gethostname(host, sizeof host) == 0)
838: screen_set_title(&wp->base, host);
1.1 nicm 839:
840: input_init(wp);
841:
842: return (wp);
843: }
844:
1.169 nicm 845: static void
1.1 nicm 846: window_pane_destroy(struct window_pane *wp)
847: {
1.55 nicm 848: window_pane_reset_mode(wp);
1.194 nicm 849: free(wp->searchstr);
1.55 nicm 850:
1.37 nicm 851: if (wp->fd != -1) {
1.70 nicm 852: bufferevent_free(wp->event);
1.1 nicm 853: close(wp->fd);
1.37 nicm 854: }
1.1 nicm 855:
856: input_free(wp);
857:
858: screen_free(&wp->base);
1.9 nicm 859: if (wp->saved_grid != NULL)
860: grid_destroy(wp->saved_grid);
1.1 nicm 861:
1.32 nicm 862: if (wp->pipe_fd != -1) {
1.70 nicm 863: bufferevent_free(wp->pipe_event);
1.32 nicm 864: close(wp->pipe_fd);
865: }
1.167 nicm 866:
867: if (event_initialized(&wp->resize_timer))
868: event_del(&wp->resize_timer);
1.32 nicm 869:
1.64 nicm 870: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
871:
1.147 nicm 872: free((void *)wp->cwd);
1.82 nicm 873: free(wp->shell);
1.110 nicm 874: cmd_free_argv(wp->argc, wp->argv);
1.177 nicm 875: free(wp->palette);
1.82 nicm 876: free(wp);
1.1 nicm 877: }
878:
879: int
1.110 nicm 880: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
1.147 nicm 881: const char *path, const char *shell, const char *cwd, struct environ *env,
1.110 nicm 882: struct termios *tio, char **cause)
1.1 nicm 883: {
1.47 nicm 884: struct winsize ws;
1.150 nicm 885: char *argv0, *cmd, **argvp;
1.147 nicm 886: const char *ptr, *first, *home;
1.47 nicm 887: struct termios tio2;
1.203 nicm 888: sigset_t set, oldset;
1.1 nicm 889:
1.37 nicm 890: if (wp->fd != -1) {
1.70 nicm 891: bufferevent_free(wp->event);
1.1 nicm 892: close(wp->fd);
1.37 nicm 893: }
1.110 nicm 894: if (argc > 0) {
895: cmd_free_argv(wp->argc, wp->argv);
896: wp->argc = argc;
897: wp->argv = cmd_copy_argv(argc, argv);
1.1 nicm 898: }
1.23 nicm 899: if (shell != NULL) {
1.82 nicm 900: free(wp->shell);
1.23 nicm 901: wp->shell = xstrdup(shell);
902: }
1.147 nicm 903: if (cwd != NULL) {
904: free((void *)wp->cwd);
905: wp->cwd = xstrdup(cwd);
1.1 nicm 906: }
1.206 nicm 907: wp->flags &= ~(PANE_STATUSREADY|PANE_STATUSDRAWN);
1.91 nicm 908:
1.110 nicm 909: cmd = cmd_stringify_argv(wp->argc, wp->argv);
1.209 nicm 910: log_debug("%s: shell=%s", __func__, wp->shell);
1.210 nicm 911: log_debug("%s: cmd=%s", __func__, cmd);
912: log_debug("%s: cwd=%s", __func__, cwd);
1.211 nicm 913: cmd_log_argv(wp->argc, wp->argv, __func__);
1.209 nicm 914: environ_log(env, "%s: environment ", __func__);
1.1 nicm 915:
916: memset(&ws, 0, sizeof ws);
917: ws.ws_col = screen_size_x(&wp->base);
918: ws.ws_row = screen_size_y(&wp->base);
919:
1.203 nicm 920: sigfillset(&set);
921: sigprocmask(SIG_BLOCK, &set, &oldset);
922: switch (wp->pid = fdforkpty(ptm_fd, &wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 923: case -1:
924: wp->fd = -1;
1.203 nicm 925:
1.1 nicm 926: xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110 nicm 927: free(cmd);
1.203 nicm 928:
929: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1 nicm 930: return (-1);
931: case 0:
1.204 nicm 932: proc_clear_signals(server_proc, 1);
1.203 nicm 933: sigprocmask(SIG_SETMASK, &oldset, NULL);
934:
1.207 nicm 935: cwd = NULL;
936: if (chdir(wp->cwd) == 0)
937: cwd = wp->cwd;
938: else if ((home = find_home()) != NULL && chdir(home) == 0)
939: cwd = home;
940: else
941: chdir("/");
1.25 nicm 942:
943: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
944: fatal("tcgetattr failed");
945: if (tio != NULL)
946: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
947: tio2.c_cc[VERASE] = '\177';
948: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
949: fatal("tcgetattr failed");
1.18 nicm 950:
1.203 nicm 951: log_close();
1.56 nicm 952: closefrom(STDERR_FILENO + 1);
953:
1.107 nicm 954: if (path != NULL)
1.150 nicm 955: environ_set(env, "PATH", "%s", path);
1.207 nicm 956: if (cwd != NULL)
957: environ_set(env, "PWD", "%s", cwd);
1.150 nicm 958: environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47 nicm 959: environ_push(env);
1.18 nicm 960:
1.80 nicm 961: setenv("SHELL", wp->shell, 1);
962: ptr = strrchr(wp->shell, '/');
963:
1.110 nicm 964: /*
965: * If given one argument, assume it should be passed to sh -c;
966: * with more than one argument, use execvp(). If there is no
967: * arguments, create a login shell.
968: */
969: if (wp->argc > 0) {
970: if (wp->argc != 1) {
971: /* Copy to ensure argv ends in NULL. */
972: argvp = cmd_copy_argv(wp->argc, wp->argv);
973: execvp(argvp[0], argvp);
974: fatal("execvp failed");
975: }
976: first = wp->argv[0];
977:
1.80 nicm 978: if (ptr != NULL && *(ptr + 1) != '\0')
979: xasprintf(&argv0, "%s", ptr + 1);
980: else
981: xasprintf(&argv0, "%s", wp->shell);
1.110 nicm 982: execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8 nicm 983: fatal("execl failed");
984: }
1.23 nicm 985: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 986: xasprintf(&argv0, "-%s", ptr + 1);
987: else
1.23 nicm 988: xasprintf(&argv0, "-%s", wp->shell);
1.110 nicm 989: execl(wp->shell, argv0, (char *)NULL);
1.1 nicm 990: fatal("execl failed");
991: }
1.209 nicm 992: log_debug("%s: master=%s", __func__, ttyname(wp->fd));
993: log_debug("%s: slave=%s", __func__, wp->tty);
1.1 nicm 994:
1.203 nicm 995: sigprocmask(SIG_SETMASK, &oldset, NULL);
1.62 nicm 996: setblocking(wp->fd, 0);
997:
1.110 nicm 998: wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
999: window_pane_error_callback, wp);
1.214 ! nicm 1000: if (wp->event == NULL)
! 1001: fatalx("out of memory");
1.131 nicm 1002:
1.183 nicm 1003: bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
1.37 nicm 1004: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 1005:
1.110 nicm 1006: free(cmd);
1.1 nicm 1007: return (0);
1008: }
1009:
1.166 nicm 1010: static void
1.149 nicm 1011: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37 nicm 1012: {
1.131 nicm 1013: struct window_pane *wp = data;
1014: struct evbuffer *evb = wp->event->input;
1.166 nicm 1015: size_t size = EVBUFFER_LENGTH(evb);
1.131 nicm 1016: char *new_data;
1.158 nicm 1017: size_t new_size;
1.131 nicm 1018:
1.166 nicm 1019: new_size = size - wp->pipe_off;
1.46 nicm 1020: if (wp->pipe_fd != -1 && new_size > 0) {
1.155 nicm 1021: new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46 nicm 1022: bufferevent_write(wp->pipe_event, new_data, new_size);
1023: }
1024:
1.183 nicm 1025: log_debug("%%%u has %zu bytes", wp->id, size);
1.46 nicm 1026: input_parse(wp);
1.37 nicm 1027:
1.173 nicm 1028: wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37 nicm 1029: }
1030:
1.166 nicm 1031: static void
1.149 nicm 1032: window_pane_error_callback(__unused struct bufferevent *bufev,
1033: __unused short what, void *data)
1.37 nicm 1034: {
1035: struct window_pane *wp = data;
1036:
1.200 nicm 1037: log_debug("%%%u error", wp->id);
1.201 nicm 1038: wp->flags |= PANE_EXITED;
1.200 nicm 1039:
1040: if (window_pane_destroy_ready(wp))
1041: server_destroy_pane(wp, 1);
1.37 nicm 1042: }
1043:
1044: void
1.1 nicm 1045: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1046: {
1047: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 1048: return;
1.1 nicm 1049: wp->sx = sx;
1050: wp->sy = sy;
1051:
1.89 nicm 1052: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 1053: if (wp->mode != NULL)
1054: wp->mode->resize(wp, sx, sy);
1.95 nicm 1055:
1056: wp->flags |= PANE_RESIZE;
1.44 nicm 1057: }
1058:
1059: /*
1060: * Enter alternative screen mode. A copy of the visible screen is saved and the
1061: * history is not updated
1062: */
1063: void
1.87 nicm 1064: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
1065: int cursor)
1.44 nicm 1066: {
1067: struct screen *s = &wp->base;
1068: u_int sx, sy;
1069:
1070: if (wp->saved_grid != NULL)
1071: return;
1.146 nicm 1072: if (!options_get_number(wp->window->options, "alternate-screen"))
1.44 nicm 1073: return;
1074: sx = screen_size_x(s);
1075: sy = screen_size_y(s);
1076:
1077: wp->saved_grid = grid_create(sx, sy, 0);
1078: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 1079: if (cursor) {
1080: wp->saved_cx = s->cx;
1081: wp->saved_cy = s->cy;
1082: }
1.44 nicm 1083: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1084:
1.170 nicm 1085: grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44 nicm 1086:
1087: wp->base.grid->flags &= ~GRID_HISTORY;
1088:
1089: wp->flags |= PANE_REDRAW;
1090: }
1091:
1092: /* Exit alternate screen mode and restore the copied grid. */
1093: void
1.87 nicm 1094: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1095: int cursor)
1.44 nicm 1096: {
1097: struct screen *s = &wp->base;
1098: u_int sx, sy;
1099:
1100: if (wp->saved_grid == NULL)
1101: return;
1.146 nicm 1102: if (!options_get_number(wp->window->options, "alternate-screen"))
1.44 nicm 1103: return;
1104: sx = screen_size_x(s);
1105: sy = screen_size_y(s);
1106:
1107: /*
1108: * If the current size is bigger, temporarily resize to the old size
1109: * before copying back.
1110: */
1111: if (sy > wp->saved_grid->sy)
1.89 nicm 1112: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1113:
1114: /* Restore the grid, cursor position and cell. */
1115: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1116: if (cursor)
1117: s->cx = wp->saved_cx;
1.44 nicm 1118: if (s->cx > screen_size_x(s) - 1)
1119: s->cx = screen_size_x(s) - 1;
1.87 nicm 1120: if (cursor)
1121: s->cy = wp->saved_cy;
1.44 nicm 1122: if (s->cy > screen_size_y(s) - 1)
1123: s->cy = screen_size_y(s) - 1;
1124: memcpy(gc, &wp->saved_cell, sizeof *gc);
1125:
1126: /*
1127: * Turn history back on (so resize can use it) and then resize back to
1128: * the current size.
1129: */
1130: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1131: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1132: screen_resize(s, sx, sy, 1);
1.44 nicm 1133:
1134: grid_destroy(wp->saved_grid);
1135: wp->saved_grid = NULL;
1136:
1.177 nicm 1137: wp->flags |= PANE_REDRAW;
1138: }
1139:
1140: void
1141: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
1142: {
1143: if (n > 0xff)
1144: return;
1145:
1146: if (wp->palette == NULL)
1147: wp->palette = xcalloc(0x100, sizeof *wp->palette);
1148:
1149: wp->palette[n] = colour;
1150: wp->flags |= PANE_REDRAW;
1151: }
1152:
1153: void
1154: window_pane_unset_palette(struct window_pane *wp, u_int n)
1155: {
1156: if (n > 0xff || wp->palette == NULL)
1157: return;
1158:
1159: wp->palette[n] = 0;
1160: wp->flags |= PANE_REDRAW;
1161: }
1162:
1163: void
1164: window_pane_reset_palette(struct window_pane *wp)
1165: {
1166: if (wp->palette == NULL)
1167: return;
1168:
1169: free(wp->palette);
1170: wp->palette = NULL;
1.44 nicm 1171: wp->flags |= PANE_REDRAW;
1.1 nicm 1172: }
1173:
1.180 nicm 1174: int
1175: window_pane_get_palette(const struct window_pane *wp, int c)
1176: {
1177: int new;
1178:
1179: if (wp == NULL || wp->palette == NULL)
1180: return (-1);
1181:
1182: new = -1;
1183: if (c < 8)
1184: new = wp->palette[c];
1185: else if (c >= 90 && c <= 97)
1186: new = wp->palette[8 + c - 90];
1187: else if (c & COLOUR_FLAG_256)
1188: new = wp->palette[c & ~COLOUR_FLAG_256];
1189: if (new == 0)
1190: return (-1);
1191: return (new);
1192: }
1193:
1.162 nicm 1194: static void
1195: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
1196: {
1197: struct window_pane *wp = arg;
1198: struct timeval tv = { .tv_sec = 10 };
1199: int n = 0;
1200:
1201: evtimer_del(&wp->modetimer);
1202: evtimer_add(&wp->modetimer, &tv);
1203:
1204: log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
1205:
1206: if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
1207: if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1208: window_pane_reset_mode(wp);
1209: }
1210: }
1211:
1.1 nicm 1212: int
1.196 nicm 1213: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode,
1214: struct cmd_find_state *fs, struct args *args)
1.1 nicm 1215: {
1216: struct screen *s;
1.162 nicm 1217: struct timeval tv = { .tv_sec = 10 };
1.1 nicm 1218:
1.15 nicm 1219: if (wp->mode != NULL)
1.1 nicm 1220: return (1);
1221: wp->mode = mode;
1222:
1.162 nicm 1223: wp->modelast = time(NULL);
1224: evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
1225: evtimer_add(&wp->modetimer, &tv);
1226:
1.196 nicm 1227: if ((s = wp->mode->init(wp, fs, args)) != NULL)
1.1 nicm 1228: wp->screen = s;
1.142 nicm 1229: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1230:
1231: server_status_window(wp->window);
1.193 nicm 1232: notify_pane("pane-mode-changed", wp);
1.1 nicm 1233: return (0);
1234: }
1235:
1236: void
1237: window_pane_reset_mode(struct window_pane *wp)
1238: {
1239: if (wp->mode == NULL)
1240: return;
1241:
1.162 nicm 1242: evtimer_del(&wp->modetimer);
1243:
1.1 nicm 1244: wp->mode->free(wp);
1245: wp->mode = NULL;
1.168 nicm 1246: wp->modeprefix = 1;
1.1 nicm 1247:
1248: wp->screen = &wp->base;
1.142 nicm 1249: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1250:
1251: server_status_window(wp->window);
1.193 nicm 1252: notify_pane("pane-mode-changed", wp);
1.1 nicm 1253: }
1254:
1255: void
1.118 nicm 1256: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148 nicm 1257: key_code key, struct mouse_event *m)
1.1 nicm 1258: {
1.27 nicm 1259: struct window_pane *wp2;
1.3 nicm 1260:
1.118 nicm 1261: if (KEYC_IS_MOUSE(key) && m == NULL)
1262: return;
1263:
1.1 nicm 1264: if (wp->mode != NULL) {
1.162 nicm 1265: wp->modelast = time(NULL);
1.1 nicm 1266: if (wp->mode->key != NULL)
1.199 nicm 1267: wp->mode->key(wp, c, s, (key & ~KEYC_XTERM), m);
1.27 nicm 1268: return;
1.30 nicm 1269: }
1.27 nicm 1270:
1.113 nicm 1271: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30 nicm 1272: return;
1.113 nicm 1273:
1.118 nicm 1274: input_key(wp, key, m);
1275:
1276: if (KEYC_IS_MOUSE(key))
1277: return;
1.146 nicm 1278: if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27 nicm 1279: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1.213 nicm 1280: if (wp2 != wp &&
1281: wp2->mode == NULL &&
1282: wp2->fd != -1 &&
1283: (~wp2->flags & PANE_INPUTOFF) &&
1284: window_pane_visible(wp2))
1.118 nicm 1285: input_key(wp2, key, NULL);
1.27 nicm 1286: }
1287: }
1.10 nicm 1288: }
1289:
1290: int
1.205 nicm 1291: window_pane_visible(struct window_pane *wp)
1.10 nicm 1292: {
1.213 nicm 1293: if (~wp->window->flags & WINDOW_ZOOMED)
1294: return (1);
1295: return (wp == wp->window->active);
1.1 nicm 1296: }
1297:
1.195 nicm 1298: u_int
1299: window_pane_search(struct window_pane *wp, const char *searchstr)
1300: {
1301: struct screen *s = &wp->base;
1302: char *newsearchstr, *line;
1303: u_int i;
1304:
1305: xasprintf(&newsearchstr, "*%s*", searchstr);
1306:
1307: for (i = 0; i < screen_size_y(s); i++) {
1308: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1309: if (fnmatch(newsearchstr, line, 0) == 0) {
1310: free(line);
1311: break;
1312: }
1313: free(line);
1314: }
1315:
1316: free(newsearchstr);
1317: if (i == screen_size_y(s))
1318: return (0);
1319: return (i + 1);
1.45 nicm 1320: }
1321:
1.109 nicm 1322: /* Get MRU pane from a list. */
1.166 nicm 1323: static struct window_pane *
1.126 nicm 1324: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1325: {
1326: struct window_pane *next, *best;
1327: u_int i;
1328:
1.126 nicm 1329: if (size == 0)
1.109 nicm 1330: return (NULL);
1331:
1.126 nicm 1332: best = list[0];
1333: for (i = 1; i < size; i++) {
1334: next = list[i];
1.109 nicm 1335: if (next->active_point > best->active_point)
1336: best = next;
1337: }
1338: return (best);
1339: }
1340:
1341: /*
1342: * Find the pane directly above another. We build a list of those adjacent to
1343: * top edge and then choose the best.
1344: */
1.45 nicm 1345: struct window_pane *
1346: window_pane_find_up(struct window_pane *wp)
1347: {
1.126 nicm 1348: struct window_pane *next, *best, **list;
1349: u_int edge, left, right, end, size;
1.187 nicm 1350: int status, found;
1.45 nicm 1351:
1.213 nicm 1352: if (wp == NULL)
1.45 nicm 1353: return (NULL);
1.187 nicm 1354: status = options_get_number(wp->window->options, "pane-border-status");
1.126 nicm 1355:
1356: list = NULL;
1357: size = 0;
1.109 nicm 1358:
1359: edge = wp->yoff;
1.187 nicm 1360: if (edge == (status == 1 ? 1 : 0))
1361: edge = wp->window->sy + 1 - (status == 2 ? 1 : 0);
1.45 nicm 1362:
1363: left = wp->xoff;
1.109 nicm 1364: right = wp->xoff + wp->sx;
1.45 nicm 1365:
1.109 nicm 1366: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1367: if (next == wp)
1.45 nicm 1368: continue;
1.109 nicm 1369: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1370: continue;
1.109 nicm 1371: end = next->xoff + next->sx - 1;
1372:
1373: found = 0;
1374: if (next->xoff < left && end > right)
1375: found = 1;
1376: else if (next->xoff >= left && next->xoff <= right)
1377: found = 1;
1378: else if (end >= left && end <= right)
1379: found = 1;
1.126 nicm 1380: if (!found)
1381: continue;
1382: list = xreallocarray(list, size + 1, sizeof *list);
1383: list[size++] = next;
1.109 nicm 1384: }
1385:
1.126 nicm 1386: best = window_pane_choose_best(list, size);
1387: free(list);
1.109 nicm 1388: return (best);
1.45 nicm 1389: }
1390:
1391: /* Find the pane directly below another. */
1392: struct window_pane *
1393: window_pane_find_down(struct window_pane *wp)
1394: {
1.126 nicm 1395: struct window_pane *next, *best, **list;
1396: u_int edge, left, right, end, size;
1.187 nicm 1397: int status, found;
1.45 nicm 1398:
1.213 nicm 1399: if (wp == NULL)
1.45 nicm 1400: return (NULL);
1.187 nicm 1401: status = options_get_number(wp->window->options, "pane-border-status");
1.126 nicm 1402:
1403: list = NULL;
1404: size = 0;
1.109 nicm 1405:
1406: edge = wp->yoff + wp->sy + 1;
1.187 nicm 1407: if (edge >= wp->window->sy - (status == 2 ? 1 : 0))
1408: edge = (status == 1 ? 1 : 0);
1.45 nicm 1409:
1410: left = wp->xoff;
1.109 nicm 1411: right = wp->xoff + wp->sx;
1.45 nicm 1412:
1.109 nicm 1413: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1414: if (next == wp)
1.45 nicm 1415: continue;
1.109 nicm 1416: if (next->yoff != edge)
1.45 nicm 1417: continue;
1.109 nicm 1418: end = next->xoff + next->sx - 1;
1419:
1420: found = 0;
1421: if (next->xoff < left && end > right)
1422: found = 1;
1423: else if (next->xoff >= left && next->xoff <= right)
1424: found = 1;
1425: else if (end >= left && end <= right)
1426: found = 1;
1.126 nicm 1427: if (!found)
1428: continue;
1429: list = xreallocarray(list, size + 1, sizeof *list);
1430: list[size++] = next;
1.45 nicm 1431: }
1.109 nicm 1432:
1.126 nicm 1433: best = window_pane_choose_best(list, size);
1434: free(list);
1.109 nicm 1435: return (best);
1.45 nicm 1436: }
1437:
1.109 nicm 1438: /* Find the pane directly to the left of another. */
1.45 nicm 1439: struct window_pane *
1440: window_pane_find_left(struct window_pane *wp)
1441: {
1.126 nicm 1442: struct window_pane *next, *best, **list;
1443: u_int edge, top, bottom, end, size;
1.109 nicm 1444: int found;
1.45 nicm 1445:
1.213 nicm 1446: if (wp == NULL)
1.45 nicm 1447: return (NULL);
1.126 nicm 1448:
1449: list = NULL;
1450: size = 0;
1.109 nicm 1451:
1452: edge = wp->xoff;
1453: if (edge == 0)
1454: edge = wp->window->sx + 1;
1.45 nicm 1455:
1456: top = wp->yoff;
1.109 nicm 1457: bottom = wp->yoff + wp->sy;
1.45 nicm 1458:
1.109 nicm 1459: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1460: if (next == wp)
1.45 nicm 1461: continue;
1.109 nicm 1462: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1463: continue;
1.109 nicm 1464: end = next->yoff + next->sy - 1;
1465:
1466: found = 0;
1467: if (next->yoff < top && end > bottom)
1468: found = 1;
1469: else if (next->yoff >= top && next->yoff <= bottom)
1470: found = 1;
1471: else if (end >= top && end <= bottom)
1472: found = 1;
1.126 nicm 1473: if (!found)
1474: continue;
1475: list = xreallocarray(list, size + 1, sizeof *list);
1476: list[size++] = next;
1.45 nicm 1477: }
1.109 nicm 1478:
1.126 nicm 1479: best = window_pane_choose_best(list, size);
1480: free(list);
1.109 nicm 1481: return (best);
1.45 nicm 1482: }
1483:
1.109 nicm 1484: /* Find the pane directly to the right of another. */
1.45 nicm 1485: struct window_pane *
1486: window_pane_find_right(struct window_pane *wp)
1487: {
1.126 nicm 1488: struct window_pane *next, *best, **list;
1489: u_int edge, top, bottom, end, size;
1.109 nicm 1490: int found;
1.45 nicm 1491:
1.213 nicm 1492: if (wp == NULL)
1.45 nicm 1493: return (NULL);
1.126 nicm 1494:
1495: list = NULL;
1496: size = 0;
1.109 nicm 1497:
1498: edge = wp->xoff + wp->sx + 1;
1499: if (edge >= wp->window->sx)
1500: edge = 0;
1.45 nicm 1501:
1502: top = wp->yoff;
1.109 nicm 1503: bottom = wp->yoff + wp->sy;
1.45 nicm 1504:
1.109 nicm 1505: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1506: if (next == wp)
1.45 nicm 1507: continue;
1.109 nicm 1508: if (next->xoff != edge)
1.45 nicm 1509: continue;
1.109 nicm 1510: end = next->yoff + next->sy - 1;
1511:
1512: found = 0;
1513: if (next->yoff < top && end > bottom)
1514: found = 1;
1515: else if (next->yoff >= top && next->yoff <= bottom)
1516: found = 1;
1517: else if (end >= top && end <= bottom)
1518: found = 1;
1.126 nicm 1519: if (!found)
1520: continue;
1521: list = xreallocarray(list, size + 1, sizeof *list);
1522: list[size++] = next;
1.109 nicm 1523: }
1524:
1.126 nicm 1525: best = window_pane_choose_best(list, size);
1526: free(list);
1.109 nicm 1527: return (best);
1.81 nicm 1528: }
1529:
1530: /* Clear alert flags for a winlink */
1531: void
1532: winlink_clear_flags(struct winlink *wl)
1533: {
1.174 nicm 1534: struct winlink *loop;
1.81 nicm 1535:
1.174 nicm 1536: wl->window->flags &= ~WINDOW_ALERTFLAGS;
1537: TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1538: if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1539: loop->flags &= ~WINLINK_ALERTFLAGS;
1540: server_status_session(loop->session);
1.81 nicm 1541: }
1542: }
1.134 nicm 1543: }
1544:
1.184 nicm 1545: /* Shuffle window indexes up. */
1.134 nicm 1546: int
1547: winlink_shuffle_up(struct session *s, struct winlink *wl)
1548: {
1549: int idx, last;
1550:
1551: idx = wl->idx + 1;
1552:
1553: /* Find the next free index. */
1554: for (last = idx; last < INT_MAX; last++) {
1555: if (winlink_find_by_index(&s->windows, last) == NULL)
1556: break;
1557: }
1558: if (last == INT_MAX)
1559: return (-1);
1560:
1561: /* Move everything from last - 1 to idx up a bit. */
1562: for (; last > idx; last--) {
1563: wl = winlink_find_by_index(&s->windows, last - 1);
1564: server_link_window(s, wl, s, last, 0, 0, NULL);
1565: server_unlink_window(s, wl);
1566: }
1567:
1568: return (idx);
1.1 nicm 1569: }