Skip to content

Commit aa5d2b9

Browse files
committed
generate: iterate on function support
1 parent 009a114 commit aa5d2b9

File tree

14 files changed

+231
-83
lines changed

14 files changed

+231
-83
lines changed

generate/codegen/gen_function.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (f *Function) GoReturn(currentModule *modules.Module) string {
4747
if f.ReturnType == nil {
4848
return ""
4949
}
50+
log.Printf("rendering GoReturn function return: %s %T", f.ReturnType, f.ReturnType)
5051
return f.ReturnType.GoName(currentModule, true)
5152
}
5253

@@ -127,10 +128,26 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, cw *CodeWriter
127128
cw.WriteLine("}")
128129
}
129130

131+
func (f *Function) WriteObjcWrapper(currentModule *modules.Module, cw *CodeWriter) {
132+
if f.Deprecated {
133+
return
134+
cw.WriteLine("// deprecated")
135+
}
136+
returnTypeStr := f.Type.ReturnType.CName()
137+
cw.WriteLineF("%v %v(%v) {", returnTypeStr, f.GoName, f.CArgs(currentModule))
138+
cw.Indent()
139+
var args []string
140+
for _, p := range f.Parameters {
141+
args = append(args, p.Name)
142+
}
143+
cw.WriteLineF("return %v(%v);", f.Type.Name, strings.Join(args, ", "))
144+
cw.UnIndent()
145+
cw.WriteLine("}")
146+
}
147+
130148
func (f *Function) WriteCSignature(currentModule *modules.Module, cw *CodeWriter) {
131149
var returnTypeStr string
132150
rt := f.Type.ReturnType
133-
log.Printf("rt: %T", rt)
134151
returnTypeStr = rt.CName()
135152
cw.WriteLineF("// %v %v(%v); ", returnTypeStr, f.GoName, f.CArgs(currentModule))
136153
}

