2009-06-02 17:49:39 +02:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
#endregion
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-10-01 22:01:27 +02:00
|
|
|
using System.Diagnostics;
|
2009-02-22 11:43:35 +01:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform
|
|
|
|
{
|
|
|
|
using Graphics;
|
2014-01-06 14:49:05 +01:00
|
|
|
using Input;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-07-24 23:52:01 +02:00
|
|
|
sealed class Factory : IPlatformFactory
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-06-02 17:49:39 +02:00
|
|
|
#region Fields
|
|
|
|
|
2013-10-01 22:01:27 +02:00
|
|
|
bool disposed;
|
2009-08-13 13:52:51 +02:00
|
|
|
static IPlatformFactory default_implementation, embedded_implementation;
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2009-06-02 17:49:39 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
2014-07-23 09:03:18 +02:00
|
|
|
static Factory()
|
|
|
|
{
|
|
|
|
Toolkit.Init();
|
|
|
|
}
|
|
|
|
|
2014-07-22 14:29:20 +02:00
|
|
|
public Factory()
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2013-11-12 20:34:53 +01:00
|
|
|
// Ensure we are correctly initialized.
|
|
|
|
Toolkit.Init();
|
|
|
|
|
|
|
|
// Create regular platform backend
|
|
|
|
if (Configuration.RunningOnSdl2) Default = new SDL2.Sdl2Factory();
|
2014-06-15 14:32:43 +02:00
|
|
|
else if (Configuration.RunningOnX11) Default = new X11.X11Factory();
|
2014-07-18 09:19:37 +02:00
|
|
|
else if (Configuration.RunningOnLinux) Default = new Linux.LinuxFactory();
|
2013-09-27 14:41:37 +02:00
|
|
|
else if (Configuration.RunningOnWindows) Default = new Windows.WinFactory();
|
2009-08-13 13:52:51 +02:00
|
|
|
else if (Configuration.RunningOnMacOS) Default = new MacOS.MacOSFactory();
|
|
|
|
else Default = new UnsupportedPlatform();
|
|
|
|
|
2013-11-12 20:34:53 +01:00
|
|
|
// Create embedded platform backend for EGL / OpenGL ES.
|
|
|
|
// Todo: we could probably delay this until the embedded
|
|
|
|
// factory is actually accessed. This might improve startup
|
|
|
|
// times slightly.
|
|
|
|
if (Configuration.RunningOnSdl2)
|
2013-10-03 11:17:21 +02:00
|
|
|
{
|
2013-11-12 20:34:53 +01:00
|
|
|
// SDL supports both EGL and desktop backends
|
|
|
|
// using the same API.
|
2013-10-04 09:57:43 +02:00
|
|
|
Embedded = Default;
|
2013-10-03 11:17:21 +02:00
|
|
|
}
|
|
|
|
else if (Egl.Egl.IsSupported)
|
2009-08-13 13:52:51 +02:00
|
|
|
{
|
2014-06-24 19:27:38 +02:00
|
|
|
if (Configuration.RunningOnLinux) Embedded = Default;
|
|
|
|
else if (Configuration.RunningOnX11) Embedded = new Egl.EglX11PlatformFactory();
|
2014-06-15 14:32:43 +02:00
|
|
|
else if (Configuration.RunningOnWindows) Embedded = new Egl.EglWinPlatformFactory();
|
2009-08-13 13:52:51 +02:00
|
|
|
else if (Configuration.RunningOnMacOS) Embedded = new Egl.EglMacPlatformFactory();
|
|
|
|
else Embedded = new UnsupportedPlatform();
|
|
|
|
}
|
2013-10-03 11:17:21 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Embedded = new UnsupportedPlatform();
|
|
|
|
}
|
2009-08-13 13:52:51 +02:00
|
|
|
|
|
|
|
if (Default is UnsupportedPlatform && !(Embedded is UnsupportedPlatform))
|
|
|
|
Default = Embedded;
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2009-06-02 17:49:39 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public Members
|
|
|
|
|
|
|
|
public static IPlatformFactory Default
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
get { return default_implementation; }
|
|
|
|
private set { default_implementation = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IPlatformFactory Embedded
|
|
|
|
{
|
|
|
|
get { return embedded_implementation; }
|
|
|
|
private set { embedded_implementation = value; }
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2009-06-02 17:49:39 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IPlatformFactory Members
|
|
|
|
|
|
|
|
public INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title,
|
|
|
|
GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
|
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
return default_implementation.CreateNativeWindow(x, y, width, height, title, mode, options, device);
|
2009-06-02 17:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public IDisplayDeviceDriver CreateDisplayDeviceDriver()
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
return default_implementation.CreateDisplayDeviceDriver();
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2009-06-02 17:49:39 +02:00
|
|
|
public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
return default_implementation.CreateGLContext(mode, window, shareContext, directRendering, major, minor, flags);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2009-11-04 17:49:56 +01:00
|
|
|
public IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
|
|
|
|
{
|
|
|
|
return default_implementation.CreateGLContext(handle, window, shareContext, directRendering, major, minor, flags);
|
|
|
|
}
|
|
|
|
|
2009-06-02 17:49:39 +02:00
|
|
|
public GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
|
2009-05-30 21:27:52 +02:00
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
return default_implementation.CreateGetCurrentGraphicsContext();
|
2009-05-30 21:27:52 +02:00
|
|
|
}
|
2009-06-02 17:49:39 +02:00
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public IKeyboardDriver2 CreateKeyboardDriver()
|
2009-07-19 19:49:51 +02:00
|
|
|
{
|
2009-08-13 13:52:51 +02:00
|
|
|
return default_implementation.CreateKeyboardDriver();
|
2009-07-19 19:49:51 +02:00
|
|
|
}
|
2009-02-22 11:43:35 +01:00
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public IMouseDriver2 CreateMouseDriver()
|
2010-10-20 16:58:38 +02:00
|
|
|
{
|
|
|
|
return default_implementation.CreateMouseDriver();
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public IGamePadDriver CreateGamePadDriver()
|
2013-01-17 02:56:31 +01:00
|
|
|
{
|
|
|
|
return default_implementation.CreateGamePadDriver();
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public IJoystickDriver2 CreateJoystickDriver()
|
2013-12-24 03:16:32 +01:00
|
|
|
{
|
|
|
|
return default_implementation.CreateJoystickDriver();
|
|
|
|
}
|
|
|
|
|
2014-07-21 16:39:58 +02:00
|
|
|
[Obsolete]
|
2014-01-06 14:49:05 +01:00
|
|
|
public IJoystickDriver CreateLegacyJoystickDriver()
|
|
|
|
{
|
2014-07-21 16:49:24 +02:00
|
|
|
#pragma warning disable 612,618
|
2014-01-06 14:49:05 +01:00
|
|
|
return default_implementation.CreateLegacyJoystickDriver();
|
2014-07-21 16:49:24 +02:00
|
|
|
#pragma warning restore 612,618
|
2014-01-06 14:49:05 +01:00
|
|
|
}
|
|
|
|
|
2014-07-23 14:34:52 +02:00
|
|
|
public void RegisterResource(IDisposable resource)
|
|
|
|
{
|
|
|
|
default_implementation.RegisterResource(resource);
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
class UnsupportedPlatform : PlatformFactoryBase
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-07-19 19:49:51 +02:00
|
|
|
#region Fields
|
2013-10-01 22:01:27 +02:00
|
|
|
|
2009-07-19 19:49:51 +02:00
|
|
|
static readonly string error_string = "Please, refer to http://www.opentk.com for more information.";
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2009-02-22 11:43:35 +01:00
|
|
|
#region IPlatformFactory Members
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice device)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-07-19 19:49:51 +02:00
|
|
|
throw new PlatformNotSupportedException(error_string);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
|
2009-11-04 17:49:56 +01:00
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException(error_string);
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
|
2009-07-24 23:52:01 +02:00
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException(error_string);
|
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IGraphicsContext CreateGLContext(ContextHandle handle, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
|
2009-02-22 11:43:35 +01:00
|
|
|
{
|
2009-07-19 19:49:51 +02:00
|
|
|
throw new PlatformNotSupportedException(error_string);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
|
2009-05-30 21:27:52 +02:00
|
|
|
{
|
2009-07-19 19:49:51 +02:00
|
|
|
throw new PlatformNotSupportedException(error_string);
|
2009-05-30 21:27:52 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IKeyboardDriver2 CreateKeyboardDriver()
|
2009-07-19 19:49:51 +02:00
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException(error_string);
|
|
|
|
}
|
2010-10-20 16:58:38 +02:00
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IMouseDriver2 CreateMouseDriver()
|
2010-10-20 16:58:38 +02:00
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException(error_string);
|
|
|
|
}
|
2013-01-17 02:56:31 +01:00
|
|
|
|
2014-01-06 14:49:05 +01:00
|
|
|
public override IJoystickDriver2 CreateJoystickDriver()
|
2013-01-17 02:56:31 +01:00
|
|
|
{
|
|
|
|
throw new PlatformNotSupportedException(error_string);
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:01:27 +02:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (manual)
|
|
|
|
{
|
|
|
|
Default.Dispose();
|
|
|
|
if (Embedded != Default)
|
|
|
|
{
|
|
|
|
Embedded.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType());
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Factory()
|
|
|
|
{
|
|
|
|
Dispose(false);
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
2009-06-02 17:49:39 +02:00
|
|
|
|
|
|
|
#endregion
|
2009-02-22 11:43:35 +01:00
|
|
|
}
|
|
|
|
}
|