xref: /openbsd-src/usr.bin/tmux/cmd-resize-pane.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-resize-pane.c,v 1.39 2019/10/15 08:25:37 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 = { "DLMRt:Ux:y:Z", 0, 1 },
40 	.usage = "[-DLMRUZ] [-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 = self->args;
53 	struct cmdq_shared	*shared = item->shared;
54 	struct window_pane	*wp = item->target.wp;
55 	struct winlink		*wl = item->target.wl;
56 	struct window		*w = wl->window;
57 	struct client		*c = item->client;
58 	struct session		*s = item->target.s;
59 	const char	       	*errstr, *p;
60 	char			*cause, *copy;
61 	u_int			 adjust;
62 	int			 x, y, percentage;
63 	size_t			 plen;
64 
65 	if (args_has(args, 'M')) {
66 		if (cmd_mouse_window(&shared->mouse, &s) == NULL)
67 			return (CMD_RETURN_NORMAL);
68 		if (c == NULL || c->session != s)
69 			return (CMD_RETURN_NORMAL);
70 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
71 		cmd_resize_pane_mouse_update(c, &shared->mouse);
72 		return (CMD_RETURN_NORMAL);
73 	}
74 
75 	if (args_has(args, 'Z')) {
76 		if (w->flags & WINDOW_ZOOMED)
77 			window_unzoom(w);
78 		else
79 			window_zoom(wp);
80 		server_redraw_window(w);
81 		server_status_window(w);
82 		return (CMD_RETURN_NORMAL);
83 	}
84 	server_unzoom_window(w);
85 
86 	if (args->argc == 0)
87 		adjust = 1;
88 	else {
89 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
90 		if (errstr != NULL) {
91 			cmdq_error(item, "adjustment %s", errstr);
92 			return (CMD_RETURN_ERROR);
93 		}
94 	}
95 
96 	if ((p = args_get(args, 'x')) != NULL) {
97 		plen = strlen(p);
98 		if (p[plen - 1] == '%') {
99 			copy = xstrdup(p);
100 			copy[plen - 1] = '\0';
101 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
102 			free(copy);
103 			if (errstr != NULL) {
104 				cmdq_error(item, "width %s", errstr);
105 				return (CMD_RETURN_ERROR);
106 			}
107 			x = (w->sx * percentage) / 100;
108 			if (x < PANE_MINIMUM)
109 				x = PANE_MINIMUM;
110 			if (x > INT_MAX)
111 				x = INT_MAX;
112 		} else {
113 			x = args_strtonum(args, 'x', PANE_MINIMUM, INT_MAX,
114 			    &cause);
115 			if (cause != NULL) {
116 				cmdq_error(item, "width %s", cause);
117 				free(cause);
118 				return (CMD_RETURN_ERROR);
119 			}
120 		}
121 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
122 	}
123 	if ((p = args_get(args, 'y')) != NULL) {
124 		plen = strlen(p);
125 		if (p[plen - 1] == '%') {
126 			copy = xstrdup(p);
127 			copy[plen - 1] = '\0';
128 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
129 			free(copy);
130 			if (errstr != NULL) {
131 				cmdq_error(item, "height %s", errstr);
132 				return (CMD_RETURN_ERROR);
133 			}
134 			y = (w->sy * percentage) / 100;
135 			if (y < PANE_MINIMUM)
136 				y = PANE_MINIMUM;
137 			if (y > INT_MAX)
138 				y = INT_MAX;
139 		}
140 		else {
141 			y = args_strtonum(args, 'y', PANE_MINIMUM, INT_MAX,
142 			    &cause);
143 			if (cause != NULL) {
144 				cmdq_error(item, "height %s", cause);
145 				free(cause);
146 				return (CMD_RETURN_ERROR);
147 			}
148 		}
149 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
150 	}
151 
152 	if (args_has(args, 'L'))
153 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
154 	else if (args_has(args, 'R'))
155 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
156 	else if (args_has(args, 'U'))
157 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
158 	else if (args_has(args, 'D'))
159 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
160 	server_redraw_window(wl->window);
161 
162 	return (CMD_RETURN_NORMAL);
163 }
164 
165 static void
166 cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
167 {
168 	struct winlink		*wl;
169 	struct window		*w;
170 	u_int			 y, ly, x, lx;
171 	static const int         offsets[][2] = {
172 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
173 	};
174 	struct layout_cell	*cells[nitems(offsets)], *lc;
175 	u_int			 ncells = 0, i, j, resizes = 0;
176 	enum layout_type	 type;
177 
178 	wl = cmd_mouse_window(m, NULL);
179 	if (wl == NULL) {
180 		c->tty.mouse_drag_update = NULL;
181 		return;
182 	}
183 	w = wl->window;
184 
185 	y = m->y + m->oy; x = m->x + m->ox;
186 	if (m->statusat == 0 && y >= m->statuslines)
187 		y -= m->statuslines;
188 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
189 		y = m->statusat - 1;
190 	ly = m->ly + m->oy; lx = m->lx + m->ox;
191 	if (m->statusat == 0 && ly >= m->statuslines)
192 		ly -= m->statuslines;
193 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
194 		ly = m->statusat - 1;
195 
196 	for (i = 0; i < nitems(cells); i++) {
197 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
198 		    ly + offsets[i][1]);
199 		if (lc == NULL)
200 			continue;
201 
202 		for (j = 0; j < ncells; j++) {
203 			if (cells[j] == lc) {
204 				lc = NULL;
205 				break;
206 			}
207 		}
208 		if (lc == NULL)
209 			continue;
210 
211 		cells[ncells] = lc;
212 		ncells++;
213 	}
214 	if (ncells == 0)
215 		return;
216 
217 	for (i = 0; i < ncells; i++) {
218 		type = cells[i]->parent->type;
219 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
220 			layout_resize_layout(w, cells[i], type, y - ly, 0);
221 			resizes++;
222 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
223 			layout_resize_layout(w, cells[i], type, x - lx, 0);
224 			resizes++;
225 		}
226 	}
227 	if (resizes != 0)
228 		server_redraw_window(w);
229 }
230