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