Пример #1
0
        public static void Initialize(MainWindow window)
        {
            IntPtr handle = new WindowInteropHelper(window).Handle;

            if (dinput == null)
            {
                dinput = new DirectInput();
            }

            Devices = new List <GamePad>();

            ContInfoFromLog = LogParser.GetDirectInputControllerIds();


            List <xinput_id> xids = new List <xinput_id>();

            // get all directinput pnp devices from WMI
            List <USBDeviceInfo> pnpDevs = pnp.GetUSBDevices().Where(a => a.DeviceID.Contains("IG_")).ToList();

            foreach (USBDeviceInfo pD in pnpDevs)
            {
                xinput_id x        = new xinput_id();
                string    deviceId = pD.DeviceID;
                // get VID and PID
                string[] arr = deviceId.Split('&');
                x.VID    = arr[0].Replace("HID\\VID_", "");
                x.PID    = arr[1].Replace("PID_", "");
                x.PIDVID = (x.PID + x.VID).ToLower();
                xids.Add(x);
            }

            xids = xids.Distinct().ToList();

            int count = 0;

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                Console.WriteLine("joydevice: {0} `{1}`", device.InstanceGuid, device.ProductName);
                string prodName  = device.ProductName;
                string devId     = device.ProductGuid.ToString();
                string devPidVid = (devId.Split('-'))[0];

                // compare PIDVID from WMI from the first part of the ProductGuid. If they match, this is an xinput controller and can be skipped (and left for GamePad360)
                var check = (from a in xids
                             where a.PIDVID == devPidVid
                             select a).ToList();
                if (check.Count > 0)
                {
                    continue;
                }

                //if (device.ProductName.Contains("XBOX 360"))
                //  continue; // Don't input XBOX 360 controllers into here; we'll process them via XInput (there are limitations in some trigger axes when xbox pads go over xinput)

                var joystick = new Joystick(dinput, device.InstanceGuid);
                joystick.SetCooperativeLevel(handle, CooperativeLevel.Background | CooperativeLevel.Nonexclusive);

                foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
                {
                    // set range for each axis (not sure if this actually works)
                    if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    {
                        joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }
                }
                joystick.Acquire();

                string nId = "";
                // get mednafen unique id for this controller from stdout.txt
                if (ContInfoFromLog.Count() > 0)
                {
                    nId = ContInfoFromLog[count].ID;
                }

                // instantiate new gamepad instance and add it to the collection
                GamePad p = new GamePad(device.InstanceName, nId, joystick, device.InstanceGuid);
                Devices.Add(p);
                count++;
            }
        }