Use the notify name string instead of going via an enum and change

existing hooks to use notifys instead.
pull/593/head
nicm 2016-10-16 22:06:40 +00:00
parent d15d54c2c8
commit 41e633acf5
18 changed files with 113 additions and 136 deletions

View File

@ -105,7 +105,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
c->session = s;
server_client_set_key_table(c, NULL);
status_timer_start(c);
notify_attached_session_changed(c);
notify_client("client-session-changed", c);
session_update_activity(s, NULL);
gettimeofday(&s->last_attached_time, NULL);
server_redraw_client(c);
@ -137,7 +137,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
c->session = s;
server_client_set_key_table(c, NULL);
status_timer_start(c);
notify_attached_session_changed(c);
notify_client("client-session-changed", c);
session_update_activity(s, NULL);
gettimeofday(&s->last_attached_time, NULL);
server_redraw_client(c);
@ -145,7 +145,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
if (~c->flags & CLIENT_CONTROL)
proc_send(c->peer, MSG_READY, -1, NULL, 0);
hooks_run(c->session->hooks, c, NULL, "client-attached");
notify_client("client-attached", c);
c->flags |= CLIENT_ATTACHED;
}
recalculate_sizes();

View File

@ -228,7 +228,7 @@ cmd_find_best_winlink_with_window(struct cmd_find_state *fs)
struct winlink *wl, *wl_loop;
wl = NULL;
if (fs->s->curw->window == fs->w)
if (fs->s->curw != NULL && fs->s->curw->window == fs->w)
wl = fs->s->curw;
else {
RB_FOREACH(wl_loop, winlinks, &fs->s->windows) {

View File

@ -153,8 +153,8 @@ cmd_join_pane_exec(struct cmd *self, struct cmdq_item *item)
if (window_count_panes(src_w) == 0)
server_kill_window(src_w);
else
notify_window_layout_changed(src_w);
notify_window_layout_changed(dst_w);
notify_window("window-layout-changed", src_w);
notify_window("window-layout-changed", dst_w);
return (CMD_RETURN_NORMAL);
}

View File

@ -281,7 +281,7 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
c->session = s;
server_client_set_key_table(c, NULL);
status_timer_start(c);
notify_attached_session_changed(c);
notify_client("client-session-changed", c);
session_update_activity(s, NULL);
gettimeofday(&s->last_attached_time, NULL);
server_redraw_client(c);

View File

@ -112,7 +112,7 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
* Can't use session_detach as it will destroy session if this
* makes it empty.
*/
notify_window_unlinked(s, wl->window);
notify_session_window("window-unlinked", s, wl->window);
wl->flags &= ~WINLINK_ALERTFLAGS;
winlink_stack_remove(&s->lastw, wl);
winlink_remove(&s->windows, wl);

View File

@ -65,7 +65,7 @@ cmd_rename_session_exec(struct cmd *self, struct cmdq_item *item)
RB_INSERT(sessions, &sessions, s);
server_status_session(s);
notify_session_renamed(s);
notify_session("session-renamed", s);
return (CMD_RETURN_NORMAL);
}

View File

@ -178,7 +178,7 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
format_free(ft);
}
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
if (to_free != NULL)
free((void *)to_free);

View File

@ -158,7 +158,7 @@ control_notify_window_renamed(struct window *w)
}
void
control_notify_attached_session_changed(struct client *c)
control_notify_client_session_changed(struct client *c)
{
struct session *s;

View File

@ -175,7 +175,7 @@ layout_parse(struct window *w, const char *layout)
layout_print_cell(lc, __func__, 0);
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
return (0);

View File

@ -578,7 +578,7 @@ layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
/* Fix cell offsets. */
layout_fix_offsets(wp->window->layout_root);
layout_fix_panes(wp->window, wp->window->sx, wp->window->sy);
notify_window_layout_changed(wp->window);
notify_window("window-layout-changed", wp->window);
}
/* Helper function to grow pane. */
@ -980,5 +980,5 @@ layout_close_pane(struct window_pane *wp)
layout_fix_offsets(w->layout_root);
layout_fix_panes(w, w->sx, w->sy);
}
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
}

151
notify.c
View File

