예제 #1
0
        private void PollJoystick(LinuxJoystickDetails js)
        {
            unsafe
            {
                const int   EventCount = 32;
                InputEvent *events     = stackalloc InputEvent[EventCount];

                long length = 0;
                while (true)
                {
                    length = (long)Libc.read(js.FileDescriptor, (void *)events, (UIntPtr)(sizeof(InputEvent) * EventCount));
                    if (length <= 0)
                    {
                        break;
                    }

                    // Only mark the joystick as connected when we actually start receiving events.
                    // Otherwise, the Xbox wireless receiver will register 4 joysticks even if no
                    // actual joystick is connected to the receiver.
                    js.Caps.SetIsConnected(true);
                    js.State.SetIsConnected(true);

                    length /= sizeof(InputEvent);
                    for (int i = 0; i < length; i++)
                    {
                        InputEvent *e = events + i;
                        switch (e->Type)
                        {
                        case EvdevType.ABS:
                        {
                            AxisInfo axis;
                            if (js.AxisMap.TryGetValue((EvdevAxis)e->Code, out axis))
                            {
                                if (axis.Info.Maximum > axis.Info.Minimum)
                                {
                                    if (e->Code >= (int)EvdevAxis.HAT0X && e->Code <= (int)EvdevAxis.HAT3Y)
                                    {
                                        // We currently treat analogue hats as digital hats
                                        // to maintain compatibility with SDL2. We can do
                                        // better than this, however.
                                        int hat     = (e->Code - (int)EvdevAxis.HAT0X) / 2;
                                        int xy_axis = (int)axis.Axis & 0x1;
                                        int value   = e->Value.CompareTo(0) + 1;
                                        switch (xy_axis)
                                        {
                                        case 0:
                                            // X-axis
                                            js.hatStates[hat, 1] = value;
                                            break;

                                        case 1:
                                            // Y-axis
                                            js.hatStates[hat, 0] = value;
                                            break;
                                        }
                                        js.State.SetHat((JoystickHat)hat, TranslateHat(js.hatStates[hat, 0], js.hatStates[hat, 1]));
                                    }
                                    else
                                    {
                                        // This axis represents a regular axis or trigger
                                        js.State.SetAxis(
                                            axis.Axis,
                                            (short)Common.HidHelper.ScaleValue(e->Value,
                                                                               axis.Info.Minimum, axis.Info.Maximum,
                                                                               short.MinValue, short.MaxValue));
                                    }
                                }
                            }
                            break;
                        }

                        case EvdevType.KEY:
                        {
                            int button;
                            if (js.ButtonMap.TryGetValue((EvdevButton)e->Code, out button))
                            {
                                js.State.SetButton(button, e->Value != 0);
                            }
                            break;
                        }
                        }

                        // Create a serial number (total seconds in 24.8 fixed point format)
                        int sec    = (int)((long)e->Time.Seconds & 0xffffffff);
                        int msec   = (int)e->Time.MicroSeconds / 1000;
                        int packet =
                            ((sec & 0x00ffffff) << 24) |
                            Common.HidHelper.ScaleValue(msec, 0, 1000, 0, 255);
                        js.State.SetPacketNumber(packet);
                    }
                }
            }
        }
예제 #2
0
        private LinuxJoystickDetails OpenJoystick(string path)
        {
            LinuxJoystickDetails stick = null;

            int number = GetJoystickNumber(Path.GetFileName(path));

            if (number >= 0)
            {
                int fd = -1;
                try
                {
                    fd = Libc.open(path, OpenFlags.NonBlock);
                    if (fd == -1)
                    {
                        return(null);
                    }

                    unsafe
                    {
                        const int evsize   = Evdev.EventCount / 8;
                        const int axissize = Evdev.AxisCount / 8;
                        const int keysize  = Evdev.KeyCount / 8;
                        byte *    evbit    = stackalloc byte[evsize];
                        byte *    axisbit  = stackalloc byte[axissize];
                        byte *    keybit   = stackalloc byte[keysize];

                        string       name;
                        EvdevInputId id;

                        // Ensure this is a joystick device
                        bool is_valid = true;

                        is_valid &= Evdev.GetBit(fd, 0, evsize, new IntPtr(evbit)) >= 0;
                        is_valid &= Evdev.GetBit(fd, EvdevType.ABS, axissize, new IntPtr(axisbit)) >= 0;
                        is_valid &= Evdev.GetBit(fd, EvdevType.KEY, keysize, new IntPtr(keybit)) >= 0;

                        is_valid &= TestBit(evbit, (int)EvdevType.KEY);
                        is_valid &= TestBit(evbit, (int)EvdevType.ABS);
                        is_valid &= TestBit(axisbit, (int)EvdevAxis.X);
                        is_valid &= TestBit(axisbit, (int)EvdevAxis.Y);

                        is_valid &= Evdev.GetName(fd, out name) >= 0;
                        is_valid &= Evdev.GetId(fd, out id) >= 0;

                        if (is_valid)
                        {
                            stick = new LinuxJoystickDetails
                            {
                                FileDescriptor = fd,
                                PathIndex      = number,
                                State          = new JoystickState(),
                                Name           = name,
                                Guid           = CreateGuid(id, name),
                            };

                            int axes, buttons, hats;
                            QueryCapabilities(stick, axisbit, axissize, keybit, keysize,
                                              out axes, out buttons, out hats);

                            stick.Caps = new JoystickCapabilities(axes, buttons, hats, false);

                            // Poll the joystick once, to initialize its state
                            PollJoystick(stick);
                        }
                    }

                    Debug.Print("Found joystick on path {0}", path);
                }
                catch (Exception e)
                {
                    Debug.Print("Error opening joystick: {0}", e.ToString());
                }
                finally
                {
                    if (stick == null && fd != -1)
                    {
                        // Not a joystick
                        Libc.close(fd);
                    }
                }
            }

            return(stick);
        }