use super::*;
pub(super) struct PpcInputSprocketDispatchContext<'a> {
pub(super) binding: &'a PpcImportBinding,
pub(super) cpu: &'a mut PpcCpu,
pub(super) memory: &'a mut PpcSectionMem,
pub(super) process_memory_manager: &'a mut ProcessNativeMemoryManager,
pub(super) heap_cursor: &'a mut u32,
pub(super) heap_limit: u32,
pub(super) input_sprocket: &'a mut PpcInputSprocketState,
pub(super) drivers: &'a mut inputsprocket_drivers::PpcInputSprocketDriverState,
pub(super) input_sprocket_virtual_elements: &'a mut Vec<PpcInputSprocketVirtualElementRecord>,
pub(super) input: PpcInputSnapshot,
pub(super) tick_count: u32,
pub(super) vfs_resources: &'a [PpcVfsResourceRecord],
pub(super) current_resource_refnum: i16,
}
pub(super) fn dispatch_inputsprocket_import(
context: PpcInputSprocketDispatchContext<'_>,
) -> Option<PpcImportAction> {
let PpcInputSprocketDispatchContext {
binding,
cpu,
memory,
process_memory_manager,
heap_cursor,
heap_limit,
input_sprocket,
drivers,
input_sprocket_virtual_elements,
input,
tick_count,
vfs_resources,
current_resource_refnum,
} = context;
match binding.dispatcher_target {
PpcImportDispatcherTarget::ISpElementNewVirtualFromNeeds => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_new_virtual_from_needs(
cpu,
process_memory_manager,
memory,
heap_cursor,
heap_limit,
input_sprocket,
input_sprocket_virtual_elements,
)),
)),
PpcImportDispatcherTarget::ISpElementListNew => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_list_new(
cpu,
process_memory_manager,
memory,
heap_cursor,
input_sprocket_virtual_elements,
)),
)),
PpcImportDispatcherTarget::ISpElementListDispose => {
Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_element_list_dispose(cpu, process_memory_manager, memory, heap_cursor),
)))
}
PpcImportDispatcherTarget::ISpElementListAddElements => {
Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_element_list_add_elements(cpu, memory, input_sprocket_virtual_elements),
)))
}
PpcImportDispatcherTarget::ISpElementListGetNextEvent => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_list_get_next_event(
cpu,
memory,
input,
*input_sprocket,
input_sprocket_virtual_elements,
tick_count,
)),
)),
PpcImportDispatcherTarget::ISpElementListFlush => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_list_flush(
cpu,
memory,
input,
*input_sprocket,
input_sprocket_virtual_elements,
)),
)),
PpcImportDispatcherTarget::ISpDevicesExtract => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_devices_extract(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpDevicesExtractByClass => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_devices_extract_by_class(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpDeviceGetDefinition => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_device_get_definition(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpDeviceGetElementList => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_device_get_element_list(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpGetGlobalElementList => {
let output = cpu.gpr[3];
let result = if output == 0 || !ppc_memory_can_write_bytes(memory, output, 4) {
PPC_PARAM_ERR
} else {
let _ = memory.write_u32_be(output, PPC_ISP_GLOBAL_ELEMENT_LIST);
PPC_NO_ERR
};
Some(PpcImportAction::Return(ppc_i16_result(result)))
}
PpcImportDispatcherTarget::ISpElementListExtract => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_list_extract(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpElementGetInfo => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_get_info(cpu, memory)),
)),
PpcImportDispatcherTarget::ISpElementGetConfigurationInfo => {
Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_element_get_configuration_info(cpu, memory),
)))
}
PpcImportDispatcherTarget::ISpElementGetSimpleState => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_element_get_simple_state(
cpu,
memory,
input,
input_sprocket,
input_sprocket_virtual_elements,
)),
)),
PpcImportDispatcherTarget::ISpGetVersion => {
if cpu.gpr[3] != 0 && ppc_memory_can_write_bytes(memory, cpu.gpr[3], 4) {
let _ = memory.write_u32_be(cpu.gpr[3], 0x0173_8000);
}
Some(PpcImportAction::ReturnPreserve)
}
PpcImportDispatcherTarget::ISpTimeToMicroseconds => {
let input = cpu.gpr[3];
let output = cpu.gpr[4];
let value = (input != 0).then(|| memory.read_u64_be(input)).flatten();
let result = if let Some(value) =
value.filter(|_| output != 0 && ppc_memory_can_write_bytes(memory, output, 8))
{
let _ = memory.write_u64_be(output, value);
PPC_NO_ERR
} else {
PPC_PARAM_ERR
};
Some(PpcImportAction::Return(ppc_i16_result(result)))
}
PpcImportDispatcherTarget::ISpStartup => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_init(input_sprocket, PpcInputSprocketKeyboardDefaults::default()),
))),
PpcImportDispatcherTarget::ISpShutdown => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_stop(input_sprocket),
))),
PpcImportDispatcherTarget::ISpInit => {
let set_list_resource_id = cpu.gpr[9] as u16 as i16;
let defaults = ppc_isp_load_default_keycodes(
vfs_resources,
current_resource_refnum,
set_list_resource_id,
)
.unwrap_or_default();
let result = ppc_isp_init(input_sprocket, defaults);
ppc_isp_assign_keyboard_defaults(input_sprocket, input_sprocket_virtual_elements);
if result != PPC_NO_ERR {
return Some(PpcImportAction::Return(ppc_i16_result(result)));
}
Some(inputsprocket_drivers::initialize_drivers(
cpu, memory, process_memory_manager, heap_cursor, drivers,
))
}
PpcImportDispatcherTarget::ISpStop => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_stop(input_sprocket),
))),
PpcImportDispatcherTarget::ISpSuspend => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_suspend(input_sprocket),
))),
PpcImportDispatcherTarget::ISpResume => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_resume(input_sprocket),
))),
PpcImportDispatcherTarget::ISpDevicesActivate => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_devices_activate(cpu, memory, input_sprocket)),
)),
PpcImportDispatcherTarget::ISpDevicesDeactivate => Some(PpcImportAction::Return(
ppc_i16_result(ppc_isp_devices_deactivate(cpu, memory, input_sprocket)),
)),
PpcImportDispatcherTarget::ISpConfigure => Some(PpcImportAction::Return(ppc_i16_result(
ppc_isp_configure(input_sprocket),
))),
PpcImportDispatcherTarget::InputSprocketCompatibility(operation) => {
Some(ppc_dispatch_input_sprocket_compatibility(
operation,
cpu,
memory,
input,
tick_count,
input_sprocket,
input_sprocket_virtual_elements,
))
}
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PpcInputSprocketCompatibilityOperation {
DevicesActivateClass,
DevicesDeactivateClass,
DeviceIsActive,
ElementDisposeVirtual,
ElementFlush,
ElementGetNextEvent,
Tickle,
}
pub(super) fn ppc_dispatch_input_sprocket_compatibility(
operation: PpcInputSprocketCompatibilityOperation,
cpu: &mut PpcCpu,
memory: &mut PpcSectionMem,
input: PpcInputSnapshot,
tick_count: u32,
input_sprocket: &mut PpcInputSprocketState,
virtual_elements: &mut Vec<PpcInputSprocketVirtualElementRecord>,
) -> PpcImportAction {
match operation {
PpcInputSprocketCompatibilityOperation::DevicesActivateClass
| PpcInputSprocketCompatibilityOperation::DevicesDeactivateClass => {
let active = operation == PpcInputSprocketCompatibilityOperation::DevicesActivateClass;
let device = match cpu.gpr[3] {
PPC_ISP_DEVICE_CLASS_KEYBOARD => Some(PPC_ISP_KEYBOARD_DEVICE),
PPC_ISP_DEVICE_CLASS_MOUSE => Some(PPC_ISP_MOUSE_DEVICE),
_ => None,
};
if let Some(device) = device {
ppc_isp_set_device_active(input_sprocket, device, active);
}
PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR))
}
PpcInputSprocketCompatibilityOperation::DeviceIsActive => {
let Some(active) = ppc_isp_device_is_active(input_sprocket, cpu.gpr[3]) else {
return PpcImportAction::Return(ppc_i16_result(PPC_PARAM_ERR));
};
let output = cpu.gpr[4];
if output == 0 || !ppc_memory_can_write_bytes(memory, output, 1) {
return PpcImportAction::Return(ppc_i16_result(PPC_PARAM_ERR));
}
let _ = memory.write_u8(output, u8::from(active));
PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR))
}
PpcInputSprocketCompatibilityOperation::ElementDisposeVirtual => {
virtual_elements.retain(|element| element.element != cpu.gpr[3]);
input_sprocket.virtual_element_count =
u32::try_from(virtual_elements.len()).unwrap_or(u32::MAX);
PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR))
}
PpcInputSprocketCompatibilityOperation::ElementGetNextEvent => {
let (element, buffer_size, event_ptr, was_event_ptr) =
(cpu.gpr[3], cpu.gpr[4], cpu.gpr[5], cpu.gpr[6]);
if was_event_ptr == 0 || !ppc_memory_can_write_bytes(memory, was_event_ptr, 1) {
return PpcImportAction::Return(ppc_i16_result(PPC_PARAM_ERR));
}
let event = (buffer_size >= PPC_ISP_ELEMENT_EVENT_SIZE
&& ppc_memory_can_write_bytes(memory, event_ptr, PPC_ISP_ELEMENT_EVENT_SIZE))
.then(|| ppc_isp_device_element_next_event(element, input, input_sprocket))
.flatten();
let Some(data) = event else {
let _ = memory.write_u8(was_event_ptr, 0);
return PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR));
};
let when = crate::time_manager::evaluate_microseconds(tick_count);
let words = [(when >> 32) as u32, when as u32, element, 0, data];
for (offset, word) in words.into_iter().enumerate() {
let _ = memory.write_u32_be(event_ptr + 4 * offset as u32, word);
}
let _ = memory.write_u8(was_event_ptr, 1);
PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR))
}
PpcInputSprocketCompatibilityOperation::ElementFlush
| PpcInputSprocketCompatibilityOperation::Tickle => {
PpcImportAction::Return(ppc_i16_result(PPC_NO_ERR))
}
}
}