ztmux 3.7.20

A Rust port of tmux — the full terminal multiplexer, server and client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright (c) 2017 Nicholas Marriott <nicholas.marriott@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::libc::strcmp;
use crate::*;

static WINDOW_CLIENT_DEFAULT_COMMAND: &str = "detach-client -t '%%'";
static WINDOW_CLIENT_DEFAULT_FORMAT: &str = "#{t/p:client_activity}: session #{session_name}";
static WINDOW_CLIENT_DEFAULT_KEY_FORMAT: &str =
    "#{?#{e|<:#{line},10},#{line},#{?#{e|<:#{line},36},M-#{a:#{e|+:97,#{e|-:#{line},10}}},}}";

static WINDOW_CLIENT_MENU_ITEMS: [menu_item; 8] = [
    menu_item::new("Detach", b'd' as _, null()),
    menu_item::new("Detach Tagged", b'D' as _, null()),
    menu_item::new("", KEYC_NONE, null()),
    menu_item::new("Tag", b't' as _, null()),
    menu_item::new("Tag All", b'\x14' as _, null()),
    menu_item::new("Tag None", b'T' as _, null()),
    menu_item::new("", KEYC_NONE, null()),
    menu_item::new("Cancel", b'q' as _, null()),
];

pub static WINDOW_CLIENT_MODE: window_mode = window_mode {
    name: "client-mode",
    default_format: Some(WINDOW_CLIENT_DEFAULT_FORMAT),

    init: window_client_init,
    free: window_client_free,
    resize: window_client_resize,
    update: Some(window_client_update),
    key: Some(window_client_key),
    key_table: None,
    command: None,
    formats: None,
};

#[derive(num_enum::TryFromPrimitive)]
#[repr(u32)]
pub enum window_client_sort_type {
    WINDOW_CLIENT_BY_NAME,
    WINDOW_CLIENT_BY_SIZE,
    WINDOW_CLIENT_BY_CREATION_TIME,
    WINDOW_CLIENT_BY_ACTIVITY_TIME,
}
static WINDOW_CLIENT_SORT_LIST: [&str; 4] = ["name", "size", "creation", "activity"];

#[repr(C)]
pub struct window_client_itemdata {
    c: *mut client,
}

#[repr(C)]
pub struct window_client_modedata {
    wp: *mut window_pane,

    data: *mut mode_tree_data,
    /// Owned format strings. Dropped with the struct in `window_client_free`, where C
    /// freed each by hand. Read via the `*_ptr` accessors.
    format: CString,
    key_format: CString,
    command: CString,

    item_list: Vec<*mut window_client_itemdata>,
}

impl window_client_modedata {
    fn format_ptr(&self) -> *const u8 {
        self.format.as_ptr().cast()
    }
    fn key_format_ptr(&self) -> *const u8 {
        self.key_format.as_ptr().cast()
    }
    fn command_ptr(&self) -> *const u8 {
        self.command.as_ptr().cast()
    }
}

/// C `vendor/tmux/window-client.c:193`: `static struct window_client_itemdata *window_client_add_item(struct window_client_modedata *data)`
pub unsafe fn window_client_add_item(
    data: *mut window_client_modedata,
) -> *mut window_client_itemdata {
    unsafe {
        (*data)
            .item_list
            .push(xcalloc1::<window_client_itemdata>() as *mut window_client_itemdata);

        (*data).item_list.last().copied().unwrap()
    }
}

/// C `vendor/tmux/window-client.c:204`: `static void window_client_free_item(struct window_client_itemdata *item)`
pub unsafe fn window_client_free_item(item: *mut window_client_itemdata) {
    unsafe {
        server_client_unref((*item).c);
        free_(item);
    }
}