@ -20,72 +20,43 @@
#include <sys/queue.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
enum notify_type {
NOTIFY_WINDOW_LAYOUT_CHANGED,
NOTIFY_WINDOW_UNLINKED,
NOTIFY_WINDOW_LINKED,
NOTIFY_WINDOW_RENAMED,
NOTIFY_ATTACHED_SESSION_CHANGED,
NOTIFY_SESSION_RENAMED,
NOTIFY_SESSION_CREATED,
NOTIFY_SESSION_CLOSED
};
static const char *notify_hooks[] = {
"window-layout-changed",
NULL, /* "window-unlinked", */
NULL, /* "window-linked", */
"window-renamed",
NULL, /* "attached-session-changed", */
"session-renamed",
"session-created",
"session-closed"
};
struct notify_entry {
enum notify_type type;
const char *name;
struct client *client;
struct session *session;
struct window *window;
int pane;
struct cmd_find_state fs;
};
static void
notify_hook(struct cmdq_item *item, struct notify_entry *ne)
{
const char *name;
struct cmd_find_state fs;
struct hook *hook;
struct cmdq_item *new_item;
struct session *s = ne->session;
struct window *w = ne->window;
name = notify_hooks[ne->type];
if (name == NULL)
return;
cmd_find_clear_state(&fs, NULL, 0);
if (s != NULL && w != NULL)
cmd_find_from_session_window(&fs, s, w);
else if (w != NULL)
cmd_find_from_window(&fs, w);
else if (s != NULL && session_alive(s))
cmd_find_from_session(&fs, s);
else
if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
cmd_find_current(&fs, item, CMD_FIND_QUIET);
if (cmd_find_empty_state(&fs) || !cmd_find_valid_state(&fs))
return;
else
cmd_find_copy_state(&fs, &ne->fs);
hook = hooks_find(fs.s->hooks, name);
hook = hooks_find(hooks_get(fs.s), ne->name);
if (hook == NULL)
return;
log_debug("notify hook %s", name);
log_debug("notify hook %s", ne->name);
new_item = cmdq_get_command(hook->cmdlist, &fs, NULL, CMDQ_NOHOOKS);
cmdq_format(new_item, "hook", "%s", name);
cmdq_format(new_item, "hook", "%s", ne->name);
if (s != NULL) {
cmdq_format(new_item, "hook_session", "$%u", s->id);
@ -95,6 +66,8 @@ notify_hook(struct cmdq_item *item, struct notify_entry *ne)
cmdq_format(new_item, "hook_window", "@%u", w->id);
cmdq_format(new_item, "hook_window_name", "%s", w->name);
}
if (ne->pane != -1)
cmdq_format(new_item, "hook_pane", "%%%d", ne->pane);
cmdq_insert_after(item, new_item);
}
@ -104,32 +77,23 @@ notify_callback(struct cmdq_item *item, void *data)
{
struct notify_entry *ne = data;
switch (ne->type) {
case NOTIFY_WINDOW_LAYOUT_CHANGED:
if (strcmp(ne->name, "window-layout-changed") == 0)
control_notify_window_layout_changed(ne->window);
break;
case NOTIFY_WINDOW_UNLINKED:
if (strcmp(ne->name, "window-unlinked") == 0)
control_notify_window_unlinked(ne->session, ne->window);
break;
case NOTIFY_WINDOW_LINKED:
if (strcmp(ne->name, "window-linked") == 0)
control_notify_window_linked(ne->session, ne->window);
break;
case NOTIFY_WINDOW_RENAMED:
if (strcmp(ne->name, "window-renamed") == 0)
control_notify_window_renamed(ne->window);
break;
case NOTIFY_ATTACHED_SESSION_CHANGED:
control_notify_attached_session_changed(ne->client);
break;
case NOTIFY_SESSION_RENAMED:
if (strcmp(ne->name, "client-session-changed") == 0)
control_notify_client_session_changed(ne->client);
if (strcmp(ne->name, "session-renamed") == 0)
control_notify_session_renamed(ne->session);
break;
case NOTIFY_SESSION_CREATED:
if (strcmp(ne->name, "session-created") == 0)
control_notify_session_created(ne->session);
break;
case NOTIFY_SESSION_CLOSED:
if (strcmp(ne->name, "session-closed") == 0)
control_notify_session_closed(ne->session);
break;
}
notify_hook(item, ne);
if (ne->client != NULL)
@ -138,24 +102,35 @@ notify_callback(struct cmdq_item *item, void *data)
session_unref(ne->session);
if (ne->window != NULL)
window_remove_ref(ne->window);
if (ne->fs.s != NULL)
session_unref(ne->fs.s);
free((void *)ne->name);
free(ne);
return (CMD_RETURN_NORMAL);
}
static void
notify_add(enum notify_type type, struct client *c, struct session *s,
struct window *w)
notify_add(const char *name, struct client *c, struct session *s,
struct window *w, struct window_pane *wp)
{
struct notify_entry *ne;
struct cmdq_item *new_item;
ne = xcalloc(1, sizeof *ne);
ne->type = type;
ne->name = xstrdup(name);
ne->client = c;
ne->session = s;
ne->window = w;
if (wp != NULL)
ne->pane = wp->id;
else
ne->pane = -1;
if (c != NULL)
c->references++;
if (s != NULL)
@ -163,6 +138,20 @@ notify_add(enum notify_type type, struct client *c, struct session *s,
if (w != NULL)
w->references++;
cmd_find_clear_state(&ne->fs, NULL, 0);
if (s != NULL && w != NULL)
cmd_find_from_session_window(&ne->fs, s, w);
else if (w != NULL)
cmd_find_from_window(&ne->fs, w);
else if (s != NULL && session_alive(s))
cmd_find_from_session(&ne->fs, s);
else if (wp != NULL)
cmd_find_from_pane(&ne->fs, wp);
else
cmd_find_current(&ne->fs, NULL, CMD_FIND_QUIET);
if (ne->fs.s != NULL)
ne->fs.s->references++; /* cmd_find_valid_state need session */
new_item = cmdq_get_callback(notify_callback, ne);
cmdq_append(NULL, new_item);
}
@ -179,49 +168,31 @@ notify_input(struct window_pane *wp, struct evbuffer *input)
}
void
notify_window_layout_changed(struct window *w)
notify_client(const char *name, struct client *c)
{
notify_add(NOTIFY_WINDOW_LAYOUT_CHANGED, NULL, NULL, w);
notify_add(name, c, NULL, NULL, NULL);
}
void
notify_window_unlinked(struct session *s, struct window *w)
notify_session(const char *name, struct session *s)
{
notify_add(NOTIFY_WINDOW_UNLINKED, NULL, s, w);
notify_add(name, NULL, s, NULL, NULL);
}
void
notify_window_linked(struct session *s, struct window *w)
notify_session_window(const char *name, struct session *s, struct window *w)
{
notify_add(NOTIFY_WINDOW_LINKED, NULL, s, w);
notify_add(name, NULL, s, w, NULL);
}
void
notify_window_renamed(struct window *w)
notify_window(const char *name, struct window *w)
{
notify_add(NOTIFY_WINDOW_RENAMED, NULL, NULL, w);
notify_add(name, NULL, NULL, w, NULL);
}
void
notify_attached_session_changed(struct client *c)
notify_pane(const char *name, struct window_pane *wp)
{
notify_add(NOTIFY_ATTACHED_SESSION_CHANGED, c, NULL, NULL);
}
void
notify_session_renamed(struct session *s)
{
notify_add(NOTIFY_SESSION_RENAMED, NULL, s, NULL);
}
void
notify_session_created(struct session *s)
{
notify_add(NOTIFY_SESSION_CREATED, NULL, s, NULL);
}
void
notify_session_closed(struct session *s)
{
notify_add(NOTIFY_SESSION_CLOSED, NULL, s, NULL);
notify_add(name, NULL, NULL, NULL, wp);
}

