Annotation of src/usr.bin/tmux/notify.c, Revision 1.21
1.21 ! nicm 1: /* $OpenBSD: notify.c,v 1.20 2017/01/11 14:56:44 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.15 nicm 39: notify_hook(struct cmdq_item *item, struct notify_entry *ne)
1.11 nicm 40: {
41: struct cmd_find_state fs;
42: struct hook *hook;
1.16 nicm 43: struct cmdq_item *new_item;
1.17 nicm 44: struct session *s = ne->session;
45: struct window *w = ne->window;
1.11 nicm 46:
47: cmd_find_clear_state(&fs, NULL, 0);
1.18 nicm 48: if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
49: cmd_find_current(&fs, item, CMD_FIND_QUIET);
1.15 nicm 50: else
1.18 nicm 51: cmd_find_copy_state(&fs, &ne->fs);
1.11 nicm 52:
1.18 nicm 53: hook = hooks_find(hooks_get(fs.s), ne->name);
1.11 nicm 54: if (hook == NULL)
55: return;
1.18 nicm 56: log_debug("notify hook %s", ne->name);
1.11 nicm 57:
1.14 nicm 58: new_item = cmdq_get_command(hook->cmdlist, &fs, NULL, CMDQ_NOHOOKS);
1.18 nicm 59: cmdq_format(new_item, "hook", "%s", ne->name);
1.17 nicm 60:
61: if (s != NULL) {
62: cmdq_format(new_item, "hook_session", "$%u", s->id);
63: cmdq_format(new_item, "hook_session_name", "%s", s->name);
64: }
65: if (w != NULL) {
66: cmdq_format(new_item, "hook_window", "@%u", w->id);
67: cmdq_format(new_item, "hook_window_name", "%s", w->name);
68: }
1.18 nicm 69: if (ne->pane != -1)
70: cmdq_format(new_item, "hook_pane", "%%%d", ne->pane);
1.17 nicm 71:
1.15 nicm 72: cmdq_insert_after(item, new_item);
73: }
74:
75: static enum cmd_retval
76: notify_callback(struct cmdq_item *item, void *data)
77: {
78: struct notify_entry *ne = data;
1.20 nicm 79:
80: log_debug("%s: %s", __func__, ne->name);
1.15 nicm 81:
1.18 nicm 82: if (strcmp(ne->name, "window-layout-changed") == 0)
1.15 nicm 83: control_notify_window_layout_changed(ne->window);
1.18 nicm 84: if (strcmp(ne->name, "window-unlinked") == 0)
1.15 nicm 85: control_notify_window_unlinked(ne->session, ne->window);
1.18 nicm 86: if (strcmp(ne->name, "window-linked") == 0)
1.15 nicm 87: control_notify_window_linked(ne->session, ne->window);
1.18 nicm 88: if (strcmp(ne->name, "window-renamed") == 0)
1.15 nicm 89: control_notify_window_renamed(ne->window);
1.18 nicm 90: if (strcmp(ne->name, "client-session-changed") == 0)
91: control_notify_client_session_changed(ne->client);
92: if (strcmp(ne->name, "session-renamed") == 0)
1.15 nicm 93: control_notify_session_renamed(ne->session);
1.18 nicm 94: if (strcmp(ne->name, "session-created") == 0)
1.15 nicm 95: control_notify_session_created(ne->session);
1.18 nicm 96: if (strcmp(ne->name, "session-closed") == 0)
1.15 nicm 97: control_notify_session_closed(ne->session);
1.18 nicm 98:
1.15 nicm 99: notify_hook(item, ne);
100:
101: if (ne->client != NULL)
102: server_client_unref(ne->client);
103: if (ne->session != NULL)
104: session_unref(ne->session);
105: if (ne->window != NULL)
106: window_remove_ref(ne->window);
1.18 nicm 107:
108: if (ne->fs.s != NULL)
109: session_unref(ne->fs.s);
110:
111: free((void *)ne->name);
1.15 nicm 112: free(ne);
113:
114: return (CMD_RETURN_NORMAL);
1.11 nicm 115: }
116:
117: static void
1.19 nicm 118: notify_add(const char *name, struct cmd_find_state *fs, struct client *c,
119: struct session *s, struct window *w, struct window_pane *wp)
1.2 nicm 120: {
121: struct notify_entry *ne;
1.15 nicm 122: struct cmdq_item *new_item;
1.2 nicm 123:
124: ne = xcalloc(1, sizeof *ne);
1.18 nicm 125: ne->name = xstrdup(name);
126:
1.2 nicm 127: ne->client = c;
128: ne->session = s;
129: ne->window = w;
130:
1.18 nicm 131: if (wp != NULL)
132: ne->pane = wp->id;
133: else
134: ne->pane = -1;
135:
1.2 nicm 136: if (c != NULL)
137: c->references++;
138: if (s != NULL)
139: s->references++;
140: if (w != NULL)
141: w->references++;
142:
1.19 nicm 143: cmd_find_copy_state(&ne->fs, fs);
1.18 nicm 144: if (ne->fs.s != NULL)
145: ne->fs.s->references++; /* cmd_find_valid_state need session */
146:
1.15 nicm 147: new_item = cmdq_get_callback(notify_callback, ne);
148: cmdq_append(NULL, new_item);
1.5 nicm 149: }
150:
151: void
152: notify_input(struct window_pane *wp, struct evbuffer *input)
153: {
154: struct client *c;
155:
1.6 nicm 156: TAILQ_FOREACH(c, &clients, entry) {
157: if (c->flags & CLIENT_CONTROL)
1.5 nicm 158: control_notify_input(c, wp, input);
1.2 nicm 159: }
160: }
161:
1.1 nicm 162: void
1.18 nicm 163: notify_client(const char *name, struct client *c)
1.1 nicm 164: {
1.19 nicm 165: struct cmd_find_state fs;
166:
167: if (c->session != NULL)
168: cmd_find_from_session(&fs, c->session);
169: else
170: cmd_find_current(&fs, NULL, CMD_FIND_QUIET);
171: notify_add(name, &fs, c, NULL, NULL, NULL);
1.1 nicm 172: }
173:
174: void
1.18 nicm 175: notify_session(const char *name, struct session *s)
1.1 nicm 176: {
1.19 nicm 177: struct cmd_find_state fs;
178:
179: if (session_alive(s))
180: cmd_find_from_session(&fs, s);
181: else
182: cmd_find_current(&fs, NULL, CMD_FIND_QUIET);
183: notify_add(name, &fs, NULL, s, NULL, NULL);
184: }
185:
186: void
1.21 ! nicm 187: notify_winlink(const char *name, struct winlink *wl)
1.19 nicm 188: {
189: struct cmd_find_state fs;
190:
1.21 ! nicm 191: cmd_find_from_winlink(&fs, wl);
! 192: notify_add(name, &fs, NULL, wl->session, wl->window, NULL);
1.1 nicm 193: }
194:
195: void
1.18 nicm 196: notify_session_window(const char *name, struct session *s, struct window *w)
1.1 nicm 197: {
1.19 nicm 198: struct cmd_find_state fs;
199:
200: cmd_find_from_session_window(&fs, s, w);
201: notify_add(name, &fs, NULL, s, w, NULL);
1.1 nicm 202: }
203:
204: void
1.18 nicm 205: notify_window(const char *name, struct window *w)
1.1 nicm 206: {
1.19 nicm 207: struct cmd_find_state fs;
208:
209: cmd_find_from_window(&fs, w);
210: notify_add(name, &fs, NULL, NULL, w, NULL);
1.1 nicm 211: }
212:
213: void
1.18 nicm 214: notify_pane(const char *name, struct window_pane *wp)
1.1 nicm 215: {
1.19 nicm 216: struct cmd_find_state fs;
217:
218: cmd_find_from_pane(&fs, wp);
219: notify_add(name, &fs, NULL, NULL, NULL, wp);
1.1 nicm 220: }