Coverage Report

Created: 2025-07-18 06:48

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