[SDL2] Initialize input driver lazily (to avoid setting global SDL input filter callb...
authorVirgile Bello <virgile@siliconstudio.co.jp>
Mon, 6 Jun 2016 03:43:47 +0000 (12:43 +0900)
committerVirgile Bello <virgile@siliconstudio.co.jp>
Tue, 21 Jun 2016 04:51:43 +0000 (13:51 +0900)
Source/OpenTK/Platform/SDL2/Sdl2Factory.cs

index 6665424..7d94281 100644 (file)
@@ -34,7 +34,8 @@ namespace OpenTK.Platform.SDL2
 {
     class Sdl2Factory : PlatformFactoryBase
     {
-        readonly Sdl2InputDriver InputDriver = new Sdl2InputDriver();
+        private readonly object inputDriverLock = new object();
+        private Sdl2InputDriver inputDriver;
 
         /// <summary>
         /// Gets or sets a value indicating whether to use SDL2 fullscreen-desktop mode
@@ -85,17 +86,17 @@ namespace OpenTK.Platform.SDL2
 
         public override IKeyboardDriver2 CreateKeyboardDriver()
         {
-            return InputDriver.KeyboardDriver;
+            return GetInputDriver().KeyboardDriver;
         }
 
         public override IMouseDriver2 CreateMouseDriver()
         {
-            return InputDriver.MouseDriver;
+            return GetInputDriver().MouseDriver;
         }
 
         public override IJoystickDriver2 CreateJoystickDriver()
         {
-            return InputDriver.JoystickDriver;
+            return GetInputDriver().JoystickDriver;
         }
 
         #endregion
@@ -108,7 +109,11 @@ namespace OpenTK.Platform.SDL2
             {
                 if (manual)
                 {
-                    InputDriver.Dispose();
+                    if (inputDriver != null)
+                    {
+                        inputDriver.Dispose();
+                        inputDriver = null;
+                    }
                 }
 
                 base.Dispose(manual);
@@ -116,6 +121,21 @@ namespace OpenTK.Platform.SDL2
         }
 
         #endregion
+
+        private Sdl2InputDriver GetInputDriver()
+        {
+            if (inputDriver == null)
+            {
+                lock (inputDriverLock)
+                {
+                    // Check again inside the lock
+                    if (inputDriver == null)
+                        inputDriver = new Sdl2InputDriver();
+                }
+            }
+
+            return inputDriver;
+        }
     }
 }