/// C `vendor/tmux/window-client.c:211`: `static void window_client_build(void *modedata, struct sort_criteria *sort_crit, __unused uint64_t *tag, const char *filter)`
pub unsafe fn window_client_build(
    modedata: NonNull<c_void>,
    sort_crit: *mut mode_tree_sort_criteria,
    _tag: *mut u64,
    filter: *const u8,
) {
    unsafe {
        let data: NonNull<window_client_modedata> = modedata.cast();
        let data = data.as_ptr();

        for item in (*data).item_list.drain(..) {
            window_client_free_item(item);
        }
        (*data).item_list = Vec::new();

        for c in crate::compat::queue::tailq_foreach(&raw mut CLIENTS).map(NonNull::as_ptr) {
            if (*c).session.is_null() || (*c).flags.intersects(CLIENT_UNATTACHEDFLAGS) {
                continue;
            }

            let item = window_client_add_item(data);
            (*item).c = c;

            (*c).references += 1;
        }

        // TODO double check this ordering is correct
        match window_client_sort_type::try_from((*sort_crit).field) {
            Ok(window_client_sort_type::WINDOW_CLIENT_BY_SIZE) => {
                (*data).item_list.sort_by(|itema, itemb| {
                    let ca = (**itema).c;
                    let cb = (**itemb).c;

                    (*cb)
                        .tty
                        .sx
                        .cmp(&(*ca).tty.sx)
                        .then_with(|| (*cb).tty.sy.cmp(&(*ca).tty.sy))
                        .then_with(|| i32_to_ordering(strcmp((*ca).name, (*cb).name)))
                        .maybe_reverse((*sort_crit).reversed)
                });
            }
            Ok(window_client_sort_type::WINDOW_CLIENT_BY_CREATION_TIME) => {
                (*data).item_list.sort_by(|itema, itemb| {
                    let ca = (**itema).c;
                    let cb = (**itemb).c;

                    timer::new(&raw const (*cb).creation_time)
                        .cmp(&timer::new(&raw const (*ca).creation_time))
                        .then_with(|| i32_to_ordering(strcmp((*ca).name, (*cb).name)))
                        .maybe_reverse((*sort_crit).reversed)
                });
            }
            Ok(window_client_sort_type::WINDOW_CLIENT_BY_ACTIVITY_TIME) => {
                (*data).item_list.sort_by(|itema, itemb| {
                    let ca = (**itema).c;
                    let cb = (**itemb).c;

                    timer::new(&raw const (*cb).activity_time)
                        .cmp(&timer::new(&raw const (*ca).activity_time))
                        .then_with(|| i32_to_ordering(strcmp((*ca).name, (*cb).name)))
                        .maybe_reverse((*sort_crit).reversed)
                });
            }
            _ => {}
        }

        for item in (*data).item_list.iter().copied() {
            let c = (*item).c;

            if !filter.is_null() {
                let cp = format_single(null_mut(), cstr_to_str(filter), c, null_mut(), null_mut(), null_mut());
                if !format_true(cp) {
                    free_(cp);
                    continue;
                }
                free_(cp);
            }

            let text = format_single(
                null_mut(),
                cstr_to_str((*data).format_ptr()),
                c,
                null_mut(),
                null_mut(),
                null_mut(),
            );
            mode_tree_add(
                (*data).data,
                null_mut(),
                item.cast(),
                c as u64,
                cstr_to_str((*c).name),
                text,
                None,
            );
            free_(text);
        }
    }
}

/// C `vendor/tmux/window-client.c:293`: `static void window_client_draw(void *modedata, void *itemdata, struct screen_write_ctx *ctx, u_int sx, u_int sy)`
pub unsafe fn window_client_draw(
    _modedata: *mut c_void,
    itemdata: Option<NonNull<c_void>>,
    ctx: *mut screen_write_ctx,
    sx: u32,
    sy: u32,
) {
    unsafe {
        let item: Option<NonNull<window_client_itemdata>> = itemdata.map(NonNull::cast);
        let c = (*item.unwrap().as_ptr()).c;
        let s = (*ctx).s;

        let cx = (*s).cx;
        let cy = (*s).cy;

        if (*c).session.is_null() || (*c).flags.intersects(CLIENT_UNATTACHEDFLAGS) {
            return;
        }
        let wp = (*(*(*(*c).session).curw).window).active;

        let mut lines = status_line_size(c);
        if lines >= sy {
            lines = 0;
        }
        let at = if status_at_line(c) == 0 { lines } else { 0 };

        screen_write_cursormove(ctx, cx as i32, (cy + at) as i32, 0);
        screen_write_preview(ctx, &raw mut (*wp).base, sx, sy - 2 - lines);

        if at != 0 {
            screen_write_cursormove(ctx, cx as i32, (cy + 2) as i32, 0);
        } else {
            screen_write_cursormove(ctx, cx as i32, (cy + sy - 1 - lines) as i32, 0);
        }
        screen_write_hline(ctx, sx, 0, 0, box_lines::BOX_LINES_DEFAULT, null());

        if at != 0 {
            screen_write_cursormove(ctx, cx as i32, cy as i32, 0);
        } else {
            screen_write_cursormove(ctx, cx as i32, (cy + sy - lines) as i32, 0);
        }
        screen_write_fast_copy(ctx, &raw mut (*c).status.screen, 0, 0, sx, lines);
    }
}

