1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-26 20:28:49 -05:00
glfw/lib/carbon/carbon_window.c

1276 lines
45 KiB
C
Raw Normal View History

2010-09-07 11:34:51 -04:00
//========================================================================
// GLFW - An OpenGL framework
// Platform: Carbon/AGL/CGL
2010-09-07 11:41:26 -04:00
// API Version: 3.0
2010-09-07 11:34:51 -04:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2003 Keith Bauer
// Copyright (c) 2003-2010 Camilla Berglund <elmindreda@elmindreda.org>
// Copyright (c) 2006-2007 Robin Leffmann
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
2010-09-08 20:29:39 -04:00
#define _glfwTestModifier(modifierMask, glfwKey) \
if (changed & modifierMask) \
2010-09-07 11:34:51 -04:00
{ \
2010-09-08 20:29:39 -04:00
_glfwInputKey(glfwKey, (modifiers & modifierMask ? GLFW_PRESS : GLFW_RELEASE)); \
2010-09-07 11:34:51 -04:00
}
//************************************************************************
//**** GLFW internal functions ****
//************************************************************************
2010-09-08 20:29:39 -04:00
static void handleMacModifierChange(UInt32 modifiers)
2010-09-07 11:34:51 -04:00
{
UInt32 changed = modifiers ^ _glfwInput.Modifiers;
// The right *key variants below never actually occur
// There also isn't even a broken right command key constant
2010-09-08 20:29:39 -04:00
_glfwTestModifier(shiftKey, GLFW_KEY_LSHIFT);
_glfwTestModifier(rightShiftKey, GLFW_KEY_RSHIFT);
_glfwTestModifier(controlKey, GLFW_KEY_LCTRL);
_glfwTestModifier(rightControlKey, GLFW_KEY_RCTRL);
_glfwTestModifier(optionKey, GLFW_KEY_LALT);
_glfwTestModifier(rightOptionKey, GLFW_KEY_RALT);
_glfwTestModifier(cmdKey, GLFW_KEY_LSUPER);
2010-09-07 11:34:51 -04:00
_glfwInput.Modifiers = modifiers;
}
2010-09-08 20:29:39 -04:00
static void handleMacKeyChange(UInt32 keyCode, int action)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
switch (keyCode)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
case MAC_KEY_ENTER: _glfwInputKey(GLFW_KEY_ENTER, action); break;
case MAC_KEY_RETURN: _glfwInputKey(GLFW_KEY_KP_ENTER, action); break;
case MAC_KEY_ESC: _glfwInputKey(GLFW_KEY_ESC, action); break;
case MAC_KEY_F1: _glfwInputKey(GLFW_KEY_F1, action); break;
case MAC_KEY_F2: _glfwInputKey(GLFW_KEY_F2, action); break;
case MAC_KEY_F3: _glfwInputKey(GLFW_KEY_F3, action); break;
case MAC_KEY_F4: _glfwInputKey(GLFW_KEY_F4, action); break;
case MAC_KEY_F5: _glfwInputKey(GLFW_KEY_F5, action); break;
case MAC_KEY_F6: _glfwInputKey(GLFW_KEY_F6, action); break;
case MAC_KEY_F7: _glfwInputKey(GLFW_KEY_F7, action); break;
case MAC_KEY_F8: _glfwInputKey(GLFW_KEY_F8, action); break;
case MAC_KEY_F9: _glfwInputKey(GLFW_KEY_F9, action); break;
case MAC_KEY_F10: _glfwInputKey(GLFW_KEY_F10, action); break;
case MAC_KEY_F11: _glfwInputKey(GLFW_KEY_F11, action); break;
case MAC_KEY_F12: _glfwInputKey(GLFW_KEY_F12, action); break;
case MAC_KEY_F13: _glfwInputKey(GLFW_KEY_F13, action); break;
case MAC_KEY_F14: _glfwInputKey(GLFW_KEY_F14, action); break;
case MAC_KEY_F15: _glfwInputKey(GLFW_KEY_F15, action); break;
case MAC_KEY_UP: _glfwInputKey(GLFW_KEY_UP, action); break;
case MAC_KEY_DOWN: _glfwInputKey(GLFW_KEY_DOWN, action); break;
case MAC_KEY_LEFT: _glfwInputKey(GLFW_KEY_LEFT, action); break;
case MAC_KEY_RIGHT: _glfwInputKey(GLFW_KEY_RIGHT, action); break;
case MAC_KEY_TAB: _glfwInputKey(GLFW_KEY_TAB, action); break;
case MAC_KEY_BACKSPACE: _glfwInputKey(GLFW_KEY_BACKSPACE, action); break;
case MAC_KEY_HELP: _glfwInputKey(GLFW_KEY_INSERT, action); break;
case MAC_KEY_DEL: _glfwInputKey(GLFW_KEY_DEL, action); break;
case MAC_KEY_PAGEUP: _glfwInputKey(GLFW_KEY_PAGEUP, action); break;
case MAC_KEY_PAGEDOWN: _glfwInputKey(GLFW_KEY_PAGEDOWN, action); break;
case MAC_KEY_HOME: _glfwInputKey(GLFW_KEY_HOME, action); break;
case MAC_KEY_END: _glfwInputKey(GLFW_KEY_END, action); break;
case MAC_KEY_KP_0: _glfwInputKey(GLFW_KEY_KP_0, action); break;
case MAC_KEY_KP_1: _glfwInputKey(GLFW_KEY_KP_1, action); break;
case MAC_KEY_KP_2: _glfwInputKey(GLFW_KEY_KP_2, action); break;
case MAC_KEY_KP_3: _glfwInputKey(GLFW_KEY_KP_3, action); break;
case MAC_KEY_KP_4: _glfwInputKey(GLFW_KEY_KP_4, action); break;
case MAC_KEY_KP_5: _glfwInputKey(GLFW_KEY_KP_5, action); break;
case MAC_KEY_KP_6: _glfwInputKey(GLFW_KEY_KP_6, action); break;
case MAC_KEY_KP_7: _glfwInputKey(GLFW_KEY_KP_7, action); break;
case MAC_KEY_KP_8: _glfwInputKey(GLFW_KEY_KP_8, action); break;
case MAC_KEY_KP_9: _glfwInputKey(GLFW_KEY_KP_9, action); break;
case MAC_KEY_KP_DIVIDE: _glfwInputKey(GLFW_KEY_KP_DIVIDE, action); break;
case MAC_KEY_KP_MULTIPLY: _glfwInputKey(GLFW_KEY_KP_MULTIPLY, action); break;
case MAC_KEY_KP_SUBTRACT: _glfwInputKey(GLFW_KEY_KP_SUBTRACT, action); break;
case MAC_KEY_KP_ADD: _glfwInputKey(GLFW_KEY_KP_ADD, action); break;
case MAC_KEY_KP_DECIMAL: _glfwInputKey(GLFW_KEY_KP_DECIMAL, action); break;
case MAC_KEY_KP_EQUAL: _glfwInputKey(GLFW_KEY_KP_EQUAL, action); break;
case MAC_KEY_KP_ENTER: _glfwInputKey(GLFW_KEY_KP_ENTER, action); break;
case MAC_KEY_NUMLOCK: _glfwInputKey(GLFW_KEY_KP_NUM_LOCK, action); break;
2010-09-07 11:34:51 -04:00
default:
{
2010-09-08 20:29:39 -04:00
extern void* KCHRPtr;
2010-09-07 11:34:51 -04:00
UInt32 state = 0;
2010-09-08 20:29:39 -04:00
char charCode = (char)KeyTranslate(KCHRPtr, keyCode, &state);
UppercaseText(&charCode, 1, smSystemScript);
_glfwInputKey((unsigned char)charCode, action);
2010-09-07 11:34:51 -04:00
}
break;
}
}
// The set of event class/kind combinations supported by keyEventHandler
// This is used by installEventHandlers below
static const EventTypeSpec GLFW_KEY_EVENT_TYPES[] =
{
{ kEventClassKeyboard, kEventRawKeyDown },
{ kEventClassKeyboard, kEventRawKeyUp },
{ kEventClassKeyboard, kEventRawKeyRepeat },
{ kEventClassKeyboard, kEventRawKeyModifiersChanged }
};
2010-09-08 20:29:39 -04:00
static OSStatus keyEventHandler(EventHandlerCallRef handlerCallRef,
EventRef event,
void* userData)
2010-09-07 11:34:51 -04:00
{
UInt32 keyCode;
short int keyChar;
UInt32 modifiers;
2010-09-08 20:29:39 -04:00
switch (GetEventKind(event))
2010-09-07 11:34:51 -04:00
{
case kEventRawKeyRepeat:
case kEventRawKeyDown:
{
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamKeyCode,
typeUInt32,
NULL,
sizeof(UInt32),
NULL,
&keyCode) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
handleMacKeyChange(keyCode, GLFW_PRESS);
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamKeyUnicodes,
typeUnicodeText,
NULL,
sizeof(keyChar),
NULL,
&keyChar) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_glfwInputChar(keyChar, GLFW_PRESS);
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:29:39 -04:00
2010-09-07 11:34:51 -04:00
return noErr;
}
case kEventRawKeyUp:
{
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamKeyCode,
typeUInt32,
NULL,
sizeof(UInt32),
NULL,
&keyCode) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
handleMacKeyChange(keyCode, GLFW_RELEASE);
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamKeyUnicodes,
typeUnicodeText,
NULL,
sizeof(keyChar),
NULL,
&keyChar) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_glfwInputChar(keyChar, GLFW_RELEASE);
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:29:39 -04:00
2010-09-07 11:34:51 -04:00
return noErr;
}
case kEventRawKeyModifiersChanged:
{
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamKeyModifiers,
typeUInt32,
NULL,
sizeof(UInt32),
NULL,
&modifiers) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
handleMacModifierChange(modifiers);
2010-09-07 11:34:51 -04:00
return noErr;
}
}
break;
}
return eventNotHandledErr;
}
// The set of event class/kind combinations supported by mouseEventHandler
// This is used by installEventHandlers below
static const EventTypeSpec GLFW_MOUSE_EVENT_TYPES[] =
{
{ kEventClassMouse, kEventMouseDown },
{ kEventClassMouse, kEventMouseUp },
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged },
{ kEventClassMouse, kEventMouseWheelMoved },
};
2010-09-08 20:29:39 -04:00
static OSStatus mouseEventHandler(EventHandlerCallRef handlerCallRef,
EventRef event,
void* userData)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
switch (GetEventKind(event))
2010-09-07 11:34:51 -04:00
{
case kEventMouseDown:
{
WindowRef window;
EventRecord oldStyleMacEvent;
2010-09-08 20:29:39 -04:00
ConvertEventRefToEventRecord(event, &oldStyleMacEvent);
if (FindWindow(oldStyleMacEvent.where, &window) == inMenuBar)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
MenuSelect(oldStyleMacEvent.where);
2010-09-07 11:34:51 -04:00
HiliteMenu(0);
return noErr;
}
else
{
EventMouseButton button;
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamMouseButton,
typeMouseButton,
NULL,
sizeof(EventMouseButton),
NULL,
&button) == noErr)
2010-09-07 11:34:51 -04:00
{
button -= kEventMouseButtonPrimary;
2010-09-08 20:29:39 -04:00
if (button <= GLFW_MOUSE_BUTTON_LAST)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_glfwInputMouseClick(button + GLFW_MOUSE_BUTTON_LEFT,
GLFW_PRESS);
2010-09-07 11:34:51 -04:00
}
return noErr;
}
}
break;
}
case kEventMouseUp:
{
EventMouseButton button;
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamMouseButton,
typeMouseButton,
NULL,
sizeof(EventMouseButton),
NULL,
&button) == noErr)
2010-09-07 11:34:51 -04:00
{
button -= kEventMouseButtonPrimary;
2010-09-08 20:29:39 -04:00
if (button <= GLFW_MOUSE_BUTTON_LAST)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_glfwInputMouseClick(button + GLFW_MOUSE_BUTTON_LEFT,
GLFW_RELEASE);
2010-09-07 11:34:51 -04:00
}
return noErr;
}
break;
}
case kEventMouseMoved:
case kEventMouseDragged:
{
HIPoint mouseLocation;
2010-09-08 20:29:39 -04:00
if (_glfwWin.mouseLock)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamMouseDelta,
typeHIPoint,
NULL,
sizeof(HIPoint),
NULL,
&mouseLocation) != noErr)
2010-09-07 11:34:51 -04:00
{
break;
}
_glfwInput.MousePosX += mouseLocation.x;
_glfwInput.MousePosY += mouseLocation.y;
}
else
{
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamMouseLocation,
typeHIPoint,
NULL,
sizeof(HIPoint),
NULL,
&mouseLocation) != noErr)
2010-09-07 11:34:51 -04:00
{
break;
}
_glfwInput.MousePosX = mouseLocation.x;
_glfwInput.MousePosY = mouseLocation.y;
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
Rect content;
2010-09-08 20:29:39 -04:00
GetWindowBounds(_glfwWin.window,
kWindowContentRgn,
&content);
2010-09-07 11:34:51 -04:00
_glfwInput.MousePosX -= content.left;
_glfwInput.MousePosY -= content.top;
}
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.mousePosCallback)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_glfwWin.mousePosCallback(_glfwInput.MousePosX,
_glfwInput.MousePosY);
2010-09-07 11:34:51 -04:00
}
break;
}
case kEventMouseWheelMoved:
{
EventMouseWheelAxis axis;
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamMouseWheelAxis,
typeMouseWheelAxis,
NULL,
sizeof(EventMouseWheelAxis),
NULL,
&axis) == noErr)
2010-09-07 11:34:51 -04:00
{
long wheelDelta;
2010-09-08 20:29:39 -04:00
if (axis == kEventMouseWheelAxisY &&
GetEventParameter(event,
kEventParamMouseWheelDelta,
typeLongInteger,
NULL,
sizeof(long),
NULL,
&wheelDelta) == noErr)
2010-09-07 11:34:51 -04:00
{
_glfwInput.WheelPos += wheelDelta;
2010-09-08 20:29:39 -04:00
if (_glfwWin.mouseWheelCallback)
_glfwWin.mouseWheelCallback(_glfwInput.WheelPos);
2010-09-07 11:34:51 -04:00
return noErr;
}
}
break;
}
}
return eventNotHandledErr;
}
// The set of event class/kind combinations supported by commandHandler
// This is used by installEventHandlers below
static const EventTypeSpec GLFW_COMMAND_EVENT_TYPES[] =
{
{ kEventClassCommand, kEventCommandProcess }
};
2010-09-08 20:29:39 -04:00
static OSStatus commandHandler(EventHandlerCallRef handlerCallRef,
EventRef event,
void* userData)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.sysKeysDisabled)
2010-09-07 11:34:51 -04:00
{
// TODO: Give adequate UI feedback that this is the case
return eventNotHandledErr;
}
HICommand command;
2010-09-08 20:29:39 -04:00
if (GetEventParameter(event,
kEventParamDirectObject,
typeHICommand,
NULL,
sizeof(HICommand),
NULL,
&command) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
switch (command.commandID)
2010-09-07 11:34:51 -04:00
{
case kHICommandClose:
case kHICommandQuit:
{
// Check if the program wants us to close the window
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowCloseCallback)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowCloseCallback())
2010-09-07 11:34:51 -04:00
glfwCloseWindow();
}
else
glfwCloseWindow();
2010-09-08 20:29:39 -04:00
2010-09-07 11:34:51 -04:00
return noErr;
}
}
}
return eventNotHandledErr;
}
// The set of event class/kind combinations supported by windowEventHandler
// This is used by installEventHandlers below
static const EventTypeSpec GLFW_WINDOW_EVENT_TYPES[] =
{
{ kEventClassWindow, kEventWindowBoundsChanged },
{ kEventClassWindow, kEventWindowClose },
{ kEventClassWindow, kEventWindowDrawContent },
{ kEventClassWindow, kEventWindowActivated },
{ kEventClassWindow, kEventWindowDeactivated },
};
2010-09-08 20:29:39 -04:00
static OSStatus windowEventHandler(EventHandlerCallRef handlerCallRef,
EventRef event,
void* userData)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
switch (GetEventKind(event))
2010-09-07 11:34:51 -04:00
{
case kEventWindowBoundsChanged:
{
WindowRef window;
2010-09-08 20:29:39 -04:00
GetEventParameter(event,
kEventParamDirectObject,
typeWindowRef,
NULL,
sizeof(WindowRef),
NULL,
&window);
2010-09-07 11:34:51 -04:00
Rect rect;
2010-09-08 20:29:39 -04:00
GetWindowPortBounds(window, &rect);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (_glfwWin.width != rect.right ||
_glfwWin.height != rect.bottom)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
aglUpdateContext(_glfwWin.aglContext);
2010-09-07 11:34:51 -04:00
_glfwWin.width = rect.right;
_glfwWin.height = rect.bottom;
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowSizeCallback)
_glfwWin.windowSizeCallback(_glfwWin.width, _glfwWin.height);
2010-09-07 11:34:51 -04:00
// Emulate (force) content invalidation
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowRefreshCallback)
2010-09-07 11:34:51 -04:00
_glfwWin.windowRefreshCallback();
}
break;
}
case kEventWindowClose:
{
// Check if the client wants us to close the window
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowCloseCallback)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowCloseCallback())
glfwCloseWindow();
2010-09-07 11:34:51 -04:00
}
else
glfwCloseWindow();
2010-09-08 20:29:39 -04:00
2010-09-07 11:34:51 -04:00
return noErr;
}
case kEventWindowDrawContent:
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowRefreshCallback)
2010-09-07 11:34:51 -04:00
_glfwWin.windowRefreshCallback();
2010-09-08 20:29:39 -04:00
2010-09-07 11:34:51 -04:00
break;
}
case kEventWindowActivated:
{
_glfwWin.active = GL_TRUE;
break;
}
case kEventWindowDeactivated:
{
_glfwWin.active = GL_FALSE;
_glfwInputDeactivation();
break;
}
}
return eventNotHandledErr;
}
2010-09-08 20:29:39 -04:00
static int installEventHandlers(void)
2010-09-07 11:34:51 -04:00
{
OSStatus error;
2010-09-08 20:29:39 -04:00
_glfwWin.mouseUPP = NewEventHandlerUPP(mouseEventHandler);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
error = InstallEventHandler(GetApplicationEventTarget(),
_glfwWin.mouseUPP,
GetEventTypeCount(GLFW_MOUSE_EVENT_TYPES),
GLFW_MOUSE_EVENT_TYPES,
NULL,
NULL);
if (error != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to install Carbon application mouse event handler\n");
2010-09-07 11:34:51 -04:00
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
_glfwWin.commandUPP = NewEventHandlerUPP(commandHandler);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
error = InstallEventHandler(GetApplicationEventTarget(),
_glfwWin.commandUPP,
GetEventTypeCount(GLFW_COMMAND_EVENT_TYPES),
GLFW_COMMAND_EVENT_TYPES,
NULL,
NULL);
if (error != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to install Carbon application command event handler\n");
2010-09-07 11:34:51 -04:00
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
_glfwWin.keyboardUPP = NewEventHandlerUPP(keyEventHandler);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
error = InstallEventHandler(GetApplicationEventTarget(),
_glfwWin.keyboardUPP,
GetEventTypeCount(GLFW_KEY_EVENT_TYPES),
GLFW_KEY_EVENT_TYPES,
NULL,
NULL);
if (error != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to install Carbon application key event handler\n");
2010-09-07 11:34:51 -04:00
return GL_FALSE;
}
return GL_TRUE;
}
//************************************************************************
//**** Platform implementation functions ****
//************************************************************************
2010-09-08 20:29:39 -04:00
#define _setAGLAttribute(aglAttributeName, AGLparameter) \
if (AGLparameter != 0) \
2010-09-07 11:34:51 -04:00
{ \
AGLpixelFormatAttributes[numAGLAttrs++] = aglAttributeName; \
AGLpixelFormatAttributes[numAGLAttrs++] = AGLparameter; \
}
2010-09-08 20:29:39 -04:00
#define _setCGLAttribute(cglAttributeName, CGLparameter) \
if (CGLparameter != 0) \
2010-09-07 11:34:51 -04:00
{ \
2010-09-08 20:33:14 -04:00
CGLpixelFormatAttributes[numCGLAttrs++] = cglAttributeName; \
CGLpixelFormatAttributes[numCGLAttrs++] = CGLparameter; \
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Here is where the window is created, and
// the OpenGL rendering context is created
//========================================================================
2010-09-08 20:29:39 -04:00
int _glfwPlatformOpenWindow(int width, int height,
const _GLFWwndconfig* wndconfig,
const _GLFWfbconfig* fbconfig)
2010-09-07 11:34:51 -04:00
{
OSStatus error;
unsigned int windowAttributes;
ProcessSerialNumber psn;
// TODO: Break up this function!
_glfwWin.windowUPP = NULL;
_glfwWin.mouseUPP = NULL;
_glfwWin.keyboardUPP = NULL;
_glfwWin.commandUPP = NULL;
_glfwWin.window = NULL;
_glfwWin.aglContext = NULL;
_glfwWin.aglPixelFormat = NULL;
_glfwWin.cglContext = NULL;
_glfwWin.cglPixelFormat = NULL;
_glfwWin.refreshRate = wndconfig->refreshRate;
// Fail if OpenGL 3.0 or above was requested
2010-09-08 20:29:39 -04:00
if (wndconfig->glMajor > 2)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "OpenGL 3.0+ is not yet supported on Mac OS X\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
if (_glfwLibrary.Unbundled)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (GetCurrentProcess(&psn) != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to get the process serial number\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
if (TransformProcessType(&psn, kProcessTransformToForegroundApplication) != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to become a foreground application\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
if (wndconfig->mode == GLFW_FULLSCREEN)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (SetFrontProcess(&psn) != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to become the front process\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
}
}
2010-09-08 20:29:39 -04:00
if (!installEventHandlers())
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to install Carbon application event handlers\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformTerminate();
return GL_FALSE;
}
// Windowed or fullscreen; AGL or CGL? Quite the mess...
// AGL appears to be the only choice for attaching OpenGL contexts to
// Carbon windows, but it leaves the user no control over fullscreen
// mode stretching. Solution: AGL for windowed, CGL for fullscreen.
2010-09-08 20:29:39 -04:00
if (wndconfig->mode == GLFW_WINDOW)
2010-09-07 11:34:51 -04:00
{
// create AGL pixel format attribute list
GLint AGLpixelFormatAttributes[256];
int numAGLAttrs = 0;
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_RGBA;
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_DOUBLEBUFFER;
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_CLOSEST_POLICY;
2010-09-08 20:29:39 -04:00
if (fbconfig->stereo)
2010-09-07 11:34:51 -04:00
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_STEREO;
2010-09-08 20:29:39 -04:00
_setAGLAttribute(AGL_AUX_BUFFERS, fbconfig->auxBuffers);
_setAGLAttribute(AGL_RED_SIZE, fbconfig->redBits);
_setAGLAttribute(AGL_GREEN_SIZE, fbconfig->greenBits);
_setAGLAttribute(AGL_BLUE_SIZE, fbconfig->blueBits);
_setAGLAttribute(AGL_ALPHA_SIZE, fbconfig->alphaBits);
_setAGLAttribute(AGL_DEPTH_SIZE, fbconfig->depthBits);
_setAGLAttribute(AGL_STENCIL_SIZE, fbconfig->stencilBits);
_setAGLAttribute(AGL_ACCUM_RED_SIZE, fbconfig->accumRedBits);
_setAGLAttribute(AGL_ACCUM_GREEN_SIZE, fbconfig->accumGreenBits);
_setAGLAttribute(AGL_ACCUM_BLUE_SIZE, fbconfig->accumBlueBits);
_setAGLAttribute(AGL_ACCUM_ALPHA_SIZE, fbconfig->accumAlphaBits);
if (fbconfig->samples > 1)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_setAGLAttribute(AGL_SAMPLE_BUFFERS_ARB, 1);
_setAGLAttribute(AGL_SAMPLES_ARB, fbconfig->samples);
2010-09-07 11:34:51 -04:00
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NO_RECOVERY;
}
AGLpixelFormatAttributes[numAGLAttrs++] = AGL_NONE;
// create pixel format descriptor
AGLDevice mainMonitor = GetMainDevice();
2010-09-08 20:29:39 -04:00
_glfwWin.aglPixelFormat = aglChoosePixelFormat(&mainMonitor,
1,
AGLpixelFormatAttributes);
if (_glfwWin.aglPixelFormat == NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to choose AGL pixel format: %s\n",
aglErrorString(aglGetError()));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// create AGL context
2010-09-08 20:29:39 -04:00
_glfwWin.aglContext = aglCreateContext(_glfwWin.aglPixelFormat, NULL);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (_glfwWin.aglContext == NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to create AGL context: %s\n",
aglErrorString(aglGetError()));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// create window
Rect windowContentBounds;
windowContentBounds.left = 0;
windowContentBounds.top = 0;
windowContentBounds.right = width;
windowContentBounds.bottom = height;
2010-09-08 20:29:39 -04:00
windowAttributes = (kWindowCloseBoxAttribute |
kWindowCollapseBoxAttribute |
kWindowStandardHandlerAttribute);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (wndconfig->windowNoResize)
2010-09-07 11:34:51 -04:00
windowAttributes |= kWindowLiveResizeAttribute;
else
{
2010-09-08 20:29:39 -04:00
windowAttributes |= (kWindowFullZoomAttribute |
kWindowResizableAttribute);
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:29:39 -04:00
error = CreateNewWindow(kDocumentWindowClass,
windowAttributes,
&windowContentBounds,
&(_glfwWin.window));
if ((error != noErr) || (_glfwWin.window == NULL))
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to create Carbon window\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
_glfwWin.windowUPP = NewEventHandlerUPP(windowEventHandler);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
error = InstallWindowEventHandler(_glfwWin.window,
_glfwWin.windowUPP,
GetEventTypeCount(GLFW_WINDOW_EVENT_TYPES),
GLFW_WINDOW_EVENT_TYPES,
NULL,
NULL);
if (error != noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to install Carbon window event handler\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Don't care if we fail here
2010-09-08 20:29:39 -04:00
SetWindowTitleWithCFString(_glfwWin.window, CFSTR("GLFW Window"));
RepositionWindow(_glfwWin.window,
NULL,
kWindowCenterOnMainScreen);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (!aglSetDrawable(_glfwWin.aglContext,
GetWindowPort(_glfwWin.window)))
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to set the AGL context as the Carbon window drawable: %s\n",
aglErrorString(aglGetError()));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Make OpenGL context current
2010-09-08 20:29:39 -04:00
if (!aglSetCurrentContext(_glfwWin.aglContext))
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to make AGL context current: %s\n",
aglErrorString(aglGetError()));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
ShowWindow(_glfwWin.window);
2010-09-07 11:34:51 -04:00
}
else
{
CGDisplayErr cgErr;
CGLError cglErr;
CFDictionaryRef optimalMode;
GLint numCGLvs = 0;
CGLPixelFormatAttribute CGLpixelFormatAttributes[64];
int numCGLAttrs = 0;
// variables for enumerating color depths
GLint rgbColorDepth;
// CGL pixel format attributes
2010-09-08 20:29:39 -04:00
_setCGLAttribute(kCGLPFADisplayMask,
CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay));
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (fbconfig->stereo)
2010-09-08 20:33:14 -04:00
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFAStereo;
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (fbconfig->samples > 1)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_setCGLAttribute(kCGLPFASamples, (CGLPixelFormatAttribute)fbconfig->samples);
_setCGLAttribute(kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1);
2010-09-08 20:33:14 -04:00
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFANoRecovery;
2010-09-07 11:34:51 -04:00
}
2010-09-08 20:33:14 -04:00
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFAFullScreen;
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFADoubleBuffer;
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFAAccelerated;
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFANoRecovery;
CGLpixelFormatAttributes[numCGLAttrs++] = kCGLPFAMinimumPolicy;
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
_setCGLAttribute(kCGLPFAAccumSize,
(CGLPixelFormatAttribute)( fbconfig->accumRedBits \
+ fbconfig->accumGreenBits \
+ fbconfig->accumBlueBits \
+ fbconfig->accumAlphaBits));
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
_setCGLAttribute(kCGLPFAAlphaSize, (CGLPixelFormatAttribute)fbconfig->alphaBits);
_setCGLAttribute(kCGLPFADepthSize, (CGLPixelFormatAttribute)fbconfig->depthBits);
_setCGLAttribute(kCGLPFAStencilSize, (CGLPixelFormatAttribute)fbconfig->stencilBits);
_setCGLAttribute(kCGLPFAAuxBuffers, (CGLPixelFormatAttribute)fbconfig->auxBuffers);
2010-09-07 11:34:51 -04:00
2010-09-08 20:33:14 -04:00
CGLpixelFormatAttributes[numCGLAttrs++] = (CGLPixelFormatAttribute)NULL;
2010-09-07 11:34:51 -04:00
// create a suitable pixel format with above attributes..
2010-09-08 20:29:39 -04:00
cglErr = CGLChoosePixelFormat(CGLpixelFormatAttributes,
&_glfwWin.cglPixelFormat,
&numCGLvs);
if (cglErr != kCGLNoError)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to choose CGL pixel format: %s\n",
CGLErrorString(cglErr));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// ..and create a rendering context using that pixel format
2010-09-08 20:29:39 -04:00
cglErr = CGLCreateContext(_glfwWin.cglPixelFormat, NULL, &_glfwWin.cglContext);
if (cglErr != kCGLNoError)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to create CGL context: %s\n",
CGLErrorString(cglErr));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// enumerate depth of RGB channels - unlike AGL, CGL works with
// a single parameter reflecting the full depth of the frame buffer
2010-09-08 20:29:39 -04:00
CGLDescribePixelFormat(_glfwWin.cglPixelFormat,
0,
kCGLPFAColorSize,
&rgbColorDepth);
2010-09-07 11:34:51 -04:00
// capture the display for our application
cgErr = CGCaptureAllDisplays();
2010-09-08 20:29:39 -04:00
if (cgErr != kCGErrorSuccess)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to capture Core Graphics displays\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// find closest matching NON-STRETCHED display mode..
optimalMode = CGDisplayBestModeForParametersAndRefreshRateWithProperty(
kCGDirectMainDisplay,
rgbColorDepth,
width,
height,
wndconfig->refreshRate,
NULL,
2010-09-08 20:29:39 -04:00
NULL);
if (optimalMode == NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to retrieve Core Graphics display mode\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// ..and switch to that mode
2010-09-08 20:29:39 -04:00
cgErr = CGDisplaySwitchToMode(kCGDirectMainDisplay, optimalMode);
if (cgErr != kCGErrorSuccess)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr, "Failed to switch to Core Graphics display mode\n");
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// switch to our OpenGL context, and bring it up fullscreen
2010-09-08 20:29:39 -04:00
cglErr = CGLSetCurrentContext(_glfwWin.cglContext);
if (cglErr != kCGLNoError)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to make CGL context current: %s\n",
CGLErrorString(cglErr));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
2010-09-08 20:29:39 -04:00
cglErr = CGLSetFullScreen(_glfwWin.cglContext);
if (cglErr != kCGLNoError)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
fprintf(stderr,
"Failed to set CGL fullscreen mode: %s\n",
CGLErrorString(cglErr));
2010-09-07 11:34:51 -04:00
_glfwPlatformCloseWindow();
return GL_FALSE;
}
}
return GL_TRUE;
}
//========================================================================
// Properly kill the window/video display
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformCloseWindow(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.mouseUPP != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
DisposeEventHandlerUPP(_glfwWin.mouseUPP);
2010-09-07 11:34:51 -04:00
_glfwWin.mouseUPP = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.commandUPP != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
DisposeEventHandlerUPP(_glfwWin.commandUPP);
2010-09-07 11:34:51 -04:00
_glfwWin.commandUPP = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.keyboardUPP != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
DisposeEventHandlerUPP(_glfwWin.keyboardUPP);
2010-09-07 11:34:51 -04:00
_glfwWin.keyboardUPP = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.windowUPP != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
DisposeEventHandlerUPP(_glfwWin.windowUPP);
2010-09-07 11:34:51 -04:00
_glfwWin.windowUPP = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.cglContext != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
CGLSetCurrentContext(NULL);
CGLClearDrawable(_glfwWin.cglContext);
CGLDestroyContext(_glfwWin.cglContext);
2010-09-07 11:34:51 -04:00
CGReleaseAllDisplays();
_glfwWin.cglContext = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.cglPixelFormat != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
CGLDestroyPixelFormat(_glfwWin.cglPixelFormat);
2010-09-07 11:34:51 -04:00
_glfwWin.cglPixelFormat = NULL;
}
}
else
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.aglContext != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
aglSetCurrentContext(NULL);
aglSetDrawable(_glfwWin.aglContext, NULL);
aglDestroyContext(_glfwWin.aglContext);
2010-09-07 11:34:51 -04:00
_glfwWin.aglContext = NULL;
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.aglPixelFormat != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
aglDestroyPixelFormat(_glfwWin.aglPixelFormat);
2010-09-07 11:34:51 -04:00
_glfwWin.aglPixelFormat = NULL;
}
}
2010-09-08 20:29:39 -04:00
if (_glfwWin.window != NULL)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
ReleaseWindow(_glfwWin.window);
2010-09-07 11:34:51 -04:00
_glfwWin.window = NULL;
}
}
//========================================================================
// Set the window title
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSetWindowTitle(const char* title)
2010-09-07 11:34:51 -04:00
{
CFStringRef windowTitle;
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
windowTitle = CFStringCreateWithCString(kCFAllocatorDefault,
title,
kCFStringEncodingISOLatin1);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
SetWindowTitleWithCFString(_glfwWin.window, windowTitle);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
CFRelease(windowTitle);
2010-09-07 11:34:51 -04:00
}
}
//========================================================================
// Set the window size
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSetWindowSize(int width, int height)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
SizeWindow(_glfwWin.window, width, height, TRUE);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Set the window position
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSetWindowPos(int x, int y)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
MoveWindow(_glfwWin.window, x, y, FALSE);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Window iconification
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformIconifyWindow(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
CollapseWindow(_glfwWin.window, TRUE);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Window un-iconification
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformRestoreWindow(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (!_glfwWin.fullscreen)
CollapseWindow(_glfwWin.window, FALSE);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Swap buffers (double-buffering) and poll any new events
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSwapBuffers(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
if (_glfwWin.fullscreen)
CGLFlushDrawable(_glfwWin.cglContext);
2010-09-07 11:34:51 -04:00
else
2010-09-08 20:29:39 -04:00
aglSwapBuffers(_glfwWin.aglContext);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Set double buffering swap interval
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSwapInterval(int interval)
2010-09-07 11:34:51 -04:00
{
GLint AGLparameter = interval;
// CGL doesn't seem to like intervals other than 0 (vsync off) or 1 (vsync on)
2010-09-08 20:29:39 -04:00
long CGLparameter = (interval ? 1 : 0);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
// Don't care if we fail here..
2010-09-08 20:29:39 -04:00
CGLSetParameter(_glfwWin.cglContext,
kCGLCPSwapInterval,
(GLint*) &CGLparameter);
2010-09-07 11:34:51 -04:00
}
else
{
// ..or here
2010-09-08 20:29:39 -04:00
aglSetInteger(_glfwWin.aglContext,
AGL_SWAP_INTERVAL,
&AGLparameter);
2010-09-07 11:34:51 -04:00
}
}
//========================================================================
// Read back framebuffer parameters from the context
//========================================================================
2010-09-08 20:29:39 -04:00
#define _getAGLAttribute(aglAttributeName, variableName) \
2010-09-07 11:34:51 -04:00
{ \
GLint aglValue; \
2010-09-08 20:29:39 -04:00
aglDescribePixelFormat(_glfwWin.aglPixelFormat, aglAttributeName, &aglValue); \
2010-09-07 11:34:51 -04:00
variableName = aglValue; \
}
2010-09-08 20:29:39 -04:00
#define _getCGLAttribute(cglAttributeName, variableName) \
2010-09-07 11:34:51 -04:00
{ \
GLint cglValue; \
2010-09-08 20:29:39 -04:00
CGLDescribePixelFormat(_glfwWin.cglPixelFormat, 0, cglAttributeName, &cglValue); \
2010-09-07 11:34:51 -04:00
variableName = cglValue; \
}
2010-09-08 20:29:39 -04:00
void _glfwPlatformRefreshWindowParams(void)
2010-09-07 11:34:51 -04:00
{
GLint rgbColorDepth;
GLint rgbaAccumDepth = 0;
GLint rgbChannelDepth = 0;
2010-09-08 20:29:39 -04:00
if (_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
_getCGLAttribute(kCGLPFAAccelerated, _glfwWin.accelerated);
_getCGLAttribute(kCGLPFAAlphaSize, _glfwWin.alphaBits);
_getCGLAttribute(kCGLPFADepthSize, _glfwWin.depthBits);
_getCGLAttribute(kCGLPFAStencilSize, _glfwWin.stencilBits);
_getCGLAttribute(kCGLPFAAuxBuffers, _glfwWin.auxBuffers);
_getCGLAttribute(kCGLPFAStereo, _glfwWin.stereo);
_getCGLAttribute(kCGLPFASamples, _glfwWin.samples);
2010-09-07 11:34:51 -04:00
// Enumerate depth of RGB channels - unlike AGL, CGL works with
// a single parameter reflecting the full depth of the frame buffer
2010-09-08 20:29:39 -04:00
CGLDescribePixelFormat(_glfwWin.cglPixelFormat,
0,
kCGLPFAColorSize,
&rgbColorDepth);
2010-09-07 11:34:51 -04:00
2010-09-08 20:29:39 -04:00
if (rgbColorDepth == 24 || rgbColorDepth == 32)
2010-09-07 11:34:51 -04:00
rgbChannelDepth = 8;
2010-09-08 20:29:39 -04:00
if (rgbColorDepth == 16)
2010-09-07 11:34:51 -04:00
rgbChannelDepth = 5;
_glfwWin.redBits = rgbChannelDepth;
_glfwWin.greenBits = rgbChannelDepth;
_glfwWin.blueBits = rgbChannelDepth;
// Get pixel depth of accumulator - I haven't got the slightest idea
// how this number conforms to any other channel depth than 8 bits,
// so this might end up giving completely knackered results...
2010-09-08 20:29:39 -04:00
_getCGLAttribute(kCGLPFAColorSize, rgbaAccumDepth);
if (rgbaAccumDepth == 32)
2010-09-07 11:34:51 -04:00
rgbaAccumDepth = 8;
_glfwWin.accumRedBits = rgbaAccumDepth;
_glfwWin.accumGreenBits = rgbaAccumDepth;
_glfwWin.accumBlueBits = rgbaAccumDepth;
_glfwWin.accumAlphaBits = rgbaAccumDepth;
}
else
{
2010-09-08 20:29:39 -04:00
_getAGLAttribute(AGL_ACCELERATED, _glfwWin.accelerated);
_getAGLAttribute(AGL_RED_SIZE, _glfwWin.redBits);
_getAGLAttribute(AGL_GREEN_SIZE, _glfwWin.greenBits);
_getAGLAttribute(AGL_BLUE_SIZE, _glfwWin.blueBits);
_getAGLAttribute(AGL_ALPHA_SIZE, _glfwWin.alphaBits);
_getAGLAttribute(AGL_DEPTH_SIZE, _glfwWin.depthBits);
_getAGLAttribute(AGL_STENCIL_SIZE, _glfwWin.stencilBits);
_getAGLAttribute(AGL_ACCUM_RED_SIZE, _glfwWin.accumRedBits);
_getAGLAttribute(AGL_ACCUM_GREEN_SIZE, _glfwWin.accumGreenBits);
_getAGLAttribute(AGL_ACCUM_BLUE_SIZE, _glfwWin.accumBlueBits);
_getAGLAttribute(AGL_ACCUM_ALPHA_SIZE, _glfwWin.accumAlphaBits);
_getAGLAttribute(AGL_AUX_BUFFERS, _glfwWin.auxBuffers);
_getAGLAttribute(AGL_STEREO, _glfwWin.stereo);
_getAGLAttribute(AGL_SAMPLES_ARB, _glfwWin.samples);
2010-09-07 11:34:51 -04:00
}
}
//========================================================================
// Poll for new window and input events
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformPollEvents(void)
2010-09-07 11:34:51 -04:00
{
EventRef event;
EventTargetRef eventDispatcher = GetEventDispatcherTarget();
2010-09-08 20:29:39 -04:00
while (ReceiveNextEvent(0, NULL, 0.0, TRUE, &event) == noErr)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
SendEventToEventTarget(event, eventDispatcher);
ReleaseEvent(event);
2010-09-07 11:34:51 -04:00
}
}
//========================================================================
// Wait for new window and input events
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformWaitEvents(void)
2010-09-07 11:34:51 -04:00
{
EventRef event;
// Wait for new events
2010-09-08 20:29:39 -04:00
ReceiveNextEvent(0, NULL, kEventDurationForever, FALSE, &event);
2010-09-07 11:34:51 -04:00
// Process the new events
_glfwPlatformPollEvents();
}
//========================================================================
// Hide mouse cursor (lock it)
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformHideMouseCursor(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
CGDisplayHideCursor(kCGDirectMainDisplay);
CGAssociateMouseAndMouseCursorPosition(false);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Show mouse cursor (unlock it)
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformShowMouseCursor(void)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
CGDisplayShowCursor(kCGDirectMainDisplay);
CGAssociateMouseAndMouseCursorPosition(true);
2010-09-07 11:34:51 -04:00
}
//========================================================================
// Set physical mouse cursor position
//========================================================================
2010-09-08 20:29:39 -04:00
void _glfwPlatformSetMouseCursorPos(int x, int y)
2010-09-07 11:34:51 -04:00
{
Rect content;
2010-09-08 20:29:39 -04:00
if (_glfwWin.fullscreen)
2010-09-07 11:34:51 -04:00
{
2010-09-08 20:29:39 -04:00
CGDisplayMoveCursorToPoint(kCGDirectMainDisplay,
CGPointMake(x, y));
2010-09-07 11:34:51 -04:00
}
else
{
GetWindowBounds(_glfwWin.window, kWindowContentRgn, &content);
_glfwInput.MousePosX = x + content.left;
_glfwInput.MousePosY = y + content.top;
2010-09-08 20:29:39 -04:00
CGDisplayMoveCursorToPoint(kCGDirectMainDisplay,
CGPointMake(_glfwInput.MousePosX,
_glfwInput.MousePosY));
2010-09-07 11:34:51 -04:00
}
}