1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;
use std::rc::Rc;

use dom_struct::dom_struct;
use embedder_traits::{DualRumbleEffectParams, EmbedderMsg};
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use js::jsval::JSVal;
use script_traits::GamepadSupportedHapticEffects;

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::GamepadHapticActuatorBinding::{
    GamepadEffectParameters, GamepadHapticActuatorMethods, GamepadHapticEffectType,
};
use crate::dom::bindings::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{reflect_dom_object_with_proto, DomObject, Reflector};
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::globalscope::GlobalScope;
use crate::dom::promise::Promise;
use crate::realms::InRealm;
use crate::script_runtime::{CanGc, JSContext};
use crate::task::TaskCanceller;
use crate::task_source::gamepad::GamepadTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};

struct HapticEffectListener {
    canceller: TaskCanceller,
    task_source: GamepadTaskSource,
    context: Trusted<GamepadHapticActuator>,
}

impl HapticEffectListener {
    fn handle_stopped(&self, stopped_successfully: bool) {
        let context = self.context.clone();
        let _ = self.task_source.queue_with_canceller(
            task!(handle_haptic_effect_stopped: move || {
                let actuator = context.root();
                actuator.handle_haptic_effect_stopped(stopped_successfully);
            }),
            &self.canceller,
        );
    }

    fn handle_completed(&self, completed_successfully: bool) {
        let context = self.context.clone();
        let _ = self.task_source.queue_with_canceller(
            task!(handle_haptic_effect_completed: move || {
                let actuator = context.root();
                actuator.handle_haptic_effect_completed(completed_successfully);
            }),
            &self.canceller,
        );
    }
}

/// <https://www.w3.org/TR/gamepad/#gamepadhapticactuator-interface>
#[dom_struct]
pub struct GamepadHapticActuator {
    reflector_: Reflector,
    gamepad_index: u32,
    /// <https://www.w3.org/TR/gamepad/#dfn-effects>
    effects: Vec<GamepadHapticEffectType>,
    /// <https://www.w3.org/TR/gamepad/#dfn-playingeffectpromise>
    #[ignore_malloc_size_of = "Rc is hard"]
    playing_effect_promise: DomRefCell<Option<Rc<Promise>>>,
    /// The current sequence ID for playing effects,
    /// incremented on every call to playEffect() or reset().
    /// Used to ensure that promises are resolved correctly.
    /// Based on this pending PR <https://github.com/w3c/gamepad/pull/201>
    sequence_id: Cell<u32>,
    /// The sequence ID during the last playEffect() call
    effect_sequence_id: Cell<u32>,
    /// The sequence ID during the last reset() call
    reset_sequence_id: Cell<u32>,
}

impl GamepadHapticActuator {
    fn new_inherited(
        gamepad_index: u32,
        supported_haptic_effects: GamepadSupportedHapticEffects,
    ) -> GamepadHapticActuator {
        let mut effects = vec![];
        if supported_haptic_effects.supports_dual_rumble {
            effects.push(GamepadHapticEffectType::Dual_rumble);
        }
        if supported_haptic_effects.supports_trigger_rumble {
            effects.push(GamepadHapticEffectType::Trigger_rumble);
        }
        Self {
            reflector_: Reflector::new(),
            gamepad_index: gamepad_index.into(),
            effects,
            playing_effect_promise: DomRefCell::new(None),
            sequence_id: Cell::new(0),
            effect_sequence_id: Cell::new(0),
            reset_sequence_id: Cell::new(0),
        }
    }

    pub fn new(
        global: &GlobalScope,
        gamepad_index: u32,
        supported_haptic_effects: GamepadSupportedHapticEffects,
    ) -> DomRoot<GamepadHapticActuator> {
        Self::new_with_proto(global, gamepad_index, supported_haptic_effects)
    }

    fn new_with_proto(
        global: &GlobalScope,
        gamepad_index: u32,
        supported_haptic_effects: GamepadSupportedHapticEffects,
    ) -> DomRoot<GamepadHapticActuator> {
        let haptic_actuator = reflect_dom_object_with_proto(
            Box::new(GamepadHapticActuator::new_inherited(
                gamepad_index,
                supported_haptic_effects,
            )),
            global,
            None,
            CanGc::note(),
        );
        haptic_actuator
    }
}

