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