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