1 /* $OpenBSD$ */ 2 3 /* 4 * Copyright (c) 2012 George Nachman <tmux@georgester.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "tmux.h" 25 26 struct notify_entry { 27 const char *name; 28 29 struct client *client; 30 struct session *session; 31 struct window *window; 32 int pane; 33 34 struct cmd_find_state fs; 35 }; 36 37 static void 38 notify_hook(struct cmdq_item *item, struct notify_entry *ne) 39 { 40 struct cmd_find_state fs; 41 struct hook *hook; 42 struct cmdq_item *new_item; 43 struct session *s = ne->session; 44 struct window *w = ne->window; 45 46 cmd_find_clear_state(&fs, 0); 47 if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs)) 48 cmd_find_from_nothing(&fs, 0); 49 else 50 cmd_find_copy_state(&fs, &ne->fs); 51 52 hook = hooks_find(hooks_get(fs.s), ne->name); 53 if (hook == NULL) 54 return; 55 log_debug("notify hook %s", ne->name); 56 57 new_item = cmdq_get_command(hook->cmdlist, &fs, NULL, CMDQ_NOHOOKS); 58 cmdq_format(new_item, "hook", "%s", ne->name); 59 60 if (s != NULL) { 61 cmdq_format(new_item, "hook_session", "$%u", s->id); 62 cmdq_format(new_item, "hook_session_name", "%s", s->name); 63 } 64 if (w != NULL) { 65 cmdq_format(new_item, "hook_window", "@%u", w->id); 66 cmdq_format(new_item, "hook_window_name", "%s", w->name); 67 } 68 if (ne->pane != -1) 69 cmdq_format(new_item, "hook_pane", "%%%d", ne->pane); 70 71 cmdq_insert_after(item, new_item); 72 } 73 74 static enum cmd_retval 75 notify_callback(struct cmdq_item *item, void *data) 76 { 77 struct notify_entry *ne = data; 78 79 log_debug("%s: %s", __func__, ne->name); 80 81 if (strcmp(ne->name, "pane-mode-changed") == 0) 82 control_notify_pane_mode_changed(ne->pane); 83 if (strcmp(ne->name, "window-layout-changed") == 0) 84 control_notify_window_layout_changed(ne->window); 85 if (strcmp(ne->name, "window-pane-changed") == 0) 86 control_notify_window_pane_changed(ne->window); 87 if (strcmp(ne->name, "window-unlinked") == 0) 88 control_notify_window_unlinked(ne->session, ne->window); 89 if (strcmp(ne->name, "window-linked") == 0) 90 control_notify_window_linked(ne->session, ne->window); 91 if (strcmp(ne->name, "window-renamed") == 0) 92 control_notify_window_renamed(ne->window); 93 if (strcmp(ne->name, "client-session-changed") == 0) 94 control_notify_client_session_changed(ne->client); 95 if (strcmp(ne->name, "session-renamed") == 0) 96 control_notify_session_renamed(ne->session); 97 if (strcmp(ne->name, "session-created") == 0) 98 control_notify_session_created(ne->session); 99 if (strcmp(ne->name, "session-closed") == 0) 100 control_notify_session_closed(ne->session); 101 if (strcmp(ne->name, "session-window-changed") == 0) 102 control_notify_session_window_changed(ne->session); 103 104 notify_hook(item, ne); 105 106 if (ne->client != NULL) 107 server_client_unref(ne->client); 108 if (ne->session != NULL) 109 session_remove_ref(ne->session, __func__); 110 if (ne->window != NULL) 111 window_remove_ref(ne->window, __func__); 112 113 if (ne->fs.s != NULL) 114 session_remove_ref(ne->fs.s, __func__); 115 116 free(__UNCONST(ne->name)); 117 free(ne); 118 119 return (CMD_RETURN_NORMAL); 120 } 121 122 static void 123 notify_add(const char *name, struct cmd_find_state *fs, struct client *c, 124 struct session *s, struct window *w, struct window_pane *wp) 125 { 126 struct notify_entry *ne; 127 struct cmdq_item *new_item; 128 129 ne = xcalloc(1, sizeof *ne); 130 ne->name = xstrdup(name); 131 132 ne->client = c; 133 ne->session = s; 134 ne->window = w; 135 136 if (wp != NULL) 137 ne->pane = wp->id; 138 else 139 ne->pane = -1; 140 141 if (c != NULL) 142 c->references++; 143 if (s != NULL) 144 session_add_ref(s, __func__); 145 if (w != NULL) 146 window_add_ref(w, __func__); 147 148 cmd_find_copy_state(&ne->fs, fs); 149 if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */ 150 session_add_ref(ne->fs.s, __func__); 151 152 new_item = cmdq_get_callback(notify_callback, ne); 153 cmdq_append(NULL, new_item); 154 } 155 156 void 157 notify_input(struct window_pane *wp, struct evbuffer *input) 158 { 159 struct client *c; 160 161 TAILQ_FOREACH(c, &clients, entry) { 162 if (c->flags & CLIENT_CONTROL) 163 control_notify_input(c, wp, input); 164 } 165 } 166 167 void 168 notify_client(const char *name, struct client *c) 169 { 170 struct cmd_find_state fs; 171 172 cmd_find_from_client(&fs, c, 0); 173 notify_add(name, &fs, c, NULL, NULL, NULL); 174 } 175 176 void 177 notify_session(const char *name, struct session *s) 178 { 179 struct cmd_find_state fs; 180 181 if (session_alive(s)) 182 cmd_find_from_session(&fs, s, 0); 183 else 184 cmd_find_from_nothing(&fs, 0); 185 notify_add(name, &fs, NULL, s, NULL, NULL); 186 } 187 188 void 189 notify_winlink(const char *name, struct winlink *wl) 190 { 191 struct cmd_find_state fs; 192 193 cmd_find_from_winlink(&fs, wl, 0); 194 notify_add(name, &fs, NULL, wl->session, wl->window, NULL); 195 } 196 197 void 198 notify_session_window(const char *name, struct session *s, struct window *w) 199 { 200 struct cmd_find_state fs; 201 202 cmd_find_from_session_window(&fs, s, w, 0); 203 notify_add(name, &fs, NULL, s, w, NULL); 204 } 205 206 void 207 notify_window(const char *name, struct window *w) 208 { 209 struct cmd_find_state fs; 210 211 cmd_find_from_window(&fs, w, 0); 212 notify_add(name, &fs, NULL, NULL, w, NULL); 213 } 214 215 void 216 notify_pane(const char *name, struct window_pane *wp) 217 { 218 struct cmd_find_state fs; 219 220 cmd_find_from_pane(&fs, wp, 0); 221 notify_add(name, &fs, NULL, NULL, NULL, wp); 222 } 223