View File

@ -156,6 +156,6 @@ recalculate_sizes(void)
}
server_redraw_window(w);
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
}
}

View File

@ -292,7 +292,7 @@ server_client_detach(struct client *c, enum msgtype msgtype)
if (s == NULL)
return;
hooks_run(c->session->hooks, c, NULL, "client-detached");
notify_client("client-detached", c);
proc_send_s(c->peer, msgtype, s->name);
}
@ -1210,7 +1210,7 @@ server_client_dispatch(struct imsg *imsg, void *arg)
server_redraw_client(c);
}
if (c->session != NULL)
hooks_run(c->session->hooks, c, NULL, "client-resized");
notify_client("client-resized", c);
break;
case MSG_EXITING:
if (datalen != 0)

View File

@ -258,7 +258,8 @@ server_link_window(struct session *src, struct winlink *srcwl,
* Can't use session_detach as it will destroy session
* if this makes it empty.
*/
notify_window_unlinked(dst, dstwl->window);
notify_session_window("window-unlinked", dst,
dstwl->window);
dstwl->flags &= ~WINLINK_ALERTFLAGS;
winlink_stack_remove(&dst->lastw, dstwl);
winlink_remove(&dst->windows, dstwl);
@ -294,13 +295,12 @@ server_unlink_window(struct session *s, struct winlink *wl)
}
void
server_destroy_pane(struct window_pane *wp, int hooks)
server_destroy_pane(struct window_pane *wp, int notify)
{
struct window *w = wp->window;
int old_fd;
struct screen_write_ctx ctx;
struct grid_cell gc;
struct cmd_find_state fs;
old_fd = wp->fd;
if (wp->fd != -1) {
@ -312,6 +312,10 @@ server_destroy_pane(struct window_pane *wp, int hooks)
if (options_get_number(w->options, "remain-on-exit")) {
if (old_fd == -1)
return;
if (notify)
notify_pane("pane-died", wp);
screen_write_start(&ctx, wp, &wp->base);
screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
@ -322,18 +326,16 @@ server_destroy_pane(struct window_pane *wp, int hooks)
screen_write_stop(&ctx);
wp->flags |= PANE_REDRAW;
if (hooks && cmd_find_from_pane(&fs, wp) == 0)
hooks_run(hooks_get(fs.s), NULL, &fs, "pane-died");
return;
}
if (notify)
notify_pane("pane-exited", wp);
server_unzoom_window(w);
layout_close_pane(wp);
window_remove_pane(w, wp);
if (hooks && cmd_find_from_window(&fs, w) == 0)
hooks_run(hooks_get(fs.s), NULL, &fs, "pane-exited");
if (TAILQ_EMPTY(&w->panes))
server_kill_window(w);
else
@ -394,7 +396,7 @@ server_destroy_session(struct session *s)
c->session = s_new;
server_client_set_key_table(c, NULL);
status_timer_start(c);
notify_attached_session_changed(c);
notify_client("client-session-changed", c);
session_update_activity(s_new, NULL);
gettimeofday(&s_new->last_attached_time, NULL);
server_redraw_client(c);

View File

@ -169,7 +169,7 @@ session_create(const char *name, int argc, char **argv, const char *path,
}
log_debug("session %s created", s->name);
notify_session_created(s);
notify_session("session-created", s);
return (s);
}
@ -214,7 +214,7 @@ session_destroy(struct session *s)
s->curw = NULL;
RB_REMOVE(sessions, &sessions, s);
notify_session_closed(s);
notify_session("session-closed", s);
free(s->tio);
@ -227,7 +227,7 @@ session_destroy(struct session *s)
winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
while (!RB_EMPTY(&s->windows)) {
wl = RB_ROOT(&s->windows);
notify_window_unlinked(s, wl->window);
notify_session_window("window-unlinked", s, wl->window);
winlink_remove(&s->windows, wl);
}
@ -357,7 +357,7 @@ session_new(struct session *s, const char *name, int argc, char **argv,
return (NULL);
}
winlink_set_window(wl, w);
notify_window_linked(s, w);
notify_session_window("window-linked", s, w);
environ_free(env);
session_group_synchronize_from(s);
@ -375,7 +375,7 @@ session_attach(struct session *s, struct window *w, int idx, char **cause)
return (NULL);
}
winlink_set_window(wl, w);
notify_window_linked(s, w);
notify_session_window("window-linked", s, w);
session_group_synchronize_from(s);
return (wl);
@ -391,7 +391,7 @@ session_detach(struct session *s, struct winlink *wl)
session_next(s, 0);
wl->flags &= ~WINLINK_ALERTFLAGS;
notify_window_unlinked(s, wl->window);
notify_session_window("window-unlinked", s, wl->window);
winlink_stack_remove(&s->lastw, wl);
winlink_remove(&s->windows, wl);
@ -681,7 +681,7 @@ session_group_synchronize1(struct session *target, struct session *s)
RB_FOREACH(wl, winlinks, ww) {
wl2 = winlink_add(&s->windows, wl->idx);
winlink_set_window(wl2, wl->window);
notify_window_linked(s, wl2->window);
notify_session_window("window-linked", s, wl2->window);
wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
}
@ -705,7 +705,7 @@ session_group_synchronize1(struct session *target, struct session *s)
wl = RB_ROOT(&old_windows);
wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
if (wl2 == NULL)
notify_window_unlinked(s, wl->window);
notify_session_window("window-unlinked", s, wl->window);
winlink_remove(&old_windows, wl);
}
}

