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