mirror of
https://github.com/acidanthera/OpenCorePkg.git
synced 2025-12-08 19:25:01 +00:00
388 lines
8.9 KiB
C
388 lines
8.9 KiB
C
/** @file
|
|
This file is part of OpenCanopy, OpenCore GUI.
|
|
|
|
Copyright (c) 2018-2019, Download-Fritz. All rights reserved.<BR>
|
|
SPDX-License-Identifier: BSD-3-Clause
|
|
**/
|
|
|
|
#include <Uefi.h>
|
|
|
|
#include <Protocol/AppleEvent.h>
|
|
#include <Protocol/AbsolutePointer.h>
|
|
|
|
#include <Library/BaseLib.h>
|
|
#include <Library/BaseMemoryLib.h>
|
|
#include <Library/DebugLib.h>
|
|
#include <Library/OcGuardLib.h>
|
|
#include <Library/OcMiscLib.h>
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
#include "../OpenCanopy.h"
|
|
#include "../GuiIo.h"
|
|
|
|
struct GUI_POINTER_CONTEXT_ {
|
|
APPLE_EVENT_PROTOCOL *AppleEvent;
|
|
EFI_ABSOLUTE_POINTER_PROTOCOL *AbsPointer;
|
|
APPLE_EVENT_HANDLE AppleEventHandle;
|
|
UINT32 MaxX;
|
|
UINT32 MaxY;
|
|
UINT32 X;
|
|
UINT32 Y;
|
|
INT32 RawX;
|
|
INT32 RawY;
|
|
UINT8 LockedBy;
|
|
BOOLEAN PrimaryDown;
|
|
BOOLEAN SecondaryDown;
|
|
};
|
|
|
|
enum {
|
|
PointerUnlocked,
|
|
PointerLockedSimple,
|
|
PointerLockedAbsolute
|
|
};
|
|
|
|
STATIC
|
|
UINT32
|
|
InternalClipPointerSimple (
|
|
IN UINT32 OldCoord,
|
|
IN INT64 DeltaCoord,
|
|
IN UINT32 MaxCoord
|
|
)
|
|
{
|
|
BOOLEAN Result;
|
|
INT64 NewCoord;
|
|
|
|
Result = OcOverflowAddS64 (OldCoord, DeltaCoord, &NewCoord);
|
|
if (!Result) {
|
|
if (NewCoord <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (NewCoord > MaxCoord) {
|
|
return MaxCoord;
|
|
}
|
|
|
|
return (UINT32) NewCoord;
|
|
}
|
|
|
|
if (DeltaCoord < 0) {
|
|
return 0;
|
|
}
|
|
|
|
return MaxCoord;
|
|
}
|
|
|
|
STATIC
|
|
INT64
|
|
InternalGetInterpolatedValue (
|
|
IN INT32 Value
|
|
)
|
|
{
|
|
INTN Bit;
|
|
|
|
//
|
|
// For now this produces most natural speed.
|
|
//
|
|
STATIC CONST INT8 AccelerationNumbers[] = {2};
|
|
|
|
if (Value != 0) {
|
|
Bit = HighBitSet32 (ABS (Value));
|
|
return (INT64) Value * AccelerationNumbers[
|
|
MIN (Bit, (INTN) ARRAY_SIZE (AccelerationNumbers) - 1)
|
|
];
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
STATIC
|
|
VOID
|
|
EFIAPI
|
|
InternalAppleEventNotification (
|
|
IN APPLE_EVENT_INFORMATION *Information,
|
|
IN VOID *NotifyContext
|
|
)
|
|
{
|
|
APPLE_POINTER_EVENT_TYPE EventType;
|
|
GUI_POINTER_CONTEXT *Context;
|
|
INT32 NewX;
|
|
INT32 NewY;
|
|
INT64 Difference;
|
|
|
|
Context = NotifyContext;
|
|
|
|
ASSERT ((Information->EventType & APPLE_ALL_MOUSE_EVENTS) != 0);
|
|
|
|
EventType = Information->EventData.PointerEventType;
|
|
|
|
if ((EventType & APPLE_EVENT_TYPE_MOUSE_MOVED) != 0) {
|
|
NewX = Information->PointerPosition.Horizontal;
|
|
NewY = Information->PointerPosition.Vertical;
|
|
if (NewX == 0 || (UINT32) NewX == Context->MaxX) {
|
|
Context->X = (UINT32) NewX;
|
|
} else {
|
|
Difference = InternalGetInterpolatedValue (NewX - Context->RawX);
|
|
Context->X = InternalClipPointerSimple (
|
|
Context->X,
|
|
Difference,
|
|
Context->MaxX
|
|
);
|
|
}
|
|
|
|
if (NewY == 0 || (UINT32) NewY == Context->MaxY) {
|
|
Context->Y = (UINT32) NewY;
|
|
} else {
|
|
Difference = InternalGetInterpolatedValue (NewY - Context->RawY);
|
|
Context->Y = InternalClipPointerSimple (
|
|
Context->Y,
|
|
Difference,
|
|
Context->MaxY
|
|
);
|
|
}
|
|
|
|
Context->RawX = NewX;
|
|
Context->RawY = NewY;
|
|
}
|
|
|
|
if ((EventType & APPLE_EVENT_TYPE_MOUSE_DOWN) != 0) {
|
|
if ((EventType & APPLE_EVENT_TYPE_LEFT_BUTTON) != 0) {
|
|
Context->PrimaryDown = TRUE;
|
|
} else if ((EventType & APPLE_EVENT_TYPE_RIGHT_BUTTON) != 0) {
|
|
Context->SecondaryDown = TRUE;
|
|
}
|
|
} else if ((EventType & APPLE_EVENT_TYPE_MOUSE_UP) != 0) {
|
|
if ((EventType & APPLE_EVENT_TYPE_LEFT_BUTTON) != 0) {
|
|
Context->PrimaryDown = FALSE;
|
|
} else if ((EventType & APPLE_EVENT_TYPE_RIGHT_BUTTON) != 0) {
|
|
Context->SecondaryDown = FALSE;
|
|
}
|
|
}
|
|
}
|
|
|
|
STATIC
|
|
VOID
|
|
InternalUpdateStateSimpleAppleEvent (
|
|
IN OUT GUI_POINTER_CONTEXT *Context,
|
|
OUT GUI_POINTER_STATE *State
|
|
)
|
|
{
|
|
EFI_TPL OldTpl;
|
|
|
|
ASSERT (Context->AppleEvent != NULL);
|
|
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
State->X = Context->X;
|
|
State->Y = Context->Y;
|
|
State->PrimaryDown = Context->PrimaryDown;
|
|
State->SecondaryDown = Context->SecondaryDown;
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
}
|
|
|
|
STATIC
|
|
VOID
|
|
InternalUpdateStateAbsolute (
|
|
IN OUT GUI_POINTER_CONTEXT *Context,
|
|
OUT GUI_POINTER_STATE *State
|
|
)
|
|
{
|
|
EFI_STATUS Status;
|
|
EFI_ABSOLUTE_POINTER_STATE PointerState;
|
|
UINT64 NewX;
|
|
UINT64 NewY;
|
|
|
|
ASSERT (Context != NULL);
|
|
ASSERT (State != NULL);
|
|
|
|
if (Context->AbsPointer == NULL) {
|
|
return;
|
|
}
|
|
|
|
Status = Context->AbsPointer->GetState (Context->AbsPointer, &PointerState);
|
|
if (EFI_ERROR (Status)) {
|
|
return;
|
|
}
|
|
|
|
NewX = PointerState.CurrentX - Context->AbsPointer->Mode->AbsoluteMinX;
|
|
NewX *= Context->MaxX + 1;
|
|
NewX = DivU64x32 (NewX, (UINT32) (Context->AbsPointer->Mode->AbsoluteMaxX - Context->AbsPointer->Mode->AbsoluteMinX));
|
|
|
|
NewY = PointerState.CurrentY - Context->AbsPointer->Mode->AbsoluteMinY;
|
|
NewY *= Context->MaxY + 1;
|
|
NewY = DivU64x32 (NewY, (UINT32) (Context->AbsPointer->Mode->AbsoluteMaxY - Context->AbsPointer->Mode->AbsoluteMinY));
|
|
|
|
State->X = (UINT32)NewX;
|
|
State->Y = (UINT32)NewY;
|
|
|
|
State->PrimaryDown = (PointerState.ActiveButtons & EFI_ABSP_TouchActive) != 0;
|
|
State->SecondaryDown = (PointerState.ActiveButtons & EFI_ABS_AltActive) != 0;
|
|
//
|
|
// Adapt X/Y based on touch position so the cursor will not jump when
|
|
// changing from touch to mouse.
|
|
//
|
|
Context->X = (UINT32)NewX;
|
|
Context->Y = (UINT32)NewY;
|
|
}
|
|
|
|
VOID
|
|
GuiPointerReset (
|
|
IN OUT GUI_POINTER_CONTEXT *Context
|
|
)
|
|
{
|
|
EFI_ABSOLUTE_POINTER_STATE AbsoluteState;
|
|
|
|
ASSERT (Context != NULL);
|
|
|
|
if (Context->AbsPointer != NULL) {
|
|
Context->AbsPointer->GetState (Context->AbsPointer, &AbsoluteState);
|
|
}
|
|
|
|
Context->LockedBy = PointerUnlocked;
|
|
}
|
|
|
|
VOID
|
|
GuiPointerGetState (
|
|
IN OUT GUI_POINTER_CONTEXT *Context,
|
|
OUT GUI_POINTER_STATE *State
|
|
)
|
|
{
|
|
ASSERT (Context != NULL);
|
|
ASSERT (State != NULL);
|
|
|
|
switch (Context->LockedBy) {
|
|
case PointerUnlocked:
|
|
{
|
|
InternalUpdateStateSimpleAppleEvent (Context, State);
|
|
if ((State->PrimaryDown | State->SecondaryDown) != 0) {
|
|
Context->LockedBy = PointerLockedSimple;
|
|
} else {
|
|
InternalUpdateStateAbsolute (Context, State);
|
|
if ((State->PrimaryDown | State->SecondaryDown) != 0) {
|
|
Context->LockedBy = PointerLockedAbsolute;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case PointerLockedSimple:
|
|
{
|
|
InternalUpdateStateSimpleAppleEvent (Context, State);
|
|
if ((State->PrimaryDown | State->SecondaryDown) == 0) {
|
|
Context->LockedBy = PointerUnlocked;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
case PointerLockedAbsolute:
|
|
{
|
|
InternalUpdateStateAbsolute (Context, State);
|
|
if ((State->PrimaryDown | State->SecondaryDown) == 0) {
|
|
Context->LockedBy = PointerUnlocked;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
ASSERT (FALSE);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
GUI_POINTER_CONTEXT *
|
|
GuiPointerConstruct (
|
|
IN OC_PICKER_CONTEXT *PickerContext,
|
|
IN UINT32 DefaultX,
|
|
IN UINT32 DefaultY,
|
|
IN UINT32 Width,
|
|
IN UINT32 Height
|
|
)
|
|
{
|
|
// TODO: alloc on the fly?
|
|
STATIC GUI_POINTER_CONTEXT Context;
|
|
|
|
EFI_STATUS Status;
|
|
EFI_STATUS Status2;
|
|
EFI_TPL OldTpl;
|
|
DIMENSION Dimension;
|
|
|
|
ASSERT (DefaultX < Width);
|
|
ASSERT (DefaultY < Height);
|
|
ASSERT (Width <= MAX_INT32);
|
|
ASSERT (Height <= MAX_INT32);
|
|
|
|
Context.MaxX = Width - 1;
|
|
Context.MaxY = Height - 1;
|
|
Context.X = DefaultX;
|
|
Context.Y = DefaultY;
|
|
Context.RawX = (INT32) DefaultX;
|
|
Context.RawY = (INT32) DefaultY;
|
|
|
|
Status = OcHandleProtocolFallback (
|
|
gST->ConsoleInHandle,
|
|
&gAppleEventProtocolGuid,
|
|
(VOID **)&Context.AppleEvent
|
|
);
|
|
if (!EFI_ERROR (Status)) {
|
|
if (Context.AppleEvent->Revision >= APPLE_EVENT_PROTOCOL_REVISION) {
|
|
OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
|
|
|
|
Status = Context.AppleEvent->RegisterHandler (
|
|
APPLE_ALL_MOUSE_EVENTS,
|
|
InternalAppleEventNotification,
|
|
&Context.AppleEventHandle,
|
|
&Context
|
|
);
|
|
|
|
Dimension.Horizontal = (INT32) DefaultX;
|
|
Dimension.Vertical = (INT32) DefaultY;
|
|
Context.AppleEvent->SetCursorPosition (
|
|
&Dimension
|
|
);
|
|
|
|
gBS->RestoreTPL (OldTpl);
|
|
} else {
|
|
Status = EFI_UNSUPPORTED;
|
|
}
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
DEBUG ((
|
|
DEBUG_WARN,
|
|
"OCUI: AppleEvent %u is unsupported - %r\n",
|
|
Context.AppleEvent->Revision,
|
|
Status
|
|
));
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
Status2 = OcHandleProtocolFallback (
|
|
gST->ConsoleInHandle,
|
|
&gEfiAbsolutePointerProtocolGuid,
|
|
(VOID **)&Context.AbsPointer
|
|
);
|
|
|
|
if (EFI_ERROR (Status) && EFI_ERROR (Status2)) {
|
|
return NULL;
|
|
}
|
|
|
|
return &Context;
|
|
}
|
|
|
|
VOID
|
|
GuiPointerDestruct (
|
|
IN GUI_POINTER_CONTEXT *Context
|
|
)
|
|
{
|
|
ASSERT (Context != NULL);
|
|
ASSERT (Context->AppleEvent != NULL);
|
|
|
|
Context->AppleEvent->UnregisterHandler (Context->AppleEventHandle);
|
|
ZeroMem (Context, sizeof (*Context));
|
|
}
|