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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
// Take a look at the license at the top of the repository in the LICENSE file.
use glib::translate::*;
use std::boxed::Box as Box_;
use std::os::unix::io::{IntoRawFd, RawFd};
use io_lifetimes::OwnedFd;
#[cfg(feature = "v0_70")]
#[cfg_attr(docsrs, doc(cfg(feature = "v0_70")))]
use crate::Regex;
use crate::{prelude::*, PtyFlags, Terminal};
pub trait TerminalExtManual: 'static {
/// Checks each regex in @regexes if the text in and around the position (x, y)
/// matches the regular expressions. If a match exists, the matched
/// text is stored in @matches at the position of the regex in @regexes; otherwise
/// [`None`] is stored there. Each non-[`None`] element of @matches should be freed with
/// g_free().
///
/// Note that the regexes in @regexes should have been created using the `PCRE2_MULTILINE` flag.
/// ## `regexes`
/// an array of #VteRegex
/// ## `match_flags`
/// PCRE2 match flags, or 0
///
/// # Returns
///
/// [`true`] iff any of the regexes produced a match
///
/// ## `matches`
/// a location to store the matches
#[cfg(feature = "v0_70")]
#[cfg_attr(docsrs, doc(cfg(feature = "v0_70")))]
#[doc(alias = "vte_terminal_check_regex_array_at")]
#[doc(alias = "vte_terminal_check_regex_simple_at")]
#[doc(alias = "check_regex_array_at")]
fn check_regex_simple_at(
&self,
x: f64,
y: f64,
regexes: &[&Regex],
match_flags: u32,
) -> Vec<glib::GString>;
/// @palette specifies the new values for the 256 palette colors: 8 standard colors,
/// their 8 bright counterparts, 6x6x6 color cube, and 24 grayscale colors.
/// Omitted entries will default to a hardcoded value.
///
/// @palette_size must be 0, 8, 16, 232 or 256.
///
/// If @foreground is [`None`] and @palette_size is greater than 0, the new foreground
/// color is taken from @palette[7]. If @background is [`None`] and @palette_size is
/// greater than 0, the new background color is taken from @palette[0].
/// ## `foreground`
/// the new foreground color, or [`None`]
/// ## `background`
/// the new background color, or [`None`]
/// ## `palette`
/// the color palette
#[doc(alias = "vte_terminal_set_colors")]
fn set_colors(
&self,
foreground: Option<&gdk::RGBA>,
background: Option<&gdk::RGBA>,
palette: &[&gdk::RGBA],
);
/// Watches @child_pid. When the process exists, the #VteTerminal::child-exited
/// signal will be called with the child's exit status.
///
/// Prior to calling this function, a #VtePty must have been set in @self
/// using vte_terminal_set_pty().
/// When the child exits, the terminal's #VtePty will be set to [`None`].
///
/// Note: g_child_watch_add() or g_child_watch_add_full() must not have
/// been called for @child_pid, nor a #GSource for it been created with
/// g_child_watch_source_new().
///
/// Note: when using the g_spawn_async() family of functions,
/// the [`glib::SpawnFlags::DO_NOT_REAP_CHILD`][crate::glib::SpawnFlags::DO_NOT_REAP_CHILD] flag MUST have been passed.
/// ## `child_pid`
/// a #GPid
#[doc(alias = "vte_terminal_watch_child")]
fn watch_child(&self, child_pid: glib::Pid);
/// A convenience function that wraps creating the #VtePty and spawning
/// the child process on it. Like vte_terminal_spawn_with_fds_async(),
/// except that this function does not allow passing file descriptors to
/// the child process. See vte_terminal_spawn_with_fds_async() for more
/// information.
/// ## `pty_flags`
/// flags from #VtePtyFlags
/// ## `working_directory`
/// the name of a directory the command should start
/// in, or [`None`] to use the current working directory
/// ## `argv`
/// child's argument vector
/// ## `envv`
/// a list of environment
/// variables to be added to the environment before starting the process, or [`None`]
/// ## `spawn_flags`
/// flags from #GSpawnFlags
/// ## `child_setup`
/// an extra child setup function to run in the child just before exec(), or [`None`]
/// ## `child_setup_data`
/// user data for @child_setup, or [`None`]
/// ## `child_setup_data_destroy`
/// a #GDestroyNotify for @child_setup_data, or [`None`]
/// ## `timeout`
/// a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #VteTerminalSpawnAsyncCallback, or [`None`]
#[doc(alias = "vte_terminal_spawn_async")]
#[allow(clippy::too_many_arguments)]
fn spawn_async<P: FnOnce(Result<glib::Pid, glib::Error>) + 'static, Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
);
#[doc(alias = "vte_terminal_spawn_async")]
#[allow(clippy::too_many_arguments)]
fn spawn_future<Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
>;
/// A convenience function that wraps creating the #VtePty and spawning
/// the child process on it. See vte_pty_new_sync(), vte_pty_spawn_with_fds_async(),
/// and vte_pty_spawn_finish() for more information.
///
/// When the operation is finished successfully, @callback will be called
/// with the child #GPid, and a [`None`] #GError. The child PID will already be
/// watched via vte_terminal_watch_child().
///
/// When the operation fails, @callback will be called with a -1 #GPid,
/// and a non-[`None`] #GError containing the error information.
///
/// Note 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 in @spawn_flags, since
/// stdin, stdout and stderr of the child process will always be connected to
/// the PTY.
///
/// If @fds is not [`None`], the child process will map the file descriptors from
/// @fds according to @map_fds; @n_map_fds must be less or equal to @n_fds.
/// This function will take ownership of the file descriptors in @fds;
/// you must not use or close them after this call.
///
/// Note that all open file descriptors apart from those mapped as above
/// will be closed in the child. (If you want to keep some other file descriptor
/// open for use in the child process, you need to use a child setup function
/// that unsets the FD_CLOEXEC flag on that file descriptor manually.)
///
/// Beginning with 0.60, and on linux only, and unless `VTE_SPAWN_NO_SYSTEMD_SCOPE` is
/// passed in @spawn_flags, the newly created child process will be moved to its own
/// systemd user scope; and if `VTE_SPAWN_REQUIRE_SYSTEMD_SCOPE` is passed, and creation
/// of the systemd user scope fails, the whole spawn will fail.
/// You can override the options used for the systemd user scope by
/// providing a systemd override file for 'vte-spawn-.scope' unit. See man:systemd.unit(5)
/// for further information.
///
/// Note that if @self has been destroyed before the operation is called,
/// @callback will be called with a [`None`] @self; you must not do anything
/// in the callback besides freeing any resources associated with @user_data,
/// but taking care not to access the now-destroyed #VteTerminal. Note that
/// in this case, if spawning was successful, the child process will be aborted
/// automatically.
///
/// Beginning with 0.52, sets PWD to @working_directory in order to preserve symlink components.
/// The caller should also make sure that symlinks were preserved while constructing the value of @working_directory,
/// e.g. by using vte_terminal_get_current_directory_uri(), g_get_current_dir() or get_current_dir_name().
/// ## `pty_flags`
/// flags from #VtePtyFlags
/// ## `working_directory`
/// the name of a directory the command should start
/// in, or [`None`] to use the current working directory
/// ## `argv`
/// child's argument vector
/// ## `envv`
/// a list of environment
/// variables to be added to the environment before starting the process, or [`None`]
/// ## `fds`
/// an array of file descriptors, or [`None`]
/// ## `map_fds`
/// an array of integers, or [`None`]
/// ## `spawn_flags`
/// flags from #GSpawnFlags
/// ## `child_setup`
/// an extra child setup function to run in the child just before exec(), or [`None`]
/// ## `child_setup_data`
/// user data for @child_setup, or [`None`]
/// ## `child_setup_data_destroy`
/// a #GDestroyNotify for @child_setup_data, or [`None`]
/// ## `timeout`
/// a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #VteTerminalSpawnAsyncCallback, or [`None`]
#[doc(alias = "vte_terminal_spawn_with_fds_async")]
#[allow(clippy::too_many_arguments)]
unsafe fn spawn_with_fds_async<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
Q: Fn() + 'static,
>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
);
#[doc(alias = "vte_terminal_spawn_async")]
#[allow(clippy::too_many_arguments)]
unsafe fn spawn_with_fds_future<Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
>;
}
impl<O: IsA<Terminal>> TerminalExtManual for O {
#[cfg(feature = "v0_70")]
#[cfg_attr(docsrs, doc(cfg(feature = "v0_70")))]
fn check_regex_simple_at(
&self,
x: f64,
y: f64,
regexes: &[&Regex],
match_flags: u32,
) -> Vec<glib::GString> {
let n_regexes = regexes.len() as _;
unsafe {
let mut n_matches = std::mem::MaybeUninit::uninit();
let ret = FromGlibContainer::from_glib_full_num(
ffi::vte_terminal_check_regex_array_at(
self.as_ref().to_glib_none().0,
x,
y,
regexes.as_ptr() as *mut _,
n_regexes,
match_flags,
n_matches.as_mut_ptr(),
),
n_matches.assume_init() as _,
);
ret
}
}
#[doc(alias = "vte_terminal_spawn_async")]
fn spawn_future<Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
> {
let working_directory = working_directory.map(ToOwned::to_owned);
let argv: Vec<String> = argv.iter().map(|p| p.to_string()).collect();
let envv: Vec<String> = envv.iter().map(|p| p.to_string()).collect();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
let argv: Vec<&str> = argv.iter().map(|s| s.as_str()).collect();
let envv: Vec<&str> = envv.iter().map(|s| s.as_str()).collect();
obj.spawn_async(
pty_flags,
working_directory.as_deref(),
&argv,
&envv,
spawn_flags,
child_setup,
timeout,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
// # Safety
//
// The map_fds have to make sense.
#[doc(alias = "vte_terminal_spawn_async")]
unsafe fn spawn_with_fds_future<Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
> {
let working_directory = working_directory.map(ToOwned::to_owned);
let argv: Vec<String> = argv.iter().map(|p| p.to_string()).collect();
let envv: Vec<String> = envv.iter().map(|p| p.to_string()).collect();
let map_fds: Vec<RawFd> = map_fds.to_vec();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
let argv: Vec<&str> = argv.iter().map(|s| s.as_str()).collect();
let envv: Vec<&str> = envv.iter().map(|s| s.as_str()).collect();
obj.spawn_with_fds_async(
pty_flags,
working_directory.as_deref(),
&argv,
&envv,
fds,
&map_fds,
spawn_flags,
child_setup,
timeout,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
#[doc(alias = "vte_terminal_set_colors")]
fn set_colors(
&self,
foreground: Option<&gdk::RGBA>,
background: Option<&gdk::RGBA>,
palette: &[&gdk::RGBA],
) {
let palette_size = palette.len();
let palette_vector = palette
.iter()
.map(|item| unsafe { *item.to_glib_none().0 })
.collect::<Vec<gdk::ffi::GdkRGBA>>();
unsafe {
ffi::vte_terminal_set_colors(
self.as_ref().to_glib_none().0,
foreground.to_glib_none().0,
background.to_glib_none().0,
palette_vector.as_ptr(),
palette_size,
);
}
}
fn watch_child(&self, child_pid: glib::Pid) {
unsafe {
ffi::vte_terminal_watch_child(self.as_ref().to_glib_none().0, child_pid.into_glib());
}
}
fn spawn_async<P: FnOnce(Result<glib::Pid, glib::Error>) + 'static, Q: Fn() + 'static>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
assert_initialized_main_thread!();
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
assert!(argv.first().is_some(), "Need to pass an argument");
let child_setup_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::new(glib::thread_guard::ThreadGuard::new(child_setup));
unsafe extern "C" fn child_setup_func<Q: Fn() + 'static>(user_data: glib::ffi::gpointer) {
let callback: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback()
}
let child_setup = Some(child_setup_func::<Q> as _);
let callback_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn spawn_async_trampoline<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
>(
_terminal: *mut ffi::VteTerminal,
pid: glib::ffi::GPid,
error: *mut glib::ffi::GError,
user_data: glib::ffi::gpointer,
) {
let pid = from_glib(pid);
let result = if let Some(err) = Option::<glib::Error>::from_glib_none(error) {
Err(err)
} else {
Ok(pid)
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback(result)
}
let callback = Some(spawn_async_trampoline::<P> as _);
unsafe extern "C" fn child_setup_data_destroy_func<Q: Fn() + 'static>(
data: glib::ffi::gpointer,
) {
let _callback: Box_<Q> = Box_::from_raw(data as *mut _);
}
let destroy_call8 = Some(child_setup_data_destroy_func::<Q> as _);
let super_callback0: Box_<glib::thread_guard::ThreadGuard<Q>> = child_setup_data;
let super_callback1: Box_<glib::thread_guard::ThreadGuard<P>> = callback_data;
unsafe {
ffi::vte_terminal_spawn_async(
self.as_ref().to_glib_none().0,
pty_flags.into_glib(),
working_directory.to_glib_none().0,
argv.to_glib_none().0,
envv.to_glib_none().0,
spawn_flags.into_glib(),
child_setup,
Box_::into_raw(super_callback0) as *mut _,
destroy_call8,
timeout,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
callback,
Box_::into_raw(super_callback1) as *mut _,
);
}
}
// # Safety
//
// The map_fds have to make sense.
unsafe fn spawn_with_fds_async<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
Q: Fn() + 'static,
>(
&self,
pty_flags: PtyFlags,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
assert_initialized_main_thread!();
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
assert!(argv.first().is_some(), "Need to pass an argument");
let n_fds = fds.len() as _;
let n_map_fds = map_fds.len() as _;
let child_setup_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::new(glib::thread_guard::ThreadGuard::new(child_setup));
unsafe extern "C" fn child_setup_func<Q: Fn() + 'static>(user_data: glib::ffi::gpointer) {
let callback: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback()
}
let child_setup = Some(child_setup_func::<Q> as _);
let callback_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn spawn_with_fds_async_trampoline<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
>(
_terminal: *mut ffi::VteTerminal,
pid: glib::ffi::GPid,
error: *mut glib::ffi::GError,
user_data: glib::ffi::gpointer,
) {
let pid = from_glib(pid);
let result = if let Some(err) = Option::<glib::Error>::from_glib_none(error) {
Err(err)
} else {
Ok(pid)
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback(result)
}
let callback = Some(spawn_with_fds_async_trampoline::<P> as _);
unsafe extern "C" fn child_setup_data_destroy_func<Q: Fn() + 'static>(
data: glib::ffi::gpointer,
) {
let _callback: Box_<Q> = Box_::from_raw(data as *mut _);
}
let destroy_call12 = Some(child_setup_data_destroy_func::<Q> as _);
let super_callback0: Box_<glib::thread_guard::ThreadGuard<Q>> = child_setup_data;
let super_callback1: Box_<glib::thread_guard::ThreadGuard<P>> = callback_data;
let fds: Vec<RawFd> = fds.into_iter().map(|x| x.into_raw_fd()).collect();
unsafe {
ffi::vte_terminal_spawn_with_fds_async(
self.as_ref().to_glib_none().0,
pty_flags.into_glib(),
working_directory.to_glib_none().0,
argv.to_glib_none().0,
envv.to_glib_none().0,
fds.to_glib_none().0,
n_fds,
map_fds.to_glib_none().0,
n_map_fds,
spawn_flags.into_glib(),
child_setup,
Box_::into_raw(super_callback0) as *mut _,
destroy_call12,
timeout,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
callback,
Box_::into_raw(super_callback1) as *mut _,
);
}
}
}