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