Annotation of src/usr.bin/tmux/cmd-resize-pane.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: cmd-resize-pane.c,v 1.24 2016/10/10 17:28:30 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.25 ! nicm 29: static enum cmd_retval cmd_resize_pane_exec(struct cmd *, struct cmd_q *);
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.21 nicm 42: .tflag = CMD_PANE,
43:
44: .flags = 0,
1.20 nicm 45: .exec = cmd_resize_pane_exec
1.1 nicm 46: };
47:
1.25 ! nicm 48: static enum cmd_retval
1.14 nicm 49: cmd_resize_pane_exec(struct cmd *self, struct cmd_q *cmdq)
1.1 nicm 50: {
1.9 nicm 51: struct args *args = self->args;
1.19 nicm 52: struct window_pane *wp = cmdq->state.tflag.wp;
53: struct winlink *wl = cmdq->state.tflag.wl;
54: struct window *w = wl->window;
1.18 nicm 55: struct client *c = cmdq->client;
1.19 nicm 56: struct session *s = cmdq->state.tflag.s;
1.1 nicm 57: const char *errstr;
1.13 nicm 58: char *cause;
1.1 nicm 59: u_int adjust;
1.13 nicm 60: int x, y;
1.1 nicm 61:
1.18 nicm 62: if (args_has(args, 'M')) {
63: if (cmd_mouse_window(&cmdq->item->mouse, &s) == NULL)
64: return (CMD_RETURN_NORMAL);
65: if (c == NULL || c->session != s)
66: return (CMD_RETURN_NORMAL);
67: c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
68: cmd_resize_pane_mouse_update(c, &cmdq->item->mouse);
69: return (CMD_RETURN_NORMAL);
70: }
71:
1.15 nicm 72: if (args_has(args, 'Z')) {
73: if (w->flags & WINDOW_ZOOMED)
74: window_unzoom(w);
75: else
76: window_zoom(wp);
77: server_redraw_window(w);
78: server_status_window(w);
79: return (CMD_RETURN_NORMAL);
80: }
81: server_unzoom_window(w);
1.1 nicm 82:
1.9 nicm 83: if (args->argc == 0)
1.1 nicm 84: adjust = 1;
85: else {
1.9 nicm 86: adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
1.1 nicm 87: if (errstr != NULL) {
1.14 nicm 88: cmdq_error(cmdq, "adjustment %s", errstr);
1.11 nicm 89: return (CMD_RETURN_ERROR);
1.1 nicm 90: }
1.13 nicm 91: }
92:
93: if (args_has(self->args, 'x')) {
94: x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
95: &cause);
96: if (cause != NULL) {
1.14 nicm 97: cmdq_error(cmdq, "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: }
103: if (args_has(self->args, 'y')) {
104: y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
105: &cause);
106: if (cause != NULL) {
1.14 nicm 107: cmdq_error(cmdq, "height %s", cause);
1.13 nicm 108: free(cause);
109: return (CMD_RETURN_ERROR);
110: }
111: layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
1.1 nicm 112: }
113:
1.9 nicm 114: if (args_has(self->args, 'L'))
1.24 nicm 115: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
1.9 nicm 116: else if (args_has(self->args, 'R'))
1.24 nicm 117: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
1.9 nicm 118: else if (args_has(self->args, 'U'))
1.24 nicm 119: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
1.9 nicm 120: else if (args_has(self->args, 'D'))
1.24 nicm 121: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
1.1 nicm 122: server_redraw_window(wl->window);
123:
1.11 nicm 124: return (CMD_RETURN_NORMAL);
1.18 nicm 125: }
126:
1.25 ! nicm 127: static void
1.18 nicm 128: cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
129: {
130: struct winlink *wl;
131: struct window_pane *wp;
132: int found;
133: u_int y, ly;
134:
135: wl = cmd_mouse_window(m, NULL);
136: if (wl == NULL) {
137: c->tty.mouse_drag_update = NULL;
138: return;
139: }
140:
141: y = m->y;
142: if (m->statusat == 0 && y > 0)
143: y--;
144: else if (m->statusat > 0 && y >= (u_int)m->statusat)
145: y = m->statusat - 1;
146: ly = m->ly;
147: if (m->statusat == 0 && ly > 0)
148: ly--;
149: else if (m->statusat > 0 && ly >= (u_int)m->statusat)
150: ly = m->statusat - 1;
151:
152: found = 0;
153: TAILQ_FOREACH(wp, &wl->window->panes, entry) {
154: if (!window_pane_visible(wp))
155: continue;
156:
157: if (wp->xoff + wp->sx == m->lx &&
1.25 ! nicm 158: wp->yoff <= 1 + ly &&
! 159: wp->yoff + wp->sy >= ly) {
1.24 nicm 160: layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx, 0);
1.18 nicm 161: found = 1;
162: }
163: if (wp->yoff + wp->sy == ly &&
1.25 ! nicm 164: wp->xoff <= 1 + m->lx &&
! 165: wp->xoff + wp->sx >= m->lx) {
1.24 nicm 166: layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly, 0);
1.18 nicm 167: found = 1;
168: }
169: }
170: if (found)
171: server_redraw_window(wl->window);
172: else
173: c->tty.mouse_drag_update = NULL;
1.1 nicm 174: }