rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize};
use std::sync::Arc;
use std::time::Instant;

use rmux_core::KeyCode;
use rmux_os::identity::UserIdentity;
use rmux_proto::{PaneTarget, SessionId, TerminalPixels, TerminalSize, WindowTarget};
use tokio::sync::mpsc;

use super::super::mode_tree_support::ModeTreeClientState;
use super::super::overlay_support::ClientOverlayState;
use super::super::prompt_support::ClientPromptState;
use super::super::scripting_support::{
    rename_pane_target_session, rename_window_target_session, QueueExecutionContext,
};
use super::super::{current_client_activity_timestamp, RequesterOrigin, StableTargetIdentity};
use super::transient_message::TransientMessageInputState;
use crate::client_flags::ClientFlags;
use crate::handler_support::{ambiguous_attached_client, attached_client_required};
use crate::mouse::ClientMouseState;
use crate::outer_terminal::{ClientTitleState, OuterTerminalContext, RenderedClientTitle};
use crate::pane_io::{AttachControl, AttachControlSender};

#[derive(Debug, Default)]
pub(in crate::handler) struct ActiveAttachState {
    pub(in crate::handler) next_id: u64,
    pub(in crate::handler) by_pid: HashMap<u32, ActiveAttach>,
    pub(in crate::handler) active_client_by_window:
        HashMap<rmux_proto::SessionName, HashMap<u32, u32>>,
}

/// How an attached client's outer terminal geometry became known.
///
/// This is server-internal typing only; it never crosses the wire and must
/// never be reconstructed from the numeric dimensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::handler) enum AttachClientSizeProvenance {
    /// The client declared this outer terminal geometry itself.
    Declared,
    /// The client declared no size, so registration anchored it to the outer
    /// terminal geometry of the session it joined. Anchoring to the session's
    /// *content* geometry instead would hand an already status-subtracted row
    /// count to every consumer that subtracts the status rows again.
    InferredFromSession,
}

