Line | Count | Source |
1 | | /* $OpenBSD: window.c,v 1.369 2026/07/29 14:06:32 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> |
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> |
20 | | #include <sys/ioctl.h> |
21 | | #include <sys/wait.h> |
22 | | |
23 | | #include <ctype.h> |
24 | | #include <errno.h> |
25 | | #include <fcntl.h> |
26 | | #include <fnmatch.h> |
27 | | #include <regex.h> |
28 | | #include <signal.h> |
29 | | #include <stdint.h> |
30 | | #include <stdlib.h> |
31 | | #include <string.h> |
32 | | #include <time.h> |
33 | | #include <unistd.h> |
34 | | |
35 | | #include "tmux.h" |
36 | | |
37 | | /* |
38 | | * Each window is attached to a number of panes, each of which is a pty. This |
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 | | */ |
55 | | |
56 | | /* Global window list. */ |
57 | | struct windows windows; |
58 | | |
59 | | /* Global panes tree. */ |
60 | | struct window_pane_tree all_window_panes; |
61 | | static u_int next_window_pane_id; |
62 | | static u_int next_window_id; |
63 | | static u_int next_active_point; |
64 | | |
65 | | struct window_pane_input_data { |
66 | | struct cmdq_item *item; |
67 | | u_int wp; |
68 | | struct client_file *file; |
69 | | }; |
70 | | |
71 | | static struct window_pane *window_pane_create(struct window *, u_int, u_int, |
72 | | u_int); |
73 | | static void window_pane_destroy(struct window_pane *); |
74 | | static void window_pane_free(struct window_pane *); |
75 | | static void window_pane_scrollbar_timer(int, short, void *); |
76 | | static void window_pane_full_size_offset(struct window_pane *, int *, int *, |
77 | | u_int *, u_int *); |
78 | | |
79 | 0 | RB_GENERATE(windows, window, entry, window_cmp); Unexecuted instantiation: windows_RB_REMOVE_COLOR Unexecuted instantiation: windows_RB_REMOVE Unexecuted instantiation: windows_RB_INSERT Unexecuted instantiation: windows_RB_FIND Unexecuted instantiation: windows_RB_NFIND Unexecuted instantiation: windows_RB_MINMAX |
80 | 0 | RB_GENERATE(winlinks, winlink, entry, winlink_cmp); Unexecuted instantiation: winlinks_RB_REMOVE_COLOR Unexecuted instantiation: winlinks_RB_REMOVE Unexecuted instantiation: winlinks_RB_INSERT Unexecuted instantiation: winlinks_RB_FIND Unexecuted instantiation: winlinks_RB_NFIND Unexecuted instantiation: winlinks_RB_MINMAX |
81 | 0 | RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp); Unexecuted instantiation: window_pane_tree_RB_REMOVE_COLOR Unexecuted instantiation: window_pane_tree_RB_REMOVE Unexecuted instantiation: window_pane_tree_RB_INSERT Unexecuted instantiation: window_pane_tree_RB_FIND Unexecuted instantiation: window_pane_tree_RB_NFIND Unexecuted instantiation: window_pane_tree_RB_MINMAX |
82 | 0 |
|
83 | 0 | struct window_pane_prompt { |
84 | 0 | u_int wp_id; |
85 | 0 | struct client *c; |
86 | 0 | status_prompt_input_cb inputcb; |
87 | 0 | prompt_free_cb freecb; |
88 | 0 | void *data; |
89 | 0 | enum prompt_type type; |
90 | 0 | }; |
91 | 0 |
|
92 | 0 | int |
93 | 0 | window_cmp(struct window *w1, struct window *w2) |
94 | 0 | { |
95 | 0 | return (w1->id - w2->id); |
96 | 0 | } |
97 | | |
98 | | static void |
99 | | window_fire_renamed(struct window *w, const char *old_name) |
100 | 0 | { |
101 | 0 | struct event_payload *ep; |
102 | 0 | struct cmd_find_state fs; |
103 | |
|
104 | 0 | ep = event_payload_create(); |
105 | 0 | cmd_find_from_window(&fs, w, 0); |
106 | 0 | event_payload_set_target(ep, &fs); |
107 | 0 | event_payload_set_window(ep, "window", w); |
108 | 0 | event_payload_set_string(ep, "old_name", "%s", old_name); |
109 | 0 | event_payload_set_string(ep, "new_name", "%s", w->name); |
110 | 0 | events_fire("window-renamed", ep); |
111 | 0 | } |
112 | | |
113 | | static void |
114 | | window_fire_pane_changed(struct window *w, struct window_pane *wp, |
115 | | struct window_pane *lastwp) |
116 | 0 | { |
117 | 0 | struct event_payload *ep; |
118 | 0 | struct cmd_find_state fs; |
119 | |
|
120 | 0 | ep = event_payload_create(); |
121 | 0 | cmd_find_from_pane(&fs, wp, 0); |
122 | 0 | event_payload_set_target(ep, &fs); |
123 | 0 | event_payload_set_window(ep, "window", w); |
124 | 0 | event_payload_set_pane(ep, "pane", wp); |
125 | 0 | event_payload_set_pane(ep, "new_pane", wp); |
126 | 0 | if (lastwp != NULL) |
127 | 0 | event_payload_set_pane(ep, "old_pane", lastwp); |
128 | 0 | events_fire("window-pane-changed", ep); |
129 | 0 | } |
130 | | |
131 | | void |
132 | | window_fire_pane_moved(struct window_pane *wp, struct window *old_w, |
133 | | int old_idx, struct window *new_w, int new_idx) |
134 | 0 | { |
135 | 0 | struct event_payload *ep; |
136 | 0 | struct cmd_find_state fs; |
137 | |
|
138 | 0 | ep = event_payload_create(); |
139 | 0 | cmd_find_from_pane(&fs, wp, 0); |
140 | 0 | event_payload_set_target(ep, &fs); |
141 | 0 | event_payload_set_pane(ep, "pane", wp); |
142 | 0 | event_payload_set_window(ep, "window", new_w); |
143 | 0 | event_payload_set_window(ep, "old_window", old_w); |
144 | 0 | event_payload_set_window(ep, "new_window", new_w); |
145 | 0 | if (old_idx != -1) |
146 | 0 | event_payload_set_int(ep, "old_window_index", old_idx); |
147 | 0 | if (new_idx != -1) { |
148 | 0 | event_payload_set_int(ep, "window_index", new_idx); |
149 | 0 | event_payload_set_int(ep, "new_window_index", new_idx); |
150 | 0 | } |
151 | 0 | events_fire("pane-moved", ep); |
152 | 0 | } |
153 | | |
154 | | static void |
155 | | window_fire_pane_mode_changed(const char *name, struct window_pane *wp, |
156 | | const char *previous, const char *current, int entered) |
157 | 0 | { |
158 | 0 | struct event_payload *ep; |
159 | 0 | struct cmd_find_state fs; |
160 | |
|
161 | 0 | ep = event_payload_create(); |
162 | 0 | cmd_find_from_pane(&fs, wp, 0); |
163 | 0 | event_payload_set_target(ep, &fs); |
164 | 0 | event_payload_set_pane(ep, "pane", wp); |
165 | 0 | event_payload_set_window(ep, "window", wp->window); |
166 | |
|
167 | 0 | if (current != NULL) |
168 | 0 | event_payload_set_string(ep, "current_mode", "%s", current); |
169 | 0 | if (previous != NULL) |
170 | 0 | event_payload_set_string(ep, "previous_mode", "%s", previous); |
171 | 0 | event_payload_set_int(ep, "mode_entered", entered); |
172 | |
|
173 | 0 | events_fire(name, ep); |
174 | 0 | } |
175 | | |
176 | | static void |
177 | | window_fire_pane_prompt(const char *name, struct window_pane *wp, |
178 | | enum prompt_type type) |
179 | 0 | { |
180 | 0 | struct event_payload *ep; |
181 | 0 | struct cmd_find_state fs; |
182 | 0 | const char *type_string = prompt_type_string(type); |
183 | |
|
184 | 0 | ep = event_payload_create(); |
185 | 0 | cmd_find_from_pane(&fs, wp, 0); |
186 | 0 | event_payload_set_target(ep, &fs); |
187 | 0 | event_payload_set_pane(ep, "pane", wp); |
188 | 0 | event_payload_set_window(ep, "window", wp->window); |
189 | 0 | event_payload_set_string(ep, "prompt_type", "%s", type_string); |
190 | 0 | events_fire(name, ep); |
191 | 0 | } |
192 | | |
193 | | int |
194 | | winlink_cmp(struct winlink *wl1, struct winlink *wl2) |
195 | 0 | { |
196 | 0 | return (wl1->idx - wl2->idx); |
197 | 0 | } |
198 | | |
199 | | int |
200 | | window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2) |
201 | 0 | { |
202 | 0 | return (wp1->id - wp2->id); |
203 | 0 | } |
204 | | |
205 | | struct winlink * |
206 | | winlink_find_by_window(struct winlinks *wwl, struct window *w) |
207 | 0 | { |
208 | 0 | struct winlink *wl; |
209 | |
|
210 | 0 | RB_FOREACH(wl, winlinks, wwl) { |
211 | 0 | if (wl->window == w) |
212 | 0 | return (wl); |
213 | 0 | } |
214 | | |
215 | 0 | return (NULL); |
216 | 0 | } |
217 | | |
218 | | struct winlink * |
219 | | winlink_find_by_index(struct winlinks *wwl, int idx) |
220 | 0 | { |
221 | 0 | struct winlink wl; |
222 | |
|
223 | 0 | if (idx < 0) |
224 | 0 | fatalx("bad index"); |
225 | | |
226 | 0 | wl.idx = idx; |
227 | 0 | return (RB_FIND(winlinks, wwl, &wl)); |
228 | 0 | } |
229 | | |
230 | | struct winlink * |
231 | | winlink_find_by_window_id(struct winlinks *wwl, u_int id) |
232 | 0 | { |
233 | 0 | struct winlink *wl; |
234 | |
|
235 | 0 | RB_FOREACH(wl, winlinks, wwl) { |
236 | 0 | if (wl->window->id == id) |
237 | 0 | return (wl); |
238 | 0 | } |
239 | 0 | return (NULL); |
240 | 0 | } |
241 | | |
242 | | static int |
243 | | winlink_next_index(struct winlinks *wwl, int idx) |
244 | 0 | { |
245 | 0 | int i; |
246 | |
|
247 | 0 | i = idx; |
248 | 0 | do { |
249 | 0 | if (winlink_find_by_index(wwl, i) == NULL) |
250 | 0 | return (i); |
251 | 0 | if (i == INT_MAX) |
252 | 0 | i = 0; |
253 | 0 | else |
254 | 0 | i++; |
255 | 0 | } while (i != idx); |
256 | 0 | return (-1); |
257 | 0 | } |
258 | | |
259 | | u_int |
260 | | winlink_count(struct winlinks *wwl) |
261 | 0 | { |
262 | 0 | struct winlink *wl; |
263 | 0 | u_int n; |
264 | |
|
265 | 0 | n = 0; |
266 | 0 | RB_FOREACH(wl, winlinks, wwl) |
267 | 0 | n++; |
268 | |
|
269 | 0 | return (n); |
270 | 0 | } |
271 | | |
272 | | struct winlink * |
273 | | winlink_add(struct winlinks *wwl, int idx) |
274 | 0 | { |
275 | 0 | struct winlink *wl; |
276 | |
|
277 | 0 | if (idx < 0) { |
278 | 0 | if ((idx = winlink_next_index(wwl, -idx - 1)) == -1) |
279 | 0 | return (NULL); |
280 | 0 | } else if (winlink_find_by_index(wwl, idx) != NULL) |
281 | 0 | return (NULL); |
282 | | |
283 | 0 | wl = xcalloc(1, sizeof *wl); |
284 | 0 | wl->idx = idx; |
285 | 0 | RB_INSERT(winlinks, wwl, wl); |
286 | |
|
287 | 0 | return (wl); |
288 | 0 | } |
289 | | |
290 | | void |
291 | | winlink_set_window(struct winlink *wl, struct window *w) |
292 | 0 | { |
293 | 0 | if (wl->window != NULL) { |
294 | 0 | TAILQ_REMOVE(&wl->window->winlinks, wl, wentry); |
295 | 0 | window_remove_ref(wl->window, __func__); |
296 | 0 | } |
297 | 0 | TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry); |
298 | 0 | wl->window = w; |
299 | 0 | window_add_ref(w, __func__); |
300 | 0 | } |
301 | | |
302 | | void |
303 | | winlink_remove(struct winlinks *wwl, struct winlink *wl) |
304 | 0 | { |
305 | 0 | struct window *w = wl->window; |
306 | |
|
307 | 0 | if (w != NULL) { |
308 | 0 | TAILQ_REMOVE(&w->winlinks, wl, wentry); |
309 | 0 | window_remove_ref(w, __func__); |
310 | 0 | } |
311 | |
|
312 | 0 | RB_REMOVE(winlinks, wwl, wl); |
313 | 0 | free(wl); |
314 | 0 | } |
315 | | |
316 | | struct winlink * |
317 | | winlink_next(struct winlink *wl) |
318 | 0 | { |
319 | 0 | return (RB_NEXT(winlinks, wwl, wl)); |
320 | 0 | } |
321 | | |
322 | | struct winlink * |
323 | | winlink_previous(struct winlink *wl) |
324 | 0 | { |
325 | 0 | return (RB_PREV(winlinks, wwl, wl)); |
326 | 0 | } |
327 | | |
328 | | struct winlink * |
329 | | winlink_next_by_number(struct winlink *wl, struct session *s, int n) |
330 | 0 | { |
331 | 0 | for (; n > 0; n--) { |
332 | 0 | if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL) |
333 | 0 | wl = RB_MIN(winlinks, &s->windows); |
334 | 0 | } |
335 | |
|
336 | 0 | return (wl); |
337 | 0 | } |
338 | | |
339 | | struct winlink * |
340 | | winlink_previous_by_number(struct winlink *wl, struct session *s, int n) |
341 | 0 | { |
342 | 0 | for (; n > 0; n--) { |
343 | 0 | if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL) |
344 | 0 | wl = RB_MAX(winlinks, &s->windows); |
345 | 0 | } |
346 | |
|
347 | 0 | return (wl); |
348 | 0 | } |
349 | | |
350 | | void |
351 | | winlink_stack_push(struct winlink_stack *stack, struct winlink *wl) |
352 | 0 | { |
353 | 0 | if (wl == NULL) |
354 | 0 | return; |
355 | | |
356 | 0 | winlink_stack_remove(stack, wl); |
357 | 0 | TAILQ_INSERT_HEAD(stack, wl, sentry); |
358 | 0 | wl->flags |= WINLINK_VISITED; |
359 | 0 | } |
360 | | |
361 | | void |
362 | | winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl) |
363 | 0 | { |
364 | 0 | if (wl != NULL && (wl->flags & WINLINK_VISITED)) { |
365 | 0 | TAILQ_REMOVE(stack, wl, sentry); |
366 | 0 | wl->flags &= ~WINLINK_VISITED; |
367 | 0 | } |
368 | 0 | } |
369 | | |
370 | | struct window * |
371 | | window_find_by_id_str(const char *s) |
372 | 0 | { |
373 | 0 | const char *errstr; |
374 | 0 | u_int id; |
375 | |
|
376 | 0 | if (*s != '@') |
377 | 0 | return (NULL); |
378 | | |
379 | 0 | id = strtonum(s + 1, 0, UINT_MAX, &errstr); |
380 | 0 | if (errstr != NULL) |
381 | 0 | return (NULL); |
382 | 0 | return (window_find_by_id(id)); |
383 | 0 | } |
384 | | |
385 | | struct window * |
386 | | window_find_by_id(u_int id) |
387 | 0 | { |
388 | 0 | struct window w; |
389 | |
|
390 | 0 | w.id = id; |
391 | 0 | return (RB_FIND(windows, &windows, &w)); |
392 | 0 | } |
393 | | |
394 | | void |
395 | | window_update_activity(struct window *w) |
396 | 0 | { |
397 | 0 | gettimeofday(&w->activity_time, NULL); |
398 | 0 | alerts_queue(w, WINDOW_ACTIVITY); |
399 | 0 | } |
400 | | |
401 | | struct window * |
402 | | window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel) |
403 | 0 | { |
404 | 0 | struct window *w; |
405 | |
|
406 | 0 | if (xpixel == 0) |
407 | 0 | xpixel = DEFAULT_XPIXEL; |
408 | 0 | if (ypixel == 0) |
409 | 0 | ypixel = DEFAULT_YPIXEL; |
410 | |
|
411 | 0 | w = xcalloc(1, sizeof *w); |
412 | 0 | w->name = xstrdup(""); |
413 | 0 | w->flags = 0; |
414 | |
|
415 | 0 | TAILQ_INIT(&w->panes); |
416 | 0 | TAILQ_INIT(&w->z_index); |
417 | 0 | TAILQ_INIT(&w->last_panes); |
418 | 0 | w->active = NULL; |
419 | |
|
420 | 0 | w->lastlayout = -1; |
421 | 0 | w->layout_root = NULL; |
422 | |
|
423 | 0 | w->sx = sx; |
424 | 0 | w->sy = sy; |
425 | 0 | w->manual_sx = sx; |
426 | 0 | w->manual_sy = sy; |
427 | 0 | w->xpixel = xpixel; |
428 | 0 | w->ypixel = ypixel; |
429 | |
|
430 | 0 | w->options = options_create(global_w_options); |
431 | 0 | w->sb = options_get_number(w->options, "pane-scrollbars"); |
432 | 0 | w->sb_pos = options_get_number(w->options, "pane-scrollbars-position"); |
433 | |
|
434 | 0 | w->references = 0; |
435 | 0 | TAILQ_INIT(&w->winlinks); |
436 | |
|
437 | 0 | w->id = next_window_id++; |
438 | 0 | RB_INSERT(windows, &windows, w); |
439 | |
|
440 | 0 | if (gettimeofday(&w->creation_time, NULL) != 0) |
441 | 0 | fatal("gettimeofday failed"); |
442 | 0 | window_update_activity(w); |
443 | |
|
444 | 0 | log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy, |
445 | 0 | w->xpixel, w->ypixel); |
446 | 0 | return (w); |
447 | 0 | } |
448 | | |
449 | | static void |
450 | | window_destroy(struct window *w) |
451 | 0 | { |
452 | 0 | log_debug("window @%u destroyed (%d references)", w->id, w->references); |
453 | |
|
454 | 0 | window_unzoom(w, 0); |
455 | 0 | RB_REMOVE(windows, &windows, w); |
456 | |
|
457 | 0 | layout_free_cell(w->layout_root, 0); |
458 | 0 | layout_free_cell(w->saved_layout_root, 0); |
459 | 0 | free(w->old_layout); |
460 | |
|
461 | 0 | menu_destroy(w); |
462 | 0 | window_destroy_panes(w); |
463 | |
|
464 | 0 | if (event_initialized(&w->name_event)) |
465 | 0 | evtimer_del(&w->name_event); |
466 | |
|
467 | 0 | if (event_initialized(&w->alerts_timer)) |
468 | 0 | evtimer_del(&w->alerts_timer); |
469 | 0 | if (event_initialized(&w->offset_timer)) |
470 | 0 | event_del(&w->offset_timer); |
471 | |
|
472 | 0 | options_free(w->options); |
473 | |
|
474 | 0 | free(w->name); |
475 | 0 | free(w); |
476 | 0 | } |
477 | | |
478 | | int |
479 | | window_pane_destroy_ready(struct window_pane *wp) |
480 | 0 | { |
481 | 0 | int n; |
482 | |
|
483 | 0 | if (wp->pipe_fd != -1 && EVBUFFER_LENGTH(wp->pipe_event->output) != 0) |
484 | 0 | return (0); |
485 | 0 | if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0) |
486 | 0 | return (0); |
487 | | |
488 | 0 | if (~wp->flags & PANE_EXITED) |
489 | 0 | return (0); |
490 | | |
491 | | /* |
492 | | * If a command queue item is blocked on this pane, wait for the |
493 | | * child's exit status before destroying it. |
494 | | */ |
495 | 0 | if (wp->wait_item != NULL && (~wp->flags & PANE_STATUSREADY)) |
496 | 0 | return (0); |
497 | 0 | if (wp->editor != NULL && (~wp->flags & PANE_STATUSREADY)) |
498 | 0 | return (0); |
499 | 0 | return (1); |
500 | 0 | } |
501 | | |
502 | | void |
503 | | window_add_ref(struct window *w, const char *from) |
504 | 0 | { |
505 | 0 | w->references++; |
506 | 0 | log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references); |
507 | 0 | } |
508 | | |
509 | | void |
510 | | window_remove_ref(struct window *w, const char *from) |
511 | 0 | { |
512 | 0 | if (w->references == 1) |
513 | 0 | events_fire_window("window-closed", w); |
514 | |
|
515 | 0 | w->references--; |
516 | 0 | log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references); |
517 | |
|
518 | 0 | if (w->references == 0) |
519 | 0 | window_destroy(w); |
520 | 0 | } |
521 | | |
522 | | void |
523 | | window_pane_add_ref(struct window_pane *wp, const char *from) |
524 | 0 | { |
525 | 0 | wp->references++; |
526 | 0 | log_debug("%s: %%%u %s, now %d", __func__, wp->id, from, |
527 | 0 | wp->references); |
528 | 0 | } |
529 | | |
530 | | void |
531 | | window_pane_remove_ref(struct window_pane *wp, const char *from) |
532 | 0 | { |
533 | 0 | wp->references--; |
534 | 0 | log_debug("%s: %%%u %s, now %d", __func__, wp->id, from, |
535 | 0 | wp->references); |
536 | |
|
537 | 0 | if (wp->references == 0) |
538 | 0 | window_pane_free(wp); |
539 | 0 | } |
540 | | |
541 | | void |
542 | | window_set_name(struct window *w, const char *new_name, int untrusted) |
543 | 0 | { |
544 | 0 | char *last, *name; |
545 | |
|
546 | 0 | name = clean_name(new_name, untrusted); |
547 | 0 | if (name != NULL) { |
548 | 0 | last = xstrdup(w->name); |
549 | 0 | free(w->name); |
550 | 0 | w->name = name; |
551 | 0 | window_fire_renamed(w, last); |
552 | 0 | free(last); |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | | void |
557 | | window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel) |
558 | 0 | { |
559 | 0 | if (xpixel == 0) |
560 | 0 | xpixel = DEFAULT_XPIXEL; |
561 | 0 | if (ypixel == 0) |
562 | 0 | ypixel = DEFAULT_YPIXEL; |
563 | |
|
564 | 0 | log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy, |
565 | 0 | xpixel == -1 ? w->xpixel : (u_int)xpixel, |
566 | 0 | ypixel == -1 ? w->ypixel : (u_int)ypixel); |
567 | 0 | w->sx = sx; |
568 | 0 | w->sy = sy; |
569 | 0 | if (w->menu != NULL) { |
570 | 0 | menu_resize(w->menu, w); |
571 | 0 | server_redraw_window(w); |
572 | 0 | } |
573 | 0 | if (xpixel != -1) |
574 | 0 | w->xpixel = xpixel; |
575 | 0 | if (ypixel != -1) |
576 | 0 | w->ypixel = ypixel; |
577 | 0 | redraw_invalidate_scene(w); |
578 | 0 | } |
579 | | |
580 | | void |
581 | | window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy) |
582 | 0 | { |
583 | 0 | struct window *w = wp->window; |
584 | 0 | struct winsize ws; |
585 | |
|
586 | 0 | if (wp->fd == -1) |
587 | 0 | return; |
588 | | |
589 | 0 | log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy); |
590 | |
|
591 | 0 | memset(&ws, 0, sizeof ws); |
592 | 0 | ws.ws_col = sx; |
593 | 0 | ws.ws_row = sy; |
594 | 0 | ws.ws_xpixel = w->xpixel * ws.ws_col; |
595 | 0 | ws.ws_ypixel = w->ypixel * ws.ws_row; |
596 | 0 | if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1) |
597 | | #ifdef __sun |
598 | | /* |
599 | | * Some versions of Solaris apparently can return an error when |
600 | | * resizing; don't know why this happens, can't reproduce on |
601 | | * other platforms and ignoring it doesn't seem to cause any |
602 | | * issues. |
603 | | */ |
604 | | if (errno != EINVAL && errno != ENXIO) |
605 | | #endif |
606 | 0 | fatal("ioctl failed"); |
607 | 0 | } |
608 | | |
609 | | int |
610 | | window_has_floating_panes(struct window *w) |
611 | 0 | { |
612 | 0 | struct window_pane *wp; |
613 | |
|
614 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
615 | 0 | if (window_pane_is_floating(wp)) |
616 | 0 | return (1); |
617 | 0 | } |
618 | 0 | return (0); |
619 | 0 | } |
620 | | |
621 | | int |
622 | | window_has_pane(struct window *w, struct window_pane *wp) |
623 | 0 | { |
624 | 0 | struct window_pane *wp1; |
625 | |
|
626 | 0 | TAILQ_FOREACH(wp1, &w->panes, entry) { |
627 | 0 | if (wp1 == wp) |
628 | 0 | return (1); |
629 | 0 | } |
630 | 0 | return (0); |
631 | 0 | } |
632 | | |
633 | | int |
634 | | window_pane_contains(struct window_pane *wp, u_int x, u_int y) |
635 | 0 | { |
636 | 0 | int xoff, yoff; |
637 | 0 | u_int sx, sy; |
638 | |
|
639 | 0 | if (!window_pane_is_visible(wp)) |
640 | 0 | return (0); |
641 | | |
642 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
643 | 0 | if (!window_pane_is_floating(wp)) { |
644 | 0 | if ((int)x < xoff || x > xoff + sx) |
645 | 0 | return (0); |
646 | 0 | if ((int)y < yoff || y > yoff + sy) |
647 | 0 | return (0); |
648 | 0 | } else if (window_pane_get_pane_lines(wp) == PANE_LINES_NONE) { |
649 | 0 | if ((int)x < xoff || (int)x >= xoff + (int)sx) |
650 | 0 | return (0); |
651 | 0 | if ((int)y < yoff || (int)y >= yoff + (int)sy) |
652 | 0 | return (0); |
653 | 0 | } else { |
654 | 0 | if ((int)x < xoff - 1 || x > xoff + sx) |
655 | 0 | return (0); |
656 | 0 | if ((int)y < yoff - 1 || y > yoff + sy) |
657 | 0 | return (0); |
658 | 0 | } |
659 | 0 | return (1); |
660 | 0 | } |
661 | | |
662 | | void |
663 | | window_update_focus(struct window *w) |
664 | 0 | { |
665 | 0 | if (w != NULL) { |
666 | 0 | log_debug("%s: @%u", __func__, w->id); |
667 | 0 | window_pane_update_focus(w->active); |
668 | 0 | } |
669 | 0 | } |
670 | | |
671 | | void |
672 | | window_pane_update_focus(struct window_pane *wp) |
673 | 0 | { |
674 | 0 | struct client *c; |
675 | 0 | int focused = 0; |
676 | |
|
677 | 0 | if (wp != NULL && (~wp->flags & PANE_EXITED)) { |
678 | 0 | if (wp != wp->window->active) |
679 | 0 | focused = 0; |
680 | 0 | else { |
681 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
682 | 0 | if (c->session != NULL && |
683 | 0 | c->session->attached != 0 && |
684 | 0 | (c->flags & CLIENT_FOCUSED) && |
685 | 0 | c->session->curw->window == wp->window && |
686 | 0 | c->overlay_draw == NULL && |
687 | 0 | wp->window->menu == NULL) { |
688 | 0 | focused = 1; |
689 | 0 | break; |
690 | 0 | } |
691 | 0 | } |
692 | 0 | } |
693 | 0 | if (!focused && (wp->flags & PANE_FOCUSED)) { |
694 | 0 | log_debug("%s: %%%u focus out", __func__, wp->id); |
695 | 0 | if (wp->base.mode & MODE_FOCUSON) |
696 | 0 | bufferevent_write(wp->event, "\033[O", 3); |
697 | 0 | events_fire_pane("pane-focus-out", wp); |
698 | 0 | wp->flags &= ~PANE_FOCUSED; |
699 | 0 | } else if (focused && (~wp->flags & PANE_FOCUSED)) { |
700 | 0 | log_debug("%s: %%%u focus in", __func__, wp->id); |
701 | 0 | if (wp->base.mode & MODE_FOCUSON) |
702 | 0 | bufferevent_write(wp->event, "\033[I", 3); |
703 | 0 | events_fire_pane("pane-focus-in", wp); |
704 | 0 | wp->flags |= PANE_FOCUSED; |
705 | 0 | } else |
706 | 0 | log_debug("%s: %%%u focus unchanged", __func__, wp->id); |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | | int |
711 | | window_set_active_pane(struct window *w, struct window_pane *wp, int notify) |
712 | 0 | { |
713 | 0 | struct window_pane *lastwp; |
714 | |
|
715 | 0 | log_debug("%s: pane %%%u", __func__, wp->id); |
716 | |
|
717 | 0 | if (wp == w->active) |
718 | 0 | return (0); |
719 | 0 | if (w->modal != NULL && wp != w->modal) |
720 | 0 | return (0); |
721 | 0 | if (w->flags & WINDOW_ZOOMED) |
722 | 0 | window_unzoom(w, 1); |
723 | 0 | lastwp = w->active; |
724 | |
|
725 | 0 | window_pane_stack_remove(&w->last_panes, wp); |
726 | 0 | window_pane_stack_push(&w->last_panes, lastwp); |
727 | |
|
728 | 0 | w->active = wp; |
729 | 0 | w->active->active_point = next_active_point++; |
730 | 0 | w->active->flags |= PANE_CHANGED; |
731 | |
|
732 | 0 | if (options_get_number(global_options, "focus-events")) { |
733 | 0 | window_pane_update_focus(lastwp); |
734 | 0 | window_pane_update_focus(w->active); |
735 | 0 | } |
736 | |
|
737 | 0 | tty_update_window_offset(w); |
738 | 0 | server_redraw_window(w); |
739 | |
|
740 | 0 | if (notify) |
741 | 0 | window_fire_pane_changed(w, w->active, lastwp); |
742 | 0 | return (1); |
743 | 0 | } |
744 | | |
745 | | static int |
746 | | window_pane_get_palette(struct window_pane *wp, int c) |
747 | 0 | { |
748 | 0 | if (wp == NULL) |
749 | 0 | return (-1); |
750 | 0 | return (colour_palette_get(&wp->palette, c)); |
751 | 0 | } |
752 | | |
753 | | void |
754 | | window_redraw_active_switch(struct window *w, struct window_pane *wp) |
755 | 0 | { |
756 | 0 | struct grid_cell *gc1, *gc2; |
757 | 0 | int c1, c2; |
758 | |
|
759 | 0 | if (w->modal != NULL && wp != w->modal) |
760 | 0 | return; |
761 | 0 | if (wp == w->active) |
762 | 0 | return; |
763 | | |
764 | 0 | for (;;) { |
765 | | /* |
766 | | * If the active and inactive styles or palettes are different, |
767 | | * need to redraw the panes. |
768 | | */ |
769 | 0 | gc1 = &wp->cached_gc; |
770 | 0 | gc2 = &wp->cached_active_gc; |
771 | 0 | if (!grid_cells_look_equal(gc1, gc2)) |
772 | 0 | wp->flags |= PANE_REDRAW; |
773 | 0 | else if (wp->cached_dim != wp->cached_active_dim) |
774 | 0 | wp->flags |= PANE_REDRAW; |
775 | 0 | else { |
776 | 0 | c1 = window_pane_get_palette(wp, gc1->fg); |
777 | 0 | c2 = window_pane_get_palette(wp, gc2->fg); |
778 | 0 | if (c1 != c2) |
779 | 0 | wp->flags |= PANE_REDRAW; |
780 | 0 | else { |
781 | 0 | c1 = window_pane_get_palette(wp, gc1->bg); |
782 | 0 | c2 = window_pane_get_palette(wp, gc2->bg); |
783 | 0 | if (c1 != c2) |
784 | 0 | wp->flags |= PANE_REDRAW; |
785 | 0 | } |
786 | 0 | } |
787 | 0 | if (wp == w->active) |
788 | 0 | break; |
789 | | |
790 | | /* If the pane is floating, move to the front. */ |
791 | 0 | if (window_pane_is_floating(wp)) { |
792 | 0 | TAILQ_REMOVE(&w->z_index, wp, zentry); |
793 | 0 | TAILQ_INSERT_HEAD(&w->z_index, wp, zentry); |
794 | 0 | wp->flags |= PANE_REDRAW; |
795 | 0 | redraw_invalidate_scene(w); |
796 | 0 | } |
797 | |
|
798 | 0 | wp = w->active; |
799 | 0 | if (wp == NULL) |
800 | 0 | break; |
801 | 0 | } |
802 | 0 | } |
803 | | |
804 | | struct window_pane * |
805 | | window_get_active_at(struct window *w, u_int x, u_int y) |
806 | 0 | { |
807 | 0 | struct window_pane *wp; |
808 | 0 | int pane_status, xoff, yoff; |
809 | 0 | u_int sx, sy; |
810 | |
|
811 | 0 | pane_status = window_get_pane_status(w); |
812 | |
|
813 | 0 | if (w->modal != NULL) { |
814 | 0 | if (window_pane_contains(w->modal, x, y)) |
815 | 0 | return (w->modal); |
816 | 0 | return (NULL); |
817 | 0 | } |
818 | | |
819 | 0 | if (pane_status == PANE_STATUS_TOP) { |
820 | | /* |
821 | | * Prefer a pane's top border status line over the pane above's |
822 | | * bottom border. |
823 | | */ |
824 | 0 | TAILQ_FOREACH(wp, &w->z_index, zentry) { |
825 | 0 | if (!window_pane_is_visible(wp) || |
826 | 0 | window_pane_is_floating(wp)) |
827 | 0 | continue; |
828 | | |
829 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, |
830 | 0 | &sy); |
831 | 0 | if ((int)x < xoff || x > xoff + sx) |
832 | 0 | continue; |
833 | 0 | if ((int)y == yoff - 1) |
834 | 0 | return (wp); |
835 | 0 | } |
836 | 0 | } |
837 | | |
838 | 0 | TAILQ_FOREACH(wp, &w->z_index, zentry) { |
839 | 0 | if (!window_pane_is_visible(wp)) |
840 | 0 | continue; |
841 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
842 | 0 | if (!window_pane_is_floating(wp)) { |
843 | | /* |
844 | | * Tiled - to and including the right border, excluding |
845 | | * the bottom border. |
846 | | */ |
847 | 0 | if ((int)x < xoff || x > xoff + sx) |
848 | 0 | continue; |
849 | 0 | if (pane_status == PANE_STATUS_TOP) { |
850 | 0 | if ((int)y < yoff - 1 || y > yoff + sy) |
851 | 0 | continue; |
852 | 0 | } else { |
853 | 0 | if ((int)y < yoff || y > yoff + sy) |
854 | 0 | continue; |
855 | 0 | } |
856 | 0 | } else { |
857 | 0 | if (window_pane_get_pane_lines(wp) == PANE_LINES_NONE) { |
858 | 0 | if ((int)x < xoff || (int)x >= xoff + (int)sx) |
859 | 0 | continue; |
860 | 0 | if ((int)y < yoff || (int)y >= yoff + (int)sy) |
861 | 0 | continue; |
862 | 0 | } else { |
863 | | /* Floating - include all borders. */ |
864 | 0 | if ((int)x < xoff - 1 || x > xoff + sx) |
865 | 0 | continue; |
866 | 0 | if ((int)y < yoff - 1 || y > yoff + sy) |
867 | 0 | continue; |
868 | 0 | } |
869 | 0 | } |
870 | 0 | return (wp); |
871 | 0 | } |
872 | 0 | return (NULL); |
873 | 0 | } |
874 | | |
875 | | struct window_pane * |
876 | | window_find_string(struct window *w, const char *s) |
877 | 0 | { |
878 | 0 | u_int x, y, top = 0, bottom = w->sy - 1; |
879 | 0 | int status; |
880 | |
|
881 | 0 | x = w->sx / 2; |
882 | 0 | y = w->sy / 2; |
883 | |
|
884 | 0 | status = window_get_pane_status(w); |
885 | 0 | if (status == PANE_STATUS_TOP) |
886 | 0 | top++; |
887 | 0 | else if (status == PANE_STATUS_BOTTOM) |
888 | 0 | bottom--; |
889 | |
|
890 | 0 | if (strcasecmp(s, "top") == 0) |
891 | 0 | y = top; |
892 | 0 | else if (strcasecmp(s, "bottom") == 0) |
893 | 0 | y = bottom; |
894 | 0 | else if (strcasecmp(s, "left") == 0) |
895 | 0 | x = 0; |
896 | 0 | else if (strcasecmp(s, "right") == 0) |
897 | 0 | x = w->sx - 1; |
898 | 0 | else if (strcasecmp(s, "top-left") == 0) { |
899 | 0 | x = 0; |
900 | 0 | y = top; |
901 | 0 | } else if (strcasecmp(s, "top-right") == 0) { |
902 | 0 | x = w->sx - 1; |
903 | 0 | y = top; |
904 | 0 | } else if (strcasecmp(s, "bottom-left") == 0) { |
905 | 0 | x = 0; |
906 | 0 | y = bottom; |
907 | 0 | } else if (strcasecmp(s, "bottom-right") == 0) { |
908 | 0 | x = w->sx - 1; |
909 | 0 | y = bottom; |
910 | 0 | } else |
911 | 0 | return (NULL); |
912 | | |
913 | 0 | return (window_get_active_at(w, x, y)); |
914 | 0 | } |
915 | | |
916 | | int |
917 | | window_zoom(struct window_pane *wp) |
918 | 0 | { |
919 | 0 | struct window *w = wp->window; |
920 | 0 | struct window_pane *wp1; |
921 | |
|
922 | 0 | if (w->flags & WINDOW_ZOOMED) |
923 | 0 | return (-1); |
924 | 0 | if (window_count_panes(w, 1) == 1) |
925 | 0 | return (-1); |
926 | | |
927 | 0 | if (w->active != wp) |
928 | 0 | window_set_active_pane(w, wp, 1); |
929 | 0 | wp->flags |= PANE_ZOOMED; |
930 | |
|
931 | 0 | TAILQ_FOREACH(wp1, &w->panes, entry) { |
932 | 0 | wp1->saved_layout_cell = wp1->layout_cell; |
933 | 0 | wp1->layout_cell = NULL; |
934 | 0 | } |
935 | |
|
936 | 0 | w->saved_layout_root = w->layout_root; |
937 | 0 | layout_init(w, wp); |
938 | 0 | w->flags |= WINDOW_ZOOMED; |
939 | 0 | events_fire_window("window-zoomed", w); |
940 | 0 | events_fire_window("window-layout-changed", w); |
941 | |
|
942 | 0 | redraw_invalidate_scene(w); |
943 | 0 | return (0); |
944 | 0 | } |
945 | | |
946 | | int |
947 | | window_unzoom(struct window *w, int notify) |
948 | 0 | { |
949 | 0 | struct window_pane *wp; |
950 | |
|
951 | 0 | if (!(w->flags & WINDOW_ZOOMED)) |
952 | 0 | return (-1); |
953 | | |
954 | 0 | w->flags &= ~WINDOW_ZOOMED; |
955 | 0 | layout_free(w, 0); |
956 | 0 | w->layout_root = w->saved_layout_root; |
957 | 0 | w->saved_layout_root = NULL; |
958 | |
|
959 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
960 | 0 | wp->layout_cell = wp->saved_layout_cell; |
961 | 0 | wp->saved_layout_cell = NULL; |
962 | 0 | wp->flags &= ~PANE_ZOOMED; |
963 | 0 | } |
964 | 0 | layout_fix_panes(w, NULL); |
965 | |
|
966 | 0 | if (notify) { |
967 | 0 | events_fire_window("window-unzoomed", w); |
968 | 0 | events_fire_window("window-layout-changed", w); |
969 | 0 | } |
970 | |
|
971 | 0 | redraw_invalidate_scene(w); |
972 | 0 | return (0); |
973 | 0 | } |
974 | | |
975 | | void |
976 | | window_push_modal_zoom(struct window *w) |
977 | 0 | { |
978 | 0 | if (w->flags & WINDOW_ZOOMED) |
979 | 0 | w->flags |= WINDOW_WASMODALZOOMED; |
980 | 0 | else |
981 | 0 | w->flags &= ~WINDOW_WASMODALZOOMED; |
982 | 0 | window_unzoom(w, 1); |
983 | 0 | } |
984 | | |
985 | | int |
986 | | window_pop_modal_zoom(struct window *w) |
987 | 0 | { |
988 | 0 | struct window_pane *wp = w->active; |
989 | |
|
990 | 0 | if (~w->flags & WINDOW_WASMODALZOOMED) |
991 | 0 | return (0); |
992 | 0 | w->flags &= ~WINDOW_WASMODALZOOMED; |
993 | 0 | if (wp != NULL && window_has_pane(w, wp)) |
994 | 0 | return (window_zoom(wp) == 0); |
995 | 0 | return (0); |
996 | 0 | } |
997 | | |
998 | | int |
999 | | window_push_zoom(struct window *w, int always, int flag) |
1000 | 0 | { |
1001 | 0 | log_debug("%s: @%u %d", __func__, w->id, |
1002 | 0 | flag && (w->flags & WINDOW_ZOOMED)); |
1003 | 0 | if (flag && (always || (w->flags & WINDOW_ZOOMED))) |
1004 | 0 | w->flags |= WINDOW_WASZOOMED; |
1005 | 0 | else |
1006 | 0 | w->flags &= ~WINDOW_WASZOOMED; |
1007 | 0 | return (window_unzoom(w, 1) == 0); |
1008 | 0 | } |
1009 | | |
1010 | | int |
1011 | | window_pop_zoom(struct window *w) |
1012 | 0 | { |
1013 | 0 | log_debug("%s: @%u %d", __func__, w->id, |
1014 | 0 | !!(w->flags & WINDOW_WASZOOMED)); |
1015 | 0 | if (w->flags & WINDOW_WASZOOMED) |
1016 | 0 | return (window_zoom(w->active) == 0); |
1017 | 0 | return (0); |
1018 | 0 | } |
1019 | | |
1020 | | struct window_pane * |
1021 | | window_add_pane(struct window *w, struct window_pane *other, u_int hlimit, |
1022 | | int flags) |
1023 | 0 | { |
1024 | 0 | struct window_pane *wp; |
1025 | |
|
1026 | 0 | if (other == NULL) |
1027 | 0 | other = w->active; |
1028 | |
|
1029 | 0 | wp = window_pane_create(w, w->sx, w->sy, hlimit); |
1030 | 0 | if (TAILQ_EMPTY(&w->panes)) { |
1031 | 0 | log_debug("%s: @%u at start", __func__, w->id); |
1032 | 0 | TAILQ_INSERT_HEAD(&w->panes, wp, entry); |
1033 | 0 | } else if (flags & SPAWN_BEFORE) { |
1034 | 0 | log_debug("%s: @%u before %%%u", __func__, w->id, wp->id); |
1035 | 0 | if (flags & SPAWN_FULLSIZE) |
1036 | 0 | TAILQ_INSERT_HEAD(&w->panes, wp, entry); |
1037 | 0 | else |
1038 | 0 | TAILQ_INSERT_BEFORE(other, wp, entry); |
1039 | 0 | } else { |
1040 | 0 | log_debug("%s: @%u after %%%u", __func__, w->id, wp->id); |
1041 | 0 | if (flags & (SPAWN_FULLSIZE|SPAWN_FLOATING)) |
1042 | 0 | TAILQ_INSERT_TAIL(&w->panes, wp, entry); |
1043 | 0 | else |
1044 | 0 | TAILQ_INSERT_AFTER(&w->panes, other, wp, entry); |
1045 | 0 | } |
1046 | 0 | if (~flags & SPAWN_FLOATING) |
1047 | 0 | TAILQ_INSERT_TAIL(&w->z_index, wp, zentry); |
1048 | 0 | else if (w->modal != NULL) |
1049 | 0 | TAILQ_INSERT_AFTER(&w->z_index, w->modal, wp, zentry); |
1050 | 0 | else { |
1051 | 0 | TAILQ_INSERT_HEAD(&w->z_index, wp, zentry); |
1052 | 0 | } |
1053 | 0 | redraw_invalidate_scene(w); |
1054 | 0 | return (wp); |
1055 | 0 | } |
1056 | | |
1057 | | void |
1058 | | window_lost_pane(struct window *w, struct window_pane *wp) |
1059 | 0 | { |
1060 | 0 | struct window_pane *lastwp; |
1061 | |
|
1062 | 0 | log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id); |
1063 | |
|
1064 | 0 | if (wp == marked_pane.wp) |
1065 | 0 | server_clear_marked(); |
1066 | 0 | if (wp == w->modal_last) |
1067 | 0 | w->modal_last = NULL; |
1068 | 0 | if (w->modal_last == NULL) |
1069 | 0 | w->flags &= ~WINDOW_WASMODALZOOMED; |
1070 | |
|
1071 | 0 | window_pane_stack_remove(&w->last_panes, wp); |
1072 | 0 | if (wp == w->active) { |
1073 | 0 | lastwp = NULL; |
1074 | 0 | if (wp == w->modal) { |
1075 | 0 | lastwp = w->modal_last; |
1076 | 0 | w->modal = NULL; |
1077 | 0 | w->modal_last = NULL; |
1078 | 0 | } |
1079 | 0 | if (lastwp != NULL && window_has_pane(w, lastwp)) |
1080 | 0 | w->active = lastwp; |
1081 | 0 | else |
1082 | 0 | w->active = TAILQ_FIRST(&w->last_panes); |
1083 | 0 | if (w->active == NULL) { |
1084 | 0 | w->active = TAILQ_PREV(wp, window_panes, entry); |
1085 | 0 | if (w->active == NULL) |
1086 | 0 | w->active = TAILQ_NEXT(wp, entry); |
1087 | 0 | } |
1088 | 0 | if (w->active != NULL) { |
1089 | 0 | window_pane_stack_remove(&w->last_panes, w->active); |
1090 | 0 | w->active->flags |= PANE_CHANGED; |
1091 | 0 | window_fire_pane_changed(w, w->active, wp); |
1092 | 0 | window_update_focus(w); |
1093 | 0 | } |
1094 | 0 | } else if (wp == w->modal) { |
1095 | 0 | w->modal = w->modal_last = NULL; |
1096 | 0 | w->flags &= ~WINDOW_WASMODALZOOMED; |
1097 | 0 | } |
1098 | 0 | redraw_invalidate_scene(w); |
1099 | 0 | } |
1100 | | |
1101 | | void |
1102 | | window_remove_pane(struct window *w, struct window_pane *wp) |
1103 | 0 | { |
1104 | 0 | int pop = (wp == w->modal); |
1105 | |
|
1106 | 0 | window_lost_pane(w, wp); |
1107 | 0 | TAILQ_REMOVE(&w->panes, wp, entry); |
1108 | 0 | TAILQ_REMOVE(&w->z_index, wp, zentry); |
1109 | 0 | if (pop && window_pop_modal_zoom(w)) |
1110 | 0 | server_redraw_window(w); |
1111 | 0 | redraw_invalidate_scene(w); |
1112 | 0 | window_pane_destroy(wp); |
1113 | 0 | } |
1114 | | |
1115 | | struct window_pane * |
1116 | | window_pane_at_index(struct window *w, u_int idx) |
1117 | 0 | { |
1118 | 0 | struct window_pane *wp; |
1119 | 0 | u_int n; |
1120 | |
|
1121 | 0 | n = options_get_number(w->options, "pane-base-index"); |
1122 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1123 | 0 | if (n == idx) |
1124 | 0 | return (wp); |
1125 | 0 | n++; |
1126 | 0 | } |
1127 | 0 | return (NULL); |
1128 | 0 | } |
1129 | | |
1130 | | struct window_pane * |
1131 | | window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n) |
1132 | 0 | { |
1133 | 0 | for (; n > 0; n--) { |
1134 | 0 | if ((wp = TAILQ_NEXT(wp, entry)) == NULL) |
1135 | 0 | wp = TAILQ_FIRST(&w->panes); |
1136 | 0 | } |
1137 | |
|
1138 | 0 | return (wp); |
1139 | 0 | } |
1140 | | |
1141 | | struct window_pane * |
1142 | | window_pane_previous_by_number(struct window *w, struct window_pane *wp, |
1143 | | u_int n) |
1144 | 0 | { |
1145 | 0 | for (; n > 0; n--) { |
1146 | 0 | if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL) |
1147 | 0 | wp = TAILQ_LAST(&w->panes, window_panes); |
1148 | 0 | } |
1149 | |
|
1150 | 0 | return (wp); |
1151 | 0 | } |
1152 | | |
1153 | | int |
1154 | | window_pane_index(struct window_pane *wp, u_int *i) |
1155 | 0 | { |
1156 | 0 | struct window *w = wp->window; |
1157 | 0 | struct window_pane *wq; |
1158 | |
|
1159 | 0 | *i = options_get_number(w->options, "pane-base-index"); |
1160 | 0 | TAILQ_FOREACH(wq, &w->panes, entry) { |
1161 | 0 | if (wp == wq) { |
1162 | 0 | return (0); |
1163 | 0 | } |
1164 | 0 | (*i)++; |
1165 | 0 | } |
1166 | | |
1167 | 0 | return (-1); |
1168 | 0 | } |
1169 | | |
1170 | | int |
1171 | | window_pane_zindex(struct window_pane *wp, u_int *i) |
1172 | 0 | { |
1173 | 0 | struct window *w = wp->window; |
1174 | 0 | struct window_pane *wq; |
1175 | |
|
1176 | 0 | *i = 0; |
1177 | 0 | TAILQ_FOREACH(wq, &w->z_index, zentry) { |
1178 | 0 | if (wq == wp) { |
1179 | 0 | if (!window_pane_is_floating(wp)) |
1180 | 0 | (*i)++; |
1181 | 0 | return (0); |
1182 | 0 | } |
1183 | 0 | if (window_pane_is_floating(wq)) |
1184 | 0 | (*i)++; |
1185 | 0 | } |
1186 | | |
1187 | 0 | return (-1); |
1188 | 0 | } |
1189 | | |
1190 | | u_int |
1191 | | window_count_panes(struct window *w, int with_floating) |
1192 | 0 | { |
1193 | 0 | struct window_pane *wp; |
1194 | 0 | u_int n = 0; |
1195 | |
|
1196 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1197 | 0 | if (with_floating || !window_pane_is_floating(wp)) |
1198 | 0 | n++; |
1199 | 0 | } |
1200 | 0 | return (n); |
1201 | 0 | } |
1202 | | |
1203 | | void |
1204 | | window_destroy_panes(struct window *w) |
1205 | 0 | { |
1206 | 0 | struct window_pane *wp; |
1207 | |
|
1208 | 0 | while (!TAILQ_EMPTY(&w->last_panes)) { |
1209 | 0 | wp = TAILQ_FIRST(&w->last_panes); |
1210 | 0 | window_pane_stack_remove(&w->last_panes, wp); |
1211 | 0 | } |
1212 | |
|
1213 | 0 | while (!TAILQ_EMPTY(&w->panes)) { |
1214 | 0 | wp = TAILQ_FIRST(&w->panes); |
1215 | 0 | TAILQ_REMOVE(&w->panes, wp, entry); |
1216 | 0 | TAILQ_REMOVE(&w->z_index, wp, zentry); |
1217 | 0 | window_pane_destroy(wp); |
1218 | 0 | } |
1219 | 0 | } |
1220 | | |
1221 | | const char * |
1222 | | window_printable_flags(struct winlink *wl, int escape) |
1223 | 0 | { |
1224 | 0 | struct session *s = wl->session; |
1225 | 0 | static char flags[32]; |
1226 | 0 | u_int pos = 0; |
1227 | |
|
1228 | 0 | if (wl->flags & WINLINK_ACTIVITY) { |
1229 | 0 | flags[pos++] = '#'; |
1230 | 0 | if (escape) |
1231 | 0 | flags[pos++] = '#'; |
1232 | 0 | } |
1233 | 0 | if (wl->flags & WINLINK_BELL) |
1234 | 0 | flags[pos++] = '!'; |
1235 | 0 | if (wl->flags & WINLINK_SILENCE) |
1236 | 0 | flags[pos++] = '~'; |
1237 | 0 | if (wl == s->curw) |
1238 | 0 | flags[pos++] = '*'; |
1239 | 0 | if (wl == TAILQ_FIRST(&s->lastw)) |
1240 | 0 | flags[pos++] = '-'; |
1241 | 0 | if (server_check_marked() && wl == marked_pane.wl) |
1242 | 0 | flags[pos++] = 'M'; |
1243 | 0 | if (wl->window->modal != NULL) |
1244 | 0 | flags[pos++] = 'O'; |
1245 | 0 | if (wl->window->flags & WINDOW_ZOOMED) |
1246 | 0 | flags[pos++] = 'Z'; |
1247 | 0 | flags[pos] = '\0'; |
1248 | 0 | return (flags); |
1249 | 0 | } |
1250 | | |
1251 | | const char * |
1252 | | window_pane_printable_flags(struct window_pane *wp) |
1253 | 0 | { |
1254 | 0 | struct window *w = wp->window; |
1255 | 0 | static char flags[32]; |
1256 | 0 | int pos = 0; |
1257 | |
|
1258 | 0 | if (wp == w->active) |
1259 | 0 | flags[pos++] = '*'; |
1260 | 0 | if (wp == TAILQ_FIRST(&w->last_panes)) |
1261 | 0 | flags[pos++] = '-'; |
1262 | 0 | if (wp->flags & PANE_ZOOMED) |
1263 | 0 | flags[pos++] = 'Z'; |
1264 | 0 | if (window_pane_is_floating(wp)) |
1265 | 0 | flags[pos++] = 'F'; |
1266 | 0 | if (wp == w->modal) |
1267 | 0 | flags[pos++] = 'O'; |
1268 | 0 | flags[pos] = '\0'; |
1269 | 0 | return (flags); |
1270 | 0 | } |
1271 | | |
1272 | | struct window_pane * |
1273 | | window_pane_find_by_id_str(const char *s) |
1274 | 0 | { |
1275 | 0 | const char *errstr; |
1276 | 0 | u_int id; |
1277 | |
|
1278 | 0 | if (*s != '%') |
1279 | 0 | return (NULL); |
1280 | | |
1281 | 0 | id = strtonum(s + 1, 0, UINT_MAX, &errstr); |
1282 | 0 | if (errstr != NULL) |
1283 | 0 | return (NULL); |
1284 | 0 | return (window_pane_find_by_id(id)); |
1285 | 0 | } |
1286 | | |
1287 | | struct window_pane * |
1288 | | window_pane_find_by_id(u_int id) |
1289 | 0 | { |
1290 | 0 | struct window_pane wp; |
1291 | |
|
1292 | 0 | wp.id = id; |
1293 | 0 | return (RB_FIND(window_pane_tree, &all_window_panes, &wp)); |
1294 | 0 | } |
1295 | | |
1296 | | static struct window_pane * |
1297 | | window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) |
1298 | 0 | { |
1299 | 0 | struct window_pane *wp; |
1300 | 0 | char host[HOST_NAME_MAX + 1]; |
1301 | |
|
1302 | 0 | wp = xcalloc(1, sizeof *wp); |
1303 | 0 | wp->references = 1; |
1304 | 0 | wp->window = w; |
1305 | 0 | wp->options = options_create(w->options); |
1306 | 0 | wp->flags = PANE_STYLECHANGED; |
1307 | 0 | wp->cmd_status = -1; |
1308 | |
|
1309 | 0 | wp->id = next_window_pane_id++; |
1310 | 0 | RB_INSERT(window_pane_tree, &all_window_panes, wp); |
1311 | |
|
1312 | 0 | wp->fd = -1; |
1313 | |
|
1314 | 0 | TAILQ_INIT(&wp->modes); |
1315 | |
|
1316 | 0 | TAILQ_INIT (&wp->resize_queue); |
1317 | |
|
1318 | 0 | wp->sx = sx; |
1319 | 0 | wp->sy = sy; |
1320 | |
|
1321 | 0 | wp->pipe_fd = -1; |
1322 | |
|
1323 | 0 | wp->control_bg = -1; |
1324 | 0 | wp->control_fg = -1; |
1325 | |
|
1326 | 0 | style_set_scrollbar_style_from_option(&wp->scrollbar_style, |
1327 | 0 | wp->options); |
1328 | |
|
1329 | 0 | colour_palette_init(&wp->palette); |
1330 | 0 | colour_palette_from_option(&wp->palette, wp->options); |
1331 | |
|
1332 | 0 | screen_init(&wp->base, sx, sy, hlimit); |
1333 | 0 | wp->screen = &wp->base; |
1334 | 0 | window_pane_default_cursor(wp); |
1335 | |
|
1336 | 0 | screen_init(&wp->status_screen, 1, 1, 0); |
1337 | 0 | style_ranges_init(&wp->border_status_line.ranges); |
1338 | 0 | evtimer_set(&wp->sb_auto_timer, window_pane_scrollbar_timer, wp); |
1339 | |
|
1340 | 0 | if (gethostname(host, sizeof host) == 0) |
1341 | 0 | screen_set_title(&wp->base, host, 0); |
1342 | |
|
1343 | 0 | return (wp); |
1344 | 0 | } |
1345 | | |
1346 | | void |
1347 | | window_pane_wait_finish(struct window_pane *wp) |
1348 | 0 | { |
1349 | 0 | struct cmdq_item *item = wp->wait_item; |
1350 | 0 | struct client *c; |
1351 | 0 | int retval = 0; |
1352 | |
|
1353 | 0 | if (item == NULL) |
1354 | 0 | return; |
1355 | 0 | wp->wait_item = NULL; |
1356 | |
|
1357 | 0 | if (wp->flags & PANE_STATUSREADY) { |
1358 | 0 | if (WIFEXITED(wp->status)) |
1359 | 0 | retval = WEXITSTATUS(wp->status); |
1360 | 0 | else if (WIFSIGNALED(wp->status)) |
1361 | 0 | retval = WTERMSIG(wp->status) + 128; |
1362 | 0 | } |
1363 | |
|
1364 | 0 | c = cmdq_get_client(item); |
1365 | 0 | if (c != NULL && c->session == NULL) |
1366 | 0 | c->retval = retval; |
1367 | 0 | cmdq_continue(item); |
1368 | 0 | } |
1369 | | |
1370 | | static void |
1371 | | window_pane_free_modes(struct window_pane *wp) |
1372 | 0 | { |
1373 | 0 | struct window_mode_entry *wme; |
1374 | |
|
1375 | 0 | while (!TAILQ_EMPTY(&wp->modes)) { |
1376 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1377 | 0 | TAILQ_REMOVE(&wp->modes, wme, entry); |
1378 | 0 | wme->mode->free(wme); |
1379 | 0 | free(wme); |
1380 | 0 | } |
1381 | |
|
1382 | 0 | wp->screen = &wp->base; |
1383 | 0 | } |
1384 | | |
1385 | | static void |
1386 | | window_pane_scrollbar_timer(__unused int fd, __unused short events, void *arg) |
1387 | 0 | { |
1388 | 0 | struct window_pane *wp = arg; |
1389 | |
|
1390 | 0 | wp->sb_auto_hover = 0; |
1391 | 0 | window_pane_scrollbar_hide(wp); |
1392 | 0 | } |
1393 | | |
1394 | | static int |
1395 | | window_pane_scrollbar_auto_hide(struct window_pane *wp) |
1396 | 0 | { |
1397 | 0 | return (wp->window->sb == PANE_SCROLLBARS_MODAL || |
1398 | 0 | wp->window->sb == PANE_SCROLLBARS_AUTOHIDE); |
1399 | 0 | } |
1400 | | |
1401 | | int |
1402 | | window_pane_scrollbar_overlay_visible(struct window_pane *wp) |
1403 | 0 | { |
1404 | 0 | return (window_pane_scrollbar_overlay(wp) && |
1405 | 0 | window_pane_scrollbar_visible(wp)); |
1406 | 0 | } |
1407 | | |
1408 | | void |
1409 | | window_pane_scrollbar_redraw(struct window_pane *wp) |
1410 | 0 | { |
1411 | 0 | if (window_pane_scrollbar_overlay_visible(wp)) { |
1412 | 0 | wp->flags |= PANE_REDRAW; |
1413 | 0 | return; |
1414 | 0 | } |
1415 | 0 | wp->flags |= PANE_REDRAWSCROLLBAR; |
1416 | 0 | } |
1417 | | |
1418 | | static void |
1419 | | window_pane_scrollbar_redraw_visibility(struct window_pane *wp) |
1420 | 0 | { |
1421 | 0 | redraw_invalidate_scene(wp->window); |
1422 | 0 | wp->flags |= PANE_REDRAW; |
1423 | 0 | server_redraw_window(wp->window); |
1424 | 0 | } |
1425 | | |
1426 | | static void |
1427 | | window_pane_destroy(struct window_pane *wp) |
1428 | 0 | { |
1429 | 0 | window_pane_wait_finish(wp); |
1430 | 0 | spawn_editor_finish(wp); |
1431 | |
|
1432 | 0 | RB_REMOVE(window_pane_tree, &all_window_panes, wp); |
1433 | 0 | wp->flags |= PANE_DESTROYED; |
1434 | 0 | window_pane_clear_prompt(wp); |
1435 | |
|
1436 | 0 | window_pane_free_modes(wp); |
1437 | 0 | screen_write_clear_dirty(wp); |
1438 | |
|
1439 | 0 | if (wp->fd != -1) { |
1440 | | #ifdef HAVE_UTEMPTER |
1441 | | utempter_remove_record(wp->fd); |
1442 | | kill(getpid(), SIGCHLD); |
1443 | | #endif |
1444 | 0 | bufferevent_free(wp->event); |
1445 | 0 | wp->event = NULL; |
1446 | 0 | close(wp->fd); |
1447 | 0 | wp->fd = -1; |
1448 | 0 | } |
1449 | 0 | if (wp->ictx != NULL) { |
1450 | 0 | input_free(wp->ictx); |
1451 | 0 | wp->ictx = NULL; |
1452 | 0 | } |
1453 | |
|
1454 | 0 | if (wp->pipe_fd != -1) { |
1455 | 0 | bufferevent_free(wp->pipe_event); |
1456 | 0 | wp->pipe_event = NULL; |
1457 | 0 | close(wp->pipe_fd); |
1458 | 0 | wp->pipe_fd = -1; |
1459 | 0 | } |
1460 | |
|
1461 | 0 | if (event_initialized(&wp->resize_timer)) |
1462 | 0 | event_del(&wp->resize_timer); |
1463 | 0 | if (event_initialized(&wp->sync_timer)) |
1464 | 0 | event_del(&wp->sync_timer); |
1465 | 0 | if (event_initialized(&wp->sb_auto_timer)) |
1466 | 0 | event_del(&wp->sb_auto_timer); |
1467 | 0 | window_pane_clear_resizes(wp, NULL); |
1468 | |
|
1469 | 0 | window_pane_remove_ref(wp, __func__); |
1470 | 0 | } |
1471 | | |
1472 | | static void |
1473 | | window_pane_free(struct window_pane *wp) |
1474 | 0 | { |
1475 | 0 | log_debug("pane %%%u freed (%d references)", wp->id, wp->references); |
1476 | |
|
1477 | 0 | free(wp->searchstr); |
1478 | |
|
1479 | 0 | screen_free(&wp->status_screen); |
1480 | 0 | screen_free(&wp->base); |
1481 | |
|
1482 | 0 | options_free(wp->options); |
1483 | 0 | free((void *)wp->cwd); |
1484 | 0 | free(wp->shell); |
1485 | 0 | cmd_free_argv(wp->argc, wp->argv); |
1486 | 0 | colour_palette_free(&wp->palette); |
1487 | 0 | style_ranges_free(&wp->border_status_line.ranges); |
1488 | 0 | free(wp); |
1489 | 0 | } |
1490 | | |
1491 | | static void |
1492 | | window_pane_read_callback(__unused struct bufferevent *bufev, void *data) |
1493 | 0 | { |
1494 | 0 | struct window_pane *wp = data; |
1495 | 0 | struct evbuffer *evb = wp->event->input; |
1496 | 0 | struct window_pane_offset *wpo = &wp->pipe_offset; |
1497 | 0 | size_t size = EVBUFFER_LENGTH(evb); |
1498 | 0 | char *new_data; |
1499 | 0 | size_t new_size; |
1500 | 0 | struct client *c; |
1501 | |
|
1502 | 0 | if (wp->pipe_fd != -1) { |
1503 | 0 | new_data = window_pane_get_new_data(wp, wpo, &new_size); |
1504 | 0 | if (new_size > 0) { |
1505 | 0 | bufferevent_write(wp->pipe_event, new_data, new_size); |
1506 | 0 | window_pane_update_used_data(wp, wpo, new_size); |
1507 | 0 | } |
1508 | 0 | } |
1509 | |
|
1510 | 0 | log_debug("%%%u has %zu bytes", wp->id, size); |
1511 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1512 | 0 | if (c->session != NULL && (c->flags & CLIENT_CONTROL)) |
1513 | 0 | control_write_output(c, wp); |
1514 | 0 | } |
1515 | 0 | input_parse_pane(wp); |
1516 | 0 | bufferevent_disable(wp->event, EV_READ); |
1517 | 0 | } |
1518 | | |
1519 | | static void |
1520 | | window_pane_error_callback(__unused struct bufferevent *bufev, |
1521 | | __unused short what, void *data) |
1522 | 0 | { |
1523 | 0 | struct window_pane *wp = data; |
1524 | |
|
1525 | 0 | log_debug("%%%u error", wp->id); |
1526 | 0 | wp->flags |= PANE_EXITED; |
1527 | |
|
1528 | 0 | if (window_pane_destroy_ready(wp)) |
1529 | 0 | server_destroy_pane(wp, 1); |
1530 | 0 | } |
1531 | | |
1532 | | void |
1533 | | window_pane_set_event(struct window_pane *wp) |
1534 | 0 | { |
1535 | 0 | setblocking(wp->fd, 0); |
1536 | |
|
1537 | 0 | wp->event = bufferevent_new(wp->fd, window_pane_read_callback, |
1538 | 0 | NULL, window_pane_error_callback, wp); |
1539 | 0 | if (wp->event == NULL) |
1540 | 0 | fatalx("out of memory"); |
1541 | 0 | wp->ictx = input_init(wp, wp->event, &wp->palette, NULL); |
1542 | |
|
1543 | 0 | bufferevent_enable(wp->event, EV_READ|EV_WRITE); |
1544 | 0 | } |
1545 | | |
1546 | | void |
1547 | | window_pane_clear_resizes(struct window_pane *wp, |
1548 | | struct window_pane_resize *except) |
1549 | 0 | { |
1550 | 0 | struct window_pane_resize *r, *r1; |
1551 | |
|
1552 | 0 | TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { |
1553 | 0 | if (r == except) |
1554 | 0 | continue; |
1555 | 0 | TAILQ_REMOVE(&wp->resize_queue, r, entry); |
1556 | 0 | free(r); |
1557 | 0 | } |
1558 | 0 | } |
1559 | | |
1560 | | void |
1561 | | window_pane_resize(struct window_pane *wp, u_int sx, u_int sy) |
1562 | 0 | { |
1563 | 0 | struct window_mode_entry *wme; |
1564 | 0 | struct window_pane_resize *r; |
1565 | 0 | struct event_payload *ep; |
1566 | 0 | struct cmd_find_state fs; |
1567 | |
|
1568 | 0 | if (sx == wp->sx && sy == wp->sy) |
1569 | 0 | return; |
1570 | | |
1571 | 0 | screen_write_stop_sync(wp); |
1572 | |
|
1573 | 0 | r = xmalloc(sizeof *r); |
1574 | 0 | r->sx = sx; |
1575 | 0 | r->sy = sy; |
1576 | 0 | r->osx = wp->sx; |
1577 | 0 | r->osy = wp->sy; |
1578 | 0 | TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry); |
1579 | |
|
1580 | 0 | wp->sx = sx; |
1581 | 0 | wp->sy = sy; |
1582 | |
|
1583 | 0 | log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy); |
1584 | 0 | screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL); |
1585 | |
|
1586 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1587 | 0 | if (wme != NULL && wme->mode->resize != NULL) |
1588 | 0 | wme->mode->resize(wme, sx, sy); |
1589 | |
|
1590 | 0 | ep = event_payload_create(); |
1591 | 0 | cmd_find_from_pane(&fs, wp, 0); |
1592 | 0 | event_payload_set_target(ep, &fs); |
1593 | 0 | event_payload_set_pane(ep, "pane", wp); |
1594 | 0 | event_payload_set_window(ep, "window", wp->window); |
1595 | 0 | event_payload_set_uint(ep, "width", sx); |
1596 | 0 | event_payload_set_uint(ep, "height", sy); |
1597 | 0 | event_payload_set_uint(ep, "old_width", r->osx); |
1598 | 0 | event_payload_set_uint(ep, "old_height", r->osy); |
1599 | 0 | events_fire("pane-resized", ep); |
1600 | 0 | } |
1601 | | |
1602 | | int |
1603 | | window_pane_set_mode(struct window_pane *wp, struct window_pane *swp, |
1604 | | const struct window_mode *mode, struct cmdq_item *item, |
1605 | | struct cmd_find_state *fs, struct args *args) |
1606 | 0 | { |
1607 | 0 | struct window_mode_entry *wme; |
1608 | 0 | struct window *w = wp->window; |
1609 | 0 | const char *name = mode->name, *oname = NULL; |
1610 | |
|
1611 | 0 | if (!TAILQ_EMPTY(&wp->modes)) { |
1612 | 0 | if (TAILQ_FIRST(&wp->modes)->mode == mode) |
1613 | 0 | return (1); |
1614 | 0 | if (TAILQ_FIRST(&wp->modes)->mode->flags & WINDOW_MODE_NO_STACK) |
1615 | 0 | window_pane_reset_mode(wp); |
1616 | 0 | } |
1617 | 0 | if (!TAILQ_EMPTY(&wp->modes)) |
1618 | 0 | oname = TAILQ_FIRST(&wp->modes)->mode->name; |
1619 | |
|
1620 | 0 | TAILQ_FOREACH(wme, &wp->modes, entry) { |
1621 | 0 | if (wme->mode == mode) |
1622 | 0 | break; |
1623 | 0 | } |
1624 | 0 | if (wme != NULL) { |
1625 | 0 | TAILQ_REMOVE(&wp->modes, wme, entry); |
1626 | 0 | TAILQ_INSERT_HEAD(&wp->modes, wme, entry); |
1627 | 0 | } else { |
1628 | 0 | wme = xcalloc(1, sizeof *wme); |
1629 | 0 | wme->wp = wp; |
1630 | 0 | wme->swp = swp; |
1631 | 0 | wme->mode = mode; |
1632 | 0 | wme->prefix = 1; |
1633 | 0 | TAILQ_INSERT_HEAD(&wp->modes, wme, entry); |
1634 | 0 | wme->screen = wme->mode->init(wme, item, fs, args); |
1635 | 0 | if (wme->screen == NULL) { |
1636 | 0 | TAILQ_REMOVE(&wp->modes, wme, entry); |
1637 | 0 | free(wme); |
1638 | 0 | return (1); |
1639 | 0 | } |
1640 | 0 | } |
1641 | 0 | wme->kill = args != NULL ? args_has(args, 'k') : 0; |
1642 | 0 | wp->screen = wme->screen; |
1643 | |
|
1644 | 0 | wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED); |
1645 | 0 | layout_fix_panes(w, NULL); |
1646 | |
|
1647 | 0 | server_redraw_window_borders(wp->window); |
1648 | 0 | server_status_window(wp->window); |
1649 | |
|
1650 | 0 | window_fire_pane_mode_changed("pane-mode-entered", wp, oname, name, 1); |
1651 | 0 | window_fire_pane_mode_changed("pane-mode-changed", wp, oname, name, 1); |
1652 | |
|
1653 | 0 | return (0); |
1654 | 0 | } |
1655 | | |
1656 | | void |
1657 | | window_pane_reset_mode(struct window_pane *wp) |
1658 | 0 | { |
1659 | 0 | struct window_mode_entry *wme, *next; |
1660 | 0 | struct window *w = wp->window; |
1661 | 0 | int kill; |
1662 | 0 | const char *name, *p; |
1663 | |
|
1664 | 0 | if (TAILQ_EMPTY(&wp->modes)) |
1665 | 0 | return; |
1666 | | |
1667 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1668 | 0 | p = wme->mode->name; |
1669 | 0 | kill = wme->kill; |
1670 | 0 | TAILQ_REMOVE(&wp->modes, wme, entry); |
1671 | 0 | wme->mode->free(wme); |
1672 | 0 | free(wme); |
1673 | |
|
1674 | 0 | next = TAILQ_FIRST(&wp->modes); |
1675 | 0 | if (next == NULL) { |
1676 | 0 | wp->flags &= ~PANE_UNSEENCHANGES; |
1677 | 0 | log_debug("%s: no next mode", __func__); |
1678 | 0 | wp->screen = &wp->base; |
1679 | 0 | } else { |
1680 | 0 | log_debug("%s: next mode is %s", __func__, next->mode->name); |
1681 | 0 | wp->screen = next->screen; |
1682 | 0 | if (next->mode->resize != NULL) |
1683 | 0 | next->mode->resize(next, wp->sx, wp->sy); |
1684 | 0 | } |
1685 | 0 | name = (next == NULL ? NULL : next->mode->name); |
1686 | |
|
1687 | 0 | wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED); |
1688 | 0 | layout_fix_panes(w, NULL); |
1689 | |
|
1690 | 0 | server_redraw_window_borders(wp->window); |
1691 | 0 | server_status_window(wp->window); |
1692 | |
|
1693 | 0 | window_fire_pane_mode_changed("pane-mode-exited", wp, p, name, 0); |
1694 | 0 | window_fire_pane_mode_changed("pane-mode-changed", wp, p, name, 0); |
1695 | |
|
1696 | 0 | if (kill) |
1697 | 0 | server_kill_pane(wp); |
1698 | 0 | } |
1699 | | |
1700 | | /* Reset all modes. */ |
1701 | | void |
1702 | | window_pane_reset_mode_all(struct window_pane *wp) |
1703 | 0 | { |
1704 | 0 | while (!TAILQ_EMPTY(&wp->modes)) |
1705 | 0 | window_pane_reset_mode(wp); |
1706 | 0 | } |
1707 | | |
1708 | | /* Prompt input callback. */ |
1709 | | static enum prompt_result |
1710 | | window_pane_prompt_input_callback(void *data, const char *s, |
1711 | | enum prompt_key_result key) |
1712 | 0 | { |
1713 | 0 | struct window_pane_prompt *wpp = data; |
1714 | |
|
1715 | 0 | if (wpp->inputcb != NULL) |
1716 | 0 | return (wpp->inputcb(wpp->c, wpp->data, s, key)); |
1717 | 0 | return (PROMPT_CLOSE); |
1718 | 0 | } |
1719 | | |
1720 | | /* Prompt free callback. */ |
1721 | | static void |
1722 | | window_pane_prompt_free_callback(void *data) |
1723 | 0 | { |
1724 | 0 | struct window_pane_prompt *wpp = data; |
1725 | 0 | struct window_pane *wp; |
1726 | |
|
1727 | 0 | wp = window_pane_find_by_id(wpp->wp_id); |
1728 | 0 | if (wp != NULL && wp->prompt_data == wpp) |
1729 | 0 | wp->prompt_data = NULL; |
1730 | 0 | if (wpp->freecb != NULL) |
1731 | 0 | wpp->freecb(wpp->data); |
1732 | 0 | free(wpp); |
1733 | 0 | } |
1734 | | |
1735 | | /* Open a prompt owned by a pane, drawn over the pane instead of the status. */ |
1736 | | void |
1737 | | window_pane_set_prompt(struct window_pane *wp, struct client *c, |
1738 | | struct cmd_find_state *fs, const char *msg, const char *input, |
1739 | | status_prompt_input_cb inputcb, prompt_free_cb freecb, void *data, |
1740 | | int flags, enum prompt_type type) |
1741 | 0 | { |
1742 | 0 | struct session *s = NULL; |
1743 | 0 | struct prompt_create_data pd; |
1744 | 0 | struct window_pane_prompt *wpp; |
1745 | |
|
1746 | 0 | if (c != NULL) |
1747 | 0 | s = c->session; |
1748 | |
|
1749 | 0 | window_pane_clear_prompt(wp); |
1750 | |
|
1751 | 0 | wpp = xcalloc(1, sizeof *wpp); |
1752 | 0 | wpp->wp_id = wp->id; |
1753 | 0 | wpp->c = c; |
1754 | 0 | wpp->inputcb = inputcb; |
1755 | 0 | wpp->freecb = freecb; |
1756 | 0 | wpp->data = data; |
1757 | 0 | wpp->type = type; |
1758 | |
|
1759 | 0 | memset(&pd, 0, sizeof pd); |
1760 | 0 | prompt_set_options(&pd, s); |
1761 | 0 | pd.fs = fs; |
1762 | 0 | pd.prompt = msg; |
1763 | 0 | pd.input = input; |
1764 | 0 | pd.type = type; |
1765 | 0 | pd.flags = flags; |
1766 | 0 | pd.inputcb = window_pane_prompt_input_callback; |
1767 | 0 | pd.freecb = window_pane_prompt_free_callback; |
1768 | 0 | pd.data = wpp; |
1769 | |
|
1770 | 0 | wp->prompt = prompt_create(&pd); |
1771 | 0 | wp->prompt_data = wpp; |
1772 | 0 | wp->flags |= PANE_REDRAW; |
1773 | |
|
1774 | 0 | prompt_incremental_start(wp->prompt); |
1775 | 0 | window_fire_pane_prompt("pane-prompt-opened", wp, type); |
1776 | 0 | } |
1777 | | |
1778 | | /* Close a pane prompt. */ |
1779 | | void |
1780 | | window_pane_clear_prompt(struct window_pane *wp) |
1781 | 0 | { |
1782 | 0 | struct prompt *prompt = wp->prompt; |
1783 | 0 | struct window_pane_prompt *wpp = wp->prompt_data; |
1784 | 0 | enum prompt_type type = PROMPT_TYPE_INVALID; |
1785 | |
|
1786 | 0 | if (prompt != NULL) { |
1787 | 0 | if (wpp != NULL) |
1788 | 0 | type = wpp->type; |
1789 | |
|
1790 | 0 | wp->prompt = NULL; |
1791 | 0 | prompt_free(prompt); |
1792 | 0 | wp->flags |= PANE_REDRAW; |
1793 | |
|
1794 | 0 | if (~wp->flags & PANE_DESTROYED) |
1795 | 0 | window_fire_pane_prompt("pane-prompt-closed", wp, type); |
1796 | 0 | } |
1797 | 0 | } |
1798 | | |
1799 | | /* Does this pane have an open prompt? */ |
1800 | | int |
1801 | | window_pane_has_prompt(struct window_pane *wp) |
1802 | 0 | { |
1803 | 0 | return (wp->prompt != NULL); |
1804 | 0 | } |
1805 | | |
1806 | | /* Replace the message and input of an open pane prompt. */ |
1807 | | void |
1808 | | window_pane_update_prompt(struct window_pane *wp, const char *msg, |
1809 | | const char *input) |
1810 | 0 | { |
1811 | 0 | if (wp->prompt != NULL) { |
1812 | 0 | prompt_update(wp->prompt, msg, input); |
1813 | 0 | wp->flags |= PANE_REDRAW; |
1814 | 0 | } |
1815 | 0 | } |
1816 | | |
1817 | | /* |
1818 | | * Pass a key to a pane prompt. The client is set transiently for the duration |
1819 | | * of the key in case the prompt or pane is destroyed by the callback. |
1820 | | */ |
1821 | | enum prompt_key_result |
1822 | | window_pane_prompt_key(struct window_pane *wp, struct client *c, key_code key, |
1823 | | struct mouse_event *m) |
1824 | 0 | { |
1825 | 0 | struct prompt *prompt = wp->prompt; |
1826 | 0 | struct window_pane_prompt *wpp = wp->prompt_data; |
1827 | 0 | enum prompt_key_result result; |
1828 | 0 | u_int wp_id = wp->id, x, y, py; |
1829 | 0 | int redraw = 0; |
1830 | |
|
1831 | 0 | if (prompt == NULL) |
1832 | 0 | return (PROMPT_KEY_NOT_HANDLED); |
1833 | | |
1834 | 0 | if (wpp != NULL) |
1835 | 0 | wpp->c = c; |
1836 | 0 | if (KEYC_IS_MOUSE(key)) { |
1837 | 0 | if (m == NULL || |
1838 | 0 | MOUSE_BUTTONS(m->b) != MOUSE_BUTTON_1 || |
1839 | 0 | MOUSE_DRAG(m->b) || |
1840 | 0 | MOUSE_RELEASE(m->b) || |
1841 | 0 | cmd_mouse_at(wp, m, &x, &y, 0) != 0) |
1842 | 0 | result = PROMPT_KEY_NOT_HANDLED; |
1843 | 0 | else { |
1844 | 0 | if (c != NULL && status_at_line(c) == 0) |
1845 | 0 | py = 0; |
1846 | 0 | else |
1847 | 0 | py = wp->sy - 1; |
1848 | 0 | if (y == py) { |
1849 | 0 | result = prompt_mouse(prompt, x, 0, wp->sx, |
1850 | 0 | &redraw); |
1851 | 0 | } else |
1852 | 0 | result = PROMPT_KEY_NOT_HANDLED; |
1853 | 0 | } |
1854 | 0 | } else |
1855 | 0 | result = prompt_key(prompt, key, &redraw); |
1856 | |
|
1857 | 0 | wp = window_pane_find_by_id(wp_id); |
1858 | 0 | if (wp == NULL) |
1859 | 0 | return (result); |
1860 | 0 | if (wpp != NULL && wp->prompt_data == wpp) |
1861 | 0 | wpp->c = NULL; |
1862 | | |
1863 | | /* |
1864 | | * Only an explicit close or the prompt marking itself closed ends it; |
1865 | | * cursor movement and editing keep it open. |
1866 | | */ |
1867 | 0 | if (wp->prompt == prompt && |
1868 | 0 | (result == PROMPT_KEY_CLOSE || prompt_closed(prompt))) |
1869 | 0 | window_pane_clear_prompt(wp); |
1870 | |
|
1871 | 0 | if (redraw || wp->prompt != prompt) |
1872 | 0 | wp->flags |= PANE_REDRAW; |
1873 | |
|
1874 | 0 | return (result); |
1875 | 0 | } |
1876 | | |
1877 | | static void |
1878 | | window_pane_copy_paste(struct window_pane *wp, char *buf, size_t len) |
1879 | 0 | { |
1880 | 0 | struct window_pane *loop; |
1881 | |
|
1882 | 0 | TAILQ_FOREACH(loop, &wp->window->panes, entry) { |
1883 | 0 | if (loop != wp && |
1884 | 0 | TAILQ_EMPTY(&loop->modes) && |
1885 | 0 | loop->fd != -1 && |
1886 | 0 | (~loop->flags & PANE_INPUTOFF) && |
1887 | 0 | window_pane_is_visible(loop) && |
1888 | 0 | options_get_number(loop->options, "synchronize-panes")) { |
1889 | 0 | log_debug("%s: %.*s", __func__, (int)len, buf); |
1890 | 0 | bufferevent_write(loop->event, buf, len); |
1891 | 0 | } |
1892 | 0 | } |
1893 | 0 | } |
1894 | | |
1895 | | static void |
1896 | | window_pane_copy_key(struct window_pane *wp, key_code key) |
1897 | 0 | { |
1898 | 0 | struct window_pane *loop; |
1899 | |
|
1900 | 0 | TAILQ_FOREACH(loop, &wp->window->panes, entry) { |
1901 | 0 | if (loop != wp && |
1902 | 0 | TAILQ_EMPTY(&loop->modes) && |
1903 | 0 | loop->fd != -1 && |
1904 | 0 | (~loop->flags & PANE_INPUTOFF) && |
1905 | 0 | window_pane_is_visible(loop) && |
1906 | 0 | options_get_number(loop->options, "synchronize-panes")) |
1907 | 0 | input_key_pane(loop, key, NULL); |
1908 | 0 | } |
1909 | 0 | } |
1910 | | |
1911 | | void |
1912 | | window_pane_paste(struct window_pane *wp, key_code key, char *buf, size_t len) |
1913 | 0 | { |
1914 | 0 | if (!TAILQ_EMPTY(&wp->modes)) |
1915 | 0 | return; |
1916 | | |
1917 | 0 | if (wp->fd == -1 || wp->flags & PANE_INPUTOFF) |
1918 | 0 | return; |
1919 | | |
1920 | 0 | if (KEYC_IS_PASTE(key) && (~wp->screen->mode & MODE_BRACKETPASTE)) |
1921 | 0 | return; |
1922 | | |
1923 | 0 | log_debug("%s: %.*s", __func__, (int)len, buf); |
1924 | 0 | bufferevent_write(wp->event, buf, len); |
1925 | |
|
1926 | 0 | if (options_get_number(wp->options, "synchronize-panes")) |
1927 | 0 | window_pane_copy_paste(wp, buf, len); |
1928 | 0 | } |
1929 | | |
1930 | | int |
1931 | | window_pane_key(struct window_pane *wp, struct client *c, struct session *s, |
1932 | | struct winlink *wl, key_code key, struct mouse_event *m) |
1933 | 0 | { |
1934 | 0 | struct window_mode_entry *wme; |
1935 | |
|
1936 | 0 | if (KEYC_IS_MOUSE(key) && m == NULL) |
1937 | 0 | return (-1); |
1938 | | |
1939 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1940 | 0 | if (wme != NULL) { |
1941 | | /* |
1942 | | * No mode uses mouse motion events, so drop them here rather |
1943 | | * than passing them on and causing a redraw on every movement. |
1944 | | */ |
1945 | 0 | if (KEYC_IS_TYPE(key, KEYC_TYPE_MOUSEMOVE)) |
1946 | 0 | return (0); |
1947 | 0 | if (wme->mode->key != NULL && c != NULL) { |
1948 | 0 | key &= ~KEYC_MASK_FLAGS; |
1949 | 0 | wme->mode->key(wme, c, s, wl, key, m); |
1950 | 0 | } |
1951 | 0 | return (0); |
1952 | 0 | } |
1953 | | |
1954 | 0 | if (wp->fd == -1 || wp->flags & PANE_INPUTOFF) |
1955 | 0 | return (0); |
1956 | | |
1957 | 0 | if (input_key_pane(wp, key, m) != 0) |
1958 | 0 | return (-1); |
1959 | | |
1960 | 0 | if (KEYC_IS_MOUSE(key)) |
1961 | 0 | return (0); |
1962 | 0 | if (options_get_number(wp->options, "synchronize-panes")) |
1963 | 0 | window_pane_copy_key(wp, key); |
1964 | 0 | return (0); |
1965 | 0 | } |
1966 | | |
1967 | | int |
1968 | | window_pane_is_visible(struct window_pane *wp) |
1969 | 0 | { |
1970 | 0 | if (~wp->window->flags & WINDOW_ZOOMED) |
1971 | 0 | return (1); |
1972 | 0 | return (wp == wp->window->active); |
1973 | 0 | } |
1974 | | |
1975 | | int |
1976 | | window_pane_exited(struct window_pane *wp) |
1977 | 0 | { |
1978 | 0 | return (wp->fd == -1 || (wp->flags & PANE_EXITED)); |
1979 | 0 | } |
1980 | | |
1981 | | u_int |
1982 | | window_pane_search(struct window_pane *wp, const char *term, int regex, |
1983 | | int ignore) |
1984 | 0 | { |
1985 | 0 | struct screen *s = &wp->base; |
1986 | 0 | regex_t r; |
1987 | 0 | char *new = NULL, *line; |
1988 | 0 | u_int i; |
1989 | 0 | int flags = 0, found; |
1990 | 0 | size_t n; |
1991 | |
|
1992 | 0 | if (!regex) { |
1993 | 0 | if (ignore) |
1994 | 0 | flags |= FNM_CASEFOLD; |
1995 | 0 | xasprintf(&new, "*%s*", term); |
1996 | 0 | } else { |
1997 | 0 | if (ignore) |
1998 | 0 | flags |= REG_ICASE; |
1999 | 0 | if (regcomp(&r, term, flags|REG_EXTENDED) != 0) |
2000 | 0 | return (0); |
2001 | 0 | } |
2002 | | |
2003 | 0 | for (i = 0; i < screen_size_y(s); i++) { |
2004 | 0 | line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s)); |
2005 | 0 | for (n = strlen(line); n > 0; n--) { |
2006 | 0 | if (!isspace((u_char)line[n - 1])) |
2007 | 0 | break; |
2008 | 0 | line[n - 1] = '\0'; |
2009 | 0 | } |
2010 | 0 | log_debug("%s: %s", __func__, line); |
2011 | 0 | if (!regex) |
2012 | 0 | found = (fnmatch(new, line, flags) == 0); |
2013 | 0 | else |
2014 | 0 | found = (regexec(&r, line, 0, NULL, 0) == 0); |
2015 | 0 | free(line); |
2016 | 0 | if (found) |
2017 | 0 | break; |
2018 | 0 | } |
2019 | 0 | if (!regex) |
2020 | 0 | free(new); |
2021 | 0 | else |
2022 | 0 | regfree(&r); |
2023 | |
|
2024 | 0 | if (i == screen_size_y(s)) |
2025 | 0 | return (0); |
2026 | 0 | return (i + 1); |
2027 | 0 | } |
2028 | | |
2029 | | /* Get MRU pane from a list. */ |
2030 | | static struct window_pane * |
2031 | | window_pane_choose_best(struct window_pane **list, u_int size) |
2032 | 0 | { |
2033 | 0 | struct window_pane *next, *best; |
2034 | 0 | u_int i; |
2035 | |
|
2036 | 0 | if (size == 0) |
2037 | 0 | return (NULL); |
2038 | | |
2039 | 0 | best = list[0]; |
2040 | 0 | for (i = 1; i < size; i++) { |
2041 | 0 | next = list[i]; |
2042 | 0 | if (next->active_point > best->active_point) |
2043 | 0 | best = next; |
2044 | 0 | } |
2045 | 0 | return (best); |
2046 | 0 | } |
2047 | | |
2048 | | /* |
2049 | | * Get full size and offset of a window pane including the area of the |
2050 | | * scrollbars if they were visible but not including the border(s). |
2051 | | */ |
2052 | | static void |
2053 | | window_pane_full_size_offset(struct window_pane *wp, int *xoff, int *yoff, |
2054 | | u_int *sx, u_int *sy) |
2055 | 0 | { |
2056 | 0 | struct window *w = wp->window; |
2057 | 0 | u_int sb_w; |
2058 | |
|
2059 | 0 | if (window_pane_scrollbar_reserve(wp)) |
2060 | 0 | sb_w = wp->scrollbar_style.width + wp->scrollbar_style.pad; |
2061 | 0 | else |
2062 | 0 | sb_w = 0; |
2063 | 0 | if (w->sb_pos == PANE_SCROLLBARS_LEFT) { |
2064 | 0 | *xoff = wp->xoff - sb_w; |
2065 | 0 | *sx = wp->sx + sb_w; |
2066 | 0 | } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */ |
2067 | 0 | *xoff = wp->xoff; |
2068 | 0 | *sx = wp->sx + sb_w; |
2069 | 0 | } |
2070 | 0 | *yoff = wp->yoff; |
2071 | 0 | *sy = wp->sy; |
2072 | 0 | } |
2073 | | |
2074 | | /* |
2075 | | * Find the pane directly above another. We build a list of those adjacent to |
2076 | | * top edge and then choose the best. |
2077 | | */ |
2078 | | struct window_pane * |
2079 | | window_pane_find_up(struct window_pane *wp) |
2080 | 0 | { |
2081 | 0 | struct window *w; |
2082 | 0 | struct window_pane *next, *best, **list; |
2083 | 0 | int edge, left, right, end, status, found; |
2084 | 0 | int xoff, yoff; |
2085 | 0 | u_int size, sx, sy; |
2086 | |
|
2087 | 0 | if (wp == NULL) |
2088 | 0 | return (NULL); |
2089 | 0 | w = wp->window; |
2090 | 0 | status = window_get_pane_status(w); |
2091 | |
|
2092 | 0 | list = NULL; |
2093 | 0 | size = 0; |
2094 | |
|
2095 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
2096 | |
|
2097 | 0 | edge = yoff; |
2098 | 0 | if (status == PANE_STATUS_TOP) { |
2099 | 0 | if (edge == 1) |
2100 | 0 | edge = (int)w->sy + 1; |
2101 | 0 | } else if (status == PANE_STATUS_BOTTOM) { |
2102 | 0 | if (edge == 0) |
2103 | 0 | edge = (int)w->sy; |
2104 | 0 | } else { |
2105 | 0 | if (edge == 0) |
2106 | 0 | edge = (int)w->sy + 1; |
2107 | 0 | } |
2108 | |
|
2109 | 0 | left = xoff; |
2110 | 0 | right = xoff + (int)sx; |
2111 | |
|
2112 | 0 | TAILQ_FOREACH(next, &w->panes, entry) { |
2113 | 0 | window_pane_full_size_offset(next, &xoff, &yoff, &sx, &sy); |
2114 | 0 | if (next == wp) |
2115 | 0 | continue; |
2116 | 0 | if (yoff + (int)sy + 1 != edge) |
2117 | 0 | continue; |
2118 | 0 | end = xoff + (int)sx - 1; |
2119 | |
|
2120 | 0 | found = 0; |
2121 | 0 | if (xoff < left && end > right) |
2122 | 0 | found = 1; |
2123 | 0 | else if (xoff >= left && xoff <= right) |
2124 | 0 | found = 1; |
2125 | 0 | else if (end >= left && end <= right) |
2126 | 0 | found = 1; |
2127 | 0 | if (!found) |
2128 | 0 | continue; |
2129 | 0 | list = xreallocarray(list, size + 1, sizeof *list); |
2130 | 0 | list[size++] = next; |
2131 | 0 | } |
2132 | |
|
2133 | 0 | best = window_pane_choose_best(list, size); |
2134 | 0 | free(list); |
2135 | 0 | return (best); |
2136 | 0 | } |
2137 | | |
2138 | | /* Find the pane directly below another. */ |
2139 | | struct window_pane * |
2140 | | window_pane_find_down(struct window_pane *wp) |
2141 | 0 | { |
2142 | 0 | struct window *w; |
2143 | 0 | struct window_pane *next, *best, **list; |
2144 | 0 | int edge, left, right, end, status, found; |
2145 | 0 | int xoff, yoff; |
2146 | 0 | u_int size, sx, sy; |
2147 | |
|
2148 | 0 | if (wp == NULL) |
2149 | 0 | return (NULL); |
2150 | 0 | w = wp->window; |
2151 | 0 | status = window_get_pane_status(w); |
2152 | |
|
2153 | 0 | list = NULL; |
2154 | 0 | size = 0; |
2155 | |
|
2156 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
2157 | |
|
2158 | 0 | edge = yoff + (int)sy + 1; |
2159 | 0 | if (status == PANE_STATUS_TOP) { |
2160 | 0 | if (edge >= (int)w->sy) |
2161 | 0 | edge = 1; |
2162 | 0 | } else if (status == PANE_STATUS_BOTTOM) { |
2163 | 0 | if (edge >= (int)w->sy - 1) |
2164 | 0 | edge = 0; |
2165 | 0 | } else { |
2166 | 0 | if (edge >= (int)w->sy) |
2167 | 0 | edge = 0; |
2168 | 0 | } |
2169 | |
|
2170 | 0 | left = wp->xoff; |
2171 | 0 | right = wp->xoff + (int)wp->sx; |
2172 | |
|
2173 | 0 | TAILQ_FOREACH(next, &w->panes, entry) { |
2174 | 0 | window_pane_full_size_offset(next, &xoff, &yoff, &sx, &sy); |
2175 | 0 | if (next == wp) |
2176 | 0 | continue; |
2177 | 0 | if (yoff != edge) |
2178 | 0 | continue; |
2179 | 0 | end = xoff + (int)sx - 1; |
2180 | |
|
2181 | 0 | found = 0; |
2182 | 0 | if (xoff < left && end > right) |
2183 | 0 | found = 1; |
2184 | 0 | else if (xoff >= left && xoff <= right) |
2185 | 0 | found = 1; |
2186 | 0 | else if (end >= left && end <= right) |
2187 | 0 | found = 1; |
2188 | 0 | if (!found) |
2189 | 0 | continue; |
2190 | 0 | list = xreallocarray(list, size + 1, sizeof *list); |
2191 | 0 | list[size++] = next; |
2192 | 0 | } |
2193 | |
|
2194 | 0 | best = window_pane_choose_best(list, size); |
2195 | 0 | free(list); |
2196 | 0 | return (best); |
2197 | 0 | } |
2198 | | |
2199 | | /* Find the pane directly to the left of another. */ |
2200 | | struct window_pane * |
2201 | | window_pane_find_left(struct window_pane *wp) |
2202 | 0 | { |
2203 | 0 | struct window *w; |
2204 | 0 | struct window_pane *next, *best, **list; |
2205 | 0 | int edge, top, bottom, end, found; |
2206 | 0 | int xoff, yoff; |
2207 | 0 | u_int size, sx, sy; |
2208 | |
|
2209 | 0 | if (wp == NULL) |
2210 | 0 | return (NULL); |
2211 | 0 | w = wp->window; |
2212 | |
|
2213 | 0 | list = NULL; |
2214 | 0 | size = 0; |
2215 | |
|
2216 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
2217 | |
|
2218 | 0 | edge = xoff; |
2219 | 0 | if (edge == 0) |
2220 | 0 | edge = (int)w->sx + 1; |
2221 | |
|
2222 | 0 | top = yoff; |
2223 | 0 | bottom = yoff + (int)sy; |
2224 | |
|
2225 | 0 | TAILQ_FOREACH(next, &w->panes, entry) { |
2226 | 0 | window_pane_full_size_offset(next, &xoff, &yoff, &sx, &sy); |
2227 | 0 | if (next == wp) |
2228 | 0 | continue; |
2229 | 0 | if (xoff + (int)sx + 1 != edge) |
2230 | 0 | continue; |
2231 | 0 | end = yoff + (int)sy - 1; |
2232 | |
|
2233 | 0 | found = 0; |
2234 | 0 | if (yoff < top && end > bottom) |
2235 | 0 | found = 1; |
2236 | 0 | else if (yoff >= top && yoff <= bottom) |
2237 | 0 | found = 1; |
2238 | 0 | else if (end >= top && end <= bottom) |
2239 | 0 | found = 1; |
2240 | 0 | if (!found) |
2241 | 0 | continue; |
2242 | 0 | list = xreallocarray(list, size + 1, sizeof *list); |
2243 | 0 | list[size++] = next; |
2244 | 0 | } |
2245 | |
|
2246 | 0 | best = window_pane_choose_best(list, size); |
2247 | 0 | free(list); |
2248 | 0 | return (best); |
2249 | 0 | } |
2250 | | |
2251 | | /* Find the pane directly to the right of another. */ |
2252 | | struct window_pane * |
2253 | | window_pane_find_right(struct window_pane *wp) |
2254 | 0 | { |
2255 | 0 | struct window *w; |
2256 | 0 | struct window_pane *next, *best, **list; |
2257 | 0 | int edge, top, bottom, end, found; |
2258 | 0 | int xoff, yoff; |
2259 | 0 | u_int size, sx, sy; |
2260 | |
|
2261 | 0 | if (wp == NULL) |
2262 | 0 | return (NULL); |
2263 | 0 | w = wp->window; |
2264 | |
|
2265 | 0 | list = NULL; |
2266 | 0 | size = 0; |
2267 | |
|
2268 | 0 | window_pane_full_size_offset(wp, &xoff, &yoff, &sx, &sy); |
2269 | |
|
2270 | 0 | edge = xoff + (int)sx + 1; |
2271 | 0 | if (edge >= (int)w->sx) |
2272 | 0 | edge = 0; |
2273 | |
|
2274 | 0 | top = wp->yoff; |
2275 | 0 | bottom = wp->yoff + (int)wp->sy; |
2276 | |
|
2277 | 0 | TAILQ_FOREACH(next, &w->panes, entry) { |
2278 | 0 | window_pane_full_size_offset(next, &xoff, &yoff, &sx, &sy); |
2279 | 0 | if (next == wp) |
2280 | 0 | continue; |
2281 | 0 | if (xoff != edge) |
2282 | 0 | continue; |
2283 | 0 | end = yoff + (int)sy - 1; |
2284 | |
|
2285 | 0 | found = 0; |
2286 | 0 | if (yoff < top && end > bottom) |
2287 | 0 | found = 1; |
2288 | 0 | else if (yoff >= top && yoff <= bottom) |
2289 | 0 | found = 1; |
2290 | 0 | else if (end >= top && end <= bottom) |
2291 | 0 | found = 1; |
2292 | 0 | if (!found) |
2293 | 0 | continue; |
2294 | 0 | list = xreallocarray(list, size + 1, sizeof *list); |
2295 | 0 | list[size++] = next; |
2296 | 0 | } |
2297 | |
|
2298 | 0 | best = window_pane_choose_best(list, size); |
2299 | 0 | free(list); |
2300 | 0 | return (best); |
2301 | 0 | } |
2302 | | |
2303 | | /* Add window to stack. */ |
2304 | | void |
2305 | | window_pane_stack_push(struct window_panes *stack, struct window_pane *wp) |
2306 | 0 | { |
2307 | 0 | if (wp != NULL) { |
2308 | 0 | window_pane_stack_remove(stack, wp); |
2309 | 0 | TAILQ_INSERT_HEAD(stack, wp, sentry); |
2310 | 0 | wp->flags |= PANE_VISITED; |
2311 | 0 | } |
2312 | 0 | } |
2313 | | |
2314 | | /* Remove window from stack. */ |
2315 | | void |
2316 | | window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp) |
2317 | 0 | { |
2318 | 0 | if (wp != NULL && (wp->flags & PANE_VISITED)) { |
2319 | 0 | TAILQ_REMOVE(stack, wp, sentry); |
2320 | 0 | wp->flags &= ~PANE_VISITED; |
2321 | 0 | } |
2322 | 0 | } |
2323 | | |
2324 | | /* Clear alert flags for a winlink */ |
2325 | | void |
2326 | | winlink_clear_flags(struct winlink *wl) |
2327 | 0 | { |
2328 | 0 | struct winlink *loop; |
2329 | |
|
2330 | 0 | wl->window->flags &= ~WINDOW_ALERTFLAGS; |
2331 | 0 | TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) { |
2332 | 0 | if ((loop->flags & WINLINK_ALERTFLAGS) != 0) { |
2333 | 0 | loop->flags &= ~WINLINK_ALERTFLAGS; |
2334 | 0 | server_status_session(loop->session); |
2335 | 0 | } |
2336 | 0 | } |
2337 | 0 | } |
2338 | | |
2339 | | /* Shuffle window indexes up. */ |
2340 | | int |
2341 | | winlink_shuffle_up(struct session *s, struct winlink *wl, int before) |
2342 | 0 | { |
2343 | 0 | int idx, last; |
2344 | |
|
2345 | 0 | if (wl == NULL) |
2346 | 0 | return (-1); |
2347 | 0 | if (before) |
2348 | 0 | idx = wl->idx; |
2349 | 0 | else |
2350 | 0 | idx = wl->idx + 1; |
2351 | | |
2352 | | /* Find the next free index. */ |
2353 | 0 | for (last = idx; last < INT_MAX; last++) { |
2354 | 0 | if (winlink_find_by_index(&s->windows, last) == NULL) |
2355 | 0 | break; |
2356 | 0 | } |
2357 | 0 | if (last == INT_MAX) |
2358 | 0 | return (-1); |
2359 | | |
2360 | | /* Move everything from last - 1 to idx up a bit. */ |
2361 | 0 | for (; last > idx; last--) { |
2362 | 0 | wl = winlink_find_by_index(&s->windows, last - 1); |
2363 | 0 | RB_REMOVE(winlinks, &s->windows, wl); |
2364 | 0 | wl->idx++; |
2365 | 0 | RB_INSERT(winlinks, &s->windows, wl); |
2366 | 0 | } |
2367 | |
|
2368 | 0 | return (idx); |
2369 | 0 | } |
2370 | | |
2371 | | static void |
2372 | | window_pane_input_callback(struct client *c, __unused const char *path, |
2373 | | int error, int closed, struct evbuffer *buffer, void *data) |
2374 | 0 | { |
2375 | 0 | struct window_pane_input_data *cdata = data; |
2376 | 0 | struct window_pane *wp; |
2377 | 0 | u_char *buf = EVBUFFER_DATA(buffer); |
2378 | 0 | size_t len = EVBUFFER_LENGTH(buffer); |
2379 | |
|
2380 | 0 | wp = window_pane_find_by_id(cdata->wp); |
2381 | 0 | if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) { |
2382 | 0 | if (wp == NULL) { |
2383 | 0 | c->retval = 1; |
2384 | 0 | c->flags |= CLIENT_EXIT; |
2385 | 0 | } |
2386 | 0 | file_cancel(cdata->file); |
2387 | 0 | } else if (cdata->file == NULL || closed || error != 0) { |
2388 | 0 | cmdq_continue(cdata->item); |
2389 | 0 | server_client_unref(c); |
2390 | 0 | free(cdata); |
2391 | 0 | } else |
2392 | 0 | input_parse_buffer(wp, buf, len); |
2393 | 0 | evbuffer_drain(buffer, len); |
2394 | 0 | } |
2395 | | |
2396 | | int |
2397 | | window_pane_start_input(struct window_pane *wp, struct cmdq_item *item, |
2398 | | char **cause) |
2399 | 0 | { |
2400 | 0 | struct client *c = cmdq_get_client(item); |
2401 | 0 | struct window_pane_input_data *cdata; |
2402 | |
|
2403 | 0 | if (~wp->flags & PANE_EMPTY) { |
2404 | 0 | *cause = xstrdup("pane is not empty"); |
2405 | 0 | return (-1); |
2406 | 0 | } |
2407 | 0 | if (c->flags & (CLIENT_DEAD|CLIENT_EXITED)) |
2408 | 0 | return (1); |
2409 | 0 | if (c->session != NULL) |
2410 | 0 | return (1); |
2411 | | |
2412 | 0 | cdata = xmalloc(sizeof *cdata); |
2413 | 0 | cdata->item = item; |
2414 | 0 | cdata->wp = wp->id; |
2415 | 0 | cdata->file = file_read(c, "-", window_pane_input_callback, cdata); |
2416 | 0 | c->references++; |
2417 | |
|
2418 | 0 | return (0); |
2419 | 0 | } |
2420 | | |
2421 | | void * |
2422 | | window_pane_get_new_data(struct window_pane *wp, |
2423 | | struct window_pane_offset *wpo, size_t *size) |
2424 | 0 | { |
2425 | 0 | size_t used = wpo->used - wp->base_offset; |
2426 | |
|
2427 | 0 | *size = EVBUFFER_LENGTH(wp->event->input) - used; |
2428 | 0 | return (EVBUFFER_DATA(wp->event->input) + used); |
2429 | 0 | } |
2430 | | |
2431 | | void |
2432 | | window_pane_update_used_data(struct window_pane *wp, |
2433 | | struct window_pane_offset *wpo, size_t size) |
2434 | 0 | { |
2435 | 0 | size_t used = wpo->used - wp->base_offset; |
2436 | |
|
2437 | 0 | if (size > EVBUFFER_LENGTH(wp->event->input) - used) |
2438 | 0 | size = EVBUFFER_LENGTH(wp->event->input) - used; |
2439 | 0 | wpo->used += size; |
2440 | 0 | } |
2441 | | |
2442 | | void |
2443 | | window_pane_default_cursor(struct window_pane *wp) |
2444 | 0 | { |
2445 | 0 | screen_set_default_cursor(wp->screen, wp->options); |
2446 | 0 | } |
2447 | | |
2448 | | int |
2449 | | window_pane_mode(struct window_pane *wp) |
2450 | 0 | { |
2451 | 0 | if (TAILQ_FIRST(&wp->modes) != NULL) { |
2452 | 0 | if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode) |
2453 | 0 | return (WINDOW_PANE_COPY_MODE); |
2454 | 0 | if (TAILQ_FIRST(&wp->modes)->mode == &window_view_mode) |
2455 | 0 | return (WINDOW_PANE_VIEW_MODE); |
2456 | 0 | } |
2457 | 0 | return (WINDOW_PANE_NO_MODE); |
2458 | 0 | } |
2459 | | |
2460 | | int |
2461 | | window_pane_show_scrollbar(struct window_pane *wp) |
2462 | 0 | { |
2463 | 0 | struct window *w = wp->window; |
2464 | 0 | struct window_mode_entry *wme; |
2465 | |
|
2466 | 0 | if (SCREEN_IS_ALTERNATE(&wp->base)) |
2467 | 0 | return (0); |
2468 | 0 | if ((w->flags & WINDOW_ZOOMED) && w->active != NULL) { |
2469 | 0 | wme = TAILQ_FIRST(&w->active->modes); |
2470 | 0 | if (wme != NULL && |
2471 | 0 | (wme->mode->flags & WINDOW_MODE_HIDE_SCROLLBARS)) { |
2472 | 0 | return (0); |
2473 | 0 | } |
2474 | 0 | } |
2475 | 0 | if (w->sb == PANE_SCROLLBARS_ALWAYS || |
2476 | 0 | w->sb == PANE_SCROLLBARS_AUTOHIDE || |
2477 | 0 | (w->sb == PANE_SCROLLBARS_MODAL && |
2478 | 0 | window_pane_mode(wp) != WINDOW_PANE_NO_MODE)) |
2479 | 0 | return (1); |
2480 | 0 | return (0); |
2481 | 0 | } |
2482 | | |
2483 | | int |
2484 | | window_pane_scrollbar_reserve(struct window_pane *wp) |
2485 | 0 | { |
2486 | 0 | if (!window_pane_show_scrollbar(wp)) |
2487 | 0 | return (0); |
2488 | 0 | return (wp->window->sb == PANE_SCROLLBARS_ALWAYS); |
2489 | 0 | } |
2490 | | |
2491 | | int |
2492 | | window_pane_scrollbar_overlay(struct window_pane *wp) |
2493 | 0 | { |
2494 | 0 | if (!window_pane_show_scrollbar(wp)) |
2495 | 0 | return (0); |
2496 | 0 | return (window_pane_scrollbar_auto_hide(wp)); |
2497 | 0 | } |
2498 | | |
2499 | | int |
2500 | | window_pane_scrollbar_visible(struct window_pane *wp) |
2501 | 0 | { |
2502 | 0 | if (!window_pane_show_scrollbar(wp)) |
2503 | 0 | return (0); |
2504 | 0 | if (!window_pane_scrollbar_auto_hide(wp)) |
2505 | 0 | return (1); |
2506 | 0 | return (wp->sb_auto_visible); |
2507 | 0 | } |
2508 | | |
2509 | | void |
2510 | | window_pane_scrollbar_start_timer(struct window_pane *wp) |
2511 | 0 | { |
2512 | 0 | struct timeval tv; |
2513 | 0 | u_int delay; |
2514 | |
|
2515 | 0 | if (!window_pane_scrollbar_auto_hide(wp) || !wp->sb_auto_visible) |
2516 | 0 | return; |
2517 | | |
2518 | 0 | delay = options_get_number(wp->window->options, |
2519 | 0 | "pane-scrollbars-timeout"); |
2520 | 0 | tv.tv_sec = delay / 1000; |
2521 | 0 | tv.tv_usec = (delay % 1000) * 1000L; |
2522 | 0 | evtimer_del(&wp->sb_auto_timer); |
2523 | 0 | evtimer_add(&wp->sb_auto_timer, &tv); |
2524 | 0 | } |
2525 | | |
2526 | | void |
2527 | | window_pane_scrollbar_show(struct window_pane *wp, int start_timer) |
2528 | 0 | { |
2529 | 0 | int changed = 0; |
2530 | 0 | if (!window_pane_scrollbar_auto_hide(wp)) |
2531 | 0 | return; |
2532 | 0 | if (!window_pane_show_scrollbar(wp)) |
2533 | 0 | return; |
2534 | 0 | if (!wp->sb_auto_visible) { |
2535 | 0 | wp->sb_auto_visible = 1; |
2536 | 0 | changed = 1; |
2537 | 0 | } |
2538 | 0 | evtimer_del(&wp->sb_auto_timer); |
2539 | 0 | if (start_timer) |
2540 | 0 | window_pane_scrollbar_start_timer(wp); |
2541 | 0 | if (changed) |
2542 | 0 | window_pane_scrollbar_redraw_visibility(wp); |
2543 | 0 | } |
2544 | | |
2545 | | void |
2546 | | window_pane_scrollbar_hide(struct window_pane *wp) |
2547 | 0 | { |
2548 | 0 | if (event_initialized(&wp->sb_auto_timer)) |
2549 | 0 | evtimer_del(&wp->sb_auto_timer); |
2550 | 0 | wp->sb_auto_hover = 0; |
2551 | 0 | if (!wp->sb_auto_visible) |
2552 | 0 | return; |
2553 | 0 | wp->sb_auto_visible = 0; |
2554 | 0 | window_pane_scrollbar_redraw_visibility(wp); |
2555 | 0 | } |
2556 | | |
2557 | | int |
2558 | | window_pane_get_bg(struct window_pane *wp) |
2559 | 0 | { |
2560 | 0 | int c; |
2561 | 0 | struct grid_cell defaults; |
2562 | |
|
2563 | 0 | c = window_pane_get_bg_control_client(wp); |
2564 | 0 | if (c == -1) { |
2565 | 0 | tty_default_colours(&defaults, wp, NULL); |
2566 | 0 | if (COLOUR_DEFAULT(defaults.bg)) |
2567 | 0 | c = window_get_bg_client(wp); |
2568 | 0 | else |
2569 | 0 | c = defaults.bg; |
2570 | 0 | } |
2571 | 0 | return (c); |
2572 | 0 | } |
2573 | | |
2574 | | /* Get a client with a background for the pane. */ |
2575 | | int |
2576 | | window_get_bg_client(struct window_pane *wp) |
2577 | 0 | { |
2578 | 0 | struct window *w = wp->window; |
2579 | 0 | struct client *loop; |
2580 | |
|
2581 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
2582 | 0 | if (loop->flags & CLIENT_UNATTACHEDFLAGS) |
2583 | 0 | continue; |
2584 | 0 | if (loop->session == NULL || !session_has(loop->session, w)) |
2585 | 0 | continue; |
2586 | 0 | if (loop->tty.bg == -1) |
2587 | 0 | continue; |
2588 | 0 | return (loop->tty.bg); |
2589 | 0 | } |
2590 | 0 | return (-1); |
2591 | 0 | } |
2592 | | |
2593 | | /* |
2594 | | * If any control mode client exists that has provided a bg color, return it. |
2595 | | * Otherwise, return -1. |
2596 | | */ |
2597 | | int |
2598 | | window_pane_get_bg_control_client(struct window_pane *wp) |
2599 | 0 | { |
2600 | 0 | struct client *c; |
2601 | |
|
2602 | 0 | if (wp->control_bg == -1) |
2603 | 0 | return (-1); |
2604 | | |
2605 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
2606 | 0 | if (c->flags & CLIENT_CONTROL) |
2607 | 0 | return (wp->control_bg); |
2608 | 0 | } |
2609 | 0 | return (-1); |
2610 | 0 | } |
2611 | | |
2612 | | /* |
2613 | | * Get a client with a foreground for the pane. There isn't much to choose |
2614 | | * between them so just use the first. |
2615 | | */ |
2616 | | int |
2617 | | window_pane_get_fg(struct window_pane *wp) |
2618 | 0 | { |
2619 | 0 | struct window *w = wp->window; |
2620 | 0 | struct client *loop; |
2621 | |
|
2622 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
2623 | 0 | if (loop->flags & CLIENT_UNATTACHEDFLAGS) |
2624 | 0 | continue; |
2625 | 0 | if (loop->session == NULL || !session_has(loop->session, w)) |
2626 | 0 | continue; |
2627 | 0 | if (loop->tty.fg == -1) |
2628 | 0 | continue; |
2629 | 0 | return (loop->tty.fg); |
2630 | 0 | } |
2631 | 0 | return (-1); |
2632 | 0 | } |
2633 | | |
2634 | | /* |
2635 | | * If any control mode client exists that has provided a fg color, return it. |
2636 | | * Otherwise, return -1. |
2637 | | */ |
2638 | | int |
2639 | | window_pane_get_fg_control_client(struct window_pane *wp) |
2640 | 0 | { |
2641 | 0 | struct client *c; |
2642 | |
|
2643 | 0 | if (wp->control_fg == -1) |
2644 | 0 | return (-1); |
2645 | | |
2646 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
2647 | 0 | if (c->flags & CLIENT_CONTROL) |
2648 | 0 | return (wp->control_fg); |
2649 | 0 | } |
2650 | 0 | return (-1); |
2651 | 0 | } |
2652 | | |
2653 | | enum client_theme |
2654 | | window_pane_get_theme(struct window_pane *wp) |
2655 | 0 | { |
2656 | 0 | struct window *w; |
2657 | 0 | struct client *loop; |
2658 | 0 | int found_light = 0, found_dark = 0; |
2659 | |
|
2660 | 0 | if (wp == NULL) |
2661 | 0 | return (THEME_UNKNOWN); |
2662 | 0 | w = wp->window; |
2663 | | |
2664 | | /* |
2665 | | * Prefer a theme reported by an attached client with mode 2031 or DSR |
2666 | | * 996: the terminal knows its own light or dark mode. |
2667 | | */ |
2668 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
2669 | 0 | if (loop->flags & CLIENT_UNATTACHEDFLAGS) |
2670 | 0 | continue; |
2671 | 0 | if (loop->session == NULL || !session_has(loop->session, w)) |
2672 | 0 | continue; |
2673 | 0 | switch (loop->theme) { |
2674 | 0 | case THEME_LIGHT: |
2675 | 0 | found_light = 1; |
2676 | 0 | break; |
2677 | 0 | case THEME_DARK: |
2678 | 0 | found_dark = 1; |
2679 | 0 | break; |
2680 | 0 | case THEME_UNKNOWN: |
2681 | 0 | break; |
2682 | 0 | } |
2683 | 0 | } |
2684 | 0 | if (found_dark && !found_light) |
2685 | 0 | return (THEME_DARK); |
2686 | 0 | if (found_light && !found_dark) |
2687 | 0 | return (THEME_LIGHT); |
2688 | | |
2689 | | /* |
2690 | | * Otherwise guess from the pane background colour, for terminals which |
2691 | | * do not report a theme themselves. |
2692 | | */ |
2693 | 0 | return (colour_totheme(window_pane_get_bg(wp))); |
2694 | 0 | } |
2695 | | |
2696 | | void |
2697 | | window_pane_send_theme_update(struct window_pane *wp) |
2698 | 0 | { |
2699 | 0 | enum client_theme theme; |
2700 | |
|
2701 | 0 | if (wp == NULL || window_pane_exited(wp)) |
2702 | 0 | return; |
2703 | 0 | if (~wp->flags & PANE_THEMECHANGED) |
2704 | 0 | return; |
2705 | 0 | if (~wp->screen->mode & MODE_THEME_UPDATES) |
2706 | 0 | return; |
2707 | | |
2708 | 0 | theme = window_pane_get_theme(wp); |
2709 | 0 | if (theme == wp->last_theme) |
2710 | 0 | return; |
2711 | 0 | wp->last_theme = theme; |
2712 | 0 | wp->flags &= ~PANE_THEMECHANGED; |
2713 | |
|
2714 | 0 | switch (theme) { |
2715 | 0 | case THEME_LIGHT: |
2716 | 0 | log_debug("%s: %%%u light theme", __func__, wp->id); |
2717 | 0 | bufferevent_write(wp->event, "\033[?997;2n", 9); |
2718 | 0 | break; |
2719 | 0 | case THEME_DARK: |
2720 | 0 | log_debug("%s: %%%u dark theme", __func__, wp->id); |
2721 | 0 | bufferevent_write(wp->event, "\033[?997;1n", 9); |
2722 | 0 | break; |
2723 | 0 | case THEME_UNKNOWN: |
2724 | 0 | log_debug("%s: %%%u unknown theme", __func__, wp->id); |
2725 | 0 | break; |
2726 | 0 | } |
2727 | 0 | } |
2728 | | |
2729 | | struct style_range * |
2730 | | window_pane_status_get_range(struct window_pane *wp, u_int x, u_int y) |
2731 | 0 | { |
2732 | 0 | struct style_ranges *srs; |
2733 | 0 | u_int line; |
2734 | 0 | int pane_status; |
2735 | |
|
2736 | 0 | if (wp == NULL) |
2737 | 0 | return (NULL); |
2738 | 0 | srs = &wp->border_status_line.ranges; |
2739 | |
|
2740 | 0 | pane_status = window_pane_get_pane_status(wp); |
2741 | 0 | if (pane_status == PANE_STATUS_TOP) |
2742 | 0 | line = wp->yoff - 1; |
2743 | 0 | else if (pane_status == PANE_STATUS_BOTTOM) |
2744 | 0 | line = wp->yoff + wp->sy; |
2745 | 0 | if (pane_status == PANE_STATUS_OFF || line != y) |
2746 | 0 | return (NULL); |
2747 | | |
2748 | | /* |
2749 | | * The border formats start 2 off but that isn't reflected in |
2750 | | * the stored bounds of the range. |
2751 | | */ |
2752 | 0 | return (style_ranges_get_range(srs, x - wp->xoff - 2)); |
2753 | 0 | } |
2754 | | |
2755 | | enum pane_lines |
2756 | | window_get_pane_lines(struct window *w) |
2757 | 0 | { |
2758 | 0 | struct options *oo; |
2759 | |
|
2760 | 0 | oo = w->options; |
2761 | 0 | return (options_get_number(oo, "pane-border-lines")); |
2762 | 0 | } |
2763 | | |
2764 | | enum pane_lines |
2765 | | window_pane_get_pane_lines(struct window_pane *wp) |
2766 | 0 | { |
2767 | 0 | struct options *oo; |
2768 | |
|
2769 | 0 | if (!window_pane_is_floating(wp)) |
2770 | 0 | oo = wp->window->options; |
2771 | 0 | else |
2772 | 0 | oo = wp->options; |
2773 | 0 | return (options_get_number(oo, "pane-border-lines")); |
2774 | 0 | } |
2775 | | |
2776 | | int |
2777 | | window_get_pane_status(struct window *w) |
2778 | 0 | { |
2779 | 0 | int status; |
2780 | |
|
2781 | 0 | status = options_get_number(w->options, "pane-border-status"); |
2782 | 0 | if (status == PANE_STATUS_TOP_FLOATING || |
2783 | 0 | status == PANE_STATUS_BOTTOM_FLOATING) |
2784 | 0 | return (PANE_STATUS_OFF); |
2785 | 0 | return (status); |
2786 | 0 | } |
2787 | | |
2788 | | int |
2789 | | window_pane_get_pane_status(struct window_pane *wp) |
2790 | 0 | { |
2791 | 0 | struct window_mode_entry *wme; |
2792 | 0 | int status; |
2793 | |
|
2794 | 0 | wme = TAILQ_FIRST(&wp->modes); |
2795 | 0 | if (wme != NULL && |
2796 | 0 | (wme->mode->flags & WINDOW_MODE_HIDE_PANE_STATUS) && |
2797 | 0 | (wp->flags & PANE_ZOOMED)) |
2798 | 0 | return (PANE_STATUS_OFF); |
2799 | | |
2800 | 0 | if (!window_pane_is_floating(wp)) |
2801 | 0 | return (window_get_pane_status(wp->window)); |
2802 | 0 | if (window_pane_get_pane_lines(wp) == PANE_LINES_NONE) |
2803 | 0 | return (PANE_STATUS_OFF); |
2804 | | |
2805 | 0 | status = options_get_number(wp->options, "pane-border-status"); |
2806 | 0 | if (status == PANE_STATUS_TOP_FLOATING) |
2807 | 0 | return (PANE_STATUS_TOP); |
2808 | 0 | if (status == PANE_STATUS_BOTTOM_FLOATING) |
2809 | 0 | return (PANE_STATUS_BOTTOM); |
2810 | 0 | return (status); |
2811 | 0 | } |
2812 | | |
2813 | | int |
2814 | | window_pane_is_floating(struct window_pane *wp) |
2815 | 0 | { |
2816 | 0 | struct layout_cell *lc = wp->layout_cell; |
2817 | |
|
2818 | 0 | if (lc == NULL || (lc->flags & LAYOUT_CELL_FLOATING) == 0) |
2819 | 0 | return (0); |
2820 | 0 | return (1); |
2821 | 0 | } |