Annotation of src/usr.bin/tmux/cmd-resize-pane.c, Revision 1.39
1.39 ! nicm 1: /* $OpenBSD: cmd-resize-pane.c,v 1.38 2019/07/06 20:56:34 nicm Exp $ */
1.1 nicm 2:
3: /*
1.22 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.39 ! nicm 22: #include <string.h>
1.1 nicm 23:
24: #include "tmux.h"
25:
26: /*
27: * Increase or decrease pane size.
28: */
29:
1.28 nicm 30: static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 31:
1.25 nicm 32: static void cmd_resize_pane_mouse_update(struct client *,
33: struct mouse_event *);
1.18 nicm 34:
1.1 nicm 35: const struct cmd_entry cmd_resize_pane_entry = {
1.20 nicm 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:
1.30 nicm 43: .target = { 't', CMD_FIND_PANE, 0 },
1.21 nicm 44:
1.26 nicm 45: .flags = CMD_AFTERHOOK,
1.20 nicm 46: .exec = cmd_resize_pane_exec
1.1 nicm 47: };
48:
1.25 nicm 49: static enum cmd_retval
1.28 nicm 50: cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 51: {
1.9 nicm 52: struct args *args = self->args;
1.29 nicm 53: struct cmdq_shared *shared = item->shared;
1.30 nicm 54: struct window_pane *wp = item->target.wp;
55: struct winlink *wl = item->target.wl;
1.19 nicm 56: struct window *w = wl->window;
1.28 nicm 57: struct client *c = item->client;
1.30 nicm 58: struct session *s = item->target.s;
1.39 ! nicm 59: const char *errstr, *p;
! 60: char *cause, *copy;
1.1 nicm 61: u_int adjust;
1.39 ! nicm 62: int x, y, percentage;
! 63: size_t plen;
1.1 nicm 64:
1.18 nicm 65: if (args_has(args, 'M')) {
1.29 nicm 66: if (cmd_mouse_window(&shared->mouse, &s) == NULL)
1.18 nicm 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;
1.29 nicm 71: cmd_resize_pane_mouse_update(c, &shared->mouse);
1.18 nicm 72: return (CMD_RETURN_NORMAL);
73: }
74:
1.15 nicm 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);
1.1 nicm 85:
1.9 nicm 86: if (args->argc == 0)
1.1 nicm 87: adjust = 1;
88: else {
1.9 nicm 89: adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
1.1 nicm 90: if (errstr != NULL) {
1.28 nicm 91: cmdq_error(item, "adjustment %s", errstr);
1.11 nicm 92: return (CMD_RETURN_ERROR);
1.1 nicm 93: }
1.13 nicm 94: }
95:
1.39 ! nicm 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: }
1.13 nicm 120: }
121: layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
122: }
1.39 ! nicm 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: }
1.13 nicm 148: }
149: layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1 nicm 150: }
151:
1.35 nicm 152: if (args_has(args, 'L'))
1.24 nicm 153: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.35 nicm 154: else if (args_has(args, 'R'))
1.24 nicm 155: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.35 nicm 156: else if (args_has(args, 'U'))
1.24 nicm 157: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.35 nicm 158: else if (args_has(args, 'D'))
1.24 nicm 159: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1 nicm 160: server_redraw_window(wl->window);
161:
1.11 nicm 162: return (CMD_RETURN_NORMAL);
1.18 nicm 163: }
164:
1.25 nicm 165: static void
1.18 nicm 166: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
167: {
168: struct winlink *wl;
1.32 nicm 169: struct window *w;
170: u_int y, ly, x, lx;
1.34 nicm 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;
1.33 nicm 175: u_int ncells = 0, i, j, resizes = 0;
176: enum layout_type type;
1.18 nicm 177:
178: wl = cmd_mouse_window(m, NULL);
179: if (wl == NULL) {
180: c->tty.mouse_drag_update = NULL;
181: return;
182: }
1.32 nicm 183: w = wl->window;
1.18 nicm 184:
1.37 nicm 185: y = m->y + m->oy; x = m->x + m->ox;
1.38 nicm 186: if (m->statusat == 0 && y >= m->statuslines)
187: y -= m->statuslines;
1.18 nicm 188: else if (m->statusat > 0 && y >= (u_int)m->statusat)
189: y = m->statusat - 1;
1.36 nicm 190: ly = m->ly + m->oy; lx = m->lx + m->ox;
1.38 nicm 191: if (m->statusat == 0 && ly >= m->statuslines)
192: ly -= m->statuslines;
1.18 nicm 193: else if (m->statusat > 0 && ly >= (u_int)m->statusat)
194: ly = m->statusat - 1;
195:
1.33 nicm 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;
1.32 nicm 210:
1.33 nicm 211: cells[ncells] = lc;
212: ncells++;
213: }
214: if (ncells == 0)
1.31 nicm 215: return;
1.32 nicm 216:
1.33 nicm 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);
1.1 nicm 229: }