[X11] Check before monitoring /dev/input

This avoids a crash on non-Linux systems that do not support /dev/input.
This commit is contained in:
thefiddler 2014-06-15 14:29:07 +02:00
parent 40f1668989
commit c657b3d11e

View file

@ -42,14 +42,14 @@ namespace OpenTK.Platform.X11
public JoystickState State; public JoystickState State;
} }
// Note: despite what the name says, this class is Linux-specific.
sealed class X11Joystick : IJoystickDriver2 sealed class X11Joystick : IJoystickDriver2
{ {
#region Fields #region Fields
readonly object sync = new object(); readonly object sync = new object();
readonly FileSystemWatcher watcher = new FileSystemWatcher(JoystickPath); readonly FileSystemWatcher watcher = new FileSystemWatcher();
readonly FileSystemWatcher watcher_legacy = new FileSystemWatcher(JoystickPathLegacy);
readonly Dictionary<int, int> index_to_stick = new Dictionary<int, int>(); readonly Dictionary<int, int> index_to_stick = new Dictionary<int, int>();
List<JoystickDevice<X11JoyDetails>> sticks = new List<JoystickDevice<X11JoyDetails>>(); List<JoystickDevice<X11JoyDetails>> sticks = new List<JoystickDevice<X11JoyDetails>>();
@ -62,43 +62,38 @@ namespace OpenTK.Platform.X11
public X11Joystick() public X11Joystick()
{ {
watcher.Created += JoystickAdded; string path =
watcher.Deleted += JoystickRemoved; Directory.Exists(JoystickPath) ? JoystickPath :
watcher.EnableRaisingEvents = true; Directory.Exists(JoystickPathLegacy) ? JoystickPathLegacy :
String.Empty;
watcher_legacy.Created += JoystickAdded; if (!String.IsNullOrEmpty(path))
watcher_legacy.Deleted += JoystickRemoved; {
watcher_legacy.EnableRaisingEvents = true; watcher.Path = path;
OpenJoysticks(); watcher.Created += JoystickAdded;
watcher.Deleted += JoystickRemoved;
watcher.EnableRaisingEvents = true;
OpenJoysticks(path);
}
} }
#endregion #endregion
#region Private Members #region Private Members
void OpenJoysticks() void OpenJoysticks(string path)
{ {
lock (sync) lock (sync)
{ {
foreach (string file in Directory.GetFiles(JoystickPath)) foreach (string file in Directory.GetFiles(path))
{ {
JoystickDevice<X11JoyDetails> stick = OpenJoystick(file); JoystickDevice<X11JoyDetails> stick = OpenJoystick(file);
if (stick != null) if (stick != null)
{ {
//stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})", //stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})",
//number, stick.Axis.Count, stick.Button.Count, JoystickPath); //number, stick.Axis.Count, stick.Button.Count, path);
sticks.Add(stick);
}
}
foreach (string file in Directory.GetFiles(JoystickPathLegacy))
{
JoystickDevice<X11JoyDetails> stick = OpenJoystick(file);
if (stick != null)
{
//stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons, {3}{0})",
//number, stick.Axis.Count, stick.Button.Count, JoystickPathLegacy);
sticks.Add(stick); sticks.Add(stick);
} }
} }
@ -447,6 +442,7 @@ namespace OpenTK.Platform.X11
{ {
} }
watcher.Dispose();
foreach (JoystickDevice<X11JoyDetails> js in sticks) foreach (JoystickDevice<X11JoyDetails> js in sticks)
{ {
CloseJoystick(js); CloseJoystick(js);