Annotation of src/usr.bin/tmux/window.c, Revision 1.231
1.231 ! nicm 1: /* $OpenBSD: window.c,v 1.230 2019/05/30 20:54:03 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.231 ! nicm 807: wp->saved_cx = UINT_MAX;
! 808: wp->saved_cy = UINT_MAX;
1.117 nicm 809:
1.221 nicm 810: style_set(&wp->style, &grid_default_cell);
1.9 nicm 811:
1.1 nicm 812: screen_init(&wp->base, sx, sy, hlimit);
813: wp->screen = &wp->base;
1.159 nicm 814:
815: screen_init(&wp->status_screen, 1, 1, 0);
1.140 nicm 816:
817: if (gethostname(host, sizeof host) == 0)
818: screen_set_title(&wp->base, host);
1.1 nicm 819:
820: input_init(wp);
821:
822: return (wp);
823: }
824:
1.169 nicm 825: static void
1.1 nicm 826: window_pane_destroy(struct window_pane *wp)
827: {
1.218 nicm 828: window_pane_reset_mode_all(wp);
1.194 nicm 829: free(wp->searchstr);
1.55 nicm 830:
1.37 nicm 831: if (wp->fd != -1) {
1.70 nicm 832: bufferevent_free(wp->event);
1.1 nicm 833: close(wp->fd);
1.37 nicm 834: }
1.1 nicm 835:
836: input_free(wp);
1.225 nicm 837:
838: screen_free(&wp->status_screen);
1.1 nicm 839:
840: screen_free(&wp->base);
1.9 nicm 841: if (wp->saved_grid != NULL)
842: grid_destroy(wp->saved_grid);
1.1 nicm 843:
1.32 nicm 844: if (wp->pipe_fd != -1) {
1.70 nicm 845: bufferevent_free(wp->pipe_event);
1.32 nicm 846: close(wp->pipe_fd);
847: }
1.167 nicm 848:
849: if (event_initialized(&wp->resize_timer))
850: event_del(&wp->resize_timer);
1.32 nicm 851:
1.64 nicm 852: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
853:
1.147 nicm 854: free((void *)wp->cwd);
1.82 nicm 855: free(wp->shell);
1.110 nicm 856: cmd_free_argv(wp->argc, wp->argv);
1.177 nicm 857: free(wp->palette);
1.82 nicm 858: free(wp);
1.1 nicm 859: }
860:
1.166 nicm 861: static void
1.149 nicm 862: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37 nicm 863: {
1.131 nicm 864: struct window_pane *wp = data;
865: struct evbuffer *evb = wp->event->input;
1.166 nicm 866: size_t size = EVBUFFER_LENGTH(evb);
1.131 nicm 867: char *new_data;
1.158 nicm 868: size_t new_size;
1.131 nicm 869:
1.166 nicm 870: new_size = size - wp->pipe_off;
1.46 nicm 871: if (wp->pipe_fd != -1 && new_size > 0) {
1.155 nicm 872: new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46 nicm 873: bufferevent_write(wp->pipe_event, new_data, new_size);
874: }
875:
1.183 nicm 876: log_debug("%%%u has %zu bytes", wp->id, size);
1.46 nicm 877: input_parse(wp);
1.37 nicm 878:
1.173 nicm 879: wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37 nicm 880: }
881:
1.166 nicm 882: static void
1.149 nicm 883: window_pane_error_callback(__unused struct bufferevent *bufev,
884: __unused short what, void *data)
1.37 nicm 885: {
886: struct window_pane *wp = data;
887:
1.200 nicm 888: log_debug("%%%u error", wp->id);
1.201 nicm 889: wp->flags |= PANE_EXITED;
1.200 nicm 890:
891: if (window_pane_destroy_ready(wp))
892: server_destroy_pane(wp, 1);
1.37 nicm 893: }
894:
895: void
1.226 nicm 896: window_pane_set_event(struct window_pane *wp)
897: {
898: setblocking(wp->fd, 0);
899:
900: wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
901: NULL, window_pane_error_callback, wp);
902:
903: bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
904: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
905: }
906:
907: void
1.1 nicm 908: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
909: {
1.218 nicm 910: struct window_mode_entry *wme;
911:
1.1 nicm 912: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 913: return;
1.1 nicm 914: wp->sx = sx;
915: wp->sy = sy;
916:
1.89 nicm 917: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.218 nicm 918:
919: wme = TAILQ_FIRST(&wp->modes);
920: if (wme != NULL && wme->mode->resize != NULL)
921: wme->mode->resize(wme, sx, sy);
1.95 nicm 922:
923: wp->flags |= PANE_RESIZE;
1.44 nicm 924: }
925:
926: /*
927: * Enter alternative screen mode. A copy of the visible screen is saved and the
928: * history is not updated
929: */
930: void
1.87 nicm 931: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
932: int cursor)
1.44 nicm 933: {
934: struct screen *s = &wp->base;
935: u_int sx, sy;
936:
937: if (wp->saved_grid != NULL)
938: return;
1.146 nicm 939: if (!options_get_number(wp->window->options, "alternate-screen"))
1.44 nicm 940: return;
941: sx = screen_size_x(s);
942: sy = screen_size_y(s);
943:
944: wp->saved_grid = grid_create(sx, sy, 0);
945: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 946: if (cursor) {
947: wp->saved_cx = s->cx;
948: wp->saved_cy = s->cy;
949: }
1.44 nicm 950: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
951:
1.170 nicm 952: grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44 nicm 953:
954: wp->base.grid->flags &= ~GRID_HISTORY;
955:
956: wp->flags |= PANE_REDRAW;
957: }
958:
959: /* Exit alternate screen mode and restore the copied grid. */
960: void
1.87 nicm 961: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
962: int cursor)
1.44 nicm 963: {
964: struct screen *s = &wp->base;
965: u_int sx, sy;
966:
1.231 ! nicm 967: if (!options_get_number(wp->window->options, "alternate-screen"))
! 968: return;
! 969:
! 970: /*
! 971: * Restore the cursor position and cell. This happens even if not
! 972: * currently in the alternate screen.
! 973: */
! 974: if (cursor && wp->saved_cx != UINT_MAX && wp->saved_cy != UINT_MAX) {
! 975: s->cx = wp->saved_cx;
! 976: if (s->cx > screen_size_x(s) - 1)
! 977: s->cx = screen_size_x(s) - 1;
! 978: s->cy = wp->saved_cy;
! 979: if (s->cy > screen_size_y(s) - 1)
! 980: s->cy = screen_size_y(s) - 1;
! 981: memcpy(gc, &wp->saved_cell, sizeof *gc);
! 982: }
! 983:
1.44 nicm 984: if (wp->saved_grid == NULL)
985: return;
986: sx = screen_size_x(s);
987: sy = screen_size_y(s);
988:
989: /*
990: * If the current size is bigger, temporarily resize to the old size
991: * before copying back.
992: */
993: if (sy > wp->saved_grid->sy)
1.89 nicm 994: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 995:
1.231 ! nicm 996: /* Restore the saved grid. */
1.44 nicm 997: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
998:
999: /*
1000: * Turn history back on (so resize can use it) and then resize back to
1001: * the current size.
1002: */
1003: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1004: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1005: screen_resize(s, sx, sy, 1);
1.44 nicm 1006:
1007: grid_destroy(wp->saved_grid);
1008: wp->saved_grid = NULL;
1009:
1.177 nicm 1010: wp->flags |= PANE_REDRAW;
1011: }
1012:
1013: void
1014: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
1015: {
1016: if (n > 0xff)
1017: return;
1018:
1019: if (wp->palette == NULL)
1020: wp->palette = xcalloc(0x100, sizeof *wp->palette);
1021:
1022: wp->palette[n] = colour;
1023: wp->flags |= PANE_REDRAW;
1024: }
1025:
1026: void
1027: window_pane_unset_palette(struct window_pane *wp, u_int n)
1028: {
1029: if (n > 0xff || wp->palette == NULL)
1030: return;
1031:
1032: wp->palette[n] = 0;
1033: wp->flags |= PANE_REDRAW;
1034: }
1035:
1036: void
1037: window_pane_reset_palette(struct window_pane *wp)
1038: {
1039: if (wp->palette == NULL)
1040: return;
1041:
1042: free(wp->palette);
1043: wp->palette = NULL;
1.44 nicm 1044: wp->flags |= PANE_REDRAW;
1.1 nicm 1045: }
1046:
1.180 nicm 1047: int
1.220 nicm 1048: window_pane_get_palette(struct window_pane *wp, int c)
1.180 nicm 1049: {
1050: int new;
1051:
1052: if (wp == NULL || wp->palette == NULL)
1053: return (-1);
1054:
1055: new = -1;
1056: if (c < 8)
1057: new = wp->palette[c];
1058: else if (c >= 90 && c <= 97)
1059: new = wp->palette[8 + c - 90];
1060: else if (c & COLOUR_FLAG_256)
1061: new = wp->palette[c & ~COLOUR_FLAG_256];
1062: if (new == 0)
1063: return (-1);
1064: return (new);
1065: }
1066:
1.162 nicm 1067: static void
1068: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
1069: {
1070: struct window_pane *wp = arg;
1071: struct timeval tv = { .tv_sec = 10 };
1072: int n = 0;
1073:
1074: evtimer_del(&wp->modetimer);
1075: evtimer_add(&wp->modetimer, &tv);
1076:
1077: log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
1078:
1079: if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
1080: if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1.218 nicm 1081: window_pane_reset_mode_all(wp);
1.162 nicm 1082: }
1083: }
1084:
1.1 nicm 1085: int
1.196 nicm 1086: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode,
1087: struct cmd_find_state *fs, struct args *args)
1.1 nicm 1088: {
1.218 nicm 1089: struct timeval tv = { .tv_sec = 10 };
1090: struct window_mode_entry *wme;
1.1 nicm 1091:
1.218 nicm 1092: if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1.1 nicm 1093: return (1);
1.217 nicm 1094:
1.162 nicm 1095: wp->modelast = time(NULL);
1.218 nicm 1096: if (TAILQ_EMPTY(&wp->modes)) {
1097: evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
1098: evtimer_add(&wp->modetimer, &tv);
1099: }
1100:
1101: TAILQ_FOREACH(wme, &wp->modes, entry) {
1102: if (wme->mode == mode)
1103: break;
1104: }
1.223 nicm 1105: if (wme != NULL) {
1.218 nicm 1106: TAILQ_REMOVE(&wp->modes, wme, entry);
1.223 nicm 1107: TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1108: } else {
1.218 nicm 1109: wme = xcalloc(1, sizeof *wme);
1110: wme->wp = wp;
1111: wme->mode = mode;
1112: wme->prefix = 1;
1.223 nicm 1113: TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1.218 nicm 1114: wme->screen = wme->mode->init(wme, fs, args);
1115: }
1.162 nicm 1116:
1.218 nicm 1117: wp->screen = wme->screen;
1.142 nicm 1118: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1119:
1120: server_status_window(wp->window);
1.193 nicm 1121: notify_pane("pane-mode-changed", wp);
1.218 nicm 1122:
1.1 nicm 1123: return (0);
1124: }
1125:
1126: void
1127: window_pane_reset_mode(struct window_pane *wp)
1128: {
1.218 nicm 1129: struct window_mode_entry *wme, *next;
1130:
1131: if (TAILQ_EMPTY(&wp->modes))
1.1 nicm 1132: return;
1133:
1.218 nicm 1134: wme = TAILQ_FIRST(&wp->modes);
1135: TAILQ_REMOVE(&wp->modes, wme, entry);
1136: wme->mode->free(wme);
1137: free(wme);
1138:
1139: next = TAILQ_FIRST(&wp->modes);
1140: if (next == NULL) {
1141: log_debug("%s: no next mode", __func__);
1142: evtimer_del(&wp->modetimer);
1143: wp->screen = &wp->base;
1144: } else {
1145: log_debug("%s: next mode is %s", __func__, next->mode->name);
1146: wp->screen = next->screen;
1.230 nicm 1147: if (next->mode->resize != NULL)
1.218 nicm 1148: next->mode->resize(next, wp->sx, wp->sy);
1149: }
1.142 nicm 1150: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1151:
1152: server_status_window(wp->window);
1.193 nicm 1153: notify_pane("pane-mode-changed", wp);
1.1 nicm 1154: }
1155:
1156: void
1.218 nicm 1157: window_pane_reset_mode_all(struct window_pane *wp)
1158: {
1159: while (!TAILQ_EMPTY(&wp->modes))
1160: window_pane_reset_mode(wp);
1161: }
1162:
1163: void
1.118 nicm 1164: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.216 nicm 1165: struct winlink *wl, key_code key, struct mouse_event *m)
1.1 nicm 1166: {
1.218 nicm 1167: struct window_mode_entry *wme;
1.217 nicm 1168: struct window_pane *wp2;
1.3 nicm 1169:
1.118 nicm 1170: if (KEYC_IS_MOUSE(key) && m == NULL)
1171: return;
1172:
1.218 nicm 1173: wme = TAILQ_FIRST(&wp->modes);
1.217 nicm 1174: if (wme != NULL) {
1.162 nicm 1175: wp->modelast = time(NULL);
1.217 nicm 1176: if (wme->mode->key != NULL)
1177: wme->mode->key(wme, c, s, wl, (key & ~KEYC_XTERM), m);
1.27 nicm 1178: return;
1.30 nicm 1179: }
1.27 nicm 1180:
1.113 nicm 1181: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30 nicm 1182: return;
1.113 nicm 1183:
1.118 nicm 1184: input_key(wp, key, m);
1185:
1186: if (KEYC_IS_MOUSE(key))
1187: return;
1.146 nicm 1188: if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27 nicm 1189: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1.213 nicm 1190: if (wp2 != wp &&
1.218 nicm 1191: TAILQ_EMPTY(&wp2->modes) &&
1.213 nicm 1192: wp2->fd != -1 &&
1193: (~wp2->flags & PANE_INPUTOFF) &&
1194: window_pane_visible(wp2))
1.118 nicm 1195: input_key(wp2, key, NULL);
1.27 nicm 1196: }
1197: }
1.10 nicm 1198: }
1199:
1200: int
1.205 nicm 1201: window_pane_visible(struct window_pane *wp)
1.10 nicm 1202: {
1.213 nicm 1203: if (~wp->window->flags & WINDOW_ZOOMED)
1204: return (1);
1205: return (wp == wp->window->active);
1.1 nicm 1206: }
1207:
1.195 nicm 1208: u_int
1209: window_pane_search(struct window_pane *wp, const char *searchstr)
1210: {
1211: struct screen *s = &wp->base;
1212: char *newsearchstr, *line;
1213: u_int i;
1214:
1215: xasprintf(&newsearchstr, "*%s*", searchstr);
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));
1219: if (fnmatch(newsearchstr, line, 0) == 0) {
1220: free(line);
1221: break;
1222: }
1223: free(line);
1224: }
1225:
1226: free(newsearchstr);
1227: if (i == screen_size_y(s))
1228: return (0);
1229: return (i + 1);
1.45 nicm 1230: }
1231:
1.109 nicm 1232: /* Get MRU pane from a list. */
1.166 nicm 1233: static struct window_pane *
1.126 nicm 1234: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1235: {
1236: struct window_pane *next, *best;
1237: u_int i;
1238:
1.126 nicm 1239: if (size == 0)
1.109 nicm 1240: return (NULL);
1241:
1.126 nicm 1242: best = list[0];
1243: for (i = 1; i < size; i++) {
1244: next = list[i];
1.109 nicm 1245: if (next->active_point > best->active_point)
1246: best = next;
1247: }
1248: return (best);
1249: }
1250:
1251: /*
1252: * Find the pane directly above another. We build a list of those adjacent to
1253: * top edge and then choose the best.
1254: */
1.45 nicm 1255: struct window_pane *
1256: window_pane_find_up(struct window_pane *wp)
1257: {
1.126 nicm 1258: struct window_pane *next, *best, **list;
1259: u_int edge, left, right, end, size;
1.187 nicm 1260: int status, found;
1.45 nicm 1261:
1.213 nicm 1262: if (wp == NULL)
1.45 nicm 1263: return (NULL);
1.187 nicm 1264: status = options_get_number(wp->window->options, "pane-border-status");
1.126 nicm 1265:
1266: list = NULL;
1267: size = 0;
1.109 nicm 1268:
1269: edge = wp->yoff;
1.187 nicm 1270: if (edge == (status == 1 ? 1 : 0))
1271: edge = wp->window->sy + 1 - (status == 2 ? 1 : 0);
1.45 nicm 1272:
1273: left = wp->xoff;
1.109 nicm 1274: right = wp->xoff + wp->sx;
1.45 nicm 1275:
1.109 nicm 1276: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1277: if (next == wp)
1.45 nicm 1278: continue;
1.109 nicm 1279: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1280: continue;
1.109 nicm 1281: end = next->xoff + next->sx - 1;
1282:
1283: found = 0;
1284: if (next->xoff < left && end > right)
1285: found = 1;
1286: else if (next->xoff >= left && next->xoff <= right)
1287: found = 1;
1288: else if (end >= left && end <= right)
1289: found = 1;
1.126 nicm 1290: if (!found)
1291: continue;
1292: list = xreallocarray(list, size + 1, sizeof *list);
1293: list[size++] = next;
1.109 nicm 1294: }
1295:
1.126 nicm 1296: best = window_pane_choose_best(list, size);
1297: free(list);
1.109 nicm 1298: return (best);
1.45 nicm 1299: }
1300:
1301: /* Find the pane directly below another. */
1302: struct window_pane *
1303: window_pane_find_down(struct window_pane *wp)
1304: {
1.126 nicm 1305: struct window_pane *next, *best, **list;
1306: u_int edge, left, right, end, size;
1.187 nicm 1307: int status, found;
1.45 nicm 1308:
1.213 nicm 1309: if (wp == NULL)
1.45 nicm 1310: return (NULL);
1.187 nicm 1311: status = options_get_number(wp->window->options, "pane-border-status");
1.126 nicm 1312:
1313: list = NULL;
1314: size = 0;
1.109 nicm 1315:
1316: edge = wp->yoff + wp->sy + 1;
1.187 nicm 1317: if (edge >= wp->window->sy - (status == 2 ? 1 : 0))
1318: edge = (status == 1 ? 1 : 0);
1.45 nicm 1319:
1320: left = wp->xoff;
1.109 nicm 1321: right = wp->xoff + wp->sx;
1.45 nicm 1322:
1.109 nicm 1323: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1324: if (next == wp)
1.45 nicm 1325: continue;
1.109 nicm 1326: if (next->yoff != edge)
1.45 nicm 1327: continue;
1.109 nicm 1328: end = next->xoff + next->sx - 1;
1329:
1330: found = 0;
1331: if (next->xoff < left && end > right)
1332: found = 1;
1333: else if (next->xoff >= left && next->xoff <= right)
1334: found = 1;
1335: else if (end >= left && end <= right)
1336: found = 1;
1.126 nicm 1337: if (!found)
1338: continue;
1339: list = xreallocarray(list, size + 1, sizeof *list);
1340: list[size++] = next;
1.45 nicm 1341: }
1.109 nicm 1342:
1.126 nicm 1343: best = window_pane_choose_best(list, size);
1344: free(list);
1.109 nicm 1345: return (best);
1.45 nicm 1346: }
1347:
1.109 nicm 1348: /* Find the pane directly to the left of another. */
1.45 nicm 1349: struct window_pane *
1350: window_pane_find_left(struct window_pane *wp)
1351: {
1.126 nicm 1352: struct window_pane *next, *best, **list;
1353: u_int edge, top, bottom, end, size;
1.109 nicm 1354: int found;
1.45 nicm 1355:
1.213 nicm 1356: if (wp == NULL)
1.45 nicm 1357: return (NULL);
1.126 nicm 1358:
1359: list = NULL;
1360: size = 0;
1.109 nicm 1361:
1362: edge = wp->xoff;
1363: if (edge == 0)
1364: edge = wp->window->sx + 1;
1.45 nicm 1365:
1366: top = wp->yoff;
1.109 nicm 1367: bottom = wp->yoff + wp->sy;
1.45 nicm 1368:
1.109 nicm 1369: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1370: if (next == wp)
1.45 nicm 1371: continue;
1.109 nicm 1372: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1373: continue;
1.109 nicm 1374: end = next->yoff + next->sy - 1;
1375:
1376: found = 0;
1377: if (next->yoff < top && end > bottom)
1378: found = 1;
1379: else if (next->yoff >= top && next->yoff <= bottom)
1380: found = 1;
1381: else if (end >= top && end <= bottom)
1382: found = 1;
1.126 nicm 1383: if (!found)
1384: continue;
1385: list = xreallocarray(list, size + 1, sizeof *list);
1386: list[size++] = next;
1.45 nicm 1387: }
1.109 nicm 1388:
1.126 nicm 1389: best = window_pane_choose_best(list, size);
1390: free(list);
1.109 nicm 1391: return (best);
1.45 nicm 1392: }
1393:
1.109 nicm 1394: /* Find the pane directly to the right of another. */
1.45 nicm 1395: struct window_pane *
1396: window_pane_find_right(struct window_pane *wp)
1397: {
1.126 nicm 1398: struct window_pane *next, *best, **list;
1399: u_int edge, top, bottom, end, size;
1.109 nicm 1400: int found;
1.45 nicm 1401:
1.213 nicm 1402: if (wp == NULL)
1.45 nicm 1403: return (NULL);
1.126 nicm 1404:
1405: list = NULL;
1406: size = 0;
1.109 nicm 1407:
1408: edge = wp->xoff + wp->sx + 1;
1409: if (edge >= wp->window->sx)
1410: edge = 0;
1.45 nicm 1411:
1412: top = wp->yoff;
1.109 nicm 1413: bottom = wp->yoff + wp->sy;
1.45 nicm 1414:
1.109 nicm 1415: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1.213 nicm 1416: if (next == wp)
1.45 nicm 1417: continue;
1.109 nicm 1418: if (next->xoff != edge)
1.45 nicm 1419: continue;
1.109 nicm 1420: end = next->yoff + next->sy - 1;
1421:
1422: found = 0;
1423: if (next->yoff < top && end > bottom)
1424: found = 1;
1425: else if (next->yoff >= top && next->yoff <= bottom)
1426: found = 1;
1427: else if (end >= top && end <= bottom)
1428: found = 1;
1.126 nicm 1429: if (!found)
1430: continue;
1431: list = xreallocarray(list, size + 1, sizeof *list);
1432: list[size++] = next;
1.109 nicm 1433: }
1434:
1.126 nicm 1435: best = window_pane_choose_best(list, size);
1436: free(list);
1.109 nicm 1437: return (best);
1.81 nicm 1438: }
1439:
1440: /* Clear alert flags for a winlink */
1441: void
1442: winlink_clear_flags(struct winlink *wl)
1443: {
1.174 nicm 1444: struct winlink *loop;
1.81 nicm 1445:
1.174 nicm 1446: wl->window->flags &= ~WINDOW_ALERTFLAGS;
1447: TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1448: if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1449: loop->flags &= ~WINLINK_ALERTFLAGS;
1450: server_status_session(loop->session);
1.81 nicm 1451: }
1452: }
1.134 nicm 1453: }
1454:
1.184 nicm 1455: /* Shuffle window indexes up. */
1.134 nicm 1456: int
1457: winlink_shuffle_up(struct session *s, struct winlink *wl)
1458: {
1459: int idx, last;
1460:
1.226 nicm 1461: if (wl == NULL)
1462: return (-1);
1.134 nicm 1463: idx = wl->idx + 1;
1464:
1465: /* Find the next free index. */
1466: for (last = idx; last < INT_MAX; last++) {
1467: if (winlink_find_by_index(&s->windows, last) == NULL)
1468: break;
1469: }
1470: if (last == INT_MAX)
1471: return (-1);
1472:
1473: /* Move everything from last - 1 to idx up a bit. */
1474: for (; last > idx; last--) {
1475: wl = winlink_find_by_index(&s->windows, last - 1);
1476: server_link_window(s, wl, s, last, 0, 0, NULL);
1477: server_unlink_window(s, wl);
1478: }
1479:
1480: return (idx);
1.228 nicm 1481: }
1482:
1483: static void
1484: window_pane_input_callback(struct client *c, int closed, void *data)
1485: {
1486: struct window_pane_input_data *cdata = data;
1487: struct window_pane *wp;
1.229 nicm 1488: struct evbuffer *evb = c->stdin_data;
1489: u_char *buf = EVBUFFER_DATA(evb);
1490: size_t len = EVBUFFER_LENGTH(evb);
1.228 nicm 1491:
1492: wp = window_pane_find_by_id(cdata->wp);
1493: if (wp == NULL || closed || c->flags & CLIENT_DEAD) {
1494: c->stdin_callback = NULL;
1495: server_client_unref(c);
1496:
1497: cdata->item->flags &= ~CMDQ_WAITING;
1498: free(cdata);
1499:
1500: return;
1501: }
1502:
1.229 nicm 1503: input_parse_buffer(wp, buf, len);
1504: evbuffer_drain(evb, len);
1.228 nicm 1505: }
1506:
1507: int
1508: window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1509: char **cause)
1510: {
1511: struct client *c = item->client;
1512: struct window_pane_input_data *cdata;
1513:
1514: if (~wp->flags & PANE_EMPTY) {
1515: *cause = xstrdup("pane is not empty");
1516: return (-1);
1517: }
1518:
1519: cdata = xmalloc(sizeof *cdata);
1520: cdata->item = item;
1521: cdata->wp = wp->id;
1522:
1523: return (server_set_stdin_callback(c, window_pane_input_callback, cdata,
1524: cause));
1.1 nicm 1525: }