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