term39 1.5.1

A modern, retro-styled terminal multiplexer with a classic MS-DOS aesthetic
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
//! Raw mouse input reader for framebuffer mode
//!
//! Reads mouse events directly from /dev/input/mice or /dev/input/event*
//! for cursor tracking when running in framebuffer mode on Linux console.

use std::fs::File;
use std::io::{self, Read};
use std::os::unix::io::AsRawFd;
use std::path::Path;

/// Mouse button states
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MouseButtons {
    pub left: bool,
    pub right: bool,
    pub middle: bool,
}

/// Raw mouse event with deltas
#[derive(Debug, Clone, Copy)]
pub struct MouseEvent {
    pub dx: i8,
    pub dy: i8,
    pub buttons: MouseButtons,
    /// Scroll wheel movement (positive = up/away, negative = down/toward)
    #[allow(dead_code)]
    pub scroll: i8,
    /// Horizontal scroll movement (positive = right, negative = left)
    #[allow(dead_code)]
    pub scroll_h: i8,
}

// Event types
const EV_REL: u16 = 0x02; // Relative movement
const EV_KEY: u16 = 0x01; // Button press/release

// Relative axis codes
const REL_X: u16 = 0x00;
const REL_Y: u16 = 0x01;
const REL_WHEEL: u16 = 0x08; // Vertical scroll wheel
const REL_HWHEEL: u16 = 0x06; // Horizontal scroll wheel

// Button codes
const BTN_LEFT: u16 = 0x110;
const BTN_RIGHT: u16 = 0x111;
const BTN_MIDDLE: u16 = 0x112;

// Size of struct input_event varies by architecture
// On 64-bit: 24 bytes (8 for timeval.tv_sec, 8 for tv_usec, 2 for type, 2 for code, 4 for value)
// On 32-bit: 16 bytes (4 for timeval.tv_sec, 4 for tv_usec, 2 for type, 2 for code, 4 for value)
#[cfg(target_pointer_width = "64")]
const INPUT_EVENT_SIZE: usize = 24;
#[cfg(target_pointer_width = "32")]
const INPUT_EVENT_SIZE: usize = 16;

/// Mouse input protocol type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Protocol {
    Ps2,        // /dev/input/mice (PS/2 protocol)
    InputEvent, // /dev/input/eventX (Linux input event protocol)
}

/// Raw mouse input reader
pub struct MouseInput {
    file: File,
    protocol: Protocol,
    dx_accumulator: i32,
    dy_accumulator: i32,
    scroll_accumulator: i32,   // Vertical scroll accumulator
    scroll_h_accumulator: i32, // Horizontal scroll accumulator
    buttons: MouseButtons,
    button_changed: bool, // Track if button state changed
}

impl MouseInput {
    /// Open the mouse input device
    /// If device_path is provided, uses that specific device.
    /// Otherwise, tries /dev/input/mice first, then searches for event devices.
    pub fn new(device_path: Option<&str>) -> io::Result<Self> {
        if let Some(path) = device_path {
            // Use the specified device
            let file = File::open(path)?;

            // Determine protocol based on path
            let protocol = if path.contains("mice") {
                Protocol::Ps2
            } else {
                Protocol::InputEvent
            };

            Self::setup_device(file, protocol)
        } else {
            // Try /dev/input/mice first (PS/2 protocol)
            if let Ok(file) = File::open("/dev/input/mice") {
                Self::setup_device(file, Protocol::Ps2)
            } else {
                // Try to find a mouse event device
                Self::find_event_device()
            }
        }
    }

    /// Find and open a mouse event device
    fn find_event_device() -> io::Result<Self> {
        // Try common event device numbers first
        for i in 0..16 {
            let path = format!("/dev/input/event{}", i);
            if Path::new(&path).exists() {
                if let Ok(file) = File::open(&path) {
                    // Try to determine if this is a mouse device
                    // For now, we'll just try the first available event device
                    // A more sophisticated approach would query device capabilities
                    if let Ok(input) = Self::setup_device(file, Protocol::InputEvent) {
                        return Ok(input);
                    }
                }
            }
        }

        Err(io::Error::new(
            io::ErrorKind::NotFound,
            "No mouse input device found (/dev/input/mice or /dev/input/event*)",
        ))
    }

    /// Setup a file descriptor for mouse input
    fn setup_device(file: File, protocol: Protocol) -> io::Result<Self> {
        // Set non-blocking mode
        unsafe {
            let flags = libc::fcntl(file.as_raw_fd(), libc::F_GETFL, 0);
            if flags < 0 {
                return Err(io::Error::last_os_error());
            }
            if libc::fcntl(file.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK) < 0 {
                return Err(io::Error::last_os_error());
            }
        }

        Ok(Self {
            file,
            protocol,
            dx_accumulator: 0,
            dy_accumulator: 0,
            scroll_accumulator: 0,
            scroll_h_accumulator: 0,
            buttons: MouseButtons {
                left: false,
                right: false,
                middle: false,
            },
            button_changed: false,
        })
    }

