xref: /openbsd-src/usr.bin/tmux/cmd-resize-pane.c (revision fcde59b201a29a2b4570b00b71e7aa25d61cb5c1)
1 /* $OpenBSD: cmd-resize-pane.c,v 1.48 2020/05/25 18:17:14 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 <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Increase or decrease pane size.
28  */
29 
30 static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
31 
32 static void	cmd_resize_pane_mouse_update(struct client *,
33 		    struct mouse_event *);
34 
35 const struct cmd_entry cmd_resize_pane_entry = {
36 	.name = "resize-pane",
37 	.alias = "resizep",
38 
39 	.args = { "DLMRTt:Ux:y:Z", 0, 1 },
40 	.usage = "[-DLMRTUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
41 		 "[adjustment]",
42 
43 	.target = { 't', CMD_FIND_PANE, 0 },
44 
45 	.flags = CMD_AFTERHOOK,
46 	.exec = cmd_resize_pane_exec
47 };
48 
49 static enum cmd_retval
50 cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
51 {
52 	struct args		*args = cmd_get_args(self);
53 	struct cmd_find_state	*target = cmdq_get_target(item);
54 	struct key_event	*event = cmdq_get_event(item);
55 	struct window_pane	*wp = target->wp;
56 	struct winlink		*wl = target->wl;
57 	struct window		*w = wl->window;
58 	struct client		*c = cmdq_get_client(item);
59 	struct session		*s = target->s;
60 	const char	       	*errstr;
61 	char			*cause;
62 	u_int			 adjust;
63 	int			 x, y;
64 	struct grid		*gd = wp->base.grid;
65 
66 	if (args_has(args, 'T')) {
67 		if (!TAILQ_EMPTY(&wp->modes))
68 			return (CMD_RETURN_NORMAL);
69 		adjust = screen_size_y(&wp->base) - 1 - wp->base.cy;
70 		if (adjust > gd->hsize)
71 			adjust = gd->hsize;
72 		grid_remove_history(gd, adjust);
73 		wp->base.cy += adjust;
74 		wp->flags |= PANE_REDRAW;
75 		return (CMD_RETURN_NORMAL);
76 	}
77 
78 	if (args_has(args, 'M')) {
79 		if (!event->m.valid || cmd_mouse_window(&event->m, &s) == NULL)
80 			return (CMD_RETURN_NORMAL);
81 		if (c == NULL || c->session != s)
82 			return (CMD_RETURN_NORMAL);
83 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
84 		cmd_resize_pane_mouse_update(c, &event->m);
85 		return (CMD_RETURN_NORMAL);
86 	}
87 
88 	if (args_has(args, 'Z')) {
89 		if (w->flags & WINDOW_ZOOMED)
90 			window_unzoom(w);
91 		else
92 			window_zoom(wp);
93 		server_redraw_window(w);
94 		return (CMD_RETURN_NORMAL);
95 	}
96 	server_unzoom_window(w);
97 
98 	if (args->argc == 0)
99 		adjust = 1;
100 	else {
101 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
102 		if (errstr != NULL) {
103 			cmdq_error(item, "adjustment %s", errstr);
104 			return (CMD_RETURN_ERROR);
105 		}
106 	}
107 
108 	if (args_has(args, 'x')) {
109 		x = args_percentage(args, 'x', 0, INT_MAX, w->sx, &cause);
110 		if (cause != NULL) {
111 			cmdq_error(item, "width %s", cause);
112 			free(cause);
113 			return (CMD_RETURN_ERROR);
114 		}
115 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
116 	}
117 	if (args_has(args, 'y')) {
118 		y = args_percentage(args, 'y', 0, INT_MAX, w->sy, &cause);
119 		if (cause != NULL) {
120 			cmdq_error(item, "height %s", cause);
121 			free(cause);
122 			return (CMD_RETURN_ERROR);
123 		}
124 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
125 	}
126 
127 	if (args_has(args, 'L'))
128 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
129 	else if (args_has(args, 'R'))
130 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
131 	else if (args_has(args, 'U'))
132 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
133 	else if (args_has(args, 'D'))
134 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
135 	server_redraw_window(wl->window);
136 
137 	return (CMD_RETURN_NORMAL);
138 }
139 
140 static void
141 cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
142 {
143 	struct winlink		*wl;
144 	struct window		*w;
145 	u_int			 y, ly, x, lx;
146 	static const int         offsets[][2] = {
147 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
148 	};
149 	struct layout_cell	*cells[nitems(offsets)], *lc;
150 	u_int			 ncells = 0, i, j, resizes = 0;
151 	enum layout_type	 type;
152 
153 	wl = cmd_mouse_window(m, NULL);
154 	if (wl == NULL) {
155 		c->tty.mouse_drag_update = NULL;
156 		return;
157 	}
158 	w = wl->window;
159 
160 	y = m->y + m->oy; x = m->x + m->ox;
161 	if (m->statusat == 0 && y >= m->statuslines)
162 		y -= m->statuslines;
163 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
164 		y = m->statusat - 1;
165 	ly = m->ly + m->oy; lx = m->lx + m->ox;
166 	if (m->statusat == 0 && ly >= m->statuslines)
167 		ly -= m->statuslines;
168 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
169 		ly = m->statusat - 1;
170 
171 	for (i = 0; i < nitems(cells); i++) {
172 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
173 		    ly + offsets[i][1]);
174 		if (lc == NULL)
175 			continue;
176 
177 		for (j = 0; j < ncells; j++) {
178 			if (cells[j] == lc) {
179 				lc = NULL;
180 				break;
181 			}
182 		}
183 		if (lc == NULL)
184 			continue;
185 
186 		cells[ncells] = lc;
187 		ncells++;
188 	}
189 	if (ncells == 0)
190 		return;
191 
192 	for (i = 0; i < ncells; i++) {
193 		type = cells[i]->parent->type;
194 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
195 			layout_resize_layout(w, cells[i], type, y - ly, 0);
196 			resizes++;
197 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
198 			layout_resize_layout(w, cells[i], type, x - lx, 0);
199 			resizes++;
200 		}
201 	}
202 	if (resizes != 0)
203 		server_redraw_window(w);
204 }
205