impl GamepadHapticActuatorMethods for GamepadHapticActuator {
    /// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-effects>
    fn Effects(&self, cx: JSContext) -> JSVal {
        to_frozen_array(self.effects.as_slice(), cx)
    }

    /// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-playeffect>
    fn PlayEffect(
        &self,
        type_: GamepadHapticEffectType,
        params: &GamepadEffectParameters,
        comp: InRealm,
    ) -> Rc<Promise> {
        let playing_effect_promise = Promise::new_in_current_realm(comp);

        // <https://www.w3.org/TR/gamepad/#dfn-valid-effect>
        match type_ {
            // <https://www.w3.org/TR/gamepad/#dfn-valid-dual-rumble-effect>
            GamepadHapticEffectType::Dual_rumble => {
                if *params.strongMagnitude < 0.0 || *params.strongMagnitude > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Strong magnitude value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                } else if *params.weakMagnitude < 0.0 || *params.weakMagnitude > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Weak magnitude value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                }
            },
            // <https://www.w3.org/TR/gamepad/#dfn-valid-trigger-rumble-effect>
            GamepadHapticEffectType::Trigger_rumble => {
                if *params.strongMagnitude < 0.0 || *params.strongMagnitude > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Strong magnitude value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                } else if *params.weakMagnitude < 0.0 || *params.weakMagnitude > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Weak magnitude value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                } else if *params.leftTrigger < 0.0 || *params.leftTrigger > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Left trigger value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                } else if *params.rightTrigger < 0.0 || *params.rightTrigger > 1.0 {
                    playing_effect_promise.reject_error(Error::Type(
                        "Right trigger value is not within range of 0.0 to 1.0.".to_string(),
                    ));
                    return playing_effect_promise;
                }
            },
        }

        let document = self.global().as_window().Document();
        if !document.is_fully_active() {
            playing_effect_promise.reject_error(Error::InvalidState);
        }

        self.sequence_id.set(self.sequence_id.get().wrapping_add(1));

        if let Some(promise) = self.playing_effect_promise.borrow_mut().take() {
            let trusted_promise = TrustedPromise::new(promise);
            let _ = self.global().gamepad_task_source().queue(
                task!(preempt_promise: move || {
                    let promise = trusted_promise.root();
                    let message = DOMString::from("preempted");
                    promise.resolve_native(&message);
                }),
                &self.global(),
            );
        }

        if !self.effects.contains(&type_) {
            playing_effect_promise.reject_error(Error::NotSupported);
            return playing_effect_promise;
        }

        *self.playing_effect_promise.borrow_mut() = Some(playing_effect_promise.clone());
        self.effect_sequence_id.set(self.sequence_id.get());

        let context = Trusted::new(self);
        let (effect_complete_sender, effect_complete_receiver) =
            ipc::channel().expect("ipc channel failure");
        let (task_source, canceller) = (
            self.global().gamepad_task_source(),
            self.global().task_canceller(TaskSourceName::Gamepad),
        );
        let listener = HapticEffectListener {
            canceller,
            task_source,
            context,
        };

        ROUTER.add_route(
            effect_complete_receiver.to_opaque(),
            Box::new(move |message| {
                let msg = message.to::<bool>();
                match msg {
                    Ok(msg) => listener.handle_completed(msg),
                    Err(err) => warn!("Error receiving a GamepadMsg: {:?}", err),
                }
            }),
        );

        // Note: The spec says we SHOULD also pass a playEffectTimestamp for more precise playback timing
        // when start_delay is non-zero, but this is left more as a footnote without much elaboration.
        // <https://www.w3.org/TR/gamepad/#dfn-issue-a-haptic-effect>

        let params = DualRumbleEffectParams {
            duration: params.duration as f64,
            start_delay: params.startDelay as f64,
            strong_magnitude: *params.strongMagnitude,
            weak_magnitude: *params.weakMagnitude,
        };
        let event = EmbedderMsg::PlayGamepadHapticEffect(
            self.gamepad_index as usize,
            embedder_traits::GamepadHapticEffectType::DualRumble(params),
            effect_complete_sender,
        );
        self.global().as_window().send_to_embedder(event);

        playing_effect_promise
    }

    /// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-reset>
    fn Reset(&self, comp: InRealm) -> Rc<Promise> {
        let promise = Promise::new_in_current_realm(comp);

        let document = self.global().as_window().Document();
        if !document.is_fully_active() {
            promise.reject_error(Error::InvalidState);
            return promise;
        }

        self.sequence_id.set(self.sequence_id.get().wrapping_add(1));

        if let Some(promise) = self.playing_effect_promise.borrow_mut().take() {
            let trusted_promise = TrustedPromise::new(promise);
            let _ = self.global().gamepad_task_source().queue(
                task!(preempt_promise: move || {
                    let promise = trusted_promise.root();
                    let message = DOMString::from("preempted");
                    promise.resolve_native(&message);
                }),
                &self.global(),
            );
        }

        *self.playing_effect_promise.borrow_mut() = Some(promise.clone());

        self.reset_sequence_id.set(self.sequence_id.get());

        let context = Trusted::new(self);
        let (effect_stop_sender, effect_stop_receiver) =
            ipc::channel().expect("ipc channel failure");
        let (task_source, canceller) = (
            self.global().gamepad_task_source(),
            self.global().task_canceller(TaskSourceName::Gamepad),
        );
        let listener = HapticEffectListener {
            canceller,
            task_source,
            context,
        };

        ROUTER.add_route(
            effect_stop_receiver.to_opaque(),
            Box::new(move |message| {
                let msg = message.to::<bool>();
                match msg {
                    Ok(msg) => listener.handle_stopped(msg),
                    Err(err) => warn!("Error receiving a GamepadMsg: {:?}", err),
                }
            }),
        );

        let event =
            EmbedderMsg::StopGamepadHapticEffect(self.gamepad_index as usize, effect_stop_sender);
        self.global().as_window().send_to_embedder(event);

        self.playing_effect_promise.borrow().clone().unwrap()
    }
}