    /// Check if there's a mouse event available (non-blocking)
    pub fn has_event(&self) -> bool {
        let fd = self.file.as_raw_fd();
        let mut fds = libc::pollfd {
            fd,
            events: libc::POLLIN,
            revents: 0,
        };

        unsafe {
            // Poll with 0 timeout (non-blocking check)
            libc::poll(&mut fds as *mut libc::pollfd, 1, 0) > 0 && (fds.revents & libc::POLLIN) != 0
        }
    }

    /// Read a mouse event (non-blocking)
    /// Returns None if no event is available
    pub fn read_event(&mut self) -> io::Result<Option<MouseEvent>> {
        if !self.has_event() {
            return Ok(None);
        }

        match self.protocol {
            Protocol::Ps2 => self.read_ps2_event(),
            Protocol::InputEvent => self.read_input_event(),
        }
    }

    /// Read PS/2 protocol event from /dev/input/mice
    fn read_ps2_event(&mut self) -> io::Result<Option<MouseEvent>> {
        let mut buf = [0u8; 4]; // 4 bytes for wheel mouse support

        // Try to read 4 bytes first (IntelliMouse with wheel)
        match self.file.read(&mut buf) {
            Ok(n) if n >= 3 => {
                // Parse PS/2 mouse protocol
                // Byte 0: [Y overflow][X overflow][Y sign][X sign][Always 1][Middle][Right][Left]
                // Byte 1: X movement (8-bit signed)
                // Byte 2: Y movement (8-bit signed)
                // Byte 3 (optional): Z/wheel movement (signed nibble in IntelliMouse protocol)

                let buttons = MouseButtons {
                    left: (buf[0] & 0x01) != 0,
                    right: (buf[0] & 0x02) != 0,
                    middle: (buf[0] & 0x04) != 0,
                };

                // Convert unsigned to signed
                let dx = buf[1] as i8;
                let dy = buf[2] as i8;

                // Scroll wheel (only if we got 4 bytes)
                let scroll = if n == 4 {
                    // IntelliMouse wheel data in byte 3 (signed 4-bit value)
                    let wheel_byte = buf[3] as i8;
                    // Typical values: -1 (down/toward), 1 (up/away)
                    // Negate to match convention (positive = up)
                    -wheel_byte
                } else {
                    0
                };

                Ok(Some(MouseEvent {
                    dx,
                    dy,
                    buttons,
                    scroll,
                    scroll_h: 0,
                }))
            }
            Ok(_) => {
                // Incomplete read, ignore
                Ok(None)
            }
            Err(e) if e.kind() == io::ErrorKind::WouldBlock => Ok(None),
            Err(e) => Err(e),
        }
    }

