progrium / darwinkit

Native Mac APIs for Go. Previously known as MacDriver
MIT License
4.97k stars 158 forks source link

Addig CoreBluetooth #96

Open PumpkinSeed opened 2 years ago

PumpkinSeed commented 2 years ago

It would be nice to have the CoreBluetooth module in the repository. I wanted to add it, but I faced with several issues. If you can help me with these I can handle the rest and creating the PR. (Assume I'm not a Mac programmer)

  1. The core bluetooth delegate implementations are Protocols, I don't know what does it mean, but the macschema can't handle it. (I cloned the repo and I built my own version, so I'm running on latest.) How can I add implementation to the macschema to handle this?
  2. If you check this, I added the generated api descriptors and it's generating the corebluetooth just fine. (Obviously I left out the delegate ones, because those were empty descriptors). But I needed to leave out the Enum descriptors as well, because I got a nil dereference error, from this section. (I just skipped this if it's Enum just for curiosity, and I got an other one from somewhere else). So it seems it's only handling Classes.

Let me know your thoughts, if you can help me, I can invest sometime to add these features.

progrium commented 2 years ago

first, awesome! this'll be rad. we haven't added enums yet, but our workaround is to just find out their value and use it directly. let me know if you'd want to help get enums working properly.

and with protocols, particularly for delegates, since those are interfaces you'd be implementing, you can for now just create a class using objc package and as long as you implement the needed methods, it should work.

PumpkinSeed commented 2 years ago

I don't think that we need enums directly. So I don't think that we want to work on it right now.

On the other hand, we've worked on the Core Bluetooth addition, but we run into issues. These are issues where I want to add a delegate to the CentralManager and I just got a panic kind of issues, and it's really hard to figure out what's going on, because everything relies on a raw pointer. Also there are some issues in the generation as well, because there are missing references even after I created the wrapper structs.

How you usually debug errors? I can't show it to you at the moment, but if you can help me with that to figure out, I can create a quick PR and tell you how to replicate certain errors.

Motivation: We are working on a tool which needs an io.Reader/io.Writer for multiple OS architectures. I'm done with the Go community for now, so we try to solve it somehow... We have a fully working version on Linux. As you see we are working on the MacOS version and once it's done, we will dive into the Windows solution.

PumpkinSeed commented 2 years ago

I replicated the generation issue here. If I run this code, I get these kind of errors (even if I have the wrapper.go and the necessary types):

# github.com/progrium/macdriver/corebluetooth
../../corebluetooth/corebluetooth_objc.gen.go:24:10: error: use of undeclared identifier 'CBATTRequest'
        return [CBATTRequest
                ^
../../corebluetooth/corebluetooth_objc.gen.go:28:10: error: use of undeclared identifier 'CBAttribute'
        return [CBAttribute
                ^
../../corebluetooth/corebluetooth_objc.gen.go:32:10: error: use of undeclared identifier 'CBCentralManager'
        return [CBCentralManager
                ^
../../corebluetooth/corebluetooth_objc.gen.go:36:10: error: use of undeclared identifier 'CBCentral'
        return [CBCentral
                ^
../../corebluetooth/corebluetooth_objc.gen.go:40:10: error: use of undeclared identifier 'CBCharacteristic'
        return [CBCharacteristic
                ^
.......
progrium commented 2 years ago

You might just need to make sure the frameworks are included. See this line for example: https://github.com/PumpkinSeed/macdriver/blob/main/cocoa/cocoa_objc.gen.go#L11

PumpkinSeed commented 2 years ago

I tried in on that way as well. Here you can see the final result (which compiled). This is my hacking environment where I just got other errors. I commented them out, because even if I added the framework CoreBluetooth that didn't work. Once I reach my Macbook (because I developing the Windows-side bluetooth management as well, but my main OS is Linux :D) I will add it and sending a new version of it.

UPDATE Ok, I was dump, I just added those. The missing wrapper errors disappeared, but there are new errors (not new, but leftover). Sorry if I'm asking too many questions. I also have my colleague who will able to work on this implementation, he will join later today and check everything.

# github.com/progrium/macdriver/corebluetooth
corebluetooth/corebluetooth_objc.gen.go:97:3: error: 'init' is unavailable
                init];
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework/Headers/CBATTRequest.h:31:1: note: 'init' has been explicitly marked unavailable here
- (instancetype)init NS_UNAVAILABLE;
^
corebluetooth/corebluetooth_objc.gen.go:117:3: error: 'init' is unavailable
                init];
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework/Headers/CBAttribute.h:21:1: note: 'init' has been explicitly marked unavailable here
- (instancetype)init NS_UNAVAILABLE;
^
corebluetooth/corebluetooth_objc.gen.go:164:3: error: 'registerForConnectionEventsWithOptions:' is unavailable: not available on macOS
                registerForConnectionEventsWithOptions: options];
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreBluetooth.framework/Headers/CBCentralManager.h:239:1: note: 'registerForConnectionEventsWithOptions:' has been explicitly marked unavailable here
- (void)registerForConnectionEventsWithOptions:(nullable NSDictionary<CBConnectionEventMatchingOption, id> *)options CB_CM_API_AVAILABLE;
^
corebluetooth/corebluetooth_objc.gen.go:184:3: error: 'init' is unavailable
                init];
                ^