Skip to content

Commit 413d938

Browse files
authored
fix: disable built-in mouse support to avoid click conflicts (#201)
1 parent 2e964c8 commit 413d938

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

PlayTools/Controls/PTFakeTouch/NSObject+Swizzle.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import <VideoSubscriberAccount/VideoSubscriberAccount.h>
1515
#import <AVFoundation/AVFoundation.h>
1616
#import <CoreMotion/CoreMotion.h>
17+
#import <GameController/GameController.h>
1718

1819
__attribute__((visibility("hidden")))
1920
@interface PTSwizzleLoader : NSObject
@@ -62,6 +63,30 @@ - (void) swizzleExchangeMethod:(SEL)origSelector withMethod:(SEL)newSelector
6263
method_exchangeImplementations(originalMethod, swizzledMethod);
6364
}
6465

66+
+ (void) swizzleClassMethod:(SEL)origSelector withMethod:(SEL)newSelector {
67+
Class cls = object_getClass((id)self);
68+
Method originalMethod = class_getClassMethod(cls, origSelector);
69+
Method swizzledMethod = class_getClassMethod(cls, newSelector);
70+
71+
if (class_addMethod(cls,
72+
origSelector,
73+
method_getImplementation(swizzledMethod),
74+
method_getTypeEncoding(swizzledMethod))) {
75+
class_replaceMethod(cls,
76+
newSelector,
77+
method_getImplementation(originalMethod),
78+
method_getTypeEncoding(originalMethod));
79+
} else {
80+
class_replaceMethod(cls,
81+
newSelector,
82+
class_replaceMethod(cls,
83+
origSelector,
84+
method_getImplementation(swizzledMethod),
85+
method_getTypeEncoding(swizzledMethod)),
86+
method_getTypeEncoding(originalMethod));
87+
}
88+
}
89+
6590
- (BOOL) hook_prefersPointerLocked {
6691
return false;
6792
}
@@ -160,6 +185,14 @@ - (instancetype)hook_CMMotionManager_init {
160185
return motionManager;
161186
}
162187

188+
+ (GCMouse *)hook_GCMouse_current {
189+
return nil;
190+
}
191+
192+
+ (NSArray *)hook_GCMouse_mice {
193+
return @[];
194+
}
195+
163196
// Hook for UIUserInterfaceIdiom
164197

165198
// - (long long) hook_userInterfaceIdiom {
@@ -301,6 +334,11 @@ + (void)load {
301334
if ([[PlaySettings shared] limitMotionUpdateFrequency]) {
302335
[objc_getClass("CMMotionManager") swizzleInstanceMethod:@selector(init) withMethod:@selector(hook_CMMotionManager_init)];
303336
}
337+
338+
if (([[PlaySettings shared] disableBuiltinMouse])) {
339+
[objc_getClass("GCMouse") swizzleClassMethod:@selector(current) withMethod:@selector(hook_GCMouse_current)];
340+
[objc_getClass("GCMouse") swizzleClassMethod:@selector(mice) withMethod:@selector(hook_GCMouse_mice)];
341+
}
304342
}
305343

306344
@end

PlayTools/Controls/PlayInput.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import UIKit
3+
import GameController
34

45
// This class is a coordinator (and module entrance), coordinating other concrete classes
56

@@ -19,6 +20,10 @@ class PlayInput {
1920
let displaylink = CADisplayLink(target: self, selector: #selector(drainMainDispatchQueue))
2021
displaylink.add(to: .main, forMode: .common)
2122

23+
if PlaySettings.shared.disableBuiltinMouse {
24+
simulateGCMouseDisconnect()
25+
}
26+
2227
if !PlaySettings.shared.keymapping {
2328
return
2429
}
@@ -41,4 +46,31 @@ class PlayInput {
4146
}
4247
mode.initialize()
4348
}
49+
50+
private func simulateGCMouseDisconnect() {
51+
NotificationCenter.default.addObserver(
52+
forName: .GCMouseDidConnect,
53+
object: nil,
54+
queue: .main
55+
) { nofitication in
56+
guard let mouse = nofitication.object as? GCMouse else {
57+
return
58+
}
59+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
60+
NotificationCenter.default.post(name: .GCMouseDidDisconnect, object: mouse)
61+
mouse.mouseInput?.leftButton.pressedChangedHandler = nil
62+
mouse.mouseInput?.leftButton.valueChangedHandler = nil
63+
mouse.mouseInput?.rightButton?.pressedChangedHandler = nil
64+
mouse.mouseInput?.rightButton?.valueChangedHandler = nil
65+
mouse.mouseInput?.middleButton?.pressedChangedHandler = nil
66+
mouse.mouseInput?.middleButton?.valueChangedHandler = nil
67+
mouse.mouseInput?.auxiliaryButtons?.forEach { button in
68+
button.pressedChangedHandler = nil
69+
button.valueChangedHandler = nil
70+
}
71+
mouse.mouseInput?.scroll.valueChangedHandler = nil
72+
mouse.mouseInput?.mouseMovedHandler = nil
73+
}
74+
}
75+
}
4476
}

PlayTools/PlaySettings.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ let settings = PlaySettings.shared
8787
@objc lazy var checkMicPermissionSync = settingsData.checkMicPermissionSync
8888

8989
@objc lazy var limitMotionUpdateFrequency = settingsData.limitMotionUpdateFrequency
90+
91+
@objc lazy var disableBuiltinMouse = settingsData.disableBuiltinMouse
9092
}
9193

9294
struct AppSettingsData: Codable {
@@ -114,4 +116,5 @@ struct AppSettingsData: Codable {
114116
var hideTitleBar = false
115117
var checkMicPermissionSync = false
116118
var limitMotionUpdateFrequency = false
119+
var disableBuiltinMouse = false
117120
}

0 commit comments

Comments
 (0)