[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.18

1.18    ! nicm        1: /* $OpenBSD: notify.c,v 1.17 2016/10/16 19:55:52 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;
                     79:
1.18    ! nicm       80:        if (strcmp(ne->name, "window-layout-changed") == 0)
1.15      nicm       81:                control_notify_window_layout_changed(ne->window);
1.18    ! nicm       82:        if (strcmp(ne->name, "window-unlinked") == 0)
1.15      nicm       83:                control_notify_window_unlinked(ne->session, ne->window);
1.18    ! nicm       84:        if (strcmp(ne->name, "window-linked") == 0)
1.15      nicm       85:                control_notify_window_linked(ne->session, ne->window);
1.18    ! nicm       86:        if (strcmp(ne->name, "window-renamed") == 0)
1.15      nicm       87:                control_notify_window_renamed(ne->window);
1.18    ! nicm       88:        if (strcmp(ne->name, "client-session-changed") == 0)
        !            89:                control_notify_client_session_changed(ne->client);
        !            90:        if (strcmp(ne->name, "session-renamed") == 0)
1.15      nicm       91:                control_notify_session_renamed(ne->session);
1.18    ! nicm       92:        if (strcmp(ne->name, "session-created") == 0)
1.15      nicm       93:                control_notify_session_created(ne->session);
1.18    ! nicm       94:        if (strcmp(ne->name, "session-closed") == 0)
1.15      nicm       95:                control_notify_session_closed(ne->session);
1.18    ! nicm       96:
1.15      nicm       97:        notify_hook(item, ne);
                     98:
                     99:        if (ne->client != NULL)
                    100:                server_client_unref(ne->client);
                    101:        if (ne->session != NULL)
                    102:                session_unref(ne->session);
                    103:        if (ne->window != NULL)
                    104:                window_remove_ref(ne->window);
1.18    ! nicm      105:
        !           106:        if (ne->fs.s != NULL)
        !           107:                session_unref(ne->fs.s);
        !           108:
        !           109:        free((void *)ne->name);
1.15      nicm      110:        free(ne);
                    111:
                    112:        return (CMD_RETURN_NORMAL);
1.11      nicm      113: }
                    114:
                    115: static void
1.18    ! nicm      116: notify_add(const char *name, struct client *c, struct session *s,
        !           117:     struct window *w, struct window_pane *wp)
1.2       nicm      118: {
                    119:        struct notify_entry     *ne;
1.15      nicm      120:        struct cmdq_item        *new_item;
1.2       nicm      121:
                    122:        ne = xcalloc(1, sizeof *ne);
1.18    ! nicm      123:        ne->name = xstrdup(name);
        !           124:
1.2       nicm      125:        ne->client = c;
                    126:        ne->session = s;
                    127:        ne->window = w;
                    128:
1.18    ! nicm      129:        if (wp != NULL)
        !           130:                ne->pane = wp->id;
        !           131:        else
        !           132:                ne->pane = -1;
        !           133:
1.2       nicm      134:        if (c != NULL)
                    135:                c->references++;
                    136:        if (s != NULL)
                    137:                s->references++;
                    138:        if (w != NULL)
                    139:                w->references++;
                    140:
1.18    ! nicm      141:        cmd_find_clear_state(&ne->fs, NULL, 0);
        !           142:        if (s != NULL && w != NULL)
        !           143:                cmd_find_from_session_window(&ne->fs, s, w);
        !           144:        else if (w != NULL)
        !           145:                cmd_find_from_window(&ne->fs, w);
        !           146:        else if (s != NULL && session_alive(s))
        !           147:                cmd_find_from_session(&ne->fs, s);
        !           148:        else if (wp != NULL)
        !           149:                cmd_find_from_pane(&ne->fs, wp);
        !           150:        else
        !           151:                cmd_find_current(&ne->fs, NULL, CMD_FIND_QUIET);
        !           152:        if (ne->fs.s != NULL)
        !           153:                ne->fs.s->references++; /* cmd_find_valid_state need session */
        !           154:
1.15      nicm      155:        new_item = cmdq_get_callback(notify_callback, ne);
                    156:        cmdq_append(NULL, new_item);
1.5       nicm      157: }
                    158:
                    159: void
                    160: notify_input(struct window_pane *wp, struct evbuffer *input)
                    161: {
                    162:        struct client   *c;
                    163:
1.6       nicm      164:        TAILQ_FOREACH(c, &clients, entry) {
                    165:                if (c->flags & CLIENT_CONTROL)
1.5       nicm      166:                        control_notify_input(c, wp, input);
1.2       nicm      167:        }
                    168: }
                    169:
1.1       nicm      170: void
1.18    ! nicm      171: notify_client(const char *name, struct client *c)
1.1       nicm      172: {
1.18    ! nicm      173:        notify_add(name, c, NULL, NULL, NULL);
1.1       nicm      174: }
                    175:
                    176: void
1.18    ! nicm      177: notify_session(const char *name, struct session *s)
1.1       nicm      178: {
1.18    ! nicm      179:        notify_add(name, NULL, s, NULL, NULL);
1.1       nicm      180: }
                    181:
                    182: void
1.18    ! nicm      183: notify_session_window(const char *name, struct session *s, struct window *w)
1.1       nicm      184: {
1.18    ! nicm      185:        notify_add(name, NULL, s, w, NULL);
1.1       nicm      186: }
                    187:
                    188: void
1.18    ! nicm      189: notify_window(const char *name, struct window *w)
1.1       nicm      190: {
1.18    ! nicm      191:        notify_add(name, NULL, NULL, w, NULL);
1.1       nicm      192: }
                    193:
                    194: void
1.18    ! nicm      195: notify_pane(const char *name, struct window_pane *wp)
1.1       nicm      196: {
1.18    ! nicm      197:        notify_add(name, NULL, NULL, NULL, wp);
1.1       nicm      198: }