From 8ed05fb45149af8a9c2f8313eb84f74d117f3cea Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 7 May 2019 14:48:06 -0400 Subject: [PATCH] Work around cgo (or Clang dwarf) bug with incorrect struct size calculation by returning void pointers for NS object constructors. Expand examples/app to start building menus. --- examples/app/main.go | 32 ++++++++++++++++++++++++++++++-- examples/app/nswrap.toml | 7 ++++++- types/node.go | 28 ++++++++++++++++++++++++++++ wrap/main.go | 24 ++++++++++++++++++++---- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/examples/app/main.go b/examples/app/main.go index 2a0a928..e5bcbef 100644 --- a/examples/app/main.go +++ b/examples/app/main.go @@ -1,13 +1,16 @@ package main +//go:generate nswrap import ( + "runtime" "gitlab.wow.st/gmp/nswrap/examples/app/ns" ) -func main() { +func nsmgr() { + //Lock OS thread because Cocoa uses thread-local storage + runtime.LockOSThread() a := ns.NSApplicationSharedApplication() a.SetActivationPolicy(ns.NSApplicationActivationPolicyRegular) - //w := ns.NSWindowAlloc() w := ns.NSWindowAlloc().InitWithContentRect( ns.NSMakeRect(200,200,600,600), ns.NSWindowStyleMaskTitled, @@ -17,6 +20,31 @@ func main() { ) w.SetTitle(ns.NSStringWithGoString("Hi World")) w.MakeKeyAndOrderFront(w) + w.SetAlphaValue(0.85) + m1 := ns.NSMenuAlloc().InitWithTitle(ns.NSStringWithGoString("Main")) + appItem := ns.NSMenuItemAlloc() + fileItem := ns.NSMenuItemAlloc() + m1.AddItem(appItem) + m1.AddItem(fileItem) + + appMenu := ns.NSMenuAlloc().InitWithTitle(ns.NSStringWithGoString("App")) + fileMenu := ns.NSMenuAlloc().InitWithTitle(ns.NSStringWithGoString("File")) + m1.SetSubmenu(appMenu, appItem) + m1.SetSubmenu(fileMenu, fileItem) + + s := ns.NSStringWithGoString("") + appMenu.AddItemWithTitle(ns.NSStringWithGoString("About"), nil, s) + appMenu.AddItemWithTitle(ns.NSStringWithGoString("Preferences"), nil, s) + a.SetMainMenu(m1) + fileMenu.AddItemWithTitle(ns.NSStringWithGoString("Open"), nil, s) + fileMenu.AddItemWithTitle(ns.NSStringWithGoString("New"), nil, s) + + a.SetMainMenu(m1) a.Run() } +func main() { + go nsmgr() + select { } +} + diff --git a/examples/app/nswrap.toml b/examples/app/nswrap.toml index 0458761..a92e81b 100644 --- a/examples/app/nswrap.toml +++ b/examples/app/nswrap.toml @@ -19,13 +19,16 @@ Classes = [ "NSApplication", "NSBundle", "NSApp", + "NSMenu", + "NSMenuItem", "NSWindow", + "NSView", "NSScreen", "NSEvent", "NSResponder", + "NSRunLoop", ] Functions = [ - "NSApplicationMain", "NSMake.*", ] Enums = [ @@ -33,6 +36,8 @@ Enums = [ "NSApplication.*", "NSBackingStore.*", "NSWindowStyleMask.*", + "NSWindowButton", + "NSWindowOrderingMode", ] Frameworks = [ "Foundation", "AppKit", "CoreGraphics" ] Pragma = [ 'clang diagnostic ignored "-Wformat-security"' ] diff --git a/types/node.go b/types/node.go index b21716f..9dc714a 100644 --- a/types/node.go +++ b/types/node.go @@ -138,3 +138,31 @@ func (n *Node) _Ctype(ignore map[string]bool) string { return s } + +func (n *Node) Qualifiers() string { + if n == nil { + return "" + } + ret := []string{} + for _,c := range n.Children { + switch c.Kind { + case "TypeQualifier": + ret = append(ret,c.Content) + } + } + return strings.Join(ret," ") +} + +func (n *Node) Annotations() string { + if n == nil { + return "" + } + ret := []string{} + for _,c := range n.Children { + switch c.Kind { + case "NullableAnnotation": + ret = append(ret,c.Content) + } + } + return strings.Join(ret," ") +} diff --git a/wrap/main.go b/wrap/main.go index 5fec78e..adb5d43 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -556,6 +556,10 @@ func (w *Wrapper) ProcessMethod(m *Method) { } func (w *Wrapper) ProcessFunction(m *Method) { + if m.Type.Node.IsId() { + //do not wrap functions that return ID because of CGo struct size bug + return + } w._processMethod(m,true) } @@ -572,6 +576,7 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) { case m.Type.GoType() != "*" + m.GoClass: gname = m.GoClass + gname default: + //Shorten class method names lens1 := len(m.Class) i := 0 if len(gname) < len(m.Class) { i = lens1 - len(gname) } @@ -592,8 +597,19 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) { if m.Class != "" { cname = m.Class + "_" + cname } - - cmtype := m.Type.CTypeAttrib() + var cmtype string + if m.Type.IsPointer() { + // work around cgo bugs with struct size calculation + cmtype = "void*" + if x := m.Type.Node.Qualifiers(); x != "" { + cmtype = x + " " + cmtype + } + if x := m.Type.Node.Annotations(); x != "" { + cmtype = cmtype + " " + x + } + } else { + cmtype = m.Type.CTypeAttrib() + } ns,tps,gplist := w.gpntp(m) grtype := m.Type.GoType() if grtype == "Void" { @@ -755,11 +771,11 @@ func %sAlloc() *%s { `,i.GoName,i.GoName,i.GoName,i.Name)) w.cCode.WriteString(fmt.Sprintf(` -%s* +void* %sAlloc() { return [%s alloc]; } -`, i.Name, i.Name, i.Name)) +`, i.Name, i.Name)) //FIXME: sort properties for _,p := range i.Properties {