Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.5.2.1
      1      1.1.1.5  christos /* $OpenBSD$ */
      2          1.1      jmmv 
      3          1.1      jmmv /*
      4          1.1      jmmv  * Copyright (c) 2009 Nicholas Marriott <nicm (at) users.sourceforge.net>
      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.5.2.1  pgoyette static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
     30          1.1      jmmv 
     31  1.1.1.5.2.1  pgoyette static void	cmd_resize_pane_mouse_update(struct client *,
     32  1.1.1.5.2.1  pgoyette 		    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.5.2.1  pgoyette 	.name = "resize-pane",
     36  1.1.1.5.2.1  pgoyette 	.alias = "resizep",
     37  1.1.1.5.2.1  pgoyette 
     38  1.1.1.5.2.1  pgoyette 	.args = { "DLMRt:Ux:y:Z", 0, 1 },
     39  1.1.1.5.2.1  pgoyette 	.usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
     40  1.1.1.5.2.1  pgoyette 		 "[adjustment]",
     41  1.1.1.5.2.1  pgoyette 
     42  1.1.1.5.2.1  pgoyette 	.tflag = CMD_PANE,
     43  1.1.1.5.2.1  pgoyette 
     44  1.1.1.5.2.1  pgoyette 	.flags = CMD_AFTERHOOK,
     45  1.1.1.5.2.1  pgoyette 	.exec = cmd_resize_pane_exec
     46          1.1      jmmv };
     47          1.1      jmmv 
     48  1.1.1.5.2.1  pgoyette static enum cmd_retval
     49  1.1.1.5.2.1  pgoyette 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.5.2.1  pgoyette 	struct window_pane	*wp = item->state.tflag.wp;
     53  1.1.1.5.2.1  pgoyette 	struct winlink		*wl = item->state.tflag.wl;
     54  1.1.1.5.2.1  pgoyette 	struct window		*w = wl->window;
     55  1.1.1.5.2.1  pgoyette 	struct client		*c = item->client;
     56  1.1.1.5.2.1  pgoyette 	struct session		*s = item->state.tflag.s;
     57          1.1      jmmv 	const char	       	*errstr;
     58      1.1.1.3  christos 	char			*cause;
     59          1.1      jmmv 	struct window_pane	*wp;
     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.5.2.1  pgoyette 		if (cmd_mouse_window(&item->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.5.2.1  pgoyette 		cmd_resize_pane_mouse_update(c, &item->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 ((wl = cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp)) == NULL)
     74      1.1.1.3  christos 		return (CMD_RETURN_ERROR);
     75      1.1.1.3  christos 	w = wl->window;
     76      1.1.1.3  christos 
     77      1.1.1.3  christos 	if (args_has(args, 'Z')) {
     78      1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     79      1.1.1.3  christos 			window_unzoom(w);
     80      1.1.1.3  christos 		else
     81      1.1.1.3  christos 			window_zoom(wp);
     82      1.1.1.3  christos 		server_redraw_window(w);
     83      1.1.1.3  christos 		server_status_window(w);
     84      1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     85      1.1.1.3  christos 	}
     86      1.1.1.3  christos 	server_unzoom_window(w);
     87          1.1      jmmv 
     88      1.1.1.2      jmmv 	if (args->argc == 0)
     89          1.1      jmmv 		adjust = 1;
     90          1.1      jmmv 	else {
     91      1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     92          1.1      jmmv 		if (errstr != NULL) {
     93  1.1.1.5.2.1  pgoyette 			cmdq_error(item, "adjustment %s", errstr);
     94      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     95      1.1.1.3  christos 		}
     96      1.1.1.3  christos 	}
     97      1.1.1.3  christos 
     98      1.1.1.3  christos 	if (args_has(self->args, 'x')) {
     99      1.1.1.3  christos 		x = args_strtonum(self->args, 'x', PANE_MINIMUM, INT_MAX,
    100      1.1.1.3  christos 		    &cause);
    101      1.1.1.3  christos 		if (cause != NULL) {
    102  1.1.1.5.2.1  pgoyette 			cmdq_error(item, "width %s", cause);
    103      1.1.1.3  christos 			free(cause);
    104      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    105      1.1.1.3  christos 		}
    106      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    107      1.1.1.3  christos 	}
    108      1.1.1.3  christos 	if (args_has(self->args, 'y')) {
    109      1.1.1.3  christos 		y = args_strtonum(self->args, 'y', PANE_MINIMUM, INT_MAX,
    110      1.1.1.3  christos 		    &cause);
    111      1.1.1.3  christos 		if (cause != NULL) {
    112  1.1.1.5.2.1  pgoyette 			cmdq_error(item, "height %s", cause);
    113      1.1.1.3  christos 			free(cause);
    114      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
    115          1.1      jmmv 		}
    116      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
    117          1.1      jmmv 	}
    118          1.1      jmmv 
    119      1.1.1.2      jmmv 	if (args_has(self->args, 'L'))
    120  1.1.1.5.2.1  pgoyette 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
    121      1.1.1.2      jmmv 	else if (args_has(self->args, 'R'))
    122  1.1.1.5.2.1  pgoyette 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
    123      1.1.1.2      jmmv 	else if (args_has(self->args, 'U'))
    124  1.1.1.5.2.1  pgoyette 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
    125      1.1.1.2      jmmv 	else if (args_has(self->args, 'D'))
    126  1.1.1.5.2.1  pgoyette 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
    127          1.1      jmmv 	server_redraw_window(wl->window);
    128          1.1      jmmv 
    129      1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    130          1.1      jmmv }
    131      1.1.1.5  christos 
    132  1.1.1.5.2.1  pgoyette static void
    133      1.1.1.5  christos cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
    134      1.1.1.5  christos {
    135      1.1.1.5  christos 	struct winlink		*wl;
    136      1.1.1.5  christos 	struct window_pane	*wp;
    137      1.1.1.5  christos 	int			 found;
    138      1.1.1.5  christos 	u_int			 y, ly;
    139      1.1.1.5  christos 
    140      1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    141      1.1.1.5  christos 	if (wl == NULL) {
    142      1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    143      1.1.1.5  christos 		return;
    144      1.1.1.5  christos 	}
    145      1.1.1.5  christos 
    146      1.1.1.5  christos 	y = m->y;
    147      1.1.1.5  christos 	if (m->statusat == 0 && y > 0)
    148      1.1.1.5  christos 		y--;
    149      1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    150      1.1.1.5  christos 		y = m->statusat - 1;
    151      1.1.1.5  christos 	ly = m->ly;
    152      1.1.1.5  christos 	if (m->statusat == 0 && ly > 0)
    153      1.1.1.5  christos 		ly--;
    154      1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    155      1.1.1.5  christos 		ly = m->statusat - 1;
    156      1.1.1.5  christos 
    157      1.1.1.5  christos 	found = 0;
    158      1.1.1.5  christos 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
    159      1.1.1.5  christos 		if (!window_pane_visible(wp))
    160      1.1.1.5  christos 			continue;
    161      1.1.1.5  christos 
    162      1.1.1.5  christos 		if (wp->xoff + wp->sx == m->lx &&
    163  1.1.1.5.2.1  pgoyette 		    wp->yoff <= 1 + ly &&
    164  1.1.1.5.2.1  pgoyette 		    wp->yoff + wp->sy >= ly) {
    165  1.1.1.5.2.1  pgoyette 			layout_resize_pane(wp, LAYOUT_LEFTRIGHT, m->x - m->lx, 0);
    166      1.1.1.5  christos 			found = 1;
    167      1.1.1.5  christos 		}
    168      1.1.1.5  christos 		if (wp->yoff + wp->sy == ly &&
    169  1.1.1.5.2.1  pgoyette 		    wp->xoff <= 1 + m->lx &&
    170  1.1.1.5.2.1  pgoyette 		    wp->xoff + wp->sx >= m->lx) {
    171  1.1.1.5.2.1  pgoyette 			layout_resize_pane(wp, LAYOUT_TOPBOTTOM, y - ly, 0);
    172      1.1.1.5  christos 			found = 1;
    173      1.1.1.5  christos 		}
    174      1.1.1.5  christos 	}
    175      1.1.1.5  christos 	if (found)
    176      1.1.1.5  christos 		server_redraw_window(wl->window);
    177      1.1.1.5  christos 	else
    178      1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    179      1.1.1.5  christos }
    180