/// <summary> /// Initializes a new PS4 controller. /// </summary> /// <param name="read">The "file" that the controller sends info to.</param> /// <param name="info">The capabilities of the controller.</param> internal Ps4Controller(SafeFileHandle read, HidPCaps info) { _handleRead = read; _caps = info; _fsRead = new FileStream(read, FileAccess.ReadWrite, _caps.InputReportByteLength, false); // Typically you don't want to explicitly new an enum as it can cause undefined results. // However Monogame doesn't have a default value for Buttons, so we're forced to. // This will make it so the variable is equal to 0, therefore having no flags set. // We could also just set the variable to zero, but this way is clearer to me. CurrentFrameState = new Buttons(); PreviousFrameState = new Buttons(); AsyncState = new Buttons(); //Start reading the file. ReadAsync(); //Determine the controller index. if (_openSlots.Count == 0) { Index = _count++; } else { Index = _openSlots.Min(); _openSlots.Remove(Index); } Ps4Input.OnControllerConnected(this); }
/// <summary> /// Dispose of all dynamic resources. Trigger Disconnect event. /// </summary> private void Close() { if (IsConnected) { if (_fsRead != null) { _fsRead.Close(); } if (_handleRead != null && !_handleRead.IsInvalid) { _handleRead.Close(); } _openSlots.Add(Index); --_count; Ps4Input.OnControllerDisconnected(Index); IsConnected = false; } }