Skip to content

[linux] Adds raw key event handler #163

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 4 commits into from
Dec 6, 2018
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
9 changes: 9 additions & 0 deletions library/linux/src/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <flutter_embedder.h>

#include "library/linux/src/internal/key_event_handler.h"
#include "library/linux/src/internal/keyboard_hook_handler.h"
#include "library/linux/src/internal/plugin_handler.h"
#include "library/linux/src/internal/text_input_plugin.h"
Expand All @@ -50,6 +51,10 @@ struct FlutterEmbedderState {
// deleted from the heap.
std::vector<flutter_desktop_embedding::KeyboardHookHandler *>
keyboard_hook_handlers;
// Handles raw key interactions from GLFW.
// TODO: Move key_event_handler once
// https://github.com/google/flutter-desktop-embedding/issues/102 is resolved.
std::unique_ptr<flutter_desktop_embedding::KeyEventHandler> key_event_handler;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a quick comment explaining what this is, as you would with a class ivar.

};

static constexpr char kDefaultWindowTitle[] = "Flutter";
Expand Down Expand Up @@ -272,6 +277,10 @@ GLFWwindow *CreateFlutterWindow(size_t initial_width, size_t initial_height,
FlutterEmbedderState *state = new FlutterEmbedderState();
state->plugin_handler = std::make_unique<PluginHandler>(engine);
state->engine = engine;

state->key_event_handler =
std::make_unique<KeyEventHandler>(state->plugin_handler.get());
state->keyboard_hook_handlers.push_back(state->key_event_handler.get());
auto input_plugin = std::make_unique<TextInputPlugin>();
state->keyboard_hook_handlers.push_back(input_plugin.get());

Expand Down
65 changes: 65 additions & 0 deletions library/linux/src/internal/key_event_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.
#include "library/linux/src/internal/key_event_handler.h"

#include <json/json.h>
#include <iostream>

#include "library/linux/src/internal/json_message_codec.h"

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

static constexpr char kKeyCodeKey[] = "keyCode";
static constexpr char kKeyMapKey[] = "keymap";
static constexpr char kTypeKey[] = "type";

static constexpr char kAndroidKeyMap[] = "android";
static constexpr char kKeyUp[] = "keyup";
static constexpr char kKeyDown[] = "keydown";
// TODO: This event is not supported by Flutter. Add once it's implemented.
static constexpr char kRepeat[] = "repeat";

namespace flutter_desktop_embedding {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be consistent about horizontal spacing; if the namespace is going to have a blank line at the end, put one at the start as well. (Same for header.)


KeyEventHandler::KeyEventHandler(const BinaryMessenger *messenger)
: messenger_(messenger), channel_(kChannelName) {}

KeyEventHandler::~KeyEventHandler() {}

void KeyEventHandler::CharHook(GLFWwindow *window, unsigned int code_point) {}

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;

switch (action) {
case GLFW_PRESS:
args[kTypeKey] = kKeyDown;
break;
case GLFW_RELEASE:
args[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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't make messenger a constructor argument, you should check that it's not null before using it.

}

} // namespace flutter_desktop_embedding
44 changes: 44 additions & 0 deletions library/linux/src/internal/key_event_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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_LINUX_SRC_INTERNAL_KEY_EVENT_HANDLER_H_
#define LIBRARY_LINUX_SRC_INTERNAL_kEY_EVENT_HANDLER_H_

#include "library/linux/include/flutter_desktop_embedding/binary_messenger.h"
#include "library/linux/src/internal/keyboard_hook_handler.h"

namespace flutter_desktop_embedding {

// Implements a KeyboardHookHandler
//
// Handles key events and forwards them to the Flutter engine.
class KeyEventHandler : public KeyboardHookHandler {
public:
explicit KeyEventHandler(const BinaryMessenger *messenger);
virtual ~KeyEventHandler();

// KeyboardHookHandler.
void KeyboardHook(GLFWwindow *window, int key, int scancode, int action,
int mods) override;
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_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't make this constructor-initialized, you should initialize it to nullptr here.

std::string channel_;
};

} // namespace flutter_desktop_embedding

#endif // LIBRARY_LINUX_SRC_INTERNAL_KEY_EVENT_HANDLER_H_