Files
EonaCat.XSBox/crates/engine/src/hle/modules/display.rs
T
EonaCat 53e6e612c9
Rust validation / Formatting (push) Failing after 26s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 21:27:30 +01:00

234 lines
6.2 KiB
Rust

#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::{c_char, c_void};
use super::xgameruntime::{
Guid, HRESULT, Interface, Methods, NOT_SUPPORTED, Object, S_OK, add_ref, missing, put, text,
with_spare,
};
pub const HDR_PROVIDER: Guid = Guid::new(
0x03f0_fe74,
0xfdd9,
0x4e5c,
[0xb6, 0x30, 0xf9, 0x33, 0x9c, 0x47, 0xac, 0xc5],
);
pub const HDR_IID: Guid = Guid::new(
0x35f0_7670,
0x706e,
0x4bfb,
[0x94, 0x76, 0x09, 0x07, 0x98, 0xc5, 0xeb, 0xf3],
);
pub const SHELL: Guid = Guid::new(
0x1b33_9674,
0x328d,
0x4283,
[0xa2, 0x00, 0x31, 0x71, 0xf1, 0x8d, 0x36, 0x39],
);
pub const CAPTURE_PROVIDER: Guid = Guid::new(
0xa4f1_aee2,
0x4bf1,
0x4485,
[0xb0, 0x08, 0xa7, 0xc2, 0x6d, 0x52, 0xac, 0x27],
);
pub const CAPTURE_IID_2026: Guid = Guid::new(
0x22e6_72d7,
0xb4e3,
0x406c,
[0xbd, 0x50, 0x8f, 0x0d, 0x25, 0x23, 0x6f, 0x9e],
);
pub static HDR_OBJECT: Object = Object::new("hdr", &HDR_VTABLE);
pub static SHELL_OBJECT: Object = Object::new("shell", &SHELL_VTABLE);
pub static CAPTURE_OBJECT: Object = Object::new("capture", &CAPTURE_VTABLE);
pub const INTERFACES: &[Interface] = &[
(HDR_PROVIDER, HDR_IID, &HDR_OBJECT),
(SHELL, SHELL, &SHELL_OBJECT),
(CAPTURE_PROVIDER, CAPTURE_IID_2026, &CAPTURE_OBJECT),
];
type This = *const Object;
extern "system" fn try_enable_hdr(_: This, preference: u32, info: *mut HdrModeInfo) -> u32 {
tracing::info!(preference, "HDR mode: disabled (SDR presentation)");
if !info.is_null() {
unsafe {
info.write(HdrModeInfo {
min_tone_map_luminance: 0.0,
max_tone_map_luminance: 100.0,
max_full_frame_tone_map_luminance: 100.0,
});
}
}
2
}
#[repr(C)]
pub struct HdrModeInfo {
min_tone_map_luminance: f32,
max_tone_map_luminance: f32,
max_full_frame_tone_map_luminance: f32,
}
extern "system" fn launch_uri(_: This, _user: *const c_void, uri: *const c_char) -> HRESULT {
tracing::info!(uri = text(uri).unwrap_or_default(), "URI not launched");
NOT_SUPPORTED
}
#[cfg(windows)]
extern "system" fn acquire_deferral(_: This, out: *mut *mut c_void) -> HRESULT {
use windows_sys::Win32::System::Power::{
PowerCreateRequest, PowerRequestDisplayRequired, PowerSetRequest,
};
use windows_sys::Win32::System::Threading::{
POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT,
};
if out.is_null() {
return super::xasync::E_INVALIDARG;
}
let mut reason: Vec<u16> = "Title display deferral\0".encode_utf16().collect();
let mut context = REASON_CONTEXT {
Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING,
..Default::default()
};
context.Reason.SimpleReasonString = reason.as_mut_ptr();
let request = unsafe { PowerCreateRequest(&context) };
if request as isize == -1 {
return last_error();
}
if unsafe { PowerSetRequest(request, PowerRequestDisplayRequired) } == 0 {
let hr = last_error();
unsafe { windows_sys::Win32::Foundation::CloseHandle(request) };
return hr;
}
put(out, request)
}
#[cfg(windows)]
extern "system" fn close_deferral(_: This, handle: *mut c_void) {
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Power::{PowerClearRequest, PowerRequestDisplayRequired};
if !handle.is_null() {
unsafe {
PowerClearRequest(handle, PowerRequestDisplayRequired);
CloseHandle(handle);
}
}
}
#[cfg(windows)]
fn last_error() -> HRESULT {
let code = unsafe { windows_sys::Win32::Foundation::GetLastError() };
(0x8007_0000 | (code & 0xffff)) as i32
}
#[repr(C)]
pub struct ScreenshotResult {
pub local_id: [u8; 250],
pub formats: u16,
}
const SDR: u16 = 1;
#[cfg(windows)]
extern "system" fn take_screenshot(
_: This,
_user: *const c_void,
result: *mut ScreenshotResult,
) -> HRESULT {
use std::sync::atomic::{AtomicU32, Ordering};
static TAKEN: AtomicU32 = AtomicU32::new(0);
if result.is_null() {
return super::xasync::E_INVALIDARG;
}
let seconds = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_secs());
let name = format!(
"photo-{seconds}-{}.png",
TAKEN.fetch_add(1, Ordering::Relaxed)
);
let path = crate::hle::runtime::title().screenshots().join(&name);
tracing::info!(path = %path.display(), "screenshot asked for: the next frame shown");
eonacat_xsbox_gpu::screenshot::request(path);
let mut out = ScreenshotResult {
local_id: [0; 250],
formats: SDR,
};
out.local_id[..name.len()].copy_from_slice(name.as_bytes());
unsafe { result.write(out) };
S_OK
}
#[cfg(not(windows))]
extern "system" fn take_screenshot(
_: This,
_user: *const c_void,
_result: *mut ScreenshotResult,
) -> HRESULT {
tracing::info!("screenshot not taken");
NOT_SUPPORTED
}
static HDR_VTABLE: Methods<13> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
missing::<3> as *const (),
try_enable_hdr as *const (),
]);
#[cfg(windows)]
static SHELL_VTABLE: Methods<14> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
launch_uri as *const (),
acquire_deferral as *const (),
close_deferral as *const (),
]);
#[cfg(not(windows))]
static SHELL_VTABLE: Methods<14> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
launch_uri as *const (),
missing::<4> as *const (),
missing::<5> as *const (),
]);
static CAPTURE_VTABLE: Methods<26> = with_spare([
missing::<0> as *const (),
add_ref as *const (),
add_ref as *const (),
missing::<3> as *const (),
missing::<4> as *const (),
take_screenshot as *const (),
missing::<6> as *const (),
missing::<7> as *const (),
missing::<8> as *const (),
missing::<9> as *const (),
missing::<10> as *const (),
missing::<11> as *const (),
missing::<12> as *const (),
missing::<13> as *const (),
missing::<14> as *const (),
missing::<15> as *const (),
missing::<16> as *const (),
missing::<17> as *const (),
]);