示例#1
0
        private MouseDevice GetMouse(IntPtr device)
        {
            int         id    = GetId(device);
            MouseDevice mouse = MouseCandidates.FromHardwareId(id);

            if (mouse != null)
            {
                Mice.Add(id, mouse);
            }
            else
            {
                Debug.Print("[Input] Mouse {0} does not exist in device list.", id);
            }
            return(mouse);
        }
示例#2
0
        private KeyboardDevice GetKeyboard(IntPtr device)
        {
            int            id       = GetId(device);
            KeyboardDevice keyboard = KeyboardCandidates.FromHardwareId(id);

            if (keyboard != null)
            {
                Keyboards.Add(id, keyboard);
            }
            else
            {
                Debug.Print("[Input] Keyboard {0} does not exist in device list.", id);
            }
            return(keyboard);
        }
示例#3
0
 void JoystickRemoved(object sender, FileSystemEventArgs e)
 {
     lock (sync)
     {
         string file   = Path.GetFileName(e.FullPath);
         int    number = GetJoystickNumber(file);
         if (number != -1)
         {
             var stick = Sticks.FromHardwareId(number);
             if (stick != null)
             {
                 CloseJoystick(stick);
             }
         }
     }
 }
示例#4
0
        private void ProcessEvents()
        {
            if (!File.Exists(InputDevicePath))
            {
                //BSD may not have the Linux ProcFS, no use running a pointless loop
                return;
            }

            while (!disposed)
            {
                /*
                 * Rather hacky filesystemwatcher equivilant, as it doesn't work on ProcFS
                 * As the thing is psuedo-files, we can't use the last modified date either
                 * We have to load the thing into a stream and hash. At least it's only a few bytes.....
                 *
                 * Note: We could possibly override the Mono filesystem watcher backend, but this is a global variable only
                 * and so may break user code
                 */
                Thread.Sleep(750); //750ms is the Mono default for the file hashing version of FileSystemWatcher backend
                List <int> connectedDevices;

                using (FileStream stream = new FileStream(InputDevicePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    string newHash = BitConverter.ToString(Hasher.ComputeHash(stream));
                    if (newHash == procHash)
                    {
                        //Hash of /proc/bus/input/devices has not changed, hence no new devices added / removed
                        continue;
                    }
                    stream.Seek(0, 0);
                    connectedDevices = GetConnectedDevices(stream);
                    procHash         = newHash;
                    OpenJoysticks(EvdevPath);
                }

                List <int> xboxPadCandidates = new List <int>();

                foreach (LinuxJoystickDetails stick in Sticks)
                {
                    if (connectedDevices.Contains(stick.PathIndex))
                    {
                        if (stick.Caps.AxisCount == 6 && stick.Caps.ButtonCount == 11 && stick.Caps.HatCount == 2)
                        {
                            xboxPadCandidates.Add(stick.PathIndex);
                        }
                        else
                        {
                            stick.Caps.SetIsConnected(true);
                            stick.State.SetIsConnected(true);
                        }
                    }
                    else
                    {
                        stick.State.SetIsConnected(false);
                        stick.Caps.SetIsConnected(false);
                    }
                }

                if (xboxPadCandidates.Count < 4)
                {
                    /*
                     * The XBox wireless reciever generates 4 sticks which may not have anything connected to them
                     * If there are less than 4, this can't be this (or it's been fixed evdev side in the meantime)
                     * so let's set them as connected
                     */
                    foreach (int idx in xboxPadCandidates)
                    {
                        LinuxJoystickDetails stick = Sticks.FromHardwareId(idx);
                        if (stick != null)
                        {
                            stick.Caps.SetIsConnected(true);
                            stick.State.SetIsConnected(true);
                        }
                    }
                }
            }
        }
示例#5
0
        public void RefreshDevices()
        {
            // Mark all devices as disconnected. We will check which of those
            // are connected below.
            foreach (var device in Devices)
            {
                device.SetConnected(false);
            }

            // Discover joystick devices
            int xinput_device_count = 0;

            foreach (RawInputDeviceList dev in WinRawInput.GetDeviceList())
            {
                // Skip non-joystick devices
                if (dev.Type != RawInputDeviceType.HID)
                {
                    continue;
                }

                // We use the device handle as the hardware id.
                // This works, but the handle will change whenever the
                // device is unplugged/replugged. We compensate for this
                // by checking device GUIDs, below.
                // Note: we cannot use the GUID as the hardware id,
                // because it is costly to query (and we need to query
                // that every time we process a device event.)
                IntPtr handle      = dev.Device;
                bool   is_xinput   = IsXInput(handle);
                Guid   guid        = GetDeviceGuid(handle);
                long   hardware_id = handle.ToInt64();

                Device device = Devices.FromHardwareId(hardware_id);
                if (device != null)
                {
                    // We have already opened this device, mark it as connected
                    device.SetConnected(true);
                }
                else
                {
                    device = new Device(handle, guid, is_xinput,
                                        is_xinput ? xinput_device_count++ : 0);

                    // This is a new device, query its capabilities and add it
                    // to the device list
                    if (!QueryDeviceCaps(device) && !is_xinput)
                    {
                        continue;
                    }
                    device.SetConnected(true);

                    // Check if a disconnected device with identical GUID already exists.
                    // If so, replace that device with this instance.
                    Device match = null;
                    foreach (Device candidate in Devices)
                    {
                        if (candidate.GetGuid() == guid && !candidate.GetCapabilities().IsConnected)
                        {
                            match = candidate;
                        }
                    }
                    if (match != null)
                    {
                        Devices.Remove(match.Handle.ToInt64());
                    }

                    Devices.Add(hardware_id, device);

                    Debug.Print("[{0}] Connected joystick {1} ({2})",
                                GetType().Name, device.GetGuid(), device.GetCapabilities());
                }
            }
        }