7
tmux.1
View File

@ -3238,6 +3238,8 @@ Run when a client is attached.
Run when a client is detached
.It client-resized
Run when a client is resized.
.It client-session-changed
Run when a client's attached session is changed.
.It pane-died
Run when the program running in a pane exits, but
.Ic remain-on-exit
@ -3250,8 +3252,12 @@ Run when a new session created.
Run when a session closed.
.It session-renamed
Run when a session is renamed.
.It window-linked
Run when a window is linked into a session.
.It window-renamed
Run when a window is renamed.
.It window-unlinked
Run when a window is unlinked from a session.
.El
.Pp
Hooks are managed with these commands:
@ -3471,6 +3477,7 @@ The following variables are available, where appropriate:
.It Li "history_limit" Ta "" Ta "Maximum window history lines"
.It Li "history_size" Ta "" Ta "Size of history in bytes"
.It Li "hook" Ta "" Ta "Name of running hook, if any"
.It Li "hook_pane" Ta "" Ta "ID of pane where hook was run, if any"
.It Li "hook_session" Ta "" Ta "ID of session where hook was run, if any"
.It Li "hook_session_name" Ta "" Ta "Name of session where hook was run, if any"
.It Li "hook_window" Ta "" Ta "ID of window where hook was run, if any"

15
tmux.h
View File

