Annotation of src/usr.bin/tmux/popup.c, Revision 1.5
1.5 ! nicm 1: /* $OpenBSD: popup.c,v 1.4 2020/03/30 07:42:44 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:
67: ft = format_create(item->client, item, FORMAT_NONE, 0);
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 &&
155: item->client != NULL &&
156: item->client->session == NULL)
157: item->client->retval = pd->status;
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) {
203: if (m->x < pd->px + 2)
204: return;
205: if (m->y < pd->py + 2)
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;
226: struct cmd_parse_result *pr;
227: struct format_tree *ft;
228: const char *cmd;
229:
230: if (KEYC_IS_MOUSE(event->key)) {
231: if (pd->dragging != OFF) {
232: popup_handle_drag(c, pd, m);
233: goto out;
234: }
235: if (m->x < pd->px ||
236: m->x > pd->px + pd->sx - 1 ||
237: m->y < pd->py ||
238: m->y > pd->py + pd->sy - 1) {
239: if (MOUSE_BUTTONS (m->b) == 1)
240: return (1);
241: return (0);
242: }
243: if ((m->b & MOUSE_MASK_META) ||
244: m->x == pd->px ||
245: m->x == pd->px + pd->sx - 1 ||
246: m->y == pd->py ||
247: m->y == pd->py + pd->sy - 1) {
248: if (!MOUSE_DRAG(m->b))
249: goto out;
250: if (MOUSE_BUTTONS(m->lb) == 0)
251: pd->dragging = MOVE;
252: else if (MOUSE_BUTTONS(m->lb) == 2)
253: pd->dragging = SIZE;
254: pd->dx = m->lx - pd->px;
255: pd->dy = m->ly - pd->py;
256: goto out;
257: }
258: }
259:
260: if (pd->ictx != NULL && (pd->flags & POPUP_WRITEKEYS)) {
261: if (KEYC_IS_MOUSE(event->key))
262: return (0);
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);
269: input_key(NULL, &pd->s, job_get_event(pd->job), event->key);
270: return (0);
271: }
272:
273: if (pd->cmd == NULL)
274: return (1);
275:
276: ft = format_create(NULL, pd->item, FORMAT_NONE, 0);
277: if (cmd_find_valid_state(fs))
278: format_defaults(ft, c, fs->s, fs->wl, fs->wp);
279: else
280: format_defaults(ft, c, NULL, NULL, NULL);
281: format_add(ft, "popup_key", "%s", key_string_lookup_key(event->key));
282: if (KEYC_IS_MOUSE(event->key)) {
283: format_add(ft, "popup_mouse", "1");
284: format_add(ft, "popup_mouse_x", "%u", m->x - pd->px);
285: format_add(ft, "popup_mouse_y", "%u", m->y - pd->py);
286: }
287: cmd = format_expand(ft, pd->cmd);
288: format_free(ft);
289:
290: pr = cmd_parse_from_string(cmd, NULL);
291: switch (pr->status) {
292: case CMD_PARSE_EMPTY:
293: break;
294: case CMD_PARSE_ERROR:
295: new_item = cmdq_get_error(pr->error);
296: free(pr->error);
297: cmdq_append(c, new_item);
298: break;
299: case CMD_PARSE_SUCCESS:
300: if (pd->item != NULL)
301: m = &pd->item->shared->mouse;
302: else
303: m = NULL;
304: new_item = cmdq_get_command(pr->cmdlist, fs, m, 0);
305: cmd_list_free(pr->cmdlist);
306: cmdq_append(c, new_item);
307: break;
308: }
309: return (1);
310:
311: out:
312: pd->lx = m->x;
313: pd->ly = m->y;
314: pd->lb = m->b;
315: return (0);
316: }
317:
318: static void
319: popup_job_update_cb(struct job *job)
320: {
321: struct popup_data *pd = job_get_data(job);
322: struct evbuffer *evb = job_get_event(job)->input;
323: struct screen *s = &pd->s;
324: void *data = EVBUFFER_DATA(evb);
325: size_t size = EVBUFFER_LENGTH(evb);
326:
327: if (size != 0) {
328: input_parse_screen(pd->ictx, s, data, size);
329: evbuffer_drain(evb, size);
330: pd->c->flags |= CLIENT_REDRAWOVERLAY;
331: }
332: }
333:
334: static void
335: popup_job_complete_cb(struct job *job)
336: {
337: struct popup_data *pd = job_get_data(job);
338: int status;
339:
340: status = job_get_status(pd->job);
341: if (WIFEXITED(status))
342: pd->status = WEXITSTATUS(status);
343: else if (WIFSIGNALED(status))
344: pd->status = WTERMSIG(status);
345: else
346: pd->status = 0;
347: pd->job = NULL;
348:
1.4 nicm 349: if ((pd->flags & POPUP_CLOSEEXIT) ||
350: ((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
1.1 nicm 351: server_client_clear_overlay(pd->c);
352: }
353:
354: u_int
1.2 nicm 355: popup_height(u_int nlines, const char **lines)
356: {
357: char *copy, *next, *loop;
358: u_int i, height = 0;
359:
360: for (i = 0; i < nlines; i++) {
361: copy = next = xstrdup(lines[i]);
362: while ((loop = strsep(&next, "\n")) != NULL)
363: height++;
364: free(copy);
365: }
366:
367: return (height);
368: }
369:
370: u_int
1.1 nicm 371: popup_width(struct cmdq_item *item, u_int nlines, const char **lines,
372: struct client *c, struct cmd_find_state *fs)
373: {
374: char *copy, *next, *loop, *tmp;
375: struct format_tree *ft;
376: u_int i, width = 0, tmpwidth;
377:
378: ft = format_create(item->client, item, FORMAT_NONE, 0);
379: if (fs != NULL && cmd_find_valid_state(fs))
380: format_defaults(ft, c, fs->s, fs->wl, fs->wp);
381: else
382: format_defaults(ft, c, NULL, NULL, NULL);
383:
384: for (i = 0; i < nlines; i++) {
385: copy = next = xstrdup(lines[i]);
386: while ((loop = strsep(&next, "\n")) != NULL) {
387: tmp = format_expand(ft, loop);
388: tmpwidth = format_width(tmp);
389: if (tmpwidth > width)
390: width = tmpwidth;
391: free(tmp);
392: }
1.2 nicm 393: free(copy);
1.1 nicm 394: }
395:
396: format_free(ft);
397: return (width);
398: }
399:
400: int
401: popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
402: u_int sy, u_int nlines, const char **lines, const char *shellcmd,
403: const char *cmd, const char *cwd, struct client *c,
404: struct cmd_find_state *fs)
405: {
406: struct popup_data *pd;
407: u_int i;
408: struct session *s;
409: int jobflags;
410:
411: if (sx < 3 || sy < 3)
412: return (-1);
413: if (c->tty.sx < sx || c->tty.sy < sy)
414: return (-1);
415:
416: pd = xcalloc(1, sizeof *pd);
417: pd->item = item;
418: pd->flags = flags;
419:
420: pd->c = c;
421: pd->c->references++;
422:
423: pd->status = 128 + SIGHUP;
424:
425: if (fs != NULL)
426: cmd_find_copy_state(&pd->fs, fs);
427: screen_init(&pd->s, sx - 2, sy - 2, 0);
428:
429: if (cmd != NULL)
430: pd->cmd = xstrdup(cmd);
431:
432: pd->px = px;
433: pd->py = py;
434: pd->sx = sx;
435: pd->sy = sy;
436:
437: pd->nlines = nlines;
438: if (pd->nlines != 0)
439: pd->lines = xreallocarray(NULL, pd->nlines, sizeof *pd->lines);
440:
441: for (i = 0; i < pd->nlines; i++)
442: pd->lines[i] = xstrdup(lines[i]);
443: popup_write_screen(c, pd);
444:
445: if (shellcmd != NULL) {
446: if (fs != NULL)
447: s = fs->s;
448: else
449: s = NULL;
450: jobflags = JOB_NOWAIT|JOB_PTY;
451: if (flags & POPUP_WRITEKEYS)
452: jobflags |= JOB_KEEPWRITE;
453: pd->job = job_run(shellcmd, s, cwd, popup_job_update_cb,
454: popup_job_complete_cb, NULL, pd, jobflags, pd->sx - 2,
455: pd->sy - 2);
1.5 ! nicm 456: pd->ictx = input_init(NULL, job_get_event(pd->job));
1.1 nicm 457: }
458:
459: server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
460: popup_draw_cb, popup_key_cb, popup_free_cb, pd);
461: return (0);
462: }