[BACK]Return to notify.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/notify.c, Revision 1.23

1.23    ! nicm        1: /* $OpenBSD: notify.c,v 1.22 2017/04/21 20:26:34 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.22      nicm       49:                cmd_find_from_nothing(&fs);
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)
1.23    ! nicm      104:                session_remove_ref(ne->session, __func__);
1.15      nicm      105:        if (ne->window != NULL)
1.23    ! nicm      106:                window_remove_ref(ne->window, __func__);
1.18      nicm      107:
                    108:        if (ne->fs.s != NULL)
1.23    ! nicm      109:                session_remove_ref(ne->fs.s, __func__);
1.18      nicm      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)
1.23    ! nicm      139:                session_add_ref(s, __func__);
1.2       nicm      140:        if (w != NULL)
1.23    ! nicm      141:                window_add_ref(w, __func__);
1.2       nicm      142:
1.19      nicm      143:        cmd_find_copy_state(&ne->fs, fs);
1.23    ! nicm      144:        if (ne->fs.s != NULL) /* cmd_find_valid_state needs session */
        !           145:                session_add_ref(ne->fs.s, __func__);
1.18      nicm      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:
1.22      nicm      167:        cmd_find_from_client(&fs, c);
1.19      nicm      168:        notify_add(name, &fs, c, NULL, NULL, NULL);
1.1       nicm      169: }
                    170:
                    171: void
1.18      nicm      172: notify_session(const char *name, struct session *s)
1.1       nicm      173: {
1.19      nicm      174:        struct cmd_find_state   fs;
                    175:
                    176:        if (session_alive(s))
                    177:                cmd_find_from_session(&fs, s);
                    178:        else
1.22      nicm      179:                cmd_find_from_nothing(&fs);
1.19      nicm      180:        notify_add(name, &fs, NULL, s, NULL, NULL);
                    181: }
                    182:
                    183: void
1.21      nicm      184: notify_winlink(const char *name, struct winlink *wl)
1.19      nicm      185: {
                    186:        struct cmd_find_state   fs;
                    187:
1.21      nicm      188:        cmd_find_from_winlink(&fs, wl);
                    189:        notify_add(name, &fs, NULL, wl->session, wl->window, NULL);
1.1       nicm      190: }
                    191:
                    192: void
1.18      nicm      193: notify_session_window(const char *name, struct session *s, struct window *w)
1.1       nicm      194: {
1.19      nicm      195:        struct cmd_find_state   fs;
                    196:
                    197:        cmd_find_from_session_window(&fs, s, w);
                    198:        notify_add(name, &fs, NULL, s, w, NULL);
1.1       nicm      199: }
                    200:
                    201: void
1.18      nicm      202: notify_window(const char *name, struct window *w)
1.1       nicm      203: {
1.19      nicm      204:        struct cmd_find_state   fs;
                    205:
                    206:        cmd_find_from_window(&fs, w);
                    207:        notify_add(name, &fs, NULL, NULL, w, NULL);
1.1       nicm      208: }
                    209:
                    210: void
1.18      nicm      211: notify_pane(const char *name, struct window_pane *wp)
1.1       nicm      212: {
1.19      nicm      213:        struct cmd_find_state   fs;
                    214:
                    215:        cmd_find_from_pane(&fs, wp);
                    216:        notify_add(name, &fs, NULL, NULL, NULL, wp);
1.1       nicm      217: }