Files

196 lines
6.3 KiB
C

// SPDX-License-Identifier: Apache-2.0
#include <Uefi.h>
#include <Protocol/GraphicsOutput.h>
#include <Protocol/SimplePointer.h>
#include <Protocol/UsbIo.h>
#include <Apple/Guid/AppleVariable.h>
#include <Apple/Protocol/AppleEvent.h>
// QEMU's full- and high-speed usb-tablet descriptors use the same input endpoint.
#define TABLET_ENDPOINT (USB_ENDPOINT_DIR_IN | 1)
#define TABLET_PACKET_SIZE 8
#define TABLET_MAX 32767
#define REPORT_COUNT 32
typedef struct {
UINT16 X;
UINT16 Y;
UINT8 Buttons;
} TABLET_REPORT;
STATIC EFI_SYSTEM_TABLE *System;
STATIC EFI_GUID UsbIoGuid = EFI_USB_IO_PROTOCOL_GUID;
STATIC EFI_GUID GraphicsGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
STATIC EFI_GUID EventGuid = APPLE_EVENT_PROTOCOL_GUID;
STATIC EFI_GUID PointerGuid = EFI_SIMPLE_POINTER_PROTOCOL_GUID;
STATIC EFI_GUID AppleVendorGuid = APPLE_VENDOR_VARIABLE_GUID;
STATIC EFI_USB_IO_PROTOCOL *Tablet;
STATIC TABLET_REPORT Reports[REPORT_COUNT];
STATIC UINTN Head, Tail;
STATIC EFI_STATUS EFIAPI TabletInput(VOID *Data, UINTN Length, VOID *Context,
UINT32 Status) {
if (Status != EFI_USB_NOERROR || Length < 5) {
return EFI_DEVICE_ERROR;
}
UINTN Next = (Head + 1) % REPORT_COUNT;
if (Next == Tail) {
Tail = (Tail + 1) % REPORT_COUNT;
}
CONST UINT8 *Bytes = Data;
Reports[Head].X = (UINT16)((UINT16)Bytes[1] | ((UINT16)Bytes[2] << 8));
Reports[Head].Y = (UINT16)((UINT16)Bytes[3] | ((UINT16)Bytes[4] << 8));
Reports[Head].Buttons = Bytes[0];
Head = Next;
return EFI_SUCCESS;
}
STATIC BOOLEAN Attach(EFI_USB_IO_PROTOCOL *Usb) {
EFI_USB_DEVICE_DESCRIPTOR Device;
EFI_USB_INTERFACE_DESCRIPTOR Interface;
if (EFI_ERROR(Usb->UsbGetDeviceDescriptor(Usb, &Device)) ||
Device.IdVendor != 0x0627 || Device.IdProduct != 0x0001 ||
EFI_ERROR(Usb->UsbGetInterfaceDescriptor(Usb, &Interface)) ||
Interface.InterfaceClass != 3 || Interface.InterfaceSubClass != 0 ||
Interface.InterfaceProtocol != 0) {
return FALSE;
}
if (EFI_ERROR(Usb->UsbAsyncInterruptTransfer(
Usb, TABLET_ENDPOINT, TRUE, 10, TABLET_PACKET_SIZE, TabletInput, NULL))) {
return FALSE;
}
Tablet = Usb;
return TRUE;
}
STATIC VOID EFIAPI FindTablet(EFI_EVENT Event, VOID *Context) {
EFI_BOOT_SERVICES *Boot = System->BootServices;
EFI_HANDLE *Handles;
UINTN Count;
if (EFI_ERROR(Boot->LocateHandleBuffer(
ByProtocol, &UsbIoGuid, NULL, &Count, &Handles))) {
return;
}
for (UINTN Index = 0; Index < Count; ++Index) {
EFI_USB_IO_PROTOCOL *Usb;
if (!EFI_ERROR(Boot->HandleProtocol(Handles[Index], &UsbIoGuid,
(VOID **)&Usb)) &&
Attach(Usb)) {
Boot->CloseEvent(Event);
break;
}
}
Boot->FreePool(Handles);
}
STATIC VOID EFIAPI StopTablet(EFI_EVENT Event, VOID *Context) {
if (Tablet != NULL) {
Tablet->UsbAsyncInterruptTransfer(
Tablet, TABLET_ENDPOINT, FALSE, 0, 0, NULL, NULL);
}
}
STATIC EFI_STATUS EFIAPI Reset(EFI_SIMPLE_POINTER_PROTOCOL *This,
BOOLEAN Extended) {
Head = Tail = 0;
return EFI_SUCCESS;
}
STATIC VOID EFIAPI WaitForInput(EFI_EVENT Event, VOID *Context) {
if (Head != Tail) {
System->BootServices->SignalEvent(Event);
}
}
STATIC INT32 TabletPixel(UINT16 Position, UINT32 Extent) {
UINT32 Coordinate = Position < TABLET_MAX ? Position : TABLET_MAX;
return (INT32)((Coordinate * (Extent - 1) + TABLET_MAX / 2) / TABLET_MAX);
}
STATIC EFI_STATUS EFIAPI ReadPointer(EFI_SIMPLE_POINTER_PROTOCOL *This,
EFI_SIMPLE_POINTER_STATE *State) {
if (Head == Tail) {
return EFI_NOT_READY;
}
EFI_BOOT_SERVICES *Boot = System->BootServices;
APPLE_EVENT_PROTOCOL *AppleEvent;
EFI_GRAPHICS_OUTPUT_PROTOCOL *Graphics;
if (EFI_ERROR(Boot->LocateProtocol(&EventGuid, NULL, (VOID **)&AppleEvent)) ||
EFI_ERROR(Boot->HandleProtocol(System->ConsoleOutHandle, &GraphicsGuid,
(VOID **)&Graphics))) {
return EFI_NOT_READY;
}
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Mode = Graphics->Mode->Info;
TABLET_REPORT Report = Reports[Tail];
Tail = (Tail + 1) % REPORT_COUNT;
DIMENSION Position = {
TabletPixel(Report.X, Mode->HorizontalResolution),
TabletPixel(Report.Y, Mode->VerticalResolution)
};
// OpenCore's Simple Pointer adapter emits a motion event only for changed
// coordinates. Anchor one UI-scaled pixel-step away from the exact target
// and return the compensating unit step. Button events use the final target.
UINT8 Scale = 1;
UINTN Size = sizeof(Scale);
if (EFI_ERROR(System->RuntimeServices->GetVariable(
APPLE_UI_SCALE_VARIABLE_NAME, &AppleVendorGuid, NULL, &Size, &Scale)) ||
Scale != 2) {
Scale = 1;
}
INT32 Step = Position.Horizontal >= Scale ? 1 : -1;
Position.Horizontal -= Step * Scale;
EFI_STATUS Status = AppleEvent->SetCursorPosition(&Position);
if (EFI_ERROR(Status)) {
return Status;
}
State->RelativeMovementX = Step;
State->RelativeMovementY = 0;
State->RelativeMovementZ = 0;
State->LeftButton = (Report.Buttons & 1) != 0;
State->RightButton = (Report.Buttons & 2) != 0;
return EFI_SUCCESS;
}
STATIC EFI_SIMPLE_POINTER_MODE PointerMode = { 1, 1, 1, TRUE, TRUE };
STATIC EFI_SIMPLE_POINTER_PROTOCOL Pointer = {
Reset, ReadPointer, NULL, &PointerMode
};
EFI_STATUS EFIAPI DriverEntry(EFI_HANDLE Image, EFI_SYSTEM_TABLE *Table) {
System = Table;
EFI_BOOT_SERVICES *Boot = Table->BootServices;
EFI_STATUS Status = Boot->CreateEvent(EVT_NOTIFY_WAIT, TPL_NOTIFY,
WaitForInput, NULL, &Pointer.WaitForInput);
if (EFI_ERROR(Status)) {
return Status;
}
EFI_EVENT AttachTimer;
Status = Boot->CreateEvent(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
FindTablet, NULL, &AttachTimer);
if (EFI_ERROR(Status)) {
goto CloseInput;
}
EFI_EVENT ExitEvent;
Status = Boot->CreateEvent(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY,
StopTablet, NULL, &ExitEvent);
if (EFI_ERROR(Status)) {
goto CloseTimer;
}
EFI_HANDLE Handle = NULL;
Status = Boot->InstallProtocolInterface(&Handle, &PointerGuid,
EFI_NATIVE_INTERFACE, &Pointer);
if (!EFI_ERROR(Status)) {
Boot->SetTimer(AttachTimer, TimerPeriodic, 1000000);
return EFI_SUCCESS;
}
Boot->CloseEvent(ExitEvent);
CloseTimer:
Boot->CloseEvent(AttachTimer);
CloseInput:
Boot->CloseEvent(Pointer.WaitForInput);
return Status;
}