Skip to content

Commit

Permalink
🐛 Fix compilation on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
SquitchYT committed Jan 9, 2024
1 parent f414d17 commit d53319b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
10 changes: 8 additions & 2 deletions src-tauri/src/pty/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ use std::sync::{mpsc, Arc};
use std::time::Duration;

use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};
use regex::Captures;
use tokio::sync::Mutex;

use std::sync::atomic::{AtomicBool, Ordering};

use crate::common::error::PtyError;
use crate::pty::utils;


#[cfg(target_os = "windows")]
use regex::Regex;
#[cfg(target_os = "windows")]
use crate::pty::utils;
#[cfg(target_os = "windows")]
use regex::Captures;

pub struct Pty {
writer: Box<dyn Write + Send>,
Expand Down Expand Up @@ -95,6 +98,9 @@ impl Pty {
let paused = Arc::from(AtomicBool::new(false));
let paused_cloned = paused.clone();

#[cfg(target_family = "unix")]
let cloned_master = master.clone();
#[cfg(target_os = "windows")]
let shell_pid = child.lock().await.process_id().ok_or(PtyError::Creation("PID not found".to_owned()))?;

std::thread::spawn(move || {
Expand Down
52 changes: 26 additions & 26 deletions src-tauri/src/pty/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::{ffi::OsString, os::windows::ffi::OsStringExt};

#[cfg(target_os = "windows")]
pub fn get_leader_pid(shell_pid: u32) -> u32 {
use std::mem::size_of;
Expand Down Expand Up @@ -30,35 +28,37 @@ pub fn get_leader_pid(shell_pid: u32) -> u32 {
leader_pid
}

#[cfg(target_os = "windows")]
pub fn get_process_title(pid: u32) -> Option<String> {
#[cfg(target_os = "windows")]
{
use windows::Win32::{
Foundation::CloseHandle,
System::Threading::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ},
};
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
use windows::Win32::{
Foundation::CloseHandle,
System::Threading::{PROCESS_QUERY_INFORMATION, PROCESS_VM_READ},
};

unsafe {
windows::Win32::System::Threading::OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
false,
pid,
)
}
.map_or(None, |handle| {
let mut path = [0; 4096];
let path_len = unsafe {
windows::Win32::System::ProcessStatus::GetModuleFileNameExW(handle, None, &mut path)
};
unsafe {
windows::Win32::System::Threading::OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
false,
pid,
)
}
.map_or(None, |handle| {
let mut path = [0; 4096];
let path_len = unsafe {
windows::Win32::System::ProcessStatus::GetModuleFileNameExW(handle, None, &mut path)
};

unsafe { CloseHandle(handle).ok() };
unsafe { CloseHandle(handle).ok() };

std::path::PathBuf::from(OsString::from_wide(&path[..path_len as usize]))
.file_name()
.and_then(|filename| filename.to_os_string().into_string().ok())
})
}
std::path::PathBuf::from(OsString::from_wide(&path[..path_len as usize]))
.file_name()
.and_then(|filename| filename.to_os_string().into_string().ok())
})
}

#[cfg(target_family = "unix")]
pub fn get_process_title(pid: i32) -> Option<String> {
#[cfg(target_family = "unix")]
{
std::fs::read_to_string(format!("/proc/{pid}/comm")).map_or(
Expand Down

0 comments on commit d53319b

Please sign in to comment.