| 1 | use std::cell::RefCell; |
| 2 | use sdl2::haptic::Haptic; |
| 3 | use sdl2::HapticSubsystem; |
| 4 | use sdl2::GameControllerSubsystem; |
| 5 | use sdl2::event::Event; |
| 6 | use sdl2::controller::GameController; |
| 7 | use std::rc::Rc; |
| 8 | |
| 9 | //#[derive(Debug)] |
| 10 | pub struct ControllerManager { |
| 11 | ctrl: GameControllerSubsystem, |
| 12 | haptic: Rc<HapticSubsystem>, |
| 13 | pub controllers: Vec<Rc<RefCell<Controller>>>, |
| 14 | } |
| 15 | |
| 16 | //#[derive(Debug)] |
| 17 | pub struct Controller { |
| 18 | id: u32, |
| 19 | pub ctrl: GameController, |
| 20 | haptic: Option<Rc<RefCell<Haptic>>>, |
| 21 | } |
| 22 | |
| 23 | impl ControllerManager { |
| 24 | pub fn new(ctrl: GameControllerSubsystem, haptic: HapticSubsystem) -> Self { |
| 25 | ControllerManager { |
| 26 | ctrl, |
| 27 | haptic: Rc::new(haptic), |
| 28 | controllers: vec![], |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | pub fn handle_event(&mut self, event: &Event) { |
| 33 | match event { |
| 34 | Event::ControllerDeviceAdded { which, .. } => { self.add_device(*which) } |
| 35 | Event::ControllerDeviceRemoved { which, .. } => { self.remove_device(*which) } |
| 36 | Event::ControllerDeviceRemapped { which, .. } => { println!("device remapped ({})!", *which) } |
| 37 | Event::ControllerButtonDown { button, .. } => { println!("button {} down!", button.string()) } |
| 38 | Event::ControllerButtonUp { button, .. } => { println!("button {} up!", button.string()) } |
| 39 | Event::ControllerAxisMotion { axis, .. } => { println!("axis motion {}!", axis.string()) } |
| 40 | _ => {} |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn add_device(&mut self, id: u32) { |
| 45 | println!("device added ({})!", id); |
| 46 | let mut ctrl = self.ctrl.open(id).unwrap(); |
| 47 | println!("opened {}", ctrl.name()); |
| 48 | |
| 49 | /* |
| 50 | note about set_rumble (for dualshock 3 at least): |
| 51 | the active range for the low frequency is from 65536/4 to 65536 and escalates in large steps throughout the range |
| 52 | the active range for the high frequency is from 256 to 65536 and effect is the same throughout the whole range |
| 53 | */ |
| 54 | let haptic = match ctrl.set_rumble(0, 256, 100) { |
| 55 | Ok(_) => self.haptic.open_from_joystick_id(id).ok(), |
| 56 | Err(_) => None |
| 57 | }; |
| 58 | |
| 59 | let detached = self.controllers.iter().find(|c| !c.borrow().ctrl.attached()); |
| 60 | match detached { |
| 61 | Some(c) => { |
| 62 | let mut c = c.borrow_mut(); |
| 63 | c.ctrl = ctrl; |
| 64 | c.haptic = haptic.map(|h| Rc::new(RefCell::new(h))); |
| 65 | } |
| 66 | None => { |
| 67 | let c = Rc::new(RefCell::new(Controller {id, ctrl, haptic: haptic.map(|h| Rc::new(RefCell::new(h)))})); |
| 68 | self.controllers.push(c); |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | pub fn remove_device(&mut self, id: i32) { |
| 74 | println!("device removed ({})!", id); |
| 75 | // TODO |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Controller { |
| 80 | /// strength [0 - 1] |
| 81 | pub fn rumble(&self, strength: f32, duration_ms: u32) { |
| 82 | if let Some(h) = &self.haptic { |
| 83 | h.borrow_mut().rumble_play(strength, duration_ms); |
| 84 | } |
| 85 | } |
| 86 | } |