Annotation of src/usr.bin/tmux/popup.c, Revision 1.14
1.14 ! nicm 1: /* $OpenBSD: popup.c,v 1.13 2020/04/13 18:59:41 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:
22: #include <signal.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "tmux.h"
27:
28: struct popup_data {
29: struct client *c;
30: struct cmdq_item *item;
31: int flags;
32:
33: char **lines;
34: u_int nlines;
35:
36: char *cmd;
37: struct cmd_find_state fs;
38: struct screen s;
39:
40: struct job *job;
41: struct input_ctx *ictx;
42: int status;
1.14 ! nicm 43: popup_close_cb cb;
! 44: void *arg;
1.1 nicm 45:
46: u_int px;
47: u_int py;
48: u_int sx;
49: u_int sy;
50:
51: enum { OFF, MOVE, SIZE } dragging;
52: u_int dx;
53: u_int dy;
54:
55: u_int lx;
56: u_int ly;
57: u_int lb;
58: };
59:
60: static void
61: popup_write_screen(struct client *c, struct popup_data *pd)
62: {
63: struct cmdq_item *item = pd->item;
64: struct screen_write_ctx ctx;
65: char *copy, *next, *loop, *tmp;
66: struct format_tree *ft;
67: u_int i, y;
68:
1.8 nicm 69: ft = format_create(c, item, FORMAT_NONE, 0);
1.1 nicm 70: if (cmd_find_valid_state(&pd->fs))
71: format_defaults(ft, c, pd->fs.s, pd->fs.wl, pd->fs.wp);
72: else
73: format_defaults(ft, c, NULL, NULL, NULL);
74:
75: screen_write_start(&ctx, NULL, &pd->s);
76: screen_write_clearscreen(&ctx, 8);
77:
78: y = 0;
79: for (i = 0; i < pd->nlines; i++) {
80: if (y == pd->sy - 2)
81: break;
82: copy = next = xstrdup(pd->lines[i]);
83: while ((loop = strsep(&next, "\n")) != NULL) {
84: if (y == pd->sy - 2)
85: break;
86: tmp = format_expand(ft, loop);
87: screen_write_cursormove(&ctx, 0, y, 0);
88: format_draw(&ctx, &grid_default_cell, pd->sx - 2, tmp,
89: NULL);
90: free(tmp);
91: y++;
92: }
93: free(copy);
94: }
95:
96: format_free(ft);
97: screen_write_cursormove(&ctx, 0, y, 0);
98: screen_write_stop(&ctx);
99: }
100:
101: static int
102: popup_mode_cb(struct client *c, u_int *cx, u_int *cy)
103: {
104: struct popup_data *pd = c->overlay_data;
105:
106: if (pd->ictx == NULL)
107: return (0);
108: *cx = pd->px + 1 + pd->s.cx;
109: *cy = pd->py + 1 + pd->s.cy;
110: return (pd->s.mode);
111: }
112:
113: static int
114: popup_check_cb(struct client *c, u_int px, u_int py)
115: {
116: struct popup_data *pd = c->overlay_data;
117:
118: if (px < pd->px || px > pd->px + pd->sx - 1)
119: return (1);
120: if (py < pd->py || py > pd->py + pd->sy - 1)
121: return (1);
122: return (0);
123: }
124:
125: static void
126: popup_draw_cb(struct client *c, __unused struct screen_redraw_ctx *ctx0)
127: {
128: struct popup_data *pd = c->overlay_data;
129: struct tty *tty = &c->tty;
130: struct screen s;
131: struct screen_write_ctx ctx;
132: u_int i, px = pd->px, py = pd->py;
133:
134: screen_init(&s, pd->sx, pd->sy, 0);
135: screen_write_start(&ctx, NULL, &s);
136: screen_write_clearscreen(&ctx, 8);
137: screen_write_box(&ctx, pd->sx, pd->sy);
138: screen_write_cursormove(&ctx, 1, 1, 0);
139: screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2, pd->sy - 2);
140: screen_write_stop(&ctx);
141:
142: c->overlay_check = NULL;
143: for (i = 0; i < pd->sy; i++)
144: tty_draw_line(tty, NULL, &s, 0, i, pd->sx, px, py + i);
145: c->overlay_check = popup_check_cb;
146: }
147:
148: static void
149: popup_free_cb(struct client *c)
150: {
151: struct popup_data *pd = c->overlay_data;
152: struct cmdq_item *item = pd->item;
153: u_int i;
154:
1.14 ! nicm 155: if (pd->cb != NULL)
! 156: pd->cb(pd->status, pd->arg);
! 157:
1.1 nicm 158: if (item != NULL) {
159: if (pd->ictx != NULL &&
1.8 nicm 160: cmdq_get_client(item) != NULL &&
161: cmdq_get_client(item)->session == NULL)
162: cmdq_get_client(item)->retval = pd->status;
1.1 nicm 163: cmdq_continue(item);
164: }
165: server_client_unref(pd->c);
166:
167: if (pd->job != NULL)
168: job_free(pd->job);
169: if (pd->ictx != NULL)
170: input_free(pd->ictx);
171:
172: for (i = 0; i < pd->nlines; i++)
173: free(pd->lines[i]);
174: free(pd->lines);
175:
176: screen_free(&pd->s);
177: free(pd->cmd);
178: free(pd);
179: }
180:
181: static void
182: popup_handle_drag(struct client *c, struct popup_data *pd,
183: struct mouse_event *m)
184: {
185: u_int px, py;
186:
187: if (!MOUSE_DRAG(m->b))
188: pd->dragging = OFF;
189: else if (pd->dragging == MOVE) {
190: if (m->x < pd->dx)
191: px = 0;
192: else if (m->x - pd->dx + pd->sx > c->tty.sx)
193: px = c->tty.sx - pd->sx;
194: else
195: px = m->x - pd->dx;
196: if (m->y < pd->dy)
197: py = 0;
198: else if (m->y - pd->dy + pd->sy > c->tty.sy)
199: py = c->tty.sy - pd->sy;
200: else
201: py = m->y - pd->dy;
202: pd->px = px;
203: pd->py = py;
204: pd->dx = m->x - pd->px;
205: pd->dy = m->y - pd->py;
206: server_redraw_client(c);
207: } else if (pd->dragging == SIZE) {
1.7 nicm 208: if (m->x < pd->px + 3)
1.1 nicm 209: return;
1.7 nicm 210: if (m->y < pd->py + 3)
1.1 nicm 211: return;
212: pd->sx = m->x - pd->px;
213: pd->sy = m->y - pd->py;
214:
215: screen_resize(&pd->s, pd->sx, pd->sy, 0);
216: if (pd->ictx == NULL)
217: popup_write_screen(c, pd);
218: else if (pd->job != NULL)
219: job_resize(pd->job, pd->sx - 2, pd->sy - 2);
220: server_redraw_client(c);
221: }
222: }
223:
224: static int
225: popup_key_cb(struct client *c, struct key_event *event)
226: {
227: struct popup_data *pd = c->overlay_data;
228: struct mouse_event *m = &event->m;
229: struct cmd_find_state *fs = &pd->fs;
230: struct format_tree *ft;
1.6 nicm 231: const char *cmd, *buf;
232: size_t len;
1.13 nicm 233: struct cmdq_state *state;
234: enum cmd_parse_status status;
235: char *error;
1.1 nicm 236:
237: if (KEYC_IS_MOUSE(event->key)) {
238: if (pd->dragging != OFF) {
239: popup_handle_drag(c, pd, m);
240: goto out;
241: }
242: if (m->x < pd->px ||
243: m->x > pd->px + pd->sx - 1 ||
244: m->y < pd->py ||
245: m->y > pd->py + pd->sy - 1) {
246: if (MOUSE_BUTTONS (m->b) == 1)
247: return (1);
248: return (0);
249: }
250: if ((m->b & MOUSE_MASK_META) ||
251: m->x == pd->px ||
252: m->x == pd->px + pd->sx - 1 ||
253: m->y == pd->py ||
254: m->y == pd->py + pd->sy - 1) {
255: if (!MOUSE_DRAG(m->b))
256: goto out;
257: if (MOUSE_BUTTONS(m->lb) == 0)
258: pd->dragging = MOVE;
259: else if (MOUSE_BUTTONS(m->lb) == 2)
260: pd->dragging = SIZE;
261: pd->dx = m->lx - pd->px;
262: pd->dy = m->ly - pd->py;
263: goto out;
264: }
265: }
266:
267: if (pd->ictx != NULL && (pd->flags & POPUP_WRITEKEYS)) {
1.3 nicm 268: if (((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0 ||
269: pd->job == NULL) &&
1.1 nicm 270: (event->key == '\033' || event->key == '\003'))
271: return (1);
272: if (pd->job == NULL)
273: return (0);
1.6 nicm 274: if (KEYC_IS_MOUSE(event->key)) {
275: /* Must be inside, checked already. */
276: if (!input_key_get_mouse(&pd->s, m, m->x - pd->px,
277: m->y - pd->py, &buf, &len))
278: return (0);
279: bufferevent_write(job_get_event(pd->job), buf, len);
280: return (0);
281: }
1.1 nicm 282: input_key(NULL, &pd->s, job_get_event(pd->job), event->key);
283: return (0);
284: }
285:
286: if (pd->cmd == NULL)
287: return (1);
288:
289: ft = format_create(NULL, pd->item, FORMAT_NONE, 0);
290: if (cmd_find_valid_state(fs))
291: format_defaults(ft, c, fs->s, fs->wl, fs->wp);
292: else
293: format_defaults(ft, c, NULL, NULL, NULL);
294: format_add(ft, "popup_key", "%s", key_string_lookup_key(event->key));
295: if (KEYC_IS_MOUSE(event->key)) {
296: format_add(ft, "popup_mouse", "1");
297: format_add(ft, "popup_mouse_x", "%u", m->x - pd->px);
298: format_add(ft, "popup_mouse_y", "%u", m->y - pd->py);
299: }
300: cmd = format_expand(ft, pd->cmd);
301: format_free(ft);
302:
1.13 nicm 303: if (pd->item != NULL)
304: event = cmdq_get_event(pd->item);
305: else
306: event = NULL;
307: state = cmdq_new_state(&pd->fs, event, 0);
308:
309: status = cmd_parse_and_append(cmd, NULL, c, state, &error);
310: if (status == CMD_PARSE_ERROR) {
311: cmdq_append(c, cmdq_get_error(error));
312: free(error);
1.1 nicm 313: }
1.13 nicm 314: cmdq_free_state(state);
315:
1.1 nicm 316: return (1);
317:
318: out:
319: pd->lx = m->x;
320: pd->ly = m->y;
321: pd->lb = m->b;
322: return (0);
323: }
324:
325: static void
326: popup_job_update_cb(struct job *job)
327: {
328: struct popup_data *pd = job_get_data(job);
329: struct evbuffer *evb = job_get_event(job)->input;
330: struct screen *s = &pd->s;
331: void *data = EVBUFFER_DATA(evb);
332: size_t size = EVBUFFER_LENGTH(evb);
333:
334: if (size != 0) {
335: input_parse_screen(pd->ictx, s, data, size);
336: evbuffer_drain(evb, size);
337: pd->c->flags |= CLIENT_REDRAWOVERLAY;
338: }
339: }
340:
341: static void
342: popup_job_complete_cb(struct job *job)
343: {
344: struct popup_data *pd = job_get_data(job);
345: int status;
346:
347: status = job_get_status(pd->job);
348: if (WIFEXITED(status))
349: pd->status = WEXITSTATUS(status);
350: else if (WIFSIGNALED(status))
351: pd->status = WTERMSIG(status);
352: else
353: pd->status = 0;
354: pd->job = NULL;
355:
1.4 nicm 356: if ((pd->flags & POPUP_CLOSEEXIT) ||
357: ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
1.1 nicm 358: server_client_clear_overlay(pd->c);
359: }
360:
361: u_int
1.2 nicm 362: popup_height(u_int nlines, const char **lines)
363: {
364: char *copy, *next, *loop;
365: u_int i, height = 0;
366:
367: for (i = 0; i < nlines; i++) {
368: copy = next = xstrdup(lines[i]);
369: while ((loop = strsep(&next, "\n")) != NULL)
370: height++;
371: free(copy);
372: }
373:
374: return (height);
375: }
376:
377: u_int
1.1 nicm 378: popup_width(struct cmdq_item *item, u_int nlines, const char **lines,
379: struct client *c, struct cmd_find_state *fs)
380: {
381: char *copy, *next, *loop, *tmp;
382: struct format_tree *ft;
383: u_int i, width = 0, tmpwidth;
384:
1.8 nicm 385: ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
1.1 nicm 386: if (fs != NULL && cmd_find_valid_state(fs))
387: format_defaults(ft, c, fs->s, fs->wl, fs->wp);
388: else
389: format_defaults(ft, c, NULL, NULL, NULL);
390:
391: for (i = 0; i < nlines; i++) {
392: copy = next = xstrdup(lines[i]);
393: while ((loop = strsep(&next, "\n")) != NULL) {
394: tmp = format_expand(ft, loop);
395: tmpwidth = format_width(tmp);
396: if (tmpwidth > width)
397: width = tmpwidth;
398: free(tmp);
399: }
1.2 nicm 400: free(copy);
1.1 nicm 401: }
402:
403: format_free(ft);
404: return (width);
405: }
406:
407: int
408: popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
409: u_int sy, u_int nlines, const char **lines, const char *shellcmd,
410: const char *cmd, const char *cwd, struct client *c,
1.14 ! nicm 411: struct cmd_find_state *fs, popup_close_cb cb, void *arg)
1.1 nicm 412: {
413: struct popup_data *pd;
414: u_int i;
415: struct session *s;
416: int jobflags;
417:
418: if (sx < 3 || sy < 3)
419: return (-1);
420: if (c->tty.sx < sx || c->tty.sy < sy)
421: return (-1);
422:
423: pd = xcalloc(1, sizeof *pd);
424: pd->item = item;
425: pd->flags = flags;
426:
427: pd->c = c;
428: pd->c->references++;
429:
1.14 ! nicm 430: pd->cb = cb;
! 431: pd->arg = arg;
1.1 nicm 432: pd->status = 128 + SIGHUP;
433:
434: if (fs != NULL)
435: cmd_find_copy_state(&pd->fs, fs);
436: screen_init(&pd->s, sx - 2, sy - 2, 0);
437:
438: if (cmd != NULL)
439: pd->cmd = xstrdup(cmd);
440:
441: pd->px = px;
442: pd->py = py;
443: pd->sx = sx;
444: pd->sy = sy;
445:
446: pd->nlines = nlines;
447: if (pd->nlines != 0)
448: pd->lines = xreallocarray(NULL, pd->nlines, sizeof *pd->lines);
449:
450: for (i = 0; i < pd->nlines; i++)
451: pd->lines[i] = xstrdup(lines[i]);
452: popup_write_screen(c, pd);
453:
454: if (shellcmd != NULL) {
455: if (fs != NULL)
456: s = fs->s;
457: else
458: s = NULL;
459: jobflags = JOB_NOWAIT|JOB_PTY;
460: if (flags & POPUP_WRITEKEYS)
461: jobflags |= JOB_KEEPWRITE;
462: pd->job = job_run(shellcmd, s, cwd, popup_job_update_cb,
463: popup_job_complete_cb, NULL, pd, jobflags, pd->sx - 2,
464: pd->sy - 2);
1.5 nicm 465: pd->ictx = input_init(NULL, job_get_event(pd->job));
1.1 nicm 466: }
467:
468: server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
469: popup_draw_cb, popup_key_cb, popup_free_cb, pd);
470: return (0);
471: }