Give added device to any disconnected controller
[kaka/rust-sdl-test.git] / src / core / controller.rs
index 7037d81..f565070 100644 (file)
@@ -10,7 +10,7 @@ use std::rc::Rc;
 pub struct ControllerManager {
     ctrl: GameControllerSubsystem,
     haptic: Rc<HapticSubsystem>,
-    pub controllers: Vec<Rc<Controller>>,
+    pub controllers: Vec<Rc<RefCell<Controller>>>,
 }
 
 //#[derive(Debug)]
@@ -31,12 +31,12 @@ impl ControllerManager {
 
     pub fn handle_event(&mut self, event: &Event) {
        match event {
-           Event::ControllerDeviceAdded { which: id, .. } => { self.add_device(*id) }
-           Event::ControllerDeviceRemoved { which: id, .. } => { self.remove_device(*id) }
-           Event::ControllerDeviceRemapped { which: id, .. } => { println!("device remapped ({})!", *id) }
-           Event::ControllerButtonDown { button: btn, .. } => { println!("button {} down!", btn.string()) }
-           Event::ControllerButtonUp { button: btn, .. } => { println!("button {} up!", btn.string()) }
-           Event::ControllerAxisMotion { axis: ax, .. } => { println!("axis motion {}!", ax.string()) }
+           Event::ControllerDeviceAdded { which, .. } => { self.add_device(*which) }
+           Event::ControllerDeviceRemoved { which, .. } => { self.remove_device(*which) }
+           Event::ControllerDeviceRemapped { which, .. } => { println!("device remapped ({})!", *which) }
+           Event::ControllerButtonDown { button, .. } => { println!("button {} down!", button.string()) }
+           Event::ControllerButtonUp { button, .. } => { println!("button {} up!", button.string()) }
+           Event::ControllerAxisMotion { axis, .. } => { println!("axis motion {}!", axis.string()) }
            _ => {}
        }
     }
@@ -45,18 +45,34 @@ impl ControllerManager {
        println!("device added ({})!", id);
        let mut ctrl = self.ctrl.open(id).unwrap();
        println!("opened {}", ctrl.name());
-       let haptic = match ctrl.set_rumble(500, 1000, 500) {
+
+       /*
+       note about set_rumble (for dualshock 3 at least):
+       the active range for the low frequency is from 65536/4 to 65536 and escalates in large steps throughout the range
+       the active range for the high frequency is from 256 to 65536 and effect is the same throughout the whole range
+        */
+       let haptic = match ctrl.set_rumble(0, 256, 100) {
            Ok(_) => self.haptic.open_from_joystick_id(id).ok(),
            Err(_) => None
        };
 
-       let c = Rc::new(Controller {id, ctrl, haptic: haptic.map(|h| Rc::new(RefCell::new(h)))});
-       c.rumble(0.5, 300);
-       self.controllers.push(c);
+       let detached = self.controllers.iter().find(|c| !c.borrow().ctrl.attached());
+       match detached {
+           Some(c) => {
+               let mut c = c.borrow_mut();
+               c.ctrl = ctrl;
+               c.haptic = haptic.map(|h| Rc::new(RefCell::new(h)));
+           }
+           None => {
+               let c = Rc::new(RefCell::new(Controller {id, ctrl, haptic: haptic.map(|h| Rc::new(RefCell::new(h)))}));
+               self.controllers.push(c);
+           }
+       };
     }
 
     pub fn remove_device(&mut self, id: i32) {
        println!("device removed ({})!", id);
+       // TODO
     }
 }