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