Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac6c78b6 authored by Michael Wright's avatar Michael Wright
Browse files

Add controller numbers for gamepads / joysticks

Change-Id: I30ac9add6a2473a5ebd83a022c571545e61d1136
parent 69441650
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -25597,6 +25597,7 @@ package android.view {
  public final class InputDevice implements android.os.Parcelable {
    method public int describeContents();
    method public int getControllerNumber();
    method public java.lang.String getDescriptor();
    method public static android.view.InputDevice getDevice(int);
    method public static int[] getDeviceIds();
+21 −4
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import java.util.List;
public final class InputDevice implements Parcelable {
    private final int mId;
    private final int mGeneration;
    private final int mControllerNumber;
    private final String mName;
    private final String mDescriptor;
    private final boolean mIsExternal;
@@ -342,12 +343,12 @@ public final class InputDevice implements Parcelable {
    };

    // Called by native code.
    private InputDevice(int id, int generation, String name, String descriptor,
            boolean isExternal, int sources,
            int keyboardType, KeyCharacterMap keyCharacterMap,
            boolean hasVibrator, boolean hasButtonUnderPad) {
    private InputDevice(int id, int generation, int controllerNumber, String name,
            String descriptor, boolean isExternal, int sources, int keyboardType,
            KeyCharacterMap keyCharacterMap, boolean hasVibrator, boolean hasButtonUnderPad) {
        mId = id;
        mGeneration = generation;
        mControllerNumber = controllerNumber;
        mName = name;
        mDescriptor = descriptor;
        mIsExternal = isExternal;
@@ -361,6 +362,7 @@ public final class InputDevice implements Parcelable {
    private InputDevice(Parcel in) {
        mId = in.readInt();
        mGeneration = in.readInt();
        mControllerNumber = in.readInt();
        mName = in.readString();
        mDescriptor = in.readString();
        mIsExternal = in.readInt() != 0;
@@ -413,6 +415,20 @@ public final class InputDevice implements Parcelable {
        return mId;
    }

    /**
     * The controller number for a given input device.
     * <p>
     * Each game controller or joystick is given a unique controller number when initially
     * configured by the system. The number is not stable and may be changed by the system at any
     * point.  All controller numbers will be non-negative. A game controller or joystick will be
     * given a unique number indexed from one; everything else will be assigned a controller number
     * of 0.
     * </p>
     */
    public int getControllerNumber() {
        return mControllerNumber;
    }

    /**
     * Gets a generation number for this input device.
     * The generation number is incremented whenever the device is reconfigured and its
@@ -739,6 +755,7 @@ public final class InputDevice implements Parcelable {
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mId);
        out.writeInt(mGeneration);
        out.writeInt(mControllerNumber);
        out.writeString(mName);
        out.writeString(mDescriptor);
        out.writeInt(mIsExternal ? 1 : 0);
+5 −4
Original line number Diff line number Diff line
@@ -55,8 +55,8 @@ jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& devi

    ScopedLocalRef<jobject> inputDeviceObj(env, env->NewObject(gInputDeviceClassInfo.clazz,
            gInputDeviceClassInfo.ctor, deviceInfo.getId(), deviceInfo.getGeneration(),
            nameObj.get(), descriptorObj.get(), deviceInfo.isExternal(),
            deviceInfo.getSources(), deviceInfo.getKeyboardType(),
             deviceInfo.getControllerNumber(), nameObj.get(), descriptorObj.get(),
            deviceInfo.isExternal(), deviceInfo.getSources(), deviceInfo.getKeyboardType(),
            kcmObj.get(), deviceInfo.hasVibrator(), deviceInfo.hasButtonUnderPad()));

    const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
@@ -86,8 +86,9 @@ int register_android_view_InputDevice(JNIEnv* env)
    FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
    gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));

    GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz, "<init>",
            "(IILjava/lang/String;Ljava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZ)V");
    GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
            "<init>",
            "(IIILjava/lang/String;Ljava/lang/String;ZIILandroid/view/KeyCharacterMap;ZZ)V");

    GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
            "addMotionRange", "(IIFFFFF)V");
+37 −2
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ EventHub::Device::Device(int fd, int32_t id, const String8& path,
        next(NULL),
        fd(fd), id(id), path(path), identifier(identifier),
        classes(0), configuration(NULL), virtualKeyMap(NULL),
        ffEffectPlaying(false), ffEffectId(-1),
        ffEffectPlaying(false), ffEffectId(-1), controllerNumber(0),
        timestampOverrideSec(0), timestampOverrideUsec(0) {
    memset(keyBitmask, 0, sizeof(keyBitmask));
    memset(absBitmask, 0, sizeof(absBitmask));
@@ -195,7 +195,7 @@ const int EventHub::EPOLL_SIZE_HINT;
const int EventHub::EPOLL_MAX_EVENTS;

EventHub::EventHub(void) :
        mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1),
        mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD), mNextDeviceId(1), mControllerNumbers(),
        mOpeningDevices(0), mClosingDevices(0),
        mNeedToSendFinishedDeviceScan(false),
        mNeedToReopenDevices(false), mNeedToScanDevices(true),
@@ -269,6 +269,13 @@ uint32_t EventHub::getDeviceClasses(int32_t deviceId) const {
    return device->classes;
}

int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
    AutoMutex _l(mLock);
    Device* device = getDeviceLocked(deviceId);
    if (device == NULL) return 0;
    return device->controllerNumber;
}

void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
    AutoMutex _l(mLock);
    Device* device = getDeviceLocked(deviceId);
@@ -1230,6 +1237,10 @@ status_t EventHub::openDeviceLocked(const char *devicePath) {
        device->classes |= INPUT_DEVICE_CLASS_EXTERNAL;
    }

    if (device->classes & (INPUT_DEVICE_CLASS_JOYSTICK | INPUT_DEVICE_CLASS_GAMEPAD)) {
        device->controllerNumber = getNextControllerNumberLocked(device);
    }

    // Register with epoll.
    struct epoll_event eventItem;
    memset(&eventItem, 0, sizeof(eventItem));
@@ -1341,6 +1352,27 @@ bool EventHub::isExternalDeviceLocked(Device* device) {
    return device->identifier.bus == BUS_USB || device->identifier.bus == BUS_BLUETOOTH;
}

int32_t EventHub::getNextControllerNumberLocked(Device* device) {
    if (mControllerNumbers.isFull()) {
        ALOGI("Maximum number of controllers reached, assigning controller number 0 to device %s",
                device->identifier.name.string());
        return 0;
    }
    // Since the controller number 0 is reserved for non-controllers, translate all numbers up by
    // one
    return static_cast<int32_t>(mControllerNumbers.markFirstUnmarkedBit() + 1);
}

void EventHub::releaseControllerNumberLocked(Device* device) {
    int32_t num = device->controllerNumber;
    device->controllerNumber= 0;
    if (num == 0) {
        return;
    }
    mControllerNumbers.clearBit(static_cast<uint32_t>(num - 1));
}


bool EventHub::hasKeycodeLocked(Device* device, int keycode) const {
    if (!device->keyMap.haveKeyLayout() || !device->keyBitmask) {
        return false;
@@ -1392,6 +1424,8 @@ void EventHub::closeDeviceLocked(Device* device) {
        }
    }

    releaseControllerNumberLocked(device);

    mDevices.removeItem(device->id);
    device->close();

@@ -1521,6 +1555,7 @@ void EventHub::dump(String8& dump) {
            dump.appendFormat(INDENT3 "Path: %s\n", device->path.string());
            dump.appendFormat(INDENT3 "Descriptor: %s\n", device->identifier.descriptor.string());
            dump.appendFormat(INDENT3 "Location: %s\n", device->identifier.location.string());
            dump.appendFormat(INDENT3 "ControllerNumber: %d\n", device->controllerNumber);
            dump.appendFormat(INDENT3 "UniqueId: %s\n", device->identifier.uniqueId.string());
            dump.appendFormat(INDENT3 "Identifier: bus=0x%04x, vendor=0x%04x, "
                    "product=0x%04x, version=0x%04x\n",
+12 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <utils/PropertyMap.h>
#include <utils/Vector.h>
#include <utils/KeyedVector.h>
#include <utils/BitSet.h>

#include <linux/input.h>
#include <sys/epoll.h>
@@ -179,6 +180,8 @@ public:

    virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const = 0;

    virtual int32_t getDeviceControllerNumber(int32_t deviceId) const = 0;

    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const = 0;

    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
@@ -263,6 +266,8 @@ public:

    virtual InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const;

    virtual int32_t getDeviceControllerNumber(int32_t deviceId) const;

    virtual void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const;

    virtual status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
@@ -343,6 +348,8 @@ private:
        bool ffEffectPlaying;
        int16_t ffEffectId; // initially -1

        int32_t controllerNumber;

        int32_t timestampOverrideSec;
        int32_t timestampOverrideUsec;

@@ -384,6 +391,9 @@ private:

    bool isExternalDeviceLocked(Device* device);

    int32_t getNextControllerNumberLocked(Device* device);
    void releaseControllerNumberLocked(Device* device);

    // Protect all internal state.
    mutable Mutex mLock;

@@ -398,6 +408,8 @@ private:

    int32_t mNextDeviceId;

    BitSet32 mControllerNumbers;

    KeyedVector<int32_t, Device*> mDevices;

    Device *mOpeningDevices;
Loading