Coverage Report

Created: 2024-07-27 06:19

/src/tmux/control-notify.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 * Copyright (c) 2012 George Nachman <tmux@georgester.com>
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <sys/types.h>
21
22
#include <stdlib.h>
23
24
#include "tmux.h"
25
26
#define CONTROL_SHOULD_NOTIFY_CLIENT(c) \
27
0
  ((c) != NULL && ((c)->flags & CLIENT_CONTROL))
28
29
void
30
control_notify_pane_mode_changed(int pane)
31
0
{
32
0
  struct client *c;
33
34
0
  TAILQ_FOREACH(c, &clients, entry) {
35
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
36
0
      continue;
37
38
0
    control_write(c, "%%pane-mode-changed %%%u", pane);
39
0
  }
40
0
}
41
42
void
43
control_notify_window_layout_changed(struct window *w)
44
0
{
45
0
  struct client *c;
46
0
  struct session  *s;
47
0
  struct winlink  *wl;
48
0
  const char  *template;
49
0
  char    *cp;
50
51
0
  template = "%layout-change #{window_id} #{window_layout} "
52
0
      "#{window_visible_layout} #{window_raw_flags}";
53
54
0
  TAILQ_FOREACH(c, &clients, entry) {
55
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL)
56
0
      continue;
57
0
    s = c->session;
58
59
0
    if (winlink_find_by_window_id(&s->windows, w->id) == NULL)
60
0
      continue;
61
62
    /*
63
     * When the last pane in a window is closed it won't have a
64
     * layout root and we don't need to inform the client about the
65
     * layout change because the whole window will go away soon.
66
     */
67
0
    if (w->layout_root == NULL)
68
0
      continue;
69
70
0
    wl = winlink_find_by_window(&s->windows, w);
71
0
    if (wl != NULL) {
72
0
      cp = format_single(NULL, template, c, NULL, wl, NULL);
73
0
      control_write(c, "%s", cp);
74
0
      free(cp);
75
0
    }
76
0
  }
77
0
}
78
79
void
80
control_notify_window_pane_changed(struct window *w)
81
0
{
82
0
  struct client *c;
83
84
0
  TAILQ_FOREACH(c, &clients, entry) {
85
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
86
0
      continue;
87
88
0
    control_write(c, "%%window-pane-changed @%u %%%u", w->id,
89
0
        w->active->id);
90
0
  }
91
0
}
92
93
void
94
control_notify_window_unlinked(__unused struct session *s, struct window *w)
95
0
{
96
0
  struct client *c;
97
0
  struct session  *cs;
98
99
0
  TAILQ_FOREACH(c, &clients, entry) {
100
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL)
101
0
      continue;
102
0
    cs = c->session;
103
104
0
    if (winlink_find_by_window_id(&cs->windows, w->id) != NULL)
105
0
      control_write(c, "%%window-close @%u", w->id);
106
0
    else
107
0
      control_write(c, "%%unlinked-window-close @%u", w->id);
108
0
  }
109
0
}
110
111
void
112
control_notify_window_linked(__unused struct session *s, struct window *w)
113
0
{
114
0
  struct client *c;
115
0
  struct session  *cs;
116
117
0
  TAILQ_FOREACH(c, &clients, entry) {
118
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL)
119
0
      continue;
120
0
    cs = c->session;
121
122
0
    if (winlink_find_by_window_id(&cs->windows, w->id) != NULL)
123
0
      control_write(c, "%%window-add @%u", w->id);
124
0
    else
125
0
      control_write(c, "%%unlinked-window-add @%u", w->id);
126
0
  }
127
0
}
128
129
void
130
control_notify_window_renamed(struct window *w)
131
1.24k
{
132
1.24k
  struct client *c;
133
1.24k
  struct session  *cs;
134
135
1.24k
  TAILQ_FOREACH(c, &clients, entry) {
136
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL)
137
0
      continue;
138
0
    cs = c->session;
139
140
0
    if (winlink_find_by_window_id(&cs->windows, w->id) != NULL) {
141
0
      control_write(c, "%%window-renamed @%u %s", w->id,
142
0
          w->name);
143
0
    } else {
144
0
      control_write(c, "%%unlinked-window-renamed @%u %s",
145
0
          w->id, w->name);
146
0
    }
147
0
  }
148
1.24k
}
149
150
void
151
control_notify_client_session_changed(struct client *cc)
152
0
{
153
0
  struct client *c;
154
0
  struct session  *s;
155
156
0
  if (cc->session == NULL)
157
0
    return;
158
0
  s = cc->session;
159
160
0
  TAILQ_FOREACH(c, &clients, entry) {
161
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c) || c->session == NULL)
162
0
      continue;
163
164
0
    if (cc == c) {
165
0
      control_write(c, "%%session-changed $%u %s", s->id,
166
0
          s->name);
167
0
    } else {
168
0
      control_write(c, "%%client-session-changed %s $%u %s",
169
0
          cc->name, s->id, s->name);
170
0
    }
171
0
  }
172
0
}
173
174
void
175
control_notify_client_detached(struct client *cc)
176
0
{
177
0
  struct client *c;
178
179
0
  TAILQ_FOREACH(c, &clients, entry) {
180
0
    if (CONTROL_SHOULD_NOTIFY_CLIENT(c))
181
0
      control_write(c, "%%client-detached %s", cc->name);
182
0
  }
183
0
}
184
185
void
186
control_notify_session_renamed(struct session *s)
187
0
{
188
0
  struct client *c;
189
190
0
  TAILQ_FOREACH(c, &clients, entry) {
191
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
192
0
      continue;
193
194
0
    control_write(c, "%%session-renamed $%u %s", s->id, s->name);
195
0
  }
196
0
}
197
198
void
199
control_notify_session_created(__unused struct session *s)
200
0
{
201
0
  struct client *c;
202
203
0
  TAILQ_FOREACH(c, &clients, entry) {
204
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
205
0
      continue;
206
207
0
    control_write(c, "%%sessions-changed");
208
0
  }
209
0
}
210
211
void
212
control_notify_session_closed(__unused struct session *s)
213
0
{
214
0
  struct client *c;
215
216
0
  TAILQ_FOREACH(c, &clients, entry) {
217
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
218
0
      continue;
219
220
0
    control_write(c, "%%sessions-changed");
221
0
  }
222
0
}
223
224
void
225
control_notify_session_window_changed(struct session *s)
226
0
{
227
0
  struct client *c;
228
229
0
  TAILQ_FOREACH(c, &clients, entry) {
230
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
231
0
      continue;
232
233
0
    control_write(c, "%%session-window-changed $%u @%u", s->id,
234
0
        s->curw->window->id);
235
0
  }
236
0
}
237
238
void
239
control_notify_paste_buffer_changed(const char *name)
240
1.10k
{
241
1.10k
  struct client *c;
242
243
1.10k
  TAILQ_FOREACH(c, &clients, entry) {
244
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
245
0
      continue;
246
247
0
    control_write(c, "%%paste-buffer-changed %s", name);
248
0
  }
249
1.10k
}
250
251
void
252
control_notify_paste_buffer_deleted(const char *name)
253
1.05k
{
254
1.05k
  struct client *c;
255
256
1.05k
  TAILQ_FOREACH(c, &clients, entry) {
257
0
    if (!CONTROL_SHOULD_NOTIFY_CLIENT(c))
258
0
      continue;
259
260
0
    control_write(c, "%%paste-buffer-deleted %s", name);
261
0
  }
262
1.05k
}