xref: /openbsd-src/usr.bin/tmux/cmd-display-menu.c (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
1 /* $OpenBSD: cmd-display-menu.c,v 1.26 2021/07/21 08:09:43 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <paths.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Display a menu on a client.
29  */
30 
31 static enum cmd_retval	cmd_display_menu_exec(struct cmd *,
32 			    struct cmdq_item *);
33 static enum cmd_retval	cmd_display_popup_exec(struct cmd *,
34 			    struct cmdq_item *);
35 
36 const struct cmd_entry cmd_display_menu_entry = {
37 	.name = "display-menu",
38 	.alias = "menu",
39 
40 	.args = { "c:t:OT:x:y:", 1, -1 },
41 	.usage = "[-O] [-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
42 		 "[-x position] [-y position] name key command ...",
43 
44 	.target = { 't', CMD_FIND_PANE, 0 },
45 
46 	.flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
47 	.exec = cmd_display_menu_exec
48 };
49 
50 const struct cmd_entry cmd_display_popup_entry = {
51 	.name = "display-popup",
52 	.alias = "popup",
53 
54 	.args = { "Cc:d:Eh:t:w:x:y:", 0, -1 },
55 	.usage = "[-CE] [-c target-client] [-d start-directory] [-h height] "
56 	         CMD_TARGET_PANE_USAGE " [-w width] "
57 	         "[-x position] [-y position] [command]",
58 
59 	.target = { 't', CMD_FIND_PANE, 0 },
60 
61 	.flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
62 	.exec = cmd_display_popup_exec
63 };
64 
65 static int
66 cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
67     struct args *args, u_int *px, u_int *py, u_int w, u_int h)
68 {
69 	struct tty		*tty = &tc->tty;
70 	struct cmd_find_state	*target = cmdq_get_target(item);
71 	struct key_event	*event = cmdq_get_event(item);
72 	struct session		*s = tc->session;
73 	struct winlink		*wl = target->wl;
74 	struct window_pane	*wp = target->wp;
75 	struct style_ranges	*ranges = NULL;
76 	struct style_range	*sr = NULL;
77 	const char		*xp, *yp;
78 	char			*p;
79 	int			 top;
80 	u_int			 line, ox, oy, sx, sy, lines, position;
81 	long			 n;
82 	struct format_tree	*ft;
83 
84 	/*
85 	 * Work out the position from the -x and -y arguments. This is the
86 	 * bottom-left position.
87 	 */
88 
89 	/* If the popup is too big, stop now. */
90 	if (w > tty->sx || h > tty->sy)
91 		return (0);
92 
93 	/* Create format with mouse position if any. */
94 	ft = format_create_from_target(item);
95 	if (event->m.valid) {
96 		format_add(ft, "popup_mouse_x", "%u", event->m.x);
97 		format_add(ft, "popup_mouse_y", "%u", event->m.y);
98 	}
99 
100 	/*
101 	 * If there are any status lines, add this window position and the
102 	 * status line position.
103 	 */
104 	top = status_at_line(tc);
105 	if (top != -1) {
106 		lines = status_line_size(tc);
107 		if (top == 0)
108 			top = lines;
109 		else
110 			top = 0;
111 		position = options_get_number(s->options, "status-position");
112 
113 		for (line = 0; line < lines; line++) {
114 			ranges = &tc->status.entries[line].ranges;
115 			TAILQ_FOREACH(sr, ranges, entry) {
116 				if (sr->type != STYLE_RANGE_WINDOW)
117 					continue;
118 				if (sr->argument == (u_int)wl->idx)
119 					break;
120 			}
121 			if (sr != NULL)
122 				break;
123 		}
124 		if (line == lines)
125 			ranges = &tc->status.entries[0].ranges;
126 
127 		if (sr != NULL) {
128 			format_add(ft, "popup_window_status_line_x", "%u",
129 			    sr->start);
130 			if (position == 0) {
131 				format_add(ft, "popup_window_status_line_y",
132 				    "%u", line + 1 + h);
133 			} else {
134 				format_add(ft, "popup_window_status_line_y",
135 				    "%u", tty->sy - lines + line);
136 			}
137 		}
138 
139 		if (position == 0)
140 			format_add(ft, "popup_status_line_y", "%u", lines + h);
141 		else {
142 			format_add(ft, "popup_status_line_y", "%u",
143 			    tty->sy - lines);
144 		}
145 	} else
146 		top = 0;
147 
148 	/* Popup width and height. */
149 	format_add(ft, "popup_width", "%u", w);
150 	format_add(ft, "popup_height", "%u", h);
151 
152 	/* Position so popup is in the centre. */
153 	n = (long)(tty->sx - 1) / 2 - w / 2;
154 	if (n < 0)
155 		format_add(ft, "popup_centre_x", "%u", 0);
156 	else
157 		format_add(ft, "popup_centre_x", "%ld", n);
158 	n = (tty->sy - 1) / 2 + h / 2;
159 	if (n >= tty->sy)
160 		format_add(ft, "popup_centre_y", "%u", tty->sy - h);
161 	else
162 		format_add(ft, "popup_centre_y", "%ld", n);
163 
164 	/* Position of popup relative to mouse. */
165 	if (event->m.valid) {
166 		n = (long)event->m.x - w / 2;
167 		if (n < 0)
168 			format_add(ft, "popup_mouse_centre_x", "%u", 0);
169 		else
170 			format_add(ft, "popup_mouse_centre_x", "%ld", n);
171 		n = event->m.y - h / 2;
172 		if (n + h >= tty->sy) {
173 			format_add(ft, "popup_mouse_centre_y", "%u",
174 			    tty->sy - h);
175 		} else
176 			format_add(ft, "popup_mouse_centre_y", "%ld", n);
177 		n = (long)event->m.y + h;
178 		if (n >= tty->sy)
179 			format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
180 		else
181 			format_add(ft, "popup_mouse_top", "%ld", n);
182 		n = event->m.y - h;
183 		if (n < 0)
184 			format_add(ft, "popup_mouse_bottom", "%u", 0);
185 		else
186 			format_add(ft, "popup_mouse_bottom", "%ld", n);
187 	}
188 
189 	/* Position in pane. */
190 	tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
191 	n = top + wp->yoff - oy + h;
192 	if (n >= tty->sy)
193 		format_add(ft, "popup_pane_top", "%u", tty->sy - h);
194 	else
195 		format_add(ft, "popup_pane_top", "%ld", n);
196 	format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
197 	format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
198 	n = (long)wp->xoff + wp->sx - ox - w;
199 	if (n < 0)
200 		format_add(ft, "popup_pane_right", "%u", 0);
201 	else
202 		format_add(ft, "popup_pane_right", "%ld", n);
203 
204 	/* Expand horizontal position. */
205 	xp = args_get(args, 'x');
206 	if (xp == NULL || strcmp(xp, "C") == 0)
207 		xp = "#{popup_centre_x}";
208 	else if (strcmp(xp, "R") == 0)
209 		xp = "#{popup_pane_right}";
210 	else if (strcmp(xp, "P") == 0)
211 		xp = "#{popup_pane_left}";
212 	else if (strcmp(xp, "M") == 0)
213 		xp = "#{popup_mouse_centre_x}";
214 	else if (strcmp(xp, "W") == 0)
215 		xp = "#{popup_window_status_line_x}";
216 	p = format_expand(ft, xp);
217 	n = strtol(p, NULL, 10);
218 	if (n + w >= tty->sx)
219 		n = tty->sx - w;
220 	else if (n < 0)
221 		n = 0;
222 	*px = n;
223 	log_debug("%s: -x: %s = %s = %u", __func__, xp, p, *px);
224 	free(p);
225 
226 	/* Expand vertical position  */
227 	yp = args_get(args, 'y');
228 	if (yp == NULL || strcmp(yp, "C") == 0)
229 		yp = "#{popup_centre_y}";
230 	else if (strcmp(yp, "P") == 0)
231 		yp = "#{popup_pane_bottom}";
232 	else if (strcmp(yp, "M") == 0)
233 		yp = "#{popup_mouse_top}";
234 	else if (strcmp(yp, "S") == 0)
235 		yp = "#{popup_status_line_y}";
236 	else if (strcmp(yp, "W") == 0)
237 		yp = "#{popup_window_status_line_y}";
238 	p = format_expand(ft, yp);
239 	n = strtol(p, NULL, 10);
240 	if (n < h)
241 		n = 0;
242 	else
243 		n -= h;
244 	if (n + h >= tty->sy)
245 		n = tty->sy - h;
246 	else if (n < 0)
247 		n = 0;
248 	*py = n;
249 	log_debug("%s: -y: %s = %s = %u", __func__, yp, p, *py);
250 	free(p);
251 
252 	return (1);
253 }
254 
255 static enum cmd_retval
256 cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
257 {
258 	struct args		*args = cmd_get_args(self);
259 	struct cmd_find_state	*target = cmdq_get_target(item);
260 	struct key_event	*event = cmdq_get_event(item);
261 	struct client		*tc = cmdq_get_target_client(item);
262 	struct menu		*menu = NULL;
263 	struct menu_item	 menu_item;
264 	const char		*key;
265 	char			*title, *name;
266 	int			 flags = 0, i;
267 	u_int			 px, py;
268 
269 	if (tc->overlay_draw != NULL)
270 		return (CMD_RETURN_NORMAL);
271 
272 	if (args_has(args, 'T'))
273 		title = format_single_from_target(item, args_get(args, 'T'));
274 	else
275 		title = xstrdup("");
276 	menu = menu_create(title);
277 
278 	for (i = 0; i != args->argc; /* nothing */) {
279 		name = args->argv[i++];
280 		if (*name == '\0') {
281 			menu_add_item(menu, NULL, item, tc, target);
282 			continue;
283 		}
284 
285 		if (args->argc - i < 2) {
286 			cmdq_error(item, "not enough arguments");
287 			free(title);
288 			menu_free(menu);
289 			return (CMD_RETURN_ERROR);
290 		}
291 		key = args->argv[i++];
292 
293 		menu_item.name = name;
294 		menu_item.key = key_string_lookup_string(key);
295 		menu_item.command = args->argv[i++];
296 
297 		menu_add_item(menu, &menu_item, item, tc, target);
298 	}
299 	free(title);
300 	if (menu == NULL) {
301 		cmdq_error(item, "invalid menu arguments");
302 		return (CMD_RETURN_ERROR);
303 	}
304 	if (menu->count == 0) {
305 		menu_free(menu);
306 		return (CMD_RETURN_NORMAL);
307 	}
308 	if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
309 	    menu->width + 4, menu->count + 2)) {
310 		menu_free(menu);
311 		return (CMD_RETURN_NORMAL);
312 	}
313 
314 	if (args_has(args, 'O'))
315 		flags |= MENU_STAYOPEN;
316 	if (!event->m.valid)
317 		flags |= MENU_NOMOUSE;
318 	if (menu_display(menu, flags, item, px, py, tc, target, NULL,
319 	    NULL) != 0)
320 		return (CMD_RETURN_NORMAL);
321 	return (CMD_RETURN_WAIT);
322 }
323 
324 static enum cmd_retval
325 cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
326 {
327 	struct args		*args = cmd_get_args(self);
328 	struct cmd_find_state	*target = cmdq_get_target(item);
329 	struct session		*s = target->s;
330 	struct client		*tc = cmdq_get_target_client(item);
331 	struct tty		*tty = &tc->tty;
332 	const char		*value, *shell[] = { NULL, NULL };
333 	const char		*shellcmd = NULL;
334 	char			*cwd, *cause, **argv = args->argv;
335 	int			 flags = 0, argc = args->argc;
336 	u_int			 px, py, w, h;
337 
338 	if (args_has(args, 'C')) {
339 		server_client_clear_overlay(tc);
340 		return (CMD_RETURN_NORMAL);
341 	}
342 	if (tc->overlay_draw != NULL)
343 		return (CMD_RETURN_NORMAL);
344 
345 	h = tty->sy / 2;
346 	if (args_has(args, 'h')) {
347 		h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
348 		if (cause != NULL) {
349 			cmdq_error(item, "height %s", cause);
350 			free(cause);
351 			return (CMD_RETURN_ERROR);
352 		}
353 	}
354 
355 	w = tty->sx / 2;
356 	if (args_has(args, 'w')) {
357 		w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
358 		if (cause != NULL) {
359 			cmdq_error(item, "width %s", cause);
360 			free(cause);
361 			return (CMD_RETURN_ERROR);
362 		}
363 	}
364 
365 	if (w > tty->sx - 1)
366 		w = tty->sx - 1;
367 	if (h > tty->sy - 1)
368 		h = tty->sy - 1;
369 	if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
370 		return (CMD_RETURN_NORMAL);
371 
372 	value = args_get(args, 'd');
373 	if (value != NULL)
374 		cwd = format_single_from_target(item, value);
375 	else
376 		cwd = xstrdup(server_client_get_cwd(tc, s));
377 	if (argc == 0)
378 		shellcmd = options_get_string(s->options, "default-command");
379 	else if (argc == 1)
380 		shellcmd = argv[0];
381 	if (argc <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
382 		shellcmd = NULL;
383 		shell[0] = options_get_string(s->options, "default-shell");
384 		if (!checkshell(shell[0]))
385 			shell[0] = _PATH_BSHELL;
386 		argc = 1;
387 		argv = (char**)shell;
388 	}
389 
390 	if (args_has(args, 'E') > 1)
391 		flags |= POPUP_CLOSEEXITZERO;
392 	else if (args_has(args, 'E'))
393 		flags |= POPUP_CLOSEEXIT;
394 	if (popup_display(flags, item, px, py, w, h, shellcmd, argc, argv, cwd,
395 	    tc, s, NULL, NULL) != 0)
396 		return (CMD_RETURN_NORMAL);
397 	return (CMD_RETURN_WAIT);
398 }
399