Skip to content

merge #7

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 3 commits into from
Jan 13, 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
16 changes: 10 additions & 6 deletions build/linux/config/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@

import("//third_party/chromium/build/config/linux/pkg_config.gni")

pkg_config("gtk3") {
packages = [ "gtk+-3.0" ]
}

pkg_config("epoxy") {
packages = [ "epoxy" ]
}

pkg_config("x11") {
packages = [ "x11" ]
pkg_config("glfw3") {
packages = [ "glfw3" ]
}

pkg_config("gtk3") {
packages = [ "gtk+-3.0" ]
}

pkg_config("jsoncpp") {
packages = [ "jsoncpp" ]
}

pkg_config("x11") {
packages = [ "x11" ]
}
5 changes: 2 additions & 3 deletions example/linux/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,13 @@ endif

# Build settings
CXX=g++ -std=c++14
CXXFLAGS=-Wall -Werror $(shell pkg-config --cflags jsoncpp)
CXXFLAGS=-Wall -Werror $(shell pkg-config --cflags jsoncpp glfw3)
CPPFLAGS=$(patsubst %,-I%,$(INCLUDE_DIRS))
ifdef USE_GN
CPPFLAGS+=-DUSE_FLATTENED_INCLUDES
endif
LDFLAGS=-L$(OUT_LIB_DIR) \
-lglfw \
$(shell pkg-config --libs jsoncpp) \
$(shell pkg-config --libs jsoncpp glfw3) \
-l$(FLUTTER_EMBEDDER_LIB_NAME) \
-l$(FLUTTER_ENGINE_LIB_NAME) \
$(patsubst %,-l$(PLUGIN_LIB_NAME_PREFIX)%,$(PLUGIN_NAMES)) \
Expand Down
13 changes: 5 additions & 8 deletions library/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ published_shared_library("flutter_embedder") {
if (is_linux || is_win) {
sources += [
"common/engine_method_result.cc",
"common/internal/json_message_codec.cc",
"common/internal/json_message_codec.h",
"common/internal/plugin_handler.cc",
"common/internal/plugin_handler.h",
"common/internal/text_input_model.cc",
"common/internal/text_input_model.h",
"common/json_message_codec.cc",
"common/json_method_codec.cc",
]
public += [
"include/flutter_desktop_embedding/basic_message_channel.h",
"include/flutter_desktop_embedding/binary_messenger.h",
"include/flutter_desktop_embedding/engine_method_result.h",
"include/flutter_desktop_embedding/fde_export.h",
"include/flutter_desktop_embedding/json_message_codec.h",
"include/flutter_desktop_embedding/json_method_codec.h",
"include/flutter_desktop_embedding/message_codec.h",
"include/flutter_desktop_embedding/method_call.h",
"include/flutter_desktop_embedding/method_channel.h",
"include/flutter_desktop_embedding/method_codec.h",
Expand All @@ -73,7 +75,6 @@ published_shared_library("flutter_embedder") {

if (is_linux) {
libs = [
"glfw",
"GL",
]

Expand All @@ -83,18 +84,14 @@ published_shared_library("flutter_embedder") {

configs += [
"//build/linux/config:epoxy",
"//build/linux/config:glfw3",
"//build/linux/config:gtk3",
"//build/linux/config:jsoncpp",
"//build/linux/config:x11",
]
}

if (is_win) {
dll_exports = rebase_path("windows/exports.def")
ldflags = [
"/DEF:$dll_exports",
]

deps += [
"//library/windows:publish_flutter_engine",
"//library/windows:fetch_glfw",
Expand Down
20 changes: 10 additions & 10 deletions library/common/glfw/key_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <json/json.h>
#include <iostream>

#include "library/common/internal/json_message_codec.h"
#include "library/include/flutter_desktop_embedding/json_message_codec.h"

static constexpr char kChannelName[] = "flutter/keyevent";

Expand All @@ -32,8 +32,9 @@ static constexpr char kRepeat[] = "repeat";

namespace flutter_desktop_embedding {

KeyEventHandler::KeyEventHandler(const BinaryMessenger *messenger)
: messenger_(messenger), channel_(kChannelName) {}
KeyEventHandler::KeyEventHandler(BinaryMessenger *messenger)
: channel_(std::make_unique<BasicMessageChannel<Json::Value>>(
messenger, kChannelName, &JsonMessageCodec::GetInstance())) {}

KeyEventHandler::~KeyEventHandler() {}

Expand All @@ -43,23 +44,22 @@ void KeyEventHandler::KeyboardHook(GLFWwindow *window, int key, int scancode,
int action, int mods) {
// TODO: Translate to a cross-platform key code system rather than passing
// the native key code.
Json::Value args;
args[kKeyCodeKey] = key;
args[kKeyMapKey] = kAndroidKeyMap;
Json::Value event;
event[kKeyCodeKey] = key;
event[kKeyMapKey] = kAndroidKeyMap;

switch (action) {
case GLFW_PRESS:
args[kTypeKey] = kKeyDown;
event[kTypeKey] = kKeyDown;
break;
case GLFW_RELEASE:
args[kTypeKey] = kKeyUp;
event[kTypeKey] = kKeyUp;
break;
default:
std::cerr << "Unknown key event action: " << action << std::endl;
return;
}
auto message = JsonMessageCodec::GetInstance().EncodeMessage(args);
messenger_->Send(channel_, message->data(), message->size());
channel_->Send(event);
}

} // namespace flutter_desktop_embedding
13 changes: 8 additions & 5 deletions library/common/glfw/key_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
#ifndef LIBRARY_COMMON_GLFW_KEY_EVENT_HANDLER_H_
#define LIBRARY_COMMON_GLFW_KEY_EVENT_HANDLER_H_

#include <memory>

#include <json/json.h>

#include "library/common/glfw/keyboard_hook_handler.h"
#include "library/include/flutter_desktop_embedding/basic_message_channel.h"
#include "library/include/flutter_desktop_embedding/binary_messenger.h"

namespace flutter_desktop_embedding {
Expand All @@ -24,7 +29,7 @@ namespace flutter_desktop_embedding {
// Handles key events and forwards them to the Flutter engine.
class KeyEventHandler : public KeyboardHookHandler {
public:
explicit KeyEventHandler(const BinaryMessenger *messenger);
explicit KeyEventHandler(BinaryMessenger *messenger);
virtual ~KeyEventHandler();

// KeyboardHookHandler.
Expand All @@ -33,10 +38,8 @@ class KeyEventHandler : public KeyboardHookHandler {
void CharHook(GLFWwindow *window, unsigned int code_point) override;

private:
// Binds this plugin to the given caller-owned binary messenger. It must
// remain valid for the life of the plugin.
const BinaryMessenger *messenger_;
std::string channel_;
// The Flutter system channel for key event messages.
std::unique_ptr<BasicMessageChannel<Json::Value>> channel_;
};

} // namespace flutter_desktop_embedding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "library/common/internal/json_message_codec.h"
#include "library/include/flutter_desktop_embedding/json_message_codec.h"

#include <iostream>
#include <string>
Expand All @@ -24,7 +24,7 @@ const JsonMessageCodec &JsonMessageCodec::GetInstance() {
return sInstance;
}

std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessage(
std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessageInternal(
const Json::Value &message) const {
Json::StreamWriterBuilder writer_builder;
std::string serialization = Json::writeString(writer_builder, message);
Expand All @@ -33,12 +33,12 @@ std::unique_ptr<std::vector<uint8_t>> JsonMessageCodec::EncodeMessage(
serialization.end());
}

std::unique_ptr<Json::Value> JsonMessageCodec::DecodeMessage(
const uint8_t *message, const size_t message_size) const {
std::unique_ptr<Json::Value> JsonMessageCodec::DecodeMessageInternal(
const uint8_t *binary_message, const size_t message_size) const {
Json::CharReaderBuilder reader_builder;
std::unique_ptr<Json::CharReader> parser(reader_builder.newCharReader());

auto raw_message = reinterpret_cast<const char *>(message);
auto raw_message = reinterpret_cast<const char *>(binary_message);
auto json_message = std::make_unique<Json::Value>();
std::string parse_errors;
bool parsing_successful =
Expand Down
2 changes: 1 addition & 1 deletion library/common/json_method_codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
#include "library/include/flutter_desktop_embedding/json_method_codec.h"

#include "library/common/internal/json_message_codec.h"
#include "library/include/flutter_desktop_embedding/json_message_codec.h"

namespace flutter_desktop_embedding {

Expand Down
106 changes: 106 additions & 0 deletions library/include/flutter_desktop_embedding/basic_message_channel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BASIC_MESSAGE_CHANNEL_H_
#define LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BASIC_MESSAGE_CHANNEL_H_

#include <iostream>
#include <string>

#include "binary_messenger.h"
#include "fde_export.h"
#include "message_codec.h"

namespace flutter_desktop_embedding {

// A message reply callback.
//
// Used for submitting a reply back to a Flutter message sender.
template <typename T>
using MessageReply = std::function<void(const T &reply)>;

// A handler for receiving a message from the Flutter engine.
//
// Implementations must asynchronously call reply exactly once with the reply
// to the message.
template <typename T>
using MessageHandler =
std::function<void(const T &message, MessageReply<T> reply)>;

// A channel for communicating with the Flutter engine by sending asynchronous
// messages.
template <typename T>
class FDE_EXPORT BasicMessageChannel {
public:
// Creates an instance that sends and receives method calls on the channel
// named |name|, encoded with |codec| and dispatched via |messenger|.
//
// TODO: Make codec optional once the standard codec is supported (Issue #67).
BasicMessageChannel(BinaryMessenger *messenger, const std::string &name,
const MessageCodec<T> *codec)
: messenger_(messenger), name_(name), codec_(codec) {}
~BasicMessageChannel() {}

// Prevent copying.
BasicMessageChannel(BasicMessageChannel const &) = delete;
BasicMessageChannel &operator=(BasicMessageChannel const &) = delete;

// Sends a message to the Flutter engine on this channel.
void Send(const T &message) {
std::unique_ptr<std::vector<uint8_t>> raw_message =
codec_->EncodeMessage(message);
messenger_->Send(name_, raw_message->data(), raw_message->size());
}

// TODO: Add support for a version of Send expecting a reply once
// https://github.com/flutter/flutter/issues/18852 is fixed.

// Registers a handler that should be called any time a message is
// received on this channel.
void SetMessageHandler(MessageHandler<T> handler) const {
const auto *codec = codec_;
std::string channel_name = name_;
BinaryMessageHandler binary_handler = [handler, codec, channel_name](
const uint8_t *binary_message,
const size_t binary_message_size,
BinaryReply binary_reply) {
// Use this channel's codec to decode the message and build a reply
// handler.
std::unique_ptr<T> message =
codec->DecodeMessage(binary_message, binary_message_size);
if (!message) {
std::cerr << "Unable to decode message on channel " << channel_name
<< std::endl;
binary_reply(nullptr, 0);
return;
}

MessageReply<T> unencoded_reply = [binary_reply,
codec](const T &unencoded_response) {
auto binary_response = codec->EncodeMessage(unencoded_response);
binary_reply(binary_response->data(), binary_response->size());
};
handler(*message, std::move(unencoded_reply));
};
messenger_->SetMessageHandler(name_, std::move(binary_handler));
}

private:
BinaryMessenger *messenger_;
std::string name_;
const MessageCodec<T> *codec_;
};

} // namespace flutter_desktop_embedding

#endif // LIBRARY_INCLUDE_FLUTTER_DESKTOP_EMBEDDING_BASIC_MESSAGE_CHANNEL_H_
4 changes: 2 additions & 2 deletions library/include/flutter_desktop_embedding/binary_messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
// the message/message_size pairs.
namespace flutter_desktop_embedding {

// A message reply callback.
// A binary message reply callback.
//
// Used for submitting a reply back to a Flutter message sender.
// Used for submitting a binary reply back to a Flutter message sender.
typedef std::function<void(const uint8_t *reply, const size_t reply_size)>
BinaryReply;

Expand Down
Loading