Move cmdq_state into cmd-queue.c.

This commit is contained in:
nicm 2020-04-13 14:46:04 +00:00
parent 9a65102bfc
commit adb76fd1ce
24 changed files with 99 additions and 89 deletions

View File

@ -50,8 +50,7 @@ enum cmd_retval
cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
int xflag, int rflag, const char *cflag, int Eflag)
{
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state target;
enum cmd_find_type type;
int flags;
@ -120,7 +119,7 @@ cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
environ_update(s->options, c->environ, s->environ);
c->session = s;
if (~state->flags & CMDQ_STATE_REPEAT)
if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
server_client_set_key_table(c, NULL);
tty_update_client_offset(c);
status_timer_start(c);

View File

@ -49,8 +49,7 @@ static enum cmd_retval
cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct cmd_find_state *source = cmdq_get_source(item);
struct client *c = cmd_find_client(item, NULL, 1);

View File

@ -57,7 +57,7 @@ static enum cmd_retval
cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct key_event *event = cmdq_get_event(item);
struct cmd_find_state *source = cmdq_get_source(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct client *c = cmdq_get_client(item);
@ -70,7 +70,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item)
}
if (args_has(args, 'M')) {
if ((wp = cmd_mouse_pane(&state->event.m, &s, NULL)) == NULL)
if ((wp = cmd_mouse_pane(&event->m, &s, NULL)) == NULL)
return (CMD_RETURN_NORMAL);
if (c == NULL || c->session != s)
return (CMD_RETURN_NORMAL);
@ -87,7 +87,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item)
swp = wp;
if (!window_pane_set_mode(wp, swp, &window_copy_mode, NULL, args)) {
if (args_has(args, 'M'))
window_copy_start_drag(c, &state->event.m);
window_copy_start_drag(c, &event->m);
}
if (args_has(args, 'u'))
window_copy_pageup(wp, 0);

View File

@ -65,8 +65,8 @@ static void
cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
struct args *args, u_int *px, u_int *py, u_int w, u_int h)
{
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct key_event *event = cmdq_get_event(item);
struct session *s = c->session;
struct winlink *wl = target->wl;
struct window_pane *wp = target->wp;
@ -100,8 +100,8 @@ cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
else
*px = 0;
} else if (strcmp(xp, "M") == 0) {
if (state->event.m.valid && state->event.m.x > w / 2)
*px = state->event.m.x - w / 2;
if (event->m.valid && event->m.x > w / 2)
*px = event->m.x - w / 2;
else
*px = 0;
} else if (strcmp(xp, "W") == 0) {
@ -134,8 +134,8 @@ cmd_display_menu_get_position(struct client *c, struct cmdq_item *item,
else
*py = 0;
} else if (strcmp(yp, "M") == 0) {
if (state->event.m.valid)
*py = state->event.m.y + h;
if (event->m.valid)
*py = event->m.y + h;
else
*py = 0;
} else if (strcmp(yp, "S") == 0) {
@ -176,8 +176,8 @@ static enum cmd_retval
cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct key_event *event = cmdq_get_event(item);
struct client *c;
struct menu *menu = NULL;
struct menu_item menu_item;
@ -230,7 +230,7 @@ cmd_display_menu_exec(struct cmd *self, struct cmdq_item *item)
cmd_display_menu_get_position(c, item, args, &px, &py, menu->width + 4,
menu->count + 2);
if (!state->event.m.valid)
if (!event->m.valid)
flags |= MENU_NOMOUSE;
if (menu_display(menu, flags, item, px, py, c, target, NULL, NULL) != 0)
return (CMD_RETURN_NORMAL);

View File

@ -961,8 +961,8 @@ cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item,
if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
fs->current = &marked_pane;
log_debug("%s: current is marked pane", __func__);
} else if (cmd_find_valid_state(&cmdq_get_state(item)->current)) {
fs->current = &cmdq_get_state(item)->current;
} else if (cmd_find_valid_state(cmdq_get_current(item))) {
fs->current = cmdq_get_current(item);
log_debug("%s: current is from queue", __func__);
} else if (cmd_find_from_client(&current, cmdq_get_client(item),
flags) == 0) {
@ -982,7 +982,7 @@ cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item,
/* Mouse target is a plain = or {mouse}. */
if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
m = &cmdq_get_state(item)->event.m;
m = &cmdq_get_event(item)->m;
switch (type) {
case CMD_FIND_PANE:
fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);

View File

@ -56,16 +56,15 @@ struct cmd_if_shell_data {
struct client *client;
struct cmdq_item *item;
struct mouse_event mouse;
struct key_event event;
};
static enum cmd_retval
cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct mouse_event *m = &state->event.m;
struct key_event *event = cmdq_get_event(item);
struct cmd_if_shell_data *cdata;
char *shellcmd, *cmd;
const char *file;
@ -102,7 +101,8 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
free(pr->error);
return (CMD_RETURN_ERROR);
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, target, m, 0);
new_item = cmdq_get_command(pr->cmdlist, target, event,
0);
cmdq_insert_after(item, new_item);
cmd_list_free(pr->cmdlist);
break;
@ -117,7 +117,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
cdata->cmd_else = xstrdup(args->argv[2]);
else
cdata->cmd_else = NULL;
memcpy(&cdata->mouse, m, sizeof cdata->mouse);
memcpy(&cdata->event, event, sizeof cdata->event);
if (!args_has(args, 'b'))
cdata->client = cmdq_get_client(item);
@ -161,7 +161,7 @@ cmd_if_shell_callback(struct job *job)
{
struct cmd_if_shell_data *cdata = job_get_data(job);
struct client *c = cdata->client;
struct mouse_event *m = &cdata->mouse;
struct key_event *event = &cdata->event;
struct cmdq_item *new_item = NULL;
char *cmd;
int status;
@ -185,7 +185,7 @@ cmd_if_shell_callback(struct job *job)
free(pr->error);
break;
case CMD_PARSE_SUCCESS:
new_item = cmdq_get_command(pr->cmdlist, NULL, m, 0);
new_item = cmdq_get_command(pr->cmdlist, NULL, event, 0);
cmd_list_free(pr->cmdlist);
break;
}

View File

@ -64,8 +64,7 @@ static enum cmd_retval
cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct cmd_find_state *source = cmdq_get_source(item);
struct session *dst_s;

View File

@ -67,8 +67,7 @@ static enum cmd_retval
cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct client *c = cmdq_get_client(item);
struct session *s, *as, *groupwith;
@ -317,7 +316,7 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
} else if (c->session != NULL)
c->last_session = c->session;
c->session = s;
if (~state->flags & CMDQ_STATE_REPEAT)
if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
server_client_set_key_table(c, NULL);
tty_update_client_offset(c);
status_timer_start(c);

