private void SendEvent(ControllerDevice device, string e) { try { if (_socket == null || _endPoint == null) { return; } bool supportedDevice = SupportedDevices.ContainsKey(device.UsbId); string outgoingString = String.Format("{0},{1},{2}", device.UsbId, device.Name, e); if (supportedDevice) { byte[] send_buffer = Encoding.ASCII.GetBytes(outgoingString); _socket.SendTo(send_buffer, _endPoint); } if (logFileStream != null) { double timestamp = DateTime.UtcNow.ToUniversalTime() .Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)) .TotalMilliseconds; logFileStream.WriteLine(Math.Round(timestamp) + "," + outgoingString); } Debug(outgoingString); } catch (Exception ex) { Console.WriteLine(ex); } }
private void RefreshDevicesTimer_Tick(object sender, System.EventArgs e) { ControllerDevice selectedItem = null; int selectedCell = 0; if (DevicesDataGridView.SelectedCells.Count > 0) { selectedItem = (ControllerDevice)DevicesDataGridView.SelectedCells[0].OwningRow.DataBoundItem; selectedCell = DevicesDataGridView.SelectedCells[0].ColumnIndex; } ScanJoysticks(); foreach (DataGridViewRow row in DevicesDataGridView.Rows) { ControllerDevice rowObject = (ControllerDevice)row.DataBoundItem; if (selectedItem != null && rowObject == selectedItem) { DevicesDataGridView.ClearSelection(); row.Cells[selectedCell].Selected = true; break; } } }
private void UpdateDevices() { _devices.Clear(); foreach (DeviceInstance device in directInput.GetDevices()) { ControllerDevice cd = new ControllerDevice(device); _devices.Add(cd); } }
private void DevicesDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { foreach (DataGridViewRow row in DevicesDataGridView.Rows) { ControllerDevice cd = (ControllerDevice)row.DataBoundItem; if (!SupportedDevices.ContainsKey(cd.UsbId)) { row.DefaultCellStyle.BackColor = Color.LightGray; //row.Cells[0].ReadOnly = true; } } }
private void AddDevice(ControllerDevice addedDevice) { if (SupportedDevices.ContainsKey(addedDevice.UsbId)) { addedDevice.OnStateUpdated += Device_OnStateUpdated; addedDevice.Supported = true; addedDevice.Enabled = true; } addedDevice.Acquire(); _devices.Add(addedDevice); SendEvent(addedDevice, "Connected=1"); }
private void ReadInputTimer_Tick(object sender, EventArgs e) { foreach (ControllerDevice device in _devices) { try { device.Update(); } catch (Exception ex) { Debug("Failure when running device Update()"); Debug(ex.Message); } } ControllerDevice selectedDevice = (ControllerDevice)DevicesDataGridView.CurrentRow.DataBoundItem; _input.Clear(); foreach (JoystickUpdate inputState in selectedDevice.CurrentState.Values) { _input.Add(new ControllerInput(inputState)); } }
private void DevicesDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex >= 0) { DevicesDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); bool enabled = (bool)this.DevicesDataGridView.CurrentCell.Value == true; this.DevicesDataGridView.Rows[e.RowIndex].Cells[0].Value = enabled; ControllerDevice selectedDevice = (ControllerDevice)this.DevicesDataGridView.Rows[e.RowIndex].DataBoundItem; if (enabled) { selectedDevice.OnStateUpdated += Device_OnStateUpdated; } else { selectedDevice.OnStateUpdated -= Device_OnStateUpdated; } } }
private void ScanJoysticks() { List <ControllerDevice> removedDevices = new List <ControllerDevice>(); List <ControllerDevice> addedDevices = new List <ControllerDevice>(); //_directInput.GetDevices().ToList().ConvertAll(device => new ControllerDevice(_directInput, device)); List <DeviceInstance> oldDeviceInstances = _devices.ToList().ConvertAll(d => d.DeviceInstance); List <DeviceInstance> foundDeviceInstances = _directInput.GetDevices().ToList(); //List<DeviceInstance> removedDeviceInstances = oldDeviceInstances.Except(foundDeviceInstances).Where(d => !IsSupported(d)).ToList(); foreach (DeviceInstance deviceInstance in foundDeviceInstances) { if (_devices.Where(d => d.DeviceInstance.InstanceGuid == deviceInstance.InstanceGuid).Count() == 0) { if (ShowAllDevicesCheckBox.Checked == true || SupportedDevices.ContainsKey(ControllerDevice.ProductGuidToUSBID(deviceInstance.ProductGuid))) { addedDevices.Add(new ControllerDevice(_directInput, deviceInstance)); } } } foreach (ControllerDevice device in _devices) { bool match = false; if (SupportedDevices.ContainsKey(device.UsbId) || ShowAllDevicesCheckBox.Checked) { match = foundDeviceInstances.ConvertAll(d => d.InstanceGuid.ToString()).Contains(device.Guid); } if (!match) { // Remove device removedDevices.Add(device); } } foreach (ControllerDevice device in removedDevices) { RemoveDevice(device); } foreach (ControllerDevice device in addedDevices) { AddDevice(device); } }
private void SendEvents(ControllerDevice device, List <string> events) { SendEvent(device, String.Join(",", events)); }
private void RemoveDevice(ControllerDevice removedDevice) { removedDevice.Unacquire(); _devices.Remove(removedDevice); SendEvent(removedDevice, "Connected=0"); }
private bool IsSupported(DeviceInstance deviceInstance) { return(IsSupported(ControllerDevice.ProductGuidToUSBID(deviceInstance.ProductGuid))); }