lios 0.1.89

Lios Linux terminal with native Aen and GTK4/VTE frontends.
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
mod cli;
mod model;
mod pty;
mod render;

use aen_window::{Configure, Create, Event, Frame, Input, InputEvent, WorkerWindow};
use model::Grid;
use std::io;
use std::os::fd::AsRawFd;
use std::os::unix::process::ExitStatusExt;
use std::time::{Duration, Instant};

pub fn run() -> i32 {
    match execute() {
        Ok(code) => code,
        Err(error) => {
            eprintln!("lios: {error}");
            1
        }
    }
}
fn execute() -> io::Result<i32> {
    let action = cli::parse(std::env::args_os().skip(1)).map_err(io::Error::other)?;
    let options = match action {
        cli::Action::Help => {
            print!("{}", cli::HELP);
            return Ok(0);
        }
        cli::Action::Version => {
            println!("lios {} (Aen)", env!("CARGO_PKG_VERSION"));
            return Ok(0);
        }
        cli::Action::Check => {
            let font = render::default_font()?;
            let _ = render::Renderer::new(Some(&font), 16.0)?;
            println!(
                "frontend=Aen\ntransport=aen-window-v3\nfont={}\nendpoint={}",
                font.display(),
                aen_window::socket_from_env()?.display()
            );
            return Ok(0);
        }
        cli::Action::Run(options) => options,
    };
    let mut renderer = render::Renderer::new(options.font.as_deref(), options.font_size)?;
    let socket = match &options.socket {
        Some(path) => path.clone(),
        None => aen_window::socket_from_env()?,
    };
    let mut window = WorkerWindow::connect(
        &socket,
        Create {
            app_id: "org.knott.lios".into(),
            title: "Lios Terminal".into(),
            preferred_width: options.width,
            preferred_height: options.height,
        },
    )?;
    // A failed native handshake cannot start a child shell or invoke an installer.
    let deadline = Instant::now() + Duration::from_secs(4);
    while window.configuration().is_none() {
        for event in window.poll()? {
            if !matches!(event, Event::Configured(_)) {
                return Err(io::Error::other("unexpected Lios handshake event"));
            }
        }
        if Instant::now() >= deadline {
            return Err(io::Error::other("Lios native configuration timed out"));
        }
        wait(&window, None)?;
    }
    let configure = window.configuration().unwrap();
    let grid = renderer.grid(configure.width, configure.height);
    let mut terminal = model::Terminal::new(grid, options.scrollback);
    let mut clipboard = aen_window::clipboard::socket_for_window(&socket)
        .and_then(aen_window::clipboard::Worker::new)
        .ok();
    let mut clipboard_request = None;
    let mut focus_epoch = 0_u64;
    let mut pending_input = InputQueue::default();
    let mut pty = pty::Pty::spawn(&options, grid.rows, grid.cols)?;
    let mut presentation = Presentation::default();
    let mut dirty = true;
    let mut buffer = [0; 8192];
    loop {
        for event in window.poll()? {
            match event {
                Event::Configured(_) => {
                    terminal.cancel_pointer();
                    dirty = true;
                }
                Event::FrameAccepted {
                    frame_serial,
                    configure_serial,
                } => {
                    let snapshot = presentation.accepted(frame_serial, configure_serial)?;
                    pty.resize(snapshot.grid.rows, snapshot.grid.cols)?;
                }
                Event::FrameDiscarded {
                    frame_serial,
                    configure_serial,
                } => {
                    presentation.discarded(frame_serial, configure_serial)?;
                    dirty = true;
                }
                Event::Input(input) => {
                    if let Some(snapshot) = presentation.input(input.clone()) {
                        if terminal.focused
                            && matches!(&input.event, InputEvent::Key { keysym: 0x51 | 0x71, pressed: true, modifiers, .. } if *modifiers == aen_window::MOD_CTRL | aen_window::MOD_SHIFT)
                        {
                            return Ok(0);
                        }
                        if matches!(&input.event, InputEvent::Focus { .. }) {
                            focus_epoch = focus_epoch
                                .checked_add(1)
                                .ok_or_else(|| io::Error::other("Lios focus epoch exhausted"))?;
                        }
                        let effect = terminal.input(input.event, snapshot.grid);
                        dirty |= effect.redraw;
                        pending_input.enqueue(&effect.bytes)?;
                        if let Some(action) = effect.clipboard {
                            let (text, paste) = match action {
                                model::ClipboardAction::Copy(text) => (Some(text), false),
                                model::ClipboardAction::Paste => (None, true),
                            };
                            if let Some(worker) = clipboard.as_mut() {
                                if paste && let Err(error) = pending_input.begin_paste() {
                                    eprintln!("lios: native clipboard request: {error}");
                                    continue;
                                }
                                match worker.request(snapshot.configure.window_id, text) {
                                    Ok(id) => clipboard_request = Some((id, focus_epoch, paste)),
                                    Err(error) => {
                                        if paste {
                                            pending_input.finish_paste(&[])?;
                                        }
                                        eprintln!("lios: native clipboard request: {error}")
                                    }
                                }
                            } else {
                                eprintln!("lios: native clipboard endpoint is unavailable");
                            }
                        }
                    }
                }
                Event::CloseRequested => return Ok(0),
            }
        }
        if let Some(outcome) = clipboard.as_mut().and_then(|worker| worker.poll())
            && let Some((id, epoch, paste)) = clipboard_request.take()
            && outcome.id == id
        {
            match outcome.value {
                Ok(Some(text)) if paste && epoch == focus_epoch && terminal.focused => {
                    pending_input.finish_paste(&terminal.paste(&text)?)?;
                    dirty = true;
                }
                Err(error) => eprintln!("lios: native clipboard request: {error}"),
                _ => {}
            }
            if paste && pending_input.paste_pending {
                pending_input.finish_paste(&[])?;
            }
        }
        let mut read = 0;
        while read < pty::READ_BUDGET && !pty.eof {
            match pty.read(&mut buffer) {
                Ok(0) => break,
                Ok(count) => {
                    read += count;
                    terminal.output(&buffer[..count]);
                    pty.enqueue(&terminal.replies()?)?;
                    dirty = true;
                }
                Err(error) if error.kind() == io::ErrorKind::WouldBlock => break,
                Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
                Err(error) => return Err(error),
            }
        }
        if pty.eof {
            pending_input = InputQueue::default();
        }
        while !pending_input.ready.is_empty() && pty.available_input_bytes() > 0 {
            let (bytes, _) = pending_input.ready.as_slices();
            let count = bytes.len().min(pty.available_input_bytes());
            pty.enqueue(&bytes[..count])?;
            pending_input.ready.drain(..count);
        }
        pty.flush()?;
        if dirty && window.can_submit() {
            let configure = window
                .configuration()
                .ok_or_else(|| io::Error::other("missing Lios native configuration"))?;
            let grid = renderer.grid(configure.width, configure.height);
            if terminal.parser.screen().size() != (grid.rows, grid.cols) {
                terminal.resize(grid);
            }
            let frame_serial = presentation
                .last_serial
                .checked_add(1)
                .ok_or_else(|| io::Error::other("Lios frame serial exhausted"))?;
            let pixels = renderer.render(&terminal, configure.width, configure.height)?;
            window.submit(Frame {
                frame_serial,
                configure_serial: configure.configure_serial,
                width: configure.width,
                height: configure.height,
                pixels: pixels.into(),
            })?;
            presentation.pending = Some(Snapshot {
                frame_serial,
                configure,
                grid,
            });
            presentation.last_serial = frame_serial;
            dirty = false;
        }
        let status = pty.status()?;
        if let Some(status) = status
            && pty.eof
            && !options.hold
            && !dirty
            && presentation.pending.is_none()
            && presentation.adopted.is_some()
        {
            return Ok(status
                .code()
                .unwrap_or_else(|| 128 + status.signal().unwrap_or(1)));
        }
        wait(&window, Some(&pty))?;
    }
}

