#![allow(clippy::not_unsafe_ptr_arg_deref)] use std::ffi::{c_char, c_void}; use super::xasync::{self, AsyncBlock, E_ABORT}; use super::xgameruntime::{ Guid, HRESULT, Interface, Methods, Object, S_OK, add_ref, missing, put, text, with_spare, }; pub const PROVIDER: Guid = Guid::new( 0xdfcd_4649, 0x4ff8, 0x4043, [0xba, 0x07, 0x35, 0xd6, 0x07, 0xdf, 0x98, 0xb0], ); pub const IID_2026: Guid = Guid::new( 0xeaf6_69df, 0x5542, 0x4590, [0x99, 0xa3, 0x8d, 0xc0, 0x61, 0xf8, 0x37, 0xcc], ); pub static OBJECT: Object = Object::new("game ui", &VTABLE); pub const INTERFACES: &[Interface] = &[(PROVIDER, IID_2026, &OBJECT)]; type This = *const Object; static PROFILE: u8 = 0; static TEXT: u8 = 0; extern "system" fn profile_card_async( _: This, block: *mut AsyncBlock, _user: *const c_void, xuid: u64, ) -> HRESULT { tracing::info!(xuid, "profile card: nothing to show"); xasync::finished(block, (&raw const PROFILE).cast(), S_OK) } extern "system" fn profile_card_result(_: This, block: *mut AsyncBlock) -> HRESULT { let none = std::ptr::null_mut(); xasync::XAsyncGetResult(block, (&raw const PROFILE).cast(), 0, none, none.cast()) } extern "system" fn text_entry_async( _: This, block: *mut AsyncBlock, title: *const c_char, description: *const c_char, default: *const c_char, scope: u32, max_length: u32, ) -> HRESULT { let [title, description, default] = [title, description, default].map(|t| text(t).unwrap_or_default()); let max_length = max_length_for(scope, max_length); let password = scope == PASSWORD; tracing::info!(title, description, max_length, password, "text entry"); xasync::pending_with(block, (&raw const TEXT).cast(), move || { match ask(&title, &description, &default, max_length, password) { Some(typed) => { tracing::info!(length = typed.chars().count(), "text entry: done"); let mut bytes = limit(&typed, max_length).into_bytes(); bytes.push(0); Ok(bytes) } None => { tracing::info!("text entry: cancelled"); Err(E_ABORT) } } }) } extern "system" fn text_entry_size(_: This, block: *mut AsyncBlock, out: *mut u32) -> HRESULT { let mut size = 0; let hr = xasync::XAsyncGetResultSize(block, &mut size); if hr < 0 { return hr; } put(out, size as u32) } extern "system" fn text_entry_result( _: This, block: *mut AsyncBlock, size: u32, buffer: *mut c_char, used: *mut u32, ) -> HRESULT { let mut written = 0; let identity = (&raw const TEXT).cast(); let hr = xasync::XAsyncGetResult(block, identity, size as usize, buffer.cast(), &mut written); if hr < 0 || used.is_null() { return hr; } put(used, written as u32) } static MESSAGE: u8 = 0; static ERROR: u8 = 0; static CLOSED: u8 = 0; static PICKER: u8 = 0; #[allow(clippy::too_many_arguments)] extern "system" fn message_dialog_async( _: This, block: *mut AsyncBlock, title: *const c_char, content: *const c_char, first: *const c_char, second: *const c_char, third: *const c_char, default: u32, cancel: u32, ) -> HRESULT { let [title, content] = [title, content].map(|t| text(t).unwrap_or_default()); let buttons: Vec<(u32, String)> = [first, second, third] .into_iter() .zip(0..) .filter_map(|(label, i)| text(label).filter(|l| !l.is_empty()).map(|l| (i, l))) .collect(); if buttons.is_empty() { return xasync::E_INVALIDARG; } tracing::info!(title, buttons = buttons.len(), "message dialog"); xasync::pending_with(block, (&raw const MESSAGE).cast(), move || { let chosen = choose(&title, &content, &buttons, default, cancel); tracing::info!(chosen, "message dialog: done"); Ok(chosen.to_le_bytes().to_vec()) }) } extern "system" fn message_dialog_result( _: This, block: *mut AsyncBlock, out: *mut u32, ) -> HRESULT { xasync::result(block, (&raw const MESSAGE).cast(), out) } extern "system" fn error_dialog_async( _: This, block: *mut AsyncBlock, error: HRESULT, context: *const c_char, ) -> HRESULT { let context = text(context).unwrap_or_default(); tracing::warn!( error = format_args!("{:#010x}", error as u32), context, "error dialog" ); xasync::pending_with(block, (&raw const ERROR).cast(), move || { show_error(error, &context); Ok(Vec::new()) }) } extern "system" fn error_dialog_result(_: This, block: *mut AsyncBlock) -> HRESULT { let none = std::ptr::null_mut(); xasync::XAsyncGetResult(block, (&raw const ERROR).cast(), 0, none, none.cast()) } extern "system" fn achievements_async( _: This, block: *mut AsyncBlock, _user: *const c_void, title_id: u32, ) -> HRESULT { tracing::info!(title_id, "achievements: nothing to show"); xasync::finished(block, (&raw const CLOSED).cast(), S_OK) } fn dismissed(block: *mut AsyncBlock, what: &str) -> HRESULT { tracing::info!(what, "a page for other players: dismissed offline"); xasync::finished(block, (&raw const CLOSED).cast(), E_ABORT) } #[allow(clippy::too_many_arguments)] extern "system" fn send_invite_async( _: This, block: *mut AsyncBlock, _user: *const c_void, _configuration: *const c_char, _template: *const c_char, _session: *const c_char, _text: *const c_char, _context: *const c_char, ) -> HRESULT { dismissed(block, "send game invite") } extern "system" fn activity_invite_async( _: This, block: *mut AsyncBlock, _user: *const c_void, ) -> HRESULT { dismissed(block, "multiplayer activity invite") } extern "system" fn state_share_async( _: This, block: *mut AsyncBlock, _user: *const c_void, _link: *const c_char, ) -> HRESULT { dismissed(block, "state share") } extern "system" fn closed_result(_: This, block: *mut AsyncBlock) -> HRESULT { let none = std::ptr::null_mut(); xasync::XAsyncGetResult(block, (&raw const CLOSED).cast(), 0, none, none.cast()) } #[allow(clippy::too_many_arguments)] extern "system" fn player_picker_async( _: This, block: *mut AsyncBlock, _user: *const c_void, _prompt: *const c_char, _count: u32, _players: *const u64, _preselected_count: u32, _preselected: *const u64, minimum: u32, _maximum: u32, ) -> HRESULT { let result = if minimum == 0 { Ok(Vec::new()) } else { Err(E_ABORT) }; xasync::finished_with(block, (&raw const PICKER).cast(), result) } extern "system" fn player_picker_count(_: This, block: *mut AsyncBlock, out: *mut u32) -> HRESULT { let mut size = 0; let hr = xasync::XAsyncGetResultSize(block, &mut size); if hr < 0 { return hr; } put(out, (size / 8) as u32) } extern "system" fn player_picker_result( _: This, block: *mut AsyncBlock, count: u32, players: *mut u64, used: *mut u32, ) -> HRESULT { let mut written = 0; let identity = (&raw const PICKER).cast(); let hr = xasync::XAsyncGetResult( block, identity, count as usize * 8, players.cast(), &mut written, ); if hr < 0 || used.is_null() { return hr; } put(used, (written / 8) as u32) } extern "system" fn notification_position(_: This, _position: u32) -> HRESULT { S_OK } extern "system" fn set_ui_callbacks(_: This, _callbacks: *const c_void, system: bool) -> HRESULT { tracing::info!(system, "XGameUiSetUiCallbacks: our dialogs show"); S_OK } fn choose(title: &str, content: &str, buttons: &[(u32, String)], default: u32, cancel: u32) -> u32 { dialog::choose(title, content, buttons, default, cancel) } fn show_error(error: HRESULT, context: &str) { dialog::show_error(error, context); } const MAX_LENGTH: u32 = 1024; const PASSWORD: u32 = 31; fn max_length_for(scope: u32, max: u32) -> u32 { if max == 0 || scope == PASSWORD { MAX_LENGTH } else { max.min(MAX_LENGTH) } } fn limit(typed: &str, max: u32) -> String { typed.chars().take(max as usize).collect() } fn ask(title: &str, description: &str, default: &str, max: u32, password: bool) -> Option { dialog::ask(title, description, default, max, password) } mod dialog { use windows_sys::Win32::Foundation::{HWND, LPARAM, WPARAM}; use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW; use windows_sys::Win32::UI::Input::KeyboardAndMouse::SetFocus; use windows_sys::Win32::UI::WindowsAndMessaging::{self as wm, DLGTEMPLATE}; const EDIT: i32 = 100; const EM_SETSEL: u32 = 0xb1; const EM_LIMITTEXT: u32 = 0xc5; struct Entry { max: u32, typed: Option, } #[allow(dead_code)] pub(super) fn ask( title: &str, description: &str, default: &str, max: u32, password: bool, ) -> Option { let mut t: Vec = Vec::new(); fn dword(t: &mut Vec, v: u32) { t.extend([v as u16, (v >> 16) as u16]); } fn text(t: &mut Vec, s: &str) { t.extend(s.encode_utf16().filter(|&c| c != 0)); t.push(0); } let style = wm::WS_POPUP | wm::WS_CAPTION | wm::WS_SYSMENU; let flags = (wm::DS_MODALFRAME | wm::DS_CENTER | wm::DS_SETFOREGROUND) as u32; dword(&mut t, style | flags); dword(&mut t, wm::WS_EX_TOPMOST); t.extend([4, 0, 0, 240, 62, 0, 0]); text(&mut t, title); let mut item = |style: u32, rect: [u16; 4], id: u16, class: u16, caption: &str| { if t.len() % 2 == 1 { t.push(0); } dword(&mut t, wm::WS_CHILD | wm::WS_VISIBLE | style); dword(&mut t, 0); t.extend(rect); t.extend([id, 0xffff, class]); text(&mut t, caption); t.push(0); }; let (button, edit, label) = (0x80, 0x81, 0x82); let tab = wm::WS_TABSTOP; item(0, [7, 7, 226, 10], 0xffff, label, description); let mut edit_style = wm::WS_BORDER | tab | wm::ES_AUTOHSCROLL as u32; if password { edit_style |= wm::ES_PASSWORD as u32; } item(edit_style, [7, 20, 226, 12], EDIT as u16, edit, default); let ok = wm::BS_DEFPUSHBUTTON as u32 | tab; item(ok, [129, 40, 50, 14], wm::IDOK as u16, button, "OK"); let cancel = wm::BS_PUSHBUTTON as u32 | tab; item( cancel, [183, 40, 50, 14], wm::IDCANCEL as u16, button, "Cancel", ); let template: Vec = t .chunks(2) .map(|c| u32::from(c[0]) | u32::from(c.get(1).copied().unwrap_or(0)) << 16) .collect(); let mut entry = Entry { max, typed: None }; unsafe { wm::DialogBoxIndirectParamW( GetModuleHandleW(std::ptr::null()), template.as_ptr().cast::(), std::ptr::null_mut(), Some(procedure), (&raw mut entry) as LPARAM, ); } entry.typed } extern "system" fn procedure( dialog: HWND, message: u32, wparam: WPARAM, lparam: LPARAM, ) -> isize { unsafe { let edit = wm::GetDlgItem(dialog, EDIT); match message { wm::WM_INITDIALOG => { wm::SetWindowLongPtrW(dialog, wm::GWLP_USERDATA, lparam); let entry = &*(lparam as *const Entry); wm::SendMessageW(edit, EM_LIMITTEXT, entry.max as WPARAM, 0); wm::SendMessageW(edit, EM_SETSEL, 0, -1); SetFocus(edit); 0 } wm::WM_COMMAND => { let user_data = wm::GetWindowLongPtrW(dialog, wm::GWLP_USERDATA); if user_data == 0 { return 0; } let entry = &mut *(user_data as *mut Entry); match (wparam & 0xffff) as i32 { wm::IDOK => { let mut buffer = vec![0u16; wm::GetWindowTextLengthW(edit) as usize + 1]; let n = wm::GetWindowTextW(edit, buffer.as_mut_ptr(), buffer.len() as i32); entry.typed = Some(String::from_utf16_lossy(&buffer[..n.max(0) as usize])); wm::EndDialog(dialog, 1); } wm::IDCANCEL => { wm::EndDialog(dialog, 0); } _ => return 0, } 1 } _ => 0, } } } struct Template(Vec); impl Template { fn new(title: &str, items: u16, size: [u16; 2]) -> Template { let mut t = Template(Vec::new()); let style = wm::WS_POPUP | wm::WS_CAPTION | wm::WS_SYSMENU; let flags = (wm::DS_MODALFRAME | wm::DS_CENTER | wm::DS_SETFOREGROUND) as u32; t.dword(style | flags); t.dword(wm::WS_EX_TOPMOST); t.0.extend([items, 0, 0, size[0], size[1], 0, 0]); t.text(title); t } fn dword(&mut self, v: u32) { self.0.extend([v as u16, (v >> 16) as u16]); } fn text(&mut self, s: &str) { self.0.extend(s.encode_utf16().filter(|&c| c != 0)); self.0.push(0); } fn item(&mut self, style: u32, rect: [u16; 4], id: u16, class: u16, caption: &str) { if self.0.len() % 2 == 1 { self.0.push(0); } self.dword(wm::WS_CHILD | wm::WS_VISIBLE | style); self.dword(0); self.0.extend(rect); self.0.extend([id, 0xffff, class]); self.text(caption); self.0.push(0); } fn words(&self) -> Vec { self.0 .chunks(2) .map(|c| u32::from(c[0]) | (u32::from(c.get(1).copied().unwrap_or(0)) << 16)) .collect() } } const FIRST_BUTTON: i32 = 1000; struct Choice { default: u32, cancel: u32, } #[allow(dead_code)] pub(super) fn choose( title: &str, content: &str, buttons: &[(u32, String)], default: u32, cancel: u32, ) -> u32 { const WIDTH: u16 = 260; const BUTTON: u16 = 70; let mut t = Template::new(title, 1 + buttons.len() as u16, [WIDTH, 96]); t.item(0, [7, 7, WIDTH - 14, 60], 0xffff, 0x82, content); let mut x = WIDTH - 7 - (BUTTON + 4) * buttons.len() as u16 + 4; for (number, label) in buttons { let kind = if *number == default { wm::BS_DEFPUSHBUTTON } else { wm::BS_PUSHBUTTON }; let id = (FIRST_BUTTON + *number as i32) as u16; t.item( kind as u32 | wm::WS_TABSTOP, [x, 74, BUTTON, 14], id, 0x80, label, ); x += BUTTON + 4; } let template = t.words(); let mut choice = Choice { default, cancel }; let ended = unsafe { wm::DialogBoxIndirectParamW( GetModuleHandleW(std::ptr::null()), template.as_ptr().cast::(), std::ptr::null_mut(), Some(buttons_procedure), (&raw mut choice) as LPARAM, ) }; u32::try_from(ended).unwrap_or(cancel) } extern "system" fn buttons_procedure( dialog: HWND, message: u32, wparam: WPARAM, lparam: LPARAM, ) -> isize { unsafe { match message { wm::WM_INITDIALOG => { wm::SetWindowLongPtrW(dialog, wm::GWLP_USERDATA, lparam); let choice = &*(lparam as *const Choice); let default = wm::GetDlgItem(dialog, FIRST_BUTTON + choice.default as i32); if default.is_null() { return 1; } SetFocus(default); 0 } wm::WM_COMMAND => { let user_data = wm::GetWindowLongPtrW(dialog, wm::GWLP_USERDATA); if user_data == 0 { return 0; } let choice = &*(user_data as *const Choice); match (wparam & 0xffff) as i32 { id @ FIRST_BUTTON..=1002 => { wm::EndDialog(dialog, (id - FIRST_BUTTON) as isize); } wm::IDCANCEL => { wm::EndDialog(dialog, choice.cancel as isize); } _ => return 0, } 1 } _ => 0, } } } #[allow(dead_code)] pub(super) fn show_error(error: i32, context: &str) { let text = if context.is_empty() { format!("Error {:#010x}", error as u32) } else { format!("{context}\n\nError {:#010x}", error as u32) }; let wide = |s: &str| s.encode_utf16().chain([0]).collect::>(); let (text, caption) = (wide(&text), wide("Error")); let style = wm::MB_OK | wm::MB_ICONERROR | wm::MB_TOPMOST | wm::MB_SETFOREGROUND; unsafe { wm::MessageBoxW(std::ptr::null_mut(), text.as_ptr(), caption.as_ptr(), style) }; } } static VTABLE: Methods<59> = with_spare([ missing::<0> as *const (), add_ref as *const (), add_ref as *const (), message_dialog_async as *const (), message_dialog_result as *const (), send_invite_async as *const (), closed_result as *const (), profile_card_async as *const (), profile_card_result as *const (), achievements_async as *const (), closed_result as *const (), player_picker_async as *const (), player_picker_count as *const (), player_picker_result as *const (), error_dialog_async as *const (), error_dialog_result as *const (), notification_position as *const (), text_entry_async as *const (), text_entry_size as *const (), text_entry_result as *const (), missing::<20> as *const (), missing::<21> as *const (), missing::<22> as *const (), missing::<23> as *const (), missing::<24> as *const (), missing::<25> as *const (), missing::<26> as *const (), missing::<27> as *const (), missing::<28> as *const (), missing::<29> as *const (), activity_invite_async as *const (), closed_result as *const (), missing::<32> as *const (), missing::<33> as *const (), missing::<34> as *const (), missing::<35> as *const (), missing::<36> as *const (), missing::<37> as *const (), missing::<38> as *const (), missing::<39> as *const (), state_share_async as *const (), closed_result as *const (), set_ui_callbacks as *const (), missing::<43> as *const (), missing::<44> as *const (), missing::<45> as *const (), missing::<46> as *const (), missing::<47> as *const (), missing::<48> as *const (), missing::<49> as *const (), missing::<50> as *const (), ]);