Skip to content

Commit

Permalink
RawInputs parsing: supporting sequential contacts trying to fix #63, #61
Browse files Browse the repository at this point in the history
, #44 and #39
  • Loading branch information
ClementGre committed Jul 22, 2024
1 parent 7150780 commit 40de4e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 54 deletions.
3 changes: 2 additions & 1 deletion ThreeFingerDragOnWindows/threefingerdrag/ThreeFingerDrag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public ThreeFingerDrag(){

public void OnTouchpadContact(TouchpadContact[] oldContacts, TouchpadContact[] contacts, long elapsed){
bool hasFingersReleased = elapsed > RELEASE_FINGERS_THRESHOLD_MS;
Logger.Log("");
Logger.Log("TFD: " + string.Join(", ", oldContacts.Select(c => c.ToString())) + " | " +
string.Join(", ", contacts.Select(c => c.ToString())) + " | " + elapsed);
bool areContactsIdsCommons = FingerCounter.AreContactsIdsCommons(oldContacts, contacts);
Expand Down Expand Up @@ -78,11 +77,13 @@ public void OnTouchpadContact(TouchpadContact[] oldContacts, TouchpadContact[] c
_dragEndTimer.Start();
}
}
Logger.Log("");
}

private void OnTimerElapsed(object source, ElapsedEventArgs e){
if(_isDragging){
Logger.Log(" STOP DRAG FROM TIMER, Left click up");
Logger.Log("");
StopDrag();
}
}
Expand Down
126 changes: 74 additions & 52 deletions ThreeFingerDragOnWindows/touchpad/TouchpadHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using ThreeFingerDragEngine.utils;
Expand Down Expand Up @@ -151,64 +152,85 @@ public static TouchpadContact[] ParseInput(IntPtr lParam){
return null;

uint scanTime = 0;
uint contactCount = 0;
TouchpadContactCreator creator = new();
uint contactCount = 99;

List<TouchpadContactCreator> creators = new();
List<TouchpadContact> contacts = new();

// Iterating though each value (scanTime, contactCount, contactId, x, y)
// Sometimes, iterates also through contacts by looping these values for each contact
String toLog = "Parsing RawInput: ";
foreach(var valueCap in valueCaps.OrderBy(x => x.LinkCollection)){
if(HidP_GetUsageValue(
HIDP_REPORT_TYPE.HidP_Input,
valueCap.UsagePage,
valueCap.LinkCollection,
valueCap.Usage,
out var value,
preparsedDataPointer,
rawHidRawDataPointer,
(uint)rawHidRawData.Length) != HIDP_STATUS_SUCCESS)
continue;

// Usage Page and ID in Windows Precision Touchpad input reports
// https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections#windows-precision-touchpad-input-reports
switch(valueCap.LinkCollection){
case 0:
switch (valueCap.UsagePage, valueCap.Usage){
case (0x0D, 0x56): // Scan Time
scanTime = value;
break;

case (0x0D, 0x54): // Contact Count
contactCount = value;
break;
}

break;

default:
switch (valueCap.UsagePage, valueCap.Usage){
case (0x0D, 0x51): // Contact ID
creator.ContactId = (int)value;
break;

case (0x01, 0x30): // X
creator.X = (int)value;
break;

case (0x01, 0x31): // Y
creator.Y = (int)value;
break;
}

break;
toLog += "| ";
// In case this valueCap contains multiple contacts at a time (rawInput.Hid.dwCount), iterates over each contact
for(int contactIndex = 0; contactIndex < rawInput.Hid.dwCount; contactIndex++){
toLog += contactIndex + ": ";
IntPtr rawHidRawDataPointerAdjusted = IntPtr.Add(rawHidRawDataPointer, (int) (rawInput.Hid.dwSizeHid * contactIndex));

if(HidP_GetUsageValue(
HIDP_REPORT_TYPE.HidP_Input,
valueCap.UsagePage,
valueCap.LinkCollection,
valueCap.Usage,
out var value,
preparsedDataPointer,
rawHidRawDataPointerAdjusted,
(uint) rawHidRawData.Length) != HIDP_STATUS_SUCCESS)
continue;

// Usage Page and ID in Windows Precision Touchpad input reports
// https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/windows-precision-touchpad-required-hid-top-level-collections#windows-precision-touchpad-input-reports
switch (valueCap.LinkCollection){
case 0:
switch (valueCap.UsagePage, valueCap.Usage){
case (0x0D, 0x56): // Scan Time
toLog += $"sT{value} ";
scanTime = value;
break;

case (0x0D, 0x54): // Contact Count
toLog += $"cC{value} ";
contactCount = value;
break;
}

break;

default:
while(creators.Count <= contactIndex){
creators.Add(new TouchpadContactCreator());
}

switch (valueCap.UsagePage, valueCap.Usage){
case (0x0D, 0x51): // Contact ID
toLog += $"ID{(int) value} ";
creators[contactIndex].ContactId = (int) value;
break;

case (0x01, 0x30): // X
toLog += "X ";
creators[contactIndex].X = (int) value;
break;

case (0x01, 0x31): // Y
toLog += "Y ";
creators[contactIndex].Y = (int) value;
break;
}
break;
}
}

if(creator.TryCreate(out var contact)){
contacts.Add(contact);
if(contacts.Count >= contactCount)
break;

creator.Clear();
creators.ForEach(creator => {
if(contacts.Count < contactCount && creator.TryCreate(out var contact)){
contacts.Add(contact);
creator.Clear();
}
});
if(contacts.Count >= contactCount){
break;
}
}
Logger.Log(toLog);

return contacts.ToArray();
} finally{
Expand Down
2 changes: 1 addition & 1 deletion ThreeFingerDragOnWindows/utils/TouchpadContact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ public void Clear(){
X = null;
Y = null;
}
}
}

0 comments on commit 40de4e2

Please sign in to comment.