Home | History | Annotate | Line # | Download | only in dist
cmd-resize-pane.c revision 1.1.1.7.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.1.7.4.1    martin #include <string.h>
     23          1.1      jmmv 
     24          1.1      jmmv #include "tmux.h"
     25          1.1      jmmv 
     26          1.1      jmmv /*
     27          1.1      jmmv  * Increase or decrease pane size.
     28          1.1      jmmv  */
     29          1.1      jmmv 
     30      1.1.1.7  christos static enum cmd_retval	cmd_resize_pane_exec(struct cmd *, struct cmdq_item *);
     31          1.1      jmmv 
     32      1.1.1.7  christos static void	cmd_resize_pane_mouse_update(struct client *,
     33      1.1.1.7  christos 		    struct mouse_event *);
     34      1.1.1.5  christos 
     35          1.1      jmmv const struct cmd_entry cmd_resize_pane_entry = {
     36      1.1.1.6  christos 	.name = "resize-pane",
     37      1.1.1.6  christos 	.alias = "resizep",
     38      1.1.1.6  christos 
     39      1.1.1.6  christos 	.args = { "DLMRt:Ux:y:Z", 0, 1 },
     40      1.1.1.6  christos 	.usage = "[-DLMRUZ] [-x width] [-y height] " CMD_TARGET_PANE_USAGE " "
     41      1.1.1.6  christos 		 "[adjustment]",
     42      1.1.1.6  christos 
     43  1.1.1.7.4.1    martin 	.target = { 't', CMD_FIND_PANE, 0 },
     44      1.1.1.6  christos 
     45      1.1.1.7  christos 	.flags = CMD_AFTERHOOK,
     46      1.1.1.6  christos 	.exec = cmd_resize_pane_exec
     47          1.1      jmmv };
     48          1.1      jmmv 
     49      1.1.1.7  christos static enum cmd_retval
     50      1.1.1.7  christos cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
     51          1.1      jmmv {
     52      1.1.1.2      jmmv 	struct args		*args = self->args;
     53  1.1.1.7.4.1    martin 	struct cmdq_shared	*shared = item->shared;
     54  1.1.1.7.4.1    martin 	struct window_pane	*wp = item->target.wp;
     55  1.1.1.7.4.1    martin 	struct winlink		*wl = item->target.wl;
     56      1.1.1.6  christos 	struct window		*w = wl->window;
     57      1.1.1.7  christos 	struct client		*c = item->client;
     58  1.1.1.7.4.1    martin 	struct session		*s = item->target.s;
     59  1.1.1.7.4.1    martin 	const char	       	*errstr, *p;
     60  1.1.1.7.4.1    martin 	char			*cause, *copy;
     61          1.1      jmmv 	u_int			 adjust;
     62  1.1.1.7.4.1    martin 	int			 x, y, percentage;
     63  1.1.1.7.4.1    martin 	size_t			 plen;
     64          1.1      jmmv 
     65      1.1.1.5  christos 	if (args_has(args, 'M')) {
     66  1.1.1.7.4.1    martin 		if (cmd_mouse_window(&shared->mouse, &s) == NULL)
     67      1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     68      1.1.1.5  christos 		if (c == NULL || c->session != s)
     69      1.1.1.5  christos 			return (CMD_RETURN_NORMAL);
     70      1.1.1.5  christos 		c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
     71  1.1.1.7.4.1    martin 		cmd_resize_pane_mouse_update(c, &shared->mouse);
     72      1.1.1.5  christos 		return (CMD_RETURN_NORMAL);
     73      1.1.1.5  christos 	}
     74      1.1.1.5  christos 
     75      1.1.1.3  christos 	if (args_has(args, 'Z')) {
     76      1.1.1.3  christos 		if (w->flags & WINDOW_ZOOMED)
     77      1.1.1.3  christos 			window_unzoom(w);
     78      1.1.1.3  christos 		else
     79      1.1.1.3  christos 			window_zoom(wp);
     80      1.1.1.3  christos 		server_redraw_window(w);
     81      1.1.1.3  christos 		server_status_window(w);
     82      1.1.1.3  christos 		return (CMD_RETURN_NORMAL);
     83      1.1.1.3  christos 	}
     84      1.1.1.3  christos 	server_unzoom_window(w);
     85          1.1      jmmv 
     86      1.1.1.2      jmmv 	if (args->argc == 0)
     87          1.1      jmmv 		adjust = 1;
     88          1.1      jmmv 	else {
     89      1.1.1.2      jmmv 		adjust = strtonum(args->argv[0], 1, INT_MAX, &errstr);
     90          1.1      jmmv 		if (errstr != NULL) {
     91      1.1.1.7  christos 			cmdq_error(item, "adjustment %s", errstr);
     92      1.1.1.3  christos 			return (CMD_RETURN_ERROR);
     93      1.1.1.3  christos 		}
     94      1.1.1.3  christos 	}
     95      1.1.1.3  christos 
     96  1.1.1.7.4.1    martin 	if ((p = args_get(args, 'x')) != NULL) {
     97  1.1.1.7.4.1    martin 		plen = strlen(p);
     98  1.1.1.7.4.1    martin 		if (p[plen - 1] == '%') {
     99  1.1.1.7.4.1    martin 			copy = xstrdup(p);
    100  1.1.1.7.4.1    martin 			copy[plen - 1] = '\0';
    101  1.1.1.7.4.1    martin 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
    102  1.1.1.7.4.1    martin 			free(copy);
    103  1.1.1.7.4.1    martin 			if (errstr != NULL) {
    104  1.1.1.7.4.1    martin 				cmdq_error(item, "width %s", errstr);
    105  1.1.1.7.4.1    martin 				return (CMD_RETURN_ERROR);
    106  1.1.1.7.4.1    martin 			}
    107  1.1.1.7.4.1    martin 			x = (w->sx * percentage) / 100;
    108  1.1.1.7.4.1    martin 			if (x < PANE_MINIMUM)
    109  1.1.1.7.4.1    martin 				x = PANE_MINIMUM;
    110  1.1.1.7.4.1    martin 			if (x > INT_MAX)
    111  1.1.1.7.4.1    martin 				x = INT_MAX;
    112  1.1.1.7.4.1    martin 		} else {
    113  1.1.1.7.4.1    martin 			x = args_strtonum(args, 'x', PANE_MINIMUM, INT_MAX,
    114  1.1.1.7.4.1    martin 			    &cause);
    115  1.1.1.7.4.1    martin 			if (cause != NULL) {
    116  1.1.1.7.4.1    martin 				cmdq_error(item, "width %s", cause);
    117  1.1.1.7.4.1    martin 				free(cause);
    118  1.1.1.7.4.1    martin 				return (CMD_RETURN_ERROR);
    119  1.1.1.7.4.1    martin 			}
    120      1.1.1.3  christos 		}
    121      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_LEFTRIGHT, x);
    122      1.1.1.3  christos 	}
    123  1.1.1.7.4.1    martin 	if ((p = args_get(args, 'y')) != NULL) {
    124  1.1.1.7.4.1    martin 		plen = strlen(p);
    125  1.1.1.7.4.1    martin 		if (p[plen - 1] == '%') {
    126  1.1.1.7.4.1    martin 			copy = xstrdup(p);
    127  1.1.1.7.4.1    martin 			copy[plen - 1] = '\0';
    128  1.1.1.7.4.1    martin 			percentage = strtonum(copy, 0, INT_MAX, &errstr);
    129  1.1.1.7.4.1    martin 			free(copy);
    130  1.1.1.7.4.1    martin 			if (errstr != NULL) {
    131  1.1.1.7.4.1    martin 				cmdq_error(item, "height %s", errstr);
    132  1.1.1.7.4.1    martin 				return (CMD_RETURN_ERROR);
    133  1.1.1.7.4.1    martin 			}
    134  1.1.1.7.4.1    martin 			y = (w->sy * percentage) / 100;
    135  1.1.1.7.4.1    martin 			if (y < PANE_MINIMUM)
    136  1.1.1.7.4.1    martin 				y = PANE_MINIMUM;
    137  1.1.1.7.4.1    martin 			if (y > INT_MAX)
    138  1.1.1.7.4.1    martin 				y = INT_MAX;
    139  1.1.1.7.4.1    martin 		}
    140  1.1.1.7.4.1    martin 		else {
    141  1.1.1.7.4.1    martin 			y = args_strtonum(args, 'y', PANE_MINIMUM, INT_MAX,
    142  1.1.1.7.4.1    martin 			    &cause);
    143  1.1.1.7.4.1    martin 			if (cause != NULL) {
    144  1.1.1.7.4.1    martin 				cmdq_error(item, "height %s", cause);
    145  1.1.1.7.4.1    martin 				free(cause);
    146  1.1.1.7.4.1    martin 				return (CMD_RETURN_ERROR);
    147  1.1.1.7.4.1    martin 			}
    148          1.1      jmmv 		}
    149      1.1.1.3  christos 		layout_resize_pane_to(wp, LAYOUT_TOPBOTTOM, y);
    150          1.1      jmmv 	}
    151          1.1      jmmv 
    152  1.1.1.7.4.1    martin 	if (args_has(args, 'L'))
    153      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, -adjust, 1);
    154  1.1.1.7.4.1    martin 	else if (args_has(args, 'R'))
    155      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_LEFTRIGHT, adjust, 1);
    156  1.1.1.7.4.1    martin 	else if (args_has(args, 'U'))
    157      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, -adjust, 1);
    158  1.1.1.7.4.1    martin 	else if (args_has(args, 'D'))
    159      1.1.1.7  christos 		layout_resize_pane(wp, LAYOUT_TOPBOTTOM, adjust, 1);
    160          1.1      jmmv 	server_redraw_window(wl->window);
    161          1.1      jmmv 
    162      1.1.1.3  christos 	return (CMD_RETURN_NORMAL);
    163          1.1      jmmv }
    164      1.1.1.5  christos 
    165      1.1.1.7  christos static void
    166      1.1.1.5  christos cmd_resize_pane_mouse_update(struct client *c, struct mouse_event *m)
    167      1.1.1.5  christos {
    168      1.1.1.5  christos 	struct winlink		*wl;
    169  1.1.1.7.4.1    martin 	struct window		*w;
    170  1.1.1.7.4.1    martin 	u_int			 y, ly, x, lx;
    171  1.1.1.7.4.1    martin 	static const int         offsets[][2] = {
    172  1.1.1.7.4.1    martin 	    { 0, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
    173  1.1.1.7.4.1    martin 	};
    174  1.1.1.7.4.1    martin 	struct layout_cell	*cells[nitems(offsets)], *lc;
    175  1.1.1.7.4.1    martin 	u_int			 ncells = 0, i, j, resizes = 0;
    176  1.1.1.7.4.1    martin 	enum layout_type	 type;
    177      1.1.1.5  christos 
    178      1.1.1.5  christos 	wl = cmd_mouse_window(m, NULL);
    179      1.1.1.5  christos 	if (wl == NULL) {
    180      1.1.1.5  christos 		c->tty.mouse_drag_update = NULL;
    181      1.1.1.5  christos 		return;
    182      1.1.1.5  christos 	}
    183  1.1.1.7.4.1    martin 	w = wl->window;
    184      1.1.1.5  christos 
    185  1.1.1.7.4.1    martin 	y = m->y + m->oy; x = m->x + m->ox;
    186  1.1.1.7.4.1    martin 	if (m->statusat == 0 && y >= m->statuslines)
    187  1.1.1.7.4.1    martin 		y -= m->statuslines;
    188      1.1.1.5  christos 	else if (m->statusat > 0 && y >= (u_int)m->statusat)
    189      1.1.1.5  christos 		y = m->statusat - 1;
    190  1.1.1.7.4.1    martin 	ly = m->ly + m->oy; lx = m->lx + m->ox;
    191  1.1.1.7.4.1    martin 	if (m->statusat == 0 && ly >= m->statuslines)
    192  1.1.1.7.4.1    martin 		ly -= m->statuslines;
    193      1.1.1.5  christos 	else if (m->statusat > 0 && ly >= (u_int)m->statusat)
    194      1.1.1.5  christos 		ly = m->statusat - 1;
    195      1.1.1.5  christos 
    196  1.1.1.7.4.1    martin 	for (i = 0; i < nitems(cells); i++) {
    197  1.1.1.7.4.1    martin 		lc = layout_search_by_border(w->layout_root, lx + offsets[i][0],
    198  1.1.1.7.4.1    martin 		    ly + offsets[i][1]);
    199  1.1.1.7.4.1    martin 		if (lc == NULL)
    200      1.1.1.5  christos 			continue;
    201      1.1.1.5  christos 
    202  1.1.1.7.4.1    martin 		for (j = 0; j < ncells; j++) {
    203  1.1.1.7.4.1    martin 			if (cells[j] == lc) {
    204  1.1.1.7.4.1    martin 				lc = NULL;
    205  1.1.1.7.4.1    martin 				break;
    206  1.1.1.7.4.1    martin 			}
    207  1.1.1.7.4.1    martin 		}
    208  1.1.1.7.4.1    martin 		if (lc == NULL)
    209  1.1.1.7.4.1    martin 			continue;
    210  1.1.1.7.4.1    martin 
    211  1.1.1.7.4.1    martin 		cells[ncells] = lc;
    212  1.1.1.7.4.1    martin 		ncells++;
    213  1.1.1.7.4.1    martin 	}
    214  1.1.1.7.4.1    martin 	if (ncells == 0)
    215  1.1.1.7.4.1    martin 		return;
    216  1.1.1.7.4.1    martin 
    217  1.1.1.7.4.1    martin 	for (i = 0; i < ncells; i++) {
    218  1.1.1.7.4.1    martin 		type = cells[i]->parent->type;
    219  1.1.1.7.4.1    martin 		if (y != ly && type == LAYOUT_TOPBOTTOM) {
    220  1.1.1.7.4.1    martin 			layout_resize_layout(w, cells[i], type, y - ly, 0);
    221  1.1.1.7.4.1    martin 			resizes++;
    222  1.1.1.7.4.1    martin 		} else if (x != lx && type == LAYOUT_LEFTRIGHT) {
    223  1.1.1.7.4.1    martin 			layout_resize_layout(w, cells[i], type, x - lx, 0);
    224  1.1.1.7.4.1    martin 			resizes++;
    225  1.1.1.7.4.1    martin 		}
    226  1.1.1.7.4.1    martin 	}
    227  1.1.1.7.4.1    martin 	if (resizes != 0)
    228  1.1.1.7.4.1    martin 		server_redraw_window(w);
    229      1.1.1.5  christos }
    230