objc2_core_haptics/generated/CHHapticEngine.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::*;
6#[cfg(feature = "objc2-avf-audio")]
7use objc2_avf_audio::*;
8use objc2_foundation::*;
9
10use crate::*;
11
12/// [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhaptictimeimmediate?language=objc)
13pub const CHHapticTimeImmediate: NSTimeInterval = 0.0;
14/// A block which is called asynchronously when a call to start or stop the haptic engine completes.
15///
16/// Parameter `error`: If the call fails, this is set to a valid NSError describing the error.
17///
18/// All callbacks are delivered on an internal queue which guarantees proper delivery order and allows reentrant calls to the API.
19///
20/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticcompletionhandler?language=objc)
21#[cfg(feature = "block2")]
22pub type CHHapticCompletionHandler = *mut block2::DynBlock<dyn Fn(*mut NSError)>;
23
24/// Constants indicating what the engine should do in response to the finished handler being called.
25///
26///
27/// Stop the engine. This is useful if the client knows that the client is about to go inactive.
28///
29///
30/// Do not stop the engine. This is useful if the client expects more activity.
31///
32/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticenginefinishedaction?language=objc)
33// NS_ENUM
34#[repr(transparent)]
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
36pub struct CHHapticEngineFinishedAction(pub NSInteger);
37impl CHHapticEngineFinishedAction {
38 #[doc(alias = "CHHapticEngineFinishedActionStopEngine")]
39 pub const StopEngine: Self = Self(1);
40 #[doc(alias = "CHHapticEngineFinishedActionLeaveEngineRunning")]
41 pub const LeaveEngineRunning: Self = Self(2);
42}
43
44unsafe impl Encode for CHHapticEngineFinishedAction {
45 const ENCODING: Encoding = NSInteger::ENCODING;
46}
47
48unsafe impl RefEncode for CHHapticEngineFinishedAction {
49 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
50}
51
52/// A block which is called asynchronously when the engine detects that all active pattern players have finished. The
53/// return value tells the system what action to take as a result of this (see `CHHapticEngineFinishedAction`).
54///
55/// Parameter `error`: If the engine detects the players have stopped due to an error, this is set to a valid NSError describing the error.
56///
57/// All callbacks are delivered on an internal queue which guarantees proper delivery order and allows reentrant calls to the API.
58///
59/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticenginefinishedhandler?language=objc)
60#[cfg(feature = "block2")]
61pub type CHHapticEngineFinishedHandler =
62 *mut block2::DynBlock<dyn Fn(*mut NSError) -> CHHapticEngineFinishedAction>;
63
64/// Constants indicating the reason why the CHHapticEngine has stopped.
65///
66///
67/// The AVAudioSession bound to this engine has been interrupted.
68///
69///
70/// The application owning this engine has been suspended (i.e., put into the background).
71///
72///
73/// The engine has stopped due to an idle timeout when the engine's `autoShutdownEnabled` property was set to YES.
74///
75///
76/// The engine has stopped due to a call to a `CHHapticEngineFinishedHandler` returning `CHHapticEngineFinishedActionStopEngine`.
77///
78///
79/// The engine has stopped because the CHHapticEngine instance was destroyed.
80///
81///
82/// The engine has stopped because the Game Controller associated with this engine disconnected.
83///
84///
85/// An error has occurred.
86///
87/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticenginestoppedreason?language=objc)
88// NS_ENUM
89#[repr(transparent)]
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
91pub struct CHHapticEngineStoppedReason(pub NSInteger);
92impl CHHapticEngineStoppedReason {
93 #[doc(alias = "CHHapticEngineStoppedReasonAudioSessionInterrupt")]
94 pub const AudioSessionInterrupt: Self = Self(1);
95 #[doc(alias = "CHHapticEngineStoppedReasonApplicationSuspended")]
96 pub const ApplicationSuspended: Self = Self(2);
97 #[doc(alias = "CHHapticEngineStoppedReasonIdleTimeout")]
98 pub const IdleTimeout: Self = Self(3);
99 #[doc(alias = "CHHapticEngineStoppedReasonNotifyWhenFinished")]
100 pub const NotifyWhenFinished: Self = Self(4);
101 #[doc(alias = "CHHapticEngineStoppedReasonEngineDestroyed")]
102 pub const EngineDestroyed: Self = Self(5);
103 #[doc(alias = "CHHapticEngineStoppedReasonGameControllerDisconnect")]
104 pub const GameControllerDisconnect: Self = Self(6);
105 #[doc(alias = "CHHapticEngineStoppedReasonSystemError")]
106 pub const SystemError: Self = Self(-1);
107}
108
109unsafe impl Encode for CHHapticEngineStoppedReason {
110 const ENCODING: Encoding = NSInteger::ENCODING;
111}
112
113unsafe impl RefEncode for CHHapticEngineStoppedReason {
114 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
115}
116
117/// A block which is called asynchronously when the engine has stopped due to external causes such as
118/// an audio session interruption or autoShutdown.
119///
120/// Parameter `stoppedReason`: This constant indicates the reason why the engine stopped.
121///
122/// This handler is NOT called if the client directly calls stopWithCompletionHandler:.
123/// All callbacks are delivered on an internal queue which guarantees proper delivery order and allows reentrant calls to the API.
124///
125/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticenginestoppedhandler?language=objc)
126#[cfg(feature = "block2")]
127pub type CHHapticEngineStoppedHandler = *mut block2::DynBlock<dyn Fn(CHHapticEngineStoppedReason)>;
128
129/// A block which is called asynchronously if the haptic engine has reset itself due a server failure.
130///
131/// In response to this handler, the app must reload all custom audio resources and recreate all necessary
132/// pattern players. The engine must of course be restarted. CHHapticPatterns do not need to be re-created.
133/// All callbacks are delivered on an internal queue which guarantees proper delivery order and allows reentrant calls to the API.
134///
135/// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticengineresethandler?language=objc)
136#[cfg(feature = "block2")]
137pub type CHHapticEngineResetHandler = *mut block2::DynBlock<dyn Fn()>;
138
139/// [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticaudioresourcekey?language=objc)
140pub type CHHapticAudioResourceKey = NSString;
141
142extern "C" {
143 /// Keys used to configure the playback behavior of a custom waveform.
144 ///
145 /// Indicates whether the audio file playback should be ramped in and out with an envelope. This can be useful for preventing clicks during playback,
146 /// or for cases where the application wants to modulate this envelope to use different attack and release times.
147 /// Value type: boolean. Default is
148 /// .
149 ///
150 /// Indicates whether the audio file will be looped when played back. The default loop range is the entire file.
151 /// Value type: boolean. Default is
152 /// .
153 ///
154 /// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticaudioresourcekeyusevolumeenvelope?language=objc)
155 pub static CHHapticAudioResourceKeyUseVolumeEnvelope: &'static CHHapticAudioResourceKey;
156}
157
158extern "C" {
159 /// [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticaudioresourcekeyloopenabled?language=objc)
160 pub static CHHapticAudioResourceKeyLoopEnabled: &'static CHHapticAudioResourceKey;
161}
162
163extern_class!(
164 /// Represents the connection with the haptic server.
165 ///
166 /// See also [Apple's documentation](https://developer.apple.com/documentation/corehaptics/chhapticengine?language=objc)
167 #[unsafe(super(NSObject))]
168 #[derive(Debug, PartialEq, Eq, Hash)]
169 pub struct CHHapticEngine;
170);
171
172extern_conformance!(
173 unsafe impl NSObjectProtocol for CHHapticEngine {}
174);
175
176impl CHHapticEngine {
177 extern_methods!(
178 #[cfg(feature = "CHHapticDeviceCapability")]
179 /// Get the protocol that describes haptic and audio capabilities on this device.
180 ///
181 /// Detailed description on the capability protocol is in CHHapticDeviceCapability.h.
182 #[unsafe(method(capabilitiesForHardware))]
183 #[unsafe(method_family = none)]
184 pub unsafe fn capabilitiesForHardware(
185 ) -> Retained<ProtocolObject<dyn CHHapticDeviceCapability>>;
186
187 /// The absolute time from which all current and future event times may be calculated.
188 /// The units are seconds.
189 #[unsafe(method(currentTime))]
190 #[unsafe(method_family = none)]
191 pub unsafe fn currentTime(&self) -> NSTimeInterval;
192
193 #[cfg(feature = "block2")]
194 /// The engine will call this block when it has stopped due to external causes (such as
195 /// an audio session interruption or the app going into the background). It will NOT be called
196 /// if the client calls stopWithCompletionHandler:.
197 ///
198 /// In general, callbacks arrive on a non-main thread and it is the client's responsibility to handle
199 /// it in a thread-safe manner.
200 #[unsafe(method(stoppedHandler))]
201 #[unsafe(method_family = none)]
202 pub unsafe fn stoppedHandler(&self) -> CHHapticEngineStoppedHandler;
203
204 #[cfg(feature = "block2")]
205 /// Setter for [`stoppedHandler`][Self::stoppedHandler].
206 #[unsafe(method(setStoppedHandler:))]
207 #[unsafe(method_family = none)]
208 pub unsafe fn setStoppedHandler(&self, stopped_handler: CHHapticEngineStoppedHandler);
209
210 #[cfg(feature = "block2")]
211 /// This block will called asynchronously if the haptic engine has to reset itself after a server failure.
212 ///
213 /// In response to this handler being called, the client must release all haptic pattern players
214 /// and recreate them. All CHHapticPattern objects and CHHapticEngine properties will have been preserved.
215 /// In general, callbacks arrive on a non-main thread and it is the client's responsibility to handle
216 /// it in a thread-safe manner.
217 #[unsafe(method(resetHandler))]
218 #[unsafe(method_family = none)]
219 pub unsafe fn resetHandler(&self) -> CHHapticEngineResetHandler;
220
221 #[cfg(feature = "block2")]
222 /// Setter for [`resetHandler`][Self::resetHandler].
223 #[unsafe(method(setResetHandler:))]
224 #[unsafe(method_family = none)]
225 pub unsafe fn setResetHandler(&self, reset_handler: CHHapticEngineResetHandler);
226
227 /// If set to YES, the CHHapticEngine will ignore all events of type CHHapticEventTypeAudio and play only haptic events.
228 ///
229 /// This behavior change will only take effect after the engine is stopped and restarted.
230 /// The default is NO.
231 #[unsafe(method(playsHapticsOnly))]
232 #[unsafe(method_family = none)]
233 pub unsafe fn playsHapticsOnly(&self) -> bool;
234
235 /// Setter for [`playsHapticsOnly`][Self::playsHapticsOnly].
236 #[unsafe(method(setPlaysHapticsOnly:))]
237 #[unsafe(method_family = none)]
238 pub unsafe fn setPlaysHapticsOnly(&self, plays_haptics_only: bool);
239
240 /// If set to YES, the CHHapticEngine will ignore all events of type CHHapticEventTypeHaptic and play only audio events.
241 ///
242 /// This behavior change will only take effect after the engine is stopped and restarted.
243 /// The default is NO.
244 #[unsafe(method(playsAudioOnly))]
245 #[unsafe(method_family = none)]
246 pub unsafe fn playsAudioOnly(&self) -> bool;
247
248 /// Setter for [`playsAudioOnly`][Self::playsAudioOnly].
249 #[unsafe(method(setPlaysAudioOnly:))]
250 #[unsafe(method_family = none)]
251 pub unsafe fn setPlaysAudioOnly(&self, plays_audio_only: bool);
252
253 /// When set to YES, the CHHapticEngine mutes audio playback from its players.
254 ///
255 /// Default is NO.
256 #[unsafe(method(isMutedForAudio))]
257 #[unsafe(method_family = none)]
258 pub unsafe fn isMutedForAudio(&self) -> bool;
259
260 /// Setter for [`isMutedForAudio`][Self::isMutedForAudio].
261 #[unsafe(method(setIsMutedForAudio:))]
262 #[unsafe(method_family = none)]
263 pub unsafe fn setIsMutedForAudio(&self, is_muted_for_audio: bool);
264
265 /// When set to YES, the CHHapticEngine mutes haptic playback from its players.
266 ///
267 /// Default is NO.
268 #[unsafe(method(isMutedForHaptics))]
269 #[unsafe(method_family = none)]
270 pub unsafe fn isMutedForHaptics(&self) -> bool;
271
272 /// Setter for [`isMutedForHaptics`][Self::isMutedForHaptics].
273 #[unsafe(method(setIsMutedForHaptics:))]
274 #[unsafe(method_family = none)]
275 pub unsafe fn setIsMutedForHaptics(&self, is_muted_for_haptics: bool);
276
277 /// When auto shutdown is enabled, the haptic engine can start and stop the hardware dynamically,
278 /// to conserve power.
279 ///
280 /// To conserve power, it is advised that the client stop the haptic engine when not in use.
281 /// But when auto shutdown is enabled, the haptic engine will stop the hardware if it was running
282 /// idle for a certain duration, and restart it later when required.
283 /// Note that, because this operation is dynamic, it may affect the start times of the pattern players
284 /// (e.g. `CHHapticPatternplayer`), if the engine has to resume from its shutdown state.
285 ///
286 /// This feature is disabled by default, but the client can enable it if needed.
287 #[unsafe(method(isAutoShutdownEnabled))]
288 #[unsafe(method_family = none)]
289 pub unsafe fn isAutoShutdownEnabled(&self) -> bool;
290
291 /// Setter for [`isAutoShutdownEnabled`][Self::isAutoShutdownEnabled].
292 #[unsafe(method(setAutoShutdownEnabled:))]
293 #[unsafe(method_family = none)]
294 pub unsafe fn setAutoShutdownEnabled(&self, auto_shutdown_enabled: bool);
295
296 #[unsafe(method(init))]
297 #[unsafe(method_family = init)]
298 pub unsafe fn init(this: Allocated<Self>) -> Option<Retained<Self>>;
299
300 /// Create an instance of the CHHapticEngine.
301 ///
302 /// More than one instance may exist within a process. Each will function independently of the others.
303 /// CHHapticEngines created using this method will be associated with the device's internal haptics hardware system,
304 /// if one exists. For systems without internal haptics, this method will fail with the error `CHHapticErrorCodeNotSupported`.
305 /// To access engine instances associated with external game controllers, see the GameController framework documentation
306 /// for the `hapticEngines` property on the GCController class.
307 #[unsafe(method(initAndReturnError:_))]
308 #[unsafe(method_family = init)]
309 pub unsafe fn initAndReturnError(
310 this: Allocated<Self>,
311 ) -> Result<Retained<Self>, Retained<NSError>>;
312
313 #[cfg(feature = "objc2-avf-audio")]
314 /// Create an instance of an CHHapticEngine and associate it with an audio session. If 'audioSession' is nil,
315 /// the engine will create its own.
316 ///
317 /// More than one instance may exist within a process. Each will function independently of the others, but all
318 /// CHHapticEngines which share an audio session will have identical audio behavior with regard to interruptions, etc.
319 /// CHHapticEngines created using this method will be associated with the device's internal haptics hardware system,
320 /// if one exists. For systems without internal haptics, this method will fail with the error `CHHapticErrorCodeNotSupported`.
321 /// To access engine instances associated with external game controllers, see the GameController framework documentation
322 /// for the `hapticEngines` property on the GCController class.
323 #[unsafe(method(initWithAudioSession:error:_))]
324 #[unsafe(method_family = init)]
325 pub unsafe fn initWithAudioSession_error(
326 this: Allocated<Self>,
327 audio_session: Option<&AVAudioSession>,
328 ) -> Result<Retained<Self>, Retained<NSError>>;
329
330 #[cfg(feature = "block2")]
331 /// Asynchronously start the engine. The handler will be called when the operation completes.
332 ///
333 /// The handler is guaranteed to be called on either success or failure.
334 #[unsafe(method(startWithCompletionHandler:))]
335 #[unsafe(method_family = none)]
336 pub unsafe fn startWithCompletionHandler(
337 &self,
338 completion_handler: CHHapticCompletionHandler,
339 );
340
341 /// Start the engine and block until the engine has started.
342 ///
343 /// This method will return NO upon failure, and outError will be set to a valid NSError describing the error.
344 #[unsafe(method(startAndReturnError:_))]
345 #[unsafe(method_family = none)]
346 pub unsafe fn startAndReturnError(&self) -> Result<(), Retained<NSError>>;
347
348 #[cfg(feature = "block2")]
349 /// Asynchronously stop the engine. The handler will be called when the operation completes.
350 ///
351 /// The handler is guaranteed to be called on either success or failure.
352 #[unsafe(method(stopWithCompletionHandler:))]
353 #[unsafe(method_family = none)]
354 pub unsafe fn stopWithCompletionHandler(
355 &self,
356 completion_handler: CHHapticCompletionHandler,
357 );
358
359 #[cfg(feature = "block2")]
360 /// Tell the engine to asynchronously call the passed-in handler when all active pattern players associated
361 /// with this engine have stopped.
362 ///
363 /// Parameter `finishedHandler`: The block that will be called asynchronously. The return value of this block determines the action the
364 /// engine will take when the block finishes (see `CHHapticEngineFinishedHandler`).
365 ///
366 /// If additional players are started after this call is made, they will delay the callback.
367 /// If no players are active or the engine is stopped, the callback will happen immediately.
368 #[unsafe(method(notifyWhenPlayersFinished:))]
369 #[unsafe(method_family = none)]
370 pub unsafe fn notifyWhenPlayersFinished(
371 &self,
372 finished_handler: CHHapticEngineFinishedHandler,
373 );
374
375 #[cfg(all(feature = "CHHapticPattern", feature = "CHHapticPatternPlayer"))]
376 /// Factory method for creating a CHHapticPatternPlayer from a CHHapticPattern.
377 ///
378 /// Parameter `pattern`: The pattern to be played.
379 #[unsafe(method(createPlayerWithPattern:error:_))]
380 #[unsafe(method_family = none)]
381 pub unsafe fn createPlayerWithPattern_error(
382 &self,
383 pattern: &CHHapticPattern,
384 ) -> Result<Retained<ProtocolObject<dyn CHHapticPatternPlayer>>, Retained<NSError>>;
385
386 #[cfg(all(feature = "CHHapticPattern", feature = "CHHapticPatternPlayer"))]
387 /// Factory method for creating a CHHapticAdvancedPatternPlayer from a CHHapticPattern.
388 ///
389 /// Parameter `pattern`: The pattern to be played.
390 #[unsafe(method(createAdvancedPlayerWithPattern:error:_))]
391 #[unsafe(method_family = none)]
392 pub unsafe fn createAdvancedPlayerWithPattern_error(
393 &self,
394 pattern: &CHHapticPattern,
395 ) -> Result<Retained<ProtocolObject<dyn CHHapticAdvancedPatternPlayer>>, Retained<NSError>>;
396
397 #[cfg(feature = "CHHapticEvent")]
398 /// Unregister and remove a previously-registered audio resource.
399 ///
400 /// Parameter `resourceID`: The resource ID that was returned when the resource was registered.
401 ///
402 /// Parameter `outError`: If the unregister operation fails, this will be set to a valid NSError describing the error.
403 #[unsafe(method(unregisterAudioResource:error:_))]
404 #[unsafe(method_family = none)]
405 pub unsafe fn unregisterAudioResource_error(
406 &self,
407 resource_id: CHHapticAudioResourceID,
408 ) -> Result<(), Retained<NSError>>;
409
410 /// Simple one-shot call to play a pattern specified by a URL.
411 ///
412 /// Parameter `fileURL`: The URL of the file containing a haptic/audio pattern dictionary.
413 ///
414 /// Parameter `outError`: If the operation fails, this will be set to a valid NSError describing the error.
415 ///
416 /// The engine should be started prior to calling this method if low latency is desired. If this is not done,
417 /// this method will start it, which can cause a significant delay.
418 #[unsafe(method(playPatternFromURL:error:_))]
419 #[unsafe(method_family = none)]
420 pub unsafe fn playPatternFromURL_error(
421 &self,
422 file_url: &NSURL,
423 ) -> Result<(), Retained<NSError>>;
424
425 /// Simple one-shot call to play a pattern specified by NSData.
426 ///
427 /// Parameter `data`: The NSData containing a haptic/audio pattern dictionary.
428 ///
429 /// Parameter `outError`: If the operation fails, this will be set to a valid NSError describing the error.
430 ///
431 /// The engine should be started prior to calling this method if low latency is desired. If this is not done,
432 /// this method will start it, which can cause a significant delay.
433 #[unsafe(method(playPatternFromData:error:_))]
434 #[unsafe(method_family = none)]
435 pub unsafe fn playPatternFromData_error(
436 &self,
437 data: &NSData,
438 ) -> Result<(), Retained<NSError>>;
439 );
440}
441
442/// Methods declared on superclass `NSObject`.
443impl CHHapticEngine {
444 extern_methods!(
445 #[unsafe(method(new))]
446 #[unsafe(method_family = new)]
447 pub unsafe fn new() -> Retained<Self>;
448 );
449}