#[derive(Debug)]
pub(in crate::handler) struct ActiveAttach {
    pub(in crate::handler) id: u64,
    /// Canonical display identity captured while the attaching process still
    /// exposes its tty. Never reconstruct this from the pid after registration.
    pub(in crate::handler) client_name: String,
    pub(in crate::handler) session_name: rmux_proto::SessionName,
    pub(in crate::handler) session_id: SessionId,
    pub(in crate::handler) last_session: Option<rmux_proto::SessionName>,
    pub(in crate::handler) last_session_id: Option<SessionId>,
    pub(in crate::handler) flags: ClientFlags,
    pub(in crate::handler) control_tx: AttachControlSender,
    pub(in crate::handler) control_backlog: Arc<AtomicUsize>,
    pub(in crate::handler) render_stream: bool,
    pub(in crate::handler) render_refresh_pending: bool,
    pub(in crate::handler) uid: u32,
    pub(in crate::handler) user: UserIdentity,
    pub(in crate::handler) can_write: bool,
    /// A timed-out OSC 52 request makes subsequent untagged responses
    /// impossible to correlate safely for this attach generation.
    pub(in crate::handler) clipboard_queries_desynchronized: bool,
    pub(in crate::handler) suspended: bool,
    pub(in crate::handler) closing: Arc<AtomicBool>,
    /// The server initiated this close without first emitting `client-detached`.
    pub(in crate::handler) emit_detached_on_finish: bool,
    pub(in crate::handler) terminal_context: OuterTerminalContext,
    /// Expanded `set-titles-string` and OSC 7 path this client's outer terminal
    /// was last told about, tmux's per-client `c->title` / `c->path`.
    pub(in crate::handler) client_title: ClientTitleState,
    /// Outer terminal geometry, status rows included. Every consumer derives
    /// content geometry from it by subtracting the status rows exactly once,
    /// so an already-subtracted row count must never be stored here.
    pub(in crate::handler) client_size: TerminalSize,
    pub(in crate::handler) client_size_provenance: AttachClientSizeProvenance,
    pub(in crate::handler) client_pixels: Option<TerminalPixels>,
    pub(in crate::handler) size_sequence: u64,
    /// Server-monotonic ordering of the client's latest accepted live input.
    pub(in crate::handler) last_activity_sequence: u64,
    /// Unix timestamp of the client's latest accepted live input.
    pub(in crate::handler) activity_at: i64,
    pub(in crate::handler) persistent_overlay_epoch: Arc<AtomicU64>,
    pub(in crate::handler) render_generation: u64,
    pub(in crate::handler) overlay_generation: u64,
    pub(in crate::handler) overlay_state_id: u64,
    pub(in crate::handler) display_panes_state_id: u64,
    pub(in crate::handler) key_table_name: Option<String>,
    pub(in crate::handler) key_table_set_at: Option<Instant>,
    pub(in crate::handler) key_table_generation: u64,
    pub(in crate::handler) repeat_deadline: Option<Instant>,
    pub(in crate::handler) repeat_active: bool,
    pub(in crate::handler) last_key: Option<KeyCode>,
    pub(in crate::handler) mouse: ClientMouseState,
    pub(in crate::handler) prompt: Option<ClientPromptState>,
    pub(in crate::handler) mode_tree_state_id: u64,
    pub(in crate::handler) mode_tree: Option<ModeTreeClientState>,
    pub(in crate::handler) mode_tree_frame: Option<Vec<u8>>,
    pub(in crate::handler) overlay: Option<ClientOverlayState>,
    pub(in crate::handler) display_panes: Option<DisplayPanesClientState>,
    pub(in crate::handler) transient_message: Option<TransientMessageInputState>,
    pub(in crate::handler) transient_terminal_prefix: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::handler) struct AttachedClientControlOutcome {
    pub(in crate::handler) session_name: rmux_proto::SessionName,
    pub(in crate::handler) client_name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ActiveAttachIdentity {
    attach_pid: u32,
    attach_id: u64,
    session_id: SessionId,
}

impl ActiveAttachIdentity {
    pub(crate) const fn new(attach_pid: u32, attach_id: u64, session_id: SessionId) -> Self {
        Self {
            attach_pid,
            attach_id,
            session_id,
        }
    }

    pub(crate) const fn attach_pid(self) -> u32 {
        self.attach_pid
    }

    pub(crate) const fn attach_id(self) -> u64 {
        self.attach_id
    }

    pub(crate) const fn session_id(self) -> SessionId {
        self.session_id
    }

    pub(in crate::handler) fn matches_active(self, active: &ActiveAttach) -> bool {
        // A live attach may legitimately switch sessions without changing its
        // forwarder.  The registration id, not the current session id, owns
        // the socket lifetime.
        self.attach_id == active.id
    }

    pub(in crate::handler) fn matches_active_session(
        self,
        active: &ActiveAttach,
        session_name: &rmux_proto::SessionName,
        session_id: SessionId,
    ) -> bool {
        self.attach_id == active.id
            && active.session_id == session_id
            && &active.session_name == session_name
    }

    /// Whether this attach is still the published one for its client and still
    /// owns `session_id`.
    ///
    /// The name a session is stored under is mutable and its id is not, so a
    /// caller that captured a name before releasing the state lock cannot use
    /// that name as identity: a rename keeps the same lifetime attached to the
    /// same client under a new key.  Callers that need a name read the current
    /// one off the attach this accepts.  A session switch does change
    /// `active.session_id`, so it is still rejected here.
    pub(in crate::handler) fn matches_active_lifetime(
        self,
        active: &ActiveAttach,
        session_id: SessionId,
    ) -> bool {
        self.attach_id == active.id && active.session_id == session_id
    }

    pub(in crate::handler) fn matches(
        self,
        attach_pid: u32,
        session_name: &rmux_proto::SessionName,
        active: &ActiveAttach,
    ) -> bool {
        self.attach_pid == attach_pid
            && self.attach_id == active.id
            && self.session_id == active.session_id
            && &active.session_name == session_name
    }
}

/// One exact attach registration: a pid *and* the generation holding it.
///
/// A pid on its own names whichever client owns it *now*.
/// `register_attach_identity` replaces `by_pid[attach_pid]` in place and hands
/// the replacement a fresh `id`, so a command that captured a registration must
/// carry this pair to keep speaking for the one it captured — and to stop
/// speaking at all once a replacement has taken the pid over.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::handler) struct AttachGeneration {
    attach_pid: u32,
    attach_id: u64,
}

impl AttachGeneration {
    pub(in crate::handler) const fn new(attach_pid: u32, attach_id: u64) -> Self {
        Self {
            attach_pid,
            attach_id,
        }
    }

    /// `true` when `active`, registered under `attach_pid`, is this exact
    /// registration rather than a same-pid replacement of it.
    pub(in crate::handler) fn is(self, attach_pid: u32, active: &ActiveAttach) -> bool {
        self.attach_pid == attach_pid && self.attach_id == active.id
    }
}

impl ActiveAttach {
    pub(in crate::handler) const fn identity(&self, attach_pid: u32) -> ActiveAttachIdentity {
        ActiveAttachIdentity::new(attach_pid, self.id, self.session_id)
    }

    /// `true` while `client_size` is still the anchor registration inferred
    /// from the session rather than geometry the client declared.
    pub(in crate::handler) const fn client_size_is_inferred(&self) -> bool {
        matches!(
            self.client_size_provenance,
            AttachClientSizeProvenance::InferredFromSession
        )
    }

