Пример #1
0
        /// <summary>Connects the Wiimote device.</summary>
        /// <param name="device">The Wiimote device to connect.</param>
        /// <returns>The created Wiimote.</returns>
        private static Wiimote Connect(WiimoteDeviceInfo device)
        {
            lock (wiimotes) {
                if (device.IsOpen)
                {
                    throw new ArgumentException("Cannot connect to device that is already in use!",
                                                nameof(device));
                }
                Wiimote connected = wiimotes.Find(wm => wm.Address == device.Address);
                if (connected != null)
                {
                    throw new WiimoteAlreadyConnectedException(connected);
                }

                Wiimote wiimote = new Wiimote(device);
                wiimotes.Add(wiimote);
                wiimote.SetPlayerLED(wiimotes.Count);
                RaiseConnected(wiimote);
                return(wiimote);
            }
        }
Пример #2
0
        private static bool HIDDiscoverLoop(CancellationToken token)
        {
            var hids = HIDDeviceInfo.EnumerateDevices(token, MatchHID);

            foreach (HIDDeviceInfo hid in hids)
            {
                if (token.IsCancellationRequested)
                {
                    return(false);
                }
                Wiimote wiimote = null;
                lock (wiimotes) {
                    wiimote = wiimotes.Find(wm => wm.DevicePath == hid.DevicePath);
                }

                if (wiimote == null)
                {
                    if (autoConnect)
                    {
                        //FIXME: Handle BOTH Bluetooth and DolphinBarMode more gracefully.
                        WiimoteDeviceInfo wiimoteDevice = new WiimoteDeviceInfo(hid, true);                        // DolphinBarMode);
                        try {
                            Connect(wiimoteDevice);
                        }
                        catch (Exception ex) {
                            RaiseConnectionFailed(wiimoteDevice, ex);
                        }
                    }
                    else if (!RaiseDiscovered(null, hid))
                    {
                        return(false);
                    }
                }
            }
            token.Sleep(1000);
            return(true);
        }
Пример #3
0
        internal Wiimote(WiimoteDeviceInfo device)
        {
            this.device    = device;
            disposed       = false;
            ioLock         = new object();
            wiimoteState   = new WiimoteState();
            altWriteMethod = false;

            //mAltWriteMethod = false;

            device.HID.Open();

            try {
                // We're not ready for this yet
                BeginAsyncRead();

                // read the calibration info from the controller
                //ReadWiimoteCalibration();
                try {
                    ReadWiimoteCalibration();
                }
                catch {
                    // if we fail above, try the alternate HID writes
                    //Debug.WriteLine("AltWriteMethod");
                    altWriteMethod = true;
                    ReadWiimoteCalibration();
                }

                // force a status check to get the state of any extensions plugged in at startup
                GetStatus(500);
            }
            catch {
                Dispose();
                throw;
            }
        }
Пример #4
0
        // Called by manager

        private static bool RaiseDiscovered(BluetoothDeviceInfo bt, HIDDeviceInfo hid)
        {
            if (bt?.IsInvalid ?? true)
            {
                Debug.WriteLine($"{hid} Discovered");
            }
            else
            {
                Debug.WriteLine($"{bt} Discovered");
            }
            WiimoteDeviceInfo device;

            //FIXME: Quick fix to support both Bluetooth and DolphinBar connections.
            if (bt?.IsInvalid ?? true)            // && DolphinBarMode)
            {
                device = new WiimoteDeviceInfo(hid, true);
            }
            else
            {
                device = new WiimoteDeviceInfo(bt, hid);
            }
            WiimoteDiscoveredEventArgs e = new WiimoteDiscoveredEventArgs(device);

            Discovered?.Invoke(null, e);
            if (e.AddDevice)
            {
                try {
                    Connect(e.Device);
                }
                catch (Exception ex) {
                    RaiseConnectionFailed(e.Device, ex);
                    return(true);
                }
            }
            return(e.KeepSearching);
        }
