Implemented Post() on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2014-07-03 11:53:21 -04:00
parent 1287ba8968
commit b460466e4b
3 changed files with 43 additions and 0 deletions

View File

@ -56,6 +56,13 @@ extern NSRect dummyRect;
@implementation appDelegate
- (void)uipost:(id)pv
{
NSValue *v = (NSValue *) pv;
appDelegate_uipost([v pointerValue]);
}
- (BOOL)windowShouldClose:(id)win
{
return appDelegate_windowShouldClose(win);
@ -123,6 +130,18 @@ BOOL initCocoa(id appDelegate)
return YES;
}
void uipost(id appDelegate, void *data)
{
// need an autorelease pool here
NSAutoreleasePool *pool;
pool = [NSAutoreleasePool new];
[appDelegate performSelectorOnMainThread:@selector(uipost:)
withObject:[NSValue valueWithPointer:data]
waitUntilDone:YES]; // note this bit; see uitask_darwin.go for details
[pool release];
}
void breakMainLoop(void)
{
NSEvent *e;

View File

@ -63,6 +63,7 @@ extern uintptr_t keyCode(id);
extern id makeAppDelegate(void);
extern id windowGetContentView(id);
extern BOOL initCocoa(id);
extern void uipost(id, void *);
extern void breakMainLoop(void);
extern void cocoaMainLoop(void);

View File

@ -4,6 +4,7 @@ package ui
import (
"fmt"
"unsafe"
)
// #cgo CFLAGS: -mmacosx-version-min=10.7 -DMACOSX_DEPLOYMENT_TARGET=10.7
@ -32,6 +33,28 @@ func ui() {
C.cocoaMainLoop()
}
// we're going to call the appDelegate selector with waitUntilDone:YES so we don't have to worry about garbage collection
// we DO need to collect the two pointers together, though
type uipostmsg struct {
w *Window
data interface{}
}
//export appDelegate_uipost
func appDelegate_uipost(xmsg unsafe.Pointer) {
msg := (*uipostmsg)(xmsg)
msg.w.sysData.post(msg.data)
}
func uipost(w *Window, data interface{}) {
msg := &uipostmsg{
w: w,
data: data,
}
C.uipost(appDelegate, unsafe.Pointer(msg))
}
func initCocoa() (err error) {
makeAppDelegate()
if C.initCocoa(appDelegate) != C.YES {