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