Пример #5
0
 public WiimoteDeviceException(WiimoteDeviceInfo device, string message,
                               Exception innerException)
     : base(message, innerException)
 {
     Device = device;
 }
Пример #6
0
 public WiimoteDeviceException(WiimoteDeviceInfo device, string message)
     : base(message)
 {
     Device = device;
 }
Пример #7
0
 public WiimoteDeviceException(WiimoteDeviceInfo device)
 {
     Device = device;
 }
Пример #8
0
        private static bool BluetoothDiscoverLoop(CancellationToken token)
        {
            HashSet <BluetoothAddress> missingDevices = new HashSet <BluetoothAddress>(ConnectedAddresses);
            var       devices   = BluetoothDeviceInfo.EnumerateDevices(token, MatchBluetooth);
            Stopwatch watch     = Stopwatch.StartNew();
            bool      anyPaired = false;

            foreach (BluetoothDeviceInfo device in devices)
            {
                if (token.IsCancellationRequested)
                {
                    return(false);
                }
                Debug.WriteLine($"Took {watch.ElapsedMilliseconds}ms to enumerate bluetooth device");
                Wiimote wiimote = null;
                lock (wiimotes) {
                    wiimote = wiimotes.Find(wm => wm.Address == device.Address);
                }


                if (device.Connected)
                {
                    if (wiimote != null)
                    {
                        // Give Wiimote the updated Bluetooth device
                        wiimote.Device.Bluetooth = device;
                        missingDevices.Remove(device.Address);
                    }
                    else
                    {
                        HIDDeviceInfo hid = HIDDeviceInfo.GetDevice(device.Address);
                        // Drivers must not be installed yet, let's wait a bit
                        if (hid != null)
                        {
                            if (autoConnect)
                            {
                                WiimoteDeviceInfo wiimoteDevice = new WiimoteDeviceInfo(device, hid);
                                try {
                                    Connect(wiimoteDevice);
                                }
                                catch (Exception ex) {
                                    RaiseConnectionFailed(wiimoteDevice, ex);
                                }
                            }
                            else if (!RaiseDiscovered(device, hid))
                            {
                                return(false);
                            }
                        }

                        /*else if (device.PairDevice(token)) {
                         *      anyPaired = true;
                         * }
                         * else {
                         *      Debug.WriteLine("{device} pair failed!");
                         * }*/
                    }
                }
                else
                {
                    if (wiimote != null)
                    {
                        lock (wiimotes) {
                            wiimote.Dispose();
                            wiimotes.Remove(wiimote);
                            RaiseDisconnected(wiimote, DisconnectReason.ConnectionLost);
                        }
                    }
                    else if (device.IsDiscoverable() /*|| !device.Remembered*/)
                    {
                        if (pairOnDiscover)
                        {
                            if (device.PairDevice(token))
                            {
                                anyPaired = true;
                            }
                            else
                            {
                                Debug.WriteLine("{device} pair failed!");
                            }
                        }
                    }
                    else if (device.Remembered && unpairOnDisconnect)
                    {
                        device.RemoveDevice(token);
                    }
                }
                watch.Restart();
            }
            token.Sleep((anyPaired ? driverInstallDelay : 0) + 1000);
            return(true);
        }
Пример #9
0
 public WiimoteConnectionFailedEventArgs(WiimoteDeviceInfo device, Exception ex)
     : base(device)
 {
     Exception = ex;
 }
Пример #10
0
 public WiimoteDiscoveredEventArgs(WiimoteDeviceInfo device) : base(device)
 {
     AddDevice     = false;
     KeepSearching = true;
 }
Пример #11
0
 public WiimoteDeviceEventArgs(WiimoteDeviceInfo device)
 {
     Device = device;
 }
Пример #12
0
 private static void RaiseConnectionFailed(WiimoteDeviceInfo device, Exception ex)
 {
     Debug.WriteLine($"{device} Connection Failed: {ex.Message}");
     ConnectionFailed?.Invoke(null, new WiimoteConnectionFailedEventArgs(device, ex));
 }