Skip to content

Commit

Permalink
Move cmdq_state into cmd-queue.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicm committed Apr 13, 2020
1 parent 9a65102 commit adb76fd
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 89 deletions.
5 changes: 2 additions & 3 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions cmd-copy-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions cmd-display-menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions cmd-find.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions cmd-if-shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions cmd-join-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions cmd-new-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions cmd-new-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 39 additions & 9 deletions cmd-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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->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);
return (item->state->flags);
}

/* Merge formats from item. */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cmd-resize-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions cmd-rotate-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions cmd-select-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions cmd-select-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions cmd-send-keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions cmd-split-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions cmd-switch-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit adb76fd

Please sign in to comment.