vanilladocs
vanillapunch.com
API / platform/desktop/gamepadhaptics

platform/desktop/gamepadhaptics

맥 게임패드 진동. (macOS 만)

  • 원본에 없는 파일이다. 원본은 브라우저의 Gamepad.vibrationActuator 로 떨고, 러스트 판 데스크탑은 gilrs force feedback 으로 떤다. gilrs 의 macOS 구현은 force feedback 이 비어 있어(is_ff_supported 가 늘 false) 맥만 여기서 애플 GameController 와 CoreHaptics 로 떤다.
  • GCController.controllers() → controller.haptics(GCDeviceHaptics) → createEngineWithLocality 로 CHHapticEngine 을 짓고, 연속 이벤트(세기, 날카로움) 하나로 된 CHHapticPattern 의 플레이어를 바로 시작한다.
  • 무거운 모터(strong)는 왼손잡이(GCHapticsLocalityLeftHandle), 가벼운 모터(weak)는 오른손잡이(GCHapticsLocalityRightHandle) 엔진에 낸다. 둘 다 대지 않는 패드는 GCHapticsLocalityHandles, 그것도 없으면 GCHapticsLocalityDefault 엔진 하나에 둘 중 큰 세기로 낸다.
  • 다시 부르면 앞 진동을 멈추고 새로 낸다. 세기나 길이가 0 이면 멈추기만 한다.
  • 연속 이벤트 하나는 30 초까지라 더 길면 30 초짜리를 이어 붙인다.
  • 엔진은 컨트롤러와 손잡이 자리마다 한 번 짓고 담아 둔다. 목록에서 사라진 컨트롤러의 엔진은 버린다. 엔진이 바깥 까닭으로 멈추거나(stoppedHandler) 서버 오류에서 돌아오면(resetHandler) 표시만 해 두고 다음에 떨 때 다시 시작한다. 시작이나 플레이어가 실패하면 엔진을 버리고 한 번 다시 지어 본다.
  • gilrs 패드와 GCController 짝 맞추기: 이름(gilrs 의 OS 이름과 GCController.vendorName, 대소문자 무시)이 같은 것을 먼저 잇고, 남은 것은 gilrs 의 연결 차례와 GCController.controllers() 의 차례로 잇는다. (그래서 컨트롤러가 하나면 이름이 달라도 그것을 쓴다) GameController 는 제조사, 제품 번호를 공개 API 로 주지 않아 번호로는 맞추지 않는다.
  • objc2-game-controller 0.3.2 는 createEngineWithLocality 를 iOS, tvOS, visionOS 에서만 묶어 내서 맥은 msg_send 로 직접 부른다. (애플 문서는 GCDeviceHaptics 를 macOS 11 부터 쓸 수 있다고 적는다)
  • GameController 객체는 메인 스레드에서 다룬다. 엔진 프레임(winit 이벤트 루프)이 메인 스레드라 그대로 부르고, 다른 스레드에서 부르면 건너뛴다.
  • startWirelessControllerDiscovery 는 새 무선 패드를 짝지을 때 쓰는 것이라 부르지 않는다. 이미 붙은 패드는 controllers() 에 있다. GCController.shouldMonitorBackgroundEvents 는 입력을 넘기는 일에 대한 것이고 입력은 gilrs 가 읽으므로 건드리지 않는다.

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패드 하나를 0.2 초 떨고 멈추기 (패드가 있어야 해서 실행하지 않는다, 보통은 GamepadManager.vibrate 가 부른다)Vibrate one pad for 0.2 seconds and stop (not run since it needs a pad, usually GamepadManager.vibrate calls it)
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

함수functionpub fn vibrate_gamepads(gamepad_name_list: &[String], strong_magnitude: f64, weak_magnitude: f64, duration_milliseconds: u32)#0.4.0 부터since 0.4.0소스source

gilrs 패드들과 짝인 컨트롤러를 떨기. (세기 0 ~ 1, 밀리초. 길이나 세기가 0 이면 멈춘다)

  • gamepad_name_list 는 연결된 gilrs 패드의 OS 이름을 연결 차례로 둔 것이다.

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]
떨 gilrs 패드의 OS 이름 목록. (연결 차례, GCController 와 짝을 맞추는 데 쓴다)List of OS names of the gilrs pads to vibrate. (connection order, used to pair with GCControllers)
strong_magnitudef64
무거운 모터(왼쪽 손잡이) 세기. (0 ~ 1)Heavy motor (left handle) intensity. (0 ~ 1)
weak_magnitudef64
가벼운 모터(오른쪽 손잡이) 세기. (0 ~ 1)Light motor (right handle) intensity. (0 ~ 1)
duration_millisecondsu32
떠는 길이. (밀리초, 0 이면 멈추기만 한다)Vibration duration. (milliseconds, only stops if 0)
함수functionpub fn release_disconnected_gamepad_haptics()#0.4.0 부터since 0.4.0소스source

연결이 끊긴 컨트롤러의 진동 엔진 버리기. (gilrs 가 패드 해제를 알릴 때 부른다)

Drops the vibration engines of disconnected controllers. (called when gilrs reports a pad disconnect)