Esempio n. 1
0
        private void CloseJoystick(LinuxJoystickDetails js)
        {
            Sticks.Remove(js.FileDescriptor);

            Libc.close(js.FileDescriptor);
            js.FileDescriptor = -1;
            js.State          = new JoystickState(); // clear joystick state
            js.Caps           = new JoystickCapabilities();
        }
Esempio n. 2
0
        private static void CloseRestrictedHandler(int fd, IntPtr data)
        {
            Debug.Print("[Input] Closing fd {0}", fd);
            int ret = Libc.close(fd);

            if (ret < 0)
            {
                Debug.Print("[Input] Failed to close fd {0}. Error: {1}", fd, ret);
            }
            else
            {
                Interlocked.Decrement(ref DeviceFDCount);
            }
        }
Esempio n. 3
0
        private static int CreateDisplay(out IntPtr gbm_device, out IntPtr egl_display)
        {
            // Query all GPUs until we find one that has a connected display.
            // This is necessary in multi-gpu systems, where only one GPU
            // can output a signal.
            // Todo: allow osuTK to drive multiple GPUs
            // Todo: allow osuTK to run on an offscreen GPU
            // Todo: allow the user to pick a GPU
            int fd = 0;

            gbm_device  = IntPtr.Zero;
            egl_display = IntPtr.Zero;

            var files = Directory.GetFiles(gpu_path);

            foreach (var gpu in files)
            {
                if (Path.GetFileName(gpu).StartsWith("card"))
                {
                    int test_fd = SetupDisplay(gpu, out gbm_device, out egl_display);
                    if (test_fd >= 0)
                    {
                        try
                        {
                            if (LinuxDisplayDriver.QueryDisplays(test_fd, null))
                            {
                                fd = test_fd;
                                break;
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.ToString());
                        }

                        Debug.Print("[KMS] GPU '{0}' is not connected, skipping.", gpu);
                        Libc.close(test_fd);
                    }
                }
            }

            if (fd == 0)
            {
                Debug.Print("[Error] No valid GPU found, bailing out.");
                throw new PlatformNotSupportedException();
            }

            return(fd);
        }
Esempio n. 4
0
        protected override void Dispose(bool manual)
        {
            if (egl_display != IntPtr.Zero)
            {
                Debug.Print("[KMS] Terminating EGL.");
                Egl.Terminate(egl_display);
                egl_display = IntPtr.Zero;
            }
            if (gbm_device != IntPtr.Zero)
            {
                Debug.Print("[KMS] Destroying GBM device.");
                Gbm.DestroyDevice(gbm_device);
                gbm_device = IntPtr.Zero;
            }
            if (_fd >= 0)
            {
                Debug.Print("[KMS] Closing GPU fd.");
                Libc.close(_fd);
            }

            base.Dispose(manual);
        }
Esempio n. 5
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);
        }