/src/tmux/window-client.c
Line | Count | Source |
1 | | /* $OpenBSD: window-client.c,v 1.49 2026/08/31 12:41:03 kirill Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.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 | | |
19 | | #include <sys/types.h> |
20 | | #include <sys/time.h> |
21 | | |
22 | | #include <stdlib.h> |
23 | | #include <string.h> |
24 | | #include <time.h> |
25 | | |
26 | | #include "tmux.h" |
27 | | |
28 | | static struct screen *window_client_init(struct window_mode_entry *, |
29 | | struct cmdq_item *, struct cmd_find_state *, |
30 | | struct args *); |
31 | | static void window_client_free(struct window_mode_entry *); |
32 | | static void window_client_resize(struct window_mode_entry *, u_int, |
33 | | u_int); |
34 | | static void window_client_update(struct window_mode_entry *); |
35 | | static void window_client_key(struct window_mode_entry *, |
36 | | struct client *, struct session *, |
37 | | struct winlink *, key_code, struct mouse_event *); |
38 | | |
39 | 0 | #define WINDOW_CLIENT_DEFAULT_COMMAND "detach-client -t '%%'" |
40 | | |
41 | | #define WINDOW_CLIENT_DEFAULT_FORMAT \ |
42 | 0 | "#[fg=themelightgrey]#{t/p:client_activity}: session #[default]#{session_name}" |
43 | | |
44 | | #define WINDOW_CLIENT_DEFAULT_KEY_FORMAT \ |
45 | 0 | "#{?#{e|<:#{line},10}," \ |
46 | 0 | "#{line}" \ |
47 | 0 | ",#{e|<:#{line},36}," \ |
48 | 0 | "M-#{a:#{e|+:97,#{e|-:#{line},10}}}" \ |
49 | 0 | "}" |
50 | | |
51 | | #define WINDOW_CLIENT_FEATURE(f) \ |
52 | | "#{?#{I/f:" #f "}," \ |
53 | | "#[fg=themegreen],#[fg=themelightgrey]}#{p/15:#{l:" #f "}}" \ |
54 | | "#[default]" |
55 | | static const char *window_client_info_lines[] = { |
56 | | "#[fg=themelightgrey]Client Name #[#{E:tree-mode-border-style},acs]x#[default] " |
57 | | "#{client_name} #[fg=themelightgrey]" |
58 | | "#[fg=themelightgrey](PID #{client_pid})#[default]", |
59 | | "#[fg=themelightgrey]Session #[#{E:tree-mode-border-style},acs]x#[default] " |
60 | | "#{session_name}", |
61 | | "#[fg=themelightgrey]Attach Time #[#{E:tree-mode-border-style},acs]x#[default] " |
62 | | "#{t:client_created} " |
63 | | "#[fg=themelightgrey](#{t/r:client_created})#[default]", |
64 | | "#[fg=themelightgrey]Activity Time #[#{E:tree-mode-border-style},acs]x#[default] " |
65 | | "#{t:client_activity} " |
66 | | "#[fg=themelightgrey](#{t/r:client_activity})#[default]", |
67 | | "#[fg=themelightgrey]Terminal Type #[#{E:tree-mode-border-style},acs]x#[default] " |
68 | | "#{?client_termtype,#{client_termtype},Unknown}", |
69 | | "#[fg=themelightgrey]TERM #[#{E:tree-mode-border-style},acs]x#[default] " |
70 | | "#{client_termname}", |
71 | | "#[fg=themelightgrey]Size #[#{E:tree-mode-border-style},acs]x#[default] " |
72 | | "#{client_width}x#{client_height} " |
73 | | "#[fg=themelightgrey](cell #{client_cell_width}x" |
74 | | "#{client_cell_height})#[default]", |
75 | | "#[fg=themelightgrey]Bytes Written #[#{E:tree-mode-border-style},acs]x#[default] " |
76 | | "#{client_written} " |
77 | | "#[fg=themelightgrey](#{client_discarded} discarded)#[default]", |
78 | | |
79 | | "#[fg=themelightgrey]Features #[#{E:tree-mode-border-style},acs]x#[default] " |
80 | | WINDOW_CLIENT_FEATURE(256) " " |
81 | | WINDOW_CLIENT_FEATURE(RGB) " " |
82 | | WINDOW_CLIENT_FEATURE(bpaste) " " |
83 | | WINDOW_CLIENT_FEATURE(ccolour), |
84 | | " #[#{E:tree-mode-border-style},acs]x#[default] " |
85 | | WINDOW_CLIENT_FEATURE(clipboard) " " |
86 | | WINDOW_CLIENT_FEATURE(cstyle) " " |
87 | | WINDOW_CLIENT_FEATURE(extkeys) " " |
88 | | WINDOW_CLIENT_FEATURE(focus), |
89 | | " #[#{E:tree-mode-border-style},acs]x#[default] " |
90 | | WINDOW_CLIENT_FEATURE(hyperlinks) " " |
91 | | WINDOW_CLIENT_FEATURE(ignorefkeys) " " |
92 | | WINDOW_CLIENT_FEATURE(margins) " " |
93 | | WINDOW_CLIENT_FEATURE(mouse), |
94 | | " #[#{E:tree-mode-border-style},acs]x#[default] " |
95 | | WINDOW_CLIENT_FEATURE(osc7) " " |
96 | | WINDOW_CLIENT_FEATURE(overline) " " |
97 | | WINDOW_CLIENT_FEATURE(progressbar) " " |
98 | | WINDOW_CLIENT_FEATURE(rectfill), |
99 | | " #[#{E:tree-mode-border-style},acs]x#[default] " |
100 | | WINDOW_CLIENT_FEATURE(sixel) " " |
101 | | WINDOW_CLIENT_FEATURE(strikethrough) " " |
102 | | WINDOW_CLIENT_FEATURE(sync) " " |
103 | | WINDOW_CLIENT_FEATURE(title), |
104 | | " #[#{E:tree-mode-border-style},acs]x#[default] " |
105 | | WINDOW_CLIENT_FEATURE(usstyle) " " |
106 | | WINDOW_CLIENT_FEATURE(utf8), |
107 | | "#[#{E:tree-mode-border-style},acs]qqqqqqqqqqqqqqn#{R:q,#{window_width}}#[default]", |
108 | | |
109 | | "#[fg=themelightgrey]prefix #[#{E:tree-mode-border-style},acs]x#[default] " |
110 | | "#{prefix}", |
111 | | |
112 | | "#[fg=themelightgrey]mouse #[#{E:tree-mode-border-style},acs]x#[default] " |
113 | | "#{?mouse,#{?#{I/c:kmous},,#[fg=themered]}on,#[fg=themelightgrey]off} " |
114 | | "#{?#{I/c:kmous},,#[align=right]unavailable: [kmous] missing}", |
115 | | |
116 | | "#[fg=themelightgrey]set-clipboard #[#{E:tree-mode-border-style},acs]x#[default] " |
117 | | "#{?#{!=:#{set-clipboard},off},#{?#{I/c:Ms},," |
118 | | "#[fg=themered]}#{set-clipboard},#[fg=themelightgrey]off} " |
119 | | "#{?#{I/c:Ms},,#[align=right]unavailable: [Ms] " |
120 | | "#{?clipboard_invalid,invalid,missing}}", |
121 | | |
122 | | "#[fg=themelightgrey]get-clipboard #[#{E:tree-mode-border-style},acs]x#[default] " |
123 | | "#{?#{!=:#{get-clipboard},off},#{?#{I/c:Ms},," |
124 | | "#[fg=themered]}#{get-clipboard},#[fg=themelightgrey]off} " |
125 | | "#{?#{I/c:Ms},,#[align=right]unavailable: [Ms] " |
126 | | "#{?clipboard_invalid,invalid,missing}}", |
127 | | |
128 | | "#[fg=themelightgrey]focus-events #[#{E:tree-mode-border-style},acs]x#[default] " |
129 | | "#{?focus-events,#{?#{I/f:focus},,#[fg=themered]}on,#[fg=themelightgrey]off} " |
130 | | "#{?#{I/f:focus},,#[align=right]unavailable: [Enfcs] or [Dcfcs] missing}", |
131 | | |
132 | | "#[fg=themelightgrey]extended-keys #[#{E:tree-mode-border-style},acs]x#[default] " |
133 | | "#{?#{!=:#{extended-keys},off},#{?#{I/f:extkeys},," |
134 | | "#[fg=themered]}#{extended-keys},#[fg=themelightgrey]off} " |
135 | | "#{?#{I/f:extkeys},,#[align=right]unavailable: [Eneks] or [Dseks] missing}", |
136 | | |
137 | | "#[fg=themelightgrey]set-titles #[#{E:tree-mode-border-style},acs]x#[default] " |
138 | | "#{?set-titles,on,#[fg=themelightgrey]off}", |
139 | | |
140 | | "#[fg=themelightgrey]escape-time #[#{E:tree-mode-border-style},acs]x#[default] " |
141 | | "#{escape-time} ms", |
142 | | }; |
143 | | |
144 | | |
145 | | static const struct menu_item window_client_menu_items[] = { |
146 | | { "Detach", 'd', NULL }, |
147 | | { "Detach Tagged", 'D', NULL }, |
148 | | { "", KEYC_NONE, NULL }, |
149 | | { "Tag", 't', NULL }, |
150 | | { "Tag All", '\024', NULL }, |
151 | | { "Tag None", 'T', NULL }, |
152 | | { "", KEYC_NONE, NULL }, |
153 | | { "Cancel", 'q', NULL }, |
154 | | |
155 | | { NULL, KEYC_NONE, NULL } |
156 | | }; |
157 | | |
158 | | const struct window_mode window_client_mode = { |
159 | | .name = "client-mode", |
160 | | .default_format = WINDOW_CLIENT_DEFAULT_FORMAT, |
161 | | |
162 | | .init = window_client_init, |
163 | | .free = window_client_free, |
164 | | .resize = window_client_resize, |
165 | | .update = window_client_update, |
166 | | .key = window_client_key, |
167 | | }; |
168 | | |
169 | | struct window_client_itemdata { |
170 | | struct client *c; |
171 | | char *ttyname; |
172 | | }; |
173 | | |
174 | | struct window_client_modedata { |
175 | | struct window_pane *wp; |
176 | | |
177 | | struct mode_tree_data *data; |
178 | | char *format; |
179 | | char *key_format; |
180 | | char *command; |
181 | | |
182 | | int hide_preview_this_pane; |
183 | | int preview_is_info; |
184 | | |
185 | | struct window_client_itemdata **item_list; |
186 | | u_int item_size; |
187 | | }; |
188 | | |
189 | | static enum sort_order window_client_order_seq[] = { |
190 | | SORT_NAME, |
191 | | SORT_SIZE, |
192 | | SORT_CREATION, |
193 | | SORT_ACTIVITY, |
194 | | SORT_END, |
195 | | }; |
196 | | |
197 | | static struct window_client_itemdata * |
198 | | window_client_add_item(struct window_client_modedata *data) |
199 | 0 | { |
200 | 0 | struct window_client_itemdata *item; |
201 | |
|
202 | 0 | data->item_list = xreallocarray(data->item_list, data->item_size + 1, |
203 | 0 | sizeof *data->item_list); |
204 | 0 | item = data->item_list[data->item_size++] = xcalloc(1, sizeof *item); |
205 | 0 | return (item); |
206 | 0 | } |
207 | | |
208 | | static void |
209 | | window_client_free_item(struct window_client_itemdata *item) |
210 | 0 | { |
211 | 0 | server_client_unref(item->c); |
212 | 0 | free(item->ttyname); |
213 | 0 | free(item); |
214 | 0 | } |
215 | | |
216 | | static void |
217 | | window_client_build(void *modedata, struct sort_criteria *sort_crit, |
218 | | __unused uint64_t *tag, const char *filter) |
219 | 0 | { |
220 | 0 | struct window_client_modedata *data = modedata; |
221 | 0 | struct window_client_itemdata *item; |
222 | 0 | u_int i, n; |
223 | 0 | struct client *c, **l; |
224 | 0 | char *text, *cp; |
225 | |
|
226 | 0 | for (i = 0; i < data->item_size; i++) |
227 | 0 | window_client_free_item(data->item_list[i]); |
228 | 0 | free(data->item_list); |
229 | 0 | data->item_list = NULL; |
230 | 0 | data->item_size = 0; |
231 | |
|
232 | 0 | l = sort_get_clients(&n, sort_crit); |
233 | 0 | for (i = 0; i < n; i++) { |
234 | 0 | if (l[i]->session == NULL || |
235 | 0 | (l[i]->flags & CLIENT_UNATTACHEDFLAGS)) |
236 | 0 | continue; |
237 | | |
238 | 0 | item = window_client_add_item(data); |
239 | 0 | item->c = l[i]; |
240 | 0 | item->ttyname = xstrdup(l[i]->ttyname); |
241 | |
|
242 | 0 | l[i]->references++; |
243 | 0 | } |
244 | |
|
245 | 0 | for (i = 0; i < data->item_size; i++) { |
246 | 0 | item = data->item_list[i]; |
247 | 0 | c = item->c; |
248 | |
|
249 | 0 | if (filter != NULL) { |
250 | 0 | cp = format_single(NULL, filter, c, NULL, NULL, NULL); |
251 | 0 | if (!format_true(cp)) { |
252 | 0 | free(cp); |
253 | 0 | continue; |
254 | 0 | } |
255 | 0 | free(cp); |
256 | 0 | } |
257 | | |
258 | 0 | text = format_single(NULL, data->format, c, NULL, NULL, NULL); |
259 | 0 | mode_tree_add(data->data, NULL, item, (uint64_t)c, c->name, |
260 | 0 | text, -1); |
261 | 0 | free(text); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | static void |
266 | | window_client_draw_info(__unused void *modedata, void *itemdata, |
267 | | struct screen_write_ctx *ctx, u_int sx, u_int sy) |
268 | 0 | { |
269 | 0 | struct window_client_itemdata *item = itemdata; |
270 | 0 | struct client *c = item->c; |
271 | 0 | struct screen *s = ctx->s; |
272 | 0 | struct window *w = c->session->curw->window; |
273 | 0 | struct grid_cell gc; |
274 | 0 | u_int cx = s->cx, cy = s->cy, i; |
275 | 0 | struct format_tree *ft; |
276 | 0 | char *expanded; |
277 | |
|
278 | 0 | ft = format_create_defaults(NULL, c, NULL, NULL, NULL); |
279 | 0 | if (c->tty.term->flags & TERM_INVALIDMS) |
280 | 0 | format_add(ft, "clipboard_invalid", "1"); |
281 | 0 | else |
282 | 0 | format_add(ft, "clipboard_invalid", "0"); |
283 | |
|
284 | 0 | screen_write_cursormove(ctx, cx, cy, 0); |
285 | 0 | for (i = 0; i < nitems(window_client_info_lines); i++) { |
286 | 0 | if (i == sy) |
287 | 0 | break; |
288 | 0 | expanded = format_expand(ft, window_client_info_lines[i]); |
289 | 0 | screen_write_cursormove(ctx, cx, cy + i, 0); |
290 | 0 | format_draw(ctx, &grid_default_cell, sx, expanded, NULL, 0); |
291 | 0 | free(expanded); |
292 | 0 | } |
293 | 0 | if (sx > 14 && i < sy) { |
294 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
295 | 0 | style_apply(&gc, w->options, "tree-mode-border-style", NULL); |
296 | 0 | screen_write_cursormove(ctx, cx + 14, cy + i, 0); |
297 | 0 | screen_write_vline(ctx, sy - i, 0, 0, &gc); |
298 | 0 | } |
299 | |
|
300 | 0 | format_free(ft); |
301 | 0 | } |
302 | | |
303 | | static void |
304 | | window_client_draw(void *modedata, void *itemdata, |
305 | | struct screen_write_ctx *ctx, u_int sx, u_int sy) |
306 | 0 | { |
307 | 0 | struct window_client_modedata *data = modedata; |
308 | 0 | struct window_client_itemdata *item = itemdata; |
309 | 0 | struct client *c = item->c; |
310 | 0 | struct session *session = c->session; |
311 | 0 | struct screen *s = ctx->s; |
312 | 0 | struct window *w; |
313 | 0 | struct window_pane *wp; |
314 | 0 | struct grid_cell gc; |
315 | 0 | u_int cx = s->cx, cy = s->cy, lines, at; |
316 | |
|
317 | 0 | if (session == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) |
318 | 0 | return; |
319 | 0 | if (data->preview_is_info) { |
320 | 0 | window_client_draw_info(modedata, itemdata, ctx, sx, sy); |
321 | 0 | return; |
322 | 0 | } |
323 | 0 | w = session->curw->window; |
324 | 0 | wp = w->active; |
325 | 0 | if (data->hide_preview_this_pane && wp == data->wp) { |
326 | 0 | if (!TAILQ_EMPTY(&w->last_panes)) |
327 | 0 | wp = TAILQ_FIRST(&w->last_panes); |
328 | 0 | else |
329 | 0 | wp = NULL; |
330 | 0 | } |
331 | |
|
332 | 0 | lines = status_line_size(c); |
333 | 0 | if (lines >= sy) |
334 | 0 | lines = 0; |
335 | 0 | if (status_at_line(c) == 0) |
336 | 0 | at = lines; |
337 | 0 | else |
338 | 0 | at = 0; |
339 | |
|
340 | 0 | screen_write_cursormove(ctx, cx, cy + at, 0); |
341 | 0 | if (wp != NULL) |
342 | 0 | screen_write_preview(ctx, &wp->base, sx, sy - 2 - lines); |
343 | |
|
344 | 0 | if (at != 0) |
345 | 0 | screen_write_cursormove(ctx, cx, cy + 2, 0); |
346 | 0 | else |
347 | 0 | screen_write_cursormove(ctx, cx, cy + sy - 1 - lines, 0); |
348 | 0 | memcpy(&gc, &grid_default_cell, sizeof gc); |
349 | 0 | style_apply(&gc, w->options, "tree-mode-border-style", NULL); |
350 | 0 | screen_write_hline(ctx, sx, 0, 0, BOX_LINES_DEFAULT, &gc); |
351 | |
|
352 | 0 | if (at != 0) |
353 | 0 | screen_write_cursormove(ctx, cx, cy, 0); |
354 | 0 | else |
355 | 0 | screen_write_cursormove(ctx, cx, cy + sy - lines, 0); |
356 | 0 | screen_write_fast_copy(ctx, &c->status.screen, 0, 0, sx, lines); |
357 | 0 | } |
358 | | |
359 | | static void |
360 | | window_client_menu(void *modedata, struct client *c, key_code key) |
361 | 0 | { |
362 | 0 | struct window_client_modedata *data = modedata; |
363 | 0 | struct window_pane *wp = data->wp; |
364 | 0 | struct window_mode_entry *wme; |
365 | |
|
366 | 0 | wme = TAILQ_FIRST(&wp->modes); |
367 | 0 | if (wme == NULL || wme->data != modedata) |
368 | 0 | return; |
369 | 0 | window_client_key(wme, c, NULL, NULL, key, NULL); |
370 | 0 | } |
371 | | |
372 | | static key_code |
373 | | window_client_get_key(void *modedata, void *itemdata, u_int line) |
374 | 0 | { |
375 | 0 | struct window_client_modedata *data = modedata; |
376 | 0 | struct window_client_itemdata *item = itemdata; |
377 | 0 | struct format_tree *ft; |
378 | 0 | char *expanded; |
379 | 0 | key_code key; |
380 | |
|
381 | 0 | ft = format_create(NULL, NULL, FORMAT_NONE, 0); |
382 | 0 | format_defaults(ft, item->c, NULL, 0, NULL); |
383 | 0 | format_add(ft, "line", "%u", line); |
384 | |
|
385 | 0 | expanded = format_expand(ft, data->key_format); |
386 | 0 | key = key_string_lookup_string(expanded); |
387 | 0 | free(expanded); |
388 | 0 | format_free(ft); |
389 | 0 | return (key); |
390 | 0 | } |
391 | | |
392 | | static void |
393 | | window_client_sort(struct sort_criteria *sort_crit) |
394 | 0 | { |
395 | 0 | sort_crit->order_seq = window_client_order_seq; |
396 | 0 | if (sort_crit->order == SORT_END) |
397 | 0 | sort_crit->order = sort_crit->order_seq[0]; |
398 | 0 | } |
399 | | |
400 | | static const char* window_client_help_lines[] = { |
401 | | "#[fg=themelightgrey]" |
402 | | " i #[#{E:tree-mode-border-style},acs]x#[default] Toggle info view", |
403 | | "#[fg=themelightgrey]" |
404 | | " Enter #[#{E:tree-mode-border-style},acs]x#[default] Choose selected %1", |
405 | | "#[fg=themelightgrey]" |
406 | | " d #[#{E:tree-mode-border-style},acs]x#[default] Detach selected %1", |
407 | | "#[fg=themelightgrey]" |
408 | | " D #[#{E:tree-mode-border-style},acs]x#[default] Detach tagged %1s", |
409 | | "#[fg=themelightgrey]" |
410 | | " x #[#{E:tree-mode-border-style},acs]x#[default] Detach selected %1", |
411 | | "#[fg=themelightgrey]" |
412 | | " X #[#{E:tree-mode-border-style},acs]x#[default] Detach tagged %1s", |
413 | | "#[fg=themelightgrey]" |
414 | | " z #[#{E:tree-mode-border-style},acs]x#[default] Suspend selected %1", |
415 | | "#[fg=themelightgrey]" |
416 | | " Z #[#{E:tree-mode-border-style},acs]x#[default] Suspend tagged %1s", |
417 | | "#[fg=themelightgrey]" |
418 | | " f #[#{E:tree-mode-border-style},acs]x#[default] Enter a filter", |
419 | | NULL |
420 | | }; |
421 | | |
422 | | static const char** |
423 | | window_client_help(u_int *width, const char **item) |
424 | 0 | { |
425 | 0 | *width = 0; |
426 | 0 | *item = "client"; |
427 | 0 | return (window_client_help_lines); |
428 | 0 | } |
429 | | |
430 | | static struct screen * |
431 | | window_client_init(struct window_mode_entry *wme, |
432 | | __unused struct cmdq_item *item, __unused struct cmd_find_state *fs, |
433 | | struct args *args) |
434 | 0 | { |
435 | 0 | struct window_pane *wp = wme->wp; |
436 | 0 | struct window_client_modedata *data; |
437 | 0 | struct screen *s; |
438 | |
|
439 | 0 | wme->data = data = xcalloc(1, sizeof *data); |
440 | 0 | data->wp = wp; |
441 | 0 | data->hide_preview_this_pane = args != NULL && args_has(args, 'h'); |
442 | 0 | data->preview_is_info = args != NULL && args_has(args, 'i'); |
443 | |
|
444 | 0 | if (args == NULL || !args_has(args, 'F')) |
445 | 0 | data->format = xstrdup(WINDOW_CLIENT_DEFAULT_FORMAT); |
446 | 0 | else |
447 | 0 | data->format = xstrdup(args_get(args, 'F')); |
448 | 0 | if (args == NULL || !args_has(args, 'K')) |
449 | 0 | data->key_format = xstrdup(WINDOW_CLIENT_DEFAULT_KEY_FORMAT); |
450 | 0 | else |
451 | 0 | data->key_format = xstrdup(args_get(args, 'K')); |
452 | 0 | if (args == NULL || args_count(args) == 0) |
453 | 0 | data->command = xstrdup(WINDOW_CLIENT_DEFAULT_COMMAND); |
454 | 0 | else |
455 | 0 | data->command = xstrdup(args_string(args, 0)); |
456 | |
|
457 | 0 | data->data = mode_tree_start(wp, args, window_client_build, |
458 | 0 | window_client_draw, NULL, window_client_menu, NULL, |
459 | 0 | window_client_get_key, NULL, window_client_sort, |
460 | 0 | window_client_help, data, window_client_menu_items, &s); |
461 | 0 | mode_tree_zoom(data->data, args); |
462 | |
|
463 | 0 | if (data->preview_is_info) |
464 | 0 | mode_tree_view_name(data->data, "info"); |
465 | 0 | else |
466 | 0 | mode_tree_view_name(data->data, "preview"); |
467 | |
|
468 | 0 | mode_tree_build(data->data); |
469 | 0 | mode_tree_draw(data->data); |
470 | |
|
471 | 0 | return (s); |
472 | 0 | } |
473 | | |
474 | | static void |
475 | | window_client_free(struct window_mode_entry *wme) |
476 | 0 | { |
477 | 0 | struct window_client_modedata *data = wme->data; |
478 | 0 | u_int i; |
479 | |
|
480 | 0 | if (data == NULL) |
481 | 0 | return; |
482 | | |
483 | 0 | mode_tree_free(data->data); |
484 | |
|
485 | 0 | for (i = 0; i < data->item_size; i++) |
486 | 0 | window_client_free_item(data->item_list[i]); |
487 | 0 | free(data->item_list); |
488 | |
|
489 | 0 | free(data->format); |
490 | 0 | free(data->key_format); |
491 | 0 | free(data->command); |
492 | |
|
493 | 0 | free(data); |
494 | 0 | } |
495 | | |
496 | | static void |
497 | | window_client_resize(struct window_mode_entry *wme, u_int sx, u_int sy) |
498 | 0 | { |
499 | 0 | struct window_client_modedata *data = wme->data; |
500 | |
|
501 | 0 | mode_tree_resize(data->data, sx, sy); |
502 | 0 | } |
503 | | |
504 | | static void |
505 | | window_client_update(struct window_mode_entry *wme) |
506 | 0 | { |
507 | 0 | struct window_client_modedata *data = wme->data; |
508 | |
|
509 | 0 | mode_tree_build(data->data); |
510 | 0 | mode_tree_draw(data->data); |
511 | 0 | data->wp->flags |= PANE_REDRAW; |
512 | 0 | } |
513 | | |
514 | | static void |
515 | | window_client_do_detach(void *modedata, void *itemdata, |
516 | | __unused struct client *c, key_code key) |
517 | 0 | { |
518 | 0 | struct window_client_modedata *data = modedata; |
519 | 0 | struct window_client_itemdata *item = itemdata; |
520 | |
|
521 | 0 | if (item == mode_tree_get_current(data->data)) |
522 | 0 | mode_tree_down(data->data, 0); |
523 | 0 | if (key == 'd' || key == 'D') |
524 | 0 | server_client_detach(item->c, MSG_DETACH); |
525 | 0 | else if (key == 'x' || key == 'X') |
526 | 0 | server_client_detach(item->c, MSG_DETACHKILL); |
527 | 0 | else if (key == 'z' || key == 'Z') |
528 | 0 | server_client_suspend(item->c); |
529 | 0 | } |
530 | | |
531 | | static void |
532 | | window_client_key(struct window_mode_entry *wme, struct client *c, |
533 | | __unused struct session *s, __unused struct winlink *wl, key_code key, |
534 | | struct mouse_event *m) |
535 | 0 | { |
536 | 0 | struct window_pane *wp = wme->wp; |
537 | 0 | struct window_client_modedata *data = wme->data; |
538 | 0 | struct mode_tree_data *mtd = data->data; |
539 | 0 | struct window_client_itemdata *item; |
540 | 0 | int finished; |
541 | |
|
542 | 0 | finished = mode_tree_key(mtd, c, &key, m, NULL, NULL); |
543 | 0 | switch (key) { |
544 | 0 | case 'd': |
545 | 0 | case 'x': |
546 | 0 | case 'z': |
547 | 0 | item = mode_tree_get_current(mtd); |
548 | 0 | window_client_do_detach(data, item, c, key); |
549 | 0 | mode_tree_build(mtd); |
550 | 0 | break; |
551 | 0 | case 'D': |
552 | 0 | case 'X': |
553 | 0 | case 'Z': |
554 | 0 | mode_tree_each_tagged(mtd, window_client_do_detach, c, key, 0); |
555 | 0 | mode_tree_build(mtd); |
556 | 0 | break; |
557 | 0 | case 'i': |
558 | 0 | data->preview_is_info = !data->preview_is_info; |
559 | 0 | if (data->preview_is_info) |
560 | 0 | mode_tree_view_name(mtd, "info"); |
561 | 0 | else |
562 | 0 | mode_tree_view_name(mtd, "preview"); |
563 | 0 | mode_tree_build(mtd); |
564 | 0 | break; |
565 | 0 | case '\r': |
566 | 0 | item = mode_tree_get_current(mtd); |
567 | 0 | mode_tree_run_command(c, NULL, data->command, item->ttyname); |
568 | 0 | finished = 1; |
569 | 0 | break; |
570 | 0 | } |
571 | 0 | if (finished || server_client_how_many() == 0) |
572 | 0 | window_pane_reset_mode(wp); |
573 | 0 | else { |
574 | 0 | mode_tree_draw(mtd); |
575 | 0 | wp->flags |= PANE_REDRAW; |
576 | 0 | } |
577 | 0 | } |