Kernel: Implement basic USB Mouse

This has the same problem I described in previous commit for keyboard
This commit is contained in:
Bananymous 2024-07-14 02:11:32 +03:00
parent ac5c77ee2c
commit baa4e6475a
4 changed files with 140 additions and 0 deletions

View File

@ -97,6 +97,7 @@ set(KERNEL_SOURCES
kernel/USB/Device.cpp kernel/USB/Device.cpp
kernel/USB/HID/HIDDriver.cpp kernel/USB/HID/HIDDriver.cpp
kernel/USB/HID/Keyboard.cpp kernel/USB/HID/Keyboard.cpp
kernel/USB/HID/Mouse.cpp
kernel/USB/USBManager.cpp kernel/USB/USBManager.cpp
kernel/USB/XHCI/Controller.cpp kernel/USB/XHCI/Controller.cpp
kernel/USB/XHCI/Device.cpp kernel/USB/XHCI/Device.cpp

View File

@ -0,0 +1,36 @@
#pragma once
#include <kernel/USB/HID/HIDDriver.h>
namespace Kernel
{
class USBMouse final : public USBHIDDevice
{
BAN_NON_COPYABLE(USBMouse);
BAN_NON_MOVABLE(USBMouse);
public:
void start_report() override;
void stop_report() override;
void handle_variable(uint16_t usage_page, uint16_t usage, int64_t state) override;
void handle_array(uint16_t usage_page, uint16_t usage) override;
private:
USBMouse()
: USBHIDDevice(InputDevice::Type::Mouse)
{}
~USBMouse() = default;
private:
BAN::Array<bool, 5> m_button_state { false };
BAN::Array<bool, 5> m_button_state_temp { false };
int64_t m_pointer_x { 0 };
int64_t m_pointer_y { 0 };
int64_t m_wheel { 0 };
friend class BAN::RefPtr<USBMouse>;
};
}

View File

@ -3,6 +3,7 @@
#include <kernel/FS/DevFS/FileSystem.h> #include <kernel/FS/DevFS/FileSystem.h>
#include <kernel/USB/HID/HIDDriver.h> #include <kernel/USB/HID/HIDDriver.h>
#include <kernel/USB/HID/Keyboard.h> #include <kernel/USB/HID/Keyboard.h>
#include <kernel/USB/HID/Mouse.h>
#define DEBUG_HID 0 #define DEBUG_HID 0
@ -219,6 +220,9 @@ namespace Kernel
switch (collection.usage_id) switch (collection.usage_id)
{ {
case 0x02:
m_hid_device = TRY(BAN::RefPtr<USBMouse>::create());
break;
case 0x06: case 0x06:
m_hid_device = TRY(BAN::RefPtr<USBKeyboard>::create()); m_hid_device = TRY(BAN::RefPtr<USBKeyboard>::create());
break; break;

View File

@ -0,0 +1,99 @@
#include <kernel/USB/HID/Mouse.h>
#include <LibInput/MouseEvent.h>
#define DEBUG_MOUSE 0
namespace Kernel
{
void USBMouse::start_report()
{
for (auto& val : m_button_state_temp)
val = false;
}
void USBMouse::stop_report()
{
if (m_pointer_x || m_pointer_y)
{
dprintln_if(DEBUG_MOUSE, "Mouse move event {}, {}", m_pointer_x, m_pointer_y);
LibInput::MouseEvent event;
event.type = LibInput::MouseEventType::MouseMoveEvent;
event.move_event.rel_x = m_pointer_x;
event.move_event.rel_y = -m_pointer_y;
add_event(BAN::ConstByteSpan::from(event));
m_pointer_x = 0;
m_pointer_y = 0;
}
if (m_wheel)
{
dprintln_if(DEBUG_MOUSE, "Mouse scroll event {}", m_wheel);
LibInput::MouseEvent event;
event.type = LibInput::MouseEventType::MouseScrollEvent;
event.scroll_event.scroll = m_wheel;
add_event(BAN::ConstByteSpan::from(event));
m_wheel = 0;
}
for (size_t i = 0; i < m_button_state.size(); i++)
{
if (m_button_state[i] == m_button_state_temp[i])
continue;
const bool pressed = m_button_state_temp[i];
dprintln_if(DEBUG_MOUSE, "Mouse button event {}: {}", i, pressed);
LibInput::MouseEvent event;
event.type = LibInput::MouseEventType::MouseButtonEvent;
event.button_event.pressed = pressed;
event.button_event.button = static_cast<LibInput::MouseButton>(i);
add_event(BAN::ConstByteSpan::from(event));
m_button_state[i] = m_button_state_temp[i];
}
}
void USBMouse::handle_variable(uint16_t usage_page, uint16_t usage, int64_t state)
{
switch (usage_page)
{
case 0x01: // pointer
switch (usage)
{
case 0x30:
m_pointer_x = state;
break;
case 0x31:
m_pointer_y = state;
break;
case 0x38:
m_wheel = state;
break;
default:
dprintln_if(DEBUG_MOUSE, "Unsupported mouse usage {2H} on page {2H}", usage, usage_page);
break;
}
break;
case 0x09: // button
if (usage == 0 || usage >= m_button_state_temp.size())
break;
m_button_state_temp[usage - 1] = state;
break;
default:
dprintln_if(DEBUG_MOUSE, "Unsupported mouse usage page {2H}", usage_page);
break;
}
}
void USBMouse::handle_array(uint16_t usage_page, uint16_t usage)
{
dprintln_if(DEBUG_MOUSE, "Unhandled array report {2H}:{2H}", usage_page, usage);
}
}