1 /* $OpenBSD: popup.c,v 1.57 2025/01/12 14:36:28 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2020 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 #include <sys/wait.h> 21 22 #include <paths.h> 23 #include <signal.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 28 #include "tmux.h" 29 30 struct popup_data { 31 struct client *c; 32 struct cmdq_item *item; 33 int flags; 34 char *title; 35 36 struct grid_cell border_cell; 37 enum box_lines border_lines; 38 39 struct screen s; 40 struct grid_cell defaults; 41 struct colour_palette palette; 42 43 struct job *job; 44 struct input_ctx *ictx; 45 int status; 46 popup_close_cb cb; 47 void *arg; 48 49 struct menu *menu; 50 struct menu_data *md; 51 int close; 52 53 /* Current position and size. */ 54 u_int px; 55 u_int py; 56 u_int sx; 57 u_int sy; 58 59 /* Preferred position and size. */ 60 u_int ppx; 61 u_int ppy; 62 u_int psx; 63 u_int psy; 64 65 enum { OFF, MOVE, SIZE } dragging; 66 u_int dx; 67 u_int dy; 68 69 u_int lx; 70 u_int ly; 71 u_int lb; 72 }; 73 74 struct popup_editor { 75 char *path; 76 popup_finish_edit_cb cb; 77 void *arg; 78 }; 79 80 static const struct menu_item popup_menu_items[] = { 81 { "Close", 'q', NULL }, 82 { "#{?buffer_name,Paste #[underscore]#{buffer_name},}", 'p', NULL }, 83 { "", KEYC_NONE, NULL }, 84 { "Fill Space", 'F', NULL }, 85 { "Centre", 'C', NULL }, 86 { "", KEYC_NONE, NULL }, 87 { "To Horizontal Pane", 'h', NULL }, 88 { "To Vertical Pane", 'v', NULL }, 89 90 { NULL, KEYC_NONE, NULL } 91 }; 92 93 static const struct menu_item popup_internal_menu_items[] = { 94 { "Close", 'q', NULL }, 95 { "", KEYC_NONE, NULL }, 96 { "Fill Space", 'F', NULL }, 97 { "Centre", 'C', NULL }, 98 99 { NULL, KEYC_NONE, NULL } 100 }; 101 102 static void 103 popup_redraw_cb(const struct tty_ctx *ttyctx) 104 { 105 struct popup_data *pd = ttyctx->arg; 106 107 pd->c->flags |= CLIENT_REDRAWOVERLAY; 108 } 109 110 static int 111 popup_set_client_cb(struct tty_ctx *ttyctx, struct client *c) 112 { 113 struct popup_data *pd = ttyctx->arg; 114 115 if (c != pd->c) 116 return (0); 117 if (pd->c->flags & CLIENT_REDRAWOVERLAY) 118 return (0); 119 120 ttyctx->bigger = 0; 121 ttyctx->wox = 0; 122 ttyctx->woy = 0; 123 ttyctx->wsx = c->tty.sx; 124 ttyctx->wsy = c->tty.sy; 125 126 if (pd->border_lines == BOX_LINES_NONE) { 127 ttyctx->xoff = ttyctx->rxoff = pd->px; 128 ttyctx->yoff = ttyctx->ryoff = pd->py; 129 } else { 130 ttyctx->xoff = ttyctx->rxoff = pd->px + 1; 131 ttyctx->yoff = ttyctx->ryoff = pd->py + 1; 132 } 133 134 return (1); 135 } 136 137 static void 138 popup_init_ctx_cb(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx) 139 { 140 struct popup_data *pd = ctx->arg; 141 142 memcpy(&ttyctx->defaults, &pd->defaults, sizeof ttyctx->defaults); 143 ttyctx->palette = &pd->palette; 144 ttyctx->redraw_cb = popup_redraw_cb; 145 ttyctx->set_client_cb = popup_set_client_cb; 146 ttyctx->arg = pd; 147 } 148 149 static struct screen * 150 popup_mode_cb(__unused struct client *c, void *data, u_int *cx, u_int *cy) 151 { 152 struct popup_data *pd = data; 153 154 if (pd->md != NULL) 155 return (menu_mode_cb(c, pd->md, cx, cy)); 156 157 if (pd->border_lines == BOX_LINES_NONE) { 158 *cx = pd->px + pd->s.cx; 159 *cy = pd->py + pd->s.cy; 160 } else { 161 *cx = pd->px + 1 + pd->s.cx; 162 *cy = pd->py + 1 + pd->s.cy; 163 } 164 return (&pd->s); 165 } 166 167 /* Return parts of the input range which are not obstructed by the popup. */ 168 static void 169 popup_check_cb(struct client* c, void *data, u_int px, u_int py, u_int nx, 170 struct overlay_ranges *r) 171 { 172 struct popup_data *pd = data; 173 struct overlay_ranges or[2]; 174 u_int i, j, k = 0; 175 176 if (pd->md != NULL) { 177 /* Check each returned range for the menu against the popup. */ 178 menu_check_cb(c, pd->md, px, py, nx, r); 179 for (i = 0; i < 2; i++) { 180 server_client_overlay_range(pd->px, pd->py, pd->sx, 181 pd->sy, r->px[i], py, r->nx[i], &or[i]); 182 } 183 184 /* 185 * or has up to OVERLAY_MAX_RANGES non-overlapping ranges, 186 * ordered from left to right. Collect them in the output. 187 */ 188 for (i = 0; i < 2; i++) { 189 /* Each or[i] only has 2 ranges. */ 190 for (j = 0; j < 2; j++) { 191 if (or[i].nx[j] > 0) { 192 r->px[k] = or[i].px[j]; 193 r->nx[k] = or[i].nx[j]; 194 k++; 195 } 196 } 197 } 198 199 /* Zero remaining ranges if any. */ 200 for (i = k; i < OVERLAY_MAX_RANGES; i++) { 201 r->px[i] = 0; 202 r->nx[i] = 0; 203 } 204 205 return; 206 } 207 208 server_client_overlay_range(pd->px, pd->py, pd->sx, pd->sy, px, py, nx, 209 r); 210 } 211 212 static void 213 popup_draw_cb(struct client *c, void *data, struct screen_redraw_ctx *rctx) 214 { 215 struct popup_data *pd = data; 216 struct tty *tty = &c->tty; 217 struct screen s; 218 struct screen_write_ctx ctx; 219 u_int i, px = pd->px, py = pd->py; 220 struct colour_palette *palette = &pd->palette; 221 struct grid_cell defaults; 222 223 screen_init(&s, pd->sx, pd->sy, 0); 224 screen_write_start(&ctx, &s); 225 screen_write_clearscreen(&ctx, 8); 226 227 if (pd->border_lines == BOX_LINES_NONE) { 228 screen_write_cursormove(&ctx, 0, 0, 0); 229 screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx, pd->sy); 230 } else if (pd->sx > 2 && pd->sy > 2) { 231 screen_write_box(&ctx, pd->sx, pd->sy, pd->border_lines, 232 &pd->border_cell, pd->title); 233 screen_write_cursormove(&ctx, 1, 1, 0); 234 screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2, 235 pd->sy - 2); 236 } 237 screen_write_stop(&ctx); 238 239 memcpy(&defaults, &pd->defaults, sizeof defaults); 240 if (defaults.fg == 8) 241 defaults.fg = palette->fg; 242 if (defaults.bg == 8) 243 defaults.bg = palette->bg; 244 245 if (pd->md != NULL) { 246 c->overlay_check = menu_check_cb; 247 c->overlay_data = pd->md; 248 } else { 249 c->overlay_check = NULL; 250 c->overlay_data = NULL; 251 } 252 for (i = 0; i < pd->sy; i++) { 253 tty_draw_line(tty, &s, 0, i, pd->sx, px, py + i, &defaults, 254 palette); 255 } 256 screen_free(&s); 257 if (pd->md != NULL) { 258 c->overlay_check = NULL; 259 c->overlay_data = NULL; 260 menu_draw_cb(c, pd->md, rctx); 261 } 262 c->overlay_check = popup_check_cb; 263 c->overlay_data = pd; 264 } 265 266 static void 267 popup_free_cb(struct client *c, void *data) 268 { 269 struct popup_data *pd = data; 270 struct cmdq_item *item = pd->item; 271 272 if (pd->md != NULL) 273 menu_free_cb(c, pd->md); 274 275 if (pd->cb != NULL) 276 pd->cb(pd->status, pd->arg); 277 278 if (item != NULL) { 279 if (cmdq_get_client(item) != NULL && 280 cmdq_get_client(item)->session == NULL) 281 cmdq_get_client(item)->retval = pd->status; 282 cmdq_continue(item); 283 } 284 server_client_unref(pd->c); 285 286 if (pd->job != NULL) 287 job_free(pd->job); 288 input_free(pd->ictx); 289 290 screen_free(&pd->s); 291 colour_palette_free(&pd->palette); 292 293 free(pd->title); 294 free(pd); 295 } 296 297 static void 298 popup_resize_cb(__unused struct client *c, void *data) 299 { 300 struct popup_data *pd = data; 301 struct tty *tty = &c->tty; 302 303 if (pd == NULL) 304 return; 305 if (pd->md != NULL) 306 menu_free_cb(c, pd->md); 307 308 /* Adjust position and size. */ 309 if (pd->psy > tty->sy) 310 pd->sy = tty->sy; 311 else 312 pd->sy = pd->psy; 313 if (pd->psx > tty->sx) 314 pd->sx = tty->sx; 315 else 316 pd->sx = pd->psx; 317 if (pd->ppy + pd->sy > tty->sy) 318 pd->py = tty->sy - pd->sy; 319 else 320 pd->py = pd->ppy; 321 if (pd->ppx + pd->sx > tty->sx) 322 pd->px = tty->sx - pd->sx; 323 else 324 pd->px = pd->ppx; 325 326 /* Avoid zero size screens. */ 327 if (pd->border_lines == BOX_LINES_NONE) { 328 screen_resize(&pd->s, pd->sx, pd->sy, 0); 329 if (pd->job != NULL) 330 job_resize(pd->job, pd->sx, pd->sy ); 331 } else if (pd->sx > 2 && pd->sy > 2) { 332 screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0); 333 if (pd->job != NULL) 334 job_resize(pd->job, pd->sx - 2, pd->sy - 2); 335 } 336 } 337 338 static void 339 popup_make_pane(struct popup_data *pd, enum layout_type type) 340 { 341 struct client *c = pd->c; 342 struct session *s = c->session; 343 struct window *w = s->curw->window; 344 struct layout_cell *lc; 345 struct window_pane *wp = w->active, *new_wp; 346 u_int hlimit; 347 const char *shell; 348 349 window_unzoom(w, 1); 350 351 lc = layout_split_pane(wp, type, -1, 0); 352 hlimit = options_get_number(s->options, "history-limit"); 353 new_wp = window_add_pane(wp->window, NULL, hlimit, 0); 354 layout_assign_pane(lc, new_wp, 0); 355 356 if (pd->job != NULL) { 357 new_wp->fd = job_transfer(pd->job, &new_wp->pid, new_wp->tty, 358 sizeof new_wp->tty); 359 pd->job = NULL; 360 } 361 362 screen_set_title(&pd->s, new_wp->base.title); 363 screen_free(&new_wp->base); 364 memcpy(&new_wp->base, &pd->s, sizeof wp->base); 365 screen_resize(&new_wp->base, new_wp->sx, new_wp->sy, 1); 366 screen_init(&pd->s, 1, 1, 0); 367 368 shell = options_get_string(s->options, "default-shell"); 369 if (!checkshell(shell)) 370 shell = _PATH_BSHELL; 371 new_wp->shell = xstrdup(shell); 372 373 window_pane_set_event(new_wp); 374 window_set_active_pane(w, new_wp, 1); 375 new_wp->flags |= PANE_CHANGED; 376 377 pd->close = 1; 378 } 379 380 static void 381 popup_menu_done(__unused struct menu *menu, __unused u_int choice, 382 key_code key, void *data) 383 { 384 struct popup_data *pd = data; 385 struct client *c = pd->c; 386 struct paste_buffer *pb; 387 const char *buf; 388 size_t len; 389 390 pd->md = NULL; 391 pd->menu = NULL; 392 server_redraw_client(pd->c); 393 394 switch (key) { 395 case 'p': 396 pb = paste_get_top(NULL); 397 if (pb != NULL) { 398 buf = paste_buffer_data(pb, &len); 399 bufferevent_write(job_get_event(pd->job), buf, len); 400 } 401 break; 402 case 'F': 403 pd->sx = c->tty.sx; 404 pd->sy = c->tty.sy; 405 pd->px = 0; 406 pd->py = 0; 407 server_redraw_client(c); 408 break; 409 case 'C': 410 pd->px = c->tty.sx / 2 - pd->sx / 2; 411 pd->py = c->tty.sy / 2 - pd->sy / 2; 412 server_redraw_client(c); 413 break; 414 case 'h': 415 popup_make_pane(pd, LAYOUT_LEFTRIGHT); 416 break; 417 case 'v': 418 popup_make_pane(pd, LAYOUT_TOPBOTTOM); 419 break; 420 case 'q': 421 pd->close = 1; 422 break; 423 } 424 } 425 426 static void 427 popup_handle_drag(struct client *c, struct popup_data *pd, 428 struct mouse_event *m) 429 { 430 u_int px, py; 431 432 if (!MOUSE_DRAG(m->b)) 433 pd->dragging = OFF; 434 else if (pd->dragging == MOVE) { 435 if (m->x < pd->dx) 436 px = 0; 437 else if (m->x - pd->dx + pd->sx > c->tty.sx) 438 px = c->tty.sx - pd->sx; 439 else 440 px = m->x - pd->dx; 441 if (m->y < pd->dy) 442 py = 0; 443 else if (m->y - pd->dy + pd->sy > c->tty.sy) 444 py = c->tty.sy - pd->sy; 445 else 446 py = m->y - pd->dy; 447 pd->px = px; 448 pd->py = py; 449 pd->dx = m->x - pd->px; 450 pd->dy = m->y - pd->py; 451 pd->ppx = px; 452 pd->ppy = py; 453 server_redraw_client(c); 454 } else if (pd->dragging == SIZE) { 455 if (pd->border_lines == BOX_LINES_NONE) { 456 if (m->x < pd->px + 1) 457 return; 458 if (m->y < pd->py + 1) 459 return; 460 } else { 461 if (m->x < pd->px + 3) 462 return; 463 if (m->y < pd->py + 3) 464 return; 465 } 466 pd->sx = m->x - pd->px; 467 pd->sy = m->y - pd->py; 468 pd->psx = pd->sx; 469 pd->psy = pd->sy; 470 471 if (pd->border_lines == BOX_LINES_NONE) { 472 screen_resize(&pd->s, pd->sx, pd->sy, 0); 473 if (pd->job != NULL) 474 job_resize(pd->job, pd->sx, pd->sy); 475 } else { 476 screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0); 477 if (pd->job != NULL) 478 job_resize(pd->job, pd->sx - 2, pd->sy - 2); 479 } 480 server_redraw_client(c); 481 } 482 } 483 484 static int 485 popup_key_cb(struct client *c, void *data, struct key_event *event) 486 { 487 struct popup_data *pd = data; 488 struct mouse_event *m = &event->m; 489 const char *buf; 490 size_t len; 491 u_int px, py, x; 492 enum { NONE, LEFT, RIGHT, TOP, BOTTOM } border = NONE; 493 494 if (pd->md != NULL) { 495 if (menu_key_cb(c, pd->md, event) == 1) { 496 pd->md = NULL; 497 pd->menu = NULL; 498 if (pd->close) 499 server_client_clear_overlay(c); 500 else 501 server_redraw_client(c); 502 } 503 return (0); 504 } 505 506 if (KEYC_IS_MOUSE(event->key)) { 507 if (pd->dragging != OFF) { 508 popup_handle_drag(c, pd, m); 509 goto out; 510 } 511 if (m->x < pd->px || 512 m->x > pd->px + pd->sx - 1 || 513 m->y < pd->py || 514 m->y > pd->py + pd->sy - 1) { 515 if (MOUSE_BUTTONS(m->b) == MOUSE_BUTTON_3) 516 goto menu; 517 return (0); 518 } 519 if (pd->border_lines != BOX_LINES_NONE) { 520 if (m->x == pd->px) 521 border = LEFT; 522 else if (m->x == pd->px + pd->sx - 1) 523 border = RIGHT; 524 else if (m->y == pd->py) 525 border = TOP; 526 else if (m->y == pd->py + pd->sy - 1) 527 border = BOTTOM; 528 } 529 if ((m->b & MOUSE_MASK_MODIFIERS) == 0 && 530 MOUSE_BUTTONS(m->b) == MOUSE_BUTTON_3 && 531 (border == LEFT || border == TOP)) 532 goto menu; 533 if (((m->b & MOUSE_MASK_MODIFIERS) == MOUSE_MASK_META) || 534 border != NONE) { 535 if (!MOUSE_DRAG(m->b)) 536 goto out; 537 if (MOUSE_BUTTONS(m->lb) == MOUSE_BUTTON_1) 538 pd->dragging = MOVE; 539 else if (MOUSE_BUTTONS(m->lb) == MOUSE_BUTTON_3) 540 pd->dragging = SIZE; 541 pd->dx = m->lx - pd->px; 542 pd->dy = m->ly - pd->py; 543 goto out; 544 } 545 } 546 if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) || 547 pd->job == NULL) && 548 (event->key == '\033' || event->key == ('c'|KEYC_CTRL))) 549 return (1); 550 if (pd->job != NULL) { 551 if (KEYC_IS_MOUSE(event->key)) { 552 /* Must be inside, checked already. */ 553 if (pd->border_lines == BOX_LINES_NONE) { 554 px = m->x - pd->px; 555 py = m->y - pd->py; 556 } else { 557 px = m->x - pd->px - 1; 558 py = m->y - pd->py - 1; 559 } 560 if (!input_key_get_mouse(&pd->s, m, px, py, &buf, &len)) 561 return (0); 562 bufferevent_write(job_get_event(pd->job), buf, len); 563 return (0); 564 } 565 input_key(&pd->s, job_get_event(pd->job), event->key); 566 } 567 return (0); 568 569 menu: 570 pd->menu = menu_create(""); 571 if (pd->flags & POPUP_INTERNAL) { 572 menu_add_items(pd->menu, popup_internal_menu_items, NULL, c, 573 NULL); 574 } else 575 menu_add_items(pd->menu, popup_menu_items, NULL, c, NULL); 576 if (m->x >= (pd->menu->width + 4) / 2) 577 x = m->x - (pd->menu->width + 4) / 2; 578 else 579 x = 0; 580 pd->md = menu_prepare(pd->menu, 0, 0, NULL, x, m->y, c, 581 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL, popup_menu_done, pd); 582 c->flags |= CLIENT_REDRAWOVERLAY; 583 584 out: 585 pd->lx = m->x; 586 pd->ly = m->y; 587 pd->lb = m->b; 588 return (0); 589 } 590 591 static void 592 popup_job_update_cb(struct job *job) 593 { 594 struct popup_data *pd = job_get_data(job); 595 struct evbuffer *evb = job_get_event(job)->input; 596 struct client *c = pd->c; 597 struct screen *s = &pd->s; 598 void *data = EVBUFFER_DATA(evb); 599 size_t size = EVBUFFER_LENGTH(evb); 600 601 if (size == 0) 602 return; 603 604 if (pd->md != NULL) { 605 c->overlay_check = menu_check_cb; 606 c->overlay_data = pd->md; 607 } else { 608 c->overlay_check = NULL; 609 c->overlay_data = NULL; 610 } 611 input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size); 612 c->overlay_check = popup_check_cb; 613 c->overlay_data = pd; 614 615 evbuffer_drain(evb, size); 616 } 617 618 static void 619 popup_job_complete_cb(struct job *job) 620 { 621 struct popup_data *pd = job_get_data(job); 622 int status; 623 624 status = job_get_status(pd->job); 625 if (WIFEXITED(status)) 626 pd->status = WEXITSTATUS(status); 627 else if (WIFSIGNALED(status)) 628 pd->status = WTERMSIG(status); 629 else 630 pd->status = 0; 631 pd->job = NULL; 632 633 if ((pd->flags & POPUP_CLOSEEXIT) || 634 ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0)) 635 server_client_clear_overlay(pd->c); 636 } 637 638 int 639 popup_display(int flags, enum box_lines lines, struct cmdq_item *item, u_int px, 640 u_int py, u_int sx, u_int sy, struct environ *env, const char *shellcmd, 641 int argc, char **argv, const char *cwd, const char *title, struct client *c, 642 struct session *s, const char *style, const char *border_style, 643 popup_close_cb cb, void *arg) 644 { 645 struct popup_data *pd; 646 u_int jx, jy; 647 struct options *o; 648 struct style sytmp; 649 650 if (s != NULL) 651 o = s->curw->window->options; 652 else 653 o = c->session->curw->window->options; 654 655 if (lines == BOX_LINES_DEFAULT) 656 lines = options_get_number(o, "popup-border-lines"); 657 if (lines == BOX_LINES_NONE) { 658 if (sx < 1 || sy < 1) 659 return (-1); 660 jx = sx; 661 jy = sy; 662 } else { 663 if (sx < 3 || sy < 3) 664 return (-1); 665 jx = sx - 2; 666 jy = sy - 2; 667 } 668 if (c->tty.sx < sx || c->tty.sy < sy) 669 return (-1); 670 671 pd = xcalloc(1, sizeof *pd); 672 pd->item = item; 673 pd->flags = flags; 674 if (title != NULL) 675 pd->title = xstrdup(title); 676 677 pd->c = c; 678 pd->c->references++; 679 680 pd->cb = cb; 681 pd->arg = arg; 682 pd->status = 128 + SIGHUP; 683 684 pd->border_lines = lines; 685 memcpy(&pd->border_cell, &grid_default_cell, sizeof pd->border_cell); 686 style_apply(&pd->border_cell, o, "popup-border-style", NULL); 687 if (border_style != NULL) { 688 style_set(&sytmp, &grid_default_cell); 689 if (style_parse(&sytmp, &pd->border_cell, border_style) == 0) { 690 pd->border_cell.fg = sytmp.gc.fg; 691 pd->border_cell.bg = sytmp.gc.bg; 692 } 693 } 694 pd->border_cell.attr = 0; 695 696 screen_init(&pd->s, jx, jy, 0); 697 screen_set_default_cursor(&pd->s, global_w_options); 698 colour_palette_init(&pd->palette); 699 colour_palette_from_option(&pd->palette, global_w_options); 700 701 memcpy(&pd->defaults, &grid_default_cell, sizeof pd->defaults); 702 style_apply(&pd->defaults, o, "popup-style", NULL); 703 if (style != NULL) { 704 style_set(&sytmp, &grid_default_cell); 705 if (style_parse(&sytmp, &pd->defaults, style) == 0) { 706 pd->defaults.fg = sytmp.gc.fg; 707 pd->defaults.bg = sytmp.gc.bg; 708 } 709 } 710 pd->defaults.attr = 0; 711 712 pd->px = px; 713 pd->py = py; 714 pd->sx = sx; 715 pd->sy = sy; 716 717 pd->ppx = px; 718 pd->ppy = py; 719 pd->psx = sx; 720 pd->psy = sy; 721 722 pd->job = job_run(shellcmd, argc, argv, env, s, cwd, 723 popup_job_update_cb, popup_job_complete_cb, NULL, pd, 724 JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE|JOB_DEFAULTSHELL, jx, jy); 725 pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette); 726 727 server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb, 728 popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd); 729 return (0); 730 } 731 732 static void 733 popup_editor_free(struct popup_editor *pe) 734 { 735 unlink(pe->path); 736 free(pe->path); 737 free(pe); 738 } 739 740 static void 741 popup_editor_close_cb(int status, void *arg) 742 { 743 struct popup_editor *pe = arg; 744 FILE *f; 745 char *buf = NULL; 746 off_t len = 0; 747 748 if (status != 0) { 749 pe->cb(NULL, 0, pe->arg); 750 popup_editor_free(pe); 751 return; 752 } 753 754 f = fopen(pe->path, "r"); 755 if (f != NULL) { 756 fseeko(f, 0, SEEK_END); 757 len = ftello(f); 758 fseeko(f, 0, SEEK_SET); 759 760 if (len == 0 || 761 (uintmax_t)len > (uintmax_t)SIZE_MAX || 762 (buf = malloc(len)) == NULL || 763 fread(buf, len, 1, f) != 1) { 764 free(buf); 765 buf = NULL; 766 len = 0; 767 } 768 fclose(f); 769 } 770 pe->cb(buf, len, pe->arg); /* callback now owns buffer */ 771 popup_editor_free(pe); 772 } 773 774 int 775 popup_editor(struct client *c, const char *buf, size_t len, 776 popup_finish_edit_cb cb, void *arg) 777 { 778 struct popup_editor *pe; 779 int fd; 780 FILE *f; 781 char *cmd; 782 char path[] = _PATH_TMP "tmux.XXXXXXXX"; 783 const char *editor; 784 u_int px, py, sx, sy; 785 786 editor = options_get_string(global_options, "editor"); 787 if (*editor == '\0') 788 return (-1); 789 790 fd = mkstemp(path); 791 if (fd == -1) 792 return (-1); 793 f = fdopen(fd, "w"); 794 if (f == NULL) 795 return (-1); 796 if (fwrite(buf, len, 1, f) != 1) { 797 fclose(f); 798 return (-1); 799 } 800 fclose(f); 801 802 pe = xcalloc(1, sizeof *pe); 803 pe->path = xstrdup(path); 804 pe->cb = cb; 805 pe->arg = arg; 806 807 sx = c->tty.sx * 9 / 10; 808 sy = c->tty.sy * 9 / 10; 809 px = (c->tty.sx / 2) - (sx / 2); 810 py = (c->tty.sy / 2) - (sy / 2); 811 812 xasprintf(&cmd, "%s %s", editor, path); 813 if (popup_display(POPUP_INTERNAL|POPUP_CLOSEEXIT, BOX_LINES_DEFAULT, 814 NULL, px, py, sx, sy, NULL, cmd, 0, NULL, _PATH_TMP, NULL, c, NULL, 815 NULL, NULL, popup_editor_close_cb, pe) != 0) { 816 popup_editor_free(pe); 817 free(cmd); 818 return (-1); 819 } 820 free(cmd); 821 return (0); 822 } 823