1 /* $OpenBSD: popup.c,v 1.22 2021/03/02 10:56:45 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 35 struct screen s; 36 struct job *job; 37 struct input_ctx *ictx; 38 int status; 39 popup_close_cb cb; 40 void *arg; 41 42 u_int px; 43 u_int py; 44 u_int sx; 45 u_int sy; 46 47 enum { OFF, MOVE, SIZE } dragging; 48 u_int dx; 49 u_int dy; 50 51 u_int lx; 52 u_int ly; 53 u_int lb; 54 }; 55 56 struct popup_editor { 57 char *path; 58 popup_finish_edit_cb cb; 59 void *arg; 60 }; 61 62 static void 63 popup_redraw_cb(const struct tty_ctx *ttyctx) 64 { 65 struct popup_data *pd = ttyctx->arg; 66 67 pd->c->flags |= CLIENT_REDRAWOVERLAY; 68 } 69 70 static int 71 popup_set_client_cb(struct tty_ctx *ttyctx, struct client *c) 72 { 73 struct popup_data *pd = ttyctx->arg; 74 75 if (c != pd->c) 76 return (0); 77 if (pd->c->flags & CLIENT_REDRAWOVERLAY) 78 return (0); 79 80 ttyctx->bigger = 0; 81 ttyctx->wox = 0; 82 ttyctx->woy = 0; 83 ttyctx->wsx = c->tty.sx; 84 ttyctx->wsy = c->tty.sy; 85 86 ttyctx->xoff = ttyctx->rxoff = pd->px + 1; 87 ttyctx->yoff = ttyctx->ryoff = pd->py + 1; 88 89 return (1); 90 } 91 92 static void 93 popup_init_ctx_cb(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx) 94 { 95 struct popup_data *pd = ctx->arg; 96 97 ttyctx->redraw_cb = popup_redraw_cb; 98 ttyctx->set_client_cb = popup_set_client_cb; 99 ttyctx->arg = pd; 100 } 101 102 static struct screen * 103 popup_mode_cb(struct client *c, u_int *cx, u_int *cy) 104 { 105 struct popup_data *pd = c->overlay_data; 106 107 *cx = pd->px + 1 + pd->s.cx; 108 *cy = pd->py + 1 + pd->s.cy; 109 return (&pd->s); 110 } 111 112 static int 113 popup_check_cb(struct client *c, u_int px, u_int py) 114 { 115 struct popup_data *pd = c->overlay_data; 116 117 if (px < pd->px || px > pd->px + pd->sx - 1) 118 return (1); 119 if (py < pd->py || py > pd->py + pd->sy - 1) 120 return (1); 121 return (0); 122 } 123 124 static void 125 popup_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0) 126 { 127 struct popup_data *pd = c->overlay_data; 128 struct tty *tty = &c->tty; 129 struct screen s; 130 struct screen_write_ctx ctx; 131 u_int i, px = pd->px, py = pd->py; 132 133 screen_init(&s, pd->sx, pd->sy, 0); 134 screen_write_start(&ctx, &s); 135 screen_write_clearscreen(&ctx, 8); 136 screen_write_box(&ctx, pd->sx, pd->sy); 137 screen_write_cursormove(&ctx, 1, 1, 0); 138 screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2, pd->sy - 2); 139 screen_write_stop(&ctx); 140 141 c->overlay_check = NULL; 142 for (i = 0; i < pd->sy; i++){ 143 tty_draw_line(tty, &s, 0, i, pd->sx, px, py + i, 144 &grid_default_cell, NULL); 145 } 146 c->overlay_check = popup_check_cb; 147 } 148 149 static void 150 popup_free_cb(struct client *c) 151 { 152 struct popup_data *pd = c->overlay_data; 153 struct cmdq_item *item = pd->item; 154 155 if (pd->cb != NULL) 156 pd->cb(pd->status, pd->arg); 157 158 if (item != NULL) { 159 if (cmdq_get_client(item) != NULL && 160 cmdq_get_client(item)->session == NULL) 161 cmdq_get_client(item)->retval = pd->status; 162 cmdq_continue(item); 163 } 164 server_client_unref(pd->c); 165 166 if (pd->job != NULL) 167 job_free(pd->job); 168 input_free(pd->ictx); 169 170 screen_free(&pd->s); 171 free(pd); 172 } 173 174 static void 175 popup_handle_drag(struct client *c, struct popup_data *pd, 176 struct mouse_event *m) 177 { 178 u_int px, py; 179 180 if (!MOUSE_DRAG(m->b)) 181 pd->dragging = OFF; 182 else if (pd->dragging == MOVE) { 183 if (m->x < pd->dx) 184 px = 0; 185 else if (m->x - pd->dx + pd->sx > c->tty.sx) 186 px = c->tty.sx - pd->sx; 187 else 188 px = m->x - pd->dx; 189 if (m->y < pd->dy) 190 py = 0; 191 else if (m->y - pd->dy + pd->sy > c->tty.sy) 192 py = c->tty.sy - pd->sy; 193 else 194 py = m->y - pd->dy; 195 pd->px = px; 196 pd->py = py; 197 pd->dx = m->x - pd->px; 198 pd->dy = m->y - pd->py; 199 server_redraw_client(c); 200 } else if (pd->dragging == SIZE) { 201 if (m->x < pd->px + 3) 202 return; 203 if (m->y < pd->py + 3) 204 return; 205 pd->sx = m->x - pd->px; 206 pd->sy = m->y - pd->py; 207 208 screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0); 209 if (pd->job != NULL) 210 job_resize(pd->job, pd->sx - 2, pd->sy - 2); 211 server_redraw_client(c); 212 } 213 } 214 215 static int 216 popup_key_cb(struct client *c, struct key_event *event) 217 { 218 struct popup_data *pd = c->overlay_data; 219 struct mouse_event *m = &event->m; 220 const char *buf; 221 size_t len; 222 223 if (KEYC_IS_MOUSE(event->key)) { 224 if (pd->dragging != OFF) { 225 popup_handle_drag(c, pd, m); 226 goto out; 227 } 228 if (m->x < pd->px || 229 m->x > pd->px + pd->sx - 1 || 230 m->y < pd->py || 231 m->y > pd->py + pd->sy - 1) { 232 if (MOUSE_BUTTONS (m->b) == 1) 233 return (1); 234 return (0); 235 } 236 if ((m->b & MOUSE_MASK_META) || 237 m->x == pd->px || 238 m->x == pd->px + pd->sx - 1 || 239 m->y == pd->py || 240 m->y == pd->py + pd->sy - 1) { 241 if (!MOUSE_DRAG(m->b)) 242 goto out; 243 if (MOUSE_BUTTONS(m->lb) == 0) 244 pd->dragging = MOVE; 245 else if (MOUSE_BUTTONS(m->lb) == 2) 246 pd->dragging = SIZE; 247 pd->dx = m->lx - pd->px; 248 pd->dy = m->ly - pd->py; 249 goto out; 250 } 251 } 252 253 if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) || 254 pd->job == NULL) && 255 (event->key == '\033' || event->key == '\003')) 256 return (1); 257 if (pd->job != NULL) { 258 if (KEYC_IS_MOUSE(event->key)) { 259 /* Must be inside, checked already. */ 260 if (!input_key_get_mouse(&pd->s, m, m->x - pd->px - 1, 261 m->y - pd->py - 1, &buf, &len)) 262 return (0); 263 bufferevent_write(job_get_event(pd->job), buf, len); 264 return (0); 265 } 266 input_key(&pd->s, job_get_event(pd->job), event->key); 267 } 268 return (0); 269 270 out: 271 pd->lx = m->x; 272 pd->ly = m->y; 273 pd->lb = m->b; 274 return (0); 275 } 276 277 static void 278 popup_job_update_cb(struct job *job) 279 { 280 struct popup_data *pd = job_get_data(job); 281 struct evbuffer *evb = job_get_event(job)->input; 282 struct client *c = pd->c; 283 struct screen *s = &pd->s; 284 void *data = EVBUFFER_DATA(evb); 285 size_t size = EVBUFFER_LENGTH(evb); 286 287 if (size == 0) 288 return; 289 290 c->overlay_check = NULL; 291 c->tty.flags &= ~TTY_FREEZE; 292 293 input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size); 294 295 c->tty.flags |= TTY_FREEZE; 296 c->overlay_check = popup_check_cb; 297 298 evbuffer_drain(evb, size); 299 } 300 301 static void 302 popup_job_complete_cb(struct job *job) 303 { 304 struct popup_data *pd = job_get_data(job); 305 int status; 306 307 status = job_get_status(pd->job); 308 if (WIFEXITED(status)) 309 pd->status = WEXITSTATUS(status); 310 else if (WIFSIGNALED(status)) 311 pd->status = WTERMSIG(status); 312 else 313 pd->status = 0; 314 pd->job = NULL; 315 316 if ((pd->flags & POPUP_CLOSEEXIT) || 317 ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0)) 318 server_client_clear_overlay(pd->c); 319 } 320 321 int 322 popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx, 323 u_int sy, const char *shellcmd, int argc, char **argv, const char *cwd, 324 struct client *c, struct session *s, popup_close_cb cb, void *arg) 325 { 326 struct popup_data *pd; 327 328 if (sx < 3 || sy < 3) 329 return (-1); 330 if (c->tty.sx < sx || c->tty.sy < sy) 331 return (-1); 332 333 pd = xcalloc(1, sizeof *pd); 334 pd->item = item; 335 pd->flags = flags; 336 337 pd->c = c; 338 pd->c->references++; 339 340 pd->cb = cb; 341 pd->arg = arg; 342 pd->status = 128 + SIGHUP; 343 344 screen_init(&pd->s, sx - 2, sy - 2, 0); 345 346 pd->px = px; 347 pd->py = py; 348 pd->sx = sx; 349 pd->sy = sy; 350 351 pd->job = job_run(shellcmd, argc, argv, s, cwd, 352 popup_job_update_cb, popup_job_complete_cb, NULL, pd, 353 JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE, pd->sx - 2, pd->sy - 2); 354 pd->ictx = input_init(NULL, job_get_event(pd->job)); 355 356 server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb, 357 popup_draw_cb, popup_key_cb, popup_free_cb, pd); 358 return (0); 359 } 360 361 static void 362 popup_editor_free(struct popup_editor *pe) 363 { 364 unlink(pe->path); 365 free(pe->path); 366 free(pe); 367 } 368 369 static void 370 popup_editor_close_cb(int status, void *arg) 371 { 372 struct popup_editor *pe = arg; 373 FILE *f; 374 char *buf = NULL; 375 off_t len = 0; 376 377 if (status != 0) { 378 pe->cb(NULL, 0, pe->arg); 379 popup_editor_free(pe); 380 return; 381 } 382 383 f = fopen(pe->path, "r"); 384 if (f != NULL) { 385 fseeko(f, 0, SEEK_END); 386 len = ftello(f); 387 fseeko(f, 0, SEEK_SET); 388 389 if (len == 0 || 390 (uintmax_t)len > (uintmax_t)SIZE_MAX || 391 (buf = malloc(len)) == NULL || 392 fread(buf, len, 1, f) != 1) { 393 free(buf); 394 buf = NULL; 395 len = 0; 396 } 397 fclose(f); 398 } 399 pe->cb(buf, len, pe->arg); /* callback now owns buffer */ 400 popup_editor_free(pe); 401 } 402 403 int 404 popup_editor(struct client *c, const char *buf, size_t len, 405 popup_finish_edit_cb cb, void *arg) 406 { 407 struct popup_editor *pe; 408 int fd; 409 FILE *f; 410 char *cmd; 411 char path[] = _PATH_TMP "tmux.XXXXXXXX"; 412 const char *editor; 413 u_int px, py, sx, sy; 414 415 editor = options_get_string(global_options, "editor"); 416 if (*editor == '\0') 417 return (-1); 418 419 fd = mkstemp(path); 420 if (fd == -1) 421 return (-1); 422 f = fdopen(fd, "w"); 423 if (fwrite(buf, len, 1, f) != 1) { 424 fclose(f); 425 return (-1); 426 } 427 fclose(f); 428 429 pe = xcalloc(1, sizeof *pe); 430 pe->path = xstrdup(path); 431 pe->cb = cb; 432 pe->arg = arg; 433 434 sx = c->tty.sx * 9 / 10; 435 sy = c->tty.sy * 9 / 10; 436 px = (c->tty.sx / 2) - (sx / 2); 437 py = (c->tty.sy / 2) - (sy / 2); 438 439 xasprintf(&cmd, "%s %s", editor, path); 440 if (popup_display(POPUP_CLOSEEXIT, NULL, px, py, sx, sy, cmd, 0, NULL, 441 _PATH_TMP, c, NULL, popup_editor_close_cb, pe) != 0) { 442 popup_editor_free(pe); 443 free(cmd); 444 return (-1); 445 } 446 free(cmd); 447 return (0); 448 } 449