.{memory, log, buffer, string} := @use("../../../libraries/stn/src/lib.hb"); .{MouseEvent} := @use("../../../libraries/intouch/src/lib.hb").events; .{bit0, bit1, bit2, bit3, bit4} := @use("bits.hb") devices := @use("devices.hb") controller := @use("controller.hb") mouse := @use("mouse.hb") format_page := memory.dangling(u8) mouse_buffer := 0 keyboard_buffer := 0 info := controller.Info.(0) send_command := fn(port: ^controller.Port, byte: u8): void { controller.send_byte(port, byte) loop { info = controller.get_info() if controller.has_input(info) == false { continue } input := controller.get_input() if controller.get_port(info) != port { if check_complete(port) == false { } } } } process := fn(port: ^controller.Port): void { if port.device.value < devices.MOUSE_5_BUTTON.value { event := MouseEvent.(0, 0, false, false, false) event.left = bit0(port.packet[0]) event.right = bit1(port.packet[0]) event.middle = bit2(port.packet[0]) event.x_change = @intcast(port.packet[1]) event.y_change = @intcast(port.packet[2]) buffer.write(MouseEvent, mouse_buffer, &event) } else if port.device == devices.MOUSE_INIT_1 { port.device.value = port.packet[0] if port.device != devices.MOUSE_SCROLLWHEEL { controller.send_byte(port, 0xF4) return } port.device = devices.MOUSE_INIT_2 } else if port.device == devices.MOUSE_INIT_2 { port.device.value = port.packet[0] } 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 } } else if port.device == devices.MOUSE_3_BUTTON { if port.packet_length == 3 return true } else 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 { mouse_buffer = buffer.create("PS/2 Mouse\0") 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) if port.packet_length > 0 & check_complete(port) { process(port) } 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 check_complete(port) { process(port) port.packet_length = 0 } } } }