164562Sgshapiro/* $OpenBSD$ */
290792Sgshapiro
364562Sgshapiro/*
464562Sgshapiro * Copyright (c) 2018 Nicholas Marriott <nicholas.marriott@gmail.com>
564562Sgshapiro *
664562Sgshapiro * Permission to use, copy, modify, and distribute this software for any
764562Sgshapiro * purpose with or without fee is hereby granted, provided that the above
864562Sgshapiro * copyright notice and this permission notice appear in all copies.
990792Sgshapiro *
1064562Sgshapiro * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1164562Sgshapiro * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1264562Sgshapiro * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1364562Sgshapiro * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1464562Sgshapiro * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1564562Sgshapiro * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1690792Sgshapiro * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1764562Sgshapiro */
1890792Sgshapiro
1990792Sgshapiro#include <sys/types.h>
2064562Sgshapiro
2164562Sgshapiro#include <stdlib.h>
2264562Sgshapiro
2364562Sgshapiro#include "tmux.h"
2464562Sgshapiro
2564562Sgshapiro/*
2664562Sgshapiro * Increase or decrease window size.
2790792Sgshapiro */
2890792Sgshapiro
2964562Sgshapirostatic enum cmd_retval	cmd_resize_window_exec(struct cmd *,
3064562Sgshapiro			    struct cmdq_item *);
3164562Sgshapiro
3264562Sgshapiroconst struct cmd_entry cmd_resize_window_entry = {
3364562Sgshapiro	.name = "resize-window",
3464562Sgshapiro	.alias = "resizew",
3564562Sgshapiro
3664562Sgshapiro	.args = { "aADLRt:Ux:y:", 0, 1, NULL },
3764562Sgshapiro	.usage = "[-aADLRU] [-x width] [-y height] " CMD_TARGET_WINDOW_USAGE " "
3864562Sgshapiro		 "[adjustment]",
3964562Sgshapiro
4064562Sgshapiro	.target = { 't', CMD_FIND_WINDOW, 0 },
4164562Sgshapiro
4264562Sgshapiro	.flags = CMD_AFTERHOOK,
4364562Sgshapiro	.exec = cmd_resize_window_exec
4464562Sgshapiro};
45
46static enum cmd_retval
47cmd_resize_window_exec(struct cmd *self, struct cmdq_item *item)
48{
49	struct args		*args = cmd_get_args(self);
50	struct cmd_find_state	*target = cmdq_get_target(item);
51	struct winlink		*wl = target->wl;
52	struct window		*w = wl->window;
53	struct session		*s = target->s;
54	const char	       	*errstr;
55	char			*cause;
56	u_int			 adjust, sx, sy, xpixel = 0, ypixel = 0;
57
58	if (args_count(args) == 0)
59		adjust = 1;
60	else {
61		adjust = strtonum(args_string(args, 0), 1, INT_MAX, &errstr);
62		if (errstr != NULL) {
63			cmdq_error(item, "adjustment %s", errstr);
64			return (CMD_RETURN_ERROR);
65		}
66	}
67
68	sx = w->sx;
69	sy = w->sy;
70
71	if (args_has(args, 'x')) {
72		sx = args_strtonum(args, 'x', WINDOW_MINIMUM, WINDOW_MAXIMUM,
73		    &cause);
74		if (cause != NULL) {
75			cmdq_error(item, "width %s", cause);
76			free(cause);
77			return (CMD_RETURN_ERROR);
78		}
79	}
80	if (args_has(args, 'y')) {
81		sy = args_strtonum(args, 'y', WINDOW_MINIMUM, WINDOW_MAXIMUM,
82		    &cause);
83		if (cause != NULL) {
84			cmdq_error(item, "height %s", cause);
85			free(cause);
86			return (CMD_RETURN_ERROR);
87		}
88	}
89
90	if (args_has(args, 'L')) {
91		if (sx >= adjust)
92			sx -= adjust;
93	} else if (args_has(args, 'R'))
94		sx += adjust;
95	else if (args_has(args, 'U')) {
96		if (sy >= adjust)
97			sy -= adjust;
98	} else if (args_has(args, 'D'))
99		sy += adjust;
100
101	if (args_has(args, 'A')) {
102		default_window_size(NULL, s, w, &sx, &sy, &xpixel, &ypixel,
103		    WINDOW_SIZE_LARGEST);
104	} else if (args_has(args, 'a')) {
105		default_window_size(NULL, s, w, &sx, &sy, &xpixel, &ypixel,
106		    WINDOW_SIZE_SMALLEST);
107	}
108
109	options_set_number(w->options, "window-size", WINDOW_SIZE_MANUAL);
110	w->manual_sx = sx;
111	w->manual_sy = sy;
112	recalculate_size(w, 1);
113
114	return (CMD_RETURN_NORMAL);
115}
116