Annotation of src/usr.bin/tmux/cmd-display-menu.c, Revision 1.5
1.5 ! nicm 1: /* $OpenBSD: cmd-display-menu.c,v 1.4 2019/05/28 07:18:42 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 *);
32:
33: const struct cmd_entry cmd_display_menu_entry = {
34: .name = "display-menu",
35: .alias = "menu",
36:
1.4 nicm 37: .args = { "c:t:T:x:y:", 1, -1 },
38: .usage = "[-c target-client] " CMD_TARGET_PANE_USAGE " [-T title] "
39: "[-x position] [-y position] name key command ...",
1.1 nicm 40:
41: .target = { 't', CMD_FIND_PANE, 0 },
42:
43: .flags = CMD_AFTERHOOK,
44: .exec = cmd_display_menu_exec
45: };
46:
1.5 ! nicm 47: static void
! 48: cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
! 49: struct args *args, u_int *px, u_int *py, u_int w, u_int h)
! 50: {
! 51: struct winlink *wl = item->target.wl;
! 52: struct window_pane *wp = item->target.wp;
! 53: struct style_range *sr;
! 54: const char *xp, *yp;
! 55: int at = status_at_line(c);
! 56: u_int ox, oy, sx, sy;
! 57:
! 58: xp = args_get(args, 'x');
! 59: if (xp == NULL)
! 60: *px = 0;
! 61: else if (strcmp(xp, "R") == 0)
! 62: *px = c->tty.sx - 1;
! 63: else if (strcmp(xp, "P") == 0) {
! 64: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
! 65: if (wp->xoff >= ox)
! 66: *px = wp->xoff - ox;
! 67: else
! 68: *px = 0;
! 69: } else if (strcmp(xp, "M") == 0 && item->shared->mouse.valid) {
! 70: if (item->shared->mouse.x > w / 2)
! 71: *px = item->shared->mouse.x - w / 2;
! 72: else
! 73: *px = 0;
! 74: } else if (strcmp(xp, "W") == 0) {
! 75: if (at == -1)
! 76: *px = 0;
! 77: else {
! 78: TAILQ_FOREACH(sr, &c->status.entries[0].ranges, entry) {
! 79: if (sr->type != STYLE_RANGE_WINDOW)
! 80: continue;
! 81: if (sr->argument == (u_int)wl->idx)
! 82: break;
! 83: }
! 84: if (sr != NULL)
! 85: *px = sr->start;
! 86: else
! 87: *px = 0;
! 88: }
! 89: } else
! 90: *px = strtoul(xp, NULL, 10);
! 91: if ((*px) + w >= c->tty.sx)
! 92: *px = c->tty.sx - w;
! 93:
! 94: yp = args_get(args, 'y');
! 95: if (yp == NULL)
! 96: *py = 0;
! 97: else if (strcmp(yp, "P") == 0) {
! 98: tty_window_offset(&c->tty, &ox, &oy, &sx, &sy);
! 99: if (wp->yoff + wp->sy >= oy)
! 100: *py = wp->yoff + wp->sy - oy;
! 101: else
! 102: *py = 0;
! 103: } else if (strcmp(yp, "M") == 0 && item->shared->mouse.valid)
! 104: *py = item->shared->mouse.y + h;
! 105: else if (strcmp(yp, "S") == 0) {
! 106: if (at == -1)
! 107: *py = c->tty.sy;
! 108: else if (at == 0)
! 109: *py = status_line_size(c) + h;
! 110: else
! 111: *py = at;
! 112: } else
! 113: *py = strtoul(yp, NULL, 10);
! 114: if (*py < h)
! 115: *py = 0;
! 116: else
! 117: *py -= h;
! 118: if ((*py) + h >= c->tty.sy)
! 119: *py = c->tty.sy - h;
! 120: }
! 121:
1.1 nicm 122: static enum cmd_retval
123: cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
124: {
125: struct args *args = self->args;
126: struct client *c;
127: struct session *s = item->target.s;
128: struct winlink *wl = item->target.wl;
129: struct window_pane *wp = item->target.wp;
130: struct cmd_find_state *fs = &item->target;
131: struct menu *menu = NULL;
1.4 nicm 132: struct menu_item menu_item;
1.5 ! nicm 133: const char *key;
1.4 nicm 134: char *title, *name;
1.5 ! nicm 135: int flags, i;
! 136: u_int px, py;
1.1 nicm 137:
138: if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
139: return (CMD_RETURN_ERROR);
140: if (c->overlay_draw != NULL)
141: return (CMD_RETURN_NORMAL);
142:
143: if (args_has(args, 'T'))
144: title = format_single(NULL, args_get(args, 'T'), c, s, wl, wp);
145: else
146: title = xstrdup("");
1.4 nicm 147:
148: menu = menu_create(title);
149:
150: for (i = 0; i != args->argc; /* nothing */) {
151: name = args->argv[i++];
152: if (*name == '\0') {
153: menu_add_item(menu, NULL, item, c, fs);
154: continue;
155: }
156:
157: if (args->argc - i < 2) {
158: cmdq_error(item, "not enough arguments");
159: free(title);
160: menu_free(menu);
161: return (CMD_RETURN_ERROR);
162: }
163: key = args->argv[i++];
164:
165: menu_item.name = name;
166: menu_item.key = key_string_lookup_string(key);
167: menu_item.command = args->argv[i++];
168:
169: menu_add_item(menu, &menu_item, item, c, fs);
170: }
1.1 nicm 171: free(title);
172: if (menu == NULL) {
1.4 nicm 173: cmdq_error(item, "invalid menu arguments");
1.1 nicm 174: return (CMD_RETURN_ERROR);
175: }
176: if (menu->count == 0) {
177: menu_free(menu);
178: return (CMD_RETURN_NORMAL);
179: }
1.5 ! nicm 180: cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
! 181: menu->count + 2);
1.1 nicm 182:
183: flags = 0;
184: if (!item->shared->mouse.valid)
185: flags |= MENU_NOMOUSE;
186: if (menu_display(menu, flags, item, px, py, c, fs, NULL, NULL) != 0)
187: return (CMD_RETURN_NORMAL);
188: return (CMD_RETURN_WAIT);
189: }