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