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