.{memory, log, string} := @use("../../../libraries/stn/src/lib.hb") devices := @use("devices.hb") controller := @use("controller.hb") format_page := memory.dangling(u8) info := controller.Info.(0) process := fn(port: ^controller.Port): void { if port.device == devices.MOUSE_3_BUTTON { } else if port.device == devices.MOUSE_INIT_1 { port.device.value = port.packet[0] | port.packet[1] << 8 if port.device == devices.MOUSE_SCROLLWHEEL { port.device = devices.MOUSE_INIT_2 } } else if port.device == devices.MOUSE_INIT_2 { port.device.value = port.packet[0] | port.packet[1] << 8 } else if port.device == devices.NO_DEVICE { if port.packet_length == 1 { port.device.value = port.packet[0] } else { port.device.value = port.packet[1] | port.packet[0] << 8 } log.info("Identified device!\0") log.info(string.display_int(port.device.value, format_page, 16)) } } check_complete := fn(port: ^controller.Port): bool { last_value := port.packet[port.packet_length - 1] if port.device == devices.NO_DEVICE { if last_value == 0 | last_value == 3 | last_value == 4 { return true } else if port.packet_length == 2 { return true } } if port.device == devices.MOUSE_3_BUTTON { if port.packet_length == 3 return true } if port.device == devices.MOUSE_SCROLLWHEEL | port.device == devices.MOUSE_5_BUTTON { if port.packet_length == 4 return true } else { log.error("Very unexpected error. Cannot handle this!\0") return true } return false } main := fn(): void { format_page = memory.alloc(u8, 1024) controller.init() loop { info = controller.get_info() if controller.timed_out(info) { log.error("Timeout error! Cannot handle these!\0") } if controller.check_parity(info) { log.error("Parity error! Cannot handle these!\0") } if controller.has_input(info) { port := controller.get_port(info) input := controller.get_input() if input == 0xAA & port.can_hot_plug { port.device = devices.NO_DEVICE } port.packet[port.packet_length] = input port.packet_length += 1 if @inline(check_complete, port) { process(port) } } } }