Exemplo n.º 1
0
        JoystickDevice <LinuxJoyDetails> OpenJoystick(string path)
        {
            JoystickDevice <LinuxJoyDetails> 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);
                    }

                    // Check joystick driver version (must be 1.0+)
                    int driver_version = 0x00000800;
                    Libc.ioctl(fd, JoystickIoctlCode.Version, ref driver_version);
                    if (driver_version < 0x00010000)
                    {
                        return(null);
                    }

                    // Get number of joystick axes
                    int axes = 0;
                    Libc.ioctl(fd, JoystickIoctlCode.Axes, ref axes);

                    // Get number of joystick buttons
                    int buttons = 0;
                    Libc.ioctl(fd, JoystickIoctlCode.Buttons, ref buttons);

                    stick = new JoystickDevice <LinuxJoyDetails>(number, axes, buttons);

                    StringBuilder sb = new StringBuilder(128);
                    Libc.ioctl(fd, JoystickIoctlCode.Name128, sb);
                    stick.Description = sb.ToString();

                    stick.Details.FileDescriptor = fd;
                    stick.Details.State.SetIsConnected(true);
                    stick.Details.Guid = CreateGuid(stick, path, number);

                    // Find the first disconnected joystick (if any)
                    int i;
                    for (i = 0; i < sticks.Count; i++)
                    {
                        if (!sticks[i].Details.State.IsConnected)
                        {
                            break;
                        }
                    }

                    // If no disconnected joystick exists, append a new slot
                    if (i == sticks.Count)
                    {
                        sticks.Add(stick);
                    }
                    else
                    {
                        sticks[i] = stick;
                    }

                    // Map player index to joystick
                    index_to_stick.Add(index_to_stick.Count, i);

                    Debug.Print("Found joystick on path {0}", path);
                }
                finally
                {
                    if (stick == null && fd != -1)
                    {
                        Libc.close(fd);
                    }
                }
            }

            return(stick);
        }
Exemplo n.º 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);
        }
Exemplo n.º 3
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;
                    }
                    //As we've received an event, this must be connected!
                    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);
                    }
                }
            }
        }
Exemplo n.º 4
0
        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.
                                        JoystickHat      hat = JoystickHat.Hat0 + (e->Code - (int)EvdevAxis.HAT0X) / 2;
                                        JoystickHatState pos = js.State.GetHat(hat);
                                        int xy_axis          = (int)axis.Axis & 0x1;
                                        switch (xy_axis)
                                        {
                                        case 0:
                                            // X-axis
                                            pos = TranslateHat(
                                                e->Value.CompareTo(0) + 1,
                                                pos.IsUp ? 0 : pos.IsDown ? 2 : 1);
                                            break;

                                        case 1:
                                            // Y-axis
                                            pos = TranslateHat(
                                                pos.IsLeft ? 0 : pos.IsRight ? 2 : 1,
                                                e->Value.CompareTo(0) + 1);
                                            break;
                                        }

                                        js.State.SetHat(hat, pos);
                                    }
                                    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:
                        {
                            JoystickButton 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);
                    }
                }
            }
        }