Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.12
1.12 ! nicm 1: /* $OpenBSD: cmd-display-menu.c,v 1.11 2020/04/02 05:35:15 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:
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Display a menu on a client.
28: */
29:
30: static enum cmd_retval cmd_display_menu_exec(struct cmd *,
31: struct cmdq_item *);
1.7 nicm 32: static enum cmd_retval cmd_display_popup_exec(struct cmd *,
33: struct cmdq_item *);
1.1 nicm 34:
35: const struct cmd_entry cmd_display_menu_entry = {
36: .name = "display-menu",
37: .alias = "menu",
38:
1.4 nicm 39: .args = { "c:t:T:x:y:", 1, -1 },
40: .usage = "[-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
41: "[-x position] [-y position] name key command ...",
1.1 nicm 42:
43: .target = { 't', CMD_FIND_PANE, 0 },
44:
45: .flags = CMD_AFTERHOOK,
46: .exec = cmd_display_menu_exec
47: };
48:
1.7 nicm 49: const struct cmd_entry cmd_display_popup_entry = {
50: .name = "display-popup",
51: .alias = "popup",
52:
53: .args = { "CEKc:d:h:R:t:w:x:y:", 0, -1 },
54: .usage = "[-CEK] [-c target-client] [-d start-directory] [-h height] "
55: "[-R shell-command] " CMD_TARGET_PANE_USAGE " [-w width] "
56: "[-x position] [-y position] [command line ...]",
57:
58: .target = { 't', CMD_FIND_PANE, 0 },
59:
60: .flags = CMD_AFTERHOOK,
61: .exec = cmd_display_popup_exec
62: };
63:
1.5 nicm 64: static void
65: cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
66: struct args *args, u_int *px, u_int *py, u_int w, u_int h)
67: {
1.11 nicm 68: struct session *s = c->session;
1.5 nicm 69: struct winlink *wl = item->target.wl;
70: struct window_pane *wp = item->target.wp;
1.11 nicm 71: struct style_ranges *ranges;
1.5 nicm 72: struct style_range *sr;
73: const char *xp, *yp;
1.11 nicm 74: u_int line, ox, oy, sx, sy, lines;
75:
76: lines = status_line_size(c);
77: for (line = 0; line < lines; line++) {
78: ranges = &c->status.entries[line].ranges;
79: TAILQ_FOREACH(sr, ranges, entry) {
80: if (sr->type == STYLE_RANGE_WINDOW)
81: break;
82: }
83: if (sr != NULL)
84: break;
85: }
86: if (line == lines)
87: ranges = &c->status.entries[0].ranges;
1.5 nicm 88:
89: xp = args_get(args, 'x');
1.10 nicm 90: if (xp == NULL || strcmp(xp, "C") == 0)
91: *px = (c->tty.sx - 1) / 2 - w / 2;
1.5 nicm 92: else if (strcmp(xp, "R") == 0)
93: *px = c->tty.sx - 1;
94: else if (strcmp(xp, "P") == 0) {
95: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
96: if (wp->xoff >= ox)
97: *px = wp->xoff - ox;
98: else
99: *px = 0;
100: } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
101: if (item->shared->mouse.x > w / 2)
102: *px = item->shared->mouse.x - w / 2;
103: else
104: *px = 0;
105: } else if (strcmp(xp, "W") == 0) {
1.11 nicm 106: if (status_at_line(c) == -1)
1.5 nicm 107: *px = 0;
108: else {
1.11 nicm 109: TAILQ_FOREACH(sr, ranges, entry) {
1.5 nicm 110: if (sr->type != STYLE_RANGE_WINDOW)
111: continue;
112: if (sr->argument == (u_int)wl->idx)
113: break;
114: }
115: if (sr != NULL)
116: *px = sr->start;
117: else
118: *px = 0;
119: }
120: } else
121: *px = strtoul(xp, NULL, 10);
122: if ((*px) + w >= c->tty.sx)
123: *px = c->tty.sx - w;
124:
125: yp = args_get(args, 'y');
1.10 nicm 126: if (yp == NULL || strcmp(yp, "C") == 0)
1.6 nicm 127: *py = (c->tty.sy - 1) / 2 + h / 2;
1.5 nicm 128: else if (strcmp(yp, "P") == 0) {
129: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
130: if (wp->yoff + wp->sy >= oy)
131: *py = wp->yoff + wp->sy - oy;
132: else
133: *py = 0;
134: } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
135: *py = item->shared->mouse.y + h;
136: else if (strcmp(yp, "S") == 0) {
1.11 nicm 137: if (options_get_number(s->options, "status-position") == 0) {
138: if (lines != 0)
139: *py = lines + h;
140: else
141: *py = 0;
142: } else {
143: if (lines != 0)
144: *py = c->tty.sy - lines;
145: else
146: *py = c->tty.sy;
147: }
148: }
149: else if (strcmp(yp, "W") == 0) {
150: if (options_get_number(s->options, "status-position") == 0) {
151: if (lines != 0)
152: *py = line + 1 + h;
153: else
154: *py = 0;
155: } else {
156: if (lines != 0)
157: *py = c->tty.sy - lines + line;
158: else
159: *py = c->tty.sy;
160: }
1.5 nicm 161: } else
162: *py = strtoul(yp, NULL, 10);
163: if (*py < h)
164: *py = 0;
165: else
166: *py -= h;
167: if ((*py) + h >= c->tty.sy)
168: *py = c->tty.sy - h;
169: }
170:
1.1 nicm 171: static enum cmd_retval
172: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
173: {
174: struct args *args = self->args;
175: struct client *c;
176: struct session *s = item->target.s;
177: struct winlink *wl = item->target.wl;
178: struct window_pane *wp = item->target.wp;
179: struct cmd_find_state *fs = &item->target;
180: struct menu *menu = NULL;
1.4 nicm 181: struct menu_item menu_item;
1.5 nicm 182: const char *key;
1.4 nicm 183: char *title, *name;
1.6 nicm 184: int flags = 0, i;
1.5 nicm 185: u_int px, py;
1.1 nicm 186:
187: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
188: return (CMD_RETURN_ERROR);
189: if (c->overlay_draw != NULL)
190: return (CMD_RETURN_NORMAL);
191:
192: if (args_has(args, 'T'))
1.12 ! nicm 193: title = format_single(item, args_get(args, 'T'), c, s, wl, wp);
1.1 nicm 194: else
195: title = xstrdup("");
1.4 nicm 196:
197: menu = menu_create(title);
198:
199: for (i = 0; i != args->argc; /* nothing */) {
200: name = args->argv[i++];
201: if (*name == '\0') {
202: menu_add_item(menu, NULL, item, c, fs);
203: continue;
204: }
205:
206: if (args->argc - i < 2) {
207: cmdq_error(item, "not enough arguments");
208: free(title);
209: menu_free(menu);
210: return (CMD_RETURN_ERROR);
211: }
212: key = args->argv[i++];
213:
214: menu_item.name = name;
215: menu_item.key = key_string_lookup_string(key);
216: menu_item.command = args->argv[i++];
217:
218: menu_add_item(menu, &menu_item, item, c, fs);
219: }
1.1 nicm 220: free(title);
221: if (menu == NULL) {
1.4 nicm 222: cmdq_error(item, "invalid menu arguments");
1.1 nicm 223: return (CMD_RETURN_ERROR);
224: }
225: if (menu->count == 0) {
226: menu_free(menu);
227: return (CMD_RETURN_NORMAL);
228: }
1.5 nicm 229: cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
230: menu->count + 2);
1.1 nicm 231:
232: if (!item->shared->mouse.valid)
233: flags |= MENU_NOMOUSE;
234: if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
1.7 nicm 235: return (CMD_RETURN_NORMAL);
236: return (CMD_RETURN_WAIT);
237: }
238:
239: static enum cmd_retval
240: cmd_display_popup_exec(struct cmd *self, struct cmdq_item *item)
241: {
242: struct args *args = self->args;
243: struct client *c;
244: struct cmd_find_state *fs = &item->target;
245: const char *value, *cmd = NULL, **lines = NULL;
246: const char *shellcmd = NULL;
247: char *cwd, *cause;
248: int flags = 0;
249: u_int px, py, w, h, nlines = 0;
250:
251: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
252: return (CMD_RETURN_ERROR);
253: if (args_has(args, 'C')) {
254: server_client_clear_overlay(c);
255: return (CMD_RETURN_NORMAL);
256: }
257: if (c->overlay_draw != NULL)
258: return (CMD_RETURN_NORMAL);
259:
260: if (args->argc >= 1)
261: cmd = args->argv[0];
262: if (args->argc >= 2) {
263: lines = (const char **)args->argv + 1;
264: nlines = args->argc - 1;
265: }
266:
267: if (nlines != 0)
1.8 nicm 268: h = popup_height(nlines, lines) + 2;
1.7 nicm 269: else
270: h = c->tty.sy / 2;
271: if (args_has(args, 'h')) {
272: h = args_percentage(args, 'h', 1, c->tty.sy, c->tty.sy, &cause);
273: if (cause != NULL) {
274: cmdq_error(item, "height %s", cause);
275: free(cause);
276: return (CMD_RETURN_ERROR);
277: }
278: }
279:
280: if (nlines != 0)
281: w = popup_width(item, nlines, lines, c, fs) + 2;
282: else
283: w = c->tty.sx / 2;
284: if (args_has(args, 'w')) {
285: w = args_percentage(args, 'w', 1, c->tty.sx, c->tty.sx, &cause);
286: if (cause != NULL) {
287: cmdq_error(item, "width %s", cause);
288: free(cause);
289: return (CMD_RETURN_ERROR);
290: }
291: }
292:
1.8 nicm 293: if (w > c->tty.sx - 1)
294: w = c->tty.sx - 1;
295: if (h > c->tty.sy - 1)
296: h = c->tty.sy - 1;
1.7 nicm 297: cmd_display_menu_get_position(c, item, args, &px, &py, w, h);
298:
299: value = args_get(args, 'd');
300: if (value != NULL)
1.12 ! nicm 301: cwd = format_single(item, value, c, fs->s, fs->wl, fs->wp);
1.7 nicm 302: else
303: cwd = xstrdup(server_client_get_cwd(c, fs->s));
304:
305: value = args_get(args, 'R');
306: if (value != NULL)
1.12 ! nicm 307: shellcmd = format_single(item, value, c, fs->s, fs->wl, fs->wp);
1.7 nicm 308:
309: if (args_has(args, 'K'))
310: flags |= POPUP_WRITEKEYS;
1.9 nicm 311: if (args_has(args, 'E') > 1)
312: flags |= POPUP_CLOSEEXITZERO;
313: else if (args_has(args, 'E'))
1.7 nicm 314: flags |= POPUP_CLOSEEXIT;
315: if (popup_display(flags, item, px, py, w, h, nlines, lines, shellcmd,
316: cmd, cwd, c, fs) != 0)
1.1 nicm 317: return (CMD_RETURN_NORMAL);
318: return (CMD_RETURN_WAIT);
319: }