Skip to content

Commit f9264b8

Browse files
committed
Add Gamepad vibrationActuator API to web-sys
The API that currently exists in the repo has been deprecated and the new API has been added. This allows gamepads to be rumbled Spec: https://w3c.github.io/gamepad/ More details: #4089
1 parent e4f8c45 commit f9264b8

File tree

9 files changed

+255
-3
lines changed

9 files changed

+255
-3
lines changed

crates/web-sys/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,14 @@ GamepadAxisMoveEventInit = []
459459
GamepadButton = []
460460
GamepadButtonEvent = ["Event", "GamepadEvent"]
461461
GamepadButtonEventInit = []
462+
GamepadEffectParameters = []
462463
GamepadEvent = ["Event"]
463464
GamepadEventInit = []
464465
GamepadHand = []
465466
GamepadHapticActuator = []
466467
GamepadHapticActuatorType = []
468+
GamepadHapticEffectType = []
469+
GamepadHapticsResult = []
467470
GamepadMappingType = []
468471
GamepadPose = []
469472
GamepadServiceTest = []

crates/web-sys/src/features/gen_Gamepad.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,14 @@ extern "C" {
9191
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/hapticActuators)"]
9292
#[doc = ""]
9393
#[doc = "*This API requires the following crate features to be activated: `Gamepad`*"]
94+
#[deprecated]
9495
pub fn haptic_actuators(this: &Gamepad) -> ::js_sys::Array;
96+
#[cfg(feature = "GamepadHapticActuator")]
97+
# [wasm_bindgen (structural , method , getter , js_class = "Gamepad" , js_name = vibrationActuator)]
98+
#[doc = "Getter for the `vibrationActuator` field of this object."]
99+
#[doc = ""]
100+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/vibrationActuator)"]
101+
#[doc = ""]
102+
#[doc = "*This API requires the following crate features to be activated: `Gamepad`, `GamepadHapticActuator`*"]
103+
pub fn vibration_actuator(this: &Gamepad) -> GamepadHapticActuator;
95104
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#![allow(unused_imports)]
2+
#![allow(clippy::all)]
3+
use super::*;
4+
use wasm_bindgen::prelude::*;
5+
#[wasm_bindgen]
6+
extern "C" {
7+
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = GamepadEffectParameters)]
8+
#[derive(Debug, Clone, PartialEq, Eq)]
9+
#[doc = "The `GamepadEffectParameters` dictionary."]
10+
#[doc = ""]
11+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
12+
pub type GamepadEffectParameters;
13+
#[doc = "Get the `duration` field of this object."]
14+
#[doc = ""]
15+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
16+
#[wasm_bindgen(method, getter = "duration")]
17+
pub fn get_duration(this: &GamepadEffectParameters) -> Option<f64>;
18+
#[doc = "Change the `duration` field of this object."]
19+
#[doc = ""]
20+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
21+
#[wasm_bindgen(method, setter = "duration")]
22+
pub fn set_duration(this: &GamepadEffectParameters, val: f64);
23+
#[doc = "Get the `leftTrigger` field of this object."]
24+
#[doc = ""]
25+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
26+
#[wasm_bindgen(method, getter = "leftTrigger")]
27+
pub fn get_left_trigger(this: &GamepadEffectParameters) -> Option<f64>;
28+
#[doc = "Change the `leftTrigger` field of this object."]
29+
#[doc = ""]
30+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
31+
#[wasm_bindgen(method, setter = "leftTrigger")]
32+
pub fn set_left_trigger(this: &GamepadEffectParameters, val: f64);
33+
#[doc = "Get the `rightTrigger` field of this object."]
34+
#[doc = ""]
35+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
36+
#[wasm_bindgen(method, getter = "rightTrigger")]
37+
pub fn get_right_trigger(this: &GamepadEffectParameters) -> Option<f64>;
38+
#[doc = "Change the `rightTrigger` field of this object."]
39+
#[doc = ""]
40+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
41+
#[wasm_bindgen(method, setter = "rightTrigger")]
42+
pub fn set_right_trigger(this: &GamepadEffectParameters, val: f64);
43+
#[doc = "Get the `startDelay` field of this object."]
44+
#[doc = ""]
45+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
46+
#[wasm_bindgen(method, getter = "startDelay")]
47+
pub fn get_start_delay(this: &GamepadEffectParameters) -> Option<f64>;
48+
#[doc = "Change the `startDelay` field of this object."]
49+
#[doc = ""]
50+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
51+
#[wasm_bindgen(method, setter = "startDelay")]
52+
pub fn set_start_delay(this: &GamepadEffectParameters, val: f64);
53+
#[doc = "Get the `strongMagnitude` field of this object."]
54+
#[doc = ""]
55+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
56+
#[wasm_bindgen(method, getter = "strongMagnitude")]
57+
pub fn get_strong_magnitude(this: &GamepadEffectParameters) -> Option<f64>;
58+
#[doc = "Change the `strongMagnitude` field of this object."]
59+
#[doc = ""]
60+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
61+
#[wasm_bindgen(method, setter = "strongMagnitude")]
62+
pub fn set_strong_magnitude(this: &GamepadEffectParameters, val: f64);
63+
#[doc = "Get the `weakMagnitude` field of this object."]
64+
#[doc = ""]
65+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
66+
#[wasm_bindgen(method, getter = "weakMagnitude")]
67+
pub fn get_weak_magnitude(this: &GamepadEffectParameters) -> Option<f64>;
68+
#[doc = "Change the `weakMagnitude` field of this object."]
69+
#[doc = ""]
70+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
71+
#[wasm_bindgen(method, setter = "weakMagnitude")]
72+
pub fn set_weak_magnitude(this: &GamepadEffectParameters, val: f64);
73+
}
74+
impl GamepadEffectParameters {
75+
#[doc = "Construct a new `GamepadEffectParameters`."]
76+
#[doc = ""]
77+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`*"]
78+
pub fn new() -> Self {
79+
#[allow(unused_mut)]
80+
let mut ret: Self = ::wasm_bindgen::JsCast::unchecked_into(::js_sys::Object::new());
81+
ret
82+
}
83+
#[deprecated = "Use `set_duration()` instead."]
84+
pub fn duration(&mut self, val: f64) -> &mut Self {
85+
self.set_duration(val);
86+
self
87+
}
88+
#[deprecated = "Use `set_left_trigger()` instead."]
89+
pub fn left_trigger(&mut self, val: f64) -> &mut Self {
90+
self.set_left_trigger(val);
91+
self
92+
}
93+
#[deprecated = "Use `set_right_trigger()` instead."]
94+
pub fn right_trigger(&mut self, val: f64) -> &mut Self {
95+
self.set_right_trigger(val);
96+
self
97+
}
98+
#[deprecated = "Use `set_start_delay()` instead."]
99+
pub fn start_delay(&mut self, val: f64) -> &mut Self {
100+
self.set_start_delay(val);
101+
self
102+
}
103+
#[deprecated = "Use `set_strong_magnitude()` instead."]
104+
pub fn strong_magnitude(&mut self, val: f64) -> &mut Self {
105+
self.set_strong_magnitude(val);
106+
self
107+
}
108+
#[deprecated = "Use `set_weak_magnitude()` instead."]
109+
pub fn weak_magnitude(&mut self, val: f64) -> &mut Self {
110+
self.set_weak_magnitude(val);
111+
self
112+
}
113+
}
114+
impl Default for GamepadEffectParameters {
115+
fn default() -> Self {
116+
Self::new()
117+
}
118+
}

crates/web-sys/src/features/gen_GamepadHapticActuator.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,58 @@ extern "C" {
1919
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/type)"]
2020
#[doc = ""]
2121
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticActuator`, `GamepadHapticActuatorType`*"]
22+
#[deprecated]
2223
pub fn type_(this: &GamepadHapticActuator) -> GamepadHapticActuatorType;
24+
# [wasm_bindgen (structural , method , getter , js_class = "GamepadHapticActuator" , js_name = effects)]
25+
#[doc = "Getter for the `effects` field of this object."]
26+
#[doc = ""]
27+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/effects)"]
28+
#[doc = ""]
29+
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticActuator`*"]
30+
pub fn effects(this: &GamepadHapticActuator) -> ::js_sys::Array;
31+
#[cfg(feature = "GamepadHapticEffectType")]
32+
# [wasm_bindgen (method , structural , js_class = "GamepadHapticActuator" , js_name = playEffect)]
33+
#[doc = "The `playEffect()` method."]
34+
#[doc = ""]
35+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/playEffect)"]
36+
#[doc = ""]
37+
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticActuator`, `GamepadHapticEffectType`*"]
38+
pub fn play_effect(
39+
this: &GamepadHapticActuator,
40+
type_: GamepadHapticEffectType,
41+
) -> ::js_sys::Promise;
42+
#[cfg(all(
43+
feature = "GamepadEffectParameters",
44+
feature = "GamepadHapticEffectType",
45+
))]
46+
# [wasm_bindgen (method , structural , js_class = "GamepadHapticActuator" , js_name = playEffect)]
47+
#[doc = "The `playEffect()` method."]
48+
#[doc = ""]
49+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/playEffect)"]
50+
#[doc = ""]
51+
#[doc = "*This API requires the following crate features to be activated: `GamepadEffectParameters`, `GamepadHapticActuator`, `GamepadHapticEffectType`*"]
52+
pub fn play_effect_with_params(
53+
this: &GamepadHapticActuator,
54+
type_: GamepadHapticEffectType,
55+
params: &GamepadEffectParameters,
56+
) -> ::js_sys::Promise;
2357
# [wasm_bindgen (catch , method , structural , js_class = "GamepadHapticActuator" , js_name = pulse)]
2458
#[doc = "The `pulse()` method."]
2559
#[doc = ""]
2660
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/pulse)"]
2761
#[doc = ""]
2862
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticActuator`*"]
63+
#[deprecated]
2964
pub fn pulse(
3065
this: &GamepadHapticActuator,
3166
value: f64,
3267
duration: f64,
3368
) -> Result<::js_sys::Promise, JsValue>;
69+
# [wasm_bindgen (method , structural , js_class = "GamepadHapticActuator" , js_name = reset)]
70+
#[doc = "The `reset()` method."]
71+
#[doc = ""]
72+
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/GamepadHapticActuator/reset)"]
73+
#[doc = ""]
74+
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticActuator`*"]
75+
pub fn reset(this: &GamepadHapticActuator) -> ::js_sys::Promise;
3476
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![allow(unused_imports)]
2+
#![allow(clippy::all)]
3+
use wasm_bindgen::prelude::*;
4+
#[wasm_bindgen]
5+
#[doc = "The `GamepadHapticEffectType` enum."]
6+
#[doc = ""]
7+
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticEffectType`*"]
8+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9+
pub enum GamepadHapticEffectType {
10+
DualRumble = "dual-rumble",
11+
TriggerRumble = "trigger-rumble",
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![allow(unused_imports)]
2+
#![allow(clippy::all)]
3+
use wasm_bindgen::prelude::*;
4+
#[wasm_bindgen]
5+
#[doc = "The `GamepadHapticsResult` enum."]
6+
#[doc = ""]
7+
#[doc = "*This API requires the following crate features to be activated: `GamepadHapticsResult`*"]
8+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9+
pub enum GamepadHapticsResult {
10+
Complete = "complete",
11+
Preempted = "preempted",
12+
}

crates/web-sys/src/features/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,6 +2959,13 @@ mod gen_GamepadButtonEventInit;
29592959
#[allow(unused_imports)]
29602960
pub use gen_GamepadButtonEventInit::*;
29612961

2962+
#[cfg(feature = "GamepadEffectParameters")]
2963+
#[allow(non_snake_case)]
2964+
mod gen_GamepadEffectParameters;
2965+
#[cfg(feature = "GamepadEffectParameters")]
2966+
#[allow(unused_imports)]
2967+
pub use gen_GamepadEffectParameters::*;
2968+
29622969
#[cfg(feature = "GamepadEvent")]
29632970
#[allow(non_snake_case)]
29642971
mod gen_GamepadEvent;
@@ -2994,6 +3001,20 @@ mod gen_GamepadHapticActuatorType;
29943001
#[allow(unused_imports)]
29953002
pub use gen_GamepadHapticActuatorType::*;
29963003

3004+
#[cfg(feature = "GamepadHapticEffectType")]
3005+
#[allow(non_snake_case)]
3006+
mod gen_GamepadHapticEffectType;
3007+
#[cfg(feature = "GamepadHapticEffectType")]
3008+
#[allow(unused_imports)]
3009+
pub use gen_GamepadHapticEffectType::*;
3010+
3011+
#[cfg(feature = "GamepadHapticsResult")]
3012+
#[allow(non_snake_case)]
3013+
mod gen_GamepadHapticsResult;
3014+
#[cfg(feature = "GamepadHapticsResult")]
3015+
#[allow(unused_imports)]
3016+
pub use gen_GamepadHapticsResult::*;
3017+
29973018
#[cfg(feature = "GamepadMappingType")]
29983019
#[allow(non_snake_case)]
29993020
mod gen_GamepadMappingType;

crates/web-sys/webidls/enabled/Gamepad.webidl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ interface Gamepad {
9494
* The current haptic actuator of the device, an array of
9595
* GamepadHapticActuator.
9696
*/
97-
[Constant, Cached, Frozen, Pref="dom.gamepad.extensions.enabled"]
97+
[Constant, Cached, Frozen, RustDeprecated, Pref="dom.gamepad.extensions.enabled"]
9898
readonly attribute sequence<GamepadHapticActuator> hapticActuators;
99+
100+
/**
101+
* The vibration actuator of the device.
102+
*/
103+
[Constant, Cached, Frozen, Pref="dom.gamepad.extensions.enabled"]
104+
readonly attribute GamepadHapticActuator vibrationActuator;
99105
};

crates/web-sys/webidls/enabled/GamepadHapticActuator.webidl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,47 @@
44
* You can obtain one at http://mozilla.org/MPL/2.0/.
55
*
66
* The origin of this IDL file is
7-
* https://w3c.github.io/gamepad/extensions.html#gamepadhapticactuator-interface
7+
* https://w3c.github.io/gamepad/
8+
* (Deprecated) https://w3c.github.io/gamepad/extensions.html#gamepadhapticactuator-interface
89
*/
910

11+
[RustDeprecated]
1012
enum GamepadHapticActuatorType {
1113
"vibration"
1214
};
1315

16+
enum GamepadHapticEffectType {
17+
"dual-rumble",
18+
"trigger-rumble"
19+
};
20+
21+
enum GamepadHapticsResult {
22+
"complete",
23+
"preempted"
24+
};
25+
26+
dictionary GamepadEffectParameters {
27+
unsigned long long duration = 0;
28+
unsigned long long startDelay = 0;
29+
double strongMagnitude = 0.0;
30+
double weakMagnitude = 0.0;
31+
double leftTrigger = 0.0;
32+
double rightTrigger = 0.0;
33+
};
34+
1435
[Pref="dom.gamepad.extensions.enabled",
1536
HeaderFile="mozilla/dom/GamepadHapticActuator.h"]
1637
interface GamepadHapticActuator
1738
{
39+
[RustDeprecated]
1840
readonly attribute GamepadHapticActuatorType type;
19-
[Throws, NewObject]
41+
[Throws, NewObject, RustDeprecated]
2042
Promise<boolean> pulse(double value, double duration);
43+
44+
readonly attribute FrozenArray<GamepadHapticEffectType> effects;
45+
Promise<GamepadHapticsResult> playEffect(
46+
GamepadHapticEffectType type,
47+
optional GamepadEffectParameters params = {}
48+
);
49+
Promise<GamepadHapticsResult> reset();
2150
};

0 commit comments

Comments
 (0)