Nicholas Marriott | 747cab4 | 2014-11-08 12:27:43 +0000 | [diff] [blame] | 1 | /* $OpenBSD$ */ |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
nicm | 995af0e | 2016-01-19 15:59:12 +0000 | [diff] [blame] | 4 | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 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> |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 20 | |
Nicholas Marriott | 4d9af27 | 2009-01-23 16:59:14 +0000 | [diff] [blame] | 21 | #include <errno.h> |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 22 | #include <fcntl.h> |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 23 | #include <fnmatch.h> |
Nicholas Marriott | 782dd94 | 2016-02-17 23:21:58 +0000 | [diff] [blame] | 24 | #include <signal.h> |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 28 | #include <termios.h> |
| 29 | #include <unistd.h> |
Nicholas Marriott | cf77c80 | 2007-10-19 20:47:09 +0000 | [diff] [blame] | 30 | |
Nicholas Marriott | c8cf438 | 2009-05-13 23:27:00 +0000 | [diff] [blame] | 31 | #include "tmux.h" |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 32 | |
| 33 | /* |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 34 | * Each window is attached to a number of panes, each of which is a pty. This |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 35 | * file contains code to handle them. |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 36 | * |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 37 | * A pane has two buffers attached, these are filled and emptied by the main |
Nicholas Marriott | b9de906 | 2007-08-27 10:08:44 +0000 | [diff] [blame] | 38 | * server poll loop. Output data is received from pty's in screen format, |
Nicholas Marriott | 35591ec | 2007-11-07 19:41:17 +0000 | [diff] [blame] | 39 | * translated and returned as a series of escape sequences and strings via |
| 40 | * input_parse (in input.c). Input data is received as key codes and written |
Nicholas Marriott | ccfeb31 | 2008-01-02 19:22:21 +0000 | [diff] [blame] | 41 | * directly via input_key. |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 42 | * |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 43 | * Each pane also has a "virtual" screen (screen.c) which contains the current |
| 44 | * state and is redisplayed when the window is reattached to a client. |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 45 | * |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 46 | * Windows are stored directly on a global array and wrapped in any number of |
Nicholas Marriott | 35591ec | 2007-11-07 19:41:17 +0000 | [diff] [blame] | 47 | * winlink structs to be linked onto local session RB trees. A reference count |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 48 | * is maintained and a window removed from the global list and destroyed when |
Nicholas Marriott | 35591ec | 2007-11-07 19:41:17 +0000 | [diff] [blame] | 49 | * it reaches zero. |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | /* Global window list. */ |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 53 | struct windows windows; |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 54 | |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 55 | /* Global panes tree. */ |
| 56 | struct window_pane_tree all_window_panes; |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 57 | u_int next_window_pane_id; |
| 58 | u_int next_window_id; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 59 | u_int next_active_point; |
nicm | c930fd5 | 2014-01-28 22:19:17 +0000 | [diff] [blame] | 60 | |
Tiago Cunha | f9f6eea | 2012-03-29 21:05:16 +0000 | [diff] [blame] | 61 | void window_pane_timer_callback(int, short, void *); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 62 | void window_pane_read_callback(struct bufferevent *, void *); |
| 63 | void window_pane_error_callback(struct bufferevent *, short, void *); |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 64 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 65 | struct window_pane *window_pane_choose_best(struct window_pane **, u_int); |
Nicholas Marriott | 22d1b94 | 2009-07-01 19:42:55 +0000 | [diff] [blame] | 66 | |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 67 | RB_GENERATE(windows, window, entry, window_cmp); |
| 68 | |
| 69 | int |
| 70 | window_cmp(struct window *w1, struct window *w2) |
| 71 | { |
| 72 | return (w1->id - w2->id); |
| 73 | } |
| 74 | |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 75 | RB_GENERATE(winlinks, winlink, entry, winlink_cmp); |
| 76 | |
| 77 | int |
| 78 | winlink_cmp(struct winlink *wl1, struct winlink *wl2) |
| 79 | { |
| 80 | return (wl1->idx - wl2->idx); |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp); |
| 84 | |
| 85 | int |
| 86 | window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2) |
| 87 | { |
| 88 | return (wp1->id - wp2->id); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | struct winlink * |
Nicholas Marriott | 8bfbc8c | 2009-07-15 17:45:09 +0000 | [diff] [blame] | 92 | winlink_find_by_window(struct winlinks *wwl, struct window *w) |
| 93 | { |
| 94 | struct winlink *wl; |
| 95 | |
| 96 | RB_FOREACH(wl, winlinks, wwl) { |
| 97 | if (wl->window == w) |
| 98 | return (wl); |
| 99 | } |
| 100 | |
| 101 | return (NULL); |
| 102 | } |
| 103 | |
| 104 | struct winlink * |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 105 | winlink_find_by_index(struct winlinks *wwl, int idx) |
| 106 | { |
| 107 | struct winlink wl; |
| 108 | |
| 109 | if (idx < 0) |
| 110 | fatalx("bad index"); |
| 111 | |
| 112 | wl.idx = idx; |
| 113 | return (RB_FIND(winlinks, wwl, &wl)); |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | struct winlink * |
| 117 | winlink_find_by_window_id(struct winlinks *wwl, u_int id) |
| 118 | { |
| 119 | struct winlink *wl; |
| 120 | |
| 121 | RB_FOREACH(wl, winlinks, wwl) { |
| 122 | if (wl->window->id == id) |
| 123 | return (wl); |
| 124 | } |
Tiago Cunha | e5b3858 | 2012-04-10 09:54:29 +0000 | [diff] [blame] | 125 | return (NULL); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int |
Tiago Cunha | e61ee94 | 2009-08-16 19:16:27 +0000 | [diff] [blame] | 129 | winlink_next_index(struct winlinks *wwl, int idx) |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 130 | { |
Tiago Cunha | e61ee94 | 2009-08-16 19:16:27 +0000 | [diff] [blame] | 131 | int i; |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 132 | |
Tiago Cunha | e61ee94 | 2009-08-16 19:16:27 +0000 | [diff] [blame] | 133 | i = idx; |
| 134 | do { |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 135 | if (winlink_find_by_index(wwl, i) == NULL) |
| 136 | return (i); |
Tiago Cunha | e61ee94 | 2009-08-16 19:16:27 +0000 | [diff] [blame] | 137 | if (i == INT_MAX) |
| 138 | i = 0; |
| 139 | else |
| 140 | i++; |
| 141 | } while (i != idx); |
| 142 | return (-1); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Nicholas Marriott | fd05d07 | 2009-01-10 14:43:43 +0000 | [diff] [blame] | 145 | u_int |
| 146 | winlink_count(struct winlinks *wwl) |
| 147 | { |
| 148 | struct winlink *wl; |
| 149 | u_int n; |
| 150 | |
| 151 | n = 0; |
| 152 | RB_FOREACH(wl, winlinks, wwl) |
| 153 | n++; |
| 154 | |
| 155 | return (n); |
| 156 | } |
| 157 | |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 158 | struct winlink * |
Tiago Cunha | 4e4568c | 2011-02-15 15:09:52 +0000 | [diff] [blame] | 159 | winlink_add(struct winlinks *wwl, int idx) |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 160 | { |
| 161 | struct winlink *wl; |
| 162 | |
Tiago Cunha | e61ee94 | 2009-08-16 19:16:27 +0000 | [diff] [blame] | 163 | if (idx < 0) { |
| 164 | if ((idx = winlink_next_index(wwl, -idx - 1)) == -1) |
| 165 | return (NULL); |
| 166 | } else if (winlink_find_by_index(wwl, idx) != NULL) |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 167 | return (NULL); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 168 | |
| 169 | wl = xcalloc(1, sizeof *wl); |
| 170 | wl->idx = idx; |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 171 | RB_INSERT(winlinks, wwl, wl); |
| 172 | |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 173 | return (wl); |
Tiago Cunha | 4e4568c | 2011-02-15 15:09:52 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
| 177 | winlink_set_window(struct winlink *wl, struct window *w) |
| 178 | { |
| 179 | wl->window = w; |
| 180 | w->references++; |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | winlink_remove(struct winlinks *wwl, struct winlink *wl) |
| 185 | { |
| 186 | struct window *w = wl->window; |
| 187 | |
| 188 | RB_REMOVE(winlinks, wwl, wl); |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 189 | free(wl->status_text); |
| 190 | free(wl); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 191 | |
Tiago Cunha | 56e3748 | 2012-08-31 09:22:08 +0000 | [diff] [blame] | 192 | if (w != NULL) |
| 193 | window_remove_ref(w); |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | struct winlink * |
Tiago Cunha | c12e0b0 | 2009-11-28 14:50:37 +0000 | [diff] [blame] | 197 | winlink_next(struct winlink *wl) |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 198 | { |
| 199 | return (RB_NEXT(winlinks, wwl, wl)); |
Nicholas Marriott | 103748d | 2007-12-06 09:46:23 +0000 | [diff] [blame] | 200 | } |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 201 | |
| 202 | struct winlink * |
Tiago Cunha | c12e0b0 | 2009-11-28 14:50:37 +0000 | [diff] [blame] | 203 | winlink_previous(struct winlink *wl) |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 204 | { |
Nicholas Marriott | 85d520c | 2008-06-03 18:38:51 +0000 | [diff] [blame] | 205 | return (RB_PREV(winlinks, wwl, wl)); |
Nicholas Marriott | 103748d | 2007-12-06 09:46:23 +0000 | [diff] [blame] | 206 | } |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 207 | |
Tiago Cunha | 8d3b726 | 2010-06-22 23:29:05 +0000 | [diff] [blame] | 208 | struct winlink * |
Tiago Cunha | 11f81e8 | 2010-07-17 14:38:13 +0000 | [diff] [blame] | 209 | winlink_next_by_number(struct winlink *wl, struct session *s, int n) |
Tiago Cunha | 8d3b726 | 2010-06-22 23:29:05 +0000 | [diff] [blame] | 210 | { |
| 211 | for (; n > 0; n--) { |
| 212 | if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL) |
Tiago Cunha | 11f81e8 | 2010-07-17 14:38:13 +0000 | [diff] [blame] | 213 | wl = RB_MIN(winlinks, &s->windows); |
Tiago Cunha | 8d3b726 | 2010-06-22 23:29:05 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | return (wl); |
| 217 | } |
| 218 | |
| 219 | struct winlink * |
Tiago Cunha | 11f81e8 | 2010-07-17 14:38:13 +0000 | [diff] [blame] | 220 | winlink_previous_by_number(struct winlink *wl, struct session *s, int n) |
Tiago Cunha | 8d3b726 | 2010-06-22 23:29:05 +0000 | [diff] [blame] | 221 | { |
| 222 | for (; n > 0; n--) { |
| 223 | if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL) |
Tiago Cunha | 11f81e8 | 2010-07-17 14:38:13 +0000 | [diff] [blame] | 224 | wl = RB_MAX(winlinks, &s->windows); |
Tiago Cunha | 8d3b726 | 2010-06-22 23:29:05 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return (wl); |
| 228 | } |
| 229 | |
Nicholas Marriott | 46f5e42 | 2008-11-16 10:10:26 +0000 | [diff] [blame] | 230 | void |
| 231 | winlink_stack_push(struct winlink_stack *stack, struct winlink *wl) |
| 232 | { |
| 233 | if (wl == NULL) |
| 234 | return; |
| 235 | |
| 236 | winlink_stack_remove(stack, wl); |
Tiago Cunha | 6a1ebb1 | 2009-10-11 23:38:16 +0000 | [diff] [blame] | 237 | TAILQ_INSERT_HEAD(stack, wl, sentry); |
Nicholas Marriott | 46f5e42 | 2008-11-16 10:10:26 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
| 241 | winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl) |
| 242 | { |
| 243 | struct winlink *wl2; |
| 244 | |
| 245 | if (wl == NULL) |
| 246 | return; |
Tiago Cunha | cc094fd | 2009-12-04 22:14:47 +0000 | [diff] [blame] | 247 | |
Tiago Cunha | 6a1ebb1 | 2009-10-11 23:38:16 +0000 | [diff] [blame] | 248 | TAILQ_FOREACH(wl2, stack, sentry) { |
Nicholas Marriott | 46f5e42 | 2008-11-16 10:10:26 +0000 | [diff] [blame] | 249 | if (wl2 == wl) { |
Tiago Cunha | 6a1ebb1 | 2009-10-11 23:38:16 +0000 | [diff] [blame] | 250 | TAILQ_REMOVE(stack, wl, sentry); |
Nicholas Marriott | 46f5e42 | 2008-11-16 10:10:26 +0000 | [diff] [blame] | 251 | return; |
| 252 | } |
| 253 | } |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 256 | struct window * |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 257 | window_find_by_id_str(const char *s) |
nicm | 6dbd63b | 2015-04-25 18:09:28 +0000 | [diff] [blame] | 258 | { |
| 259 | const char *errstr; |
| 260 | u_int id; |
| 261 | |
| 262 | if (*s != '@') |
| 263 | return (NULL); |
| 264 | |
| 265 | id = strtonum(s + 1, 0, UINT_MAX, &errstr); |
| 266 | if (errstr != NULL) |
| 267 | return (NULL); |
| 268 | return (window_find_by_id(id)); |
Nicholas Marriott | 46f5e42 | 2008-11-16 10:10:26 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 271 | struct window * |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 272 | window_find_by_id(u_int id) |
| 273 | { |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 274 | struct window w; |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 275 | |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 276 | w.id = id; |
| 277 | return (RB_FIND(windows, &windows, &w)); |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 278 | } |
| 279 | |
nicm | b5aaefc | 2015-08-29 08:30:54 +0000 | [diff] [blame] | 280 | void |
| 281 | window_update_activity(struct window *w) |
| 282 | { |
| 283 | gettimeofday(&w->activity_time, NULL); |
| 284 | alerts_queue(w, WINDOW_ACTIVITY); |
| 285 | } |
| 286 | |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 287 | struct window * |
Nicholas Marriott | 56f80a5 | 2009-03-07 09:29:54 +0000 | [diff] [blame] | 288 | window_create1(u_int sx, u_int sy) |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 289 | { |
| 290 | struct window *w; |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 291 | |
Tiago Cunha | ab38d91 | 2009-11-08 23:22:24 +0000 | [diff] [blame] | 292 | w = xcalloc(1, sizeof *w); |
Nicholas Marriott | 4d9af27 | 2009-01-23 16:59:14 +0000 | [diff] [blame] | 293 | w->name = NULL; |
Nicholas Marriott | 0f95671 | 2008-06-04 17:54:27 +0000 | [diff] [blame] | 294 | w->flags = 0; |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 295 | |
| 296 | TAILQ_INIT(&w->panes); |
| 297 | w->active = NULL; |
Nicholas Marriott | d90d646 | 2008-06-29 07:04:31 +0000 | [diff] [blame] | 298 | |
Tiago Cunha | d9dcc5e | 2009-07-28 23:04:29 +0000 | [diff] [blame] | 299 | w->lastlayout = -1; |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 300 | w->layout_root = NULL; |
Tiago Cunha | cc094fd | 2009-12-04 22:14:47 +0000 | [diff] [blame] | 301 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 302 | w->sx = sx; |
| 303 | w->sy = sy; |
Tiago Cunha | ab38d91 | 2009-11-08 23:22:24 +0000 | [diff] [blame] | 304 | |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 305 | w->options = options_create(global_w_options); |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 306 | |
Nicholas Marriott | 4ba3cf6 | 2007-10-26 12:29:07 +0000 | [diff] [blame] | 307 | w->references = 0; |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 308 | |
| 309 | w->id = next_window_id++; |
nicm | 8e9b6e0 | 2015-05-07 11:42:56 +0000 | [diff] [blame] | 310 | RB_INSERT(windows, &windows, w); |
nicm | b5aaefc | 2015-08-29 08:30:54 +0000 | [diff] [blame] | 311 | |
| 312 | window_update_activity(w); |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 313 | |
Nicholas Marriott | 56f80a5 | 2009-03-07 09:29:54 +0000 | [diff] [blame] | 314 | return (w); |
| 315 | } |
| 316 | |
| 317 | struct window * |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 318 | window_create(const char *name, int argc, char **argv, const char *path, |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 319 | const char *shell, const char *cwd, struct environ *env, |
| 320 | struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause) |
Nicholas Marriott | 56f80a5 | 2009-03-07 09:29:54 +0000 | [diff] [blame] | 321 | { |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 322 | struct window *w; |
| 323 | struct window_pane *wp; |
Nicholas Marriott | 56f80a5 | 2009-03-07 09:29:54 +0000 | [diff] [blame] | 324 | |
| 325 | w = window_create1(sx, sy); |
Tiago Cunha | 6708ad1 | 2009-07-23 13:10:38 +0000 | [diff] [blame] | 326 | wp = window_add_pane(w, hlimit); |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 327 | layout_init(w, wp); |
Nicholas Marriott | 31407b7 | 2013-02-22 14:31:38 +0000 | [diff] [blame] | 328 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 329 | if (window_pane_spawn(wp, argc, argv, path, shell, cwd, env, tio, |
nicm | 3e27be3 | 2014-04-17 13:02:59 +0000 | [diff] [blame] | 330 | cause) != 0) { |
Nicholas Marriott | d90d646 | 2008-06-29 07:04:31 +0000 | [diff] [blame] | 331 | window_destroy(w); |
| 332 | return (NULL); |
| 333 | } |
Nicholas Marriott | 31407b7 | 2013-02-22 14:31:38 +0000 | [diff] [blame] | 334 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 335 | w->active = TAILQ_FIRST(&w->panes); |
Nicholas Marriott | 2d15f59 | 2009-01-20 19:35:03 +0000 | [diff] [blame] | 336 | if (name != NULL) { |
| 337 | w->name = xstrdup(name); |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 338 | options_set_number(w->options, "automatic-rename", 0); |
Nicholas Marriott | 2d15f59 | 2009-01-20 19:35:03 +0000 | [diff] [blame] | 339 | } else |
| 340 | w->name = default_window_name(w); |
Nicholas Marriott | 31407b7 | 2013-02-22 14:31:38 +0000 | [diff] [blame] | 341 | |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 342 | return (w); |
| 343 | } |
| 344 | |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 345 | void |
| 346 | window_destroy(struct window *w) |
| 347 | { |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 348 | RB_REMOVE(windows, &windows, w); |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 349 | |
| 350 | if (w->layout_root != NULL) |
nicm | bad8d0f | 2015-07-17 13:09:07 +0000 | [diff] [blame] | 351 | layout_free_cell(w->layout_root); |
| 352 | if (w->saved_layout_root != NULL) |
| 353 | layout_free_cell(w->saved_layout_root); |
nicm | 7717444 | 2015-04-28 10:43:13 +0000 | [diff] [blame] | 354 | free(w->old_layout); |
Tiago Cunha | ab38d91 | 2009-11-08 23:22:24 +0000 | [diff] [blame] | 355 | |
nicm | b7861f3 | 2015-08-29 00:29:15 +0000 | [diff] [blame] | 356 | if (event_initialized(&w->name_event)) |
| 357 | evtimer_del(&w->name_event); |
nicm | b5aaefc | 2015-08-29 08:30:54 +0000 | [diff] [blame] | 358 | |
| 359 | if (event_initialized(&w->alerts_timer)) |
| 360 | evtimer_del(&w->alerts_timer); |
Nicholas Marriott | 103748d | 2007-12-06 09:46:23 +0000 | [diff] [blame] | 361 | |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 362 | options_free(w->options); |
Nicholas Marriott | 9d563c3 | 2007-10-01 14:15:48 +0000 | [diff] [blame] | 363 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 364 | window_destroy_panes(w); |
Nicholas Marriott | 4d9af27 | 2009-01-23 16:59:14 +0000 | [diff] [blame] | 365 | |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 366 | free(w->name); |
| 367 | free(w); |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Tiago Cunha | d29b57f | 2009-07-22 17:46:53 +0000 | [diff] [blame] | 370 | void |
Tiago Cunha | 56e3748 | 2012-08-31 09:22:08 +0000 | [diff] [blame] | 371 | window_remove_ref(struct window *w) |
| 372 | { |
| 373 | if (w->references == 0) |
| 374 | fatal("bad reference count"); |
| 375 | w->references--; |
| 376 | if (w->references == 0) |
| 377 | window_destroy(w); |
| 378 | } |
| 379 | |
| 380 | void |
Tiago Cunha | c82e068 | 2012-02-02 02:00:12 +0000 | [diff] [blame] | 381 | window_set_name(struct window *w, const char *new_name) |
| 382 | { |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 383 | free(w->name); |
Tiago Cunha | c82e068 | 2012-02-02 02:00:12 +0000 | [diff] [blame] | 384 | w->name = xstrdup(new_name); |
Tiago Cunha | f41efd9 | 2012-03-18 02:22:09 +0000 | [diff] [blame] | 385 | notify_window_renamed(w); |
Tiago Cunha | c82e068 | 2012-02-02 02:00:12 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 389 | window_resize(struct window *w, u_int sx, u_int sy) |
| 390 | { |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 391 | w->sx = sx; |
| 392 | w->sy = sy; |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 393 | } |
| 394 | |
nicm | a02c2e5 | 2014-10-21 22:22:04 +0000 | [diff] [blame] | 395 | int |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 396 | window_has_pane(struct window *w, struct window_pane *wp) |
| 397 | { |
| 398 | struct window_pane *wp1; |
| 399 | |
| 400 | TAILQ_FOREACH(wp1, &w->panes, entry) { |
| 401 | if (wp1 == wp) |
| 402 | return (1); |
| 403 | } |
| 404 | return (0); |
| 405 | } |
| 406 | |
| 407 | int |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 408 | window_set_active_pane(struct window *w, struct window_pane *wp) |
| 409 | { |
Nicholas Marriott | 36e537b | 2010-12-06 21:53:50 +0000 | [diff] [blame] | 410 | if (wp == w->active) |
nicm | a02c2e5 | 2014-10-21 22:22:04 +0000 | [diff] [blame] | 411 | return (0); |
Tiago Cunha | cd079e8 | 2010-10-24 01:34:30 +0000 | [diff] [blame] | 412 | w->last = w->active; |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 413 | w->active = wp; |
Nicholas Marriott | 1e574bb | 2009-07-15 17:42:44 +0000 | [diff] [blame] | 414 | while (!window_pane_visible(w->active)) { |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 415 | w->active = TAILQ_PREV(w->active, window_panes, entry); |
Nicholas Marriott | 1e574bb | 2009-07-15 17:42:44 +0000 | [diff] [blame] | 416 | if (w->active == NULL) |
| 417 | w->active = TAILQ_LAST(&w->panes, window_panes); |
| 418 | if (w->active == wp) |
nicm | a02c2e5 | 2014-10-21 22:22:04 +0000 | [diff] [blame] | 419 | return (1); |
Nicholas Marriott | 1e574bb | 2009-07-15 17:42:44 +0000 | [diff] [blame] | 420 | } |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 421 | w->active->active_point = next_active_point++; |
nicm | fc58e44 | 2015-08-28 07:49:24 +0000 | [diff] [blame] | 422 | w->active->flags |= PANE_CHANGED; |
nicm | a02c2e5 | 2014-10-21 22:22:04 +0000 | [diff] [blame] | 423 | return (1); |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 424 | } |
| 425 | |
nicm | af16ce6 | 2015-09-14 11:34:50 +0000 | [diff] [blame] | 426 | void |
| 427 | window_redraw_active_switch(struct window *w, struct window_pane *wp) |
| 428 | { |
| 429 | const struct grid_cell *agc, *wgc; |
| 430 | |
| 431 | if (wp == w->active) |
| 432 | return; |
| 433 | |
| 434 | /* |
| 435 | * If window-style and window-active-style are the same, we don't need |
| 436 | * to redraw panes when switching active panes. Otherwise, if the |
| 437 | * active or inactive pane do not have a custom style, they will need |
| 438 | * to be redrawn. |
| 439 | */ |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 440 | agc = options_get_style(w->options, "window-active-style"); |
| 441 | wgc = options_get_style(w->options, "window-style"); |
nicm | af16ce6 | 2015-09-14 11:34:50 +0000 | [diff] [blame] | 442 | if (style_equal(agc, wgc)) |
| 443 | return; |
| 444 | if (style_equal(&grid_default_cell, &w->active->colgc)) |
| 445 | w->active->flags |= PANE_REDRAW; |
| 446 | if (style_equal(&grid_default_cell, &wp->colgc)) |
| 447 | wp->flags |= PANE_REDRAW; |
| 448 | } |
| 449 | |
Nicholas Marriott | fa0f10d | 2011-06-23 19:21:26 +0000 | [diff] [blame] | 450 | struct window_pane * |
| 451 | window_get_active_at(struct window *w, u_int x, u_int y) |
| 452 | { |
| 453 | struct window_pane *wp; |
| 454 | |
| 455 | TAILQ_FOREACH(wp, &w->panes, entry) { |
| 456 | if (!window_pane_visible(wp)) |
| 457 | continue; |
| 458 | if (x < wp->xoff || x > wp->xoff + wp->sx) |
| 459 | continue; |
| 460 | if (y < wp->yoff || y > wp->yoff + wp->sy) |
| 461 | continue; |
| 462 | return (wp); |
| 463 | } |
| 464 | return (NULL); |
Nicholas Marriott | fa0f10d | 2011-06-23 19:21:26 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | struct window_pane * |
| 468 | window_find_string(struct window *w, const char *s) |
| 469 | { |
| 470 | u_int x, y; |
| 471 | |
| 472 | x = w->sx / 2; |
| 473 | y = w->sy / 2; |
| 474 | |
| 475 | if (strcasecmp(s, "top") == 0) |
| 476 | y = 0; |
| 477 | else if (strcasecmp(s, "bottom") == 0) |
| 478 | y = w->sy - 1; |
| 479 | else if (strcasecmp(s, "left") == 0) |
| 480 | x = 0; |
| 481 | else if (strcasecmp(s, "right") == 0) |
| 482 | x = w->sx - 1; |
| 483 | else if (strcasecmp(s, "top-left") == 0) { |
| 484 | x = 0; |
| 485 | y = 0; |
| 486 | } else if (strcasecmp(s, "top-right") == 0) { |
| 487 | x = w->sx - 1; |
| 488 | y = 0; |
| 489 | } else if (strcasecmp(s, "bottom-left") == 0) { |
| 490 | x = 0; |
| 491 | y = w->sy - 1; |
| 492 | } else if (strcasecmp(s, "bottom-right") == 0) { |
| 493 | x = w->sx - 1; |
| 494 | y = w->sy - 1; |
| 495 | } else |
| 496 | return (NULL); |
| 497 | |
| 498 | return (window_get_active_at(w, x, y)); |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | int |
| 502 | window_zoom(struct window_pane *wp) |
| 503 | { |
| 504 | struct window *w = wp->window; |
| 505 | struct window_pane *wp1; |
| 506 | |
| 507 | if (w->flags & WINDOW_ZOOMED) |
| 508 | return (-1); |
| 509 | |
| 510 | if (!window_pane_visible(wp)) |
| 511 | return (-1); |
Nicholas Marriott | 8aa40ec | 2013-03-12 12:18:52 +0000 | [diff] [blame] | 512 | |
| 513 | if (window_count_panes(w) == 1) |
| 514 | return (-1); |
| 515 | |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 516 | if (w->active != wp) |
| 517 | window_set_active_pane(w, wp); |
| 518 | |
| 519 | TAILQ_FOREACH(wp1, &w->panes, entry) { |
| 520 | wp1->saved_layout_cell = wp1->layout_cell; |
| 521 | wp1->layout_cell = NULL; |
| 522 | } |
| 523 | |
| 524 | w->saved_layout_root = w->layout_root; |
| 525 | layout_init(w, wp); |
| 526 | w->flags |= WINDOW_ZOOMED; |
nicm | 160e3e2 | 2014-12-15 10:04:18 +0000 | [diff] [blame] | 527 | notify_window_layout_changed(w); |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 528 | |
| 529 | return (0); |
| 530 | } |
| 531 | |
| 532 | int |
| 533 | window_unzoom(struct window *w) |
| 534 | { |
Nicholas Marriott | 771d7db | 2013-03-26 10:54:48 +0000 | [diff] [blame] | 535 | struct window_pane *wp; |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 536 | |
| 537 | if (!(w->flags & WINDOW_ZOOMED)) |
| 538 | return (-1); |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 539 | |
| 540 | w->flags &= ~WINDOW_ZOOMED; |
| 541 | layout_free(w); |
| 542 | w->layout_root = w->saved_layout_root; |
nicm | 69f292a | 2015-04-21 22:38:49 +0000 | [diff] [blame] | 543 | w->saved_layout_root = NULL; |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 544 | |
Nicholas Marriott | 771d7db | 2013-03-26 10:54:48 +0000 | [diff] [blame] | 545 | TAILQ_FOREACH(wp, &w->panes, entry) { |
| 546 | wp->layout_cell = wp->saved_layout_cell; |
| 547 | wp->saved_layout_cell = NULL; |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 548 | } |
| 549 | layout_fix_panes(w, w->sx, w->sy); |
nicm | 160e3e2 | 2014-12-15 10:04:18 +0000 | [diff] [blame] | 550 | notify_window_layout_changed(w); |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 551 | |
| 552 | return (0); |
Tiago Cunha | ea1721b | 2009-10-11 23:46:02 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 555 | struct window_pane * |
Tiago Cunha | 6708ad1 | 2009-07-23 13:10:38 +0000 | [diff] [blame] | 556 | window_add_pane(struct window *w, u_int hlimit) |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 557 | { |
| 558 | struct window_pane *wp; |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 559 | |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 560 | wp = window_pane_create(w, w->sx, w->sy, hlimit); |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 561 | if (TAILQ_EMPTY(&w->panes)) |
| 562 | TAILQ_INSERT_HEAD(&w->panes, wp, entry); |
| 563 | else |
| 564 | TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry); |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 565 | return (wp); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 568 | void |
nicm | 2e98c90 | 2014-04-17 09:13:13 +0000 | [diff] [blame] | 569 | window_lost_pane(struct window *w, struct window_pane *wp) |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 570 | { |
nicm | 12da13c | 2015-12-15 00:00:01 +0000 | [diff] [blame] | 571 | if (wp == marked_pane.wp) |
nicm | a863834 | 2015-06-04 11:43:51 +0000 | [diff] [blame] | 572 | server_clear_marked(); |
| 573 | |
Tiago Cunha | 8703e9f | 2010-10-24 01:32:35 +0000 | [diff] [blame] | 574 | if (wp == w->active) { |
Tiago Cunha | cd079e8 | 2010-10-24 01:34:30 +0000 | [diff] [blame] | 575 | w->active = w->last; |
| 576 | w->last = NULL; |
| 577 | if (w->active == NULL) { |
| 578 | w->active = TAILQ_PREV(wp, window_panes, entry); |
| 579 | if (w->active == NULL) |
| 580 | w->active = TAILQ_NEXT(wp, entry); |
| 581 | } |
nicm | 7236838 | 2015-12-02 23:09:22 +0000 | [diff] [blame] | 582 | if (w->active != NULL) |
| 583 | w->active->flags |= PANE_CHANGED; |
Tiago Cunha | cd079e8 | 2010-10-24 01:34:30 +0000 | [diff] [blame] | 584 | } else if (wp == w->last) |
| 585 | w->last = NULL; |
nicm | 2e98c90 | 2014-04-17 09:13:13 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void |
| 589 | window_remove_pane(struct window *w, struct window_pane *wp) |
| 590 | { |
| 591 | window_lost_pane(w, wp); |
Nicholas Marriott | f855591 | 2009-01-13 06:50:10 +0000 | [diff] [blame] | 592 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 593 | TAILQ_REMOVE(&w->panes, wp, entry); |
| 594 | window_pane_destroy(wp); |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | struct window_pane * |
| 598 | window_pane_at_index(struct window *w, u_int idx) |
| 599 | { |
| 600 | struct window_pane *wp; |
| 601 | u_int n; |
| 602 | |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 603 | n = options_get_number(w->options, "pane-base-index"); |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 604 | TAILQ_FOREACH(wp, &w->panes, entry) { |
| 605 | if (n == idx) |
| 606 | return (wp); |
| 607 | n++; |
| 608 | } |
| 609 | return (NULL); |
| 610 | } |
| 611 | |
Tiago Cunha | 11f81e8 | 2010-07-17 14:38:13 +0000 | [diff] [blame] | 612 | struct window_pane * |
| 613 | window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n) |
| 614 | { |
| 615 | for (; n > 0; n--) { |
| 616 | if ((wp = TAILQ_NEXT(wp, entry)) == NULL) |
| 617 | wp = TAILQ_FIRST(&w->panes); |
| 618 | } |
| 619 | |
| 620 | return (wp); |
| 621 | } |
| 622 | |
| 623 | struct window_pane * |
| 624 | window_pane_previous_by_number(struct window *w, struct window_pane *wp, |
| 625 | u_int n) |
| 626 | { |
| 627 | for (; n > 0; n--) { |
| 628 | if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL) |
| 629 | wp = TAILQ_LAST(&w->panes, window_panes); |
| 630 | } |
| 631 | |
| 632 | return (wp); |
| 633 | } |
| 634 | |
Tiago Cunha | 9ec4575 | 2011-11-25 13:30:45 +0000 | [diff] [blame] | 635 | int |
| 636 | window_pane_index(struct window_pane *wp, u_int *i) |
Tiago Cunha | ae7dda1 | 2009-07-17 18:32:54 +0000 | [diff] [blame] | 637 | { |
| 638 | struct window_pane *wq; |
Tiago Cunha | 9ec4575 | 2011-11-25 13:30:45 +0000 | [diff] [blame] | 639 | struct window *w = wp->window; |
Tiago Cunha | ae7dda1 | 2009-07-17 18:32:54 +0000 | [diff] [blame] | 640 | |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 641 | *i = options_get_number(w->options, "pane-base-index"); |
Tiago Cunha | ae7dda1 | 2009-07-17 18:32:54 +0000 | [diff] [blame] | 642 | TAILQ_FOREACH(wq, &w->panes, entry) { |
Tiago Cunha | 9ec4575 | 2011-11-25 13:30:45 +0000 | [diff] [blame] | 643 | if (wp == wq) { |
| 644 | return (0); |
| 645 | } |
| 646 | (*i)++; |
Tiago Cunha | ae7dda1 | 2009-07-17 18:32:54 +0000 | [diff] [blame] | 647 | } |
Tiago Cunha | 9ec4575 | 2011-11-25 13:30:45 +0000 | [diff] [blame] | 648 | |
| 649 | return (-1); |
Tiago Cunha | ae7dda1 | 2009-07-17 18:32:54 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | u_int |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 653 | window_count_panes(struct window *w) |
| 654 | { |
| 655 | struct window_pane *wp; |
| 656 | u_int n; |
| 657 | |
| 658 | n = 0; |
| 659 | TAILQ_FOREACH(wp, &w->panes, entry) |
| 660 | n++; |
| 661 | return (n); |
| 662 | } |
| 663 | |
| 664 | void |
| 665 | window_destroy_panes(struct window *w) |
| 666 | { |
| 667 | struct window_pane *wp; |
| 668 | |
| 669 | while (!TAILQ_EMPTY(&w->panes)) { |
| 670 | wp = TAILQ_FIRST(&w->panes); |
| 671 | TAILQ_REMOVE(&w->panes, wp, entry); |
| 672 | window_pane_destroy(wp); |
| 673 | } |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 674 | } |
| 675 | |
nicm | 31b1ab4 | 2015-05-06 08:35:39 +0000 | [diff] [blame] | 676 | /* Retuns the printable flags on a window, empty string if no flags set. */ |
Tiago Cunha | 30f6d9b | 2011-01-07 16:55:40 +0000 | [diff] [blame] | 677 | char * |
| 678 | window_printable_flags(struct session *s, struct winlink *wl) |
| 679 | { |
nicm | 6f58757 | 2015-04-20 07:50:49 +0000 | [diff] [blame] | 680 | char flags[32]; |
Tiago Cunha | 30f6d9b | 2011-01-07 16:55:40 +0000 | [diff] [blame] | 681 | int pos; |
| 682 | |
| 683 | pos = 0; |
| 684 | if (wl->flags & WINLINK_ACTIVITY) |
| 685 | flags[pos++] = '#'; |
| 686 | if (wl->flags & WINLINK_BELL) |
| 687 | flags[pos++] = '!'; |
Tiago Cunha | 30f6d9b | 2011-01-07 16:55:40 +0000 | [diff] [blame] | 688 | if (wl->flags & WINLINK_SILENCE) |
| 689 | flags[pos++] = '~'; |
| 690 | if (wl == s->curw) |
| 691 | flags[pos++] = '*'; |
| 692 | if (wl == TAILQ_FIRST(&s->lastw)) |
| 693 | flags[pos++] = '-'; |
nicm | 12da13c | 2015-12-15 00:00:01 +0000 | [diff] [blame] | 694 | if (server_check_marked() && wl == marked_pane.wl) |
nicm | a863834 | 2015-06-04 11:43:51 +0000 | [diff] [blame] | 695 | flags[pos++] = 'M'; |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 696 | if (wl->window->flags & WINDOW_ZOOMED) |
| 697 | flags[pos++] = 'Z'; |
Tiago Cunha | 30f6d9b | 2011-01-07 16:55:40 +0000 | [diff] [blame] | 698 | flags[pos] = '\0'; |
| 699 | return (xstrdup(flags)); |
| 700 | } |
| 701 | |
nicm | 6dbd63b | 2015-04-25 18:09:28 +0000 | [diff] [blame] | 702 | struct window_pane * |
| 703 | window_pane_find_by_id_str(const char *s) |
| 704 | { |
| 705 | const char *errstr; |
| 706 | u_int id; |
| 707 | |
| 708 | if (*s != '%') |
| 709 | return (NULL); |
| 710 | |
| 711 | id = strtonum(s + 1, 0, UINT_MAX, &errstr); |
| 712 | if (errstr != NULL) |
| 713 | return (NULL); |
| 714 | return (window_pane_find_by_id(id)); |
| 715 | } |
| 716 | |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 717 | struct window_pane * |
| 718 | window_pane_find_by_id(u_int id) |
| 719 | { |
| 720 | struct window_pane wp; |
| 721 | |
| 722 | wp.id = id; |
| 723 | return (RB_FIND(window_pane_tree, &all_window_panes, &wp)); |
| 724 | } |
| 725 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 726 | struct window_pane * |
| 727 | window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) |
| 728 | { |
| 729 | struct window_pane *wp; |
nicm | d9b3133 | 2015-08-28 17:11:12 +0000 | [diff] [blame] | 730 | char host[HOST_NAME_MAX + 1]; |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 731 | |
Nicholas Marriott | b4ac8c1 | 2009-01-14 19:29:32 +0000 | [diff] [blame] | 732 | wp = xcalloc(1, sizeof *wp); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 733 | wp->window = w; |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 734 | |
Tiago Cunha | 2ee0d85 | 2012-01-31 12:01:43 +0000 | [diff] [blame] | 735 | wp->id = next_window_pane_id++; |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 736 | RB_INSERT(window_pane_tree, &all_window_panes, wp); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 737 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 738 | wp->argc = 0; |
| 739 | wp->argv = NULL; |
Tiago Cunha | a3a150f | 2009-09-02 01:02:44 +0000 | [diff] [blame] | 740 | wp->shell = NULL; |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 741 | wp->cwd = NULL; |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 742 | |
| 743 | wp->fd = -1; |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 744 | wp->event = NULL; |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 745 | |
| 746 | wp->mode = NULL; |
| 747 | |
Tiago Cunha | 545893d | 2009-07-20 15:42:05 +0000 | [diff] [blame] | 748 | wp->layout_cell = NULL; |
| 749 | |
Nicholas Marriott | b6450b1 | 2009-04-01 18:21:42 +0000 | [diff] [blame] | 750 | wp->xoff = 0; |
Tiago Cunha | cc094fd | 2009-12-04 22:14:47 +0000 | [diff] [blame] | 751 | wp->yoff = 0; |
Nicholas Marriott | b6450b1 | 2009-04-01 18:21:42 +0000 | [diff] [blame] | 752 | |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 753 | wp->sx = sx; |
| 754 | wp->sy = sy; |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 755 | |
Tiago Cunha | 6091b05 | 2009-10-12 00:35:08 +0000 | [diff] [blame] | 756 | wp->pipe_fd = -1; |
Tiago Cunha | 6091b05 | 2009-10-12 00:35:08 +0000 | [diff] [blame] | 757 | wp->pipe_off = 0; |
Tiago Cunha | cb0bf6a | 2009-11-08 22:59:53 +0000 | [diff] [blame] | 758 | wp->pipe_event = NULL; |
Tiago Cunha | 6091b05 | 2009-10-12 00:35:08 +0000 | [diff] [blame] | 759 | |
Nicholas Marriott | e63567d | 2009-07-14 06:40:33 +0000 | [diff] [blame] | 760 | wp->saved_grid = NULL; |
Nicholas Marriott | 3592859 | 2009-07-13 10:43:52 +0000 | [diff] [blame] | 761 | |
nicm | ee123c2 | 2015-04-19 21:05:27 +0000 | [diff] [blame] | 762 | memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc); |
Nicholas Marriott | e63567d | 2009-07-14 06:40:33 +0000 | [diff] [blame] | 763 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 764 | screen_init(&wp->base, sx, sy, hlimit); |
| 765 | wp->screen = &wp->base; |
Nicholas Marriott | 35876ea | 2009-06-01 22:58:49 +0000 | [diff] [blame] | 766 | |
nicm | d9b3133 | 2015-08-28 17:11:12 +0000 | [diff] [blame] | 767 | if (gethostname(host, sizeof host) == 0) |
| 768 | screen_set_title(&wp->base, host); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 769 | |
| 770 | input_init(wp); |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 771 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 772 | return (wp); |
| 773 | } |
| 774 | |
| 775 | void |
| 776 | window_pane_destroy(struct window_pane *wp) |
| 777 | { |
Tiago Cunha | 01052ca | 2010-08-29 14:46:13 +0000 | [diff] [blame] | 778 | window_pane_reset_mode(wp); |
| 779 | |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 780 | if (event_initialized(&wp->timer)) |
| 781 | evtimer_del(&wp->timer); |
Tiago Cunha | f9f6eea | 2012-03-29 21:05:16 +0000 | [diff] [blame] | 782 | |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 783 | if (wp->fd != -1) { |
Nicholas Marriott | 4273c1b | 2014-02-24 23:07:22 +0000 | [diff] [blame] | 784 | #ifdef HAVE_UTEMPTER |
| 785 | utempter_remove_record(wp->fd); |
| 786 | #endif |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 787 | bufferevent_free(wp->event); |
Tiago Cunha | e23df3a | 2012-01-29 12:53:33 +0000 | [diff] [blame] | 788 | close(wp->fd); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 789 | } |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 790 | |
| 791 | input_free(wp); |
| 792 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 793 | screen_free(&wp->base); |
Nicholas Marriott | e63567d | 2009-07-14 06:40:33 +0000 | [diff] [blame] | 794 | if (wp->saved_grid != NULL) |
| 795 | grid_destroy(wp->saved_grid); |
Tiago Cunha | 6091b05 | 2009-10-12 00:35:08 +0000 | [diff] [blame] | 796 | |
| 797 | if (wp->pipe_fd != -1) { |
Tiago Cunha | cb0bf6a | 2009-11-08 22:59:53 +0000 | [diff] [blame] | 798 | bufferevent_free(wp->pipe_event); |
Tiago Cunha | e23df3a | 2012-01-29 12:53:33 +0000 | [diff] [blame] | 799 | close(wp->pipe_fd); |
Tiago Cunha | 6091b05 | 2009-10-12 00:35:08 +0000 | [diff] [blame] | 800 | } |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 801 | |
Nicholas Marriott | 536fc24 | 2011-04-06 22:16:33 +0000 | [diff] [blame] | 802 | RB_REMOVE(window_pane_tree, &all_window_panes, wp); |
| 803 | |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 804 | free((void *)wp->cwd); |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 805 | free(wp->shell); |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 806 | cmd_free_argv(wp->argc, wp->argv); |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 807 | free(wp); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | int |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 811 | window_pane_spawn(struct window_pane *wp, int argc, char **argv, |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 812 | const char *path, const char *shell, const char *cwd, struct environ *env, |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 813 | struct termios *tio, char **cause) |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 814 | { |
Nicholas Marriott | 0fc6553 | 2010-04-06 21:59:19 +0000 | [diff] [blame] | 815 | struct winsize ws; |
nicm | 62d3af1 | 2015-11-24 23:46:15 +0000 | [diff] [blame] | 816 | char *argv0, *cmd, **argvp; |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 817 | const char *ptr, *first, *home; |
Nicholas Marriott | 0fc6553 | 2010-04-06 21:59:19 +0000 | [diff] [blame] | 818 | struct termios tio2; |
Nicholas Marriott | 4273c1b | 2014-02-24 23:07:22 +0000 | [diff] [blame] | 819 | #ifdef HAVE_UTEMPTER |
| 820 | char s[32]; |
| 821 | #endif |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 822 | int i; |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 823 | |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 824 | if (wp->fd != -1) { |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 825 | bufferevent_free(wp->event); |
Tiago Cunha | e23df3a | 2012-01-29 12:53:33 +0000 | [diff] [blame] | 826 | close(wp->fd); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 827 | } |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 828 | if (argc > 0) { |
| 829 | cmd_free_argv(wp->argc, wp->argv); |
| 830 | wp->argc = argc; |
| 831 | wp->argv = cmd_copy_argv(argc, argv); |
Tiago Cunha | a3a150f | 2009-09-02 01:02:44 +0000 | [diff] [blame] | 832 | } |
| 833 | if (shell != NULL) { |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 834 | free(wp->shell); |
Tiago Cunha | a3a150f | 2009-09-02 01:02:44 +0000 | [diff] [blame] | 835 | wp->shell = xstrdup(shell); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 836 | } |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 837 | if (cwd != NULL) { |
| 838 | free((void *)wp->cwd); |
| 839 | wp->cwd = xstrdup(cwd); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 840 | } |
| 841 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 842 | cmd = cmd_stringify_argv(wp->argc, wp->argv); |
| 843 | log_debug("spawn: %s -- %s", wp->shell, cmd); |
| 844 | for (i = 0; i < wp->argc; i++) |
| 845 | log_debug("spawn: argv[%d] = %s", i, wp->argv[i]); |
Nicholas Marriott | 31407b7 | 2013-02-22 14:31:38 +0000 | [diff] [blame] | 846 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 847 | memset(&ws, 0, sizeof ws); |
| 848 | ws.ws_col = screen_size_x(&wp->base); |
| 849 | ws.ws_row = screen_size_y(&wp->base); |
Nicholas Marriott | 2d15f59 | 2009-01-20 19:35:03 +0000 | [diff] [blame] | 850 | |
Tiago Cunha | cc094fd | 2009-12-04 22:14:47 +0000 | [diff] [blame] | 851 | switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) { |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 852 | case -1: |
Nicholas Marriott | 4d9af27 | 2009-01-23 16:59:14 +0000 | [diff] [blame] | 853 | wp->fd = -1; |
| 854 | xasprintf(cause, "%s: %s", cmd, strerror(errno)); |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 855 | free(cmd); |
Nicholas Marriott | 4d9af27 | 2009-01-23 16:59:14 +0000 | [diff] [blame] | 856 | return (-1); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 857 | case 0: |
nicm | 01defc9 | 2015-10-31 08:13:58 +0000 | [diff] [blame] | 858 | if (chdir(wp->cwd) != 0) { |
| 859 | if ((home = find_home()) == NULL || chdir(home) != 0) |
| 860 | chdir("/"); |
| 861 | } |
Tiago Cunha | 29b1b2f | 2009-08-09 17:48:55 +0000 | [diff] [blame] | 862 | |
Nicholas Marriott | 15b643f | 2009-09-16 12:36:28 +0000 | [diff] [blame] | 863 | if (tcgetattr(STDIN_FILENO, &tio2) != 0) |
| 864 | fatal("tcgetattr failed"); |
| 865 | if (tio != NULL) |
| 866 | memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc); |
| 867 | tio2.c_cc[VERASE] = '\177'; |
Nicholas Marriott | db7570d | 2012-01-20 20:18:20 +0000 | [diff] [blame] | 868 | #ifdef IUTF8 |
Nicholas Marriott | b7397bf | 2015-11-13 16:05:58 +0000 | [diff] [blame] | 869 | tio2.c_iflag |= IUTF8; |
Nicholas Marriott | db7570d | 2012-01-20 20:18:20 +0000 | [diff] [blame] | 870 | #endif |
Nicholas Marriott | 15b643f | 2009-09-16 12:36:28 +0000 | [diff] [blame] | 871 | if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0) |
| 872 | fatal("tcgetattr failed"); |
| 873 | |
Tiago Cunha | d4b58c7 | 2010-10-24 00:45:57 +0000 | [diff] [blame] | 874 | closefrom(STDERR_FILENO + 1); |
| 875 | |
nicm | 3e27be3 | 2014-04-17 13:02:59 +0000 | [diff] [blame] | 876 | if (path != NULL) |
nicm | 62d3af1 | 2015-11-24 23:46:15 +0000 | [diff] [blame] | 877 | environ_set(env, "PATH", "%s", path); |
| 878 | environ_set(env, "TMUX_PANE", "%%%u", wp->id); |
Nicholas Marriott | 0fc6553 | 2010-04-06 21:59:19 +0000 | [diff] [blame] | 879 | environ_push(env); |
Tiago Cunha | 29b1b2f | 2009-08-09 17:48:55 +0000 | [diff] [blame] | 880 | |
Tiago Cunha | 56040be | 2010-08-29 14:42:11 +0000 | [diff] [blame] | 881 | clear_signals(1); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 882 | log_close(); |
| 883 | |
Tiago Cunha | 2e3a260 | 2012-05-30 13:47:33 +0000 | [diff] [blame] | 884 | setenv("SHELL", wp->shell, 1); |
| 885 | ptr = strrchr(wp->shell, '/'); |
Tiago Cunha | 5838ee1 | 2009-09-02 01:08:32 +0000 | [diff] [blame] | 886 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 887 | /* |
| 888 | * If given one argument, assume it should be passed to sh -c; |
| 889 | * with more than one argument, use execvp(). If there is no |
| 890 | * arguments, create a login shell. |
| 891 | */ |
| 892 | if (wp->argc > 0) { |
| 893 | if (wp->argc != 1) { |
| 894 | /* Copy to ensure argv ends in NULL. */ |
| 895 | argvp = cmd_copy_argv(wp->argc, wp->argv); |
| 896 | execvp(argvp[0], argvp); |
| 897 | fatal("execvp failed"); |
| 898 | } |
| 899 | first = wp->argv[0]; |
| 900 | |
Tiago Cunha | 2e3a260 | 2012-05-30 13:47:33 +0000 | [diff] [blame] | 901 | if (ptr != NULL && *(ptr + 1) != '\0') |
| 902 | xasprintf(&argv0, "%s", ptr + 1); |
| 903 | else |
| 904 | xasprintf(&argv0, "%s", wp->shell); |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 905 | execl(wp->shell, argv0, "-c", first, (char *)NULL); |
Nicholas Marriott | 22d1b94 | 2009-07-01 19:42:55 +0000 | [diff] [blame] | 906 | fatal("execl failed"); |
| 907 | } |
Tiago Cunha | a3a150f | 2009-09-02 01:02:44 +0000 | [diff] [blame] | 908 | if (ptr != NULL && *(ptr + 1) != '\0') |
Nicholas Marriott | 22d1b94 | 2009-07-01 19:42:55 +0000 | [diff] [blame] | 909 | xasprintf(&argv0, "-%s", ptr + 1); |
| 910 | else |
Tiago Cunha | a3a150f | 2009-09-02 01:02:44 +0000 | [diff] [blame] | 911 | xasprintf(&argv0, "-%s", wp->shell); |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 912 | execl(wp->shell, argv0, (char *)NULL); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 913 | fatal("execl failed"); |
| 914 | } |
| 915 | |
Nicholas Marriott | 4273c1b | 2014-02-24 23:07:22 +0000 | [diff] [blame] | 916 | #ifdef HAVE_UTEMPTER |
Nicholas Marriott | f315207 | 2014-02-24 23:11:25 +0000 | [diff] [blame] | 917 | xsnprintf(s, sizeof s, "tmux(%lu).%%%u", (long) getpid(), wp->id); |
Nicholas Marriott | 4273c1b | 2014-02-24 23:07:22 +0000 | [diff] [blame] | 918 | utempter_add_record(wp->fd, s); |
Nicholas Marriott | 782dd94 | 2016-02-17 23:21:58 +0000 | [diff] [blame] | 919 | kill(getpid(), SIGCHLD); |
Nicholas Marriott | 4273c1b | 2014-02-24 23:07:22 +0000 | [diff] [blame] | 920 | #endif |
| 921 | |
Tiago Cunha | 492e3aa | 2011-01-21 23:44:13 +0000 | [diff] [blame] | 922 | setblocking(wp->fd, 0); |
| 923 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 924 | wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL, |
| 925 | window_pane_error_callback, wp); |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 926 | |
| 927 | bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 928 | bufferevent_enable(wp->event, EV_READ|EV_WRITE); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 929 | |
nicm | b3e8d44 | 2014-05-13 08:08:32 +0000 | [diff] [blame] | 930 | free(cmd); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 931 | return (0); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Tiago Cunha | f9f6eea | 2012-03-29 21:05:16 +0000 | [diff] [blame] | 934 | void |
nicm | 577c0e3 | 2015-11-18 14:27:44 +0000 | [diff] [blame] | 935 | window_pane_timer_callback(__unused int fd, __unused short events, void *data) |
Tiago Cunha | f9f6eea | 2012-03-29 21:05:16 +0000 | [diff] [blame] | 936 | { |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 937 | window_pane_read_callback(NULL, data); |
Tiago Cunha | f9f6eea | 2012-03-29 21:05:16 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 940 | void |
nicm | 577c0e3 | 2015-11-18 14:27:44 +0000 | [diff] [blame] | 941 | window_pane_read_callback(__unused struct bufferevent *bufev, void *data) |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 942 | { |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 943 | struct window_pane *wp = data; |
| 944 | struct evbuffer *evb = wp->event->input; |
| 945 | char *new_data; |
| 946 | size_t new_size, available; |
| 947 | struct client *c; |
| 948 | struct timeval tv; |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 949 | |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 950 | if (event_initialized(&wp->timer)) |
| 951 | evtimer_del(&wp->timer); |
| 952 | |
| 953 | log_debug("%%%u has %zu bytes", wp->id, EVBUFFER_LENGTH(evb)); |
| 954 | |
| 955 | TAILQ_FOREACH(c, &clients, entry) { |
| 956 | if (!tty_client_ready(c, wp)) |
| 957 | continue; |
| 958 | |
| 959 | available = EVBUFFER_LENGTH(c->tty.event->output); |
| 960 | if (available > READ_BACKOFF) |
| 961 | goto start_timer; |
| 962 | } |
| 963 | |
| 964 | new_size = EVBUFFER_LENGTH(evb) - wp->pipe_off; |
Nicholas Marriott | 091db41 | 2010-04-06 21:58:33 +0000 | [diff] [blame] | 965 | if (wp->pipe_fd != -1 && new_size > 0) { |
nicm | f84d32c | 2015-12-31 18:14:13 +0000 | [diff] [blame] | 966 | new_data = EVBUFFER_DATA(evb) + wp->pipe_off; |
Nicholas Marriott | 091db41 | 2010-04-06 21:58:33 +0000 | [diff] [blame] | 967 | bufferevent_write(wp->pipe_event, new_data, new_size); |
| 968 | } |
| 969 | |
| 970 | input_parse(wp); |
| 971 | |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 972 | wp->pipe_off = EVBUFFER_LENGTH(evb); |
nicm | 3f4ee98 | 2015-05-12 22:40:38 +0000 | [diff] [blame] | 973 | return; |
| 974 | |
| 975 | start_timer: |
| 976 | log_debug("%%%u backing off (%s %zu > %d)", wp->id, c->ttyname, |
| 977 | available, READ_BACKOFF); |
| 978 | |
| 979 | tv.tv_sec = 0; |
| 980 | tv.tv_usec = READ_TIME; |
| 981 | |
| 982 | evtimer_set(&wp->timer, window_pane_timer_callback, wp); |
| 983 | evtimer_add(&wp->timer, &tv); |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | void |
nicm | 577c0e3 | 2015-11-18 14:27:44 +0000 | [diff] [blame] | 987 | window_pane_error_callback(__unused struct bufferevent *bufev, |
| 988 | __unused short what, void *data) |
Tiago Cunha | 2df0882 | 2009-11-08 23:02:56 +0000 | [diff] [blame] | 989 | { |
| 990 | struct window_pane *wp = data; |
| 991 | |
nicm | 021c643 | 2015-12-16 21:50:37 +0000 | [diff] [blame] | 992 | server_destroy_pane(wp, 1); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Tiago Cunha | d29b57f | 2009-07-22 17:46:53 +0000 | [diff] [blame] | 995 | void |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 996 | window_pane_resize(struct window_pane *wp, u_int sx, u_int sy) |
| 997 | { |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 998 | if (sx == wp->sx && sy == wp->sy) |
Tiago Cunha | d29b57f | 2009-07-22 17:46:53 +0000 | [diff] [blame] | 999 | return; |
Nicholas Marriott | 7cd3cf0 | 2009-01-12 18:22:47 +0000 | [diff] [blame] | 1000 | wp->sx = sx; |
| 1001 | wp->sy = sy; |
Nicholas Marriott | 103748d | 2007-12-06 09:46:23 +0000 | [diff] [blame] | 1002 | |
Nicholas Marriott | 8903c1f | 2013-02-05 11:08:59 +0000 | [diff] [blame] | 1003 | screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1004 | if (wp->mode != NULL) |
| 1005 | wp->mode->resize(wp, sx, sy); |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 1006 | |
Nicholas Marriott | dbd8e47 | 2013-02-22 21:35:29 +0000 | [diff] [blame] | 1007 | wp->flags |= PANE_RESIZE; |
Nicholas Marriott | a41ece5 | 2007-07-09 19:04:12 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | * Enter alternative screen mode. A copy of the visible screen is saved and the |
| 1012 | * history is not updated |
| 1013 | */ |
| 1014 | void |
Nicholas Marriott | 24d7d07 | 2012-11-27 20:08:42 +0000 | [diff] [blame] | 1015 | window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc, |
| 1016 | int cursor) |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1017 | { |
| 1018 | struct screen *s = &wp->base; |
| 1019 | u_int sx, sy; |
| 1020 | |
| 1021 | if (wp->saved_grid != NULL) |
| 1022 | return; |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 1023 | if (!options_get_number(wp->window->options, "alternate-screen")) |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1024 | return; |
| 1025 | sx = screen_size_x(s); |
| 1026 | sy = screen_size_y(s); |
| 1027 | |
| 1028 | wp->saved_grid = grid_create(sx, sy, 0); |
| 1029 | grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy); |
Nicholas Marriott | 24d7d07 | 2012-11-27 20:08:42 +0000 | [diff] [blame] | 1030 | if (cursor) { |
| 1031 | wp->saved_cx = s->cx; |
| 1032 | wp->saved_cy = s->cy; |
| 1033 | } |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1034 | memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell); |
| 1035 | |
| 1036 | grid_view_clear(s->grid, 0, 0, sx, sy); |
| 1037 | |
| 1038 | wp->base.grid->flags &= ~GRID_HISTORY; |
| 1039 | |
| 1040 | wp->flags |= PANE_REDRAW; |
| 1041 | } |
| 1042 | |
| 1043 | /* Exit alternate screen mode and restore the copied grid. */ |
| 1044 | void |
Nicholas Marriott | 24d7d07 | 2012-11-27 20:08:42 +0000 | [diff] [blame] | 1045 | window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc, |
| 1046 | int cursor) |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1047 | { |
| 1048 | struct screen *s = &wp->base; |
| 1049 | u_int sx, sy; |
| 1050 | |
| 1051 | if (wp->saved_grid == NULL) |
| 1052 | return; |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 1053 | if (!options_get_number(wp->window->options, "alternate-screen")) |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1054 | return; |
| 1055 | sx = screen_size_x(s); |
| 1056 | sy = screen_size_y(s); |
| 1057 | |
| 1058 | /* |
| 1059 | * If the current size is bigger, temporarily resize to the old size |
| 1060 | * before copying back. |
| 1061 | */ |
| 1062 | if (sy > wp->saved_grid->sy) |
Nicholas Marriott | 8903c1f | 2013-02-05 11:08:59 +0000 | [diff] [blame] | 1063 | screen_resize(s, sx, wp->saved_grid->sy, 1); |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* Restore the grid, cursor position and cell. */ |
| 1066 | grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy); |
Nicholas Marriott | 24d7d07 | 2012-11-27 20:08:42 +0000 | [diff] [blame] | 1067 | if (cursor) |
| 1068 | s->cx = wp->saved_cx; |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1069 | if (s->cx > screen_size_x(s) - 1) |
| 1070 | s->cx = screen_size_x(s) - 1; |
Nicholas Marriott | 24d7d07 | 2012-11-27 20:08:42 +0000 | [diff] [blame] | 1071 | if (cursor) |
| 1072 | s->cy = wp->saved_cy; |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1073 | if (s->cy > screen_size_y(s) - 1) |
| 1074 | s->cy = screen_size_y(s) - 1; |
| 1075 | memcpy(gc, &wp->saved_cell, sizeof *gc); |
| 1076 | |
| 1077 | /* |
| 1078 | * Turn history back on (so resize can use it) and then resize back to |
| 1079 | * the current size. |
| 1080 | */ |
| 1081 | wp->base.grid->flags |= GRID_HISTORY; |
Nicholas Marriott | 8903c1f | 2013-02-05 11:08:59 +0000 | [diff] [blame] | 1082 | if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx) |
| 1083 | screen_resize(s, sx, sy, 1); |
Nicholas Marriott | 9f5b9ba | 2010-03-15 12:51:23 +0000 | [diff] [blame] | 1084 | |
| 1085 | grid_destroy(wp->saved_grid); |
| 1086 | wp->saved_grid = NULL; |
| 1087 | |
| 1088 | wp->flags |= PANE_REDRAW; |
| 1089 | } |
| 1090 | |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1091 | int |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1092 | window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode) |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1093 | { |
| 1094 | struct screen *s; |
| 1095 | |
Tiago Cunha | d29b57f | 2009-07-22 17:46:53 +0000 | [diff] [blame] | 1096 | if (wp->mode != NULL) |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1097 | return (1); |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1098 | wp->mode = mode; |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1099 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1100 | if ((s = wp->mode->init(wp)) != NULL) |
| 1101 | wp->screen = s; |
nicm | 5267ce8 | 2015-08-29 00:39:18 +0000 | [diff] [blame] | 1102 | wp->flags |= (PANE_REDRAW|PANE_CHANGED); |
nicm | 2e4503a | 2016-03-01 12:05:15 +0000 | [diff] [blame] | 1103 | |
| 1104 | server_status_window(wp->window); |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1105 | return (0); |
| 1106 | } |
| 1107 | |
| 1108 | void |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1109 | window_pane_reset_mode(struct window_pane *wp) |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1110 | { |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1111 | if (wp->mode == NULL) |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1112 | return; |
| 1113 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1114 | wp->mode->free(wp); |
| 1115 | wp->mode = NULL; |
Nicholas Marriott | 7dc18f6 | 2007-12-06 10:04:43 +0000 | [diff] [blame] | 1116 | |
Nicholas Marriott | 162bacd | 2009-01-11 23:31:46 +0000 | [diff] [blame] | 1117 | wp->screen = &wp->base; |
nicm | 5267ce8 | 2015-08-29 00:39:18 +0000 | [diff] [blame] | 1118 | wp->flags |= (PANE_REDRAW|PANE_CHANGED); |
nicm | 2e4503a | 2016-03-01 12:05:15 +0000 | [diff] [blame] | 1119 | |
| 1120 | server_status_window(wp->window); |
Nicholas Marriott | 9a6e47c | 2007-11-21 13:11:41 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 1124 | window_pane_key(struct window_pane *wp, struct client *c, struct session *s, |
nicm | 69e0b83 | 2015-11-12 11:05:34 +0000 | [diff] [blame] | 1125 | key_code key, struct mouse_event *m) |
Nicholas Marriott | 9a6e47c | 2007-11-21 13:11:41 +0000 | [diff] [blame] | 1126 | { |
Tiago Cunha | 9ac062a | 2009-10-09 13:07:04 +0000 | [diff] [blame] | 1127 | struct window_pane *wp2; |
| 1128 | |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 1129 | if (KEYC_IS_MOUSE(key) && m == NULL) |
| 1130 | return; |
| 1131 | |
Nicholas Marriott | 4428987 | 2009-01-28 19:52:21 +0000 | [diff] [blame] | 1132 | if (wp->mode != NULL) { |
| 1133 | if (wp->mode->key != NULL) |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 1134 | wp->mode->key(wp, c, s, key, m); |
Tiago Cunha | 9ac062a | 2009-10-09 13:07:04 +0000 | [diff] [blame] | 1135 | return; |
Tiago Cunha | cf9804f | 2009-10-12 00:04:56 +0000 | [diff] [blame] | 1136 | } |
Tiago Cunha | 9ac062a | 2009-10-09 13:07:04 +0000 | [diff] [blame] | 1137 | |
nicm | f518a07 | 2014-08-11 22:14:30 +0000 | [diff] [blame] | 1138 | if (wp->fd == -1 || wp->flags & PANE_INPUTOFF) |
Tiago Cunha | cf9804f | 2009-10-12 00:04:56 +0000 | [diff] [blame] | 1139 | return; |
nicm | f518a07 | 2014-08-11 22:14:30 +0000 | [diff] [blame] | 1140 | |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 1141 | input_key(wp, key, m); |
| 1142 | |
| 1143 | if (KEYC_IS_MOUSE(key)) |
| 1144 | return; |
nicm | 44657bf | 2015-10-27 15:58:42 +0000 | [diff] [blame] | 1145 | if (options_get_number(wp->window->options, "synchronize-panes")) { |
Tiago Cunha | 9ac062a | 2009-10-09 13:07:04 +0000 | [diff] [blame] | 1146 | TAILQ_FOREACH(wp2, &wp->window->panes, entry) { |
| 1147 | if (wp2 == wp || wp2->mode != NULL) |
| 1148 | continue; |
nicm | bdbec09 | 2015-12-19 08:43:04 +0000 | [diff] [blame] | 1149 | if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF) |
| 1150 | continue; |
| 1151 | if (window_pane_visible(wp2)) |
nicm | bf635e7 | 2015-04-19 21:34:21 +0000 | [diff] [blame] | 1152 | input_key(wp2, key, NULL); |
Tiago Cunha | 9ac062a | 2009-10-09 13:07:04 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
Nicholas Marriott | 4428987 | 2009-01-28 19:52:21 +0000 | [diff] [blame] | 1155 | } |
Nicholas Marriott | a9e3d5c | 2009-06-25 16:47:00 +0000 | [diff] [blame] | 1156 | |
Nicholas Marriott | 1e574bb | 2009-07-15 17:42:44 +0000 | [diff] [blame] | 1157 | int |
| 1158 | window_pane_visible(struct window_pane *wp) |
| 1159 | { |
| 1160 | struct window *w = wp->window; |
| 1161 | |
Nicholas Marriott | c5239c5 | 2013-02-24 00:25:03 +0000 | [diff] [blame] | 1162 | if (wp->layout_cell == NULL) |
| 1163 | return (0); |
Nicholas Marriott | 1e574bb | 2009-07-15 17:42:44 +0000 | [diff] [blame] | 1164 | if (wp->xoff >= w->sx || wp->yoff >= w->sy) |
| 1165 | return (0); |
| 1166 | if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy) |
| 1167 | return (0); |
| 1168 | return (1); |
| 1169 | } |
| 1170 | |
Tiago Cunha | 80af85a | 2009-05-19 13:32:55 +0000 | [diff] [blame] | 1171 | char * |
nicm | a5d4b7f | 2014-04-17 14:45:49 +0000 | [diff] [blame] | 1172 | window_pane_search(struct window_pane *wp, const char *searchstr, |
| 1173 | u_int *lineno) |
Tiago Cunha | 80af85a | 2009-05-19 13:32:55 +0000 | [diff] [blame] | 1174 | { |
Nicholas Marriott | 853ad68 | 2009-06-25 16:02:37 +0000 | [diff] [blame] | 1175 | struct screen *s = &wp->base; |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 1176 | char *newsearchstr, *line, *msg; |
Nicholas Marriott | 853ad68 | 2009-06-25 16:02:37 +0000 | [diff] [blame] | 1177 | u_int i; |
Tiago Cunha | 80af85a | 2009-05-19 13:32:55 +0000 | [diff] [blame] | 1178 | |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 1179 | msg = NULL; |
| 1180 | xasprintf(&newsearchstr, "*%s*", searchstr); |
| 1181 | |
Nicholas Marriott | 853ad68 | 2009-06-25 16:02:37 +0000 | [diff] [blame] | 1182 | for (i = 0; i < screen_size_y(s); i++) { |
| 1183 | line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s)); |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 1184 | if (fnmatch(newsearchstr, line, 0) == 0) { |
| 1185 | msg = line; |
| 1186 | if (lineno != NULL) |
| 1187 | *lineno = i; |
| 1188 | break; |
| 1189 | } |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 1190 | free(line); |
Tiago Cunha | 80af85a | 2009-05-19 13:32:55 +0000 | [diff] [blame] | 1191 | } |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 1192 | |
Tiago Cunha | a432fcd | 2012-07-11 19:34:16 +0000 | [diff] [blame] | 1193 | free(newsearchstr); |
Nicholas Marriott | f7a9eb4 | 2009-06-25 16:04:24 +0000 | [diff] [blame] | 1194 | return (msg); |
Tiago Cunha | 80af85a | 2009-05-19 13:32:55 +0000 | [diff] [blame] | 1195 | } |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1196 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1197 | /* Get MRU pane from a list. */ |
| 1198 | struct window_pane * |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1199 | window_pane_choose_best(struct window_pane **list, u_int size) |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1200 | { |
| 1201 | struct window_pane *next, *best; |
| 1202 | u_int i; |
| 1203 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1204 | if (size == 0) |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1205 | return (NULL); |
| 1206 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1207 | best = list[0]; |
| 1208 | for (i = 1; i < size; i++) { |
| 1209 | next = list[i]; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1210 | if (next->active_point > best->active_point) |
| 1211 | best = next; |
| 1212 | } |
| 1213 | return (best); |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Find the pane directly above another. We build a list of those adjacent to |
| 1218 | * top edge and then choose the best. |
| 1219 | */ |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1220 | struct window_pane * |
| 1221 | window_pane_find_up(struct window_pane *wp) |
| 1222 | { |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1223 | struct window_pane *next, *best, **list; |
| 1224 | u_int edge, left, right, end, size; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1225 | int found; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1226 | |
| 1227 | if (wp == NULL || !window_pane_visible(wp)) |
| 1228 | return (NULL); |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1229 | |
| 1230 | list = NULL; |
| 1231 | size = 0; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1232 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1233 | edge = wp->yoff; |
| 1234 | if (edge == 0) |
| 1235 | edge = wp->window->sy + 1; |
| 1236 | |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1237 | left = wp->xoff; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1238 | right = wp->xoff + wp->sx; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1239 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1240 | TAILQ_FOREACH(next, &wp->window->panes, entry) { |
| 1241 | if (next == wp || !window_pane_visible(next)) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1242 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1243 | if (next->yoff + next->sy + 1 != edge) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1244 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1245 | end = next->xoff + next->sx - 1; |
| 1246 | |
| 1247 | found = 0; |
| 1248 | if (next->xoff < left && end > right) |
| 1249 | found = 1; |
| 1250 | else if (next->xoff >= left && next->xoff <= right) |
| 1251 | found = 1; |
| 1252 | else if (end >= left && end <= right) |
| 1253 | found = 1; |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1254 | if (!found) |
| 1255 | continue; |
| 1256 | list = xreallocarray(list, size + 1, sizeof *list); |
| 1257 | list[size++] = next; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1258 | } |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1259 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1260 | best = window_pane_choose_best(list, size); |
| 1261 | free(list); |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1262 | return (best); |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /* Find the pane directly below another. */ |
| 1266 | struct window_pane * |
| 1267 | window_pane_find_down(struct window_pane *wp) |
| 1268 | { |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1269 | struct window_pane *next, *best, **list; |
| 1270 | u_int edge, left, right, end, size; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1271 | int found; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1272 | |
| 1273 | if (wp == NULL || !window_pane_visible(wp)) |
| 1274 | return (NULL); |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1275 | |
| 1276 | list = NULL; |
| 1277 | size = 0; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1278 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1279 | edge = wp->yoff + wp->sy + 1; |
| 1280 | if (edge >= wp->window->sy) |
| 1281 | edge = 0; |
| 1282 | |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1283 | left = wp->xoff; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1284 | right = wp->xoff + wp->sx; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1285 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1286 | TAILQ_FOREACH(next, &wp->window->panes, entry) { |
| 1287 | if (next == wp || !window_pane_visible(next)) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1288 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1289 | if (next->yoff != edge) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1290 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1291 | end = next->xoff + next->sx - 1; |
| 1292 | |
| 1293 | found = 0; |
| 1294 | if (next->xoff < left && end > right) |
| 1295 | found = 1; |
| 1296 | else if (next->xoff >= left && next->xoff <= right) |
| 1297 | found = 1; |
| 1298 | else if (end >= left && end <= right) |
| 1299 | found = 1; |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1300 | if (!found) |
| 1301 | continue; |
| 1302 | list = xreallocarray(list, size + 1, sizeof *list); |
| 1303 | list[size++] = next; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1304 | } |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1305 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1306 | best = window_pane_choose_best(list, size); |
| 1307 | free(list); |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1308 | return (best); |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1311 | /* Find the pane directly to the left of another. */ |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1312 | struct window_pane * |
| 1313 | window_pane_find_left(struct window_pane *wp) |
| 1314 | { |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1315 | struct window_pane *next, *best, **list; |
| 1316 | u_int edge, top, bottom, end, size; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1317 | int found; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1318 | |
| 1319 | if (wp == NULL || !window_pane_visible(wp)) |
| 1320 | return (NULL); |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1321 | |
| 1322 | list = NULL; |
| 1323 | size = 0; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1324 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1325 | edge = wp->xoff; |
| 1326 | if (edge == 0) |
| 1327 | edge = wp->window->sx + 1; |
| 1328 | |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1329 | top = wp->yoff; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1330 | bottom = wp->yoff + wp->sy; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1331 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1332 | TAILQ_FOREACH(next, &wp->window->panes, entry) { |
| 1333 | if (next == wp || !window_pane_visible(next)) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1334 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1335 | if (next->xoff + next->sx + 1 != edge) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1336 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1337 | end = next->yoff + next->sy - 1; |
| 1338 | |
| 1339 | found = 0; |
| 1340 | if (next->yoff < top && end > bottom) |
| 1341 | found = 1; |
| 1342 | else if (next->yoff >= top && next->yoff <= bottom) |
| 1343 | found = 1; |
| 1344 | else if (end >= top && end <= bottom) |
| 1345 | found = 1; |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1346 | if (!found) |
| 1347 | continue; |
| 1348 | list = xreallocarray(list, size + 1, sizeof *list); |
| 1349 | list[size++] = next; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1350 | } |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1351 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1352 | best = window_pane_choose_best(list, size); |
| 1353 | free(list); |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1354 | return (best); |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1357 | /* Find the pane directly to the right of another. */ |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1358 | struct window_pane * |
| 1359 | window_pane_find_right(struct window_pane *wp) |
| 1360 | { |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1361 | struct window_pane *next, *best, **list; |
| 1362 | u_int edge, top, bottom, end, size; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1363 | int found; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (wp == NULL || !window_pane_visible(wp)) |
| 1366 | return (NULL); |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1367 | |
| 1368 | list = NULL; |
| 1369 | size = 0; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1370 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1371 | edge = wp->xoff + wp->sx + 1; |
| 1372 | if (edge >= wp->window->sx) |
| 1373 | edge = 0; |
| 1374 | |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1375 | top = wp->yoff; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1376 | bottom = wp->yoff + wp->sy; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1377 | |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1378 | TAILQ_FOREACH(next, &wp->window->panes, entry) { |
| 1379 | if (next == wp || !window_pane_visible(next)) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1380 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1381 | if (next->xoff != edge) |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1382 | continue; |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1383 | end = next->yoff + next->sy - 1; |
| 1384 | |
| 1385 | found = 0; |
| 1386 | if (next->yoff < top && end > bottom) |
| 1387 | found = 1; |
| 1388 | else if (next->yoff >= top && next->yoff <= bottom) |
| 1389 | found = 1; |
| 1390 | else if (end >= top && end <= bottom) |
| 1391 | found = 1; |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1392 | if (!found) |
| 1393 | continue; |
| 1394 | list = xreallocarray(list, size + 1, sizeof *list); |
| 1395 | list[size++] = next; |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1396 | } |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1397 | |
nicm | 05e7fbd | 2015-04-25 18:56:05 +0000 | [diff] [blame] | 1398 | best = window_pane_choose_best(list, size); |
| 1399 | free(list); |
nicm | 540f0b3 | 2014-05-08 06:03:30 +0000 | [diff] [blame] | 1400 | return (best); |
Nicholas Marriott | aa8f901 | 2010-03-15 22:03:38 +0000 | [diff] [blame] | 1401 | } |
Tiago Cunha | 38530de | 2012-07-11 17:06:11 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* Clear alert flags for a winlink */ |
| 1404 | void |
| 1405 | winlink_clear_flags(struct winlink *wl) |
| 1406 | { |
Tiago Cunha | 38530de | 2012-07-11 17:06:11 +0000 | [diff] [blame] | 1407 | struct session *s; |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 1408 | struct winlink *wl_loop; |
Tiago Cunha | 38530de | 2012-07-11 17:06:11 +0000 | [diff] [blame] | 1409 | |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 1410 | RB_FOREACH(s, sessions, &sessions) { |
| 1411 | RB_FOREACH(wl_loop, winlinks, &s->windows) { |
| 1412 | if (wl_loop->window != wl->window) |
| 1413 | continue; |
| 1414 | if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0) |
Tiago Cunha | 38530de | 2012-07-11 17:06:11 +0000 | [diff] [blame] | 1415 | continue; |
| 1416 | |
nicm | 8d66f4f | 2015-04-22 15:30:11 +0000 | [diff] [blame] | 1417 | wl_loop->flags &= ~WINLINK_ALERTFLAGS; |
| 1418 | wl_loop->window->flags &= ~WINDOW_ALERTFLAGS; |
Tiago Cunha | 38530de | 2012-07-11 17:06:11 +0000 | [diff] [blame] | 1419 | server_status_session(s); |
| 1420 | } |
| 1421 | } |
Tiago Cunha | 200b0e5 | 2012-08-12 19:28:20 +0000 | [diff] [blame] | 1422 | } |
nicm | 0ff3359 | 2015-06-17 16:50:28 +0000 | [diff] [blame] | 1423 | |
| 1424 | int |
| 1425 | winlink_shuffle_up(struct session *s, struct winlink *wl) |
| 1426 | { |
| 1427 | int idx, last; |
| 1428 | |
| 1429 | idx = wl->idx + 1; |
| 1430 | |
| 1431 | /* Find the next free index. */ |
| 1432 | for (last = idx; last < INT_MAX; last++) { |
| 1433 | if (winlink_find_by_index(&s->windows, last) == NULL) |
| 1434 | break; |
| 1435 | } |
| 1436 | if (last == INT_MAX) |
| 1437 | return (-1); |
| 1438 | |
| 1439 | /* Move everything from last - 1 to idx up a bit. */ |
| 1440 | for (; last > idx; last--) { |
| 1441 | wl = winlink_find_by_index(&s->windows, last - 1); |
| 1442 | server_link_window(s, wl, s, last, 0, 0, NULL); |
| 1443 | server_unlink_window(s, wl); |
| 1444 | } |
| 1445 | |
| 1446 | return (idx); |
| 1447 | } |