Annotation of src/usr.bin/tmux/popup.c, Revision 1.26
1.26 ! nicm 1: /* $OpenBSD: popup.c,v 1.25 2021/08/11 20:49:55 nicm Exp $ */
1.1 nicm 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:
1.17 nicm 22: #include <paths.h>
1.1 nicm 23: #include <signal.h>
24: #include <stdlib.h>
25: #include <string.h>
1.17 nicm 26: #include <unistd.h>
1.1 nicm 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;
1.25 nicm 36: struct colour_palette palette;
1.1 nicm 37: struct job *job;
38: struct input_ctx *ictx;
39: int status;
1.14 nicm 40: popup_close_cb cb;
41: void *arg;
1.1 nicm 42:
1.23 nicm 43: /* Current position and size. */
1.1 nicm 44: u_int px;
45: u_int py;
46: u_int sx;
47: u_int sy;
48:
1.23 nicm 49: /* Preferred position and size. */
50: u_int ppx;
51: u_int ppy;
52: u_int psx;
53: u_int psy;
54:
1.1 nicm 55: enum { OFF, MOVE, SIZE } dragging;
56: u_int dx;
57: u_int dy;
58:
59: u_int lx;
60: u_int ly;
61: u_int lb;
62: };
63:
1.17 nicm 64: struct popup_editor {
65: char *path;
66: popup_finish_edit_cb cb;
67: void *arg;
68: };
69:
1.1 nicm 70: static void
1.15 nicm 71: popup_redraw_cb(const struct tty_ctx *ttyctx)
72: {
73: struct popup_data *pd = ttyctx->arg;
74:
75: pd->c->flags |= CLIENT_REDRAWOVERLAY;
76: }
77:
78: static int
79: popup_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
80: {
81: struct popup_data *pd = ttyctx->arg;
82:
1.16 nicm 83: if (c != pd->c)
84: return (0);
1.15 nicm 85: if (pd->c->flags & CLIENT_REDRAWOVERLAY)
1.16 nicm 86: return (0);
1.15 nicm 87:
88: ttyctx->bigger = 0;
89: ttyctx->wox = 0;
90: ttyctx->woy = 0;
91: ttyctx->wsx = c->tty.sx;
92: ttyctx->wsy = c->tty.sy;
93:
1.26 ! nicm 94: if (pd->flags & POPUP_NOBORDER) {
! 95: ttyctx->xoff = ttyctx->rxoff = pd->px;
! 96: ttyctx->yoff = ttyctx->ryoff = pd->py;
! 97: } else {
! 98: ttyctx->xoff = ttyctx->rxoff = pd->px + 1;
! 99: ttyctx->yoff = ttyctx->ryoff = pd->py + 1;
! 100: }
1.15 nicm 101:
102: return (1);
103: }
104:
105: static void
106: popup_init_ctx_cb(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
107: {
108: struct popup_data *pd = ctx->arg;
109:
1.25 nicm 110: ttyctx->palette = &pd->palette;
1.15 nicm 111: ttyctx->redraw_cb = popup_redraw_cb;
112: ttyctx->set_client_cb = popup_set_client_cb;
113: ttyctx->arg = pd;
114: }
115:
116: static struct screen *
1.1 nicm 117: popup_mode_cb(struct client *c, u_int *cx, u_int *cy)
118: {
119: struct popup_data *pd = c->overlay_data;
120:
1.26 ! nicm 121: if (pd->flags & POPUP_NOBORDER) {
! 122: *cx = pd->px + pd->s.cx;
! 123: *cy = pd->py + pd->s.cy;
! 124: } else {
! 125: *cx = pd->px + 1 + pd->s.cx;
! 126: *cy = pd->py + 1 + pd->s.cy;
! 127: }
1.15 nicm 128: return (&pd->s);
1.1 nicm 129: }
130:
131: static int
132: popup_check_cb(struct client *c, u_int px, u_int py)
133: {
134: struct popup_data *pd = c->overlay_data;
135:
136: if (px < pd->px || px > pd->px + pd->sx - 1)
137: return (1);
138: if (py < pd->py || py > pd->py + pd->sy - 1)
139: return (1);
140: return (0);
141: }
142:
143: static void
144: popup_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0)
145: {
146: struct popup_data *pd = c->overlay_data;
147: struct tty *tty = &c->tty;
148: struct screen s;
149: struct screen_write_ctx ctx;
150: u_int i, px = pd->px, py = pd->py;
1.25 nicm 151: struct colour_palette *palette = &pd->palette;
152: struct grid_cell gc;
1.1 nicm 153:
154: screen_init(&s, pd->sx, pd->sy, 0);
1.15 nicm 155: screen_write_start(&ctx, &s);
1.1 nicm 156: screen_write_clearscreen(&ctx, 8);
1.23 nicm 157:
1.26 ! nicm 158: if (pd->flags & POPUP_NOBORDER) {
! 159: screen_write_cursormove(&ctx, 0, 0, 0);
! 160: screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx, pd->sy);
! 161: } else if (pd->sx > 2 && pd->sy > 2) {
1.23 nicm 162: screen_write_box(&ctx, pd->sx, pd->sy);
163: screen_write_cursormove(&ctx, 1, 1, 0);
164: screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2,
165: pd->sy - 2);
166: }
1.1 nicm 167: screen_write_stop(&ctx);
168:
1.25 nicm 169: memcpy(&gc, &grid_default_cell, sizeof gc);
170: gc.fg = pd->palette.fg;
171: gc.bg = pd->palette.bg;
172:
1.1 nicm 173: c->overlay_check = NULL;
1.25 nicm 174: for (i = 0; i < pd->sy; i++)
175: tty_draw_line(tty, &s, 0, i, pd->sx, px, py + i, &gc, palette);
1.1 nicm 176: c->overlay_check = popup_check_cb;
177: }
178:
179: static void
180: popup_free_cb(struct client *c)
181: {
182: struct popup_data *pd = c->overlay_data;
183: struct cmdq_item *item = pd->item;
184:
1.14 nicm 185: if (pd->cb != NULL)
186: pd->cb(pd->status, pd->arg);
187:
1.1 nicm 188: if (item != NULL) {
1.22 nicm 189: if (cmdq_get_client(item) != NULL &&
1.8 nicm 190: cmdq_get_client(item)->session == NULL)
191: cmdq_get_client(item)->retval = pd->status;
1.1 nicm 192: cmdq_continue(item);
193: }
194: server_client_unref(pd->c);
195:
196: if (pd->job != NULL)
197: job_free(pd->job);
1.22 nicm 198: input_free(pd->ictx);
1.1 nicm 199:
200: screen_free(&pd->s);
1.25 nicm 201: colour_palette_free(&pd->palette);
1.1 nicm 202: free(pd);
203: }
204:
205: static void
1.23 nicm 206: popup_resize_cb(struct client *c)
207: {
208: struct popup_data *pd = c->overlay_data;
209: struct tty *tty = &c->tty;
210:
211: if (pd == NULL)
212: return;
213:
214: /* Adjust position and size. */
215: if (pd->psy > tty->sy)
216: pd->sy = tty->sy;
217: else
218: pd->sy = pd->psy;
219: if (pd->psx > tty->sx)
220: pd->sx = tty->sx;
221: else
222: pd->sx = pd->psx;
223: if (pd->ppy + pd->sy > tty->sy)
224: pd->py = tty->sy - pd->sy;
225: else
226: pd->py = pd->ppy;
227: if (pd->ppx + pd->sx > tty->sx)
228: pd->px = tty->sx - pd->sx;
229: else
230: pd->px = pd->ppx;
231:
232: /* Avoid zero size screens. */
1.26 ! nicm 233: if (pd->flags & POPUP_NOBORDER) {
! 234: screen_resize(&pd->s, pd->sx, pd->sy, 0);
! 235: if (pd->job != NULL)
! 236: job_resize(pd->job, pd->sx, pd->sy );
! 237: } else if (pd->sx > 2 && pd->sy > 2) {
1.23 nicm 238: screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
239: if (pd->job != NULL)
240: job_resize(pd->job, pd->sx - 2, pd->sy - 2);
241: }
242: }
243:
244: static void
1.1 nicm 245: popup_handle_drag(struct client *c, struct popup_data *pd,
246: struct mouse_event *m)
247: {
248: u_int px, py;
249:
250: if (!MOUSE_DRAG(m->b))
251: pd->dragging = OFF;
252: else if (pd->dragging == MOVE) {
253: if (m->x < pd->dx)
254: px = 0;
255: else if (m->x - pd->dx + pd->sx > c->tty.sx)
256: px = c->tty.sx - pd->sx;
257: else
258: px = m->x - pd->dx;
259: if (m->y < pd->dy)
260: py = 0;
261: else if (m->y - pd->dy + pd->sy > c->tty.sy)
262: py = c->tty.sy - pd->sy;
263: else
264: py = m->y - pd->dy;
265: pd->px = px;
266: pd->py = py;
267: pd->dx = m->x - pd->px;
268: pd->dy = m->y - pd->py;
1.23 nicm 269: pd->ppx = px;
270: pd->ppy = py;
1.1 nicm 271: server_redraw_client(c);
272: } else if (pd->dragging == SIZE) {
1.26 ! nicm 273: if (pd->flags & POPUP_NOBORDER) {
! 274: if (m->x < pd->px + 1)
! 275: return;
! 276: if (m->y < pd->py + 1)
! 277: return;
! 278: } else {
! 279: if (m->x < pd->px + 3)
! 280: return;
! 281: if (m->y < pd->py + 3)
! 282: return;
! 283: }
1.1 nicm 284: pd->sx = m->x - pd->px;
285: pd->sy = m->y - pd->py;
1.23 nicm 286: pd->psx = pd->sx;
287: pd->psy = pd->sy;
1.1 nicm 288:
1.26 ! nicm 289: if (pd->flags & POPUP_NOBORDER) {
! 290: screen_resize(&pd->s, pd->sx, pd->sy, 0);
! 291: if (pd->job != NULL)
! 292: job_resize(pd->job, pd->sx, pd->sy);
! 293: } else {
! 294: screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
! 295: if (pd->job != NULL)
! 296: job_resize(pd->job, pd->sx - 2, pd->sy - 2);
! 297: }
1.1 nicm 298: server_redraw_client(c);
299: }
300: }
301:
302: static int
303: popup_key_cb(struct client *c, struct key_event *event)
304: {
305: struct popup_data *pd = c->overlay_data;
306: struct mouse_event *m = &event->m;
1.22 nicm 307: const char *buf;
1.6 nicm 308: size_t len;
1.26 ! nicm 309: u_int px, py;
1.1 nicm 310:
311: if (KEYC_IS_MOUSE(event->key)) {
312: if (pd->dragging != OFF) {
313: popup_handle_drag(c, pd, m);
314: goto out;
315: }
316: if (m->x < pd->px ||
317: m->x > pd->px + pd->sx - 1 ||
318: m->y < pd->py ||
319: m->y > pd->py + pd->sy - 1) {
320: if (MOUSE_BUTTONS (m->b) == 1)
321: return (1);
322: return (0);
323: }
324: if ((m->b & MOUSE_MASK_META) ||
1.26 ! nicm 325: ((~pd->flags & POPUP_NOBORDER) &&
! 326: (m->x == pd->px ||
1.1 nicm 327: m->x == pd->px + pd->sx - 1 ||
328: m->y == pd->py ||
1.26 ! nicm 329: m->y == pd->py + pd->sy - 1))) {
1.1 nicm 330: if (!MOUSE_DRAG(m->b))
331: goto out;
332: if (MOUSE_BUTTONS(m->lb) == 0)
333: pd->dragging = MOVE;
334: else if (MOUSE_BUTTONS(m->lb) == 2)
335: pd->dragging = SIZE;
336: pd->dx = m->lx - pd->px;
337: pd->dy = m->ly - pd->py;
338: goto out;
339: }
340: }
341:
1.22 nicm 342: if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) ||
343: pd->job == NULL) &&
344: (event->key == '\033' || event->key == '\003'))
345: return (1);
346: if (pd->job != NULL) {
1.6 nicm 347: if (KEYC_IS_MOUSE(event->key)) {
348: /* Must be inside, checked already. */
1.26 ! nicm 349: if (pd->flags & POPUP_NOBORDER) {
! 350: px = m->x - pd->px;
! 351: py = m->y - pd->py;
! 352: } else {
! 353: px = m->x - pd->px - 1;
! 354: py = m->y - pd->py - 1;
! 355: }
! 356: if (!input_key_get_mouse(&pd->s, m, px, py, &buf, &len))
1.6 nicm 357: return (0);
358: bufferevent_write(job_get_event(pd->job), buf, len);
359: return (0);
360: }
1.18 nicm 361: input_key(&pd->s, job_get_event(pd->job), event->key);
1.1 nicm 362: }
1.22 nicm 363: return (0);
1.1 nicm 364:
365: out:
366: pd->lx = m->x;
367: pd->ly = m->y;
368: pd->lb = m->b;
369: return (0);
370: }
371:
372: static void
373: popup_job_update_cb(struct job *job)
374: {
375: struct popup_data *pd = job_get_data(job);
376: struct evbuffer *evb = job_get_event(job)->input;
1.15 nicm 377: struct client *c = pd->c;
1.1 nicm 378: struct screen *s = &pd->s;
379: void *data = EVBUFFER_DATA(evb);
380: size_t size = EVBUFFER_LENGTH(evb);
381:
1.15 nicm 382: if (size == 0)
383: return;
384:
385: c->overlay_check = NULL;
386: input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size);
387: c->overlay_check = popup_check_cb;
388:
389: evbuffer_drain(evb, size);
1.1 nicm 390: }
391:
392: static void
393: popup_job_complete_cb(struct job *job)
394: {
395: struct popup_data *pd = job_get_data(job);
396: int status;
397:
398: status = job_get_status(pd->job);
399: if (WIFEXITED(status))
400: pd->status = WEXITSTATUS(status);
401: else if (WIFSIGNALED(status))
402: pd->status = WTERMSIG(status);
403: else
404: pd->status = 0;
405: pd->job = NULL;
406:
1.4 nicm 407: if ((pd->flags & POPUP_CLOSEEXIT) ||
408: ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
1.1 nicm 409: server_client_clear_overlay(pd->c);
410: }
411:
412: int
413: popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
1.22 nicm 414: u_int sy, const char *shellcmd, int argc, char **argv, const char *cwd,
415: struct client *c, struct session *s, popup_close_cb cb, void *arg)
1.1 nicm 416: {
417: struct popup_data *pd;
1.26 ! nicm 418: u_int jx, jy;
1.1 nicm 419:
1.26 ! nicm 420: if (flags & POPUP_NOBORDER) {
! 421: if (sx < 1 || sy < 1)
! 422: return (-1);
! 423: jx = sx;
! 424: jy = sy;
! 425: } else {
! 426: if (sx < 3 || sy < 3)
! 427: return (-1);
! 428: jx = sx - 2;
! 429: jy = sy - 2;
! 430: }
1.1 nicm 431: if (c->tty.sx < sx || c->tty.sy < sy)
432: return (-1);
433:
434: pd = xcalloc(1, sizeof *pd);
435: pd->item = item;
436: pd->flags = flags;
437:
438: pd->c = c;
439: pd->c->references++;
440:
1.14 nicm 441: pd->cb = cb;
442: pd->arg = arg;
1.1 nicm 443: pd->status = 128 + SIGHUP;
444:
445: screen_init(&pd->s, sx - 2, sy - 2, 0);
1.25 nicm 446: colour_palette_init(&pd->palette);
447: colour_palette_from_option(&pd->palette, global_w_options);
1.1 nicm 448:
449: pd->px = px;
450: pd->py = py;
451: pd->sx = sx;
452: pd->sy = sy;
453:
1.23 nicm 454: pd->ppx = px;
455: pd->ppy = py;
456: pd->psx = sx;
457: pd->psy = sy;
458:
1.22 nicm 459: pd->job = job_run(shellcmd, argc, argv, s, cwd,
460: popup_job_update_cb, popup_job_complete_cb, NULL, pd,
1.26 ! nicm 461: JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE, jx, jy);
1.25 nicm 462: pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette);
1.1 nicm 463:
464: server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
1.23 nicm 465: popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd);
1.17 nicm 466: return (0);
467: }
468:
469: static void
470: popup_editor_free(struct popup_editor *pe)
471: {
472: unlink(pe->path);
473: free(pe->path);
474: free(pe);
475: }
476:
477: static void
478: popup_editor_close_cb(int status, void *arg)
479: {
480: struct popup_editor *pe = arg;
481: FILE *f;
482: char *buf = NULL;
483: off_t len = 0;
484:
485: if (status != 0) {
486: pe->cb(NULL, 0, pe->arg);
487: popup_editor_free(pe);
488: return;
489: }
490:
491: f = fopen(pe->path, "r");
492: if (f != NULL) {
493: fseeko(f, 0, SEEK_END);
494: len = ftello(f);
495: fseeko(f, 0, SEEK_SET);
496:
497: if (len == 0 ||
498: (uintmax_t)len > (uintmax_t)SIZE_MAX ||
499: (buf = malloc(len)) == NULL ||
500: fread(buf, len, 1, f) != 1) {
501: free(buf);
502: buf = NULL;
503: len = 0;
504: }
505: fclose(f);
506: }
507: pe->cb(buf, len, pe->arg); /* callback now owns buffer */
508: popup_editor_free(pe);
509: }
510:
511: int
512: popup_editor(struct client *c, const char *buf, size_t len,
513: popup_finish_edit_cb cb, void *arg)
514: {
515: struct popup_editor *pe;
516: int fd;
517: FILE *f;
518: char *cmd;
519: char path[] = _PATH_TMP "tmux.XXXXXXXX";
520: const char *editor;
521: u_int px, py, sx, sy;
522:
523: editor = options_get_string(global_options, "editor");
524: if (*editor == '\0')
525: return (-1);
526:
527: fd = mkstemp(path);
528: if (fd == -1)
529: return (-1);
530: f = fdopen(fd, "w");
531: if (fwrite(buf, len, 1, f) != 1) {
532: fclose(f);
533: return (-1);
534: }
535: fclose(f);
536:
537: pe = xcalloc(1, sizeof *pe);
538: pe->path = xstrdup(path);
539: pe->cb = cb;
540: pe->arg = arg;
541:
542: sx = c->tty.sx * 9 / 10;
543: sy = c->tty.sy * 9 / 10;
544: px = (c->tty.sx / 2) - (sx / 2);
545: py = (c->tty.sy / 2) - (sy / 2);
546:
547: xasprintf(&cmd, "%s %s", editor, path);
1.22 nicm 548: if (popup_display(POPUP_CLOSEEXIT, NULL, px, py, sx, sy, cmd, 0, NULL,
549: _PATH_TMP, c, NULL, popup_editor_close_cb, pe) != 0) {
1.17 nicm 550: popup_editor_free(pe);
551: free(cmd);
552: return (-1);
553: }
554: free(cmd);
1.1 nicm 555: return (0);
556: }