4 * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
28 * Display a menu on a client.
31 static enum args_parse_type
cmd_display_menu_args_parse(struct args
*,
33 static enum cmd_retval
cmd_display_menu_exec(struct cmd
*,
35 static enum cmd_retval
cmd_display_popup_exec(struct cmd
*,
38 const struct cmd_entry cmd_display_menu_entry
= {
39 .name
= "display-menu",
42 .args
= { "c:t:S:OT:x:y:", 1, -1, cmd_display_menu_args_parse
},
43 .usage
= "[-O] [-c target-client] [-S starting-choice] "
44 CMD_TARGET_PANE_USAGE
" [-T title] [-x position] "
45 "[-y position] name key command ...",
47 .target
= { 't', CMD_FIND_PANE
, 0 },
49 .flags
= CMD_AFTERHOOK
|CMD_CLIENT_CFLAG
,
50 .exec
= cmd_display_menu_exec
53 const struct cmd_entry cmd_display_popup_entry
= {
54 .name
= "display-popup",
57 .args
= { "Bb:Cc:d:e:Eh:s:S:t:T:w:x:y:", 0, -1, NULL
},
58 .usage
= "[-BCE] [-b border-lines] [-c target-client] "
59 "[-d start-directory] [-e environment] [-h height] "
60 "[-s style] [-S border-style] " CMD_TARGET_PANE_USAGE
61 "[-T title] [-w width] [-x position] [-y position] "
64 .target
= { 't', CMD_FIND_PANE
, 0 },
66 .flags
= CMD_AFTERHOOK
|CMD_CLIENT_CFLAG
,
67 .exec
= cmd_display_popup_exec
70 static enum args_parse_type
71 cmd_display_menu_args_parse(struct args
*args
, u_int idx
, __unused
char **cause
)
74 enum args_parse_type type
= ARGS_PARSE_STRING
;
77 type
= ARGS_PARSE_STRING
;
80 if (*args_string(args
, i
++) == '\0')
83 type
= ARGS_PARSE_STRING
;
87 type
= ARGS_PARSE_COMMANDS_OR_STRING
;
95 cmd_display_menu_get_position(struct client
*tc
, struct cmdq_item
*item
,
96 struct args
*args
, u_int
*px
, u_int
*py
, u_int w
, u_int h
)
98 struct tty
*tty
= &tc
->tty
;
99 struct cmd_find_state
*target
= cmdq_get_target(item
);
100 struct key_event
*event
= cmdq_get_event(item
);
101 struct session
*s
= tc
->session
;
102 struct winlink
*wl
= target
->wl
;
103 struct window_pane
*wp
= target
->wp
;
104 struct style_ranges
*ranges
= NULL
;
105 struct style_range
*sr
= NULL
;
109 u_int line
, ox
, oy
, sx
, sy
, lines
, position
;
111 struct format_tree
*ft
;
114 * Work out the position from the -x and -y arguments. This is the
115 * bottom-left position.
118 /* If the popup is too big, stop now. */
119 if (w
> tty
->sx
|| h
> tty
->sy
)
122 /* Create format with mouse position if any. */
123 ft
= format_create_from_target(item
);
124 if (event
->m
.valid
) {
125 format_add(ft
, "popup_mouse_x", "%u", event
->m
.x
);
126 format_add(ft
, "popup_mouse_y", "%u", event
->m
.y
);
130 * If there are any status lines, add this window position and the
131 * status line position.
133 top
= status_at_line(tc
);
135 lines
= status_line_size(tc
);
140 position
= options_get_number(s
->options
, "status-position");
142 for (line
= 0; line
< lines
; line
++) {
143 ranges
= &tc
->status
.entries
[line
].ranges
;
144 TAILQ_FOREACH(sr
, ranges
, entry
) {
145 if (sr
->type
!= STYLE_RANGE_WINDOW
)
147 if (sr
->argument
== (u_int
)wl
->idx
)
155 format_add(ft
, "popup_window_status_line_x", "%u",
158 format_add(ft
, "popup_window_status_line_y",
161 format_add(ft
, "popup_window_status_line_y",
162 "%u", tty
->sy
- lines
+ line
);
167 format_add(ft
, "popup_status_line_y", "%u", lines
+ h
);
169 format_add(ft
, "popup_status_line_y", "%u",
175 /* Popup width and height. */
176 format_add(ft
, "popup_width", "%u", w
);
177 format_add(ft
, "popup_height", "%u", h
);
179 /* Position so popup is in the centre. */
180 n
= (long)(tty
->sx
- 1) / 2 - w
/ 2;
182 format_add(ft
, "popup_centre_x", "%u", 0);
184 format_add(ft
, "popup_centre_x", "%ld", n
);
185 n
= (tty
->sy
- 1) / 2 + h
/ 2;
187 format_add(ft
, "popup_centre_y", "%u", tty
->sy
- h
);
189 format_add(ft
, "popup_centre_y", "%ld", n
);
191 /* Position of popup relative to mouse. */
192 if (event
->m
.valid
) {
193 n
= (long)event
->m
.x
- w
/ 2;
195 format_add(ft
, "popup_mouse_centre_x", "%u", 0);
197 format_add(ft
, "popup_mouse_centre_x", "%ld", n
);
198 n
= event
->m
.y
- h
/ 2;
199 if (n
+ h
>= tty
->sy
) {
200 format_add(ft
, "popup_mouse_centre_y", "%u",
203 format_add(ft
, "popup_mouse_centre_y", "%ld", n
);
204 n
= (long)event
->m
.y
+ h
;
206 format_add(ft
, "popup_mouse_top", "%u", tty
->sy
- 1);
208 format_add(ft
, "popup_mouse_top", "%ld", n
);
211 format_add(ft
, "popup_mouse_bottom", "%u", 0);
213 format_add(ft
, "popup_mouse_bottom", "%ld", n
);
216 /* Position in pane. */
217 tty_window_offset(&tc
->tty
, &ox
, &oy
, &sx
, &sy
);
218 n
= top
+ wp
->yoff
- oy
+ h
;
220 format_add(ft
, "popup_pane_top", "%u", tty
->sy
- h
);
222 format_add(ft
, "popup_pane_top", "%ld", n
);
223 format_add(ft
, "popup_pane_bottom", "%u", top
+ wp
->yoff
+ wp
->sy
- oy
);
224 format_add(ft
, "popup_pane_left", "%u", wp
->xoff
- ox
);
225 n
= (long)wp
->xoff
+ wp
->sx
- ox
- w
;
227 format_add(ft
, "popup_pane_right", "%u", 0);
229 format_add(ft
, "popup_pane_right", "%ld", n
);
231 /* Expand horizontal position. */
232 xp
= args_get(args
, 'x');
233 if (xp
== NULL
|| strcmp(xp
, "C") == 0)
234 xp
= "#{popup_centre_x}";
235 else if (strcmp(xp
, "R") == 0)
236 xp
= "#{popup_pane_right}";
237 else if (strcmp(xp
, "P") == 0)
238 xp
= "#{popup_pane_left}";
239 else if (strcmp(xp
, "M") == 0)
240 xp
= "#{popup_mouse_centre_x}";
241 else if (strcmp(xp
, "W") == 0)
242 xp
= "#{popup_window_status_line_x}";
243 p
= format_expand(ft
, xp
);
244 n
= strtol(p
, NULL
, 10);
245 if (n
+ w
>= tty
->sx
)
250 log_debug("%s: -x: %s = %s = %u (-w %u)", __func__
, xp
, p
, *px
, w
);
253 /* Expand vertical position */
254 yp
= args_get(args
, 'y');
255 if (yp
== NULL
|| strcmp(yp
, "C") == 0)
256 yp
= "#{popup_centre_y}";
257 else if (strcmp(yp
, "P") == 0)
258 yp
= "#{popup_pane_bottom}";
259 else if (strcmp(yp
, "M") == 0)
260 yp
= "#{popup_mouse_top}";
261 else if (strcmp(yp
, "S") == 0)
262 yp
= "#{popup_status_line_y}";
263 else if (strcmp(yp
, "W") == 0)
264 yp
= "#{popup_window_status_line_y}";
265 p
= format_expand(ft
, yp
);
266 n
= strtol(p
, NULL
, 10);
271 if (n
+ h
>= tty
->sy
)
276 log_debug("%s: -y: %s = %s = %u (-h %u)", __func__
, yp
, p
, *py
, h
);
283 static enum cmd_retval
284 cmd_display_menu_exec(struct cmd
*self
, struct cmdq_item
*item
)
286 struct args
*args
= cmd_get_args(self
);
287 struct cmd_find_state
*target
= cmdq_get_target(item
);
288 struct key_event
*event
= cmdq_get_event(item
);
289 struct client
*tc
= cmdq_get_target_client(item
);
290 struct menu
*menu
= NULL
;
291 struct menu_item menu_item
;
292 const char *key
, *name
;
294 int flags
= 0, starting_choice
= 0;
295 u_int px
, py
, i
, count
= args_count(args
);
297 if (tc
->overlay_draw
!= NULL
)
298 return (CMD_RETURN_NORMAL
);
300 if (args_has(args
, 'S')) {
301 if (strcmp(args_get(args
, 'S'), "-") == 0)
302 starting_choice
= -1;
304 starting_choice
= args_strtonum(args
, 'S', 0, UINT_MAX
,
307 cmdq_error(item
, "starting choice %s", cause
);
309 return (CMD_RETURN_ERROR
);
314 if (args_has(args
, 'T'))
315 title
= format_single_from_target(item
, args_get(args
, 'T'));
318 menu
= menu_create(title
);
320 for (i
= 0; i
!= count
; /* nothing */) {
321 name
= args_string(args
, i
++);
323 menu_add_item(menu
, NULL
, item
, tc
, target
);
328 cmdq_error(item
, "not enough arguments");
331 return (CMD_RETURN_ERROR
);
333 key
= args_string(args
, i
++);
335 menu_item
.name
= name
;
336 menu_item
.key
= key_string_lookup_string(key
);
337 menu_item
.command
= args_string(args
, i
++);
339 menu_add_item(menu
, &menu_item
, item
, tc
, target
);
343 cmdq_error(item
, "invalid menu arguments");
344 return (CMD_RETURN_ERROR
);
346 if (menu
->count
== 0) {
348 return (CMD_RETURN_NORMAL
);
350 if (!cmd_display_menu_get_position(tc
, item
, args
, &px
, &py
,
351 menu
->width
+ 4, menu
->count
+ 2)) {
353 return (CMD_RETURN_NORMAL
);
356 if (args_has(args
, 'O'))
357 flags
|= MENU_STAYOPEN
;
359 flags
|= MENU_NOMOUSE
;
360 if (menu_display(menu
, flags
, starting_choice
, item
, px
, py
, tc
, target
,
362 return (CMD_RETURN_NORMAL
);
363 return (CMD_RETURN_WAIT
);
366 static enum cmd_retval
367 cmd_display_popup_exec(struct cmd
*self
, struct cmdq_item
*item
)
369 struct args
*args
= cmd_get_args(self
);
370 struct cmd_find_state
*target
= cmdq_get_target(item
);
371 struct session
*s
= target
->s
;
372 struct client
*tc
= cmdq_get_target_client(item
);
373 struct tty
*tty
= &tc
->tty
;
374 const char *value
, *shell
, *shellcmd
= NULL
;
375 const char *style
= args_get(args
, 's');
376 const char *border_style
= args_get(args
, 'S');
377 char *cwd
, *cause
= NULL
, **argv
= NULL
, *title
;
378 int flags
= 0, argc
= 0;
379 enum box_lines lines
= BOX_LINES_DEFAULT
;
380 u_int px
, py
, w
, h
, count
= args_count(args
);
381 struct args_value
*av
;
382 struct environ
*env
= NULL
;
383 struct options
*o
= s
->curw
->window
->options
;
384 struct options_entry
*oe
;
386 if (args_has(args
, 'C')) {
387 server_client_clear_overlay(tc
);
388 return (CMD_RETURN_NORMAL
);
390 if (tc
->overlay_draw
!= NULL
)
391 return (CMD_RETURN_NORMAL
);
394 if (args_has(args
, 'h')) {
395 h
= args_percentage(args
, 'h', 1, tty
->sy
, tty
->sy
, &cause
);
397 cmdq_error(item
, "height %s", cause
);
399 return (CMD_RETURN_ERROR
);
404 if (args_has(args
, 'w')) {
405 w
= args_percentage(args
, 'w', 1, tty
->sx
, tty
->sx
, &cause
);
407 cmdq_error(item
, "width %s", cause
);
409 return (CMD_RETURN_ERROR
);
417 if (!cmd_display_menu_get_position(tc
, item
, args
, &px
, &py
, w
, h
))
418 return (CMD_RETURN_NORMAL
);
420 value
= args_get(args
, 'b');
421 if (args_has(args
, 'B'))
422 lines
= BOX_LINES_NONE
;
423 else if (value
!= NULL
) {
424 oe
= options_get(o
, "popup-border-lines");
425 lines
= options_find_choice(options_table_entry(oe
), value
,
428 cmdq_error(item
, "popup-border-lines %s", cause
);
430 return (CMD_RETURN_ERROR
);
434 value
= args_get(args
, 'd');
436 cwd
= format_single_from_target(item
, value
);
438 cwd
= xstrdup(server_client_get_cwd(tc
, s
));
440 shellcmd
= options_get_string(s
->options
, "default-command");
442 shellcmd
= args_string(args
, 0);
443 if (count
<= 1 && (shellcmd
== NULL
|| *shellcmd
== '\0')) {
445 shell
= options_get_string(s
->options
, "default-shell");
446 if (!checkshell(shell
))
447 shell
= _PATH_BSHELL
;
448 cmd_append_argv(&argc
, &argv
, shell
);
450 args_to_vector(args
, &argc
, &argv
);
452 if (args_has(args
, 'e') >= 1) {
453 env
= environ_create();
454 av
= args_first_value(args
, 'e');
456 environ_put(env
, av
->string
, 0);
457 av
= args_next_value(av
);
461 if (args_has(args
, 'T'))
462 title
= format_single_from_target(item
, args_get(args
, 'T'));
465 if (args_has(args
, 'E') > 1)
466 flags
|= POPUP_CLOSEEXITZERO
;
467 else if (args_has(args
, 'E'))
468 flags
|= POPUP_CLOSEEXIT
;
469 if (popup_display(flags
, lines
, item
, px
, py
, w
, h
, env
, shellcmd
, argc
,
470 argv
, cwd
, title
, tc
, s
, style
, border_style
, NULL
, NULL
) != 0) {
471 cmd_free_argv(argc
, argv
);
476 return (CMD_RETURN_NORMAL
);
482 cmd_free_argv(argc
, argv
);
483 return (CMD_RETURN_WAIT
);