Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.8.4.1
      1      1.1.1.5  christos /* $OpenBSD$ */
      2          1.1      jmmv 
      3          1.1      jmmv /*
      4      1.1.1.6  christos  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5          1.1      jmmv  *
      6          1.1      jmmv  * Permission to use, copy, modify, and distribute this software for any
      7          1.1      jmmv  * purpose with or without fee is hereby granted, provided that the above
      8          1.1      jmmv  * copyright notice and this permission notice appear in all copies.
      9          1.1      jmmv  *
     10          1.1      jmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11          1.1      jmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12          1.1      jmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13          1.1      jmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14          1.1      jmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15          1.1      jmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16          1.1      jmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17          1.1      jmmv  */
     18          1.1      jmmv 
     19          1.1      jmmv #include <sys/types.h>
     20          1.1      jmmv 
     21          1.1      jmmv #include <stdlib.h>
     22          1.1      jmmv 
     23          1.1      jmmv #include "tmux.h"
     24          1.1      jmmv 
     25          1.1      jmmv /*
     26          1.1      jmmv  * Increase or decrease pane size.
     27          1.1      jmmv  */
     28          1.1      jmmv 
     29      1.1.1.7  christos static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
     30          1.1      jmmv 
     31      1.1.1.7  christos static void	cmd_resize_pane_mouse_update(struct client *,
     32      1.1.1.7  christos 		    struct mouse_event *);
     33      1.1.1.5  christos 
     34          1.1      jmmv const struct cmd_entry cmd_resize_pane_entry = {
     35      1.1.1.6  christos 	.name = "resize-pane",
     36      1.1.1.6  christos 	.alias = "resizep",
     37      1.1.1.6  christos 
     38      1.1.1.6  christos 	.args = { "DLMRt:Ux:y:Z", 0, 1 },
     39      1.1.1.6  christos 	.usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
     40      1.1.1.6  christos 		 "[adjustment]",
     41      1.1.1.6  christos 
     42      1.1.1.8  christos 	.target = { 't', CMD_FIND_PANE, 0 },
     43      1.1.1.6  christos 
     44      1.1.1.7  christos 	.flags = CMD_AFTERHOOK,
     45      1.1.1.6  christos 	.exec = cmd_resize_pane_exec
     46          1.1      jmmv };
     47          1.1      jmmv 
     48      1.1.1.7  christos static enum cmd_retval
     49      1.1.1.7  christos cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
     50          1.1      jmmv {
     51      1.1.1.2      jmmv 	struct args		*args = self->args;
     52      1.1.1.8  christos 	struct cmdq_shared	*shared = item->shared;
     53      1.1.1.8  christos 	struct window_pane	*wp = item->target.wp;
     54      1.1.1.8  christos 	struct winlink		*wl = item->target.wl;
     55      1.1.1.6  christos 	struct window		*w = wl->window;
     56      1.1.1.7  christos 	struct client		*c = item->client;
     57      1.1.1.8  christos 	struct session		*s = item->target.s;
     58          1.1      jmmv 	const char	       	*errstr;
     59      1.1.1.3  christos 	char			*cause;
     60          1.1      jmmv 	u_int			 adjust;
     61      1.1.1.3  christos 	int			 x, y;
     62          1.1      jmmv 
     63      1.1.1.5  christos 	if (args_has(args, 'M')) {
     64      1.1.1.8  christos 		if (cmd_mouse_window(&shared->mouse, &s) == NULL)
     65      1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     66      1.1.1.5  christos 		if (c == NULL || c->session != s)
     67      1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     68      1.1.1.5  christos 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
     69      1.1.1.8  christos 		cmd_resize_pane_mouse_update(c, &shared->mouse);
     70      1.1.1.5  christos 		return (CMD_RETURN_NORMAL);
     71      1.1.1.5  christos 	}
     72      1.1.1.5  christos 
     73      1.1.1.3  christos 	if (args_has(args, 'Z')) {
     74      1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     75      1.1.1.3  christos 			window_unzoom(w);
     76      1.1.1.3  christos 		else
     77      1.1.1.3  christos 			window_zoom(wp);
     78      1.1.1.3  christos 		server_redraw_window(w);
     79      1.1.1.3  christos 		server_status_window(w);
     80      1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     81      1.1.1.3  christos 	}
     82      1.1.1.3  christos 	server_unzoom_window(w);
     83          1.1      jmmv 
     84      1.1.1.2      jmmv 	if (args->argc == 0)
     85          1.1      jmmv 		adjust = 1;
     86          1.1      jmmv 	else {
     87      1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     88          1.1      jmmv 		if (errstr != NULL) {
     89      1.1.1.7  christos 			cmdq_error(item, "adjustment %s", errstr);
     90      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     91      1.1.1.3  christos 		}
     92      1.1.1.3  christos 	}
     93      1.1.1.3  christos 
     94      1.1.1.3  christos 	if (args_has(self->args, 'x')) {
     95      1.1.1.3  christos 		x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
     96      1.1.1.3  christos 		    &cause);
     97      1.1.1.3  christos 		if (cause != NULL) {
     98      1.1.1.7  christos 			cmdq_error(item, "width %s", cause);
     99      1.1.1.3  christos 			free(cause);
    100      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    101      1.1.1.3  christos 		}
    102      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    103      1.1.1.3  christos 	}
    104      1.1.1.3  christos 	if (args_has(self->args, 'y')) {
    105      1.1.1.3  christos 		y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
    106      1.1.1.3  christos 		    &cause);
    107      1.1.1.3  christos 		if (cause != NULL) {
    108      1.1.1.7  christos 			cmdq_error(item, "height %s", cause);
    109      1.1.1.3  christos 			free(cause);
    110      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    111          1.1      jmmv 		}
    112      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
    113          1.1      jmmv 	}
    114          1.1      jmmv 
    115      1.1.1.2      jmmv 	if (args_has(self->args, 'L'))
    116      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
    117      1.1.1.2      jmmv 	else if (args_has(self->args, 'R'))
    118      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
    119      1.1.1.2      jmmv 	else if (args_has(self->args, 'U'))
    120      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
    121      1.1.1.2      jmmv 	else if (args_has(self->args, 'D'))
    122      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
    123          1.1      jmmv 	server_redraw_window(wl->window);
    124          1.1      jmmv 
    125      1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    126          1.1      jmmv }
    127      1.1.1.5  christos 
    128      1.1.1.7  christos static void
    129      1.1.1.5  christos cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
    130      1.1.1.5  christos {
    131      1.1.1.5  christos 	struct winlink		*wl;
    132  1.1.1.8.4.1  christos 	struct window		*w;
    133  1.1.1.8.4.1  christos 	u_int			 y, ly, x, lx;
    134  1.1.1.8.4.1  christos 	static const int         offsets[][2] = {
    135  1.1.1.8.4.1  christos 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
    136  1.1.1.8.4.1  christos 	};
    137  1.1.1.8.4.1  christos 	struct layout_cell	*cells[nitems(offsets)], *lc;
    138  1.1.1.8.4.1  christos 	u_int			 ncells = 0, i, j, resizes = 0;
    139  1.1.1.8.4.1  christos 	enum layout_type	 type;
    140      1.1.1.5  christos 
    141      1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    142      1.1.1.5  christos 	if (wl == NULL) {
    143      1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    144      1.1.1.5  christos 		return;
    145      1.1.1.5  christos 	}
    146  1.1.1.8.4.1  christos 	w = wl->window;
    147      1.1.1.5  christos 
    148      1.1.1.8  christos 	y = m->y; x = m->x;
    149      1.1.1.5  christos 	if (m->statusat == 0 && y > 0)
    150      1.1.1.5  christos 		y--;
    151      1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    152      1.1.1.5  christos 		y = m->statusat - 1;
    153      1.1.1.8  christos 	ly = m->ly; lx = m->lx;
    154      1.1.1.5  christos 	if (m->statusat == 0 && ly > 0)
    155      1.1.1.5  christos 		ly--;
    156      1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    157      1.1.1.5  christos 		ly = m->statusat - 1;
    158      1.1.1.5  christos 
    159  1.1.1.8.4.1  christos 	for (i = 0; i < nitems(cells); i++) {
    160  1.1.1.8.4.1  christos 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
    161  1.1.1.8.4.1  christos 		    ly + offsets[i][1]);
    162  1.1.1.8.4.1  christos 		if (lc == NULL)
    163      1.1.1.5  christos 			continue;
    164      1.1.1.5  christos 
    165  1.1.1.8.4.1  christos 		for (j = 0; j < ncells; j++) {
    166  1.1.1.8.4.1  christos 			if (cells[j] == lc) {
    167  1.1.1.8.4.1  christos 				lc = NULL;
    168  1.1.1.8.4.1  christos 				break;
    169  1.1.1.8.4.1  christos 			}
    170  1.1.1.8.4.1  christos 		}
    171  1.1.1.8.4.1  christos 		if (lc == NULL)
    172  1.1.1.8.4.1  christos 			continue;
    173  1.1.1.8.4.1  christos 
    174  1.1.1.8.4.1  christos 		cells[ncells] = lc;
    175  1.1.1.8.4.1  christos 		ncells++;
    176      1.1.1.5  christos 	}
    177  1.1.1.8.4.1  christos 	if (ncells == 0)
    178      1.1.1.8  christos 		return;
    179  1.1.1.8.4.1  christos 
    180  1.1.1.8.4.1  christos 	for (i = 0; i < ncells; i++) {
    181  1.1.1.8.4.1  christos 		type = cells[i]->parent->type;
    182  1.1.1.8.4.1  christos 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
    183  1.1.1.8.4.1  christos 			layout_resize_layout(w, cells[i], type, y - ly, 0);
    184  1.1.1.8.4.1  christos 			resizes++;
    185  1.1.1.8.4.1  christos 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
    186  1.1.1.8.4.1  christos 			layout_resize_layout(w, cells[i], type, x - lx, 0);
    187  1.1.1.8.4.1  christos 			resizes++;
    188  1.1.1.8.4.1  christos 		}
    189      1.1.1.8  christos 	}
    190  1.1.1.8.4.1  christos 	if (resizes != 0)
    191  1.1.1.8.4.1  christos 		server_redraw_window(w);
    192      1.1.1.5  christos }
    193