Annotation of src/usr.bin/tmux/menu.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: menu.c,v 1.1 2019/05/10 18:04:06 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: struct menu_data {
27: struct cmdq_item *item;
28: int flags;
29:
30: struct cmd_find_state fs;
31: struct screen s;
32:
33: u_int px;
34: u_int py;
35:
36: struct menu *menu;
37: int choice;
38:
39: menu_choice_cb cb;
40: void *data;
41: };
42:
43: static void
44: menu_add_item(struct menu *menu, struct menu_item *item, struct client *c,
45: struct cmd_find_state *fs)
46: {
47: struct menu_item *new_item;
48: const char *key;
49: char *name;
50: u_int width;
51:
52: menu->items = xreallocarray(menu->items, menu->count + 1,
53: sizeof *menu->items);
54: new_item = &menu->items[menu->count++];
55: memset(new_item, 0, sizeof *new_item);
56:
57: if (item == NULL || *item->name == '\0') /* horizontal line */
58: return;
59: name = format_single(NULL, item->name, c, fs->s, fs->wl, fs->wp);
60: if (*name == '\0') { /* no item if empty after format expanded */
61: menu->count--;
62: return;
63: }
64: if (item->key != KEYC_UNKNOWN) {
65: key = key_string_lookup_key(item->key);
66: xasprintf(&new_item->name, "%s #[align=right](%s)", name, key);
67: } else
68: xasprintf(&new_item->name, "%s", name);
69: free(name);
70:
71: if (item->command != NULL)
72: new_item->command = xstrdup(item->command);
73: else
74: new_item->command = NULL;
75: new_item->key = item->key;
76:
77: width = format_width(new_item->name);
78: if (width > menu->width)
79: menu->width = width;
80: }
81:
82: static void
83: menu_parse_item(struct menu *menu, const char *s, struct client *c,
84: struct cmd_find_state *fs)
85: {
86: char *copy, *first;
87: const char *second, *third;
88: struct menu_item item;
89:
90: first = copy = xstrdup(s);
91: if ((second = format_skip(first, ",")) != NULL) {
92: *(char *)second++ = '\0';
93: if ((third = format_skip(second, ",")) != NULL) {
94: *(char *)third++ = '\0';
95:
96: item.name = first;
97: item.command = (char *)third;
98: item.key = key_string_lookup_string(second);
99: menu_add_item(menu, &item, c, fs);
100: }
101: }
102: free(copy);
103: }
104:
105: struct menu *
1.2 ! nicm 106: menu_create(const char *s, struct client *c, struct cmd_find_state *fs,
! 107: const char *title)
1.1 nicm 108: {
109: struct menu *menu;
110: char *copy, *string, *next;
111:
112: if (*s == '\0')
113: return (NULL);
114:
115: menu = xcalloc(1, sizeof *menu);
116: menu->title = xstrdup(title);
117:
118: copy = string = xstrdup(s);
119: do {
120: next = (char *)format_skip(string, "|");
121: log_debug("XXX %s -- %s", next, string);
122: if (next != NULL)
123: *next++ = '\0';
124: if (*string == '\0')
125: menu_add_item(menu, NULL, c, fs);
126: else
127: menu_parse_item(menu, string, c, fs);
128: string = next;
129: } while (next != NULL);
130: free(copy);
131:
132: return (menu);
133: }
134:
135: void
136: menu_free(struct menu *menu)
137: {
138: u_int i;
139:
140: for (i = 0; i < menu->count; i++) {
141: free(menu->items[i].name);
142: free(menu->items[i].command);
143: }
144: free(menu->items);
145:
146: free(menu->title);
147: free(menu);
148: }
149:
150: static void
151: menu_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0)
152: {
153: struct menu_data *md = c->overlay_data;
154: struct tty *tty = &c->tty;
155: struct screen *s = &md->s;
156: struct menu *menu = md->menu;
157: struct screen_write_ctx ctx;
158: u_int i, px, py;
159:
160: screen_write_start(&ctx, NULL, s);
161: screen_write_clearscreen(&ctx, 8);
162: screen_write_menu(&ctx, menu, md->choice);
163: screen_write_stop(&ctx);
164:
165: px = md->px;
166: py = md->py;
167:
168: for (i = 0; i < screen_size_y(&md->s); i++)
169: tty_draw_line(tty, NULL, s, 0, i, menu->width + 4, px, py + i);
170:
171: if (~md->flags & MENU_NOMOUSE)
172: tty_update_mode(tty, MODE_MOUSE_ALL, NULL);
173: }
174:
175: static void
176: menu_free_cb(struct client *c)
177: {
178: struct menu_data *md = c->overlay_data;
179:
180: if (md->item != NULL)
181: md->item->flags &= ~CMDQ_WAITING;
182:
183: screen_free(&md->s);
184: menu_free(md->menu);
185: free(md);
186: }
187:
188: static enum cmd_retval
189: menu_error_cb(struct cmdq_item *item, void *data)
190: {
191: char *error = data;
192:
193: cmdq_error(item, "%s", error);
194: free(error);
195:
196: return (CMD_RETURN_NORMAL);
197: }
198:
199: static int
200: menu_key_cb(struct client *c, struct key_event *event)
201: {
202: struct menu_data *md = c->overlay_data;
203: struct menu *menu = md->menu;
204: struct mouse_event *m = &event->m;
205: u_int i;
206: int count = menu->count, old = md->choice;
207: const struct menu_item *item;
208: struct cmd_list *cmdlist;
209: struct cmdq_item *new_item;
210: char *cause;
211:
212: if (KEYC_IS_MOUSE(event->key)) {
213: if (md->flags & MENU_NOMOUSE)
214: return (0);
215: if (m->x < md->px ||
216: m->x > md->px + 4 + menu->width ||
217: m->y < md->py + 1 ||
218: m->y > md->py + 1 + count - 1) {
219: if (MOUSE_RELEASE(m->b))
220: return (1);
221: if (md->choice != -1) {
222: md->choice = -1;
223: c->flags |= CLIENT_REDRAWOVERLAY;
224: }
225: return (0);
226: }
227: md->choice = m->y - (md->py + 1);
228: if (MOUSE_RELEASE(m->b))
229: goto chosen;
230: if (md->choice != old)
231: c->flags |= CLIENT_REDRAWOVERLAY;
232: return (0);
233: }
234: switch (event->key) {
235: case KEYC_UP:
236: do {
237: if (md->choice == -1 || md->choice == 0)
238: md->choice = count - 1;
239: else
240: md->choice--;
241: } while (menu->items[md->choice].name == NULL);
242: c->flags |= CLIENT_REDRAWOVERLAY;
243: return (0);
244: case KEYC_DOWN:
245: do {
246: if (md->choice == -1 || md->choice == count - 1)
247: md->choice = 0;
248: else
249: md->choice++;
250: } while (menu->items[md->choice].name == NULL);
251: c->flags |= CLIENT_REDRAWOVERLAY;
252: return (0);
253: case '\r':
254: goto chosen;
255: case '\033': /* Escape */
256: case '\003': /* C-c */
257: case '\007': /* C-g */
258: case 'q':
259: return (1);
260: }
261: for (i = 0; i < (u_int)count; i++) {
262: if (event->key == menu->items[i].key) {
263: md->choice = i;
264: goto chosen;
265: }
266: }
267: return (0);
268:
269: chosen:
270: if (md->choice == -1)
271: return (1);
272: item = &menu->items[md->choice];
273: if (item->name == NULL)
274: return (1);
275: if (md->cb != NULL) {
276: md->cb(md->menu, md->choice, item->key, md->data);
277: return (1);
278: }
279: cmdlist = cmd_string_parse(item->command, NULL, 0, &cause);
280: if (cmdlist == NULL && cause != NULL)
281: new_item = cmdq_get_callback(menu_error_cb, cause);
282: else if (cmdlist == NULL)
283: new_item = NULL;
284: else {
285: new_item = cmdq_get_command(cmdlist, &md->fs, NULL, 0);
286: cmd_list_free(cmdlist);
287: }
288: if (new_item != NULL) {
289: if (md->item != NULL)
290: cmdq_insert_after(md->item, new_item);
291: else
292: cmdq_append(c, new_item);
293: }
294: return (1);
295: }
296:
297: int
298: menu_display(struct menu *menu, int flags, struct cmdq_item *item, u_int px,
299: u_int py, struct client *c, struct cmd_find_state *fs, menu_choice_cb cb,
300: void *data)
301: {
302: struct menu_data *md;
303:
304: if (c->tty.sx < menu->width + 4 || c->tty.sy < menu->count + 2)
305: return (-1);
306:
307: md = xcalloc(1, sizeof *md);
308: md->item = item;
309: md->flags = flags;
310:
311: cmd_find_copy_state(&md->fs, fs);
312: screen_init(&md->s, menu->width + 4, menu->count + 2, 0);
313:
314: md->px = px;
315: md->py = py;
316:
317: md->menu = menu;
318: md->choice = -1;
319:
320: md->cb = cb;
321: md->data = data;
322:
323: server_client_set_overlay(c, 0, menu_draw_cb, menu_key_cb, menu_free_cb,
324: md);
325: return (0);
326: }