Skip to content

Commit

Permalink
Remove redundant conversions and pass raw data
Browse files Browse the repository at this point in the history
  • Loading branch information
Reeywhaar committed Sep 11, 2023
1 parent e664244 commit c92e0fb
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 167 deletions.
14 changes: 2 additions & 12 deletions MiddleMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
F3685AE82AAE513500901633 /* MultitouchSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F3685AE72AAE513400901633 /* MultitouchSupport.framework */; };
F3685AE92AAE513500901633 /* MultitouchSupport.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = F3685AE72AAE513400901633 /* MultitouchSupport.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
F3795EA32AA53FD8006AFC22 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3795EA22AA53FD8006AFC22 /* Logger.swift */; };
F37B36232AAE58900054E184 /* OpenMTTouch.m in Sources */ = {isa = PBXBuildFile; fileRef = F3A69CA52AAE55D300B143CE /* OpenMTTouch.m */; };
F37B36242AAE58920054E184 /* OpenMTManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F3A69CA92AAE55D300B143CE /* OpenMTManager.m */; };
F37B36252AAE58940054E184 /* OpenMTEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = F3A69CAA2AAE55D300B143CE /* OpenMTEvent.m */; };
F37B36262AAE58960054E184 /* OpenMTListener.m in Sources */ = {isa = PBXBuildFile; fileRef = F3A69CAC2AAE55D300B143CE /* OpenMTListener.m */; };
F3A69CA22AAE54B400B143CE /* MiddleMe-Bridging-Header.h in Sources */ = {isa = PBXBuildFile; fileRef = F3A69CA12AAE546900B143CE /* MiddleMe-Bridging-Header.h */; };
F3CE2CAA283FFCB30081B57D /* MiddleMeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3CE2CA9283FFCB30081B57D /* MiddleMeApp.swift */; };
Expand Down Expand Up @@ -71,15 +69,12 @@
F3795EA22AA53FD8006AFC22 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
F3A69CA12AAE546900B143CE /* MiddleMe-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MiddleMe-Bridging-Header.h"; sourceTree = "<group>"; };
F3A69CA42AAE55D300B143CE /* OpenMTListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMTListener.h; sourceTree = "<group>"; };
F3A69CA52AAE55D300B143CE /* OpenMTTouch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OpenMTTouch.m; sourceTree = "<group>"; };
F3A69CA72AAE55D300B143CE /* OpenMTInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMTInternal.h; sourceTree = "<group>"; };
F3A69CA82AAE55D300B143CE /* OpenMultitouchSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMultitouchSupport.h; sourceTree = "<group>"; };
F3A69CA92AAE55D300B143CE /* OpenMTManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OpenMTManager.m; sourceTree = "<group>"; };
F3A69CAA2AAE55D300B143CE /* OpenMTEvent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OpenMTEvent.m; sourceTree = "<group>"; };
F3A69CAB2AAE55D300B143CE /* OpenMTTouch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMTTouch.h; sourceTree = "<group>"; };
F3A69CAC2AAE55D300B143CE /* OpenMTListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OpenMTListener.m; sourceTree = "<group>"; };
F3A69CB02AAE55D300B143CE /* OpenMTEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMTEvent.h; sourceTree = "<group>"; };
F3A69CB22AAE55D300B143CE /* OpenMTManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OpenMTManager.h; sourceTree = "<group>"; };
F3B91DD82AAFAE9500079D3B /* OpenMTPublic.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OpenMTPublic.h; sourceTree = "<group>"; };
F3CE2CA6283FFCB30081B57D /* MiddleMe.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MiddleMe.app; sourceTree = BUILT_PRODUCTS_DIR; };
F3CE2CA9283FFCB30081B57D /* MiddleMeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MiddleMeApp.swift; sourceTree = "<group>"; };
F3CE2CAB283FFCB30081B57D /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -139,10 +134,7 @@
children = (
F3A69CA82AAE55D300B143CE /* OpenMultitouchSupport.h */,
F3A69CA72AAE55D300B143CE /* OpenMTInternal.h */,
F3A69CB02AAE55D300B143CE /* OpenMTEvent.h */,
F3A69CAA2AAE55D300B143CE /* OpenMTEvent.m */,
F3A69CAB2AAE55D300B143CE /* OpenMTTouch.h */,
F3A69CA52AAE55D300B143CE /* OpenMTTouch.m */,
F3B91DD82AAFAE9500079D3B /* OpenMTPublic.h */,
F3A69CA42AAE55D300B143CE /* OpenMTListener.h */,
F3A69CAC2AAE55D300B143CE /* OpenMTListener.m */,
F3A69CB22AAE55D300B143CE /* OpenMTManager.h */,
Expand Down Expand Up @@ -421,9 +413,7 @@
buildActionMask = 2147483647;
files = (
F3A69CA22AAE54B400B143CE /* MiddleMe-Bridging-Header.h in Sources */,
F37B36232AAE58900054E184 /* OpenMTTouch.m in Sources */,
F37B36242AAE58920054E184 /* OpenMTManager.m in Sources */,
F37B36252AAE58940054E184 /* OpenMTEvent.m in Sources */,
F37B36262AAE58960054E184 /* OpenMTListener.m in Sources */,
F3795EA32AA53FD8006AFC22 /* Logger.swift in Sources */,
F3CE2CD928400E250081B57D /* GlobalEventMonitor.swift in Sources */,
Expand Down
7 changes: 4 additions & 3 deletions MiddleMe/GlobalEventMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ class GlobalEventMonitor {
listener = nil
}

@objc func process(_ event: OpenMTEvent) {
guard let touches = event.touches as NSArray as? [OpenMTTouch] else { return }
@objc func process(_ count: Int, _ touches: UnsafePointer<MTTouch>) {
let buffer = UnsafeBufferPointer(start: touches, count: count);
let touches = Array(buffer)
let touchesCount = touches.count

let ended = touchesCount == 0

if touchesCount == 3 {
lastCoordinates = averageCoordinates(coords: touches.map({ NSPoint(x: CGFloat($0.posX), y: CGFloat($0.posY)) }))
lastCoordinates = averageCoordinates(coords: touches.map({ NSPoint(x: CGFloat($0.normalizedPosition.position.x), y: CGFloat($0.normalizedPosition.position.y)) }))
if startCoordinates == .zero { startCoordinates = lastCoordinates }
}

Expand Down
22 changes: 0 additions & 22 deletions MiddleMe/OpenMultitouchSupport/OpenMTEvent.h

This file was deleted.

15 changes: 0 additions & 15 deletions MiddleMe/OpenMultitouchSupport/OpenMTEvent.m

This file was deleted.

44 changes: 2 additions & 42 deletions MiddleMe/OpenMultitouchSupport/OpenMTInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,12 @@
#ifndef OpenMTInternal_h
#define OpenMTInternal_h

#import "OpenMTPublic.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
float x;
float y;
} MTPoint;

typedef struct {
MTPoint position;
MTPoint velocity;
} MTVector;

enum {
MTTouchStateNotTracking = 0,
MTTouchStateStartInRange = 1,
MTTouchStateHoverInRange = 2,
MTTouchStateMakeTouch = 3,
MTTouchStateTouching = 4,
MTTouchStateBreakTouch = 5,
MTTouchStateLingerInRange = 6,
MTTouchStateOutOfRange = 7
};

typedef int MTTouchState;

typedef struct {
int frame;
double timestamp;
int identifier;
MTTouchState state;
int fingerId;
int handId;
MTVector normalizedPosition;
float total; //total of capacitance
float pressure;
float angle;
float majorAxis;
float minorAxis;
MTVector absolutePosition;
int field14;
int field15;
float density; //area density of capacitance
} MTTouch;

typedef void *MTDeviceRef;
typedef void (*MTFrameCallbackFunction)(MTDeviceRef device, MTTouch touches[], int numTouches, double timestamp, int frame);
typedef void (*MTPathCallbackFunction)(MTDeviceRef device, long pathID, long state, MTTouch* touch);
Expand Down
6 changes: 4 additions & 2 deletions MiddleMe/OpenMultitouchSupport/OpenMTListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
#define OpenMTListener_h

#import <Foundation/Foundation.h>
#import "OpenMTEvent.h"
#import "OpenMTInternal.h"

@interface OpenMTListener: NSObject

- (instancetype)initWithTarget:(id)target selector:(SEL)selector;
- (void)listenToEvent:(OpenMTEvent *)event;
- (void)count:(int)numTouches touches:(MTTouch [])touches;
- (BOOL)dead;

@property (assign, readwrite) BOOL listening;

@end

typedef void (^OpenMTEventCallback)(MTTouch *touches);

#endif /* OpenMTListener_h */
7 changes: 4 additions & 3 deletions MiddleMe/OpenMultitouchSupport/OpenMTListener.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "OpenMTListener.h"
#import "OpenMTInternal.h"

@implementation OpenMTListener {
@private
Expand All @@ -30,16 +31,16 @@ - (instancetype)initWithTarget:(id)target selector:(SEL)selector {
return self;
}

- (void)listenToEvent:(OpenMTEvent *)event {
- (void)count:(int)numTouches touches:(MTTouch [])touches {
if (self.dead || !self.listening) {
return;
}
if (_callback) {
_callback(event);
_callback(touches);
return;
}
if (_target) {
((void(*)(id, SEL, OpenMTEvent*))[_target methodForSelector:_selector])(_target, _selector, event);
((void(*)(id, SEL, int, MTTouch[]))[_target methodForSelector:_selector])(_target, _selector, numTouches, touches);
return;
}
}
Expand Down
1 change: 0 additions & 1 deletion MiddleMe/OpenMultitouchSupport/OpenMTManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import <Foundation/Foundation.h>

#import "OpenMTListener.h"
#import "OpenMTEvent.h"

@interface OpenMTManager: NSObject

Expand Down
24 changes: 4 additions & 20 deletions MiddleMe/OpenMultitouchSupport/OpenMTManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

#import "OpenMTManager.h"
#import "OpenMTListener.h"
#import "OpenMTTouch.h"
#import "OpenMTEvent.h"
#import "OpenMTInternal.h"

@interface OpenMTManager()
Expand Down Expand Up @@ -106,11 +104,7 @@ - (void)makeDevice {
// MTPrintImageRegionDescriptors(self.device); work
}

//- (void)handlePathEvent:(OpenMTTouch *)touch {
// NSLog(@"%@", touch.description);
//}

- (void)handleMultitouchEvent:(OpenMTEvent *)event {
- (void)handleCount:(int)numTouches touches:(MTTouch [])eventTouches {
for (int i = 0; i < (int)self.listeners.count; i++) {
OpenMTListener *listener = self.listeners[i];
if (listener.dead) {
Expand All @@ -121,7 +115,7 @@ - (void)handleMultitouchEvent:(OpenMTEvent *)event {
continue;
}
dispatchResponse(^{
[listener listenToEvent:event];
[listener count:numTouches touches:eventTouches];
});
}
}
Expand Down Expand Up @@ -205,18 +199,8 @@ static void dispatchResponse(dispatch_block_t block) {
dispatch_sync(responseQueue, block);
}

static void contactEventHandler(MTDeviceRef eventDevice, MTTouch eventTouches[], int numTouches, double timestamp, int frame) {
NSMutableArray *touches = [NSMutableArray array];

for (int i = 0; i < numTouches; i++) {
OpenMTTouch *touch = [[OpenMTTouch alloc] initWithMTTouch:&eventTouches[i]];
[touches addObject:touch];
}

OpenMTEvent *event = OpenMTEvent.new;
event.touches = touches;

[OpenMTManager.sharedManager handleMultitouchEvent:event];
static void contactEventHandler(MTDeviceRef eventDevice, MTTouch touches[], int numTouches, double timestamp, int frame) {
[OpenMTManager.sharedManager handleCount:numTouches touches:touches];
}

@end
53 changes: 53 additions & 0 deletions MiddleMe/OpenMultitouchSupport/OpenMTPublic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// OpenMTPublic.h
// MiddleMe
//
// Created by Mikhail Vyrtsev on 11.09.2023.
//

#ifndef OpenMTPublic_h
#define OpenMTPublic_h

typedef struct {
float x;
float y;
} MTPoint;

typedef struct {
MTPoint position;
MTPoint velocity;
} MTVector;

enum {
MTTouchStateNotTracking = 0,
MTTouchStateStartInRange = 1,
MTTouchStateHoverInRange = 2,
MTTouchStateMakeTouch = 3,
MTTouchStateTouching = 4,
MTTouchStateBreakTouch = 5,
MTTouchStateLingerInRange = 6,
MTTouchStateOutOfRange = 7
};

typedef int MTTouchState;

typedef struct {
int frame;
double timestamp;
int identifier;
MTTouchState state;
int fingerId;
int handId;
MTVector normalizedPosition;
float total; //total of capacitance
float pressure;
float angle;
float majorAxis;
float minorAxis;
MTVector absolutePosition;
int field14;
int field15;
float density; //area density of capacitance
} MTTouch;

#endif /* OpenMTPublic_h */
23 changes: 0 additions & 23 deletions MiddleMe/OpenMultitouchSupport/OpenMTTouch.h

This file was deleted.

22 changes: 0 additions & 22 deletions MiddleMe/OpenMultitouchSupport/OpenMTTouch.m

This file was deleted.

3 changes: 1 addition & 2 deletions MiddleMe/OpenMultitouchSupport/OpenMultitouchSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ FOUNDATION_EXPORT const unsigned char OpenMultitouchSupportVersionString[];

#import "OpenMTManager.h"
#import "OpenMTListener.h"
#import "OpenMTEvent.h"
#import "OpenMTTouch.h"
#import "OpenMTPublic.h"

0 comments on commit c92e0fb

Please sign in to comment.