@ -1583,14 +1583,11 @@ enum mode_key_cmd mode_key_lookup(struct mode_key_data *, key_code);
/* notify.c */
void notify_input(struct window_pane *, struct evbuffer *);
void notify_window_layout_changed(struct window *);
void notify_window_unlinked(struct session *, struct window *);
void notify_window_linked(struct session *, struct window *);
void notify_window_renamed(struct window *);
void notify_attached_session_changed(struct client *);
void notify_session_renamed(struct session *);
void notify_session_created(struct session *);
void notify_session_closed(struct session *);
void notify_client(const char *, struct client *);
void notify_session(const char *, struct session *);
void notify_session_window(const char *, struct session *, struct window *);
void notify_window(const char *, struct window *);
void notify_pane(const char *, struct window_pane *);
/* options.c */
struct options *options_create(struct options *);
@ -2209,7 +2206,7 @@ void control_notify_window_layout_changed(struct window *);
void control_notify_window_unlinked(struct session *, struct window *);
void control_notify_window_linked(struct session *, struct window *);
void control_notify_window_renamed(struct window *);
void control_notify_attached_session_changed(struct client *);
void control_notify_client_session_changed(struct client *);
void control_notify_session_renamed(struct session *);
void control_notify_session_created(struct session *);
void control_notify_session_closed(struct session *);

View File

@ -390,7 +390,7 @@ window_set_name(struct window *w, const char *new_name)
{
free(w->name);
w->name = xstrdup(new_name);
notify_window_renamed(w);
notify_window("window-renamed", w);
}
void
@ -532,7 +532,7 @@ window_zoom(struct window_pane *wp)
w->saved_layout_root = w->layout_root;
layout_init(w, wp);
w->flags |= WINDOW_ZOOMED;
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
return (0);
}
@ -555,7 +555,7 @@ window_unzoom(struct window *w)
wp->saved_layout_cell = NULL;
}
layout_fix_panes(w, w->sx, w->sy);
notify_window_layout_changed(w);
notify_window("window-layout-changed", w);
return (0);
}