2014-07-08 15:47:28 -05:00
|
|
|
// 8 july 2014
|
|
|
|
|
|
|
|
#import "objc_darwin.h"
|
|
|
|
#import "_cgo_export.h"
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2014-08-15 11:56:33 -05:00
|
|
|
#define toNSWindow(x) ((NSWindow *) (x))
|
|
|
|
|
2014-08-05 13:40:08 -05:00
|
|
|
static Class areaClass;
|
|
|
|
|
|
|
|
@interface goApplication : NSApplication
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation goApplication
|
|
|
|
|
|
|
|
// by default, NSApplication eats some key events
|
|
|
|
// this prevents that from happening with Area
|
|
|
|
// see http://stackoverflow.com/questions/24099063/how-do-i-detect-keyup-in-my-nsview-with-the-command-key-held and http://lists.apple.com/archives/cocoa-dev/2003/Oct/msg00442.html
|
|
|
|
- (void)sendEvent:(NSEvent *)e
|
|
|
|
{
|
|
|
|
NSEventType type;
|
|
|
|
|
|
|
|
type = [e type];
|
|
|
|
if (type == NSKeyDown || type == NSKeyUp || type == NSFlagsChanged) {
|
|
|
|
id focused;
|
|
|
|
|
|
|
|
focused = [[e window] firstResponder];
|
2014-08-10 07:04:20 -05:00
|
|
|
if (focused != nil && [focused isKindOfClass:areaClass])
|
2014-08-05 13:40:08 -05:00
|
|
|
switch (type) {
|
|
|
|
case NSKeyDown:
|
|
|
|
[focused keyDown:e];
|
|
|
|
return;
|
|
|
|
case NSKeyUp:
|
|
|
|
[focused keyUp:e];
|
|
|
|
return;
|
|
|
|
case NSFlagsChanged:
|
|
|
|
[focused flagsChanged:e];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else fall through
|
|
|
|
}
|
|
|
|
// otherwise, let NSApplication do it
|
|
|
|
[super sendEvent:e];
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:56:33 -05:00
|
|
|
// ok AppKit, wanna play hardball? let's play hardball.
|
|
|
|
// because I can neither break out of the special version of the NSModalPanelRunLoopMode that the regular terminate: puts us in nor avoid the exit(0); call included, I'm taking control
|
|
|
|
// note that this is called AFTER applicationShouldTerminate:
|
|
|
|
- (void)terminate:(id)sender
|
|
|
|
{
|
|
|
|
// DO ABSOLUTELY NOTHING
|
|
|
|
// the magic is [NSApp run] will just... stop.
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:40:08 -05:00
|
|
|
@end
|
|
|
|
|
2014-07-20 16:27:56 -05:00
|
|
|
@interface appDelegateClass : NSObject <NSApplicationDelegate>
|
2014-07-08 15:47:28 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation appDelegateClass
|
2014-08-15 04:15:44 -05:00
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
|
|
|
|
{
|
|
|
|
NSArray *windows;
|
|
|
|
NSUInteger i, n;
|
|
|
|
|
|
|
|
windows = [NSApp windows];
|
|
|
|
n = [windows count];
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
NSWindow *w;
|
|
|
|
|
|
|
|
w = toNSWindow([windows objectAtIndex:i]);
|
|
|
|
if (![[w delegate] windowShouldClose:w])
|
|
|
|
// stop at the first rejection; thanks Lyle42 in irc.freenode.net/#macdev
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
// all windows closed; stop gracefully for Go
|
2014-08-15 11:56:33 -05:00
|
|
|
// note that this is designed for our special terminate: above
|
|
|
|
return NSTerminateNow;
|
2014-08-15 04:15:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-07-08 15:47:28 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
appDelegateClass *appDelegate;
|
|
|
|
|
|
|
|
id getAppDelegate(void)
|
|
|
|
{
|
|
|
|
return appDelegate;
|
|
|
|
}
|
|
|
|
|
2014-08-10 21:24:08 -05:00
|
|
|
void uiinit(char **errmsg)
|
2014-07-08 15:47:28 -05:00
|
|
|
{
|
2014-08-05 13:40:08 -05:00
|
|
|
areaClass = getAreaClass();
|
2014-07-08 15:47:28 -05:00
|
|
|
appDelegate = [appDelegateClass new];
|
2014-08-05 13:40:08 -05:00
|
|
|
[goApplication sharedApplication];
|
2014-08-05 07:23:16 -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
|
2014-07-08 15:47:28 -05:00
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
[NSApp setDelegate:appDelegate];
|
|
|
|
}
|
|
|
|
|
|
|
|
void uimsgloop(void)
|
|
|
|
{
|
|
|
|
[NSApp run];
|
2014-08-15 11:56:33 -05:00
|
|
|
// NSLog(@"you shouldn't see this under normal circumstances, but screw the rules, I have SUBCLASSING");
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|
|
|
|
|
2014-07-13 20:31:13 -05:00
|
|
|
// don't use [NSApp terminate:]; that quits the program
|
|
|
|
void uistop(void)
|
|
|
|
{
|
|
|
|
NSEvent *e;
|
|
|
|
|
|
|
|
[NSApp stop:NSApp];
|
|
|
|
// 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];
|
|
|
|
[NSApp postEvent:e atStart:NO]; // let pending events take priority
|
|
|
|
}
|
|
|
|
|
|
|
|
// thanks to mikeash in irc.freenode.net/#macdev for suggesting the use of Grand Central Dispatch and blocks for this
|
2014-07-08 15:47:28 -05:00
|
|
|
void issue(void *what)
|
|
|
|
{
|
2014-07-08 15:50:52 -05:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
doissue(what);
|
|
|
|
});
|
2014-07-08 15:47:28 -05:00
|
|
|
}
|