View File

@ -52,8 +52,7 @@ static enum cmd_retval
cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct spawn_context sc;
struct client *c = cmd_find_client(item, NULL, 1);

View File

@ -65,6 +65,23 @@ struct cmdq_item {
};
TAILQ_HEAD(cmdq_list, cmdq_item);
/*
* Command queue state. This is the context for commands on the command queue.
* It holds information about how the commands were fired (the key and flags),
* any additional formats for the commands, and the current default target.
* Multiple commands can share the same state and a command may update the
* default target.
*/
struct cmdq_state {
int references;
int flags;
struct format_tree *formats;
struct key_event event;
struct cmd_find_state current;
};
/* Get command queue name. */
static const char *
cmdq_name(struct client *c)
@ -142,11 +159,25 @@ cmdq_get_source(struct cmdq_item *item)
return (&item->source);
}
/* Get item state. */
struct cmdq_state *
cmdq_get_state(struct cmdq_item *item)
/* Get state event. */
struct key_event *
cmdq_get_event(struct cmdq_item *item)
{
return (item->state);
return (&item->state->event);
}
/* Get state current target. */
struct cmd_find_state *
cmdq_get_current(struct cmdq_item *item)
{
return (&item->state->current);
}
/* Get state flags. */
int
cmdq_get_flags(struct cmdq_item *item)
{
return (item->state->flags);
}
/* Merge formats from item. */
@ -317,7 +348,7 @@ cmdq_remove_group(struct cmdq_item *item)
/* Get a command for the command queue. */
struct cmdq_item *
cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
struct mouse_event *m, int flags)
struct key_event *event, int flags)
{
struct cmdq_item *item, *first = NULL, *last = NULL;
struct cmd *cmd;
@ -333,10 +364,9 @@ cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
cmd_find_copy_state(&state->current, current);
else
cmd_find_clear_state(&state->current, 0);
if (m != NULL) {
state->event.key = KEYC_NONE;
memcpy(&state->event.m, m,
sizeof state->event.m);
if (event != NULL) {
memcpy(&state->event, event,
sizeof state->event);
}
state->flags = flags;
last_group = group;

View File

@ -50,8 +50,8 @@ static enum cmd_retval
cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct key_event *event = cmdq_get_event(item);
struct window_pane *wp = target->wp;
struct winlink *wl = target->wl;
struct window *w = wl->window;
@ -76,12 +76,12 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item)
}
if (args_has(args, 'M')) {
if (cmd_mouse_window(&state->event.m, &s) == NULL)
if (!event->m.valid || cmd_mouse_window(&event->m, &s) == NULL)
return (CMD_RETURN_NORMAL);
if (c == NULL || c->session != s)
return (CMD_RETURN_NORMAL);
c->tty.mouse_drag_update = cmd_resize_pane_mouse_update;
cmd_resize_pane_mouse_update(c, &state->event.m);
cmd_resize_pane_mouse_update(c, &event->m);
return (CMD_RETURN_NORMAL);
}

