servo/
gamepad_provider.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use embedder_traits::GamepadHapticEffectType;
6
7pub enum GamepadHapticEffectRequestType {
8    Play(GamepadHapticEffectType),
9    Stop,
10}
11
12pub struct GamepadHapticEffectRequest {
13    gamepad_index: usize,
14    request_type: GamepadHapticEffectRequestType,
15    callback: Option<Box<dyn FnOnce(bool)>>,
16}
17
18impl GamepadHapticEffectRequest {
19    pub(crate) fn new(
20        gamepad_index: usize,
21        request_type: GamepadHapticEffectRequestType,
22        callback: Box<dyn FnOnce(bool)>,
23    ) -> Self {
24        Self {
25            gamepad_index,
26            request_type,
27            callback: Some(callback),
28        }
29    }
30
31    pub fn gamepad_index(&self) -> usize {
32        self.gamepad_index
33    }
34
35    pub fn request_type(&self) -> &GamepadHapticEffectRequestType {
36        &self.request_type
37    }
38
39    pub fn failed(mut self) {
40        if let Some(callback) = self.callback.take() {
41            callback(false);
42        }
43    }
44
45    pub fn succeeded(mut self) {
46        if let Some(callback) = self.callback.take() {
47            callback(true);
48        }
49    }
50}
51
52impl Drop for GamepadHapticEffectRequest {
53    fn drop(&mut self) {
54        if let Some(callback) = self.callback.take() {
55            callback(false);
56        }
57    }
58}
59
60pub trait GamepadProvider {
61    /// Handle a request to play or stop a haptic effect on a connected gamepad.
62    fn handle_haptic_effect_request(&self, _request: GamepadHapticEffectRequest) {}
63}
64
65pub(crate) struct DefaultGamepadProvider;
66
67impl GamepadProvider for DefaultGamepadProvider {}