2012-03-17 22:35:09 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-07-13 06:27:41 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-10-16 22:06:40 +00:00
|
|
|
#include <string.h>
|
2012-07-13 06:27:41 +00:00
|
|
|
|
2012-03-17 22:35:09 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
2012-07-13 06:27:41 +00:00
|
|
|
struct notify_entry {
|
2016-10-16 22:06:40 +00:00
|
|
|
const char *name;
|
2012-07-13 06:27:41 +00:00
|
|
|
|
|
|
|
struct client *client;
|
|
|
|
struct session *session;
|
|
|
|
struct window *window;
|
2016-10-16 22:06:40 +00:00
|
|
|
int pane;
|
|
|
|
|
|
|
|
struct cmd_find_state fs;
|
2012-07-13 06:27:41 +00:00
|
|
|
};
|
|
|
|
|
2016-10-15 00:09:30 +00:00
|
|
|
static void
|
2016-10-16 19:15:02 +00:00
|
|
|
notify_hook(struct cmdq_item *item, struct notify_entry *ne)
|
2016-10-15 00:09:30 +00:00
|
|
|
{
|
|
|
|
struct cmd_find_state fs;
|
|
|
|
struct hook *hook;
|
2016-10-16 19:36:37 +00:00
|
|
|
struct cmdq_item *new_item;
|
2016-10-16 19:55:52 +00:00
|
|
|
struct session *s = ne->session;
|
|
|
|
struct window *w = ne->window;
|
2016-10-15 00:09:30 +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-04-21 20:26:34 +00:00
|
|
|
cmd_find_from_nothing(&fs);
|
2016-10-16 22:06:40 +00:00
|
|
|
else
|
|
|
|
cmd_find_copy_state(&fs, &ne->fs);
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2016-10-16 22:06:40 +00:00
|
|
|
hook = hooks_find(hooks_get(fs.s), ne->name);
|
2016-10-15 00:09:30 +00:00
|
|
|
if (hook == NULL)
|
|
|
|
return;
|
2016-10-16 22:06:40 +00:00
|
|
|
log_debug("notify hook %s", ne->name);
|
2016-10-15 00:09:30 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
new_item = cmdq_get_command(hook->cmdlist, &fs, NULL, CMDQ_NOHOOKS);
|
2016-10-16 22:06:40 +00:00
|
|
|
cmdq_format(new_item, "hook", "%s", ne->name);
|
2016-10-16 19:55:52 +00:00
|
|
|
|
|
|
|
if (s != NULL) {
|
|
|
|
cmdq_format(new_item, "hook_session", "$%u", s->id);
|
|
|
|
cmdq_format(new_item, "hook_session_name", "%s", s->name);
|
|
|
|
}
|
|
|
|
if (w != NULL) {
|
|
|
|
cmdq_format(new_item, "hook_window", "@%u", w->id);
|
|
|
|
cmdq_format(new_item, "hook_window_name", "%s", w->name);
|
|
|
|
}
|
2016-10-16 22:06:40 +00:00
|
|
|
if (ne->pane != -1)
|
|
|
|
cmdq_format(new_item, "hook_pane", "%%%d", ne->pane);
|
2016-10-16 19:55:52 +00:00
|
|
|
|
2016-10-16 19:15:02 +00:00
|
|
|
cmdq_insert_after(item, new_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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);
|
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);
|
|
|
|
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);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
2016-10-16 19:15:02 +00:00
|
|
|
notify_hook(item, ne);
|
|
|
|
|
|
|
|
if (ne->client != NULL)
|
|
|
|
server_client_unref(ne->client);
|
|
|
|
if (ne->session != NULL)
|
|
|
|
session_unref(ne->session);
|
|
|
|
if (ne->window != NULL)
|
|
|
|
window_remove_ref(ne->window);
|
2016-10-16 22:06:40 +00:00
|
|
|
|
|
|
|
if (ne->fs.s != NULL)
|
|
|
|
session_unref(ne->fs.s);
|
|
|
|
|
|
|
|
free((void *)ne->name);
|
2016-10-16 19:15:02 +00:00
|
|
|
free(ne);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
2016-10-15 00:09:30 +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,
|
|
|
|
struct session *s, struct window *w, struct window_pane *wp)
|
2012-07-13 06:27:41 +00:00
|
|
|
{
|
|
|
|
struct notify_entry *ne;
|
2016-10-16 19:15:02 +00:00
|
|
|
struct cmdq_item *new_item;
|
2012-07-13 06:27:41 +00:00
|
|
|
|
|
|
|
ne = xcalloc(1, sizeof *ne);
|
2016-10-16 22:06:40 +00:00
|
|
|
ne->name = xstrdup(name);
|
|
|
|
|
2012-07-13 06:27:41 +00:00
|
|
|
ne->client = c;
|
|
|
|
ne->session = s;
|
|
|
|
ne->window = w;
|
|
|
|
|
2016-10-16 22:06:40 +00:00
|
|
|
if (wp != NULL)
|
|
|
|
ne->pane = wp->id;
|
|
|
|
else
|
|
|
|
ne->pane = -1;
|
|
|
|
|
2012-07-13 06:27:41 +00:00
|
|
|
if (c != NULL)
|
|
|
|
c->references++;
|
|
|
|
if (s != NULL)
|
|
|
|
s->references++;
|
|
|
|
if (w != NULL)
|
|
|
|
w->references++;
|
|
|
|
|
2016-10-16 22:18:04 +00:00
|
|
|
cmd_find_copy_state(&ne->fs, fs);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (ne->fs.s != NULL)
|
|
|
|
ne->fs.s->references++; /* cmd_find_valid_state need session */
|
|
|
|
|
2016-10-16 19:15:02 +00:00
|
|
|
new_item = cmdq_get_callback(notify_callback, ne);
|
|
|
|
cmdq_append(NULL, new_item);
|
2012-09-25 07:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notify_input(struct window_pane *wp, struct evbuffer *input)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->flags & CLIENT_CONTROL)
|
2012-09-25 07:41:22 +00:00
|
|
|
control_notify_input(c, wp, input);
|
2012-07-13 06:27:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 22:35:09 +00:00
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_client(const char *name, struct client *c)
|
2012-03-17 22:35:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-04-21 20:26:34 +00:00
|
|
|
cmd_find_from_client(&fs, c);
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_add(name, &fs, c, NULL, NULL, NULL);
|
2012-03-17 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_session(const char *name, struct session *s)
|
2012-03-17 22:35:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
|
|
|
if (session_alive(s))
|
|
|
|
cmd_find_from_session(&fs, s);
|
|
|
|
else
|
2017-04-21 20:26:34 +00:00
|
|
|
cmd_find_from_nothing(&fs);
|
2016-10-16 22:18:04 +00:00
|
|
|
notify_add(name, &fs, NULL, s, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-04-21 14:09:44 +00:00
|
|
|
notify_winlink(const char *name, struct winlink *wl)
|
2016-10-16 22:18:04 +00:00
|
|
|
{
|
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
2017-04-21 14:09:44 +00:00
|
|
|
cmd_find_from_winlink(&fs, wl);
|
|
|
|
notify_add(name, &fs, NULL, wl->session, wl->window, NULL);
|
2012-03-17 22:35: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-17 22:35:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
|
|
|
cmd_find_from_session_window(&fs, s, w);
|
|
|
|
notify_add(name, &fs, NULL, s, w, NULL);
|
2012-03-17 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_window(const char *name, struct window *w)
|
2012-03-17 22:35:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
|
|
|
cmd_find_from_window(&fs, w);
|
|
|
|
notify_add(name, &fs, NULL, NULL, w, NULL);
|
2012-03-17 22:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_pane(const char *name, struct window_pane *wp)
|
2012-03-17 22:35:09 +00:00
|
|
|
{
|
2016-10-16 22:18:04 +00:00
|
|
|
struct cmd_find_state fs;
|
|
|
|
|
|
|
|
cmd_find_from_pane(&fs, wp);
|
|
|
|
notify_add(name, &fs, NULL, NULL, NULL, wp);
|
2012-03-17 22:35:09 +00:00
|
|
|
}
|