Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.30
1.30 ! nicm 1: /* $OpenBSD: cmd-display-menu.c,v 1.29 2021/08/21 10:22:38 nicm Exp $ */
1.1 nicm 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:
1.23 nicm 21: #include <paths.h>
1.1 nicm 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 *);
1.7 nicm 33: static enum cmd_retval cmd_display_popup_exec(struct cmd *,
34: struct cmdq_item *);
1.1 nicm 35:
36: const struct cmd_entry cmd_display_menu_entry = {
37: .name = "display-menu",
38: .alias = "menu",
39:
1.29 nicm 40: .args = { "c:t:OT:x:y:", 1, -1, NULL },
1.20 nicm 41: .usage = "[-O] [-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
1.4 nicm 42: "[-x position] [-y position] name key command ...",
1.1 nicm 43:
44: .target = { 't', CMD_FIND_PANE, 0 },
45:
1.18 nicm 46: .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.1 nicm 47: .exec = cmd_display_menu_exec
48: };
49:
1.7 nicm 50: const struct cmd_entry cmd_display_popup_entry = {
51: .name = "display-popup",
52: .alias = "popup",
53:
1.29 nicm 54: .args = { "BCc:d:Eh:t:w:x:y:", 0, -1, NULL },
1.27 nicm 55: .usage = "[-BCE] [-c target-client] [-d start-directory] [-h height] "
1.23 nicm 56: CMD_TARGET_PANE_USAGE " [-w width] "
57: "[-x position] [-y position] [command]",
1.7 nicm 58:
59: .target = { 't', CMD_FIND_PANE, 0 },
60:
1.18 nicm 61: .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG,
1.7 nicm 62: .exec = cmd_display_popup_exec
63: };
64:
1.21 nicm 65: static int
1.18 nicm 66: cmd_display_menu_get_position(struct client *tc, struct cmdq_item *item,
1.5 nicm 67: struct args *args, u_int *px, u_int *py, u_int w, u_int h)
68: {
1.18 nicm 69: struct tty *tty = &tc->tty;
1.14 nicm 70: struct cmd_find_state *target = cmdq_get_target(item);
1.17 nicm 71: struct key_event *event = cmdq_get_event(item);
1.18 nicm 72: struct session *s = tc->session;
1.14 nicm 73: struct winlink *wl = target->wl;
74: struct window_pane *wp = target->wp;
1.21 nicm 75: struct style_ranges *ranges = NULL;
76: struct style_range *sr = NULL;
1.5 nicm 77: const char *xp, *yp;
1.21 nicm 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);
1.11 nicm 98: }
1.5 nicm 99:
1.21 nicm 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;
1.5 nicm 109: else
1.21 nicm 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;
1.11 nicm 115: TAILQ_FOREACH(sr, ranges, entry) {
1.5 nicm 116: if (sr->type != STYLE_RANGE_WINDOW)
117: continue;
118: if (sr->argument == (u_int)wl->idx)
119: break;
120: }
121: if (sr != NULL)
1.21 nicm 122: break;
123: }
124:
125: if (sr != NULL) {
126: format_add(ft, "popup_window_status_line_x", "%u",
127: sr->start);
128: if (position == 0) {
129: format_add(ft, "popup_window_status_line_y",
130: "%u", line + 1 + h);
131: } else {
132: format_add(ft, "popup_window_status_line_y",
133: "%u", tty->sy - lines + line);
134: }
135: }
136:
137: if (position == 0)
138: format_add(ft, "popup_status_line_y", "%u", lines + h);
139: else {
140: format_add(ft, "popup_status_line_y", "%u",
141: tty->sy - lines);
1.5 nicm 142: }
143: } else
1.21 nicm 144: top = 0;
1.5 nicm 145:
1.21 nicm 146: /* Popup width and height. */
147: format_add(ft, "popup_width", "%u", w);
148: format_add(ft, "popup_height", "%u", h);
149:
150: /* Position so popup is in the centre. */
151: n = (long)(tty->sx - 1) / 2 - w / 2;
152: if (n < 0)
153: format_add(ft, "popup_centre_x", "%u", 0);
154: else
155: format_add(ft, "popup_centre_x", "%ld", n);
156: n = (tty->sy - 1) / 2 + h / 2;
1.22 nicm 157: if (n >= tty->sy)
1.21 nicm 158: format_add(ft, "popup_centre_y", "%u", tty->sy - h);
159: else
160: format_add(ft, "popup_centre_y", "%ld", n);
161:
162: /* Position of popup relative to mouse. */
163: if (event->m.valid) {
164: n = (long)event->m.x - w / 2;
165: if (n < 0)
166: format_add(ft, "popup_mouse_centre_x", "%u", 0);
167: else
168: format_add(ft, "popup_mouse_centre_x", "%ld", n);
169: n = event->m.y - h / 2;
170: if (n + h >= tty->sy) {
171: format_add(ft, "popup_mouse_centre_y", "%u",
172: tty->sy - h);
173: } else
174: format_add(ft, "popup_mouse_centre_y", "%ld", n);
175: n = (long)event->m.y + h;
1.26 nicm 176: if (n >= tty->sy)
1.25 nicm 177: format_add(ft, "popup_mouse_top", "%u", tty->sy - 1);
1.21 nicm 178: else
179: format_add(ft, "popup_mouse_top", "%ld", n);
180: n = event->m.y - h;
181: if (n < 0)
182: format_add(ft, "popup_mouse_bottom", "%u", 0);
183: else
184: format_add(ft, "popup_mouse_bottom", "%ld", n);
185: }
186:
187: /* Position in pane. */
188: tty_window_offset(&tc->tty, &ox, &oy, &sx, &sy);
189: n = top + wp->yoff - oy + h;
190: if (n >= tty->sy)
191: format_add(ft, "popup_pane_top", "%u", tty->sy - h);
192: else
193: format_add(ft, "popup_pane_top", "%ld", n);
194: format_add(ft, "popup_pane_bottom", "%u", top + wp->yoff + wp->sy - oy);
195: format_add(ft, "popup_pane_left", "%u", wp->xoff - ox);
196: n = (long)wp->xoff + wp->sx - ox - w;
197: if (n < 0)
198: format_add(ft, "popup_pane_right", "%u", 0);
199: else
200: format_add(ft, "popup_pane_right", "%ld", n);
201:
202: /* Expand horizontal position. */
203: xp = args_get(args, 'x');
204: if (xp == NULL || strcmp(xp, "C") == 0)
205: xp = "#{popup_centre_x}";
206: else if (strcmp(xp, "R") == 0)
1.24 nicm 207: xp = "#{popup_pane_right}";
1.21 nicm 208: else if (strcmp(xp, "P") == 0)
209: xp = "#{popup_pane_left}";
210: else if (strcmp(xp, "M") == 0)
211: xp = "#{popup_mouse_centre_x}";
212: else if (strcmp(xp, "W") == 0)
213: xp = "#{popup_window_status_line_x}";
214: p = format_expand(ft, xp);
215: n = strtol(p, NULL, 10);
216: if (n + w >= tty->sx)
217: n = tty->sx - w;
218: else if (n < 0)
219: n = 0;
220: *px = n;
221: log_debug("%s: -x: %s = %s = %u", __func__, xp, p, *px);
222: free(p);
223:
224: /* Expand vertical position */
1.5 nicm 225: yp = args_get(args, 'y');
1.10 nicm 226: if (yp == NULL || strcmp(yp, "C") == 0)
1.21 nicm 227: yp = "#{popup_centre_y}";
228: else if (strcmp(yp, "P") == 0)
229: yp = "#{popup_pane_bottom}";
230: else if (strcmp(yp, "M") == 0)
231: yp = "#{popup_mouse_top}";
232: else if (strcmp(yp, "S") == 0)
233: yp = "#{popup_status_line_y}";
234: else if (strcmp(yp, "W") == 0)
235: yp = "#{popup_window_status_line_y}";
236: p = format_expand(ft, yp);
237: n = strtol(p, NULL, 10);
238: if (n < h)
239: n = 0;
1.5 nicm 240: else
1.21 nicm 241: n -= h;
242: if (n + h >= tty->sy)
243: n = tty->sy - h;
244: else if (n < 0)
245: n = 0;
246: *py = n;
247: log_debug("%s: -y: %s = %s = %u", __func__, yp, p, *py);
248: free(p);
249:
250: return (1);
1.5 nicm 251: }
252:
1.1 nicm 253: static enum cmd_retval
254: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
255: {
1.13 nicm 256: struct args *args = cmd_get_args(self);
1.14 nicm 257: struct cmd_find_state *target = cmdq_get_target(item);
1.17 nicm 258: struct key_event *event = cmdq_get_event(item);
1.18 nicm 259: struct client *tc = cmdq_get_target_client(item);
1.1 nicm 260: struct menu *menu = NULL;
1.4 nicm 261: struct menu_item menu_item;
1.28 nicm 262: const char *key, *name;
263: char *title;
264: int flags = 0;
265: u_int px, py, i, count = args_count(args);
1.1 nicm 266:
1.18 nicm 267: if (tc->overlay_draw != NULL)
1.1 nicm 268: return (CMD_RETURN_NORMAL);
269:
270: if (args_has(args, 'T'))
1.18 nicm 271: title = format_single_from_target(item, args_get(args, 'T'));
1.1 nicm 272: else
273: title = xstrdup("");
1.4 nicm 274: menu = menu_create(title);
275:
1.28 nicm 276: for (i = 0; i != count; /* nothing */) {
277: name = args_string(args, i++);
1.4 nicm 278: if (*name == '\0') {
1.18 nicm 279: menu_add_item(menu, NULL, item, tc, target);
1.4 nicm 280: continue;
281: }
282:
1.28 nicm 283: if (count - i < 2) {
1.4 nicm 284: cmdq_error(item, "not enough arguments");
285: free(title);
286: menu_free(menu);
287: return (CMD_RETURN_ERROR);
288: }
1.28 nicm 289: key = args_string(args, i++);
1.4 nicm 290:
291: menu_item.name = name;
292: menu_item.key = key_string_lookup_string(key);
1.28 nicm 293: menu_item.command = args_string(args, i++);
1.4 nicm 294:
1.18 nicm 295: menu_add_item(menu, &menu_item, item, tc, target);
1.4 nicm 296: }
1.1 nicm 297: free(title);
298: if (menu == NULL) {
1.4 nicm 299: cmdq_error(item, "invalid menu arguments");
1.1 nicm 300: return (CMD_RETURN_ERROR);
301: }
302: if (menu->count == 0) {
303: menu_free(menu);
304: return (CMD_RETURN_NORMAL);
305: }
1.21 nicm 306: if (!cmd_display_menu_get_position(tc, item, args, &px, &py,
307: menu->width + 4, menu->count + 2)) {
308: menu_free(menu);
309: return (CMD_RETURN_NORMAL);
310: }
1.1 nicm 311:
1.20 nicm 312: if (args_has(args, 'O'))
313: flags |= MENU_STAYOPEN;
1.17 nicm 314: if (!event->m.valid)
1.1 nicm 315: flags |= MENU_NOMOUSE;
1.18 nicm 316: if (menu_display(menu, flags, item, px, py, tc, target, NULL,
317: NULL) != 0)
1.7 nicm 318: return (CMD_RETURN_NORMAL);
319: return (CMD_RETURN_WAIT);
320: }
321:
322: static enum cmd_retval
323: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
324: {
1.13 nicm 325: struct args *args = cmd_get_args(self);
1.14 nicm 326: struct cmd_find_state *target = cmdq_get_target(item);
1.23 nicm 327: struct session *s = target->s;
1.18 nicm 328: struct client *tc = cmdq_get_target_client(item);
329: struct tty *tty = &tc->tty;
1.28 nicm 330: const char *value, *shell, *shellcmd = NULL;
331: char *cwd, *cause, **argv = NULL;
332: int flags = 0, argc = 0;
333: u_int px, py, w, h, count = args_count(args);
1.7 nicm 334:
335: if (args_has(args, 'C')) {
1.18 nicm 336: server_client_clear_overlay(tc);
1.7 nicm 337: return (CMD_RETURN_NORMAL);
338: }
1.18 nicm 339: if (tc->overlay_draw != NULL)
1.7 nicm 340: return (CMD_RETURN_NORMAL);
341:
1.23 nicm 342: h = tty->sy / 2;
1.7 nicm 343: if (args_has(args, 'h')) {
1.18 nicm 344: h = args_percentage(args, 'h', 1, tty->sy, tty->sy, &cause);
1.7 nicm 345: if (cause != NULL) {
346: cmdq_error(item, "height %s", cause);
347: free(cause);
348: return (CMD_RETURN_ERROR);
349: }
350: }
351:
1.23 nicm 352: w = tty->sx / 2;
1.7 nicm 353: if (args_has(args, 'w')) {
1.18 nicm 354: w = args_percentage(args, 'w', 1, tty->sx, tty->sx, &cause);
1.7 nicm 355: if (cause != NULL) {
356: cmdq_error(item, "width %s", cause);
357: free(cause);
358: return (CMD_RETURN_ERROR);
359: }
360: }
361:
1.18 nicm 362: if (w > tty->sx - 1)
363: w = tty->sx - 1;
364: if (h > tty->sy - 1)
365: h = tty->sy - 1;
1.21 nicm 366: if (!cmd_display_menu_get_position(tc, item, args, &px, &py, w, h))
367: return (CMD_RETURN_NORMAL);
1.7 nicm 368:
369: value = args_get(args, 'd');
370: if (value != NULL)
1.18 nicm 371: cwd = format_single_from_target(item, value);
1.7 nicm 372: else
1.23 nicm 373: cwd = xstrdup(server_client_get_cwd(tc, s));
1.28 nicm 374: if (count == 0)
1.23 nicm 375: shellcmd = options_get_string(s->options, "default-command");
1.28 nicm 376: else if (count == 1)
377: shellcmd = args_string(args, 0);
378: if (count <= 1 && (shellcmd == NULL || *shellcmd == '\0')) {
1.23 nicm 379: shellcmd = NULL;
1.28 nicm 380: shell = options_get_string(s->options, "default-shell");
381: if (!checkshell(shell))
382: shell = _PATH_BSHELL;
383: cmd_append_argv(&argc, &argv, shell);
384: } else
385: args_vector(args, &argc, &argv);
1.7 nicm 386:
1.9 nicm 387: if (args_has(args, 'E') > 1)
388: flags |= POPUP_CLOSEEXITZERO;
389: else if (args_has(args, 'E'))
1.7 nicm 390: flags |= POPUP_CLOSEEXIT;
1.27 nicm 391: if (args_has(args, 'B'))
392: flags |= POPUP_NOBORDER;
1.23 nicm 393: if (popup_display(flags, item, px, py, w, h, shellcmd, argc, argv, cwd,
1.28 nicm 394: tc, s, NULL, NULL) != 0) {
395: cmd_free_argv(argc, argv);
1.1 nicm 396: return (CMD_RETURN_NORMAL);
1.28 nicm 397: }
398: cmd_free_argv(argc, argv);
1.1 nicm 399: return (CMD_RETURN_WAIT);
400: }