Cocoa: Fix can't exit legacy fullscreen mode

This commit is contained in:
UnknownShadow200 2022-02-01 22:51:44 +11:00
parent f55cd88cf3
commit c3d98109a9

View File

@ -11,6 +11,7 @@ static NSApplication* appHandle;
static NSWindow* winHandle; static NSWindow* winHandle;
static NSView* viewHandle; static NSView* viewHandle;
static cc_bool canCheckOcclusion; static cc_bool canCheckOcclusion;
static cc_bool legacy_fullscreen;
/*########################################################################################################################* /*########################################################################################################################*
*---------------------------------------------------Shared with Carbon----------------------------------------------------* *---------------------------------------------------Shared with Carbon----------------------------------------------------*
@ -309,9 +310,13 @@ void Window_SetTitle(const cc_string* title) {
int Window_GetWindowState(void) { int Window_GetWindowState(void) {
int flags; int flags;
// modern fullscreen using toggleFullScreen
flags = [winHandle styleMask]; flags = [winHandle styleMask];
if (flags & _NSFullScreenWindowMask) return WINDOW_STATE_FULLSCREEN; if (flags & _NSFullScreenWindowMask) return WINDOW_STATE_FULLSCREEN;
// legacy fullscreen using CGLSetFullscreen
if (legacy_fullscreen) return WINDOW_STATE_FULLSCREEN;
flags = [winHandle isMiniaturized]; flags = [winHandle isMiniaturized];
return flags ? WINDOW_STATE_MINIMISED : WINDOW_STATE_NORMAL; return flags ? WINDOW_STATE_MINIMISED : WINDOW_STATE_NORMAL;
} }
@ -600,17 +605,18 @@ void Window_CloseKeyboard(void) { }
static NSOpenGLContext* ctxHandle; static NSOpenGLContext* ctxHandle;
static NSOpenGLPixelFormat* MakePixelFormat(cc_bool fullscreen) { static NSOpenGLPixelFormat* MakePixelFormat(cc_bool fullscreen) {
NSOpenGLPixelFormatAttribute attribs[7] = { NSOpenGLPixelFormatAttribute attribs[] = {
NSOpenGLPFAColorSize, 0, NSOpenGLPFAColorSize, DisplayInfo.Depth,
NSOpenGLPFADepthSize, 24, NSOpenGLPFADepthSize, 24,
NSOpenGLPFADoubleBuffer, 0, 0 NSOpenGLPFADoubleBuffer,
fullscreen ? NSOpenGLPFAFullScreen : 0,
0
}; };
// NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
attribs[1] = DisplayInfo.Depth;
attribs[5] = fullscreen ? NSOpenGLPFAFullScreen : 0;
return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs]; return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
} }
void GLContext_Create(void) { void GLContext_Create(void) {
NSOpenGLPixelFormat* fmt; NSOpenGLPixelFormat* fmt;
fmt = MakePixelFormat(true); fmt = MakePixelFormat(true);
@ -663,37 +669,41 @@ void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) {
void GLContext_GetApiInfo(cc_string* info) { } void GLContext_GetApiInfo(cc_string* info) { }
static int SupportsNativeFullscreen(void) { static int SupportsModernFullscreen(void) {
return [winHandle respondsToSelector:@selector(toggleFullScreen:)]; return [winHandle respondsToSelector:@selector(toggleFullScreen:)];
} }
// TODO: Only works on 10.7+
cc_result Window_EnterFullscreen(void) { cc_result Window_EnterFullscreen(void) {
if (SupportsNativeFullscreen()) { if (SupportsModernFullscreen()) {
[winHandle toggleFullScreen:appHandle]; [winHandle toggleFullScreen:appHandle];
return 0; return 0;
} }
legacy_fullscreen = true;
[ctxHandle clearDrawable]; [ctxHandle clearDrawable];
// setFullScreen doesn't return an error code, which is unfortunate // setFullScreen doesn't return an error code, which is unfortunate
// because if setFullscreen fails, you're left with a blank window // because if setFullscreen fails, you're left with a blank window
// that's still rendering thousands of frames per second // that's still rendering thousands of frames per second
//[ctxHandle setFullScreen]; //[ctxHandle setFullScreen];
//return 0;
cc_result res = CGLSetFullScreen([ctxHandle CGLContextObj]); cc_result res = CGLSetFullScreen([ctxHandle CGLContextObj]);
// TODO doesn't seem to work on 10.7 or later??
//cc_result res = CGLSetFullScreenOnDisplay([ctxHandle CGLContextObj], CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()));
if (res) Window_ExitFullscreen(); if (res) Window_ExitFullscreen();
return res; return res;
} }
cc_result Window_ExitFullscreen(void) { cc_result Window_ExitFullscreen(void) {
if (SupportsNativeFullscreen()) { if (SupportsModernFullscreen()) {
[winHandle toggleFullScreen:appHandle]; [winHandle toggleFullScreen:appHandle];
return 0; return 0;
} }
legacy_fullscreen = false;
[ctxHandle clearDrawable]; [ctxHandle clearDrawable];
[ctxHandle setView:viewHandle]; [ctxHandle setView:viewHandle];
return 0; return 0;
} }
#endif #endif