/// C `vendor/tmux/window-client.c:349`: `static void window_client_menu(void *modedata, struct client *c, key_code key)`
pub unsafe fn window_client_menu(modedata: NonNull<c_void>, c: *mut client, key: key_code) {
    unsafe {
        let data: NonNull<window_client_modedata> = modedata.cast();
        let wp: *mut window_pane = (*data.as_ptr()).wp;

        if let Some(wme) = NonNull::new(tailq_first(&raw mut (*wp).modes))
            && (*wme.as_ptr()).data == modedata.as_ptr()
        {
            window_client_key(wme, c, null_mut(), null_mut(), key, null_mut());
        }
    }
}

/// C `vendor/tmux/window-client.c:362`: `static key_code window_client_get_key(void *modedata, void *itemdata, u_int line)`
pub unsafe fn window_client_get_key(
    modedata: NonNull<c_void>,
    itemdata: NonNull<c_void>,
    line: u32,
) -> key_code {
    unsafe {
        let data: NonNull<window_client_modedata> = modedata.cast();
        let item: NonNull<window_client_itemdata> = itemdata.cast();

        let ft = format_create(null_mut(), null_mut(), FORMAT_NONE, format_flags::empty());
        format_defaults(ft, (*item.as_ptr()).c, None, None, None);
        format_add!(ft, "line", "{line}");

        let expanded = format_expand(ft, (*data.as_ptr()).key_format_ptr());
        let key = key_string_lookup_string(expanded);
        free_(expanded);
        format_free(ft);
        key
    }
}

/// C `vendor/tmux/window-client.c:420`: `static struct screen *window_client_init(struct window_mode_entry *wme, __unused struct cmd_find_state *fs, struct args *args)`
pub unsafe fn window_client_init(
    wme: NonNull<window_mode_entry>,
    _fs: *mut cmd_find_state,
    args: *mut args,
) -> *mut screen {
    unsafe {
        let wp: *mut window_pane = (*wme.as_ptr()).wp;
        let mut s: *mut screen = null_mut();

        // C allocates this with xcalloc. That is not valid for a Rust `Vec`: an all-zero
        // Vec has a null data pointer, which breaks its non-null invariant, so the very
        // first `item_list.drain(..)` in window_client_build dereferenced null. Build the
        // struct through Box so every field starts out a valid Rust value.
        let arg_str = |flag: u8| -> Option<CString> {
            if args.is_null() || !args_has(args, flag as char) {
                return None;
            }
            Some(CStr::from_ptr(args_get_(args, flag as char).cast()).to_owned())
        };

        let command = if args.is_null() || args_count(args) == 0 {
            cstring_truncating(WINDOW_CLIENT_DEFAULT_COMMAND.to_owned())
        } else {
            CStr::from_ptr(args_string(args, 0).cast()).to_owned()
        };

        let data = Box::into_raw(Box::new(window_client_modedata {
            wp,
            data: null_mut(),
            format: arg_str(b'F')
                .unwrap_or_else(|| cstring_truncating(WINDOW_CLIENT_DEFAULT_FORMAT.to_owned())),
            key_format: arg_str(b'K')
                .unwrap_or_else(|| cstring_truncating(WINDOW_CLIENT_DEFAULT_KEY_FORMAT.to_owned())),
            command,
            item_list: Vec::new(),
        }));
        (*wme.as_ptr()).data = data.cast();

        (*data).data = mode_tree_start(
            wp,
            args,
            Some(window_client_build),
            Some(window_client_draw),
            None,
            Some(window_client_menu),
            None,
            Some(window_client_get_key),
            data.cast(),
            WINDOW_CLIENT_MENU_ITEMS.as_slice(),
            &WINDOW_CLIENT_SORT_LIST,
            &raw mut s,
        );
        mode_tree_zoom((*data).data, args);

        mode_tree_build((*data).data);
        mode_tree_draw(&mut *(*data).data);

        s
    }
}

/// C `vendor/tmux/window-client.c:463`: `static void window_client_free(struct window_mode_entry *wme)`
pub unsafe fn window_client_free(wme: NonNull<window_mode_entry>) {
    unsafe {
        let data: *mut window_client_modedata = (*wme.as_ptr()).data as *mut window_client_modedata;

        if data.is_null() {
            return;
        }

        mode_tree_free((*data).data);

        // The items are raw C allocations, so they still need freeing by hand; the owned
        // format strings and the Vec itself go with the Box.
        for item in (*data).item_list.drain(..) {
            window_client_free_item(item);
        }

        drop(Box::from_raw(data));
    }
}

