Annotation of src/usr.bin/tmux/cmd-resize-pane.c, Revision 1.37
1.37 ! nicm 1: /* $OpenBSD: cmd-resize-pane.c,v 1.36 2019/05/03 16:51:29 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>
22:
23: #include "tmux.h"
24:
25: /*
26: * Increase or decrease pane size.
27: */
28:
1.28 nicm 29: static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
1.1 nicm 30:
1.25 nicm 31: static void cmd_resize_pane_mouse_update(struct client *,
32: struct mouse_event *);
1.18 nicm 33:
1.1 nicm 34: const struct cmd_entry cmd_resize_pane_entry = {
1.20 nicm 35: .name = "resize-pane",
36: .alias = "resizep",
37:
38: .args = { "DLMRt:Ux:y:Z", 0, 1 },
39: .usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
40: "[adjustment]",
41:
1.30 nicm 42: .target = { 't', CMD_FIND_PANE, 0 },
1.21 nicm 43:
1.26 nicm 44: .flags = CMD_AFTERHOOK,
1.20 nicm 45: .exec = cmd_resize_pane_exec
1.1 nicm 46: };
47:
1.25 nicm 48: static enum cmd_retval
1.28 nicm 49: cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 50: {
1.9 nicm 51: struct args *args = self->args;
1.29 nicm 52: struct cmdq_shared *shared = item->shared;
1.30 nicm 53: struct window_pane *wp = item->target.wp;
54: struct winlink *wl = item->target.wl;
1.19 nicm 55: struct window *w = wl->window;
1.28 nicm 56: struct client *c = item->client;
1.30 nicm 57: struct session *s = item->target.s;
1.1 nicm 58: const char *errstr;
1.13 nicm 59: char *cause;
1.1 nicm 60: u_int adjust;
1.13 nicm 61: int x, y;
1.1 nicm 62:
1.18 nicm 63: if (args_has(args, 'M')) {
1.29 nicm 64: if (cmd_mouse_window(&shared->mouse, &s) == NULL)
1.18 nicm 65: return (CMD_RETURN_NORMAL);
66: if (c == NULL || c->session != s)
67: return (CMD_RETURN_NORMAL);
68: c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
1.29 nicm 69: cmd_resize_pane_mouse_update(c, &shared->mouse);
1.18 nicm 70: return (CMD_RETURN_NORMAL);
71: }
72:
1.15 nicm 73: if (args_has(args, 'Z')) {
74: if (w->flags & WINDOW_ZOOMED)
75: window_unzoom(w);
76: else
77: window_zoom(wp);
78: server_redraw_window(w);
79: server_status_window(w);
80: return (CMD_RETURN_NORMAL);
81: }
82: server_unzoom_window(w);
1.1 nicm 83:
1.9 nicm 84: if (args->argc == 0)
1.1 nicm 85: adjust = 1;
86: else {
1.9 nicm 87: adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
1.1 nicm 88: if (errstr != NULL) {
1.28 nicm 89: cmdq_error(item, "adjustment %s", errstr);
1.11 nicm 90: return (CMD_RETURN_ERROR);
1.1 nicm 91: }
1.13 nicm 92: }
93:
1.35 nicm 94: if (args_has(args, 'x')) {
95: x = args_strtonum(args, 'x', PANE_MINIMUM, INT_MAX, &cause);
1.13 nicm 96: if (cause != NULL) {
1.28 nicm 97: cmdq_error(item, "width %s", cause);
1.13 nicm 98: free(cause);
99: return (CMD_RETURN_ERROR);
100: }
101: layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
102: }
1.35 nicm 103: if (args_has(args, 'y')) {
104: y = args_strtonum(args, 'y', PANE_MINIMUM, INT_MAX, &cause);
1.13 nicm 105: if (cause != NULL) {
1.28 nicm 106: cmdq_error(item, "height %s", cause);
1.13 nicm 107: free(cause);
108: return (CMD_RETURN_ERROR);
109: }
110: layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1 nicm 111: }
112:
1.35 nicm 113: if (args_has(args, 'L'))
1.24 nicm 114: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.35 nicm 115: else if (args_has(args, 'R'))
1.24 nicm 116: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.35 nicm 117: else if (args_has(args, 'U'))
1.24 nicm 118: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.35 nicm 119: else if (args_has(args, 'D'))
1.24 nicm 120: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1 nicm 121: server_redraw_window(wl->window);
122:
1.11 nicm 123: return (CMD_RETURN_NORMAL);
1.18 nicm 124: }
125:
1.25 nicm 126: static void
1.18 nicm 127: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
128: {
129: struct winlink *wl;
1.32 nicm 130: struct window *w;
131: u_int y, ly, x, lx;
1.34 nicm 132: static const int offsets[][2] = {
133: { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
134: };
135: struct layout_cell *cells[nitems(offsets)], *lc;
1.33 nicm 136: u_int ncells = 0, i, j, resizes = 0;
137: enum layout_type type;
1.18 nicm 138:
139: wl = cmd_mouse_window(m, NULL);
140: if (wl == NULL) {
141: c->tty.mouse_drag_update = NULL;
142: return;
143: }
1.32 nicm 144: w = wl->window;
1.18 nicm 145:
1.37 ! nicm 146: y = m->y + m->oy; x = m->x + m->ox;
1.18 nicm 147: if (m->statusat == 0 && y > 0)
148: y--;
149: else if (m->statusat > 0 && y >= (u_int)m->statusat)
150: y = m->statusat - 1;
1.36 nicm 151: ly = m->ly + m->oy; lx = m->lx + m->ox;
1.18 nicm 152: if (m->statusat == 0 && ly > 0)
153: ly--;
154: else if (m->statusat > 0 && ly >= (u_int)m->statusat)
155: ly = m->statusat - 1;
156:
1.33 nicm 157: for (i = 0; i < nitems(cells); i++) {
158: lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
159: ly + offsets[i][1]);
160: if (lc == NULL)
161: continue;
162:
163: for (j = 0; j < ncells; j++) {
164: if (cells[j] == lc) {
165: lc = NULL;
166: break;
167: }
168: }
169: if (lc == NULL)
170: continue;
1.32 nicm 171:
1.33 nicm 172: cells[ncells] = lc;
173: ncells++;
174: }
175: if (ncells == 0)
1.31 nicm 176: return;
1.32 nicm 177:
1.33 nicm 178: for (i = 0; i < ncells; i++) {
179: type = cells[i]->parent->type;
180: if (y != ly && type == LAYOUT_TOPBOTTOM) {
181: layout_resize_layout(w, cells[i], type, y - ly, 0);
182: resizes++;
183: } else if (x != lx && type == LAYOUT_LEFTRIGHT) {
184: layout_resize_layout(w, cells[i], type, x - lx, 0);
185: resizes++;
186: }
187: }
188: if (resizes != 0)
189: server_redraw_window(w);
1.1 nicm 190: }