impl GamepadHapticActuator {
    /// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-playeffect>
    /// We are in the task queued by the "in-parallel" steps.
    pub fn handle_haptic_effect_completed(&self, completed_successfully: bool) {
        if self.effect_sequence_id.get() != self.sequence_id.get() || !completed_successfully {
            return;
        }
        let playing_effect_promise = self.playing_effect_promise.borrow_mut().take();
        if let Some(promise) = playing_effect_promise {
            let message = DOMString::from("complete");
            promise.resolve_native(&message);
        }
    }

    /// <https://www.w3.org/TR/gamepad/#dom-gamepadhapticactuator-reset>
    /// We are in the task queued by the "in-parallel" steps.
    pub fn handle_haptic_effect_stopped(&self, stopped_successfully: bool) {
        if !stopped_successfully {
            return;
        }

        let playing_effect_promise = self.playing_effect_promise.borrow_mut().take();

        if let Some(promise) = playing_effect_promise {
            let trusted_promise = TrustedPromise::new(promise);
            let sequence_id = self.sequence_id.get();
            let reset_sequence_id = self.reset_sequence_id.get();
            let _ = self.global().gamepad_task_source().queue(
                task!(complete_promise: move || {
                    if sequence_id != reset_sequence_id {
                        warn!("Mismatched sequence/reset sequence ids: {} != {}", sequence_id, reset_sequence_id);
                        return;
                    }
                    let promise = trusted_promise.root();
                    let message = DOMString::from("complete");
                    promise.resolve_native(&message);
                }),
                &self.global(),
            );
        }
    }

    /// <https://www.w3.org/TR/gamepad/#handling-visibility-change>
    pub fn handle_visibility_change(&self) {
        if self.playing_effect_promise.borrow().is_none() {
            return;
        }

        let this = Trusted::new(&*self);
        let _ = self.global().gamepad_task_source().queue(
            task!(stop_playing_effect: move || {
                let actuator = this.root();
                let Some(promise) = actuator.playing_effect_promise.borrow_mut().take() else {
                    return;
                };
                let message = DOMString::from("preempted");
                promise.resolve_native(&message);
            }),
            &self.global(),
        );

        let (send, _rcv) = ipc::channel().expect("ipc channel failure");

        let event = EmbedderMsg::StopGamepadHapticEffect(self.gamepad_index as usize, send);
        self.global().as_window().send_to_embedder(event);
    }
}