2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2012-03-18 02:22:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 George Nachman <tmux@georgester.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.
|
|
|
|
*/
|
|
|
|
|
2012-08-12 19:23:01 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-10-16 22:06:40 +00:00
|
|
|
#include <string.h>
|
2012-08-12 19:23:01 +00:00
|
|
|
|
2012-03-18 02:22:09 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
2012-08-12 19:23:01 +00:00
|
|
|
struct notify_entry {
|
2016-10-16 22:06:40 +00:00
|
|
|
const char *name;
|
2021-08-12 08:05:11 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
struct format_tree *formats;
|
2012-08-12 19:23:01 +00:00
|
|
|
|
|
|
|
struct client *client;
|
|
|
|
struct session *session;
|
|
|
|
struct window *window;
|
2016-10-16 22:06:40 +00:00
|
|
|
int pane;
|
2022-08-15 09:10:34 +00:00
|
|
|
const char *pbname;
|
2012-08-12 19:23:01 +00:00
|
|
|
};
|
|
|
|
|
2022-03-08 21:58:37 +00:00
|
|
|
static struct cmdq_item *
|
|
|
|
notify_insert_one_hook(struct cmdq_item *item, struct notify_entry *ne,
|
|
|
|
struct cmd_list *cmdlist, struct cmdq_state *state)
|
|
|
|
{
|
|
|
|
struct cmdq_item *new_item;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (cmdlist == NULL)
|
|
|
|
return (item);
|
|
|
|
if (log_get_level() != 0) {
|
|
|
|
s = cmd_list_print(cmdlist, 0);
|
|
|
|
log_debug("%s: hook %s is: %s", __func__, ne->name, s);
|
2022-04-06 15:39:46 +00:00
|
|
|
free(s);
|
2022-03-08 21:58:37 +00:00
|
|
|
}
|
|
|
|
new_item = cmdq_get_command(cmdlist, state);
|
|
|
|
return (cmdq_insert_after(item, new_item));
|
|
|
|
}
|
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
static void
|
|
|
|
notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne)
|
|
|
|
{
|
|
|
|
struct cmd_find_state fs;
|
|
|
|
struct options *oo;
|
2022-03-08 21:58:37 +00:00
|
|
|
struct cmdq_state *state;
|
2019-04-26 11:38:51 +00:00
|
|
|
struct options_entry *o;
|
|
|
|
struct options_array_item *a;
|
|
|
|
struct cmd_list *cmdlist;
|
2022-03-08 21:58:37 +00:00
|
|
|
const char *value;
|
|
|
|
struct cmd_parse_result *pr;
|
2019-04-26 11:38:51 +00:00
|
|
|
|
2022-03-08 21:58:37 +00:00
|
|
|
log_debug("%s: inserting hook %s", __func__, ne->name);
|
2012-08-12 19:23:01 +00:00
|
|
|
|
2017-04-21 20:26:34 +00:00
|
|
|
cmd_find_clear_state(&fs, 0);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_nothing(&fs, 0);
|
2016-10-16 22:06:40 +00:00
|
|
|
else
|
|
|
|
cmd_find_copy_state(&fs, &ne->fs);
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
if (fs.s == NULL)
|
|
|
|
oo = global_s_options;
|
|
|
|
else
|
|
|
|
oo = fs.s->options;
|
|
|
|
o = options_get(oo, ne->name);
|
2020-04-13 07:25:33 +00:00
|
|
|
if (o == NULL && fs.wp != NULL) {
|
|
|
|
oo = fs.wp->options;
|
|
|
|
o = options_get(oo, ne->name);
|
|
|
|
}
|
|
|
|
if (o == NULL && fs.wl != NULL) {
|
|
|
|
oo = fs.wl->window->options;
|
|
|
|
o = options_get(oo, ne->name);
|
|
|
|
}
|
2022-03-08 21:58:37 +00:00
|
|
|
if (o == NULL) {
|
|
|
|
log_debug("%s: hook %s not found", __func__, ne->name);
|
2016-10-15 00:09:30 +00:00
|
|
|
return;
|
2022-03-08 21:58:37 +00:00
|
|
|
}
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2022-03-08 21:58:37 +00:00
|
|
|
state = cmdq_new_state(&fs, NULL, CMDQ_STATE_NOHOOKS);
|
|
|
|
cmdq_add_formats(state, ne->formats);
|
|
|
|
|
|
|
|
if (*ne->name == '@') {
|
|
|
|
value = options_get_string(oo, ne->name);
|
|
|
|
pr = cmd_parse_from_string(value, NULL);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_ERROR:
|
|
|
|
log_debug("%s: can't parse hook %s: %s", __func__,
|
|
|
|
ne->name, pr->error);
|
|
|
|
free(pr->error);
|
|
|
|
break;
|
|
|
|
case CMD_PARSE_SUCCESS:
|
|
|
|
notify_insert_one_hook(item, ne, pr->cmdlist, state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
a = options_array_first(o);
|
|
|
|
while (a != NULL) {
|
|
|
|
cmdlist = options_array_item_value(a)->cmdlist;
|
|
|
|
item = notify_insert_one_hook(item, ne, cmdlist, state);
|
|
|
|
a = options_array_next(a);
|
2019-04-26 11:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 15:55:51 +00:00
|
|
|
|
2022-03-08 21:58:37 +00:00
|
|
|
cmdq_free_state(state);
|
2016-10-16 19:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum cmd_retval
|
|
|
|
notify_callback(struct cmdq_item *item, void *data)
|
|
|
|
{
|
|
|
|
struct notify_entry *ne = data;
|
|
|
|
|
2017-01-11 14:56:44 +00:00
|
|
|
log_debug("%s: %s", __func__, ne->name);
|
|
|
|
|
2017-05-04 07:16:43 +00:00
|
|
|
if (strcmp(ne->name, "pane-mode-changed") == 0)
|
|
|
|
control_notify_pane_mode_changed(ne->pane);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "window-layout-changed") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_window_layout_changed(ne->window);
|
2017-05-04 07:16:43 +00:00
|
|
|
if (strcmp(ne->name, "window-pane-changed") == 0)
|
|
|
|
control_notify_window_pane_changed(ne->window);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "window-unlinked") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_window_unlinked(ne->session, ne->window);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "window-linked") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_window_linked(ne->session, ne->window);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "window-renamed") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_window_renamed(ne->window);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "client-session-changed") == 0)
|
|
|
|
control_notify_client_session_changed(ne->client);
|
2021-03-16 09:14:58 +00:00
|
|
|
if (strcmp(ne->name, "client-detached") == 0)
|
|
|
|
control_notify_client_detached(ne->client);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "session-renamed") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_session_renamed(ne->session);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "session-created") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_session_created(ne->session);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (strcmp(ne->name, "session-closed") == 0)
|
2016-10-16 19:15:02 +00:00
|
|
|
control_notify_session_closed(ne->session);
|
2017-05-04 07:16:43 +00:00
|
|
|
if (strcmp(ne->name, "session-window-changed") == 0)
|
|
|
|
control_notify_session_window_changed(ne->session);
|
2022-08-15 09:10:34 +00:00
|
|
|
if (strcmp(ne->name, "paste-buffer-changed") == 0)
|
|
|
|
control_notify_paste_buffer_changed(ne->pbname);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
notify_insert_hook(item, ne);
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2016-10-16 19:15:02 +00:00
|
|
|
if (ne->client != NULL)
|
|
|
|
server_client_unref(ne->client);
|
|
|
|
if (ne->session != NULL)
|
2017-04-28 19:13:55 +00:00
|
|
|
session_remove_ref(ne->session, __func__);
|
2016-10-16 19:15:02 +00:00
|
|
|
if (ne->window != NULL)
|
2017-04-28 19:13:55 +00:00
|
|
|
window_remove_ref(ne->window, __func__);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
|
|
|
if (ne->fs.s != NULL)
|
2017-04-28 19:13:55 +00:00
|
|
|
session_remove_ref(ne->fs.s, __func__);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
format_free(ne->formats);
|
2016-10-16 22:06:40 +00:00
|
|
|
free((void *)ne->name);
|
2022-08-15 09:10:34 +00:00
|
|
|
free((void *)ne->pbname);
|
2016-10-16 19:15:02 +00:00
|
|
|
free(ne);
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2016-10-16 19:15:02 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2012-08-12 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_add(const char *name, struct cmd_find_state *fs, struct client *c,
|
2022-08-15 09:10:34 +00:00
|
|
|
struct session *s, struct window *w, struct window_pane *wp,
|
|
|
|
const char *pbname)
|
2012-08-12 19:23:01 +00:00
|
|
|
{
|
|
|
|
struct notify_entry *ne;
|
2020-04-14 06:00:52 +00:00
|
|
|
struct cmdq_item *item;
|
|
|
|
|
|
|
|
item = cmdq_running(NULL);
|
|
|
|
if (item != NULL && (cmdq_get_flags(item) & CMDQ_STATE_NOHOOKS))
|
|
|
|
return;
|
2012-08-12 19:23:01 +00:00
|
|
|
|
|
|
|
ne = xcalloc(1, sizeof *ne);
|
2016-10-16 22:06:40 +00:00
|
|
|
ne->name = xstrdup(name);
|
|
|
|
|
2012-08-12 19:23:01 +00:00
|
|
|
ne->client = c;
|
|
|
|
ne->session = s;
|
|
|
|
ne->window = w;
|
2021-08-12 08:05:11 +00:00
|
|
|
ne->pane = (wp != NULL ? wp->id : -1);
|
2022-08-15 09:10:34 +00:00
|
|
|
ne->pbname = (pbname != NULL ? xstrdup(pbname) : NULL);
|
2012-08-12 19:23:01 +00:00
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
ne->formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
|
|
|
|
format_add(ne->formats, "hook", "%s", name);
|
|
|
|
if (c != NULL)
|
|
|
|
format_add(ne->formats, "hook_client", "%s", c->name);
|
|
|
|
if (s != NULL) {
|
|
|
|
format_add(ne->formats, "hook_session", "$%u", s->id);
|
|
|
|
format_add(ne->formats, "hook_session_name", "%s", s->name);
|
|
|
|
}
|
|
|
|
if (w != NULL) {
|
|
|
|
format_add(ne->formats, "hook_window", "@%u", w->id);
|
|
|
|
format_add(ne->formats, "hook_window_name", "%s", w->name);
|
|
|
|
}
|
2016-10-16 22:06:40 +00:00
|
|
|
if (wp != NULL)
|
2021-08-12 08:05:11 +00:00
|
|
|
format_add(ne->formats, "hook_pane", "%%%d", wp->id);
|
|
|
|
format_log_debug(ne->formats, __func__);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
2012-08-12 19:23:01 +00:00
|
|
|
if (c != NULL)
|
|
|
|
c->references++;
|
|
|
|
if (s != NULL)
|
2017-04-28 19:13:55 +00:00
|
|
|
session_add_ref(s, __func__);
|
2012-08-12 19:23:01 +00:00
|
|
|
if (w != NULL)
|
2017-04-28 19:13:55 +00:00
|
|
|
window_add_ref(w, __func__);
|
2012-08-12 19:23:01 +00:00
|
|
|
|
2016-10-16 22:18:04 +00:00
|
|
|
cmd_find_copy_state(&ne->fs, fs);
|
2017-04-28 19:13:55 +00:00
|
|
|
if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */
|
|
|
|
session_add_ref(ne->fs.s, __func__);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
2020-04-14 06:00:52 +00:00
|
|
|
cmdq_append(NULL, cmdq_get_callback(notify_callback, ne));
|
2012-09-25 07:41:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 12:30:52 +00:00
|
|
|
void
|
|
|
|
notify_hook(struct cmdq_item *item, const char *name)
|
|
|
|
{
|
2020-04-13 10:59:58 +00:00
|
|
|
struct cmd_find_state *target = cmdq_get_target(item);
|
|
|
|
struct notify_entry ne;
|
2018-07-04 12:30:52 +00:00
|
|
|
|
|
|
|
memset(&ne, 0, sizeof ne);
|
|
|
|
|
|
|
|
ne.name = name;
|
2020-04-13 10:59:58 +00:00
|
|
|
cmd_find_copy_state(&ne.fs, target);
|
2018-07-04 12:30:52 +00:00
|
|
|
|
2020-04-13 10:59:58 +00:00
|
|
|
ne.client = cmdq_get_client(item);
|
|
|
|
ne.session = target->s;
|
|
|
|
ne.window = target->w;
|
2021-08-12 08:10:20 +00:00
|
|
|
ne.pane = (target->wp != NULL ? target->wp->id : -1);
|
2018-07-04 12:30:52 +00:00
|
|
|
|
2021-08-12 08:05:11 +00:00
|
|
|
ne.formats = format_create(NULL, NULL, 0, FORMAT_NOJOBS);
|
|
|
|
format_add(ne.formats, "hook", "%s", name);
|
|
|
|
format_log_debug(ne.formats, __func__);
|
2018-07-04 12:30:52 +00:00
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
notify_insert_hook(item, &ne);
|
2021-08-12 08:05:11 +00:00
|
|
|
format_free(ne.formats);
|
2018-07-04 12:30:52 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 02:22:09 +00:00
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_client(const char *name, struct client *c)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
2012-03-18 02:22:09 +00:00
|
|
|
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_client(&fs, c, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, c, NULL, NULL, NULL, NULL);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_session(const char *name, struct session *s)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
2012-03-18 02:22:09 +00:00
|
|
|
|
2016-10-16 22:18:04 +00:00
|
|
|
if (session_alive(s))
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_session(&fs, s, 0);
|
2016-10-16 22:18:04 +00:00
|
|
|
else
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_nothing(&fs, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, NULL, s, NULL, NULL, NULL);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-21 14:09:44 +00:00
|
|
|
notify_winlink(const char *name, struct winlink *wl)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_winlink(&fs, wl, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, NULL, wl->session, wl->window, NULL, NULL);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_session_window(const char *name, struct session *s, struct window *w)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_session_window(&fs, s, w, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, NULL, s, w, NULL, NULL);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_window(const char *name, struct window *w)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_window(&fs, w, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, NULL, NULL, w, NULL, NULL);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_pane(const char *name, struct window_pane *wp)
|
2012-03-18 02:22:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-08-30 10:33:57 +00:00
|
|
|
cmd_find_from_pane(&fs, wp, 0);
|
2022-08-15 09:10:34 +00:00
|
|
|
notify_add(name, &fs, NULL, NULL, NULL, wp, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notify_paste_buffer(const char *pbname)
|
|
|
|
{
|
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
|
|
|
cmd_find_clear_state(&fs, 0);
|
|
|
|
notify_add("paste-buffer-changed", &fs, NULL, NULL, NULL, NULL, pbname);
|
2012-03-18 02:22:09 +00:00
|
|
|
}
|