    /// Read Linux input event protocol from /dev/input/eventX
    fn read_input_event(&mut self) -> io::Result<Option<MouseEvent>> {
        let mut buf = [0u8; INPUT_EVENT_SIZE];

        // Byte offsets for type, code, value depend on architecture
        // 64-bit: timeval is 16 bytes, so type starts at offset 16
        // 32-bit: timeval is 8 bytes, so type starts at offset 8
        #[cfg(target_pointer_width = "64")]
        const TYPE_OFFSET: usize = 16;
        #[cfg(target_pointer_width = "32")]
        const TYPE_OFFSET: usize = 8;

        loop {
            match self.file.read(&mut buf) {
                Ok(n) if n == INPUT_EVENT_SIZE => {
                    // Parse input_event structure
                    let type_ = u16::from_ne_bytes([buf[TYPE_OFFSET], buf[TYPE_OFFSET + 1]]);
                    let code = u16::from_ne_bytes([buf[TYPE_OFFSET + 2], buf[TYPE_OFFSET + 3]]);
                    let value = i32::from_ne_bytes([
                        buf[TYPE_OFFSET + 4],
                        buf[TYPE_OFFSET + 5],
                        buf[TYPE_OFFSET + 6],
                        buf[TYPE_OFFSET + 7],
                    ]);

                    match type_ {
                        EV_REL => {
                            // Relative movement and scroll
                            match code {
                                REL_X => self.dx_accumulator += value,
                                REL_Y => self.dy_accumulator += value,
                                REL_WHEEL => self.scroll_accumulator += value,
                                REL_HWHEEL => self.scroll_h_accumulator += value,
                                _ => {}
                            }
                        }
                        EV_KEY => {
                            // Button press/release
                            let old_buttons = self.buttons;
                            match code {
                                BTN_LEFT => self.buttons.left = value != 0,
                                BTN_RIGHT => self.buttons.right = value != 0,
                                BTN_MIDDLE => self.buttons.middle = value != 0,
                                _ => {}
                            }
                            // Mark if button state changed
                            if self.buttons.left != old_buttons.left
                                || self.buttons.right != old_buttons.right
                                || self.buttons.middle != old_buttons.middle
                            {
                                self.button_changed = true;
                            }
                        }
                        _ => {}
                    }

                    // Check if we have accumulated movement, scroll, OR button state change to report
                    let has_data = self.dx_accumulator != 0
                        || self.dy_accumulator != 0
                        || self.scroll_accumulator != 0
                        || self.scroll_h_accumulator != 0
                        || self.button_changed;

                    if has_data {
                        // Clamp to i8 range
                        let dx = self.dx_accumulator.clamp(-127, 127) as i8;
                        let dy = self.dy_accumulator.clamp(-127, 127) as i8;
                        let scroll = self.scroll_accumulator.clamp(-127, 127) as i8;
                        let scroll_h = self.scroll_h_accumulator.clamp(-127, 127) as i8;

                        self.dx_accumulator = 0;
                        self.dy_accumulator = 0;
                        self.scroll_accumulator = 0;
                        self.scroll_h_accumulator = 0;
                        self.button_changed = false;

                        return Ok(Some(MouseEvent {
                            dx,
                            dy,
                            buttons: self.buttons,
                            scroll,
                            scroll_h,
                        }));
                    }
                }
                Ok(_) => {
                    // Incomplete read, ignore
                    return Ok(None);
                }
                Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                    // No more events available
                    let has_data = self.dx_accumulator != 0
                        || self.dy_accumulator != 0
                        || self.scroll_accumulator != 0
                        || self.scroll_h_accumulator != 0
                        || self.button_changed;

                    if has_data {
                        let dx = self.dx_accumulator.clamp(-127, 127) as i8;
                        let dy = self.dy_accumulator.clamp(-127, 127) as i8;
                        let scroll = self.scroll_accumulator.clamp(-127, 127) as i8;
                        let scroll_h = self.scroll_h_accumulator.clamp(-127, 127) as i8;

                        self.dx_accumulator = 0;
                        self.dy_accumulator = 0;
                        self.scroll_accumulator = 0;
                        self.scroll_h_accumulator = 0;
                        self.button_changed = false;

                        return Ok(Some(MouseEvent {
                            dx,
                            dy,
                            buttons: self.buttons,
                            scroll,
                            scroll_h,
                        }));
                    }
                    return Ok(None);
                }
                Err(e) => return Err(e),
            }
        }
    }
}

/// Cursor position tracker
pub struct CursorTracker {
    pub x: usize,
    pub y: usize,
    max_x: usize,
    max_y: usize,
    sensitivity: f32,
    invert_x: bool,
    invert_y: bool,
}

impl CursorTracker {
    /// Create a new cursor tracker
    pub fn new(max_x: usize, max_y: usize, invert_x: bool, invert_y: bool) -> Self {
        Self {
            x: max_x / 2,
            y: max_y / 2,
            max_x,
            max_y,
            sensitivity: 1.0,
            invert_x,
            invert_y,
        }
    }

    /// Update cursor position with mouse deltas
    pub fn update(&mut self, dx: i8, dy: i8) {
        // Apply axis inversions if configured
        let dx = if self.invert_x { -dx } else { dx };
        let dy = if self.invert_y { -dy } else { dy };

        // Apply sensitivity and deltas
        let new_x = (self.x as i32 + (dx as f32 * self.sensitivity) as i32)
            .max(0)
            .min(self.max_x as i32 - 1);
        let new_y = (self.y as i32 + (dy as f32 * self.sensitivity) as i32)
            .max(0)
            .min(self.max_y as i32 - 1);

        self.x = new_x as usize;
        self.y = new_y as usize;
    }

    /// Set cursor position
    #[allow(dead_code)]
    pub fn set_position(&mut self, x: usize, y: usize) {
        self.x = x.min(self.max_x - 1);
        self.y = y.min(self.max_y - 1);
    }

    /// Update screen bounds (for resize)
    #[allow(dead_code)]
    pub fn set_bounds(&mut self, max_x: usize, max_y: usize) {
        self.max_x = max_x;
        self.max_y = max_y;
        self.x = self.x.min(max_x - 1);
        self.y = self.y.min(max_y - 1);
    }

    /// Set mouse sensitivity
    #[allow(dead_code)]
    pub fn set_sensitivity(&mut self, sensitivity: f32) {
        self.sensitivity = sensitivity.clamp(0.1, 10.0);
    }
}