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