// Reserve space for a complete bounded paste while preserving later keystrokes.
// In particular, Ctrl+Shift+V followed immediately by Enter cannot submit Enter
// before the asynchronous clipboard read finishes.
#[derive(Default)]
struct InputQueue {
    ready: std::collections::VecDeque<u8>,
    deferred: std::collections::VecDeque<u8>,
    paste_pending: bool,
}
impl InputQueue {
    const LIMIT: usize = 128 * 1024;
    const PASTE_RESERVE: usize = aen_window::clipboard::MAX_TEXT_BYTES + 12;
    fn enqueue(&mut self, bytes: &[u8]) -> io::Result<()> {
        let limit = Self::LIMIT
            - if self.paste_pending {
                Self::PASTE_RESERVE
            } else {
                0
            };
        if bytes.len() > limit.saturating_sub(self.ready.len() + self.deferred.len()) {
            return Err(io::Error::other("Lios pending input exceeded its limit"));
        }
        let queue = if self.paste_pending {
            &mut self.deferred
        } else {
            &mut self.ready
        };
        queue.extend(bytes);
        Ok(())
    }
    fn begin_paste(&mut self) -> io::Result<()> {
        if self.paste_pending || self.ready.len() > Self::LIMIT - Self::PASTE_RESERVE {
            return Err(io::Error::new(
                io::ErrorKind::WouldBlock,
                "Lios paste input is busy",
            ));
        }
        self.paste_pending = true;
        Ok(())
    }
    fn finish_paste(&mut self, bytes: &[u8]) -> io::Result<()> {
        if !self.paste_pending || bytes.len() > Self::PASTE_RESERVE {
            return Err(io::Error::other("invalid Lios paste completion"));
        }
        self.ready.extend(bytes);
        self.ready.extend(self.deferred.drain(..));
        self.paste_pending = false;
        Ok(())
    }
}

