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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::{ffi, PtyFlags};
use glib::{prelude::*, translate::*};

glib::wrapper! {
    ///
    ///
    /// ## Properties
    ///
    ///
    /// #### `fd`
    ///  The file descriptor of the PTY master.
    ///
    /// Readable | Writeable | Construct Only
    ///
    ///
    /// #### `flags`
    ///  Flags.
    ///
    /// Readable | Writeable | Construct Only
    ///
    /// # Implements
    ///
    /// [`trait@gio::prelude::InitableExt`]
    #[doc(alias = "VtePty")]
    pub struct Pty(Object<ffi::VtePty, ffi::VtePtyClass>) @implements gio::Initable;

    match fn {
        type_ => || ffi::vte_pty_get_type(),
    }
}

impl Pty {
    /// Allocates a new pseudo-terminal.
    ///
    /// You can later use fork() or the g_spawn_async() family of functions
    /// to start a process on the PTY.
    ///
    /// If using fork(), you MUST call vte_pty_child_setup() in the child.
    ///
    /// If using g_spawn_async() and friends, you MUST either use
    /// vte_pty_child_setup() directly as the child setup function, or call
    /// vte_pty_child_setup() from your own child setup function supplied.
    ///
    /// When using vte_terminal_spawn_sync() with a custom child setup
    /// function, vte_pty_child_setup() will be called before the supplied
    /// function; you must not call it again.
    ///
    /// Also, you MUST pass the [`glib::SpawnFlags::DO_NOT_REAP_CHILD`][crate::glib::SpawnFlags::DO_NOT_REAP_CHILD] flag.
    ///
    /// Note also that [`glib::SpawnFlags::STDOUT_TO_DEV_NULL`][crate::glib::SpawnFlags::STDOUT_TO_DEV_NULL], [`glib::SpawnFlags::STDERR_TO_DEV_NULL`][crate::glib::SpawnFlags::STDERR_TO_DEV_NULL],
    /// and [`glib::SpawnFlags::CHILD_INHERITS_STDIN`][crate::glib::SpawnFlags::CHILD_INHERITS_STDIN] are not supported, since stdin, stdout
    /// and stderr of the child process will always be connected to the PTY.
    ///
    /// Note that you should set the PTY's size using vte_pty_set_size() before
    /// spawning the child process, so that the child process has the correct
    /// size from the start instead of starting with a default size and then
    /// shortly afterwards receiving a <literal>SIGWINCH</literal> signal. You
    /// should prefer using vte_terminal_pty_new_sync() which does this
    /// automatically.
    /// ## `flags`
    /// flags from #VtePtyFlags
    /// ## `cancellable`
    /// a #GCancellable, or [`None`]
    ///
    /// # Returns
    ///
    /// a new #VtePty, or [`None`] on error with @error filled in
    #[doc(alias = "vte_pty_new_sync")]
    pub fn new_sync(
        flags: PtyFlags,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
    ) -> Result<Pty, glib::Error> {
        assert_initialized_main_thread!();
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::vte_pty_new_sync(
                flags.into_glib(),
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(from_glib_full(ret))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`Pty`] objects.
    ///
    /// This method returns an instance of [`PtyBuilder`](crate::builders::PtyBuilder) which can be used to create [`Pty`] objects.
    pub fn builder() -> PtyBuilder {
        PtyBuilder::new()
    }

    #[doc(alias = "vte_pty_child_setup")]
    pub fn child_setup(&self) {
        unsafe {
            ffi::vte_pty_child_setup(self.to_glib_none().0);
        }
    }

    /// Reads the pseudo terminal's window size.
    ///
    /// If getting the window size failed, @error will be set to a #GIOError.
    ///
    /// # Returns
    ///
    /// [`true`] on success, [`false`] on failure with @error filled in
    ///
    /// ## `rows`
    /// a location to store the number of rows, or [`None`]
    ///
    /// ## `columns`
    /// a location to store the number of columns, or [`None`]
    #[doc(alias = "vte_pty_get_size")]
    #[doc(alias = "get_size")]
    pub fn size(&self) -> Result<(i32, i32), glib::Error> {
        unsafe {
            let mut rows = std::mem::MaybeUninit::uninit();
            let mut columns = std::mem::MaybeUninit::uninit();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_get_size(
                self.to_glib_none().0,
                rows.as_mut_ptr(),
                columns.as_mut_ptr(),
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok((rows.assume_init(), columns.assume_init()))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Attempts to resize the pseudo terminal's window size.  If successful, the
    /// OS kernel will send <literal>SIGWINCH</literal> to the child process group.
    ///
    /// If setting the window size failed, @error will be set to a #GIOError.
    /// ## `rows`
    /// the desired number of rows
    /// ## `columns`
    /// the desired number of columns
    ///
    /// # Returns
    ///
    /// [`true`] on success, [`false`] on failure with @error filled in
    #[doc(alias = "vte_pty_set_size")]
    pub fn set_size(&self, rows: i32, columns: i32) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_set_size(self.to_glib_none().0, rows, columns, &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Tells the kernel whether the terminal is UTF-8 or not, in case it can make
    /// use of the info.  Linux 2.6.5 or so defines IUTF8 to make the line
    /// discipline do multibyte backspace correctly.
    /// ## `utf8`
    /// whether or not the pty is in UTF-8 mode
    ///
    /// # Returns
    ///
    /// [`true`] on success, [`false`] on failure with @error filled in
    #[doc(alias = "vte_pty_set_utf8")]
    pub fn set_utf8(&self, utf8: bool) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::vte_pty_set_utf8(self.to_glib_none().0, utf8.into_glib(), &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Flags.
    pub fn flags(&self) -> PtyFlags {
        ObjectExt::property(self, "flags")
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`Pty`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct PtyBuilder {
    builder: glib::object::ObjectBuilder<'static, Pty>,
}

impl PtyBuilder {
    fn new() -> Self {
        Self {
            builder: glib::object::Object::builder(),
        }
    }

    /// The file descriptor of the PTY master.
    pub fn fd(self, fd: i32) -> Self {
        Self {
            builder: self.builder.property("fd", fd),
        }
    }

    /// Flags.
    pub fn flags(self, flags: PtyFlags) -> Self {
        Self {
            builder: self.builder.property("flags", flags),
        }
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`Pty`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> Pty {
        self.builder.build()
    }
}