Annotation of src/usr.bin/tmux/notify.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: notify.c,v 1.27 2019/04/26 11:38: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.27 nicm 39: notify_hook_formats(struct cmdq_item *item, struct session *s, struct window *w,
40: int pane)
1.11 nicm 41: {
1.27 nicm 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);
1.11 nicm 67:
1.22 nicm 68: cmd_find_clear_state(&fs, 0);
1.18 nicm 69: if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
1.25 nicm 70: cmd_find_from_nothing(&fs, 0);
1.15 nicm 71: else
1.18 nicm 72: cmd_find_copy_state(&fs, &ne->fs);
1.11 nicm 73:
1.27 nicm 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)
1.11 nicm 80: return;
81:
1.27 nicm 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:
94: cmdq_insert_after(item, new_item);
95: item = new_item;
1.17 nicm 96:
1.27 nicm 97: a = options_array_next(a);
1.17 nicm 98: }
1.15 nicm 99: }
100:
101: static enum cmd_retval
102: notify_callback(struct cmdq_item *item, void *data)
103: {
104: struct notify_entry *ne = data;
1.20 nicm 105:
106: log_debug("%s: %s", __func__, ne->name);
1.15 nicm 107:
1.24 nicm 108: if (strcmp(ne->name, "pane-mode-changed") == 0)
109: control_notify_pane_mode_changed(ne->pane);
1.18 nicm 110: if (strcmp(ne->name, "window-layout-changed") == 0)
1.15 nicm 111: control_notify_window_layout_changed(ne->window);
1.24 nicm 112: if (strcmp(ne->name, "window-pane-changed") == 0)
113: control_notify_window_pane_changed(ne->window);
1.18 nicm 114: if (strcmp(ne->name, "window-unlinked") == 0)
1.15 nicm 115: control_notify_window_unlinked(ne->session, ne->window);
1.18 nicm 116: if (strcmp(ne->name, "window-linked") == 0)
1.15 nicm 117: control_notify_window_linked(ne->session, ne->window);
1.18 nicm 118: if (strcmp(ne->name, "window-renamed") == 0)
1.15 nicm 119: control_notify_window_renamed(ne->window);
1.18 nicm 120: if (strcmp(ne->name, "client-session-changed") == 0)
121: control_notify_client_session_changed(ne->client);
122: if (strcmp(ne->name, "session-renamed") == 0)
1.15 nicm 123: control_notify_session_renamed(ne->session);
1.18 nicm 124: if (strcmp(ne->name, "session-created") == 0)
1.15 nicm 125: control_notify_session_created(ne->session);
1.18 nicm 126: if (strcmp(ne->name, "session-closed") == 0)
1.15 nicm 127: control_notify_session_closed(ne->session);
1.24 nicm 128: if (strcmp(ne->name, "session-window-changed") == 0)
129: control_notify_session_window_changed(ne->session);
1.18 nicm 130:
1.27 nicm 131: notify_insert_hook(item, ne);
1.15 nicm 132:
133: if (ne->client != NULL)
134: server_client_unref(ne->client);
135: if (ne->session != NULL)
1.23 nicm 136: session_remove_ref(ne->session, __func__);
1.15 nicm 137: if (ne->window != NULL)
1.23 nicm 138: window_remove_ref(ne->window, __func__);
1.18 nicm 139:
140: if (ne->fs.s != NULL)
1.23 nicm 141: session_remove_ref(ne->fs.s, __func__);
1.18 nicm 142:
143: free((void *)ne->name);
1.15 nicm 144: free(ne);
145:
146: return (CMD_RETURN_NORMAL);
1.11 nicm 147: }
148:
149: static void
1.19 nicm 150: notify_add(const char *name, struct cmd_find_state *fs, struct client *c,
151: struct session *s, struct window *w, struct window_pane *wp)
1.2 nicm 152: {
153: struct notify_entry *ne;
1.15 nicm 154: struct cmdq_item *new_item;
1.2 nicm 155:
156: ne = xcalloc(1, sizeof *ne);
1.18 nicm 157: ne->name = xstrdup(name);
158:
1.2 nicm 159: ne->client = c;
160: ne->session = s;
161: ne->window = w;
162:
1.18 nicm 163: if (wp != NULL)
164: ne->pane = wp->id;
165: else
166: ne->pane = -1;
167:
1.2 nicm 168: if (c != NULL)
169: c->references++;
170: if (s != NULL)
1.23 nicm 171: session_add_ref(s, __func__);
1.2 nicm 172: if (w != NULL)
1.23 nicm 173: window_add_ref(w, __func__);
1.2 nicm 174:
1.19 nicm 175: cmd_find_copy_state(&ne->fs, fs);
1.23 nicm 176: if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */
177: session_add_ref(ne->fs.s, __func__);
1.18 nicm 178:
1.15 nicm 179: new_item = cmdq_get_callback(notify_callback, ne);
180: cmdq_append(NULL, new_item);
1.26 nicm 181: }
182:
183: void
184: notify_hook(struct cmdq_item *item, const char *name)
185: {
186: struct notify_entry ne;
187:
188: memset(&ne, 0, sizeof ne);
189:
190: ne.name = name;
191: cmd_find_copy_state(&ne.fs, &item->target);
192:
193: ne.client = item->client;
194: ne.session = item->target.s;
195: ne.window = item->target.w;
196: ne.pane = item->target.wp->id;
197:
1.27 nicm 198: notify_insert_hook(item, &ne);
1.5 nicm 199: }
200:
201: void
1.28 ! nicm 202: notify_input(struct window_pane *wp, const u_char *buf, size_t len)
1.5 nicm 203: {
204: struct client *c;
205:
1.6 nicm 206: TAILQ_FOREACH(c, &clients, entry) {
207: if (c->flags & CLIENT_CONTROL)
1.28 ! nicm 208: control_notify_input(c, wp, buf, len);
1.2 nicm 209: }
210: }
211:
1.1 nicm 212: void
1.18 nicm 213: notify_client(const char *name, struct client *c)
1.1 nicm 214: {
1.19 nicm 215: struct cmd_find_state fs;
216:
1.25 nicm 217: cmd_find_from_client(&fs, c, 0);
1.19 nicm 218: notify_add(name, &fs, c, NULL, NULL, NULL);
1.1 nicm 219: }
220:
221: void
1.18 nicm 222: notify_session(const char *name, struct session *s)
1.1 nicm 223: {
1.19 nicm 224: struct cmd_find_state fs;
225:
226: if (session_alive(s))
1.25 nicm 227: cmd_find_from_session(&fs, s, 0);
1.19 nicm 228: else
1.25 nicm 229: cmd_find_from_nothing(&fs, 0);
1.19 nicm 230: notify_add(name, &fs, NULL, s, NULL, NULL);
231: }
232:
233: void
1.21 nicm 234: notify_winlink(const char *name, struct winlink *wl)
1.19 nicm 235: {
236: struct cmd_find_state fs;
237:
1.25 nicm 238: cmd_find_from_winlink(&fs, wl, 0);
1.21 nicm 239: notify_add(name, &fs, NULL, wl->session, wl->window, NULL);
1.1 nicm 240: }
241:
242: void
1.18 nicm 243: notify_session_window(const char *name, struct session *s, struct window *w)
1.1 nicm 244: {
1.19 nicm 245: struct cmd_find_state fs;
246:
1.25 nicm 247: cmd_find_from_session_window(&fs, s, w, 0);
1.19 nicm 248: notify_add(name, &fs, NULL, s, w, NULL);
1.1 nicm 249: }
250:
251: void
1.18 nicm 252: notify_window(const char *name, struct window *w)
1.1 nicm 253: {
1.19 nicm 254: struct cmd_find_state fs;
255:
1.25 nicm 256: cmd_find_from_window(&fs, w, 0);
1.19 nicm 257: notify_add(name, &fs, NULL, NULL, w, NULL);
1.1 nicm 258: }
259:
260: void
1.18 nicm 261: notify_pane(const char *name, struct window_pane *wp)
1.1 nicm 262: {
1.19 nicm 263: struct cmd_find_state fs;
264:
1.25 nicm 265: cmd_find_from_pane(&fs, wp, 0);
1.19 nicm 266: notify_add(name, &fs, NULL, NULL, NULL, wp);
1.1 nicm 267: }