Skip to content

Convert embedder.h to a C API #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions library/common/glfw/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ static FlutterEmbedderState *GetSavedEmbedderState(GLFWwindow *window) {
}

// Converts a FlutterPlatformMessage to an equivalent FlutterEmbedderMessage.
static flutter_desktop_embedding::FlutterEmbedderMessage
ConvertToEmbedderMessage(const FlutterPlatformMessage &engine_message) {
flutter_desktop_embedding::FlutterEmbedderMessage embedder_message = {};
static FlutterEmbedderMessage ConvertToEmbedderMessage(
const FlutterPlatformMessage &engine_message) {
FlutterEmbedderMessage embedder_message = {};
embedder_message.struct_size = sizeof(embedder_message);
embedder_message.channel = engine_message.channel;
embedder_message.message = engine_message.message;
Expand Down Expand Up @@ -284,17 +284,18 @@ static void GLFWErrorCallback(int error_code, const char *description) {
// the necessary callbacks for rendering within a GLFWwindow.
//
// Returns a caller-owned pointer to the engine.
static FlutterEngine RunFlutterEngine(
GLFWwindow *window, const std::string &assets_path,
const std::string &icu_data_path,
const std::vector<std::string> &arguments) {
static FlutterEngine RunFlutterEngine(GLFWwindow *window,
const char *assets_path,
const char *icu_data_path,
const char **arguments,
size_t arguments_count) {
// FlutterProjectArgs is expecting a full argv, so when processing it for
// flags the first item is treated as the executable and ignored. Add a dummy
// value so that all provided arguments are used.
std::vector<const char *> argv = {"placeholder"};
std::transform(
arguments.begin(), arguments.end(), std::back_inserter(argv),
[](const std::string &arg) -> const char * { return arg.c_str(); });
if (arguments_count > 0) {
argv.insert(argv.end(), &arguments[0], &arguments[arguments_count]);
}

FlutterRendererConfig config = {};
config.type = kOpenGL;
Expand All @@ -306,8 +307,8 @@ static FlutterEngine RunFlutterEngine(
config.open_gl.gl_proc_resolver = GLFWProcResolver;
FlutterProjectArgs args = {};
args.struct_size = sizeof(FlutterProjectArgs);
args.assets_path = assets_path.c_str();
args.icu_data_path = icu_data_path.c_str();
args.assets_path = assets_path;
args.icu_data_path = icu_data_path;
args.command_line_argc = argv.size();
args.command_line_argv = &argv[0];
args.platform_message_callback = GLFWOnFlutterPlatformMessage;
Expand All @@ -322,20 +323,17 @@ static FlutterEngine RunFlutterEngine(
return engine;
}

namespace flutter_desktop_embedding {

bool FlutterInit() {
bool FlutterEmbedderInit() {
// Before making any GLFW calls, set up a logging error handler.
glfwSetErrorCallback(GLFWErrorCallback);
return glfwInit();
}

void FlutterTerminate() { glfwTerminate(); }
void FlutterEmbedderTerminate() { glfwTerminate(); }

FlutterWindowRef CreateFlutterWindow(
size_t initial_width, size_t initial_height, const std::string &assets_path,
const std::string &icu_data_path,
const std::vector<std::string> &arguments) {
FlutterWindowRef FlutterEmbedderCreateWindow(
size_t initial_width, size_t initial_height, const char *assets_path,
const char *icu_data_path, const char **arguments, size_t argument_count) {
#ifdef __linux__
gtk_init(0, nullptr);
#endif
Expand All @@ -348,7 +346,8 @@ FlutterWindowRef CreateFlutterWindow(
GLFWClearCanvas(window);

// Start the engine.
auto engine = RunFlutterEngine(window, assets_path, icu_data_path, arguments);
auto engine = RunFlutterEngine(window, assets_path, icu_data_path, arguments,
argument_count);
if (engine == nullptr) {
glfwDestroyWindow(window);
return nullptr;
Expand All @@ -360,14 +359,18 @@ FlutterWindowRef CreateFlutterWindow(
glfwSetWindowUserPointer(window, state);
state->engine = engine;
state->message_dispatcher =
std::make_unique<IncomingMessageDispatcher>(state);
state->plugin_handler = std::make_unique<PluginHandler>(state);
std::make_unique<flutter_desktop_embedding::IncomingMessageDispatcher>(
state);
state->plugin_handler =
std::make_unique<flutter_desktop_embedding::PluginHandler>(state);

// Set up the keyboard handlers.
state->keyboard_hook_handlers.push_back(
std::make_unique<KeyEventHandler>(state->plugin_handler.get()));
std::make_unique<flutter_desktop_embedding::KeyEventHandler>(
state->plugin_handler.get()));
state->keyboard_hook_handlers.push_back(
std::make_unique<TextInputPlugin>(state->plugin_handler.get()));
std::make_unique<flutter_desktop_embedding::TextInputPlugin>(
state->plugin_handler.get()));

// Trigger an initial size callback to send size information to Flutter.
state->monitor_screen_coordinates_per_inch = GetScreenCoordinatesPerInch();
Expand All @@ -382,7 +385,7 @@ FlutterWindowRef CreateFlutterWindow(
return state;
}

void FlutterWindowLoop(FlutterWindowRef flutter_window) {
void FlutterEmbedderRunWindowLoop(FlutterWindowRef flutter_window) {
GLFWwindow *window = flutter_window->window;
#ifdef __linux__
// Necessary for GTK thread safety.
Expand Down Expand Up @@ -438,5 +441,3 @@ void FlutterEmbedderEnableInputBlocking(FlutterWindowRef flutter_window,
const char *channel) {
flutter_window->message_dispatcher->EnableInputBlockingForChannel(channel);
}

} // namespace flutter_desktop_embedding
20 changes: 14 additions & 6 deletions library/common/glfw/flutter_window_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "library/include/flutter_desktop_embedding/glfw/flutter_window_controller.h"

#include <algorithm>
#include <iostream>

#include "library/common/internal/plugin_handler.h"
Expand All @@ -22,20 +23,21 @@ namespace flutter_desktop_embedding {

FlutterWindowController::FlutterWindowController(std::string &icu_data_path)
: icu_data_path_(icu_data_path) {
init_succeeded_ = FlutterInit();
init_succeeded_ = FlutterEmbedderInit();
}

FlutterWindowController::~FlutterWindowController() {
if (init_succeeded_) {
FlutterTerminate();
FlutterEmbedderTerminate();
}
}

bool FlutterWindowController::CreateWindow(
size_t width, size_t height, const std::string &assets_path,
const std::vector<std::string> &arguments) {
if (!init_succeeded_) {
std::cerr << "Could not create window; FlutterInit failed." << std::endl;
std::cerr << "Could not create window; FlutterEmbedderInit failed."
<< std::endl;
return false;
}

Expand All @@ -44,8 +46,14 @@ bool FlutterWindowController::CreateWindow(
return false;
}

window_ = CreateFlutterWindow(width, height, assets_path, icu_data_path_,
arguments);
std::vector<const char *> engine_arguments;
std::transform(
arguments.begin(), arguments.end(), std::back_inserter(engine_arguments),
[](const std::string &arg) -> const char * { return arg.c_str(); });

window_ = FlutterEmbedderCreateWindow(
width, height, assets_path.c_str(), icu_data_path_.c_str(),
&engine_arguments[0], engine_arguments.size());
if (!window_) {
std::cerr << "Failed to create window." << std::endl;
return false;
Expand All @@ -69,7 +77,7 @@ PluginRegistrar *FlutterWindowController::GetRegistrarForPlugin(

void FlutterWindowController::RunEventLoop() {
if (window_) {
FlutterWindowLoop(window_);
FlutterEmbedderRunWindowLoop(window_);
}
}

Expand Down
36 changes: 19 additions & 17 deletions library/include/flutter_desktop_embedding/glfw/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
#ifndef LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_GLFW_EMBEDDER_H_
#define LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_GLFW_EMBEDDER_H_

#include <memory>
#include <string>
#include <vector>
#include <stddef.h>
#include <stdint.h>

// On Linux, the header output is always flattened; on Windows the GN build
// is still optional. Once GN is required on Windows, eliminate this and just
Expand All @@ -34,29 +33,31 @@
#include "../fde_export.h"
#endif

#if defined(__cplusplus)
extern "C" {
#endif

// Opaque reference to a Flutter window.
typedef struct FlutterEmbedderState *FlutterWindowRef;

// Opaque handle for tracking responses to messages.
typedef struct _FlutterPlatformMessageResponseHandle
FlutterEmbedderMessageResponseHandle;

namespace flutter_desktop_embedding {

// Sets up the embedder's graphic context. Must be called before any other
// methods.
//
// Note: Internally, this library uses GLFW, which does not support multiple
// copies within the same process. Internally this calls glfwInit, which will
// fail if you have called glfwInit elsewhere in the process.
FDE_EXPORT bool FlutterInit();
FDE_EXPORT bool FlutterEmbedderInit();

// Tears down embedder state. Must be called before the process terminates.
FDE_EXPORT void FlutterTerminate();
FDE_EXPORT void FlutterEmbedderTerminate();

// Creates a Window running a Flutter Application.
//
// FlutterInit() must be called prior to this function.
// FlutterEmbedderInit() must be called prior to this function.
//
// The |assets_path| is the path to the flutter_assets folder for the Flutter
// application to be run. |icu_data_path| is the path to the icudtl.dat file
Expand All @@ -67,19 +68,18 @@ FDE_EXPORT void FlutterTerminate();
// for details. Not all arguments will apply to embedding mode.
//
// Returns a null pointer in the event of an error. Otherwise, the pointer is
// valid until FlutterWindowLoop has been called and returned. Note that calling
// CreateFlutterWindow without later calling FlutterWindowLoop on that pointer
// is a memory leak.
FDE_EXPORT FlutterWindowRef CreateFlutterWindow(
size_t initial_width, size_t initial_height, const std::string &assets_path,
const std::string &icu_data_path,
const std::vector<std::string> &arguments);
// valid until FlutterEmbedderRunWindowLoop has been called and returned.
// Note that calling FlutterEmbedderCreateWindow without later calling
// FlutterEmbedderRunWindowLoop on the returned reference is a memory leak.
FDE_EXPORT FlutterWindowRef FlutterEmbedderCreateWindow(
size_t initial_width, size_t initial_height, const char *assets_path,
const char *icu_data_path, const char **arguments, size_t argument_count);

// Loops on Flutter window events until the window is closed.
//
// Once this function returns, FlutterWindowRef is no longer valid, and must
// not be used again.
FDE_EXPORT void FlutterWindowLoop(FlutterWindowRef flutter_window);
FDE_EXPORT void FlutterEmbedderRunWindowLoop(FlutterWindowRef flutter_window);

// A received from Flutter.
typedef struct {
Expand Down Expand Up @@ -141,6 +141,8 @@ FDE_EXPORT void FlutterEmbedderSetMessageCallback(
FDE_EXPORT void FlutterEmbedderEnableInputBlocking(
FlutterWindowRef flutter_window, const char *channel);

} // namespace flutter_desktop_embedding
#if defined(__cplusplus)
} // extern "C"
#endif

#endif // LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_GLFW_EMBEDDER_H_
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FDE_EXPORT FlutterWindowController {
// for any window created.
std::string icu_data_path_;

// Whether or not FlutterInit succeeded at creation time.
// Whether or not FlutterEmbedderInit succeeded at creation time.
bool init_succeeded_ = false;

// The curent Flutter window, if any.
Expand Down