public devices.IDevice ResolveDevice (IHIDDevice hidDevice)
		{
			int i = 0;

			int inx;

			//Give Unity Update(Input works only in Update on Main single thread
			//and this funciton is happen on Event on other thread)
			//to be done so GetJoysticksNames return something
			//!!! Not tested yet on WIN
			System.Threading.Thread.Sleep (300);//sleep this Event add device thread
                    
			//find device match based on "names"
			string[] names = Input.GetJoystickNames ();
			inx = Array.IndexOf (names, hidDevice.Name);


			Debug.Log (String.Join (",", names));

			if (inx < 0)
				return null;

			if (inx > 3) {
				Debug.LogWarning ("Unity supports up to 4 Joysticks");
				return null;
			}

			DeviceProfile profile = hidDevice.loadProfile ();

					
                           
                        

			JoystickDevice device = new JoystickDevice (inx, hidDevice.PID, hidDevice.VID, hidDevice.ID, 12, 20, this);
			device.Name = hidDevice.Name;
			device.profile = profile;

			int numAxis = device.Axis.Count;
			int numButtons = device.Buttons.Count;

			for (; i < numAxis; i++) {
				device.Axis [i] = new AxisDetails ();
				if (profile != null && profile.axisNaming.Length > i) {
					device.Axis [i].name = profile.axisNaming [i];

				}
                
			}

			for (i=0; i < numButtons; i++) {
				device.Buttons [i] = new ButtonDetails ();
				if (profile != null && profile.buttonNaming.Length > i) {
					device.Buttons [i].name = profile.buttonNaming [i];
				}
			}
				
				

			return device;
		}
        public IDevice ResolveDevice(IHIDDevice info)
        {

            GenericHIDDevice genericDevice = (GenericHIDDevice)info;

            _hidInterface = info.hidInterface;

           

            JoystickDevice joystick;

            int numAxes = genericDevice.numAxes; //info.Extension.axes;
            int numButtons = genericDevice.numButtons; //info.Extension.buttons;

         
              joystick = new JoystickDevice(info.index, info.PID, info.VID,info.ID, Math.Max(8,numAxes),numButtons, this);
              joystick.Name = info.Name;

                    

                    int index = 0;

                    for (; index < numButtons; index++)
                    {
                        joystick.Buttons[index] = new ButtonDetails();
                    }

             
                    for (index = 0; index < numAxes; index++)
                    {
                                             
                          joystick.Axis[index] = new AxisDetails();
                       
                    }

                    //joystick.numPOV = 1;
               



                   // joystick.Axis[index - 1].isHat = true;
                   //joystick.Axis[index - 2].isHat = true;

                    return joystick;

        }
		public IDevice ResolveDevice(IHIDDevice hidDevice)
		{
			_hidInterface = hidDevice.hidInterface;
			
			JoystickDevice device;
			
			//Get jostick capabilities
			Native.JoyCaps caps;
			Native.JoystickError result = Native.JoystickError.InvalidParameters;



            DeviceProfile profile;

          
			
			int i;
			for (i = 0; i < 16; i++)
			{
				
				result = Native.joyGetDevCaps(i, out caps, Native.JoyCaps.SizeInBytes);
				
				
				if (result == Native.JoystickError.NoError && caps.PID == hidDevice.PID && hidDevice.VID == caps.VID)
				{
					
					
					//UnityEngine.Debug.Log("ID:"+i+" on PID:"+info.PID+" VID:"+info.VID+" info:"+info.DevicePath+"Bts"+caps.NumButtons.ToString()+"axes"+caps.NumAxes
					//  +"ProdID"+caps.PID+" name:"+caps.ProductName+" regkey"+caps.RegKey+"Man:"+caps.VID);
					
					
					
					
					
					
					
					int num_axes = caps.NumAxes;
					
					//!!! save ordNumber(I don't have still function that would return ordNum for WinMM from PID);
					((GenericHIDDevice)hidDevice).ord = i;
					
                    profile=hidDevice.loadProfile();
					
					device = new JoystickDevice(hidDevice.index, hidDevice.PID, hidDevice.VID,hidDevice.ID, 8, caps.NumButtons, this);
					device.Extension = new WinDefaultExtension();
					device.Name = hidDevice.Name;
					device.profile = profile;
					
					int buttonIndex = 0;
					for (; buttonIndex < caps.NumButtons; buttonIndex++)
					{
						device.Buttons[buttonIndex] = new ButtonDetails();
						
						if (profile != null && profile.buttonNaming.Length > buttonIndex)
						{
							device.Buttons[buttonIndex].name = profile.buttonNaming[buttonIndex];
						}
					}
					
					
					// Make sure to reverse the vertical axes, so that +1 points up and -1 points down.
					int axis = 0;
					AxisDetails axisDetails;
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.max = caps.XMax;
						axisDetails.min = caps.XMin;
						device.Axis[axis] = axisDetails;
						//if(axisDetails.min==0 && axisDetails.max==255) axisDetails.isTrigger=true;
						
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						
						axis++;
					}
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.max = caps.YMax;
						axisDetails.min = caps.YMin;
						// if (axisDetails.min == 0 && axisDetails.max == 32767) axisDetails.isTrigger = true;
						device.Axis[axis] = axisDetails;
						
						
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						
						//	stick.Details.Min[axis] = caps.YMin; stick.Details.Max[axis] = caps.YMax; 
						axis++;
					}
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.max = caps.ZMax;
						axisDetails.min = caps.ZMin;
						//if(axisDetails.min==0) axisDetails.isTrigger=true;
						device.Axis[axis] = axisDetails;
						
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						
						axis++;
					}
					
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.min = caps.RMin;
						axisDetails.max = caps.RMax;
						//    if (axisDetails.min == 0 && axisDetails.max == 255) axisDetails.isTrigger = true;
						device.Axis[axis] = axisDetails;
						
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						
						axis++;
					}
					
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.min = caps.UMin;
						axisDetails.max = caps.UMax;
						//     if (axisDetails.min == 0 && axisDetails.max == 255) axisDetails.isTrigger = true;
						device.Axis[axis] = axisDetails;
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						axis++;
					}
					
					if (axis < num_axes)
					{
						axisDetails = new AxisDetails();
						axisDetails.max = caps.VMax;
						axisDetails.min = caps.VMin;
						//     if (axisDetails.min == 0 && axisDetails.max == 255) axisDetails.isTrigger = true;
						device.Axis[axis] = axisDetails;
						
						if (profile != null && profile.axisNaming.Length > axis)
						{
							axisDetails.name = profile.axisNaming[axis];
							
						}
						axis++;
					}
					
					if ((caps.Capabilities & Native.JoystCapsFlags.HasPov) != 0)
					{
						device.Axis[JoystickAxis.AxisPovX] = new AxisDetails();
						device.Axis[JoystickAxis.AxisPovY] = new AxisDetails();
						
						if (profile != null && profile.axisNaming.Length > (int)JoystickAxis.AxisPovX)
						{
							device.Axis[JoystickAxis.AxisPovX].name = profile.axisNaming[(int)JoystickAxis.AxisPovX];
							
						}
						
						if (profile != null && profile.axisNaming.Length > (int)JoystickAxis.AxisPovY)
						{
							device.Axis[JoystickAxis.AxisPovY].name = profile.axisNaming[(int)JoystickAxis.AxisPovY];
							
						}
						
						
						device.numPOV = 1;
						
						//                        WinDefaultExtension extension = joystick.Extension as WinDefaultExtension;
						//
						//                        extension.PovType = Native.PovType.Exists;
						//                        if ((caps.Capabilities & Native.JoystCapsFlags.HasPov4Dir) != 0)
						//                            extension.PovType |= Native.PovType.Discrete;
						//                        if ((caps.Capabilities & Native.JoystCapsFlags.HasPovContinuous) != 0)
						//                            extension.PovType |= Native.PovType.Continuous;
					}
					
					
					
					//  UnityEngine.Debug.Log(" max:" + caps.YMax + " min:" + caps.YMin + " max:" + caps.ZMax + " min:" + caps.ZMin);
					
					//    UnityEngine.Debug.Log(" max:" + caps.RMax + " min:" + caps.RMin + " max:" + caps.UMax + " min:" + caps.UMin);
					
					return device;
					
					
				}
			}
			
			
			return null;
			
			//return (IDevice<IAxisDetails, IButtonDetails, IDeviceExtension>)joystick;
		}
Example #4
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;

		}