Esempio n. 1
0
            protected override void RequestInputReportWorker(object cancellationToken)
            {
                var token           = (CancellationToken)cancellationToken;
                var buffer          = new byte[0x32];
                var unmanagedBuffer = Marshal.AllocHGlobal(buffer.Length);

                try
                {
                    while (!token.IsCancellationRequested)
                    {
                        var ret = DeviceHandle.OverlappedDeviceIoControl(
                            IOCTL_BTHPS3_HID_INTERRUPT_READ,
                            IntPtr.Zero,
                            0,
                            unmanagedBuffer,
                            buffer.Length,
                            out _
                            );

                        if (!ret)
                        {
                            OnDisconnected();
                            return;
                        }

                        Marshal.Copy(unmanagedBuffer, buffer, 0, buffer.Length);

                        /*
                         * When connected via Bluetooth the Sixaxis occasionally sends
                         * a report with the second byte 0xff and the rest zeroed.
                         *
                         * This report does not reflect the actual state of the
                         * controller must be ignored to avoid generating false input
                         * events.
                         */
                        if (buffer[2] == 0xff)
                        {
                            continue;
                        }

                        try
                        {
                            OnInputReport(DualShock3InputReport.FromBuffer(buffer.Skip(1).ToArray()));
                        }
                        catch (InvalidDataException ide)
                        {
                            Log.Warning("Malformed input report received: {Exception}", ide);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("{Exception}", ex);
                }
                finally
                {
                    Marshal.FreeHGlobal(unmanagedBuffer);
                }
            }
Esempio n. 2
0
        protected override void RequestInputReportWorker(object cancellationToken)
        {
            var token         = (CancellationToken)cancellationToken;
            var requestSize   = Marshal.SizeOf <AirBenderHost.AirbenderGetDs3InputReport>();
            var requestBuffer = Marshal.AllocHGlobal(requestSize);

            Marshal.StructureToPtr(
                new AirBenderHost.AirbenderGetDs3InputReport
            {
                ClientAddress = ClientAddress.ToNativeBdAddr()
            },
                requestBuffer, false);

            try
            {
                while (!token.IsCancellationRequested)
                {
                    //
                    // This call blocks until the driver supplies new data.
                    //
                    var ret = HostDevice.DeviceHandle.OverlappedDeviceIoControl(
                        AirBenderHost.IoctlAirbenderGetDs3InputReport,
                        requestBuffer, requestSize, requestBuffer, requestSize,
                        out _);

                    if (!ret)
                    {
                        throw new AirBenderGetDs3InputReportFailedException("Input Report Request failed.",
                                                                            new Win32Exception(Marshal.GetLastWin32Error()));
                    }

                    var resp = Marshal.PtrToStructure <AirBenderHost.AirbenderGetDs3InputReport>(requestBuffer);

                    try
                    {
                        OnInputReport(DualShock3InputReport.FromBuffer(resp.ReportBuffer));
                    }
                    catch (InvalidDataException ide)
                    {
                        Log.Warning("Malformed input report received: {Exception}", ide);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("{Exception}", ex);
            }
            finally
            {
                Marshal.FreeHGlobal(requestBuffer);
            }
        }
Esempio n. 3
0
        protected override void RequestInputReportWorker(object cancellationToken)
        {
            var token           = (CancellationToken)cancellationToken;
            var buffer          = new byte[512];
            var unmanagedBuffer = Marshal.AllocHGlobal(buffer.Length);

            try
            {
                while (!token.IsCancellationRequested)
                {
                    var ret = DeviceHandle.OverlappedReadFile(
                        unmanagedBuffer,
                        buffer.Length,
                        out var bytesReturned);

                    if (!ret)
                    {
                        var nex = new Win32Exception(Marshal.GetLastWin32Error());

                        // Valid exception in case the device got surprise-removed, end worker
                        if (nex.NativeErrorCode == Win32ErrorCode.ERROR_OPERATION_ABORTED)
                        {
                            return;
                        }

                        throw new FireShockReadInputReportFailedException(
                                  "Failed to read input report.", nex);
                    }

                    Marshal.Copy(unmanagedBuffer, buffer, 0, bytesReturned);

                    try
                    {
                        OnInputReport(DualShock3InputReport.FromBuffer(buffer));
                    }
                    catch (InvalidDataException ide)
                    {
                        Log.Warning("Malformed input report received: {Exception}", ide);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("{Exception}", ex);
            }
            finally
            {
                Marshal.FreeHGlobal(unmanagedBuffer);
            }
        }
Esempio n. 4
0
            protected override void RequestInputReportWorker(object cancellationToken)
            {
                var token           = (CancellationToken)cancellationToken;
                var buffer          = new byte[0x32];
                var unmanagedBuffer = Marshal.AllocHGlobal(buffer.Length);

                try
                {
                    while (!token.IsCancellationRequested)
                    {
                        var ret = DeviceHandle.OverlappedDeviceIoControl(
                            IOCTL_BTHPS3_HID_INTERRUPT_READ,
                            IntPtr.Zero,
                            0,
                            unmanagedBuffer,
                            buffer.Length,
                            out _
                            );

                        if (!ret)
                        {
                            OnDisconnected();
                            return;
                        }

                        Marshal.Copy(unmanagedBuffer, buffer, 0, buffer.Length);

                        try
                        {
                            OnInputReport(DualShock3InputReport.FromBuffer(buffer.Skip(1).ToArray()));
                        }
                        catch (InvalidDataException ide)
                        {
                            Log.Warning("Malformed input report received: {Exception}", ide);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("{Exception}", ex);
                }
                finally
                {
                    Marshal.FreeHGlobal(unmanagedBuffer);
                }
            }