Example #1
0
 @Override
 public boolean handleMotionEvent(MotionEvent event) {
   if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
     int actionPointerIndex = event.getActionIndex();
     int action = event.getActionMasked();
     switch (action) {
       case MotionEvent.ACTION_MOVE:
         SDLJoystick joystick = getJoystick(event.getDeviceId());
         if (joystick != null) {
           for (int i = 0; i < joystick.axes.size(); i++) {
             InputDevice.MotionRange range = joystick.axes.get(i);
             /* Normalize the value to -1...1 */
             float value =
                 (event.getAxisValue(range.getAxis(), actionPointerIndex) - range.getMin())
                         / range.getRange()
                         * 2.0f
                     - 1.0f;
             SDLActivity.onNativeJoy(joystick.device_id, i, value);
           }
         }
         break;
       default:
         break;
     }
   }
   return true;
 }
Example #2
0
  @Override
  public void pollInputDevices() {
    int[] deviceIds = InputDevice.getDeviceIds();
    // It helps processing the device ids in reverse order
    // For example, in the case of the XBox 360 wireless dongle,
    // so the first controller seen by SDL matches what the receiver
    // considers to be the first controller

    for (int i = deviceIds.length - 1; i > -1; i--) {
      SDLJoystick joystick = getJoystick(deviceIds[i]);
      if (joystick == null) {
        joystick = new SDLJoystick();
        InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]);
        if ((joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
          joystick.device_id = deviceIds[i];
          joystick.name = joystickDevice.getName();
          joystick.axes = new ArrayList<InputDevice.MotionRange>();
          joystick.hats = new ArrayList<InputDevice.MotionRange>();

          List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
          Collections.sort(ranges, new RangeComparator());
          for (InputDevice.MotionRange range : ranges) {
            if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
              if (range.getAxis() == MotionEvent.AXIS_HAT_X
                  || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
                joystick.hats.add(range);
              } else {
                joystick.axes.add(range);
              }
            }
          }

          mJoysticks.add(joystick);
          SDLActivity.nativeAddJoystick(
              joystick.device_id,
              joystick.name,
              0,
              -1,
              joystick.axes.size(),
              joystick.hats.size() / 2,
              0);
        }
      }
    }

    /* Check removed devices */
    ArrayList<Integer> removedDevices = new ArrayList<Integer>();
    for (int i = 0; i < mJoysticks.size(); i++) {
      int device_id = mJoysticks.get(i).device_id;
      int j;
      for (j = 0; j < deviceIds.length; j++) {
        if (device_id == deviceIds[j]) break;
      }
      if (j == deviceIds.length) {
        removedDevices.add(Integer.valueOf(device_id));
      }
    }

    for (int i = 0; i < removedDevices.size(); i++) {
      int device_id = removedDevices.get(i).intValue();
      SDLActivity.nativeRemoveJoystick(device_id);
      for (int j = 0; j < mJoysticks.size(); j++) {
        if (mJoysticks.get(j).device_id == device_id) {
          mJoysticks.remove(j);
          break;
        }
      }
    }
  }
Example #3
0
 @Override
 public int compare(InputDevice.MotionRange arg0, InputDevice.MotionRange arg1) {
   return arg0.getAxis() - arg1.getAxis();
 }