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