Moved some Objective-C stuff to objc_darwin.go, added some helper functions there too, and cleaned up runtimetest.go.
This commit is contained in:
parent
3ad482297c
commit
ea24130fc8
|
@ -1,16 +1,72 @@
|
||||||
// 28 february 2014
|
// 28 february 2014
|
||||||
package main
|
package main
|
||||||
|
|
||||||
/*
|
import (
|
||||||
These are wrapper functions for the functions in bleh_darwin.m to wrap around stdint.h type casting.
|
"unsafe"
|
||||||
|
)
|
||||||
This will eventually be expanded to include the other Objective-C runtime support functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// #cgo LDFLAGS: -lobjc -framework Foundation
|
// #cgo LDFLAGS: -lobjc -framework Foundation
|
||||||
|
// #include <stdlib.h>
|
||||||
// #include "objc_darwin.h"
|
// #include "objc_darwin.h"
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
func objc_getClass(class string) C.id {
|
||||||
|
cclass := C.CString(class)
|
||||||
|
defer C.free(unsafe.Pointer(cclass))
|
||||||
|
|
||||||
|
return C.objc_getClass(cclass)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sel_getUid(sel string) C.SEL {
|
||||||
|
csel := C.CString(sel)
|
||||||
|
defer C.free(unsafe.Pointer(csel))
|
||||||
|
|
||||||
|
return C.sel_getUid(csel)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Common Objective-C types and selectors.
|
||||||
|
var (
|
||||||
|
_NSString = objc_getClass("NSString")
|
||||||
|
|
||||||
|
_alloc = sel_getUid("alloc")
|
||||||
|
_new = sel_getUid("new")
|
||||||
|
_release = sel_getUid("release")
|
||||||
|
_stringWithUTF8String = sel_getUid("stringWithUTF8String:")
|
||||||
|
_UTF8String = sel_getUid("UTF8String")
|
||||||
|
)
|
||||||
|
|
||||||
|
// some helper functions
|
||||||
|
|
||||||
|
func objc_alloc(class C.id) C.id {
|
||||||
|
return C.objc_msgSend_noargs(class, _alloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func objc_new(class C.id) C.id {
|
||||||
|
return C.objc_msgSend_noargs(class, _new)
|
||||||
|
}
|
||||||
|
|
||||||
|
func objc_release(obj C.id) {
|
||||||
|
C.objc_msgSend_noargs(obj, _release)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toNSString(str string) C.id {
|
||||||
|
cstr := C.CString(str)
|
||||||
|
defer C.free(unsafe.Pointer(cstr))
|
||||||
|
|
||||||
|
return C.objc_msgSend_str(_NSString,
|
||||||
|
_stringWithUTF8String,
|
||||||
|
cstr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromNSString(str C.id) string {
|
||||||
|
cstr := C.objc_msgSend_noargs(str, _UTF8String)
|
||||||
|
return C.GoString((*C.char)(unsafe.Pointer(cstr)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
These are wrapper functions for the functions in bleh_darwin.m to wrap around stdint.h type casting.
|
||||||
|
*/
|
||||||
|
|
||||||
func objc_msgSend_rect(obj C.id, sel C.SEL, x int, y int, w int, h int) C.id {
|
func objc_msgSend_rect(obj C.id, sel C.SEL, x int, y int, w int, h int) C.id {
|
||||||
return C._objc_msgSend_rect(obj, sel,
|
return C._objc_msgSend_rect(obj, sel,
|
||||||
C.int64_t(x), C.int64_t(y), C.int64_t(w), C.int64_t(h))
|
C.int64_t(x), C.int64_t(y), C.int64_t(w), C.int64_t(h))
|
||||||
|
|
|
@ -3,7 +3,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unsafe"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,19 +13,14 @@ import (
|
||||||
// id Nilid = nil;
|
// id Nilid = nil;
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
func objc_getClass(class string) C.id {
|
var (
|
||||||
cclass := C.CString(class)
|
_NSApplication = objc_getClass("NSApplication")
|
||||||
defer C.free(unsafe.Pointer(cclass))
|
_NSNotificationCenter = objc_getClass("NSNotificationCenter")
|
||||||
|
|
||||||
return C.objc_getClass(cclass)
|
_sharedApplication = sel_getUid("sharedApplication")
|
||||||
}
|
_defaultCenter = sel_getUid("defaultCenter")
|
||||||
|
_run = sel_getUid("run")
|
||||||
func sel_getUid(sel string) C.SEL {
|
)
|
||||||
csel := C.CString(sel)
|
|
||||||
defer C.free(unsafe.Pointer(csel))
|
|
||||||
|
|
||||||
return C.sel_getUid(csel)
|
|
||||||
}
|
|
||||||
|
|
||||||
var NSApp C.id
|
var NSApp C.id
|
||||||
var defNC C.id
|
var defNC C.id
|
||||||
|
@ -35,21 +29,15 @@ var notesel C.SEL
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21
|
// need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21
|
||||||
NSApplication := objc_getClass("NSApplication")
|
NSApp = C.objc_msgSend_noargs(_NSApplication, _sharedApplication)
|
||||||
sharedApplication := sel_getUid("sharedApplication")
|
|
||||||
NSApp = C.objc_msgSend_noargs(NSApplication, sharedApplication)
|
|
||||||
|
|
||||||
defNC = C.objc_msgSend_noargs(
|
defNC = C.objc_msgSend_noargs(_NSNotificationCenter, _defaultCenter)
|
||||||
objc_getClass("NSNotificationCenter"),
|
|
||||||
sel_getUid("defaultCenter"))
|
|
||||||
|
|
||||||
selW := sel_getUid("windowShouldClose:")
|
selW := sel_getUid("windowShouldClose:")
|
||||||
selB := sel_getUid("buttonClicked:")
|
selB := sel_getUid("buttonClicked:")
|
||||||
selN := sel_getUid("gotNotification:")
|
selN := sel_getUid("gotNotification:")
|
||||||
mk("hello", selW, selB, selN)
|
mk("hello", selW, selB, selN)
|
||||||
delegate = C.objc_msgSend_noargs(
|
delegate = objc_alloc(objc_getClass("hello"))
|
||||||
objc_getClass("hello"),
|
|
||||||
alloc)
|
|
||||||
|
|
||||||
notesel = selN
|
notesel = selN
|
||||||
}
|
}
|
||||||
|
@ -71,69 +59,67 @@ const (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NSRoundedBezelStyle = 1
|
NSRoundedBezelStyle = 1
|
||||||
|
// TODO copy in the rest?
|
||||||
)
|
)
|
||||||
|
|
||||||
var alloc = sel_getUid("alloc")
|
var (
|
||||||
|
_NSAutoreleasePool = objc_getClass("NSAutoreleasePool")
|
||||||
|
|
||||||
|
_performSelectorOnMainThread =
|
||||||
|
sel_getUid("performSelectorOnMainThread:withObject:waitUntilDone:")
|
||||||
|
)
|
||||||
|
|
||||||
func notify(source string) {
|
func notify(source string) {
|
||||||
csource := C.CString(source)
|
|
||||||
defer C.free(unsafe.Pointer(csource))
|
|
||||||
|
|
||||||
// we need to make an NSAutoreleasePool, otherwise we get leak warnings on stderr
|
// we need to make an NSAutoreleasePool, otherwise we get leak warnings on stderr
|
||||||
pool := C.objc_msgSend_noargs(
|
pool := objc_new(_NSAutoreleasePool)
|
||||||
objc_getClass("NSAutoreleasePool"),
|
src := toNSString(source)
|
||||||
sel_getUid("new"))
|
|
||||||
src := C.objc_msgSend_str(
|
|
||||||
objc_getClass("NSString"),
|
|
||||||
sel_getUid("stringWithUTF8String:"),
|
|
||||||
csource)
|
|
||||||
C.objc_msgSend_sel_id_bool(
|
C.objc_msgSend_sel_id_bool(
|
||||||
delegate,
|
delegate,
|
||||||
sel_getUid("performSelectorOnMainThread:withObject:waitUntilDone:"),
|
_performSelectorOnMainThread,
|
||||||
notesel,
|
notesel,
|
||||||
src,
|
src,
|
||||||
C.BOOL(C.YES)) // wait so we can properly drain the autorelease pool; on other platforms we wind up waiting anyway (since the main thread can only handle one thing at a time) so
|
C.BOOL(C.YES)) // wait so we can properly drain the autorelease pool; on other platforms we wind up waiting anyway (since the main thread can only handle one thing at a time) so
|
||||||
C.objc_msgSend_noargs(pool,
|
objc_release(pool)
|
||||||
sel_getUid("release"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
var (
|
||||||
NSWindow := objc_getClass("NSWindow")
|
_NSWindow = objc_getClass("NSWindow")
|
||||||
NSWindowinit :=
|
_NSButton = objc_getClass("NSButton")
|
||||||
sel_getUid("initWithContentRect:styleMask:backing:defer:")
|
|
||||||
setDelegate := sel_getUid("setDelegate:")
|
|
||||||
makeKeyAndOrderFront := sel_getUid("makeKeyAndOrderFront:")
|
|
||||||
|
|
||||||
|
_initWithContentRect = sel_getUid("initWithContentRect:styleMask:backing:defer:")
|
||||||
|
_setDelegate = sel_getUid("setDelegate:")
|
||||||
|
_makeKeyAndOrderFront = sel_getUid("makeKeyAndOrderFront:")
|
||||||
|
_contentView = sel_getUid("contentView")
|
||||||
|
_initWithFrame = sel_getUid("initWithFrame:")
|
||||||
|
_setTarget = sel_getUid("setTarget:")
|
||||||
|
_setAction = sel_getUid("setAction:")
|
||||||
|
_setBezelStyle = sel_getUid("setBezelStyle:")
|
||||||
|
_addSubview = sel_getUid("addSubview:")
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
style := uintptr(NSTitledWindowMask | NSClosableWindowMask)
|
style := uintptr(NSTitledWindowMask | NSClosableWindowMask)
|
||||||
backing := uintptr(NSBackingStoreBuffered)
|
backing := uintptr(NSBackingStoreBuffered)
|
||||||
deferx := C.BOOL(C.YES)
|
deferx := C.BOOL(C.YES)
|
||||||
window := C.objc_msgSend_noargs(NSWindow, alloc)
|
window := objc_alloc(_NSWindow)
|
||||||
window = objc_msgSend_rect_uint_uint_bool(window, NSWindowinit,
|
window = objc_msgSend_rect_uint_uint_bool(window,
|
||||||
|
_initWithContentRect,
|
||||||
100, 100, 320, 240,
|
100, 100, 320, 240,
|
||||||
style, backing, deferx)
|
style, backing, deferx)
|
||||||
C.objc_msgSend_id(window, makeKeyAndOrderFront, window)
|
C.objc_msgSend_id(window, _makeKeyAndOrderFront, window)
|
||||||
C.objc_msgSend_id(window, setDelegate,
|
C.objc_msgSend_id(window, _setDelegate, delegate)
|
||||||
delegate)
|
windowView := C.objc_msgSend_noargs(window, _contentView)
|
||||||
windowView := C.objc_msgSend_noargs(window,
|
|
||||||
sel_getUid("contentView"))
|
|
||||||
|
|
||||||
NSButton := objc_getClass("NSButton")
|
button := objc_alloc(_NSButton)
|
||||||
button := C.objc_msgSend_noargs(NSButton, alloc)
|
|
||||||
button = objc_msgSend_rect(button,
|
button = objc_msgSend_rect(button,
|
||||||
sel_getUid("initWithFrame:"),
|
_initWithFrame,
|
||||||
20, 20, 200, 200)
|
20, 20, 200, 200)
|
||||||
C.objc_msgSend_id(button,
|
C.objc_msgSend_id(button, _setTarget, delegate)
|
||||||
sel_getUid("setTarget:"),
|
|
||||||
delegate)
|
|
||||||
C.objc_msgSend_sel(button,
|
C.objc_msgSend_sel(button,
|
||||||
sel_getUid("setAction:"),
|
_setAction,
|
||||||
sel_getUid("buttonClicked:"))
|
sel_getUid("buttonClicked:"))
|
||||||
objc_msgSend_uint(button,
|
objc_msgSend_uint(button, _setBezelStyle, NSRoundedBezelStyle)
|
||||||
sel_getUid("setBezelStyle:"),
|
C.objc_msgSend_id(windowView, _addSubview, button)
|
||||||
NSRoundedBezelStyle)
|
|
||||||
C.objc_msgSend_id(windowView,
|
|
||||||
sel_getUid("addSubview:"),
|
|
||||||
button)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
@ -143,23 +129,5 @@ func main() {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
C.objc_msgSend_noargs(NSApp,
|
C.objc_msgSend_noargs(NSApp, _run)
|
||||||
sel_getUid("run"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func helloworld() {
|
|
||||||
_hello := C.CString("hello, world\n")
|
|
||||||
defer C.free(unsafe.Pointer(_hello))
|
|
||||||
|
|
||||||
NSString := objc_getClass("NSString")
|
|
||||||
stringWithUTF8String :=
|
|
||||||
sel_getUid("stringWithUTF8String:")
|
|
||||||
str := C.objc_msgSend_str(NSString,
|
|
||||||
stringWithUTF8String,
|
|
||||||
_hello)
|
|
||||||
UTF8String := sel_getUid("UTF8String")
|
|
||||||
res := C.objc_msgSend_noargs(str,
|
|
||||||
UTF8String)
|
|
||||||
cres := (*C.char)(unsafe.Pointer(res))
|
|
||||||
fmt.Printf("%s", C.GoString(cres))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue