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