2020-03-24 08:09:43 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Nicholas Marriott <nicholas.marriott@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2020-05-16 16:13:09 +00:00
|
|
|
#include <paths.h>
|
2020-03-24 08:09:43 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-05-16 16:13:09 +00:00
|
|
|
#include <unistd.h>
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
struct popup_data {
|
|
|
|
struct client *c;
|
|
|
|
struct cmdq_item *item;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
struct screen s;
|
2021-08-11 20:49:55 +00:00
|
|
|
struct colour_palette palette;
|
2020-03-24 08:09:43 +00:00
|
|
|
struct job *job;
|
|
|
|
struct input_ctx *ictx;
|
|
|
|
int status;
|
2020-05-16 15:24:28 +00:00
|
|
|
popup_close_cb cb;
|
|
|
|
void *arg;
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
struct menu *menu;
|
|
|
|
struct menu_data *md;
|
|
|
|
int close;
|
|
|
|
|
2021-07-21 08:06:36 +00:00
|
|
|
/* Current position and size. */
|
2020-03-24 08:09:43 +00:00
|
|
|
u_int px;
|
|
|
|
u_int py;
|
|
|
|
u_int sx;
|
|
|
|
u_int sy;
|
|
|
|
|
2021-07-21 08:06:36 +00:00
|
|
|
/* Preferred position and size. */
|
|
|
|
u_int ppx;
|
|
|
|
u_int ppy;
|
|
|
|
u_int psx;
|
|
|
|
u_int psy;
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
enum { OFF, MOVE, SIZE } dragging;
|
|
|
|
u_int dx;
|
|
|
|
u_int dy;
|
|
|
|
|
|
|
|
u_int lx;
|
|
|
|
u_int ly;
|
|
|
|
u_int lb;
|
|
|
|
};
|
|
|
|
|
2020-05-16 16:13:09 +00:00
|
|
|
struct popup_editor {
|
|
|
|
char *path;
|
|
|
|
popup_finish_edit_cb cb;
|
|
|
|
void *arg;
|
|
|
|
};
|
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
static const struct menu_item popup_menu_items[] = {
|
|
|
|
{ "Close", 'q', NULL },
|
|
|
|
{ "#{?buffer_name,Paste #[underscore]#{buffer_name},}", 'p', NULL },
|
|
|
|
{ "", KEYC_NONE, NULL },
|
|
|
|
{ "Fill Space", 'F', NULL },
|
|
|
|
{ "Centre", 'C', NULL },
|
2021-08-13 19:27:25 +00:00
|
|
|
{ "", KEYC_NONE, NULL },
|
2021-08-13 19:55:11 +00:00
|
|
|
{ "To Horizontal Pane", 'h', NULL },
|
|
|
|
{ "To Vertical Pane", 'v', NULL },
|
2021-08-13 19:27:25 +00:00
|
|
|
|
|
|
|
{ NULL, KEYC_NONE, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct menu_item popup_internal_menu_items[] = {
|
|
|
|
{ "Close", 'q', NULL },
|
|
|
|
{ "", KEYC_NONE, NULL },
|
|
|
|
{ "Fill Space", 'F', NULL },
|
|
|
|
{ "Centre", 'C', NULL },
|
2021-08-13 18:54:54 +00:00
|
|
|
|
|
|
|
{ NULL, KEYC_NONE, NULL }
|
|
|
|
};
|
|
|
|
|
2020-05-16 15:34:08 +00:00
|
|
|
static void
|
|
|
|
popup_redraw_cb(const struct tty_ctx *ttyctx)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = ttyctx->arg;
|
|
|
|
|
|
|
|
pd->c->flags |= CLIENT_REDRAWOVERLAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
popup_set_client_cb(struct tty_ctx *ttyctx, struct client *c)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = ttyctx->arg;
|
|
|
|
|
2020-05-16 15:41:54 +00:00
|
|
|
if (c != pd->c)
|
|
|
|
return (0);
|
2020-05-16 15:34:08 +00:00
|
|
|
if (pd->c->flags & CLIENT_REDRAWOVERLAY)
|
2020-05-16 15:41:54 +00:00
|
|
|
return (0);
|
2020-05-16 15:34:08 +00:00
|
|
|
|
|
|
|
ttyctx->bigger = 0;
|
|
|
|
ttyctx->wox = 0;
|
|
|
|
ttyctx->woy = 0;
|
|
|
|
ttyctx->wsx = c->tty.sx;
|
|
|
|
ttyctx->wsy = c->tty.sy;
|
|
|
|
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
ttyctx->xoff = ttyctx->rxoff = pd->px;
|
|
|
|
ttyctx->yoff = ttyctx->ryoff = pd->py;
|
|
|
|
} else {
|
|
|
|
ttyctx->xoff = ttyctx->rxoff = pd->px + 1;
|
|
|
|
ttyctx->yoff = ttyctx->ryoff = pd->py + 1;
|
|
|
|
}
|
2020-05-16 15:34:08 +00:00
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popup_init_ctx_cb(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = ctx->arg;
|
|
|
|
|
2021-08-11 20:49:55 +00:00
|
|
|
ttyctx->palette = &pd->palette;
|
2020-05-16 15:34:08 +00:00
|
|
|
ttyctx->redraw_cb = popup_redraw_cb;
|
|
|
|
ttyctx->set_client_cb = popup_set_client_cb;
|
|
|
|
ttyctx->arg = pd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct screen *
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_mode_cb(__unused struct client *c, void *data, u_int *cx, u_int *cy)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
|
|
|
|
|
|
|
if (pd->md != NULL)
|
|
|
|
return (menu_mode_cb(c, pd->md, cx, cy));
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
*cx = pd->px + pd->s.cx;
|
|
|
|
*cy = pd->py + pd->s.cy;
|
|
|
|
} else {
|
|
|
|
*cx = pd->px + 1 + pd->s.cx;
|
|
|
|
*cy = pd->py + 1 + pd->s.cy;
|
|
|
|
}
|
2020-05-16 15:34:08 +00:00
|
|
|
return (&pd->s);
|
2020-03-24 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_check_cb(struct client *c, void *data, u_int px, u_int py)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-08-13 20:04:45 +00:00
|
|
|
if (pd->md != NULL && menu_check_cb(c, pd->md, px, py) == 0)
|
|
|
|
return (0);
|
2020-03-24 08:09:43 +00:00
|
|
|
if (px < pd->px || px > pd->px + pd->sx - 1)
|
|
|
|
return (1);
|
|
|
|
if (py < pd->py || py > pd->py + pd->sy - 1)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_draw_cb(struct client *c, void *data, struct screen_redraw_ctx *rctx)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
2020-03-24 08:09:43 +00:00
|
|
|
struct tty *tty = &c->tty;
|
|
|
|
struct screen s;
|
|
|
|
struct screen_write_ctx ctx;
|
|
|
|
u_int i, px = pd->px, py = pd->py;
|
2021-08-11 20:49:55 +00:00
|
|
|
struct colour_palette *palette = &pd->palette;
|
|
|
|
struct grid_cell gc;
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
screen_init(&s, pd->sx, pd->sy, 0);
|
2020-05-16 15:34:08 +00:00
|
|
|
screen_write_start(&ctx, &s);
|
2020-03-24 08:09:43 +00:00
|
|
|
screen_write_clearscreen(&ctx, 8);
|
2021-07-21 08:06:36 +00:00
|
|
|
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
screen_write_cursormove(&ctx, 0, 0, 0);
|
|
|
|
screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx, pd->sy);
|
|
|
|
} else if (pd->sx > 2 && pd->sy > 2) {
|
2021-07-21 08:06:36 +00:00
|
|
|
screen_write_box(&ctx, pd->sx, pd->sy);
|
|
|
|
screen_write_cursormove(&ctx, 1, 1, 0);
|
|
|
|
screen_write_fast_copy(&ctx, &pd->s, 0, 0, pd->sx - 2,
|
|
|
|
pd->sy - 2);
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
screen_write_stop(&ctx);
|
|
|
|
|
2021-08-11 20:49:55 +00:00
|
|
|
memcpy(&gc, &grid_default_cell, sizeof gc);
|
|
|
|
gc.fg = pd->palette.fg;
|
|
|
|
gc.bg = pd->palette.bg;
|
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
if (pd->md != NULL) {
|
|
|
|
c->overlay_check = menu_check_cb;
|
|
|
|
c->overlay_data = pd->md;
|
|
|
|
} else {
|
|
|
|
c->overlay_check = NULL;
|
|
|
|
c->overlay_data = NULL;
|
|
|
|
}
|
2021-08-11 20:49:55 +00:00
|
|
|
for (i = 0; i < pd->sy; i++)
|
|
|
|
tty_draw_line(tty, &s, 0, i, pd->sx, px, py + i, &gc, palette);
|
2021-08-13 18:54:54 +00:00
|
|
|
if (pd->md != NULL) {
|
|
|
|
c->overlay_check = NULL;
|
|
|
|
c->overlay_data = NULL;
|
|
|
|
menu_draw_cb(c, pd->md, rctx);
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
c->overlay_check = popup_check_cb;
|
2021-08-13 18:54:54 +00:00
|
|
|
c->overlay_data = pd;
|
2020-03-24 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_free_cb(struct client *c, void *data)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
2020-03-24 08:09:43 +00:00
|
|
|
struct cmdq_item *item = pd->item;
|
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
if (pd->md != NULL)
|
|
|
|
menu_free_cb(c, pd->md);
|
|
|
|
|
2020-05-16 15:24:28 +00:00
|
|
|
if (pd->cb != NULL)
|
|
|
|
pd->cb(pd->status, pd->arg);
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
if (item != NULL) {
|
2021-03-02 10:56:45 +00:00
|
|
|
if (cmdq_get_client(item) != NULL &&
|
2020-04-13 10:59:58 +00:00
|
|
|
cmdq_get_client(item)->session == NULL)
|
|
|
|
cmdq_get_client(item)->retval = pd->status;
|
2020-03-24 08:09:43 +00:00
|
|
|
cmdq_continue(item);
|
|
|
|
}
|
|
|
|
server_client_unref(pd->c);
|
|
|
|
|
|
|
|
if (pd->job != NULL)
|
|
|
|
job_free(pd->job);
|
2021-03-02 10:56:45 +00:00
|
|
|
input_free(pd->ictx);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
screen_free(&pd->s);
|
2021-08-11 20:49:55 +00:00
|
|
|
colour_palette_free(&pd->palette);
|
2021-08-13 18:54:54 +00:00
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
free(pd);
|
|
|
|
}
|
|
|
|
|
2021-07-21 08:06:36 +00:00
|
|
|
static void
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_resize_cb(__unused struct client *c, void *data)
|
2021-07-21 08:06:36 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
2021-07-21 08:06:36 +00:00
|
|
|
struct tty *tty = &c->tty;
|
|
|
|
|
|
|
|
if (pd == NULL)
|
|
|
|
return;
|
2021-08-13 18:54:54 +00:00
|
|
|
if (pd->md != NULL)
|
|
|
|
menu_free_cb(c, pd->md);
|
2021-07-21 08:06:36 +00:00
|
|
|
|
|
|
|
/* Adjust position and size. */
|
|
|
|
if (pd->psy > tty->sy)
|
|
|
|
pd->sy = tty->sy;
|
|
|
|
else
|
|
|
|
pd->sy = pd->psy;
|
|
|
|
if (pd->psx > tty->sx)
|
|
|
|
pd->sx = tty->sx;
|
|
|
|
else
|
|
|
|
pd->sx = pd->psx;
|
|
|
|
if (pd->ppy + pd->sy > tty->sy)
|
|
|
|
pd->py = tty->sy - pd->sy;
|
|
|
|
else
|
|
|
|
pd->py = pd->ppy;
|
|
|
|
if (pd->ppx + pd->sx > tty->sx)
|
|
|
|
pd->px = tty->sx - pd->sx;
|
|
|
|
else
|
|
|
|
pd->px = pd->ppx;
|
|
|
|
|
|
|
|
/* Avoid zero size screens. */
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
screen_resize(&pd->s, pd->sx, pd->sy, 0);
|
|
|
|
if (pd->job != NULL)
|
|
|
|
job_resize(pd->job, pd->sx, pd->sy );
|
|
|
|
} else if (pd->sx > 2 && pd->sy > 2) {
|
2021-07-21 08:06:36 +00:00
|
|
|
screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
|
|
|
|
if (pd->job != NULL)
|
|
|
|
job_resize(pd->job, pd->sx - 2, pd->sy - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 19:27:25 +00:00
|
|
|
static void
|
|
|
|
popup_make_pane(struct popup_data *pd, enum layout_type type)
|
|
|
|
{
|
|
|
|
struct client *c = pd->c;
|
|
|
|
struct session *s = c->session;
|
|
|
|
struct window *w = s->curw->window;
|
|
|
|
struct layout_cell *lc;
|
|
|
|
struct window_pane *wp = w->active, *new_wp;
|
|
|
|
u_int hlimit;
|
2021-08-13 19:55:11 +00:00
|
|
|
const char *shell;
|
2021-08-13 19:27:25 +00:00
|
|
|
|
|
|
|
window_unzoom(w);
|
|
|
|
|
|
|
|
lc = layout_split_pane(wp, type, -1, 0);
|
|
|
|
hlimit = options_get_number(s->options, "history-limit");
|
|
|
|
new_wp = window_add_pane(wp->window, NULL, hlimit, 0);
|
|
|
|
layout_assign_pane(lc, new_wp, 0);
|
|
|
|
|
2021-08-13 19:55:11 +00:00
|
|
|
new_wp->fd = job_transfer(pd->job, &new_wp->pid, new_wp->tty,
|
|
|
|
sizeof new_wp->tty);
|
2021-08-13 19:27:25 +00:00
|
|
|
pd->job = NULL;
|
|
|
|
|
2021-08-13 19:55:11 +00:00
|
|
|
screen_set_title(&pd->s, new_wp->base.title);
|
2021-08-13 19:27:25 +00:00
|
|
|
screen_free(&new_wp->base);
|
|
|
|
memcpy(&new_wp->base, &pd->s, sizeof wp->base);
|
|
|
|
screen_resize(&new_wp->base, new_wp->sx, new_wp->sy, 1);
|
|
|
|
screen_init(&pd->s, 1, 1, 0);
|
|
|
|
|
2021-08-13 19:55:11 +00:00
|
|
|
shell = options_get_string(s->options, "default-shell");
|
|
|
|
if (!checkshell(shell))
|
|
|
|
shell = _PATH_BSHELL;
|
|
|
|
new_wp->shell = xstrdup(shell);
|
|
|
|
|
2021-08-13 19:27:25 +00:00
|
|
|
window_pane_set_event(new_wp);
|
|
|
|
window_set_active_pane(w, new_wp, 1);
|
2021-08-13 19:55:11 +00:00
|
|
|
new_wp->flags |= PANE_CHANGED;
|
2021-08-13 19:27:25 +00:00
|
|
|
|
|
|
|
pd->close = 1;
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
static void
|
|
|
|
popup_menu_done(__unused struct menu *menu, __unused u_int choice,
|
|
|
|
key_code key, void *data)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = data;
|
|
|
|
struct client *c = pd->c;
|
|
|
|
struct paste_buffer *pb;
|
|
|
|
const char *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pd->md = NULL;
|
|
|
|
pd->menu = NULL;
|
|
|
|
server_redraw_client(pd->c);
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'p':
|
|
|
|
pb = paste_get_top(NULL);
|
|
|
|
if (pb != NULL) {
|
|
|
|
buf = paste_buffer_data(pb, &len);
|
|
|
|
bufferevent_write(job_get_event(pd->job), buf, len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
pd->sx = c->tty.sx;
|
|
|
|
pd->sy = c->tty.sy;
|
|
|
|
pd->px = 0;
|
|
|
|
pd->py = 0;
|
|
|
|
server_redraw_client(c);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
pd->px = c->tty.sx / 2 - pd->sx / 2;
|
|
|
|
pd->py = c->tty.sy / 2 - pd->sy / 2;
|
|
|
|
server_redraw_client(c);
|
|
|
|
break;
|
2021-08-13 19:27:25 +00:00
|
|
|
case 'h':
|
|
|
|
popup_make_pane(pd, LAYOUT_LEFTRIGHT);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
popup_make_pane(pd, LAYOUT_TOPBOTTOM);
|
|
|
|
break;
|
2021-08-13 18:54:54 +00:00
|
|
|
case 'q':
|
|
|
|
pd->close = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
static void
|
|
|
|
popup_handle_drag(struct client *c, struct popup_data *pd,
|
|
|
|
struct mouse_event *m)
|
|
|
|
{
|
|
|
|
u_int px, py;
|
|
|
|
|
|
|
|
if (!MOUSE_DRAG(m->b))
|
|
|
|
pd->dragging = OFF;
|
|
|
|
else if (pd->dragging == MOVE) {
|
|
|
|
if (m->x < pd->dx)
|
|
|
|
px = 0;
|
|
|
|
else if (m->x - pd->dx + pd->sx > c->tty.sx)
|
|
|
|
px = c->tty.sx - pd->sx;
|
|
|
|
else
|
|
|
|
px = m->x - pd->dx;
|
|
|
|
if (m->y < pd->dy)
|
|
|
|
py = 0;
|
|
|
|
else if (m->y - pd->dy + pd->sy > c->tty.sy)
|
|
|
|
py = c->tty.sy - pd->sy;
|
|
|
|
else
|
|
|
|
py = m->y - pd->dy;
|
|
|
|
pd->px = px;
|
|
|
|
pd->py = py;
|
|
|
|
pd->dx = m->x - pd->px;
|
|
|
|
pd->dy = m->y - pd->py;
|
2021-07-21 08:06:36 +00:00
|
|
|
pd->ppx = px;
|
|
|
|
pd->ppy = py;
|
2020-03-24 08:09:43 +00:00
|
|
|
server_redraw_client(c);
|
|
|
|
} else if (pd->dragging == SIZE) {
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
if (m->x < pd->px + 1)
|
|
|
|
return;
|
|
|
|
if (m->y < pd->py + 1)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (m->x < pd->px + 3)
|
|
|
|
return;
|
|
|
|
if (m->y < pd->py + 3)
|
|
|
|
return;
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
pd->sx = m->x - pd->px;
|
|
|
|
pd->sy = m->y - pd->py;
|
2021-07-21 08:06:36 +00:00
|
|
|
pd->psx = pd->sx;
|
|
|
|
pd->psy = pd->sy;
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
screen_resize(&pd->s, pd->sx, pd->sy, 0);
|
|
|
|
if (pd->job != NULL)
|
|
|
|
job_resize(pd->job, pd->sx, pd->sy);
|
|
|
|
} else {
|
|
|
|
screen_resize(&pd->s, pd->sx - 2, pd->sy - 2, 0);
|
|
|
|
if (pd->job != NULL)
|
|
|
|
job_resize(pd->job, pd->sx - 2, pd->sy - 2);
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
server_redraw_client(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-08-13 18:54:54 +00:00
|
|
|
popup_key_cb(struct client *c, void *data, struct key_event *event)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
2021-08-13 18:54:54 +00:00
|
|
|
struct popup_data *pd = data;
|
2020-03-24 08:09:43 +00:00
|
|
|
struct mouse_event *m = &event->m;
|
2021-03-02 10:56:45 +00:00
|
|
|
const char *buf;
|
2020-04-01 09:05:27 +00:00
|
|
|
size_t len;
|
2021-08-13 18:54:54 +00:00
|
|
|
u_int px, py, x;
|
2021-08-13 23:05:40 +00:00
|
|
|
enum { NONE, LEFT, RIGHT, TOP, BOTTOM } border = NONE;
|
2021-08-13 18:54:54 +00:00
|
|
|
|
|
|
|
if (pd->md != NULL) {
|
|
|
|
if (menu_key_cb(c, pd->md, event) == 1) {
|
|
|
|
pd->md = NULL;
|
|
|
|
pd->menu = NULL;
|
|
|
|
if (pd->close)
|
|
|
|
server_client_clear_overlay(c);
|
|
|
|
else
|
|
|
|
server_redraw_client(c);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
if (KEYC_IS_MOUSE(event->key)) {
|
|
|
|
if (pd->dragging != OFF) {
|
|
|
|
popup_handle_drag(c, pd, m);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (m->x < pd->px ||
|
|
|
|
m->x > pd->px + pd->sx - 1 ||
|
|
|
|
m->y < pd->py ||
|
|
|
|
m->y > pd->py + pd->sy - 1) {
|
2021-08-13 18:54:54 +00:00
|
|
|
if (MOUSE_BUTTONS(m->b) == 2)
|
|
|
|
goto menu;
|
2020-03-24 08:09:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2021-08-13 23:05:40 +00:00
|
|
|
if (~pd->flags & POPUP_NOBORDER) {
|
|
|
|
if (m->x == pd->px)
|
|
|
|
border = LEFT;
|
|
|
|
else if (m->x == pd->px + pd->sx - 1)
|
|
|
|
border = RIGHT;
|
|
|
|
else if (m->y == pd->py)
|
|
|
|
border = TOP;
|
|
|
|
else if (m->y == pd->py + pd->sy - 1)
|
|
|
|
border = BOTTOM;
|
|
|
|
}
|
|
|
|
if ((m->b & MOUSE_MASK_MODIFIERS) == 0 &&
|
2021-08-13 18:54:54 +00:00
|
|
|
MOUSE_BUTTONS(m->b) == 2 &&
|
2021-08-13 23:05:40 +00:00
|
|
|
(border == LEFT || border == TOP))
|
2021-08-13 18:54:54 +00:00
|
|
|
goto menu;
|
2021-08-13 23:05:40 +00:00
|
|
|
if (((m->b & MOUSE_MASK_MODIFIERS) == MOUSE_MASK_META) ||
|
|
|
|
border != NONE) {
|
2020-03-24 08:09:43 +00:00
|
|
|
if (!MOUSE_DRAG(m->b))
|
|
|
|
goto out;
|
|
|
|
if (MOUSE_BUTTONS(m->lb) == 0)
|
|
|
|
pd->dragging = MOVE;
|
|
|
|
else if (MOUSE_BUTTONS(m->lb) == 2)
|
|
|
|
pd->dragging = SIZE;
|
|
|
|
pd->dx = m->lx - pd->px;
|
|
|
|
pd->dy = m->ly - pd->py;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 10:56:45 +00:00
|
|
|
if ((((pd->flags & (POPUP_CLOSEEXIT|POPUP_CLOSEEXITZERO)) == 0) ||
|
|
|
|
pd->job == NULL) &&
|
|
|
|
(event->key == '\033' || event->key == '\003'))
|
|
|
|
return (1);
|
|
|
|
if (pd->job != NULL) {
|
2020-04-01 09:05:27 +00:00
|
|
|
if (KEYC_IS_MOUSE(event->key)) {
|
|
|
|
/* Must be inside, checked already. */
|
2021-08-13 17:03:29 +00:00
|
|
|
if (pd->flags & POPUP_NOBORDER) {
|
|
|
|
px = m->x - pd->px;
|
|
|
|
py = m->y - pd->py;
|
|
|
|
} else {
|
|
|
|
px = m->x - pd->px - 1;
|
|
|
|
py = m->y - pd->py - 1;
|
|
|
|
}
|
|
|
|
if (!input_key_get_mouse(&pd->s, m, px, py, &buf, &len))
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
|
|
|
bufferevent_write(job_get_event(pd->job), buf, len);
|
|
|
|
return (0);
|
|
|
|
}
|
2020-05-16 16:35:13 +00:00
|
|
|
input_key(&pd->s, job_get_event(pd->job), event->key);
|
2020-03-24 08:09:43 +00:00
|
|
|
}
|
2021-03-02 10:56:45 +00:00
|
|
|
return (0);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
2021-08-13 18:54:54 +00:00
|
|
|
menu:
|
|
|
|
pd->menu = menu_create("");
|
2021-08-13 19:27:25 +00:00
|
|
|
if (pd->flags & POPUP_INTERNAL) {
|
|
|
|
menu_add_items(pd->menu, popup_internal_menu_items, NULL, NULL,
|
|
|
|
NULL);
|
|
|
|
} else
|
|
|
|
menu_add_items(pd->menu, popup_menu_items, NULL, NULL, NULL);
|
2021-08-13 18:54:54 +00:00
|
|
|
if (m->x >= (pd->menu->width + 4) / 2)
|
|
|
|
x = m->x - (pd->menu->width + 4) / 2;
|
|
|
|
else
|
|
|
|
x = 0;
|
|
|
|
pd->md = menu_prepare(pd->menu, 0, NULL, x, m->y, c, NULL,
|
|
|
|
popup_menu_done, pd);
|
|
|
|
c->flags |= CLIENT_REDRAWOVERLAY;
|
|
|
|
|
2020-03-24 08:09:43 +00:00
|
|
|
out:
|
|
|
|
pd->lx = m->x;
|
|
|
|
pd->ly = m->y;
|
|
|
|
pd->lb = m->b;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popup_job_update_cb(struct job *job)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = job_get_data(job);
|
|
|
|
struct evbuffer *evb = job_get_event(job)->input;
|
2020-05-16 15:34:08 +00:00
|
|
|
struct client *c = pd->c;
|
2020-03-24 08:09:43 +00:00
|
|
|
struct screen *s = &pd->s;
|
|
|
|
void *data = EVBUFFER_DATA(evb);
|
|
|
|
size_t size = EVBUFFER_LENGTH(evb);
|
|
|
|
|
2020-05-16 15:34:08 +00:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2021-08-13 19:25:24 +00:00
|
|
|
if (pd->md != NULL) {
|
|
|
|
c->overlay_check = menu_check_cb;
|
|
|
|
c->overlay_data = pd->md;
|
|
|
|
} else {
|
|
|
|
c->overlay_check = NULL;
|
|
|
|
c->overlay_data = NULL;
|
|
|
|
}
|
2020-05-16 15:34:08 +00:00
|
|
|
input_parse_screen(pd->ictx, s, popup_init_ctx_cb, pd, data, size);
|
|
|
|
c->overlay_check = popup_check_cb;
|
2021-08-13 19:25:24 +00:00
|
|
|
c->overlay_data = pd;
|
2020-05-16 15:34:08 +00:00
|
|
|
|
|
|
|
evbuffer_drain(evb, size);
|
2020-03-24 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popup_job_complete_cb(struct job *job)
|
|
|
|
{
|
|
|
|
struct popup_data *pd = job_get_data(job);
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status = job_get_status(pd->job);
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
pd->status = WEXITSTATUS(status);
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
pd->status = WTERMSIG(status);
|
|
|
|
else
|
|
|
|
pd->status = 0;
|
|
|
|
pd->job = NULL;
|
|
|
|
|
2020-03-30 07:42:44 +00:00
|
|
|
if ((pd->flags & POPUP_CLOSEEXIT) ||
|
|
|
|
((pd->flags & POPUP_CLOSEEXITZERO) && pd->status == 0))
|
2020-03-28 09:51:12 +00:00
|
|
|
server_client_clear_overlay(pd->c);
|
2020-03-24 08:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
popup_display(int flags, struct cmdq_item *item, u_int px, u_int py, u_int sx,
|
2021-03-02 10:56:45 +00:00
|
|
|
u_int sy, const char *shellcmd, int argc, char **argv, const char *cwd,
|
|
|
|
struct client *c, struct session *s, popup_close_cb cb, void *arg)
|
2020-03-24 08:09:43 +00:00
|
|
|
{
|
|
|
|
struct popup_data *pd;
|
2021-08-13 17:03:29 +00:00
|
|
|
u_int jx, jy;
|
|
|
|
|
|
|
|
if (flags & POPUP_NOBORDER) {
|
|
|
|
if (sx < 1 || sy < 1)
|
|
|
|
return (-1);
|
|
|
|
jx = sx;
|
|
|
|
jy = sy;
|
|
|
|
} else {
|
|
|
|
if (sx < 3 || sy < 3)
|
|
|
|
return (-1);
|
|
|
|
jx = sx - 2;
|
|
|
|
jy = sy - 2;
|
|
|
|
}
|
2020-03-24 08:09:43 +00:00
|
|
|
if (c->tty.sx < sx || c->tty.sy < sy)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
pd = xcalloc(1, sizeof *pd);
|
|
|
|
pd->item = item;
|
|
|
|
pd->flags = flags;
|
|
|
|
|
|
|
|
pd->c = c;
|
|
|
|
pd->c->references++;
|
|
|
|
|
2020-05-16 15:24:28 +00:00
|
|
|
pd->cb = cb;
|
|
|
|
pd->arg = arg;
|
2020-03-24 08:09:43 +00:00
|
|
|
pd->status = 128 + SIGHUP;
|
|
|
|
|
|
|
|
screen_init(&pd->s, sx - 2, sy - 2, 0);
|
2021-08-11 20:49:55 +00:00
|
|
|
colour_palette_init(&pd->palette);
|
|
|
|
colour_palette_from_option(&pd->palette, global_w_options);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
pd->px = px;
|
|
|
|
pd->py = py;
|
|
|
|
pd->sx = sx;
|
|
|
|
pd->sy = sy;
|
|
|
|
|
2021-07-21 08:06:36 +00:00
|
|
|
pd->ppx = px;
|
|
|
|
pd->ppy = py;
|
|
|
|
pd->psx = sx;
|
|
|
|
pd->psy = sy;
|
|
|
|
|
2021-03-02 10:56:45 +00:00
|
|
|
pd->job = job_run(shellcmd, argc, argv, s, cwd,
|
|
|
|
popup_job_update_cb, popup_job_complete_cb, NULL, pd,
|
2021-08-13 17:03:29 +00:00
|
|
|
JOB_NOWAIT|JOB_PTY|JOB_KEEPWRITE, jx, jy);
|
2021-08-11 20:49:55 +00:00
|
|
|
pd->ictx = input_init(NULL, job_get_event(pd->job), &pd->palette);
|
2020-03-24 08:09:43 +00:00
|
|
|
|
|
|
|
server_client_set_overlay(c, 0, popup_check_cb, popup_mode_cb,
|
2021-07-21 08:06:36 +00:00
|
|
|
popup_draw_cb, popup_key_cb, popup_free_cb, popup_resize_cb, pd);
|
2020-03-24 08:09:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2020-05-16 16:13:09 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
popup_editor_free(struct popup_editor *pe)
|
|
|
|
{
|
|
|
|
unlink(pe->path);
|
|
|
|
free(pe->path);
|
|
|
|
free(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
popup_editor_close_cb(int status, void *arg)
|
|
|
|
{
|
|
|
|
struct popup_editor *pe = arg;
|
|
|
|
FILE *f;
|
|
|
|
char *buf = NULL;
|
|
|
|
off_t len = 0;
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
pe->cb(NULL, 0, pe->arg);
|
|
|
|
popup_editor_free(pe);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen(pe->path, "r");
|
|
|
|
if (f != NULL) {
|
|
|
|
fseeko(f, 0, SEEK_END);
|
|
|
|
len = ftello(f);
|
|
|
|
fseeko(f, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (len == 0 ||
|
|
|
|
(uintmax_t)len > (uintmax_t)SIZE_MAX ||
|
|
|
|
(buf = malloc(len)) == NULL ||
|
|
|
|
fread(buf, len, 1, f) != 1) {
|
|
|
|
free(buf);
|
|
|
|
buf = NULL;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
pe->cb(buf, len, pe->arg); /* callback now owns buffer */
|
|
|
|
popup_editor_free(pe);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
popup_editor(struct client *c, const char *buf, size_t len,
|
|
|
|
popup_finish_edit_cb cb, void *arg)
|
|
|
|
{
|
|
|
|
struct popup_editor *pe;
|
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
char *cmd;
|
|
|
|
char path[] = _PATH_TMP "tmux.XXXXXXXX";
|
|
|
|
const char *editor;
|
|
|
|
u_int px, py, sx, sy;
|
|
|
|
|
|
|
|
editor = options_get_string(global_options, "editor");
|
|
|
|
if (*editor == '\0')
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
fd = mkstemp(path);
|
|
|
|
if (fd == -1)
|
|
|
|
return (-1);
|
|
|
|
f = fdopen(fd, "w");
|
|
|
|
if (fwrite(buf, len, 1, f) != 1) {
|
|
|
|
fclose(f);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
pe = xcalloc(1, sizeof *pe);
|
|
|
|
pe->path = xstrdup(path);
|
|
|
|
pe->cb = cb;
|
|
|
|
pe->arg = arg;
|
|
|
|
|
|
|
|
sx = c->tty.sx * 9 / 10;
|
|
|
|
sy = c->tty.sy * 9 / 10;
|
|
|
|
px = (c->tty.sx / 2) - (sx / 2);
|
|
|
|
py = (c->tty.sy / 2) - (sy / 2);
|
|
|
|
|
|
|
|
xasprintf(&cmd, "%s %s", editor, path);
|
2021-08-13 19:27:25 +00:00
|
|
|
if (popup_display(POPUP_INTERNAL|POPUP_CLOSEEXIT, NULL, px, py, sx, sy,
|
|
|
|
cmd, 0, NULL, _PATH_TMP, c, NULL, popup_editor_close_cb, pe) != 0) {
|
2020-05-16 16:13:09 +00:00
|
|
|
popup_editor_free(pe);
|
|
|
|
free(cmd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
free(cmd);
|
|
|
|
return (0);
|
|
|
|
}
|