1*e3eeb0f8Snicm /* $OpenBSD: cmd-resize-window.c,v 1.10 2023/06/30 13:19:32 nicm Exp $ */
27b470e93Snicm
37b470e93Snicm /*
47b470e93Snicm * Copyright (c) 2018 Nicholas Marriott <nicholas.marriott@gmail.com>
57b470e93Snicm *
67b470e93Snicm * Permission to use, copy, modify, and distribute this software for any
77b470e93Snicm * purpose with or without fee is hereby granted, provided that the above
87b470e93Snicm * copyright notice and this permission notice appear in all copies.
97b470e93Snicm *
107b470e93Snicm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
117b470e93Snicm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
127b470e93Snicm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
137b470e93Snicm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
147b470e93Snicm * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
157b470e93Snicm * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
167b470e93Snicm * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
177b470e93Snicm */
187b470e93Snicm
197b470e93Snicm #include <sys/types.h>
207b470e93Snicm
217b470e93Snicm #include <stdlib.h>
227b470e93Snicm
237b470e93Snicm #include "tmux.h"
247b470e93Snicm
257b470e93Snicm /*
267b470e93Snicm * Increase or decrease window size.
277b470e93Snicm */
287b470e93Snicm
297b470e93Snicm static enum cmd_retval cmd_resize_window_exec(struct cmd *,
307b470e93Snicm struct cmdq_item *);
317b470e93Snicm
327b470e93Snicm const struct cmd_entry cmd_resize_window_entry = {
337b470e93Snicm .name = "resize-window",
347b470e93Snicm .alias = "resizew",
357b470e93Snicm
36a51dead1Snicm .args = { "aADLRt:Ux:y:", 0, 1, NULL },
377b470e93Snicm .usage = "[-aADLRU] [-x width] [-y height] " CMD_TARGET_WINDOW_USAGE " "
387b470e93Snicm "[adjustment]",
397b470e93Snicm
407b470e93Snicm .target = { 't', CMD_FIND_WINDOW, 0 },
417b470e93Snicm
427b470e93Snicm .flags = CMD_AFTERHOOK,
437b470e93Snicm .exec = cmd_resize_window_exec
447b470e93Snicm };
457b470e93Snicm
467b470e93Snicm static enum cmd_retval
cmd_resize_window_exec(struct cmd * self,struct cmdq_item * item)477b470e93Snicm cmd_resize_window_exec(struct cmd *self, struct cmdq_item *item)
487b470e93Snicm {
4990d7ba38Snicm struct args *args = cmd_get_args(self);
50040343aeSnicm struct cmd_find_state *target = cmdq_get_target(item);
51040343aeSnicm struct winlink *wl = target->wl;
527b470e93Snicm struct window *w = wl->window;
53040343aeSnicm struct session *s = target->s;
547b470e93Snicm const char *errstr;
557b470e93Snicm char *cause;
56*e3eeb0f8Snicm u_int adjust, sx, sy, xpixel = 0, ypixel = 0;
577b470e93Snicm
581693b10bSnicm if (args_count(args) == 0)
597b470e93Snicm adjust = 1;
607b470e93Snicm else {
61825f884aSnicm adjust = strtonum(args_string(args, 0), 1, INT_MAX, &errstr);
627b470e93Snicm if (errstr != NULL) {
637b470e93Snicm cmdq_error(item, "adjustment %s", errstr);
647b470e93Snicm return (CMD_RETURN_ERROR);
657b470e93Snicm }
667b470e93Snicm }
677b470e93Snicm
687b470e93Snicm sx = w->sx;
697b470e93Snicm sy = w->sy;
707b470e93Snicm
717b470e93Snicm if (args_has(args, 'x')) {
727b470e93Snicm sx = args_strtonum(args, 'x', WINDOW_MINIMUM, WINDOW_MAXIMUM,
737b470e93Snicm &cause);
747b470e93Snicm if (cause != NULL) {
757b470e93Snicm cmdq_error(item, "width %s", cause);
767b470e93Snicm free(cause);
777b470e93Snicm return (CMD_RETURN_ERROR);
787b470e93Snicm }
797b470e93Snicm }
807b470e93Snicm if (args_has(args, 'y')) {
817b470e93Snicm sy = args_strtonum(args, 'y', WINDOW_MINIMUM, WINDOW_MAXIMUM,
827b470e93Snicm &cause);
837b470e93Snicm if (cause != NULL) {
847b470e93Snicm cmdq_error(item, "height %s", cause);
857b470e93Snicm free(cause);
867b470e93Snicm return (CMD_RETURN_ERROR);
877b470e93Snicm }
887b470e93Snicm }
897b470e93Snicm
907b470e93Snicm if (args_has(args, 'L')) {
917b470e93Snicm if (sx >= adjust)
927b470e93Snicm sx -= adjust;
937b470e93Snicm } else if (args_has(args, 'R'))
947b470e93Snicm sx += adjust;
957b470e93Snicm else if (args_has(args, 'U')) {
967b470e93Snicm if (sy >= adjust)
977b470e93Snicm sy -= adjust;
987b470e93Snicm } else if (args_has(args, 'D'))
997b470e93Snicm sy += adjust;
1007b470e93Snicm
1014a8b0ea5Snicm if (args_has(args, 'A')) {
1024a8b0ea5Snicm default_window_size(NULL, s, w, &sx, &sy, &xpixel, &ypixel,
1034a8b0ea5Snicm WINDOW_SIZE_LARGEST);
1044a8b0ea5Snicm } else if (args_has(args, 'a')) {
1054a8b0ea5Snicm default_window_size(NULL, s, w, &sx, &sy, &xpixel, &ypixel,
1064a8b0ea5Snicm WINDOW_SIZE_SMALLEST);
1074a8b0ea5Snicm }
1087b470e93Snicm
1097b470e93Snicm options_set_number(w->options, "window-size", WINDOW_SIZE_MANUAL);
11034ce0afcSnicm w->manual_sx = sx;
11134ce0afcSnicm w->manual_sy = sy;
11234ce0afcSnicm recalculate_size(w, 1);
1137b470e93Snicm
1147b470e93Snicm return (CMD_RETURN_NORMAL);
1157b470e93Snicm }
116