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