1/* $OpenBSD: popup.c,v 1.53 2024/03/21 11:30:42 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 30struct 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 74struct popup_editor { 75 char *path; 76 popup_finish_edit_cb cb; 77 void *arg; 78}; 79 80static 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 93static 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 102static void 103popup_redraw_cb(const struct tty_ctx *ttyctx) 104{ 105 struct popup_data *pd = ttyctx->arg; 106 107 pd->c->flags |= CLIENT_REDRAWOVERLAY; 108} 109 110static int 111popup_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 137static void 138popup_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 149static struct screen * 150popup_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. */ 168static void 169popup_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 212static void 213popup_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 266static void 267popup_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 297static void 298popup_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 338static void 339popup_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 new_wp->fd = job_transfer(pd->job, &new_wp->pid, new_wp->tty, 357 sizeof new_wp->tty); 358 pd->job = NULL; 359 360 screen_set_title(&pd->s, new_wp->base.title); 361 screen_free(&new_wp->base); 362 memcpy(&new_wp->base, &pd->s, sizeof wp->base); 363 screen_resize(&new_wp->base, new_wp->sx, new_wp->sy, 1); 364 screen_init(&pd->s, 1, 1, 0); 365 366 shell = options_get_string(s->options, "default-shell"); 367 if (!checkshell(shell)) 368 shell = _PATH_BSHELL; 369 new_wp->shell = xstrdup(shell); 370 371 window_pane_set_event(new_wp); 372 window_set_active_pane(w, new_wp, 1); 373 new_wp->flags |= PANE_CHANGED; 374 375 pd->close = 1; 376} 377 378static void 379popup_menu_done(__unused struct menu *menu, __unused u_int choice, 380 key_code key, void *data) 381{ 382 struct popup_data *pd = data; 383 struct client *c = pd->c; 384 struct paste_buffer *pb; 385 const char *buf; 386 size_t len; 387 388 pd->md = NULL; 389 pd->menu = NULL; 390 server_redraw_client(pd->c); 391 392 switch (key) { 393 case 'p': 394 pb = paste_get_top(NULL); 395 if (pb != NULL) { 396 buf = paste_buffer_data(pb, &len); 397 bufferevent_write(job_get_event(pd->job), buf, len); 398 } 399 break; 400 case 'F': 401 pd->sx = c->tty.sx; 402 pd->sy = c->tty.sy; 403 pd->px = 0; 404 pd->py = 0; 405 server_redraw_client(c); 406 break; 407 case 'C': 408 pd->px = c->tty.sx / 2 - pd->sx / 2; 409 pd->py = c->tty.sy / 2 - pd->sy / 2; 410 server_redraw_client(c); 411 break; 412 case 'h': 413 popup_make_pane(pd, LAYOUT_LEFTRIGHT); 414 break; 415 case 'v': 416 popup_make_pane(pd, LAYOUT_TOPBOTTOM); 417 break; 418 case 'q': 419 pd->close = 1; 420 break; 421 } 422} 423 424static void 425popup_handle_drag(struct client *c, struct popup_data *pd, 426 struct mouse_event *m) 427{ 428 u_int px, py; 429 430 if (!MOUSE_DRAG(m->b)) 431 pd->dragging = OFF; 432 else if (pd->dragging == MOVE) { 433 if (m->x < pd->dx) 434 px = 0; 435 else if (m->x - pd->dx + pd->sx > c->tty.sx) 436 px = c->tty.sx - pd->sx; 437 else 438 px = m->x - pd->dx; 439 if (m->y < pd->dy) 440 py = 0; 441 else if (m->y - pd->dy + pd->sy > c->tty.sy) 442 py = c->tty.sy - pd->sy; 443 else 444 py = m->y - pd->dy; 445 pd->px = px; 446 pd->py = py; 447 pd->dx = m->x - pd->px; 448 pd->dy = m->y - pd->py; 449 pd->ppx = px; 450 pd->ppy = py; 451 server_redraw_client(c); 452 } else if (pd->dragging == SIZE) { 453 if (pd->border_lines == BOX_LINES_NONE) { 454 if (m->x < pd->px + 1) 455 return; 456 if (m->y < pd->py + 1) 457 return; 458 } else { 459 if (m->x < pd->px + 3) 460 return; 461 if (m->y < pd->py + 3) 462 return; 463 } 464 pd->sx = m->x - pd->px; 465 pd->sy = m->y - pd->py; 466 pd->psx = pd->sx; 467 pd->psy = pd->sy; 468 469 if (pd->border_lines == BOX_LINES_NONE) { 470 screen_resize(&pd->s, pd->sx, pd->sy, 0); 471 if (pd->job != NULL) 472 job_resize(pd->job, pd->sx, pd->sy); 473 } else { 474 screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0); 475 if (pd->job != NULL) 476 job_resize(pd->job, pd->sx - 2, pd->sy - 2); 477 } 478 server_redraw_client(c); 479 } 480} 481 482static int 483popup_key_cb(struct client *c, void *data, struct key_event *event) 484{ 485 struct popup_data *pd = data; 486 struct mouse_event *m = &event->m; 487 const char *buf; 488 size_t len; 489 u_int px, py, x; 490 enum { NONE, LEFT, RIGHT, TOP, BOTTOM } border = NONE; 491 492 if (pd->md != NULL) { 493 if (menu_key_cb(c, pd->md, event) == 1) { 494 pd->md = NULL; 495 pd->menu = NULL; 496 if (pd->close) 497 server_client_clear_overlay(c); 498 else 499 server_redraw_client(c); 500 } 501 return (0); 502 } 503 504 if (KEYC_IS_MOUSE(event->key)) { 505 if (pd->dragging != OFF) { 506 popup_handle_drag(c, pd, m); 507 goto out; 508 } 509 if (m->x < pd->px || 510 m->x > pd->px + pd->sx - 1 || 511 m->y < pd->py || 512 m->y > pd->py + pd->sy - 1) { 513 if (MOUSE_BUTTONS(m->b) == MOUSE_BUTTON_3) 514 goto menu; 515 return (0); 516 } 517 if (pd->border_lines != BOX_LINES_NONE) { 518 if (m->x == pd->px) 519 border = LEFT; 520 else if (m->x == pd->px + pd->sx - 1) 521 border = RIGHT; 522 else if (m->y == pd->py) 523 border = TOP; 524 else if (m->y == pd->py + pd->sy - 1) 525 border = BOTTOM; 526 } 527 if ((m->b & MOUSE_MASK_MODIFIERS) == 0 && 528 MOUSE_BUTTONS(m->b) == MOUSE_BUTTON_3 && 529 (border == LEFT || border == TOP)) 530 goto menu; 531 if (((m->b & MOUSE_MASK_MODIFIERS) == MOUSE_MASK_META) || 532 border != NONE) { 533 if (!MOUSE_DRAG(m->b)) 534 goto out; 535 if (MOUSE_BUTTONS(m->lb) == MOUSE_BUTTON_1) 536 pd->dragging = MOVE; 537 else if (MOUSE_BUTTONS(m->lb) == MOUSE_BUTTON_3) 538 pd->dragging = SIZE; 539 pd->dx = m->lx - pd->px; 540 pd->dy = m->ly - pd->py; 541 goto out; 542 } 543 } 544 if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) || 545 pd->job == NULL) && 546 (event->key == '\033' || event->key == '\003')) 547 return (1); 548 if (pd->job != NULL) { 549 if (KEYC_IS_MOUSE(event->key)) { 550 /* Must be inside, checked already. */ 551 if (pd->border_lines == BOX_LINES_NONE) { 552 px = m->x - pd->px; 553 py = m->y - pd->py; 554 } else { 555 px = m->x - pd->px - 1; 556 py = m->y - pd->py - 1; 557 } 558 if (!input_key_get_mouse(&pd->s, m, px, py, &buf, &len)) 559 return (0); 560 bufferevent_write(job_get_event(pd->job), buf, len); 561 return (0); 562 } 563 input_key(&pd->s, job_get_event(pd->job), event->key); 564 } 565 return (0); 566 567menu: 568 pd->menu = menu_create(""); 569 if (pd->flags & POPUP_INTERNAL) { 570 menu_add_items(pd->menu, popup_internal_menu_items, NULL, c, 571 NULL); 572 } else 573 menu_add_items(pd->menu, popup_menu_items, NULL, c, NULL); 574 if (m->x >= (pd->menu->width + 4) / 2) 575 x = m->x - (pd->menu->width + 4) / 2; 576 else 577 x = 0; 578 pd->md = menu_prepare(pd->menu, 0, 0, NULL, x, m->y, c, 579 BOX_LINES_DEFAULT, NULL, NULL, NULL, NULL, popup_menu_done, pd); 580 c->flags |= CLIENT_REDRAWOVERLAY; 581 582out: 583 pd->lx = m->x; 584 pd->ly = m->y; 585 pd->lb = m->b; 586 return (0); 587} 588 589static void 590popup_job_update_cb(struct job *job) 591{ 592 struct popup_data *pd = job_get_data(job); 593 struct evbuffer *evb = job_get_event(job)->input; 594 struct client *c = pd->c; 595 struct screen *s = &pd->s; 596 void *data = EVBUFFER_DATA(evb); 597 size_t size = EVBUFFER_LENGTH(evb); 598 599 if (size == 0) 600 return; 601 602 if (pd->md != NULL) { 603 c->overlay_check = menu_check_cb; 604 c->overlay_data = pd->md; 605 } else { 606 c->overlay_check = NULL; 607 c->overlay_data = NULL; 608 } 609 input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size); 610 c->overlay_check = popup_check_cb; 611 c->overlay_data = pd; 612 613 evbuffer_drain(evb, size); 614} 615 616static void 617popup_job_complete_cb(struct job *job) 618{ 619 struct popup_data *pd = job_get_data(job); 620 int status; 621 622 status = job_get_status(pd->job); 623 if (WIFEXITED(status)) 624 pd->status = WEXITSTATUS(status); 625 else if (WIFSIGNALED(status)) 626 pd->status = WTERMSIG(status); 627 else 628 pd->status = 0; 629 pd->job = NULL; 630 631 if ((pd->flags & POPUP_CLOSEEXIT) || 632 ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0)) 633 server_client_clear_overlay(pd->c); 634} 635 636int 637popup_display(int flags, enum box_lines lines, struct cmdq_item *item, u_int px, 638 u_int py, u_int sx, u_int sy, struct environ *env, const char *shellcmd, 639 int argc, char **argv, const char *cwd, const char *title, struct client *c, 640 struct session *s, const char *style, const char *border_style, 641 popup_close_cb cb, void *arg) 642{ 643 struct popup_data *pd; 644 u_int jx, jy; 645 struct options *o; 646 struct style sytmp; 647 648 if (s != NULL) 649 o = s->curw->window->options; 650 else 651 o = c->session->curw->window->options; 652 653 if (lines == BOX_LINES_DEFAULT) 654 lines = options_get_number(o, "popup-border-lines"); 655 if (lines == BOX_LINES_NONE) { 656 if (sx < 1 || sy < 1) 657 return (-1); 658 jx = sx; 659 jy = sy; 660 } else { 661 if (sx < 3 || sy < 3) 662 return (-1); 663 jx = sx - 2; 664 jy = sy - 2; 665 } 666 if (c->tty.sx < sx || c->tty.sy < sy) 667 return (-1); 668 669 pd = xcalloc(1, sizeof *pd); 670 pd->item = item; 671 pd->flags = flags; 672 if (title != NULL) 673 pd->title = xstrdup(title); 674 675 pd->c = c; 676 pd->c->references++; 677 678 pd->cb = cb; 679 pd->arg = arg; 680 pd->status = 128 + SIGHUP; 681 682 pd->border_lines = lines; 683 memcpy(&pd->border_cell, &grid_default_cell, sizeof pd->border_cell); 684 style_apply(&pd->border_cell, o, "popup-border-style", NULL); 685 if (border_style != NULL) { 686 style_set(&sytmp, &grid_default_cell); 687 if (style_parse(&sytmp, &pd->border_cell, border_style) == 0) { 688 pd->border_cell.fg = sytmp.gc.fg; 689 pd->border_cell.bg = sytmp.gc.bg; 690 } 691 } 692 pd->border_cell.attr = 0; 693 694 screen_init(&pd->s, jx, jy, 0); 695 colour_palette_init(&pd->palette); 696 colour_palette_from_option(&pd->palette, global_w_options); 697 698 memcpy(&pd->defaults, &grid_default_cell, sizeof pd->defaults); 699 style_apply(&pd->defaults, o, "popup-style", NULL); 700 if (style != NULL) { 701 style_set(&sytmp, &grid_default_cell); 702 if (style_parse(&sytmp, &pd->defaults, style) == 0) { 703 pd->defaults.fg = sytmp.gc.fg; 704 pd->defaults.bg = sytmp.gc.bg; 705 } 706 } 707 pd->defaults.attr = 0; 708 709 pd->px = px; 710 pd->py = py; 711 pd->sx = sx; 712 pd->sy = sy; 713 714 pd->ppx = px; 715 pd->ppy = py; 716 pd->psx = sx; 717 pd->psy = sy; 718 719 pd->job = job_run(shellcmd, argc, argv, env, s, cwd, 720 popup_job_update_cb, popup_job_complete_cb, NULL, pd, 721 JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE, jx, jy); 722 pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette); 723 724 server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb, 725 popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd); 726 return (0); 727} 728 729static void 730popup_editor_free(struct popup_editor *pe) 731{ 732 unlink(pe->path); 733 free(pe->path); 734 free(pe); 735} 736 737static void 738popup_editor_close_cb(int status, void *arg) 739{ 740 struct popup_editor *pe = arg; 741 FILE *f; 742 char *buf = NULL; 743 off_t len = 0; 744 745 if (status != 0) { 746 pe->cb(NULL, 0, pe->arg); 747 popup_editor_free(pe); 748 return; 749 } 750 751 f = fopen(pe->path, "r"); 752 if (f != NULL) { 753 fseeko(f, 0, SEEK_END); 754 len = ftello(f); 755 fseeko(f, 0, SEEK_SET); 756 757 if (len == 0 || 758 (uintmax_t)len > (uintmax_t)SIZE_MAX || 759 (buf = malloc(len)) == NULL || 760 fread(buf, len, 1, f) != 1) { 761 free(buf); 762 buf = NULL; 763 len = 0; 764 } 765 fclose(f); 766 } 767 pe->cb(buf, len, pe->arg); /* callback now owns buffer */ 768 popup_editor_free(pe); 769} 770 771int 772popup_editor(struct client *c, const char *buf, size_t len, 773 popup_finish_edit_cb cb, void *arg) 774{ 775 struct popup_editor *pe; 776 int fd; 777 FILE *f; 778 char *cmd; 779 char path[] = _PATH_TMP "tmux.XXXXXXXX"; 780 const char *editor; 781 u_int px, py, sx, sy; 782 783 editor = options_get_string(global_options, "editor"); 784 if (*editor == '\0') 785 return (-1); 786 787 fd = mkstemp(path); 788 if (fd == -1) 789 return (-1); 790 f = fdopen(fd, "w"); 791 if (f == NULL) 792 return (-1); 793 if (fwrite(buf, len, 1, f) != 1) { 794 fclose(f); 795 return (-1); 796 } 797 fclose(f); 798 799 pe = xcalloc(1, sizeof *pe); 800 pe->path = xstrdup(path); 801 pe->cb = cb; 802 pe->arg = arg; 803 804 sx = c->tty.sx * 9 / 10; 805 sy = c->tty.sy * 9 / 10; 806 px = (c->tty.sx / 2) - (sx / 2); 807 py = (c->tty.sy / 2) - (sy / 2); 808 809 xasprintf(&cmd, "%s %s", editor, path); 810 if (popup_display(POPUP_INTERNAL|POPUP_CLOSEEXIT, BOX_LINES_DEFAULT, 811 NULL, px, py, sx, sy, NULL, cmd, 0, NULL, _PATH_TMP, NULL, c, NULL, 812 NULL, NULL, popup_editor_close_cb, pe) != 0) { 813 popup_editor_free(pe); 814 free(cmd); 815 return (-1); 816 } 817 free(cmd); 818 return (0); 819} 820