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 ///
207 /// # Safety
208 ///
209 /// `stopped_handler` must be a valid pointer.
210 #[unsafe(method(setStoppedHandler:))]
211 #[unsafe(method_family = none)]
212 pub unsafe fn setStoppedHandler(&self, stopped_handler: CHHapticEngineStoppedHandler);
213
214 #[cfg(feature = "block2")]
215 /// This block will called asynchronously if the haptic engine has to reset itself after a server failure.
216 ///
217 /// In response to this handler being called, the client must release all haptic pattern players
218 /// and recreate them. All CHHapticPattern objects and CHHapticEngine properties will have been preserved.
219 /// In general, callbacks arrive on a non-main thread and it is the client's responsibility to handle
220 /// it in a thread-safe manner.
221 #[unsafe(method(resetHandler))]
222 #[unsafe(method_family = none)]
223 pub unsafe fn resetHandler(&self) -> CHHapticEngineResetHandler;
224
225 #[cfg(feature = "block2")]
226 /// Setter for [`resetHandler`][Self::resetHandler].
227 ///
228 /// # Safety
229 ///
230 /// `reset_handler` must be a valid pointer.
231 #[unsafe(method(setResetHandler:))]
232 #[unsafe(method_family = none)]
233 pub unsafe fn setResetHandler(&self, reset_handler: CHHapticEngineResetHandler);
234
235 /// If set to YES, the CHHapticEngine will ignore all events of type CHHapticEventTypeAudio and play only haptic events.
236 ///
237 /// This behavior change will only take effect after the engine is stopped and restarted.
238 /// The default is NO.
239 #[unsafe(method(playsHapticsOnly))]
240 #[unsafe(method_family = none)]
241 pub unsafe fn playsHapticsOnly(&self) -> bool;
242
243 /// Setter for [`playsHapticsOnly`][Self::playsHapticsOnly].
244 #[unsafe(method(setPlaysHapticsOnly:))]
245 #[unsafe(method_family = none)]
246 pub unsafe fn setPlaysHapticsOnly(&self, plays_haptics_only: bool);
247
248 /// If set to YES, the CHHapticEngine will ignore all events of type CHHapticEventTypeHaptic and play only audio events.
249 ///
250 /// This behavior change will only take effect after the engine is stopped and restarted.
251 /// The default is NO.
252 #[unsafe(method(playsAudioOnly))]
253 #[unsafe(method_family = none)]
254 pub unsafe fn playsAudioOnly(&self) -> bool;
255
256 /// Setter for [`playsAudioOnly`][Self::playsAudioOnly].
257 #[unsafe(method(setPlaysAudioOnly:))]
258 #[unsafe(method_family = none)]
259 pub unsafe fn setPlaysAudioOnly(&self, plays_audio_only: bool);
260
261 /// When set to YES, the CHHapticEngine mutes audio playback from its players.
262 ///
263 /// Default is NO.
264 #[unsafe(method(isMutedForAudio))]
265 #[unsafe(method_family = none)]
266 pub unsafe fn isMutedForAudio(&self) -> bool;
267
268 /// Setter for [`isMutedForAudio`][Self::isMutedForAudio].
269 #[unsafe(method(setIsMutedForAudio:))]
270 #[unsafe(method_family = none)]
271 pub unsafe fn setIsMutedForAudio(&self, is_muted_for_audio: bool);
272
273 /// When set to YES, the CHHapticEngine mutes haptic playback from its players.
274 ///
275 /// Default is NO.
276 #[unsafe(method(isMutedForHaptics))]
277 #[unsafe(method_family = none)]
278 pub unsafe fn isMutedForHaptics(&self) -> bool;
279
280 /// Setter for [`isMutedForHaptics`][Self::isMutedForHaptics].
281 #[unsafe(method(setIsMutedForHaptics:))]
282 #[unsafe(method_family = none)]
283 pub unsafe fn setIsMutedForHaptics(&self, is_muted_for_haptics: bool);
284
285 /// When auto shutdown is enabled, the haptic engine can start and stop the hardware dynamically,
286 /// to conserve power.
287 ///
288 /// To conserve power, it is advised that the client stop the haptic engine when not in use.
289 /// But when auto shutdown is enabled, the haptic engine will stop the hardware if it was running
290 /// idle for a certain duration, and restart it later when required.
291 /// Note that, because this operation is dynamic, it may affect the start times of the pattern players
292 /// (e.g. `CHHapticPatternplayer`), if the engine has to resume from its shutdown state.
293 ///
294 /// This feature is disabled by default, but the client can enable it if needed.
295 #[unsafe(method(isAutoShutdownEnabled))]
296 #[unsafe(method_family = none)]
297 pub unsafe fn isAutoShutdownEnabled(&self) -> bool;
298
299 /// Setter for [`isAutoShutdownEnabled`][Self::isAutoShutdownEnabled].
300 #[unsafe(method(setAutoShutdownEnabled:))]
301 #[unsafe(method_family = none)]
302 pub unsafe fn setAutoShutdownEnabled(&self, auto_shutdown_enabled: bool);
303
304 #[unsafe(method(init))]
305 #[unsafe(method_family = init)]
306 pub unsafe fn init(this: Allocated<Self>) -> Option<Retained<Self>>;
307
308 /// Create an instance of the CHHapticEngine.
309 ///
310 /// More than one instance may exist within a process. Each will function independently of the others.
311 /// CHHapticEngines created using this method will be associated with the device's internal haptics hardware system,
312 /// if one exists. For systems without internal haptics, this method will fail with the error `CHHapticErrorCodeNotSupported`.
313 /// To access engine instances associated with external game controllers, see the GameController framework documentation
314 /// for the `hapticEngines` property on the GCController class.
315 #[unsafe(method(initAndReturnError:_))]
316 #[unsafe(method_family = init)]
317 pub unsafe fn initAndReturnError(
318 this: Allocated<Self>,
319 ) -> Result<Retained<Self>, Retained<NSError>>;
320
321 #[cfg(feature = "objc2-avf-audio")]
322 /// Create an instance of an CHHapticEngine and associate it with an audio session. If 'audioSession' is nil,
323 /// the engine will create its own.
324 ///
325 /// More than one instance may exist within a process. Each will function independently of the others, but all
326 /// CHHapticEngines which share an audio session will have identical audio behavior with regard to interruptions, etc.
327 /// CHHapticEngines created using this method will be associated with the device's internal haptics hardware system,
328 /// if one exists. For systems without internal haptics, this method will fail with the error `CHHapticErrorCodeNotSupported`.
329 /// To access engine instances associated with external game controllers, see the GameController framework documentation
330 /// for the `hapticEngines` property on the GCController class.
331 #[unsafe(method(initWithAudioSession:error:_))]
332 #[unsafe(method_family = init)]
333 pub unsafe fn initWithAudioSession_error(
334 this: Allocated<Self>,
335 audio_session: Option<&AVAudioSession>,
336 ) -> Result<Retained<Self>, Retained<NSError>>;
337
338 #[cfg(feature = "block2")]
339 /// Asynchronously start the engine. The handler will be called when the operation completes.
340 ///
341 /// The handler is guaranteed to be called on either success or failure.
342 ///
343 /// # Safety
344 ///
345 /// `completion_handler` must be a valid pointer or null.
346 #[unsafe(method(startWithCompletionHandler:))]
347 #[unsafe(method_family = none)]
348 pub unsafe fn startWithCompletionHandler(
349 &self,
350 completion_handler: CHHapticCompletionHandler,
351 );
352
353 /// Start the engine and block until the engine has started.
354 ///
355 /// This method will return NO upon failure, and outError will be set to a valid NSError describing the error.
356 #[unsafe(method(startAndReturnError:_))]
357 #[unsafe(method_family = none)]
358 pub unsafe fn startAndReturnError(&self) -> Result<(), Retained<NSError>>;
359
360 #[cfg(feature = "block2")]
361 /// Asynchronously stop the engine. The handler will be called when the operation completes.
362 ///
363 /// The handler is guaranteed to be called on either success or failure.
364 ///
365 /// # Safety
366 ///
367 /// `completion_handler` must be a valid pointer or null.
368 #[unsafe(method(stopWithCompletionHandler:))]
369 #[unsafe(method_family = none)]
370 pub unsafe fn stopWithCompletionHandler(
371 &self,
372 completion_handler: CHHapticCompletionHandler,
373 );
374
375 #[cfg(feature = "block2")]
376 /// Tell the engine to asynchronously call the passed-in handler when all active pattern players associated
377 /// with this engine have stopped.
378 ///
379 /// Parameter `finishedHandler`: The block that will be called asynchronously. The return value of this block determines the action the
380 /// engine will take when the block finishes (see `CHHapticEngineFinishedHandler`).
381 ///
382 /// If additional players are started after this call is made, they will delay the callback.
383 /// If no players are active or the engine is stopped, the callback will happen immediately.
384 ///
385 /// # Safety
386 ///
387 /// `finished_handler` must be a valid pointer.
388 #[unsafe(method(notifyWhenPlayersFinished:))]
389 #[unsafe(method_family = none)]
390 pub unsafe fn notifyWhenPlayersFinished(
391 &self,
392 finished_handler: CHHapticEngineFinishedHandler,
393 );
394
395 #[cfg(all(feature = "CHHapticPattern", feature = "CHHapticPatternPlayer"))]
396 /// Factory method for creating a CHHapticPatternPlayer from a CHHapticPattern.
397 ///
398 /// Parameter `pattern`: The pattern to be played.
399 #[unsafe(method(createPlayerWithPattern:error:_))]
400 #[unsafe(method_family = none)]
401 pub unsafe fn createPlayerWithPattern_error(
402 &self,
403 pattern: &CHHapticPattern,
404 ) -> Result<Retained<ProtocolObject<dyn CHHapticPatternPlayer>>, Retained<NSError>>;
405
406 #[cfg(all(feature = "CHHapticPattern", feature = "CHHapticPatternPlayer"))]
407 /// Factory method for creating a CHHapticAdvancedPatternPlayer from a CHHapticPattern.
408 ///
409 /// Parameter `pattern`: The pattern to be played.
410 #[unsafe(method(createAdvancedPlayerWithPattern:error:_))]
411 #[unsafe(method_family = none)]
412 pub unsafe fn createAdvancedPlayerWithPattern_error(
413 &self,
414 pattern: &CHHapticPattern,
415 ) -> Result<Retained<ProtocolObject<dyn CHHapticAdvancedPatternPlayer>>, Retained<NSError>>;
416
417 #[cfg(feature = "CHHapticEvent")]
418 /// Unregister and remove a previously-registered audio resource.
419 ///
420 /// Parameter `resourceID`: The resource ID that was returned when the resource was registered.
421 ///
422 /// Parameter `outError`: If the unregister operation fails, this will be set to a valid NSError describing the error.
423 #[unsafe(method(unregisterAudioResource:error:_))]
424 #[unsafe(method_family = none)]
425 pub unsafe fn unregisterAudioResource_error(
426 &self,
427 resource_id: CHHapticAudioResourceID,
428 ) -> Result<(), Retained<NSError>>;
429
430 /// Simple one-shot call to play a pattern specified by a URL.
431 ///
432 /// Parameter `fileURL`: The URL of the file containing a haptic/audio pattern dictionary.
433 ///
434 /// Parameter `outError`: If the operation fails, this will be set to a valid NSError describing the error.
435 ///
436 /// The engine should be started prior to calling this method if low latency is desired. If this is not done,
437 /// this method will start it, which can cause a significant delay.
438 #[unsafe(method(playPatternFromURL:error:_))]
439 #[unsafe(method_family = none)]
440 pub unsafe fn playPatternFromURL_error(
441 &self,
442 file_url: &NSURL,
443 ) -> Result<(), Retained<NSError>>;
444
445 /// Simple one-shot call to play a pattern specified by NSData.
446 ///
447 /// Parameter `data`: The NSData containing a haptic/audio pattern dictionary.
448 ///
449 /// Parameter `outError`: If the operation fails, this will be set to a valid NSError describing the error.
450 ///
451 /// The engine should be started prior to calling this method if low latency is desired. If this is not done,
452 /// this method will start it, which can cause a significant delay.
453 #[unsafe(method(playPatternFromData:error:_))]
454 #[unsafe(method_family = none)]
455 pub unsafe fn playPatternFromData_error(
456 &self,
457 data: &NSData,
458 ) -> Result<(), Retained<NSError>>;
459 );
460}
461
462/// Methods declared on superclass `NSObject`.
463impl CHHapticEngine {
464 extern_methods!(
465 #[unsafe(method(new))]
466 #[unsafe(method_family = new)]
467 pub unsafe fn new() -> Retained<Self>;
468 );
469}