Exemple #1
0
        /// <summary>
        /// Handler of callback by OS planned to be used with default OSXDriver
        /// </summary>
        /// <param name="context">Context.</param>
        /// <param name="res">Res.</param>
        /// <param name="sender">Sender.</param>
        /// <param name="valRef">Value reference.</param>
        internal void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef valRef)
        {
            IOHIDElementRef element = Native.IOHIDValueGetElement(valRef);
            uint            uid     = Native.IOHIDElementGetCookie(element);
            int             value;

            Native.IOHIDElementType type = Native.IOHIDElementGetType(element);
            GenericHIDDevice        device;

            value = Native.IOHIDValueGetIntegerValue(valRef);

            //UnityEngine.Debug.Log ("DeviceValueReceived:"+value);


            try{
                GCHandle gch = GCHandle.FromIntPtr(context);
                device = (GenericHIDDevice)gch.Target;
            }
            catch (Exception e) {
                UnityEngine.Debug.LogException(e);
                return;
            }



            byte[] typeBuff  = BitConverter.GetBytes((uint)type);
            byte[] uidBuff   = BitConverter.GetBytes(uid);
            byte[] valueBuff = BitConverter.GetBytes(value);


            byte[] result = new byte[typeBuff.Length + uidBuff.Length + valueBuff.Length];
            System.Buffer.BlockCopy(typeBuff, 0, result, 0, typeBuff.Length);
            System.Buffer.BlockCopy(uidBuff, 0, result, typeBuff.Length, uidBuff.Length);
            System.Buffer.BlockCopy(valueBuff, 0, result, typeBuff.Length + uidBuff.Length, valueBuff.Length);



            device.__lastHIDReport.Status = HIDReport.ReadStatus.Success;
            device.__lastHIDReport.Data   = result;
        }
Exemple #2
0
        /// <summary>
        /// Resolves the device.
        /// </summary>
        /// <returns>returns JoystickDevice if driver is for this device or null</returns>
        /// <param name="info">Info.</param>
        /// <param name="hidDevice">Hid device.</param>
        public IDevice ResolveDevice(IHIDDevice hidDevice)
        {
            this._hidInterface = hidDevice.hidInterface;

            IntPtr deviceRef = hidDevice.deviceHandle;

            JoystickDevice device;
            int            axisIndex   = 0;
            int            buttonIndex = 0;

            Native.CFArray   elements = new Native.CFArray();
            IOHIDElementRef  element;
            IOHIDElementType type;

            //copy all matched
            elements.typeRef = Native.IOHIDDeviceCopyMatchingElements(deviceRef, IntPtr.Zero, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone);

            int numButtons = 0;

            int numPov = 0;

            int numElements    = elements.Length;
            int HIDElementType = Native.IOHIDElementGetTypeID();

            //check for profile
            DeviceProfile profile = hidDevice.loadProfile();



            IAxisDetails axisDetailsPovX = null;
            IAxisDetails axisDetailsPovY = null;

            List <IAxisDetails> axisDetailsList = new List <IAxisDetails>();


            IAxisDetails axisDetails;

            for (int elementIndex = 0; elementIndex < numElements; elementIndex++)
            {
                element = elements[elementIndex].typeRef;


                if (element != IntPtr.Zero && Native.CFGetTypeID(element) == HIDElementType)
                {
                    type = Native.IOHIDElementGetType(element);


                    // All of the axis elements I've ever detected have been kIOHIDElementTypeInput_Misc. kIOHIDElementTypeInput_Axis is only included for good faith...
                    if (type == IOHIDElementType.kIOHIDElementTypeInput_Misc ||
                        type == IOHIDElementType.kIOHIDElementTypeInput_Axis)
                    {
                        axisDetails = new AxisDetails();

                        axisDetails.uid        = Native.IOHIDElementGetCookie(element);
                        axisDetails.min        = (int)Native.IOHIDElementGetLogicalMin(element);
                        axisDetails.max        = (int)Native.IOHIDElementGetLogicalMax(element);
                        axisDetails.isNullable = Native.IOHIDElementHasNullState(element);



                        if (Native.IOHIDElementGetUsage(element) == (uint)Native.HIDUsageGD.Hatswitch)
                        {
                            axisDetails.isHat = true;
                            axisDetailsPovX   = axisDetails;

                            axisDetailsPovY = new AxisDetails();

                            axisDetailsPovY.uid        = Native.IOHIDElementGetCookie(element);
                            axisDetailsPovY.min        = (int)Native.IOHIDElementGetLogicalMin(element);
                            axisDetailsPovY.max        = (int)Native.IOHIDElementGetLogicalMax(element);
                            axisDetailsPovY.isNullable = Native.IOHIDElementHasNullState(element);
                            axisDetailsPovY.isHat      = true;

                            numPov++;
                        }
                        else
                        {
                            if (axisDetails.min == 0)
                            {
                                axisDetails.isTrigger = true;
                            }
                            axisDetailsList.Add(axisDetails);
                        }
                    }
                    else if (type == IOHIDElementType.kIOHIDElementTypeInput_Button)
                    {
                        numButtons++;
                    }
                }
            }


            if (axisDetailsPovX != null)
            {
                int diff;

                diff = axisDetailsList.Count - 8;
                //need 2 slots for Pov X,Y
                if (diff >= 0)
                {
                    //just insert them
                    axisDetailsList.Insert((int)JoystickAxis.AxisPovX, axisDetailsPovX);
                    axisDetailsList.Insert((int)JoystickAxis.AxisPovY, axisDetailsPovY);
                }
                else if (diff < -1)
                {
                    diff = diff + 2;
                    while (diff < 0)
                    {
                        axisDetailsList.Add(null);
                        diff += 1;
                    }

                    axisDetailsList.Add(axisDetailsPovX);
                    axisDetailsList.Add(axisDetailsPovY);
                }
                else                  //=-1
                {
                    axisDetailsList.Resize(9);
                    axisDetailsList[(int)JoystickAxis.AxisPovX] = axisDetailsPovX;
                    axisDetailsList[(int)JoystickAxis.AxisPovY] = axisDetailsPovY;
                }
            }


            device         = new JoystickDevice(hidDevice.index, hidDevice.PID, hidDevice.VID, hidDevice.ID, axisDetailsList.Count, numButtons, this);
            device.Name    = hidDevice.Name;
            device.numPOV  = numPov;
            device.profile = profile;

            for (; axisIndex < device.Axis.Count; axisIndex++)
            {
                device.Axis[(JoystickAxis)axisIndex] = axisDetailsList[axisIndex];
                if (profile != null && profile.axisNaming.Length > axisIndex && device.Axis[axisIndex] != null)
                {
                    device.Axis[axisIndex].name = profile.axisNaming [axisIndex];
                }
            }



            for (int elementIndex = 0; elementIndex < numElements; elementIndex++)
            {
                element = elements[elementIndex].typeRef;

                if (element != IntPtr.Zero && Native.CFGetTypeID(element) == HIDElementType)
                {
                    type = Native.IOHIDElementGetType(element);
                    if (type == IOHIDElementType.kIOHIDElementTypeInput_Button)
                    {
                        //
                        device.Buttons[buttonIndex] = new ButtonDetails(Native.IOHIDElementGetCookie(element));


                        if (profile != null && profile.buttonNaming.Length > buttonIndex)
                        {
                            device.Buttons[buttonIndex].name = profile.buttonNaming [buttonIndex];
                        }
                        buttonIndex++;
                    }
                }
            }



            //joystick.isReady = false;

            device.Extension = new OSXDefaultExtension();



            return(device);
        }