public void SaveDataGridStatesToDisk() { // convert to json string json = JsonConvert.SerializeObject(DataGridStates, Formatting.Indented); // save to disk (overwrite) try { File.WriteAllText(DGStatesPath, json); } catch { // IO error MessagePopper.ShowMessageDialog("There was an error writing to the config file: \n\n" + DGStatesPath + "\n\n Do you have the file open elsewhere??", "IO ERROR!", MessagePopper.DialogButtonOptions.YES); //MessageBox.Show("There was an error writing to the config file: \n\n" + DGStatesPath + "\n\n Do you have the file open elsewhere??", "IO ERROR!", MessageBoxButton.OK, MessageBoxImage.Error); } }
public static void Initialize(MainWindow window) { IntPtr handle = new WindowInteropHelper(window).Handle; if (dinput == null) { dinput = new DirectInput(); } Devices = new List <GamePad>(); ContInfoFromLog = LogParser.Instance.GetAttachedControllers(true).Where(a => a.Type == ControllerType.DirectInput).ToArray(); 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; } if (nId == "") { try { string error = "WARNING\n\nThe following gamepad/joystick has been detected by DirectInput:\n\n" + device.InstanceName + "\n" + device.InstanceGuid + "\n\n" + "BUT, we couldnt find a mednafen ID for it. Please check stdout.txt for any errors. You must correct them before trying to RE-POLL devices again."; MessagePopper.ShowMessageDialog(error, "GAMEPAD DETECTION ISSUE"); } catch (Exception) { } } // instantiate new gamepad instance and add it to the collection GamePad p = new GamePad(device.InstanceName, nId, joystick, device.InstanceGuid); Devices.Add(p); count++; } }
public static void Initialize(MainWindow window) { IntPtr handle = new WindowInteropHelper(window).Handle; IsAvailable = false; // clear devices Devices.Clear(); ContInfoFromLog = LogParser.Instance.GetAttachedControllers(true).Where(a => a.Type == ControllerType.XInput).ToArray(); try { //some users wont even have xinput installed. in order to avoid spurious exceptions and possible instability, check for the library first HasGetInputStateEx = true; LibraryHandle = NativeMethods.LoadLibrary("xinput1_3.dll"); // Win32.LoadLibrary("xinput1_3.dll"); if (LibraryHandle == IntPtr.Zero) { LibraryHandle = NativeMethods.LoadLibrary("xinput1_4.dll"); } if (LibraryHandle == IntPtr.Zero) { LibraryHandle = NativeMethods.LoadLibrary("xinput9_1_0.dll"); HasGetInputStateEx = false; } if (LibraryHandle != IntPtr.Zero) { if (HasGetInputStateEx) { IntPtr proc = GetProcAddressOrdinal(LibraryHandle, new IntPtr(100)); XInputGetStateExProc = (XInputGetStateExProcDelegate)Marshal.GetDelegateForFunctionPointer(proc, typeof(XInputGetStateExProcDelegate)); } //don't remove this code. it's important to catch errors on systems with broken xinput installs. //(probably, checking for the library was adequate, but lets not get rid of this anyway) var test = new SlimDX.XInput.Controller(UserIndex.One).IsConnected; IsAvailable = true; } } catch { } if (!IsAvailable) { return; } //now, at this point, slimdx may be using one xinput, and we may be using another //i'm not sure how slimdx picks its dll to bind to. //i'm not sure how troublesome this will be //maybe we should get rid of slimdx for this altogether var c1 = new Controller(UserIndex.One); var c2 = new Controller(UserIndex.Two); var c3 = new Controller(UserIndex.Three); var c4 = new Controller(UserIndex.Four); try { string error = "WARNING\n\nYou have one or more XInput gamepads/joypads that have been detected, but we couldnt find a mednafen ID for them\n\n" + "Please check stdout.txt for any errors. You must correct them before trying to RE-POLL devices again."; if (c1.IsConnected) { if (ContInfoFromLog.Length < 1) { MessagePopper.ShowMessageDialog(error, "GAMEPAD DETECTION ISSUE"); } Devices.Add(new GamePad360(0, c1, ContInfoFromLog[0])); } if (c2.IsConnected) { if (ContInfoFromLog.Length < 2) { MessagePopper.ShowMessageDialog(error, "GAMEPAD DETECTION ISSUE"); } Devices.Add(new GamePad360(1, c2, ContInfoFromLog[1])); } if (c3.IsConnected) { if (ContInfoFromLog.Length < 3) { MessagePopper.ShowMessageDialog(error, "GAMEPAD DETECTION ISSUE"); } Devices.Add(new GamePad360(2, c3, ContInfoFromLog[2])); } if (c4.IsConnected) { if (ContInfoFromLog.Length < 4) { MessagePopper.ShowMessageDialog(error, "GAMEPAD DETECTION ISSUE"); } Devices.Add(new GamePad360(3, c4, ContInfoFromLog[3])); } } catch (Exception) { } }