View File

@ -44,8 +44,7 @@ static enum cmd_retval
cmd_rotate_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct winlink *wl = target->wl;
struct window *w = wl->window;

View File

@ -85,8 +85,7 @@ cmd_select_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
const struct cmd_entry *entry = cmd_get_entry(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct client *c = cmd_find_client(item, NULL, 1);
struct winlink *wl = target->wl;

View File

@ -85,8 +85,7 @@ static enum cmd_retval
cmd_select_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct winlink *wl = target->wl;
struct session *s = target->s;

View File

@ -134,13 +134,13 @@ static enum cmd_retval
cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct client *c = cmd_find_client(item, NULL, 1);
struct window_pane *wp = target->wp;
struct session *s = target->s;
struct winlink *wl = target->wl;
struct mouse_event *m = &state->event.m;
struct key_event *event = cmdq_get_event(item);
struct mouse_event *m = &event->m;
struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
int i;
key_code key;

View File

@ -54,8 +54,7 @@ static enum cmd_retval
cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state *target = cmdq_get_target(item);
struct spawn_context sc;
struct client *c = cmd_find_client(item, NULL, 1);

View File

@ -48,8 +48,7 @@ static enum cmd_retval
cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = cmd_get_args(self);
struct cmdq_state *state = cmdq_get_state(item);
struct cmd_find_state *current = &state->current;
struct cmd_find_state *current = cmdq_get_current(item);
struct cmd_find_state target;
const char *tflag = args_get(args, 't');
enum cmd_find_type type;
@ -137,7 +136,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
if (c->session != NULL && c->session != s)
c->last_session = c->session;
c->session = s;
if (~state->flags & CMDQ_STATE_REPEAT)
if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
server_client_set_key_table(c, NULL);
tty_update_client_offset(c);
status_timer_start(c);

View File

@ -85,8 +85,8 @@ control_callback(__unused struct client *c, __unused const char *path,
cmdq_append(c, item);
break;
case CMD_PARSE_SUCCESS:
item = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
cmdq_get_state(item)->flags |= CMDQ_STATE_CONTROL;
item = cmdq_get_command(pr->cmdlist, NULL, NULL,
CMDQ_STATE_CONTROL);
cmdq_append(c, item);
cmd_list_free(pr->cmdlist);
break;

View File

@ -1124,16 +1124,13 @@ format_merge(struct format_tree *ft, struct format_tree *from)
static void
format_create_add_item(struct format_tree *ft, struct cmdq_item *item)
{
struct cmdq_state *state = cmdq_get_state(item);
struct mouse_event *m;
struct key_event *event = cmdq_get_event(item);
struct mouse_event *m = &event->m;
struct window_pane *wp;
u_int x, y;
cmdq_merge_formats(item, ft);
if (state == NULL)
return;
m = &state->event.m;
if (m->valid && ((wp = cmd_mouse_pane(m, NULL, NULL)) != NULL)) {
format_add(ft, "mouse_pane", "%%%u", wp->id);
if (cmd_mouse_at(wp, m, &x, &y, 0) == 0) {

View File

@ -535,10 +535,10 @@ key_bindings_read_only(struct cmdq_item *item, __unused void *data)
struct cmdq_item *
key_bindings_dispatch(struct key_binding *bd, struct cmdq_item *item,
struct client *c, struct mouse_event *m, struct cmd_find_state *fs)
struct client *c, struct key_event *event, struct cmd_find_state *fs)
{
struct cmdq_item *new_item;
int readonly;
int readonly, flags = 0;
if (c == NULL || (~c->flags & CLIENT_READONLY))
readonly = 1;
@ -547,9 +547,9 @@ key_bindings_dispatch(struct key_binding *bd, struct cmdq_item *item,
if (!readonly)
new_item = cmdq_get_callback(key_bindings_read_only, NULL);
else {
new_item = cmdq_get_command(bd->cmdlist, fs, m, 0);
if (bd->flags & KEY_BINDING_REPEAT)
cmdq_get_state(new_item)->flags |= CMDQ_STATE_REPEAT;
flags |= CMDQ_STATE_REPEAT;
new_item = cmdq_get_command(bd->cmdlist, fs, event, flags);
}
if (item != NULL)
new_item = cmdq_insert_after(item, new_item);

6
menu.c
View File

@ -282,10 +282,10 @@ chosen:
break;
case CMD_PARSE_SUCCESS:
if (md->item != NULL)
m = &cmdq_get_state(md->item)->event.m;
event = cmdq_get_event(md->item);
else
m = NULL;
new_item = cmdq_get_command(pr->cmdlist, &md->fs, m, 0);
event = NULL;
new_item = cmdq_get_command(pr->cmdlist, &md->fs, event, 0);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;

View File

@ -305,10 +305,10 @@ popup_key_cb(struct client *c, struct key_event *event)
break;
case CMD_PARSE_SUCCESS:
if (pd->item != NULL)
m = &cmdq_get_state(pd->item)->event.m;
event = cmdq_get_event(pd->item);
else
m = NULL;
new_item = cmdq_get_command(pr->cmdlist, fs, m, 0);
event = NULL;
new_item = cmdq_get_command(pr->cmdlist, fs, event, 0);
cmd_list_free(pr->cmdlist);
cmdq_append(c, new_item);
break;

View File

@ -1223,7 +1223,7 @@ try_again:
server_status_client(c);
/* Execute the key binding. */
key_bindings_dispatch(bd, item, c, m, &fs);
key_bindings_dispatch(bd, item, c, event, &fs);
key_bindings_unref_table(table);
goto out;
}

21
tmux.h
View File

@ -43,6 +43,7 @@ struct cmd;
struct cmd_find_state;
struct cmdq_item;
struct cmdq_list;
struct cmdq_state;
struct cmds;
struct environ;
struct format_job_tree;
@ -1378,21 +1379,11 @@ struct cmd_parse_input {
struct cmd_find_state fs;
};
/* Command queue item state. */
struct cmdq_state {
int references;
int flags;
/* Command queue flags. */
#define CMDQ_STATE_REPEAT 0x1
#define CMDQ_STATE_CONTROL 0x2
#define CMDQ_STATE_NOHOOKS 0x4
struct format_tree *formats;
struct key_event event;
struct cmd_find_state current;
};
/* Command queue callback. */
typedef enum cmd_retval (*cmdq_cb) (struct cmdq_item *, void *);
@ -2108,10 +2099,12 @@ const char *cmdq_get_name(struct cmdq_item *);
struct client *cmdq_get_client(struct cmdq_item *);
struct cmd_find_state *cmdq_get_target(struct cmdq_item *);
struct cmd_find_state *cmdq_get_source(struct cmdq_item *);
struct cmdq_state *cmdq_get_state(struct cmdq_item *);
struct key_event *cmdq_get_event(struct cmdq_item *);
struct cmd_find_state *cmdq_get_current(struct cmdq_item *);
int cmdq_get_flags(struct cmdq_item *);
void cmdq_merge_formats(struct cmdq_item *, struct format_tree *);
struct cmdq_item *cmdq_get_command(struct cmd_list *, struct cmd_find_state *,
struct mouse_event *, int);
struct key_event *, int);
#define cmdq_get_callback(cb, data) cmdq_get_callback1(#cb, cb, data)
struct cmdq_item *cmdq_get_callback1(const char *, cmdq_cb, void *);
struct cmdq_item *cmdq_get_error(const char *);
@ -2147,7 +2140,7 @@ void key_bindings_remove(const char *, key_code);
void key_bindings_remove_table(const char *);
void key_bindings_init(void);
struct cmdq_item *key_bindings_dispatch(struct key_binding *,
struct cmdq_item *, struct client *, struct mouse_event *,
struct cmdq_item *, struct client *, struct key_event *,
struct cmd_find_state *);
/* key-string.c */