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