2015-08-16 21:30:44 -05:00
|
|
|
// 6 april 2015
|
|
|
|
#import "uipriv_darwin.h"
|
2018-03-10 18:02:10 -06:00
|
|
|
#import "attrstr.h"
|
2015-08-16 21:30:44 -05:00
|
|
|
|
|
|
|
static BOOL canQuit = NO;
|
2016-05-23 23:09:46 -05:00
|
|
|
static NSAutoreleasePool *globalPool;
|
2016-06-16 12:45:24 -05:00
|
|
|
static applicationClass *app;
|
|
|
|
static appDelegate *delegate;
|
|
|
|
|
|
|
|
static BOOL (^isRunning)(void);
|
|
|
|
static BOOL stepsIsRunning;
|
2015-08-16 21:30:44 -05:00
|
|
|
|
|
|
|
@implementation applicationClass
|
|
|
|
|
2016-01-09 14:05:19 -06:00
|
|
|
- (void)sendEvent:(NSEvent *)e
|
|
|
|
{
|
|
|
|
if (sendAreaEvents(e) != 0)
|
|
|
|
return;
|
|
|
|
[super sendEvent:e];
|
|
|
|
}
|
|
|
|
|
2016-04-14 13:01:33 -05:00
|
|
|
// NSColorPanel always sends changeColor: to the first responder regardless of whether there's a target set on it
|
|
|
|
// we can override it here (see colorbutton.m)
|
|
|
|
// thanks to mikeash in irc.freenode.net/#macdev for informing me this is how the first responder chain is initiated
|
|
|
|
// it turns out NSFontManager also sends changeFont: through this; let's inhibit that here too (see fontbutton.m)
|
|
|
|
- (BOOL)sendAction:(SEL)sel to:(id)to from:(id)from
|
|
|
|
{
|
2016-05-15 19:51:33 -05:00
|
|
|
if (colorButtonInhibitSendAction(sel, from, to))
|
|
|
|
return NO;
|
2018-03-04 18:51:45 -06:00
|
|
|
if (uiprivFontButtonInhibitSendAction(sel, from, to))
|
2016-04-14 13:01:33 -05:00
|
|
|
return NO;
|
|
|
|
return [super sendAction:sel to:to from:from];
|
|
|
|
}
|
|
|
|
|
|
|
|
// likewise, NSFontManager also sends NSFontPanelValidation messages to the first responder, however it does NOT use sendAction:from:to:!
|
|
|
|
// instead, it uses this one (thanks swillits in irc.freenode.net/#macdev)
|
|
|
|
// we also need to override it (see fontbutton.m)
|
|
|
|
- (id)targetForAction:(SEL)sel to:(id)to from:(id)from
|
|
|
|
{
|
|
|
|
id override;
|
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
if (uiprivFontButtonOverrideTargetForAction(sel, from, to, &override))
|
2016-04-14 13:01:33 -05:00
|
|
|
return override;
|
|
|
|
return [super targetForAction:sel to:to from:from];
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:30:44 -05:00
|
|
|
// hey look! we're overriding terminate:!
|
|
|
|
// we're going to make sure we can go back to main() whether Cocoa likes it or not!
|
|
|
|
// and just how are we going to do that, hm?
|
|
|
|
// (note: this is called after applicationShouldTerminate:)
|
|
|
|
- (void)terminate:(id)sender
|
|
|
|
{
|
|
|
|
// yes that's right folks: DO ABSOLUTELY NOTHING.
|
|
|
|
// the magic is [NSApp run] will just... stop.
|
|
|
|
|
|
|
|
// well let's not do nothing; let's actually quit our graceful way
|
|
|
|
NSEvent *e;
|
|
|
|
|
|
|
|
if (!canQuit)
|
2016-05-13 19:14:46 -05:00
|
|
|
implbug("call to [NSApp terminate:] when not ready to terminate; definitely contact andlabs");
|
2015-08-16 21:30:44 -05:00
|
|
|
|
|
|
|
[realNSApp() stop:realNSApp()];
|
|
|
|
// stop: won't register until another event has passed; let's synthesize one
|
|
|
|
e = [NSEvent otherEventWithType:NSApplicationDefined
|
|
|
|
location:NSZeroPoint
|
|
|
|
modifierFlags:0
|
|
|
|
timestamp:[[NSProcessInfo processInfo] systemUptime]
|
|
|
|
windowNumber:0
|
|
|
|
context:[NSGraphicsContext currentContext]
|
|
|
|
subtype:0
|
|
|
|
data1:0
|
|
|
|
data2:0];
|
|
|
|
[realNSApp() postEvent:e atStart:NO]; // let pending events take priority (this is what PostQuitMessage() on Windows does so we have to do it here too for parity; thanks to mikeash in irc.freenode.net/#macdev for confirming that this parameter should indeed be NO)
|
2016-06-16 12:45:24 -05:00
|
|
|
|
|
|
|
// and in case uiMainSteps() was called
|
|
|
|
stepsIsRunning = NO;
|
2015-08-16 21:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation appDelegate
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-05-23 23:09:46 -05:00
|
|
|
// Apple docs: "Don't Use Accessor Methods in Initializer Methods and dealloc"
|
|
|
|
[_menuManager release];
|
2015-08-16 21:30:44 -05:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
|
|
|
|
{
|
|
|
|
// for debugging
|
|
|
|
NSLog(@"in applicationShouldTerminate:");
|
|
|
|
if (shouldQuit()) {
|
|
|
|
canQuit = YES;
|
|
|
|
// this will call terminate:, which is the same as uiQuit()
|
|
|
|
return NSTerminateNow;
|
|
|
|
}
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
uiInitOptions options;
|
|
|
|
|
|
|
|
const char *uiInit(uiInitOptions *o)
|
|
|
|
{
|
2016-05-23 23:09:46 -05:00
|
|
|
@autoreleasepool {
|
2016-05-24 21:44:40 -05:00
|
|
|
options = *o;
|
2016-05-24 22:17:08 -05:00
|
|
|
app = [[applicationClass sharedApplication] retain];
|
2016-05-24 21:44:40 -05:00
|
|
|
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
|
|
|
|
// see https://github.com/andlabs/ui/issues/6
|
|
|
|
[realNSApp() setActivationPolicy:NSApplicationActivationPolicyRegular];
|
2016-05-24 22:17:08 -05:00
|
|
|
delegate = [appDelegate new];
|
|
|
|
[realNSApp() setDelegate:delegate];
|
2016-05-23 23:09:46 -05:00
|
|
|
|
2016-05-24 21:44:40 -05:00
|
|
|
initAlloc();
|
2017-05-19 15:40:52 -05:00
|
|
|
loadFutures();
|
2017-11-03 20:55:43 -05:00
|
|
|
loadUndocumented();
|
2015-08-16 21:30:44 -05:00
|
|
|
|
2016-05-24 21:44:40 -05:00
|
|
|
// always do this so we always have an application menu
|
|
|
|
appDelegate().menuManager = [[menuManager new] autorelease];
|
|
|
|
[realNSApp() setMainMenu:[appDelegate().menuManager makeMenubar]];
|
2015-08-16 21:30:44 -05:00
|
|
|
|
2018-03-04 18:51:45 -06:00
|
|
|
uiprivSetupFontPanel();
|
2017-02-14 13:57:56 -06:00
|
|
|
|
2018-03-10 18:02:10 -06:00
|
|
|
uiprivInitUnderlineColors();
|
2016-05-24 21:44:40 -05:00
|
|
|
}
|
2016-05-24 22:17:08 -05:00
|
|
|
|
|
|
|
globalPool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
return NULL;
|
2015-08-16 21:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiUninit(void)
|
|
|
|
{
|
2016-05-24 22:17:08 -05:00
|
|
|
if (!globalPool) {
|
|
|
|
userbug("You must call uiInit() first!");
|
|
|
|
}
|
|
|
|
[globalPool release];
|
|
|
|
|
2016-05-23 23:09:46 -05:00
|
|
|
@autoreleasepool {
|
2018-03-10 18:02:10 -06:00
|
|
|
uiprivUninitUnderlineColors();
|
2016-05-24 22:17:08 -05:00
|
|
|
[delegate release];
|
2016-05-24 21:44:40 -05:00
|
|
|
[realNSApp() setDelegate:nil];
|
2016-05-24 22:17:08 -05:00
|
|
|
[app release];
|
2016-05-24 21:44:40 -05:00
|
|
|
uninitAlloc();
|
|
|
|
}
|
2015-08-16 21:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiFreeInitError(const char *err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiMain(void)
|
|
|
|
{
|
2016-06-16 12:45:24 -05:00
|
|
|
isRunning = ^{
|
|
|
|
return [realNSApp() isRunning];
|
|
|
|
};
|
2015-08-16 21:30:44 -05:00
|
|
|
[realNSApp() run];
|
|
|
|
}
|
|
|
|
|
2016-06-17 08:22:31 -05:00
|
|
|
void uiMainSteps(void)
|
2016-06-16 12:45:24 -05:00
|
|
|
{
|
2016-10-26 08:39:43 -05:00
|
|
|
// SDL does this and it seems to be necessary for the menubar to work (see #182)
|
|
|
|
[realNSApp() finishLaunching];
|
2016-06-16 12:45:24 -05:00
|
|
|
isRunning = ^{
|
|
|
|
return stepsIsRunning;
|
|
|
|
};
|
|
|
|
stepsIsRunning = YES;
|
|
|
|
}
|
|
|
|
|
2016-10-27 22:32:33 -05:00
|
|
|
int uiMainStep(int wait)
|
|
|
|
{
|
2016-10-31 13:33:11 -05:00
|
|
|
struct nextEventArgs nea;
|
|
|
|
|
|
|
|
nea.mask = NSAnyEventMask;
|
|
|
|
|
|
|
|
// ProPuke did this in his original PR requesting this
|
|
|
|
// I'm not sure if this will work, but I assume it will...
|
|
|
|
nea.duration = [NSDate distantPast];
|
|
|
|
if (wait) // but this is normal so it will work
|
|
|
|
nea.duration = [NSDate distantFuture];
|
|
|
|
|
|
|
|
nea.mode = NSDefaultRunLoopMode;
|
|
|
|
nea.dequeue = YES;
|
|
|
|
|
|
|
|
return mainStep(&nea, ^(NSEvent *e) {
|
2016-10-27 22:32:33 -05:00
|
|
|
return NO;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-24 21:44:40 -05:00
|
|
|
// see also:
|
|
|
|
// - http://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html
|
|
|
|
// - https://github.com/gnustep/gui/blob/master/Source/NSApplication.m
|
2016-10-31 13:33:11 -05:00
|
|
|
int mainStep(struct nextEventArgs *nea, BOOL (^interceptEvent)(NSEvent *e))
|
2016-05-24 21:44:40 -05:00
|
|
|
{
|
|
|
|
NSDate *expire;
|
|
|
|
NSEvent *e;
|
|
|
|
NSEventType type;
|
|
|
|
|
|
|
|
@autoreleasepool {
|
2016-06-16 12:45:24 -05:00
|
|
|
if (!isRunning())
|
2016-05-24 21:44:40 -05:00
|
|
|
return 0;
|
|
|
|
|
2016-10-31 13:33:11 -05:00
|
|
|
e = [realNSApp() nextEventMatchingMask:nea->mask
|
|
|
|
untilDate:nea->duration
|
|
|
|
inMode:nea->mode
|
|
|
|
dequeue:nea->dequeue];
|
2016-05-24 21:44:40 -05:00
|
|
|
if (e == nil)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
type = [e type];
|
2016-10-27 22:32:33 -05:00
|
|
|
if (!interceptEvent(e))
|
|
|
|
[realNSApp() sendEvent:e];
|
2016-05-24 21:44:40 -05:00
|
|
|
[realNSApp() updateWindows];
|
|
|
|
|
|
|
|
// GNUstep does this
|
|
|
|
// it also updates the Services menu but there doesn't seem to be a public API for that so
|
|
|
|
if (type != NSPeriodic && type != NSMouseMoved)
|
|
|
|
[[realNSApp() mainMenu] update];
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:30:44 -05:00
|
|
|
void uiQuit(void)
|
|
|
|
{
|
|
|
|
canQuit = YES;
|
|
|
|
[realNSApp() terminate:realNSApp()];
|
|
|
|
}
|
2015-12-04 22:42:03 -06:00
|
|
|
|
|
|
|
// thanks to mikeash in irc.freenode.net/#macdev for suggesting the use of Grand Central Dispatch for this
|
2016-05-28 17:01:25 -05:00
|
|
|
// LONGTERM will dispatch_get_main_queue() break after _CFRunLoopSetCurrent()?
|
2015-12-04 22:42:03 -06:00
|
|
|
void uiQueueMain(void (*f)(void *data), void *data)
|
|
|
|
{
|
|
|
|
// dispatch_get_main_queue() is a serial queue so it will not execute multiple uiQueueMain() functions concurrently
|
|
|
|
// the signature of f matches dispatch_function_t
|
|
|
|
dispatch_async_f(dispatch_get_main_queue(), data, f);
|
|
|
|
}
|
2017-08-19 17:32:52 -05:00
|
|
|
|
2017-08-19 17:41:04 -05:00
|
|
|
@interface TimerDelegate : NSObject {
|
|
|
|
int (*f)(void *data);
|
|
|
|
void *data;
|
|
|
|
}
|
|
|
|
- (id)initWithCallback:(int (*)(void *))callback data:(void*)callbackData;
|
|
|
|
- (void)doTimer:(NSTimer *)timer;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation TimerDelegate
|
|
|
|
|
|
|
|
- (id)initWithCallback:(int (*)(void *))callback data:(void*)callbackData
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
f = callback;
|
|
|
|
data = callbackData;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)doTimer:(NSTimer *)timer
|
|
|
|
{
|
|
|
|
if (!f(data))
|
|
|
|
[timer invalidate];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-08-19 17:32:52 -05:00
|
|
|
void uiTimer(int milliseconds, int (*f)(void *data), void *data)
|
|
|
|
{
|
2017-08-19 17:41:04 -05:00
|
|
|
TimerDelegate *delegate;
|
|
|
|
|
|
|
|
delegate = [[TimerDelegate alloc] initWithCallback:f data:data];
|
|
|
|
[NSTimer scheduledTimerWithTimeInterval:milliseconds / 1000.0
|
|
|
|
target:delegate
|
|
|
|
selector:@selector(doTimer:)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
2017-08-20 17:05:41 -05:00
|
|
|
[delegate release];
|
2017-08-19 17:32:52 -05:00
|
|
|
}
|