Skip to content

Commit

Permalink
Some hooks API changes to fire a hook while waiting another cmdq and
Browse files Browse the repository at this point in the history
infrastructure that will be needed soon.
  • Loading branch information
nicm committed Dec 15, 2015
1 parent 9d88d82 commit ac97783
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ cmd_attach_session(struct cmd_q *cmdq, int dflag, int rflag, const char *cflag,

if (~c->flags & CLIENT_CONTROL)
proc_send(c->peer, MSG_READY, -1, NULL, 0);
hooks_run(c->session->hooks, "client-attached", c);
hooks_run(c->session->hooks, c, "client-attached");
cmdq->client_exit = 0;
}
recalculate_sizes();
Expand Down
77 changes: 69 additions & 8 deletions hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RB_GENERATE(hooks_tree, hook, entry, hooks_cmp);

static struct hook *hooks_find1(struct hooks *, const char *);
static void hooks_free1(struct hooks *, struct hook *);
static void hooks_emptyfn(struct cmd_q *);

static int
hooks_cmp(struct hook *hook1, struct hook *hook2)
Expand Down Expand Up @@ -132,18 +133,78 @@ hooks_find(struct hooks *hooks, const char *name)
return (hook);
}

void
hooks_run(struct hooks *hooks, const char *name, struct client *c)
static void
hooks_emptyfn(struct cmd_q *hooks_cmdq)
{
struct cmd_q *cmdq = hooks_cmdq->data;

if (cmdq != NULL) {
if (hooks_cmdq->client_exit >= 0)
cmdq->client_exit = hooks_cmdq->client_exit;
if (!cmdq_free(cmdq))
cmdq_continue(cmdq);
}
cmdq_free(hooks_cmdq);
}

int
hooks_run(struct hooks *hooks, struct client *c, const char *fmt, ...)
{
struct hook *hook;
struct cmd_q *cmdq;
struct cmd_q *hooks_cmdq;
va_list ap;
char *name;

va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
va_end(ap);

hook = hooks_find(hooks, name);
if (hook == NULL)
return;
if (hook == NULL) {
free(name);
return (-1);
}
log_debug("running hook %s", name);
free(name);

hooks_cmdq = cmdq_new(c);
hooks_cmdq->flags |= CMD_Q_NOHOOKS;
hooks_cmdq->parent = NULL;

cmdq_run(hooks_cmdq, hook->cmdlist, NULL);
cmdq_free(hooks_cmdq);
return (0);
}

int
hooks_wait(struct hooks *hooks, struct cmd_q *cmdq, const char *fmt, ...)
{
struct hook *hook;
struct cmd_q *hooks_cmdq;
va_list ap;
char *name;

va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
va_end(ap);

hook = hooks_find(hooks, name);
if (hook == NULL) {
free(name);
return (-1);
}
log_debug("running hook %s (parent %p)", name, cmdq);
free(name);

hooks_cmdq = cmdq_new(cmdq->client);
hooks_cmdq->flags |= CMD_Q_NOHOOKS;
hooks_cmdq->parent = cmdq;

hooks_cmdq->emptyfn = hooks_emptyfn;
hooks_cmdq->data = cmdq;

cmdq = cmdq_new(c);
cmdq_run(cmdq, hook->cmdlist, NULL);
cmdq_free(cmdq);
if (cmdq != NULL)
cmdq->references++;
cmdq_run(hooks_cmdq, hook->cmdlist, NULL);
return (0);
}
4 changes: 2 additions & 2 deletions server-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ server_client_detach(struct client *c, enum msgtype msgtype)
if (s == NULL)
return;

hooks_run(c->session->hooks, "client-detached", c);
hooks_run(c->session->hooks, c, "client-detached");
proc_send_s(c->peer, msgtype, s->name);
}

Expand Down Expand Up @@ -1027,7 +1027,7 @@ server_client_dispatch(struct imsg *imsg, void *arg)
server_redraw_client(c);
}
if (c->session != NULL)
hooks_run(c->session->hooks, "client-resized", c);
hooks_run(c->session->hooks, c, "client-resized");
break;
case MSG_EXITING:
if (datalen != 0)
Expand Down
8 changes: 7 additions & 1 deletion tmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -1368,13 +1368,16 @@ struct cmd_q {
int references;
int flags;
#define CMD_Q_DEAD 0x1
#define CMD_Q_REENTRY 0x2
#define CMD_Q_NOHOOKS 0x4

struct client *client;
int client_exit;

struct cmd_q_items queue;
struct cmd_q_item *item;
struct cmd *cmd;
struct cmd_q *parent;

struct cmd_state state;

Expand Down Expand Up @@ -1581,7 +1584,10 @@ void hooks_add(struct hooks *, const char *, struct cmd_list *);
void hooks_copy(struct hooks *, struct hooks *);
void hooks_remove(struct hooks *, const char *);
struct hook *hooks_find(struct hooks *, const char *);
void hooks_run(struct hooks *, const char *, struct client *);
int printflike(3, 4) hooks_run(struct hooks *, struct client *, const char *,
...);
int printflike(3, 4) hooks_wait(struct hooks *, struct cmd_q *, const char *,
...);

/* mode-key.c */
extern const struct mode_key_table mode_key_tables[];
Expand Down

0 comments on commit ac97783

Please sign in to comment.