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_formats(struct cmdq_item *item, struct session *s, struct window *w, 39 int pane) 40 { 41 if (s != NULL) { 42 cmdq_format(item, "hook_session", "$%u", s->id); 43 cmdq_format(item, "hook_session_name", "%s", s->name); 44 } 45 if (w != NULL) { 46 cmdq_format(item, "hook_window", "@%u", w->id); 47 cmdq_format(item, "hook_window_name", "%s", w->name); 48 } 49 if (pane != -1) 50 cmdq_format(item, "hook_pane", "%%%d", pane); 51 } 52 53 static void 54 notify_insert_hook(struct cmdq_item *item, struct notify_entry *ne) 55 { 56 struct cmd_find_state fs; 57 struct options *oo; 58 struct cmdq_item *new_item; 59 struct session *s = ne->session; 60 struct window *w = ne->window; 61 struct options_entry *o; 62 struct options_array_item *a; 63 struct cmd_list *cmdlist; 64 65 log_debug("%s: %s", __func__, ne->name); 66 67 cmd_find_clear_state(&fs, 0); 68 if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs)) 69 cmd_find_from_nothing(&fs, 0); 70 else 71 cmd_find_copy_state(&fs, &ne->fs); 72 73 if (fs.s == NULL) 74 oo = global_s_options; 75 else 76 oo = fs.s->options; 77 o = options_get(oo, ne->name); 78 if (o == NULL) 79 return; 80 81 a = options_array_first(o); 82 while (a != NULL) { 83 cmdlist = options_array_item_value(a)->cmdlist; 84 if (cmdlist == NULL) { 85 a = options_array_next(a); 86 continue; 87 } 88 89 new_item = cmdq_get_command(cmdlist, &fs, NULL, CMDQ_NOHOOKS); 90 cmdq_format(new_item, "hook", "%s", ne->name); 91 notify_hook_formats(new_item, s, w, ne->pane); 92 93 cmdq_insert_after(item, new_item); 94 item = new_item; 95 96 a = options_array_next(a); 97 } 98 } 99 100 static enum cmd_retval 101 notify_callback(struct cmdq_item *item, void *data) 102 { 103 struct notify_entry *ne = data; 104 105 log_debug("%s: %s", __func__, ne->name); 106 107 if (strcmp(ne->name, "pane-mode-changed") == 0) 108 control_notify_pane_mode_changed(ne->pane); 109 if (strcmp(ne->name, "window-layout-changed") == 0) 110 control_notify_window_layout_changed(ne->window); 111 if (strcmp(ne->name, "window-pane-changed") == 0) 112 control_notify_window_pane_changed(ne->window); 113 if (strcmp(ne->name, "window-unlinked") == 0) 114 control_notify_window_unlinked(ne->session, ne->window); 115 if (strcmp(ne->name, "window-linked") == 0) 116 control_notify_window_linked(ne->session, ne->window); 117 if (strcmp(ne->name, "window-renamed") == 0) 118 control_notify_window_renamed(ne->window); 119 if (strcmp(ne->name, "client-session-changed") == 0) 120 control_notify_client_session_changed(ne->client); 121 if (strcmp(ne->name, "session-renamed") == 0) 122 control_notify_session_renamed(ne->session); 123 if (strcmp(ne->name, "session-created") == 0) 124 control_notify_session_created(ne->session); 125 if (strcmp(ne->name, "session-closed") == 0) 126 control_notify_session_closed(ne->session); 127 if (strcmp(ne->name, "session-window-changed") == 0) 128 control_notify_session_window_changed(ne->session); 129 130 notify_insert_hook(item, ne); 131 132 if (ne->client != NULL) 133 server_client_unref(ne->client); 134 if (ne->session != NULL) 135 session_remove_ref(ne->session, __func__); 136 if (ne->window != NULL) 137 window_remove_ref(ne->window, __func__); 138 139 if (ne->fs.s != NULL) 140 session_remove_ref(ne->fs.s, __func__); 141 142 free(__UNCONST(ne->name)); 143 free(ne); 144 145 return (CMD_RETURN_NORMAL); 146 } 147 148 static void 149 notify_add(const char *name, struct cmd_find_state *fs, struct client *c, 150 struct session *s, struct window *w, struct window_pane *wp) 151 { 152 struct notify_entry *ne; 153 struct cmdq_item *new_item; 154 155 ne = xcalloc(1, sizeof *ne); 156 ne->name = xstrdup(name); 157 158 ne->client = c; 159 ne->session = s; 160 ne->window = w; 161 162 if (wp != NULL) 163 ne->pane = wp->id; 164 else 165 ne->pane = -1; 166 167 if (c != NULL) 168 c->references++; 169 if (s != NULL) 170 session_add_ref(s, __func__); 171 if (w != NULL) 172 window_add_ref(w, __func__); 173 174 cmd_find_copy_state(&ne->fs, fs); 175 if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */ 176 session_add_ref(ne->fs.s, __func__); 177 178 new_item = cmdq_get_callback(notify_callback, ne); 179 cmdq_append(NULL, new_item); 180 } 181 182 void 183 notify_hook(struct cmdq_item *item, const char *name) 184 { 185 struct notify_entry ne; 186 187 memset(&ne, 0, sizeof ne); 188 189 ne.name = name; 190 cmd_find_copy_state(&ne.fs, &item->target); 191 192 ne.client = item->client; 193 ne.session = item->target.s; 194 ne.window = item->target.w; 195 ne.pane = item->target.wp->id; 196 197 notify_insert_hook(item, &ne); 198 } 199 200 void 201 notify_input(struct window_pane *wp, const u_char *buf, size_t len) 202 { 203 struct client *c; 204 205 TAILQ_FOREACH(c, &clients, entry) { 206 if (c->flags & CLIENT_CONTROL) 207 control_notify_input(c, wp, buf, len); 208 } 209 } 210 211 void 212 notify_client(const char *name, struct client *c) 213 { 214 struct cmd_find_state fs; 215 216 cmd_find_from_client(&fs, c, 0); 217 notify_add(name, &fs, c, NULL, NULL, NULL); 218 } 219 220 void 221 notify_session(const char *name, struct session *s) 222 { 223 struct cmd_find_state fs; 224 225 if (session_alive(s)) 226 cmd_find_from_session(&fs, s, 0); 227 else 228 cmd_find_from_nothing(&fs, 0); 229 notify_add(name, &fs, NULL, s, NULL, NULL); 230 } 231 232 void 233 notify_winlink(const char *name, struct winlink *wl) 234 { 235 struct cmd_find_state fs; 236 237 cmd_find_from_winlink(&fs, wl, 0); 238 notify_add(name, &fs, NULL, wl->session, wl->window, NULL); 239 } 240 241 void 242 notify_session_window(const char *name, struct session *s, struct window *w) 243 { 244 struct cmd_find_state fs; 245 246 cmd_find_from_session_window(&fs, s, w, 0); 247 notify_add(name, &fs, NULL, s, w, NULL); 248 } 249 250 void 251 notify_window(const char *name, struct window *w) 252 { 253 struct cmd_find_state fs; 254 255 cmd_find_from_window(&fs, w, 0); 256 notify_add(name, &fs, NULL, NULL, w, NULL); 257 } 258 259 void 260 notify_pane(const char *name, struct window_pane *wp) 261 { 262 struct cmd_find_state fs; 263 264 cmd_find_from_pane(&fs, wp, 0); 265 notify_add(name, &fs, NULL, NULL, NULL, wp); 266 } 267