generate/codegen/gen_struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// Struct is code generator for objective-c struct
88
type Struct struct {
9-
Type *typing.StructType
9+
Type typing.Type
1010
Name string // the first part of objc function name
1111
GoName string
1212
Deprecated bool // if has been deprecated

generate/codegen/modulewriter.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (m *ModuleWriter) WriteCode() {
3333
m.WriteTypeAliases()
3434
m.WriteStructs()
3535
m.WriteFunctions()
36+
m.WriteFunctionWrappers()
3637
if m.Module.Package == "coreimage" {
3738
// filter protocols maybe arent "real" protocols?
3839
// get "cannot find protocol declaration" with protocol imports
@@ -117,19 +118,20 @@ func (m *ModuleWriter) WriteStructs() {
117118
cw.WriteLine(")")
118119

119120
for _, s := range m.Structs {
120-
if s.DocURL != "" {
121-
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", s.Description))
122-
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", s.DocURL))
123-
}
124-
125121
// if Ref type, allias to unsafe.Pointer
126122
if strings.HasSuffix(s.Name, "Ref") {
123+
if s.DocURL != "" {
124+
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", s.Description))
125+
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", s.DocURL))
126+
}
127+
127128
cw.WriteLineF("type %s unsafe.Pointer", s.GoName)
128129
continue
129130
}
130131
}
131132
}
132133

134+
// WriteFunctions writes the go code to call exposed functions.
133135
func (m *ModuleWriter) WriteFunctions() {
134136
if len(m.Functions) == 0 {
135137
return
@@ -148,10 +150,10 @@ func (m *ModuleWriter) WriteFunctions() {
148150
cw.WriteLine("package " + m.Module.Package)
149151

150152
//TODO: determine imports from functions
151-
cw.WriteLine(`// #import <stdlib.h>
152-
// #import <stdint.h>
153-
// #import <stdbool.h>
154-
// #import <CoreGraphics/CGGeometry.h>`)
153+
cw.WriteLineF(`// #import <stdlib.h>
154+
// #import <stdint.h>
155+
// #import <stdbool.h>
156+
// #import "%s"`, m.Module.Header)
155157
for _, f := range m.Functions {
156158
f.WriteCSignature(&m.Module, cw)
157159
}
@@ -177,6 +179,32 @@ func (m *ModuleWriter) WriteFunctions() {
177179
}
178180
}
179181

182+
// WriteFunctionWrappers writes the objc code to wrap exposed functions.
183+
// The cgo type system is unaware of objective c types so these wrappers must exist to allow
184+
// us to call the functions and return appropritely.
185+
func (m *ModuleWriter) WriteFunctionWrappers() {
186+
if len(m.Functions) == 0 {
187+
return
188+
}
189+
190+
filePath := filepath.Join(m.PlatformDir, m.Module.Package, "functions.gen.m")
191+
os.MkdirAll(filepath.Dir(filePath), 0755)
192+
f, err := os.Create(filePath)
193+
if err != nil {
194+
panic(err)
195+
}
196+
defer f.Close()
197+
198+
cw := &CodeWriter{Writer: f, IndentStr: "\t"}
199+
cw.WriteLine(AutoGeneratedMark)
200+
201+
//TODO: determine appropriate imports
202+
cw.WriteLineF("#import \"%s\"", m.Module.Header)
203+
for _, f := range m.Functions {
204+
f.WriteObjcWrapper(&m.Module, cw)
205+
}
206+
}
207+
180208
func (m *ModuleWriter) WriteEnumAliases() {
181209
enums := make([]*AliasInfo, len(m.EnumAliases))
182210
copy(enums, m.EnumAliases)

generate/function.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66

77
"github.com/progrium/macdriver/generate/codegen"
8+
"github.com/progrium/macdriver/generate/modules"
89
"github.com/progrium/macdriver/generate/typing"
910
)
1011

@@ -253,7 +254,7 @@ func (db *Generator) ToFunction(fw string, sym Symbol) *codegen.Function {
253254
}
254255
fn := &codegen.Function{
255256
Name: sym.Name,
256-
GoName: sym.Name,
257+
GoName: modules.TrimPrefix(sym.Name),
257258
Description: sym.Description,
258259
DocURL: sym.DocURL(),
259260
Type: fntyp,

generate/generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
113113
}
114114
mw.Functions = append(mw.Functions, fn)
115115
case "Struct":
116-
fn := db.ToStruct(framework, s)
117-
if fn == nil {
116+
s := db.ToStruct(framework, s)
117+
if s == nil {
118118
continue
119119
}
120-
mw.Structs = append(mw.Structs, fn)
120+
mw.Structs = append(mw.Structs, s)
121121
}
122122
}
123123
mw.WriteCode()

generate/struct.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/progrium/macdriver/generate/codegen"
77
"github.com/progrium/macdriver/generate/modules"
8-
"github.com/progrium/macdriver/generate/typing"
98
)
109

1110
func (db *Generator) ToStruct(fw string, sym Symbol) *codegen.Struct {
@@ -16,16 +15,12 @@ func (db *Generator) ToStruct(fw string, sym Symbol) *codegen.Struct {
1615
return nil
1716
}
1817
typ := db.TypeFromSymbol(sym)
19-
styp, ok := typ.(*typing.StructType)
20-
if !ok {
21-
return nil
22-
}
2318
s := &codegen.Struct{
2419
Name: sym.Name,
2520
GoName: modules.TrimPrefix(sym.Name),
2621
Description: sym.Description,
2722
DocURL: sym.DocURL(),
28-
Type: styp,
23+
Type: typ,
2924
}
3025

3126
return s

generate/types.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
4848
}
4949
case "Union":
5050
return &typing.RefType{
51-
Name: sym.Name,
51+
Name: sym.Name,
52+
GName: modules.TrimPrefix(sym.Name),
5253
}
5354
case "Type":
5455
if sym.Type != "Type Alias" {
@@ -60,7 +61,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
6061
// sym.Name == "NSZone" ||
6162
sym.Name == "MusicSequence" {
6263
return &typing.RefType{
63-
Name: sym.Name,
64+
Name: sym.Name,
65+
GName: modules.TrimPrefix(sym.Name),
6466
}
6567
}
6668
st, err := sym.Parse()
@@ -70,7 +72,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
7072
}
7173
if st.Struct != nil {
7274
return &typing.RefType{
73-
Name: st.Struct.Name,
75+
Name: st.Struct.Name,
76+
GName: modules.TrimPrefix(sym.Name),
7477
}
7578
}
7679
if st.TypeAlias == nil {
@@ -91,7 +94,9 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
9194
case "Struct":
9295
if strings.HasSuffix(sym.Name, "Ref") {
9396
return &typing.RefType{
94-
Name: sym.Name,
97+
Name: sym.Name,
98+
GName: modules.TrimPrefix(sym.Name),
99+
Module: modules.Get(module),
95100
}
96101
}
97102
return &typing.StructType{
@@ -100,7 +105,8 @@ func (db *Generator) TypeFromSymbol(sym Symbol) typing.Type {
100105
Module: modules.Get(module),
101106
}
102107
case "Function":
103-
if sym.Name != "CGDisplayCreateImage" {
108+
if sym.Name != "CGDisplayCreateImage" &&
109+
sym.Name != "CGMainDisplayID" {
104110
return nil
105111
}
106112
typ, err := sym.Parse()
@@ -219,7 +225,10 @@ func (db *Generator) ParseType(ti declparse.TypeInfo) (typ typing.Type) {
219225
}
220226
ref = true
221227
case "NSZone", "ipc_port_t":
222-
typ = &typing.RefType{Name: ti.Name}
228+
typ = &typing.RefType{
229+
Name: ti.Name,
230+
GName: modules.TrimPrefix(ti.Name),
231+
}
223232
ref = true
224233
case "NSDictionary":
225234
dt := &typing.DictType{}

generate/typing/ref_type.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ import (
88

99
// for weird struct refs like those ending in "Ref"
1010
type RefType struct {
11-
Name string // c and objc type name
12-
// GName string // the go struct name
13-
// Module *modules.Module // the module
11+
Name string // c and objc type name
12+
GName string // the go struct name
13+
Module *modules.Module // the module
1414
}
1515

1616
func (s *RefType) GoImports() set.Set[string] {
17-
return set.New("unsafe")
17+
if s.Module == nil {
18+
return set.New("unsafe")
19+
}
20+
return set.New("github.com/progrium/macdriver/macos/" + s.Module.Package)
1821
}
1922

2023
func (s *RefType) GoName(currentModule *modules.Module, receiveFromObjc bool) string {
21-
return "unsafe.Pointer"
24+
if s.Module == nil {
25+
return "unsafe.Pointer"
26+
}
27+
return FullGoName(*s.Module, s.GName, *currentModule)
2228
}
2329

2430
func (s *RefType) ObjcName() string {
@@ -30,5 +36,5 @@ func (s *RefType) CName() string {
3036
}
3137

3238
func (s *RefType) DeclareModule() *modules.Module {
33-
return nil
39+
return s.Module
3440
}

macos/coregraphics/aliastypes.gen.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package coregraphics
55
import (
66
"unsafe"
77

8+
"github.com/progrium/macdriver/macos/corefoundation"
9+
"github.com/progrium/macdriver/macos/iosurface"
810
"github.com/progrium/macdriver/objc"
911
)
1012

@@ -46,12 +48,12 @@ type DataProviderReleaseBytePointerCallback = func(info unsafe.Pointer, pointer
4648
// Draws a pattern cell. [Full Topic]
4749
//
4850
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgpatterndrawpatterncallback?language=objc
49-
type PatternDrawPatternCallback = func(info unsafe.Pointer, context unsafe.Pointer)
51+
type PatternDrawPatternCallback = func(info unsafe.Pointer, context ContextRef)
5052

5153
// Passes messages generated during a PostScript conversion process. [Full Topic]
5254
//
5355
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgpsconvertermessagecallback?language=objc
54-
type PSConverterMessageCallback = func(info unsafe.Pointer, message unsafe.Pointer)
56+
type PSConverterMessageCallback = func(info unsafe.Pointer, message corefoundation.StringRef)
5557

5658
// A client-supplied callback function that’s invoked whenever the configuration of a local display is changed. [Full Topic]
5759
//
@@ -66,7 +68,7 @@ type PSConverterProgressCallback = func(info unsafe.Pointer)
6668
// A client-supplied callback function that’s invoked whenever an associated event tap receives a Quartz event. [Full Topic]
6769
//
6870
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgeventtapcallback?language=objc
69-
type EventTapCallBack = func(proxy unsafe.Pointer, type_ EventType, event unsafe.Pointer, userInfo unsafe.Pointer) unsafe.Pointer
71+
type EventTapCallBack = func(proxy unsafe.Pointer, type_ EventType, event EventRef, userInfo unsafe.Pointer) EventRef
7072

7173
// Copies data from a Core Graphics-supplied buffer into a data consumer. [Full Topic]
7274
//
@@ -111,7 +113,7 @@ type PSConverterReleaseInfoCallback = func(info unsafe.Pointer)
111113
// Performs custom tasks at the beginning of each page in a PostScript conversion process. [Full Topic]
112114
//
113115
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgpsconverterbeginpagecallback?language=objc
114-
type PSConverterBeginPageCallback = func(info unsafe.Pointer, pageNumber uint, pageInfo unsafe.Pointer)
116+
type PSConverterBeginPageCallback = func(info unsafe.Pointer, pageNumber uint, pageInfo corefoundation.DictionaryRef)
115117

116118
// Performs custom clean-up tasks when Core Graphics deallocates a CGFunctionRef object. [Full Topic]
117119
//
@@ -136,12 +138,12 @@ type ErrorCallback = func()
136138
// Performs custom tasks at the end of each page of a PostScript conversion process. [Full Topic]
137139
//
138140
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgpsconverterendpagecallback?language=objc
139-
type PSConverterEndPageCallback = func(info unsafe.Pointer, pageNumber uint, pageInfo unsafe.Pointer)
141+
type PSConverterEndPageCallback = func(info unsafe.Pointer, pageNumber uint, pageInfo corefoundation.DictionaryRef)
140142

141143
// A block called when a data stream has a new frame event to process. [Full Topic]
142144
//
143145
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/cgdisplaystreamframeavailablehandler?language=objc
144-
type DisplayStreamFrameAvailableHandler = func(status DisplayStreamFrameStatus, displayTime uint64, frameSurface unsafe.Pointer, updateRef unsafe.Pointer)
146+
type DisplayStreamFrameAvailableHandler = func(status DisplayStreamFrameStatus, displayTime uint64, frameSurface iosurface.Ref, updateRef DisplayStreamUpdateRef)
145147

146148
// Performs custom operations on the supplied input data to produce output data. [Full Topic]
147149
//

macos/coregraphics/functions.gen.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ package coregraphics
55
// #import <stdlib.h>
66
// #import <stdint.h>
77
// #import <stdbool.h>
8-
// #import <CoreGraphics/CGGeometry.h>
9-
// void * CGDisplayCreateImage(uint32_t displayID);
8+
// #import "CoreGraphics/CoreGraphics.h"
9+
// uint32_t MainDisplayID();
10+
// void * DisplayCreateImage(uint32_t displayID);
1011
import "C"
11-
import (
12-
"unsafe"
13-
)
12+
13+
// Returns the display ID of the main display. [Full Topic]
14+
//
15+
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/1455620-cgmaindisplayid?language=objc
16+
func MainDisplayID() DirectDisplayID {
17+
rv := C.MainDisplayID()
18+
return DirectDisplayID(rv)
19+
}
1420

1521
// Returns an image containing the contents of the specified display. [Full Topic]
1622
//
1723
// [Full Topic]: https://developer.apple.com/documentation/coregraphics/1455691-cgdisplaycreateimage?language=objc
18-
func DisplayCreateImage(displayID DirectDisplayID) unsafe.Pointer {
19-
rv := C.CGDisplayCreateImage(C.uint32_t(displayID))
20-
return unsafe.Pointer(rv)
24+
func DisplayCreateImage(displayID DirectDisplayID) ImageRef {
25+
rv := C.DisplayCreateImage(C.uint32_t(displayID))
26+
return ImageRef(rv)
2127
}

0 commit comments

Comments
 (0)