2013-01-15 23:37:07 -05:00
|
|
|
/*
|
2015-01-02 17:43:11 -05:00
|
|
|
* Copyright 2011-2015 Branimir Karadzic. All rights reserved.
|
2013-01-15 23:37:07 -05:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
#include "entry_p.h"
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2013-08-15 00:08:46 -04:00
|
|
|
#if ENTRY_CONFIG_USE_NATIVE && BX_PLATFORM_OSX
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2013-08-08 01:50:01 -04:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2013-08-08 02:11:20 -04:00
|
|
|
#include <bgfxplatform.h>
|
|
|
|
|
2013-08-08 01:50:01 -04:00
|
|
|
#include <bx/uint32_t.h>
|
|
|
|
#include <bx/thread.h>
|
2013-11-18 23:43:17 -05:00
|
|
|
#include <bx/os.h>
|
2014-10-01 23:25:56 -04:00
|
|
|
#include <bx/handlealloc.h>
|
2013-01-15 23:37:07 -05:00
|
|
|
|
2013-07-22 00:53:20 -04:00
|
|
|
@interface AppDelegate : NSObject<NSApplicationDelegate>
|
|
|
|
{
|
2013-01-15 23:37:07 -05:00
|
|
|
bool terminated;
|
|
|
|
}
|
2013-07-22 00:53:20 -04:00
|
|
|
|
|
|
|
+ (AppDelegate *)sharedDelegate;
|
2013-01-15 23:37:07 -05:00
|
|
|
- (id)init;
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
|
|
|
|
- (bool)applicationHasTerminated;
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
@end
|
|
|
|
|
2013-07-22 00:53:20 -04:00
|
|
|
@interface Window : NSObject<NSWindowDelegate>
|
|
|
|
{
|
2014-10-01 23:25:56 -04:00
|
|
|
uint32_t windowCount;
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
+ (Window*)sharedDelegate;
|
2013-01-15 23:37:07 -05:00
|
|
|
- (id)init;
|
2014-10-01 23:25:56 -04:00
|
|
|
- (void)windowCreated:(NSWindow*)window;
|
|
|
|
- (void)windowWillClose:(NSNotification*)notification;
|
|
|
|
- (BOOL)windowShouldClose:(NSWindow*)window;
|
|
|
|
- (void)windowDidResize:(NSNotification*)notification;
|
2013-01-15 23:37:07 -05:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
namespace entry
|
|
|
|
{
|
2014-09-24 16:25:16 -04:00
|
|
|
static WindowHandle s_defaultWindow = { 0 }; // TODO: Add support for more windows
|
|
|
|
static uint8_t s_translateKey[256];
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
struct MainThreadEntry
|
|
|
|
{
|
|
|
|
int m_argc;
|
|
|
|
char** m_argv;
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
static int32_t threadFunc(void* _userData)
|
|
|
|
{
|
2015-02-04 01:24:40 -05:00
|
|
|
CFBundleRef mainBundle = CFBundleGetMainBundle();
|
|
|
|
if ( mainBundle != nil )
|
|
|
|
{
|
|
|
|
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
|
|
|
|
if ( resourcesURL != nil )
|
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
if (CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX) )
|
|
|
|
{
|
|
|
|
chdir(path);
|
|
|
|
}
|
|
|
|
CFRelease(resourcesURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
MainThreadEntry* self = (MainThreadEntry*)_userData;
|
2013-08-08 01:50:01 -04:00
|
|
|
return main(self->m_argc, self->m_argv);
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
};
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
Context()
|
2015-03-04 21:28:47 -05:00
|
|
|
: m_scroll(0)
|
|
|
|
, m_exit(false)
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2014-09-28 16:36:23 -04:00
|
|
|
s_translateKey[27] = Key::Esc;
|
|
|
|
s_translateKey[13] = Key::Return;
|
|
|
|
s_translateKey[9] = Key::Tab;
|
|
|
|
s_translateKey[127] = Key::Backspace;
|
|
|
|
s_translateKey[uint8_t(' ')] = Key::Space;
|
|
|
|
|
|
|
|
s_translateKey[uint8_t('+')] =
|
|
|
|
s_translateKey[uint8_t('=')] = Key::Plus;
|
|
|
|
s_translateKey[uint8_t('_')] =
|
|
|
|
s_translateKey[uint8_t('-')] = Key::Minus;
|
|
|
|
|
2014-09-24 19:06:11 -04:00
|
|
|
s_translateKey[uint8_t('0')] = Key::Key0;
|
|
|
|
s_translateKey[uint8_t('1')] = Key::Key1;
|
|
|
|
s_translateKey[uint8_t('2')] = Key::Key2;
|
|
|
|
s_translateKey[uint8_t('3')] = Key::Key3;
|
|
|
|
s_translateKey[uint8_t('4')] = Key::Key4;
|
|
|
|
s_translateKey[uint8_t('5')] = Key::Key5;
|
|
|
|
s_translateKey[uint8_t('6')] = Key::Key6;
|
|
|
|
s_translateKey[uint8_t('7')] = Key::Key7;
|
|
|
|
s_translateKey[uint8_t('8')] = Key::Key8;
|
|
|
|
s_translateKey[uint8_t('9')] = Key::Key9;
|
2014-09-28 16:36:23 -04:00
|
|
|
|
|
|
|
for (char ch = 'a'; ch <= 'z'; ++ch)
|
|
|
|
{
|
2015-03-04 21:28:47 -05:00
|
|
|
s_translateKey[uint8_t(ch)] =
|
2014-09-28 16:36:23 -04:00
|
|
|
s_translateKey[uint8_t(ch - ' ')] = Key::KeyA + (ch - 'a');
|
|
|
|
}
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-09-23 11:11:25 -04:00
|
|
|
NSEvent* waitEvent()
|
2013-07-22 00:53:20 -04:00
|
|
|
{
|
|
|
|
return [NSApp
|
2013-07-21 17:44:53 -04:00
|
|
|
nextEventMatchingMask:NSAnyEventMask
|
|
|
|
untilDate:[NSDate distantFuture] // wait for event
|
|
|
|
inMode:NSDefaultRunLoopMode
|
2013-07-22 00:53:20 -04:00
|
|
|
dequeue:YES
|
|
|
|
];
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-09-23 11:11:25 -04:00
|
|
|
NSEvent* peekEvent()
|
2013-07-22 00:53:20 -04:00
|
|
|
{
|
|
|
|
return [NSApp
|
2013-07-21 17:44:53 -04:00
|
|
|
nextEventMatchingMask:NSAnyEventMask
|
|
|
|
untilDate:[NSDate distantPast] // do not wait for event
|
|
|
|
inMode:NSDefaultRunLoopMode
|
2013-07-22 00:53:20 -04:00
|
|
|
dequeue:YES
|
|
|
|
];
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-09-24 16:25:16 -04:00
|
|
|
void getMousePos(int* outX, int* outY)
|
|
|
|
{
|
2014-10-01 23:25:56 -04:00
|
|
|
WindowHandle handle = { 0 };
|
|
|
|
NSWindow* window = m_window[handle.idx];
|
|
|
|
NSRect originalFrame = [window frame];
|
|
|
|
NSPoint location = [window mouseLocationOutsideOfEventStream];
|
2014-09-24 16:25:16 -04:00
|
|
|
NSRect adjustFrame = [NSWindow contentRectForFrameRect: originalFrame styleMask: NSTitledWindowMask];
|
|
|
|
|
|
|
|
int x = location.x;
|
|
|
|
int y = (int)adjustFrame.size.height - (int)location.y;
|
|
|
|
|
|
|
|
// clamp within the range of the window
|
|
|
|
|
|
|
|
if (x < 0) x = 0;
|
|
|
|
if (y < 0) y = 0;
|
|
|
|
if (x > (int)adjustFrame.size.width) x = (int)adjustFrame.size.width;
|
|
|
|
if (y > (int)adjustFrame.size.height) y = (int)adjustFrame.size.height;
|
|
|
|
|
|
|
|
*outX = x;
|
|
|
|
*outY = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t translateModifiers(int flags)
|
|
|
|
{
|
|
|
|
uint8_t mask = 0;
|
|
|
|
|
|
|
|
if (flags & NSShiftKeyMask)
|
|
|
|
mask |= Modifier::LeftShift | Modifier::RightShift;
|
|
|
|
|
|
|
|
if (flags & NSAlternateKeyMask)
|
|
|
|
mask |= Modifier::LeftAlt | Modifier::RightAlt;
|
|
|
|
|
|
|
|
if (flags & NSControlKeyMask)
|
|
|
|
mask |= Modifier::LeftCtrl | Modifier::RightCtrl;
|
|
|
|
|
|
|
|
if (flags & NSCommandKeyMask)
|
|
|
|
mask |= Modifier::LeftMeta | Modifier::RightMeta;
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key::Enum handleKeyEvent(NSEvent* event, uint8_t* specialKeys)
|
|
|
|
{
|
|
|
|
NSString* key = [event charactersIgnoringModifiers];
|
|
|
|
unichar keyChar = 0;
|
2014-09-28 16:36:23 -04:00
|
|
|
//DBG("keyChar %d", keyChar);
|
2014-09-24 16:25:16 -04:00
|
|
|
if ([key length] == 0)
|
2014-09-28 16:36:23 -04:00
|
|
|
{
|
2014-09-24 16:25:16 -04:00
|
|
|
return Key::None;
|
2014-09-28 16:36:23 -04:00
|
|
|
}
|
2014-09-24 16:25:16 -04:00
|
|
|
|
|
|
|
keyChar = [key characterAtIndex:0];
|
|
|
|
|
|
|
|
int keyCode = keyChar;
|
2014-09-28 16:36:23 -04:00
|
|
|
//DBG("keyCode %d", keyCode);
|
2014-09-24 16:25:16 -04:00
|
|
|
*specialKeys = translateModifiers([event modifierFlags]);
|
|
|
|
|
|
|
|
// if this is a unhandled key just return None
|
2014-09-28 16:36:23 -04:00
|
|
|
if (keyCode < 256)
|
|
|
|
{
|
|
|
|
return (Key::Enum)s_translateKey[keyCode];
|
|
|
|
}
|
2014-09-24 16:25:16 -04:00
|
|
|
|
2014-09-28 16:36:23 -04:00
|
|
|
switch (keyCode)
|
|
|
|
{
|
|
|
|
case NSF1FunctionKey: return Key::F1;
|
|
|
|
case NSF2FunctionKey: return Key::F2;
|
|
|
|
case NSF3FunctionKey: return Key::F3;
|
|
|
|
case NSF4FunctionKey: return Key::F4;
|
|
|
|
case NSF5FunctionKey: return Key::F5;
|
|
|
|
case NSF6FunctionKey: return Key::F6;
|
|
|
|
case NSF7FunctionKey: return Key::F7;
|
|
|
|
case NSF8FunctionKey: return Key::F8;
|
|
|
|
case NSF9FunctionKey: return Key::F9;
|
|
|
|
case NSF10FunctionKey: return Key::F10;
|
|
|
|
case NSF11FunctionKey: return Key::F11;
|
|
|
|
case NSF12FunctionKey: return Key::F12;
|
|
|
|
|
|
|
|
case NSLeftArrowFunctionKey: return Key::Left;
|
|
|
|
case NSRightArrowFunctionKey: return Key::Right;
|
|
|
|
case NSUpArrowFunctionKey: return Key::Up;
|
|
|
|
case NSDownArrowFunctionKey: return Key::Down;
|
|
|
|
|
|
|
|
case NSPageUpFunctionKey: return Key::PageUp;
|
|
|
|
case NSPageDownFunctionKey: return Key::PageDown;
|
|
|
|
case NSHomeFunctionKey: return Key::Home;
|
|
|
|
case NSEndFunctionKey: return Key::End;
|
|
|
|
|
|
|
|
case NSPrintScreenFunctionKey: return Key::Print;
|
|
|
|
}
|
2014-09-24 16:25:16 -04:00
|
|
|
|
2014-09-28 16:36:23 -04:00
|
|
|
return Key::None;
|
2014-09-24 16:25:16 -04:00
|
|
|
}
|
|
|
|
|
2014-09-23 11:11:25 -04:00
|
|
|
bool dispatchEvent(NSEvent* event)
|
2013-07-22 00:53:20 -04:00
|
|
|
{
|
|
|
|
if (event)
|
|
|
|
{
|
2014-09-24 16:25:16 -04:00
|
|
|
NSEventType eventType = [event type];
|
|
|
|
|
|
|
|
switch (eventType)
|
|
|
|
{
|
2014-09-28 16:36:23 -04:00
|
|
|
case NSMouseMoved:
|
2015-01-15 13:58:33 -05:00
|
|
|
case NSLeftMouseDragged:
|
|
|
|
case NSRightMouseDragged:
|
2015-03-04 21:28:47 -05:00
|
|
|
case NSOtherMouseDragged:
|
2014-09-24 16:25:16 -04:00
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
2015-03-04 21:28:47 -05:00
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll);
|
2014-09-24 16:25:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSLeftMouseDown:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
2015-03-04 21:28:47 -05:00
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Left, true);
|
2014-09-24 16:25:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSLeftMouseUp:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
2015-03-04 21:28:47 -05:00
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Left, false);
|
2014-09-24 16:25:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSRightMouseDown:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
2015-03-04 21:28:47 -05:00
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Right, true);
|
2014-09-24 16:25:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSRightMouseUp:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
2015-03-04 21:28:47 -05:00
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Right, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSOtherMouseDown:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Middle, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSOtherMouseUp:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll, MouseButton::Middle, false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSScrollWheel:
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getMousePos(&x, &y);
|
|
|
|
m_scroll += ([event deltaY] > 0.0f) ? 1 : -1;
|
|
|
|
m_eventQueue.postMouseEvent(s_defaultWindow, x, y, m_scroll);
|
2014-09-24 16:25:16 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSKeyDown:
|
|
|
|
{
|
|
|
|
uint8_t modifiers = 0;
|
|
|
|
Key::Enum key = handleKeyEvent(event, &modifiers);
|
|
|
|
|
|
|
|
// If KeyCode is none we don't don't handle the key and special case for cmd+q (quit)
|
|
|
|
// Note that return false here means that we take care of the key (instead of the default behavior)
|
|
|
|
if (key != Key::None)
|
|
|
|
{
|
2014-09-28 16:36:23 -04:00
|
|
|
if (key != Key::KeyQ
|
|
|
|
&& !(modifiers & Modifier::RightMeta) )
|
2014-09-24 16:25:16 -04:00
|
|
|
{
|
|
|
|
m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case NSKeyUp:
|
|
|
|
{
|
|
|
|
uint8_t modifiers = 0;
|
|
|
|
Key::Enum key = handleKeyEvent(event, &modifiers);
|
|
|
|
|
|
|
|
if (key != Key::None)
|
|
|
|
{
|
|
|
|
m_eventQueue.postKeyEvent(s_defaultWindow, key, modifiers, false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 16:36:23 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
[NSApp sendEvent:event];
|
|
|
|
[NSApp updateWindows];
|
2014-09-24 16:25:16 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
void windowDidResize()
|
|
|
|
{
|
|
|
|
WindowHandle handle = { 0 };
|
|
|
|
NSWindow* window = m_window[handle.idx];
|
|
|
|
NSRect rect = [window frame];
|
|
|
|
uint32_t width = uint32_t(rect.size.width);
|
|
|
|
uint32_t height = uint32_t(rect.size.height);
|
|
|
|
m_eventQueue.postSizeEvent(handle, width, height);
|
|
|
|
}
|
|
|
|
|
2013-07-22 00:53:20 -04:00
|
|
|
int32_t run(int _argc, char** _argv)
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2013-07-21 17:44:53 -04:00
|
|
|
[NSApplication sharedApplication];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
|
|
|
id dg = [AppDelegate sharedDelegate];
|
2013-01-15 23:37:07 -05:00
|
|
|
[NSApp setDelegate:dg];
|
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
|
|
|
[NSApp finishLaunching];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2013-07-21 17:44:53 -04:00
|
|
|
postNotificationName:NSApplicationWillFinishLaunchingNotification
|
|
|
|
object:NSApp];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2013-07-21 17:44:53 -04:00
|
|
|
postNotificationName:NSApplicationDidFinishLaunchingNotification
|
|
|
|
object:NSApp];
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
id quitMenuItem = [NSMenuItem new];
|
|
|
|
[quitMenuItem
|
|
|
|
initWithTitle:@"Quit"
|
|
|
|
action:@selector(terminate:)
|
|
|
|
keyEquivalent:@"q"];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
id appMenu = [NSMenu new];
|
|
|
|
[appMenu addItem:quitMenuItem];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
id appMenuItem = [NSMenuItem new];
|
|
|
|
[appMenuItem setSubmenu:appMenu];
|
2013-07-22 00:53:20 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
id menubar = [[NSMenu new] autorelease];
|
|
|
|
[menubar addItem:appMenuItem];
|
|
|
|
[NSApp setMainMenu:menubar];
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
m_windowAlloc.alloc();
|
2015-03-04 21:28:47 -05:00
|
|
|
NSRect rect = NSMakeRect(0, 0, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT);
|
2014-10-01 23:25:56 -04:00
|
|
|
NSWindow* window = [[NSWindow alloc]
|
2013-07-21 17:44:53 -04:00
|
|
|
initWithContentRect:rect
|
|
|
|
styleMask:0
|
|
|
|
|NSTitledWindowMask
|
|
|
|
|NSClosableWindowMask
|
|
|
|
|NSMiniaturizableWindowMask
|
|
|
|
|NSResizableWindowMask
|
|
|
|
backing:NSBackingStoreBuffered defer:NO
|
2014-10-01 23:25:56 -04:00
|
|
|
];
|
2013-07-21 17:44:53 -04:00
|
|
|
NSString* appName = [[NSProcessInfo processInfo] processName];
|
2013-01-15 23:37:07 -05:00
|
|
|
[window setTitle:appName];
|
|
|
|
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
|
2013-11-18 23:43:17 -05:00
|
|
|
[window makeKeyAndOrderFront:window];
|
2014-09-24 16:25:16 -04:00
|
|
|
[window setAcceptsMouseMovedEvents:YES];
|
2014-09-23 11:11:25 -04:00
|
|
|
[window setBackgroundColor:[NSColor blackColor]];
|
2013-07-22 00:53:20 -04:00
|
|
|
[[Window sharedDelegate] windowCreated:window];
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
m_window[0] = window;
|
2014-09-24 16:25:16 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
bgfx::osxSetNSWindow(window);
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
MainThreadEntry mte;
|
|
|
|
mte.m_argc = _argc;
|
|
|
|
mte.m_argv = _argv;
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
bx::Thread thread;
|
|
|
|
thread.init(mte.threadFunc, &mte);
|
|
|
|
|
2013-07-22 00:53:20 -04:00
|
|
|
while (!(m_exit = [dg applicationHasTerminated]) )
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2013-11-18 23:43:17 -05:00
|
|
|
if (bgfx::RenderFrame::Exiting == bgfx::renderFrame() )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-09-23 11:11:25 -04:00
|
|
|
|
|
|
|
while (dispatchEvent(peekEvent() ) )
|
|
|
|
{
|
|
|
|
}
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
2013-11-18 23:43:17 -05:00
|
|
|
|
|
|
|
|
2013-02-03 16:33:25 -05:00
|
|
|
m_eventQueue.postExitEvent();
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-12-07 13:19:54 -05:00
|
|
|
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
|
2013-01-15 23:37:07 -05:00
|
|
|
thread.shutdown();
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-02-03 16:33:25 -05:00
|
|
|
EventQueue m_eventQueue;
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
bx::HandleAllocT<ENTRY_CONFIG_MAX_WINDOWS> m_windowAlloc;
|
|
|
|
NSWindow* m_window[ENTRY_CONFIG_MAX_WINDOWS];
|
|
|
|
|
2015-03-04 21:28:47 -05:00
|
|
|
int32_t m_scroll;
|
2013-01-15 23:37:07 -05:00
|
|
|
bool m_exit;
|
|
|
|
};
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
static Context s_ctx;
|
2013-07-21 17:44:53 -04:00
|
|
|
|
2013-01-17 01:44:51 -05:00
|
|
|
const Event* poll()
|
2013-01-15 23:37:07 -05:00
|
|
|
{
|
2013-02-03 16:33:25 -05:00
|
|
|
return s_ctx.m_eventQueue.poll();
|
2013-01-17 01:44:51 -05:00
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:10 -04:00
|
|
|
const Event* poll(WindowHandle _handle)
|
|
|
|
{
|
|
|
|
return s_ctx.m_eventQueue.poll(_handle);
|
|
|
|
}
|
|
|
|
|
2013-01-17 01:44:51 -05:00
|
|
|
void release(const Event* _event)
|
|
|
|
{
|
2013-02-03 16:33:25 -05:00
|
|
|
s_ctx.m_eventQueue.release(_event);
|
2013-01-17 01:44:51 -05:00
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:10 -04:00
|
|
|
WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
|
|
|
|
{
|
|
|
|
BX_UNUSED(_x, _y, _width, _height, _flags, _title);
|
|
|
|
WindowHandle handle = { UINT16_MAX };
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyWindow(WindowHandle _handle)
|
|
|
|
{
|
|
|
|
BX_UNUSED(_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
|
|
|
|
{
|
|
|
|
BX_UNUSED(_handle, _x, _y);
|
|
|
|
}
|
|
|
|
|
2014-09-19 13:33:13 -04:00
|
|
|
void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
|
2013-02-03 16:33:25 -05:00
|
|
|
{
|
2014-09-19 13:33:13 -04:00
|
|
|
BX_UNUSED(_handle, _width, _height);
|
2013-01-17 01:44:51 -05:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:33:13 -04:00
|
|
|
void setWindowTitle(WindowHandle _handle, const char* _title)
|
2014-08-05 15:57:52 -04:00
|
|
|
{
|
2014-09-19 13:33:13 -04:00
|
|
|
BX_UNUSED(_handle, _title);
|
2014-08-05 15:57:52 -04:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:33:13 -04:00
|
|
|
void toggleWindowFrame(WindowHandle _handle)
|
2013-02-03 16:33:25 -05:00
|
|
|
{
|
2014-09-19 13:33:13 -04:00
|
|
|
BX_UNUSED(_handle);
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
2014-09-19 13:33:13 -04:00
|
|
|
void setMouseLock(WindowHandle _handle, bool _lock)
|
2013-01-19 03:22:25 -05:00
|
|
|
{
|
2014-09-19 13:33:13 -04:00
|
|
|
BX_UNUSED(_handle, _lock);
|
2013-01-19 03:22:25 -05:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
} // namespace entry
|
|
|
|
|
2014-10-01 23:25:56 -04:00
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
+ (AppDelegate *)sharedDelegate
|
|
|
|
{
|
|
|
|
static id delegate = [AppDelegate new];
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (nil == self)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->terminated = false;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
|
|
|
|
{
|
|
|
|
BX_UNUSED(sender);
|
|
|
|
self->terminated = true;
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (bool)applicationHasTerminated
|
|
|
|
{
|
|
|
|
return self->terminated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Window
|
|
|
|
|
|
|
|
+ (Window*)sharedDelegate
|
|
|
|
{
|
|
|
|
static id windowDelegate = [Window new];
|
|
|
|
return windowDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (nil == self)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->windowCount = 0;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowCreated:(NSWindow*)window
|
|
|
|
{
|
|
|
|
assert(window);
|
|
|
|
|
|
|
|
[window setDelegate:self];
|
|
|
|
|
|
|
|
assert(self->windowCount < ~0u);
|
|
|
|
self->windowCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillClose:(NSNotification*)notification
|
|
|
|
{
|
|
|
|
BX_UNUSED(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)windowShouldClose:(NSWindow*)window
|
|
|
|
{
|
|
|
|
assert(window);
|
|
|
|
|
|
|
|
[window setDelegate:nil];
|
|
|
|
|
|
|
|
assert(self->windowCount);
|
|
|
|
self->windowCount -= 1;
|
|
|
|
|
|
|
|
if (self->windowCount == 0)
|
|
|
|
{
|
|
|
|
[NSApp terminate:self];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowDidResize:(NSNotification*)notification
|
|
|
|
{
|
|
|
|
BX_UNUSED(notification);
|
|
|
|
using namespace entry;
|
|
|
|
s_ctx.windowDidResize();
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2013-01-15 23:37:07 -05:00
|
|
|
int main(int _argc, char** _argv)
|
|
|
|
{
|
|
|
|
using namespace entry;
|
2013-07-22 00:53:20 -04:00
|
|
|
return s_ctx.run(_argc, _argv);
|
2013-01-15 23:37:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // BX_PLATFORM_OSX
|