2015-04-26 01:31:02 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2016-01-10 03:41:52 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2015-04-26 01:31:02 +00:00
|
|
|
*
|
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-04-26 01:48:35 +00:00
|
|
|
/* Apple CGL context.
|
|
|
|
Based on http://fernlightning.com/doku.php?id=randd:xopengl.
|
|
|
|
*/
|
2015-04-26 01:31:02 +00:00
|
|
|
|
2015-09-16 03:53:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-06-02 06:48:07 +00:00
|
|
|
#include <ApplicationServices/ApplicationServices.h>
|
2015-04-26 01:59:55 +00:00
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
#include <OpenGL/CGLTypes.h>
|
|
|
|
#include <OpenGL/CGLCurrent.h>
|
|
|
|
#include <OpenGL/OpenGL.h>
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
|
2016-09-05 16:31:32 +00:00
|
|
|
#include "../../configuration.h"
|
2015-04-27 14:15:08 +00:00
|
|
|
#include "../../runloop.h"
|
|
|
|
#include "../../configuration.h"
|
2015-04-26 01:31:02 +00:00
|
|
|
#include "../video_context_driver.h"
|
|
|
|
|
|
|
|
typedef int CGSConnectionID;
|
|
|
|
typedef int CGSWindowID;
|
|
|
|
typedef int CGSSurfaceID;
|
|
|
|
|
2015-06-02 16:55:46 +00:00
|
|
|
typedef uint32_t _CGWindowID;
|
|
|
|
|
2015-06-26 13:44:19 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
/* Undocumented CGS */
|
|
|
|
extern CGSConnectionID CGSMainConnectionID(void);
|
2015-06-02 16:55:46 +00:00
|
|
|
extern CGError CGSAddSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID *sid);
|
|
|
|
extern CGError CGSSetSurfaceBounds(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, CGRect rect);
|
|
|
|
extern CGError CGSOrderSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, int a, int b);
|
2015-04-26 01:31:02 +00:00
|
|
|
|
|
|
|
/* Undocumented CGL */
|
2015-04-26 01:48:35 +00:00
|
|
|
extern CGLError CGLSetSurface(CGLContextObj gl, CGSConnectionID cid, CGSWindowID wid, CGSSurfaceID sid);
|
2015-04-26 01:31:02 +00:00
|
|
|
|
2015-06-26 13:44:19 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
typedef struct gfx_ctx_cgl_data
|
|
|
|
{
|
|
|
|
CGLContextObj glCtx;
|
|
|
|
CGDirectDisplayID displayID;
|
|
|
|
int width, height;
|
|
|
|
} gfx_ctx_cgl_data_t;
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_swap_interval(void *data, unsigned interval)
|
|
|
|
{
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
|
|
|
|
GLint params = interval;
|
|
|
|
|
|
|
|
CGLSetParameter(cgl->glCtx, kCGLCPSwapInterval, ¶ms);
|
|
|
|
}
|
|
|
|
|
2015-04-26 02:24:13 +00:00
|
|
|
static void gfx_ctx_cgl_get_video_size(void *data, unsigned *width, unsigned *height)
|
|
|
|
{
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
|
|
|
|
*width = cgl->width;
|
|
|
|
*height = cgl->height;
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
static void gfx_ctx_cgl_check_window(void *data, bool *quit,
|
|
|
|
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
|
|
|
|
{
|
2015-04-26 02:26:57 +00:00
|
|
|
unsigned new_width, new_height;
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
(void)frame_count;
|
2015-04-26 02:24:13 +00:00
|
|
|
|
|
|
|
*quit = false;
|
|
|
|
|
|
|
|
gfx_ctx_cgl_get_video_size(data, &new_width, &new_height);
|
|
|
|
if (new_width != *width || new_height != *height)
|
|
|
|
{
|
|
|
|
*width = new_width;
|
|
|
|
*height = new_height;
|
|
|
|
*resize = true;
|
|
|
|
}
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_swap_buffers(void *data)
|
|
|
|
{
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
|
|
|
|
|
|
|
|
CGLFlushDrawable(cgl->glCtx);
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:58:33 +00:00
|
|
|
static bool gfx_ctx_cgl_set_resize(void *data, unsigned width, unsigned height)
|
2015-04-26 01:31:02 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)width;
|
|
|
|
(void)height;
|
2016-01-06 23:58:33 +00:00
|
|
|
return false;
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_update_window_title(void *data)
|
|
|
|
{
|
2015-06-13 00:10:06 +00:00
|
|
|
char buf[128] = {0};
|
|
|
|
char buf_fps[128] = {0};
|
2015-04-27 14:15:08 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
(void)data;
|
2015-04-27 14:15:08 +00:00
|
|
|
|
|
|
|
video_monitor_get_fps(buf, sizeof(buf),
|
|
|
|
buf_fps, sizeof(buf_fps));
|
|
|
|
if (settings->fps_show)
|
2015-12-07 14:32:14 +00:00
|
|
|
runloop_msg_queue_push(buf_fps, 1, 1, false);
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool gfx_ctx_cgl_set_video_mode(void *data,
|
|
|
|
unsigned width, unsigned height,
|
|
|
|
bool fullscreen)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)width;
|
|
|
|
(void)height;
|
|
|
|
(void)fullscreen;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_destroy(void *data)
|
|
|
|
{
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
|
|
|
|
|
|
|
|
if (cgl->glCtx)
|
|
|
|
{
|
|
|
|
CGLSetCurrentContext(NULL);
|
|
|
|
CGLDestroyContext(cgl->glCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cgl->displayID)
|
|
|
|
CGDisplayRelease(cgl->displayID);
|
|
|
|
|
|
|
|
if (cgl)
|
|
|
|
free(cgl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_input_driver(void *data, const input_driver_t **input, void **input_data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)input;
|
|
|
|
(void)input_data;
|
|
|
|
}
|
|
|
|
|
2015-04-27 04:57:35 +00:00
|
|
|
static gfx_ctx_proc_t gfx_ctx_cgl_get_proc_address(const char *symbol_name)
|
|
|
|
{
|
2015-04-27 13:50:49 +00:00
|
|
|
CFURLRef bundle_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
|
|
|
|
CFSTR
|
|
|
|
("/System/Library/Frameworks/OpenGL.framework"),
|
|
|
|
kCFURLPOSIXPathStyle, true);
|
|
|
|
CFBundleRef opengl_bundle_ref = CFBundleCreate(kCFAllocatorDefault, bundle_url);
|
|
|
|
CFStringRef function = CFStringCreateWithCString(kCFAllocatorDefault, symbol_name,
|
|
|
|
kCFStringEncodingASCII);
|
|
|
|
gfx_ctx_proc_t ret = (gfx_ctx_proc_t)CFBundleGetFunctionPointerForName(
|
|
|
|
opengl_bundle_ref, function);
|
|
|
|
|
2015-04-27 13:52:37 +00:00
|
|
|
CFRelease(bundle_url);
|
2015-04-27 13:50:49 +00:00
|
|
|
CFRelease(function);
|
2015-04-27 13:52:37 +00:00
|
|
|
CFRelease(opengl_bundle_ref);
|
2015-04-27 13:50:49 +00:00
|
|
|
|
|
|
|
return ret;
|
2015-04-27 04:57:35 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
static bool gfx_ctx_cgl_has_focus(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gfx_ctx_cgl_suppress_screensaver(void *data, bool enable)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)enable;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gfx_ctx_cgl_has_windowed(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
2015-04-27 14:28:52 +00:00
|
|
|
return false;
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 01:48:35 +00:00
|
|
|
static bool gfx_ctx_cgl_bind_api(void *data, enum gfx_ctx_api api,
|
|
|
|
unsigned major, unsigned minor)
|
2015-04-26 01:31:02 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)api;
|
|
|
|
(void)major;
|
|
|
|
(void)minor;
|
|
|
|
|
2015-04-26 02:38:35 +00:00
|
|
|
return api == GFX_CTX_OPENGL_API;
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_show_mouse(void *data, bool state)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_bind_hw_render(void *data, bool enable)
|
|
|
|
{
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)data;
|
|
|
|
|
|
|
|
(void)enable;
|
|
|
|
|
|
|
|
CGLSetCurrentContext(cgl->glCtx);
|
|
|
|
|
|
|
|
/* TODO - needs to handle HW render context too */
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGLContextObj gfx_ctx_cgl_init_create(void)
|
|
|
|
{
|
2015-04-27 14:28:52 +00:00
|
|
|
GLint num, params = 1;
|
2015-04-26 01:31:02 +00:00
|
|
|
CGLPixelFormatObj pix;
|
|
|
|
CGLContextObj glCtx = NULL;
|
|
|
|
CGLPixelFormatAttribute attributes[] = {
|
|
|
|
kCGLPFAAccelerated,
|
2015-04-26 01:48:35 +00:00
|
|
|
kCGLPFADoubleBuffer,
|
2015-04-26 01:31:02 +00:00
|
|
|
(CGLPixelFormatAttribute)0
|
|
|
|
};
|
|
|
|
|
|
|
|
CGLChoosePixelFormat(attributes, &pix, &num);
|
|
|
|
CGLCreateContext(pix, NULL, &glCtx);
|
|
|
|
CGLDestroyPixelFormat(pix);
|
|
|
|
|
2015-04-27 14:28:52 +00:00
|
|
|
CGLSetParameter(glCtx, kCGLCPSwapInterval, ¶ms);
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
return glCtx;
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:48:35 +00:00
|
|
|
static CGSSurfaceID attach_gl_context_to_window(CGLContextObj glCtx,
|
|
|
|
CGSWindowID wid, int *width, int *height)
|
|
|
|
{
|
|
|
|
CFArrayRef wins;
|
|
|
|
CFDictionaryRef win, bnd;
|
|
|
|
GLint params = 0;
|
|
|
|
Float64 w = 0, h = 0;
|
|
|
|
CGSSurfaceID sid = 0;
|
|
|
|
CGSConnectionID cid = CGSMainConnectionID();
|
|
|
|
|
|
|
|
printf("cid:%d wid:%d\n", cid, wid);
|
|
|
|
|
|
|
|
/* determine window size */
|
2015-06-02 17:11:01 +00:00
|
|
|
/* FIXME/TODO - CGWindowListCopyWindowInfo was introduced on OSX 10.5,
|
|
|
|
* find alternative for lower versions. */
|
2015-04-26 01:48:35 +00:00
|
|
|
wins = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, wid); /* expect one result only */
|
2015-06-25 15:40:09 +00:00
|
|
|
win = (CFDictionaryRef)CFArrayGetValueAtIndex(wins, 0);
|
|
|
|
bnd = (CFDictionaryRef)CFDictionaryGetValue(win, kCGWindowBounds);
|
2015-06-25 15:44:11 +00:00
|
|
|
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue((CFDictionaryRef)bnd, CFSTR("Width")),
|
2015-04-26 01:48:35 +00:00
|
|
|
kCFNumberFloat64Type, &w);
|
2015-06-25 15:44:11 +00:00
|
|
|
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(bnd, CFSTR("Height")),
|
2015-04-26 01:48:35 +00:00
|
|
|
kCFNumberFloat64Type, &h);
|
|
|
|
CFRelease(wins);
|
|
|
|
|
|
|
|
/* create a surface. */
|
|
|
|
if(CGSAddSurface(cid, wid, &sid) != kCGErrorSuccess)
|
|
|
|
{
|
|
|
|
printf("ERR: no surface\n");
|
|
|
|
}
|
|
|
|
printf("sid:%d\n", sid);
|
|
|
|
|
|
|
|
/* set surface size, and order it frontmost */
|
|
|
|
if(CGSSetSurfaceBounds(cid, wid, sid, CGRectMake(0, 0, w, h)) != kCGErrorSuccess)
|
|
|
|
printf("ERR: cant set bounds\n");
|
|
|
|
if(CGSOrderSurface(cid, wid, sid, 1, 0) != kCGErrorSuccess)
|
|
|
|
printf("ERR: cant order front\n");
|
|
|
|
|
|
|
|
/* attach context to the surface */
|
|
|
|
if(CGLSetSurface(glCtx, cid, wid, sid) != kCGErrorSuccess)
|
|
|
|
{
|
|
|
|
printf("ERR: cant set surface\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check drawable */
|
|
|
|
CGLGetParameter(glCtx, kCGLCPHasDrawable, ¶ms);
|
|
|
|
if(params != 1)
|
|
|
|
{
|
|
|
|
printf("ERR: no drawable\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
*width = (int)w;
|
|
|
|
*height = (int)h;
|
|
|
|
|
|
|
|
return sid;
|
|
|
|
}
|
|
|
|
|
2015-12-08 15:18:57 +00:00
|
|
|
static void *gfx_ctx_cgl_init(void *video_driver)
|
2015-04-26 01:31:02 +00:00
|
|
|
{
|
|
|
|
CGError err;
|
|
|
|
gfx_ctx_cgl_data_t *cgl = (gfx_ctx_cgl_data_t*)calloc(1, sizeof(gfx_ctx_cgl_data_t));
|
|
|
|
|
|
|
|
if (!cgl)
|
2015-04-26 01:48:35 +00:00
|
|
|
goto error;
|
2015-04-26 01:31:02 +00:00
|
|
|
|
|
|
|
cgl->displayID = CGMainDisplayID();
|
|
|
|
|
|
|
|
err = CGDisplayCapture(cgl->displayID);
|
|
|
|
|
|
|
|
if (err != kCGErrorSuccess)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
cgl->glCtx = gfx_ctx_cgl_init_create();
|
|
|
|
|
|
|
|
if (!cgl->glCtx)
|
|
|
|
goto error;
|
|
|
|
|
2015-04-26 01:48:35 +00:00
|
|
|
attach_gl_context_to_window(cgl->glCtx,
|
|
|
|
CGShieldingWindowID(cgl->displayID), &cgl->width, &cgl->height);
|
|
|
|
|
2015-04-27 14:28:52 +00:00
|
|
|
printf("size:%dx%d\n", cgl->width, cgl->height);
|
|
|
|
|
2015-04-27 14:15:08 +00:00
|
|
|
CGLSetCurrentContext(cgl->glCtx);
|
|
|
|
|
2015-12-08 15:18:57 +00:00
|
|
|
return cgl;
|
2015-04-26 01:31:02 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
gfx_ctx_cgl_destroy(cgl);
|
|
|
|
|
2015-12-08 15:18:57 +00:00
|
|
|
return NULL;
|
2015-04-26 01:31:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 03:35:35 +00:00
|
|
|
static uint32_t gfx_ctx_cgl_get_flags(void *data)
|
2016-05-05 15:35:28 +00:00
|
|
|
{
|
|
|
|
uint32_t flags = 0;
|
|
|
|
BIT32_SET(flags, GFX_CTX_FLAGS_NONE);
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_ctx_cgl_set_flags(void *data, uint32_t flags)
|
2016-05-05 03:35:35 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:31:02 +00:00
|
|
|
const gfx_ctx_driver_t gfx_ctx_cgl = {
|
|
|
|
gfx_ctx_cgl_init,
|
|
|
|
gfx_ctx_cgl_destroy,
|
|
|
|
gfx_ctx_cgl_bind_api,
|
|
|
|
gfx_ctx_cgl_swap_interval,
|
|
|
|
gfx_ctx_cgl_set_video_mode,
|
|
|
|
gfx_ctx_cgl_get_video_size,
|
|
|
|
NULL, /* get_video_output_size */
|
|
|
|
NULL, /* get_video_output_prev */
|
|
|
|
NULL, /* get_video_output_next */
|
|
|
|
NULL, /* get_metrics */
|
|
|
|
NULL,
|
|
|
|
gfx_ctx_cgl_update_window_title,
|
|
|
|
gfx_ctx_cgl_check_window,
|
|
|
|
gfx_ctx_cgl_set_resize,
|
|
|
|
gfx_ctx_cgl_has_focus,
|
|
|
|
gfx_ctx_cgl_suppress_screensaver,
|
|
|
|
gfx_ctx_cgl_has_windowed,
|
|
|
|
gfx_ctx_cgl_swap_buffers,
|
|
|
|
gfx_ctx_cgl_input_driver,
|
2015-04-27 04:57:35 +00:00
|
|
|
gfx_ctx_cgl_get_proc_address,
|
2015-04-26 01:31:02 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
gfx_ctx_cgl_show_mouse,
|
|
|
|
"cgl",
|
2016-05-05 03:35:35 +00:00
|
|
|
gfx_ctx_cgl_get_flags,
|
2016-05-05 15:35:28 +00:00
|
|
|
gfx_ctx_cgl_set_flags,
|
2016-08-31 13:02:07 +00:00
|
|
|
gfx_ctx_cgl_bind_hw_render,
|
|
|
|
NULL,
|
|
|
|
NULL
|
2015-04-26 01:31:02 +00:00
|
|
|
};
|