2024-11-17 15:38:07 -06:00
|
|
|
.{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")
|
2024-11-17 14:30:58 -06:00
|
|
|
devices := @use("devices.hb")
|
2024-11-17 04:17:32 -06:00
|
|
|
controller := @use("controller.hb")
|
2024-11-17 15:38:07 -06:00
|
|
|
mouse := @use("mouse.hb")
|
2024-11-15 13:47:11 -06:00
|
|
|
format_page := memory.dangling(u8)
|
2024-11-10 14:24:19 -06:00
|
|
|
|
2024-11-17 15:38:07 -06:00
|
|
|
mouse_buffer := 0
|
|
|
|
keyboard_buffer := 0
|
2024-11-17 10:57:06 -06:00
|
|
|
info := controller.Info.(0)
|
|
|
|
|
2024-11-17 15:38:07 -06:00
|
|
|
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 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 12:11:13 -06:00
|
|
|
process := fn(port: ^controller.Port): void {
|
2024-11-17 15:38:07 -06:00
|
|
|
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)
|
2024-11-17 14:30:58 -06:00
|
|
|
} else if port.device == devices.MOUSE_INIT_1 {
|
2024-11-17 15:38:07 -06:00
|
|
|
port.device.value = port.packet[0]
|
|
|
|
if port.device != devices.MOUSE_SCROLLWHEEL {
|
|
|
|
controller.send_byte(port, 0xF4)
|
|
|
|
return
|
2024-11-17 14:30:58 -06:00
|
|
|
}
|
2024-11-17 15:38:07 -06:00
|
|
|
port.device = devices.MOUSE_INIT_2
|
2024-11-17 14:30:58 -06:00
|
|
|
} else if port.device == devices.MOUSE_INIT_2 {
|
2024-11-17 15:38:07 -06:00
|
|
|
port.device.value = port.packet[0]
|
2024-11-17 14:30:58 -06:00
|
|
|
} 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))
|
|
|
|
}
|
2024-11-17 10:57:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
check_complete := fn(port: ^controller.Port): bool {
|
2024-11-17 14:30:58 -06:00
|
|
|
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
|
|
|
|
}
|
2024-11-17 15:38:07 -06:00
|
|
|
} else if port.device == devices.MOUSE_3_BUTTON {
|
2024-11-17 14:30:58 -06:00
|
|
|
if port.packet_length == 3 return true
|
2024-11-17 15:38:07 -06:00
|
|
|
} else if port.device == devices.MOUSE_SCROLLWHEEL | port.device == devices.MOUSE_5_BUTTON {
|
2024-11-17 14:30:58 -06:00
|
|
|
if port.packet_length == 4 return true
|
|
|
|
} else {
|
|
|
|
log.error("Very unexpected error. Cannot handle this!\0")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2024-11-17 10:57:06 -06:00
|
|
|
}
|
|
|
|
|
2024-11-10 14:24:19 -06:00
|
|
|
main := fn(): void {
|
2024-11-17 15:38:07 -06:00
|
|
|
mouse_buffer = buffer.create("PS/2 Mouse\0")
|
2024-11-15 13:47:11 -06:00
|
|
|
format_page = memory.alloc(u8, 1024)
|
2024-11-17 04:17:32 -06:00
|
|
|
|
|
|
|
controller.init()
|
2024-11-17 10:57:06 -06:00
|
|
|
|
|
|
|
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)
|
2024-11-17 15:38:07 -06:00
|
|
|
if port.packet_length > 0 & check_complete(port) {
|
|
|
|
process(port)
|
|
|
|
}
|
2024-11-17 14:30:58 -06:00
|
|
|
input := controller.get_input()
|
|
|
|
if input == 0xAA & port.can_hot_plug {
|
|
|
|
port.device = devices.NO_DEVICE
|
|
|
|
}
|
|
|
|
port.packet[port.packet_length] = input
|
2024-11-17 10:57:06 -06:00
|
|
|
port.packet_length += 1
|
2024-11-17 15:38:07 -06:00
|
|
|
if check_complete(port) {
|
2024-11-17 10:57:06 -06:00
|
|
|
process(port)
|
2024-11-17 15:38:07 -06:00
|
|
|
port.packet_length = 0
|
2024-11-17 10:57:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-10 14:24:19 -06:00
|
|
|
}
|