linux_io/
tty.rs

1use linux_unsafe::{int, ulong, ushort};
2
3use crate::fd::ioctl::{
4    ioctl_read, ioctl_write, ioctl_writeread, IoctlReqRead, IoctlReqWrite, IoctlReqWriteRead, _IOR,
5    _IOW,
6};
7
8/// `ioctl` request for retrieving the current window size of a tty.
9// NOTE: This ioctl number isn't valid for all Linux architectures, but is valid
10// for all of the ones linux-unsafe supoorts at the time of writing.
11pub const TIOCGWINSZ: IoctlReqRead<TtyDevice, WindowSize> = unsafe { ioctl_read(0x5413) };
12
13/// `ioctl` request for changing the window size of a tty.
14// NOTE: This ioctl number isn't valid for all Linux architectures, but is valid
15// for all of the ones linux-unsafe supoorts at the time of writing.
16pub const TIOCSWINSZ: IoctlReqWrite<TtyDevice, WindowSize> = unsafe { ioctl_write(0x5414) };
17
18/// Represents the size of the window (or equivalent) that a tty is presented
19/// through.
20#[derive(Clone, Copy, Debug)]
21#[repr(C)]
22pub struct WindowSize {
23    /// The number of rows in the window.
24    pub ws_row: ushort,
25
26    /// The number of columns in the window.
27    pub ws_col: ushort,
28
29    /// Not actually used.
30    pub ws_xpixel: ushort,
31
32    /// Not actually used.
33    pub ws_ypixel: ushort,
34}
35
36/// A marker type for [`super::File`] objects that represent tty devices.
37pub struct TtyDevice;
38
39impl super::fd::ioctl::IoDevice for TtyDevice {}
40
41/// A marker type for [`super::File`] objects that represent pseudoterminal
42/// controllers, as are typically opened from `/dev/ptmx`.
43pub struct PtyControllerDevice;
44
45impl super::fd::ioctl::IoDevice for PtyControllerDevice {}
46
47const IOCTL_TTY: ulong = b'T' as ulong;
48
49/// `ioctl` request for setting or removing the lock on the terminal device
50/// associated with a [`PtyControllerDevice`].
51// NOTE: This ioctl number isn't valid for all Linux architectures, but is valid
52// for all of the ones linux-unsafe supoorts at the time of writing.
53pub const TIOCSPTLCK: IoctlReqWrite<PtyControllerDevice, int> =
54    unsafe { ioctl_write(_IOW(IOCTL_TTY, 0x31, core::mem::size_of::<int>() as ulong)) };
55
56/// `ioctl` request for finding the device unit number for a pseudoterminal.
57/// Use this with a [`PtyControllerDevice`] to determine which node to open
58/// from the `/dev/pts` directory.
59// NOTE: This ioctl number isn't valid for all Linux architectures, but is valid
60// for all of the ones linux-unsafe supoorts at the time of writing.
61pub const TIOCGPTN: IoctlReqWriteRead<PtyControllerDevice, int> =
62    unsafe { ioctl_writeread(_IOR(IOCTL_TTY, 0x30, core::mem::size_of::<int>() as ulong)) };