Skip to content

Commit cbee8fe

Browse files
committed
Conditionally compile GameController
1 parent 4a7cef3 commit cbee8fe

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

Sources/AndroidInput/GameController.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Alsey Coleman Miller on 2/27/26.
66
//
77

8+
#if AGDK
89
#if os(Android)
910
import Android
1011
import AndroidNDK
@@ -16,13 +17,13 @@ public struct GameController: ~Copyable {
1617

1718
let environment: JNIEnvironment
1819

19-
public init(context: jobject, environment: JNIEnvironment) {
20+
public init(context: jobject, environment: JNIEnvironment) throws {
2021
let result = Paddleboat_init(environment, context)
2122
guard result == 0 else {
22-
throw Error(rawValue: result) ?? .notInitialized
23+
throw GameController.Error(rawValue: result) ?? GameController.Error.notInitialized
2324
}
2425
guard Paddleboat_isInitialized() else {
25-
throw .notInitialized
26+
throw GameController.Error.notInitialized
2627
}
2728
self.environment = environment
2829
}
@@ -31,7 +32,7 @@ public struct GameController: ~Copyable {
3132
Paddleboat_destroy(environment)
3233
}
3334

34-
public static func update() {
35+
public func update() {
3536
Paddleboat_update(environment)
3637
}
3738

@@ -51,27 +52,25 @@ public struct GameController: ~Copyable {
5152

5253
public static func getControllerName(index: Int32, bufferSize: Int = 128) -> (ErrorCode, String) {
5354
var buffer = [CChar](repeating: 0, count: bufferSize)
54-
let err = Paddleboat_getControllerName(index, buffer.count, &buffer)
55+
let err = Paddleboat_getControllerName(index, Int32(buffer.count), &buffer)
5556
let code = ErrorCode(rawValue: err) ?? .noError
56-
let name = String(cString: buffer)
57+
let name = buffer.withUnsafeBufferPointer { String(cString: $0.baseAddress!) }
5758
return (code, name)
5859
}
5960

6061
// MARK: - Lights / Vibration
6162
@discardableResult
62-
public static func setControllerLight(index: Int32, type: LightType, data: UInt32) -> ErrorCode {
63-
guard let env = currentJNIEnv() else { return .notInitialized }
64-
let err = Paddleboat_setControllerLight(index, type.rawValue, data, env)
63+
public func setControllerLight(index: Int32, type: LightType, data: UInt32) -> ErrorCode {
64+
let err = Paddleboat_setControllerLight(index, type.rawValue, data, environment)
6565
return ErrorCode(rawValue: err) ?? .noError
6666
}
67-
67+
6868
@discardableResult
69-
public static func setControllerVibration(index: Int32, vibration: VibrationData) -> ErrorCode {
70-
guard let env = currentJNIEnv() else { return .notInitialized }
69+
public func setControllerVibration(index: Int32, vibration: VibrationData) -> ErrorCode {
7170
var cData = Paddleboat_Vibration_Data(duration_ms: vibration.durationMs,
7271
left_motor_intensity: vibration.intensityLeft,
7372
right_motor_intensity: vibration.intensityRight)
74-
let err = Paddleboat_setControllerVibrationData(index, &cData, env)
73+
let err = Paddleboat_setControllerVibrationData(index, &cData, environment)
7574
return ErrorCode(rawValue: err) ?? .noError
7675
}
7776

@@ -90,6 +89,7 @@ public extension GameController {
9089

9190
// MARK: - Error
9291
public enum Error: Int32, Swift.Error {
92+
case noError = 0
9393
case alreadyInitialized = -2000
9494
case notInitialized = -2001
9595
case invalidParameter = -2002
@@ -102,15 +102,17 @@ public extension GameController {
102102
case noMouse = -2009
103103
case initGCMFailure = -2010
104104
}
105-
106-
public enum Status: Int32 {
105+
106+
typealias ErrorCode = Error
107+
108+
public enum ControllerStatus: Int32 {
107109
case inactive = 0
108110
case active = 1
109111
case justConnected = 2
110112
case justDisconnected = 3
111113
}
112-
113-
public struct Buttons: OptionSet {
114+
115+
public struct Buttons: OptionSet, Sendable {
114116
public let rawValue: UInt32
115117
public init(rawValue: UInt32) { self.rawValue = rawValue }
116118
public static let a = Buttons(rawValue: 1 << 0)
@@ -142,7 +144,7 @@ public extension GameController {
142144
case rgb = 1
143145
}
144146

145-
public struct IntegratedMotionSensorFlags: OptionSet {
147+
public struct IntegratedMotionSensorFlags: OptionSet, Sendable {
146148
public let rawValue: UInt32
147149
public init(rawValue: UInt32) { self.rawValue = rawValue }
148150
public static let none = IntegratedMotionSensorFlags([])
@@ -162,3 +164,4 @@ public extension GameController {
162164
}
163165
}
164166
}
167+
#endif

0 commit comments

Comments
 (0)