    /// Records outer terminal geometry the client itself reported, promoting an
    /// inferred anchor even when the dimensions are numerically unchanged.
    pub(in crate::handler) fn set_declared_client_size(&mut self, client_size: TerminalSize) {
        self.client_size = client_size;
        self.client_size_provenance = AttachClientSizeProvenance::Declared;
    }

    /// Remembers what this client's outer terminal was just given.
    ///
    /// A render made while `set-titles` is off commits nothing and must leave
    /// the remembered values alone: tmux keeps `c->title` across the option
    /// going off and back on, so a title that is still current is not
    /// re-emitted when it is switched back on. A render that resolved a title
    /// but wrote no bytes commits nothing either — see
    /// [`RenderedClientTitle::committed`].
    pub(in crate::handler) fn remember_client_title(
        &mut self,
        rendered: Option<&RenderedClientTitle>,
    ) {
        if let Some(state) = rendered.and_then(RenderedClientTitle::committed) {
            self.client_title = state.clone();
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::handler) struct DisplayPanesClientState {
    pub(in crate::handler) id: u64,
    pub(in crate::handler) origin: RequesterOrigin,
    pub(in crate::handler) command_context: QueueExecutionContext,
    pub(in crate::handler) window: WindowTarget,
    pub(in crate::handler) labels: Vec<DisplayPanesLabel>,
    pub(in crate::handler) input: String,
    pub(in crate::handler) template: Option<String>,
    pub(in crate::handler) clear_frame: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::handler) struct DisplayPanesLabel {
    pub(in crate::handler) label: String,
    pub(in crate::handler) target: PaneTarget,
    pub(in crate::handler) target_string: String,
    pub(in crate::handler) target_identity: StableTargetIdentity,
}

#[derive(Debug)]
pub(crate) struct AttachRegistration {
    pub(crate) control_tx: mpsc::UnboundedSender<AttachControl>,
    pub(crate) control_backlog: Arc<AtomicUsize>,
    pub(crate) closing: Arc<AtomicBool>,
    pub(crate) persistent_overlay_epoch: Arc<AtomicU64>,
    pub(crate) terminal_context: OuterTerminalContext,
    /// What the attach frame this registration publishes already told the
    /// outer terminal, so the client's first refresh does not repeat it.
    pub(crate) client_title: Option<ClientTitleState>,
    pub(crate) flags: ClientFlags,
    pub(crate) render_stream: bool,
    pub(crate) uid: u32,
    pub(crate) user: UserIdentity,
    pub(crate) can_write: bool,
    pub(crate) client_size: Option<TerminalSize>,
}

fn rename_display_panes_state(
    state: &mut DisplayPanesClientState,
    old_name: &rmux_proto::SessionName,
    new_name: &rmux_proto::SessionName,
) -> bool {
    let mut renamed = state.clone();
    renamed
        .command_context
        .rename_session_targets(old_name, new_name);
    rename_window_target_session(&mut renamed.window, old_name, new_name);
    let old_prefix = format!("={old_name}:");
    for label in &mut renamed.labels {
        if label.target.session_name() != old_name {
            continue;
        }
        let Some(suffix) = label.target_string.strip_prefix(&old_prefix) else {
            return false;
        };
        rename_pane_target_session(&mut label.target, old_name, new_name);
        label.target_string = format!("={new_name}:{suffix}");
        label.target_identity.rename_session(old_name, new_name);
    }
    *state = renamed;
    true
}

impl ActiveAttachState {
    pub(in crate::handler) fn record_client_activity(
        &mut self,
        attach_pid: u32,
        sequence: u64,
    ) -> bool {
        let Some(active) = self.by_pid.get_mut(&attach_pid) else {
            return false;
        };
        active.last_activity_sequence = sequence;
        active.activity_at = current_client_activity_timestamp();
        true
    }

    pub(in crate::handler) fn attached_count(
        &self,
        session_name: &rmux_proto::SessionName,
    ) -> usize {
        self.by_pid
            .values()
            .filter(|active| &active.session_name == session_name && !active.suspended)
            .count()
    }

    pub(in crate::handler) fn rename_session(
        &mut self,
        session_name: &rmux_proto::SessionName,
        session_id: SessionId,
        new_name: &rmux_proto::SessionName,
    ) {
        for active in self.by_pid.values_mut() {
            if &active.session_name == session_name && active.session_id == session_id {
                active.session_name = new_name.clone();
                if let Some(mode_tree) = active.mode_tree.as_mut() {
                    mode_tree.rename_session(session_name, new_name);
                }
            }
            if active.display_panes.as_mut().is_some_and(|display_panes| {
                !rename_display_panes_state(display_panes, session_name, new_name)
            }) {
                active.display_panes = None;
                active.display_panes_state_id = active.display_panes_state_id.saturating_add(1);
            }
            if active.last_session.as_ref() == Some(session_name)
                && active.last_session_id == Some(session_id)
            {
                active.last_session = Some(new_name.clone());
            }
            active
                .mouse
                .rename_session_targets(session_name, session_id, new_name);
            if let Some(prompt) = active.prompt.as_mut() {
                prompt.rename_session_targets(session_name, new_name);
            }
            if let Some(overlay) = active.overlay.as_mut() {
                overlay.rename_session_targets(session_name, new_name);
            }
        }
        if let Some(renamed_windows) = self.active_client_by_window.remove(session_name) {
            self.active_client_by_window
                .entry(new_name.clone())
                .or_default()
                .extend(renamed_windows);
        }
    }

    pub(in crate::handler) fn remove_attached_client(
        &mut self,
        attach_pid: u32,
    ) -> Option<ActiveAttach> {
        let active = self.by_pid.remove(&attach_pid)?;
        self.forget_attached_client_windows(attach_pid);
        Some(active)
    }

    pub(in crate::handler) fn forget_attached_client_windows(&mut self, attach_pid: u32) {
        self.active_client_by_window.retain(|_, windows| {
            windows.retain(|_, pid| *pid != attach_pid);
            !windows.is_empty()
        });
    }

    pub(in crate::handler) fn forget_window(&mut self, target: &WindowTarget) {
        let remove_session = self
            .active_client_by_window
            .get_mut(target.session_name())
            .is_some_and(|windows| {
                let _ = windows.remove(&target.window_index());
                windows.is_empty()
            });
        if remove_session {
            let _ = self.active_client_by_window.remove(target.session_name());
        }
    }

    pub(in crate::handler) fn seed_active_client_for_window(
        &mut self,
        attach_pid: u32,
        session_name: &rmux_proto::SessionName,
        window_index: u32,
    ) {
        if self.by_pid.contains_key(&attach_pid) {
            self.active_client_by_window
                .entry(session_name.clone())
                .or_default()
                .insert(window_index, attach_pid);
        }
    }

    pub(in crate::handler) fn record_active_client_for_window(
        &mut self,
        attach_pid: u32,
        target: &PaneTarget,
    ) -> bool {
        if self
            .active_client_by_window
            .get(target.session_name())
            .and_then(|windows| windows.get(&target.window_index()))
            == Some(&attach_pid)
        {
            return false;
        }

        match self
            .active_client_by_window
            .entry(target.session_name().clone())
            .or_default()
            .insert(target.window_index(), attach_pid)
        {
            Some(previous) => previous != attach_pid,
            None => false,
        }
    }

    pub(in crate::handler) fn toggle_read_only_for_identity(
        &mut self,
        attach_pid: u32,
        expected_attach_id: u64,
    ) -> Result<ClientFlags, rmux_proto::RmuxError> {
        let active = self
            .by_pid
            .get_mut(&attach_pid)
            .filter(|active| active.id == expected_attach_id)
            .ok_or_else(|| {
                rmux_proto::RmuxError::Server("attached client disappeared".to_owned())
            })?;
        active.flags.toggle_read_only();
        Ok(active.flags)
    }

    #[cfg(test)]
    pub(in crate::handler) fn last_session_for_client(
        &self,
        attach_pid: u32,
    ) -> Result<Option<rmux_proto::SessionName>, rmux_proto::RmuxError> {
        self.last_session_identity_for_client(attach_pid)
            .map(|identity| identity.map(|(session_name, _)| session_name))
    }

    #[cfg(test)]
    pub(in crate::handler) fn last_session_identity_for_client(
        &self,
        attach_pid: u32,
    ) -> Result<Option<(rmux_proto::SessionName, SessionId)>, rmux_proto::RmuxError> {
        self.by_pid
            .get(&attach_pid)
            .map(|active| active.last_session.clone().zip(active.last_session_id))
            .ok_or_else(|| rmux_proto::RmuxError::Server("attached client disappeared".to_owned()))
    }

    pub(in crate::handler) fn current_session_candidate(
        &self,
        requester_pid: u32,
    ) -> Option<rmux_proto::SessionName> {
        if let Some(active) = self.by_pid.get(&requester_pid) {
            return Some(active.session_name.clone());
        }

        if self.by_pid.len() == 1 {
            return self
                .by_pid
                .values()
                .next()
                .map(|active| active.session_name.clone());
        }

        None
    }

    pub(in crate::handler) fn resolve_attached_client_pid(
        &self,
        requester_pid: u32,
        command_name: &str,
    ) -> Result<u32, rmux_proto::RmuxError> {
        if self.by_pid.contains_key(&requester_pid) {
            return Ok(requester_pid);
        }

        match self.by_pid.len() {
            0 => Err(attached_client_required(command_name)),
            1 => Ok(*self
                .by_pid
                .keys()
                .next()
                .expect("single-entry attach map must have one key")),
            _ => Err(ambiguous_attached_client(command_name)),
        }
    }
}