# platform/desktop/gamepadhaptics (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/desktop/gamepadhaptics/

Mac gamepad vibration. (macOS only)
- A file not in the original. The original vibrates with the browser's Gamepad.vibrationActuator, and the Rust version on desktop vibrates with gilrs force feedback.
  gilrs's macOS implementation has empty force feedback (is_ff_supported is always false), so only on Mac it vibrates here with Apple GameController and CoreHaptics.
- Builds a CHHapticEngine with GCController.controllers() → controller.haptics (GCDeviceHaptics) → createEngineWithLocality,
  and starts right away a player of a CHHapticPattern made of one continuous event (intensity, sharpness).
  - The heavy motor (strong) goes to the left handle (GCHapticsLocalityLeftHandle) engine, the light motor (weak) to the right handle (GCHapticsLocalityRightHandle) engine.
    A pad that offers neither goes to one GCHapticsLocalityHandles engine, or failing that a GCHapticsLocalityDefault engine, with the larger of the two intensities.
  - Calling again stops the previous vibration and starts a new one. If the intensity or duration is 0 it only stops.
  - One continuous event lasts up to 30 seconds, so longer ones chain 30-second events.
  - Engines are built once per controller and handle position and kept. Engines of controllers gone from the list are dropped.
    If an engine stops for an outside reason (stoppedHandler) or comes back from a server error (resetHandler), it is only marked and restarted on the next vibration.
    If starting or the player fails, the engine is dropped and rebuilt once.
- Pairing gilrs pads with GCControllers: first pairs those with the same name (gilrs's OS name and GCController.vendorName, case-insensitive),
  and pairs the rest by gilrs connection order and the order of GCController.controllers(). (so with one controller it is used even if the names differ)
  GameController does not give the vendor and product IDs through a public API, so they are not matched by ID.
- objc2-game-controller 0.3.2 binds createEngineWithLocality only for iOS, tvOS, visionOS, so on Mac it is called directly with msg_send.
  (Apple's documentation states GCDeviceHaptics is available from macOS 11)
- GameController objects are handled on the main thread. The engine frame (winit event loop) is on the main thread so it calls as is, and skips if called from another thread.
- startWirelessControllerDiscovery is for pairing new wireless pads, so it is not called. Pads already connected are in controllers().
  GCController.shouldMonitorBackgroundEvents is about forwarding input, and gilrs reads the input, so it is left alone.

Example: Vibrate one pad for 0.2 seconds and stop (not run since it needs a pad, usually GamepadManager.vibrate calls it)

```rust
use vanilla::platform;

let gamepad_name_list = vec![String::from("DualSense Wireless Controller")];
platform::vibrate_gamepads(&gamepad_name_list, 1.0, 0.5, 200);
platform::vibrate_gamepads(&gamepad_name_list, 0.0, 0.0, 0);
```

## Functions and constants

- `pub fn vibrate_gamepads(gamepad_name_list: &[String], strong_magnitude: f64, weak_magnitude: f64, duration_milliseconds: u32)` (method, since 0.4.0): Vibrates the controllers paired with the gilrs pads. (intensity 0 ~ 1, milliseconds. Stops if the duration or intensity is 0)
  - gamepad_name_list holds the OS names of the connected gilrs pads in connection order.
  - `gamepad_name_list` &[String]: List of OS names of the gilrs pads to vibrate. (connection order, used to pair with GCControllers)
  - `strong_magnitude` f64: Heavy motor (left handle) intensity. (0 ~ 1)
  - `weak_magnitude` f64: Light motor (right handle) intensity. (0 ~ 1)
  - `duration_milliseconds` u32: Vibration duration. (milliseconds, only stops if 0)
- `pub fn release_disconnected_gamepad_haptics()` (method, since 0.4.0): Drops the vibration engines of disconnected controllers. (called when gilrs reports a pad disconnect)
