objc2_game_controller/generated/
GCController.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5use objc2::__framework_prelude::*;
6use objc2_foundation::*;
7
8use crate::*;
9
10extern "C" {
11    /// Use these constants with NSNotificationCenter to listen to connection and disconnection events.
12    ///
13    /// Use GCControllerDidConnectNotification for observing connections of controllers.
14    /// Use GCControllerDidDisconnectNotification for observing disconnections of controllers.
15    ///
16    /// Connections and disconnections of controllers will also be reflected in the controllers array
17    /// of the GCController class.
18    ///
19    /// The 'object' property of the notification will contain the GCController that was connected or disconnected.
20    /// For example:
21    ///
22    /// - (void)controllerDidConnect:(NSNotification *)note {
23    ///
24    /// GCController *controller = note.object;
25    ///
26    /// ....
27    /// }
28    ///
29    ///
30    /// See: NSNotificationCenter
31    ///
32    /// See: GCController.controllers
33    ///
34    /// See also [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerdidconnectnotification?language=objc)
35    pub static GCControllerDidConnectNotification: &'static NSString;
36}
37
38extern "C" {
39    /// [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerdiddisconnectnotification?language=objc)
40    pub static GCControllerDidDisconnectNotification: &'static NSString;
41}
42
43extern "C" {
44    /// Use these constants with NSNotificationCenter to listen to a controller becoming the most recently used controller.
45    /// This is a good time to swap out UI to match the new current controller, and unregister any handlers with
46    /// the old current controller.
47    ///
48    /// The 'object' property of the notification will contain the GCController that became the current controller.
49    /// For example:
50    ///
51    /// - (void)controllerDidBecomeCurrent:(NSNotification *)note {
52    ///
53    /// GCController *controller = note.object;
54    ///
55    /// ...
56    /// }
57    ///
58    /// See: NSNotificationCenter
59    ///
60    /// See: GCController.controllers
61    ///
62    /// See also [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerdidbecomecurrentnotification?language=objc)
63    pub static GCControllerDidBecomeCurrentNotification: &'static NSString;
64}
65
66extern "C" {
67    /// [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerdidstopbeingcurrentnotification?language=objc)
68    pub static GCControllerDidStopBeingCurrentNotification: &'static NSString;
69}
70
71extern "C" {
72    /// Use this constant with NSNotificationCenter to listen to controller user customization events.
73    ///
74    /// When a user customizes the button mappings or other settings of a controller this notification will be
75    /// posted. This is a good time to swap out UI to match the new user settings. Users can modify game
76    /// controller settings through the Settings app on iOS, tvOS, and macOS.
77    ///
78    /// The 'object' property of the notification will contain the GCController that was customized.
79    /// For example:
80    ///
81    /// - (void)controllerDidConnect:(NSNotification *)note {
82    ///
83    /// GCController *controller = note.object;
84    ///
85    /// ....
86    /// }
87    ///
88    ///
89    /// See: NSNotificationCenter
90    ///
91    /// See: GCController.controllers
92    ///
93    /// See also [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerusercustomizationsdidchangenotification?language=objc)
94    pub static GCControllerUserCustomizationsDidChangeNotification: &'static NSString;
95}
96
97/// This is the player index that a connected controller will have if it has never been assigned a player index on the current system.
98/// Controllers retain the player index they have been assigned between game sessions, so if you wish to unset the player index of a
99/// controller set it back to this value.
100///
101/// See also [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontrollerplayerindex?language=objc)
102// NS_ENUM
103#[repr(transparent)]
104#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
105pub struct GCControllerPlayerIndex(pub NSInteger);
106impl GCControllerPlayerIndex {
107    #[doc(alias = "GCControllerPlayerIndexUnset")]
108    pub const IndexUnset: Self = Self(-1);
109    #[doc(alias = "GCControllerPlayerIndex1")]
110    pub const Index1: Self = Self(0);
111    #[doc(alias = "GCControllerPlayerIndex2")]
112    pub const Index2: Self = Self(1);
113    #[doc(alias = "GCControllerPlayerIndex3")]
114    pub const Index3: Self = Self(2);
115    #[doc(alias = "GCControllerPlayerIndex4")]
116    pub const Index4: Self = Self(3);
117}
118
119unsafe impl Encode for GCControllerPlayerIndex {
120    const ENCODING: Encoding = NSInteger::ENCODING;
121}
122
123unsafe impl RefEncode for GCControllerPlayerIndex {
124    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
125}
126
127extern_class!(
128    /// Controllers are available to an application that links to GameController.framework. There are 2 ways to access controllers
129    /// paired to the system, adopt both to ensure the best user experience:
130    ///
131    /// 1: Querying for the the current array or controllers using [GCController controllers].
132    /// 2: Registering for Connection/Disconnection notifications from NSNotificationCenter.
133    ///
134    /// Only controllers that support one of the allowed profiles, such as GCExtendedGamepad, will be enumerated. Check for the profile
135    /// supported before using a controller in your application. Ignore a controller that doesn't support a profile that suits
136    /// your application, as the user will expect their controller to either be fully supported or not supported at all.
137    ///
138    /// See also [Apple's documentation](https://developer.apple.com/documentation/gamecontroller/gccontroller?language=objc)
139    #[unsafe(super(NSObject))]
140    #[derive(Debug, PartialEq, Eq, Hash)]
141    pub struct GCController;
142);
143
144#[cfg(feature = "GCDevice")]
145extern_conformance!(
146    unsafe impl GCDevice for GCController {}
147);
148
149extern_conformance!(
150    unsafe impl NSObjectProtocol for GCController {}
151);
152
153impl GCController {
154    extern_methods!(
155        /// Get a list of controllers currently attached to the system.
156        ///
157        ///
158        /// See: GCControllerDidConnectNotification
159        ///
160        /// See: GCControllerDidDisconnectNotification
161        #[unsafe(method(controllers))]
162        #[unsafe(method_family = none)]
163        pub unsafe fn controllers() -> Retained<NSArray<GCController>>;
164
165        /// The most recently used game controller. If a user actuates a game controller
166        /// input, that controller will become the current one.
167        ///
168        ///
169        /// Note: This is useful for single player games where you only care about whether an
170        /// input is pressed, and not where it came from.  You will still need to
171        /// register for changes to GCController.current so that your UI can remain
172        /// up-to-date with the current controller.
173        #[unsafe(method(current))]
174        #[unsafe(method_family = none)]
175        pub unsafe fn current() -> Option<Retained<GCController>>;
176
177        #[cfg(feature = "block2")]
178        /// Set this block to be notified when a user intends to suspend or resume the current game state. A controller will have a button
179        /// dedicated to suspending and resuming play and invoking context sensitive actions. During event handling the system will
180        /// notify the application using this block such that the application can handle the suspension and resumption from the given controller.
181        ///
182        /// Use this to implement your canonical transition to a pause menu for example if that is your application's desired handling
183        /// of suspension in play. You may pause and resume based on game state as well so the event is only called each time the
184        /// pause/resume button is pressed.
185        ///
186        ///
187        /// Note: This handler has been deprecated in favor of the Menu button found on GCMicroGamepad and GCExtendedGamepad.
188        ///
189        /// See: microGamepad
190        ///
191        /// See: extendedGamepad
192        ///
193        /// # Safety
194        ///
195        /// The returned block's argument must be a valid pointer.
196        #[deprecated = "Use the Menu button found on the controller's input profile, if it exists."]
197        #[unsafe(method(controllerPausedHandler))]
198        #[unsafe(method_family = none)]
199        pub unsafe fn controllerPausedHandler(
200            &self,
201        ) -> *mut block2::DynBlock<dyn Fn(NonNull<GCController>)>;
202
203        #[cfg(feature = "block2")]
204        /// Setter for [`controllerPausedHandler`][Self::controllerPausedHandler].
205        ///
206        /// This is [copied][objc2_foundation::NSCopying::copy] when set.
207        #[deprecated = "Use the Menu button found on the controller's input profile, if it exists."]
208        #[unsafe(method(setControllerPausedHandler:))]
209        #[unsafe(method_family = none)]
210        pub unsafe fn setControllerPausedHandler(
211            &self,
212            controller_paused_handler: Option<&block2::DynBlock<dyn Fn(NonNull<GCController>)>>,
213        );
214
215        /// Whether the current application should monitor and respond to game controller events when it is not the frontmost application.
216        ///
217        ///
218        /// not be forwarded to the application. Once the application becomes the frontmost application, game controller events will be forwarded.
219        ///
220        ///
221        /// Note: Starting with macOS Big Sur 11.3, shouldMonitorBackgroundEvents will be NO by default. For applications built prior to macOS Big Sur 11.3,
222        /// (or running on devices with an earlier version of macOS), shouldMonitorBackgroundEvents will be YES by default. On iOS and tvOS, this property is ignored.
223        #[unsafe(method(shouldMonitorBackgroundEvents))]
224        #[unsafe(method_family = none)]
225        pub unsafe fn shouldMonitorBackgroundEvents() -> bool;
226
227        /// Setter for [`shouldMonitorBackgroundEvents`][Self::shouldMonitorBackgroundEvents].
228        #[unsafe(method(setShouldMonitorBackgroundEvents:))]
229        #[unsafe(method_family = none)]
230        pub unsafe fn setShouldMonitorBackgroundEvents(should_monitor_background_events: bool);
231
232        /// A controller may be form fitting or otherwise closely attached to the device. This closeness to other inputs on the device
233        /// may suggest that interaction with the device may use other inputs easily. This is presented to developers to allow them to
234        /// make informed decisions about UI and interactions to choose for their game in this situation.
235        #[unsafe(method(isAttachedToDevice))]
236        #[unsafe(method_family = none)]
237        pub unsafe fn isAttachedToDevice(&self) -> bool;
238
239        /// A player index for the controller, defaults to GCControllerPlayerIndexUnset.
240        ///
241        /// This can be set both for the application to keep track of controllers and as a signal to make a controller display a player
242        /// index on a set of LEDs or some other mechanism.
243        ///
244        /// A controller is not guaranteed to have a visual display of the playerIndex, playerIndex does not persist for a controller
245        /// with regards to a system.
246        ///
247        /// Negative values less than GCControllerPlayerIndexUnset will just map back to GCControllerPlayerIndexUnset when read back.
248        #[unsafe(method(playerIndex))]
249        #[unsafe(method_family = none)]
250        pub unsafe fn playerIndex(&self) -> GCControllerPlayerIndex;
251
252        /// Setter for [`playerIndex`][Self::playerIndex].
253        #[unsafe(method(setPlayerIndex:))]
254        #[unsafe(method_family = none)]
255        pub unsafe fn setPlayerIndex(&self, player_index: GCControllerPlayerIndex);
256
257        #[cfg(feature = "GCControllerInput")]
258        /// Gets the input profile for the controller.
259        #[unsafe(method(input))]
260        #[unsafe(method_family = none)]
261        pub unsafe fn input(&self) -> Retained<GCControllerLiveInput>;
262
263        #[cfg(feature = "GCDeviceBattery")]
264        /// Gets the battery information if controller supports one
265        ///
266        /// This property is useful when you try to notify your user to change or charge controller before it runs out of battery life
267        /// or simply display the current battery level and status.
268        #[unsafe(method(battery))]
269        #[unsafe(method_family = none)]
270        pub unsafe fn battery(&self) -> Option<Retained<GCDeviceBattery>>;
271
272        #[cfg(feature = "GCPhysicalInputProfile")]
273        /// Gets the physical input profile for the controller.
274        ///
275        ///
276        /// Note: This is equivalent to the controller's microGamepad, or extendedGamepad instance.
277        ///
278        /// See: microGamepad
279        ///
280        /// See: extendedGamepad
281        #[unsafe(method(physicalInputProfile))]
282        #[unsafe(method_family = none)]
283        pub unsafe fn physicalInputProfile(&self) -> Retained<GCPhysicalInputProfile>;
284
285        #[cfg(all(feature = "GCGamepad", feature = "GCPhysicalInputProfile"))]
286        /// Gets the profile for the controller that suits current application.
287        ///
288        /// There are several supported profiles, with an additional optional profile for motion as well.
289        /// Each controller may be able to map its inputs into all profiles or just one kind of profile. Query for the controller
290        /// profile that suits your game, the simplest kind will be supported by the broadest variety
291        /// of controllers. A controller supporting the Extended Gamepad profile for example supports the Gamepad profile and more.
292        /// As such it can always be used just in the Gamepad profile if that suits the game.
293        ///
294        /// A physical controller that supports a profile must support it completely. That means that all buttons and axis inputs must
295        /// be valid inputs that a developer can utilize.
296        ///
297        /// If a controller does not support the given profile the returned value will be nil. Use this to filter controllers if the
298        /// application requires a specific kind of profile.
299        ///
300        /// See: motion
301        #[deprecated]
302        #[unsafe(method(gamepad))]
303        #[unsafe(method_family = none)]
304        pub unsafe fn gamepad(&self) -> Option<Retained<GCGamepad>>;
305
306        #[cfg(all(feature = "GCMicroGamepad", feature = "GCPhysicalInputProfile"))]
307        #[unsafe(method(microGamepad))]
308        #[unsafe(method_family = none)]
309        pub unsafe fn microGamepad(&self) -> Option<Retained<GCMicroGamepad>>;
310
311        #[cfg(all(feature = "GCExtendedGamepad", feature = "GCPhysicalInputProfile"))]
312        #[unsafe(method(extendedGamepad))]
313        #[unsafe(method_family = none)]
314        pub unsafe fn extendedGamepad(&self) -> Option<Retained<GCExtendedGamepad>>;
315
316        #[cfg(feature = "GCMotion")]
317        /// Gets the motion input profile. This profile is optional and may be available if the controller is attached to a device that supports motion.
318        /// If this is nil the controller does not support motion input and only the gamepad
319        /// &
320        /// extendedGamepad profiles are available.
321        ///
322        /// See: gamepad
323        ///
324        /// See: extendedGamepad
325        #[unsafe(method(motion))]
326        #[unsafe(method_family = none)]
327        pub unsafe fn motion(&self) -> Option<Retained<GCMotion>>;
328
329        #[cfg(feature = "GCDeviceLight")]
330        /// Gets the light for the controller, if one exists.
331        ///
332        /// A controller's light can be used to signal information to the player, such as using different light colors based on the player
333        /// index. It can also be used to react to in-game events and enhance user immersion.
334        #[unsafe(method(light))]
335        #[unsafe(method_family = none)]
336        pub unsafe fn light(&self) -> Option<Retained<GCDeviceLight>>;
337
338        #[cfg(feature = "GCDeviceHaptics")]
339        /// Gets the haptics for the controller, if one exists.
340        ///
341        /// Use this property to create CHHapticEngine instances according to your needs.
342        ///
343        ///
344        /// Note: Haptics are a drain on the controller's battery, and can be distracting when used excessively.
345        #[unsafe(method(haptics))]
346        #[unsafe(method_family = none)]
347        pub unsafe fn haptics(&self) -> Option<Retained<GCDeviceHaptics>>;
348    );
349}
350
351/// Methods declared on superclass `NSObject`.
352impl GCController {
353    extern_methods!(
354        #[unsafe(method(init))]
355        #[unsafe(method_family = init)]
356        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
357
358        #[unsafe(method(new))]
359        #[unsafe(method_family = new)]
360        pub unsafe fn new() -> Retained<Self>;
361    );
362}
363
364/// Snapshot.
365impl GCController {
366    extern_methods!(
367        /// A controller may represent a real device managed by the operating system,
368        /// or a virtual snapshot created by the developer.  If a controller is created
369        /// by the developer, it is considered to be a snapshot, allowing direct writes
370        /// to any GCControllerElement of its profiles.  If the controller is not
371        /// snapshot, the system will reject any write requests to GCControllerElement.
372        ///
373        ///
374        /// See: controllerWithMicroGamepad
375        ///
376        /// See: controllerWithExtendedGamepad
377        ///
378        /// See: capture
379        #[unsafe(method(isSnapshot))]
380        #[unsafe(method_family = none)]
381        pub unsafe fn isSnapshot(&self) -> bool;
382
383        /// Polls the state vector of the controller and saves it to a new and writable
384        /// instance of GCController.
385        ///
386        /// If your application is heavily multithreaded this may also be useful to
387        /// guarantee atomicity of input handling as a snapshot will not change based
388        /// on user input once it is taken.
389        ///
390        ///
391        /// See: snapshot
392        ///
393        /// Returns: A new controller with the duplicated state vector of the current
394        /// controller.
395        #[unsafe(method(capture))]
396        #[unsafe(method_family = none)]
397        pub unsafe fn capture(&self) -> Retained<GCController>;
398
399        /// Creates a controller with a micro gamepad profile.
400        ///
401        /// This controller will be considered a snapshot, allowing developers to write
402        /// to any GCControllerElement of its profiles.
403        ///
404        ///
405        /// See: snapshot
406        ///
407        /// Returns: A new controller with a micro gamepad profile
408        #[unsafe(method(controllerWithMicroGamepad))]
409        #[unsafe(method_family = none)]
410        pub unsafe fn controllerWithMicroGamepad() -> Retained<GCController>;
411
412        /// Creates a controller with an extended gamepad profile.
413        ///
414        /// This controller will be considered a snapshot, allowing developers to write to any GCControllerElement of its profiles.
415        ///
416        ///
417        /// See: snapshot
418        ///
419        /// Returns: A new controller with an extended gamepad profile
420        #[unsafe(method(controllerWithExtendedGamepad))]
421        #[unsafe(method_family = none)]
422        pub unsafe fn controllerWithExtendedGamepad() -> Retained<GCController>;
423    );
424}
425
426/// Discovery.
427impl GCController {
428    extern_methods!(
429        #[cfg(feature = "block2")]
430        /// Start discovery of new wireless controllers that are discoverable. This is an asynchronous and the supplied completionHandler
431        /// will get called once no more devices can be found. If there are already multiple controllers available for use, there
432        /// may be little reason to automatically start discovery of new wireless controllers. In this situation it may be best to
433        /// allow the user to start discovery manually via in-game UI.
434        ///
435        /// Once discovery has started new controllers will notify themselves as connected via GCControllerDidConnectNotification.
436        /// As the notification arrives the controller is also available in the controllers array.
437        ///
438        /// The completionHandler could be used to update UI and/or game state to indicate that no more controllers will be found
439        /// and the current set of controllers is what is available for use in the game.
440        ///
441        /// If a completionHandler was provided, it will be called once when discovery stops. Either from an explicit call to
442        /// stopWirelessControllerDiscovery or from timing out or stopping in its natural course of operation. Thus the
443        /// completionHandler will at most be called once per call to startWirelessControllerDiscoveryWithCompletionHandler:.
444        ///
445        /// The completionHandler may also not get called at all, if for example startWirelessControllerDiscoveryWithCompletionHandler:
446        /// is called multiple times during dicovery. For this case the net effect is that the completionHandler is replaced with each call
447        /// and only the last one set before discovery stops will be called.
448        ///
449        ///
450        /// Parameter `completionHandler`: an optional handler that is called when discovery stops. (may be nil, in which case you will not be notified when discovery stops)
451        ///
452        /// See: stopWirelessControllerDiscovery
453        ///
454        /// See: controllers
455        #[unsafe(method(startWirelessControllerDiscoveryWithCompletionHandler:))]
456        #[unsafe(method_family = none)]
457        pub unsafe fn startWirelessControllerDiscoveryWithCompletionHandler(
458            completion_handler: Option<&block2::DynBlock<dyn Fn()>>,
459        );
460
461        /// If no more controllers are needed, depending on game state or number of controllers supported by a game, the discovery
462        /// process can be stopped. Calling stopWirelessControllerDiscovery when no discovery is currently in progress will return
463        /// immediately without any effect, thus it is safe to call even if the completionHandler of
464        /// startWirelessControllerDiscoveryWithCompletionHandler: has been called.
465        ///
466        ///
467        /// See: startWirelessControllerDiscoveryWithCompletionHandler:
468        #[unsafe(method(stopWirelessControllerDiscovery))]
469        #[unsafe(method_family = none)]
470        pub unsafe fn stopWirelessControllerDiscovery();
471    );
472}