/// C `vendor/tmux/window-client.c:485`: `static void window_client_resize(struct window_mode_entry *wme, u_int sx, u_int sy)`
pub unsafe fn window_client_resize(wme: NonNull<window_mode_entry>, sx: u32, sy: u32) {
    unsafe {
        let data = (*wme.as_ptr()).data as *mut window_client_modedata;

        mode_tree_resize((*data).data, sx, sy);
    }
}

/// C `vendor/tmux/window-client.c:493`: `static void window_client_update(struct window_mode_entry *wme)`
pub unsafe fn window_client_update(wme: NonNull<window_mode_entry>) {
    unsafe {
        let data = (*wme.as_ptr()).data as *mut window_client_modedata;

        mode_tree_build((*data).data);
        mode_tree_draw(&mut *(*data).data);
        (*(*data).wp).flags |= window_pane_flags::PANE_REDRAW;
    }
}

/// C `vendor/tmux/window-client.c:503`: `static void window_client_do_detach(void *modedata, void *itemdata, __unused struct client *c, key_code key)`
pub unsafe fn window_client_do_detach(
    modedata: NonNull<c_void>,
    itemdata: NonNull<c_void>,
    _c: *mut client,
    key: key_code,
) {
    let data: NonNull<window_client_modedata> = modedata.cast();
    let item: NonNull<window_client_itemdata> = itemdata.cast();

    // TODO I'm not conviced this NonNull (item) is correct here

    unsafe {
        if item == mode_tree_get_current((*data.as_ptr()).data).cast() {
            mode_tree_down((*data.as_ptr()).data, 0);
        }
        if key == 'd' as key_code || key == 'D' as key_code {
            server_client_detach((*item.as_ptr()).c, msgtype::MSG_DETACH);
        } else if key == 'x' as key_code || key == 'X' as key_code {
            server_client_detach((*item.as_ptr()).c, msgtype::MSG_DETACHKILL);
        } else if key == 'z' as key_code || key == 'Z' as key_code {
            server_client_suspend((*item.as_ptr()).c);
        }
    }
}

/// C `vendor/tmux/window-client.c:520`: `static void window_client_key(struct window_mode_entry *wme, struct client *c, __unused struct session *s, __unused struct winlink *wl, key_code key, struct mouse_event *m)`
pub unsafe fn window_client_key(
    wme: NonNull<window_mode_entry>,
    c: *mut client,
    _: *mut session,
    _wl: *mut winlink,
    mut key: key_code,
    m: *mut mouse_event,
) {
    unsafe {
        let wp = (*wme.as_ptr()).wp;
        let data = (*wme.as_ptr()).data as *mut window_client_modedata;
        let mtd: *mut mode_tree_data = (*data).data;

        let mut finished = mode_tree_key(mtd, c, &raw mut key, m, null_mut(), null_mut()) != 0;
        // C switches on the full key_code. Truncating to u8 lets a KEYC_* code alias an
        // ASCII command here: KEYC_MOUSEUP11_STATUS_DEFAULT ends in 0x78 ('x', the Kill
        // prompt) and KEYC_DOUBLECLICK11_PANE ends in 0x58 ('X', Kill Tagged). Only a key
        // that really is a bare ASCII byte may match these arms.
        let key_byte = if key < 0x80 { key as u8 } else { 0 };
        match key_byte {
            b'd' | b'x' | b'z' => {
                let item: NonNull<window_client_itemdata> = mode_tree_get_current(mtd).cast();
                window_client_do_detach(NonNull::new(data.cast()).unwrap(), item.cast(), c, key);
                mode_tree_build(mtd);
            }
            b'D' | b'X' | b'Z' => {
                mode_tree_each_tagged(mtd, Some(window_client_do_detach), c, key, 0);
                mode_tree_build(mtd);
            }
            b'\r' => {
                let item: NonNull<window_client_itemdata> = mode_tree_get_current(mtd).cast();
                mode_tree_run_command(
                    c,
                    null_mut(),
                    (*data).command_ptr(),
                    cstr_to_str_((*(*item.as_ptr()).c).ttyname_ptr()),
                );
                finished = true;
            }
            _ => (),
        }

        if finished || server_client_how_many() == 0 {
            window_pane_reset_mode(wp);
        } else {
            mode_tree_draw(&mut *mtd);
            (*wp).flags |= window_pane_flags::PANE_REDRAW;
        }
    }
}