Skip to content

Commit 8e13fa7

Browse files
Switch to macOS Plugin instead of Dynamic (#38)
Switch to macOS Plugin instead of Dynamic (#38) * Add Plugin * AKPluginLoader * AKPluginLoader * Remove majority of Dynamic * Remove Dynamic * Init AKInterface earlier * Fix redundant key press removal * Try to eliminate beep in UIKit * Finish reimplementing remaining dynamic logic * Comment out sound fix for now * Add handler for redundant key press removal * Hopefully? functional monstrosity * Cleanup 1 * Cleanup 2 * Fix Cleanup 2 * Resolve conflicts * Update event handlers * SwiftLint * Fix conflicts * Fix conflicts * Debug check for fullscreen * Revert "Debug check for fullscreen" This reverts commit 3e5d705.
1 parent b313e60 commit 8e13fa7

23 files changed

+436
-1003
lines changed

AKInterface-Bridging-Header.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+

AKPlugin.swift

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// MacPlugin.swift
3+
// AKInterface
4+
//
5+
// Created by Isaac Marovitz on 13/09/2022.
6+
//
7+
8+
import AppKit
9+
import Foundation
10+
11+
class AKPlugin: NSObject, Plugin {
12+
required override init() {
13+
}
14+
15+
var screenCount: Int {
16+
NSScreen.screens.count
17+
}
18+
19+
var mousePoint: CGPoint {
20+
NSApplication.shared.windows.first!.mouseLocationOutsideOfEventStream as CGPoint
21+
}
22+
23+
var windowFrame: CGRect {
24+
NSApplication.shared.windows.first!.frame as CGRect
25+
}
26+
27+
var isMainScreenEqualToFirst: Bool {
28+
return NSScreen.main == NSScreen.screens.first
29+
}
30+
31+
var mainScreenFrame: CGRect {
32+
return NSScreen.main!.frame as CGRect
33+
}
34+
35+
var isFullscreen: Bool {
36+
NSApplication.shared.windows.first!.styleMask.contains(.fullScreen)
37+
}
38+
39+
func hideCursor() {
40+
NSCursor.hide()
41+
}
42+
43+
func unhideCursor() {
44+
NSCursor.unhide()
45+
}
46+
47+
func terminateApplication() {
48+
NSApplication.shared.terminate(self)
49+
}
50+
51+
func eliminateRedundantKeyPressEvents(_ dontIgnore: @escaping() -> Bool) {
52+
NSEvent.addLocalMonitorForEvents(matching: .keyDown, handler: { event in
53+
if dontIgnore() {
54+
return event
55+
}
56+
return nil
57+
})
58+
}
59+
60+
func setupMouseButton(_ _up: Int, _ _down: Int, _ dontIgnore: @escaping(Int, Bool, Bool) -> Bool) {
61+
NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask(rawValue: UInt64(_up)), handler: { event in
62+
let isEventWindow = event.window == NSApplication.shared.windows.first!
63+
if dontIgnore(_up, true, isEventWindow) {
64+
return event
65+
}
66+
return nil
67+
})
68+
NSEvent.addLocalMonitorForEvents(matching: NSEvent.EventTypeMask(rawValue: UInt64(_down)), handler: { event in
69+
if dontIgnore(_up, false, true) {
70+
return event
71+
}
72+
return nil
73+
})
74+
}
75+
76+
func urlForApplicationWithBundleIdentifier(_ value: String) -> URL? {
77+
NSWorkspace.shared.urlForApplication(withBundleIdentifier: value)
78+
}
79+
80+
func setMenuBarVisible(_ visible: Bool) {
81+
NSMenu.setMenuBarVisible(visible)
82+
}
83+
}

PlayTools.xcodeproj/project.pbxproj

+169-33
Large diffs are not rendered by default.

PlayTools.xcodeproj/xcshareddata/xcschemes/PlayTools.xcscheme

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
parallelizeBuildables = "YES"
77
buildImplicitDependencies = "YES">
88
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "6E84A14B28D0F96D00BF7495"
18+
BuildableName = "AKInterface.bundle"
19+
BlueprintName = "AKInterface"
20+
ReferencedContainer = "container:PlayTools.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
923
<BuildActionEntry
1024
buildForTesting = "YES"
1125
buildForRunning = "YES"

PlayTools/Controls/ControlMode.swift

+5-9
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import Foundation
77

88
let mode = ControlMode.mode
99

10-
@objc final public class ControlMode: NSObject {
10+
public class ControlMode {
1111

12-
@objc static public let mode = ControlMode()
13-
@objc public var visible: Bool = PlaySettings.shared.mouseMapping
14-
15-
@objc static public func isMouseClick(_ event: Any) -> Bool {
16-
return [1, 2].contains(Dynamic(event).type.asInt)
17-
}
12+
static public let mode = ControlMode()
13+
public var visible: Bool = PlaySettings.shared.mouseMapping
1814

1915
func show(_ show: Bool) {
2016
if !editor.editorMode {
@@ -24,15 +20,15 @@ let mode = ControlMode.mode
2420
screen.switchDock(true)
2521
}
2622
if PlaySettings.shared.mouseMapping {
27-
Dynamic.NSCursor.unhide()
23+
AKInterface.shared!.unhideCursor()
2824
disableCursor(1)
2925
}
3026
PlayInput.shared.invalidate()
3127
}
3228
} else {
3329
if visible {
3430
if PlaySettings.shared.mouseMapping {
35-
Dynamic.NSCursor.hide()
31+
AKInterface.shared!.hideCursor()
3632
disableCursor(0)
3733
}
3834
if screen.fullscreen {

PlayTools/Controls/PlayAction.swift

+5-8
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ class ButtonAction: Action {
2525
}
2626

2727
let key: GCKeyCode
28-
let keyid: Int
2928
let point: CGPoint
3029
var id: Int
3130

32-
init(id: Int, keyid: Int, key: GCKeyCode, point: CGPoint) {
33-
self.keyid = keyid
31+
init(id: Int, key: GCKeyCode, point: CGPoint) {
3432
self.key = key
3533
self.point = point
3634
self.id = id
3735
if let keyboard = GCKeyboard.coalesced?.keyboardInput {
38-
if !PlayMice.shared.setMiceButtons(keyid, action: self) {
36+
if !PlayMice.shared.setMiceButtons(key.rawValue, action: self) {
3937
let handler = keyboard.button(forKeyCode: key)?.pressedChangedHandler
4038
keyboard.button(forKeyCode: key)?.pressedChangedHandler = { button, value, pressed in
4139
if !mode.visible && !PlayInput.cmdPressed() {
@@ -52,8 +50,7 @@ class ButtonAction: Action {
5250
convenience init(id: Int, data: Button) {
5351
self.init(
5452
id: id,
55-
keyid: data.keyCode,
56-
key: GCKeyCode(rawValue: CFIndex(data.keyCode)),
53+
key: GCKeyCode(rawValue: data.keyCode),
5754
point: CGPoint(
5855
x: data.transform.xCoord.absoluteX,
5956
y: data.transform.yCoord.absoluteY))
@@ -73,9 +70,9 @@ class DraggableButtonAction: ButtonAction {
7370

7471
var releasePoint: CGPoint
7572

76-
override init(id: Int, keyid: Int, key: GCKeyCode, point: CGPoint) {
73+
override init(id: Int, key: GCKeyCode, point: CGPoint) {
7774
self.releasePoint = point
78-
super.init(id: id, keyid: keyid, key: key, point: point)
75+
super.init(id: id, key: key, point: point)
7976
if settings.mouseMapping {
8077
PlayMice.shared.setupMouseMovedHandler()
8178
}

PlayTools/Controls/PlayInput.swift

+11-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import GameController
33
import UIKit
44

5-
final class PlayInput: NSObject {
5+
class PlayInput {
66
static let shared = PlayInput()
77
var actions = [Action]()
88
var timeoutForBind = true
@@ -52,10 +52,10 @@ final class PlayInput: NSObject {
5252
EditorController.shared.setKeyCode(keyCode.rawValue)
5353
}
5454
}
55-
keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))?.pressedChangedHandler = { _, _, pressed in
55+
keyboard.button(forKeyCode: .leftGUI)?.pressedChangedHandler = { _, _, pressed in
5656
PlayInput.lCmdPressed = pressed
5757
}
58-
keyboard.button(forKeyCode: GCKeyCode(rawValue: 231))?.pressedChangedHandler = { _, _, pressed in
58+
keyboard.button(forKeyCode: .rightGUI)?.pressedChangedHandler = { _, _, pressed in
5959
PlayInput.rCmdPressed = pressed
6060
}
6161
keyboard.button(forKeyCode: .leftAlt)?.pressedChangedHandler = { _, _, pressed in
@@ -68,8 +68,6 @@ final class PlayInput: NSObject {
6868
}
6969

7070
static public func cmdPressed() -> Bool {
71-
// return keyboard.button(forKeyCode: GCKeyCode(rawValue: 227))!.isPressed
72-
// || keyboard.button(forKeyCode: GCKeyCode(rawValue: 231))!.isPressed
7371
return lCmdPressed || rCmdPressed
7472
}
7573

@@ -85,8 +83,8 @@ final class PlayInput: NSObject {
8583
}
8684

8785
private static let FORBIDDEN: [GCKeyCode] = [
88-
GCKeyCode.init(rawValue: 227), // LCmd
89-
GCKeyCode.init(rawValue: 231), // RCmd
86+
.leftGUI,
87+
.rightGUI,
9088
.leftAlt,
9189
.rightAlt,
9290
.printScreen
@@ -125,18 +123,13 @@ final class PlayInput: NSObject {
125123
}
126124

127125
setup()
128-
// fix beep sound
129-
eliminateRedundantKeyPressEvents()
126+
127+
// Fix beep sound
128+
AKInterface.shared!
129+
.eliminateRedundantKeyPressEvents({ self.dontIgnore() })
130130
}
131131

132-
private func eliminateRedundantKeyPressEvents() {
133-
// TODO later: should not be hard-coded
134-
let NSEventMaskKeyDown: UInt64 = 1024
135-
Dynamic.NSEvent.addLocalMonitorForEventsMatchingMask( NSEventMaskKeyDown, handler: { event in
136-
if (mode.visible && !EditorController.shared.editorMode) || PlayInput.cmdPressed() {
137-
return event
138-
}
139-
return nil
140-
} as ResponseBlock)
132+
func dontIgnore() -> Bool {
133+
(mode.visible && !EditorController.shared.editorMode) || PlayInput.cmdPressed()
141134
}
142135
}

0 commit comments

Comments
 (0)