Skip to content

Commit

Permalink
Feature/callback messages (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampio committed Aug 4, 2019
1 parent f7b8240 commit 5a56184
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 141 deletions.
2 changes: 1 addition & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (a *Application) Run() error {
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)

if a.config.windowInitialLocations.xpos != 0 {
// To create the window at a specific position, make it initially invisible
// using the Visible window hint, set its position and then show it.
Expand Down
50 changes: 46 additions & 4 deletions embedder/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package embedder
// #include "embedder.h"
// FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine, FlutterProjectArgs * Args,
// const char *const * vmArgs, int nVmAgrs);
// FlutterEngineResult
// createMessageResponseHandle(FlutterEngine engine, void *user_data,
// FlutterPlatformMessageResponseHandle **reply);
// char** makeCharArray(int size);
// void setArrayString(char **a, char *s, int n);
// const int32_t kFlutterSemanticsNodeIdBatchEnd = -1;
// const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1;
import "C"
import (
"errors"
"fmt"
"runtime"
"runtime/debug"
"sync"
"unsafe"
Expand Down Expand Up @@ -256,8 +261,11 @@ type PlatformMessage struct {
Channel string
Message []byte

// ResponseHandle is only set when receiving a platform message.
// https://github.com/flutter/flutter/issues/18852
// ResponseHandle is set on some recieved platform message. All
// PlatformMessage recieved with this attribute must send a response with
// `SendPlatformMessageResponse`.
// ResponseHandle can also be created from the embedder side when a
// platform(golang) message needs native callback.
ResponseHandle PlatformMessageResponseHandle
}

Expand Down Expand Up @@ -357,8 +365,42 @@ func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Res
return (Result)(res)
}

// FlutterEngineGetCurrentTime gets the current time in nanoseconds from the
// clock used by the flutter engine.
// DataCallback is a function called when a PlatformMessage response send back
// to the embedder.
type DataCallback func(binaryReply []byte)

// CreatePlatformMessageResponseHandle creates a platform message response
// handle that allows the embedder to set a native callback for a response to a
// message.
// Must be collected via `ReleasePlatformMessageResponseHandle` after the call
// to `SendPlatformMessage`.
func (flu *FlutterEngine) CreatePlatformMessageResponseHandle(callback DataCallback) (PlatformMessageResponseHandle, error) {
var responseHandle *C.FlutterPlatformMessageResponseHandle

callbackPointer := uintptr(unsafe.Pointer(&callback))
defer func() {
runtime.KeepAlive(callbackPointer)
}()

res := C.createMessageResponseHandle(flu.Engine, unsafe.Pointer(&callbackPointer), &responseHandle)
if (Result)(res) != ResultSuccess {
return 0, errors.New("failed to create a response handle")
}
return PlatformMessageResponseHandle(unsafe.Pointer(responseHandle)), nil
}

// ReleasePlatformMessageResponseHandle collects a platform message response
// handle.
func (flu *FlutterEngine) ReleasePlatformMessageResponseHandle(responseHandle PlatformMessageResponseHandle) {
cResponseHandle := (*C.FlutterPlatformMessageResponseHandle)(unsafe.Pointer(responseHandle))
res := C.FlutterPlatformMessageReleaseResponseHandle(flu.Engine, cResponseHandle)
if (Result)(res) != ResultSuccess {
fmt.Printf("go-flutter: failed to collect platform response message handle")
}
}

// FlutterEngineGetCurrentTime gets the current time in nanoseconds from the clock used by the flutter
// engine.
func FlutterEngineGetCurrentTime() uint64 {
return uint64(C.FlutterEngineGetCurrentTime())
}
Loading

0 comments on commit 5a56184

Please sign in to comment.