2015-05-06 00:50:57 +00:00
|
|
|
#include <AppKit/AppKit.h>
|
2015-11-01 00:06:56 +00:00
|
|
|
#include <thread>
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
#include "boo/IApplication.hpp"
|
2015-11-08 00:36:38 +00:00
|
|
|
#include "boo/graphicsdev/Metal.hpp"
|
2015-11-09 02:24:45 +00:00
|
|
|
#include "CocoaCommon.hpp"
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2016-03-08 21:18:38 +00:00
|
|
|
#include "logvisor/logvisor.hpp"
|
2015-11-18 23:55:25 +00:00
|
|
|
|
2015-12-27 04:20:07 +00:00
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
#error ARC Required
|
|
|
|
#endif
|
|
|
|
|
2017-07-17 02:45:49 +00:00
|
|
|
/* If set, application will terminate once client thread reaches end;
|
|
|
|
* main() will not get back control. Otherwise, main will get back control
|
|
|
|
* but App will not terminate in the normal Cocoa manner (possibly resulting
|
|
|
|
* in CoreAnimation warnings). */
|
|
|
|
#define COCOA_TERMINATE 1
|
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
namespace boo {class ApplicationCocoa;}
|
2015-05-06 00:50:57 +00:00
|
|
|
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
|
|
|
{
|
2015-11-01 00:06:56 +00:00
|
|
|
boo::ApplicationCocoa* m_app;
|
2015-05-06 01:03:49 +00:00
|
|
|
@public
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
2015-11-01 00:06:56 +00:00
|
|
|
- (id)initWithApp:(boo::ApplicationCocoa*)app;
|
2015-05-06 00:50:57 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
2016-03-08 21:18:38 +00:00
|
|
|
static logvisor::Module Log("boo::ApplicationCocoa");
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2017-07-17 02:45:49 +00:00
|
|
|
std::shared_ptr<IWindow> _WindowCocoaNew(const SystemString& title, NSOpenGLContext* lastGLCtx,
|
|
|
|
MetalContext* metalCtx, uint32_t sampleCount);
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
class ApplicationCocoa : public IApplication
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2015-11-01 00:06:56 +00:00
|
|
|
public:
|
2015-05-06 00:50:57 +00:00
|
|
|
IApplicationCallback& m_callback;
|
2015-12-27 04:20:07 +00:00
|
|
|
AppDelegate* m_appDelegate;
|
2015-11-01 00:06:56 +00:00
|
|
|
private:
|
2015-09-02 19:09:13 +00:00
|
|
|
const SystemString m_uniqueName;
|
|
|
|
const SystemString m_friendlyName;
|
|
|
|
const SystemString m_pname;
|
|
|
|
const std::vector<SystemString> m_args;
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
NSPanel* aboutPanel;
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* All windows */
|
2017-07-17 02:45:49 +00:00
|
|
|
std::unordered_map<uintptr_t, std::weak_ptr<IWindow>> m_windows;
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-08 00:36:38 +00:00
|
|
|
MetalContext m_metalCtx;
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
void _deletedWindow(IWindow* window)
|
|
|
|
{
|
2015-12-27 04:20:07 +00:00
|
|
|
m_windows.erase(window->getPlatformHandle());
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-09-02 19:09:13 +00:00
|
|
|
ApplicationCocoa(IApplicationCallback& callback,
|
|
|
|
const SystemString& uniqueName,
|
|
|
|
const SystemString& friendlyName,
|
|
|
|
const SystemString& pname,
|
|
|
|
const std::vector<SystemString>& args)
|
2015-05-06 00:50:57 +00:00
|
|
|
: m_callback(callback),
|
2015-09-02 19:09:13 +00:00
|
|
|
m_uniqueName(uniqueName),
|
2015-05-06 00:50:57 +00:00
|
|
|
m_friendlyName(friendlyName),
|
|
|
|
m_pname(pname),
|
|
|
|
m_args(args)
|
|
|
|
{
|
2015-12-27 04:20:07 +00:00
|
|
|
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* Delegate (OS X callbacks) */
|
2015-12-27 04:20:07 +00:00
|
|
|
m_appDelegate = [[AppDelegate alloc] initWithApp:this];
|
|
|
|
[[NSApplication sharedApplication] setDelegate:m_appDelegate];
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* App menu */
|
2016-01-08 00:04:37 +00:00
|
|
|
NSMenu* rootMenu = [[NSMenu alloc] initWithTitle:@"main"];
|
|
|
|
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]];
|
|
|
|
NSMenuItem* fsItem = [appMenu addItemWithTitle:@"Toggle Full Screen"
|
2015-11-01 00:06:56 +00:00
|
|
|
action:@selector(toggleFs:)
|
|
|
|
keyEquivalent:@"f"];
|
2016-10-15 18:39:09 +00:00
|
|
|
[fsItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
2016-01-08 00:04:37 +00:00
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
2016-01-14 01:20:25 +00:00
|
|
|
NSMenuItem* quitItem = [appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()]
|
|
|
|
action:@selector(quitApp:)
|
|
|
|
keyEquivalent:@"q"];
|
2016-10-15 18:39:09 +00:00
|
|
|
[quitItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
2016-01-08 00:04:37 +00:00
|
|
|
[[rootMenu addItemWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]
|
|
|
|
action:nil keyEquivalent:@""] setSubmenu:appMenu];
|
|
|
|
[[NSApplication sharedApplication] setMainMenu:rootMenu];
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-09 02:24:45 +00:00
|
|
|
/* Determine which graphics API to use */
|
2015-11-16 22:03:46 +00:00
|
|
|
#if BOO_HAS_METAL
|
2015-11-09 02:24:45 +00:00
|
|
|
for (const SystemString& arg : args)
|
|
|
|
if (!arg.compare("--metal"))
|
|
|
|
{
|
|
|
|
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
|
2016-01-11 22:26:40 +00:00
|
|
|
m_metalCtx.m_q = [m_metalCtx.m_dev newCommandQueue];
|
2016-03-08 21:18:38 +00:00
|
|
|
Log.report(logvisor::Info, "using Metal renderer");
|
2015-11-09 02:24:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-18 23:55:25 +00:00
|
|
|
if (!m_metalCtx.m_dev)
|
2016-03-08 21:18:38 +00:00
|
|
|
Log.report(logvisor::Info, "using OpenGL renderer");
|
2015-11-18 23:55:25 +00:00
|
|
|
#else
|
2016-07-05 21:47:04 +00:00
|
|
|
Log.report(logvisor::Info, "using OpenGL renderer");
|
2015-11-16 22:03:46 +00:00
|
|
|
#endif
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
EPlatformType getPlatformType() const
|
|
|
|
{
|
2015-11-21 02:16:15 +00:00
|
|
|
return EPlatformType::Cocoa;
|
2015-09-02 19:09:13 +00:00
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
std::thread m_clientThread;
|
|
|
|
int m_clientReturn = 0;
|
2017-07-17 02:45:49 +00:00
|
|
|
bool m_terminateNow = false;
|
2015-11-01 00:06:56 +00:00
|
|
|
int run()
|
2015-09-02 19:09:13 +00:00
|
|
|
{
|
2015-11-01 00:06:56 +00:00
|
|
|
/* Spawn client thread */
|
|
|
|
m_clientThread = std::thread([&]()
|
|
|
|
{
|
2017-01-17 01:20:50 +00:00
|
|
|
logvisor::RegisterThreadName("Boo Client Thread");
|
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* Run app */
|
|
|
|
m_clientReturn = m_callback.appMain(this);
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2017-07-17 02:45:49 +00:00
|
|
|
/* Cleanup */
|
|
|
|
for (auto& w : m_windows)
|
|
|
|
if (std::shared_ptr<IWindow> window = w.second.lock())
|
|
|
|
window->closeWindow();
|
|
|
|
|
|
|
|
#if COCOA_TERMINATE
|
|
|
|
/* Continue termination */
|
|
|
|
dispatch_sync(dispatch_get_main_queue(),
|
|
|
|
^{
|
|
|
|
/* Ends modal run loop and continues Cocoa termination */
|
|
|
|
[[NSApplication sharedApplication] replyToApplicationShouldTerminate:YES];
|
|
|
|
|
|
|
|
/* If this is reached, application didn't spawn any windows
|
|
|
|
* and must be explicitly terminated */
|
|
|
|
m_terminateNow = true;
|
|
|
|
[[NSApplication sharedApplication] terminate:nil];
|
|
|
|
});
|
|
|
|
#else
|
|
|
|
/* Return control to main() */
|
|
|
|
dispatch_sync(dispatch_get_main_queue(),
|
|
|
|
^{
|
|
|
|
[[NSApplication sharedApplication] stop:nil];
|
|
|
|
});
|
|
|
|
#endif
|
2015-11-01 00:06:56 +00:00
|
|
|
});
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* Already in Cocoa's event loop; return now */
|
|
|
|
return 0;
|
2015-09-02 19:09:13 +00:00
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
void quit()
|
|
|
|
{
|
|
|
|
[NSApp terminate:nil];
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
const SystemString& getUniqueName() const
|
|
|
|
{
|
|
|
|
return m_uniqueName;
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
const SystemString& getFriendlyName() const
|
|
|
|
{
|
|
|
|
return m_friendlyName;
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
const SystemString& getProcessName() const
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
|
|
|
return m_pname;
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-09-02 19:09:13 +00:00
|
|
|
const std::vector<SystemString>& getArgs() const
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
|
|
|
return m_args;
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2017-07-17 02:45:49 +00:00
|
|
|
std::shared_ptr<IWindow> newWindow(const std::string& title, uint32_t sampleCount)
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2017-07-17 02:45:49 +00:00
|
|
|
auto newWindow = _WindowCocoaNew(title, m_lastGLCtx, &m_metalCtx, sampleCount);
|
2015-12-27 04:20:07 +00:00
|
|
|
m_windows[newWindow->getPlatformHandle()] = newWindow;
|
2015-11-01 00:06:56 +00:00
|
|
|
return newWindow;
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
/* Last GL context */
|
|
|
|
NSOpenGLContext* m_lastGLCtx = nullptr;
|
2015-05-06 00:50:57 +00:00
|
|
|
};
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx)
|
|
|
|
{
|
|
|
|
static_cast<ApplicationCocoa*>(APP)->m_lastGLCtx = lastGLCtx;
|
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
|
2015-11-01 00:06:56 +00:00
|
|
|
IApplication* APP = nullptr;
|
|
|
|
int ApplicationRun(IApplication::EPlatformType platform,
|
|
|
|
IApplicationCallback& cb,
|
|
|
|
const SystemString& uniqueName,
|
|
|
|
const SystemString& friendlyName,
|
|
|
|
const SystemString& pname,
|
|
|
|
const std::vector<SystemString>& args,
|
|
|
|
bool singleInstance)
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2017-01-17 01:20:50 +00:00
|
|
|
logvisor::RegisterThreadName("Boo Main Thread");
|
2015-11-01 00:06:56 +00:00
|
|
|
@autoreleasepool
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2015-11-01 00:06:56 +00:00
|
|
|
if (!APP)
|
|
|
|
{
|
2015-11-21 02:16:15 +00:00
|
|
|
if (platform != IApplication::EPlatformType::Cocoa &&
|
|
|
|
platform != IApplication::EPlatformType::Auto)
|
2015-11-01 00:06:56 +00:00
|
|
|
return 1;
|
2017-07-17 02:45:49 +00:00
|
|
|
/* Never deallocated to ensure window destructors have access */
|
2015-11-01 00:06:56 +00:00
|
|
|
APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args);
|
|
|
|
}
|
2015-12-27 04:20:07 +00:00
|
|
|
[[NSApplication sharedApplication] run];
|
2017-07-17 02:45:49 +00:00
|
|
|
ApplicationCocoa* appCocoa = static_cast<ApplicationCocoa*>(APP);
|
|
|
|
if (appCocoa->m_clientThread.joinable())
|
|
|
|
appCocoa->m_clientThread.join();
|
|
|
|
return appCocoa->m_clientReturn;
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-24 21:07:48 +00:00
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
2015-11-01 00:06:56 +00:00
|
|
|
|
|
|
|
@implementation AppDelegate
|
|
|
|
- (id)initWithApp:(boo::ApplicationCocoa*)app
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
m_app = app;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification*)notification
|
|
|
|
{
|
|
|
|
(void)notification;
|
|
|
|
m_app->run();
|
|
|
|
}
|
2017-07-17 02:45:49 +00:00
|
|
|
#if COCOA_TERMINATE
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
|
2015-11-01 00:06:56 +00:00
|
|
|
{
|
2017-07-17 02:45:49 +00:00
|
|
|
(void)app;
|
|
|
|
if (m_app->m_terminateNow)
|
|
|
|
return NSTerminateNow;
|
2015-11-01 00:06:56 +00:00
|
|
|
m_app->m_callback.appQuitting(m_app);
|
2017-07-17 02:45:49 +00:00
|
|
|
return NSTerminateLater;
|
2015-11-01 00:06:56 +00:00
|
|
|
}
|
2017-07-17 02:45:49 +00:00
|
|
|
#else
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
|
|
|
|
{
|
|
|
|
(void)app;
|
|
|
|
m_app->m_callback.appQuitting(m_app);
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-01 00:06:56 +00:00
|
|
|
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename
|
|
|
|
{
|
|
|
|
std::vector<boo::SystemString> strVec;
|
|
|
|
strVec.push_back(boo::SystemString([filename UTF8String]));
|
|
|
|
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames
|
|
|
|
{
|
|
|
|
std::vector<boo::SystemString> strVec;
|
|
|
|
strVec.reserve([filenames count]);
|
|
|
|
for (NSString* str in filenames)
|
|
|
|
strVec.push_back(boo::SystemString([str UTF8String]));
|
|
|
|
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
|
|
|
}
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender
|
|
|
|
{
|
|
|
|
(void)sender;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
- (IBAction)toggleFs:(id)sender
|
|
|
|
{
|
|
|
|
(void)sender;
|
|
|
|
[[NSApp keyWindow] toggleFullScreen:nil];
|
|
|
|
}
|
|
|
|
- (IBAction)quitApp:(id)sender
|
|
|
|
{
|
|
|
|
(void)sender;
|
|
|
|
[NSApp terminate:nil];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|