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(); }
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); } }
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 OpenTK to drive multiple GPUs // Todo: allow OpenTK 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); }
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); }
void CloseJoystick(JoystickDevice <LinuxJoyDetails> js) { Libc.close(js.Details.FileDescriptor); js.Details.State = new JoystickState(); // clear joystick state js.Details.FileDescriptor = -1; // find and remove the joystick index from index_to_stick int key = -1; foreach (int i in index_to_stick.Keys) { if (sticks[index_to_stick[i]] == js) { key = i; break; } } if (index_to_stick.ContainsKey(key)) { index_to_stick.Remove(key); } }
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); }
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); }