Annotation of src/usr.bin/tmux/window.c, Revision 1.271
1.271 ! nicm 1: /* $OpenBSD: window.c,v 1.270 2021/03/11 06:31:05 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.245 nicm 321: w->xpixel = xpixel;
322: w->ypixel = ypixel;
1.133 nicm 323:
1.146 nicm 324: w->options = options_create(global_w_options);
1.1 nicm 325:
326: w->references = 0;
1.174 nicm 327: TAILQ_INIT(&w->winlinks);
1.1 nicm 328:
1.122 nicm 329: w->id = next_window_id++;
1.129 nicm 330: RB_INSERT(windows, &windows, w);
1.122 nicm 331:
1.143 nicm 332: window_update_activity(w);
333:
1.271 ! nicm 334: log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
! 335: w->xpixel, w->ypixel);
1.1 nicm 336: return (w);
337: }
338:
1.240 nicm 339: static void
1.1 nicm 340: window_destroy(struct window *w)
341: {
1.192 nicm 342: log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174 nicm 343:
1.122 nicm 344: RB_REMOVE(windows, &windows, w);
1.1 nicm 345:
1.14 nicm 346: if (w->layout_root != NULL)
1.135 nicm 347: layout_free_cell(w->layout_root);
348: if (w->saved_layout_root != NULL)
349: layout_free_cell(w->saved_layout_root);
1.127 nicm 350: free(w->old_layout);
1.139 nicm 351:
1.227 nicm 352: window_destroy_panes(w);
353:
1.141 nicm 354: if (event_initialized(&w->name_event))
355: evtimer_del(&w->name_event);
1.38 nicm 356:
1.143 nicm 357: if (event_initialized(&w->alerts_timer))
358: evtimer_del(&w->alerts_timer);
1.213 nicm 359: if (event_initialized(&w->offset_timer))
360: event_del(&w->offset_timer);
1.143 nicm 361:
1.146 nicm 362: options_free(w->options);
1.1 nicm 363:
1.82 nicm 364: free(w->name);
365: free(w);
1.84 nicm 366: }
367:
1.200 nicm 368: int
369: window_pane_destroy_ready(struct window_pane *wp)
370: {
1.201 nicm 371: int n;
372:
373: if (wp->pipe_fd != -1) {
374: if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
375: return (0);
376: if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
377: return (0);
378: }
1.200 nicm 379:
380: if (~wp->flags & PANE_EXITED)
381: return (0);
382: return (1);
383: }
384:
1.84 nicm 385: void
1.192 nicm 386: window_add_ref(struct window *w, const char *from)
387: {
388: w->references++;
389: log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
390: }
391:
392: void
393: window_remove_ref(struct window *w, const char *from)
1.84 nicm 394: {
395: w->references--;
1.192 nicm 396: log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
397:
1.84 nicm 398: if (w->references == 0)
399: window_destroy(w);
1.72 nicm 400: }
401:
402: void
403: window_set_name(struct window *w, const char *new_name)
404: {
1.82 nicm 405: free(w->name);
1.198 nicm 406: utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.172 nicm 407: notify_window("window-renamed", w);
1.1 nicm 408: }
409:
1.15 nicm 410: void
1.245 nicm 411: window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
1.1 nicm 412: {
1.245 nicm 413: if (xpixel == 0)
414: xpixel = DEFAULT_XPIXEL;
415: if (ypixel == 0)
416: ypixel = DEFAULT_YPIXEL;
417:
418: log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
1.249 nicm 419: xpixel == -1 ? w->xpixel : (u_int)xpixel,
420: ypixel == -1 ? w->ypixel : (u_int)ypixel);
1.1 nicm 421: w->sx = sx;
422: w->sy = sy;
1.245 nicm 423: if (xpixel != -1)
424: w->xpixel = xpixel;
425: if (ypixel != -1)
426: w->ypixel = ypixel;
427: }
428:
429: void
1.265 nicm 430: window_pane_send_resize(struct window_pane *wp, int force)
1.245 nicm 431: {
432: struct window *w = wp->window;
433: struct winsize ws;
1.265 nicm 434: u_int sy;
1.245 nicm 435:
436: if (wp->fd == -1)
437: return;
1.265 nicm 438:
439: if (!force)
440: sy = wp->sy;
441: else if (wp->sy <= 1)
442: sy = wp->sy + 1;
443: else
444: sy = wp->sy - 1;
1.262 nicm 445: log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, sy);
1.245 nicm 446:
447: memset(&ws, 0, sizeof ws);
448: ws.ws_col = wp->sx;
1.262 nicm 449: ws.ws_row = sy;
1.245 nicm 450: ws.ws_xpixel = w->xpixel * ws.ws_col;
451: ws.ws_ypixel = w->ypixel * ws.ws_row;
452: if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
453: fatal("ioctl failed");
1.1 nicm 454: }
455:
1.114 nicm 456: int
1.118 nicm 457: window_has_pane(struct window *w, struct window_pane *wp)
458: {
459: struct window_pane *wp1;
460:
461: TAILQ_FOREACH(wp1, &w->panes, entry) {
462: if (wp1 == wp)
463: return (1);
464: }
465: return (0);
466: }
467:
468: int
1.226 nicm 469: window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
1.1 nicm 470: {
1.226 nicm 471: log_debug("%s: pane %%%u", __func__, wp->id);
472:
1.59 nicm 473: if (wp == w->active)
1.114 nicm 474: return (0);
1.58 nicm 475: w->last = w->active;
1.226 nicm 476:
1.1 nicm 477: w->active = wp;
1.109 nicm 478: w->active->active_point = next_active_point++;
1.136 nicm 479: w->active->flags |= PANE_CHANGED;
1.226 nicm 480:
1.213 nicm 481: tty_update_window_offset(w);
1.226 nicm 482:
483: if (notify)
484: notify_window("window-pane-changed", w);
1.114 nicm 485: return (1);
1.145 nicm 486: }
487:
488: void
489: window_redraw_active_switch(struct window *w, struct window_pane *wp)
490: {
1.258 nicm 491: struct grid_cell *gc1, *gc2;
492: int c1, c2;
1.145 nicm 493:
494: if (wp == w->active)
495: return;
496:
1.234 nicm 497: for (;;) {
498: /*
499: * If the active and inactive styles or palettes are different,
500: * need to redraw the panes.
501: */
1.258 nicm 502: gc1 = &wp->cached_gc;
503: gc2 = &wp->cached_active_gc;
504: if (!grid_cells_look_equal(gc1, gc2))
1.234 nicm 505: wp->flags |= PANE_REDRAW;
506: else {
1.258 nicm 507: c1 = window_pane_get_palette(wp, gc1->fg);
508: c2 = window_pane_get_palette(wp, gc2->fg);
1.234 nicm 509: if (c1 != c2)
510: wp->flags |= PANE_REDRAW;
511: else {
1.258 nicm 512: c1 = window_pane_get_palette(wp, gc1->bg);
513: c2 = window_pane_get_palette(wp, gc2->bg);
1.234 nicm 514: if (c1 != c2)
515: wp->flags |= PANE_REDRAW;
516: }
517: }
518: if (wp == w->active)
519: break;
520: wp = w->active;
521: }
1.29 nicm 522: }
523:
1.66 nicm 524: struct window_pane *
525: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 526: {
527: struct window_pane *wp;
528:
529: TAILQ_FOREACH(wp, &w->panes, entry) {
1.219 nicm 530: if (!window_pane_visible(wp))
531: continue;
1.66 nicm 532: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 533: continue;
1.66 nicm 534: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 535: continue;
1.66 nicm 536: return (wp);
537: }
538: return (NULL);
539: }
540:
541: struct window_pane *
542: window_find_string(struct window *w, const char *s)
543: {
1.248 nicm 544: u_int x, y, top = 0, bottom = w->sy - 1;
545: int status;
1.66 nicm 546:
547: x = w->sx / 2;
548: y = w->sy / 2;
549:
1.248 nicm 550: status = options_get_number(w->options, "pane-border-status");
551: if (status == PANE_STATUS_TOP)
552: top++;
553: else if (status == PANE_STATUS_BOTTOM)
554: bottom--;
555:
1.66 nicm 556: if (strcasecmp(s, "top") == 0)
1.248 nicm 557: y = top;
1.66 nicm 558: else if (strcasecmp(s, "bottom") == 0)
1.248 nicm 559: y = bottom;
1.66 nicm 560: else if (strcasecmp(s, "left") == 0)
561: x = 0;
562: else if (strcasecmp(s, "right") == 0)
563: x = w->sx - 1;
564: else if (strcasecmp(s, "top-left") == 0) {
565: x = 0;
1.248 nicm 566: y = top;
1.66 nicm 567: } else if (strcasecmp(s, "top-right") == 0) {
568: x = w->sx - 1;
1.248 nicm 569: y = top;
1.66 nicm 570: } else if (strcasecmp(s, "bottom-left") == 0) {
571: x = 0;
1.248 nicm 572: y = bottom;
1.66 nicm 573: } else if (strcasecmp(s, "bottom-right") == 0) {
574: x = w->sx - 1;
1.248 nicm 575: y = bottom;
1.66 nicm 576: } else
577: return (NULL);
578:
579: return (window_get_active_at(w, x, y));
1.1 nicm 580: }
581:
1.93 nicm 582: int
583: window_zoom(struct window_pane *wp)
584: {
585: struct window *w = wp->window;
586: struct window_pane *wp1;
587:
588: if (w->flags & WINDOW_ZOOMED)
589: return (-1);
590:
1.94 nicm 591: if (window_count_panes(w) == 1)
592: return (-1);
593:
1.93 nicm 594: if (w->active != wp)
1.226 nicm 595: window_set_active_pane(w, wp, 1);
1.93 nicm 596:
597: TAILQ_FOREACH(wp1, &w->panes, entry) {
598: wp1->saved_layout_cell = wp1->layout_cell;
599: wp1->layout_cell = NULL;
600: }
601:
602: w->saved_layout_root = w->layout_root;
603: layout_init(w, wp);
604: w->flags |= WINDOW_ZOOMED;
1.172 nicm 605: notify_window("window-layout-changed", w);
1.93 nicm 606:
607: return (0);
608: }
609:
610: int
611: window_unzoom(struct window *w)
612: {
1.97 nicm 613: struct window_pane *wp;
1.93 nicm 614:
615: if (!(w->flags & WINDOW_ZOOMED))
616: return (-1);
617:
618: w->flags &= ~WINDOW_ZOOMED;
619: layout_free(w);
620: w->layout_root = w->saved_layout_root;
1.120 nicm 621: w->saved_layout_root = NULL;
1.93 nicm 622:
1.97 nicm 623: TAILQ_FOREACH(wp, &w->panes, entry) {
624: wp->layout_cell = wp->saved_layout_cell;
625: wp->saved_layout_cell = NULL;
1.93 nicm 626: }
1.270 nicm 627: layout_fix_panes(w, NULL);
1.172 nicm 628: notify_window("window-layout-changed", w);
1.93 nicm 629:
1.241 nicm 630: return (0);
631: }
632:
633: int
1.270 nicm 634: window_push_zoom(struct window *w, int always, int flag)
1.241 nicm 635: {
636: log_debug("%s: @%u %d", __func__, w->id,
637: flag && (w->flags & WINDOW_ZOOMED));
1.270 nicm 638: if (flag && (always || (w->flags & WINDOW_ZOOMED)))
1.241 nicm 639: w->flags |= WINDOW_WASZOOMED;
640: else
641: w->flags &= ~WINDOW_WASZOOMED;
642: return (window_unzoom(w) == 0);
643: }
644:
645: int
646: window_pop_zoom(struct window *w)
647: {
648: log_debug("%s: @%u %d", __func__, w->id,
649: !!(w->flags & WINDOW_WASZOOMED));
650: if (w->flags & WINDOW_WASZOOMED)
651: return (window_zoom(w->active) == 0);
1.93 nicm 652: return (0);
653: }
654:
1.1 nicm 655: struct window_pane *
1.226 nicm 656: window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
657: int flags)
1.1 nicm 658: {
659: struct window_pane *wp;
660:
1.185 nicm 661: if (other == NULL)
662: other = w->active;
663:
1.14 nicm 664: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186 nicm 665: if (TAILQ_EMPTY(&w->panes)) {
666: log_debug("%s: @%u at start", __func__, w->id);
1.1 nicm 667: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.226 nicm 668: } else if (flags & SPAWN_BEFORE) {
1.186 nicm 669: log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.226 nicm 670: if (flags & SPAWN_FULLSIZE)
1.208 nicm 671: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
672: else
673: TAILQ_INSERT_BEFORE(other, wp, entry);
1.186 nicm 674: } else {
675: log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.226 nicm 676: if (flags & SPAWN_FULLSIZE)
1.208 nicm 677: TAILQ_INSERT_TAIL(&w->panes, wp, entry);
678: else
679: TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186 nicm 680: }
1.1 nicm 681: return (wp);
682: }
683:
684: void
1.105 nicm 685: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 686: {
1.205 nicm 687: log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
688:
1.152 nicm 689: if (wp == marked_pane.wp)
1.132 nicm 690: server_clear_marked();
691:
1.57 nicm 692: if (wp == w->active) {
1.58 nicm 693: w->active = w->last;
694: w->last = NULL;
695: if (w->active == NULL) {
696: w->active = TAILQ_PREV(wp, window_panes, entry);
697: if (w->active == NULL)
698: w->active = TAILQ_NEXT(wp, entry);
699: }
1.193 nicm 700: if (w->active != NULL) {
1.151 nicm 701: w->active->flags |= PANE_CHANGED;
1.193 nicm 702: notify_window("window-pane-changed", w);
703: }
1.58 nicm 704: } else if (wp == w->last)
705: w->last = NULL;
1.105 nicm 706: }
707:
708: void
709: window_remove_pane(struct window *w, struct window_pane *wp)
710: {
711: window_lost_pane(w, wp);
1.1 nicm 712:
713: TAILQ_REMOVE(&w->panes, wp, entry);
714: window_pane_destroy(wp);
715: }
716:
717: struct window_pane *
718: window_pane_at_index(struct window *w, u_int idx)
719: {
720: struct window_pane *wp;
721: u_int n;
722:
1.146 nicm 723: n = options_get_number(w->options, "pane-base-index");
1.1 nicm 724: TAILQ_FOREACH(wp, &w->panes, entry) {
725: if (n == idx)
726: return (wp);
727: n++;
728: }
729: return (NULL);
1.53 nicm 730: }
731:
732: struct window_pane *
733: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
734: {
735: for (; n > 0; n--) {
736: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
737: wp = TAILQ_FIRST(&w->panes);
738: }
739:
740: return (wp);
741: }
742:
743: struct window_pane *
744: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
745: u_int n)
746: {
747: for (; n > 0; n--) {
748: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
749: wp = TAILQ_LAST(&w->panes, window_panes);
750: }
751:
752: return (wp);
1.13 nicm 753: }
754:
1.69 nicm 755: int
756: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 757: {
758: struct window_pane *wq;
1.69 nicm 759: struct window *w = wp->window;
1.13 nicm 760:
1.146 nicm 761: *i = options_get_number(w->options, "pane-base-index");
1.13 nicm 762: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 763: if (wp == wq) {
764: return (0);
765: }
766: (*i)++;
1.13 nicm 767: }
1.69 nicm 768:
769: return (-1);
1.1 nicm 770: }
771:
772: u_int
773: window_count_panes(struct window *w)
774: {
775: struct window_pane *wp;
776: u_int n;
777:
778: n = 0;
779: TAILQ_FOREACH(wp, &w->panes, entry)
780: n++;
781: return (n);
782: }
783:
784: void
785: window_destroy_panes(struct window *w)
786: {
787: struct window_pane *wp;
788:
789: while (!TAILQ_EMPTY(&w->panes)) {
790: wp = TAILQ_FIRST(&w->panes);
791: TAILQ_REMOVE(&w->panes, wp, entry);
792: window_pane_destroy(wp);
793: }
1.61 nicm 794: }
795:
1.188 nicm 796: const char *
1.268 nicm 797: window_printable_flags(struct winlink *wl, int escape)
1.61 nicm 798: {
1.189 nicm 799: struct session *s = wl->session;
800: static char flags[32];
801: int pos;
1.61 nicm 802:
803: pos = 0;
1.268 nicm 804: if (wl->flags & WINLINK_ACTIVITY) {
1.61 nicm 805: flags[pos++] = '#';
1.268 nicm 806: if (escape)
807: flags[pos++] = '#';
808: }
1.61 nicm 809: if (wl->flags & WINLINK_BELL)
810: flags[pos++] = '!';
811: if (wl->flags & WINLINK_SILENCE)
812: flags[pos++] = '~';
813: if (wl == s->curw)
814: flags[pos++] = '*';
815: if (wl == TAILQ_FIRST(&s->lastw))
816: flags[pos++] = '-';
1.152 nicm 817: if (server_check_marked() && wl == marked_pane.wl)
1.132 nicm 818: flags[pos++] = 'M';
1.93 nicm 819: if (wl->window->flags & WINDOW_ZOOMED)
820: flags[pos++] = 'Z';
1.61 nicm 821: flags[pos] = '\0';
1.188 nicm 822: return (flags);
1.1 nicm 823: }
824:
1.123 nicm 825: struct window_pane *
826: window_pane_find_by_id_str(const char *s)
827: {
828: const char *errstr;
829: u_int id;
830:
831: if (*s != '%')
832: return (NULL);
833:
834: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
835: if (errstr != NULL)
836: return (NULL);
837: return (window_pane_find_by_id(id));
838: }
839:
1.64 nicm 840: struct window_pane *
841: window_pane_find_by_id(u_int id)
842: {
843: struct window_pane wp;
844:
845: wp.id = id;
846: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
847: }
848:
1.169 nicm 849: static struct window_pane *
1.1 nicm 850: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
851: {
852: struct window_pane *wp;
1.140 nicm 853: char host[HOST_NAME_MAX + 1];
1.1 nicm 854:
855: wp = xcalloc(1, sizeof *wp);
856: wp->window = w;
1.234 nicm 857: wp->options = options_create(w->options);
858: wp->flags = PANE_STYLECHANGED;
1.1 nicm 859:
1.71 nicm 860: wp->id = next_window_pane_id++;
1.64 nicm 861: RB_INSERT(window_pane_tree, &all_window_panes, wp);
862:
1.110 nicm 863: wp->argc = 0;
864: wp->argv = NULL;
1.23 nicm 865: wp->shell = NULL;
1.147 nicm 866: wp->cwd = NULL;
1.1 nicm 867:
868: wp->fd = -1;
1.37 nicm 869: wp->event = NULL;
1.1 nicm 870:
1.259 nicm 871: wp->fg = 8;
872: wp->bg = 8;
873:
1.218 nicm 874: TAILQ_INIT(&wp->modes);
1.14 nicm 875:
876: wp->layout_cell = NULL;
1.1 nicm 877:
878: wp->xoff = 0;
1.42 nicm 879: wp->yoff = 0;
1.1 nicm 880:
1.265 nicm 881: wp->sx = sx;
882: wp->sy = sy;
1.1 nicm 883:
1.32 nicm 884: wp->pipe_fd = -1;
1.36 nicm 885: wp->pipe_event = NULL;
1.32 nicm 886:
1.1 nicm 887: screen_init(&wp->base, sx, sy, hlimit);
888: wp->screen = &wp->base;
1.159 nicm 889:
890: screen_init(&wp->status_screen, 1, 1, 0);
1.140 nicm 891:
892: if (gethostname(host, sizeof host) == 0)
893: screen_set_title(&wp->base, host);
1.1 nicm 894:
895: return (wp);
896: }
897:
1.169 nicm 898: static void
1.1 nicm 899: window_pane_destroy(struct window_pane *wp)
900: {
1.218 nicm 901: window_pane_reset_mode_all(wp);
1.194 nicm 902: free(wp->searchstr);
1.55 nicm 903:
1.37 nicm 904: if (wp->fd != -1) {
1.70 nicm 905: bufferevent_free(wp->event);
1.1 nicm 906: close(wp->fd);
1.37 nicm 907: }
1.251 nicm 908: if (wp->ictx != NULL)
909: input_free(wp->ictx);
1.225 nicm 910:
911: screen_free(&wp->status_screen);
1.1 nicm 912:
913: screen_free(&wp->base);
914:
1.32 nicm 915: if (wp->pipe_fd != -1) {
1.70 nicm 916: bufferevent_free(wp->pipe_event);
1.32 nicm 917: close(wp->pipe_fd);
918: }
1.167 nicm 919:
920: if (event_initialized(&wp->resize_timer))
921: event_del(&wp->resize_timer);
1.265 nicm 922: if (event_initialized(&wp->force_timer))
923: event_del(&wp->force_timer);
1.32 nicm 924:
1.64 nicm 925: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
926:
1.234 nicm 927: options_free(wp->options);
1.147 nicm 928: free((void *)wp->cwd);
1.82 nicm 929: free(wp->shell);
1.110 nicm 930: cmd_free_argv(wp->argc, wp->argv);
1.177 nicm 931: free(wp->palette);
1.82 nicm 932: free(wp);
1.1 nicm 933: }
934:
1.166 nicm 935: static void
1.149 nicm 936: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37 nicm 937: {
1.263 nicm 938: struct window_pane *wp = data;
939: struct evbuffer *evb = wp->event->input;
940: struct window_pane_offset *wpo = &wp->pipe_offset;
941: size_t size = EVBUFFER_LENGTH(evb);
942: char *new_data;
943: size_t new_size;
944: struct client *c;
945:
946: if (wp->pipe_fd != -1) {
947: new_data = window_pane_get_new_data(wp, wpo, &new_size);
948: if (new_size > 0) {
949: bufferevent_write(wp->pipe_event, new_data, new_size);
1.264 nicm 950: window_pane_update_used_data(wp, wpo, new_size);
1.263 nicm 951: }
1.46 nicm 952: }
953:
1.183 nicm 954: log_debug("%%%u has %zu bytes", wp->id, size);
1.263 nicm 955: TAILQ_FOREACH(c, &clients, entry) {
1.264 nicm 956: if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1.263 nicm 957: control_write_output(c, wp);
958: }
1.250 nicm 959: input_parse_pane(wp);
1.264 nicm 960: bufferevent_disable(wp->event, EV_READ);
1.37 nicm 961: }
962:
1.166 nicm 963: static void
1.149 nicm 964: window_pane_error_callback(__unused struct bufferevent *bufev,
965: __unused short what, void *data)
1.37 nicm 966: {
967: struct window_pane *wp = data;
968:
1.200 nicm 969: log_debug("%%%u error", wp->id);
1.201 nicm 970: wp->flags |= PANE_EXITED;
1.200 nicm 971:
972: if (window_pane_destroy_ready(wp))
973: server_destroy_pane(wp, 1);
1.37 nicm 974: }
975:
976: void
1.226 nicm 977: window_pane_set_event(struct window_pane *wp)
978: {
979: setblocking(wp->fd, 0);
980:
981: wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
982: NULL, window_pane_error_callback, wp);
1.251 nicm 983: wp->ictx = input_init(wp, wp->event);
1.226 nicm 984:
985: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
986: }
987:
988: void
1.1 nicm 989: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
990: {
1.218 nicm 991: struct window_mode_entry *wme;
992:
1.1 nicm 993: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 994: return;
1.1 nicm 995: wp->sx = sx;
996: wp->sy = sy;
997:
1.238 nicm 998: log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1.252 nicm 999: screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1.218 nicm 1000:
1001: wme = TAILQ_FIRST(&wp->modes);
1002: if (wme != NULL && wme->mode->resize != NULL)
1003: wme->mode->resize(wme, sx, sy);
1.265 nicm 1004:
1005: /*
1006: * If the pane has already been resized, set the force flag and make
1007: * the application resize twice to force it to redraw.
1008: */
1009: if (wp->flags & PANE_RESIZE)
1010: wp->flags |= PANE_RESIZEFORCE;
1011: wp->flags |= PANE_RESIZE;
1.177 nicm 1012: }
1013:
1014: void
1015: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
1016: {
1017: if (n > 0xff)
1018: return;
1019:
1020: if (wp->palette == NULL)
1021: wp->palette = xcalloc(0x100, sizeof *wp->palette);
1022:
1023: wp->palette[n] = colour;
1024: wp->flags |= PANE_REDRAW;
1025: }
1026:
1027: void
1028: window_pane_unset_palette(struct window_pane *wp, u_int n)
1029: {
1030: if (n > 0xff || wp->palette == NULL)
1031: return;
1032:
1033: wp->palette[n] = 0;
1034: wp->flags |= PANE_REDRAW;
1035: }
1036:
1037: void
1038: window_pane_reset_palette(struct window_pane *wp)
1039: {
1040: if (wp->palette == NULL)
1041: return;
1042:
1043: free(wp->palette);
1044: wp->palette = NULL;
1.44 nicm 1045: wp->flags |= PANE_REDRAW;
1.1 nicm 1046: }
1047:
1.180 nicm 1048: int
1.220 nicm 1049: window_pane_get_palette(struct window_pane *wp, int c)
1.180 nicm 1050: {
1051: int new;
1052:
1053: if (wp == NULL || wp->palette == NULL)
1054: return (-1);
1055:
1056: new = -1;
1057: if (c < 8)
1058: new = wp->palette[c];
1059: else if (c >= 90 && c <= 97)
1060: new = wp->palette[8 + c - 90];
1061: else if (c & COLOUR_FLAG_256)
1062: new = wp->palette[c & ~COLOUR_FLAG_256];
1063: if (new == 0)
1064: return (-1);
1065: return (new);
1066: }
1067:
1.1 nicm 1068: int
1.255 nicm 1069: window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1070: const struct window_mode *mode, struct cmd_find_state *fs,
1071: struct args *args)
1.1 nicm 1072: {
1.218 nicm 1073: struct window_mode_entry *wme;
1.1 nicm 1074:
1.218 nicm 1075: if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1.1 nicm 1076: return (1);
1.217 nicm 1077:
1.218 nicm 1078: TAILQ_FOREACH(wme, &wp->modes, entry) {
1079: if (wme->mode == mode)
1080: break;
1081: }
1.223 nicm 1082: if (wme != NULL) {
1.218 nicm 1083: TAILQ_REMOVE(&wp->modes, wme, entry);
1.223 nicm 1084: TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1085: } else {
1.218 nicm 1086: wme = xcalloc(1, sizeof *wme);
1087: wme->wp = wp;
1.255 nicm 1088: wme->swp = swp;
1.218 nicm 1089: wme->mode = mode;
1090: wme->prefix = 1;
1.223 nicm 1091: TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1.218 nicm 1092: wme->screen = wme->mode->init(wme, fs, args);
1093: }
1.162 nicm 1094:
1.218 nicm 1095: wp->screen = wme->screen;
1.142 nicm 1096: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1097:
1.259 nicm 1098: server_redraw_window_borders(wp->window);
1.157 nicm 1099: server_status_window(wp->window);
1.193 nicm 1100: notify_pane("pane-mode-changed", wp);
1.218 nicm 1101:
1.1 nicm 1102: return (0);
1103: }
1104:
1105: void
1106: window_pane_reset_mode(struct window_pane *wp)
1107: {
1.218 nicm 1108: struct window_mode_entry *wme, *next;
1109:
1110: if (TAILQ_EMPTY(&wp->modes))
1.1 nicm 1111: return;
1112:
1.218 nicm 1113: wme = TAILQ_FIRST(&wp->modes);
1114: TAILQ_REMOVE(&wp->modes, wme, entry);
1115: wme->mode->free(wme);
1116: free(wme);
1117:
1118: next = TAILQ_FIRST(&wp->modes);
1119: if (next == NULL) {
1120: log_debug("%s: no next mode", __func__);
1121: wp->screen = &wp->base;
1122: } else {
1123: log_debug("%s: next mode is %s", __func__, next->mode->name);
1124: wp->screen = next->screen;
1.230 nicm 1125: if (next->mode->resize != NULL)
1.218 nicm 1126: next->mode->resize(next, wp->sx, wp->sy);
1127: }
1.142 nicm 1128: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1129:
1.259 nicm 1130: server_redraw_window_borders(wp->window);
1.157 nicm 1131: server_status_window(wp->window);
1.193 nicm 1132: notify_pane("pane-mode-changed", wp);
1.1 nicm 1133: }
1134:
1135: void
1.218 nicm 1136: window_pane_reset_mode_all(struct window_pane *wp)
1137: {
1138: while (!TAILQ_EMPTY(&wp->modes))
1139: window_pane_reset_mode(wp);
1140: }
1141:
1.267 nicm 1142: static void
1143: window_pane_copy_key(struct window_pane *wp, key_code key)
1144: {
1145: struct window_pane *loop;
1146:
1147: TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1148: if (loop != wp &&
1149: TAILQ_EMPTY(&loop->modes) &&
1150: loop->fd != -1 &&
1151: (~loop->flags & PANE_INPUTOFF) &&
1152: window_pane_visible(loop) &&
1153: options_get_number(loop->options, "synchronize-panes"))
1154: input_key_pane(loop, key, NULL);
1155: }
1156: }
1157:
1.247 nicm 1158: int
1.118 nicm 1159: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.216 nicm 1160: struct winlink *wl, key_code key, struct mouse_event *m)
1.1 nicm 1161: {
1.218 nicm 1162: struct window_mode_entry *wme;
1.3 nicm 1163:
1.118 nicm 1164: if (KEYC_IS_MOUSE(key) && m == NULL)
1.247 nicm 1165: return (-1);
1.118 nicm 1166:
1.218 nicm 1167: wme = TAILQ_FIRST(&wp->modes);
1.217 nicm 1168: if (wme != NULL) {
1.261 nicm 1169: if (wme->mode->key != NULL && c != NULL) {
1170: key &= ~KEYC_MASK_FLAGS;
1171: wme->mode->key(wme, c, s, wl, key, m);
1172: }
1.247 nicm 1173: return (0);
1.30 nicm 1174: }
1.27 nicm 1175:
1.113 nicm 1176: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.247 nicm 1177: return (0);
1.113 nicm 1178:
1.250 nicm 1179: if (input_key_pane(wp, key, m) != 0)
1.247 nicm 1180: return (-1);
1.118 nicm 1181:
1182: if (KEYC_IS_MOUSE(key))
1.247 nicm 1183: return (0);
1.267 nicm 1184: if (options_get_number(wp->options, "synchronize-panes"))
1185: window_pane_copy_key(wp, key);
1.247 nicm 1186: return (0);
1.10 nicm 1187: }
1188:
1189: int
1.205 nicm 1190: window_pane_visible(struct window_pane *wp)
1.10 nicm 1191: {
1.213 nicm 1192: if (~wp->window->flags & WINDOW_ZOOMED)
1193: return (1);
1194: return (wp == wp->window->active);
1.1 nicm 1195: }
1196:
1.195 nicm 1197: u_int
1.232 nicm 1198: window_pane_search(struct window_pane *wp, const char *term, int regex,
1199: int ignore)
1.195 nicm 1200: {
1201: struct screen *s = &wp->base;
1.232 nicm 1202: regex_t r;
1203: char *new = NULL, *line;
1.195 nicm 1204: u_int i;
1.232 nicm 1205: int flags = 0, found;
1.236 nicm 1206: size_t n;
1.195 nicm 1207:
1.232 nicm 1208: if (!regex) {
1209: if (ignore)
1210: flags |= FNM_CASEFOLD;
1211: xasprintf(&new, "*%s*", term);
1212: } else {
1213: if (ignore)
1214: flags |= REG_ICASE;
1215: if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1216: return (0);
1217: }
1.195 nicm 1218:
1219: for (i = 0; i < screen_size_y(s); i++) {
1220: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.236 nicm 1221: for (n = strlen(line); n > 0; n--) {
1222: if (!isspace((u_char)line[n - 1]))
1223: break;
1224: line[n - 1] = '\0';
1225: }
1226: log_debug("%s: %s", __func__, line);
1.232 nicm 1227: if (!regex)
1.254 nicm 1228: found = (fnmatch(new, line, flags) == 0);
1.232 nicm 1229: else
1230: found = (regexec(&r, line, 0, NULL, 0) == 0);
1231: free(line);
1232: if (found)
1.195 nicm 1233: break;
1234: }
1.232 nicm 1235: if (!regex)
1236: free(new);
1237: else
1238: regfree(&r);
1.195 nicm 1239:
1240: if (i == screen_size_y(s))
1241: return (0);
1242: return (i + 1);
1.45 nicm 1243: }
1244:
1.109 nicm 1245: /* Get MRU pane from a list. */
1.166 nicm 1246: static struct window_pane *
1.126 nicm 1247: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1248: {
1249: struct window_pane *next, *best;
1250: u_int i;
1251:
1.126 nicm 1252: if (size == 0)
1.109 nicm 1253: return (NULL);
1254:
1.126 nicm 1255: best = list[0];
1256: for (i = 1; i < size; i++) {
1257: next = list[i];
1.109 nicm 1258: if (next->active_point > best->active_point)
1259: best = next;
1260: }
1261: return (best);
1262: }
1263:
1264: /*
1265: * Find the pane directly above another. We build a list of those adjacent to
1266: * top edge and then choose the best.
1267: */
1.45 nicm 1268: struct window_pane *
1269: window_pane_find_up(struct window_pane *wp)
1270: {
1.237 nicm 1271: struct window *w;
1.126 nicm 1272: struct window_pane *next, *best, **list;
1273: u_int edge, left, right, end, size;
1.187 nicm 1274: int status, found;
1.45 nicm 1275:
1.213 nicm 1276: if (wp == NULL)
1.45 nicm 1277: return (NULL);
1.237 nicm 1278: w = wp->window;
1279: status = options_get_number(w->options, "pane-border-status");
1.126 nicm 1280:
1281: list = NULL;
1282: size = 0;
1.109 nicm 1283:
1284: edge = wp->yoff;
1.237 nicm 1285: if (status == PANE_STATUS_TOP) {
1286: if (edge == 1)
1287: edge = w->sy + 1;
1288: } else if (status == PANE_STATUS_BOTTOM) {
1289: if (edge == 0)
1290: edge = w->sy;
1291: } else {
1292: if (edge == 0)
1293: edge = w->sy + 1;
1294: }
1.45 nicm 1295:
1296: left = wp->xoff;
1.109 nicm 1297: right = wp->xoff + wp->sx;
1.45 nicm 1298:
1.237 nicm 1299: TAILQ_FOREACH(next, &w->panes, entry) {
1.213 nicm 1300: if (next == wp)
1.45 nicm 1301: continue;
1.109 nicm 1302: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1303: continue;
1.109 nicm 1304: end = next->xoff + next->sx - 1;
1305:
1306: found = 0;
1307: if (next->xoff < left && end > right)
1308: found = 1;
1309: else if (next->xoff >= left && next->xoff <= right)
1310: found = 1;
1311: else if (end >= left && end <= right)
1312: found = 1;
1.126 nicm 1313: if (!found)
1314: continue;
1315: list = xreallocarray(list, size + 1, sizeof *list);
1316: list[size++] = next;
1.109 nicm 1317: }
1318:
1.126 nicm 1319: best = window_pane_choose_best(list, size);
1320: free(list);
1.109 nicm 1321: return (best);
1.45 nicm 1322: }
1323:
1324: /* Find the pane directly below another. */
1325: struct window_pane *
1326: window_pane_find_down(struct window_pane *wp)
1327: {
1.237 nicm 1328: struct window *w;
1.126 nicm 1329: struct window_pane *next, *best, **list;
1330: u_int edge, left, right, end, size;
1.187 nicm 1331: int status, found;
1.45 nicm 1332:
1.213 nicm 1333: if (wp == NULL)
1.45 nicm 1334: return (NULL);
1.237 nicm 1335: w = wp->window;
1336: status = options_get_number(w->options, "pane-border-status");
1.126 nicm 1337:
1338: list = NULL;
1339: size = 0;
1.109 nicm 1340:
1341: edge = wp->yoff + wp->sy + 1;
1.237 nicm 1342: if (status == PANE_STATUS_TOP) {
1343: if (edge >= w->sy)
1344: edge = 1;
1345: } else if (status == PANE_STATUS_BOTTOM) {
1346: if (edge >= w->sy - 1)
1347: edge = 0;
1348: } else {
1.239 nicm 1349: if (edge >= w->sy)
1.237 nicm 1350: edge = 0;
1351: }
1.45 nicm 1352:
1353: left = wp->xoff;
1.109 nicm 1354: right = wp->xoff + wp->sx;
1.45 nicm 1355:
1.237 nicm 1356: TAILQ_FOREACH(next, &w->panes, entry) {
1.213 nicm 1357: if (next == wp)
1.45 nicm 1358: continue;
1.109 nicm 1359: if (next->yoff != edge)
1.45 nicm 1360: continue;
1.109 nicm 1361: end = next->xoff + next->sx - 1;
1362:
1363: found = 0;
1364: if (next->xoff < left && end > right)
1365: found = 1;
1366: else if (next->xoff >= left && next->xoff <= right)
1367: found = 1;
1368: else if (end >= left && end <= right)
1369: found = 1;
1.126 nicm 1370: if (!found)
1371: continue;
1372: list = xreallocarray(list, size + 1, sizeof *list);
1373: list[size++] = next;
1.45 nicm 1374: }
1.109 nicm 1375:
1.126 nicm 1376: best = window_pane_choose_best(list, size);
1377: free(list);
1.109 nicm 1378: return (best);
1.45 nicm 1379: }
1380:
1.109 nicm 1381: /* Find the pane directly to the left of another. */
1.45 nicm 1382: struct window_pane *
1383: window_pane_find_left(struct window_pane *wp)
1384: {
1.237 nicm 1385: struct window *w;
1.126 nicm 1386: struct window_pane *next, *best, **list;
1387: u_int edge, top, bottom, end, size;
1.109 nicm 1388: int found;
1.45 nicm 1389:
1.213 nicm 1390: if (wp == NULL)
1.45 nicm 1391: return (NULL);
1.237 nicm 1392: w = wp->window;
1.126 nicm 1393:
1394: list = NULL;
1395: size = 0;
1.109 nicm 1396:
1397: edge = wp->xoff;
1398: if (edge == 0)
1.237 nicm 1399: edge = w->sx + 1;
1.45 nicm 1400:
1401: top = wp->yoff;
1.109 nicm 1402: bottom = wp->yoff + wp->sy;
1.45 nicm 1403:
1.237 nicm 1404: TAILQ_FOREACH(next, &w->panes, entry) {
1.213 nicm 1405: if (next == wp)
1.45 nicm 1406: continue;
1.109 nicm 1407: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1408: continue;
1.109 nicm 1409: end = next->yoff + next->sy - 1;
1410:
1411: found = 0;
1412: if (next->yoff < top && end > bottom)
1413: found = 1;
1414: else if (next->yoff >= top && next->yoff <= bottom)
1415: found = 1;
1416: else if (end >= top && end <= bottom)
1417: found = 1;
1.126 nicm 1418: if (!found)
1419: continue;
1420: list = xreallocarray(list, size + 1, sizeof *list);
1421: list[size++] = next;
1.45 nicm 1422: }
1.109 nicm 1423:
1.126 nicm 1424: best = window_pane_choose_best(list, size);
1425: free(list);
1.109 nicm 1426: return (best);
1.45 nicm 1427: }
1428:
1.109 nicm 1429: /* Find the pane directly to the right of another. */
1.45 nicm 1430: struct window_pane *
1431: window_pane_find_right(struct window_pane *wp)
1432: {
1.237 nicm 1433: struct window *w;
1.126 nicm 1434: struct window_pane *next, *best, **list;
1435: u_int edge, top, bottom, end, size;
1.109 nicm 1436: int found;
1.45 nicm 1437:
1.213 nicm 1438: if (wp == NULL)
1.45 nicm 1439: return (NULL);
1.237 nicm 1440: w = wp->window;
1.126 nicm 1441:
1442: list = NULL;
1443: size = 0;
1.109 nicm 1444:
1445: edge = wp->xoff + wp->sx + 1;
1.237 nicm 1446: if (edge >= w->sx)
1.109 nicm 1447: edge = 0;
1.45 nicm 1448:
1449: top = wp->yoff;
1.109 nicm 1450: bottom = wp->yoff + wp->sy;
1.45 nicm 1451:
1.237 nicm 1452: TAILQ_FOREACH(next, &w->panes, entry) {
1.213 nicm 1453: if (next == wp)
1.45 nicm 1454: continue;
1.109 nicm 1455: if (next->xoff != edge)
1.45 nicm 1456: continue;
1.109 nicm 1457: end = next->yoff + next->sy - 1;
1458:
1459: found = 0;
1460: if (next->yoff < top && end > bottom)
1461: found = 1;
1462: else if (next->yoff >= top && next->yoff <= bottom)
1463: found = 1;
1464: else if (end >= top && end <= bottom)
1465: found = 1;
1.126 nicm 1466: if (!found)
1467: continue;
1468: list = xreallocarray(list, size + 1, sizeof *list);
1469: list[size++] = next;
1.109 nicm 1470: }
1471:
1.126 nicm 1472: best = window_pane_choose_best(list, size);
1473: free(list);
1.109 nicm 1474: return (best);
1.81 nicm 1475: }
1476:
1477: /* Clear alert flags for a winlink */
1478: void
1479: winlink_clear_flags(struct winlink *wl)
1480: {
1.174 nicm 1481: struct winlink *loop;
1.81 nicm 1482:
1.174 nicm 1483: wl->window->flags &= ~WINDOW_ALERTFLAGS;
1484: TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1485: if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1486: loop->flags &= ~WINLINK_ALERTFLAGS;
1487: server_status_session(loop->session);
1.81 nicm 1488: }
1489: }
1.134 nicm 1490: }
1491:
1.184 nicm 1492: /* Shuffle window indexes up. */
1.134 nicm 1493: int
1.266 nicm 1494: winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1.134 nicm 1495: {
1496: int idx, last;
1497:
1.226 nicm 1498: if (wl == NULL)
1499: return (-1);
1.266 nicm 1500: if (before)
1501: idx = wl->idx;
1502: else
1503: idx = wl->idx + 1;
1.134 nicm 1504:
1505: /* Find the next free index. */
1506: for (last = idx; last < INT_MAX; last++) {
1507: if (winlink_find_by_index(&s->windows, last) == NULL)
1508: break;
1509: }
1510: if (last == INT_MAX)
1511: return (-1);
1512:
1513: /* Move everything from last - 1 to idx up a bit. */
1514: for (; last > idx; last--) {
1515: wl = winlink_find_by_index(&s->windows, last - 1);
1.266 nicm 1516: RB_REMOVE(winlinks, &s->windows, wl);
1517: wl->idx++;
1518: RB_INSERT(winlinks, &s->windows, wl);
1.134 nicm 1519: }
1520:
1521: return (idx);
1.228 nicm 1522: }
1523:
1524: static void
1.246 nicm 1525: window_pane_input_callback(struct client *c, __unused const char *path,
1526: int error, int closed, struct evbuffer *buffer, void *data)
1.228 nicm 1527: {
1528: struct window_pane_input_data *cdata = data;
1529: struct window_pane *wp;
1.246 nicm 1530: u_char *buf = EVBUFFER_DATA(buffer);
1531: size_t len = EVBUFFER_LENGTH(buffer);
1.228 nicm 1532:
1533: wp = window_pane_find_by_id(cdata->wp);
1.246 nicm 1534: if (wp == NULL || closed || error != 0 || c->flags & CLIENT_DEAD) {
1.243 nicm 1535: if (wp == NULL)
1536: c->flags |= CLIENT_EXIT;
1537:
1.246 nicm 1538: evbuffer_drain(buffer, len);
1539: cmdq_continue(cdata->item);
1540:
1.228 nicm 1541: server_client_unref(c);
1542: free(cdata);
1543: return;
1544: }
1.229 nicm 1545: input_parse_buffer(wp, buf, len);
1.246 nicm 1546: evbuffer_drain(buffer, len);
1.228 nicm 1547: }
1548:
1549: int
1550: window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1551: char **cause)
1552: {
1.256 nicm 1553: struct client *c = cmdq_get_client(item);
1.228 nicm 1554: struct window_pane_input_data *cdata;
1555:
1556: if (~wp->flags & PANE_EMPTY) {
1557: *cause = xstrdup("pane is not empty");
1558: return (-1);
1559: }
1560:
1561: cdata = xmalloc(sizeof *cdata);
1562: cdata->item = item;
1563: cdata->wp = wp->id;
1564:
1.246 nicm 1565: c->references++;
1566: file_read(c, "-", window_pane_input_callback, cdata);
1567:
1568: return (0);
1.263 nicm 1569: }
1570:
1571: void *
1572: window_pane_get_new_data(struct window_pane *wp,
1573: struct window_pane_offset *wpo, size_t *size)
1574: {
1575: size_t used = wpo->used - wp->base_offset;
1576:
1577: *size = EVBUFFER_LENGTH(wp->event->input) - used;
1578: return (EVBUFFER_DATA(wp->event->input) + used);
1579: }
1580:
1581: void
1582: window_pane_update_used_data(struct window_pane *wp,
1.264 nicm 1583: struct window_pane_offset *wpo, size_t size)
1.263 nicm 1584: {
1585: size_t used = wpo->used - wp->base_offset;
1586:
1587: if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1588: size = EVBUFFER_LENGTH(wp->event->input) - used;
1589: wpo->used += size;
1.1 nicm 1590: }