/src/tmux/cmd-resize-pane.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-resize-pane.c,v 1.68 2026/08/31 07:44:39 nicm Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2009 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 | | |
21 | | #include <ctype.h> |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | |
25 | | #include "tmux.h" |
26 | | |
27 | | /* |
28 | | * Increase or decrease pane size. |
29 | | */ |
30 | | |
31 | | static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmdq_item *); |
32 | | |
33 | | static enum cmd_retval cmd_resize_pane_mouse_update(struct cmd *, |
34 | | struct cmdq_item *); |
35 | | static void cmd_resize_pane_mouse_resize_move_floating( |
36 | | struct client *, struct mouse_event *); |
37 | | static void cmd_resize_pane_mouse_resize_tiled(struct client *, |
38 | | struct mouse_event *); |
39 | | |
40 | | const struct cmd_entry cmd_resize_pane_entry = { |
41 | | .name = "resize-pane", |
42 | | .alias = "resizep", |
43 | | |
44 | | .args = { "D::L::MR::Tt:U::x:y:Z", 0, 1, NULL }, |
45 | | .usage = "[-MTZ] [-D lines] [-L columns] [-R columns] [-U lines] " |
46 | | "[-x width] [-y height] " CMD_TARGET_PANE_USAGE, |
47 | | |
48 | | .target = { 't', CMD_FIND_PANE, 0 }, |
49 | | |
50 | | .flags = CMD_AFTERHOOK, |
51 | | .exec = cmd_resize_pane_exec |
52 | | }; |
53 | | |
54 | | static enum cmd_retval |
55 | | cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item) |
56 | 0 | { |
57 | 0 | struct args *args = cmd_get_args(self); |
58 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
59 | 0 | struct window_pane *wp = target->wp; |
60 | 0 | struct winlink *wl = target->wl; |
61 | 0 | struct window *w = wl->window; |
62 | 0 | struct layout_cell *lc = wp->layout_cell; |
63 | 0 | enum layout_type type; |
64 | 0 | const char *errstr, *argval; |
65 | 0 | const char flags[4] = { 'U', 'D', 'L', 'R' }; |
66 | 0 | char *cause = NULL, flag; |
67 | 0 | int adjust, x, y, status, opposite = 0; |
68 | 0 | long unsigned i; |
69 | 0 | struct grid *gd = wp->base.grid; |
70 | |
|
71 | 0 | if (args_has(args, 'T')) { |
72 | 0 | if (!TAILQ_EMPTY(&wp->modes)) |
73 | 0 | return (CMD_RETURN_NORMAL); |
74 | 0 | adjust = screen_size_y(&wp->base) - 1 - wp->base.cy; |
75 | 0 | if (adjust > (int)gd->hsize) |
76 | 0 | adjust = gd->hsize; |
77 | 0 | grid_remove_history(gd, adjust); |
78 | 0 | wp->base.cy += adjust; |
79 | 0 | wp->flags |= PANE_REDRAW; |
80 | 0 | return (CMD_RETURN_NORMAL); |
81 | 0 | } |
82 | | |
83 | 0 | if (args_has(args, 'M')) |
84 | 0 | return (cmd_resize_pane_mouse_update(self, item)); |
85 | | |
86 | 0 | if (args_has(args, 'Z')) { |
87 | 0 | if (w->flags & WINDOW_ZOOMED) |
88 | 0 | window_unzoom(w, 1); |
89 | 0 | else |
90 | 0 | window_zoom(wp); |
91 | 0 | server_redraw_window(w); |
92 | 0 | return (CMD_RETURN_NORMAL); |
93 | 0 | } |
94 | 0 | server_unzoom_window(w); |
95 | 0 | lc = wp->layout_cell; /* may have been replaced by unzoom */ |
96 | |
|
97 | 0 | if (args_has(args, 'x')) { |
98 | 0 | x = args_percentage(args, 'x', 0, PANE_MAXIMUM, w->sx, &cause); |
99 | 0 | if (cause != NULL) { |
100 | 0 | cmdq_error(item, "width %s", cause); |
101 | 0 | free(cause); |
102 | 0 | return (CMD_RETURN_ERROR); |
103 | 0 | } |
104 | 0 | if (window_pane_is_floating(wp)) { |
105 | 0 | if (layout_resize_floating_pane_to(wp, LAYOUT_LEFTRIGHT, |
106 | 0 | x, &cause) != 0) { |
107 | 0 | cmdq_error(item, "size %s", cause); |
108 | 0 | free(cause); |
109 | 0 | return (CMD_RETURN_ERROR); |
110 | 0 | } |
111 | 0 | } else |
112 | 0 | layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x); |
113 | 0 | } |
114 | 0 | if (args_has(args, 'y')) { |
115 | 0 | y = args_percentage(args, 'y', 0, PANE_MAXIMUM, w->sy, &cause); |
116 | 0 | if (cause != NULL) { |
117 | 0 | cmdq_error(item, "height %s", cause); |
118 | 0 | free(cause); |
119 | 0 | return (CMD_RETURN_ERROR); |
120 | 0 | } |
121 | 0 | status = window_get_pane_status(w); |
122 | 0 | switch (status) { |
123 | 0 | case PANE_STATUS_TOP: |
124 | 0 | if (y != INT_MAX && wp->yoff == 1) |
125 | 0 | y++; |
126 | 0 | break; |
127 | 0 | case PANE_STATUS_BOTTOM: |
128 | 0 | if (y != INT_MAX && wp->yoff + wp->sy == w->sy - 1) |
129 | 0 | y++; |
130 | 0 | break; |
131 | 0 | } |
132 | 0 | if (window_pane_is_floating(wp)) { |
133 | 0 | if (layout_resize_floating_pane_to(wp, LAYOUT_TOPBOTTOM, |
134 | 0 | y, &cause) != 0) { |
135 | 0 | cmdq_error(item, "size %s", cause); |
136 | 0 | free(cause); |
137 | 0 | return (CMD_RETURN_ERROR); |
138 | 0 | } |
139 | 0 | } else |
140 | 0 | layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y); |
141 | 0 | } |
142 | | |
143 | 0 | for (i = 0; i < nitems(flags); i++) { |
144 | 0 | flag = flags[i]; |
145 | 0 | if (!args_has(args, flag)) |
146 | 0 | continue; |
147 | | |
148 | 0 | argval = args_get(args, flag); |
149 | 0 | if (argval == NULL) { |
150 | 0 | if (args_count(args) == 0) |
151 | 0 | argval = "1"; |
152 | 0 | else |
153 | 0 | argval = args_string(args, 0); |
154 | 0 | } |
155 | |
|
156 | 0 | adjust = strtonum(argval, INT_MIN, INT_MAX, &errstr); |
157 | 0 | if (errstr != NULL) { |
158 | 0 | cmdq_error(item, "adjustment %s", errstr); |
159 | 0 | return (CMD_RETURN_ERROR); |
160 | 0 | } |
161 | | |
162 | 0 | type = LAYOUT_TOPBOTTOM; |
163 | 0 | if (flag == 'L' || flag == 'R') |
164 | 0 | type = LAYOUT_LEFTRIGHT; |
165 | |
|
166 | 0 | if (window_pane_is_floating(wp)) { |
167 | 0 | if (flag == 'L' || flag == 'U') |
168 | 0 | opposite = 1; |
169 | |
|
170 | 0 | if (layout_resize_floating_pane(wp, type, adjust, |
171 | 0 | opposite, &cause) != 0) { |
172 | 0 | cmdq_error(item, "adjustment %s", cause); |
173 | 0 | free(cause); |
174 | 0 | return (CMD_RETURN_ERROR); |
175 | 0 | } |
176 | 0 | } else { |
177 | 0 | if (flag == 'L' || flag == 'U') |
178 | 0 | adjust = -adjust; |
179 | 0 | layout_resize_pane(wp, type, adjust, 1); |
180 | 0 | } |
181 | 0 | } |
182 | | |
183 | 0 | if (lc->parent != NULL) |
184 | 0 | layout_fix_offsets(w); |
185 | 0 | layout_fix_panes(w, NULL); |
186 | 0 | events_fire_window("window-layout-changed", w); |
187 | 0 | server_redraw_window(w); |
188 | |
|
189 | 0 | return (CMD_RETURN_NORMAL); |
190 | 0 | } |
191 | | |
192 | | static enum cmd_retval |
193 | | cmd_resize_pane_mouse_update(__unused struct cmd *self, struct cmdq_item *item) |
194 | 0 | { |
195 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
196 | 0 | struct key_event *event = cmdq_get_event(item); |
197 | 0 | struct window_pane *wp = target->wp; |
198 | 0 | struct winlink *wl = target->wl; |
199 | 0 | struct window *w = wl->window; |
200 | 0 | struct client *c = cmdq_get_client(item); |
201 | 0 | struct session *s = target->s; |
202 | |
|
203 | 0 | if (!event->m.valid) |
204 | 0 | return (CMD_RETURN_NORMAL); |
205 | 0 | wp = cmd_mouse_pane(&event->m, &s, NULL); |
206 | 0 | if (wp == NULL || c == NULL || c->session != s) |
207 | 0 | return (CMD_RETURN_NORMAL); |
208 | | |
209 | 0 | if (!window_pane_is_floating(wp)) { |
210 | 0 | c->tty.mouse_drag_update = cmd_resize_pane_mouse_resize_tiled; |
211 | 0 | cmd_resize_pane_mouse_resize_tiled(c, &event->m); |
212 | 0 | return (CMD_RETURN_NORMAL); |
213 | 0 | } |
214 | | |
215 | 0 | window_redraw_active_switch(w, wp); |
216 | 0 | window_set_active_pane(w, wp, 1); |
217 | |
|
218 | 0 | c->tty.mouse_drag_update = cmd_resize_pane_mouse_resize_move_floating; |
219 | 0 | cmd_resize_pane_mouse_resize_move_floating(c, &event->m); |
220 | 0 | return (CMD_RETURN_NORMAL); |
221 | 0 | } |
222 | | |
223 | | /* |
224 | | * Resizes or moves the pane by dragging. Resize a floating pane by dragging |
225 | | * the borders or corners. Grabbing an edge only resizes that axis (special |
226 | | * case). Moves the pane if dragging the top border. Since characters are |
227 | | * generally rectangular, to make it easier to grab the corner, the character |
228 | | * next to the corner is also considered the corner. |
229 | | */ |
230 | | static void |
231 | | cmd_resize_pane_mouse_resize_move_floating(struct client *c, |
232 | | struct mouse_event *m) |
233 | 0 | { |
234 | 0 | struct winlink *wl; |
235 | 0 | struct window *w; |
236 | 0 | struct window_pane *wp; |
237 | 0 | struct layout_cell *lc; |
238 | 0 | int y, ly, x, lx, sx, sy, new_sx, new_sy; |
239 | 0 | int left, right; |
240 | 0 | int new_xoff, new_yoff, resizes = 0; |
241 | |
|
242 | 0 | wp = cmd_mouse_pane(m, NULL, &wl); |
243 | 0 | if (wp == NULL) { |
244 | 0 | c->tty.mouse_drag_update = NULL; |
245 | 0 | return; |
246 | 0 | } |
247 | 0 | w = wl->window; |
248 | 0 | lc = wp->layout_cell; |
249 | 0 | sx = wp->sx; |
250 | 0 | sy = wp->sy; |
251 | 0 | left = wp->xoff - 1; |
252 | 0 | right = wp->xoff + sx; |
253 | 0 | if (window_pane_scrollbar_reserve(wp) && |
254 | 0 | w->sb_pos == PANE_SCROLLBARS_LEFT) { |
255 | 0 | left -= wp->scrollbar_style.width + wp->scrollbar_style.pad; |
256 | 0 | } else if (window_pane_scrollbar_reserve(wp) && |
257 | 0 | w->sb_pos == PANE_SCROLLBARS_RIGHT) { |
258 | 0 | right += wp->scrollbar_style.width + wp->scrollbar_style.pad; |
259 | 0 | } |
260 | |
|
261 | 0 | y = m->y + m->oy; x = m->x + m->ox; |
262 | 0 | if (m->statusat == 0 && y >= (int)m->statuslines) |
263 | 0 | y -= m->statuslines; |
264 | 0 | else if (m->statusat > 0 && y >= m->statusat) |
265 | 0 | y = m->statusat - 1; |
266 | 0 | ly = m->ly + m->oy; lx = m->lx + m->ox; |
267 | 0 | if (m->statusat == 0 && ly >= (int)m->statuslines) |
268 | 0 | ly -= m->statuslines; |
269 | 0 | else if (m->statusat > 0 && ly >= m->statusat) |
270 | 0 | ly = m->statusat - 1; |
271 | |
|
272 | 0 | if ((lx == left || lx == left + 1) && ly == wp->yoff - 1) { |
273 | | /* Top left corner. */ |
274 | 0 | new_sx = lc->g.sx + (lx - x); |
275 | 0 | if (new_sx < PANE_MINIMUM) |
276 | 0 | new_sx = PANE_MINIMUM; |
277 | 0 | new_sy = lc->g.sy + (ly - y); |
278 | 0 | if (new_sy < PANE_MINIMUM) |
279 | 0 | new_sy = PANE_MINIMUM; |
280 | 0 | new_xoff = x + 1; /* because mouse is on border at xoff - 1 */ |
281 | 0 | new_yoff = y + 1; |
282 | 0 | layout_set_size(lc, new_sx, new_sy, new_xoff, new_yoff); |
283 | 0 | resizes++; |
284 | 0 | } else if ((lx == right + 1 || lx == right) && |
285 | 0 | ly == wp->yoff - 1) { |
286 | | /* Top right corner. */ |
287 | 0 | new_sx = x - lc->g.xoff; |
288 | 0 | if (new_sx < PANE_MINIMUM) |
289 | 0 | new_sx = PANE_MINIMUM; |
290 | 0 | new_sy = lc->g.sy + (ly - y); |
291 | 0 | if (new_sy < PANE_MINIMUM) |
292 | 0 | new_sy = PANE_MINIMUM; |
293 | 0 | new_yoff = y + 1; |
294 | 0 | layout_set_size(lc, new_sx, new_sy, lc->g.xoff, new_yoff); |
295 | 0 | resizes++; |
296 | 0 | } else if ((lx == left || lx == left + 1) && |
297 | 0 | ly == wp->yoff + sy) { |
298 | | /* Bottom left corner. */ |
299 | 0 | new_sx = lc->g.sx + (lx - x); |
300 | 0 | if (new_sx < PANE_MINIMUM) |
301 | 0 | new_sx = PANE_MINIMUM; |
302 | 0 | new_sy = y - lc->g.yoff; |
303 | 0 | if (new_sy < PANE_MINIMUM) |
304 | 0 | return; |
305 | 0 | new_xoff = x + 1; |
306 | 0 | layout_set_size(lc, new_sx, new_sy, new_xoff, lc->g.yoff); |
307 | 0 | resizes++; |
308 | 0 | } else if ((lx == right + 1 || lx == right) && |
309 | 0 | ly == wp->yoff + sy) { |
310 | | /* Bottom right corner. */ |
311 | 0 | new_sx = x - lc->g.xoff; |
312 | 0 | if (new_sx < PANE_MINIMUM) |
313 | 0 | new_sx = PANE_MINIMUM; |
314 | 0 | new_sy = y - lc->g.yoff; |
315 | 0 | if (new_sy < PANE_MINIMUM) |
316 | 0 | new_sy = PANE_MINIMUM; |
317 | 0 | layout_set_size(lc, new_sx, new_sy, lc->g.xoff, lc->g.yoff); |
318 | 0 | resizes++; |
319 | 0 | } else if (lx == right) { |
320 | | /* Right border. */ |
321 | 0 | new_sx = x - lc->g.xoff; |
322 | 0 | if (new_sx < PANE_MINIMUM) |
323 | 0 | return; |
324 | 0 | layout_set_size(lc, new_sx, lc->g.sy, lc->g.xoff, lc->g.yoff); |
325 | 0 | resizes++; |
326 | 0 | } else if (lx == left) { |
327 | | /* Left border. */ |
328 | 0 | new_sx = lc->g.sx + (lx - x); |
329 | 0 | if (new_sx < PANE_MINIMUM) |
330 | 0 | return; |
331 | 0 | new_xoff = x + 1; |
332 | 0 | layout_set_size(lc, new_sx, lc->g.sy, new_xoff, lc->g.yoff); |
333 | 0 | resizes++; |
334 | 0 | } else if (ly == wp->yoff + sy) { |
335 | | /* Bottom border. */ |
336 | 0 | new_sy = y - lc->g.yoff; |
337 | 0 | if (new_sy < PANE_MINIMUM) |
338 | 0 | return; |
339 | 0 | layout_set_size(lc, lc->g.sx, new_sy, lc->g.xoff, lc->g.yoff); |
340 | 0 | resizes++; |
341 | 0 | } else if (ly == wp->yoff - 1) { |
342 | | /* Top border (move instead of resize). */ |
343 | 0 | new_xoff = lc->g.xoff + (x - lx); |
344 | 0 | new_yoff = y + 1; |
345 | 0 | layout_set_size(lc, lc->g.sx, lc->g.sy, new_xoff, new_yoff); |
346 | 0 | resizes++; |
347 | 0 | } |
348 | 0 | if (resizes != 0) { |
349 | 0 | layout_fix_panes(w, NULL); |
350 | 0 | server_redraw_window(w); |
351 | 0 | server_redraw_window_borders(w); |
352 | 0 | } |
353 | 0 | } |
354 | | |
355 | | static void |
356 | | cmd_resize_pane_mouse_resize_tiled(struct client *c, struct mouse_event *m) |
357 | 0 | { |
358 | 0 | struct winlink *wl; |
359 | 0 | struct window *w; |
360 | 0 | u_int y, ly, x, lx; |
361 | 0 | static const int offsets[][2] = { |
362 | 0 | { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 }, |
363 | 0 | }; |
364 | 0 | struct layout_cell *cells[nitems(offsets)], *lc; |
365 | 0 | u_int ncells = 0, i, j, resizes = 0; |
366 | 0 | enum layout_type type; |
367 | |
|
368 | 0 | wl = cmd_mouse_window(m, NULL); |
369 | 0 | if (wl == NULL) { |
370 | 0 | c->tty.mouse_drag_update = NULL; |
371 | 0 | return; |
372 | 0 | } |
373 | 0 | w = wl->window; |
374 | |
|
375 | 0 | y = m->y + m->oy; x = m->x + m->ox; |
376 | 0 | if (m->statusat == 0 && y >= m->statuslines) |
377 | 0 | y -= m->statuslines; |
378 | 0 | else if (m->statusat > 0 && y >= (u_int)m->statusat) |
379 | 0 | y = m->statusat - 1; |
380 | 0 | ly = m->ly + m->oy; lx = m->lx + m->ox; |
381 | 0 | if (m->statusat == 0 && ly >= m->statuslines) |
382 | 0 | ly -= m->statuslines; |
383 | 0 | else if (m->statusat > 0 && ly >= (u_int)m->statusat) |
384 | 0 | ly = m->statusat - 1; |
385 | |
|
386 | 0 | for (i = 0; i < nitems(cells); i++) { |
387 | 0 | lc = layout_search_by_border(w->layout_root, lx + offsets[i][0], |
388 | 0 | ly + offsets[i][1]); |
389 | 0 | if (lc == NULL) |
390 | 0 | continue; |
391 | | |
392 | 0 | for (j = 0; j < ncells; j++) { |
393 | 0 | if (cells[j] == lc) { |
394 | 0 | lc = NULL; |
395 | 0 | break; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | if (lc == NULL) |
399 | 0 | continue; |
400 | | |
401 | 0 | cells[ncells] = lc; |
402 | 0 | ncells++; |
403 | 0 | } |
404 | 0 | if (ncells == 0) |
405 | 0 | return; |
406 | | |
407 | 0 | for (i = 0; i < ncells; i++) { |
408 | 0 | type = cells[i]->parent->type; |
409 | 0 | if (y != ly && type == LAYOUT_TOPBOTTOM) { |
410 | 0 | layout_resize_layout(w, cells[i], type, y - ly, 0); |
411 | 0 | resizes++; |
412 | 0 | } else if (x != lx && type == LAYOUT_LEFTRIGHT) { |
413 | 0 | layout_resize_layout(w, cells[i], type, x - lx, 0); |
414 | 0 | resizes++; |
415 | 0 | } |
416 | 0 | } |
417 | 0 | if (resizes != 0) |
418 | 0 | server_redraw_window(w); |
419 | 0 | } |