/src/tmux/window-client.c
Line | Count | Source |
1 | | /* $OpenBSD$ */ |
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 cmd_find_state *, struct args *); |
30 | | static void window_client_free(struct window_mode_entry *); |
31 | | static void window_client_resize(struct window_mode_entry *, u_int, |
32 | | u_int); |
33 | | static void window_client_update(struct window_mode_entry *); |
34 | | static void window_client_key(struct window_mode_entry *, |
35 | | struct client *, struct session *, |
36 | | struct winlink *, key_code, struct mouse_event *); |
37 | | |
38 | 0 | #define WINDOW_CLIENT_DEFAULT_COMMAND "detach-client -t '%%'" |
39 | | |
40 | | #define WINDOW_CLIENT_DEFAULT_FORMAT \ |
41 | 0 | "#{t/p:client_activity}: session #{session_name}" |
42 | | |
43 | | #define WINDOW_CLIENT_DEFAULT_KEY_FORMAT \ |
44 | 0 | "#{?#{e|<:#{line},10}," \ |
45 | 0 | "#{line}" \ |
46 | 0 | ",#{e|<:#{line},36}," \ |
47 | 0 | "M-#{a:#{e|+:97,#{e|-:#{line},10}}}" \ |
48 | 0 | "}" |
49 | | |
50 | | static const struct menu_item window_client_menu_items[] = { |
51 | | { "Detach", 'd', NULL }, |
52 | | { "Detach Tagged", 'D', NULL }, |
53 | | { "", KEYC_NONE, NULL }, |
54 | | { "Tag", 't', NULL }, |
55 | | { "Tag All", '\024', NULL }, |
56 | | { "Tag None", 'T', NULL }, |
57 | | { "", KEYC_NONE, NULL }, |
58 | | { "Cancel", 'q', NULL }, |
59 | | |
60 | | { NULL, KEYC_NONE, NULL } |
61 | | }; |
62 | | |
63 | | const struct window_mode window_client_mode = { |
64 | | .name = "client-mode", |
65 | | .default_format = WINDOW_CLIENT_DEFAULT_FORMAT, |
66 | | |
67 | | .init = window_client_init, |
68 | | .free = window_client_free, |
69 | | .resize = window_client_resize, |
70 | | .update = window_client_update, |
71 | | .key = window_client_key, |
72 | | }; |
73 | | |
74 | | struct window_client_itemdata { |
75 | | struct client *c; |
76 | | }; |
77 | | |
78 | | struct window_client_modedata { |
79 | | struct window_pane *wp; |
80 | | |
81 | | struct mode_tree_data *data; |
82 | | char *format; |
83 | | char *key_format; |
84 | | char *command; |
85 | | int hide_preview_this_pane; |
86 | | |
87 | | struct window_client_itemdata **item_list; |
88 | | u_int item_size; |
89 | | }; |
90 | | |
91 | | static enum sort_order window_client_order_seq[] = { |
92 | | SORT_NAME, |
93 | | SORT_SIZE, |
94 | | SORT_CREATION, |
95 | | SORT_ACTIVITY, |
96 | | SORT_END, |
97 | | }; |
98 | | |
99 | | static struct window_client_itemdata * |
100 | | window_client_add_item(struct window_client_modedata *data) |
101 | 0 | { |
102 | 0 | struct window_client_itemdata *item; |
103 | |
|
104 | 0 | data->item_list = xreallocarray(data->item_list, data->item_size + 1, |
105 | 0 | sizeof *data->item_list); |
106 | 0 | item = data->item_list[data->item_size++] = xcalloc(1, sizeof *item); |
107 | 0 | return (item); |
108 | 0 | } |
109 | | |
110 | | static void |
111 | | window_client_free_item(struct window_client_itemdata *item) |
112 | 0 | { |
113 | 0 | server_client_unref(item->c); |
114 | 0 | free(item); |
115 | 0 | } |
116 | | |
117 | | static void |
118 | | window_client_build(void *modedata, struct sort_criteria *sort_crit, |
119 | | __unused uint64_t *tag, const char *filter) |
120 | 0 | { |
121 | 0 | struct window_client_modedata *data = modedata; |
122 | 0 | struct window_client_itemdata *item; |
123 | 0 | u_int i, n; |
124 | 0 | struct client *c, **l; |
125 | 0 | char *text, *cp; |
126 | |
|
127 | 0 | for (i = 0; i < data->item_size; i++) |
128 | 0 | window_client_free_item(data->item_list[i]); |
129 | 0 | free(data->item_list); |
130 | 0 | data->item_list = NULL; |
131 | 0 | data->item_size = 0; |
132 | |
|
133 | 0 | l = sort_get_clients(&n, sort_crit); |
134 | 0 | for (i = 0; i < n; i++) { |
135 | 0 | if (l[i]->session == NULL || |
136 | 0 | (l[i]->flags & CLIENT_UNATTACHEDFLAGS)) |
137 | 0 | continue; |
138 | | |
139 | 0 | item = window_client_add_item(data); |
140 | 0 | item->c = l[i]; |
141 | |
|
142 | 0 | l[i]->references++; |
143 | 0 | } |
144 | |
|
145 | 0 | for (i = 0; i < data->item_size; i++) { |
146 | 0 | item = data->item_list[i]; |
147 | 0 | c = item->c; |
148 | |
|
149 | 0 | if (filter != NULL) { |
150 | 0 | cp = format_single(NULL, filter, c, NULL, NULL, NULL); |
151 | 0 | if (!format_true(cp)) { |
152 | 0 | free(cp); |
153 | 0 | continue; |
154 | 0 | } |
155 | 0 | free(cp); |
156 | 0 | } |
157 | | |
158 | 0 | text = format_single(NULL, data->format, c, NULL, NULL, NULL); |
159 | 0 | mode_tree_add(data->data, NULL, item, (uint64_t)c, c->name, |
160 | 0 | text, -1); |
161 | 0 | free(text); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | static void |
166 | | window_client_draw(void *modedata, void *itemdata, |
167 | | struct screen_write_ctx *ctx, u_int sx, u_int sy) |
168 | 0 | { |
169 | 0 | struct window_client_modedata *data = modedata; |
170 | 0 | struct window_client_itemdata *item = itemdata; |
171 | 0 | struct client *c = item->c; |
172 | 0 | struct screen *s = ctx->s; |
173 | 0 | struct window_pane *wp; |
174 | 0 | u_int cx = s->cx, cy = s->cy, lines, at; |
175 | |
|
176 | 0 | if (c->session == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) |
177 | 0 | return; |
178 | 0 | wp = c->session->curw->window->active; |
179 | 0 | if (data->hide_preview_this_pane && wp == data->wp) { |
180 | 0 | if (!TAILQ_EMPTY(&c->session->curw->window->last_panes)) |
181 | 0 | wp = TAILQ_FIRST(&c->session->curw->window->last_panes); |
182 | 0 | else |
183 | 0 | wp = NULL; |
184 | 0 | } |
185 | |
|
186 | 0 | lines = status_line_size(c); |
187 | 0 | if (lines >= sy) |
188 | 0 | lines = 0; |
189 | 0 | if (status_at_line(c) == 0) |
190 | 0 | at = lines; |
191 | 0 | else |
192 | 0 | at = 0; |
193 | |
|
194 | 0 | screen_write_cursormove(ctx, cx, cy + at, 0); |
195 | 0 | if (wp != NULL) |
196 | 0 | screen_write_preview(ctx, &wp->base, sx, sy - 2 - lines); |
197 | |
|
198 | 0 | if (at != 0) |
199 | 0 | screen_write_cursormove(ctx, cx, cy + 2, 0); |
200 | 0 | else |
201 | 0 | screen_write_cursormove(ctx, cx, cy + sy - 1 - lines, 0); |
202 | 0 | screen_write_hline(ctx, sx, 0, 0, BOX_LINES_DEFAULT, NULL); |
203 | |
|
204 | 0 | if (at != 0) |
205 | 0 | screen_write_cursormove(ctx, cx, cy, 0); |
206 | 0 | else |
207 | 0 | screen_write_cursormove(ctx, cx, cy + sy - lines, 0); |
208 | 0 | screen_write_fast_copy(ctx, &c->status.screen, 0, 0, sx, lines); |
209 | 0 | } |
210 | | |
211 | | static void |
212 | | window_client_menu(void *modedata, struct client *c, key_code key) |
213 | 0 | { |
214 | 0 | struct window_client_modedata *data = modedata; |
215 | 0 | struct window_pane *wp = data->wp; |
216 | 0 | struct window_mode_entry *wme; |
217 | |
|
218 | 0 | wme = TAILQ_FIRST(&wp->modes); |
219 | 0 | if (wme == NULL || wme->data != modedata) |
220 | 0 | return; |
221 | 0 | window_client_key(wme, c, NULL, NULL, key, NULL); |
222 | 0 | } |
223 | | |
224 | | static key_code |
225 | | window_client_get_key(void *modedata, void *itemdata, u_int line) |
226 | 0 | { |
227 | 0 | struct window_client_modedata *data = modedata; |
228 | 0 | struct window_client_itemdata *item = itemdata; |
229 | 0 | struct format_tree *ft; |
230 | 0 | char *expanded; |
231 | 0 | key_code key; |
232 | |
|
233 | 0 | ft = format_create(NULL, NULL, FORMAT_NONE, 0); |
234 | 0 | format_defaults(ft, item->c, NULL, 0, NULL); |
235 | 0 | format_add(ft, "line", "%u", line); |
236 | |
|
237 | 0 | expanded = format_expand(ft, data->key_format); |
238 | 0 | key = key_string_lookup_string(expanded); |
239 | 0 | free(expanded); |
240 | 0 | format_free(ft); |
241 | 0 | return (key); |
242 | 0 | } |
243 | | |
244 | | static void |
245 | | window_client_sort(struct sort_criteria *sort_crit) |
246 | 0 | { |
247 | 0 | sort_crit->order_seq = window_client_order_seq; |
248 | 0 | if (sort_crit->order == SORT_END) |
249 | 0 | sort_crit->order = sort_crit->order_seq[0]; |
250 | 0 | } |
251 | | |
252 | | static const char* window_client_help_lines[] = { |
253 | | "\r\033[1m Enter \033[0m\016x\017 \033[0mChoose selected %1\n", |
254 | | "\r\033[1m d \033[0m\016x\017 \033[0mDetach selected %1\n", |
255 | | "\r\033[1m D \033[0m\016x\017 \033[0mDetach tagged %1s\n", |
256 | | "\r\033[1m x \033[0m\016x\017 \033[0mDetach selected %1\n", |
257 | | "\r\033[1m X \033[0m\016x\017 \033[0mDetach tagged %1s\n", |
258 | | "\r\033[1m z \033[0m\016x\017 \033[0mSuspend selected %1\n", |
259 | | "\r\033[1m Z \033[0m\016x\017 \033[0mSuspend tagged %1s\n", |
260 | | "\r\033[1m f \033[0m\016x\017 \033[0mEnter a filter\n", |
261 | | NULL |
262 | | }; |
263 | | |
264 | | static const char** |
265 | | window_client_help(u_int *width, const char **item) |
266 | 0 | { |
267 | 0 | *width = 0; |
268 | 0 | *item = "client"; |
269 | 0 | return (window_client_help_lines); |
270 | 0 | } |
271 | | |
272 | | static struct screen * |
273 | | window_client_init(struct window_mode_entry *wme, |
274 | | __unused struct cmd_find_state *fs, struct args *args) |
275 | 0 | { |
276 | 0 | struct window_pane *wp = wme->wp; |
277 | 0 | struct window_client_modedata *data; |
278 | 0 | struct screen *s; |
279 | |
|
280 | 0 | wme->data = data = xcalloc(1, sizeof *data); |
281 | 0 | data->wp = wp; |
282 | 0 | data->hide_preview_this_pane = args != NULL && args_has(args, 'h'); |
283 | |
|
284 | 0 | if (args == NULL || !args_has(args, 'F')) |
285 | 0 | data->format = xstrdup(WINDOW_CLIENT_DEFAULT_FORMAT); |
286 | 0 | else |
287 | 0 | data->format = xstrdup(args_get(args, 'F')); |
288 | 0 | if (args == NULL || !args_has(args, 'K')) |
289 | 0 | data->key_format = xstrdup(WINDOW_CLIENT_DEFAULT_KEY_FORMAT); |
290 | 0 | else |
291 | 0 | data->key_format = xstrdup(args_get(args, 'K')); |
292 | 0 | if (args == NULL || args_count(args) == 0) |
293 | 0 | data->command = xstrdup(WINDOW_CLIENT_DEFAULT_COMMAND); |
294 | 0 | else |
295 | 0 | data->command = xstrdup(args_string(args, 0)); |
296 | |
|
297 | 0 | data->data = mode_tree_start(wp, args, window_client_build, |
298 | 0 | window_client_draw, NULL, window_client_menu, NULL, |
299 | 0 | window_client_get_key, NULL, window_client_sort, |
300 | 0 | window_client_help, data, window_client_menu_items, &s); |
301 | 0 | mode_tree_zoom(data->data, args); |
302 | |
|
303 | 0 | mode_tree_build(data->data); |
304 | 0 | mode_tree_draw(data->data); |
305 | |
|
306 | 0 | return (s); |
307 | 0 | } |
308 | | |
309 | | static void |
310 | | window_client_free(struct window_mode_entry *wme) |
311 | 0 | { |
312 | 0 | struct window_client_modedata *data = wme->data; |
313 | 0 | u_int i; |
314 | |
|
315 | 0 | if (data == NULL) |
316 | 0 | return; |
317 | | |
318 | 0 | mode_tree_free(data->data); |
319 | |
|
320 | 0 | for (i = 0; i < data->item_size; i++) |
321 | 0 | window_client_free_item(data->item_list[i]); |
322 | 0 | free(data->item_list); |
323 | |
|
324 | 0 | free(data->format); |
325 | 0 | free(data->key_format); |
326 | 0 | free(data->command); |
327 | |
|
328 | 0 | free(data); |
329 | 0 | } |
330 | | |
331 | | static void |
332 | | window_client_resize(struct window_mode_entry *wme, u_int sx, u_int sy) |
333 | 0 | { |
334 | 0 | struct window_client_modedata *data = wme->data; |
335 | |
|
336 | 0 | mode_tree_resize(data->data, sx, sy); |
337 | 0 | } |
338 | | |
339 | | static void |
340 | | window_client_update(struct window_mode_entry *wme) |
341 | 0 | { |
342 | 0 | struct window_client_modedata *data = wme->data; |
343 | |
|
344 | 0 | mode_tree_build(data->data); |
345 | 0 | mode_tree_draw(data->data); |
346 | 0 | data->wp->flags |= PANE_REDRAW; |
347 | 0 | } |
348 | | |
349 | | static void |
350 | | window_client_do_detach(void *modedata, void *itemdata, |
351 | | __unused struct client *c, key_code key) |
352 | 0 | { |
353 | 0 | struct window_client_modedata *data = modedata; |
354 | 0 | struct window_client_itemdata *item = itemdata; |
355 | |
|
356 | 0 | if (item == mode_tree_get_current(data->data)) |
357 | 0 | mode_tree_down(data->data, 0); |
358 | 0 | if (key == 'd' || key == 'D') |
359 | 0 | server_client_detach(item->c, MSG_DETACH); |
360 | 0 | else if (key == 'x' || key == 'X') |
361 | 0 | server_client_detach(item->c, MSG_DETACHKILL); |
362 | 0 | else if (key == 'z' || key == 'Z') |
363 | 0 | server_client_suspend(item->c); |
364 | 0 | } |
365 | | |
366 | | static void |
367 | | window_client_key(struct window_mode_entry *wme, struct client *c, |
368 | | __unused struct session *s, __unused struct winlink *wl, key_code key, |
369 | | struct mouse_event *m) |
370 | 0 | { |
371 | 0 | struct window_pane *wp = wme->wp; |
372 | 0 | struct window_client_modedata *data = wme->data; |
373 | 0 | struct mode_tree_data *mtd = data->data; |
374 | 0 | struct window_client_itemdata *item; |
375 | 0 | int finished; |
376 | |
|
377 | 0 | finished = mode_tree_key(mtd, c, &key, m, NULL, NULL); |
378 | 0 | switch (key) { |
379 | 0 | case 'd': |
380 | 0 | case 'x': |
381 | 0 | case 'z': |
382 | 0 | item = mode_tree_get_current(mtd); |
383 | 0 | window_client_do_detach(data, item, c, key); |
384 | 0 | mode_tree_build(mtd); |
385 | 0 | break; |
386 | 0 | case 'D': |
387 | 0 | case 'X': |
388 | 0 | case 'Z': |
389 | 0 | mode_tree_each_tagged(mtd, window_client_do_detach, c, key, 0); |
390 | 0 | mode_tree_build(mtd); |
391 | 0 | break; |
392 | 0 | case '\r': |
393 | 0 | item = mode_tree_get_current(mtd); |
394 | 0 | mode_tree_run_command(c, NULL, data->command, item->c->ttyname); |
395 | 0 | finished = 1; |
396 | 0 | break; |
397 | 0 | } |
398 | 0 | if (finished || server_client_how_many() == 0) |
399 | 0 | window_pane_reset_mode(wp); |
400 | 0 | else { |
401 | 0 | mode_tree_draw(mtd); |
402 | 0 | wp->flags |= PANE_REDRAW; |
403 | 0 | } |
404 | 0 | } |