1 use std::collections::HashMap;
2 use std::cell::RefCell;
3 use sdl2::haptic::Haptic;
4 use sdl2::HapticSubsystem;
5 pub use sdl2::GameControllerSubsystem;
6 use sdl2::event::Event;
7 use sdl2::controller::GameController;
11 pub struct ControllerManager {
12 pub ctrl: GameControllerSubsystem,
13 haptic: Rc<HapticSubsystem>,
14 pub controllers: HashMap<u32, Rc<RefCell<Controller>>>,
18 pub struct Controller {
19 pub ctrl: GameController,
20 haptic: Option<Rc<RefCell<Haptic>>>,
23 impl ControllerManager {
24 pub fn new(ctrl: GameControllerSubsystem, haptic: HapticSubsystem) -> Self {
25 let mut c = ControllerManager {
27 haptic: Rc::new(haptic),
28 controllers: HashMap::new(),
35 for i in 0..self.ctrl.num_joysticks().unwrap() {
40 pub fn handle_event(&mut self, event: &Event) {
42 Event::ControllerDeviceAdded { which, .. } => { self.add_device(*which) }
43 Event::ControllerDeviceRemoved { which, .. } => { self.remove_device(*which) }
44 Event::ControllerDeviceRemapped { which, .. } => { println!("device remapped ({})!", *which) }
45 Event::ControllerButtonDown { button, .. } => { println!("button {} down!", button.string()) }
46 Event::ControllerButtonUp { button, .. } => { println!("button {} up!", button.string()) }
47 Event::ControllerAxisMotion { axis, .. } => { println!("axis motion {}!", axis.string()) }
52 pub fn add_device(&mut self, id: u32) {
53 println!("device added ({})!", id);
54 let mut ctrl = self.ctrl.open(id).unwrap();
55 println!("opened {}", ctrl.name());
58 note about set_rumble (for dualshock 3 at least):
59 the active range for the low frequency is from 65536/4 to 65536 and escalates in large steps throughout the range
60 the active range for the high frequency is from 256 to 65536 and effect is the same throughout the whole range
62 let haptic = match ctrl.set_rumble(0, 256, 100) {
63 Ok(_) => self.haptic.open_from_joystick_id(id).ok(),
67 if self.controllers.contains_key(&id) {
71 let detached = self.controllers.values().find(|c| !c.borrow().ctrl.attached());
74 let mut c = c.borrow_mut();
76 c.haptic = haptic.map(|h| Rc::new(RefCell::new(h)));
79 let c = Rc::new(RefCell::new(Controller {ctrl, haptic: haptic.map(|h| Rc::new(RefCell::new(h)))}));
80 self.controllers.insert(id, c);
85 pub fn remove_device(&mut self, id: i32) {
86 println!("device removed ({})!", id);
93 pub fn rumble(&self, strength: f32, duration_ms: u32) {
94 if let Some(h) = &self.haptic {
95 h.borrow_mut().rumble_play(strength, duration_ms);