Skip to content

Commit

Permalink
Implements set_center for windows, minor improvements (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuchAFuriousDeath authored Jan 1, 2025
1 parent 1c9838c commit 96f4632
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
2 changes: 2 additions & 0 deletions gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ windows-sys = { version = "0.59.0", features = [
"Win32_System_Memory",
"Win32_System_Registry",
"Win32_System_SystemInformation",
"Win32_UI",
"Win32_UI_WindowsAndMessaging",
] }

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion gui/src/graphics/vulkan/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl Vulkan {
let queue = unsafe { instance.get_physical_device_queue_family_properties(physical) }
.into_iter()
.position(|p| p.queue_flags.contains(QueueFlags::GRAPHICS))
.unwrap(); // We required all selectable devices to supports graphics operations.
.unwrap(); // We required all selectable devices to support graphics operations.

let mut queues = DeviceQueueCreateInfo::default();
let priorities = [1.0];

Expand Down
2 changes: 1 addition & 1 deletion gui/src/graphics/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum GraphicsError {
#[error("couldn't enumerate physical devices")]
EnumeratePhysicalDevices(#[source] ash::vk::Result),

#[error("no any Vulkan physical device available")]
#[error("no Vulkan physical devices available")]
NoPhysicalDevice,

#[error("no Vulkan device supports graphics operations with Vulkan 1.3")]
Expand Down
7 changes: 2 additions & 5 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,7 @@ async fn run(args: ProgramArgs, exe: PathBuf) -> Result<(), ProgramError> {
// Poll all futures.
let (vmm, debug) = std::future::poll_fn(move |cx| {
let vmm = vmm.as_mut().poll(cx);
let debug = match &mut debug {
Some(v) => v.poll_unpin(cx),
None => Poll::Pending,
};
let debug = debug.as_mut().map_or(Poll::Pending, |d| d.poll_unpin(cx));

match (vmm, debug) {
(Poll::Ready(v), Poll::Ready(d)) => Poll::Ready((Some(v), Some(d))),
Expand Down Expand Up @@ -552,6 +549,6 @@ enum ProgramError {
#[error("thread for vCPU #{0} was stopped unexpectedly")]
CpuThread(#[source] CpuError),

#[error("vCPU #{0} was panic, see {1} for more information")]
#[error("vCPU #{0} panicked, see {1} for more information")]
CpuPanic(usize, PathBuf),
}
80 changes: 77 additions & 3 deletions gui/src/ui/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
use super::PlatformExt;
use crate::rt::RuntimeWindow;
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use slint::ComponentHandle;
use thiserror::Error;
use windows_sys::Win32::{
Foundation::HWND,
UI::WindowsAndMessaging::{
GetSystemMetrics, GetWindowRect, SetWindowPos, HWND_TOP, SM_CXSCREEN, SM_CYSCREEN,
SWP_NOSIZE, SWP_NOZORDER,
},
};

impl<T: ComponentHandle> PlatformExt for T {
fn set_center(&self) -> Result<(), PlatformError> {
todo!()
let win = self.window().window_handle();
let raw_handle = win.window_handle().unwrap();

let RawWindowHandle::Win32(h) = raw_handle.as_ref() else {
unreachable!("Unsupported handle type on Windows");
};

unsafe {
let hwnd = h.hwnd.get() as HWND;
let mut rect = std::mem::zeroed();

let ret = GetWindowRect(hwnd, &mut rect);

if ret == 0 {
return Err(PlatformError::GetWindowRectFailed(
std::io::Error::last_os_error(),
));
}

let win_width = rect.right - rect.left;
let win_height = rect.bottom - rect.top;

let screen_width = GetSystemMetrics(SM_CXSCREEN);

if screen_width == 0 {
return Err(PlatformError::GetScreenWidthFailed(
std::io::Error::last_os_error(),
));
}

let screen_height = GetSystemMetrics(SM_CYSCREEN);

if screen_height == 0 {
return Err(PlatformError::GetScreenHeightFailed(
std::io::Error::last_os_error(),
));
}

let ret = SetWindowPos(
hwnd,
HWND_TOP,
(screen_width - win_width) / 2,
(screen_height - win_height) / 2,
0,
0,
SWP_NOSIZE | SWP_NOZORDER,
);

if ret == 0 {
return Err(PlatformError::SetWindowPosFailed(
std::io::Error::last_os_error(),
));
}
}

Ok(())
}

fn set_modal<P>(&self, parent: &P) -> Result<(), PlatformError>
Expand All @@ -16,6 +79,17 @@ impl<T: ComponentHandle> PlatformExt for T {
}
}

/// Windows-specific error for [`PlatformExt`].
#[derive(Debug, Error)]
pub enum PlatformError {}
pub enum PlatformError {
#[error("failed to get window rect")]
GetWindowRectFailed(#[source] std::io::Error),

#[error("failed to get screen width")]
GetScreenWidthFailed(#[source] std::io::Error),

#[error("failed to get screen height")]
GetScreenHeightFailed(#[source] std::io::Error),

#[error("failed to set window position")]
SetWindowPosFailed(#[source] std::io::Error),
}
19 changes: 15 additions & 4 deletions gui/src/vmm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ impl Vmm<()> {
shutdown: shutdown.clone(),
};

vmm.spawn(map.kern_vaddr + img.entry(), Some(map), debugger.is_some());
vmm.spawn(map.kern_vaddr + img.entry(), Some(map), debugger.is_some())
.map_err(VmmError::SpawnMainCpu)?;

Ok(vmm)
}
Expand Down Expand Up @@ -310,7 +311,12 @@ impl<H: Hypervisor> Vmm<H> {
const GDB_ENOENT: u8 = 2;
const GDB_EFAULT: u8 = 14;

pub fn spawn(&mut self, start: usize, map: Option<RamMap>, debug: bool) {
pub fn spawn(
&mut self,
start: usize,
map: Option<RamMap>,
debug: bool,
) -> Result<(), std::io::Error> {
// Setup arguments.
let args = CpuArgs {
hv: self.hv.clone(),
Expand All @@ -331,13 +337,13 @@ impl<H: Hypervisor> Vmm<H> {
let id = self.next;
let (tx, exiting) = futures::channel::oneshot::channel();
let thread = match map {
Some(map) => std::thread::spawn(move || {
Some(map) => std::thread::Builder::new().spawn(move || {
let r = Self::main_cpu(args, debugger, start, map);
tx.send(()).unwrap();
r
}),
None => todo!(),
};
}?;

self.next += 1;

Expand All @@ -352,6 +358,8 @@ impl<H: Hypervisor> Vmm<H> {
},
)
.is_none());

Ok(())
}

fn main_cpu(
Expand Down Expand Up @@ -883,6 +891,9 @@ pub enum VmmError {

#[error("couldn't build RAM")]
BuildRam(#[source] ram::RamBuilderError),

#[error("couldn't spawn the main CPU")]
SpawnMainCpu(#[source] std::io::Error),
}

/// Represents an error when a vCPU fails.
Expand Down

0 comments on commit 96f4632

Please sign in to comment.