Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magic Trackpad 2 Improvement #10

Merged
merged 4 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/MagicTrackpad2PtpDevice/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ AmtPtpEvtDevicePrepareHardware(
(pDeviceContext->DeviceInfo->w.max - pDeviceContext->DeviceInfo->w.min) / pDeviceContext->DeviceInfo->w.snratio :
0.0;

pDeviceContext->SgContactSizeQualLevel = SIZE_QUALIFICATION_THRESHOLD;
pDeviceContext->MuContactSizeQualLevel = SIZE_MU_LOWER_THRESHOLD;
pDeviceContext->PressureQualLevel = PRESSURE_QUALIFICATION_THRESHOLD;

TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DEVICE,
Expand Down
4 changes: 4 additions & 0 deletions src/MagicTrackpad2PtpDevice/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ typedef struct _DEVICE_CONTEXT

ULONG UsbDeviceTraits;

UCHAR PressureQualLevel;
UCHAR SgContactSizeQualLevel;
UCHAR MuContactSizeQualLevel;

BOOL IsWellspringModeOn;
BOOL IsSurfaceReportOn;
BOOL IsButtonReportOn;
Expand Down
83 changes: 80 additions & 3 deletions src/MagicTrackpad2PtpDevice/Hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

HID_REPORT_DESCRIPTOR AAPLMagicTrackpad2ReportDescriptor[] = {
AAPL_MAGIC_TRACKPAD2_PTP_TLC,
AAPL_PTP_CONFIGURATION_TLC
AAPL_PTP_WINDOWS_CONFIGURATION_TLC,
AAPL_PTP_USERMODE_CONFIGURATION_APP_TLC
};

CONST HID_DESCRIPTOR AAPLMagicTrackpad2DefaultHidDescriptor = {
Expand Down Expand Up @@ -611,7 +612,7 @@ AmtPtpReportFeatures(
);

// Size sanity check
reportSize = sizeof(PPTP_DEVICE_CAPS_FEATURE_REPORT) + sizeof(packet.reportId);
reportSize = sizeof(PTP_DEVICE_CAPS_FEATURE_REPORT) + sizeof(packet.reportId);
if (packet.reportBufferLen < reportSize) {
status = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(
Expand Down Expand Up @@ -661,7 +662,7 @@ AmtPtpReportFeatures(
);

// Size sanity check
reportSize = sizeof(PPTP_DEVICE_HQA_CERTIFICATION_REPORT) + sizeof(packet.reportId);
reportSize = sizeof(PTP_DEVICE_HQA_CERTIFICATION_REPORT) + sizeof(packet.reportId);
if (packet.reportBufferLen < reportSize) {
status = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(
Expand Down Expand Up @@ -689,6 +690,45 @@ AmtPtpReportFeatures(
);
break;
}
case REPORTID_UMAPP_CONF:
{
TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DRIVER,
"%!FUNC! Report REPORTID_UMAPP_CONF is requested"
);

// Size sanity check
reportSize = sizeof(PTP_USERMODEAPP_CONF_REPORT) + sizeof(packet.reportId);
if (packet.reportBufferLen < reportSize) {
status = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(
TRACE_LEVEL_ERROR,
TRACE_DRIVER,
"%!FUNC! Report buffer is too small."
);
goto exit;
}

PPTP_USERMODEAPP_CONF_REPORT confReport = (PPTP_USERMODEAPP_CONF_REPORT)packet.reportBuffer;

confReport->ReportID = REPORTID_UMAPP_CONF;
confReport->MultipleContactSizeQualificationLevel = deviceContext->MuContactSizeQualLevel;
confReport->SingleContactSizeQualificationLevel = deviceContext->SgContactSizeQualLevel;
confReport->PressureQualificationLevel = deviceContext->PressureQualLevel;

TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DRIVER,
"%!FUNC! Report REPORTID_UMAPP_CONF is fulfilled"
);

WdfRequestSetInformation(
Request,
reportSize
);
break;
}
default:
TraceEvents(
TRACE_LEVEL_INFORMATION,
Expand Down Expand Up @@ -846,6 +886,43 @@ AmtPtpSetFeatures(
);
break;
}
case REPORTID_UMAPP_CONF:
{
TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DRIVER,
"%!FUNC! Report REPORTID_UMAPP_CONF is requested"
);
PPTP_USERMODEAPP_CONF_REPORT umConfInput = (PPTP_USERMODEAPP_CONF_REPORT) packet.reportBuffer;

// Set value
deviceContext->SgContactSizeQualLevel = umConfInput->SingleContactSizeQualificationLevel;
deviceContext->MuContactSizeQualLevel = umConfInput->MultipleContactSizeQualificationLevel;
deviceContext->PressureQualLevel = umConfInput->PressureQualificationLevel;

TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DRIVER,
"%!FUNC! Report REPORTID_UMAPP_CONF requested PressureQual = %d, SgSize = %d, MuSize = %d",
umConfInput->PressureQualificationLevel,
umConfInput->SingleContactSizeQualificationLevel,
umConfInput->MultipleContactSizeQualificationLevel
);

// Report back
WdfRequestSetInformation(
Request,
sizeof(PTP_USERMODEAPP_CONF_REPORT)
);

TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_DRIVER,
"%!FUNC! Report REPORTID_UMAPP_CONF is fulfilled"
);

break;
}
default:
TraceEvents(
TRACE_LEVEL_INFORMATION,
Expand Down
76 changes: 44 additions & 32 deletions src/MagicTrackpad2PtpDevice/Hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef UCHAR HID_REPORT_DESCRIPTOR, *PHID_REPORT_DESCRIPTOR;
#define REPORTID_PTPHQA 0x08
#define REPORTID_FUNCSWITCH 0x06
#define REPORTID_DEVICE_CAPS 0x07
#define REPORTID_UMAPP_CONF 0x09

#define BUTTON_SWITCH 0x57
#define SURFACE_SWITCH 0x58
Expand Down Expand Up @@ -50,7 +51,20 @@ typedef UCHAR HID_REPORT_DESCRIPTOR, *PHID_REPORT_DESCRIPTOR;
#define BEGIN_COLLECTION 0xa1
#define END_COLLECTION 0xc0

#define AAPL_PTP_CONFIGURATION_TLC \
#define AAPL_PTP_USERMODE_CONFIGURATION_APP_TLC \
USAGE_PAGE_1, 0x00, 0xff, /* Usage Page: Vendor defined */ \
USAGE, 0x01, /* Usage: Vendor Usage 0x01 */ \
BEGIN_COLLECTION, 0x01, /* Begin Collection: Application */ \
REPORT_ID, REPORTID_UMAPP_CONF, /* Report ID: User-mode Application configuration */ \
USAGE, 0x01, /* Usage: Vendor Usage 0x01 */ \
LOGICAL_MINIMUM, 0x00, /* Logical Minimum 0 */ \
LOGICAL_MAXIMUM_2, 0xff, 0x00, /* Logical Maximum 255 */ \
REPORT_SIZE, 0x08, /* Report Size: 8 */ \
REPORT_COUNT, 0x03, /* Report Count: 3 */ \
FEATURE, 0x02, /* Feature: (Data, Var, Abs) */ \
END_COLLECTION

#define AAPL_PTP_WINDOWS_CONFIGURATION_TLC \
USAGE_PAGE, 0x0d, /* Usage Page: Digitizer */ \
USAGE, 0x0e, /* Usage: Configuration */ \
BEGIN_COLLECTION, 0x01, /* Begin Collection: Application */ \
Expand All @@ -62,7 +76,7 @@ typedef UCHAR HID_REPORT_DESCRIPTOR, *PHID_REPORT_DESCRIPTOR;
LOGICAL_MAXIMUM, MAX_FINGERS, /* Logical Maximum: MAX_TOUCH_COUNT fingers */ \
REPORT_SIZE, 0x08, /* Report Size: 0x08 */ \
REPORT_COUNT, 0x01, /* Report Count: 0x01 */ \
FEATURE, 0x02, /* Feature: (Data, Var, Avs) */ \
FEATURE, 0x02, /* Feature: (Data, Var, Abs) */ \
END_COLLECTION, /* End Collection */ \
BEGIN_COLLECTION, 0x00, /* Begin Collection: Physical */ \
REPORT_ID, REPORTID_FUNCSWITCH, /* Report ID: Function Switch */ \
Expand Down Expand Up @@ -253,75 +267,73 @@ typedef UCHAR HID_REPORT_DESCRIPTOR, *PHID_REPORT_DESCRIPTOR;
#define PTP_CONTACT_TIPSWITCH_BIT 2

typedef struct _HID_AAPL_MOUSE_REPORT {
struct
{
struct {
UCHAR bButtons;
UCHAR wXData;
UCHAR wYData;
UINT Padding;
} InputReport;
} HID_AAPL_MOUSE_REPORT, *PHID_AAPL_MOUSE_REPORT;

typedef struct _HID_INPUT_REPORT
{
typedef struct _HID_INPUT_REPORT {
UCHAR ReportID;
HID_AAPL_MOUSE_REPORT MouseReport;
} HID_INPUT_REPORT, *PHID_INPUT_REPORT;

typedef struct _PTP_DEVICE_CAPS_FEATURE_REPORT
{
typedef struct _PTP_DEVICE_CAPS_FEATURE_REPORT {
UCHAR ReportID;
UCHAR MaximumContactPoints;
UCHAR ButtonType;
} PTP_DEVICE_CAPS_FEATURE_REPORT, *PPTP_DEVICE_CAPS_FEATURE_REPORT;

typedef struct _PTP_DEVICE_HQA_CERTIFICATION_REPORT
{
typedef struct _PTP_DEVICE_HQA_CERTIFICATION_REPORT {
UCHAR ReportID;
UCHAR CertificationBlob[256];
} PTP_DEVICE_HQA_CERTIFICATION_REPORT, *PPTP_DEVICE_HQA_CERTIFICATION_REPORT;

typedef struct _PTP_DEVICE_INPUT_MODE_REPORT
{
typedef struct _PTP_DEVICE_INPUT_MODE_REPORT {
UCHAR ReportID;
UCHAR Mode;
} PTP_DEVICE_INPUT_MODE_REPORT, *PPTP_DEVICE_INPUT_MODE_REPORT;

#pragma pack(1)
typedef struct _PTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT
{
typedef struct _PTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT {
UCHAR ReportID;
UCHAR ButtonReport : 1;
UCHAR SurfaceReport : 1;
UCHAR Padding : 6;
} PTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT, *PPTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT;

#pragma pack(1)
typedef struct _PTP_CONTACT
{
UCHAR Confidence : 1;
UCHAR TipSwitch : 1;
UCHAR ContactID : 3;
UCHAR Padding : 3;
USHORT X;
USHORT Y;
typedef struct _PTP_CONTACT {
UCHAR Confidence : 1;
UCHAR TipSwitch : 1;
UCHAR ContactID : 3;
UCHAR Padding : 3;
USHORT X;
USHORT Y;
} PTP_CONTACT, *PPTP_CONTACT;

// Used for defuzz - not report
typedef struct _PTP_CONTACT_RAW
{
USHORT X;
USHORT Y;
UCHAR Pressure;
UCHAR Size;
UCHAR ContactId;
typedef struct _PTP_CONTACT_RAW {
USHORT X;
USHORT Y;
UCHAR Pressure;
UCHAR Size;
UCHAR ContactId;
} PTP_CONTACT_RAW, *PPTP_CONTACT_RAW;

typedef struct _PTP_REPORT
{
typedef struct _PTP_REPORT {
UCHAR ReportID;
PTP_CONTACT Contacts[5];
USHORT ScanTime;
UCHAR ContactCount;
UCHAR IsButtonClicked;
} PTP_REPORT, *PPTP_REPORT;
} PTP_REPORT, *PPTP_REPORT;

typedef struct _PTP_USERMODEAPP_CONF_REPORT {
UCHAR ReportID;
UCHAR PressureQualificationLevel;
UCHAR SingleContactSizeQualificationLevel;
UCHAR MultipleContactSizeQualificationLevel;
} PTP_USERMODEAPP_CONF_REPORT, *PPTP_USERMODEAPP_CONF_REPORT;
31 changes: 9 additions & 22 deletions src/MagicTrackpad2PtpDevice/InputInterrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ AmtPtpServiceTouchInputInterruptType5(

// Set flags (by cases)
if (raw_n == 1) {
report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD;
report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_QUALIFICATION_THRESHOLD;
report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > DeviceContext->PressureQualLevel;
report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= DeviceContext->SgContactSizeQualLevel;

TraceEvents(
TRACE_LEVEL_INFORMATION,
Expand All @@ -258,26 +258,13 @@ AmtPtpServiceTouchInputInterruptType5(
report.Contacts[i].Confidence
);
} else {

// Save the information
// Use size to determine confidence in MU scenario
if (raw_n == 2) {
report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD;
report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_MU_LOWER_THRESHOLD;
}
else {
if (DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD) {
report.Contacts[i].TipSwitch = 1;
muTotalPressure += DeviceContext->ContactRepository[i].Pressure;
}

if (DeviceContext->ContactRepository[i].Size >= SIZE_MU_LOWER_THRESHOLD) {
report.Contacts[i].Confidence = 1;
muTotalSize += DeviceContext->ContactRepository[i].Size;
}
}

report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD;
report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_MU_LOWER_THRESHOLD;
muTotalPressure += DeviceContext->ContactRepository[i].Pressure;
muTotalSize += DeviceContext->ContactRepository[i].Size;
report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > DeviceContext->PressureQualLevel;
report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= DeviceContext->MuContactSizeQualLevel;

TraceEvents(
TRACE_LEVEL_INFORMATION,
Expand All @@ -297,7 +284,7 @@ AmtPtpServiceTouchInputInterruptType5(
}

if (actualFingers > 2) {
if (muTotalPressure > PRESSURE_MU_QUALIFICATION_THRESHOLD_TOTAL) {
if (muTotalPressure > DeviceContext->PressureQualLevel * 2.15) {
TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_INPUT,
Expand All @@ -307,7 +294,7 @@ AmtPtpServiceTouchInputInterruptType5(
report.Contacts[i].TipSwitch = 1;
}

if (muTotalSize > SIZE_MU_QUALIFICATION_THRESHOLD_TOTAL) {
if (muTotalSize > DeviceContext->MuContactSizeQualLevel * 2.15) {
TraceEvents(
TRACE_LEVEL_INFORMATION,
TRACE_INPUT,
Expand Down