Merge pull request #5276 from ligfx/macosheadless

Add headless support on macOS
This commit is contained in:
shuffle2 2017-06-05 20:49:02 -07:00 committed by GitHub
commit 192fec50b9
6 changed files with 79 additions and 56 deletions

View File

@ -396,9 +396,13 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif()
if(ENABLE_HEADLESS)
message(STATUS "Enabling Headless! Disabling GUI, force enabling EGL!")
if(APPLE)
message(STATUS "Enabling Headless! Disabling GUI.")
else()
message(STATUS "Enabling Headless! Disabling GUI, force enabling EGL!")
set(USE_EGL 1)
endif()
set(USE_X11 0)
set(USE_EGL 1)
set(DISABLE_WX 1)
set(ENABLE_QT2 0)
add_definitions(-DUSE_HEADLESS)

View File

@ -15,10 +15,6 @@ struct NSView;
class cInterfaceAGL : public cInterfaceBase
{
private:
NSView* cocoaWin;
NSOpenGLContext* cocoaCtx;
public:
void Swap() override;
bool Create(void* window_handle, bool core) override;
@ -27,4 +23,8 @@ public:
void Shutdown() override;
void Update() override;
void SwapInterval(int interval) override;
private:
NSView* m_view;
NSOpenGLContext* m_context;
};

View File

@ -5,31 +5,54 @@
#include "Common/GL/GLInterface/AGL.h"
#include "Common/Logging/Log.h"
static bool UpdateCachedDimensions(NSView* view, u32* width, u32* height)
{
NSWindow* window = [view window];
NSSize size = [view frame].size;
float scale = [window backingScaleFactor];
size.width *= scale;
size.height *= scale;
if (*width == size.width && *height == size.height)
return false;
*width = size.width;
*height = size.height;
return true;
}
static bool AttachContextToView(NSOpenGLContext* context, NSView* view, u32* width, u32* height)
{
// Enable high-resolution display support.
[view setWantsBestResolutionOpenGLSurface:YES];
NSWindow* window = [view window];
if (window == nil)
{
ERROR_LOG(VIDEO, "failed to get NSWindow");
return false;
}
(void)UpdateCachedDimensions(view, width, height);
[window makeFirstResponder:view];
[context setView:view];
[window makeKeyAndOrderFront:nil];
return true;
}
void cInterfaceAGL::Swap()
{
[cocoaCtx flushBuffer];
[m_context flushBuffer];
}
// Create rendering window.
// Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
bool cInterfaceAGL::Create(void* window_handle, bool core)
{
cocoaWin = reinterpret_cast<NSView*>(window_handle);
NSSize size = [cocoaWin frame].size;
// Enable high-resolution display support.
[cocoaWin setWantsBestResolutionOpenGLSurface:YES];
NSWindow* window = [cocoaWin window];
float scale = [window backingScaleFactor];
size.width *= scale;
size.height *= scale;
// Control window size and picture scaling
s_backbuffer_width = size.width;
s_backbuffer_height = size.height;
NSOpenGLPixelFormatAttribute attr[] = {NSOpenGLPFADoubleBuffer, NSOpenGLPFAOpenGLProfile,
core ? NSOpenGLProfileVersion3_2Core :
NSOpenGLProfileVersionLegacy,
@ -41,30 +64,24 @@ bool cInterfaceAGL::Create(void* window_handle, bool core)
return false;
}
cocoaCtx = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
m_context = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
[fmt release];
if (cocoaCtx == nil)
if (m_context == nil)
{
ERROR_LOG(VIDEO, "failed to create context");
return false;
}
if (cocoaWin == nil)
{
ERROR_LOG(VIDEO, "failed to create window");
return false;
}
if (!window_handle)
return true;
[window makeFirstResponder:cocoaWin];
[cocoaCtx setView:cocoaWin];
[window makeKeyAndOrderFront:nil];
return true;
m_view = static_cast<NSView*>(window_handle);
return AttachContextToView(m_context, m_view, &s_backbuffer_width, &s_backbuffer_height);
}
bool cInterfaceAGL::MakeCurrent()
{
[cocoaCtx makeCurrentContext];
[m_context makeCurrentContext];
return true;
}
@ -77,30 +94,21 @@ bool cInterfaceAGL::ClearCurrent()
// Close backend
void cInterfaceAGL::Shutdown()
{
[cocoaCtx clearDrawable];
[cocoaCtx release];
cocoaCtx = nil;
[m_context clearDrawable];
[m_context release];
m_context = nil;
}
void cInterfaceAGL::Update()
{
NSWindow* window = [cocoaWin window];
NSSize size = [cocoaWin frame].size;
float scale = [window backingScaleFactor];
size.width *= scale;
size.height *= scale;
if (s_backbuffer_width == size.width && s_backbuffer_height == size.height)
if (!m_view)
return;
s_backbuffer_width = size.width;
s_backbuffer_height = size.height;
[cocoaCtx update];
if (UpdateCachedDimensions(m_view, &s_backbuffer_width, &s_backbuffer_height))
[m_context update];
}
void cInterfaceAGL::SwapInterval(int interval)
{
[cocoaCtx setValues:(GLint*)&interval forParameter:NSOpenGLCPSwapInterval];
[m_context setValues:static_cast<GLint*>(&interval) forParameter:NSOpenGLCPSwapInterval];
}

View File

@ -363,7 +363,7 @@ class PlatformX11 : public Platform
static Platform* GetPlatform()
{
#if defined(USE_EGL) && defined(USE_HEADLESS)
#if defined(USE_HEADLESS)
return new Platform();
#elif HAVE_X11
return new PlatformX11();

View File

@ -173,13 +173,21 @@ static void DeviceMatchingCallback(void* inContext, IOReturn inResult, void* inS
// Add a device if it's of a type we want
if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard))
g_controller_interface.AddDevice(std::make_shared<Keyboard>(inIOHIDDeviceRef, name, g_window));
{
if (g_window)
g_controller_interface.AddDevice(
std::make_shared<Keyboard>(inIOHIDDeviceRef, name, g_window));
}
#if 0
else if (IOHIDDeviceConformsTo(inIOHIDDeviceRef, kHIDPage_GenericDesktop, kHIDUsage_GD_Mouse))
{
g_controller_interface.AddDevice(new Mouse(inIOHIDDeviceRef, name));
}
#endif
else
{
g_controller_interface.AddDevice(std::make_shared<Joystick>(inIOHIDDeviceRef, name));
}
NOTICE_LOG(SERIALINTERFACE, "Added device: %s", name.c_str());
g_controller_interface.InvokeHotplugCallbacks();
@ -187,12 +195,12 @@ static void DeviceMatchingCallback(void* inContext, IOReturn inResult, void* inS
void Init(void* window)
{
g_window = window;
HIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (!HIDManager)
ERROR_LOG(SERIALINTERFACE, "Failed to create HID Manager reference");
g_window = window;
IOHIDManagerSetDeviceMatching(HIDManager, nullptr);
if (IOHIDManagerOpen(HIDManager, kIOHIDOptionsTypeNone) != kIOReturnSuccess)
ERROR_LOG(SERIALINTERFACE, "Failed to open HID Manager");

View File

@ -12,6 +12,9 @@ namespace Quartz
{
void PopulateDevices(void* window)
{
if (!window)
return;
g_controller_interface.AddDevice(std::make_shared<KeyboardAndMouse>(window));
}