-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunloop_nsapp.m
More file actions
150 lines (120 loc) · 3.68 KB
/
runloop_nsapp.m
File metadata and controls
150 lines (120 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "runloop_nsapp.h"
#include "_cgo_export.h"
@interface ContentView: NSView
@end
ContentView *contentView;
@interface AppDelegate: NSObject <NSApplicationDelegate>
@end
@implementation AppDelegate {
NSWindow *mainWindow;
}
- (void)applicationWillFinishLaunching:(NSNotification *)note {
NSLog(@"Will finish launching");
}
- (void) applicationDidFinishLaunching:(NSNotification *)note {
NSLog(@"Did finish launching");
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// Menubar
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
id appMenu = [[NSMenu new] autorelease];
id appName = [NSProcessInfo processInfo].processName;
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"]
autorelease];
[NSApp setMainMenu:menubar];
[menubar addItem:appMenuItem];
[appMenuItem setSubmenu:appMenu];
[appMenu addItem:quitMenuItem];
// Create the window and content view
mainWindow = [[NSWindow alloc]
initWithContentRect:contentView.frame
styleMask: NSWindowStyleMaskClosable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:NO];
[mainWindow cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[mainWindow setTitle:appName];
mainWindow.contentView = contentView;
[mainWindow makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
@end
@implementation ContentView {
}
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
self.layer.magnificationFilter = kCAFilterNearest;
}
return self;
}
- (void) setImage:(id)image {
dispatch_async(dispatch_get_main_queue(), ^{
self.layer.contents = image;
});
}
- (void) forwardMouseEvent: (NSEvent *)event {
NSPoint pos = [self convertPoint: event.locationInWindow fromView:nil];
BOOL mouseDown = NSEvent.pressedMouseButtons & 1;
int x = pos.x;
int y = self.frame.size.height - pos.y;
// Call the Go function that handles mouse events.
receiveMouseEvent(x, y, mouseDown);
}
- (void) mouseDown: (NSEvent *)event {
[self forwardMouseEvent:event];
}
- (void) mouseUp: (NSEvent *)event {
[self forwardMouseEvent:event];
}
- (void) mouseDragged: (NSEvent *)event {
[self forwardMouseEvent:event];
}
@end
void InitApp(int w, int h) {
@autoreleasepool {
// Create the content view with correct width and height
contentView = [[ContentView alloc] initWithFrame: NSMakeRect(0, 0, w, h)];
// Constant size for the content view
contentView.autoresizingMask = NSViewNotSizable;
}
}
void RunApp() {
@autoreleasepool {
// Create and assign the app delegate, and run it.
[NSApplication sharedApplication];
NSApp.delegate = [AppDelegate new];
[NSApp run];
}
}
void StopApp() {
@autoreleasepool {
[NSApp terminate:nil];
}
}
void DrawRGBA(void* bytes, int length, int w, int h) {
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bytes, length, nil);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef image = CGImageCreate(
w,
h,
8,
32,
4*w,
colorspace,
kCGBitmapByteOrderDefault,
provider,
NULL,
false,
kCGRenderingIntentDefault
);
[contentView setImage: (__bridge id)image];
CGImageRelease(image);
CGColorSpaceRelease(colorspace);
CGDataProviderRelease(provider);
}