コード例 #1
0
ファイル: LinuxInput.cs プロジェクト: zanzo420/opentk
        private static int OpenRestrictedHandler(IntPtr path, int flags, IntPtr data)
        {
            int fd = Libc.open(path, (OpenFlags)flags);

            Debug.Print("[Input] Opening '{0}' with flags {1}. fd:{2}",
                        Marshal.PtrToStringAnsi(path), (OpenFlags)flags, fd);

            if (fd >= 0)
            {
                Interlocked.Increment(ref DeviceFDCount);
            }

            return(fd);
        }
コード例 #2
0
        private static int SetupDisplay(string gpu, out IntPtr gbm_device, out IntPtr egl_display)
        {
            Debug.Print("[KMS] Attempting to use gpu '{0}'.", gpu);

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

            var fd = Libc.open(gpu, OpenFlags.ReadWrite | OpenFlags.CloseOnExec);

            if (fd < 0)
            {
                Debug.Print("[KMS] Failed to open gpu");
                return(fd);
            }

            Debug.Print("[KMS] GPU '{0}' opened as fd:{1}", gpu, fd);

            gbm_device = Gbm.CreateDevice(fd);
            if (gbm_device == IntPtr.Zero)
            {
                throw new NotSupportedException("[KMS] Failed to create GBM device");
            }

            Debug.Print("[KMS] GBM {0:x} created successfully; ", gbm_device);

            egl_display = Egl.Egl.GetDisplay(gbm_device);
            if (egl_display == IntPtr.Zero)
            {
                throw new NotSupportedException("[KMS] Failed to create EGL display");
            }

            Debug.Print("[KMS] EGL display {0:x} created successfully", egl_display);

            int major, minor;

            if (!Egl.Egl.Initialize(egl_display, out major, out minor))
            {
                var error = Egl.Egl.GetError();
                throw new NotSupportedException("[KMS] Failed to initialize EGL display. Error code: " + error);
            }

            Debug.Print("[KMS] EGL {0}.{1} initialized successfully on display {2:x}", major, minor, egl_display);

            return(fd);
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: LinuxJoystick.cs プロジェクト: albfan/Opentk
        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);
        }