Exemple #1
0
        private void HandlePointerButton(MouseDevice mouse, PointerEvent e)
        {
            if (mouse != null)
            {
                mouse.State.SetIsConnected(true);

                MouseButton button = Evdev.GetMouseButton(e.Button);
                ButtonState state  = e.ButtonState;
                mouse.State[(MouseButton)button] = state == ButtonState.Pressed;
            }
        }
Exemple #2
0
        private unsafe static void QueryCapabilities(LinuxJoystickDetails stick,
                                                     byte *axisbit, int axisbytes,
                                                     byte *keybit, int keybytes,
                                                     out int axes, out int buttons, out int hats)
        {
            // Note: since we are trying to be compatible with the SDL2 gamepad database,
            // we have to match SDL2 behavior bug-for-bug. This means:
            // HAT0-HAT3 are all reported as hats (even if the docs say that HAT1 and HAT2 can be analogue triggers)
            // DPAD buttons are reported as buttons, not as hats (unlike Windows and Mac OS X)

            axes = buttons = hats = 0;
            for (EvdevAxis axis = 0; axis < EvdevAxis.CNT && (int)axis < axisbytes * 8; axis++)
            {
                InputAbsInfo info;
                bool         is_valid = true;
                is_valid &= TestBit(axisbit, (int)axis);
                is_valid &= Evdev.GetAbs(stick.FileDescriptor, axis, out info) >= 0;
                if (is_valid)
                {
                    if (axis >= EvdevAxis.HAT0X && axis <= EvdevAxis.HAT3Y)
                    {
                        // Analogue hat
                        stick.AxisMap.Add(axis, new AxisInfo
                        {
                            Axis = (int)(JoystickHat)hats++,
                            Info = info
                        });
                    }
                    else
                    {
                        // Regular axis
                        stick.AxisMap.Add(axis, new AxisInfo
                        {
                            Axis = axes++,
                            Info = info
                        });
                    }
                }
            }

            for (EvdevButton button = 0; button < EvdevButton.Last && (int)button < keybytes * 8; button++)
            {
                if (TestBit(keybit, (int)button))
                {
                    stick.ButtonMap.Add(button, buttons++);
                }
            }
        }
Exemple #3
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);
        }