fn wait(window: &WorkerWindow, pty: Option<&pty::Pty>) -> io::Result<()> {
    let mut descriptors = [
        libc::pollfd {
            fd: window.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        },
        libc::pollfd {
            fd: -1,
            events: 0,
            revents: 0,
        },
    ];
    if let Some(pty) = pty.filter(|p| !p.eof) {
        descriptors[1].fd = pty.as_raw_fd();
        descriptors[1].events = libc::POLLIN | if pty.wants_write() { libc::POLLOUT } else { 0 };
    }
    // SAFETY: descriptors are initialized live SDK/PTY fds; the bounded wait
    // never opens a display/input device or connects to any host bus.
    if unsafe {
        libc::poll(
            descriptors.as_mut_ptr(),
            descriptors.len() as libc::nfds_t,
            20,
        )
    } < 0
    {
        let error = io::Error::last_os_error();
        if error.kind() != io::ErrorKind::Interrupted {
            return Err(error);
        }
    }
    Ok(())
}

#[derive(Clone, Copy)]
struct Snapshot {
    frame_serial: u64,
    configure: Configure,
    grid: Grid,
}
#[derive(Default)]
struct Presentation {
    last_serial: u64,
    pending: Option<Snapshot>,
    adopted: Option<Snapshot>,
}
impl Presentation {
    fn accepted(&mut self, frame: u64, configuration: u64) -> io::Result<Snapshot> {
        let snapshot = self.match_pending(frame, configuration)?;
        self.pending = None;
        self.adopted = Some(snapshot);
        Ok(snapshot)
    }
    fn discarded(&mut self, frame: u64, configuration: u64) -> io::Result<()> {
        self.match_pending(frame, configuration)?;
        self.pending = None;
        Ok(())
    }
    fn match_pending(&self, frame: u64, configuration: u64) -> io::Result<Snapshot> {
        self.pending
            .filter(|s| s.frame_serial == frame && s.configure.configure_serial == configuration)
            .ok_or_else(|| io::Error::other("mismatched Lios native frame acknowledgement"))
    }
    fn input(&self, input: Input) -> Option<Snapshot> {
        self.adopted
            .filter(|s| s.frame_serial == input.frame_serial)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn snapshot(frame_serial: u64, configure_serial: u64, cols: u16) -> Snapshot {
        Snapshot {
            frame_serial,
            configure: Configure {
                window_id: 1,
                configure_serial,
                width: 800,
                height: 600,
                scale: 1,
            },
            grid: Grid {
                rows: 6,
                cols,
                cell_width: 10,
                cell_height: 20,
                padding: 8,
            },
        }
    }
    #[test]
    fn asynchronous_paste_precedes_enter_and_cancel_releases_deferred_input() {
        let mut queue = InputQueue::default();
        queue.enqueue(b"before ").unwrap();
        queue.begin_paste().unwrap();
        queue.enqueue(b"\r").unwrap();
        assert_eq!(queue.ready.iter().copied().collect::<Vec<_>>(), b"before ");
        assert!(queue.begin_paste().is_err());
        queue.finish_paste(b"selection").unwrap();
        assert_eq!(
            queue.ready.iter().copied().collect::<Vec<_>>(),
            b"before selection\r"
        );
        queue.ready.clear();
        queue.begin_paste().unwrap();
        queue.enqueue(b"typed").unwrap();
        assert!(queue.enqueue(&vec![0; InputQueue::LIMIT]).is_err());
        queue.finish_paste(&[]).unwrap();
        assert_eq!(queue.ready.iter().copied().collect::<Vec<_>>(), b"typed");
        queue.ready.clear();
        queue.begin_paste().unwrap();
        queue.enqueue(b"\r").unwrap();
        queue
            .finish_paste(&vec![b'a'; InputQueue::PASTE_RESERVE])
            .unwrap();
        assert_eq!(queue.ready.back(), Some(&b'\r'));
        assert!(queue.ready.len() <= InputQueue::LIMIT);
    }
    #[test]
    fn input_and_resize_follow_exact_adopted_frames() {
        let mut presentation = Presentation {
            pending: Some(snapshot(1, 1, 80)),
            ..Presentation::default()
        };
        let input = |serial| Input {
            frame_serial: serial,
            event: InputEvent::Motion { x: 5, y: 5 },
        };
        assert!(presentation.input(input(1)).is_none());
        assert!(presentation.accepted(1, 2).is_err());
        presentation.accepted(1, 1).unwrap();
        presentation.pending = Some(snapshot(2, 2, 40));
        assert_eq!(presentation.input(input(1)).unwrap().grid.cols, 80);
        presentation.discarded(2, 2).unwrap();
        assert_eq!(presentation.input(input(1)).unwrap().grid.cols, 80);
        presentation.pending = Some(snapshot(3, 2, 40));
        presentation.accepted(3, 2).unwrap();
        assert!(presentation.input(input(1)).is_none());
        assert_eq!(presentation.input(input(3)).unwrap().grid.cols, 40);
    }
}