Annotation of src/usr.bin/tmux/notify.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: notify.c,v 1.24 2017/05/04 07:16:43 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:
1.22 nicm 47: cmd_find_clear_state(&fs, 0);
1.18 nicm 48: if (cmd_find_empty_state(&ne->fs) || !cmd_find_valid_state(&ne->fs))
1.25 ! nicm 49: cmd_find_from_nothing(&fs, 0);
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.24 nicm 82: if (strcmp(ne->name, "pane-mode-changed") == 0)
83: control_notify_pane_mode_changed(ne->pane);
1.18 nicm 84: if (strcmp(ne->name, "window-layout-changed") == 0)
1.15 nicm 85: control_notify_window_layout_changed(ne->window);
1.24 nicm 86: if (strcmp(ne->name, "window-pane-changed") == 0)
87: control_notify_window_pane_changed(ne->window);
1.18 nicm 88: if (strcmp(ne->name, "window-unlinked") == 0)
1.15 nicm 89: control_notify_window_unlinked(ne->session, ne->window);
1.18 nicm 90: if (strcmp(ne->name, "window-linked") == 0)
1.15 nicm 91: control_notify_window_linked(ne->session, ne->window);
1.18 nicm 92: if (strcmp(ne->name, "window-renamed") == 0)
1.15 nicm 93: control_notify_window_renamed(ne->window);
1.18 nicm 94: if (strcmp(ne->name, "client-session-changed") == 0)
95: control_notify_client_session_changed(ne->client);
96: if (strcmp(ne->name, "session-renamed") == 0)
1.15 nicm 97: control_notify_session_renamed(ne->session);
1.18 nicm 98: if (strcmp(ne->name, "session-created") == 0)
1.15 nicm 99: control_notify_session_created(ne->session);
1.18 nicm 100: if (strcmp(ne->name, "session-closed") == 0)
1.15 nicm 101: control_notify_session_closed(ne->session);
1.24 nicm 102: if (strcmp(ne->name, "session-window-changed") == 0)
103: control_notify_session_window_changed(ne->session);
1.18 nicm 104:
1.15 nicm 105: notify_hook(item, ne);
106:
107: if (ne->client != NULL)
108: server_client_unref(ne->client);
109: if (ne->session != NULL)
1.23 nicm 110: session_remove_ref(ne->session, __func__);
1.15 nicm 111: if (ne->window != NULL)
1.23 nicm 112: window_remove_ref(ne->window, __func__);
1.18 nicm 113:
114: if (ne->fs.s != NULL)
1.23 nicm 115: session_remove_ref(ne->fs.s, __func__);
1.18 nicm 116:
117: free((void *)ne->name);
1.15 nicm 118: free(ne);
119:
120: return (CMD_RETURN_NORMAL);
1.11 nicm 121: }
122:
123: static void
1.19 nicm 124: notify_add(const char *name, struct cmd_find_state *fs, struct client *c,
125: struct session *s, struct window *w, struct window_pane *wp)
1.2 nicm 126: {
127: struct notify_entry *ne;
1.15 nicm 128: struct cmdq_item *new_item;
1.2 nicm 129:
130: ne = xcalloc(1, sizeof *ne);
1.18 nicm 131: ne->name = xstrdup(name);
132:
1.2 nicm 133: ne->client = c;
134: ne->session = s;
135: ne->window = w;
136:
1.18 nicm 137: if (wp != NULL)
138: ne->pane = wp->id;
139: else
140: ne->pane = -1;
141:
1.2 nicm 142: if (c != NULL)
143: c->references++;
144: if (s != NULL)
1.23 nicm 145: session_add_ref(s, __func__);
1.2 nicm 146: if (w != NULL)
1.23 nicm 147: window_add_ref(w, __func__);
1.2 nicm 148:
1.19 nicm 149: cmd_find_copy_state(&ne->fs, fs);
1.23 nicm 150: if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */
151: session_add_ref(ne->fs.s, __func__);
1.18 nicm 152:
1.15 nicm 153: new_item = cmdq_get_callback(notify_callback, ne);
154: cmdq_append(NULL, new_item);
1.5 nicm 155: }
156:
157: void
158: notify_input(struct window_pane *wp, struct evbuffer *input)
159: {
160: struct client *c;
161:
1.6 nicm 162: TAILQ_FOREACH(c, &clients, entry) {
163: if (c->flags & CLIENT_CONTROL)
1.5 nicm 164: control_notify_input(c, wp, input);
1.2 nicm 165: }
166: }
167:
1.1 nicm 168: void
1.18 nicm 169: notify_client(const char *name, struct client *c)
1.1 nicm 170: {
1.19 nicm 171: struct cmd_find_state fs;
172:
1.25 ! nicm 173: cmd_find_from_client(&fs, c, 0);
1.19 nicm 174: notify_add(name, &fs, c, NULL, NULL, NULL);
1.1 nicm 175: }
176:
177: void
1.18 nicm 178: notify_session(const char *name, struct session *s)
1.1 nicm 179: {
1.19 nicm 180: struct cmd_find_state fs;
181:
182: if (session_alive(s))
1.25 ! nicm 183: cmd_find_from_session(&fs, s, 0);
1.19 nicm 184: else
1.25 ! nicm 185: cmd_find_from_nothing(&fs, 0);
1.19 nicm 186: notify_add(name, &fs, NULL, s, NULL, NULL);
187: }
188:
189: void
1.21 nicm 190: notify_winlink(const char *name, struct winlink *wl)
1.19 nicm 191: {
192: struct cmd_find_state fs;
193:
1.25 ! nicm 194: cmd_find_from_winlink(&fs, wl, 0);
1.21 nicm 195: notify_add(name, &fs, NULL, wl->session, wl->window, NULL);
1.1 nicm 196: }
197:
198: void
1.18 nicm 199: notify_session_window(const char *name, struct session *s, struct window *w)
1.1 nicm 200: {
1.19 nicm 201: struct cmd_find_state fs;
202:
1.25 ! nicm 203: cmd_find_from_session_window(&fs, s, w, 0);
1.19 nicm 204: notify_add(name, &fs, NULL, s, w, NULL);
1.1 nicm 205: }
206:
207: void
1.18 nicm 208: notify_window(const char *name, struct window *w)
1.1 nicm 209: {
1.19 nicm 210: struct cmd_find_state fs;
211:
1.25 ! nicm 212: cmd_find_from_window(&fs, w, 0);
1.19 nicm 213: notify_add(name, &fs, NULL, NULL, w, NULL);
1.1 nicm 214: }
215:
216: void
1.18 nicm 217: notify_pane(const char *name, struct window_pane *wp)
1.1 nicm 218: {
1.19 nicm 219: struct cmd_find_state fs;
220:
1.25 ! nicm 221: cmd_find_from_pane(&fs, wp, 0);
1.19 nicm 222: notify_add(name, &fs, NULL, NULL, NULL, wp);
1.1 nicm 223: }